PageRenderTime 135ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 2ms

/vendors/tcpdf/tcpdf.php

https://bitbucket.org/svbailey/agora-advertising.com-fork
PHP | 13727 lines | 8723 code | 526 blank | 4478 comment | 1998 complexity | 5ca4be6136a24d890df772ef2bb56086 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Begin : 2002-08-03
  5. // Last Update : 2009-08-30
  6. // Author : Nicola Asuni - info@tecnick.com - http://www.tcpdf.org
  7. // Version : 4.7.001
  8. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  9. // ----------------------------------------------------------------------------
  10. // Copyright (C) 2002-2009 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU Lesser General Public License as published by
  14. // the Free Software Foundation, either version 2.1 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU Lesser General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. // See LICENSE.TXT file for more information.
  26. // ----------------------------------------------------------------------------
  27. //
  28. // Description : This is a PHP class for generating PDF documents without
  29. // requiring external extensions.
  30. //
  31. // NOTE:
  32. // This class was originally derived in 2002 from the Public
  33. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  34. // but now is almost entirely rewritten.
  35. //
  36. // Main features:
  37. // * no external libraries are required for the basic functions;
  38. // * supports all ISO page formats;
  39. // * supports custom page formats, margins and units of measure;
  40. // * supports UTF-8 Unicode and Right-To-Left languages;
  41. // * supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  42. // * supports document encryption;
  43. // * includes methods to publish some XHTML code;
  44. // * includes graphic (geometric) and transformation methods;
  45. // * includes Javascript and forms support;
  46. // * includes a method to print various barcode formats: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS;
  47. // * includes methods to set Bookmarks and print a Table of Content;
  48. // * includes methods to move and delete pages;
  49. // * includes methods for automatic page header and footer management;
  50. // * supports automatic page break;
  51. // * supports automatic page numbering and page groups;
  52. // * supports automatic line break and text justification;
  53. // * supports JPEG and PNG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)
  54. // * supports stroke and clipping mode for text;
  55. // * supports clipping masks;
  56. // * supports Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  57. // * supports several annotations, including links, text and file attachments;
  58. // * supports page compression (requires zlib extension);
  59. // * supports text hyphenation.
  60. // * supports transactions to UNDO commands.
  61. // * supports signature certifications.
  62. //
  63. // -----------------------------------------------------------
  64. // THANKS TO:
  65. //
  66. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  67. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  68. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  69. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  70. // dullus for text Justification.
  71. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  72. // Patrick Benny for text stretch suggestion on Cell().
  73. // Johannes Güntert for JavaScript support.
  74. // Denis Van Nuffelen for Dynamic Form.
  75. // Jacek Czekaj for multibyte justification
  76. // Anthony Ferrara for the reintroduction of legacy image methods.
  77. // Sourceforge user 1707880 (hucste) for line-trough mode.
  78. // Larry Stanbery for page groups.
  79. // Martin Hall-May for transparency.
  80. // Aaron C. Spike for Polycurve method.
  81. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  82. // Moritz Wagner and Andreas Wurmser for graphic functions.
  83. // Andrew Whitehead for core fonts support.
  84. // Esteban Joël Marín for OpenType font conversion.
  85. // Teus Hagen for several suggestions and fixes.
  86. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  87. // Kosmas Papachristos for some CSS improvements.
  88. // Marcel Partap for some fixes.
  89. // Won Kyu Park for several suggestions, fixes and patches.
  90. // Anyone that has reported a bug or sent a suggestion.
  91. //============================================================+
  92. /**
  93. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  94. * TCPDF project (http://www.tcpdf.org) was originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  95. * <h3>TCPDF main features are:</h3>
  96. * <ul>
  97. * <li>no external libraries are required for the basic functions;</li>
  98. * <li>supports all ISO page formats;</li>
  99. * <li>supports custom page formats, margins and units of measure;</li>
  100. * <li>supports UTF-8 Unicode and Right-To-Left languages;</li>
  101. * <li>supports TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;</li>
  102. * <li>supports document encryption;</li>
  103. * <li>includes methods to publish some XHTML code;</li>
  104. * <li>includes graphic (geometric) and transformation methods;</li>
  105. * <li>includes Javascript and forms support;</li>
  106. * <li>includes a method to print various barcode formats: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 and 5 Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index - Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS;</li>
  107. * <li>includes methods to set Bookmarks and print a Table of Content;</li>
  108. * <li>includes methods to move and delete pages;</li>
  109. * <li>includes methods for automatic page header and footer management;</li>
  110. * <li>supports automatic page break;</li>
  111. * <li>supports automatic page numbering and page groups;</li>
  112. * <li>supports automatic line break and text justification;</li>
  113. * <li>supports JPEG and PNG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and all images supported via ImagMagick (http://www.imagemagick.org/www/formats.html)</li>
  114. * <li>supports stroke and clipping mode for text;</li>
  115. * <li>supports clipping masks;</li>
  116. * <li>supports Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  117. * <li>supports several annotations, including links, text and file attachments;</li>
  118. * <li>supports page compression (requires zlib extension);</li>
  119. * <li>supports text hyphenation.</li>
  120. * <li>supports transactions to UNDO commands.</li>
  121. * <li>supports signature certifications.</li>
  122. * </ul>
  123. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  124. * @package com.tecnick.tcpdf
  125. * @abstract Class for generating PDF files on-the-fly without requiring external extensions.
  126. * @author Nicola Asuni
  127. * @copyright 2002-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
  128. * @link http://www.tcpdf.org
  129. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  130. * @version 4.7.001
  131. */
  132. /**
  133. * main configuration file
  134. */
  135. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  136. // includes some support files
  137. /**
  138. * unicode data
  139. */
  140. require_once(dirname(__FILE__).'/unicode_data.php');
  141. /**
  142. * html colors table
  143. */
  144. require_once(dirname(__FILE__).'/htmlcolors.php');
  145. if (!class_exists('TCPDF', false)) {
  146. /**
  147. * define default PDF document producer
  148. */
  149. define('PDF_PRODUCER', 'TCPDF 4.7.001 (http://www.tcpdf.org)');
  150. /**
  151. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  152. * TCPDF project (http://www.tcpdf.org) has been originally derived in 2002 from the Public Domain FPDF class by Olivier Plathey (http://www.fpdf.org), but now is almost entirely rewritten.<br>
  153. * @name TCPDF
  154. * @package com.tecnick.tcpdf
  155. * @version 4.7.001
  156. * @author Nicola Asuni - info@tecnick.com
  157. * @link http://www.tcpdf.org
  158. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  159. */
  160. class TCPDF {
  161. // protected or Protected properties
  162. /**
  163. * @var current page number
  164. * @access protected
  165. */
  166. protected $page;
  167. /**
  168. * @var current object number
  169. * @access protected
  170. */
  171. protected $n;
  172. /**
  173. * @var array of object offsets
  174. * @access protected
  175. */
  176. protected $offsets;
  177. /**
  178. * @var buffer holding in-memory PDF
  179. * @access protected
  180. */
  181. protected $buffer;
  182. /**
  183. * @var array containing pages
  184. * @access protected
  185. */
  186. protected $pages = array();
  187. /**
  188. * @var current document state
  189. * @access protected
  190. */
  191. protected $state;
  192. /**
  193. * @var compression flag
  194. * @access protected
  195. */
  196. protected $compress;
  197. /**
  198. * @var current page orientation (P = Portrait, L = Landscape)
  199. * @access protected
  200. */
  201. protected $CurOrientation;
  202. /**
  203. * @var array that stores page dimensions and graphic status.<ul><li>$this->pagedim[$this->page]['w'] => page_width_in_points</li><li>$this->pagedim[$this->page]['h'] => height in points</li><li>$this->pagedim[$this->page]['wk'] => page_width_in_points</li><li>$this->pagedim[$this->page]['hk'] => height</li><li>$this->pagedim[$this->page]['tm'] => top_margin</li><li>$this->pagedim[$this->page]['bm'] => bottom_margin</li><li>$this->pagedim[$this->page]['lm'] => left_margin</li><li>$this->pagedim[$this->page]['rm'] => right_margin</li><li>$this->pagedim[$this->page]['pb'] => auto_page_break</li><li>$this->pagedim[$this->page]['or'] => page_orientation</li><li>$this->pagedim[$this->page]['olm'] => original_left_margin</li><li>$this->pagedim[$this->page]['orm'] => original_right_margin</li></ul>
  204. * @access protected
  205. */
  206. protected $pagedim = array();
  207. /**
  208. * @var scale factor (number of points in user unit)
  209. * @access protected
  210. */
  211. protected $k;
  212. /**
  213. * @var width of page format in points
  214. * @access protected
  215. */
  216. protected $fwPt;
  217. /**
  218. * @var height of page format in points
  219. * @access protected
  220. */
  221. protected $fhPt;
  222. /**
  223. * @var current width of page in points
  224. * @access protected
  225. */
  226. protected $wPt;
  227. /**
  228. * @var current height of page in points
  229. * @access protected
  230. */
  231. protected $hPt;
  232. /**
  233. * @var current width of page in user unit
  234. * @access protected
  235. */
  236. protected $w;
  237. /**
  238. * @var current height of page in user unit
  239. * @access protected
  240. */
  241. protected $h;
  242. /**
  243. * @var left margin
  244. * @access protected
  245. */
  246. protected $lMargin;
  247. /**
  248. * @var top margin
  249. * @access protected
  250. */
  251. protected $tMargin;
  252. /**
  253. * @var right margin
  254. * @access protected
  255. */
  256. protected $rMargin;
  257. /**
  258. * @var page break margin
  259. * @access protected
  260. */
  261. protected $bMargin;
  262. /**
  263. * @var cell internal padding
  264. * @access protected
  265. */
  266. //protected
  267. public $cMargin;
  268. /**
  269. * @var cell internal padding (previous value)
  270. * @access protected
  271. */
  272. protected $oldcMargin;
  273. /**
  274. * @var current horizontal position in user unit for cell positioning
  275. * @access protected
  276. */
  277. protected $x;
  278. /**
  279. * @var current vertical position in user unit for cell positioning
  280. * @access protected
  281. */
  282. protected $y;
  283. /**
  284. * @var height of last cell printed
  285. * @access protected
  286. */
  287. protected $lasth;
  288. /**
  289. * @var line width in user unit
  290. * @access protected
  291. */
  292. protected $LineWidth;
  293. /**
  294. * @var array of standard font names
  295. * @access protected
  296. */
  297. protected $CoreFonts;
  298. /**
  299. * @var array of used fonts
  300. * @access protected
  301. */
  302. protected $fonts = array();
  303. /**
  304. * @var array of font files
  305. * @access protected
  306. */
  307. protected $FontFiles = array();
  308. /**
  309. * @var array of encoding differences
  310. * @access protected
  311. */
  312. protected $diffs = array();
  313. /**
  314. * @var array of used images
  315. * @access protected
  316. */
  317. protected $images = array();
  318. /**
  319. * @var array of Annotations in pages
  320. * @access protected
  321. */
  322. protected $PageAnnots = array();
  323. /**
  324. * @var array of internal links
  325. * @access protected
  326. */
  327. protected $links = array();
  328. /**
  329. * @var current font family
  330. * @access protected
  331. */
  332. protected $FontFamily;
  333. /**
  334. * @var current font style
  335. * @access protected
  336. */
  337. protected $FontStyle;
  338. /**
  339. * @var current font ascent (distance between font top and baseline)
  340. * @access protected
  341. * @since 2.8.000 (2007-03-29)
  342. */
  343. protected $FontAscent;
  344. /**
  345. * @var current font descent (distance between font bottom and baseline)
  346. * @access protected
  347. * @since 2.8.000 (2007-03-29)
  348. */
  349. protected $FontDescent;
  350. /**
  351. * @var underlining flag
  352. * @access protected
  353. */
  354. protected $underline;
  355. /**
  356. * @var current font info
  357. * @access protected
  358. */
  359. protected $CurrentFont;
  360. /**
  361. * @var current font size in points
  362. * @access protected
  363. */
  364. protected $FontSizePt;
  365. /**
  366. * @var current font size in user unit
  367. * @access protected
  368. */
  369. protected $FontSize;
  370. /**
  371. * @var commands for drawing color
  372. * @access protected
  373. */
  374. protected $DrawColor;
  375. /**
  376. * @var commands for filling color
  377. * @access protected
  378. */
  379. protected $FillColor;
  380. /**
  381. * @var commands for text color
  382. * @access protected
  383. */
  384. protected $TextColor;
  385. /**
  386. * @var indicates whether fill and text colors are different
  387. * @access protected
  388. */
  389. protected $ColorFlag;
  390. /**
  391. * @var automatic page breaking
  392. * @access protected
  393. */
  394. protected $AutoPageBreak;
  395. /**
  396. * @var threshold used to trigger page breaks
  397. * @access protected
  398. */
  399. protected $PageBreakTrigger;
  400. /**
  401. * @var flag set when processing footer
  402. * @access protected
  403. */
  404. protected $InFooter = false;
  405. /**
  406. * @var zoom display mode
  407. * @access protected
  408. */
  409. protected $ZoomMode;
  410. /**
  411. * @var layout display mode
  412. * @access protected
  413. */
  414. protected $LayoutMode;
  415. /**
  416. * @var title
  417. * @access protected
  418. */
  419. protected $title = '';
  420. /**
  421. * @var subject
  422. * @access protected
  423. */
  424. protected $subject = '';
  425. /**
  426. * @var author
  427. * @access protected
  428. */
  429. protected $author = '';
  430. /**
  431. * @var keywords
  432. * @access protected
  433. */
  434. protected $keywords = '';
  435. /**
  436. * @var creator
  437. * @access protected
  438. */
  439. protected $creator = '';
  440. /**
  441. * @var alias for total number of pages
  442. * @access protected
  443. */
  444. protected $AliasNbPages = '{nb}';
  445. /**
  446. * @var alias for page number
  447. * @access protected
  448. */
  449. protected $AliasNumPage = '{pnb}';
  450. /**
  451. * @var right-bottom corner X coordinate of inserted image
  452. * @since 2002-07-31
  453. * @author Nicola Asuni
  454. * @access protected
  455. */
  456. protected $img_rb_x;
  457. /**
  458. * @var right-bottom corner Y coordinate of inserted image
  459. * @since 2002-07-31
  460. * @author Nicola Asuni
  461. * @access protected
  462. */
  463. protected $img_rb_y;
  464. /**
  465. * @var adjusting factor to convert pixels to user units.
  466. * @since 2004-06-14
  467. * @author Nicola Asuni
  468. * @access protected
  469. */
  470. protected $imgscale = 1;
  471. /**
  472. * @var boolean set to true when the input text is unicode (require unicode fonts)
  473. * @since 2005-01-02
  474. * @author Nicola Asuni
  475. * @access protected
  476. */
  477. protected $isunicode = false;
  478. /**
  479. * @var PDF version
  480. * @since 1.5.3
  481. * @access protected
  482. */
  483. protected $PDFVersion = '1.7';
  484. // ----------------------
  485. /**
  486. * @var Minimum distance between header and top page margin.
  487. * @access protected
  488. */
  489. protected $header_margin;
  490. /**
  491. * @var Minimum distance between footer and bottom page margin.
  492. * @access protected
  493. */
  494. protected $footer_margin;
  495. /**
  496. * @var original left margin value
  497. * @access protected
  498. * @since 1.53.0.TC013
  499. */
  500. protected $original_lMargin;
  501. /**
  502. * @var original right margin value
  503. * @access protected
  504. * @since 1.53.0.TC013
  505. */
  506. protected $original_rMargin;
  507. /**
  508. * @var Header font.
  509. * @access protected
  510. */
  511. protected $header_font;
  512. /**
  513. * @var Footer font.
  514. * @access protected
  515. */
  516. protected $footer_font;
  517. /**
  518. * @var Language templates.
  519. * @access protected
  520. */
  521. protected $l;
  522. /**
  523. * @var Barcode to print on page footer (only if set).
  524. * @access protected
  525. */
  526. protected $barcode = false;
  527. /**
  528. * @var If true prints header
  529. * @access protected
  530. */
  531. protected $print_header = true;
  532. /**
  533. * @var If true prints footer.
  534. * @access protected
  535. */
  536. protected $print_footer = true;
  537. /**
  538. * @var Header image logo.
  539. * @access protected
  540. */
  541. protected $header_logo = '';
  542. /**
  543. * @var Header image logo width in mm.
  544. * @access protected
  545. */
  546. protected $header_logo_width = 30;
  547. /**
  548. * @var String to print as title on document header.
  549. * @access protected
  550. */
  551. protected $header_title = '';
  552. /**
  553. * @var String to print on document header.
  554. * @access protected
  555. */
  556. protected $header_string = '';
  557. /**
  558. * @var Default number of columns for html table.
  559. * @access protected
  560. */
  561. protected $default_table_columns = 4;
  562. // variables for html parser
  563. /**
  564. * @var HTML PARSER: array to store current link and rendering styles.
  565. * @access protected
  566. */
  567. protected $HREF = array();
  568. /**
  569. * @var store a list of available fonts on filesystem.
  570. * @access protected
  571. */
  572. protected $fontlist = array();
  573. /**
  574. * @var current foreground color
  575. * @access protected
  576. */
  577. protected $fgcolor;
  578. /**
  579. * @var HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  580. * @access protected
  581. */
  582. protected $listordered = array();
  583. /**
  584. * @var HTML PARSER: array count list items on nested lists.
  585. * @access protected
  586. */
  587. protected $listcount = array();
  588. /**
  589. * @var HTML PARSER: current list nesting level.
  590. * @access protected
  591. */
  592. protected $listnum = 0;
  593. /**
  594. * @var HTML PARSER: indent amount for lists.
  595. * @access protected
  596. */
  597. protected $listindent;
  598. /**
  599. * @var current background color
  600. * @access protected
  601. */
  602. protected $bgcolor;
  603. /**
  604. * @var Store temporary font size in points.
  605. * @access protected
  606. */
  607. protected $tempfontsize = 10;
  608. /**
  609. * @var spacer for LI tags.
  610. * @access protected
  611. */
  612. protected $lispacer = '';
  613. /**
  614. * @var default encoding
  615. * @access protected
  616. * @since 1.53.0.TC010
  617. */
  618. protected $encoding = 'UTF-8';
  619. /**
  620. * @var PHP internal encoding
  621. * @access protected
  622. * @since 1.53.0.TC016
  623. */
  624. protected $internal_encoding;
  625. /**
  626. * @var indicates if the document language is Right-To-Left
  627. * @access protected
  628. * @since 2.0.000
  629. */
  630. protected $rtl = false;
  631. /**
  632. * @var used to force RTL or LTR string inversion
  633. * @access protected
  634. * @since 2.0.000
  635. */
  636. protected $tmprtl = false;
  637. // --- Variables used for document encryption:
  638. /**
  639. * Indicates whether document is protected
  640. * @access protected
  641. * @since 2.0.000 (2008-01-02)
  642. */
  643. protected $encrypted;
  644. /**
  645. * U entry in pdf document
  646. * @access protected
  647. * @since 2.0.000 (2008-01-02)
  648. */
  649. protected $Uvalue;
  650. /**
  651. * O entry in pdf document
  652. * @access protected
  653. * @since 2.0.000 (2008-01-02)
  654. */
  655. protected $Ovalue;
  656. /**
  657. * P entry in pdf document
  658. * @access protected
  659. * @since 2.0.000 (2008-01-02)
  660. */
  661. protected $Pvalue;
  662. /**
  663. * encryption object id
  664. * @access protected
  665. * @since 2.0.000 (2008-01-02)
  666. */
  667. protected $enc_obj_id;
  668. /**
  669. * last RC4 key encrypted (cached for optimisation)
  670. * @access protected
  671. * @since 2.0.000 (2008-01-02)
  672. */
  673. protected $last_rc4_key;
  674. /**
  675. * last RC4 computed key
  676. * @access protected
  677. * @since 2.0.000 (2008-01-02)
  678. */
  679. protected $last_rc4_key_c;
  680. /**
  681. * RC4 padding
  682. * @access protected
  683. */
  684. protected $padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  685. /**
  686. * RC4 encryption key
  687. * @access protected
  688. */
  689. protected $encryption_key;
  690. // --- bookmark ---
  691. /**
  692. * Outlines for bookmark
  693. * @access protected
  694. * @since 2.1.002 (2008-02-12)
  695. */
  696. protected $outlines = array();
  697. /**
  698. * Outline root for bookmark
  699. * @access protected
  700. * @since 2.1.002 (2008-02-12)
  701. */
  702. protected $OutlineRoot;
  703. // --- javascript and form ---
  704. /**
  705. * javascript code
  706. * @access protected
  707. * @since 2.1.002 (2008-02-12)
  708. */
  709. protected $javascript = '';
  710. /**
  711. * javascript counter
  712. * @access protected
  713. * @since 2.1.002 (2008-02-12)
  714. */
  715. protected $n_js;
  716. /**
  717. * line trough state
  718. * @access protected
  719. * @since 2.8.000 (2008-03-19)
  720. */
  721. protected $linethrough;
  722. // --- Variables used for User's Rights ---
  723. // See PDF reference chapter 8.7 Digital Signatures
  724. /**
  725. * If true enables user's rights on PDF reader
  726. * @access protected
  727. * @since 2.9.000 (2008-03-26)
  728. */
  729. protected $ur;
  730. /**
  731. * Names specifying additional document-wide usage rights for the document.
  732. * @access protected
  733. * @since 2.9.000 (2008-03-26)
  734. */
  735. protected $ur_document;
  736. /**
  737. * Names specifying additional annotation-related usage rights for the document.
  738. * @access protected
  739. * @since 2.9.000 (2008-03-26)
  740. */
  741. protected $ur_annots;
  742. /**
  743. * Names specifying additional form-field-related usage rights for the document.
  744. * @access protected
  745. * @since 2.9.000 (2008-03-26)
  746. */
  747. protected $ur_form;
  748. /**
  749. * Names specifying additional signature-related usage rights for the document.
  750. * @access protected
  751. * @since 2.9.000 (2008-03-26)
  752. */
  753. protected $ur_signature;
  754. /**
  755. * Dot Per Inch Document Resolution (do not change)
  756. * @access protected
  757. * @since 3.0.000 (2008-03-27)
  758. */
  759. protected $dpi = 72;
  760. /**
  761. * Array of page numbers were a new page group was started
  762. * @access protected
  763. * @since 3.0.000 (2008-03-27)
  764. */
  765. protected $newpagegroup = array();
  766. /**
  767. * Contains the number of pages of the groups
  768. * @access protected
  769. * @since 3.0.000 (2008-03-27)
  770. */
  771. protected $pagegroups;
  772. /**
  773. * Contains the alias of the current page group
  774. * @access protected
  775. * @since 3.0.000 (2008-03-27)
  776. */
  777. protected $currpagegroup;
  778. /**
  779. * Restrict the rendering of some elements to screen or printout.
  780. * @access protected
  781. * @since 3.0.000 (2008-03-27)
  782. */
  783. protected $visibility = 'all';
  784. /**
  785. * Print visibility.
  786. * @access protected
  787. * @since 3.0.000 (2008-03-27)
  788. */
  789. protected $n_ocg_print;
  790. /**
  791. * View visibility.
  792. * @access protected
  793. * @since 3.0.000 (2008-03-27)
  794. */
  795. protected $n_ocg_view;
  796. /**
  797. * Array of transparency objects and parameters.
  798. * @access protected
  799. * @since 3.0.000 (2008-03-27)
  800. */
  801. protected $extgstates;
  802. /**
  803. * Set the default JPEG compression quality (1-100)
  804. * @access protected
  805. * @since 3.0.000 (2008-03-27)
  806. */
  807. protected $jpeg_quality;
  808. /**
  809. * Default cell height ratio.
  810. * @access protected
  811. * @since 3.0.014 (2008-05-23)
  812. */
  813. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  814. /**
  815. * PDF viewer preferences.
  816. * @access protected
  817. * @since 3.1.000 (2008-06-09)
  818. */
  819. protected $viewer_preferences;
  820. /**
  821. * A name object specifying how the document should be displayed when opened.
  822. * @access protected
  823. * @since 3.1.000 (2008-06-09)
  824. */
  825. protected $PageMode;
  826. /**
  827. * Array for storing gradient information.
  828. * @access protected
  829. * @since 3.1.000 (2008-06-09)
  830. */
  831. protected $gradients = array();
  832. /**
  833. * Array used to store positions inside the pages buffer.
  834. * keys are the page numbers
  835. * @access protected
  836. * @since 3.2.000 (2008-06-26)
  837. */
  838. protected $intmrk = array();
  839. /**
  840. * Array used to store content positions inside the pages buffer.
  841. * keys are the page numbers
  842. * @access protected
  843. * @since 4.6.021 (2009-07-20)
  844. */
  845. protected $cntmrk = array();
  846. /**
  847. * Array used to store footer positions of each page.
  848. * @access protected
  849. * @since 3.2.000 (2008-07-01)
  850. */
  851. protected $footerpos = array();
  852. /**
  853. * Array used to store footer lenght of each page.
  854. * @access protected
  855. * @since 4.0.014 (2008-07-29)
  856. */
  857. protected $footerlen = array();
  858. /**
  859. * True if a newline is created.
  860. * @access protected
  861. * @since 3.2.000 (2008-07-01)
  862. */
  863. protected $newline = true;
  864. /**
  865. * End position of the latest inserted line
  866. * @access protected
  867. * @since 3.2.000 (2008-07-01)
  868. */
  869. protected $endlinex = 0;
  870. /**
  871. * PDF string for last line width
  872. * @access protected
  873. * @since 4.0.006 (2008-07-16)
  874. */
  875. protected $linestyleWidth = '';
  876. /**
  877. * PDF string for last line width
  878. * @access protected
  879. * @since 4.0.006 (2008-07-16)
  880. */
  881. protected $linestyleCap = '0 J';
  882. /**
  883. * PDF string for last line width
  884. * @access protected
  885. * @since 4.0.006 (2008-07-16)
  886. */
  887. protected $linestyleJoin = '0 j';
  888. /**
  889. * PDF string for last line width
  890. * @access protected
  891. * @since 4.0.006 (2008-07-16)
  892. */
  893. protected $linestyleDash = '[] 0 d';
  894. /**
  895. * True if marked-content sequence is open
  896. * @access protected
  897. * @since 4.0.013 (2008-07-28)
  898. */
  899. protected $openMarkedContent = false;
  900. /**
  901. * Count the latest inserted vertical spaces on HTML
  902. * @access protected
  903. * @since 4.0.021 (2008-08-24)
  904. */
  905. protected $htmlvspace = 0;
  906. /**
  907. * Array of Spot colors
  908. * @access protected
  909. * @since 4.0.024 (2008-09-12)
  910. */
  911. protected $spot_colors = array();
  912. /**
  913. * Symbol used for HTML unordered list items
  914. * @access protected
  915. * @since 4.0.028 (2008-09-26)
  916. */
  917. protected $lisymbol = '';
  918. /**
  919. * String used to mark the beginning and end of EPS image blocks
  920. * @access protected
  921. * @since 4.1.000 (2008-10-18)
  922. */
  923. protected $epsmarker = 'x#!#EPS#!#x';
  924. /**
  925. * Array of transformation matrix
  926. * @access protected
  927. * @since 4.2.000 (2008-10-29)
  928. */
  929. protected $transfmatrix = array();
  930. /**
  931. * Booklet mode for double-sided pages
  932. * @access protected
  933. * @since 4.2.000 (2008-10-29)
  934. */
  935. protected $booklet = false;
  936. /**
  937. * Epsilon value used for float calculations
  938. * @access protected
  939. * @since 4.2.000 (2008-10-29)
  940. */
  941. protected $feps = 0.005;
  942. /**
  943. * Array used for custom vertical spaces for HTML tags
  944. * @access protected
  945. * @since 4.2.001 (2008-10-30)
  946. */
  947. protected $tagvspaces = array();
  948. /**
  949. * @var HTML PARSER: custom indent amount for lists.
  950. * Negative value means disabled.
  951. * @access protected
  952. * @since 4.2.007 (2008-11-12)
  953. */
  954. protected $customlistindent = -1;
  955. /**
  956. * @var if true keeps the border open for the cell sides that cross the page.
  957. * @access protected
  958. * @since 4.2.010 (2008-11-14)
  959. */
  960. protected $opencell = true;
  961. /**
  962. * @var array of files to embedd
  963. * @access protected
  964. * @since 4.4.000 (2008-12-07)
  965. */
  966. protected $embeddedfiles = array();
  967. /**
  968. * @var boolean true when inside html pre tag
  969. * @access protected
  970. * @since 4.4.001 (2008-12-08)
  971. */
  972. protected $premode = false;
  973. /**
  974. * Array used to store positions of graphics transformation blocks inside the page buffer.
  975. * keys are the page numbers
  976. * @access protected
  977. * @since 4.4.002 (2008-12-09)
  978. */
  979. protected $transfmrk = array();
  980. /**
  981. * Default color for html links
  982. * @access protected
  983. * @since 4.4.003 (2008-12-09)
  984. */
  985. protected $htmlLinkColorArray = array(0, 0, 255);
  986. /**
  987. * Default font style to add to html links
  988. * @access protected
  989. * @since 4.4.003 (2008-12-09)
  990. */
  991. protected $htmlLinkFontStyle = 'U';
  992. /**
  993. * Counts the number of pages.
  994. * @access protected
  995. * @since 4.5.000 (2008-12-31)
  996. */
  997. protected $numpages = 0;
  998. /**
  999. * Array containing page lenghts in bytes.
  1000. * @access protected
  1001. * @since 4.5.000 (2008-12-31)
  1002. */
  1003. protected $pagelen = array();
  1004. /**
  1005. * Counts the number of pages.
  1006. * @access protected
  1007. * @since 4.5.000 (2008-12-31)
  1008. */
  1009. protected $numimages = 0;
  1010. /**
  1011. * Store the image keys.
  1012. * @access protected
  1013. * @since 4.5.000 (2008-12-31)
  1014. */
  1015. protected $imagekeys = array();
  1016. /**
  1017. * Lenght of the buffer in bytes.
  1018. * @access protected
  1019. * @since 4.5.000 (2008-12-31)
  1020. */
  1021. protected $bufferlen = 0;
  1022. /**
  1023. * If true enables disk caching.
  1024. * @access protected
  1025. * @since 4.5.000 (2008-12-31)
  1026. */
  1027. protected $diskcache = false;
  1028. /**
  1029. * Counts the number of fonts.
  1030. * @access protected
  1031. * @since 4.5.000 (2009-01-02)
  1032. */
  1033. protected $numfonts = 0;
  1034. /**
  1035. * Store the font keys.
  1036. * @access protected
  1037. * @since 4.5.000 (2009-01-02)
  1038. */
  1039. protected $fontkeys = array();
  1040. /**
  1041. * Store the fage status (true when opened, false when closed).
  1042. * @access protected
  1043. * @since 4.5.000 (2009-01-02)
  1044. */
  1045. protected $pageopen = array();
  1046. /**
  1047. * Default monospaced font
  1048. * @access protected
  1049. * @since 4.5.025 (2009-03-10)
  1050. */
  1051. protected $default_monospaced_font = 'courier';
  1052. /**
  1053. * Used to store a cloned copy of the current class object
  1054. * @access protected
  1055. * @since 4.5.029 (2009-03-19)
  1056. */
  1057. protected $objcopy;
  1058. /**
  1059. * Array used to store the lenghts of cache files
  1060. * @access protected
  1061. * @since 4.5.029 (2009-03-19)
  1062. */
  1063. protected $cache_file_lenght = array();
  1064. /**
  1065. * Table header content to be repeated on each new page
  1066. * @access protected
  1067. * @since 4.5.030 (2009-03-20)
  1068. */
  1069. protected $thead = '';
  1070. /**
  1071. * Margins used for table header.
  1072. * @access protected
  1073. * @since 4.5.030 (2009-03-20)
  1074. */
  1075. protected $theadMargins = array();
  1076. /**
  1077. * Cache array for UTF8StringToArray() method.
  1078. * @access protected
  1079. * @since 4.5.037 (2009-04-07)
  1080. */
  1081. protected $cache_UTF8StringToArray = array();
  1082. /**
  1083. * Maximum size of cache array used for UTF8StringToArray() method.
  1084. * @access protected
  1085. * @since 4.5.037 (2009-04-07)
  1086. */
  1087. protected $cache_maxsize_UTF8StringToArray = 8;
  1088. /**
  1089. * Current size of cache array used for UTF8StringToArray() method.
  1090. * @access protected
  1091. * @since 4.5.037 (2009-04-07)
  1092. */
  1093. protected $cache_size_UTF8StringToArray = 0;
  1094. /**
  1095. * If true enables document signing
  1096. * @access protected
  1097. * @since 4.6.005 (2009-04-24)
  1098. */
  1099. protected $sign = false;
  1100. /**
  1101. * Signature data
  1102. * @access protected
  1103. * @since 4.6.005 (2009-04-24)
  1104. */
  1105. protected $signature_data = array();
  1106. /**
  1107. * Signature max lenght
  1108. * @access protected
  1109. * @since 4.6.005 (2009-04-24)
  1110. */
  1111. protected $signature_max_lenght = 5120;
  1112. /**
  1113. * Regular expression used to find blank characters used for word-wrapping.
  1114. * @access protected
  1115. * @since 4.6.006 (2009-04-28)
  1116. */
  1117. protected $re_spaces = '/[\s]/';
  1118. /**
  1119. * Signature object ID
  1120. * @access protected
  1121. * @since 4.6.022 (2009-06-23)
  1122. */
  1123. protected $sig_obj_id = 0;
  1124. /**
  1125. * ByteRange placemark used during signature process.
  1126. * @access protected
  1127. * @since 4.6.028 (2009-08-25)
  1128. */
  1129. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1130. /**
  1131. * Placemark used during signature process.
  1132. * @access protected
  1133. * @since 4.6.028 (2009-08-25)
  1134. */
  1135. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1136. /**
  1137. * ID of page objects
  1138. * @access protected
  1139. * @since 4.7.000 (2009-08-29)
  1140. */
  1141. protected $page_obj_id = array();
  1142. /**
  1143. * Start ID for embedded file objects
  1144. * @access protected
  1145. * @since 4.7.000 (2009-08-29)
  1146. */
  1147. protected $embedded_start_obj_id = 100000;
  1148. /**
  1149. * Start ID for annotation objects
  1150. * @access protected
  1151. * @since 4.7.000 (2009-08-29)
  1152. */
  1153. protected $annots_start_obj_id = 200000;
  1154. /**
  1155. * Current ID of annotation object
  1156. * @access protected
  1157. * @since 4.7.000 (2009-08-29)
  1158. */
  1159. protected $annot_obj_id = 200000;
  1160. //------------------------------------------------------------
  1161. // METHODS
  1162. //------------------------------------------------------------
  1163. /**
  1164. * This is the class constructor.
  1165. * It allows to set up the page format, the orientation and
  1166. * the measure unit used in all the methods (except for the font sizes).
  1167. * @since 1.0
  1168. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li></ul>
  1169. * @param string $unit User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
  1170. * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
  1171. * @param boolean $unicode TRUE means that the input text is unicode (default = true)
  1172. * @param boolean $diskcache if TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1173. * @param String $encoding charset encoding; default is UTF-8
  1174. * @access public
  1175. */
  1176. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false) {
  1177. /* Set internal character encoding to ASCII */
  1178. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1179. $this->internal_encoding = mb_internal_encoding();
  1180. mb_internal_encoding('ASCII');
  1181. }
  1182. // set disk caching
  1183. $this->diskcache = $diskcache ? true : false;
  1184. // set language direction
  1185. $this->rtl = false;
  1186. $this->tmprtl = false;
  1187. //Some checks
  1188. $this->_dochecks();
  1189. //Initialization of properties
  1190. $this->isunicode = $unicode;
  1191. $this->page = 0;
  1192. $this->transfmrk[0] = array();
  1193. $this->pagedim = array();
  1194. $this->n = 2;
  1195. $this->buffer = '';
  1196. $this->pages = array();
  1197. $this->state = 0;
  1198. $this->fonts = array();
  1199. $this->FontFiles = array();
  1200. $this->diffs = array();
  1201. $this->images = array();
  1202. $this->links = array();
  1203. $this->gradients = array();
  1204. $this->InFooter = false;
  1205. $this->lasth = 0;
  1206. $this->FontFamily = 'helvetica';
  1207. $this->FontStyle = '';
  1208. $this->FontSizePt = 12;
  1209. $this->underline = false;
  1210. $this->linethrough = false;
  1211. $this->DrawColor = '0 G';
  1212. $this->FillColor = '0 g';
  1213. $this->TextColor = '0 g';
  1214. $this->ColorFlag = false;
  1215. // encryption values
  1216. $this->encrypted = false;
  1217. $this->last_rc4_key = '';
  1218. $this->padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
  1219. //Standard Unicode fonts
  1220. $this->CoreFonts = array(
  1221. 'courier'=>'Courier',
  1222. 'courierB'=>'Courier-Bold',
  1223. 'courierI'=>'Courier-Oblique',
  1224. 'courierBI'=>'Courier-BoldOblique',
  1225. 'helvetica'=>'Helvetica',
  1226. 'helveticaB'=>'Helvetica-Bold',
  1227. 'helveticaI'=>'Helvetica-Oblique',
  1228. 'helveticaBI'=>'Helvetica-BoldOblique',
  1229. 'times'=>'Times-Roman',
  1230. 'timesB'=>'Times-Bold',
  1231. 'timesI'=>'Times-Italic',
  1232. 'timesBI'=>'Times-BoldItalic',
  1233. 'symbol'=>'Symbol',
  1234. 'zapfdingbats'=>'ZapfDingbats'
  1235. );
  1236. //Set scale factor
  1237. $this->setPageUnit($unit);
  1238. // set page format and orientation
  1239. $this->setPageFormat($format, $orientation);
  1240. //Page margins (1 cm)
  1241. $margin = 28.35 / $this->k;
  1242. $this->SetMargins($margin, $margin);
  1243. //Interior cell margin
  1244. $this->cMargin = $margin / 10;
  1245. //Line width (0.2 mm)
  1246. $this->LineWidth = 0.57 / $this->k;
  1247. $this->linestyleWidth = sprintf('%.2F w', ($this->LineWidth * $this->k));
  1248. $this->linestyleCap = '0 J';
  1249. $this->linestyleJoin = '0 j';
  1250. $this->linestyleDash = '[] 0 d';
  1251. //Automatic page break
  1252. $this->SetAutoPageBreak(true, (2 * $margin));
  1253. //Full width display mode
  1254. $this->SetDisplayMode('fullwidth');
  1255. //Compression
  1256. $this->SetCompression(true);
  1257. //Set default PDF version number
  1258. $this->PDFVersion = '1.7';
  1259. $this->encoding = $encoding;
  1260. $this->HREF = array();
  1261. $this->getFontsList();
  1262. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1263. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1264. $this->extgstates = array();
  1265. // user's rights
  1266. $this->sign = false;
  1267. $this->ur = false;
  1268. $this->ur_document = '/FullSave';
  1269. $this->ur_annots = '/Create/Delete/Modify/Copy/Import/Export';
  1270. $this->ur_form = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1271. $this->ur_signature = '/Modify';
  1272. // set default JPEG quality
  1273. $this->jpeg_quality = 75;
  1274. // initialize some settings
  1275. $this->utf8Bidi(array(''), '');
  1276. // set default font
  1277. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1278. // check if PCRE Unicode support is enabled
  1279. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1280. // PCRE unicode support is turned ON
  1281. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1282. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1283. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1284. //$this->re_spaces = '/[\s\p{Z}\p{Lo}]/u';
  1285. $this->re_spaces = '/[\s\p{Z}]/u';
  1286. } else {
  1287. // PCRE unicode support is turned OFF
  1288. $this->re_spaces = '/[\s]/';
  1289. }
  1290. $this->annot_obj_id = $this->annots_start_obj_id;
  1291. }
  1292. /**
  1293. * Default destructor.
  1294. * @access public
  1295. * @since 1.53.0.TC016
  1296. */
  1297. public function __destruct() {
  1298. // restore internal encoding
  1299. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1300. mb_internal_encoding($this->internal_encoding);
  1301. }
  1302. // unset all class variables
  1303. $this->_destroy(true);
  1304. }
  1305. /**
  1306. * Set the units of measure for the document.
  1307. * @param string $unit User measure unit. Possible values are:<ul><li>pt: point</li><li>mm: millimeter (default)</li><li>cm: centimeter</li><li>in: inch</li></ul><br />A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit.
  1308. * @access public
  1309. * @since 3.0.015 (2008-06-06)
  1310. */
  1311. public function setPageUnit($unit) {
  1312. //Set scale factor
  1313. switch (strtolower($unit)) {
  1314. // points
  1315. case 'px':
  1316. case 'pt': {
  1317. $this->k = 1;
  1318. break;
  1319. }
  1320. // millimeters
  1321. case 'mm': {
  1322. $this->k = $this->dpi / 25.4;
  1323. break;
  1324. }
  1325. // centimeters
  1326. case 'cm': {
  1327. $this->k = $this->dpi / 2.54;
  1328. break;
  1329. }
  1330. // inches
  1331. case 'in': {
  1332. $this->k = $this->dpi;
  1333. break;
  1334. }
  1335. // unsupported unit
  1336. default : {
  1337. $this->Error('Incorrect unit: '.$unit);
  1338. break;
  1339. }
  1340. }
  1341. if (isset($this->CurOrientation)) {
  1342. $this->setPageOrientation($this->CurOrientation);
  1343. }
  1344. }
  1345. /**
  1346. * Set the page format
  1347. * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
  1348. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1349. * @access public
  1350. * @since 3.0.015 (2008-06-06)
  1351. */
  1352. public function setPageFormat($format, $orientation='P') {
  1353. //Page format
  1354. if (is_string($format)) {
  1355. // Page formats (45 standard ISO paper formats and 4 american common formats).
  1356. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 2.54 cm)
  1357. switch (strtoupper($format)) {
  1358. case '4A0': {$format = array(4767.87,6740.79); break;}
  1359. case '2A0': {$format = array(3370.39,4767.87); break;}
  1360. case 'A0': {$format = array(2383.94,3370.39); break;}
  1361. case 'A1': {$format = array(1683.78,2383.94); break;}
  1362. case 'A2': {$format = array(1190.55,1683.78); break;}
  1363. case 'A3': {$format = array(841.89,1190.55); break;}
  1364. case 'A4': default: {$format = array(595.28,841.89); break;}
  1365. case 'A5': {$format = array(419.53,595.28); break;}
  1366. case 'A6': {$format = array(297.64,419.53); break;}
  1367. case 'A7': {$format = array(209.76,297.64); break;}
  1368. case 'A8': {$format = array(147.40,209.76); break;}
  1369. case 'A9': {$format = array(104.88,147.40); break;}
  1370. case 'A10': {$format = array(73.70,104.88); break;}
  1371. case 'B0': {$format = array(2834.65,4008.19); break;}
  1372. case 'B1': {$format = array(2004.09,2834.65); break;}
  1373. case 'B2': {$format = array(1417.32,2004.09); break;}
  1374. case 'B3': {$format = array(1000.63,1417.32); break;}
  1375. case 'B4': {$format = array(708.66,1000.63); break;}
  1376. case 'B5': {$format = array(498.90,708.66); break;}
  1377. case 'B6': {$format = array(354.33,498.90); break;}
  1378. case 'B7': {$format = array(249.45,354.33); break;}
  1379. case 'B8': {$format = array(175.75,249.45); break;}
  1380. case 'B9': {$format = array(124.72,175.75); break;}
  1381. case 'B10': {$format = array(87.87,124.72); break;}
  1382. case 'C0': {$format = array(2599.37,3676.54); break;}
  1383. case 'C1': {$format = array(1836.85,2599.37); break;}
  1384. case 'C2': {$format = array(1298.27,1836.85); break;}
  1385. case 'C3': {$format = array(918.43,1298.27); break;}
  1386. case 'C4': {$format = array(649.13,918.43); break;}
  1387. case 'C5': {$format = array(459.21,649.13); break;}
  1388. case 'C6': {$format = array(323.15,459.21); break;}
  1389. case 'C7': {$format = array(229.61,323.15); break;}
  1390. case 'C8': {$format = array(161.57,229.61); break;}
  1391. case 'C9': {$format = array(113.39,161.57); break;}
  1392. case 'C10': {$format = array(79.37,113.39); break;}
  1393. case 'RA0': {$format = array(2437.80,3458.27); break;}
  1394. case 'RA1': {$format = array(1729.13,2437.80); break;}
  1395. case 'RA2': {$format = array(1218.90,1729.13); break;}
  1396. case 'RA3': {$format = array(864.57,1218.90); break;}
  1397. case 'RA4': {$format = array(609.45,864.57); break;}
  1398. case 'SRA0': {$format = array(2551.18,3628.35); break;}
  1399. case 'SRA1': {$format = array(1814.17,2551.18); break;}
  1400. case 'SRA2': {$format = array(1275.59,1814.17); break;}
  1401. case 'SRA3': {$format = array(907.09,1275.59); break;}
  1402. case 'SRA4': {$format = array(637.80,907.09); break;}
  1403. case 'LETTER': {$format = array(612.00,792.00); break;}
  1404. case 'LEGAL': {$format = array(612.00,1008.00); break;}
  1405. case 'EXECUTIVE': {$format = array(521.86,756.00); break;}
  1406. case 'FOLIO': {$format = array(612.00,936.00); break;}
  1407. }
  1408. $this->fwPt = $format[0];
  1409. $this->fhPt = $format[1];
  1410. } else {
  1411. $this->fwPt = $format[0] * $this->k;
  1412. $this->fhPt = $format[1] * $this->k;
  1413. }
  1414. $this->setPageOrientation($orientation);
  1415. }
  1416. /**
  1417. * Set page orientation.
  1418. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1419. * @param boolean $autopagebreak Boolean indicating if auto-page-break mode should be on or off.
  1420. * @param float $bottommargin bottom margin of the page.
  1421. * @access public
  1422. * @since 3.0.015 (2008-06-06)
  1423. */
  1424. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  1425. $orientation = strtoupper($orientation);
  1426. if (($orientation == 'P') OR ($orientation == 'PORTRAIT')) {
  1427. $this->CurOrientation = 'P';
  1428. $this->wPt = $this->fwPt;
  1429. $this->hPt = $this->fhPt;
  1430. } elseif (($orientation == 'L') OR ($orientation == 'LANDSCAPE')) {
  1431. $this->CurOrientation = 'L';
  1432. $this->wPt = $this->fhPt;
  1433. $this->hPt = $this->fwPt;
  1434. } else {
  1435. $this->Error('Incorrect orientation: '.$orientation);
  1436. }
  1437. $this->w = $this->wPt / $this->k;
  1438. $this->h = $this->hPt / $this->k;
  1439. if ($this->empty_string($autopagebreak)) {
  1440. if (isset($this->AutoPageBreak)) {
  1441. $autopagebreak = $this->AutoPageBreak;
  1442. } else {
  1443. $autopagebreak = true;
  1444. }
  1445. }
  1446. if ($this->empty_string($bottommargin)) {
  1447. if (isset($this->bMargin)) {
  1448. $bottommargin = $this->bMargin;
  1449. } else {
  1450. // default value = 2 cm
  1451. $bottommargin = 2 * 28.35 / $this->k;
  1452. }
  1453. }
  1454. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  1455. // store page dimensions
  1456. $this->pagedim[$this->page] = array('w' => $this->wPt, 'h' => $this->hPt, 'wk' => $this->w, 'hk' => $this->h, 'tm' => $this->tMargin, 'bm' => $bottommargin, 'lm' => $this->lMargin, 'rm' => $this->rMargin, 'pb' => $autopagebreak, 'or' => $this->CurOrientation, 'olm' => $this->original_lMargin, 'orm' => $this->original_rMargin);
  1457. }
  1458. /**
  1459. * Set regular expression to detect withespaces or word separators.
  1460. * @param string $re regular expression (leave empty for default).
  1461. * @access public
  1462. * @since 4.6.016 (2009-06-15)
  1463. */
  1464. public function setSpacesRE($re='/[\s]/') {
  1465. // if PCRE unicode support is turned ON:
  1466. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1467. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1468. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1469. $this->re_spaces = $re;
  1470. }
  1471. /**
  1472. * Enable or disable Right-To-Left language mode
  1473. * @param Boolean $enable if true enable Right-To-Left language mode.
  1474. * @param Boolean $resetx if true reset the X position on direction change.
  1475. * @access public
  1476. * @since 2.0.000 (2008-01-03)
  1477. */
  1478. public function setRTL($enable, $resetx=true) {
  1479. $enable = $enable ? true : false;
  1480. $resetx = ($resetx AND ($enable != $this->rtl));
  1481. $this->rtl = $enable;
  1482. $this->tmprtl = false;
  1483. if ($resetx) {
  1484. $this->Ln(0);
  1485. }
  1486. }
  1487. /**
  1488. * Return the RTL status
  1489. * @return boolean
  1490. * @access public
  1491. * @since 4.0.012 (2008-07-24)
  1492. */
  1493. public function getRTL() {
  1494. return $this->rtl;
  1495. }
  1496. /**
  1497. * Force temporary RTL language direction
  1498. * @param mixed $mode can be false, 'L' for LTR or 'R' for RTL
  1499. * @access public
  1500. * @since 2.1.000 (2008-01-09)
  1501. */
  1502. public function setTempRTL($mode) {
  1503. switch ($mode) {
  1504. case false:
  1505. case 'L':
  1506. case 'R': {
  1507. $this->tmprtl = $mode;
  1508. }
  1509. }
  1510. }
  1511. /**
  1512. * Set the last cell height.
  1513. * @param float $h cell height.
  1514. * @author Nicola Asuni
  1515. * @access public
  1516. * @since 1.53.0.TC034
  1517. */
  1518. public function setLastH($h) {
  1519. $this->lasth = $h;
  1520. }
  1521. /**
  1522. * Get the last cell height.
  1523. * @return last cell height
  1524. * @access public
  1525. * @since 4.0.017 (2008-08-05)
  1526. */
  1527. public function getLastH() {
  1528. return $this->lasth;
  1529. }
  1530. /**
  1531. * Set the adjusting factor to convert pixels to user units.
  1532. * @param float $scale adjusting factor to convert pixels to user units.
  1533. * @author Nicola Asuni
  1534. * @access public
  1535. * @since 1.5.2
  1536. */
  1537. public function setImageScale($scale) {
  1538. $this->imgscale = $scale;
  1539. }
  1540. /**
  1541. * Returns the adjusting factor to convert pixels to user units.
  1542. * @return float adjusting factor to convert pixels to user units.
  1543. * @author Nicola Asuni
  1544. * @access public
  1545. * @since 1.5.2
  1546. */
  1547. public function getImageScale() {
  1548. return $this->imgscale;
  1549. }
  1550. /**
  1551. * Returns an array of page dimensions:
  1552. * <ul><li>$this->pagedim[$this->page]['w'] => page_width_in_points</li><li>$this->pagedim[$this->page]['h'] => height in points</li><li>$this->pagedim[$this->page]['wk'] => page_width_in_points</li><li>$this->pagedim[$this->page]['hk'] => height</li><li>$this->pagedim[$this->page]['tm'] => top_margin</li><li>$this->pagedim[$this->page]['bm'] => bottom_margin</li><li>$this->pagedim[$this->page]['lm'] => left_margin</li><li>$this->pagedim[$this->page]['rm'] => right_margin</li><li>$this->pagedim[$this->page]['pb'] => auto_page_break</li><li>$this->pagedim[$this->page]['or'] => page_orientation</li><li>$this->pagedim[$this->page]['olm'] => original_left_margin</li><li>$this->pagedim[$this->page]['orm'] => original_right_margin</li></ul>
  1553. * @param int $pagenum page number (empty = current page)
  1554. * @return array of page dimensions.
  1555. * @author Nicola Asuni
  1556. * @access public
  1557. * @since 4.5.027 (2009-03-16)
  1558. */
  1559. public function getPageDimensions($pagenum='') {
  1560. if (empty($pagenum)) {
  1561. $pagenum = $this->page;
  1562. }
  1563. return $this->pagedim[$pagenum];
  1564. }
  1565. /**
  1566. * Returns the page width in units.
  1567. * @param int $pagenum page number (empty = current page)
  1568. * @return int page width.
  1569. * @author Nicola Asuni
  1570. * @access public
  1571. * @since 1.5.2
  1572. * @see getPageDimensions()
  1573. */
  1574. public function getPageWidth($pagenum='') {
  1575. if (empty($pagenum)) {
  1576. return $this->w;
  1577. }
  1578. return $this->pagedim[$pagenum]['w'];
  1579. }
  1580. /**
  1581. * Returns the page height in units.
  1582. * @param int $pagenum page number (empty = current page)
  1583. * @return int page height.
  1584. * @author Nicola Asuni
  1585. * @access public
  1586. * @since 1.5.2
  1587. * @see getPageDimensions()
  1588. */
  1589. public function getPageHeight($pagenum='') {
  1590. if (empty($pagenum)) {
  1591. return $this->h;
  1592. }
  1593. return $this->pagedim[$pagenum]['h'];
  1594. }
  1595. /**
  1596. * Returns the page break margin.
  1597. * @param int $pagenum page number (empty = current page)
  1598. * @return int page break margin.
  1599. * @author Nicola Asuni
  1600. * @access public
  1601. * @since 1.5.2
  1602. * @see getPageDimensions()
  1603. */
  1604. public function getBreakMargin($pagenum='') {
  1605. if (empty($pagenum)) {
  1606. return $this->bMargin;
  1607. }
  1608. return $this->pagedim[$pagenum]['bm'];
  1609. }
  1610. /**
  1611. * Returns the scale factor (number of points in user unit).
  1612. * @return int scale factor.
  1613. * @author Nicola Asuni
  1614. * @access public
  1615. * @since 1.5.2
  1616. */
  1617. public function getScaleFactor() {
  1618. return $this->k;
  1619. }
  1620. /**
  1621. * Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.
  1622. * @param float $left Left margin.
  1623. * @param float $top Top margin.
  1624. * @param float $right Right margin. Default value is the left one.
  1625. * @access public
  1626. * @since 1.0
  1627. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  1628. */
  1629. public function SetMargins($left, $top, $right=-1) {
  1630. //Set left, top and right margins
  1631. $this->lMargin = $left;
  1632. $this->tMargin = $top;
  1633. if ($right == -1) {
  1634. $right = $left;
  1635. }
  1636. $this->rMargin = $right;
  1637. }
  1638. /**
  1639. * Defines the left margin. The method can be called before creating the first page. If the current abscissa gets out of page, it is brought back to the margin.
  1640. * @param float $margin The margin.
  1641. * @access public
  1642. * @since 1.4
  1643. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1644. */
  1645. public function SetLeftMargin($margin) {
  1646. //Set left margin
  1647. $this->lMargin=$margin;
  1648. if (($this->page > 0) AND ($this->x < $margin)) {
  1649. $this->x = $margin;
  1650. }
  1651. }
  1652. /**
  1653. * Defines the top margin. The method can be called before creating the first page.
  1654. * @param float $margin The margin.
  1655. * @access public
  1656. * @since 1.5
  1657. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  1658. */
  1659. public function SetTopMargin($margin) {
  1660. //Set top margin
  1661. $this->tMargin=$margin;
  1662. if (($this->page > 0) AND ($this->y < $margin)) {
  1663. $this->y = $margin;
  1664. }
  1665. }
  1666. /**
  1667. * Defines the right margin. The method can be called before creating the first page.
  1668. * @param float $margin The margin.
  1669. * @access public
  1670. * @since 1.5
  1671. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1672. */
  1673. public function SetRightMargin($margin) {
  1674. $this->rMargin=$margin;
  1675. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  1676. $this->x = $this->w - $margin;
  1677. }
  1678. }
  1679. /**
  1680. * Set the internal Cell padding.
  1681. * @param float $pad internal padding.
  1682. * @access public
  1683. * @since 2.1.000 (2008-01-09)
  1684. * @see Cell(), SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  1685. */
  1686. public function SetCellPadding($pad) {
  1687. $this->cMargin = $pad;
  1688. }
  1689. /**
  1690. * Enables or disables the automatic page breaking mode. When enabling, the second parameter is the distance from the bottom of the page that defines the triggering limit. By default, the mode is on and the margin is 2 cm.
  1691. * @param boolean $auto Boolean indicating if mode should be on or off.
  1692. * @param float $margin Distance from the bottom of the page.
  1693. * @access public
  1694. * @since 1.0
  1695. * @see Cell(), MultiCell(), AcceptPageBreak()
  1696. */
  1697. public function SetAutoPageBreak($auto, $margin=0) {
  1698. //Set auto page break mode and triggering margin
  1699. $this->AutoPageBreak = $auto;
  1700. $this->bMargin = $margin;
  1701. $this->PageBreakTrigger = $this->h - $margin;
  1702. }
  1703. /**
  1704. * Defines the way the document is to be displayed by the viewer.
  1705. * @param mixed $zoom The zoom to use. It can be one of the following string values or a number indicating the zooming factor to use. <ul><li>fullpage: displays the entire page on screen </li><li>fullwidth: uses maximum width of window</li><li>real: uses real size (equivalent to 100% zoom)</li><li>default: uses viewer default mode</li></ul>
  1706. * @param string $layout The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
  1707. * @param string $mode A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
  1708. * @access public
  1709. * @since 1.2
  1710. */
  1711. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  1712. //Set display mode in viewer
  1713. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  1714. $this->ZoomMode = $zoom;
  1715. } else {
  1716. $this->Error('Incorrect zoom display mode: '.$zoom);
  1717. }
  1718. switch ($layout) {
  1719. case 'default':
  1720. case 'single':
  1721. case 'SinglePage': {
  1722. $this->LayoutMode = 'SinglePage';
  1723. break;
  1724. }
  1725. case 'continuous':
  1726. case 'OneColumn': {
  1727. $this->LayoutMode = 'OneColumn';
  1728. break;
  1729. }
  1730. case 'two':
  1731. case 'TwoColumnLeft': {
  1732. $this->LayoutMode = 'TwoColumnLeft';
  1733. break;
  1734. }
  1735. case 'TwoColumnRight': {
  1736. $this->LayoutMode = 'TwoColumnRight';
  1737. break;
  1738. }
  1739. case 'TwoPageLeft': {
  1740. $this->LayoutMode = 'TwoPageLeft';
  1741. break;
  1742. }
  1743. case 'TwoPageRight': {
  1744. $this->LayoutMode = 'TwoPageRight';
  1745. break;
  1746. }
  1747. default: {
  1748. $this->LayoutMode = 'SinglePage';
  1749. }
  1750. }
  1751. switch ($mode) {
  1752. case 'UseNone': {
  1753. $this->PageMode = 'UseNone';
  1754. break;
  1755. }
  1756. case 'UseOutlines': {
  1757. $this->PageMode = 'UseOutlines';
  1758. break;
  1759. }
  1760. case 'UseThumbs': {
  1761. $this->PageMode = 'UseThumbs';
  1762. break;
  1763. }
  1764. case 'FullScreen': {
  1765. $this->PageMode = 'FullScreen';
  1766. break;
  1767. }
  1768. case 'UseOC': {
  1769. $this->PageMode = 'UseOC';
  1770. break;
  1771. }
  1772. case '': {
  1773. $this->PageMode = 'UseAttachments';
  1774. break;
  1775. }
  1776. default: {
  1777. $this->PageMode = 'UseNone';
  1778. }
  1779. }
  1780. }
  1781. /**
  1782. * Activates or deactivates page compression. When activated, the internal representation of each page is compressed, which leads to a compression ratio of about 2 for the resulting document. Compression is on by default.
  1783. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  1784. * @param boolean $compress Boolean indicating if compression must be enabled.
  1785. * @access public
  1786. * @since 1.4
  1787. */
  1788. public function SetCompression($compress) {
  1789. //Set page compression
  1790. if (function_exists('gzcompress')) {
  1791. $this->compress = $compress;
  1792. } else {
  1793. $this->compress = false;
  1794. }
  1795. }
  1796. /**
  1797. * Defines the title of the document.
  1798. * @param string $title The title.
  1799. * @access public
  1800. * @since 1.2
  1801. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  1802. */
  1803. public function SetTitle($title) {
  1804. //Title of document
  1805. $this->title = $title;
  1806. }
  1807. /**
  1808. * Defines the subject of the document.
  1809. * @param string $subject The subject.
  1810. * @access public
  1811. * @since 1.2
  1812. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  1813. */
  1814. public function SetSubject($subject) {
  1815. //Subject of document
  1816. $this->subject = $subject;
  1817. }
  1818. /**
  1819. * Defines the author of the document.
  1820. * @param string $author The name of the author.
  1821. * @access public
  1822. * @since 1.2
  1823. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  1824. */
  1825. public function SetAuthor($author) {
  1826. //Author of document
  1827. $this->author = $author;
  1828. }
  1829. /**
  1830. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  1831. * @param string $keywords The list of keywords.
  1832. * @access public
  1833. * @since 1.2
  1834. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  1835. */
  1836. public function SetKeywords($keywords) {
  1837. //Keywords of document
  1838. $this->keywords = $keywords;
  1839. }
  1840. /**
  1841. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  1842. * @param string $creator The name of the creator.
  1843. * @access public
  1844. * @since 1.2
  1845. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  1846. */
  1847. public function SetCreator($creator) {
  1848. //Creator of document
  1849. $this->creator = $creator;
  1850. }
  1851. /**
  1852. * This method is automatically called in case of fatal error; it simply outputs the message and halts the execution. An inherited class may override it to customize the error handling but should always halt the script, or the resulting document would probably be invalid.
  1853. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  1854. * @param string $msg The error message
  1855. * @access public
  1856. * @since 1.0
  1857. */
  1858. public function Error($msg) {
  1859. // unset all class variables
  1860. $this->_destroy(true);
  1861. // exit program and print error
  1862. die('<strong>TCPDF ERROR: </strong>'.$msg);
  1863. }
  1864. /**
  1865. * This method begins the generation of the PDF document.
  1866. * It is not necessary to call it explicitly because AddPage() does it automatically.
  1867. * Note: no page is created by this method
  1868. * @access public
  1869. * @since 1.0
  1870. * @see AddPage(), Close()
  1871. */
  1872. public function Open() {
  1873. //Begin document
  1874. $this->state = 1;
  1875. }
  1876. /**
  1877. * Terminates the PDF document.
  1878. * It is not necessary to call this method explicitly because Output() does it automatically.
  1879. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  1880. * @access public
  1881. * @since 1.0
  1882. * @see Open(), Output()
  1883. */
  1884. public function Close() {
  1885. if ($this->state == 3) {
  1886. return;
  1887. }
  1888. if ($this->page == 0) {
  1889. $this->AddPage();
  1890. }
  1891. // close page
  1892. $this->endPage();
  1893. // close document
  1894. $this->_enddoc();
  1895. // unset all class variables (except critical ones)
  1896. $this->_destroy(false);
  1897. }
  1898. /**
  1899. * Move pointer at the specified document page and update page dimensions.
  1900. * @param int $pnum page number
  1901. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  1902. * @access public
  1903. * @since 2.1.000 (2008-01-07)
  1904. * @see getPage(), lastpage(), getNumPages()
  1905. */
  1906. public function setPage($pnum, $resetmargins=false) {
  1907. if ($pnum == $this->page) {
  1908. return;
  1909. }
  1910. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  1911. $this->state = 2;
  1912. // save current graphic settings
  1913. //$gvars = $this->getGraphicVars();
  1914. $oldpage = $this->page;
  1915. $this->page = $pnum;
  1916. $this->wPt = $this->pagedim[$this->page]['w'];
  1917. $this->hPt = $this->pagedim[$this->page]['h'];
  1918. $this->w = $this->wPt / $this->k;
  1919. $this->h = $this->hPt / $this->k;
  1920. $this->tMargin = $this->pagedim[$this->page]['tm'];
  1921. $this->bMargin = $this->pagedim[$this->page]['bm'];
  1922. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  1923. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  1924. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  1925. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  1926. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  1927. // restore graphic settings
  1928. //$this->setGraphicVars($gvars);
  1929. if ($resetmargins) {
  1930. $this->lMargin = $this->pagedim[$this->page]['olm'];
  1931. $this->rMargin = $this->pagedim[$this->page]['orm'];
  1932. $this->SetY($this->tMargin);
  1933. } else {
  1934. // account for booklet mode
  1935. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  1936. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  1937. $this->lMargin += $deltam;
  1938. $this->rMargin -= $deltam;
  1939. }
  1940. }
  1941. } else {
  1942. $this->Error('Wrong page number on setPage() function.');
  1943. }
  1944. }
  1945. /**
  1946. * Reset pointer to the last document page.
  1947. * @param boolean $resetmargins if true reset left, right, top margins and Y position.
  1948. * @access public
  1949. * @since 2.0.000 (2008-01-04)
  1950. * @see setPage(), getPage(), getNumPages()
  1951. */
  1952. public function lastPage($resetmargins=false) {
  1953. $this->setPage($this->getNumPages(), $resetmargins);
  1954. }
  1955. /**
  1956. * Get current document page number.
  1957. * @return int page number
  1958. * @access public
  1959. * @since 2.1.000 (2008-01-07)
  1960. * @see setPage(), lastpage(), getNumPages()
  1961. */
  1962. public function getPage() {
  1963. return $this->page;
  1964. }
  1965. /**
  1966. * Get the total number of insered pages.
  1967. * @return int number of pages
  1968. * @access public
  1969. * @since 2.1.000 (2008-01-07)
  1970. * @see setPage(), getPage(), lastpage()
  1971. */
  1972. public function getNumPages() {
  1973. return $this->numpages;
  1974. }
  1975. /**
  1976. * Adds a new page to the document. If a page is already present, the Footer() method is called first to output the footer (if enabled). Then the page is added, the current position set to the top-left corner according to the left and top margins (or top-right if in RTL mode), and Header() is called to display the header (if enabled).
  1977. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  1978. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  1979. * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
  1980. * @access public
  1981. * @since 1.0
  1982. * @see startPage(), endPage()
  1983. */
  1984. public function AddPage($orientation='', $format='') {
  1985. if (!isset($this->original_lMargin)) {
  1986. $this->original_lMargin = $this->lMargin;
  1987. }
  1988. if (!isset($this->original_rMargin)) {
  1989. $this->original_rMargin = $this->rMargin;
  1990. }
  1991. // terminate previous page
  1992. $this->endPage();
  1993. // start new page
  1994. $this->startPage($orientation, $format);
  1995. }
  1996. /**
  1997. * Terminate the current page
  1998. * @access protected
  1999. * @since 4.2.010 (2008-11-14)
  2000. * @see startPage(), AddPage()
  2001. */
  2002. protected function endPage() {
  2003. // check if page is already closed
  2004. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  2005. return;
  2006. }
  2007. $this->InFooter = true;
  2008. // print page footer
  2009. $this->setFooter();
  2010. // close page
  2011. $this->_endpage();
  2012. // mark page as closed
  2013. $this->pageopen[$this->page] = false;
  2014. $this->InFooter = false;
  2015. }
  2016. /**
  2017. * Starts a new page to the document. The page must be closed using the endPage() function.
  2018. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  2019. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  2020. * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
  2021. * @access protected
  2022. * @since 4.2.010 (2008-11-14)
  2023. * @see endPage(), AddPage()
  2024. */
  2025. protected function startPage($orientation='', $format='') {
  2026. if ($this->numpages > $this->page) {
  2027. // this page has been already added
  2028. $this->setPage($this->page + 1);
  2029. $this->SetY($this->tMargin);
  2030. return;
  2031. }
  2032. // start a new page
  2033. if ($this->state == 0) {
  2034. $this->Open();
  2035. }
  2036. ++$this->numpages;
  2037. $this->swapMargins($this->booklet);
  2038. // save current graphic settings
  2039. $gvars = $this->getGraphicVars();
  2040. // start new page
  2041. $this->_beginpage($orientation, $format);
  2042. // mark page as open
  2043. $this->pageopen[$this->page] = true;
  2044. // restore graphic settings
  2045. $this->setGraphicVars($gvars);
  2046. // mark this point
  2047. $this->setPageMark();
  2048. // print page header
  2049. $this->setHeader();
  2050. // restore graphic settings
  2051. $this->setGraphicVars($gvars);
  2052. // mark this point
  2053. $this->setPageMark();
  2054. // print table header (if any)
  2055. $this->setTableHeader();
  2056. }
  2057. /**
  2058. * Set start-writing mark on current page for multicell borders and fills.
  2059. * This function must be called after calling Image() function for a background image.
  2060. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  2061. * @access public
  2062. * @since 4.0.016 (2008-07-30)
  2063. */
  2064. public function setPageMark() {
  2065. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  2066. $this->setContentMark();
  2067. }
  2068. /**
  2069. * Set start-writing mark on selected page.
  2070. * @param int $page page number (default is the current page)
  2071. * @access protected
  2072. * @since 4.6.021 (2009-07-20)
  2073. */
  2074. protected function setContentMark($page=0) {
  2075. if ($page <= 0) {
  2076. $page = $this->page;
  2077. }
  2078. if (isset($this->footerlen[$page])) {
  2079. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  2080. } else {
  2081. $this->cntmrk[$page] = $this->pagelen[$page];
  2082. }
  2083. }
  2084. /**
  2085. * Set header data.
  2086. * @param string $ln header image logo
  2087. * @param string $lw header image logo width in mm
  2088. * @param string $ht string to print as title on document header
  2089. * @param string $hs string to print on document header
  2090. * @access public
  2091. */
  2092. public function setHeaderData($ln='', $lw=0, $ht='', $hs='') {
  2093. $this->header_logo = $ln;
  2094. $this->header_logo_width = $lw;
  2095. $this->header_title = $ht;
  2096. $this->header_string = $hs;
  2097. }
  2098. /**
  2099. * Returns header data:
  2100. * <ul><li>$ret['logo'] = logo image</li><li>$ret['logo_width'] = width of the image logo in user units</li><li>$ret['title'] = header title</li><li>$ret['string'] = header description string</li></ul>
  2101. * @return array()
  2102. * @access public
  2103. * @since 4.0.012 (2008-07-24)
  2104. */
  2105. public function getHeaderData() {
  2106. $ret = array();
  2107. $ret['logo'] = $this->header_logo;
  2108. $ret['logo_width'] = $this->header_logo_width;
  2109. $ret['title'] = $this->header_title;
  2110. $ret['string'] = $this->header_string;
  2111. return $ret;
  2112. }
  2113. /**
  2114. * Set header margin.
  2115. * (minimum distance between header and top page margin)
  2116. * @param int $hm distance in user units
  2117. * @access public
  2118. */
  2119. public function setHeaderMargin($hm=10) {
  2120. $this->header_margin = $hm;
  2121. }
  2122. /**
  2123. * Returns header margin in user units.
  2124. * @return float
  2125. * @since 4.0.012 (2008-07-24)
  2126. * @access public
  2127. */
  2128. public function getHeaderMargin() {
  2129. return $this->header_margin;
  2130. }
  2131. /**
  2132. * Set footer margin.
  2133. * (minimum distance between footer and bottom page margin)
  2134. * @param int $fm distance in user units
  2135. * @access public
  2136. */
  2137. public function setFooterMargin($fm=10) {
  2138. $this->footer_margin = $fm;
  2139. }
  2140. /**
  2141. * Returns footer margin in user units.
  2142. * @return float
  2143. * @since 4.0.012 (2008-07-24)
  2144. * @access public
  2145. */
  2146. public function getFooterMargin() {
  2147. return $this->footer_margin;
  2148. }
  2149. /**
  2150. * Set a flag to print page header.
  2151. * @param boolean $val set to true to print the page header (default), false otherwise.
  2152. * @access public
  2153. */
  2154. public function setPrintHeader($val=true) {
  2155. $this->print_header = $val;
  2156. }
  2157. /**
  2158. * Set a flag to print page footer.
  2159. * @param boolean $value set to true to print the page footer (default), false otherwise.
  2160. * @access public
  2161. */
  2162. public function setPrintFooter($val=true) {
  2163. $this->print_footer = $val;
  2164. }
  2165. /**
  2166. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  2167. * @return float
  2168. * @access public
  2169. */
  2170. public function getImageRBX() {
  2171. return $this->img_rb_x;
  2172. }
  2173. /**
  2174. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  2175. * @return float
  2176. * @access public
  2177. */
  2178. public function getImageRBY() {
  2179. return $this->img_rb_y;
  2180. }
  2181. /**
  2182. * This method is used to render the page header.
  2183. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2184. * @access public
  2185. */
  2186. public function Header() {
  2187. $ormargins = $this->getOriginalMargins();
  2188. $headerfont = $this->getHeaderFont();
  2189. $headerdata = $this->getHeaderData();
  2190. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  2191. $this->Image(K_PATH_IMAGES.$headerdata['logo'], $this->GetX(), $this->getHeaderMargin(), $headerdata['logo_width']);
  2192. $imgy = $this->getImageRBY();
  2193. } else {
  2194. $imgy = $this->GetY();
  2195. }
  2196. $cell_height = round(($this->getCellHeightRatio() * $headerfont[2]) / $this->getScaleFactor(), 2);
  2197. // set starting margin for text data cell
  2198. if ($this->getRTL()) {
  2199. $header_x = $ormargins['right'] + ($headerdata['logo_width'] * 1.1);
  2200. } else {
  2201. $header_x = $ormargins['left'] + ($headerdata['logo_width'] * 1.1);
  2202. }
  2203. $this->SetTextColor(0, 0, 0);
  2204. // header title
  2205. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  2206. $this->SetX($header_x);
  2207. $this->Cell(0, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  2208. // header string
  2209. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  2210. $this->SetX($header_x);
  2211. $this->MultiCell(0, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false);
  2212. // print an ending header line
  2213. $this->SetLineStyle(array('width' => 0.85 / $this->getScaleFactor(), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2214. $this->SetY((2.835 / $this->getScaleFactor()) + max($imgy, $this->GetY()));
  2215. if ($this->getRTL()) {
  2216. $this->SetX($ormargins['right']);
  2217. } else {
  2218. $this->SetX($ormargins['left']);
  2219. }
  2220. $this->Cell(0, 0, '', 'T', 0, 'C');
  2221. }
  2222. /**
  2223. * This method is used to render the page footer.
  2224. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  2225. * @access public
  2226. */
  2227. public function Footer() {
  2228. $cur_y = $this->GetY();
  2229. $ormargins = $this->getOriginalMargins();
  2230. $this->SetTextColor(0, 0, 0);
  2231. //set style for cell border
  2232. $line_width = 0.85 / $this->getScaleFactor();
  2233. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  2234. //print document barcode
  2235. $barcode = $this->getBarcode();
  2236. if (!empty($barcode)) {
  2237. $this->Ln($line_width);
  2238. $barcode_width = round(($this->getPageWidth() - $ormargins['left'] - $ormargins['right'])/3);
  2239. $this->write1DBarcode($barcode, 'C128B', $this->GetX(), $cur_y + $line_width, $barcode_width, (($this->getFooterMargin() / 3) - $line_width), 0.3, '', '');
  2240. }
  2241. if (empty($this->pagegroups)) {
  2242. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  2243. } else {
  2244. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  2245. }
  2246. $this->SetY($cur_y);
  2247. //Print page number
  2248. if ($this->getRTL()) {
  2249. $this->SetX($ormargins['right']);
  2250. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  2251. } else {
  2252. $this->SetX($ormargins['left']);
  2253. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'R');
  2254. }
  2255. }
  2256. /**
  2257. * This method is used to render the page header.
  2258. * @access protected
  2259. * @since 4.0.012 (2008-07-24)
  2260. */
  2261. protected function setHeader() {
  2262. if ($this->print_header) {
  2263. $lasth = $this->lasth;
  2264. $this->_out('q');
  2265. $this->rMargin = $this->original_rMargin;
  2266. $this->lMargin = $this->original_lMargin;
  2267. $this->cMargin = 0;
  2268. //set current position
  2269. if ($this->rtl) {
  2270. $this->SetXY($this->original_rMargin, $this->header_margin);
  2271. } else {
  2272. $this->SetXY($this->original_lMargin, $this->header_margin);
  2273. }
  2274. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  2275. $this->Header();
  2276. //restore position
  2277. if ($this->rtl) {
  2278. $this->SetXY($this->original_rMargin, $this->tMargin);
  2279. } else {
  2280. $this->SetXY($this->original_lMargin, $this->tMargin);
  2281. }
  2282. $this->_out('Q');
  2283. $this->lasth = $lasth;
  2284. }
  2285. }
  2286. /**
  2287. * This method is used to render the page footer.
  2288. * @access protected
  2289. * @since 4.0.012 (2008-07-24)
  2290. */
  2291. protected function setFooter() {
  2292. //Page footer
  2293. // save current graphic settings
  2294. $gvars = $this->getGraphicVars();
  2295. // mark this point
  2296. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  2297. $this->_out("\n");
  2298. if ($this->print_footer) {
  2299. $lasth = $this->lasth;
  2300. $this->_out('q');
  2301. $this->rMargin = $this->original_rMargin;
  2302. $this->lMargin = $this->original_lMargin;
  2303. $this->cMargin = 0;
  2304. //set current position
  2305. $footer_y = $this->h - $this->footer_margin;
  2306. if ($this->rtl) {
  2307. $this->SetXY($this->original_rMargin, $footer_y);
  2308. } else {
  2309. $this->SetXY($this->original_lMargin, $footer_y);
  2310. }
  2311. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  2312. $this->Footer();
  2313. //restore position
  2314. if ($this->rtl) {
  2315. $this->SetXY($this->original_rMargin, $this->tMargin);
  2316. } else {
  2317. $this->SetXY($this->original_lMargin, $this->tMargin);
  2318. }
  2319. $this->_out('Q');
  2320. $this->lasth = $lasth;
  2321. }
  2322. // restore graphic settings
  2323. $this->setGraphicVars($gvars);
  2324. // calculate footer lenght
  2325. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  2326. }
  2327. /**
  2328. * This method is used to render the table header on new page (if any).
  2329. * @access protected
  2330. * @since 4.5.030 (2009-03-25)
  2331. */
  2332. protected function setTableHeader() {
  2333. if (isset($this->theadMargins['top'])) {
  2334. // restore the original top-margin
  2335. $this->tMargin = $this->theadMargins['top'];
  2336. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2337. $this->y = $this->tMargin;
  2338. }
  2339. if (!$this->empty_string($this->thead)) {
  2340. // set margins
  2341. $prev_lMargin = $this->lMargin;
  2342. $prev_rMargin = $this->rMargin;
  2343. $this->lMargin = $this->pagedim[$this->page]['olm'];
  2344. $this->rMargin = $this->pagedim[$this->page]['orm'];
  2345. $this->cMargin = $this->theadMargins['cmargin'];
  2346. // print table header
  2347. $this->writeHTML($this->thead, false, false, false, false, '');
  2348. // set new top margin to skip the table headers
  2349. if (!isset($this->theadMargins['top'])) {
  2350. $this->theadMargins['top'] = $this->tMargin;
  2351. }
  2352. $this->tMargin = $this->y;
  2353. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2354. $this->lasth = 0;
  2355. $this->lMargin = $prev_lMargin;
  2356. $this->rMargin = $prev_rMargin;
  2357. }
  2358. }
  2359. /**
  2360. * Returns the current page number.
  2361. * @return int page number
  2362. * @access public
  2363. * @since 1.0
  2364. * @see AliasNbPages(), getAliasNbPages()
  2365. */
  2366. public function PageNo() {
  2367. return $this->page;
  2368. }
  2369. /**
  2370. * Defines a new spot color.
  2371. * It can be expressed in RGB components or gray scale.
  2372. * The method can be called before the first page is created and the value is retained from page to page.
  2373. * @param int $c Cyan color for CMYK. Value between 0 and 255
  2374. * @param int $m Magenta color for CMYK. Value between 0 and 255
  2375. * @param int $y Yellow color for CMYK. Value between 0 and 255
  2376. * @param int $k Key (Black) color for CMYK. Value between 0 and 255
  2377. * @access public
  2378. * @since 4.0.024 (2008-09-12)
  2379. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2380. */
  2381. public function AddSpotColor($name, $c, $m, $y, $k) {
  2382. if (!isset($this->spot_colors[$name])) {
  2383. $i = 1 + count($this->spot_colors);
  2384. $this->spot_colors[$name] = array('i' => $i, 'c' => $c, 'm' => $m, 'y' => $y, 'k' => $k);
  2385. }
  2386. }
  2387. /**
  2388. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  2389. * It can be expressed in RGB components or gray scale.
  2390. * The method can be called before the first page is created and the value is retained from page to page.
  2391. * @param array $color array of colors
  2392. * @access public
  2393. * @since 3.1.000 (2008-06-11)
  2394. * @see SetDrawColor()
  2395. */
  2396. public function SetDrawColorArray($color) {
  2397. if (isset($color)) {
  2398. $color = array_values($color);
  2399. $r = isset($color[0]) ? $color[0] : -1;
  2400. $g = isset($color[1]) ? $color[1] : -1;
  2401. $b = isset($color[2]) ? $color[2] : -1;
  2402. $k = isset($color[3]) ? $color[3] : -1;
  2403. if ($r >= 0) {
  2404. $this->SetDrawColor($r, $g, $b, $k);
  2405. }
  2406. }
  2407. }
  2408. /**
  2409. * Defines the color used for all drawing operations (lines, rectangles and cell borders). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
  2410. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2411. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2412. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2413. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2414. * @access public
  2415. * @since 1.3
  2416. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  2417. */
  2418. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2419. // set default values
  2420. if (!is_numeric($col1)) {
  2421. $col1 = 0;
  2422. }
  2423. if (!is_numeric($col2)) {
  2424. $col2 = -1;
  2425. }
  2426. if (!is_numeric($col3)) {
  2427. $col3 = -1;
  2428. }
  2429. if (!is_numeric($col4)) {
  2430. $col4 = -1;
  2431. }
  2432. //Set color for all stroking operations
  2433. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2434. // Grey scale
  2435. $this->DrawColor = sprintf('%.3F G', $col1/255);
  2436. } elseif ($col4 == -1) {
  2437. // RGB
  2438. $this->DrawColor = sprintf('%.3F %.3F %.3F RG', $col1/255, $col2/255, $col3/255);
  2439. } else {
  2440. // CMYK
  2441. $this->DrawColor = sprintf('%.3F %.3F %.3F %.3F K', $col1/100, $col2/100, $col3/100, $col4/100);
  2442. }
  2443. if ($this->page > 0) {
  2444. $this->_out($this->DrawColor);
  2445. }
  2446. }
  2447. /**
  2448. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  2449. * @param string $name name of the spot color
  2450. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2451. * @access public
  2452. * @since 4.0.024 (2008-09-12)
  2453. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  2454. */
  2455. public function SetDrawSpotColor($name, $tint=100) {
  2456. if (!isset($this->spot_colors[$name])) {
  2457. $this->Error('Undefined spot color: '.$name);
  2458. }
  2459. $this->DrawColor = sprintf('/CS%d CS %.3F SCN', $this->spot_colors[$name]['i'], $tint/100);
  2460. if ($this->page > 0) {
  2461. $this->_out($this->DrawColor);
  2462. }
  2463. }
  2464. /**
  2465. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  2466. * It can be expressed in RGB components or gray scale.
  2467. * The method can be called before the first page is created and the value is retained from page to page.
  2468. * @param array $color array of colors
  2469. * @access public
  2470. * @since 3.1.000 (2008-6-11)
  2471. * @see SetFillColor()
  2472. */
  2473. public function SetFillColorArray($color) {
  2474. if (isset($color)) {
  2475. $color = array_values($color);
  2476. $r = isset($color[0]) ? $color[0] : -1;
  2477. $g = isset($color[1]) ? $color[1] : -1;
  2478. $b = isset($color[2]) ? $color[2] : -1;
  2479. $k = isset($color[3]) ? $color[3] : -1;
  2480. if ($r >= 0) {
  2481. $this->SetFillColor($r, $g, $b, $k);
  2482. }
  2483. }
  2484. }
  2485. /**
  2486. * Defines the color used for all filling operations (filled rectangles and cell backgrounds). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
  2487. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2488. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2489. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2490. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2491. * @access public
  2492. * @since 1.3
  2493. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  2494. */
  2495. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2496. // set default values
  2497. if (!is_numeric($col1)) {
  2498. $col1 = 0;
  2499. }
  2500. if (!is_numeric($col2)) {
  2501. $col2 = -1;
  2502. }
  2503. if (!is_numeric($col3)) {
  2504. $col3 = -1;
  2505. }
  2506. if (!is_numeric($col4)) {
  2507. $col4 = -1;
  2508. }
  2509. //Set color for all filling operations
  2510. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2511. // Grey scale
  2512. $this->FillColor = sprintf('%.3F g', $col1/255);
  2513. $this->bgcolor = array('G' => $col1);
  2514. } elseif ($col4 == -1) {
  2515. // RGB
  2516. $this->FillColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2517. $this->bgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2518. } else {
  2519. // CMYK
  2520. $this->FillColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2521. $this->bgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2522. }
  2523. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2524. if ($this->page > 0) {
  2525. $this->_out($this->FillColor);
  2526. }
  2527. }
  2528. /**
  2529. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  2530. * @param string $name name of the spot color
  2531. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2532. * @access public
  2533. * @since 4.0.024 (2008-09-12)
  2534. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  2535. */
  2536. public function SetFillSpotColor($name, $tint=100) {
  2537. if (!isset($this->spot_colors[$name])) {
  2538. $this->Error('Undefined spot color: '.$name);
  2539. }
  2540. $this->FillColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2541. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2542. if ($this->page > 0) {
  2543. $this->_out($this->FillColor);
  2544. }
  2545. }
  2546. /**
  2547. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  2548. * The method can be called before the first page is created and the value is retained from page to page.
  2549. * @param array $color array of colors
  2550. * @access public
  2551. * @since 3.1.000 (2008-6-11)
  2552. * @see SetFillColor()
  2553. */
  2554. public function SetTextColorArray($color) {
  2555. if (isset($color)) {
  2556. $color = array_values($color);
  2557. $r = isset($color[0]) ? $color[0] : -1;
  2558. $g = isset($color[1]) ? $color[1] : -1;
  2559. $b = isset($color[2]) ? $color[2] : -1;
  2560. $k = isset($color[3]) ? $color[3] : -1;
  2561. if ($r >= 0) {
  2562. $this->SetTextColor($r, $g, $b, $k);
  2563. }
  2564. }
  2565. }
  2566. /**
  2567. * Defines the color used for text. It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.
  2568. * @param int $col1 Gray level for single color, or Red color for RGB, or Cyan color for CMYK. Value between 0 and 255
  2569. * @param int $col2 Green color for RGB, or Magenta color for CMYK. Value between 0 and 255
  2570. * @param int $col3 Blue color for RGB, or Yellow color for CMYK. Value between 0 and 255
  2571. * @param int $col4 Key (Black) color for CMYK. Value between 0 and 255
  2572. * @access public
  2573. * @since 1.3
  2574. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  2575. */
  2576. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1) {
  2577. // set default values
  2578. if (!is_numeric($col1)) {
  2579. $col1 = 0;
  2580. }
  2581. if (!is_numeric($col2)) {
  2582. $col2 = -1;
  2583. }
  2584. if (!is_numeric($col3)) {
  2585. $col3 = -1;
  2586. }
  2587. if (!is_numeric($col4)) {
  2588. $col4 = -1;
  2589. }
  2590. //Set color for text
  2591. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  2592. // Grey scale
  2593. $this->TextColor = sprintf('%.3F g', $col1/255);
  2594. $this->fgcolor = array('G' => $col1);
  2595. } elseif ($col4 == -1) {
  2596. // RGB
  2597. $this->TextColor = sprintf('%.3F %.3F %.3F rg', $col1/255, $col2/255, $col3/255);
  2598. $this->fgcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  2599. } else {
  2600. // CMYK
  2601. $this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $col1/100, $col2/100, $col3/100, $col4/100);
  2602. $this->fgcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  2603. }
  2604. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2605. }
  2606. /**
  2607. * Defines the spot color used for text.
  2608. * @param string $name name of the spot color
  2609. * @param int $tint the intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  2610. * @access public
  2611. * @since 4.0.024 (2008-09-12)
  2612. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  2613. */
  2614. public function SetTextSpotColor($name, $tint=100) {
  2615. if (!isset($this->spot_colors[$name])) {
  2616. $this->Error('Undefined spot color: '.$name);
  2617. }
  2618. $this->TextColor = sprintf('/CS%d cs %.3F scn', $this->spot_colors[$name]['i'], $tint/100);
  2619. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  2620. if ($this->page > 0) {
  2621. $this->_out($this->TextColor);
  2622. }
  2623. }
  2624. /**
  2625. * Returns the length of a string in user unit. A font must be selected.<br>
  2626. * @param string $s The string whose length is to be computed
  2627. * @param string $fontname Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
  2628. * @param string $fontstyle Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li></ul> or any combination. The default value is regular.
  2629. * @param float $fontsize Font size in points. The default value is the current size.
  2630. * @return int string length
  2631. * @author Nicola Asuni
  2632. * @access public
  2633. * @since 1.2
  2634. */
  2635. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0) {
  2636. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize);
  2637. }
  2638. /**
  2639. * Returns the string length of an array of chars in user unit. A font must be selected.<br>
  2640. * @param string $sa The array of chars whose total length is to be computed
  2641. * @param string $fontname Family font. It can be either a name defined by AddFont() or one of the standard families. It is also possible to pass an empty string, in that case, the current family is retained.
  2642. * @param string $fontstyle Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li></ul> or any combination. The default value is regular.
  2643. * @param float $fontsize Font size in points. The default value is the current size.
  2644. * @return int string length
  2645. * @author Nicola Asuni
  2646. * @access public
  2647. * @since 2.4.000 (2008-03-06)
  2648. */
  2649. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0) {
  2650. // store current values
  2651. if (!$this->empty_string($fontname)) {
  2652. $prev_FontFamily = $this->FontFamily;
  2653. $prev_FontStyle = $this->FontStyle;
  2654. $prev_FontSizePt = $this->FontSizePt;
  2655. $this->SetFont($fontname, $fontstyle, $fontsize);
  2656. }
  2657. $w = 0;
  2658. foreach ($sa as $char) {
  2659. $w += $this->GetCharWidth($char);
  2660. }
  2661. // restore previous values
  2662. if (!$this->empty_string($fontname)) {
  2663. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt);
  2664. }
  2665. return $w;
  2666. }
  2667. /**
  2668. * Returns the length of the char in user unit for the current font.<br>
  2669. * @param int $char The char code whose length is to be returned
  2670. * @return int char width
  2671. * @author Nicola Asuni
  2672. * @access public
  2673. * @since 2.4.000 (2008-03-06)
  2674. */
  2675. public function GetCharWidth($char) {
  2676. if ($char == 173) {
  2677. // SHY character will not be printed
  2678. return (0);
  2679. }
  2680. $cw = &$this->CurrentFont['cw'];
  2681. if (isset($cw[$char])) {
  2682. $w = $cw[$char];
  2683. } elseif (isset($this->CurrentFont['dw'])) {
  2684. // default width
  2685. $w = $this->CurrentFont['dw'];
  2686. } elseif (isset($cw[32])) {
  2687. // default width
  2688. $dw = $cw[32];
  2689. } else {
  2690. $w = 600;
  2691. }
  2692. return ($w * $this->FontSize / 1000);
  2693. }
  2694. /**
  2695. * Returns the numbero of characters in a string.
  2696. * @param string $s The input string.
  2697. * @return int number of characters
  2698. * @access public
  2699. * @since 2.0.0001 (2008-01-07)
  2700. */
  2701. public function GetNumChars($s) {
  2702. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  2703. return count($this->UTF8StringToArray($s));
  2704. }
  2705. return strlen($s);
  2706. }
  2707. /**
  2708. * Fill the list of available fonts ($this->fontlist).
  2709. * @access protected
  2710. * @since 4.0.013 (2008-07-28)
  2711. */
  2712. protected function getFontsList() {
  2713. $fontsdir = opendir($this->_getfontpath());
  2714. while (($file = readdir($fontsdir)) !== false) {
  2715. if (substr($file, -4) == '.php') {
  2716. array_push($this->fontlist, strtolower(basename($file, '.php')));
  2717. }
  2718. }
  2719. closedir($fontsdir);
  2720. }
  2721. /**
  2722. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  2723. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  2724. * The definition file (and the font file itself when embedding) must be present either in the current directory or in the one indicated by K_PATH_FONTS if the constant is defined. If it could not be found, the error "Could not include font definition file" is generated.
  2725. * @param string $family Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  2726. * @param string $style Font style. Possible values are (case insensitive):<ul><li>empty string: regular (default)</li><li>B: bold</li><li>I: italic</li><li>BI or IB: bold italic</li></ul>
  2727. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  2728. * @return array containing the font data, or false in case of error.
  2729. * @access public
  2730. * @since 1.5
  2731. * @see SetFont()
  2732. */
  2733. public function AddFont($family, $style='', $fontfile='') {
  2734. if ($this->empty_string($family)) {
  2735. if (!$this->empty_string($this->FontFamily)) {
  2736. $family = $this->FontFamily;
  2737. } else {
  2738. $this->Error('Empty font family');
  2739. }
  2740. }
  2741. $family = strtolower($family);
  2742. if ((!$this->isunicode) AND ($family == 'arial')) {
  2743. $family = 'helvetica';
  2744. }
  2745. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  2746. $style = '';
  2747. }
  2748. $tempstyle = strtoupper($style);
  2749. $style = '';
  2750. // underline
  2751. if (strpos($tempstyle, 'U') !== false) {
  2752. $this->underline = true;
  2753. } else {
  2754. $this->underline = false;
  2755. }
  2756. // line through (deleted)
  2757. if (strpos($tempstyle, 'D') !== false) {
  2758. $this->linethrough = true;
  2759. } else {
  2760. $this->linethrough = false;
  2761. }
  2762. // bold
  2763. if (strpos($tempstyle, 'B') !== false) {
  2764. $style .= 'B';
  2765. }
  2766. // oblique
  2767. if (strpos($tempstyle, 'I') !== false) {
  2768. $style .= 'I';
  2769. }
  2770. $bistyle = $style;
  2771. $fontkey = $family.$style;
  2772. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '');
  2773. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  2774. // check if the font has been already added
  2775. if ($this->getFontBuffer($fontkey) !== false) {
  2776. return $fontdata;
  2777. }
  2778. if (isset($type)) {
  2779. unset($type);
  2780. }
  2781. if (isset($cw)) {
  2782. unset($cw);
  2783. }
  2784. // get specified font directory (if any)
  2785. $fontdir = '';
  2786. if (!$this->empty_string($fontfile)) {
  2787. $fontdir = dirname($fontfile);
  2788. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  2789. $fontdir = '';
  2790. } else {
  2791. $fontdir .= '/';
  2792. }
  2793. }
  2794. // search and include font file
  2795. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  2796. // build a standard filenames for specified font
  2797. $fontfile1 = str_replace(' ', '', $family).strtolower($style).'.php';
  2798. $fontfile2 = str_replace(' ', '', $family).'.php';
  2799. // search files on various directories
  2800. if (file_exists($fontdir.$fontfile1)) {
  2801. $fontfile = $fontdir.$fontfile1;
  2802. } elseif (file_exists($this->_getfontpath().$fontfile1)) {
  2803. $fontfile = $this->_getfontpath().$fontfile1;
  2804. } elseif (file_exists($fontfile1)) {
  2805. $fontfile = $fontfile1;
  2806. } elseif (file_exists($fontdir.$fontfile2)) {
  2807. $fontfile = $fontdir.$fontfile2;
  2808. } elseif (file_exists($this->_getfontpath().$fontfile2)) {
  2809. $fontfile = $this->_getfontpath().$fontfile2;
  2810. } else {
  2811. $fontfile = $fontfile2;
  2812. }
  2813. }
  2814. // include font file
  2815. if (file_exists($fontfile)) {
  2816. include($fontfile);
  2817. } else {
  2818. $this->Error('Could not include font definition file: '.$family.'');
  2819. }
  2820. // check font parameters
  2821. if ((!isset($type)) OR (!isset($cw))) {
  2822. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  2823. }
  2824. // SET default parameters
  2825. if (!isset($file) OR $this->empty_string($file)) {
  2826. $file = '';
  2827. }
  2828. if (!isset($enc) OR $this->empty_string($enc)) {
  2829. $enc = '';
  2830. }
  2831. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  2832. $cidinfo = array('Registry'=>'Adobe','Ordering'=>'Identity','Supplement'=>0);
  2833. $cidinfo['uni2cid'] = array();
  2834. }
  2835. if (!isset($ctg) OR $this->empty_string($ctg)) {
  2836. $ctg = '';
  2837. }
  2838. if (!isset($desc) OR $this->empty_string($desc)) {
  2839. $desc = array();
  2840. }
  2841. if (!isset($up) OR $this->empty_string($up)) {
  2842. $up = -100;
  2843. }
  2844. if (!isset($ut) OR $this->empty_string($ut)) {
  2845. $ut = 50;
  2846. }
  2847. if (!isset($cw) OR $this->empty_string($cw)) {
  2848. $cw = array();
  2849. }
  2850. if (!isset($dw) OR $this->empty_string($dw)) {
  2851. // set default width
  2852. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  2853. $dw = $desc['MissingWidth'];
  2854. } elseif (isset($cw[32])) {
  2855. $dw = $cw[32];
  2856. } else {
  2857. $dw = 600;
  2858. }
  2859. }
  2860. ++$this->numfonts;
  2861. if ($type == 'cidfont0') {
  2862. // register CID font (all styles at once)
  2863. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  2864. $sname = $name.$styles[$bistyle];
  2865. if ((strpos($bistyle, 'B') !== false) AND (isset($desc['StemV'])) AND ($desc['StemV'] == 70)) {
  2866. $desc['StemV'] = 120;
  2867. }
  2868. } elseif ($type == 'core') {
  2869. $name = $this->CoreFonts[$fontkey];
  2870. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  2871. // ...
  2872. } elseif ($type == 'TrueTypeUnicode') {
  2873. $enc = 'Identity-H';
  2874. } else {
  2875. $this->Error('Unknow font type: '.$type.'');
  2876. }
  2877. $this->setFontBuffer($fontkey, array('i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg));
  2878. if (isset($diff) AND (!empty($diff))) {
  2879. //Search existing encodings
  2880. $d = 0;
  2881. $nb = count($this->diffs);
  2882. for ($i=1; $i <= $nb; ++$i) {
  2883. if ($this->diffs[$i] == $diff) {
  2884. $d = $i;
  2885. break;
  2886. }
  2887. }
  2888. if ($d == 0) {
  2889. $d = $nb + 1;
  2890. $this->diffs[$d] = $diff;
  2891. }
  2892. $this->setFontSubBuffer($fontkey, 'diff', $d);
  2893. }
  2894. if (!$this->empty_string($file)) {
  2895. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  2896. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir);
  2897. } elseif ($type != 'core') {
  2898. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir);
  2899. }
  2900. }
  2901. return $fontdata;
  2902. }
  2903. /**
  2904. * Sets the font used to print character strings.
  2905. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  2906. * The method can be called before the first page is created and the font is retained from page to page.
  2907. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  2908. * Note: for the standard fonts, the font metric files must be accessible. There are three possibilities for this:<ul><li>They are in the current directory (the one where the running script lies)</li><li>They are in one of the directories defined by the include_path parameter</li><li>They are in the directory defined by the K_PATH_FONTS constant</li></ul><br />
  2909. * @param string $family Family font. It can be either a name defined by AddFont() or one of the standard Type1 families (case insensitive):<ul><li>times (Times-Roman)</li><li>timesb (Times-Bold)</li><li>timesi (Times-Italic)</li><li>timesbi (Times-BoldItalic)</li><li>helvetica (Helvetica)</li><li>helveticab (Helvetica-Bold)</li><li>helveticai (Helvetica-Oblique)</li><li>helveticabi (Helvetica-BoldOblique)</li><li>courier (Courier)</li><li>courierb (Courier-Bold)</li><li>courieri (Courier-Oblique)</li><li>courierbi (Courier-BoldOblique)</li><li>symbol (Symbol)</li><li>zapfdingbats (ZapfDingbats)</li></ul> It is also possible to pass an empty string. In that case, the current family is retained.
  2910. * @param string $style Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li></ul> or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
  2911. * @param float $size Font size in points. The default value is the current size. If no size has been specified since the beginning of the document, the value taken is 12
  2912. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  2913. * @access public
  2914. * @since 1.0
  2915. * @see AddFont(), SetFontSize()
  2916. */
  2917. public function SetFont($family, $style='', $size=0, $fontfile='') {
  2918. //Select a font; size given in points
  2919. if ($size == 0) {
  2920. $size = $this->FontSizePt;
  2921. }
  2922. // try to add font (if not already added)
  2923. $fontdata = $this->AddFont($family, $style, $fontfile);
  2924. $this->FontFamily = $fontdata['family'];
  2925. $this->FontStyle = $fontdata['style'];
  2926. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  2927. $this->SetFontSize($size);
  2928. }
  2929. /**
  2930. * Defines the size of the current font.
  2931. * @param float $size The size (in points)
  2932. * @access public
  2933. * @since 1.0
  2934. * @see SetFont()
  2935. */
  2936. public function SetFontSize($size) {
  2937. //Set font size in points
  2938. $this->FontSizePt = $size;
  2939. $this->FontSize = $size / $this->k;
  2940. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  2941. $this->FontAscent = $this->CurrentFont['desc']['Ascent'] * $this->FontSize / 1000;
  2942. } else {
  2943. $this->FontAscent = 0.8 * $this->FontSize;
  2944. }
  2945. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] > 0)) {
  2946. $this->FontDescent = - $this->CurrentFont['desc']['Descent'] * $this->FontSize / 1000;
  2947. } else {
  2948. $this->FontDescent = 0.2 * $this->FontSize;
  2949. }
  2950. if (($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  2951. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  2952. }
  2953. }
  2954. /**
  2955. * Defines the default monospaced font.
  2956. * @param string $font Font name.
  2957. * @access public
  2958. * @since 4.5.025
  2959. */
  2960. public function SetDefaultMonospacedFont($font) {
  2961. $this->default_monospaced_font = $font;
  2962. }
  2963. /**
  2964. * Creates a new internal link and returns its identifier. An internal link is a clickable area which directs to another place within the document.<br />
  2965. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  2966. * @access public
  2967. * @since 1.5
  2968. * @see Cell(), Write(), Image(), Link(), SetLink()
  2969. */
  2970. public function AddLink() {
  2971. //Create a new internal link
  2972. $n = count($this->links) + 1;
  2973. $this->links[$n] = array(0, 0);
  2974. return $n;
  2975. }
  2976. /**
  2977. * Defines the page and position a link points to.
  2978. * @param int $link The link identifier returned by AddLink()
  2979. * @param float $y Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  2980. * @param int $page Number of target page; -1 indicates the current page. This is the default value
  2981. * @access public
  2982. * @since 1.5
  2983. * @see AddLink()
  2984. */
  2985. public function SetLink($link, $y=0, $page=-1) {
  2986. if ($y == -1) {
  2987. $y = $this->y;
  2988. }
  2989. if ($page == -1) {
  2990. $page = $this->page;
  2991. }
  2992. $this->links[$link] = array($page, $y);
  2993. }
  2994. /**
  2995. * Puts a link on a rectangular area of the page.
  2996. * Text or image links are generally put via Cell(), Write() or Image(), but this method can be useful for instance to define a clickable area inside an image.
  2997. * @param float $x Abscissa of the upper-left corner of the rectangle
  2998. * @param float $y Ordinate of the upper-left corner of the rectangle
  2999. * @param float $w Width of the rectangle
  3000. * @param float $h Height of the rectangle
  3001. * @param mixed $link URL or identifier returned by AddLink()
  3002. * @param int $spaces number of spaces on the text to link
  3003. * @access public
  3004. * @since 1.5
  3005. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  3006. */
  3007. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  3008. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  3009. }
  3010. /**
  3011. * Puts a markup annotation on a rectangular area of the page.
  3012. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  3013. * @param float $x Abscissa of the upper-left corner of the rectangle
  3014. * @param float $y Ordinate of the upper-left corner of the rectangle
  3015. * @param float $w Width of the rectangle
  3016. * @param float $h Height of the rectangle
  3017. * @param string $text annotation text or alternate content
  3018. * @param array $opt array of options (see section 8.4 of PDF reference 1.7).
  3019. * @param int $spaces number of spaces on the text to link
  3020. * @access public
  3021. * @since 4.0.018 (2008-08-06)
  3022. */
  3023. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  3024. // recalculate coordinates to account for graphic transformations
  3025. if (isset($this->transfmatrix)) {
  3026. $maxid = count($this->transfmatrix) - 1;
  3027. for ($i=$maxid; $i >= 0; $i--) {
  3028. $ctm = $this->transfmatrix[$i];
  3029. if (isset($ctm['a'])) {
  3030. $x = $x * $this->k;
  3031. $y = ($this->h - $y) * $this->k;
  3032. $w = $w * $this->k;
  3033. $h = $h * $this->k;
  3034. // top left
  3035. $xt = $x;
  3036. $yt = $y;
  3037. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3038. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3039. // top right
  3040. $xt = $x + $w;
  3041. $yt = $y;
  3042. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3043. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3044. // bottom left
  3045. $xt = $x;
  3046. $yt = $y - $h;
  3047. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3048. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3049. // bottom right
  3050. $xt = $x + $w;
  3051. $yt = $y - $h;
  3052. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  3053. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  3054. // new coordinates (rectangle area)
  3055. $x = min($x1, $x2, $x3, $x4);
  3056. $y = max($y1, $y2, $y3, $y4);
  3057. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  3058. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  3059. $x = $x / $this->k;
  3060. $y = $this->h - ($y / $this->k);
  3061. }
  3062. }
  3063. }
  3064. if ($this->page <= 0) {
  3065. $page = 1;
  3066. } else {
  3067. $page = $this->page;
  3068. }
  3069. $this->PageAnnots[$page][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  3070. if (($opt['Subtype'] == 'FileAttachment') AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  3071. $this->embeddedfiles[basename($opt['FS'])] = array('file' => $opt['FS'], 'n' => (count($this->embeddedfiles) + $this->embedded_start_obj_id));
  3072. }
  3073. }
  3074. /**
  3075. * Embedd the attached files.
  3076. * @since 4.4.000 (2008-12-07)
  3077. * @access protected
  3078. * @see Annotation()
  3079. */
  3080. protected function _putEmbeddedFiles() {
  3081. reset($this->embeddedfiles);
  3082. foreach ($this->embeddedfiles as $filename => $filedata) {
  3083. $data = file_get_contents($filedata['file']);
  3084. $filter = '';
  3085. if ($this->compress) {
  3086. $data = gzcompress($data);
  3087. $filter = ' /Filter /FlateDecode';
  3088. }
  3089. $this->offsets[$filedata['n']] = $this->bufferlen;
  3090. $this->_out($filedata['n'].' 0 obj');
  3091. $this->_out('<</Type /EmbeddedFile'.$filter.' /Length '.strlen($data).' >>');
  3092. $this->_putstream($data);
  3093. $this->_out('endobj');
  3094. }
  3095. }
  3096. /**
  3097. * Prints a character string.
  3098. * The origin is on the left of the first charcter, on the baseline.
  3099. * This method allows to place a string precisely on the page.
  3100. * @param float $x Abscissa of the origin
  3101. * @param float $y Ordinate of the origin
  3102. * @param string $txt String to print
  3103. * @param int $stroke outline size in points (0 = disable)
  3104. * @param boolean $clip if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  3105. * @access public
  3106. * @since 1.0
  3107. * @deprecated deprecated since version 4.3.005 (2008-11-25)
  3108. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  3109. */
  3110. public function Text($x, $y, $txt, $stroke=0, $clip=false) {
  3111. //Output a string
  3112. if ($this->rtl) {
  3113. // bidirectional algorithm (some chars may be changed affecting the line length)
  3114. $s = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  3115. $l = $this->GetArrStringWidth($s);
  3116. $xr = $this->w - $x - $this->GetArrStringWidth($s);
  3117. } else {
  3118. $xr = $x;
  3119. }
  3120. $opt = '';
  3121. if (($stroke > 0) AND (!$clip)) {
  3122. $opt .= '1 Tr '.intval($stroke).' w ';
  3123. } elseif (($stroke > 0) AND $clip) {
  3124. $opt .= '5 Tr '.intval($stroke).' w ';
  3125. } elseif ($clip) {
  3126. $opt .= '7 Tr ';
  3127. }
  3128. $s = sprintf('BT %.2F %.2F Td %s(%s) Tj ET 0 Tr', $xr * $this->k, ($this->h-$y) * $this->k, $opt, $this->_escapetext($txt));
  3129. if ($this->underline AND ($txt!='')) {
  3130. $s .= ' '.$this->_dounderline($xr, $y, $txt);
  3131. }
  3132. if ($this->linethrough AND ($txt!='')) {
  3133. $s .= ' '.$this->_dolinethrough($xr, $y, $txt);
  3134. }
  3135. if ($this->ColorFlag AND (!$clip)) {
  3136. $s='q '.$this->TextColor.' '.$s.' Q';
  3137. }
  3138. $this->_out($s);
  3139. }
  3140. /**
  3141. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  3142. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  3143. * This method is called automatically and should not be called directly by the application.
  3144. * @return boolean
  3145. * @access public
  3146. * @since 1.4
  3147. * @see SetAutoPageBreak()
  3148. */
  3149. public function AcceptPageBreak() {
  3150. return $this->AutoPageBreak;
  3151. }
  3152. /**
  3153. * Add page if needed.
  3154. * @param float $h Cell height. Default value: 0.
  3155. * @param mixed $y starting y position, leave empty for current position.
  3156. * @param boolean $addpage if true add a page, otherwise only return the true/false state
  3157. * @return boolean true in case of page break, false otherwise.
  3158. * @since 3.2.000 (2008-07-01)
  3159. * @access protected
  3160. */
  3161. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  3162. if ($this->empty_string($y)) {
  3163. $y = $this->y;
  3164. }
  3165. if ((($y + $h) > $this->PageBreakTrigger) AND (!$this->InFooter) AND ($this->AcceptPageBreak())) {
  3166. if ($addpage) {
  3167. //Automatic page break
  3168. $x = $this->x;
  3169. $this->AddPage($this->CurOrientation);
  3170. $this->y = $this->tMargin;
  3171. $oldpage = $this->page - 1;
  3172. if ($this->rtl) {
  3173. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  3174. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  3175. } else {
  3176. $this->x = $x;
  3177. }
  3178. } else {
  3179. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3180. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  3181. } else {
  3182. $this->x = $x;
  3183. }
  3184. }
  3185. }
  3186. return true;
  3187. }
  3188. return false;
  3189. }
  3190. /**
  3191. * Prints a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
  3192. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3193. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3194. * @param float $h Cell height. Default value: 0.
  3195. * @param string $txt String to print. Default value: empty string.
  3196. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  3197. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>
  3198. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3199. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  3200. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3201. * @param mixed $link URL or identifier returned by AddLink().
  3202. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  3203. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3204. * @access public
  3205. * @since 1.0
  3206. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  3207. */
  3208. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false) {
  3209. //$min_cell_height = $this->FontAscent + $this->FontDescent;
  3210. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3211. if ($h < $min_cell_height) {
  3212. $h = $min_cell_height;
  3213. }
  3214. $this->checkPageBreak($h);
  3215. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height));
  3216. }
  3217. /**
  3218. * Removes SHY characters from text.
  3219. * @param string $txt input string
  3220. * @return string without SHY characters.
  3221. * @access public
  3222. * @since (4.5.019) 2009-02-28
  3223. */
  3224. public function removeSHY($txt='') {
  3225. /*
  3226. * Unicode Data
  3227. * Name : SOFT HYPHEN, commonly abbreviated as SHY
  3228. * HTML Entity (decimal): &#173;
  3229. * HTML Entity (hex): &#xad;
  3230. * HTML Entity (named): &shy;
  3231. * How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]
  3232. * UTF-8 (hex): 0xC2 0xAD (c2ad)
  3233. * UTF-8 character: chr(194).chr(173)
  3234. */
  3235. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  3236. if (!$this->isunicode) {
  3237. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  3238. }
  3239. return $txt;
  3240. }
  3241. /**
  3242. * Returns the PDF string code to print a cell (rectangular area) with optional borders, background color and character string. The upper-left corner of the cell corresponds to the current position. The text can be aligned or centered. After the call, the current position moves to the right or to the next line. It is possible to put a link on the text.<br />
  3243. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  3244. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  3245. * @param float $h Cell height. Default value: 0.
  3246. * @param string $txt String to print. Default value: empty string.
  3247. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  3248. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL languages)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  3249. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  3250. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3251. * @param mixed $link URL or identifier returned by AddLink().
  3252. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  3253. * @param boolean $ignore_min_height if true ignore automatic minimum height value.
  3254. * @access protected
  3255. * @since 1.0
  3256. * @see Cell()
  3257. */
  3258. protected function getCellCode($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0, $ignore_min_height=false) {
  3259. $txt = $this->removeSHY($txt);
  3260. $rs = ''; //string to be returned
  3261. if (!$ignore_min_height) {
  3262. $min_cell_height = $this->FontSize * $this->cell_height_ratio;
  3263. if ($h < $min_cell_height) {
  3264. $h = $min_cell_height;
  3265. }
  3266. }
  3267. $k = $this->k;
  3268. if ($this->empty_string($w) OR ($w <= 0)) {
  3269. if ($this->rtl) {
  3270. $w = $this->x - $this->lMargin;
  3271. } else {
  3272. $w = $this->w - $this->rMargin - $this->x;
  3273. }
  3274. }
  3275. $s = '';
  3276. if (($fill == 1) OR ($border == 1)) {
  3277. if ($fill == 1) {
  3278. $op = ($border == 1) ? 'B' : 'f';
  3279. } else {
  3280. $op = 'S';
  3281. }
  3282. if ($this->rtl) {
  3283. $xk = (($this->x - $w) * $k);
  3284. } else {
  3285. $xk = ($this->x * $k);
  3286. }
  3287. $s .= sprintf('%.2F %.2F %.2F %.2F re %s ', $xk, (($this->h - $this->y) * $k), ($w * $k), (-$h * $k), $op);
  3288. }
  3289. if (is_string($border)) {
  3290. $lm = ($this->LineWidth / 2);
  3291. $x = $this->x;
  3292. $y = $this->y;
  3293. if (strpos($border,'L') !== false) {
  3294. if ($this->rtl) {
  3295. $xk = ($x - $w) * $k;
  3296. } else {
  3297. $xk = $x * $k;
  3298. }
  3299. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm)) * $k));
  3300. }
  3301. if (strpos($border,'T') !== false) {
  3302. if ($this->rtl) {
  3303. $xk = ($x - $w + $lm) * $k;
  3304. $xwk = ($x - $lm) * $k;
  3305. } else {
  3306. $xk = ($x - $lm) * $k;
  3307. $xwk = ($x + $w + $lm) * $k;
  3308. }
  3309. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y) * $k), $xwk, (($this->h - $y) * $k));
  3310. }
  3311. if (strpos($border,'R') !== false) {
  3312. if ($this->rtl) {
  3313. $xk = $x * $k;
  3314. } else {
  3315. $xk = ($x + $w) * $k;
  3316. }
  3317. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - $y + $lm) * $k), $xk, (($this->h - ($y + $h + $lm))* $k));
  3318. }
  3319. if (strpos($border,'B') !== false) {
  3320. if ($this->rtl) {
  3321. $xk = ($x - $w + $lm) * $k;
  3322. $xwk = ($x - $lm) * $k;
  3323. } else {
  3324. $xk = ($x - $lm) * $k;
  3325. $xwk = ($x + $w + $lm) * $k;
  3326. }
  3327. $s .= sprintf('%.2F %.2F m %.2F %.2F l S ', $xk, (($this->h - ($y + $h)) * $k), $xwk, (($this->h - ($y + $h)) * $k));
  3328. }
  3329. }
  3330. if ($txt != '') {
  3331. // text lenght
  3332. $width = $this->GetStringWidth($txt);
  3333. // ratio between cell lenght and text lenght
  3334. $ratio = ($w - (2 * $this->cMargin)) / $width;
  3335. // stretch text if required
  3336. if (($stretch > 0) AND (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0)))) {
  3337. if ($stretch > 2) {
  3338. // spacing
  3339. //Calculate character spacing in points
  3340. $char_space = (($w - $width - (2 * $this->cMargin)) * $this->k) / max($this->GetNumChars($txt)-1,1);
  3341. //Set character spacing
  3342. $rs .= sprintf('BT %.2F Tc ET ', $char_space);
  3343. } else {
  3344. // scaling
  3345. //Calculate horizontal scaling
  3346. $horiz_scale = $ratio * 100.0;
  3347. //Set horizontal scaling
  3348. $rs .= sprintf('BT %.2F Tz ET ', $horiz_scale);
  3349. }
  3350. $align = '';
  3351. $width = $w - (2 * $this->cMargin);
  3352. } else {
  3353. $stretch == 0;
  3354. }
  3355. if ($align == 'L') {
  3356. if ($this->rtl) {
  3357. $dx = $w - $width - $this->cMargin;
  3358. } else {
  3359. $dx = $this->cMargin;
  3360. }
  3361. } elseif ($align == 'R') {
  3362. if ($this->rtl) {
  3363. $dx = $this->cMargin;
  3364. } else {
  3365. $dx = $w - $width - $this->cMargin;
  3366. }
  3367. } elseif ($align == 'C') {
  3368. $dx = ($w - $width) / 2;
  3369. } elseif ($align == 'J') {
  3370. if ($this->rtl) {
  3371. $dx = $w - $width - $this->cMargin;
  3372. } else {
  3373. $dx = $this->cMargin;
  3374. }
  3375. } else {
  3376. $dx = $this->cMargin;
  3377. }
  3378. if ($this->ColorFlag) {
  3379. $s .= 'q '.$this->TextColor.' ';
  3380. }
  3381. $txt2 = $this->_escapetext($txt);
  3382. if ($this->rtl) {
  3383. $xdk = ($this->x - $dx - $width) * $k;
  3384. } else {
  3385. $xdk = ($this->x + $dx) * $k;
  3386. }
  3387. // Justification
  3388. if ($align == 'J') {
  3389. // count number of spaces
  3390. $ns = substr_count($txt, ' ');
  3391. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  3392. // get string width without spaces
  3393. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  3394. // calculate average space width
  3395. $spacewidth = -1000 * ($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1) / $this->FontSize;
  3396. // set word position to be used with TJ operator
  3397. $txt2 = str_replace(chr(0).' ', ') '.($spacewidth).' (', $txt2);
  3398. } else {
  3399. // get string width
  3400. $width = $this->GetStringWidth($txt);
  3401. $spacewidth = (($w - $width - (2 * $this->cMargin)) / ($ns?$ns:1)) * $this->k;
  3402. $rs .= sprintf('BT %.3F Tw ET ', $spacewidth);
  3403. }
  3404. }
  3405. // calculate approximate position of the font base line
  3406. //$basefonty = $this->y + (($h + $this->FontAscent - $this->FontDescent)/2);
  3407. $basefonty = $this->y + ($h/2) + ($this->FontSize/3);
  3408. // print text
  3409. $s .= sprintf('BT %.2F %.2F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  3410. if ($this->rtl) {
  3411. $xdx = $this->x - $dx - $width;
  3412. } else {
  3413. $xdx = $this->x + $dx;
  3414. }
  3415. if ($this->underline) {
  3416. $s .= ' '.$this->_dounderline($xdx, $basefonty, $txt);
  3417. }
  3418. if ($this->linethrough) {
  3419. $s .= ' '.$this->_dolinethrough($xdx, $basefonty, $txt);
  3420. }
  3421. if ($this->ColorFlag) {
  3422. $s .= ' Q';
  3423. }
  3424. if ($link) {
  3425. $this->Link($xdx, $this->y + (($h - $this->FontSize)/2), $width, $this->FontSize, $link, substr_count($txt, chr(32)));
  3426. }
  3427. }
  3428. // output cell
  3429. if ($s) {
  3430. // output cell
  3431. $rs .= $s;
  3432. // reset text stretching
  3433. if ($stretch > 2) {
  3434. //Reset character horizontal spacing
  3435. $rs .= ' BT 0 Tc ET';
  3436. } elseif ($stretch > 0) {
  3437. //Reset character horizontal scaling
  3438. $rs .= ' BT 100 Tz ET';
  3439. }
  3440. }
  3441. // reset word spacing
  3442. if (!(($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($align == 'J')) {
  3443. $rs .= ' BT 0 Tw ET';
  3444. }
  3445. $this->lasth = $h;
  3446. if ($ln > 0) {
  3447. //Go to the beginning of the next line
  3448. $this->y += $h;
  3449. if ($ln == 1) {
  3450. if ($this->rtl) {
  3451. $this->x = $this->w - $this->rMargin;
  3452. } else {
  3453. $this->x = $this->lMargin;
  3454. }
  3455. }
  3456. } else {
  3457. // go left or right by case
  3458. if ($this->rtl) {
  3459. $this->x -= $w;
  3460. } else {
  3461. $this->x += $w;
  3462. }
  3463. }
  3464. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  3465. $rs = $gstyles.$rs;
  3466. return $rs;
  3467. }
  3468. /**
  3469. * This method allows printing text with line breaks.
  3470. * They can be automatic (as soon as the text reaches the right border of the cell) or explicit (via the \n character). As many cells as necessary are output, one below the other.<br />
  3471. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  3472. * @param float $w Width of cells. If 0, they extend up to the right margin of the page.
  3473. * @param float $h Cell minimum height. The cell extends automatically if needed.
  3474. * @param string $txt String to print
  3475. * @param mixed $border Indicates if borders must be drawn around the cell block. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  3476. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align</li><li>C: center</li><li>R: right align</li><li>J: justification (default value when $ishtml=false)</li></ul>
  3477. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  3478. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right</li><li>1: to the beginning of the next line [DEFAULT]</li><li>2: below</li></ul>
  3479. * @param float $x x position in user units
  3480. * @param float $y y position in user units
  3481. * @param boolean $reseth if true reset the last cell height (default true).
  3482. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  3483. * @param boolean $ishtml set to true if $txt is HTML content (default = false).
  3484. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  3485. * @param float $maxh maximum height. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. This feature works only when $ishtml=false.
  3486. * @return int Return the number of cells or 1 for html mode.
  3487. * @access public
  3488. * @since 1.3
  3489. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  3490. */
  3491. public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0) {
  3492. if ($this->empty_string($this->lasth) OR $reseth) {
  3493. //set row height
  3494. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  3495. }
  3496. if (!$this->empty_string($y)) {
  3497. $this->SetY($y);
  3498. } else {
  3499. $y = $this->GetY();
  3500. }
  3501. // check for page break
  3502. $this->checkPageBreak($h);
  3503. $y = $this->GetY();
  3504. // get current page number
  3505. $startpage = $this->page;
  3506. if (!$this->empty_string($x)) {
  3507. $this->SetX($x);
  3508. } else {
  3509. $x = $this->GetX();
  3510. }
  3511. if ($this->empty_string($w) OR ($w <= 0)) {
  3512. if ($this->rtl) {
  3513. $w = $this->x - $this->lMargin;
  3514. } else {
  3515. $w = $this->w - $this->rMargin - $this->x;
  3516. }
  3517. }
  3518. // store original margin values
  3519. $lMargin = $this->lMargin;
  3520. $rMargin = $this->rMargin;
  3521. if ($this->rtl) {
  3522. $this->SetRightMargin($this->w - $this->x);
  3523. $this->SetLeftMargin($this->x - $w);
  3524. } else {
  3525. $this->SetLeftMargin($this->x);
  3526. $this->SetRightMargin($this->w - $this->x - $w);
  3527. }
  3528. $starty = $this->y;
  3529. if ($autopadding) {
  3530. // Adjust internal padding
  3531. if ($this->cMargin < ($this->LineWidth / 2)) {
  3532. $this->cMargin = ($this->LineWidth / 2);
  3533. }
  3534. // Add top space if needed
  3535. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  3536. $this->y += $this->LineWidth / 2;
  3537. }
  3538. // add top padding
  3539. $this->y += $this->cMargin;
  3540. }
  3541. if ($ishtml) {
  3542. // ******* Write HTML text
  3543. $this->writeHTML($txt, true, 0, $reseth, true, $align);
  3544. $nl = 1;
  3545. } else {
  3546. // ******* Write text
  3547. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, false, $maxh);
  3548. }
  3549. if ($autopadding) {
  3550. // add bottom padding
  3551. $this->y += $this->cMargin;
  3552. // Add bottom space if needed
  3553. if (($this->lasth - $this->FontSize) < $this->LineWidth) {
  3554. $this->y += $this->LineWidth / 2;
  3555. }
  3556. }
  3557. // Get end-of-text Y position
  3558. $currentY = $this->y;
  3559. // get latest page number
  3560. $endpage = $this->page;
  3561. // check if a new page has been created
  3562. if ($endpage > $startpage) {
  3563. // design borders around HTML cells.
  3564. for ($page=$startpage; $page <= $endpage; ++$page) {
  3565. $this->setPage($page);
  3566. if ($page == $startpage) {
  3567. $this->y = $starty; // put cursor at the beginning of cell on the first page
  3568. $h = $this->getPageHeight() - $starty - $this->getBreakMargin();
  3569. $cborder = $this->getBorderMode($border, $position='start');
  3570. } elseif ($page == $endpage) {
  3571. $this->y = $this->tMargin; // put cursor at the beginning of last page
  3572. $h = $currentY - $this->tMargin;
  3573. $cborder = $this->getBorderMode($border, $position='end');
  3574. } else {
  3575. $this->y = $this->tMargin; // put cursor at the beginning of the current page
  3576. $h = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  3577. $cborder = $this->getBorderMode($border, $position='middle');
  3578. }
  3579. $nx = $x;
  3580. // account for margin changes
  3581. if ($page > $startpage) {
  3582. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  3583. $nx = $x + ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  3584. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  3585. $nx = $x + ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  3586. }
  3587. }
  3588. $this->SetX($nx);
  3589. $ccode = $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, false);
  3590. if ($cborder OR $fill) {
  3591. $pagebuff = $this->getPageBuffer($this->page);
  3592. $pstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  3593. $pend = substr($pagebuff, $this->intmrk[$this->page]);
  3594. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  3595. $this->intmrk[$this->page] += strlen($ccode."\n");
  3596. }
  3597. }
  3598. } else {
  3599. $h = max($h, ($currentY - $y));
  3600. // put cursor at the beginning of text
  3601. $this->SetY($y);
  3602. $this->SetX($x);
  3603. // design a cell around the text
  3604. $ccode = $this->getCellCode($w, $h, '', $border, 1, '', $fill, '', 0, true);
  3605. if ($border OR $fill) {
  3606. if (end($this->transfmrk[$this->page]) !== false) {
  3607. $pagemarkkey = key($this->transfmrk[$this->page]);
  3608. $pagemark = &$this->transfmrk[$this->page][$pagemarkkey];
  3609. } elseif ($this->InFooter) {
  3610. $pagemark = &$this->footerpos[$this->page];
  3611. } else {
  3612. $pagemark = &$this->intmrk[$this->page];
  3613. }
  3614. $pagebuff = $this->getPageBuffer($this->page);
  3615. $pstart = substr($pagebuff, 0, $pagemark);
  3616. $pend = substr($pagebuff, $pagemark);
  3617. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  3618. $pagemark += strlen($ccode."\n");
  3619. }
  3620. }
  3621. // Get end-of-cell Y position
  3622. $currentY = $this->GetY();
  3623. // restore original margin values
  3624. $this->SetLeftMargin($lMargin);
  3625. $this->SetRightMargin($rMargin);
  3626. if ($ln > 0) {
  3627. //Go to the beginning of the next line
  3628. $this->SetY($currentY);
  3629. if ($ln == 2) {
  3630. $this->SetX($x + $w);
  3631. }
  3632. } else {
  3633. // go left or right by case
  3634. $this->setPage($startpage);
  3635. $this->y = $y;
  3636. $this->SetX($x + $w);
  3637. }
  3638. $this->setContentMark();
  3639. return $nl;
  3640. }
  3641. /**
  3642. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  3643. * @param mixed $border Indicates if borders must be drawn around the cell block. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  3644. * @param string multicell position: 'start', 'middle', 'end'
  3645. * @return border mode
  3646. * @access protected
  3647. * @since 4.4.002 (2008-12-09)
  3648. */
  3649. protected function getBorderMode($border, $position='start') {
  3650. if ((!$this->opencell) AND ($border == 1)) {
  3651. return 1;
  3652. }
  3653. $cborder = '';
  3654. switch ($position) {
  3655. case 'start': {
  3656. if ($border == 1) {
  3657. $cborder = 'LTR';
  3658. } else {
  3659. if (!(false === strpos($border, 'L'))) {
  3660. $cborder .= 'L';
  3661. }
  3662. if (!(false === strpos($border, 'T'))) {
  3663. $cborder .= 'T';
  3664. }
  3665. if (!(false === strpos($border, 'R'))) {
  3666. $cborder .= 'R';
  3667. }
  3668. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  3669. $cborder .= 'B';
  3670. }
  3671. }
  3672. break;
  3673. }
  3674. case 'middle': {
  3675. if ($border == 1) {
  3676. $cborder = 'LR';
  3677. } else {
  3678. if (!(false === strpos($border, 'L'))) {
  3679. $cborder .= 'L';
  3680. }
  3681. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  3682. $cborder .= 'T';
  3683. }
  3684. if (!(false === strpos($border, 'R'))) {
  3685. $cborder .= 'R';
  3686. }
  3687. if ((!$this->opencell) AND (!(false === strpos($border, 'B')))) {
  3688. $cborder .= 'B';
  3689. }
  3690. }
  3691. break;
  3692. }
  3693. case 'end': {
  3694. if ($border == 1) {
  3695. $cborder = 'LRB';
  3696. } else {
  3697. if (!(false === strpos($border, 'L'))) {
  3698. $cborder .= 'L';
  3699. }
  3700. if ((!$this->opencell) AND (!(false === strpos($border, 'T')))) {
  3701. $cborder .= 'T';
  3702. }
  3703. if (!(false === strpos($border, 'R'))) {
  3704. $cborder .= 'R';
  3705. }
  3706. if (!(false === strpos($border, 'B'))) {
  3707. $cborder .= 'B';
  3708. }
  3709. }
  3710. break;
  3711. }
  3712. default: {
  3713. $cborder = $border;
  3714. break;
  3715. }
  3716. }
  3717. return $cborder;
  3718. }
  3719. /**
  3720. * This method returns the estimated number of lines required to print the text.
  3721. * @param string $txt text to print
  3722. * @param float $w width of cell. If 0, they extend up to the right margin of the page.
  3723. * @return int Return the estimated number of lines.
  3724. * @access public
  3725. * @since 4.5.011
  3726. */
  3727. public function getNumLines($txt, $w=0) {
  3728. $lines = 0;
  3729. if ($this->empty_string($w) OR ($w <= 0)) {
  3730. if ($this->rtl) {
  3731. $w = $this->x - $this->lMargin;
  3732. } else {
  3733. $w = $this->w - $this->rMargin - $this->x;
  3734. }
  3735. }
  3736. // max column width
  3737. $wmax = $w - (2 * $this->cMargin);
  3738. // remove carriage returns
  3739. $txt = str_replace("\r", '', $txt);
  3740. // remove last newline (if any)
  3741. if (substr($txt,-1) == "\n") {
  3742. $txt = substr($txt, 0, -1);
  3743. }
  3744. // divide text in blocks
  3745. $txtblocks = explode("\n", $txt);
  3746. // for each block;
  3747. foreach ($txtblocks as $block) {
  3748. // estimate the number of lines
  3749. $lines += $this->empty_string($block) ? 1 : (ceil($this->GetStringWidth($block) / $wmax));
  3750. }
  3751. return $lines;
  3752. }
  3753. /**
  3754. * This method prints text from the current position.<br />
  3755. * @param float $h Line height
  3756. * @param string $txt String to print
  3757. * @param mixed $link URL or identifier returned by AddLink()
  3758. * @param int $fill Indicates if the background must be painted (1) or transparent (0). Default value: 0.
  3759. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L or empty string: left align (default value)</li><li>C: center</li><li>R: right align</li><li>J: justify</li></ul>
  3760. * @param boolean $ln if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  3761. * @param int $stretch stretch carachter mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if necessary</li><li>2 = forced horizontal scaling</li><li>3 = character spacing only if necessary</li><li>4 = forced character spacing</li></ul>
  3762. * @param boolean $firstline if true prints only the first line and return the remaining string.
  3763. * @param boolean $firstblock if true the string is the starting of a line.
  3764. * @param float $maxh maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature.
  3765. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  3766. * @access public
  3767. * @since 1.5
  3768. */
  3769. public function Write($h, $txt, $link='', $fill=0, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0) {
  3770. if (strlen($txt) == 0) {
  3771. $txt = ' ';
  3772. }
  3773. // remove carriage returns
  3774. $s = str_replace("\r", '', $txt);
  3775. // check if string contains arabic text
  3776. if (preg_match(K_RE_PATTERN_ARABIC, $s)) {
  3777. $arabic = true;
  3778. } else {
  3779. $arabic = false;
  3780. }
  3781. // check if string contains RTL text
  3782. if ($arabic OR $this->tmprtl OR preg_match(K_RE_PATTERN_RTL, $txt)) {
  3783. $rtlmode = true;
  3784. } else {
  3785. $rtlmode = false;
  3786. }
  3787. // get a char width
  3788. $chrwidth = $this->GetCharWidth('.');
  3789. // get array of unicode values
  3790. $chars = $this->UTF8StringToArray($s);
  3791. // get array of chars
  3792. $uchars = $this->UTF8ArrayToUniArray($chars);
  3793. // get the number of characters
  3794. $nb = count($chars);
  3795. // replacement for SHY character (minus symbol)
  3796. $shy_replacement = 45;
  3797. $shy_replacement_char = $this->unichr($shy_replacement);
  3798. // widht for SHY replacement
  3799. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  3800. // store current position
  3801. $prevx = $this->x;
  3802. $prevy = $this->y;
  3803. // max Y
  3804. $maxy = $this->y + $maxh - $h - (2 * $this->cMargin);
  3805. // calculate remaining line width ($w)
  3806. if ($this->rtl) {
  3807. $w = $this->x - $this->lMargin;
  3808. } else {
  3809. $w = $this->w - $this->rMargin - $this->x;
  3810. }
  3811. // max column width
  3812. $wmax = $w - (2 * $this->cMargin);
  3813. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  3814. // a single character do not fit on column
  3815. return '';
  3816. }
  3817. $i = 0; // character position
  3818. $j = 0; // current starting position
  3819. $sep = -1; // position of the last blank space
  3820. $shy = false; // true if the last blank is a soft hypen (SHY)
  3821. $l = 0; // current string lenght
  3822. $nl = 0; //number of lines
  3823. $linebreak = false;
  3824. // for each character
  3825. while ($i < $nb) {
  3826. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  3827. $firstline = true;
  3828. }
  3829. //Get the current character
  3830. $c = $chars[$i];
  3831. if ($c == 10) { // 10 = "\n" = new line
  3832. //Explicit line break
  3833. if ($align == 'J') {
  3834. if ($this->rtl) {
  3835. $talign = 'R';
  3836. } else {
  3837. $talign = 'L';
  3838. }
  3839. } else {
  3840. $talign = $align;
  3841. }
  3842. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  3843. if ($firstline) {
  3844. $startx = $this->x;
  3845. $tmparr = array_slice($chars, $j, $i);
  3846. if ($rtlmode) {
  3847. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3848. }
  3849. $linew = $this->GetArrStringWidth($tmparr);
  3850. unset($tmparr);
  3851. if ($this->rtl) {
  3852. $this->endlinex = $startx - $linew;
  3853. } else {
  3854. $this->endlinex = $startx + $linew;
  3855. }
  3856. $w = $linew;
  3857. $tmpcmargin = $this->cMargin;
  3858. if ($maxh == 0) {
  3859. $this->cMargin = 0;
  3860. }
  3861. }
  3862. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  3863. unset($tmpstr);
  3864. if ($firstline) {
  3865. $this->cMargin = $tmpcmargin;
  3866. return ($this->UniArrSubString($uchars, $i));
  3867. }
  3868. ++$nl;
  3869. $j = $i + 1;
  3870. $l = 0;
  3871. $sep = -1;
  3872. $shy = false;
  3873. // account for margin changes
  3874. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  3875. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  3876. $this->AcceptPageBreak();
  3877. }
  3878. $w = $this->getRemainingWidth();
  3879. $wmax = $w - (2 * $this->cMargin);
  3880. } else {
  3881. // 160 is the non-breaking space.
  3882. // 173 is SHY (Soft Hypen).
  3883. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  3884. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  3885. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  3886. if (($c != 160) AND (($c == 173) OR preg_match($this->re_spaces, $this->unichr($c)))) {
  3887. // update last blank space position
  3888. $sep = $i;
  3889. // check if is a SHY
  3890. if ($c == 173) {
  3891. $shy = true;
  3892. } else {
  3893. $shy = false;
  3894. }
  3895. }
  3896. // update string length
  3897. if ((($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) AND ($arabic)) {
  3898. // with bidirectional algorithm some chars may be changed affecting the line length
  3899. // *** very slow ***
  3900. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, $i-$j+1), '', $this->tmprtl));
  3901. } else {
  3902. $l += $this->GetCharWidth($c);
  3903. }
  3904. if (($l > $wmax) OR ($shy AND (($l + $shy_replacement_width) > $wmax)) ) {
  3905. // we have reached the end of column
  3906. if ($sep == -1) {
  3907. // check if the line was already started
  3908. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $chrwidth)))
  3909. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $chrwidth)))) {
  3910. // print a void cell and go to next line
  3911. $this->Cell($w, $h, '', 0, 1);
  3912. $linebreak = true;
  3913. if ($firstline) {
  3914. return ($this->UniArrSubString($uchars, $j));
  3915. }
  3916. } else {
  3917. // truncate the word because do not fit on column
  3918. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  3919. if ($firstline) {
  3920. $startx = $this->x;
  3921. $tmparr = array_slice($chars, $j, $i);
  3922. if ($rtlmode) {
  3923. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3924. }
  3925. $linew = $this->GetArrStringWidth($tmparr);
  3926. unset($tmparr);
  3927. if ($this->rtl) {
  3928. $this->endlinex = $startx - $linew;
  3929. } else {
  3930. $this->endlinex = $startx + $linew;
  3931. }
  3932. $w = $linew;
  3933. $tmpcmargin = $this->cMargin;
  3934. if ($maxh == 0) {
  3935. $this->cMargin = 0;
  3936. }
  3937. }
  3938. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  3939. unset($tmpstr);
  3940. if ($firstline) {
  3941. $this->cMargin = $tmpcmargin;
  3942. return ($this->UniArrSubString($uchars, $i));
  3943. }
  3944. $j = $i;
  3945. --$i;
  3946. }
  3947. } else {
  3948. // word wrapping
  3949. if ($this->rtl AND (!$firstblock)) {
  3950. $endspace = 1;
  3951. } else {
  3952. $endspace = 0;
  3953. }
  3954. if ($shy) {
  3955. // add hypen (minus symbol) at the end of the line
  3956. $shy_width = $shy_replacement_width;
  3957. if ($this->rtl) {
  3958. $shy_char_left = $shy_replacement_char;
  3959. $shy_char_right = '';
  3960. } else {
  3961. $shy_char_left = '';
  3962. $shy_char_right = $shy_replacement_char;
  3963. }
  3964. } else {
  3965. $shy_width = 0;
  3966. $shy_char_left = '';
  3967. $shy_char_right = '';
  3968. }
  3969. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  3970. if ($firstline) {
  3971. $startx = $this->x;
  3972. $tmparr = array_slice($chars, $j, ($sep + $endspace));
  3973. if ($rtlmode) {
  3974. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  3975. }
  3976. $linew = $this->GetArrStringWidth($tmparr);
  3977. unset($tmparr);
  3978. if ($this->rtl) {
  3979. $this->endlinex = $startx - $linew - $shy_width;
  3980. } else {
  3981. $this->endlinex = $startx + $linew + $shy_width;
  3982. }
  3983. $w = $linew;
  3984. $tmpcmargin = $this->cMargin;
  3985. if ($maxh == 0) {
  3986. $this->cMargin = 0;
  3987. }
  3988. }
  3989. // print the line
  3990. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  3991. unset($tmpstr);
  3992. if ($firstline) {
  3993. // return the remaining text
  3994. $this->cMargin = $tmpcmargin;
  3995. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  3996. }
  3997. $i = $sep;
  3998. $sep = -1;
  3999. $shy = false;
  4000. $j = ($i+1);
  4001. }
  4002. // account for margin changes
  4003. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND (!$this->InFooter)) {
  4004. // AcceptPageBreak() may be overriden on extended classed to include margin changes
  4005. $this->AcceptPageBreak();
  4006. }
  4007. $w = $this->getRemainingWidth();
  4008. $wmax = $w - (2 * $this->cMargin);
  4009. if ($linebreak) {
  4010. $linebreak = false;
  4011. } else {
  4012. ++$nl;
  4013. $l = 0;
  4014. }
  4015. }
  4016. }
  4017. ++$i;
  4018. } // end while i < nb
  4019. // print last substring (if any)
  4020. if ($l > 0) {
  4021. switch ($align) {
  4022. case 'J':
  4023. case 'C': {
  4024. $w = $w;
  4025. break;
  4026. }
  4027. case 'L': {
  4028. if ($this->rtl) {
  4029. $w = $w;
  4030. } else {
  4031. $w = $l;
  4032. }
  4033. break;
  4034. }
  4035. case 'R': {
  4036. if ($this->rtl) {
  4037. $w = $l;
  4038. } else {
  4039. $w = $w;
  4040. }
  4041. break;
  4042. }
  4043. default: {
  4044. $w = $l;
  4045. break;
  4046. }
  4047. }
  4048. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  4049. if ($firstline) {
  4050. $startx = $this->x;
  4051. $tmparr = array_slice($chars, $j, $nb);
  4052. if ($rtlmode) {
  4053. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  4054. }
  4055. $linew = $this->GetArrStringWidth($tmparr);
  4056. unset($tmparr);
  4057. if ($this->rtl) {
  4058. $this->endlinex = $startx - $linew;
  4059. } else {
  4060. $this->endlinex = $startx + $linew;
  4061. }
  4062. $w = $linew;
  4063. $tmpcmargin = $this->cMargin;
  4064. if ($maxh == 0) {
  4065. $this->cMargin = 0;
  4066. }
  4067. }
  4068. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  4069. unset($tmpstr);
  4070. if ($firstline) {
  4071. $this->cMargin = $tmpcmargin;
  4072. return ($this->UniArrSubString($uchars, $nb));
  4073. }
  4074. ++$nl;
  4075. }
  4076. if ($firstline) {
  4077. return '';
  4078. }
  4079. return $nl;
  4080. }
  4081. /**
  4082. * Returns the remaining width between the current position and margins.
  4083. * @return int Return the remaining width
  4084. * @access protected
  4085. */
  4086. protected function getRemainingWidth() {
  4087. if ($this->rtl) {
  4088. return ($this->x - $this->lMargin);
  4089. } else {
  4090. return ($this->w - $this->rMargin - $this->x);
  4091. }
  4092. }
  4093. /**
  4094. * Extract a slice of the $strarr array and return it as string.
  4095. * @param string $strarr The input array of characters.
  4096. * @param int $start the starting element of $strarr.
  4097. * @param int $end first element that will not be returned.
  4098. * @return Return part of a string
  4099. * @access public
  4100. */
  4101. public function UTF8ArrSubString($strarr, $start='', $end='') {
  4102. if (strlen($start) == 0) {
  4103. $start = 0;
  4104. }
  4105. if (strlen($end) == 0) {
  4106. $end = count($strarr);
  4107. }
  4108. $string = '';
  4109. for ($i=$start; $i < $end; ++$i) {
  4110. $string .= $this->unichr($strarr[$i]);
  4111. }
  4112. return $string;
  4113. }
  4114. /**
  4115. * Extract a slice of the $uniarr array and return it as string.
  4116. * @param string $uniarr The input array of characters.
  4117. * @param int $start the starting element of $strarr.
  4118. * @param int $end first element that will not be returned.
  4119. * @return Return part of a string
  4120. * @access public
  4121. * @since 4.5.037 (2009-04-07)
  4122. */
  4123. public function UniArrSubString($uniarr, $start='', $end='') {
  4124. if (strlen($start) == 0) {
  4125. $start = 0;
  4126. }
  4127. if (strlen($end) == 0) {
  4128. $end = count($uniarr);
  4129. }
  4130. $string = '';
  4131. for ($i=$start; $i < $end; ++$i) {
  4132. $string .= $uniarr[$i];
  4133. }
  4134. return $string;
  4135. }
  4136. /**
  4137. * Convert an array of UTF8 values to array of unicode characters
  4138. * @param string $ta The input array of UTF8 values.
  4139. * @return Return array of unicode characters
  4140. * @access public
  4141. * @since 4.5.037 (2009-04-07)
  4142. */
  4143. public function UTF8ArrayToUniArray($ta) {
  4144. return array_map(array($this, 'unichr'), $ta);
  4145. }
  4146. /**
  4147. * Returns the unicode caracter specified by UTF-8 code
  4148. * @param int $c UTF-8 code
  4149. * @return Returns the specified character.
  4150. * @author Miguel Perez, Nicola Asuni
  4151. * @access public
  4152. * @since 2.3.000 (2008-03-05)
  4153. */
  4154. public function unichr($c) {
  4155. if (!$this->isunicode) {
  4156. return chr($c);
  4157. } elseif ($c <= 0x7F) {
  4158. // one byte
  4159. return chr($c);
  4160. } elseif ($c <= 0x7FF) {
  4161. // two bytes
  4162. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  4163. } elseif ($c <= 0xFFFF) {
  4164. // three bytes
  4165. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4166. } elseif ($c <= 0x10FFFF) {
  4167. // four bytes
  4168. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  4169. } else {
  4170. return '';
  4171. }
  4172. }
  4173. /**
  4174. * Puts an image in the page.
  4175. * The upper-left corner must be given.
  4176. * The dimensions can be specified in different ways:<ul>
  4177. * <li>explicit width and height (expressed in user unit)</li>
  4178. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  4179. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  4180. * Supported formats are JPEG and PNG images whitout GD library and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;
  4181. * The format can be specified explicitly or inferred from the file extension.<br />
  4182. * It is possible to put a link on the image.<br />
  4183. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  4184. * @param string $file Name of the file containing the image.
  4185. * @param float $x Abscissa of the upper-left corner.
  4186. * @param float $y Ordinate of the upper-left corner.
  4187. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4188. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4189. * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  4190. * @param mixed $link URL or identifier returned by AddLink().
  4191. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  4192. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  4193. * @param int $dpi dot-per-inch resolution used on resize
  4194. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  4195. * @param boolean $ismask true if this image is a mask, false otherwise
  4196. * @param mixed $imgmask image object returned by this function or false
  4197. * @param mixed $border Indicates if borders must be drawn around the image. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  4198. * @param boolean $fitbox If true scale image dimensions proportionally to fit within the ($w, $h) box.
  4199. * @return image information
  4200. * @access public
  4201. * @since 1.1
  4202. */
  4203. public function Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false) {
  4204. if ($x === '') {
  4205. $x = $this->x;
  4206. }
  4207. if ($y === '') {
  4208. $y = $this->y;
  4209. }
  4210. // get image dimensions
  4211. $imsize = @getimagesize($file);
  4212. if ($imsize === FALSE) {
  4213. // encode spaces on filename
  4214. $file = str_replace(' ', '%20', $file);
  4215. $imsize = @getimagesize($file);
  4216. if ($imsize === FALSE) {
  4217. $this->Error('[Image] No such file or directory in '.$file);
  4218. }
  4219. }
  4220. // get original image width and height in pixels
  4221. list($pixw, $pixh) = $imsize;
  4222. // calculate image width and height on document
  4223. if (($w <= 0) AND ($h <= 0)) {
  4224. // convert image size to document unit
  4225. $w = $this->pixelsToUnits($pixw);
  4226. $h = $this->pixelsToUnits($pixh);
  4227. } elseif ($w <= 0) {
  4228. $w = $h * $pixw / $pixh;
  4229. } elseif ($h <= 0) {
  4230. $h = $w * $pixh / $pixw;
  4231. } elseif ($fitbox AND ($w > 0) AND ($h > 0)) {
  4232. // scale image dimensions proportionally to fit within the ($w, $h) box
  4233. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  4234. $h = $w * $pixh / $pixw;
  4235. } else {
  4236. $w = $h * $pixw / $pixh;
  4237. }
  4238. }
  4239. // calculate new minimum dimensions in pixels
  4240. $neww = round($w * $this->k * $dpi / $this->dpi);
  4241. $newh = round($h * $this->k * $dpi / $this->dpi);
  4242. // check if resize is necessary (resize is used only to reduce the image)
  4243. if (($neww * $newh) >= ($pixw * $pixh)) {
  4244. $resize = false;
  4245. }
  4246. // check if image has been already added on document
  4247. if (!in_array($file, $this->imagekeys)) {
  4248. //First use of image, get info
  4249. if ($type == '') {
  4250. $fileinfo = pathinfo($file);
  4251. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  4252. $type = $fileinfo['extension'];
  4253. } else {
  4254. $this->Error('Image file has no extension and no type was specified: '.$file);
  4255. }
  4256. }
  4257. $type = strtolower($type);
  4258. if ($type == 'jpg') {
  4259. $type = 'jpeg';
  4260. }
  4261. $mqr = $this->get_mqr();
  4262. $this->set_mqr(false);
  4263. // Specific image handlers
  4264. $mtd = '_parse'.$type;
  4265. // GD image handler function
  4266. $gdfunction = 'imagecreatefrom'.$type;
  4267. $info = false;
  4268. if ((method_exists($this, $mtd)) AND (!($resize AND function_exists($gdfunction)))) {
  4269. // TCPDF image functions
  4270. $info = $this->$mtd($file);
  4271. if ($info == 'pngalpha') {
  4272. return $this->ImagePngAlpha($file, $x, $y, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign);
  4273. }
  4274. }
  4275. if (!$info) {
  4276. if (function_exists($gdfunction)) {
  4277. // GD library
  4278. $img = $gdfunction($file);
  4279. if ($resize) {
  4280. $imgr = imagecreatetruecolor($neww, $newh);
  4281. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  4282. $info = $this->_toJPEG($imgr);
  4283. } else {
  4284. $info = $this->_toJPEG($img);
  4285. }
  4286. } elseif (extension_loaded('imagick')) {
  4287. // ImageMagick library
  4288. $img = new Imagick();
  4289. $img->readImage($file);
  4290. if ($resize) {
  4291. $img->resizeImage($neww, $newh, 10, 1, false);
  4292. }
  4293. $img->setCompressionQuality($this->jpeg_quality);
  4294. $img->setImageFormat('jpeg');
  4295. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  4296. $img->writeImage($tempname);
  4297. $info = $this->_parsejpeg($tempname);
  4298. unlink($tempname);
  4299. $img->destroy();
  4300. } else {
  4301. return;
  4302. }
  4303. }
  4304. if ($info === false) {
  4305. //If false, we cannot process image
  4306. return;
  4307. }
  4308. $this->set_mqr($mqr);
  4309. if ($ismask) {
  4310. // force grayscale
  4311. $info['cs'] = 'DeviceGray';
  4312. }
  4313. $info['i'] = $this->numimages + 1;
  4314. if ($imgmask !== false) {
  4315. $info['masked'] = $imgmask;
  4316. }
  4317. // add image to document
  4318. $this->setImageBuffer($file, $info);
  4319. } else {
  4320. $info = $this->getImageBuffer($file);
  4321. }
  4322. // Check whether we need a new page first as this does not fit
  4323. if ($this->checkPageBreak($h, $y)) {
  4324. $y = $this->GetY() + $this->cMargin;
  4325. }
  4326. // set bottomcoordinates
  4327. $this->img_rb_y = $y + $h;
  4328. // set alignment
  4329. if ($this->rtl) {
  4330. if ($palign == 'L') {
  4331. $ximg = $this->lMargin;
  4332. // set right side coordinate
  4333. $this->img_rb_x = $ximg + $w;
  4334. } elseif ($palign == 'C') {
  4335. $ximg = ($this->w - $x - $w) / 2;
  4336. // set right side coordinate
  4337. $this->img_rb_x = $ximg + $w;
  4338. } else {
  4339. $ximg = $this->w - $x - $w;
  4340. // set left side coordinate
  4341. $this->img_rb_x = $ximg;
  4342. }
  4343. } else {
  4344. if ($palign == 'R') {
  4345. $ximg = $this->w - $this->rMargin - $w;
  4346. // set left side coordinate
  4347. $this->img_rb_x = $ximg;
  4348. } elseif ($palign == 'C') {
  4349. $ximg = ($this->w - $x - $w) / 2;
  4350. // set right side coordinate
  4351. $this->img_rb_x = $ximg + $w;
  4352. } else {
  4353. $ximg = $x;
  4354. // set right side coordinate
  4355. $this->img_rb_x = $ximg + $w;
  4356. }
  4357. }
  4358. if ($ismask) {
  4359. // embed hidden, ouside the canvas
  4360. $xkimg = (10 * $this->pagedim[$this->page]['w']);
  4361. } else {
  4362. $xkimg = $ximg * $this->k;
  4363. }
  4364. $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  4365. if (!empty($border)) {
  4366. $bx = $x;
  4367. $by = $y;
  4368. $this->x = $ximg;
  4369. $this->y = $y;
  4370. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  4371. $this->x = $bx;
  4372. $this->y = $by;
  4373. }
  4374. if ($link) {
  4375. $this->Link($ximg, $y, $w, $h, $link, 0);
  4376. }
  4377. // set pointer to align the successive text/objects
  4378. switch($align) {
  4379. case 'T': {
  4380. $this->y = $y;
  4381. $this->x = $this->img_rb_x;
  4382. break;
  4383. }
  4384. case 'M': {
  4385. $this->y = $y + round($h/2);
  4386. $this->x = $this->img_rb_x;
  4387. break;
  4388. }
  4389. case 'B': {
  4390. $this->y = $this->img_rb_y;
  4391. $this->x = $this->img_rb_x;
  4392. break;
  4393. }
  4394. case 'N': {
  4395. $this->SetY($this->img_rb_y);
  4396. break;
  4397. }
  4398. default:{
  4399. break;
  4400. }
  4401. }
  4402. $this->endlinex = $this->img_rb_x;
  4403. return $info['i'];
  4404. }
  4405. /**
  4406. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  4407. * @param boolean $mqr FALSE for off, TRUE for on.
  4408. * @since 4.6.025 (2009-08-17)
  4409. */
  4410. public function set_mqr($mqr) {
  4411. if (function_exists('set_magic_quotes_runtime')) {
  4412. @set_magic_quotes_runtime($mqr);
  4413. }
  4414. }
  4415. /**
  4416. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  4417. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  4418. * @since 4.6.025 (2009-08-17)
  4419. */
  4420. public function get_mqr() {
  4421. if (function_exists('get_magic_quotes_runtime')) {
  4422. return @get_magic_quotes_runtime();
  4423. }
  4424. return 0;
  4425. }
  4426. /**
  4427. * Convert the loaded php image to a JPEG and then return a structure for the PDF creator.
  4428. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  4429. * @param string $file Image file name.
  4430. * @param image $image Image object.
  4431. * return image JPEG image object.
  4432. * @access protected
  4433. */
  4434. protected function _toJPEG($image) {
  4435. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  4436. imagejpeg($image, $tempname, $this->jpeg_quality);
  4437. imagedestroy($image);
  4438. $retvars = $this->_parsejpeg($tempname);
  4439. // tidy up by removing temporary image
  4440. unlink($tempname);
  4441. return $retvars;
  4442. }
  4443. /**
  4444. * Extract info from a JPEG file without using the GD library.
  4445. * @param string $file image file to parse
  4446. * @return array structure containing the image data
  4447. * @access protected
  4448. */
  4449. protected function _parsejpeg($file) {
  4450. $a = getimagesize($file);
  4451. if (empty($a)) {
  4452. $this->Error('Missing or incorrect image file: '.$file);
  4453. }
  4454. if ($a[2] != 2) {
  4455. $this->Error('Not a JPEG file: '.$file);
  4456. }
  4457. if ((!isset($a['channels'])) OR ($a['channels'] == 3)) {
  4458. $colspace = 'DeviceRGB';
  4459. } elseif ($a['channels'] == 4) {
  4460. $colspace = 'DeviceCMYK';
  4461. } else {
  4462. $colspace = 'DeviceGray';
  4463. }
  4464. $bpc = isset($a['bits']) ? $a['bits'] : 8;
  4465. $data = file_get_contents($file);
  4466. return array('w' => $a[0], 'h' => $a[1], 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  4467. }
  4468. /**
  4469. * Extract info from a PNG file without using the GD library.
  4470. * @param string $file image file to parse
  4471. * @return array structure containing the image data
  4472. * @access protected
  4473. */
  4474. protected function _parsepng($file) {
  4475. $f = fopen($file, 'rb');
  4476. if ($f === false) {
  4477. $this->Error('Can\'t open image file: '.$file);
  4478. }
  4479. //Check signature
  4480. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  4481. $this->Error('Not a PNG file: '.$file);
  4482. }
  4483. //Read header chunk
  4484. fread($f, 4);
  4485. if (fread($f, 4) != 'IHDR') {
  4486. $this->Error('Incorrect PNG file: '.$file);
  4487. }
  4488. $w = $this->_freadint($f);
  4489. $h = $this->_freadint($f);
  4490. $bpc = ord(fread($f, 1));
  4491. if ($bpc > 8) {
  4492. //$this->Error('16-bit depth not supported: '.$file);
  4493. fclose($f);
  4494. return false;
  4495. }
  4496. $ct = ord(fread($f, 1));
  4497. if ($ct == 0) {
  4498. $colspace = 'DeviceGray';
  4499. } elseif ($ct == 2) {
  4500. $colspace = 'DeviceRGB';
  4501. } elseif ($ct == 3) {
  4502. $colspace = 'Indexed';
  4503. } else {
  4504. // alpha channel
  4505. fclose($f);
  4506. return 'pngalpha';
  4507. }
  4508. if (ord(fread($f, 1)) != 0) {
  4509. //$this->Error('Unknown compression method: '.$file);
  4510. fclose($f);
  4511. return false;
  4512. }
  4513. if (ord(fread($f, 1)) != 0) {
  4514. //$this->Error('Unknown filter method: '.$file);
  4515. fclose($f);
  4516. return false;
  4517. }
  4518. if (ord(fread($f, 1)) != 0) {
  4519. //$this->Error('Interlacing not supported: '.$file);
  4520. fclose($f);
  4521. return false;
  4522. }
  4523. fread($f, 4);
  4524. $parms = '/DecodeParms <</Predictor 15 /Colors '.($ct==2 ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w.'>>';
  4525. //Scan chunks looking for palette, transparency and image data
  4526. $pal = '';
  4527. $trns = '';
  4528. $data = '';
  4529. do {
  4530. $n = $this->_freadint($f);
  4531. $type = fread($f, 4);
  4532. if ($type == 'PLTE') {
  4533. //Read palette
  4534. $pal = $this->rfread($f, $n);
  4535. fread($f, 4);
  4536. } elseif ($type == 'tRNS') {
  4537. //Read transparency info
  4538. $t = $this->rfread($f, $n);
  4539. if ($ct == 0) {
  4540. $trns = array(ord(substr($t, 1, 1)));
  4541. } elseif ($ct == 2) {
  4542. $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1)));
  4543. } else {
  4544. $pos = strpos($t, chr(0));
  4545. if ($pos !== false) {
  4546. $trns = array($pos);
  4547. }
  4548. }
  4549. fread($f, 4);
  4550. } elseif ($type == 'IDAT') {
  4551. //Read image data block
  4552. $data .= $this->rfread($f, $n);
  4553. fread($f, 4);
  4554. } elseif ($type == 'IEND') {
  4555. break;
  4556. } else {
  4557. $this->rfread($f, $n + 4);
  4558. }
  4559. } while ($n);
  4560. if (($colspace == 'Indexed') AND (empty($pal))) {
  4561. //$this->Error('Missing palette in '.$file);
  4562. fclose($f);
  4563. return false;
  4564. }
  4565. fclose($f);
  4566. return array('w' => $w, 'h' => $h, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  4567. }
  4568. /**
  4569. * Binary-safe and URL-safe file read.
  4570. * Reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met: length bytes have been read; EOF (end of file) is reached.
  4571. * @param resource $handle
  4572. * @param int $length
  4573. * @return Returns the read string or FALSE in case of error.
  4574. * @author Nicola Asuni
  4575. * @access protected
  4576. * @since 4.5.027 (2009-03-16)
  4577. */
  4578. protected function rfread($handle, $length) {
  4579. $data = fread($handle, $length);
  4580. if ($data === false) {
  4581. return false;
  4582. }
  4583. $rest = $length - strlen($data);
  4584. if ($rest > 0) {
  4585. $data .= $this->rfread($handle, $rest);
  4586. }
  4587. return $data;
  4588. }
  4589. /**
  4590. * Extract info from a PNG image with alpha channel using the GD library.
  4591. * @param string $file Name of the file containing the image.
  4592. * @param float $x Abscissa of the upper-left corner.
  4593. * @param float $y Ordinate of the upper-left corner.
  4594. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4595. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  4596. * @param string $type Image format. Possible values are (case insensitive): JPEG and PNG (whitout GD library) and all images supported by GD: GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM;. If not specified, the type is inferred from the file extension.
  4597. * @param mixed $link URL or identifier returned by AddLink().
  4598. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  4599. * @param boolean $resize If true resize (reduce) the image to fit $w and $h (requires GD library).
  4600. * @param int $dpi dot-per-inch resolution used on resize
  4601. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  4602. * @author Valentin Schmidt, Nicola Asuni
  4603. * @access protected
  4604. * @since 4.3.007 (2008-12-04)
  4605. * @see Image()
  4606. */
  4607. protected function ImagePngAlpha($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='') {
  4608. // get image size
  4609. list($wpx, $hpx) = getimagesize($file);
  4610. // generate images
  4611. $img = imagecreatefrompng($file);
  4612. $imgalpha = imagecreate($wpx, $hpx);
  4613. // generate gray scale pallete
  4614. for ($c = 0; $c < 256; ++$c) {
  4615. ImageColorAllocate($imgalpha, $c, $c, $c);
  4616. }
  4617. // extract alpha channel
  4618. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  4619. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  4620. $colorindex = imagecolorat($img, $xpx, $ypx);
  4621. $col = imagecolorsforindex($img, $colorindex);
  4622. imagesetpixel($imgalpha, $xpx, $ypx, $this->getGDgamma((127 - $col['alpha']) * 255 / 127));
  4623. }
  4624. }
  4625. // create temp alpha file
  4626. $tempfile_alpha = tempnam(K_PATH_CACHE, 'mska_');
  4627. imagepng($imgalpha, $tempfile_alpha);
  4628. imagedestroy($imgalpha);
  4629. // extract image without alpha channel
  4630. $imgplain = imagecreatetruecolor($wpx, $hpx);
  4631. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  4632. // create temp image file
  4633. $tempfile_plain = tempnam(K_PATH_CACHE, 'mskp_');
  4634. imagepng($imgplain, $tempfile_plain);
  4635. imagedestroy($imgplain);
  4636. // embed mask image
  4637. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  4638. // embed image, masked with previously embedded mask
  4639. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  4640. // remove temp files
  4641. unlink($tempfile_alpha);
  4642. unlink($tempfile_plain);
  4643. }
  4644. /**
  4645. * Correct the gamma value to be used with GD library
  4646. * @param float $v the gamma value to be corrected
  4647. * @access protected
  4648. * @since 4.3.007 (2008-12-04)
  4649. */
  4650. protected function getGDgamma($v) {
  4651. return (pow(($v / 255), 2.2) * 255);
  4652. }
  4653. /**
  4654. * Performs a line break.
  4655. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  4656. * @param float $h The height of the break. By default, the value equals the height of the last printed cell.
  4657. * @param boolean $cell if true add a cMargin to the x coordinate
  4658. * @access public
  4659. * @since 1.0
  4660. * @see Cell()
  4661. */
  4662. public function Ln($h='', $cell=false) {
  4663. //Line feed; default value is last cell height
  4664. if ($cell) {
  4665. $cellmargin = $this->cMargin;
  4666. } else {
  4667. $cellmargin = 0;
  4668. }
  4669. if ($this->rtl) {
  4670. $this->x = $this->w - $this->rMargin - $cellmargin;
  4671. } else {
  4672. $this->x = $this->lMargin + $cellmargin;
  4673. }
  4674. if (is_string($h)) {
  4675. $this->y += $this->lasth;
  4676. } else {
  4677. $this->y += $h;
  4678. }
  4679. $this->newline = true;
  4680. }
  4681. /**
  4682. * Returns the relative X value of current position.
  4683. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  4684. * @return float
  4685. * @access public
  4686. * @since 1.2
  4687. * @see SetX(), GetY(), SetY()
  4688. */
  4689. public function GetX() {
  4690. //Get x position
  4691. if ($this->rtl) {
  4692. return ($this->w - $this->x);
  4693. } else {
  4694. return $this->x;
  4695. }
  4696. }
  4697. /**
  4698. * Returns the absolute X value of current position.
  4699. * @return float
  4700. * @access public
  4701. * @since 1.2
  4702. * @see SetX(), GetY(), SetY()
  4703. */
  4704. public function GetAbsX() {
  4705. return $this->x;
  4706. }
  4707. /**
  4708. * Returns the ordinate of the current position.
  4709. * @return float
  4710. * @access public
  4711. * @since 1.0
  4712. * @see SetY(), GetX(), SetX()
  4713. */
  4714. public function GetY() {
  4715. //Get y position
  4716. return $this->y;
  4717. }
  4718. /**
  4719. * Defines the abscissa of the current position.
  4720. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  4721. * @param float $x The value of the abscissa.
  4722. * @access public
  4723. * @since 1.2
  4724. * @see GetX(), GetY(), SetY(), SetXY()
  4725. */
  4726. public function SetX($x) {
  4727. //Set x position
  4728. if ($this->rtl) {
  4729. if ($x >= 0) {
  4730. $this->x = $this->w - $x;
  4731. } else {
  4732. $this->x = abs($x);
  4733. }
  4734. } else {
  4735. if ($x >= 0) {
  4736. $this->x = $x;
  4737. } else {
  4738. $this->x = $this->w + $x;
  4739. }
  4740. }
  4741. if ($this->x < 0) {
  4742. $this->x = 0;
  4743. }
  4744. if ($this->x > $this->w) {
  4745. $this->x = $this->w;
  4746. }
  4747. }
  4748. /**
  4749. * Moves the current abscissa back to the left margin and sets the ordinate.
  4750. * If the passed value is negative, it is relative to the bottom of the page.
  4751. * @param float $y The value of the ordinate.
  4752. * @param bool $resetx if true (default) reset the X position.
  4753. * @access public
  4754. * @since 1.0
  4755. * @see GetX(), GetY(), SetY(), SetXY()
  4756. */
  4757. public function SetY($y, $resetx=true) {
  4758. if ($resetx) {
  4759. //reset x
  4760. if ($this->rtl) {
  4761. $this->x = $this->w - $this->rMargin;
  4762. } else {
  4763. $this->x = $this->lMargin;
  4764. }
  4765. }
  4766. if ($y >= 0) {
  4767. $this->y = $y;
  4768. } else {
  4769. $this->y = $this->h + $y;
  4770. }
  4771. if ($this->y < 0) {
  4772. $this->y = 0;
  4773. }
  4774. if ($this->y > $this->h) {
  4775. $this->y = $this->h;
  4776. }
  4777. }
  4778. /**
  4779. * Defines the abscissa and ordinate of the current position.
  4780. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  4781. * @param float $x The value of the abscissa
  4782. * @param float $y The value of the ordinate
  4783. * @access public
  4784. * @since 1.2
  4785. * @see SetX(), SetY()
  4786. */
  4787. public function SetXY($x, $y) {
  4788. //Set x and y positions
  4789. $this->SetY($y);
  4790. $this->SetX($x);
  4791. }
  4792. /**
  4793. * Send the document to a given destination: string, local file or browser.
  4794. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  4795. * The method first calls Close() if necessary to terminate the document.
  4796. * @param string $name The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
  4797. * @param string $dest Destination where to send the document. It can take one of the following values:<ul><li>I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.</li><li>D: send to the browser and force a file download with the name given by name.</li><li>F: save to a local file with the name given by name.</li><li>S: return the document as a string. name is ignored.</li></ul>
  4798. * @access public
  4799. * @since 1.0
  4800. * @see Close()
  4801. */
  4802. public function Output($name='doc.pdf', $dest='I') {
  4803. //Output PDF to some destination
  4804. //Finish document if necessary
  4805. if ($this->state < 3) {
  4806. $this->Close();
  4807. }
  4808. //Normalize parameters
  4809. if (is_bool($dest)) {
  4810. $dest = $dest ? 'D' : 'F';
  4811. }
  4812. $dest = strtoupper($dest);
  4813. if ($dest != 'F') {
  4814. $name = preg_replace('/[\s]+/', '_', $name);
  4815. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  4816. }
  4817. if ($this->sign) {
  4818. // *** apply digital signature to the document ***
  4819. // get the document content
  4820. $pdfdoc = $this->getBuffer();
  4821. // remove last newline
  4822. $pdfdoc = substr($pdfdoc, 0, -1);
  4823. // Remove the original buffer
  4824. if (isset($this->diskcache) AND $this->diskcache) {
  4825. // remove buffer file from cache
  4826. unlink($this->buffer);
  4827. }
  4828. unset($this->buffer);
  4829. // remove filler space
  4830. $byterange_string_len = strlen($this->byterange_string);
  4831. // define the ByteRange
  4832. $byte_range = array();
  4833. $byte_range[0] = 0;
  4834. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  4835. $byte_range[2] = $byte_range[1] + $this->signature_max_lenght + 2;
  4836. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  4837. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  4838. // replace the ByteRange
  4839. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  4840. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  4841. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  4842. // write the document to a temporary folder
  4843. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  4844. $f = fopen($tempdoc, 'wb');
  4845. if (!$f) {
  4846. $this->Error('Unable to create temporary file: '.$tempdoc);
  4847. }
  4848. $pdfdoc_lenght = strlen($pdfdoc);
  4849. fwrite($f, $pdfdoc, $pdfdoc_lenght);
  4850. fclose($f);
  4851. // get digital signature via openssl library
  4852. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  4853. if (empty($this->signature_data['extracerts'])) {
  4854. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  4855. } else {
  4856. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED, $this->signature_data['extracerts']);
  4857. }
  4858. unlink($tempdoc);
  4859. // read signature
  4860. $signature = file_get_contents($tempsign, false, null, $pdfdoc_lenght);
  4861. unlink($tempsign);
  4862. // extract signature
  4863. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  4864. $tmparr = explode("\n\n", $signature);
  4865. $signature = $tmparr[1];
  4866. unset($tmparr);
  4867. // decode signature
  4868. $signature = base64_decode(trim($signature));
  4869. // convert signature to hex
  4870. $signature = current(unpack('H*', $signature));
  4871. $signature = str_pad($signature, $this->signature_max_lenght, '0');
  4872. // Add signature to the document
  4873. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, ($byte_range[1]));
  4874. $this->diskcache = false;
  4875. $this->buffer = &$pdfdoc;
  4876. $this->bufferlen = strlen($pdfdoc);
  4877. }
  4878. switch($dest) {
  4879. case 'I': {
  4880. // Send PDF to the standard output
  4881. if (ob_get_contents()) {
  4882. $this->Error('Some data has already been output, can\'t send PDF file');
  4883. }
  4884. if (php_sapi_name() != 'cli') {
  4885. //We send to a browser
  4886. header('Content-Type: application/pdf');
  4887. if (headers_sent()) {
  4888. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  4889. }
  4890. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  4891. header('Pragma: public');
  4892. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  4893. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  4894. header('Content-Length: '.$this->bufferlen);
  4895. header('Content-Disposition: inline; filename="'.basename($name).'";');
  4896. }
  4897. echo $this->getBuffer();
  4898. break;
  4899. }
  4900. case 'D': {
  4901. // Download PDF as file
  4902. if (ob_get_contents()) {
  4903. $this->Error('Some data has already been output, can\'t send PDF file');
  4904. }
  4905. header('Content-Description: File Transfer');
  4906. if (headers_sent()) {
  4907. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  4908. }
  4909. header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  4910. header('Pragma: public');
  4911. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  4912. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  4913. // force download dialog
  4914. header('Content-Type: application/force-download');
  4915. header('Content-Type: application/octet-stream', false);
  4916. header('Content-Type: application/download', false);
  4917. header('Content-Type: application/pdf', false);
  4918. // use the Content-Disposition header to supply a recommended filename
  4919. header('Content-Disposition: attachment; filename="'.basename($name).'";');
  4920. header('Content-Transfer-Encoding: binary');
  4921. header('Content-Length: '.$this->bufferlen);
  4922. echo $this->getBuffer();
  4923. break;
  4924. }
  4925. case 'F': {
  4926. // Save PDF to a local file
  4927. if ($this->diskcache) {
  4928. copy($this->buffer, $name);
  4929. } else {
  4930. $f = fopen($name, 'wb');
  4931. if (!$f) {
  4932. $this->Error('Unable to create output file: '.$name);
  4933. }
  4934. fwrite($f, $this->getBuffer(), $this->bufferlen);
  4935. fclose($f);
  4936. }
  4937. break;
  4938. }
  4939. case 'S': {
  4940. // Returns PDF as a string
  4941. return $this->getBuffer();
  4942. }
  4943. default: {
  4944. $this->Error('Incorrect output destination: '.$dest);
  4945. }
  4946. }
  4947. return '';
  4948. }
  4949. /**
  4950. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  4951. * @param boolean $destroyall if true destroys all class variables, otherwise preserves critical variables.
  4952. * @param boolean $preserve_objcopy if true preserves the objcopy variable
  4953. * @access public
  4954. * @since 4.5.016 (2009-02-24)
  4955. */
  4956. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  4957. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  4958. // remove buffer file from cache
  4959. unlink($this->buffer);
  4960. }
  4961. foreach (array_keys(get_object_vars($this)) as $val) {
  4962. if ($destroyall OR (
  4963. ($val != 'internal_encoding')
  4964. AND ($val != 'state')
  4965. AND ($val != 'bufferlen')
  4966. AND ($val != 'buffer')
  4967. AND ($val != 'diskcache')
  4968. AND ($val != 'sign')
  4969. AND ($val != 'signature_data')
  4970. AND ($val != 'signature_max_lenght')
  4971. AND ($val != 'byterange_string')
  4972. )) {
  4973. if (!$preserve_objcopy OR ($val != 'objcopy')) {
  4974. unset($this->$val);
  4975. }
  4976. }
  4977. }
  4978. }
  4979. /**
  4980. * Check for locale-related bug
  4981. * @access protected
  4982. */
  4983. protected function _dochecks() {
  4984. //Check for locale-related bug
  4985. if (1.1 == 1) {
  4986. $this->Error('Don\'t alter the locale before including class file');
  4987. }
  4988. //Check for decimal separator
  4989. if (sprintf('%.1F', 1.0) != '1.0') {
  4990. setlocale(LC_NUMERIC, 'C');
  4991. }
  4992. }
  4993. /**
  4994. * Return fonts path
  4995. * @return string
  4996. * @access protected
  4997. */
  4998. protected function _getfontpath() {
  4999. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  5000. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  5001. }
  5002. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  5003. }
  5004. /**
  5005. * Output pages.
  5006. * @access protected
  5007. */
  5008. protected function _putpages() {
  5009. $nb = $this->numpages;
  5010. if (!empty($this->AliasNbPages)) {
  5011. $nbs = $this->formatPageNumber($nb);
  5012. $nbu = $this->UTF8ToUTF16BE($nbs, false); // replacement for unicode font
  5013. $alias_a = $this->_escape($this->AliasNbPages);
  5014. $alias_au = $this->_escape('{'.$this->AliasNbPages.'}');
  5015. if ($this->isunicode) {
  5016. $alias_b = $this->_escape($this->UTF8ToLatin1($this->AliasNbPages));
  5017. $alias_bu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNbPages.'}'));
  5018. $alias_c = $this->_escape($this->utf8StrRev($this->AliasNbPages, false, $this->tmprtl));
  5019. $alias_cu = $this->_escape($this->utf8StrRev('{'.$this->AliasNbPages.'}', false, $this->tmprtl));
  5020. }
  5021. }
  5022. if (!empty($this->AliasNumPage)) {
  5023. $alias_pa = $this->_escape($this->AliasNumPage);
  5024. $alias_pau = $this->_escape('{'.$this->AliasNumPage.'}');
  5025. if ($this->isunicode) {
  5026. $alias_pb = $this->_escape($this->UTF8ToLatin1($this->AliasNumPage));
  5027. $alias_pbu = $this->_escape($this->UTF8ToLatin1('{'.$this->AliasNumPage.'}'));
  5028. $alias_pc = $this->_escape($this->utf8StrRev($this->AliasNumPage, false, $this->tmprtl));
  5029. $alias_pcu = $this->_escape($this->utf8StrRev('{'.$this->AliasNumPage.'}', false, $this->tmprtl));
  5030. }
  5031. }
  5032. $pagegroupnum = 0;
  5033. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  5034. for ($n=1; $n <= $nb; ++$n) {
  5035. $temppage = $this->getPageBuffer($n);
  5036. if (!empty($this->pagegroups)) {
  5037. if(isset($this->newpagegroup[$n])) {
  5038. $pagegroupnum = 0;
  5039. }
  5040. ++$pagegroupnum;
  5041. foreach ($this->pagegroups as $k => $v) {
  5042. // replace total pages group numbers
  5043. $vs = $this->formatPageNumber($v);
  5044. $vu = $this->UTF8ToUTF16BE($vs, false);
  5045. $alias_ga = $this->_escape($k);
  5046. $alias_gau = $this->_escape('{'.$k.'}');
  5047. if ($this->isunicode) {
  5048. $alias_gb = $this->_escape($this->UTF8ToLatin1($k));
  5049. $alias_gbu = $this->_escape($this->UTF8ToLatin1('{'.$k.'}'));
  5050. $alias_gc = $this->_escape($this->utf8StrRev($k, false, $this->tmprtl));
  5051. $alias_gcu = $this->_escape($this->utf8StrRev('{'.$k.'}', false, $this->tmprtl));
  5052. }
  5053. $temppage = str_replace($alias_gau, $vu, $temppage);
  5054. if ($this->isunicode) {
  5055. $temppage = str_replace($alias_gbu, $vu, $temppage);
  5056. $temppage = str_replace($alias_gcu, $vu, $temppage);
  5057. $temppage = str_replace($alias_gb, $vs, $temppage);
  5058. $temppage = str_replace($alias_gc, $vs, $temppage);
  5059. }
  5060. $temppage = str_replace($alias_ga, $vs, $temppage);
  5061. // replace page group numbers
  5062. $pvs = $this->formatPageNumber($pagegroupnum);
  5063. $pvu = $this->UTF8ToUTF16BE($pvs, false);
  5064. $pk = str_replace('{nb', '{pnb', $k);
  5065. $alias_pga = $this->_escape($pk);
  5066. $alias_pgau = $this->_escape('{'.$pk.'}');
  5067. if ($this->isunicode) {
  5068. $alias_pgb = $this->_escape($this->UTF8ToLatin1($pk));
  5069. $alias_pgbu = $this->_escape($this->UTF8ToLatin1('{'.$pk.'}'));
  5070. $alias_pgc = $this->_escape($this->utf8StrRev($pk, false, $this->tmprtl));
  5071. $alias_pgcu = $this->_escape($this->utf8StrRev('{'.$pk.'}', false, $this->tmprtl));
  5072. }
  5073. $temppage = str_replace($alias_pgau, $pvu, $temppage);
  5074. if ($this->isunicode) {
  5075. $temppage = str_replace($alias_pgbu, $pvu, $temppage);
  5076. $temppage = str_replace($alias_pgcu, $pvu, $temppage);
  5077. $temppage = str_replace($alias_pgb, $pvs, $temppage);
  5078. $temppage = str_replace($alias_pgc, $pvs, $temppage);
  5079. }
  5080. $temppage = str_replace($alias_pga, $pvs, $temppage);
  5081. }
  5082. }
  5083. if (!empty($this->AliasNbPages)) {
  5084. // replace total pages number
  5085. $temppage = str_replace($alias_au, $nbu, $temppage);
  5086. if ($this->isunicode) {
  5087. $temppage = str_replace($alias_bu, $nbu, $temppage);
  5088. $temppage = str_replace($alias_cu, $nbu, $temppage);
  5089. $temppage = str_replace($alias_b, $nbs, $temppage);
  5090. $temppage = str_replace($alias_c, $nbs, $temppage);
  5091. }
  5092. $temppage = str_replace($alias_a, $nbs, $temppage);
  5093. }
  5094. if (!empty($this->AliasNumPage)) {
  5095. // replace page number
  5096. $pnbs = $this->formatPageNumber($n);
  5097. $pnbu = $this->UTF8ToUTF16BE($pnbs, false); // replacement for unicode font
  5098. $temppage = str_replace($alias_pau, $pnbu, $temppage);
  5099. if ($this->isunicode) {
  5100. $temppage = str_replace($alias_pbu, $pnbu, $temppage);
  5101. $temppage = str_replace($alias_pcu, $pnbu, $temppage);
  5102. $temppage = str_replace($alias_pb, $pnbs, $temppage);
  5103. $temppage = str_replace($alias_pc, $pnbs, $temppage);
  5104. }
  5105. $temppage = str_replace($alias_pa, $pnbs, $temppage);
  5106. }
  5107. $temppage = str_replace($this->epsmarker, '', $temppage);
  5108. //Page
  5109. $this->page_obj_id[$n] = $this->_newobj();
  5110. $this->_out('<</Type /Page');
  5111. $this->_out('/Parent 1 0 R');
  5112. $this->_out(sprintf('/MediaBox [0 0 %.2F %.2F]', $this->pagedim[$n]['w'], $this->pagedim[$n]['h']));
  5113. $this->_out('/Resources 2 0 R');
  5114. $this->_putannotsrefs($n);
  5115. $this->_out('/Contents '.($this->n + 1).' 0 R>>');
  5116. $this->_out('endobj');
  5117. //Page content
  5118. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  5119. $this->_newobj();
  5120. $this->_out('<<'.$filter.'/Length '.strlen($p).'>>');
  5121. $this->_putstream($p);
  5122. $this->_out('endobj');
  5123. if ($this->diskcache) {
  5124. // remove temporary files
  5125. unlink($this->pages[$n]);
  5126. }
  5127. }
  5128. //Pages root
  5129. $this->offsets[1] = $this->bufferlen;
  5130. $this->_out('1 0 obj');
  5131. $this->_out('<</Type /Pages');
  5132. $this->_out('/Kids [');
  5133. foreach($this->page_obj_id as $page_obj) {
  5134. $this->_out($page_obj.' 0 R');
  5135. }
  5136. $this->_out(']');
  5137. $this->_out('/Count '.$nb);
  5138. $this->_out('>>');
  5139. $this->_out('endobj');
  5140. }
  5141. /**
  5142. * Output referencees to page annotations
  5143. * @param int $n page number
  5144. * @access protected
  5145. * @author Nicola Asuni
  5146. * @since 4.7.000 (2008-08-29)
  5147. */
  5148. protected function _putannotsrefs($n) {
  5149. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type']) AND ($this->signature_data['cert_type'] > 0)))) {
  5150. return;
  5151. }
  5152. $this->_out('/Annots [');
  5153. if (isset($this->PageAnnots[$n])) {
  5154. // set page annotations
  5155. foreach ($this->PageAnnots[$n] as $id) {
  5156. ++$this->annot_obj_id;
  5157. $this->_out($this->annot_obj_id.' 0 R');
  5158. }
  5159. }
  5160. if (($n==1) AND $this->sign AND isset($this->signature_data['cert_type']) AND ($this->signature_data['cert_type'] > 0)) {
  5161. // set reference for signature object
  5162. $this->_out($this->sig_annot_ref);
  5163. }
  5164. $this->_out(']');
  5165. }
  5166. /**
  5167. * Output annotations objects for all pages.
  5168. * !!! THIS FUNCTION IS NOT YET COMPLETED !!!
  5169. * See section 8.4 of PDF reference.
  5170. * @access protected
  5171. * @author Nicola Asuni
  5172. * @since 4.0.018 (2008-08-06)
  5173. */
  5174. protected function _putannotsobjs() {
  5175. // reset object counter
  5176. $this->annot_obj_id = $this->annots_start_obj_id;
  5177. for ($n=1; $n <= $this->numpages; ++$n) {
  5178. if (isset($this->PageAnnots[$n])) {
  5179. // set page annotations
  5180. foreach ($this->PageAnnots[$n] as $key => $pl) {
  5181. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  5182. $a = $pl['x'] * $this->k;
  5183. $b = $this->pagedim[$n]['h'] - ($pl['y'] * $this->k);
  5184. $c = $pl['w'] * $this->k;
  5185. $d = $pl['h'] * $this->k;
  5186. $rect = sprintf('%.2F %.2F %.2F %.2F', $a, $b, $a+$c, $b-$d);
  5187. // create new annotation object
  5188. $annots = '<</Type /Annot';
  5189. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  5190. $annots .= ' /Rect ['.$rect.']';
  5191. $annots .= ' /Contents '.$this->_textstring($pl['txt']);
  5192. //$annots .= ' /P ';
  5193. $annots .= ' /NM '.$this->_textstring(sprintf('%04u-%04u', $n, $key));
  5194. $annots .= ' /M '.$this->_datestring();
  5195. if (isset($pl['opt']['f'])) {
  5196. $val = 0;
  5197. if (is_array($pl['opt']['f'])) {
  5198. foreach ($pl['opt']['f'] as $f) {
  5199. switch (strtolower($f)) {
  5200. case 'invisible': {
  5201. $val += 1 << 0;
  5202. break;
  5203. }
  5204. case 'hidden': {
  5205. $val += 1 << 1;
  5206. break;
  5207. }
  5208. case 'print': {
  5209. $val += 1 << 2;
  5210. break;
  5211. }
  5212. case 'nozoom': {
  5213. $val += 1 << 3;
  5214. break;
  5215. }
  5216. case 'norotate': {
  5217. $val += 1 << 4;
  5218. break;
  5219. }
  5220. case 'noview': {
  5221. $val += 1 << 5;
  5222. break;
  5223. }
  5224. case 'readonly': {
  5225. $val += 1 << 6;
  5226. break;
  5227. }
  5228. case 'locked': {
  5229. $val += 1 << 8;
  5230. break;
  5231. }
  5232. case 'togglenoview': {
  5233. $val += 1 << 9;
  5234. break;
  5235. }
  5236. case 'lockedcontents': {
  5237. $val += 1 << 10;
  5238. break;
  5239. }
  5240. default: {
  5241. break;
  5242. }
  5243. }
  5244. }
  5245. }
  5246. $annots .= ' /F '.intval($val);
  5247. }
  5248. //$annots .= ' /AP ';
  5249. //$annots .= ' /AS ';
  5250. $annots .= ' /Border [';
  5251. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  5252. $annots .= intval($pl['opt']['border'][0]).' ';
  5253. $annots .= intval($pl['opt']['border'][1]).' ';
  5254. $annots .= intval($pl['opt']['border'][2]);
  5255. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  5256. $annots .= ' [';
  5257. foreach ($pl['opt']['border'][3] as $dash) {
  5258. $annots .= intval($dash).' ';
  5259. }
  5260. $annots .= ']';
  5261. }
  5262. } else {
  5263. $annots .= '0 0 0';
  5264. }
  5265. $annots .= ']';
  5266. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  5267. $annots .= ' /BS <<Type /Border';
  5268. if (isset($pl['opt']['bs']['w'])) {
  5269. $annots .= ' /W '.sprintf("%.4F", floatval($pl['opt']['bs']['w']));
  5270. }
  5271. $bstyles = array('S', 'D', 'B', 'I', 'U');
  5272. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  5273. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  5274. }
  5275. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  5276. $annots .= ' /D [';
  5277. foreach ($pl['opt']['bs']['d'] as $cord) {
  5278. $cord = floatval($cord);
  5279. $annots .= sprintf(" %.4F", $cord);
  5280. }
  5281. $annots .= ']';
  5282. }
  5283. $annots .= '>> ';
  5284. }
  5285. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  5286. $annots .= ' /BE <<';
  5287. $bstyles = array('S', 'C');
  5288. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $markups)) {
  5289. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  5290. } else {
  5291. $annots .= ' /S /S';
  5292. }
  5293. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  5294. $annots .= ' /I '.sprintf(" %.4F", $pl['opt']['be']['i']);
  5295. }
  5296. $annots .= '>>';
  5297. }
  5298. $annots .= ' /C [';
  5299. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c']))) {
  5300. foreach ($pl['opt']['c'] as $col) {
  5301. $col = intval($col);
  5302. $color = $col <= 0 ? 0 : ($col >= 255 ? 1 : $col / 255);
  5303. $annots .= sprintf(" %.4F", $color);
  5304. }
  5305. }
  5306. $annots .= ']';
  5307. //$annots .= ' /StructParent ';
  5308. //$annots .= ' /OC ';
  5309. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  5310. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  5311. // this is a markup type
  5312. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  5313. $annots .= ' /T '.$this->_textstring($pl['opt']['t']);
  5314. }
  5315. //$annots .= ' /Popup ';
  5316. if (isset($pl['opt']['ca'])) {
  5317. $annots .= ' /CA '.sprintf("%.4F", floatval($pl['opt']['ca']));
  5318. }
  5319. if (isset($pl['opt']['rc'])) {
  5320. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc']);
  5321. }
  5322. $annots .= ' /CreationDate '.$this->_datestring();
  5323. //$annots .= ' /IRT ';
  5324. if (isset($pl['opt']['subj'])) {
  5325. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj']);
  5326. }
  5327. //$annots .= ' /RT ';
  5328. //$annots .= ' /IT ';
  5329. //$annots .= ' /ExData ';
  5330. }
  5331. switch (strtolower($pl['opt']['subtype'])) {
  5332. case 'text': {
  5333. if (isset($pl['opt']['open'])) {
  5334. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  5335. }
  5336. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  5337. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5338. $annots .= ' /Name /'.$pl['opt']['name'];
  5339. } else {
  5340. $annots .= ' /Name /Note';
  5341. }
  5342. $statemodels = array('Marked', 'Review');
  5343. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  5344. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  5345. } else {
  5346. $pl['opt']['statemodel'] = 'Marked';
  5347. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  5348. }
  5349. if ($pl['opt']['statemodel'] == 'Marked') {
  5350. $states = array('Accepted', 'Unmarked');
  5351. } else {
  5352. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  5353. }
  5354. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  5355. $annots .= ' /State /'.$pl['opt']['state'];
  5356. } else {
  5357. if ($pl['opt']['statemodel'] == 'Marked') {
  5358. $annots .= ' /State /Unmarked';
  5359. } else {
  5360. $annots .= ' /State /None';
  5361. }
  5362. }
  5363. break;
  5364. }
  5365. case 'link': {
  5366. if(is_string($pl['txt'])) {
  5367. // external URI link
  5368. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt'])).'>>';
  5369. } else {
  5370. // internal link
  5371. $l = $this->links[$pl['txt']];
  5372. $annots .= sprintf(' /Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $l[0])), ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  5373. }
  5374. $hmodes = array('N', 'I', 'O', 'P');
  5375. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  5376. $annots .= ' /H /'.$pl['opt']['h'];
  5377. } else {
  5378. $annots .= ' /H /I';
  5379. }
  5380. //$annots .= ' /PA ';
  5381. //$annots .= ' /Quadpoints ';
  5382. break;
  5383. }
  5384. case 'freetext': {
  5385. $annots .= ' /DA '.$this->_textstring($pl['txt']);
  5386. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  5387. $annots .= ' /Q '.intval($pl['opt']['q']);
  5388. }
  5389. if (isset($pl['opt']['rc'])) {
  5390. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc']);
  5391. }
  5392. if (isset($pl['opt']['ds'])) {
  5393. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds']);
  5394. }
  5395. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  5396. $annots .= ' /CL [';
  5397. foreach ($pl['opt']['cl'] as $cl) {
  5398. $annots .= sprintf("%.4F ", $cl * $this->k);
  5399. }
  5400. $annots .= ']';
  5401. }
  5402. $tfit = array('FreeTextCallout', 'FreeTextTypeWriter');
  5403. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  5404. $annots .= ' /IT '.$pl['opt']['it'];
  5405. }
  5406. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  5407. $l = $pl['opt']['rd'][0] * $this->k;
  5408. $r = $pl['opt']['rd'][1] * $this->k;
  5409. $t = $pl['opt']['rd'][2] * $this->k;
  5410. $b = $pl['opt']['rd'][3] * $this->k;
  5411. $annots .= ' /RD ['.sprintf('%.2F %.2F %.2F %.2F', $l, $r, $t, $b).']';
  5412. }
  5413. //$annots .= ' /LE ';
  5414. break;
  5415. }
  5416. // ... to be completed ...
  5417. case 'line': {
  5418. break;
  5419. }
  5420. case 'square': {
  5421. break;
  5422. }
  5423. case 'circle': {
  5424. break;
  5425. }
  5426. case 'polygon': {
  5427. break;
  5428. }
  5429. case 'polyline': {
  5430. break;
  5431. }
  5432. case 'highlight': {
  5433. break;
  5434. }
  5435. case 'underline': {
  5436. break;
  5437. }
  5438. case 'squiggly': {
  5439. break;
  5440. }
  5441. case 'strikeout': {
  5442. break;
  5443. }
  5444. case 'stamp': {
  5445. break;
  5446. }
  5447. case 'caret': {
  5448. break;
  5449. }
  5450. case 'ink': {
  5451. break;
  5452. }
  5453. case 'popup': {
  5454. break;
  5455. }
  5456. case 'fileattachment': {
  5457. if (!isset($pl['opt']['fs'])) {
  5458. break;
  5459. }
  5460. $filename = basename($pl['opt']['fs']);
  5461. if (isset($this->embeddedfiles[$filename]['n'])) {
  5462. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  5463. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  5464. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5465. $annots .= ' /Name /'.$pl['opt']['name'];
  5466. } else {
  5467. $annots .= ' /Name /PushPin';
  5468. }
  5469. }
  5470. break;
  5471. }
  5472. case 'sound': {
  5473. if (!isset($pl['opt']['sound'])) {
  5474. break;
  5475. }
  5476. $filename = basename($pl['opt']['sound']);
  5477. if (isset($this->embeddedfiles[$filename]['n'])) {
  5478. // ... TO BE COMPLETED ...
  5479. $iconsapp = array('Speaker', 'Mic');
  5480. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  5481. $annots .= ' /Name /'.$pl['opt']['name'];
  5482. } else {
  5483. $annots .= ' /Name /Speaker';
  5484. }
  5485. }
  5486. break;
  5487. }
  5488. case 'movie': {
  5489. break;
  5490. }
  5491. case 'widget': {
  5492. // PDF32000_2008.pdf page 408 (... TO BE COMPLETED ...)
  5493. $hmode = array('N', 'I', 'O', 'P', 'T');
  5494. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  5495. $annots .= ' /H /'.$pl['opt']['h'];
  5496. }
  5497. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk']))) {
  5498. $annots .= ' /MK <<';
  5499. // ... TO BE COMPLETED ...
  5500. $annots .= '>>';
  5501. }
  5502. break;
  5503. }
  5504. case 'screen': {
  5505. break;
  5506. }
  5507. case 'printermark': {
  5508. break;
  5509. }
  5510. case 'trapnet': {
  5511. break;
  5512. }
  5513. case 'watermark': {
  5514. break;
  5515. }
  5516. case '3d': {
  5517. break;
  5518. }
  5519. default: {
  5520. break;
  5521. }
  5522. }
  5523. $annots .= '>>';
  5524. ++$this->annot_obj_id;
  5525. $this->offsets[$this->annot_obj_id] = $this->bufferlen;
  5526. $this->_out($this->annot_obj_id.' 0 obj');
  5527. $this->_out($annots);
  5528. $this->_out('endobj');
  5529. }
  5530. }
  5531. } // end for each page
  5532. }
  5533. /**
  5534. * Output fonts.
  5535. * @access protected
  5536. */
  5537. protected function _putfonts() {
  5538. $nf = $this->n;
  5539. foreach ($this->diffs as $diff) {
  5540. //Encodings
  5541. $this->_newobj();
  5542. $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.']>>');
  5543. $this->_out('endobj');
  5544. }
  5545. $mqr = $this->get_mqr();
  5546. $this->set_mqr(false);
  5547. foreach ($this->FontFiles as $file => $info) {
  5548. // search and get font file to embedd
  5549. $fontdir = $info['fontdir'];
  5550. $file = strtolower($file);
  5551. $fontfile = '';
  5552. // search files on various directories
  5553. if (file_exists($fontdir.$file)) {
  5554. $fontfile = $fontdir.$file;
  5555. } elseif (file_exists($this->_getfontpath().$file)) {
  5556. $fontfile = $this->_getfontpath().$file;
  5557. } elseif (file_exists($file)) {
  5558. $fontfile = $file;
  5559. }
  5560. if (!$this->empty_string($fontfile)) {
  5561. $font = file_get_contents($fontfile);
  5562. $compressed = (substr($file, -2) == '.z');
  5563. if ((!$compressed) AND (isset($info['length2']))) {
  5564. $header = (ord($font{0}) == 128);
  5565. if ($header) {
  5566. //Strip first binary header
  5567. $font = substr($font, 6);
  5568. }
  5569. if ($header AND (ord($font{$info['length1']}) == 128)) {
  5570. //Strip second binary header
  5571. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  5572. }
  5573. }
  5574. $this->_newobj();
  5575. $this->FontFiles[$file]['n'] = $this->n;
  5576. $this->_out('<</Length '.strlen($font));
  5577. if ($compressed) {
  5578. $this->_out('/Filter /FlateDecode');
  5579. }
  5580. $this->_out('/Length1 '.$info['length1']);
  5581. if (isset($info['length2'])) {
  5582. $this->_out('/Length2 '.$info['length2'].' /Length3 0');
  5583. }
  5584. $this->_out('>>');
  5585. $this->_putstream($font);
  5586. $this->_out('endobj');
  5587. }
  5588. }
  5589. $this->set_mqr($mqr);
  5590. foreach ($this->fontkeys as $k) {
  5591. //Font objects
  5592. $this->setFontSubBuffer($k, 'n', $this->n + 1);
  5593. $font = $this->getFontBuffer($k);
  5594. $type = $font['type'];
  5595. $name = $font['name'];
  5596. if ($type == 'core') {
  5597. //Standard font
  5598. $this->_newobj();
  5599. $this->_out('<</Type /Font');
  5600. $this->_out('/Subtype /Type1');
  5601. $this->_out('/BaseFont /'.$name);
  5602. $this->_out('/Name /F'.$font['i']);
  5603. if (($name != 'symbol') AND ($name != 'zapfdingbats')) {
  5604. $this->_out('/Encoding /WinAnsiEncoding');
  5605. }
  5606. $this->_out('>>');
  5607. $this->_out('endobj');
  5608. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  5609. //Additional Type1 or TrueType font
  5610. $this->_newobj();
  5611. $this->_out('<</Type /Font');
  5612. $this->_out('/Subtype /'.$type);
  5613. $this->_out('/BaseFont /'.$name);
  5614. $this->_out('/Name /F'.$font['i']);
  5615. $this->_out('/FirstChar 32 /LastChar 255');
  5616. $this->_out('/Widths '.($this->n + 1).' 0 R');
  5617. $this->_out('/FontDescriptor '.($this->n + 2).' 0 R');
  5618. if ($font['enc']) {
  5619. if (isset($font['diff'])) {
  5620. $this->_out('/Encoding '.($nf + $font['diff']).' 0 R');
  5621. } else {
  5622. $this->_out('/Encoding /WinAnsiEncoding');
  5623. }
  5624. }
  5625. $this->_out('>>');
  5626. $this->_out('endobj');
  5627. // Widths
  5628. $this->_newobj();
  5629. $cw = &$font['cw'];
  5630. $s = '[';
  5631. for ($i = 32; $i < 256; ++$i) {
  5632. $s .= $cw[$i].' ';
  5633. }
  5634. $this->_out($s.']');
  5635. $this->_out('endobj');
  5636. //Descriptor
  5637. $this->_newobj();
  5638. $s = '<</Type /FontDescriptor /FontName /'.$name;
  5639. foreach ($font['desc'] as $k => $v) {
  5640. $s .= ' /'.$k.' '.$v.'';
  5641. }
  5642. if (!$this->empty_string($font['file'])) {
  5643. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  5644. }
  5645. $this->_out($s.'>>');
  5646. $this->_out('endobj');
  5647. } else {
  5648. //Allow for additional types
  5649. $mtd = '_put'.strtolower($type);
  5650. if (!method_exists($this, $mtd)) {
  5651. $this->Error('Unsupported font type: '.$type);
  5652. }
  5653. $this->$mtd($font);
  5654. }
  5655. }
  5656. }
  5657. /**
  5658. * Outputs font widths
  5659. * @parameter array $font font data
  5660. * @parameter int $cidoffset offset for CID values
  5661. * @author Nicola Asuni
  5662. * @access protected
  5663. * @since 4.4.000 (2008-12-07)
  5664. */
  5665. protected function _putfontwidths($font, $cidoffset=0) {
  5666. ksort($font['cw']);
  5667. $rangeid = 0;
  5668. $range = array();
  5669. $prevcid = -2;
  5670. $prevwidth = -1;
  5671. $interval = false;
  5672. // for each character
  5673. foreach ($font['cw'] as $cid => $width) {
  5674. $cid -= $cidoffset;
  5675. if ($width != $font['dw']) {
  5676. if ($cid == ($prevcid + 1)) {
  5677. // consecutive CID
  5678. if ($width == $prevwidth) {
  5679. if ($width == $range[$rangeid][0]) {
  5680. $range[$rangeid][] = $width;
  5681. } else {
  5682. array_pop($range[$rangeid]);
  5683. // new range
  5684. $rangeid = $prevcid;
  5685. $range[$rangeid] = array();
  5686. $range[$rangeid][] = $prevwidth;
  5687. $range[$rangeid][] = $width;
  5688. }
  5689. $interval = true;
  5690. $range[$rangeid]['interval'] = true;
  5691. } else {
  5692. if ($interval) {
  5693. // new range
  5694. $rangeid = $cid;
  5695. $range[$rangeid] = array();
  5696. $range[$rangeid][] = $width;
  5697. } else {
  5698. $range[$rangeid][] = $width;
  5699. }
  5700. $interval = false;
  5701. }
  5702. } else {
  5703. // new range
  5704. $rangeid = $cid;
  5705. $range[$rangeid] = array();
  5706. $range[$rangeid][] = $width;
  5707. $interval = false;
  5708. }
  5709. $prevcid = $cid;
  5710. $prevwidth = $width;
  5711. }
  5712. }
  5713. // optimize ranges
  5714. $prevk = -1;
  5715. $nextk = -1;
  5716. $prevint = false;
  5717. foreach ($range as $k => $ws) {
  5718. $cws = count($ws);
  5719. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  5720. if (isset($range[$k]['interval'])) {
  5721. unset($range[$k]['interval']);
  5722. }
  5723. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  5724. unset($range[$k]);
  5725. } else {
  5726. $prevk = $k;
  5727. }
  5728. $nextk = $k + $cws;
  5729. if (isset($ws['interval'])) {
  5730. if ($cws > 3) {
  5731. $prevint = true;
  5732. } else {
  5733. $prevint = false;
  5734. }
  5735. unset($range[$k]['interval']);
  5736. --$nextk;
  5737. } else {
  5738. $prevint = false;
  5739. }
  5740. }
  5741. // output data
  5742. $w = '';
  5743. foreach ($range as $k => $ws) {
  5744. if (count(array_count_values($ws)) == 1) {
  5745. // interval mode is more compact
  5746. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  5747. } else {
  5748. // range mode
  5749. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  5750. }
  5751. }
  5752. $this->_out('/W ['.$w.' ]');
  5753. }
  5754. /**
  5755. * Adds unicode fonts.<br>
  5756. * Based on PDF Reference 1.3 (section 5)
  5757. * @parameter array $font font data
  5758. * @access protected
  5759. * @author Nicola Asuni
  5760. * @since 1.52.0.TC005 (2005-01-05)
  5761. */
  5762. protected function _puttruetypeunicode($font) {
  5763. // Type0 Font
  5764. // A composite font composed of other fonts, organized hierarchically
  5765. $this->_newobj();
  5766. $this->_out('<</Type /Font');
  5767. $this->_out('/Subtype /Type0');
  5768. $this->_out('/BaseFont /'.$font['name'].'');
  5769. $this->_out('/Name /F'.$font['i']);
  5770. $this->_out('/Encoding /'.$font['enc']);
  5771. $this->_out('/ToUnicode /Identity-H');
  5772. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  5773. $this->_out('>>');
  5774. $this->_out('endobj');
  5775. // CIDFontType2
  5776. // A CIDFont whose glyph descriptions are based on TrueType font technology
  5777. $this->_newobj();
  5778. $this->_out('<</Type /Font');
  5779. $this->_out('/Subtype /CIDFontType2');
  5780. $this->_out('/BaseFont /'.$font['name'].'');
  5781. // A dictionary containing entries that define the character collection of the CIDFont.
  5782. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  5783. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  5784. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  5785. $this->_out('/CIDSystemInfo <<'.$cidinfo.'>>');
  5786. $this->_out('/FontDescriptor '.($this->n + 1).' 0 R');
  5787. $this->_out('/DW '.$font['dw'].''); // default width
  5788. $this->_putfontwidths($font, 0);
  5789. $this->_out('/CIDToGIDMap '.($this->n + 2).' 0 R');
  5790. $this->_out('>>');
  5791. $this->_out('endobj');
  5792. // Font descriptor
  5793. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  5794. $this->_newobj();
  5795. $this->_out('<</Type /FontDescriptor');
  5796. $this->_out('/FontName /'.$font['name']);
  5797. foreach ($font['desc'] as $key => $value) {
  5798. $this->_out('/'.$key.' '.$value);
  5799. }
  5800. $fontdir = '';
  5801. if (!$this->empty_string($font['file'])) {
  5802. // A stream containing a TrueType font
  5803. $this->_out('/FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R');
  5804. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  5805. }
  5806. $this->_out('>>');
  5807. $this->_out('endobj');
  5808. $this->_newobj();
  5809. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  5810. // Embed CIDToGIDMap
  5811. // A specification of the mapping from CIDs to glyph indices
  5812. // search and get CTG font file to embedd
  5813. $ctgfile = strtolower($font['ctg']);
  5814. // search and get ctg font file to embedd
  5815. $fontfile = '';
  5816. // search files on various directories
  5817. if (file_exists($fontdir.$ctgfile)) {
  5818. $fontfile = $fontdir.$ctgfile;
  5819. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  5820. $fontfile = $this->_getfontpath().$ctgfile;
  5821. } elseif (file_exists($ctgfile)) {
  5822. $fontfile = $ctgfile;
  5823. }
  5824. if ($this->empty_string($fontfile)) {
  5825. $this->Error('Font file not found: '.$ctgfile);
  5826. }
  5827. $size = filesize($fontfile);
  5828. $this->_out('<</Length '.$size.'');
  5829. if (substr($fontfile, -2) == '.z') { // check file extension
  5830. // Decompresses data encoded using the public-domain
  5831. // zlib/deflate compression method, reproducing the
  5832. // original text or binary data
  5833. $this->_out('/Filter /FlateDecode');
  5834. }
  5835. $this->_out('>>');
  5836. $this->_putstream(file_get_contents($fontfile));
  5837. }
  5838. $this->_out('endobj');
  5839. }
  5840. /**
  5841. * Output CID-0 fonts.
  5842. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  5843. * @param array $font font data
  5844. * @access protected
  5845. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  5846. * @since 3.2.000 (2008-06-23)
  5847. */
  5848. protected function _putcidfont0($font) {
  5849. $cidoffset = 31;
  5850. if (isset($font['cidinfo']['uni2cid'])) {
  5851. // convert unicode to cid.
  5852. $uni2cid = $font['cidinfo']['uni2cid'];
  5853. $cw = array();
  5854. foreach ($font['cw'] as $uni => $width) {
  5855. if (isset($uni2cid[$uni])) {
  5856. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  5857. } elseif ($uni < 256) {
  5858. $cw[$uni] = $width;
  5859. } // else unknown character
  5860. }
  5861. $font = array_merge($font, array('cw' => $cw));
  5862. }
  5863. $name = $font['name'];
  5864. $enc = $font['enc'];
  5865. if ($enc) {
  5866. $longname = $name.'-'.$enc;
  5867. } else {
  5868. $longname = $name;
  5869. }
  5870. $this->_newobj();
  5871. $this->_out('<</Type /Font');
  5872. $this->_out('/Subtype /Type0');
  5873. $this->_out('/BaseFont /'.$longname);
  5874. $this->_out('/Name /F'.$font['i']);
  5875. if ($enc) {
  5876. $this->_out('/Encoding /'.$enc);
  5877. }
  5878. $this->_out('/DescendantFonts ['.($this->n + 1).' 0 R]');
  5879. $this->_out('>>');
  5880. $this->_out('endobj');
  5881. $this->_newobj();
  5882. $this->_out('<</Type /Font');
  5883. $this->_out('/Subtype /CIDFontType0');
  5884. $this->_out('/BaseFont /'.$name);
  5885. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry']);
  5886. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering']);
  5887. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  5888. $this->_out('/CIDSystemInfo <<'.$cidinfo.'>>');
  5889. $this->_out('/FontDescriptor '.($this->n + 1).' 0 R');
  5890. $this->_out('/DW '.$font['dw']);
  5891. $this->_putfontwidths($font, $cidoffset);
  5892. $this->_out('>>');
  5893. $this->_out('endobj');
  5894. $this->_newobj();
  5895. $s = '<</Type /FontDescriptor /FontName /'.$name;
  5896. foreach ($font['desc'] as $k => $v) {
  5897. if ($k != 'Style') {
  5898. $s .= ' /'.$k.' '.$v.'';
  5899. }
  5900. }
  5901. $this->_out($s.'>>');
  5902. $this->_out('endobj');
  5903. }
  5904. /**
  5905. * Output images.
  5906. * @access protected
  5907. */
  5908. protected function _putimages() {
  5909. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  5910. foreach ($this->imagekeys as $file) {
  5911. $info = $this->getImageBuffer($file);
  5912. $this->_newobj();
  5913. $this->setImageSubBuffer($file, 'n', $this->n);
  5914. $this->_out('<</Type /XObject');
  5915. $this->_out('/Subtype /Image');
  5916. $this->_out('/Width '.$info['w']);
  5917. $this->_out('/Height '.$info['h']);
  5918. if (isset($info['masked'])) {
  5919. $this->_out('/SMask '.($this->n - 1).' 0 R');
  5920. }
  5921. if ($info['cs'] == 'Indexed') {
  5922. $this->_out('/ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]');
  5923. } else {
  5924. $this->_out('/ColorSpace /'.$info['cs']);
  5925. if ($info['cs'] == 'DeviceCMYK') {
  5926. $this->_out('/Decode [1 0 1 0 1 0 1 0]');
  5927. }
  5928. }
  5929. $this->_out('/BitsPerComponent '.$info['bpc']);
  5930. if (isset($info['f'])) {
  5931. $this->_out('/Filter /'.$info['f']);
  5932. }
  5933. if (isset($info['parms'])) {
  5934. $this->_out($info['parms']);
  5935. }
  5936. if (isset($info['trns']) AND is_array($info['trns'])) {
  5937. $trns='';
  5938. $count_info = count($info['trns']);
  5939. for ($i=0; $i < $count_info; ++$i) {
  5940. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  5941. }
  5942. $this->_out('/Mask ['.$trns.']');
  5943. }
  5944. $this->_out('/Length '.strlen($info['data']).'>>');
  5945. $this->_putstream($info['data']);
  5946. $this->_out('endobj');
  5947. //Palette
  5948. if ($info['cs'] == 'Indexed') {
  5949. $this->_newobj();
  5950. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  5951. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>>');
  5952. $this->_putstream($pal);
  5953. $this->_out('endobj');
  5954. }
  5955. }
  5956. }
  5957. /**
  5958. * Output Spot Colors Resources.
  5959. * @access protected
  5960. * @since 4.0.024 (2008-09-12)
  5961. */
  5962. protected function _putspotcolors() {
  5963. foreach ($this->spot_colors as $name => $color) {
  5964. $this->_newobj();
  5965. $this->spot_colors[$name]['n'] = $this->n;
  5966. $this->_out('[/Separation /'.str_replace(' ', '#20', $name));
  5967. $this->_out('/DeviceCMYK <<');
  5968. $this->_out('/Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0] ');
  5969. $this->_out(sprintf('/C1 [%.4F %.4F %.4F %.4F] ', $color['c']/100, $color['m']/100, $color['y']/100, $color['k']/100));
  5970. $this->_out('/FunctionType 2 /Domain [0 1] /N 1>>]');
  5971. $this->_out('endobj');
  5972. }
  5973. }
  5974. /**
  5975. * Output object dictionary for images.
  5976. * @access protected
  5977. */
  5978. protected function _putxobjectdict() {
  5979. foreach ($this->imagekeys as $file) {
  5980. $info = $this->getImageBuffer($file);
  5981. $this->_out('/I'.$info['i'].' '.$info['n'].' 0 R');
  5982. }
  5983. }
  5984. /**
  5985. * Output Resources Dictionary.
  5986. * @access protected
  5987. */
  5988. protected function _putresourcedict() {
  5989. $this->_out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  5990. $this->_out('/Font <<');
  5991. foreach ($this->fontkeys as $fontkey) {
  5992. $font = $this->getFontBuffer($fontkey);
  5993. $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
  5994. }
  5995. $this->_out('>>');
  5996. $this->_out('/XObject <<');
  5997. $this->_putxobjectdict();
  5998. $this->_out('>>');
  5999. // visibility
  6000. $this->_out('/Properties <</OC1 '.$this->n_ocg_print.' 0 R /OC2 '.$this->n_ocg_view.' 0 R>>');
  6001. // transparency
  6002. $this->_out('/ExtGState <<');
  6003. foreach ($this->extgstates as $k => $extgstate) {
  6004. $this->_out('/GS'.$k.' '.$extgstate['n'].' 0 R');
  6005. }
  6006. $this->_out('>>');
  6007. // gradients
  6008. if (isset($this->gradients) AND (count($this->gradients) > 0)) {
  6009. $this->_out('/Shading <<');
  6010. foreach ($this->gradients as $id => $grad) {
  6011. $this->_out('/Sh'.$id.' '.$grad['id'].' 0 R');
  6012. }
  6013. $this->_out('>>');
  6014. }
  6015. // spot colors
  6016. if (isset($this->spot_colors) AND (count($this->spot_colors) > 0)) {
  6017. $this->_out('/ColorSpace <<');
  6018. foreach ($this->spot_colors as $color) {
  6019. $this->_out('/CS'.$color['i'].' '.$color['n'].' 0 R');
  6020. }
  6021. $this->_out('>>');
  6022. }
  6023. }
  6024. /**
  6025. * Output Resources.
  6026. * @access protected
  6027. */
  6028. protected function _putresources() {
  6029. $this->_putextgstates();
  6030. $this->_putocg();
  6031. $this->_putfonts();
  6032. $this->_putimages();
  6033. $this->_putspotcolors();
  6034. $this->_putshaders();
  6035. //Resource dictionary
  6036. $this->offsets[2] = $this->bufferlen;
  6037. $this->_out('2 0 obj');
  6038. $this->_out('<<');
  6039. $this->_putresourcedict();
  6040. $this->_out('>>');
  6041. $this->_out('endobj');
  6042. $this->_putjavascript();
  6043. $this->_putbookmarks();
  6044. $this->_putEmbeddedFiles();
  6045. $this->_putannotsobjs();
  6046. // encryption
  6047. if ($this->encrypted) {
  6048. $this->_newobj();
  6049. $this->enc_obj_id = $this->n;
  6050. $this->_out('<<');
  6051. $this->_putencryption();
  6052. $this->_out('>>');
  6053. $this->_out('endobj');
  6054. }
  6055. }
  6056. /**
  6057. * Adds some Metadata information
  6058. * (see Chapter 10.2 of PDF Reference)
  6059. * @access protected
  6060. */
  6061. protected function _putinfo() {
  6062. if ($this->empty_string($this->title)) {
  6063. $this->title = '?';
  6064. }
  6065. $this->_out('/Title '.$this->_textstring($this->title));
  6066. if ($this->empty_string($this->author)) {
  6067. $this->author = '?';
  6068. }
  6069. $this->_out('/Author '.$this->_textstring($this->author));
  6070. if ($this->empty_string($this->subject)) {
  6071. $this->subject = '?';
  6072. }
  6073. $this->_out('/Subject '.$this->_textstring($this->subject));
  6074. if ($this->empty_string($this->keywords)) {
  6075. $this->keywords = '?';
  6076. }
  6077. $this->_out('/Keywords '.$this->_textstring($this->keywords));
  6078. if ($this->empty_string($this->creator)) {
  6079. $this->creator = '?';
  6080. }
  6081. $this->_out('/Creator '.$this->_textstring($this->creator));
  6082. if (defined('PDF_PRODUCER')) {
  6083. $this->_out('/Producer '.$this->_textstring(PDF_PRODUCER));
  6084. } else {
  6085. $this->_out('/Producer '.$this->_textstring('TCPDF'));
  6086. }
  6087. $this->_out('/CreationDate '.$this->_datestring());
  6088. $this->_out('/ModDate '.$this->_datestring());
  6089. }
  6090. /**
  6091. * Output Catalog.
  6092. * @access protected
  6093. */
  6094. protected function _putcatalog() {
  6095. $this->_out('/Type /Catalog');
  6096. $this->_out('/Pages 1 0 R');
  6097. if ($this->ZoomMode == 'fullpage') {
  6098. $this->_out('/OpenAction [3 0 R /Fit]');
  6099. } elseif ($this->ZoomMode == 'fullwidth') {
  6100. $this->_out('/OpenAction [3 0 R /FitH null]');
  6101. } elseif ($this->ZoomMode == 'real') {
  6102. $this->_out('/OpenAction [3 0 R /XYZ null null 1]');
  6103. } elseif (!is_string($this->ZoomMode)) {
  6104. $this->_out('/OpenAction [3 0 R /XYZ null null '.($this->ZoomMode / 100).']');
  6105. }
  6106. // signatures
  6107. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  6108. if ($this->signature_data['cert_type'] > 0) {
  6109. $this->_out('/AcroForm<<');
  6110. $this->_out('/Fields ['.$this->sig_obj_id.' 0 R]');
  6111. $this->_out('/NeedAppearances false');
  6112. $this->_out('/SigFlags 3');
  6113. $this->_out('>>');
  6114. $this->_out('/Perms<</DocMDP '.($this->sig_obj_id + 1).' 0 R>>');
  6115. } else {
  6116. $this->_out('/Perms<</UR3 '.($this->sig_obj_id + 1).' 0 R>>');
  6117. }
  6118. }
  6119. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  6120. $this->_out('/PageLayout /'.$this->LayoutMode.'');
  6121. }
  6122. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  6123. $this->_out('/PageMode /'.$this->PageMode);
  6124. }
  6125. if (isset($this->l['a_meta_language'])) {
  6126. $this->_out('/Lang /'.$this->l['a_meta_language']);
  6127. }
  6128. $this->_out('/Names <<');
  6129. if (!$this->empty_string($this->javascript)) {
  6130. $this->_out('/JavaScript '.($this->n_js).' 0 R');
  6131. }
  6132. $this->_out('>>');
  6133. if (count($this->outlines) > 0) {
  6134. $this->_out('/Outlines '.$this->OutlineRoot.' 0 R');
  6135. $this->_out('/PageMode /UseOutlines');
  6136. }
  6137. $this->_putviewerpreferences();
  6138. $p = $this->n_ocg_print.' 0 R';
  6139. $v = $this->n_ocg_view.' 0 R';
  6140. $as = '<</Event /Print /OCGs ['.$p.' '.$v.'] /Category [/Print]>> <</Event /View /OCGs ['.$p.' '.$v.'] /Category [/View]>>';
  6141. $this->_out('/OCProperties <</OCGs ['.$p.' '.$v.'] /D <</ON ['.$p.'] /OFF ['.$v.'] /AS ['.$as.']>>>>');
  6142. }
  6143. /**
  6144. * Output viewer preferences.
  6145. * @author Nicola asuni
  6146. * @since 3.1.000 (2008-06-09)
  6147. * @access protected
  6148. */
  6149. protected function _putviewerpreferences() {
  6150. $this->_out('/ViewerPreferences<<');
  6151. if ($this->rtl) {
  6152. $this->_out('/Direction /R2L');
  6153. } else {
  6154. $this->_out('/Direction /L2R');
  6155. }
  6156. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  6157. $this->_out('/HideToolbar true');
  6158. }
  6159. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  6160. $this->_out('/HideMenubar true');
  6161. }
  6162. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  6163. $this->_out('/HideWindowUI true');
  6164. }
  6165. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  6166. $this->_out('/FitWindow true');
  6167. }
  6168. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  6169. $this->_out('/CenterWindow true');
  6170. }
  6171. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  6172. $this->_out('/DisplayDocTitle true');
  6173. }
  6174. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  6175. $this->_out('/NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'].'');
  6176. }
  6177. if (isset($this->viewer_preferences['ViewArea'])) {
  6178. $this->_out('/ViewArea /'.$this->viewer_preferences['ViewArea']);
  6179. }
  6180. if (isset($this->viewer_preferences['ViewClip'])) {
  6181. $this->_out('/ViewClip /'.$this->viewer_preferences['ViewClip']);
  6182. }
  6183. if (isset($this->viewer_preferences['PrintArea'])) {
  6184. $this->_out('/PrintArea /'.$this->viewer_preferences['PrintArea']);
  6185. }
  6186. if (isset($this->viewer_preferences['PrintClip'])) {
  6187. $this->_out('/PrintClip /'.$this->viewer_preferences['PrintClip']);
  6188. }
  6189. if (isset($this->viewer_preferences['PrintScaling'])) {
  6190. $this->_out('/PrintScaling /'.$this->viewer_preferences['PrintScaling']);
  6191. }
  6192. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  6193. $this->_out('/Duplex /'.$this->viewer_preferences['Duplex']);
  6194. }
  6195. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  6196. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  6197. $this->_out('/PickTrayByPDFSize true');
  6198. } else {
  6199. $this->_out('/PickTrayByPDFSize false');
  6200. }
  6201. }
  6202. if (isset($this->viewer_preferences['PrintPageRange'])) {
  6203. $PrintPageRangeNum = '';
  6204. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  6205. $PrintPageRangeNum .= ' '.($v - 1).'';
  6206. }
  6207. $this->_out('/PrintPageRange ['.substr($PrintPageRangeNum,1).']');
  6208. }
  6209. if (isset($this->viewer_preferences['NumCopies'])) {
  6210. $this->_out('/NumCopies '.intval($this->viewer_preferences['NumCopies']));
  6211. }
  6212. $this->_out('>>');
  6213. }
  6214. /**
  6215. * Output trailer.
  6216. * @access protected
  6217. */
  6218. protected function _puttrailer() {
  6219. $this->_out('/Size '.($this->n + 1));
  6220. $this->_out('/Root '.$this->n.' 0 R');
  6221. $this->_out('/Info '.($this->n - 1).' 0 R');
  6222. if ($this->encrypted) {
  6223. $this->_out('/Encrypt '.$this->enc_obj_id.' 0 R');
  6224. $this->_out('/ID [()()]');
  6225. }
  6226. }
  6227. /**
  6228. * Output PDF header.
  6229. * @access protected
  6230. */
  6231. protected function _putheader() {
  6232. $this->_out('%PDF-'.$this->PDFVersion);
  6233. }
  6234. /**
  6235. * Output end of document (EOF).
  6236. * @access protected
  6237. */
  6238. protected function _enddoc() {
  6239. $this->state = 1;
  6240. $this->_putheader();
  6241. $this->_putpages();
  6242. $this->_putresources();
  6243. // Signature
  6244. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  6245. // widget annotation for signature
  6246. $this->sig_obj_id = $this->_newobj();
  6247. // --- replace signature ID on the first page ---
  6248. // get the document content
  6249. $pdfdoc = $this->getBuffer();
  6250. // Remove the original buffer
  6251. if (isset($this->diskcache) AND $this->diskcache) {
  6252. // remove buffer file from cache
  6253. unlink($this->buffer);
  6254. }
  6255. unset($this->buffer);
  6256. $signature_widget_ref = sprintf('%u 0 R', $this->sig_obj_id);
  6257. $signature_widget_ref .= str_repeat(' ', (strlen($this->sig_annot_ref) - strlen($signature_widget_ref)));
  6258. $pdfdoc = str_replace($this->sig_annot_ref, $signature_widget_ref, $pdfdoc);
  6259. $this->diskcache = false;
  6260. $this->buffer = &$pdfdoc;
  6261. $this->bufferlen = strlen($pdfdoc);
  6262. // ---
  6263. $this->_out('<<');
  6264. $this->_out('/Type /Annot /Subtype /Widget /Rect [0 0 0 0]');
  6265. $this->_out('/P 3 0 R'); // link to first page object
  6266. $this->_out('/FT /Sig');
  6267. $this->_out('/T '.$this->_textstring('Signature'));
  6268. $this->_out('/Ff 0');
  6269. $this->_out('/V '.($this->sig_obj_id + 1).' 0 R');
  6270. $this->_out('>>');
  6271. $this->_out('endobj');
  6272. // signature
  6273. $this->_newobj();
  6274. $this->_out('<<');
  6275. $this->_putsignature();
  6276. $this->_putursignature();
  6277. $this->_out('>>');
  6278. $this->_out('endobj');
  6279. }
  6280. // Info
  6281. $this->_newobj();
  6282. $this->_out('<<');
  6283. $this->_putinfo();
  6284. $this->_out('>>');
  6285. $this->_out('endobj');
  6286. // Catalog
  6287. $this->_newobj();
  6288. $this->_out('<<');
  6289. $this->_putcatalog();
  6290. $this->_out('>>');
  6291. $this->_out('endobj');
  6292. // Cross-ref
  6293. $o = $this->bufferlen;
  6294. $this->_out('xref');
  6295. $this->_out('0 '.($this->n + 1));
  6296. $this->_out('0000000000 65535 f ');
  6297. for ($i=1; $i <= $this->n; ++$i) {
  6298. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6299. }
  6300. // Embedded Files
  6301. if (isset($this->embeddedfiles) AND count($this->embeddedfiles) > 0) {
  6302. $this->_out($this->embedded_start_obj_id.' '.count($this->embeddedfiles));
  6303. foreach ($this->embeddedfiles as $filename => $filedata) {
  6304. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$filedata['n']]));
  6305. }
  6306. }
  6307. // Annotation Objects
  6308. if ($this->annot_obj_id > $this->annots_start_obj_id) {
  6309. $this->_out(($this->annots_start_obj_id + 1).' '.($this->annot_obj_id - $this->annots_start_obj_id));
  6310. for ($i = ($this->annots_start_obj_id + 1); $i <= $this->annot_obj_id; ++$i) {
  6311. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  6312. }
  6313. }
  6314. //Trailer
  6315. $this->_out('trailer');
  6316. $this->_out('<<');
  6317. $this->_puttrailer();
  6318. $this->_out('>>');
  6319. $this->_out('startxref');
  6320. $this->_out($o);
  6321. $this->_out('%%EOF');
  6322. $this->state = 3; // end-of-doc
  6323. if ($this->diskcache) {
  6324. // remove temporary files used for images
  6325. foreach ($this->imagekeys as $key) {
  6326. // remove temporary files
  6327. unlink($this->images[$key]);
  6328. }
  6329. foreach ($this->fontkeys as $key) {
  6330. // remove temporary files
  6331. unlink($this->fonts[$key]);
  6332. }
  6333. }
  6334. }
  6335. /**
  6336. * Initialize a new page.
  6337. * @param string $orientation page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  6338. * @param mixed $format The format used for pages. It can be either one of the following values (case insensitive) or a custom format in the form of a two-element array containing the width and the height (expressed in the unit given by unit).<ul><li>4A0</li><li>2A0</li><li>A0</li><li>A1</li><li>A2</li><li>A3</li><li>A4 (default)</li><li>A5</li><li>A6</li><li>A7</li><li>A8</li><li>A9</li><li>A10</li><li>B0</li><li>B1</li><li>B2</li><li>B3</li><li>B4</li><li>B5</li><li>B6</li><li>B7</li><li>B8</li><li>B9</li><li>B10</li><li>C0</li><li>C1</li><li>C2</li><li>C3</li><li>C4</li><li>C5</li><li>C6</li><li>C7</li><li>C8</li><li>C9</li><li>C10</li><li>RA0</li><li>RA1</li><li>RA2</li><li>RA3</li><li>RA4</li><li>SRA0</li><li>SRA1</li><li>SRA2</li><li>SRA3</li><li>SRA4</li><li>LETTER</li><li>LEGAL</li><li>EXECUTIVE</li><li>FOLIO</li></ul>
  6339. * @access protected
  6340. */
  6341. protected function _beginpage($orientation='', $format='') {
  6342. ++$this->page;
  6343. $this->setPageBuffer($this->page, '');
  6344. // initialize array for graphics tranformation positions inside a page buffer
  6345. $this->transfmrk[$this->page] = array();
  6346. $this->state = 2;
  6347. if ($this->empty_string($orientation)) {
  6348. if (isset($this->CurOrientation)) {
  6349. $orientation = $this->CurOrientation;
  6350. } else {
  6351. $orientation = 'P';
  6352. }
  6353. }
  6354. if ($this->empty_string($format)) {
  6355. $this->setPageOrientation($orientation);
  6356. } else {
  6357. $this->setPageFormat($format, $orientation);
  6358. }
  6359. if ($this->rtl) {
  6360. $this->x = $this->w - $this->rMargin;
  6361. } else {
  6362. $this->x = $this->lMargin;
  6363. }
  6364. $this->y = $this->tMargin;
  6365. if (isset($this->newpagegroup[$this->page])) {
  6366. // start a new group
  6367. $n = sizeof($this->pagegroups) + 1;
  6368. $alias = '{nb'.$n.'}';
  6369. $this->pagegroups[$alias] = 1;
  6370. $this->currpagegroup = $alias;
  6371. } elseif ($this->currpagegroup) {
  6372. ++$this->pagegroups[$this->currpagegroup];
  6373. }
  6374. }
  6375. /**
  6376. * Mark end of page.
  6377. * @access protected
  6378. */
  6379. protected function _endpage() {
  6380. $this->setVisibility('all');
  6381. $this->state = 1;
  6382. }
  6383. /**
  6384. * Begin a new object and return the object number.
  6385. * @return int object number
  6386. * @access protected
  6387. */
  6388. protected function _newobj() {
  6389. ++$this->n;
  6390. $this->offsets[$this->n] = $this->bufferlen;
  6391. $this->_out($this->n.' 0 obj');
  6392. return $this->n;
  6393. }
  6394. /**
  6395. * Underline text.
  6396. * @param int $x X coordinate
  6397. * @param int $y Y coordinate
  6398. * @param string $txt text to underline
  6399. * @access protected
  6400. */
  6401. protected function _dounderline($x, $y, $txt) {
  6402. $up = $this->CurrentFont['up'];
  6403. $ut = $this->CurrentFont['ut'];
  6404. $w = $this->GetStringWidth($txt);
  6405. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - ($y - $up / 1000 * $this->FontSize)) * $this->k, $w * $this->k, -$ut / 1000 * $this->FontSizePt);
  6406. }
  6407. /**
  6408. * Line through text.
  6409. * @param int $x X coordinate
  6410. * @param int $y Y coordinate
  6411. * @param string $txt text to linethrough
  6412. * @access protected
  6413. */
  6414. protected function _dolinethrough($x, $y, $txt) {
  6415. $up = $this->CurrentFont['up'];
  6416. $ut = $this->CurrentFont['ut'];
  6417. $w = $this->GetStringWidth($txt);
  6418. return sprintf('%.2F %.2F %.2F %.2F re f', $x * $this->k, ($this->h - ($y - ($this->FontSize/2) - $up / 1000 * $this->FontSize)) * $this->k, $w * $this->k, -$ut / 1000 * $this->FontSizePt);
  6419. }
  6420. /**
  6421. * Read a 4-byte integer from file.
  6422. * @param string $f file name.
  6423. * @return 4-byte integer
  6424. * @access protected
  6425. */
  6426. protected function _freadint($f) {
  6427. $a = unpack('Ni', fread($f, 4));
  6428. return $a['i'];
  6429. }
  6430. /**
  6431. * Add "\" before "\", "(" and ")"
  6432. * @param string $s string to escape.
  6433. * @return string escaped string.
  6434. * @access protected
  6435. */
  6436. protected function _escape($s) {
  6437. // the chr(13) substitution fixes the Bugs item #1421290.
  6438. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  6439. }
  6440. /**
  6441. * Format a data string for meta information
  6442. * @param string $s date string to escape.
  6443. * @return string escaped string.
  6444. * @access protected
  6445. */
  6446. protected function _datastring($s) {
  6447. if ($this->encrypted) {
  6448. $s = $this->_RC4($this->_objectkey($this->n), $s);
  6449. }
  6450. return '('. $this->_escape($s).')';
  6451. }
  6452. /**
  6453. * Returns a formatted date for meta information
  6454. * @return string escaped date string.
  6455. * @access protected
  6456. * @since 4.6.028 (2009-08-25)
  6457. */
  6458. protected function _datestring() {
  6459. $current_time = substr_replace(date('YmdHisO'), '\'', (0 - 2), 0).'\'';
  6460. return $this->_datastring('D:'.$current_time);
  6461. }
  6462. /**
  6463. * Format a text string for meta information
  6464. * @param string $s string to escape.
  6465. * @return string escaped string.
  6466. * @access protected
  6467. */
  6468. protected function _textstring($s) {
  6469. if ($this->isunicode) {
  6470. //Convert string to UTF-16BE
  6471. $s = $this->UTF8ToUTF16BE($s, true);
  6472. }
  6473. return $this->_datastring($s);
  6474. }
  6475. /**
  6476. * Format a text string
  6477. * @param string $s string to escape.
  6478. * @return string escaped string.
  6479. * @access protected
  6480. */
  6481. protected function _escapetext($s) {
  6482. if ($this->isunicode) {
  6483. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  6484. $s = $this->UTF8ToLatin1($s);
  6485. } else {
  6486. //Convert string to UTF-16BE and reverse RTL language
  6487. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  6488. }
  6489. }
  6490. return $this->_escape($s);
  6491. }
  6492. /**
  6493. * Output a stream.
  6494. * @param string $s string to output.
  6495. * @access protected
  6496. */
  6497. protected function _putstream($s) {
  6498. if ($this->encrypted) {
  6499. $s = $this->_RC4($this->_objectkey($this->n), $s);
  6500. }
  6501. $this->_out('stream');
  6502. $this->_out($s);
  6503. $this->_out('endstream');
  6504. }
  6505. /**
  6506. * Output a string to the document.
  6507. * @param string $s string to output.
  6508. * @access protected
  6509. */
  6510. protected function _out($s) {
  6511. if ($this->state == 2) {
  6512. if ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  6513. // puts data before page footer
  6514. $pagebuff = $this->getPageBuffer($this->page);
  6515. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  6516. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  6517. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  6518. // update footer position
  6519. $this->footerpos[$this->page] += strlen($s."\n");
  6520. } else {
  6521. $this->setPageBuffer($this->page, $s."\n", true);
  6522. }
  6523. } else {
  6524. $this->setBuffer($s."\n");
  6525. }
  6526. }
  6527. /**
  6528. * Converts UTF-8 strings to codepoints array.<br>
  6529. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  6530. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  6531. * <pre>
  6532. * Char. number range | UTF-8 octet sequence
  6533. * (hexadecimal) | (binary)
  6534. * --------------------+-----------------------------------------------
  6535. * 0000 0000-0000 007F | 0xxxxxxx
  6536. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  6537. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  6538. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  6539. * ---------------------------------------------------------------------
  6540. *
  6541. * ABFN notation:
  6542. * ---------------------------------------------------------------------
  6543. * UTF8-octets = *( UTF8-char )
  6544. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  6545. * UTF8-1 = %x00-7F
  6546. * UTF8-2 = %xC2-DF UTF8-tail
  6547. *
  6548. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  6549. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  6550. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  6551. * %xF4 %x80-8F 2( UTF8-tail )
  6552. * UTF8-tail = %x80-BF
  6553. * ---------------------------------------------------------------------
  6554. * </pre>
  6555. * @param string $str string to process.
  6556. * @return array containing codepoints (UTF-8 characters values)
  6557. * @access protected
  6558. * @author Nicola Asuni
  6559. * @since 1.53.0.TC005 (2005-01-05)
  6560. */
  6561. protected function UTF8StringToArray($str) {
  6562. if (isset($this->cache_UTF8StringToArray['_'.$str])) {
  6563. // return cached value
  6564. return($this->cache_UTF8StringToArray['_'.$str]);
  6565. }
  6566. // check cache size
  6567. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  6568. // remove first element
  6569. array_shift($this->cache_UTF8StringToArray);
  6570. }
  6571. ++$this->cache_size_UTF8StringToArray;
  6572. if (!$this->isunicode) {
  6573. // split string into array of equivalent codes
  6574. $strarr = array();
  6575. $strlen = strlen($str);
  6576. for ($i=0; $i < $strlen; ++$i) {
  6577. $strarr[] = ord($str{$i});
  6578. }
  6579. // insert new value on cache
  6580. $this->cache_UTF8StringToArray['_'.$str] = $strarr;
  6581. return $strarr;
  6582. }
  6583. $unicode = array(); // array containing unicode values
  6584. $bytes = array(); // array containing single character byte sequences
  6585. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  6586. $str .= ''; // force $str to be a string
  6587. $length = strlen($str);
  6588. for ($i = 0; $i < $length; ++$i) {
  6589. $char = ord($str{$i}); // get one string character at time
  6590. if (count($bytes) == 0) { // get starting octect
  6591. if ($char <= 0x7F) {
  6592. $unicode[] = $char; // use the character "as is" because is ASCII
  6593. $numbytes = 1;
  6594. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  6595. $bytes[] = ($char - 0xC0) << 0x06;
  6596. $numbytes = 2;
  6597. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  6598. $bytes[] = ($char - 0xE0) << 0x0C;
  6599. $numbytes = 3;
  6600. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  6601. $bytes[] = ($char - 0xF0) << 0x12;
  6602. $numbytes = 4;
  6603. } else {
  6604. // use replacement character for other invalid sequences
  6605. $unicode[] = 0xFFFD;
  6606. $bytes = array();
  6607. $numbytes = 1;
  6608. }
  6609. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  6610. $bytes[] = $char - 0x80;
  6611. if (count($bytes) == $numbytes) {
  6612. // compose UTF-8 bytes to a single unicode value
  6613. $char = $bytes[0];
  6614. for ($j = 1; $j < $numbytes; ++$j) {
  6615. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  6616. }
  6617. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  6618. /* The definition of UTF-8 prohibits encoding character numbers between
  6619. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  6620. encoding form (as surrogate pairs) and do not directly represent
  6621. characters. */
  6622. $unicode[] = 0xFFFD; // use replacement character
  6623. } else {
  6624. $unicode[] = $char; // add char to array
  6625. }
  6626. // reset data for next char
  6627. $bytes = array();
  6628. $numbytes = 1;
  6629. }
  6630. } else {
  6631. // use replacement character for other invalid sequences
  6632. $unicode[] = 0xFFFD;
  6633. $bytes = array();
  6634. $numbytes = 1;
  6635. }
  6636. }
  6637. // insert new value on cache
  6638. $this->cache_UTF8StringToArray['_'.$str] = $unicode;
  6639. return $unicode;
  6640. }
  6641. /**
  6642. * Converts UTF-8 strings to UTF16-BE.<br>
  6643. * @param string $str string to process.
  6644. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  6645. * @return string
  6646. * @access protected
  6647. * @author Nicola Asuni
  6648. * @since 1.53.0.TC005 (2005-01-05)
  6649. * @uses UTF8StringToArray(), arrUTF8ToUTF16BE()
  6650. */
  6651. protected function UTF8ToUTF16BE($str, $setbom=true) {
  6652. if (!$this->isunicode) {
  6653. return $str; // string is not in unicode
  6654. }
  6655. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  6656. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  6657. }
  6658. /**
  6659. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  6660. * @param string $str string to process.
  6661. * @return string
  6662. * @author Andrew Whitehead, Nicola Asuni
  6663. * @access protected
  6664. * @since 3.2.000 (2008-06-23)
  6665. */
  6666. protected function UTF8ToLatin1($str) {
  6667. global $utf8tolatin;
  6668. if (!$this->isunicode) {
  6669. return $str; // string is not in unicode
  6670. }
  6671. $outstr = ''; // string to be returned
  6672. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  6673. foreach ($unicode as $char) {
  6674. if ($char < 256) {
  6675. $outstr .= chr($char);
  6676. } elseif (array_key_exists($char, $utf8tolatin)) {
  6677. // map from UTF-8
  6678. $outstr .= chr($utf8tolatin[$char]);
  6679. } elseif ($char == 0xFFFD) {
  6680. // skip
  6681. } else {
  6682. $outstr .= '?';
  6683. }
  6684. }
  6685. return $outstr;
  6686. }
  6687. /**
  6688. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  6689. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  6690. * <pre>
  6691. * Encoding UTF-16:
  6692. *
  6693. * Encoding of a single character from an ISO 10646 character value to
  6694. * UTF-16 proceeds as follows. Let U be the character number, no greater
  6695. * than 0x10FFFF.
  6696. *
  6697. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  6698. * terminate.
  6699. *
  6700. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  6701. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  6702. * represented in 20 bits.
  6703. *
  6704. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  6705. * 0xDC00, respectively. These integers each have 10 bits free to
  6706. * encode the character value, for a total of 20 bits.
  6707. *
  6708. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  6709. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  6710. * bits of W2. Terminate.
  6711. *
  6712. * Graphically, steps 2 through 4 look like:
  6713. * U' = yyyyyyyyyyxxxxxxxxxx
  6714. * W1 = 110110yyyyyyyyyy
  6715. * W2 = 110111xxxxxxxxxx
  6716. * </pre>
  6717. * @param array $unicode array containing UTF-8 unicode values
  6718. * @param boolean $setbom if true set the Byte Order Mark (BOM = 0xFEFF)
  6719. * @return string
  6720. * @access protected
  6721. * @author Nicola Asuni
  6722. * @since 2.1.000 (2008-01-08)
  6723. * @see UTF8ToUTF16BE()
  6724. */
  6725. protected function arrUTF8ToUTF16BE($unicode, $setbom=true) {
  6726. $outstr = ''; // string to be returned
  6727. if ($setbom) {
  6728. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  6729. }
  6730. foreach ($unicode as $char) {
  6731. if ($char == 0xFFFD) {
  6732. $outstr .= "\xFF\xFD"; // replacement character
  6733. } elseif ($char < 0x10000) {
  6734. $outstr .= chr($char >> 0x08);
  6735. $outstr .= chr($char & 0xFF);
  6736. } else {
  6737. $char -= 0x10000;
  6738. $w1 = 0xD800 | ($char >> 0x10);
  6739. $w2 = 0xDC00 | ($char & 0x3FF);
  6740. $outstr .= chr($w1 >> 0x08);
  6741. $outstr .= chr($w1 & 0xFF);
  6742. $outstr .= chr($w2 >> 0x08);
  6743. $outstr .= chr($w2 & 0xFF);
  6744. }
  6745. }
  6746. return $outstr;
  6747. }
  6748. // ====================================================
  6749. /**
  6750. * Set header font.
  6751. * @param array $font font
  6752. * @access public
  6753. * @since 1.1
  6754. */
  6755. public function setHeaderFont($font) {
  6756. $this->header_font = $font;
  6757. }
  6758. /**
  6759. * Get header font.
  6760. * @return array()
  6761. * @access public
  6762. * @since 4.0.012 (2008-07-24)
  6763. */
  6764. public function getHeaderFont() {
  6765. return $this->header_font;
  6766. }
  6767. /**
  6768. * Set footer font.
  6769. * @param array $font font
  6770. * @access public
  6771. * @since 1.1
  6772. */
  6773. public function setFooterFont($font) {
  6774. $this->footer_font = $font;
  6775. }
  6776. /**
  6777. * Get Footer font.
  6778. * @return array()
  6779. * @access public
  6780. * @since 4.0.012 (2008-07-24)
  6781. */
  6782. public function getFooterFont() {
  6783. return $this->footer_font;
  6784. }
  6785. /**
  6786. * Set language array.
  6787. * @param array $language
  6788. * @access public
  6789. * @since 1.1
  6790. */
  6791. public function setLanguageArray($language) {
  6792. $this->l = $language;
  6793. if (isset($this->l['a_meta_dir'])) {
  6794. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  6795. } else {
  6796. $this->rtl = false;
  6797. }
  6798. }
  6799. /**
  6800. * Returns the PDF data.
  6801. * @access public
  6802. */
  6803. public function getPDFData() {
  6804. if ($this->state < 3) {
  6805. $this->Close();
  6806. }
  6807. return $this->buffer;
  6808. }
  6809. /**
  6810. * Output anchor link.
  6811. * @param string $url link URL or internal link (i.e.: &lt;a href="#23"&gt;link to page 23&lt;/a&gt;)
  6812. * @param string $name link name
  6813. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  6814. * @param boolean $firstline if true prints only the first line and return the remaining string.
  6815. * @param array $color array of RGB text color
  6816. * @param string $style font style (U, D, B, I)
  6817. * @return the number of cells used or the remaining text if $firstline = true;
  6818. * @access public
  6819. */
  6820. public function addHtmlLink($url, $name, $fill=0, $firstline=false, $color='', $style=-1) {
  6821. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  6822. // convert url to internal link
  6823. $page = intval(substr($url, 1));
  6824. $url = $this->AddLink();
  6825. $this->SetLink($url, 0, $page);
  6826. }
  6827. // store current settings
  6828. $prevcolor = $this->fgcolor;
  6829. $prevstyle = $this->FontStyle;
  6830. if (empty($color)) {
  6831. $this->SetTextColorArray($this->htmlLinkColorArray);
  6832. } else {
  6833. $this->SetTextColorArray($color);
  6834. }
  6835. if ($style == -1) {
  6836. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  6837. } else {
  6838. $this->SetFont('', $this->FontStyle.$style);
  6839. }
  6840. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline);
  6841. // restore settings
  6842. $this->SetFont('', $prevstyle);
  6843. $this->SetTextColorArray($prevcolor);
  6844. return $ret;
  6845. }
  6846. /**
  6847. * Returns an associative array (keys: R,G,B) from an html color name or a six-digit or three-digit hexadecimal color representation (i.e. #3FE5AA or #7FF).
  6848. * @param string $color html color
  6849. * @return array RGB color or false in case of error.
  6850. * @access public
  6851. */
  6852. public function convertHTMLColorToDec($color='#FFFFFF') {
  6853. global $webcolor;
  6854. $returncolor = false;
  6855. $color = preg_replace('/[\s]*/', '', $color); // remove extra spaces
  6856. $color = strtolower($color);
  6857. if (strlen($color) == 0) {
  6858. return false;
  6859. }
  6860. if (substr($color, 0, 3) == 'rgb') {
  6861. $codes = substr($color, 4);
  6862. $codes = str_replace(')', '', $codes);
  6863. $returncolor = explode(',', $codes, 3);
  6864. return $returncolor;
  6865. }
  6866. if (substr($color, 0, 1) != '#') {
  6867. // decode color name
  6868. if (isset($webcolor[$color])) {
  6869. $color_code = $webcolor[$color];
  6870. } else {
  6871. return false;
  6872. }
  6873. } else {
  6874. $color_code = substr($color, 1);
  6875. }
  6876. switch (strlen($color_code)) {
  6877. case 3: {
  6878. // three-digit hexadecimal representation
  6879. $r = substr($color_code, 0, 1);
  6880. $g = substr($color_code, 1, 1);
  6881. $b = substr($color_code, 2, 1);
  6882. $returncolor['R'] = hexdec($r.$r);
  6883. $returncolor['G'] = hexdec($g.$g);
  6884. $returncolor['B'] = hexdec($b.$b);
  6885. break;
  6886. }
  6887. case 6: {
  6888. // six-digit hexadecimal representation
  6889. $returncolor['R'] = hexdec(substr($color_code, 0, 2));
  6890. $returncolor['G'] = hexdec(substr($color_code, 2, 2));
  6891. $returncolor['B'] = hexdec(substr($color_code, 4, 2));
  6892. break;
  6893. }
  6894. }
  6895. return $returncolor;
  6896. }
  6897. /**
  6898. * Converts pixels to User's Units.
  6899. * @param int $px pixels
  6900. * @return float value in user's unit
  6901. * @access public
  6902. * @see setImageScale(), getImageScale()
  6903. */
  6904. public function pixelsToUnits($px) {
  6905. return ($px / ($this->imgscale * $this->k));
  6906. }
  6907. /**
  6908. * Reverse function for htmlentities.
  6909. * Convert entities in UTF-8.
  6910. * @param $text_to_convert Text to convert.
  6911. * @return string converted
  6912. * @access public
  6913. */
  6914. public function unhtmlentities($text_to_convert) {
  6915. return html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  6916. }
  6917. // ENCRYPTION METHODS ----------------------------------
  6918. // SINCE 2.0.000 (2008-01-02)
  6919. /**
  6920. * Compute encryption key depending on object number where the encrypted data is stored
  6921. * @param int $n object number
  6922. * @access protected
  6923. * @since 2.0.000 (2008-01-02)
  6924. */
  6925. protected function _objectkey($n) {
  6926. return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10);
  6927. }
  6928. /**
  6929. * Put encryption on PDF document.
  6930. * @access protected
  6931. * @since 2.0.000 (2008-01-02)
  6932. */
  6933. protected function _putencryption() {
  6934. $this->_out('/Filter /Standard');
  6935. $this->_out('/V 1');
  6936. $this->_out('/R 2');
  6937. $this->_out('/O ('.$this->_escape($this->Ovalue).')');
  6938. $this->_out('/U ('.$this->_escape($this->Uvalue).')');
  6939. $this->_out('/P '.$this->Pvalue);
  6940. }
  6941. /**
  6942. * Returns the input text exrypted using RC4 algorithm and the specified key.
  6943. * RC4 is the standard encryption algorithm used in PDF format
  6944. * @param string $key encryption key
  6945. * @param String $text input text to be encrypted
  6946. * @return String encrypted text
  6947. * @access protected
  6948. * @since 2.0.000 (2008-01-02)
  6949. * @author Klemen Vodopivec
  6950. */
  6951. protected function _RC4($key, $text) {
  6952. if ($this->last_rc4_key != $key) {
  6953. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  6954. $rc4 = range(0, 255);
  6955. $j = 0;
  6956. for ($i = 0; $i < 256; ++$i) {
  6957. $t = $rc4[$i];
  6958. $j = ($j + $t + ord($k{$i})) % 256;
  6959. $rc4[$i] = $rc4[$j];
  6960. $rc4[$j] = $t;
  6961. }
  6962. $this->last_rc4_key = $key;
  6963. $this->last_rc4_key_c = $rc4;
  6964. } else {
  6965. $rc4 = $this->last_rc4_key_c;
  6966. }
  6967. $len = strlen($text);
  6968. $a = 0;
  6969. $b = 0;
  6970. $out = '';
  6971. for ($i = 0; $i < $len; ++$i) {
  6972. $a = ($a + 1) % 256;
  6973. $t = $rc4[$a];
  6974. $b = ($b + $t) % 256;
  6975. $rc4[$a] = $rc4[$b];
  6976. $rc4[$b] = $t;
  6977. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  6978. $out .= chr(ord($text{$i}) ^ $k);
  6979. }
  6980. return $out;
  6981. }
  6982. /**
  6983. * Encrypts a string using MD5 and returns it's value as a binary string.
  6984. * @param string $str input string
  6985. * @return String MD5 encrypted binary string
  6986. * @access protected
  6987. * @since 2.0.000 (2008-01-02)
  6988. * @author Klemen Vodopivec
  6989. */
  6990. protected function _md5_16($str) {
  6991. return pack('H*', md5($str));
  6992. }
  6993. /**
  6994. * Compute O value (used for RC4 encryption)
  6995. * @param String $user_pass user password
  6996. * @param String $owner_pass user password
  6997. * @return String O value
  6998. * @access protected
  6999. * @since 2.0.000 (2008-01-02)
  7000. * @author Klemen Vodopivec
  7001. */
  7002. protected function _Ovalue($user_pass, $owner_pass) {
  7003. $tmp = $this->_md5_16($owner_pass);
  7004. $owner_RC4_key = substr($tmp, 0, 5);
  7005. return $this->_RC4($owner_RC4_key, $user_pass);
  7006. }
  7007. /**
  7008. * Compute U value (used for RC4 encryption)
  7009. * @return String U value
  7010. * @access protected
  7011. * @since 2.0.000 (2008-01-02)
  7012. * @author Klemen Vodopivec
  7013. */
  7014. protected function _Uvalue() {
  7015. return $this->_RC4($this->encryption_key, $this->padding);
  7016. }
  7017. /**
  7018. * Compute encryption key
  7019. * @param String $user_pass user password
  7020. * @param String $owner_pass user password
  7021. * @param String $protection protection type
  7022. * @access protected
  7023. * @since 2.0.000 (2008-01-02)
  7024. * @author Klemen Vodopivec
  7025. */
  7026. protected function _generateencryptionkey($user_pass, $owner_pass, $protection) {
  7027. // Pad passwords
  7028. $user_pass = substr($user_pass.$this->padding, 0, 32);
  7029. $owner_pass = substr($owner_pass.$this->padding, 0, 32);
  7030. // Compute O value
  7031. $this->Ovalue = $this->_Ovalue($user_pass, $owner_pass);
  7032. // Compute encyption key
  7033. $tmp = $this->_md5_16($user_pass.$this->Ovalue.chr($protection)."\xFF\xFF\xFF");
  7034. $this->encryption_key = substr($tmp, 0, 5);
  7035. // Compute U value
  7036. $this->Uvalue = $this->_Uvalue();
  7037. // Compute P value
  7038. $this->Pvalue = -(($protection^255) + 1);
  7039. }
  7040. /**
  7041. * Set document protection
  7042. * The permission array is composed of values taken from the following ones:
  7043. * - copy: copy text and images to the clipboard
  7044. * - print: print the document
  7045. * - modify: modify it (except for annotations and forms)
  7046. * - annot-forms: add annotations and forms
  7047. * Remark: the protection against modification is for people who have the full Acrobat product.
  7048. * If you don't set any password, the document will open as usual. If you set a user password, the PDF viewer will ask for it before displaying the document. The master password, if different from the user one, can be used to get full access.
  7049. * Note: protecting a document requires to encrypt it, which increases the processing time a lot. This can cause a PHP time-out in some cases, especially if the document contains images or fonts.
  7050. * @param Array $permissions the set of permissions. Empty by default (only viewing is allowed). (print, modify, copy, annot-forms)
  7051. * @param String $user_pass user password. Empty by default.
  7052. * @param String $owner_pass owner password. If not specified, a random value is used.
  7053. * @access public
  7054. * @since 2.0.000 (2008-01-02)
  7055. * @author Klemen Vodopivec
  7056. */
  7057. public function SetProtection($permissions=array(), $user_pass='', $owner_pass=null) {
  7058. $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32);
  7059. $protection = 192;
  7060. foreach ($permissions as $permission) {
  7061. if (!isset($options[$permission])) {
  7062. $this->Error('Incorrect permission: '.$permission);
  7063. }
  7064. $protection += $options[$permission];
  7065. }
  7066. if ($owner_pass === null) {
  7067. $owner_pass = uniqid(rand());
  7068. }
  7069. $this->encrypted = true;
  7070. $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
  7071. }
  7072. // END OF ENCRYPTION FUNCTIONS -------------------------
  7073. // START TRANSFORMATIONS SECTION -----------------------
  7074. /**
  7075. * Starts a 2D tranformation saving current graphic state.
  7076. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  7077. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  7078. * @access public
  7079. * @since 2.1.000 (2008-01-07)
  7080. * @see StartTransform(), StopTransform()
  7081. */
  7082. public function StartTransform() {
  7083. $this->_out('q');
  7084. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  7085. }
  7086. /**
  7087. * Stops a 2D tranformation restoring previous graphic state.
  7088. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  7089. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  7090. * @access public
  7091. * @since 2.1.000 (2008-01-07)
  7092. * @see StartTransform(), StopTransform()
  7093. */
  7094. public function StopTransform() {
  7095. $this->_out('Q');
  7096. if (isset($this->transfmatrix)) {
  7097. array_pop($this->transfmatrix);
  7098. }
  7099. array_pop($this->transfmrk[$this->page]);
  7100. }
  7101. /**
  7102. * Horizontal Scaling.
  7103. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  7104. * @param int $x abscissa of the scaling center. Default is current x position
  7105. * @param int $y ordinate of the scaling center. Default is current y position
  7106. * @access public
  7107. * @since 2.1.000 (2008-01-07)
  7108. * @see StartTransform(), StopTransform()
  7109. */
  7110. public function ScaleX($s_x, $x='', $y='') {
  7111. $this->Scale($s_x, 100, $x, $y);
  7112. }
  7113. /**
  7114. * Vertical Scaling.
  7115. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  7116. * @param int $x abscissa of the scaling center. Default is current x position
  7117. * @param int $y ordinate of the scaling center. Default is current y position
  7118. * @access public
  7119. * @since 2.1.000 (2008-01-07)
  7120. * @see StartTransform(), StopTransform()
  7121. */
  7122. public function ScaleY($s_y, $x='', $y='') {
  7123. $this->Scale(100, $s_y, $x, $y);
  7124. }
  7125. /**
  7126. * Vertical and horizontal proportional Scaling.
  7127. * @param float $s scaling factor for width and height as percent. 0 is not allowed.
  7128. * @param int $x abscissa of the scaling center. Default is current x position
  7129. * @param int $y ordinate of the scaling center. Default is current y position
  7130. * @access public
  7131. * @since 2.1.000 (2008-01-07)
  7132. * @see StartTransform(), StopTransform()
  7133. */
  7134. public function ScaleXY($s, $x='', $y='') {
  7135. $this->Scale($s, $s, $x, $y);
  7136. }
  7137. /**
  7138. * Vertical and horizontal non-proportional Scaling.
  7139. * @param float $s_x scaling factor for width as percent. 0 is not allowed.
  7140. * @param float $s_y scaling factor for height as percent. 0 is not allowed.
  7141. * @param int $x abscissa of the scaling center. Default is current x position
  7142. * @param int $y ordinate of the scaling center. Default is current y position
  7143. * @access public
  7144. * @since 2.1.000 (2008-01-07)
  7145. * @see StartTransform(), StopTransform()
  7146. */
  7147. public function Scale($s_x, $s_y, $x='', $y='') {
  7148. if ($x === '') {
  7149. $x = $this->x;
  7150. }
  7151. if ($y === '') {
  7152. $y = $this->y;
  7153. }
  7154. if ($this->rtl) {
  7155. $x = $this->w - $x;
  7156. }
  7157. if (($s_x == 0) OR ($s_y == 0)) {
  7158. $this->Error('Please do not use values equal to zero for scaling');
  7159. }
  7160. $y = ($this->h - $y) * $this->k;
  7161. $x *= $this->k;
  7162. //calculate elements of transformation matrix
  7163. $s_x /= 100;
  7164. $s_y /= 100;
  7165. $tm[0] = $s_x;
  7166. $tm[1] = 0;
  7167. $tm[2] = 0;
  7168. $tm[3] = $s_y;
  7169. $tm[4] = $x * (1 - $s_x);
  7170. $tm[5] = $y * (1 - $s_y);
  7171. //scale the coordinate system
  7172. $this->Transform($tm);
  7173. }
  7174. /**
  7175. * Horizontal Mirroring.
  7176. * @param int $x abscissa of the point. Default is current x position
  7177. * @access public
  7178. * @since 2.1.000 (2008-01-07)
  7179. * @see StartTransform(), StopTransform()
  7180. */
  7181. public function MirrorH($x='') {
  7182. $this->Scale(-100, 100, $x);
  7183. }
  7184. /**
  7185. * Verical Mirroring.
  7186. * @param int $y ordinate of the point. Default is current y position
  7187. * @access public
  7188. * @since 2.1.000 (2008-01-07)
  7189. * @see StartTransform(), StopTransform()
  7190. */
  7191. public function MirrorV($y='') {
  7192. $this->Scale(100, -100, '', $y);
  7193. }
  7194. /**
  7195. * Point reflection mirroring.
  7196. * @param int $x abscissa of the point. Default is current x position
  7197. * @param int $y ordinate of the point. Default is current y position
  7198. * @access public
  7199. * @since 2.1.000 (2008-01-07)
  7200. * @see StartTransform(), StopTransform()
  7201. */
  7202. public function MirrorP($x='',$y='') {
  7203. $this->Scale(-100, -100, $x, $y);
  7204. }
  7205. /**
  7206. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  7207. * @param float $angle gradient angle of the straight line. Default is 0 (horizontal line).
  7208. * @param int $x abscissa of the point. Default is current x position
  7209. * @param int $y ordinate of the point. Default is current y position
  7210. * @access public
  7211. * @since 2.1.000 (2008-01-07)
  7212. * @see StartTransform(), StopTransform()
  7213. */
  7214. public function MirrorL($angle=0, $x='',$y='') {
  7215. $this->Scale(-100, 100, $x, $y);
  7216. $this->Rotate(-2*($angle-90), $x, $y);
  7217. }
  7218. /**
  7219. * Translate graphic object horizontally.
  7220. * @param int $t_x movement to the right (or left for RTL)
  7221. * @access public
  7222. * @since 2.1.000 (2008-01-07)
  7223. * @see StartTransform(), StopTransform()
  7224. */
  7225. public function TranslateX($t_x) {
  7226. $this->Translate($t_x, 0);
  7227. }
  7228. /**
  7229. * Translate graphic object vertically.
  7230. * @param int $t_y movement to the bottom
  7231. * @access public
  7232. * @since 2.1.000 (2008-01-07)
  7233. * @see StartTransform(), StopTransform()
  7234. */
  7235. public function TranslateY($t_y) {
  7236. $this->Translate(0, $t_y);
  7237. }
  7238. /**
  7239. * Translate graphic object horizontally and vertically.
  7240. * @param int $t_x movement to the right
  7241. * @param int $t_y movement to the bottom
  7242. * @access public
  7243. * @since 2.1.000 (2008-01-07)
  7244. * @see StartTransform(), StopTransform()
  7245. */
  7246. public function Translate($t_x, $t_y) {
  7247. if ($this->rtl) {
  7248. $t_x = -$t_x;
  7249. }
  7250. //calculate elements of transformation matrix
  7251. $tm[0] = 1;
  7252. $tm[1] = 0;
  7253. $tm[2] = 0;
  7254. $tm[3] = 1;
  7255. $tm[4] = $t_x * $this->k;
  7256. $tm[5] = -$t_y * $this->k;
  7257. //translate the coordinate system
  7258. $this->Transform($tm);
  7259. }
  7260. /**
  7261. * Rotate object.
  7262. * @param float $angle angle in degrees for counter-clockwise rotation
  7263. * @param int $x abscissa of the rotation center. Default is current x position
  7264. * @param int $y ordinate of the rotation center. Default is current y position
  7265. * @access public
  7266. * @since 2.1.000 (2008-01-07)
  7267. * @see StartTransform(), StopTransform()
  7268. */
  7269. public function Rotate($angle, $x='', $y='') {
  7270. if ($x === '') {
  7271. $x = $this->x;
  7272. }
  7273. if ($y === '') {
  7274. $y = $this->y;
  7275. }
  7276. if ($this->rtl) {
  7277. $x = $this->w - $x;
  7278. $angle = -$angle;
  7279. }
  7280. $y = ($this->h - $y) * $this->k;
  7281. $x *= $this->k;
  7282. //calculate elements of transformation matrix
  7283. $tm[0] = cos(deg2rad($angle));
  7284. $tm[1] = sin(deg2rad($angle));
  7285. $tm[2] = -$tm[1];
  7286. $tm[3] = $tm[0];
  7287. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  7288. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  7289. //rotate the coordinate system around ($x,$y)
  7290. $this->Transform($tm);
  7291. }
  7292. /**
  7293. * Skew horizontally.
  7294. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  7295. * @param int $x abscissa of the skewing center. default is current x position
  7296. * @param int $y ordinate of the skewing center. default is current y position
  7297. * @access public
  7298. * @since 2.1.000 (2008-01-07)
  7299. * @see StartTransform(), StopTransform()
  7300. */
  7301. public function SkewX($angle_x, $x='', $y='') {
  7302. $this->Skew($angle_x, 0, $x, $y);
  7303. }
  7304. /**
  7305. * Skew vertically.
  7306. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  7307. * @param int $x abscissa of the skewing center. default is current x position
  7308. * @param int $y ordinate of the skewing center. default is current y position
  7309. * @access public
  7310. * @since 2.1.000 (2008-01-07)
  7311. * @see StartTransform(), StopTransform()
  7312. */
  7313. public function SkewY($angle_y, $x='', $y='') {
  7314. $this->Skew(0, $angle_y, $x, $y);
  7315. }
  7316. /**
  7317. * Skew.
  7318. * @param float $angle_x angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  7319. * @param float $angle_y angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  7320. * @param int $x abscissa of the skewing center. default is current x position
  7321. * @param int $y ordinate of the skewing center. default is current y position
  7322. * @access public
  7323. * @since 2.1.000 (2008-01-07)
  7324. * @see StartTransform(), StopTransform()
  7325. */
  7326. public function Skew($angle_x, $angle_y, $x='', $y='') {
  7327. if ($x === '') {
  7328. $x = $this->x;
  7329. }
  7330. if ($y === '') {
  7331. $y = $this->y;
  7332. }
  7333. if ($this->rtl) {
  7334. $x = $this->w - $x;
  7335. $angle_x = -$angle_x;
  7336. }
  7337. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  7338. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  7339. }
  7340. $x *= $this->k;
  7341. $y = ($this->h - $y) * $this->k;
  7342. //calculate elements of transformation matrix
  7343. $tm[0] = 1;
  7344. $tm[1] = tan(deg2rad($angle_y));
  7345. $tm[2] = tan(deg2rad($angle_x));
  7346. $tm[3] = 1;
  7347. $tm[4] = -$tm[2] * $y;
  7348. $tm[5] = -$tm[1] * $x;
  7349. //skew the coordinate system
  7350. $this->Transform($tm);
  7351. }
  7352. /**
  7353. * Apply graphic transformations.
  7354. * @access protected
  7355. * @since 2.1.000 (2008-01-07)
  7356. * @see StartTransform(), StopTransform()
  7357. */
  7358. protected function Transform($tm) {
  7359. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  7360. // store transformation matrix
  7361. $this->transfmatrix[] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  7362. // update tranformation mark
  7363. if (end($this->transfmrk[$this->page]) !== false) {
  7364. $key = key($this->transfmrk[$this->page]);
  7365. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  7366. }
  7367. }
  7368. // END TRANSFORMATIONS SECTION -------------------------
  7369. // START GRAPHIC FUNCTIONS SECTION ---------------------
  7370. // The following section is based on the code provided by David Hernandez Sanz
  7371. /**
  7372. * Defines the line width. By default, the value equals 0.2 mm. The method can be called before the first page is created and the value is retained from page to page.
  7373. * @param float $width The width.
  7374. * @access public
  7375. * @since 1.0
  7376. * @see Line(), Rect(), Cell(), MultiCell()
  7377. */
  7378. public function SetLineWidth($width) {
  7379. //Set line width
  7380. $this->LineWidth = $width;
  7381. $this->linestyleWidth = sprintf('%.2F w', ($width * $this->k));
  7382. if ($this->page > 0) {
  7383. $this->_out($this->linestyleWidth);
  7384. }
  7385. }
  7386. /**
  7387. * Returns the current the line width.
  7388. * @return int Line width
  7389. * @access public
  7390. * @since 2.1.000 (2008-01-07)
  7391. * @see Line(), SetLineWidth()
  7392. */
  7393. public function GetLineWidth() {
  7394. return $this->LineWidth;
  7395. }
  7396. /**
  7397. * Set line style.
  7398. * @param array $style Line style. Array with keys among the following:
  7399. * <ul>
  7400. * <li>width (float): Width of the line in user units.</li>
  7401. * <li>cap (string): Type of cap to put on the line. Possible values are:
  7402. * butt, round, square. The difference between "square" and "butt" is that
  7403. * "square" projects a flat end past the end of the line.</li>
  7404. * <li>join (string): Type of join. Possible values are: miter, round,
  7405. * bevel.</li>
  7406. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  7407. * series of length values, which are the lengths of the on and off dashes.
  7408. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  7409. * 1 off, 2 on, 1 off, ...</li>
  7410. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  7411. * the point at which the pattern starts.</li>
  7412. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K).</li>
  7413. * </ul>
  7414. * @access public
  7415. * @since 2.1.000 (2008-01-08)
  7416. */
  7417. public function SetLineStyle($style) {
  7418. if (!is_array($style)) {
  7419. return;
  7420. }
  7421. extract($style);
  7422. if (isset($width)) {
  7423. $width_prev = $this->LineWidth;
  7424. $this->SetLineWidth($width);
  7425. $this->LineWidth = $width_prev;
  7426. }
  7427. if (isset($cap)) {
  7428. $ca = array('butt' => 0, 'round'=> 1, 'square' => 2);
  7429. if (isset($ca[$cap])) {
  7430. $this->linestyleCap = $ca[$cap].' J';
  7431. $this->_out($this->linestyleCap);
  7432. }
  7433. }
  7434. if (isset($join)) {
  7435. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  7436. if (isset($ja[$join])) {
  7437. $this->linestyleJoin = $ja[$join].' j';
  7438. $this->_out($this->linestyleJoin);
  7439. }
  7440. }
  7441. if (isset($dash)) {
  7442. $dash_string = '';
  7443. if ($dash) {
  7444. if (preg_match('/^.+,/', $dash) > 0) {
  7445. $tab = explode(',', $dash);
  7446. } else {
  7447. $tab = array($dash);
  7448. }
  7449. $dash_string = '';
  7450. foreach ($tab as $i => $v) {
  7451. if ($i) {
  7452. $dash_string .= ' ';
  7453. }
  7454. $dash_string .= sprintf("%.2F", $v);
  7455. }
  7456. }
  7457. if (!isset($phase) OR !$dash) {
  7458. $phase = 0;
  7459. }
  7460. $this->linestyleDash = sprintf("[%s] %.2F d", $dash_string, $phase);
  7461. $this->_out($this->linestyleDash);
  7462. }
  7463. if (isset($color)) {
  7464. $this->SetDrawColorArray($color);
  7465. }
  7466. }
  7467. /*
  7468. * Set a draw point.
  7469. * @param float $x Abscissa of point.
  7470. * @param float $y Ordinate of point.
  7471. * @access protected
  7472. * @since 2.1.000 (2008-01-08)
  7473. */
  7474. protected function _outPoint($x, $y) {
  7475. if ($this->rtl) {
  7476. $x = $this->w - $x;
  7477. }
  7478. $this->_out(sprintf("%.2F %.2F m", $x * $this->k, ($this->h - $y) * $this->k));
  7479. }
  7480. /*
  7481. * Draws a line from last draw point.
  7482. * @param float $x Abscissa of end point.
  7483. * @param float $y Ordinate of end point.
  7484. * @access protected
  7485. * @since 2.1.000 (2008-01-08)
  7486. */
  7487. protected function _outLine($x, $y) {
  7488. if ($this->rtl) {
  7489. $x = $this->w - $x;
  7490. }
  7491. $this->_out(sprintf("%.2F %.2F l", $x * $this->k, ($this->h - $y) * $this->k));
  7492. }
  7493. /**
  7494. * Draws a rectangle.
  7495. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  7496. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  7497. * @param float $w Width.
  7498. * @param float $h Height.
  7499. * @param string $op options
  7500. * @access protected
  7501. * @since 2.1.000 (2008-01-08)
  7502. */
  7503. protected function _outRect($x, $y, $w, $h, $op) {
  7504. if ($this->rtl) {
  7505. $x = $this->w - $x - $w;
  7506. }
  7507. $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k, $op));
  7508. }
  7509. /*
  7510. * Draws a Bezier curve from last draw point.
  7511. * The Bezier curve is a tangent to the line between the control points at either end of the curve.
  7512. * @param float $x1 Abscissa of control point 1.
  7513. * @param float $y1 Ordinate of control point 1.
  7514. * @param float $x2 Abscissa of control point 2.
  7515. * @param float $y2 Ordinate of control point 2.
  7516. * @param float $x3 Abscissa of end point.
  7517. * @param float $y3 Ordinate of end point.
  7518. * @access protected
  7519. * @since 2.1.000 (2008-01-08)
  7520. */
  7521. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  7522. if ($this->rtl) {
  7523. $x1 = $this->w - $x1;
  7524. $x2 = $this->w - $x2;
  7525. $x3 = $this->w - $x3;
  7526. }
  7527. $this->_out(sprintf("%.2F %.2F %.2F %.2F %.2F %.2F c", $x1 * $this->k, ($this->h - $y1) * $this->k, $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  7528. }
  7529. /**
  7530. * Draws a line between two points.
  7531. * @param float $x1 Abscissa of first point.
  7532. * @param float $y1 Ordinate of first point.
  7533. * @param float $x2 Abscissa of second point.
  7534. * @param float $y2 Ordinate of second point.
  7535. * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7536. * @access public
  7537. * @since 1.0
  7538. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  7539. */
  7540. public function Line($x1, $y1, $x2, $y2, $style=array()) {
  7541. if (is_array($style)) {
  7542. $this->SetLineStyle($style);
  7543. }
  7544. $this->_outPoint($x1, $y1);
  7545. $this->_outLine($x2, $y2);
  7546. $this->_out(' S');
  7547. }
  7548. /**
  7549. * Draws a rectangle.
  7550. * @param float $x Abscissa of upper-left corner (or upper-right corner for RTL language).
  7551. * @param float $y Ordinate of upper-left corner (or upper-right corner for RTL language).
  7552. * @param float $w Width.
  7553. * @param float $h Height.
  7554. * @param string $style Style of rendering. Possible values are:
  7555. * <ul>
  7556. * <li>D or empty string: Draw (default).</li>
  7557. * <li>F: Fill.</li>
  7558. * <li>DF or FD: Draw and fill.</li>
  7559. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7560. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7561. * </ul>
  7562. * @param array $border_style Border style of rectangle. Array with keys among the following:
  7563. * <ul>
  7564. * <li>all: Line style of all borders. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7565. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7566. * </ul>
  7567. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  7568. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7569. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7570. * @access public
  7571. * @since 1.0
  7572. * @see SetLineStyle()
  7573. */
  7574. public function Rect($x, $y, $w, $h, $style='', $border_style=array(), $fill_color=array()) {
  7575. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7576. $this->SetFillColorArray($fill_color);
  7577. }
  7578. switch ($style) {
  7579. case 'F': {
  7580. $op = 'f';
  7581. $border_style = array();
  7582. $this->_outRect($x, $y, $w, $h, $op);
  7583. break;
  7584. }
  7585. case 'DF':
  7586. case 'FD': {
  7587. if ((!$border_style) OR (isset($border_style['all']))) {
  7588. $op = 'B';
  7589. if (isset($border_style['all'])) {
  7590. $this->SetLineStyle($border_style['all']);
  7591. $border_style = array();
  7592. }
  7593. } else {
  7594. $op = 'f';
  7595. }
  7596. $this->_outRect($x, $y, $w, $h, $op);
  7597. break;
  7598. }
  7599. case 'CNZ': {
  7600. $op = 'W n';
  7601. $this->_outRect($x, $y, $w, $h, $op);
  7602. break;
  7603. }
  7604. case 'CEO': {
  7605. $op = 'W* n';
  7606. $this->_outRect($x, $y, $w, $h, $op);
  7607. break;
  7608. }
  7609. default: {
  7610. $op = 'S';
  7611. if ((!$border_style) OR (isset($border_style['all']))) {
  7612. if (isset($border_style['all']) AND $border_style['all']) {
  7613. $this->SetLineStyle($border_style['all']);
  7614. $border_style = array();
  7615. }
  7616. $this->_outRect($x, $y, $w, $h, $op);
  7617. }
  7618. break;
  7619. }
  7620. }
  7621. if ($border_style) {
  7622. $border_style2 = array();
  7623. foreach ($border_style as $line => $value) {
  7624. $lenght = strlen($line);
  7625. for ($i = 0; $i < $lenght; ++$i) {
  7626. $border_style2[$line[$i]] = $value;
  7627. }
  7628. }
  7629. $border_style = $border_style2;
  7630. if (isset($border_style['L']) AND $border_style['L']) {
  7631. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  7632. }
  7633. if (isset($border_style['T']) AND $border_style['T']) {
  7634. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  7635. }
  7636. if (isset($border_style['R']) AND $border_style['R']) {
  7637. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  7638. }
  7639. if (isset($border_style['B']) AND $border_style['B']) {
  7640. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  7641. }
  7642. }
  7643. }
  7644. /**
  7645. * Draws a Bezier curve.
  7646. * The Bezier curve is a tangent to the line between the control points at
  7647. * either end of the curve.
  7648. * @param float $x0 Abscissa of start point.
  7649. * @param float $y0 Ordinate of start point.
  7650. * @param float $x1 Abscissa of control point 1.
  7651. * @param float $y1 Ordinate of control point 1.
  7652. * @param float $x2 Abscissa of control point 2.
  7653. * @param float $y2 Ordinate of control point 2.
  7654. * @param float $x3 Abscissa of end point.
  7655. * @param float $y3 Ordinate of end point.
  7656. * @param string $style Style of rendering. Possible values are:
  7657. * <ul>
  7658. * <li>D or empty string: Draw (default).</li>
  7659. * <li>F: Fill.</li>
  7660. * <li>DF or FD: Draw and fill.</li>
  7661. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7662. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7663. * </ul>
  7664. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7665. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7666. * @access public
  7667. * @see SetLineStyle()
  7668. * @since 2.1.000 (2008-01-08)
  7669. */
  7670. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style='', $line_style=array(), $fill_color=array()) {
  7671. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7672. $this->SetFillColorArray($fill_color);
  7673. }
  7674. switch ($style) {
  7675. case 'F': {
  7676. $op = 'f';
  7677. $line_style = array();
  7678. break;
  7679. }
  7680. case 'FD':
  7681. case 'DF': {
  7682. $op = 'B';
  7683. break;
  7684. }
  7685. case 'CNZ': {
  7686. $op = 'W n';
  7687. break;
  7688. }
  7689. case 'CEO': {
  7690. $op = 'W* n';
  7691. break;
  7692. }
  7693. default: {
  7694. $op = 'S';
  7695. break;
  7696. }
  7697. }
  7698. if ($line_style) {
  7699. $this->SetLineStyle($line_style);
  7700. }
  7701. $this->_outPoint($x0, $y0);
  7702. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  7703. $this->_out($op);
  7704. }
  7705. /**
  7706. * Draws a poly-Bezier curve.
  7707. * Each Bezier curve segment is a tangent to the line between the control points at
  7708. * either end of the curve.
  7709. * @param float $x0 Abscissa of start point.
  7710. * @param float $y0 Ordinate of start point.
  7711. * @param float $segments An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  7712. * @param string $style Style of rendering. Possible values are:
  7713. * <ul>
  7714. * <li>D or empty string: Draw (default).</li>
  7715. * <li>F: Fill.</li>
  7716. * <li>DF or FD: Draw and fill.</li>
  7717. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7718. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7719. * </ul>
  7720. * @param array $line_style Line style of curve. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7721. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7722. * @access public
  7723. * @see SetLineStyle()
  7724. * @since 3.0008 (2008-05-12)
  7725. */
  7726. public function Polycurve($x0, $y0, $segments, $style='', $line_style=array(), $fill_color=array()) {
  7727. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7728. $this->SetFillColorArray($fill_color);
  7729. }
  7730. switch ($style) {
  7731. case 'F': {
  7732. $op = 'f';
  7733. $line_style = array();
  7734. break;
  7735. }
  7736. case 'FD':
  7737. case 'DF': {
  7738. $op = 'B';
  7739. break;
  7740. }
  7741. case 'CNZ': {
  7742. $op = 'W n';
  7743. break;
  7744. }
  7745. case 'CEO': {
  7746. $op = 'W* n';
  7747. break;
  7748. }
  7749. default: {
  7750. $op = 'S';
  7751. break;
  7752. }
  7753. }
  7754. if ($line_style) {
  7755. $this->SetLineStyle($line_style);
  7756. }
  7757. $this->_outPoint($x0, $y0);
  7758. foreach ($segments as $segment) {
  7759. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  7760. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  7761. }
  7762. $this->_out($op);
  7763. }
  7764. /**
  7765. * Draws an ellipse.
  7766. * An ellipse is formed from n Bezier curves.
  7767. * @param float $x0 Abscissa of center point.
  7768. * @param float $y0 Ordinate of center point.
  7769. * @param float $rx Horizontal radius.
  7770. * @param float $ry Vertical radius (if ry = 0 then is a circle, see {@link Circle Circle}). Default value: 0.
  7771. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  7772. * @param float $astart: Angle start of draw line. Default value: 0.
  7773. * @param float $afinish: Angle finish of draw line. Default value: 360.
  7774. * @param string $style Style of rendering. Possible values are:
  7775. * <ul>
  7776. * <li>D or empty string: Draw (default).</li>
  7777. * <li>F: Fill.</li>
  7778. * <li>DF or FD: Draw and fill.</li>
  7779. * <li>C: Draw close.</li>
  7780. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7781. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7782. * </ul>
  7783. * @param array $line_style Line style of ellipse. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7784. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7785. * @param integer $nc Number of curves used in ellipse. Default value: 8.
  7786. * @access public
  7787. * @since 2.1.000 (2008-01-08)
  7788. */
  7789. public function Ellipse($x0, $y0, $rx, $ry=0, $angle=0, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=8) {
  7790. if ($angle) {
  7791. $this->StartTransform();
  7792. $this->Rotate($angle, $x0, $y0);
  7793. $this->Ellipse($x0, $y0, $rx, $ry, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  7794. $this->StopTransform();
  7795. return;
  7796. }
  7797. if ($rx) {
  7798. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7799. $this->SetFillColorArray($fill_color);
  7800. }
  7801. switch ($style) {
  7802. case 'F': {
  7803. $op = 'f';
  7804. $line_style = array();
  7805. break;
  7806. }
  7807. case 'FD':
  7808. case 'DF': {
  7809. $op = 'B';
  7810. break;
  7811. }
  7812. case 'C': {
  7813. $op = 's'; // Small 's' signifies closing the path as well
  7814. break;
  7815. }
  7816. case 'CNZ': {
  7817. $op = 'W n';
  7818. break;
  7819. }
  7820. case 'CEO': {
  7821. $op = 'W* n';
  7822. break;
  7823. }
  7824. default: {
  7825. $op = 'S';
  7826. break;
  7827. }
  7828. }
  7829. if ($line_style) {
  7830. $this->SetLineStyle($line_style);
  7831. }
  7832. if (!$ry) {
  7833. $ry = $rx;
  7834. }
  7835. $rx *= $this->k;
  7836. $ry *= $this->k;
  7837. if ($nc < 2) {
  7838. $nc = 2;
  7839. }
  7840. $astart = deg2rad((float) $astart);
  7841. $afinish = deg2rad((float) $afinish);
  7842. $total_angle = $afinish - $astart;
  7843. $dt = $total_angle / $nc;
  7844. $dtm = $dt / 3;
  7845. $x0 *= $this->k;
  7846. $y0 = ($this->h - $y0) * $this->k;
  7847. $t1 = $astart;
  7848. $a0 = $x0 + ($rx * cos($t1));
  7849. $b0 = $y0 + ($ry * sin($t1));
  7850. $c0 = -$rx * sin($t1);
  7851. $d0 = $ry * cos($t1);
  7852. $this->_outPoint($a0 / $this->k, $this->h - ($b0 / $this->k));
  7853. for ($i = 1; $i <= $nc; ++$i) {
  7854. // Draw this bit of the total curve
  7855. $t1 = ($i * $dt) + $astart;
  7856. $a1 = $x0 + ($rx * cos($t1));
  7857. $b1 = $y0 + ($ry * sin($t1));
  7858. $c1 = -$rx * sin($t1);
  7859. $d1 = $ry * cos($t1);
  7860. $this->_outCurve(($a0 + ($c0 * $dtm)) / $this->k, $this->h - (($b0 + ($d0 * $dtm)) / $this->k), ($a1 - ($c1 * $dtm)) / $this->k, $this->h - (($b1 - ($d1 * $dtm)) / $this->k), $a1 / $this->k, $this->h - ($b1 / $this->k));
  7861. $a0 = $a1;
  7862. $b0 = $b1;
  7863. $c0 = $c1;
  7864. $d0 = $d1;
  7865. }
  7866. $this->_out($op);
  7867. }
  7868. }
  7869. /**
  7870. * Draws a circle.
  7871. * A circle is formed from n Bezier curves.
  7872. * @param float $x0 Abscissa of center point.
  7873. * @param float $y0 Ordinate of center point.
  7874. * @param float $r Radius.
  7875. * @param float $astart: Angle start of draw line. Default value: 0.
  7876. * @param float $afinish: Angle finish of draw line. Default value: 360.
  7877. * @param string $style Style of rendering. Possible values are:
  7878. * <ul>
  7879. * <li>D or empty string: Draw (default).</li>
  7880. * <li>F: Fill.</li>
  7881. * <li>DF or FD: Draw and fill.</li>
  7882. * <li>C: Draw close.</li>
  7883. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7884. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7885. * </ul>
  7886. * @param array $line_style Line style of circle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  7887. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  7888. * @param integer $nc Number of curves used in circle. Default value: 8.
  7889. * @access public
  7890. * @since 2.1.000 (2008-01-08)
  7891. */
  7892. public function Circle($x0, $y0, $r, $astart=0, $afinish=360, $style='', $line_style=array(), $fill_color=array(), $nc=8) {
  7893. $this->Ellipse($x0, $y0, $r, 0, 0, $astart, $afinish, $style, $line_style, $fill_color, $nc);
  7894. }
  7895. /**
  7896. * Draws a polygon.
  7897. * @param array $p Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  7898. * @param string $style Style of rendering. Possible values are:
  7899. * <ul>
  7900. * <li>D or empty string: Draw (default).</li>
  7901. * <li>F: Fill.</li>
  7902. * <li>DF or FD: Draw and fill.</li>
  7903. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  7904. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  7905. * </ul>
  7906. * @param array $line_style Line style of polygon. Array with keys among the following:
  7907. * <ul>
  7908. * <li>all: Line style of all lines. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7909. * <li>0 to ($np - 1): Line style of each line. Array like for {@link SetLineStyle SetLineStyle}.</li>
  7910. * </ul>
  7911. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  7912. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  7913. * @access public
  7914. * @since 2.1.000 (2008-01-08)
  7915. */
  7916. public function Polygon($p, $style='', $line_style=array(), $fill_color=array()) {
  7917. $nc = count($p); // number of coordinates
  7918. $np = $nc / 2; // number of points
  7919. for ($i = 0; $i < 4; ++$i) {
  7920. // close polygon by adding the first 2 points at the end (one line)
  7921. $p[$nc + $i] = $p[$i];
  7922. }
  7923. // copy style for the last added line
  7924. if (isset($line_style[0])) {
  7925. $line_style[$np] = $line_style[0];
  7926. }
  7927. $nc += 4;
  7928. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  7929. $this->SetFillColorArray($fill_color);
  7930. }
  7931. switch ($style) {
  7932. case 'F': {
  7933. $line_style = array();
  7934. $op = 'f';
  7935. break;
  7936. }
  7937. case 'FD':
  7938. case 'DF': {
  7939. $op = 'B';
  7940. break;
  7941. }
  7942. case 'CNZ': {
  7943. $op = 'W n';
  7944. break;
  7945. }
  7946. case 'CEO': {
  7947. $op = 'W* n';
  7948. break;
  7949. }
  7950. default: {
  7951. $op = 'S';
  7952. break;
  7953. }
  7954. }
  7955. $draw = true;
  7956. if ($line_style) {
  7957. if (isset($line_style['all'])) {
  7958. $this->SetLineStyle($line_style['all']);
  7959. } else {
  7960. $draw = false;
  7961. if ($op == 'B') {
  7962. // draw fill
  7963. $op = 'f';
  7964. $this->_outPoint($p[0], $p[1]);
  7965. for ($i = 2; $i < $nc; $i = $i + 2) {
  7966. $this->_outLine($p[$i], $p[$i + 1]);
  7967. }
  7968. $this->_out($op);
  7969. }
  7970. // draw outline
  7971. $this->_outPoint($p[0], $p[1]);
  7972. for ($i = 2; $i < $nc; $i = $i + 2) {
  7973. $line_num = ($i / 2) - 1;
  7974. if (isset($line_style[$line_num])) {
  7975. if ($line_style[$line_num] != 0) {
  7976. if (is_array($line_style[$line_num])) {
  7977. $this->_out('S');
  7978. $this->SetLineStyle($line_style[$line_num]);
  7979. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  7980. $this->_outLine($p[$i], $p[$i + 1]);
  7981. $this->_out('S');
  7982. $this->_outPoint($p[$i], $p[$i + 1]);
  7983. } else {
  7984. $this->_outLine($p[$i], $p[$i + 1]);
  7985. }
  7986. }
  7987. } else {
  7988. $this->_outLine($p[$i], $p[$i + 1]);
  7989. }
  7990. }
  7991. $this->_out($op);
  7992. }
  7993. }
  7994. if ($draw) {
  7995. $this->_outPoint($p[0], $p[1]);
  7996. for ($i = 2; $i < $nc; $i = $i + 2) {
  7997. $this->_outLine($p[$i], $p[$i + 1]);
  7998. }
  7999. $this->_out($op);
  8000. }
  8001. }
  8002. /**
  8003. * Draws a regular polygon.
  8004. * @param float $x0 Abscissa of center point.
  8005. * @param float $y0 Ordinate of center point.
  8006. * @param float $r: Radius of inscribed circle.
  8007. * @param integer $ns Number of sides.
  8008. * @param float $angle Angle oriented (anti-clockwise). Default value: 0.
  8009. * @param boolean $draw_circle Draw inscribed circle or not. Default value: false.
  8010. * @param string $style Style of rendering. Possible values are:
  8011. * <ul>
  8012. * <li>D or empty string: Draw (default).</li>
  8013. * <li>F: Fill.</li>
  8014. * <li>DF or FD: Draw and fill.</li>
  8015. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8016. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8017. * </ul>
  8018. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  8019. * <ul>
  8020. * <li>all: Line style of all sides. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8021. * <li>0 to ($ns - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8022. * </ul>
  8023. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  8024. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  8025. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  8026. * <ul>
  8027. * <li>D or empty string: Draw (default).</li>
  8028. * <li>F: Fill.</li>
  8029. * <li>DF or FD: Draw and fill.</li>
  8030. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8031. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8032. * </ul>
  8033. * @param array $circle_outLine_style Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8034. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  8035. * @access public
  8036. * @since 2.1.000 (2008-01-08)
  8037. */
  8038. public function RegularPolygon($x0, $y0, $r, $ns, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
  8039. if (3 > $ns) {
  8040. $ns = 3;
  8041. }
  8042. if ($draw_circle) {
  8043. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  8044. }
  8045. $p = array();
  8046. for ($i = 0; $i < $ns; ++$i) {
  8047. $a = $angle + ($i * 360 / $ns);
  8048. $a_rad = deg2rad((float) $a);
  8049. $p[] = $x0 + ($r * sin($a_rad));
  8050. $p[] = $y0 + ($r * cos($a_rad));
  8051. }
  8052. $this->Polygon($p, $style, $line_style, $fill_color);
  8053. }
  8054. /**
  8055. * Draws a star polygon
  8056. * @param float $x0 Abscissa of center point.
  8057. * @param float $y0 Ordinate of center point.
  8058. * @param float $r Radius of inscribed circle.
  8059. * @param integer $nv Number of vertices.
  8060. * @param integer $ng Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  8061. * @param float $angle: Angle oriented (anti-clockwise). Default value: 0.
  8062. * @param boolean $draw_circle: Draw inscribed circle or not. Default value is false.
  8063. * @param string $style Style of rendering. Possible values are:
  8064. * <ul>
  8065. * <li>D or empty string: Draw (default).</li>
  8066. * <li>F: Fill.</li>
  8067. * <li>DF or FD: Draw and fill.</li>
  8068. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8069. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8070. * </ul>
  8071. * @param array $line_style Line style of polygon sides. Array with keys among the following:
  8072. * <ul>
  8073. * <li>all: Line style of all sides. Array like for
  8074. * {@link SetLineStyle SetLineStyle}.</li>
  8075. * <li>0 to (n - 1): Line style of each side. Array like for {@link SetLineStyle SetLineStyle}.</li>
  8076. * </ul>
  8077. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  8078. * @param array $fill_color Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  8079. * @param string $circle_style Style of rendering of inscribed circle (if draws). Possible values are:
  8080. * <ul>
  8081. * <li>D or empty string: Draw (default).</li>
  8082. * <li>F: Fill.</li>
  8083. * <li>DF or FD: Draw and fill.</li>
  8084. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8085. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8086. * </ul>
  8087. * @param array $circle_outLine_style Line style of inscribed circle (if draws). Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8088. * @param array $circle_fill_color Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  8089. * @access public
  8090. * @since 2.1.000 (2008-01-08)
  8091. */
  8092. public function StarPolygon($x0, $y0, $r, $nv, $ng, $angle=0, $draw_circle=false, $style='', $line_style=array(), $fill_color=array(), $circle_style='', $circle_outLine_style=array(), $circle_fill_color=array()) {
  8093. if ($nv < 2) {
  8094. $nv = 2;
  8095. }
  8096. if ($draw_circle) {
  8097. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  8098. }
  8099. $p2 = array();
  8100. $visited = array();
  8101. for ($i = 0; $i < $nv; ++$i) {
  8102. $a = $angle + ($i * 360 / $nv);
  8103. $a_rad = deg2rad((float) $a);
  8104. $p2[] = $x0 + ($r * sin($a_rad));
  8105. $p2[] = $y0 + ($r * cos($a_rad));
  8106. $visited[] = false;
  8107. }
  8108. $p = array();
  8109. $i = 0;
  8110. do {
  8111. $p[] = $p2[$i * 2];
  8112. $p[] = $p2[($i * 2) + 1];
  8113. $visited[$i] = true;
  8114. $i += $ng;
  8115. $i %= $nv;
  8116. } while (!$visited[$i]);
  8117. $this->Polygon($p, $style, $line_style, $fill_color);
  8118. }
  8119. /**
  8120. * Draws a rounded rectangle.
  8121. * @param float $x Abscissa of upper-left corner.
  8122. * @param float $y Ordinate of upper-left corner.
  8123. * @param float $w Width.
  8124. * @param float $h Height.
  8125. * @param float $r Radius of the rounded corners.
  8126. * @param string $round_corner Draws rounded corner or not. String with a 0 (not rounded i-corner) or 1 (rounded i-corner) in i-position. Positions are, in order and begin to 0: top left, top right, bottom right and bottom left. Default value: all rounded corner ("1111").
  8127. * @param string $style Style of rendering. Possible values are:
  8128. * <ul>
  8129. * <li>D or empty string: Draw (default).</li>
  8130. * <li>F: Fill.</li>
  8131. * <li>DF or FD: Draw and fill.</li>
  8132. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  8133. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  8134. * </ul>
  8135. * @param array $border_style Border style of rectangle. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
  8136. * @param array $fill_color Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K). Default value: default color (empty array).
  8137. * @access public
  8138. * @since 2.1.000 (2008-01-08)
  8139. */
  8140. public function RoundedRect($x, $y, $w, $h, $r, $round_corner='1111', $style='', $border_style=array(), $fill_color=array()) {
  8141. if ('0000' == $round_corner) { // Not rounded
  8142. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  8143. } else { // Rounded
  8144. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  8145. $this->SetFillColorArray($fill_color);
  8146. }
  8147. switch ($style) {
  8148. case 'F': {
  8149. $border_style = array();
  8150. $op = 'f';
  8151. break;
  8152. }
  8153. case 'FD':
  8154. case 'DF': {
  8155. $op = 'B';
  8156. break;
  8157. }
  8158. case 'CNZ': {
  8159. $op = 'W n';
  8160. break;
  8161. }
  8162. case 'CEO': {
  8163. $op = 'W* n';
  8164. break;
  8165. }
  8166. default: {
  8167. $op = 'S';
  8168. break;
  8169. }
  8170. }
  8171. if ($border_style) {
  8172. $this->SetLineStyle($border_style);
  8173. }
  8174. $MyArc = 4 / 3 * (sqrt(2) - 1);
  8175. $this->_outPoint($x + $r, $y);
  8176. $xc = $x + $w - $r;
  8177. $yc = $y + $r;
  8178. $this->_outLine($xc, $y);
  8179. if ($round_corner[0]) {
  8180. $this->_outCurve($xc + ($r * $MyArc), $yc - $r, $xc + $r, $yc - ($r * $MyArc), $xc + $r, $yc);
  8181. } else {
  8182. $this->_outLine($x + $w, $y);
  8183. }
  8184. $xc = $x + $w - $r;
  8185. $yc = $y + $h - $r;
  8186. $this->_outLine($x + $w, $yc);
  8187. if ($round_corner[1]) {
  8188. $this->_outCurve($xc + $r, $yc + ($r * $MyArc), $xc + ($r * $MyArc), $yc + $r, $xc, $yc + $r);
  8189. } else {
  8190. $this->_outLine($x + $w, $y + $h);
  8191. }
  8192. $xc = $x + $r;
  8193. $yc = $y + $h - $r;
  8194. $this->_outLine($xc, $y + $h);
  8195. if ($round_corner[2]) {
  8196. $this->_outCurve($xc - ($r * $MyArc), $yc + $r, $xc - $r, $yc + ($r * $MyArc), $xc - $r, $yc);
  8197. } else {
  8198. $this->_outLine($x, $y + $h);
  8199. }
  8200. $xc = $x + $r;
  8201. $yc = $y + $r;
  8202. $this->_outLine($x, $yc);
  8203. if ($round_corner[3]) {
  8204. $this->_outCurve($xc - $r, $yc - ($r * $MyArc), $xc - ($r * $MyArc), $yc - $r, $xc, $yc - $r);
  8205. } else {
  8206. $this->_outLine($x, $y);
  8207. $this->_outLine($x + $r, $y);
  8208. }
  8209. $this->_out($op);
  8210. }
  8211. }
  8212. /**
  8213. * Draws a grahic arrow.
  8214. * @parameter float $x0 Abscissa of first point.
  8215. * @parameter float $y0 Ordinate of first point.
  8216. * @parameter float $x0 Abscissa of second point.
  8217. * @parameter float $y1 Ordinate of second point.
  8218. * @parameter int $head_style (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  8219. * @parameter float $arm_size length of arrowhead arms
  8220. * @parameter int $arm_angle angle between an arm and the shaft
  8221. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  8222. * @since 4.6.018 (2009-07-10)
  8223. */
  8224. public function Arrow($x0, $y0, $x1, $y1, $head_style=0, $arm_size=5, $arm_angle=15) {
  8225. // getting arrow direction angle
  8226. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  8227. $dir_angle = rad2deg(atan2(($y0 - $y1), ($x0 - $x1)));
  8228. $sx1 = $x1;
  8229. $sy1 = $y1;
  8230. if ($head_style > 0) {
  8231. // calculate the stopping point for the arrow shaft
  8232. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos(deg2rad($dir_angle)));
  8233. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin(deg2rad($dir_angle)));
  8234. }
  8235. // main arrow line / shaft
  8236. $this->Line($x0, $y0, $sx1, $sy1);
  8237. // left arrowhead arm tip
  8238. $x2L = $x1 + ($arm_size * cos(deg2rad($dir_angle + $arm_angle)));
  8239. $y2L = $y1 + ($arm_size * sin(deg2rad($dir_angle + $arm_angle)));
  8240. // right arrowhead arm tip
  8241. $x2R = $x1 + ($arm_size * cos(deg2rad($dir_angle - $arm_angle)));
  8242. $y2R = $y1 + ($arm_size * sin(deg2rad($dir_angle - $arm_angle)));
  8243. $mode = 'D';
  8244. $style = array();
  8245. switch ($head_style) {
  8246. case 0: {
  8247. // draw only arrowhead arms
  8248. $mode = 'D';
  8249. $style = array(1, 1, 0);
  8250. break;
  8251. }
  8252. case 1: {
  8253. // draw closed arrowhead, but no fill
  8254. $mode = 'D';
  8255. break;
  8256. }
  8257. case 2: {
  8258. // closed and filled arrowhead
  8259. $mode = 'DF';
  8260. break;
  8261. }
  8262. case 3: {
  8263. // filled arrowhead
  8264. $mode = 'F';
  8265. break;
  8266. }
  8267. }
  8268. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  8269. }
  8270. // END GRAPHIC FUNCTIONS SECTION -----------------------
  8271. // BIDIRECTIONAL TEXT SECTION --------------------------
  8272. /**
  8273. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  8274. * @param string $str string to manipulate.
  8275. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  8276. * @return string
  8277. * @access protected
  8278. * @author Nicola Asuni
  8279. * @since 2.1.000 (2008-01-08)
  8280. */
  8281. protected function utf8StrRev($str, $setbom=false, $forcertl=false) {
  8282. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($this->UTF8StringToArray($str), $str, $forcertl), $setbom);
  8283. }
  8284. /**
  8285. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  8286. * @param array $ta array of characters composing the string.
  8287. * @param string $str string to process
  8288. * @param bool $forcertl if 'R' forces RTL, if 'L' forces LTR
  8289. * @return string
  8290. * @author Nicola Asuni
  8291. * @access protected
  8292. * @since 2.4.000 (2008-03-06)
  8293. */
  8294. protected function utf8Bidi($ta, $str='', $forcertl=false) {
  8295. global $unicode, $unicode_mirror, $unicode_arlet, $laa_array, $diacritics;
  8296. // paragraph embedding level
  8297. $pel = 0;
  8298. // max level
  8299. $maxlevel = 0;
  8300. if ($this->empty_string($str)) {
  8301. // create string from array
  8302. $str = $this->UTF8ArrSubString($ta);
  8303. }
  8304. // check if string contains arabic text
  8305. if (preg_match(K_RE_PATTERN_ARABIC, $str)) {
  8306. $arabic = true;
  8307. } else {
  8308. $arabic = false;
  8309. }
  8310. // check if string contains RTL text
  8311. if (!($forcertl OR $arabic OR preg_match(K_RE_PATTERN_RTL, $str))) {
  8312. return $ta;
  8313. }
  8314. // get number of chars
  8315. $numchars = count($ta);
  8316. if ($forcertl == 'R') {
  8317. $pel = 1;
  8318. } elseif ($forcertl == 'L') {
  8319. $pel = 0;
  8320. } else {
  8321. // P2. In each paragraph, find the first character of type L, AL, or R.
  8322. // P3. If a character is found in P2 and it is of type AL or R, then set the paragraph embedding level to one; otherwise, set it to zero.
  8323. for ($i=0; $i < $numchars; ++$i) {
  8324. $type = $unicode[$ta[$i]];
  8325. if ($type == 'L') {
  8326. $pel = 0;
  8327. break;
  8328. } elseif (($type == 'AL') OR ($type == 'R')) {
  8329. $pel = 1;
  8330. break;
  8331. }
  8332. }
  8333. }
  8334. // Current Embedding Level
  8335. $cel = $pel;
  8336. // directional override status
  8337. $dos = 'N';
  8338. $remember = array();
  8339. // start-of-level-run
  8340. $sor = $pel % 2 ? 'R' : 'L';
  8341. $eor = $sor;
  8342. // Array of characters data
  8343. $chardata = Array();
  8344. // X1. Begin by setting the current embedding level to the paragraph embedding level. Set the directional override status to neutral. Process each character iteratively, applying rules X2 through X9. Only embedding levels from 0 to 61 are valid in this phase.
  8345. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  8346. for ($i=0; $i < $numchars; ++$i) {
  8347. if ($ta[$i] == K_RLE) {
  8348. // X2. With each RLE, compute the least greater odd embedding level.
  8349. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  8350. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8351. $next_level = $cel + ($cel % 2) + 1;
  8352. if ($next_level < 62) {
  8353. $remember[] = array('num' => K_RLE, 'cel' => $cel, 'dos' => $dos);
  8354. $cel = $next_level;
  8355. $dos = 'N';
  8356. $sor = $eor;
  8357. $eor = $cel % 2 ? 'R' : 'L';
  8358. }
  8359. } elseif ($ta[$i] == K_LRE) {
  8360. // X3. With each LRE, compute the least greater even embedding level.
  8361. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to neutral.
  8362. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8363. $next_level = $cel + 2 - ($cel % 2);
  8364. if ( $next_level < 62 ) {
  8365. $remember[] = array('num' => K_LRE, 'cel' => $cel, 'dos' => $dos);
  8366. $cel = $next_level;
  8367. $dos = 'N';
  8368. $sor = $eor;
  8369. $eor = $cel % 2 ? 'R' : 'L';
  8370. }
  8371. } elseif ($ta[$i] == K_RLO) {
  8372. // X4. With each RLO, compute the least greater odd embedding level.
  8373. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to right-to-left.
  8374. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8375. $next_level = $cel + ($cel % 2) + 1;
  8376. if ($next_level < 62) {
  8377. $remember[] = array('num' => K_RLO, 'cel' => $cel, 'dos' => $dos);
  8378. $cel = $next_level;
  8379. $dos = 'R';
  8380. $sor = $eor;
  8381. $eor = $cel % 2 ? 'R' : 'L';
  8382. }
  8383. } elseif ($ta[$i] == K_LRO) {
  8384. // X5. With each LRO, compute the least greater even embedding level.
  8385. // a. If this new level would be valid, then this embedding code is valid. Remember (push) the current embedding level and override status. Reset the current level to this new level, and reset the override status to left-to-right.
  8386. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  8387. $next_level = $cel + 2 - ($cel % 2);
  8388. if ( $next_level < 62 ) {
  8389. $remember[] = array('num' => K_LRO, 'cel' => $cel, 'dos' => $dos);
  8390. $cel = $next_level;
  8391. $dos = 'L';
  8392. $sor = $eor;
  8393. $eor = $cel % 2 ? 'R' : 'L';
  8394. }
  8395. } elseif ($ta[$i] == K_PDF) {
  8396. // X7. With each PDF, determine the matching embedding or override code. If there was a valid matching code, restore (pop) the last remembered (pushed) embedding level and directional override.
  8397. if (count($remember)) {
  8398. $last = count($remember ) - 1;
  8399. if (($remember[$last]['num'] == K_RLE) OR
  8400. ($remember[$last]['num'] == K_LRE) OR
  8401. ($remember[$last]['num'] == K_RLO) OR
  8402. ($remember[$last]['num'] == K_LRO)) {
  8403. $match = array_pop($remember);
  8404. $cel = $match['cel'];
  8405. $dos = $match['dos'];
  8406. $sor = $eor;
  8407. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  8408. }
  8409. }
  8410. } elseif (($ta[$i] != K_RLE) AND
  8411. ($ta[$i] != K_LRE) AND
  8412. ($ta[$i] != K_RLO) AND
  8413. ($ta[$i] != K_LRO) AND
  8414. ($ta[$i] != K_PDF)) {
  8415. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  8416. // a. Set the level of the current character to the current embedding level.
  8417. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  8418. if ($dos != 'N') {
  8419. $chardir = $dos;
  8420. } else {
  8421. if (isset($unicode[$ta[$i]])) {
  8422. $chardir = $unicode[$ta[$i]];
  8423. } else {
  8424. $chardir = 'L';
  8425. }
  8426. }
  8427. // stores string characters and other information
  8428. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  8429. }
  8430. } // end for each char
  8431. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  8432. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  8433. // X10. The remaining rules are applied to each run of characters at the same level. For each run, determine the start-of-level-run (sor) and end-of-level-run (eor) type, either L or R. This depends on the higher of the two levels on either side of the boundary (at the start or end of the paragraph, the level of the 'other' run is the base embedding level). If the higher level is odd, the type is R; otherwise, it is L.
  8434. // 3.3.3 Resolving Weak Types
  8435. // Weak types are now resolved one level run at a time. At level run boundaries where the type of the character on the other side of the boundary is required, the type assigned to sor or eor is used.
  8436. // Nonspacing marks are now resolved based on the previous characters.
  8437. $numchars = count($chardata);
  8438. // W1. Examine each nonspacing mark (NSM) in the level run, and change the type of the NSM to the type of the previous character. If the NSM is at the start of the level run, it will get the type of sor.
  8439. $prevlevel = -1; // track level changes
  8440. $levcount = 0; // counts consecutive chars at the same level
  8441. for ($i=0; $i < $numchars; ++$i) {
  8442. if ($chardata[$i]['type'] == 'NSM') {
  8443. if ($levcount) {
  8444. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8445. } elseif ($i > 0) {
  8446. $chardata[$i]['type'] = $chardata[($i-1)]['type'];
  8447. }
  8448. }
  8449. if ($chardata[$i]['level'] != $prevlevel) {
  8450. $levcount = 0;
  8451. } else {
  8452. ++$levcount;
  8453. }
  8454. $prevlevel = $chardata[$i]['level'];
  8455. }
  8456. // W2. Search backward from each instance of a European number until the first strong type (R, L, AL, or sor) is found. If an AL is found, change the type of the European number to Arabic number.
  8457. $prevlevel = -1;
  8458. $levcount = 0;
  8459. for ($i=0; $i < $numchars; ++$i) {
  8460. if ($chardata[$i]['char'] == 'EN') {
  8461. for ($j=$levcount; $j >= 0; $j--) {
  8462. if ($chardata[$j]['type'] == 'AL') {
  8463. $chardata[$i]['type'] = 'AN';
  8464. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  8465. break;
  8466. }
  8467. }
  8468. }
  8469. if ($chardata[$i]['level'] != $prevlevel) {
  8470. $levcount = 0;
  8471. } else {
  8472. ++$levcount;
  8473. }
  8474. $prevlevel = $chardata[$i]['level'];
  8475. }
  8476. // W3. Change all ALs to R.
  8477. for ($i=0; $i < $numchars; ++$i) {
  8478. if ($chardata[$i]['type'] == 'AL') {
  8479. $chardata[$i]['type'] = 'R';
  8480. }
  8481. }
  8482. // W4. A single European separator between two European numbers changes to a European number. A single common separator between two numbers of the same type changes to that type.
  8483. $prevlevel = -1;
  8484. $levcount = 0;
  8485. for ($i=0; $i < $numchars; ++$i) {
  8486. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  8487. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  8488. $chardata[$i]['type'] = 'EN';
  8489. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'EN') AND ($chardata[($i+1)]['type'] == 'EN')) {
  8490. $chardata[$i]['type'] = 'EN';
  8491. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i-1)]['type'] == 'AN') AND ($chardata[($i+1)]['type'] == 'AN')) {
  8492. $chardata[$i]['type'] = 'AN';
  8493. }
  8494. }
  8495. if ($chardata[$i]['level'] != $prevlevel) {
  8496. $levcount = 0;
  8497. } else {
  8498. ++$levcount;
  8499. }
  8500. $prevlevel = $chardata[$i]['level'];
  8501. }
  8502. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  8503. $prevlevel = -1;
  8504. $levcount = 0;
  8505. for ($i=0; $i < $numchars; ++$i) {
  8506. if ($chardata[$i]['type'] == 'ET') {
  8507. if (($levcount > 0) AND ($chardata[($i-1)]['type'] == 'EN')) {
  8508. $chardata[$i]['type'] = 'EN';
  8509. } else {
  8510. $j = $i+1;
  8511. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  8512. if ($chardata[$j]['type'] == 'EN') {
  8513. $chardata[$i]['type'] = 'EN';
  8514. break;
  8515. } elseif ($chardata[$j]['type'] != 'ET') {
  8516. break;
  8517. }
  8518. ++$j;
  8519. }
  8520. }
  8521. }
  8522. if ($chardata[$i]['level'] != $prevlevel) {
  8523. $levcount = 0;
  8524. } else {
  8525. ++$levcount;
  8526. }
  8527. $prevlevel = $chardata[$i]['level'];
  8528. }
  8529. // W6. Otherwise, separators and terminators change to Other Neutral.
  8530. $prevlevel = -1;
  8531. $levcount = 0;
  8532. for ($i=0; $i < $numchars; ++$i) {
  8533. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  8534. $chardata[$i]['type'] = 'ON';
  8535. }
  8536. if ($chardata[$i]['level'] != $prevlevel) {
  8537. $levcount = 0;
  8538. } else {
  8539. ++$levcount;
  8540. }
  8541. $prevlevel = $chardata[$i]['level'];
  8542. }
  8543. //W7. Search backward from each instance of a European number until the first strong type (R, L, or sor) is found. If an L is found, then change the type of the European number to L.
  8544. $prevlevel = -1;
  8545. $levcount = 0;
  8546. for ($i=0; $i < $numchars; ++$i) {
  8547. if ($chardata[$i]['char'] == 'EN') {
  8548. for ($j=$levcount; $j >= 0; $j--) {
  8549. if ($chardata[$j]['type'] == 'L') {
  8550. $chardata[$i]['type'] = 'L';
  8551. } elseif ($chardata[$j]['type'] == 'R') {
  8552. break;
  8553. }
  8554. }
  8555. }
  8556. if ($chardata[$i]['level'] != $prevlevel) {
  8557. $levcount = 0;
  8558. } else {
  8559. ++$levcount;
  8560. }
  8561. $prevlevel = $chardata[$i]['level'];
  8562. }
  8563. // N1. A sequence of neutrals takes the direction of the surrounding strong text if the text on both sides has the same direction. European and Arabic numbers act as if they were R in terms of their influence on neutrals. Start-of-level-run (sor) and end-of-level-run (eor) are used at level run boundaries.
  8564. $prevlevel = -1;
  8565. $levcount = 0;
  8566. for ($i=0; $i < $numchars; ++$i) {
  8567. if (($levcount > 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  8568. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  8569. $chardata[$i]['type'] = 'L';
  8570. } elseif (($chardata[$i]['type'] == 'N') AND
  8571. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  8572. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  8573. $chardata[$i]['type'] = 'R';
  8574. } elseif ($chardata[$i]['type'] == 'N') {
  8575. // N2. Any remaining neutrals take the embedding direction
  8576. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8577. }
  8578. } elseif (($levcount == 0) AND (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] == $prevlevel)) {
  8579. // first char
  8580. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i+1)]['type'] == 'L')) {
  8581. $chardata[$i]['type'] = 'L';
  8582. } elseif (($chardata[$i]['type'] == 'N') AND
  8583. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  8584. (($chardata[($i+1)]['type'] == 'R') OR ($chardata[($i+1)]['type'] == 'EN') OR ($chardata[($i+1)]['type'] == 'AN'))) {
  8585. $chardata[$i]['type'] = 'R';
  8586. } elseif ($chardata[$i]['type'] == 'N') {
  8587. // N2. Any remaining neutrals take the embedding direction
  8588. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8589. }
  8590. } elseif (($levcount > 0) AND ((($i+1) == $numchars) OR (($i+1) < $numchars) AND ($chardata[($i+1)]['level'] != $prevlevel))) {
  8591. //last char
  8592. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i-1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  8593. $chardata[$i]['type'] = 'L';
  8594. } elseif (($chardata[$i]['type'] == 'N') AND
  8595. (($chardata[($i-1)]['type'] == 'R') OR ($chardata[($i-1)]['type'] == 'EN') OR ($chardata[($i-1)]['type'] == 'AN')) AND
  8596. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  8597. $chardata[$i]['type'] = 'R';
  8598. } elseif ($chardata[$i]['type'] == 'N') {
  8599. // N2. Any remaining neutrals take the embedding direction
  8600. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8601. }
  8602. } elseif ($chardata[$i]['type'] == 'N') {
  8603. // N2. Any remaining neutrals take the embedding direction
  8604. $chardata[$i]['type'] = $chardata[$i]['sor'];
  8605. }
  8606. if ($chardata[$i]['level'] != $prevlevel) {
  8607. $levcount = 0;
  8608. } else {
  8609. ++$levcount;
  8610. }
  8611. $prevlevel = $chardata[$i]['level'];
  8612. }
  8613. // I1. For all characters with an even (left-to-right) embedding direction, those of type R go up one level and those of type AN or EN go up two levels.
  8614. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  8615. for ($i=0; $i < $numchars; ++$i) {
  8616. $odd = $chardata[$i]['level'] % 2;
  8617. if ($odd) {
  8618. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  8619. $chardata[$i]['level'] += 1;
  8620. }
  8621. } else {
  8622. if ($chardata[$i]['type'] == 'R') {
  8623. $chardata[$i]['level'] += 1;
  8624. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  8625. $chardata[$i]['level'] += 2;
  8626. }
  8627. }
  8628. $maxlevel = max($chardata[$i]['level'],$maxlevel);
  8629. }
  8630. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  8631. // 1. Segment separators,
  8632. // 2. Paragraph separators,
  8633. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  8634. // 4. Any sequence of white space characters at the end of the line.
  8635. for ($i=0; $i < $numchars; ++$i) {
  8636. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  8637. $chardata[$i]['level'] = $pel;
  8638. } elseif ($chardata[$i]['type'] == 'WS') {
  8639. $j = $i+1;
  8640. while ($j < $numchars) {
  8641. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  8642. (($j == ($numchars-1)) AND ($chardata[$j]['type'] == 'WS'))) {
  8643. $chardata[$i]['level'] = $pel;
  8644. break;
  8645. } elseif ($chardata[$j]['type'] != 'WS') {
  8646. break;
  8647. }
  8648. ++$j;
  8649. }
  8650. }
  8651. }
  8652. // Arabic Shaping
  8653. // Cursively connected scripts, such as Arabic or Syriac, require the selection of positional character shapes that depend on adjacent characters. Shaping is logically applied after the Bidirectional Algorithm is used and is limited to characters within the same directional run.
  8654. if ($arabic) {
  8655. $endedletter = array(1569,1570,1571,1572,1573,1575,1577,1583,1584,1585,1586,1608,1688);
  8656. $alfletter = array(1570,1571,1573,1575);
  8657. $chardata2 = $chardata;
  8658. $laaletter = false;
  8659. $charAL = array();
  8660. $x = 0;
  8661. for ($i=0; $i < $numchars; ++$i) {
  8662. if (($unicode[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  8663. $charAL[$x] = $chardata[$i];
  8664. $charAL[$x]['i'] = $i;
  8665. $chardata[$i]['x'] = $x;
  8666. ++$x;
  8667. }
  8668. }
  8669. $numAL = $x;
  8670. for ($i=0; $i < $numchars; ++$i) {
  8671. $thischar = $chardata[$i];
  8672. if ($i > 0) {
  8673. $prevchar = $chardata[($i-1)];
  8674. } else {
  8675. $prevchar = false;
  8676. }
  8677. if (($i+1) < $numchars) {
  8678. $nextchar = $chardata[($i+1)];
  8679. } else {
  8680. $nextchar = false;
  8681. }
  8682. if ($unicode[$thischar['char']] == 'AL') {
  8683. $x = $thischar['x'];
  8684. if ($x > 0) {
  8685. $prevchar = $charAL[($x-1)];
  8686. } else {
  8687. $prevchar = false;
  8688. }
  8689. if (($x+1) < $numAL) {
  8690. $nextchar = $charAL[($x+1)];
  8691. } else {
  8692. $nextchar = false;
  8693. }
  8694. // if laa letter
  8695. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  8696. $arabicarr = $laa_array;
  8697. $laaletter = true;
  8698. if ($x > 1) {
  8699. $prevchar = $charAL[($x-2)];
  8700. } else {
  8701. $prevchar = false;
  8702. }
  8703. } else {
  8704. $arabicarr = $unicode_arlet;
  8705. $laaletter = false;
  8706. }
  8707. if (($prevchar !== false) AND ($nextchar !== false) AND
  8708. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  8709. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  8710. ($prevchar['type'] == $thischar['type']) AND
  8711. ($nextchar['type'] == $thischar['type']) AND
  8712. ($nextchar['char'] != 1567)) {
  8713. if (in_array($prevchar['char'], $endedletter)) {
  8714. if (isset($arabicarr[$thischar['char']][2])) {
  8715. // initial
  8716. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  8717. }
  8718. } else {
  8719. if (isset($arabicarr[$thischar['char']][3])) {
  8720. // medial
  8721. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  8722. }
  8723. }
  8724. } elseif (($nextchar !== false) AND
  8725. (($unicode[$nextchar['char']] == 'AL') OR ($unicode[$nextchar['char']] == 'NSM')) AND
  8726. ($nextchar['type'] == $thischar['type']) AND
  8727. ($nextchar['char'] != 1567)) {
  8728. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  8729. // initial
  8730. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  8731. }
  8732. } elseif ((($prevchar !== false) AND
  8733. (($unicode[$prevchar['char']] == 'AL') OR ($unicode[$prevchar['char']] == 'NSM')) AND
  8734. ($prevchar['type'] == $thischar['type'])) OR
  8735. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  8736. // final
  8737. if (($i > 1) AND ($thischar['char'] == 1607) AND
  8738. ($chardata[$i-1]['char'] == 1604) AND
  8739. ($chardata[$i-2]['char'] == 1604)) {
  8740. //Allah Word
  8741. // mark characters to delete with false
  8742. $chardata2[$i-2]['char'] = false;
  8743. $chardata2[$i-1]['char'] = false;
  8744. $chardata2[$i]['char'] = 65010;
  8745. } else {
  8746. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  8747. if (isset($arabicarr[$thischar['char']][0])) {
  8748. // isolated
  8749. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  8750. }
  8751. } else {
  8752. if (isset($arabicarr[$thischar['char']][1])) {
  8753. // final
  8754. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  8755. }
  8756. }
  8757. }
  8758. } elseif (isset($arabicarr[$thischar['char']][0])) {
  8759. // isolated
  8760. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  8761. }
  8762. // if laa letter
  8763. if ($laaletter) {
  8764. // mark characters to delete with false
  8765. $chardata2[($charAL[($x-1)]['i'])]['char'] = false;
  8766. }
  8767. } // end if AL (Arabic Letter)
  8768. } // end for each char
  8769. /*
  8770. * Combining characters that can occur with Shadda (0651 HEX, 1617 DEC) are placed in UE586-UE594.
  8771. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  8772. */
  8773. $cw = &$this->CurrentFont['cw'];
  8774. for ($i = 0; $i < ($numchars-1); ++$i) {
  8775. if (($chardata2[$i]['char'] == 1617) AND (isset($diacritics[($chardata2[$i+1]['char'])]))) {
  8776. // check if the subtitution font is defined on current font
  8777. if (isset($cw[($diacritics[($chardata2[$i+1]['char'])])])) {
  8778. $chardata2[$i]['char'] = false;
  8779. $chardata2[$i+1]['char'] = $diacritics[($chardata2[$i+1]['char'])];
  8780. }
  8781. }
  8782. }
  8783. // remove marked characters
  8784. foreach ($chardata2 as $key => $value) {
  8785. if ($value['char'] === false) {
  8786. unset($chardata2[$key]);
  8787. }
  8788. }
  8789. $chardata = array_values($chardata2);
  8790. $numchars = count($chardata);
  8791. unset($chardata2);
  8792. unset($arabicarr);
  8793. unset($laaletter);
  8794. unset($charAL);
  8795. }
  8796. // L2. From the highest level found in the text to the lowest odd level on each line, including intermediate levels not actually present in the text, reverse any contiguous sequence of characters that are at that level or higher.
  8797. for ($j=$maxlevel; $j > 0; $j--) {
  8798. $ordarray = Array();
  8799. $revarr = Array();
  8800. $onlevel = false;
  8801. for ($i=0; $i < $numchars; ++$i) {
  8802. if ($chardata[$i]['level'] >= $j) {
  8803. $onlevel = true;
  8804. if (isset($unicode_mirror[$chardata[$i]['char']])) {
  8805. // L4. A character is depicted by a mirrored glyph if and only if (a) the resolved directionality of that character is R, and (b) the Bidi_Mirrored property value of that character is true.
  8806. $chardata[$i]['char'] = $unicode_mirror[$chardata[$i]['char']];
  8807. }
  8808. $revarr[] = $chardata[$i];
  8809. } else {
  8810. if ($onlevel) {
  8811. $revarr = array_reverse($revarr);
  8812. $ordarray = array_merge($ordarray, $revarr);
  8813. $revarr = Array();
  8814. $onlevel = false;
  8815. }
  8816. $ordarray[] = $chardata[$i];
  8817. }
  8818. }
  8819. if ($onlevel) {
  8820. $revarr = array_reverse($revarr);
  8821. $ordarray = array_merge($ordarray, $revarr);
  8822. }
  8823. $chardata = $ordarray;
  8824. }
  8825. $ordarray = array();
  8826. for ($i=0; $i < $numchars; ++$i) {
  8827. $ordarray[] = $chardata[$i]['char'];
  8828. }
  8829. return $ordarray;
  8830. }
  8831. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  8832. /*
  8833. * Adds a bookmark.
  8834. * @param string $txt bookmark description.
  8835. * @param int $level bookmark level (minimum value is 0).
  8836. * @param float $y Ordinate of the boorkmark position (default = -1 = current position).
  8837. * @param int $page target page number (leave empty for current page).
  8838. * @access public
  8839. * @author Olivier Plathey, Nicola Asuni
  8840. * @since 2.1.002 (2008-02-12)
  8841. */
  8842. public function Bookmark($txt, $level=0, $y=-1, $page='') {
  8843. if ($level < 0) {
  8844. $level = 0;
  8845. }
  8846. if (isset($this->outlines[0])) {
  8847. $lastoutline = end($this->outlines);
  8848. $maxlevel = $lastoutline['l'] + 1;
  8849. } else {
  8850. $maxlevel = 0;
  8851. }
  8852. if ($level > $maxlevel) {
  8853. $level = $maxlevel;
  8854. }
  8855. if ($y == -1) {
  8856. $y = $this->GetY();
  8857. }
  8858. if (empty($page)) {
  8859. $page = $this->PageNo();
  8860. }
  8861. $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $page);
  8862. }
  8863. /*
  8864. * Create a bookmark PDF string.
  8865. * @access protected
  8866. * @author Olivier Plathey, Nicola Asuni
  8867. * @since 2.1.002 (2008-02-12)
  8868. */
  8869. protected function _putbookmarks() {
  8870. $nb = count($this->outlines);
  8871. if ($nb == 0) {
  8872. return;
  8873. }
  8874. $lru = array();
  8875. $level = 0;
  8876. foreach ($this->outlines as $i => $o) {
  8877. if ($o['l'] > 0) {
  8878. $parent = $lru[($o['l'] - 1)];
  8879. //Set parent and last pointers
  8880. $this->outlines[$i]['parent'] = $parent;
  8881. $this->outlines[$parent]['last'] = $i;
  8882. if ($o['l'] > $level) {
  8883. //Level increasing: set first pointer
  8884. $this->outlines[$parent]['first'] = $i;
  8885. }
  8886. } else {
  8887. $this->outlines[$i]['parent'] = $nb;
  8888. }
  8889. if (($o['l'] <= $level) AND ($i > 0)) {
  8890. //Set prev and next pointers
  8891. $prev = $lru[$o['l']];
  8892. $this->outlines[$prev]['next'] = $i;
  8893. $this->outlines[$i]['prev'] = $prev;
  8894. }
  8895. $lru[$o['l']] = $i;
  8896. $level = $o['l'];
  8897. }
  8898. //Outline items
  8899. $n = $this->n + 1;
  8900. foreach ($this->outlines as $i => $o) {
  8901. $this->_newobj();
  8902. $this->_out('<</Title '.$this->_textstring($o['t']));
  8903. $this->_out('/Parent '.($n + $o['parent']).' 0 R');
  8904. if (isset($o['prev']))
  8905. $this->_out('/Prev '.($n + $o['prev']).' 0 R');
  8906. if (isset($o['next']))
  8907. $this->_out('/Next '.($n + $o['next']).' 0 R');
  8908. if (isset($o['first']))
  8909. $this->_out('/First '.($n + $o['first']).' 0 R');
  8910. if (isset($o['last']))
  8911. $this->_out('/Last '.($n + $o['last']).' 0 R');
  8912. $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]', (1 + (2 * $o['p'])), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k))));
  8913. $this->_out('/Count 0>>');
  8914. $this->_out('endobj');
  8915. }
  8916. //Outline root
  8917. $this->_newobj();
  8918. $this->OutlineRoot = $this->n;
  8919. $this->_out('<</Type /Outlines /First '.$n.' 0 R');
  8920. $this->_out('/Last '.($n + $lru[0]).' 0 R>>');
  8921. $this->_out('endobj');
  8922. }
  8923. // --- JAVASCRIPT - FORMS ------------------------------
  8924. /*
  8925. * Adds a javascript
  8926. * @access public
  8927. * @author Johannes Güntert, Nicola Asuni
  8928. * @since 2.1.002 (2008-02-12)
  8929. */
  8930. public function IncludeJS($script) {
  8931. $this->javascript .= $script;
  8932. }
  8933. /*
  8934. * Create a javascript PDF string.
  8935. * @access protected
  8936. * @author Johannes Güntert, Nicola Asuni
  8937. * @since 2.1.002 (2008-02-12)
  8938. */
  8939. protected function _putjavascript() {
  8940. if (empty($this->javascript)) {
  8941. return;
  8942. }
  8943. // the following two lines are uded to avoid form fields duplication after saving
  8944. $js1 = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  8945. $js2 = "getField('tcpdfdocsaved').value = 'saved';";
  8946. $this->_newobj();
  8947. $this->n_js = $this->n;
  8948. $this->_out('<<');
  8949. $this->_out('/Names [(EmbeddedJS) '.($this->n + 1).' 0 R ]');
  8950. $this->_out('>>');
  8951. $this->_out('endobj');
  8952. $this->_newobj();
  8953. $this->_out('<<');
  8954. $this->_out('/S /JavaScript');
  8955. $this->_out('/JS '.$this->_textstring($js1."\n".$this->javascript."\n".$js2));
  8956. $this->_out('>>');
  8957. $this->_out('endobj');
  8958. }
  8959. /*
  8960. * Convert color to javascript color.
  8961. * @param string $color color name or #RRGGBB
  8962. * @access protected
  8963. * @author Denis Van Nuffelen, Nicola Asuni
  8964. * @since 2.1.002 (2008-02-12)
  8965. */
  8966. protected function _JScolor($color) {
  8967. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  8968. if (substr($color,0,1) == '#') {
  8969. return sprintf("['RGB',%.3F,%.3F,%.3F]", hexdec(substr($color,1,2))/255, hexdec(substr($color,3,2))/255, hexdec(substr($color,5,2))/255);
  8970. }
  8971. if (!in_array($color,$aColors)) {
  8972. $this->Error('Invalid color: '.$color);
  8973. }
  8974. return 'color.'.$color;
  8975. }
  8976. /*
  8977. * Adds a javascript form field.
  8978. * @param string $type field type
  8979. * @param string $name field name
  8980. * @param int $x horizontal position
  8981. * @param int $y vertical position
  8982. * @param int $w width
  8983. * @param int $h height
  8984. * @param array $prop array of properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  8985. * @access protected
  8986. * @author Denis Van Nuffelen, Nicola Asuni
  8987. * @since 2.1.002 (2008-02-12)
  8988. */
  8989. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  8990. if ($this->rtl) {
  8991. $x = $x - $w;
  8992. }
  8993. // the followind avoid fields duplication after saving the document
  8994. $this->javascript .= "if(getField('tcpdfdocsaved').value != 'saved') {";
  8995. $k = $this->k;
  8996. $this->javascript .= sprintf("f".$name."=this.addField('%s','%s',%d,[%.2F,%.2F,%.2F,%.2F]);", $name, $type, $this->PageNo()-1, $x*$k, ($this->h-$y)*$k+1, ($x+$w)*$k, ($this->h-$y-$h)*$k+1)."\n";
  8997. $this->javascript .= 'f'.$name.'.textSize='.$this->FontSizePt.";\n";
  8998. while (list($key, $val) = each($prop)) {
  8999. if (strcmp(substr($key, -5), 'Color') == 0) {
  9000. $val = $this->_JScolor($val);
  9001. } else {
  9002. $val = "'".$val."'";
  9003. }
  9004. $this->javascript .= 'f'.$name.'.'.$key.'='.$val.";\n";
  9005. }
  9006. if ($this->rtl) {
  9007. $this->x -= $w;
  9008. } else {
  9009. $this->x += $w;
  9010. }
  9011. $this->javascript .= '}';
  9012. }
  9013. /*
  9014. * Creates a text field
  9015. * @param string $name field name
  9016. * @param int $w width
  9017. * @param int $h height
  9018. * @param string $prop properties. The value property allows to set the initial value. The multiline property allows to define the field as multiline. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  9019. * @access public
  9020. * @author Denis Van Nuffelen, Nicola Asuni
  9021. * @since 2.1.002 (2008-02-12)
  9022. */
  9023. public function TextField($name, $w, $h, $prop=array()) {
  9024. $this->_addfield('text', $name, $this->x, $this->y, $w, $h, $prop);
  9025. }
  9026. /*
  9027. * Creates a RadioButton field
  9028. * @param string $name field name
  9029. * @param int $w width
  9030. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  9031. * @access public
  9032. * @author Nicola Asuni
  9033. * @since 2.2.003 (2008-03-03)
  9034. */
  9035. public function RadioButton($name, $w, $prop=array()) {
  9036. if (!isset($prop['strokeColor'])) {
  9037. $prop['strokeColor']='black';
  9038. }
  9039. $this->_addfield('radiobutton', $name, $this->x, $this->y, $w, $w, $prop);
  9040. }
  9041. /*
  9042. * Creates a List-box field
  9043. * @param string $name field name
  9044. * @param int $w width
  9045. * @param int $h height
  9046. * @param array $values array containing the list of values.
  9047. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  9048. * @access public
  9049. * @author Nicola Asuni
  9050. * @since 2.2.003 (2008-03-03)
  9051. */
  9052. public function ListBox($name, $w, $h, $values, $prop=array()) {
  9053. if (!isset($prop['strokeColor'])) {
  9054. $prop['strokeColor'] = 'ltGray';
  9055. }
  9056. $this->_addfield('listbox', $name, $this->x, $this->y, $w, $h, $prop);
  9057. $s = '';
  9058. foreach ($values as $value) {
  9059. $s .= "'".addslashes($value)."',";
  9060. }
  9061. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  9062. }
  9063. /*
  9064. * Creates a Combo-box field
  9065. * @param string $name field name
  9066. * @param int $w width
  9067. * @param int $h height
  9068. * @param array $values array containing the list of values.
  9069. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  9070. * @access public
  9071. * @author Denis Van Nuffelen, Nicola Asuni
  9072. * @since 2.1.002 (2008-02-12)
  9073. */
  9074. public function ComboBox($name, $w, $h, $values, $prop=array()) {
  9075. $this->_addfield('combobox', $name, $this->x, $this->y, $w, $h, $prop);
  9076. $s = '';
  9077. foreach ($values as $value) {
  9078. $s .= "'".addslashes($value)."',";
  9079. }
  9080. $this->javascript .= 'f'.$name.'.setItems(['.substr($s, 0, -1)."]);\n";
  9081. }
  9082. /*
  9083. * Creates a CheckBox field
  9084. * @param string $name field name
  9085. * @param int $w width
  9086. * @param boolean $checked define the initial state (default = false).
  9087. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  9088. * @access public
  9089. * @author Denis Van Nuffelen, Nicola Asuni
  9090. * @since 2.1.002 (2008-02-12)
  9091. */
  9092. public function CheckBox($name, $w, $checked=false, $prop=array()) {
  9093. $prop['value'] = ($checked ? 'Yes' : 'Off');
  9094. if (!isset($prop['strokeColor'])) {
  9095. $prop['strokeColor'] = 'black';
  9096. }
  9097. $this->_addfield('checkbox', $name, $this->x, $this->y, $w, $w, $prop);
  9098. }
  9099. /*
  9100. * Creates a button field
  9101. * @param string $name field name
  9102. * @param int $w width
  9103. * @param int $h height
  9104. * @param string $caption caption.
  9105. * @param string $action action triggered by the button (JavaScript code).
  9106. * @param string $prop properties. Possible values are (http://www.adobe.com/devnet/acrobat/pdfs/js_developer_guide.pdf): <ul><li>rect: Position and size of field on page.</li><li>borderStyle: Rectangle border appearance.</li><li>strokeColor: Color of bounding rectangle.</li><li>lineWidth: Width of the edge of the surrounding rectangle.</li><li>rotation: Rotation of field in 90-degree increments.</li><li>fillColor: Background color of field (gray, transparent, RGB, or CMYK).</li><li>userName: Short description of field that appears on mouse-over.</li><li>readonly: Whether the user may change the field contents.</li><li>doNotScroll: Whether text fields may scroll.</li><li>display: Whether visible or hidden on screen or in print.</li><li>textFont: Text font.</li><li>textColor: Text color.</li><li>textSize: Text size.</li><li>richText: Rich text.</li><li>richValue: Text.</li><li>comb: Text comb format.</li><li>multiline: Text multiline.</li><li>charLimit: Text limit to number of characters.</li><li>fileSelect: Text file selection format.</li><li>password: Text password format.</li><li>alignment: Text layout in text fields.</li><li>buttonAlignX: X alignment of icon on button face.</li><li>buttonAlignY: Y alignment of icon on button face.</li><li>buttonFitBounds: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleHow: Relative scaling of an icon to fit inside a button face.</li><li>buttonScaleWhen: Relative scaling of an icon to fit inside a button face.</li><li>highlight: Appearance of a button when pushed.</li><li>style: Glyph style for checkbox and radio buttons.</li><li>numItems: Number of items in a combo box or list box.</li><li>editable: Whether the user can type in a combo box.</li><li>multipleSelection: Whether multiple list box items may be selected.</li></ul>
  9107. * @access public
  9108. * @author Denis Van Nuffelen, Nicola Asuni
  9109. * @since 2.1.002 (2008-02-12)
  9110. */
  9111. public function Button($name, $w, $h, $caption, $action, $prop=array()) {
  9112. if (!isset($prop['strokeColor'])) {
  9113. $prop['strokeColor'] = 'black';
  9114. }
  9115. if (!isset($prop['borderStyle'])) {
  9116. $prop['borderStyle'] = 'beveled';
  9117. }
  9118. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  9119. $this->javascript .= 'f'.$name.".buttonSetCaption('".addslashes($caption)."');\n";
  9120. $this->javascript .= 'f'.$name.".setAction('MouseUp','".addslashes($action)."');\n";
  9121. $this->javascript .= 'f'.$name.".highlight='push';\n";
  9122. $this->javascript .= 'f'.$name.".print=false;\n";
  9123. }
  9124. // END JAVASCRIPT - FORMS ------------------------------
  9125. /*
  9126. * Enable Write permissions for PDF Reader.
  9127. * WARNING: This works only using the Adobe private key with the setSignature() method.
  9128. * @access protected
  9129. * @author Nicola Asuni
  9130. * @since 2.9.000 (2008-03-26)
  9131. */
  9132. protected function _putursignature() {
  9133. if ((!$this->sign) OR (isset($this->signature_data['cert_type']) AND ($this->signature_data['cert_type'] > 0))) {
  9134. return;
  9135. }
  9136. $this->_out('/Type /Sig');
  9137. $this->_out('/Filter /Adobe.PPKLite');
  9138. $this->_out('/SubFilter /adbe.pkcs7.detached');
  9139. $this->_out($this->byterange_string);
  9140. $this->_out('/Contents<>'.str_repeat(' ', $this->signature_max_lenght));
  9141. if ($this->ur) {
  9142. $this->_out('/Reference');
  9143. $this->_out('[');
  9144. $this->_out('<<');
  9145. $this->_out('/Type /SigRef');
  9146. $this->_out('/TransformMethod /UR3');
  9147. $this->_out('/TransformParams');
  9148. $this->_out('<<');
  9149. $this->_out('/Type /TransformParams');
  9150. $this->_out('/V /2.2');
  9151. if (!$this->empty_string($this->ur_document)) {
  9152. $this->_out('/Document['.$this->ur_document.']');
  9153. }
  9154. if (!$this->empty_string($this->ur_annots)) {
  9155. $this->_out('/Annots['.$this->ur_annots.']');
  9156. }
  9157. if (!$this->empty_string($this->ur_form)) {
  9158. $this->_out('/Form['.$this->ur_form.']');
  9159. }
  9160. if (!$this->empty_string($this->ur_signature)) {
  9161. $this->_out('/Signature['.$this->ur_signature.']');
  9162. }
  9163. $this->_out('>>');
  9164. $this->_out('>>');
  9165. $this->_out(']');
  9166. }
  9167. $this->_out('/M '.$this->_datestring());
  9168. }
  9169. /*
  9170. * Add certification signature (DocMDP)
  9171. * @access protected
  9172. * @author Nicola Asuni
  9173. * @since 4.6.008 (2009-05-07)
  9174. */
  9175. protected function _putsignature() {
  9176. if ((!$this->sign) OR (isset($this->signature_data['cert_type']) AND ($this->signature_data['cert_type'] <= 0))) {
  9177. return;
  9178. }
  9179. $this->_out('/Type /Sig');
  9180. $this->_out('/Filter /Adobe.PPKLite');
  9181. $this->_out('/SubFilter /adbe.pkcs7.detached');
  9182. $this->_out($this->byterange_string);
  9183. $this->_out('/Contents<>'.str_repeat(' ', $this->signature_max_lenght));
  9184. $this->_out('/Reference');
  9185. $this->_out('[');
  9186. $this->_out('<<');
  9187. $this->_out('/Type /SigRef');
  9188. $this->_out('/TransformMethod /DocMDP');
  9189. $this->_out('/TransformParams');
  9190. $this->_out('<<');
  9191. $this->_out('/Type /TransformParams');
  9192. $this->_out('/V /1.2');
  9193. $this->_out('/P '.$this->signature_data['cert_type'].'');
  9194. $this->_out('>>');
  9195. $this->_out('>>');
  9196. $this->_out(']');
  9197. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  9198. $this->_out('/Name '.$this->_textstring($this->signature_data['info']['Name']).'');
  9199. }
  9200. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  9201. $this->_out('/Location '.$this->_textstring($this->signature_data['info']['Location']).'');
  9202. }
  9203. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  9204. $this->_out('/Reason '.$this->_textstring($this->signature_data['info']['Reason']).'');
  9205. }
  9206. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  9207. $this->_out('/ContactInfo '.$this->_textstring($this->signature_data['info']['ContactInfo']).'');
  9208. }
  9209. $this->_out('/M '.$this->_datestring());
  9210. }
  9211. /*
  9212. * Set User's Rights for PDF Reader
  9213. * WARNING: This works only using the Adobe private key with the setSignature() method!.
  9214. * Check the PDF Reference 8.7.1 Transform Methods,
  9215. * Table 8.105 Entries in the UR transform parameters dictionary
  9216. * @param boolean $enable if true enable user's rights on PDF reader
  9217. * @param string $document Names specifying additional document-wide usage rights for the document. The only defined value is "/FullSave", which permits a user to save the document along with modified form and/or annotation data.
  9218. * @param string $annots Names specifying additional annotation-related usage rights for the document. Valid names in PDF 1.5 and later are /Create/Delete/Modify/Copy/Import/Export, which permit the user to perform the named operation on annotations.
  9219. * @param string $form Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  9220. * @param string $signature Names specifying additional signature-related usage rights for the document. The only defined value is /Modify, which permits a user to apply a digital signature to an existing signature form field or clear a signed signature form field.
  9221. * @access public
  9222. * @author Nicola Asuni
  9223. * @since 2.9.000 (2008-03-26)
  9224. */
  9225. public function setUserRights(
  9226. $enable=true,
  9227. $document='/FullSave',
  9228. $annots='/Create/Delete/Modify/Copy/Import/Export',
  9229. $form='/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate',
  9230. $signature='/Modify') {
  9231. $this->ur = $enable;
  9232. $this->ur_document = $document;
  9233. $this->ur_annots = $annots;
  9234. $this->ur_form = $form;
  9235. $this->ur_signature = $signature;
  9236. }
  9237. /*
  9238. * Enable document signature (requires the OpenSSL Library).
  9239. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  9240. * @param mixed $signing_cert signing certificate (string or filename prefixed with 'file://')
  9241. * @param mixed $private_key private key (string or filename prefixed with 'file://')
  9242. * @param string $private_key_password password
  9243. * @param string $extracerts specifies the name of a file containing a bunch of extra certificates to include in the signature which can for example be used to help the recipient to verify the certificate that you used.
  9244. * @param int $cert_type The access permissions granted for this document. Valid values shall be: 1 = No changes to the document shall be permitted; any change to the document shall invalidate the signature; 2 = Permitted changes shall be filling in forms, instantiating page templates, and signing; other changes shall invalidate the signature; 3 = Permitted changes shall be the same as for 2, as well as annotation creation, deletion, and modification; other changes shall invalidate the signature.
  9245. * @parm array $info array of option information: Name, Location, Reason, ContactInfo.
  9246. * @access public
  9247. * @author Nicola Asuni
  9248. * @since 4.6.005 (2009-04-24)
  9249. */
  9250. public function setSignature($signing_cert='', $private_key='', $private_key_password='', $extracerts='', $cert_type=2, $info=array()) {
  9251. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  9252. // to convert pfx certificate to pem: openssl
  9253. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  9254. $this->sign = true;
  9255. $this->signature_data = array();
  9256. if (strlen($signing_cert) == 0) {
  9257. $signing_cert = 'file://'.dirname(__FILE__).'/tcpdf.crt';
  9258. }
  9259. if (strlen($private_key) == 0) {
  9260. $private_key = $signing_cert;
  9261. }
  9262. $this->signature_data['signcert'] = $signing_cert;
  9263. $this->signature_data['privkey'] = $private_key;
  9264. $this->signature_data['password'] = $private_key_password;
  9265. $this->signature_data['extracerts'] = $extracerts;
  9266. $this->signature_data['cert_type'] = $cert_type;
  9267. $this->signature_data['info'] = $info;
  9268. }
  9269. /*
  9270. * Create a new page group.
  9271. * NOTE: call this function before calling AddPage()
  9272. * @param int $page starting group page (leave empty for next page).
  9273. * @access public
  9274. * @since 3.0.000 (2008-03-27)
  9275. */
  9276. public function startPageGroup($page='') {
  9277. if (empty($page)) {
  9278. $page = $this->page + 1;
  9279. }
  9280. $this->newpagegroup[$page] = true;
  9281. }
  9282. /**
  9283. * Defines an alias for the total number of pages.
  9284. * It will be substituted as the document is closed.
  9285. * @param string $alias The alias.
  9286. * @access public
  9287. * @since 1.4
  9288. * @see getAliasNbPages(), PageNo(), Footer()
  9289. */
  9290. public function AliasNbPages($alias='{nb}') {
  9291. $this->AliasNbPages = $alias;
  9292. }
  9293. /**
  9294. * Returns the string alias used for the total number of pages.
  9295. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  9296. * @return string
  9297. * @access public
  9298. * @since 4.0.018 (2008-08-08)
  9299. * @see AliasNbPages(), PageNo(), Footer()
  9300. */
  9301. public function getAliasNbPages() {
  9302. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  9303. return '{'.$this->AliasNbPages.'}';
  9304. }
  9305. return $this->AliasNbPages;
  9306. }
  9307. /**
  9308. * Defines an alias for the page number.
  9309. * It will be substituted as the document is closed.
  9310. * @param string $alias The alias.
  9311. * @access public
  9312. * @since 4.5.000 (2009-01-02)
  9313. * @see getAliasNbPages(), PageNo(), Footer()
  9314. */
  9315. public function AliasNumPage($alias='{pnb}') {
  9316. //Define an alias for total number of pages
  9317. $this->AliasNumPage = $alias;
  9318. }
  9319. /**
  9320. * Returns the string alias used for the page number.
  9321. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  9322. * @return string
  9323. * @access public
  9324. * @since 4.5.000 (2009-01-02)
  9325. * @see AliasNbPages(), PageNo(), Footer()
  9326. */
  9327. public function getAliasNumPage() {
  9328. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  9329. return '{'.$this->AliasNumPage.'}';
  9330. }
  9331. return $this->AliasNumPage;
  9332. }
  9333. /*
  9334. * Return the current page in the group.
  9335. * @return current page in the group
  9336. * @access public
  9337. * @since 3.0.000 (2008-03-27)
  9338. */
  9339. public function getGroupPageNo() {
  9340. return $this->pagegroups[$this->currpagegroup];
  9341. }
  9342. /**
  9343. * Returns the current group page number formatted as a string.
  9344. * @access public
  9345. * @since 4.3.003 (2008-11-18)
  9346. * @see PaneNo(), formatPageNumber()
  9347. */
  9348. public function getGroupPageNoFormatted() {
  9349. return $this->formatPageNumber($this->getGroupPageNo());
  9350. }
  9351. /*
  9352. * Return the alias of the current page group
  9353. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  9354. * (will be replaced by the total number of pages in this group).
  9355. * @return alias of the current page group
  9356. * @access public
  9357. * @since 3.0.000 (2008-03-27)
  9358. */
  9359. public function getPageGroupAlias() {
  9360. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  9361. return '{'.$this->currpagegroup.'}';
  9362. }
  9363. return $this->currpagegroup;
  9364. }
  9365. /*
  9366. * Return the alias for the page number on the current page group
  9367. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  9368. * (will be replaced by the total number of pages in this group).
  9369. * @return alias of the current page group
  9370. * @access public
  9371. * @since 4.5.000 (2009-01-02)
  9372. */
  9373. public function getPageNumGroupAlias() {
  9374. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  9375. return '{'.str_replace('{nb', '{pnb', $this->currpagegroup).'}';
  9376. }
  9377. return str_replace('{nb', '{pnb', $this->currpagegroup);
  9378. }
  9379. /**
  9380. * Format the page numbers.
  9381. * This method can be overriden for custom formats.
  9382. * @param int $num page number
  9383. * @access protected
  9384. * @since 4.2.005 (2008-11-06)
  9385. */
  9386. protected function formatPageNumber($num) {
  9387. return number_format((float)$num, 0, '', '.');
  9388. }
  9389. /**
  9390. * Format the page numbers on the Table Of Content.
  9391. * This method can be overriden for custom formats.
  9392. * @param int $num page number
  9393. * @access protected
  9394. * @since 4.5.001 (2009-01-04)
  9395. * @see addTOC()
  9396. */
  9397. protected function formatTOCPageNumber($num) {
  9398. return number_format((float)$num, 0, '', '.');
  9399. }
  9400. /**
  9401. * Returns the current page number formatted as a string.
  9402. * @access public
  9403. * @since 4.2.005 (2008-11-06)
  9404. * @see PaneNo(), formatPageNumber()
  9405. */
  9406. public function PageNoFormatted() {
  9407. return $this->formatPageNumber($this->PageNo());
  9408. }
  9409. /*
  9410. * Put visibility settings.
  9411. * @access protected
  9412. * @since 3.0.000 (2008-03-27)
  9413. */
  9414. protected function _putocg() {
  9415. $this->_newobj();
  9416. $this->n_ocg_print = $this->n;
  9417. $this->_out('<</Type /OCG /Name '.$this->_textstring('print'));
  9418. $this->_out('/Usage <</Print <</PrintState /ON>> /View <</ViewState /OFF>>>>>>');
  9419. $this->_out('endobj');
  9420. $this->_newobj();
  9421. $this->n_ocg_view=$this->n;
  9422. $this->_out('<</Type /OCG /Name '.$this->_textstring('view'));
  9423. $this->_out('/Usage <</Print <</PrintState /OFF>> /View <</ViewState /ON>>>>>>');
  9424. $this->_out('endobj');
  9425. }
  9426. /*
  9427. * Set the visibility of the successive elements.
  9428. * This can be useful, for instance, to put a background
  9429. * image or color that will show on screen but won't print.
  9430. * @param string $v visibility mode. Legal values are: all, print, screen.
  9431. * @access public
  9432. * @since 3.0.000 (2008-03-27)
  9433. */
  9434. public function setVisibility($v) {
  9435. if ($this->openMarkedContent) {
  9436. // close existing open marked-content
  9437. $this->_out('EMC');
  9438. $this->openMarkedContent = false;
  9439. }
  9440. switch($v) {
  9441. case 'print': {
  9442. $this->_out('/OC /OC1 BDC');
  9443. $this->openMarkedContent = true;
  9444. break;
  9445. }
  9446. case 'screen': {
  9447. $this->_out('/OC /OC2 BDC');
  9448. $this->openMarkedContent = true;
  9449. break;
  9450. }
  9451. case 'all': {
  9452. $this->_out('');
  9453. break;
  9454. }
  9455. default: {
  9456. $this->Error('Incorrect visibility: '.$v);
  9457. break;
  9458. }
  9459. }
  9460. $this->visibility = $v;
  9461. }
  9462. /*
  9463. * Add transparency parameters to the current extgstate
  9464. * @param array $params parameters
  9465. * @return the number of extgstates
  9466. * @access protected
  9467. * @since 3.0.000 (2008-03-27)
  9468. */
  9469. protected function addExtGState($parms) {
  9470. $n = count($this->extgstates) + 1;
  9471. $this->extgstates[$n]['parms'] = $parms;
  9472. return $n;
  9473. }
  9474. /*
  9475. * Add an extgstate
  9476. * @param array $gs extgstate
  9477. * @access protected
  9478. * @since 3.0.000 (2008-03-27)
  9479. */
  9480. protected function setExtGState($gs) {
  9481. $this->_out(sprintf('/GS%d gs', $gs));
  9482. }
  9483. /*
  9484. * Put extgstates for object transparency
  9485. * @param array $gs extgstate
  9486. * @access protected
  9487. * @since 3.0.000 (2008-03-27)
  9488. */
  9489. protected function _putextgstates() {
  9490. $ne = count($this->extgstates);
  9491. for ($i = 1; $i <= $ne; ++$i) {
  9492. $this->_newobj();
  9493. $this->extgstates[$i]['n'] = $this->n;
  9494. $this->_out('<</Type /ExtGState');
  9495. foreach ($this->extgstates[$i]['parms'] as $k => $v) {
  9496. $this->_out('/'.$k.' '.$v);
  9497. }
  9498. $this->_out('>>');
  9499. $this->_out('endobj');
  9500. }
  9501. }
  9502. /*
  9503. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  9504. * @param float $alpha real value from 0 (transparent) to 1 (opaque)
  9505. * @param string $bm blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
  9506. * @access public
  9507. * @since 3.0.000 (2008-03-27)
  9508. */
  9509. public function setAlpha($alpha, $bm='Normal') {
  9510. $gs = $this->addExtGState(array('ca' => $alpha, 'CA' => $alpha, 'BM' => '/'.$bm));
  9511. $this->setExtGState($gs);
  9512. }
  9513. /*
  9514. * Set the default JPEG compression quality (1-100)
  9515. * @param int $quality JPEG quality, integer between 1 and 100
  9516. * @access public
  9517. * @since 3.0.000 (2008-03-27)
  9518. */
  9519. public function setJPEGQuality($quality) {
  9520. if (($quality < 1) OR ($quality > 100)) {
  9521. $quality = 75;
  9522. }
  9523. $this->jpeg_quality = intval($quality);
  9524. }
  9525. /*
  9526. * Set the default number of columns in a row for HTML tables.
  9527. * @param int $cols number of columns
  9528. * @access public
  9529. * @since 3.0.014 (2008-06-04)
  9530. */
  9531. public function setDefaultTableColumns($cols=4) {
  9532. $this->default_table_columns = intval($cols);
  9533. }
  9534. /*
  9535. * Set the height of the cell (line height) respect the font height.
  9536. * @param int $h cell proportion respect font height (typical value = 1.25).
  9537. * @access public
  9538. * @since 3.0.014 (2008-06-04)
  9539. */
  9540. public function setCellHeightRatio($h) {
  9541. $this->cell_height_ratio = $h;
  9542. }
  9543. /*
  9544. * return the height of cell repect font height.
  9545. * @access public
  9546. * @since 4.0.012 (2008-07-24)
  9547. */
  9548. public function getCellHeightRatio() {
  9549. return $this->cell_height_ratio;
  9550. }
  9551. /*
  9552. * Set the PDF version (check PDF reference for valid values).
  9553. * Default value is 1.t
  9554. * @access public
  9555. * @since 3.1.000 (2008-06-09)
  9556. */
  9557. public function setPDFVersion($version='1.7') {
  9558. $this->PDFVersion = $version;
  9559. }
  9560. /*
  9561. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  9562. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  9563. * <ul>
  9564. * <li>HideToolbar boolean (Optional) A flag specifying whether to hide the viewer application's tool bars when the document is active. Default value: false.</li>
  9565. * <li>HideMenubar boolean (Optional) A flag specifying whether to hide the viewer application's menu bar when the document is active. Default value: false.</li>
  9566. * <li>HideWindowUI boolean (Optional) A flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. Default value: false.</li>
  9567. * <li>FitWindow boolean (Optional) A flag specifying whether to resize the document's window to fit the size of the first displayed page. Default value: false.</li>
  9568. * <li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li>
  9569. * <li>DisplayDocTitle boolean (Optional; PDF 1.4) A flag specifying whether the window's title bar should display the document title taken from the Title entry of the document information dictionary (see Section 10.2.1, "Document Information Dictionary"). If false, the title bar should instead display the name of the PDF file containing the document. Default value: false.</li>
  9570. * <li>NonFullScreenPageMode name (Optional) The document's page mode, specifying how to display the document on exiting full-screen mode:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>UseOC Optional content group panel visible</li><ul>This entry is meaningful only if the value of the PageMode entry in the catalog dictionary (see Section 3.6.1, "Document Catalog") is FullScreen; it is ignored otherwise. Default value: UseNone.</li>
  9571. * <li>ViewArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be displayed when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9572. * <li>ViewClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when viewing the document on the screen. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9573. * <li>PrintArea name (Optional; PDF 1.4) The name of the page boundary representing the area of a page to be rendered when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9574. * <li>PrintClip name (Optional; PDF 1.4) The name of the page boundary to which the contents of a page are to be clipped when printing the document. Valid values are (see Section 10.10.1, "Page Boundaries").:<ul><li>MediaBox</li><li>CropBox (default)</li><li>BleedBox</li><li>TrimBox</li><li>ArtBox</li></ul></li>
  9575. * <li>PrintScaling name (Optional; PDF 1.6) The page scaling option to be selected when a print dialog is displayed for this document. Valid values are: <ul><li>None, which indicates that the print dialog should reflect no page scaling</li><li>AppDefault (default), which indicates that applications should use the current print scaling</li><ul></li>
  9576. * <li>Duplex name (Optional; PDF 1.7) The paper handling option to use when printing the file from the print dialog. The following values are valid:<ul><li>Simplex - Print single-sided</li><li>DuplexFlipShortEdge - Duplex and flip on the short edge of the sheet</li><li>DuplexFlipLongEdge - Duplex and flip on the long edge of the sheet</li></ul>Default value: none</li>
  9577. * <li>PickTrayByPDFSize boolean (Optional; PDF 1.7) A flag specifying whether the PDF page size is used to select the input paper tray. This setting influences only the preset values used to populate the print dialog presented by a PDF viewer application. If PickTrayByPDFSize is true, the check box in the print dialog associated with input paper tray is checked. Note: This setting has no effect on Mac OS systems, which do not provide the ability to pick the input tray by size.</li>
  9578. * <li>PrintPageRange array (Optional; PDF 1.7) The page numbers used to initialize the print dialog box when the file is printed. The first page of the PDF file is denoted by 1. Each pair consists of the first and last pages in the sub-range. An odd number of integers causes this entry to be ignored. Negative numbers cause the entire array to be ignored. Default value: as defined by PDF viewer application</li>
  9579. * <li>NumCopies integer (Optional; PDF 1.7) The number of copies to be printed when the print dialog is opened for this file. Supported values are the integers 2 through 5. Values outside this range are ignored. Default value: as defined by PDF viewer application, but typically 1</li>
  9580. * </ul>
  9581. * @param array $preferences array of options.
  9582. * @author Nicola Asuni
  9583. * @access public
  9584. * @since 3.1.000 (2008-06-09)
  9585. */
  9586. public function setViewerPreferences($preferences) {
  9587. $this->viewer_preferences = $preferences;
  9588. }
  9589. /**
  9590. * Paints a linear colour gradient.
  9591. * @param float $x abscissa of the top left corner of the rectangle.
  9592. * @param float $y ordinate of the top left corner of the rectangle.
  9593. * @param float $w width of the rectangle.
  9594. * @param float $h height of the rectangle.
  9595. * @param array $col1 first color (RGB components).
  9596. * @param array $col2 second color (RGB components).
  9597. * @param array $coords array of the form (x1, y1, x2, y2) which defines the gradient vector (see linear_gradient_coords.jpg). The default value is from left to right (x1=0, y1=0, x2=1, y2=0).
  9598. * @author Andreas Würmser, Nicola Asuni
  9599. * @since 3.1.000 (2008-06-09)
  9600. * @access public
  9601. */
  9602. public function LinearGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0,0,1,0)) {
  9603. $this->Clip($x, $y, $w, $h);
  9604. $this->Gradient(2, $col1, $col2, $coords);
  9605. }
  9606. /**
  9607. * Paints a radial colour gradient.
  9608. * @param float $x abscissa of the top left corner of the rectangle.
  9609. * @param float $y ordinate of the top left corner of the rectangle.
  9610. * @param float $w width of the rectangle.
  9611. * @param float $h height of the rectangle.
  9612. * @param array $col1 first color (RGB components).
  9613. * @param array $col2 second color (RGB components).
  9614. * @param array $coords array of the form (fx, fy, cx, cy, r) where (fx, fy) is the starting point of the gradient with color1, (cx, cy) is the center of the circle with color2, and r is the radius of the circle (see radial_gradient_coords.jpg). (fx, fy) should be inside the circle, otherwise some areas will not be defined.
  9615. * @author Andreas Würmser, Nicola Asuni
  9616. * @since 3.1.000 (2008-06-09)
  9617. * @access public
  9618. */
  9619. public function RadialGradient($x, $y, $w, $h, $col1=array(), $col2=array(), $coords=array(0.5,0.5,0.5,0.5,1)) {
  9620. $this->Clip($x, $y, $w, $h);
  9621. $this->Gradient(3, $col1, $col2, $coords);
  9622. }
  9623. /**
  9624. * Paints a coons patch mesh.
  9625. * @param float $x abscissa of the top left corner of the rectangle.
  9626. * @param float $y ordinate of the top left corner of the rectangle.
  9627. * @param float $w width of the rectangle.
  9628. * @param float $h height of the rectangle.
  9629. * @param array $col1 first color (lower left corner) (RGB components).
  9630. * @param array $col2 second color (lower right corner) (RGB components).
  9631. * @param array $col3 third color (upper right corner) (RGB components).
  9632. * @param array $col4 fourth color (upper left corner) (RGB components).
  9633. * @param array $coords <ul><li>for one patch mesh: array(float x1, float y1, .... float x12, float y12): 12 pairs of coordinates (normally from 0 to 1) which specify the Bezier control points that define the patch. First pair is the lower left edge point, next is its right control point (control point 2). Then the other points are defined in the order: control point 1, edge point, control point 2 going counter-clockwise around the patch. Last (x12, y12) is the first edge point's left control point (control point 1).</li><li>for two or more patch meshes: array[number of patches]: arrays with the following keys for each patch: f: where to put that patch (0 = first patch, 1, 2, 3 = right, top and left of precedent patch - I didn't figure this out completely - just try and error ;-) points: 12 pairs of coordinates of the Bezier control points as above for the first patch, 8 pairs of coordinates for the following patches, ignoring the coordinates already defined by the precedent patch (I also didn't figure out the order of these - also: try and see what's happening) colors: must be 4 colors for the first patch, 2 colors for the following patches</li></ul>
  9634. * @param array $coords_min minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
  9635. * @param array $coords_max maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
  9636. * @author Andreas Würmser, Nicola Asuni
  9637. * @since 3.1.000 (2008-06-09)
  9638. * @access public
  9639. */
  9640. public function CoonsPatchMesh($x, $y, $w, $h, $col1=array(), $col2=array(), $col3=array(), $col4=array(), $coords=array(0.00,0.0,0.33,0.00,0.67,0.00,1.00,0.00,1.00,0.33,1.00,0.67,1.00,1.00,0.67,1.00,0.33,1.00,0.00,1.00,0.00,0.67,0.00,0.33), $coords_min=0, $coords_max=1) {
  9641. $this->Clip($x, $y, $w, $h);
  9642. $n = count($this->gradients) + 1;
  9643. $this->gradients[$n]['type'] = 6; //coons patch mesh
  9644. //check the coords array if it is the simple array or the multi patch array
  9645. if (!isset($coords[0]['f'])) {
  9646. //simple array -> convert to multi patch array
  9647. if (!isset($col1[1])) {
  9648. $col1[1] = $col1[2] = $col1[0];
  9649. }
  9650. if (!isset($col2[1])) {
  9651. $col2[1] = $col2[2] = $col2[0];
  9652. }
  9653. if (!isset($col3[1])) {
  9654. $col3[1] = $col3[2] = $col3[0];
  9655. }
  9656. if (!isset($col4[1])) {
  9657. $col4[1] = $col4[2] = $col4[0];
  9658. }
  9659. $patch_array[0]['f'] = 0;
  9660. $patch_array[0]['points'] = $coords;
  9661. $patch_array[0]['colors'][0]['r'] = $col1[0];
  9662. $patch_array[0]['colors'][0]['g'] = $col1[1];
  9663. $patch_array[0]['colors'][0]['b'] = $col1[2];
  9664. $patch_array[0]['colors'][1]['r'] = $col2[0];
  9665. $patch_array[0]['colors'][1]['g'] = $col2[1];
  9666. $patch_array[0]['colors'][1]['b'] = $col2[2];
  9667. $patch_array[0]['colors'][2]['r'] = $col3[0];
  9668. $patch_array[0]['colors'][2]['g'] = $col3[1];
  9669. $patch_array[0]['colors'][2]['b'] = $col3[2];
  9670. $patch_array[0]['colors'][3]['r'] = $col4[0];
  9671. $patch_array[0]['colors'][3]['g'] = $col4[1];
  9672. $patch_array[0]['colors'][3]['b'] = $col4[2];
  9673. } else {
  9674. //multi patch array
  9675. $patch_array = $coords;
  9676. }
  9677. $bpcd = 65535; //16 BitsPerCoordinate
  9678. //build the data stream
  9679. $this->gradients[$n]['stream'] = '';
  9680. $count_patch = count($patch_array);
  9681. for ($i=0; $i < $count_patch; ++$i) {
  9682. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  9683. $count_points = count($patch_array[$i]['points']);
  9684. for ($j=0; $j < $count_points; ++$j) {
  9685. //each point as 16 bit
  9686. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  9687. if ($patch_array[$i]['points'][$j] < 0) {
  9688. $patch_array[$i]['points'][$j] = 0;
  9689. }
  9690. if ($patch_array[$i]['points'][$j] > $bpcd) {
  9691. $patch_array[$i]['points'][$j] = $bpcd;
  9692. }
  9693. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  9694. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  9695. }
  9696. $count_cols = count($patch_array[$i]['colors']);
  9697. for ($j=0; $j < $count_cols; ++$j) {
  9698. //each color component as 8 bit
  9699. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  9700. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  9701. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  9702. }
  9703. }
  9704. //paint the gradient
  9705. $this->_out('/Sh'.$n.' sh');
  9706. //restore previous Graphic State
  9707. $this->_out('Q');
  9708. }
  9709. /**
  9710. * Set a rectangular clipping area.
  9711. * @param float $x abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  9712. * @param float $y ordinate of the top left corner of the rectangle.
  9713. * @param float $w width of the rectangle.
  9714. * @param float $h height of the rectangle.
  9715. * @author Andreas Würmser, Nicola Asuni
  9716. * @since 3.1.000 (2008-06-09)
  9717. * @access protected
  9718. */
  9719. protected function Clip($x, $y, $w, $h) {
  9720. if ($this->rtl) {
  9721. $x = $this->w - $x - $w;
  9722. }
  9723. //save current Graphic State
  9724. $s = 'q';
  9725. //set clipping area
  9726. $s .= sprintf(' %.2F %.2F %.2F %.2F re W n', $x*$this->k, ($this->h-$y)*$this->k, $w*$this->k, -$h*$this->k);
  9727. //set up transformation matrix for gradient
  9728. $s .= sprintf(' %.3F 0 0 %.3F %.3F %.3F cm', $w*$this->k, $h*$this->k, $x*$this->k, ($this->h-($y+$h))*$this->k);
  9729. $this->_out($s);
  9730. }
  9731. /**
  9732. * Output gradient.
  9733. * @param int $type type of gradient.
  9734. * @param array $col1 first color (RGB components).
  9735. * @param array $col2 second color (RGB components).
  9736. * @param array $coords array of coordinates.
  9737. * @author Andreas Würmser, Nicola Asuni
  9738. * @since 3.1.000 (2008-06-09)
  9739. * @access protected
  9740. */
  9741. protected function Gradient($type, $col1, $col2, $coords) {
  9742. $n = count($this->gradients) + 1;
  9743. $this->gradients[$n]['type'] = $type;
  9744. if (!isset($col1[1])) {
  9745. $col1[1]=$col1[2]=$col1[0];
  9746. }
  9747. $this->gradients[$n]['col1'] = sprintf('%.3F %.3F %.3F', ($col1[0]/255), ($col1[1]/255), ($col1[2]/255));
  9748. if (!isset($col2[1])) {
  9749. $col2[1] = $col2[2] = $col2[0];
  9750. }
  9751. $this->gradients[$n]['col2'] = sprintf('%.3F %.3F %.3F', ($col2[0]/255), ($col2[1]/255), ($col2[2]/255));
  9752. $this->gradients[$n]['coords'] = $coords;
  9753. //paint the gradient
  9754. $this->_out('/Sh'.$n.' sh');
  9755. //restore previous Graphic State
  9756. $this->_out('Q');
  9757. }
  9758. /**
  9759. * Output shaders.
  9760. * @author Andreas Würmser, Nicola Asuni
  9761. * @since 3.1.000 (2008-06-09)
  9762. * @access protected
  9763. */
  9764. function _putshaders() {
  9765. foreach ($this->gradients as $id => $grad) {
  9766. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  9767. $this->_newobj();
  9768. $this->_out('<<');
  9769. $this->_out('/FunctionType 2');
  9770. $this->_out('/Domain [0.0 1.0]');
  9771. $this->_out('/C0 ['.$grad['col1'].']');
  9772. $this->_out('/C1 ['.$grad['col2'].']');
  9773. $this->_out('/N 1');
  9774. $this->_out('>>');
  9775. $this->_out('endobj');
  9776. $f1 = $this->n;
  9777. }
  9778. $this->_newobj();
  9779. $this->_out('<<');
  9780. $this->_out('/ShadingType '.$grad['type']);
  9781. $this->_out('/ColorSpace /DeviceRGB');
  9782. if ($grad['type'] == 2) {
  9783. $this->_out(sprintf('/Coords [%.3F %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]));
  9784. $this->_out('/Function '.$f1.' 0 R');
  9785. $this->_out('/Extend [true true] ');
  9786. $this->_out('>>');
  9787. } elseif ($grad['type'] == 3) {
  9788. //x0, y0, r0, x1, y1, r1
  9789. //at this this time radius of inner circle is 0
  9790. $this->_out(sprintf('/Coords [%.3F %.3F 0 %.3F %.3F %.3F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]));
  9791. $this->_out('/Function '.$f1.' 0 R');
  9792. $this->_out('/Extend [true true] ');
  9793. $this->_out('>>');
  9794. } elseif ($grad['type'] == 6) {
  9795. $this->_out('/BitsPerCoordinate 16');
  9796. $this->_out('/BitsPerComponent 8');
  9797. $this->_out('/Decode[0 1 0 1 0 1 0 1 0 1]');
  9798. $this->_out('/BitsPerFlag 8');
  9799. $this->_out('/Length '.strlen($grad['stream']));
  9800. $this->_out('>>');
  9801. $this->_putstream($grad['stream']);
  9802. }
  9803. $this->_out('endobj');
  9804. $this->gradients[$id]['id'] = $this->n;
  9805. }
  9806. }
  9807. /**
  9808. * Output an arc
  9809. * @author Maxime Delorme, Nicola Asuni
  9810. * @since 3.1.000 (2008-06-09)
  9811. * @access protected
  9812. */
  9813. protected function _outarc($x1, $y1, $x2, $y2, $x3, $y3 ) {
  9814. $h = $this->h;
  9815. $this->_out(sprintf('%.2F %.2F %.2F %.2F %.2F %.2F c', $x1*$this->k, ($h-$y1)*$this->k, $x2*$this->k, ($h-$y2)*$this->k, $x3*$this->k, ($h-$y3)*$this->k));
  9816. }
  9817. /**
  9818. * Draw the sector of a circle.
  9819. * It can be used for instance to render pie charts.
  9820. * @param float $xc abscissa of the center.
  9821. * @param float $yc ordinate of the center.
  9822. * @param float $r radius.
  9823. * @param float $a start angle (in degrees).
  9824. * @param float $b end angle (in degrees).
  9825. * @param string $style: D, F, FD or DF (draw, fill, fill and draw). Default: FD.
  9826. * @param float $cw: indicates whether to go clockwise (default: true).
  9827. * @param float $o: origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
  9828. * @author Maxime Delorme, Nicola Asuni
  9829. * @since 3.1.000 (2008-06-09)
  9830. * @access public
  9831. */
  9832. public function PieSector($xc, $yc, $r, $a, $b, $style='FD', $cw=true, $o=90) {
  9833. if ($this->rtl) {
  9834. $xc = $this->w - $xc;
  9835. }
  9836. if ($cw) {
  9837. $d = $b;
  9838. $b = $o - $a;
  9839. $a = $o - $d;
  9840. } else {
  9841. $b += $o;
  9842. $a += $o;
  9843. }
  9844. $a = ($a % 360) + 360;
  9845. $b = ($b % 360) + 360;
  9846. if ($a > $b) {
  9847. $b +=360;
  9848. }
  9849. $b = $b / 360 * 2 * M_PI;
  9850. $a = $a / 360 * 2 * M_PI;
  9851. $d = $b - $a;
  9852. if ($d == 0 ) {
  9853. $d = 2 * M_PI;
  9854. }
  9855. $k = $this->k;
  9856. $hp = $this->h;
  9857. if ($style=='F') {
  9858. $op = 'f';
  9859. } elseif ($style=='FD' or $style=='DF') {
  9860. $op = 'b';
  9861. } else {
  9862. $op = 's';
  9863. }
  9864. if (sin($d/2)) {
  9865. $MyArc = 4/3 * (1 - cos($d/2)) / sin($d/2) * $r;
  9866. }
  9867. //first put the center
  9868. $this->_out(sprintf('%.2F %.2F m', ($xc)*$k, ($hp-$yc)*$k));
  9869. //put the first point
  9870. $this->_out(sprintf('%.2F %.2F l', ($xc+$r*cos($a))*$k, (($hp-($yc-$r*sin($a)))*$k)));
  9871. //draw the arc
  9872. if ($d < (M_PI/2)) {
  9873. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9874. } else {
  9875. $b = $a + $d/4;
  9876. $MyArc = 4/3*(1-cos($d/8))/sin($d/8)*$r;
  9877. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9878. $a = $b;
  9879. $b = $a + $d/4;
  9880. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9881. $a = $b;
  9882. $b = $a + $d/4;
  9883. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b) );
  9884. $a = $b;
  9885. $b = $a + $d/4;
  9886. $this->_outarc($xc+$r*cos($a)+$MyArc*cos(M_PI/2+$a), $yc-$r*sin($a)-$MyArc*sin(M_PI/2+$a), $xc+$r*cos($b)+$MyArc*cos($b-M_PI/2), $yc-$r*sin($b)-$MyArc*sin($b-M_PI/2), $xc+$r*cos($b), $yc-$r*sin($b));
  9887. }
  9888. //terminate drawing
  9889. $this->_out($op);
  9890. }
  9891. /**
  9892. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  9893. * Only vector drawing is supported, not text or bitmap.
  9894. * Although the script was successfully tested with various AI format versions, best results are probably achieved with files that were exported in the AI3 format (tested with Illustrator CS2, Freehand MX and Photoshop CS2).
  9895. * @param string $file Name of the file containing the image.
  9896. * @param float $x Abscissa of the upper-left corner.
  9897. * @param float $y Ordinate of the upper-left corner.
  9898. * @param float $w Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  9899. * @param float $h Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  9900. * @param mixed $link URL or identifier returned by AddLink().
  9901. * @param boolean useBoundingBox specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  9902. * @param string $align Indicates the alignment of the pointer next to image insertion relative to image height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  9903. * @param string $palign Allows to center or align the image on the current line. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  9904. * @param mixed $border Indicates if borders must be drawn around the image. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  9905. * @author Valentin Schmidt, Nicola Asuni
  9906. * @since 3.1.000 (2008-06-09)
  9907. * @access public
  9908. */
  9909. public function ImageEps($file, $x='', $y='', $w=0, $h=0, $link='', $useBoundingBox=true, $align='', $palign='', $border=0) {
  9910. if ($x === '') {
  9911. $x = $this->x;
  9912. }
  9913. if ($y === '') {
  9914. $y = $this->y;
  9915. }
  9916. $k = $this->k;
  9917. $data = file_get_contents($file);
  9918. if ($data === false) {
  9919. $this->Error('EPS file not found: '.$file);
  9920. }
  9921. $regs = array();
  9922. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  9923. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  9924. if (count($regs) > 1) {
  9925. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  9926. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  9927. $versexp = explode(' ', $version_str);
  9928. $version = (float)array_pop($versexp);
  9929. if ($version >= 9) {
  9930. $this->Error('This version of Adobe Illustrator file is not supported: '.$file);
  9931. }
  9932. }
  9933. }
  9934. // strip binary bytes in front of PS-header
  9935. $start = strpos($data, '%!PS-Adobe');
  9936. if ($start > 0) {
  9937. $data = substr($data, $start);
  9938. }
  9939. // find BoundingBox params
  9940. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  9941. if (count($regs) > 1) {
  9942. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  9943. } else {
  9944. $this->Error('No BoundingBox found in EPS file: '.$file);
  9945. }
  9946. $start = strpos($data, '%%EndSetup');
  9947. if ($start === false) {
  9948. $start = strpos($data, '%%EndProlog');
  9949. }
  9950. if ($start === false) {
  9951. $start = strpos($data, '%%BoundingBox');
  9952. }
  9953. $data = substr($data, $start);
  9954. $end = strpos($data, '%%PageTrailer');
  9955. if ($end===false) {
  9956. $end = strpos($data, 'showpage');
  9957. }
  9958. if ($end) {
  9959. $data = substr($data, 0, $end);
  9960. }
  9961. if ($w > 0) {
  9962. $scale_x = $w / (($x2 - $x1) / $k);
  9963. if ($h > 0) {
  9964. $scale_y = $h / (($y2 - $y1) / $k);
  9965. } else {
  9966. $scale_y = $scale_x;
  9967. $h = ($y2 - $y1) / $k * $scale_y;
  9968. }
  9969. } else {
  9970. if ($h > 0) {
  9971. $scale_y = $h / (($y2 - $y1) / $k);
  9972. $scale_x = $scale_y;
  9973. $w = ($x2-$x1) / $k * $scale_x;
  9974. } else {
  9975. $w = ($x2 - $x1) / $k;
  9976. $h = ($y2 - $y1) / $k;
  9977. }
  9978. }
  9979. // Check whether we need a new page first as this does not fit
  9980. if ($this->checkPageBreak($h, $y)) {
  9981. $y = $this->GetY() + $this->cMargin;
  9982. }
  9983. // set bottomcoordinates
  9984. $this->img_rb_y = $y + $h;
  9985. // set alignment
  9986. if ($this->rtl) {
  9987. if ($palign == 'L') {
  9988. $ximg = $this->lMargin;
  9989. // set right side coordinate
  9990. $this->img_rb_x = $ximg + $w;
  9991. } elseif ($palign == 'C') {
  9992. $ximg = ($this->w - $x - $w) / 2;
  9993. // set right side coordinate
  9994. $this->img_rb_x = $ximg + $w;
  9995. } else {
  9996. $ximg = $this->w - $x - $w;
  9997. // set left side coordinate
  9998. $this->img_rb_x = $ximg;
  9999. }
  10000. } else {
  10001. if ($palign == 'R') {
  10002. $ximg = $this->w - $this->rMargin - $w;
  10003. // set left side coordinate
  10004. $this->img_rb_x = $ximg;
  10005. } elseif ($palign == 'C') {
  10006. $ximg = ($this->w - $x - $w) / 2;
  10007. // set right side coordinate
  10008. $this->img_rb_x = $ximg + $w;
  10009. } else {
  10010. $ximg = $x;
  10011. // set right side coordinate
  10012. $this->img_rb_x = $ximg + $w;
  10013. }
  10014. }
  10015. if ($useBoundingBox) {
  10016. $dx = $ximg * $k - $x1;
  10017. $dy = $y * $k - $y1;
  10018. } else {
  10019. $dx = $ximg * $k;
  10020. $dy = $y * $k;
  10021. }
  10022. // save the current graphic state
  10023. $this->_out('q'.$this->epsmarker);
  10024. // translate
  10025. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  10026. // scale
  10027. if (isset($scale_x)) {
  10028. $this->_out(sprintf('%.3F %.3F %.3F %.3F %.3F %.3F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  10029. }
  10030. // handle pc/unix/mac line endings
  10031. preg_match('/[\r\n]+/s', $data, $regs);
  10032. $lines = explode($regs[0], $data);
  10033. $u=0;
  10034. $cnt = count($lines);
  10035. for ($i=0; $i < $cnt; ++$i) {
  10036. $line = $lines[$i];
  10037. if (($line == '') OR ($line{0} == '%')) {
  10038. continue;
  10039. }
  10040. $len = strlen($line);
  10041. $chunks = explode(' ', $line);
  10042. $cmd = array_pop($chunks);
  10043. // RGB
  10044. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  10045. $b = array_pop($chunks);
  10046. $g = array_pop($chunks);
  10047. $r = array_pop($chunks);
  10048. $this->_out(''.$r.' '.$g.' '.$b.' '.($cmd=='Xa'?'rg':'RG')); //substr($line, 0, -2).'rg' -> in EPS (AI8): c m y k r g b rg!
  10049. continue;
  10050. }
  10051. switch ($cmd) {
  10052. case 'm':
  10053. case 'l':
  10054. case 'v':
  10055. case 'y':
  10056. case 'c':
  10057. case 'k':
  10058. case 'K':
  10059. case 'g':
  10060. case 'G':
  10061. case 's':
  10062. case 'S':
  10063. case 'J':
  10064. case 'j':
  10065. case 'w':
  10066. case 'M':
  10067. case 'd':
  10068. case 'n':
  10069. case 'v': {
  10070. $this->_out($line);
  10071. break;
  10072. }
  10073. case 'x': {// custom fill color
  10074. list($c,$m,$y,$k) = $chunks;
  10075. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' k');
  10076. break;
  10077. }
  10078. case 'X': { // custom stroke color
  10079. list($c,$m,$y,$k) = $chunks;
  10080. $this->_out(''.$c.' '.$m.' '.$y.' '.$k.' K');
  10081. break;
  10082. }
  10083. case 'Y':
  10084. case 'N':
  10085. case 'V':
  10086. case 'L':
  10087. case 'C': {
  10088. $line{$len-1} = strtolower($cmd);
  10089. $this->_out($line);
  10090. break;
  10091. }
  10092. case 'b':
  10093. case 'B': {
  10094. $this->_out($cmd . '*');
  10095. break;
  10096. }
  10097. case 'f':
  10098. case 'F': {
  10099. if ($u > 0) {
  10100. $isU = false;
  10101. $max = min($i+5, $cnt);
  10102. for ($j=$i+1; $j < $max; ++$j)
  10103. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  10104. if ($isU) {
  10105. $this->_out('f*');
  10106. }
  10107. } else {
  10108. $this->_out('f*');
  10109. }
  10110. break;
  10111. }
  10112. case '*u': {
  10113. ++$u;
  10114. break;
  10115. }
  10116. case '*U': {
  10117. --$u;
  10118. break;
  10119. }
  10120. }
  10121. }
  10122. // restore previous graphic state
  10123. $this->_out($this->epsmarker.'Q');
  10124. if (!empty($border)) {
  10125. $bx = $x;
  10126. $by = $y;
  10127. $this->x = $x;
  10128. $this->y = $y;
  10129. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0);
  10130. $this->x = $bx;
  10131. $this->y = $by;
  10132. }
  10133. if ($link) {
  10134. $this->Link($ximg, $y, $w, $h, $link, 0);
  10135. }
  10136. // set pointer to align the successive text/objects
  10137. switch($align) {
  10138. case 'T':{
  10139. $this->y = $y;
  10140. $this->x = $this->img_rb_x;
  10141. break;
  10142. }
  10143. case 'M':{
  10144. $this->y = $y + round($h/2);
  10145. $this->x = $this->img_rb_x;
  10146. break;
  10147. }
  10148. case 'B':{
  10149. $this->y = $this->img_rb_y;
  10150. $this->x = $this->img_rb_x;
  10151. break;
  10152. }
  10153. case 'N':{
  10154. $this->SetY($this->img_rb_y);
  10155. break;
  10156. }
  10157. default:{
  10158. break;
  10159. }
  10160. }
  10161. $this->endlinex = $this->img_rb_x;
  10162. }
  10163. /**
  10164. * Set document barcode.
  10165. * @param string $bc barcode
  10166. * @access public
  10167. */
  10168. public function setBarcode($bc='') {
  10169. $this->barcode = $bc;
  10170. }
  10171. /**
  10172. * Get current barcode.
  10173. * @return string
  10174. * @access public
  10175. * @since 4.0.012 (2008-07-24)
  10176. */
  10177. public function getBarcode() {
  10178. return $this->barcode;
  10179. }
  10180. /**
  10181. * Print a Linear Barcode.
  10182. * @param string $code code to print
  10183. * @param string $type type of barcode.
  10184. * @param int $x x position in user units
  10185. * @param int $y y position in user units
  10186. * @param int $w width in user units
  10187. * @param int $h height in user units
  10188. * @param float $xres width of the smallest bar in user units
  10189. * @param array $style array of options:<ul><li>string $style['position'] barcode position inside the specified width: L = left (default for LTR); C = center; R = right (default for RTL); S = stretch</li><li>boolean $style['border'] if true prints a border around the barcode</li><li>int $style['padding'] padding to leave around the barcode in user units</li><li>array $style['fgcolor'] color array for bars and text</li><li>mixed $style['bgcolor'] color array for background or false for transparent</li><li>boolean $style["text"] boolean if true prints text below the barcode</li><li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li><li>int $style['stretchtext']: 0 = disabled; 1 = horizontal scaling only if necessary; 2 = forced horizontal scaling; 3 = character spacing only if necessary; 4 = forced character spacing</li></ul>
  10190. * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  10191. * @author Nicola Asuni
  10192. * @since 3.1.000 (2008-06-09)
  10193. * @access public
  10194. */
  10195. public function write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='') {
  10196. if ($this->empty_string($code)) {
  10197. return;
  10198. }
  10199. require_once(dirname(__FILE__).'/barcodes.php');
  10200. // save current graphic settings
  10201. $gvars = $this->getGraphicVars();
  10202. // create new barcode object
  10203. $barcodeobj = new TCPDFBarcode($code, $type);
  10204. $arrcode = $barcodeobj->getBarcodeArray();
  10205. if ($arrcode === false) {
  10206. $this->Error('Error in 1D barcode string');
  10207. }
  10208. // set default values
  10209. if (!isset($style['position'])) {
  10210. if ($this->rtl) {
  10211. $style['position'] = 'R';
  10212. } else {
  10213. $style['position'] = 'L';
  10214. }
  10215. }
  10216. if (!isset($style['padding'])) {
  10217. $style['padding'] = 0;
  10218. }
  10219. if (!isset($style['fgcolor'])) {
  10220. $style['fgcolor'] = array(0,0,0); // default black
  10221. }
  10222. if (!isset($style['bgcolor'])) {
  10223. $style['bgcolor'] = false; // default transparent
  10224. }
  10225. if (!isset($style['border'])) {
  10226. $style['border'] = false;
  10227. }
  10228. $fontsize = 0;
  10229. if (!isset($style['text'])) {
  10230. $style['text'] = false;
  10231. }
  10232. if ($style['text'] AND isset($style['font'])) {
  10233. if (isset($style['fontsize'])) {
  10234. $fontsize = $style['fontsize'];
  10235. }
  10236. $this->SetFont($style['font'], '', $fontsize);
  10237. }
  10238. if (!isset($style['stretchtext'])) {
  10239. $style['stretchtext'] = 4;
  10240. }
  10241. // set foreground color
  10242. $this->SetDrawColorArray($style['fgcolor']);
  10243. $this->SetTextColorArray($style['fgcolor']);
  10244. if ($this->empty_string($w) OR ($w <= 0)) {
  10245. if ($this->rtl) {
  10246. $w = $this->x - $this->lMargin;
  10247. } else {
  10248. $w = $this->w - $this->rMargin - $this->x;
  10249. }
  10250. }
  10251. if ($this->empty_string($x)) {
  10252. $x = $this->GetX();
  10253. }
  10254. if ($this->rtl) {
  10255. $x = $this->w - $x;
  10256. }
  10257. if ($this->empty_string($y)) {
  10258. $y = $this->GetY();
  10259. }
  10260. if ($this->empty_string($xres)) {
  10261. $xres = 0.4;
  10262. }
  10263. $fbw = ($arrcode['maxw'] * $xres) + (2 * $style['padding']);
  10264. $extraspace = ($this->cell_height_ratio * $fontsize / $this->k) + (2 * $style['padding']);
  10265. if ($this->empty_string($h) OR ($h <= 0)) {
  10266. $h = 10 + $extraspace;
  10267. }
  10268. if ($this->checkPageBreak($h)) {
  10269. $y = $this->y;
  10270. }
  10271. // maximum bar heigth
  10272. $barh = $h - $extraspace;
  10273. switch ($style['position']) {
  10274. case 'L': { // left
  10275. if ($this->rtl) {
  10276. $xpos = $x - $w;
  10277. } else {
  10278. $xpos = $x;
  10279. }
  10280. break;
  10281. }
  10282. case 'C': { // center
  10283. $xdiff = (($w - $fbw) / 2);
  10284. if ($this->rtl) {
  10285. $xpos = $x - $w + $xdiff;
  10286. } else {
  10287. $xpos = $x + $xdiff;
  10288. }
  10289. break;
  10290. }
  10291. case 'R': { // right
  10292. if ($this->rtl) {
  10293. $xpos = $x - $fbw;
  10294. } else {
  10295. $xpos = $x + $w - $fbw;
  10296. }
  10297. break;
  10298. }
  10299. case 'S': { // stretch
  10300. $fbw = $w;
  10301. $xres = ($w - (2 * $style['padding'])) / $arrcode['maxw'];
  10302. if ($this->rtl) {
  10303. $xpos = $x - $w;
  10304. } else {
  10305. $xpos = $x;
  10306. }
  10307. break;
  10308. }
  10309. }
  10310. $xpos_rect = $xpos;
  10311. $xpos = $xpos_rect + $style['padding'];
  10312. $xpos_text = $xpos;
  10313. // barcode is always printed in LTR direction
  10314. $tempRTL = $this->rtl;
  10315. $this->rtl = false;
  10316. // print background color
  10317. if ($style['bgcolor']) {
  10318. $this->Rect($xpos_rect, $y, $fbw, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  10319. } elseif ($style['border']) {
  10320. $this->Rect($xpos_rect, $y, $fbw, $h, 'D');
  10321. }
  10322. // print bars
  10323. if ($arrcode !== false) {
  10324. foreach ($arrcode['bcode'] as $k => $v) {
  10325. $bw = ($v['w'] * $xres);
  10326. if ($v['t']) {
  10327. // draw a vertical bar
  10328. $ypos = $y + $style['padding'] + ($v['p'] * $barh / $arrcode['maxh']);
  10329. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  10330. }
  10331. $xpos += $bw;
  10332. }
  10333. }
  10334. // print text
  10335. if ($style['text']) {
  10336. // print text
  10337. $this->x = $xpos_text;
  10338. $this->y = $y + $style['padding'] + $barh;
  10339. $this->Cell(($arrcode['maxw'] * $xres), ($this->cell_height_ratio * $fontsize / $this->k), $code, 0, 0, 'C', 0, '', $style['stretchtext']);
  10340. }
  10341. // restore original direction
  10342. $this->rtl = $tempRTL;
  10343. // restore previous settings
  10344. $this->setGraphicVars($gvars);
  10345. // set bottomcoordinates
  10346. $this->img_rb_y = $y + $h;
  10347. if ($this->rtl) {
  10348. // set left side coordinate
  10349. $this->img_rb_x = ($this->w - $x - $w);
  10350. } else {
  10351. // set right side coordinate
  10352. $this->img_rb_x = $x + $w;
  10353. }
  10354. // set pointer to align the successive text/objects
  10355. switch($align) {
  10356. case 'T':{
  10357. $this->y = $y;
  10358. $this->x = $this->img_rb_x;
  10359. break;
  10360. }
  10361. case 'M':{
  10362. $this->y = $y + round($h/2);
  10363. $this->x = $this->img_rb_x;
  10364. break;
  10365. }
  10366. case 'B':{
  10367. $this->y = $this->img_rb_y;
  10368. $this->x = $this->img_rb_x;
  10369. break;
  10370. }
  10371. case 'N':{
  10372. $this->SetY($this->img_rb_y);
  10373. break;
  10374. }
  10375. default:{
  10376. break;
  10377. }
  10378. }
  10379. }
  10380. /**
  10381. * This function is DEPRECATED, please use the new write1DBarcode() function.
  10382. * @param int $x x position in user units
  10383. * @param int $y y position in user units
  10384. * @param int $w width in user units
  10385. * @param int $h height position in user units
  10386. * @param string $type type of barcode (I25, C128A, C128B, C128C, C39)
  10387. * @param string $style barcode style
  10388. * @param string $font font for text
  10389. * @param int $xres x resolution
  10390. * @param string $code code to print
  10391. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  10392. * @access public
  10393. * @see write1DBarcode()
  10394. */
  10395. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  10396. // convert old settings for the new write1DBarcode() function.
  10397. $xres = 1 / $xres;
  10398. $newstyle = array(
  10399. 'position' => 'L',
  10400. 'border' => false,
  10401. 'padding' => 0,
  10402. 'fgcolor' => array(0,0,0),
  10403. 'bgcolor' => false,
  10404. 'text' => true,
  10405. 'font' => $font,
  10406. 'fontsize' => 8,
  10407. 'stretchtext' => 4
  10408. );
  10409. if ($style & 1) {
  10410. $newstyle['border'] = true;
  10411. }
  10412. if ($style & 2) {
  10413. $newstyle['bgcolor'] = false;
  10414. }
  10415. if ($style & 4) {
  10416. $newstyle['position'] = 'C';
  10417. } elseif ($style & 8) {
  10418. $newstyle['position'] = 'L';
  10419. } elseif ($style & 16) {
  10420. $newstyle['position'] = 'R';
  10421. }
  10422. if ($style & 128) {
  10423. $newstyle['text'] = true;
  10424. }
  10425. if ($style & 256) {
  10426. $newstyle['stretchtext'] = 4;
  10427. }
  10428. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  10429. }
  10430. /**
  10431. * Print 2D Barcode.
  10432. * @param string $code code to print
  10433. * @param string $type type of barcode.
  10434. * @param int $x x position in user units
  10435. * @param int $y y position in user units
  10436. * @param int $w width in user units
  10437. * @param int $h height in user units
  10438. * @param array $style array of options:<ul><li>boolean $style['border'] if true prints a border around the barcode</li><li>int $style['padding'] padding to leave around the barcode in user units</li><li>array $style['fgcolor'] color array for bars and text</li><li>mixed $style['bgcolor'] color array for background or false for transparent</li></ul>
  10439. * @param string $align Indicates the alignment of the pointer next to barcode insertion relative to barcode height. The value can be:<ul><li>T: top-right for LTR or top-left for RTL</li><li>M: middle-right for LTR or middle-left for RTL</li><li>B: bottom-right for LTR or bottom-left for RTL</li><li>N: next line</li></ul>
  10440. * @author Nicola Asuni
  10441. * @since 4.5.037 (2009-04-07)
  10442. * @access public
  10443. */
  10444. public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='') {
  10445. if ($this->empty_string($code)) {
  10446. return;
  10447. }
  10448. require_once(dirname(__FILE__).'/2dbarcodes.php');
  10449. // save current graphic settings
  10450. $gvars = $this->getGraphicVars();
  10451. // create new barcode object
  10452. $barcodeobj = new TCPDF2DBarcode($code, $type);
  10453. $arrcode = $barcodeobj->getBarcodeArray();
  10454. if ($arrcode === false) {
  10455. $this->Error('Error in 2D barcode string');
  10456. }
  10457. // set default values
  10458. if (!isset($style['padding'])) {
  10459. $style['padding'] = 0;
  10460. }
  10461. if (!isset($style['fgcolor'])) {
  10462. $style['fgcolor'] = array(0,0,0); // default black
  10463. }
  10464. if (!isset($style['bgcolor'])) {
  10465. $style['bgcolor'] = false; // default transparent
  10466. }
  10467. if (!isset($style['border'])) {
  10468. $style['border'] = false;
  10469. }
  10470. // set foreground color
  10471. $this->SetDrawColorArray($style['fgcolor']);
  10472. if ($this->empty_string($x)) {
  10473. $x = $this->GetX();
  10474. }
  10475. if ($this->rtl) {
  10476. $x = $this->w - $x;
  10477. }
  10478. if ($this->empty_string($y)) {
  10479. $y = $this->GetY();
  10480. }
  10481. if ($this->empty_string($w) OR ($w <= 0)) {
  10482. if ($this->rtl) {
  10483. $w = $x - $this->lMargin;
  10484. } else {
  10485. $w = $this->w - $this->rMargin - $x;
  10486. }
  10487. }
  10488. if ($this->empty_string($h) OR ($h <= 0)) {
  10489. // 2d barcodes are square by default
  10490. $h = $w;
  10491. }
  10492. if ($this->checkPageBreak($h)) {
  10493. $y = $this->y;
  10494. }
  10495. // calculate barcode size (excluding padding)
  10496. $bw = $w - (2 * $style['padding']);
  10497. $bh = $h - (2 * $style['padding']);
  10498. // calculate starting coordinates
  10499. if ($this->rtl) {
  10500. $xpos = $x - $w;
  10501. } else {
  10502. $xpos = $x;
  10503. }
  10504. $xpos += $style['padding'];
  10505. $ypos = $y + $style['padding'];
  10506. // barcode is always printed in LTR direction
  10507. $tempRTL = $this->rtl;
  10508. $this->rtl = false;
  10509. // print background color
  10510. if ($style['bgcolor']) {
  10511. $this->Rect($x, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  10512. } elseif ($style['border']) {
  10513. $this->Rect($x, $y, $w, $h, 'D');
  10514. }
  10515. // print barcode cells
  10516. if ($arrcode !== false) {
  10517. $rows = $arrcode['num_rows'];
  10518. $cols = $arrcode['num_cols'];
  10519. // calculate dimension of single barcode cell
  10520. $cw = $bw / $cols;
  10521. $ch = $bh / $rows;
  10522. // for each row
  10523. for ($r = 0; $r < $rows; ++$r) {
  10524. $xr = $xpos;
  10525. // for each column
  10526. for ($c = 0; $c < $cols; ++$c) {
  10527. if ($arrcode['bcode'][$r][$c] == 1) {
  10528. // draw a single barcode cell
  10529. $this->Rect($xr, $ypos, $cw, $ch, 'F', array(), $style['fgcolor']);
  10530. }
  10531. $xr += $cw;
  10532. }
  10533. $ypos += $ch;
  10534. }
  10535. }
  10536. // restore original direction
  10537. $this->rtl = $tempRTL;
  10538. // restore previous settings
  10539. $this->setGraphicVars($gvars);
  10540. // set bottomcoordinates
  10541. $this->img_rb_y = $y + $h;
  10542. if ($this->rtl) {
  10543. // set left side coordinate
  10544. $this->img_rb_x = ($this->w - $x - $w);
  10545. } else {
  10546. // set right side coordinate
  10547. $this->img_rb_x = $x + $w;
  10548. }
  10549. // set pointer to align the successive text/objects
  10550. switch($align) {
  10551. case 'T':{
  10552. $this->y = $y;
  10553. $this->x = $this->img_rb_x;
  10554. break;
  10555. }
  10556. case 'M':{
  10557. $this->y = $y + round($h/2);
  10558. $this->x = $this->img_rb_x;
  10559. break;
  10560. }
  10561. case 'B':{
  10562. $this->y = $this->img_rb_y;
  10563. $this->x = $this->img_rb_x;
  10564. break;
  10565. }
  10566. case 'N':{
  10567. $this->SetY($this->img_rb_y);
  10568. break;
  10569. }
  10570. default:{
  10571. break;
  10572. }
  10573. }
  10574. }
  10575. /**
  10576. * Returns an array containing current margins:
  10577. * <ul>
  10578. <li>$ret['left'] = left margin</li>
  10579. <li>$ret['right'] = right margin</li>
  10580. <li>$ret['top'] = top margin</li>
  10581. <li>$ret['bottom'] = bottom margin</li>
  10582. <li>$ret['header'] = header margin</li>
  10583. <li>$ret['footer'] = footer margin</li>
  10584. <li>$ret['cell'] = cell margin</li>
  10585. * </ul>
  10586. * @return array containing all margins measures
  10587. * @access public
  10588. * @since 3.2.000 (2008-06-23)
  10589. */
  10590. public function getMargins() {
  10591. $ret = array(
  10592. 'left' => $this->lMargin,
  10593. 'right' => $this->rMargin,
  10594. 'top' => $this->tMargin,
  10595. 'bottom' => $this->bMargin,
  10596. 'header' => $this->header_margin,
  10597. 'footer' => $this->footer_margin,
  10598. 'cell' => $this->cMargin,
  10599. );
  10600. return $ret;
  10601. }
  10602. /**
  10603. * Returns an array containing original margins:
  10604. * <ul>
  10605. <li>$ret['left'] = left margin</li>
  10606. <li>$ret['right'] = right margin</li>
  10607. * </ul>
  10608. * @return array containing all margins measures
  10609. * @access public
  10610. * @since 4.0.012 (2008-07-24)
  10611. */
  10612. public function getOriginalMargins() {
  10613. $ret = array(
  10614. 'left' => $this->original_lMargin,
  10615. 'right' => $this->original_rMargin
  10616. );
  10617. return $ret;
  10618. }
  10619. /**
  10620. * Returns the current font size.
  10621. * @return current font size
  10622. * @access public
  10623. * @since 3.2.000 (2008-06-23)
  10624. */
  10625. public function getFontSize() {
  10626. return $this->FontSize;
  10627. }
  10628. /**
  10629. * Returns the current font size in points unit.
  10630. * @return current font size in points unit
  10631. * @access public
  10632. * @since 3.2.000 (2008-06-23)
  10633. */
  10634. public function getFontSizePt() {
  10635. return $this->FontSizePt;
  10636. }
  10637. /**
  10638. * Returns the current font family name.
  10639. * @return string current font family name
  10640. * @access public
  10641. * @since 4.3.008 (2008-12-05)
  10642. */
  10643. public function getFontFamily() {
  10644. return $this->FontFamily;
  10645. }
  10646. /**
  10647. * Returns the current font style.
  10648. * @return string current font style
  10649. * @access public
  10650. * @since 4.3.008 (2008-12-05)
  10651. */
  10652. public function getFontStyle() {
  10653. return $this->FontStyle;
  10654. }
  10655. /**
  10656. * Prints a cell (rectangular area) with optional borders, background color and html text string.
  10657. * The upper-left corner of the cell corresponds to the current position. After the call, the current position moves to the right or to the next line.<br />
  10658. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  10659. * @param float $w Cell width. If 0, the cell extends up to the right margin.
  10660. * @param float $h Cell minimum height. The cell extends automatically if needed.
  10661. * @param float $x upper-left corner X coordinate
  10662. * @param float $y upper-left corner Y coordinate
  10663. * @param string $html html text to print. Default value: empty string.
  10664. * @param mixed $border Indicates if borders must be drawn around the cell. The value can be either a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul>
  10665. * @param int $ln Indicates where the current position should go after the call. Possible values are:<ul><li>0: to the right (or left for RTL language)</li><li>1: to the beginning of the next line</li><li>2: below</li></ul>
  10666. Putting 1 is equivalent to putting 0 and calling Ln() just after. Default value: 0.
  10667. * @param int $fill Indicates if the cell background must be painted (1) or transparent (0). Default value: 0.
  10668. * @param boolean $reseth if true reset the last cell height (default true).
  10669. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  10670. * @param boolean $autopadding if true, uses internal padding and automatically adjust it to account for line width.
  10671. * @access public
  10672. * @uses MultiCell()
  10673. * @see Multicell(), writeHTML()
  10674. */
  10675. public function writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true) {
  10676. return $this->MultiCell($w, $h, $html, $border, $align, $fill, $ln, $x, $y, $reseth, 0, true, $autopadding, 0);
  10677. }
  10678. /**
  10679. * Returns the HTML DOM array.
  10680. * <ul><li>$dom[$key]['tag'] = true if tag, false otherwise;</li><li>$dom[$key]['value'] = tag name or text;</li><li>$dom[$key]['opening'] = true if opening tag, false otherwise;</li><li>$dom[$key]['attribute'] = array of attributes (attribute name is the key);</li><li>$dom[$key]['style'] = array of style attributes (attribute name is the key);</li><li>$dom[$key]['parent'] = id of parent element;</li><li>$dom[$key]['fontname'] = font family name;</li><li>$dom[$key]['fontstyle'] = font style;</li><li>$dom[$key]['fontsize'] = font size in points;</li><li>$dom[$key]['bgcolor'] = RGB array of background color;</li><li>$dom[$key]['fgcolor'] = RGB array of foreground color;</li><li>$dom[$key]['width'] = width in pixels;</li><li>$dom[$key]['height'] = height in pixels;</li><li>$dom[$key]['align'] = text alignment;</li><li>$dom[$key]['cols'] = number of colums in table;</li><li>$dom[$key]['rows'] = number of rows in table;</li></ul>
  10681. * @param string $html html code
  10682. * @return array
  10683. * @access protected
  10684. * @since 3.2.000 (2008-06-20)
  10685. */
  10686. protected function getHtmlDomArray($html) {
  10687. // remove all unsupported tags (the line below lists all supported tags)
  10688. $html = strip_tags($html, '<marker/><a><b><blockquote><br><br/><dd><del><div><dl><dt><em><font><h1><h2><h3><h4><h5><h6><hr><i><img><li><ol><p><pre><small><span><strong><sub><sup><table><tablehead><tcpdf><td><th><thead><tr><tt><u><ul>');
  10689. //replace some blank characters
  10690. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  10691. $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dt|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
  10692. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  10693. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  10694. $html = strtr($html, $repTable);
  10695. $offset = 0;
  10696. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  10697. $html_a = substr($html, 0, $offset);
  10698. $html_b = substr($html, $offset, ($pos - $offset + 6));
  10699. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  10700. // preserve newlines on <pre> tag
  10701. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  10702. }
  10703. $html = $html_a.$html_b.substr($html, $pos + 6);
  10704. $offset = strlen($html_a.$html_b);
  10705. }
  10706. $html = str_replace("\n", ' ', $html);
  10707. // remove extra spaces from code
  10708. $html = preg_replace('/[\s]+<\/(table|tr|td|th|ul|ol|li)>/', '</\\1>', $html);
  10709. $html = preg_replace('/[\s]+<(tr|td|th|ul|ol|li|br)/', '<\\1', $html);
  10710. $html = preg_replace('/<\/(table|tr|td|th|blockquote|dd|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|ul|p)>[\s]+</', '</\\1><', $html);
  10711. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  10712. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  10713. $html = preg_replace('/<img/', ' <img', $html);
  10714. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span></span>', $html);
  10715. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  10716. // trim string
  10717. $html = preg_replace('/^[\s]+/', '', $html);
  10718. $html = preg_replace('/[\s]+$/', '', $html);
  10719. // pattern for generic tag
  10720. $tagpattern = '/(<[^>]+>)/';
  10721. // explodes the string
  10722. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  10723. // count elements
  10724. $maxel = count($a);
  10725. $elkey = 0;
  10726. $key = 0;
  10727. // create an array of elements
  10728. $dom = array();
  10729. $dom[$key] = array();
  10730. // set first void element
  10731. $dom[$key]['tag'] = false;
  10732. $dom[$key]['value'] = '';
  10733. $dom[$key]['parent'] = 0;
  10734. $dom[$key]['fontname'] = $this->FontFamily;
  10735. $dom[$key]['fontstyle'] = $this->FontStyle;
  10736. $dom[$key]['fontsize'] = $this->FontSizePt;
  10737. $dom[$key]['bgcolor'] = false;
  10738. $dom[$key]['fgcolor'] = $this->fgcolor;
  10739. $dom[$key]['align'] = '';
  10740. $dom[$key]['listtype'] = '';
  10741. $thead = false; // true when we are inside the THEAD tag
  10742. ++$key;
  10743. $level = array();
  10744. array_push($level, 0); // root
  10745. while ($elkey < $maxel) {
  10746. $dom[$key] = array();
  10747. $element = $a[$elkey];
  10748. $dom[$key]['elkey'] = $elkey;
  10749. if (preg_match($tagpattern, $element)) {
  10750. // html tag
  10751. $element = substr($element, 1, -1);
  10752. // get tag name
  10753. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  10754. $tagname = strtolower($tag[1]);
  10755. // check if we are inside a table header
  10756. if ($tagname == 'thead') {
  10757. if ($element{0} == '/') {
  10758. $thead = false;
  10759. } else {
  10760. $thead = true;
  10761. }
  10762. ++$elkey;
  10763. continue;
  10764. }
  10765. $dom[$key]['tag'] = true;
  10766. $dom[$key]['value'] = $tagname;
  10767. if ($element{0} == '/') {
  10768. // closing html tag
  10769. $dom[$key]['opening'] = false;
  10770. $dom[$key]['parent'] = end($level);
  10771. array_pop($level);
  10772. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  10773. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  10774. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  10775. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  10776. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  10777. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  10778. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  10779. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  10780. }
  10781. // set the number of columns in table tag
  10782. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  10783. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  10784. }
  10785. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  10786. $dom[($dom[$key]['parent'])]['content'] = '';
  10787. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  10788. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  10789. }
  10790. $key = $i;
  10791. }
  10792. // store header rows on a new table
  10793. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] == true)) {
  10794. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  10795. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  10796. }
  10797. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  10798. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  10799. }
  10800. }
  10801. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  10802. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  10803. }
  10804. } else {
  10805. // opening html tag
  10806. $dom[$key]['opening'] = true;
  10807. $dom[$key]['parent'] = end($level);
  10808. if (substr($element, -1, 1) != '/') {
  10809. // not self-closing tag
  10810. array_push($level, $key);
  10811. $dom[$key]['self'] = false;
  10812. } else {
  10813. $dom[$key]['self'] = true;
  10814. }
  10815. // copy some values from parent
  10816. $parentkey = 0;
  10817. if ($key > 0) {
  10818. $parentkey = $dom[$key]['parent'];
  10819. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  10820. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  10821. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  10822. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  10823. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  10824. $dom[$key]['align'] = $dom[$parentkey]['align'];
  10825. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  10826. }
  10827. // get attributes
  10828. preg_match_all('/([^=\s]*)=["]?([^"]*)["]?/', $element, $attr_array, PREG_PATTERN_ORDER);
  10829. $dom[$key]['attribute'] = array(); // reset attribute array
  10830. while (list($id, $name) = each($attr_array[1])) {
  10831. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  10832. }
  10833. // split style attributes
  10834. if (isset($dom[$key]['attribute']['style'])) {
  10835. // get style attributes
  10836. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  10837. $dom[$key]['style'] = array(); // reset style attribute array
  10838. while (list($id, $name) = each($style_array[1])) {
  10839. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  10840. }
  10841. // --- get some style attributes ---
  10842. if (isset($dom[$key]['style']['font-family'])) {
  10843. // font family
  10844. if (isset($dom[$key]['style']['font-family'])) {
  10845. $fontslist = preg_split('/[,]/', strtolower($dom[$key]['style']['font-family']));
  10846. foreach ($fontslist as $font) {
  10847. $font = trim(strtolower($font));
  10848. if (in_array($font, $this->fontlist) OR in_array($font, $this->fontkeys)) {
  10849. $dom[$key]['fontname'] = $font;
  10850. break;
  10851. }
  10852. }
  10853. }
  10854. }
  10855. // list-style-type
  10856. if (isset($dom[$key]['style']['list-style-type'])) {
  10857. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  10858. if ($dom[$key]['listtype'] == 'inherit') {
  10859. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  10860. }
  10861. }
  10862. // font size
  10863. if (isset($dom[$key]['style']['font-size'])) {
  10864. $fsize = trim($dom[$key]['style']['font-size']);
  10865. switch ($fsize) {
  10866. // absolute-size
  10867. case 'xx-small': {
  10868. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
  10869. break;
  10870. }
  10871. case 'x-small': {
  10872. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
  10873. break;
  10874. }
  10875. case 'small': {
  10876. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
  10877. break;
  10878. }
  10879. case 'medium': {
  10880. $dom[$key]['fontsize'] = $dom[0]['fontsize'];
  10881. break;
  10882. }
  10883. case 'large': {
  10884. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
  10885. break;
  10886. }
  10887. case 'x-large': {
  10888. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
  10889. break;
  10890. }
  10891. case 'xx-large': {
  10892. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
  10893. break;
  10894. }
  10895. // relative-size
  10896. case 'smaller': {
  10897. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
  10898. break;
  10899. }
  10900. case 'larger': {
  10901. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
  10902. break;
  10903. }
  10904. default: {
  10905. $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
  10906. }
  10907. }
  10908. }
  10909. // font style
  10910. if (isset($dom[$key]['style']['font-weight']) AND (strtolower($dom[$key]['style']['font-weight']{0}) == 'b')) {
  10911. $dom[$key]['fontstyle'] .= 'B';
  10912. }
  10913. if (isset($dom[$key]['style']['font-style']) AND (strtolower($dom[$key]['style']['font-style']{0}) == 'i')) {
  10914. $dom[$key]['fontstyle'] .= '"I';
  10915. }
  10916. // font color
  10917. if (isset($dom[$key]['style']['color']) AND (!$this->empty_string($dom[$key]['style']['color']))) {
  10918. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['color']);
  10919. }
  10920. // background color
  10921. if (isset($dom[$key]['style']['background-color']) AND (!$this->empty_string($dom[$key]['style']['background-color']))) {
  10922. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['style']['background-color']);
  10923. }
  10924. // text-decoration
  10925. if (isset($dom[$key]['style']['text-decoration'])) {
  10926. $decors = explode(' ', strtolower($dom[$key]['style']['text-decoration']));
  10927. foreach ($decors as $dec) {
  10928. $dec = trim($dec);
  10929. if (!$this->empty_string($dec)) {
  10930. if ($dec{0} == 'u') {
  10931. $dom[$key]['fontstyle'] .= 'U';
  10932. } elseif ($dec{0} == 'l') {
  10933. $dom[$key]['fontstyle'] .= 'D';
  10934. }
  10935. }
  10936. }
  10937. }
  10938. // check for width attribute
  10939. if (isset($dom[$key]['style']['width'])) {
  10940. $dom[$key]['width'] = $dom[$key]['style']['width'];
  10941. }
  10942. // check for height attribute
  10943. if (isset($dom[$key]['style']['height'])) {
  10944. $dom[$key]['height'] = $dom[$key]['style']['height'];
  10945. }
  10946. // check for text alignment
  10947. if (isset($dom[$key]['style']['text-align'])) {
  10948. $dom[$key]['align'] = strtoupper($dom[$key]['style']['text-align']{0});
  10949. }
  10950. // check for border attribute
  10951. if (isset($dom[$key]['style']['border'])) {
  10952. $dom[$key]['attribute']['border'] = $dom[$key]['style']['border'];
  10953. }
  10954. }
  10955. // check for font tag
  10956. if ($dom[$key]['value'] == 'font') {
  10957. // font family
  10958. if (isset($dom[$key]['attribute']['face'])) {
  10959. $fontslist = preg_split('/[,]/', strtolower($dom[$key]['attribute']['face']));
  10960. foreach ($fontslist as $font) {
  10961. $font = trim(strtolower($font));
  10962. if (in_array($font, $this->fontlist) OR in_array($font, $this->fontkeys)) {
  10963. $dom[$key]['fontname'] = $font;
  10964. break;
  10965. }
  10966. }
  10967. }
  10968. // font size
  10969. if (isset($dom[$key]['attribute']['size'])) {
  10970. if ($key > 0) {
  10971. if ($dom[$key]['attribute']['size']{0} == '+') {
  10972. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] + intval(substr($dom[$key]['attribute']['size'], 1));
  10973. } elseif ($dom[$key]['attribute']['size']{0} == '-') {
  10974. $dom[$key]['fontsize'] = $dom[($dom[$key]['parent'])]['fontsize'] - intval(substr($dom[$key]['attribute']['size'], 1));
  10975. } else {
  10976. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  10977. }
  10978. } else {
  10979. $dom[$key]['fontsize'] = intval($dom[$key]['attribute']['size']);
  10980. }
  10981. }
  10982. }
  10983. // force natural alignment for lists
  10984. if ((($dom[$key]['value'] == 'ul') OR ($dom[$key]['value'] == 'ol') OR ($dom[$key]['value'] == 'dl'))
  10985. AND (!isset($dom[$key]['align']) OR $this->empty_string($dom[$key]['align']) OR ($dom[$key]['align'] != 'J'))) {
  10986. if ($this->rtl) {
  10987. $dom[$key]['align'] = 'R';
  10988. } else {
  10989. $dom[$key]['align'] = 'L';
  10990. }
  10991. }
  10992. if (($dom[$key]['value'] == 'small') OR ($dom[$key]['value'] == 'sup') OR ($dom[$key]['value'] == 'sub')) {
  10993. $dom[$key]['fontsize'] = $dom[$key]['fontsize'] * K_SMALL_RATIO;
  10994. }
  10995. if (($dom[$key]['value'] == 'strong') OR ($dom[$key]['value'] == 'b')) {
  10996. $dom[$key]['fontstyle'] .= 'B';
  10997. }
  10998. if (($dom[$key]['value'] == 'em') OR ($dom[$key]['value'] == 'i')) {
  10999. $dom[$key]['fontstyle'] .= 'I';
  11000. }
  11001. if ($dom[$key]['value'] == 'u') {
  11002. $dom[$key]['fontstyle'] .= 'U';
  11003. }
  11004. if ($dom[$key]['value'] == 'del') {
  11005. $dom[$key]['fontstyle'] .= 'D';
  11006. }
  11007. if (($dom[$key]['value'] == 'pre') OR ($dom[$key]['value'] == 'tt')) {
  11008. $dom[$key]['fontname'] = $this->default_monospaced_font;
  11009. }
  11010. if (($dom[$key]['value']{0} == 'h') AND (intval($dom[$key]['value']{1}) > 0) AND (intval($dom[$key]['value']{1}) < 7)) {
  11011. $headsize = (4 - intval($dom[$key]['value']{1})) * 2;
  11012. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + $headsize;
  11013. $dom[$key]['fontstyle'] .= 'B';
  11014. }
  11015. if (($dom[$key]['value'] == 'table')) {
  11016. $dom[$key]['rows'] = 0; // number of rows
  11017. $dom[$key]['trids'] = array(); // IDs of TR elements
  11018. $dom[$key]['thead'] = ''; // table header rows
  11019. }
  11020. if (($dom[$key]['value'] == 'tr')) {
  11021. $dom[$key]['cols'] = 0;
  11022. // store the number of rows on table element
  11023. ++$dom[($dom[$key]['parent'])]['rows'];
  11024. // store the TR elements IDs on table element
  11025. array_push($dom[($dom[$key]['parent'])]['trids'], $key);
  11026. if ($thead) {
  11027. $dom[$key]['thead'] = true;
  11028. } else {
  11029. $dom[$key]['thead'] = false;
  11030. }
  11031. }
  11032. if (($dom[$key]['value'] == 'th') OR ($dom[$key]['value'] == 'td')) {
  11033. if (isset($dom[$key]['attribute']['colspan'])) {
  11034. $colspan = intval($dom[$key]['attribute']['colspan']);
  11035. } else {
  11036. $colspan = 1;
  11037. }
  11038. $dom[$key]['attribute']['colspan'] = $colspan;
  11039. $dom[($dom[$key]['parent'])]['cols'] += $colspan;
  11040. }
  11041. // set foreground color attribute
  11042. if (isset($dom[$key]['attribute']['color']) AND (!$this->empty_string($dom[$key]['attribute']['color']))) {
  11043. $dom[$key]['fgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['color']);
  11044. }
  11045. // set background color attribute
  11046. if (isset($dom[$key]['attribute']['bgcolor']) AND (!$this->empty_string($dom[$key]['attribute']['bgcolor']))) {
  11047. $dom[$key]['bgcolor'] = $this->convertHTMLColorToDec($dom[$key]['attribute']['bgcolor']);
  11048. }
  11049. // check for width attribute
  11050. if (isset($dom[$key]['attribute']['width'])) {
  11051. $dom[$key]['width'] = $dom[$key]['attribute']['width'];
  11052. }
  11053. // check for height attribute
  11054. if (isset($dom[$key]['attribute']['height'])) {
  11055. $dom[$key]['height'] = $dom[$key]['attribute']['height'];
  11056. }
  11057. // check for text alignment
  11058. if (isset($dom[$key]['attribute']['align']) AND (!$this->empty_string($dom[$key]['attribute']['align'])) AND ($dom[$key]['value'] !== 'img')) {
  11059. $dom[$key]['align'] = strtoupper($dom[$key]['attribute']['align']{0});
  11060. }
  11061. } // end opening tag
  11062. } else {
  11063. // text
  11064. $dom[$key]['tag'] = false;
  11065. $dom[$key]['value'] = stripslashes($this->unhtmlentities($element));
  11066. $dom[$key]['parent'] = end($level);
  11067. }
  11068. ++$elkey;
  11069. ++$key;
  11070. }
  11071. return $dom;
  11072. }
  11073. /**
  11074. * Allows to preserve some HTML formatting (limited support).<br />
  11075. * IMPORTANT: The HTML must be well formatted - try to clean-up it using an application like HTML-Tidy before submitting.
  11076. * Supported tags are: a, b, blockquote, br, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, img, li, ol, p, pre, small, span, strong, sub, sup, table, tcpdf, td, th, thead, tr, tt, u, ul
  11077. * @param string $html text to display
  11078. * @param boolean $ln if true add a new line after text (default = true)
  11079. * @param int $fill Indicates if the background must be painted (true) or transparent (false).
  11080. * @param boolean $reseth if true reset the last cell height (default false).
  11081. * @param boolean $cell if true add the default cMargin space to each Write (default false).
  11082. * @param string $align Allows to center or align the text. Possible values are:<ul><li>L : left align</li><li>C : center</li><li>R : right align</li><li>'' : empty string : left for LTR or right for RTL</li></ul>
  11083. * @access public
  11084. */
  11085. public function writeHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
  11086. $gvars = $this->getGraphicVars();
  11087. // store current values
  11088. $prevPage = $this->page;
  11089. $prevlMargin = $this->lMargin;
  11090. $prevrMargin = $this->rMargin;
  11091. $curfontname = $this->FontFamily;
  11092. $curfontstyle = $this->FontStyle;
  11093. $curfontsize = $this->FontSizePt;
  11094. $this->newline = true;
  11095. $minstartliney = $this->y;
  11096. $yshift = 0;
  11097. $startlinepage = $this->page;
  11098. $newline = true;
  11099. $loop = 0;
  11100. $curpos = 0;
  11101. $blocktags = array('blockquote','br','dd','div','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','ul','tcpdf');
  11102. $this->premode = false;
  11103. if (isset($this->PageAnnots[$this->page])) {
  11104. $pask = count($this->PageAnnots[$this->page]);
  11105. } else {
  11106. $pask = 0;
  11107. }
  11108. if (isset($this->footerlen[$this->page])) {
  11109. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11110. } else {
  11111. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11112. }
  11113. $startlinepos = $this->footerpos[$this->page];
  11114. $lalign = $align;
  11115. $plalign = $align;
  11116. if ($this->rtl) {
  11117. $w = $this->x - $this->lMargin;
  11118. } else {
  11119. $w = $this->w - $this->rMargin - $this->x;
  11120. }
  11121. $w -= (2 * $this->cMargin);
  11122. if ($cell) {
  11123. if ($this->rtl) {
  11124. $this->x -= $this->cMargin;
  11125. } else {
  11126. $this->x += $this->cMargin;
  11127. }
  11128. }
  11129. if ($this->customlistindent >= 0) {
  11130. $this->listindent = $this->customlistindent;
  11131. } else {
  11132. $this->listindent = $this->GetStringWidth('0000');
  11133. }
  11134. $this->listnum = 0;
  11135. if (($this->empty_string($this->lasth)) OR ($reseth)) {
  11136. //set row height
  11137. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  11138. }
  11139. $dom = $this->getHtmlDomArray($html);
  11140. $maxel = count($dom);
  11141. $key = 0;
  11142. while ($key < $maxel) {
  11143. if ($dom[$key]['tag'] OR ($key == 0)) {
  11144. if ((($dom[$key]['value'] == 'table') OR ($dom[$key]['value'] == 'tr')) AND (isset($dom[$key]['align']))) {
  11145. $dom[$key]['align'] = ($this->rtl) ? 'R' : 'L';
  11146. }
  11147. // vertically align image in line
  11148. if ((!$this->newline)
  11149. AND ($dom[$key]['value'] == 'img')
  11150. AND (isset($dom[$key]['attribute']['height']))
  11151. AND ($dom[$key]['attribute']['height'] > 0)) {
  11152. // get image height
  11153. $imgh = $this->getHTMLUnitToUnits($dom[$key]['attribute']['height'], $this->lasth, 'px');
  11154. if (!$this->InFooter) {
  11155. // check for page break
  11156. $this->checkPageBreak($imgh);
  11157. }
  11158. if ($this->page > $startlinepage) {
  11159. // fix line splitted over two pages
  11160. if (isset($this->footerlen[$startlinepage])) {
  11161. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11162. }
  11163. // line to be moved one page forward
  11164. $pagebuff = $this->getPageBuffer($startlinepage);
  11165. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  11166. $tstart = substr($pagebuff, 0, $startlinepos);
  11167. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  11168. // remove line from previous page
  11169. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  11170. $pagebuff = $this->getPageBuffer($this->page);
  11171. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  11172. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  11173. // add line start to current page
  11174. $yshift = $minstartliney - $this->y;
  11175. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  11176. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  11177. // shift the annotations and links
  11178. if (isset($this->PageAnnots[$this->page])) {
  11179. $next_pask = count($this->PageAnnots[$this->page]);
  11180. } else {
  11181. $next_pask = 0;
  11182. }
  11183. if (isset($this->PageAnnots[$startlinepage])) {
  11184. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  11185. if ($pak >= $pask) {
  11186. $this->PageAnnots[$this->page][] = $pac;
  11187. unset($this->PageAnnots[$startlinepage][$pak]);
  11188. $npak = count($this->PageAnnots[$this->page]) - 1;
  11189. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  11190. }
  11191. }
  11192. }
  11193. $pask = $next_pask;
  11194. $startlinepos = $this->cntmrk[$this->page];
  11195. $startlinepage = $this->page;
  11196. $startliney = $this->y;
  11197. }
  11198. $this->y += (($curfontsize / $this->k) - $imgh);
  11199. $minstartliney = min($this->y, $minstartliney);
  11200. } elseif (isset($dom[$key]['fontname']) OR isset($dom[$key]['fontstyle']) OR isset($dom[$key]['fontsize'])) {
  11201. // account for different font size
  11202. $pfontname = $curfontname;
  11203. $pfontstyle = $curfontstyle;
  11204. $pfontsize = $curfontsize;
  11205. $fontname = isset($dom[$key]['fontname']) ? $dom[$key]['fontname'] : $curfontname;
  11206. $fontstyle = isset($dom[$key]['fontstyle']) ? $dom[$key]['fontstyle'] : $curfontstyle;
  11207. $fontsize = isset($dom[$key]['fontsize']) ? $dom[$key]['fontsize'] : $curfontsize;
  11208. if (($fontname != $curfontname) OR ($fontstyle != $curfontstyle) OR ($fontsize != $curfontsize)) {
  11209. $this->SetFont($fontname, $fontstyle, $fontsize);
  11210. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  11211. if (is_numeric($fontsize) AND ($fontsize > 0)
  11212. AND is_numeric($curfontsize) AND ($curfontsize > 0)
  11213. AND ($fontsize != $curfontsize) AND (!$this->newline)
  11214. AND ($key < ($maxel - 1))
  11215. ) {
  11216. if ((!$this->newline) AND ($this->page > $startlinepage)) {
  11217. // fix lines splitted over two pages
  11218. if (isset($this->footerlen[$startlinepage])) {
  11219. $curpos = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11220. }
  11221. // line to be moved one page forward
  11222. $pagebuff = $this->getPageBuffer($startlinepage);
  11223. $linebeg = substr($pagebuff, $startlinepos, ($curpos - $startlinepos));
  11224. $tstart = substr($pagebuff, 0, $startlinepos);
  11225. $tend = substr($this->getPageBuffer($startlinepage), $curpos);
  11226. // remove line start from previous page
  11227. $this->setPageBuffer($startlinepage, $tstart.''.$tend);
  11228. $pagebuff = $this->getPageBuffer($this->page);
  11229. $tstart = substr($pagebuff, 0, $this->cntmrk[$this->page]);
  11230. $tend = substr($pagebuff, $this->cntmrk[$this->page]);
  11231. // add line start to current page
  11232. $yshift = $minstartliney - $this->y;
  11233. $try = sprintf('1 0 0 1 0 %.3F cm', ($yshift * $this->k));
  11234. $this->setPageBuffer($this->page, $tstart."\nq\n".$try."\n".$linebeg."\nQ\n".$tend);
  11235. // shift the annotations and links
  11236. if (isset($this->PageAnnots[$this->page])) {
  11237. $next_pask = count($this->PageAnnots[$this->page]);
  11238. } else {
  11239. $next_pask = 0;
  11240. }
  11241. if (isset($this->PageAnnots[$startlinepage])) {
  11242. foreach ($this->PageAnnots[$startlinepage] as $pak => $pac) {
  11243. if ($pak >= $pask) {
  11244. $this->PageAnnots[$this->page][] = $pac;
  11245. unset($this->PageAnnots[$startlinepage][$pak]);
  11246. $npak = count($this->PageAnnots[$this->page]) - 1;
  11247. $this->PageAnnots[$this->page][$npak]['y'] -= $yshift;
  11248. }
  11249. }
  11250. }
  11251. $pask = $next_pask;
  11252. }
  11253. $this->y += (($curfontsize - $fontsize) / $this->k);
  11254. $minstartliney = min($this->y, $minstartliney);
  11255. }
  11256. $curfontname = $fontname;
  11257. $curfontstyle = $fontstyle;
  11258. $curfontsize = $fontsize;
  11259. }
  11260. }
  11261. if (($plalign == 'J') AND (in_array($dom[$key]['value'], $blocktags))) {
  11262. $plalign = '';
  11263. }
  11264. // get current position on page buffer
  11265. $curpos = $this->pagelen[$startlinepage];
  11266. if (isset($dom[$key]['bgcolor']) AND ($dom[$key]['bgcolor'] !== false)) {
  11267. $this->SetFillColorArray($dom[$key]['bgcolor']);
  11268. $wfill = true;
  11269. } else {
  11270. $wfill = $fill | false;
  11271. }
  11272. if (isset($dom[$key]['fgcolor']) AND ($dom[$key]['fgcolor'] !== false)) {
  11273. $this->SetTextColorArray($dom[$key]['fgcolor']);
  11274. }
  11275. if (isset($dom[$key]['align'])) {
  11276. $lalign = $dom[$key]['align'];
  11277. }
  11278. if ($this->empty_string($lalign)) {
  11279. $lalign = $align;
  11280. }
  11281. }
  11282. // align lines
  11283. if ($this->newline AND (strlen($dom[$key]['value']) > 0) AND ($dom[$key]['value'] != 'td') AND ($dom[$key]['value'] != 'th')) {
  11284. $newline = true;
  11285. // we are at the beginning of a new line
  11286. if (isset($startlinex)) {
  11287. $yshift = $minstartliney - $startliney;
  11288. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  11289. $yshift = 0;
  11290. }
  11291. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl))))) OR ($yshift < 0)) {
  11292. // the last line must be shifted to be aligned as requested
  11293. $linew = abs($this->endlinex - $startlinex);
  11294. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  11295. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  11296. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11297. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  11298. } elseif (isset($opentagpos)) {
  11299. $midpos = $opentagpos;
  11300. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  11301. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11302. $midpos = $this->footerpos[$startlinepage];
  11303. } else {
  11304. $midpos = 0;
  11305. }
  11306. if ($midpos > 0) {
  11307. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  11308. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  11309. } else {
  11310. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  11311. $pend = '';
  11312. }
  11313. // calculate shifting amount
  11314. $tw = $w;
  11315. if ($this->lMargin != $prevlMargin) {
  11316. $tw += ($prevlMargin - $this->lMargin);
  11317. }
  11318. if ($this->rMargin != $prevrMargin) {
  11319. $tw += ($prevrMargin - $this->rMargin);
  11320. }
  11321. $mdiff = abs($tw - $linew);
  11322. $t_x = 0;
  11323. if ($plalign == 'C') {
  11324. if ($this->rtl) {
  11325. $t_x = -($mdiff / 2);
  11326. } else {
  11327. $t_x = ($mdiff / 2);
  11328. }
  11329. } elseif (($plalign == 'R') AND (!$this->rtl)) {
  11330. // right alignment on LTR document
  11331. $t_x = $mdiff;
  11332. } elseif (($plalign == 'L') AND ($this->rtl)) {
  11333. // left alignment on RTL document
  11334. $t_x = -$mdiff;
  11335. } elseif (($plalign == 'J') AND ($plalign == $lalign)) {
  11336. // Justification
  11337. if ($this->rtl OR $this->tmprtl) {
  11338. $t_x = $this->lMargin - $this->endlinex;
  11339. }
  11340. $no = 0;
  11341. $ns = 0;
  11342. $pmidtemp = $pmid;
  11343. // escape special characters
  11344. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  11345. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  11346. // search spaces
  11347. if (preg_match_all('/\[\(([^\)]*)\)\]/x', $pmidtemp, $lnstring, PREG_PATTERN_ORDER)) {
  11348. $maxkk = count($lnstring[1]) - 1;
  11349. for ($kk=0; $kk <= $maxkk; ++$kk) {
  11350. // restore special characters
  11351. $lnstring[1][$kk] = str_replace('#!#OP#!#', '(', $lnstring[1][$kk]);
  11352. $lnstring[1][$kk] = str_replace('#!#CP#!#', ')', $lnstring[1][$kk]);
  11353. if ($kk == $maxkk) {
  11354. if ($this->rtl OR $this->tmprtl) {
  11355. $tvalue = ltrim($lnstring[1][$kk]);
  11356. } else {
  11357. $tvalue = rtrim($lnstring[1][$kk]);
  11358. }
  11359. } else {
  11360. $tvalue = $lnstring[1][$kk];
  11361. }
  11362. // count spaces on line
  11363. $no += substr_count($lnstring[1][$kk], chr(32));
  11364. $ns += substr_count($tvalue, chr(32));
  11365. }
  11366. if ($this->rtl OR $this->tmprtl) {
  11367. $t_x = $this->lMargin - $this->endlinex - (($no - $ns - 1) * $this->GetStringWidth(chr(32)));
  11368. }
  11369. // calculate additional space to add to each space
  11370. $spacelen = $this->GetStringWidth(chr(32));
  11371. $spacewidth = (($tw - $linew + (($no - $ns) * $spacelen)) / ($ns?$ns:1)) * $this->k;
  11372. $spacewidthu = -1000 * ($tw - $linew + ($no * $spacelen)) / ($ns?$ns:1) / $this->FontSize;
  11373. $nsmax = $ns;
  11374. $ns = 0;
  11375. reset($lnstring);
  11376. $offset = 0;
  11377. $strcount = 0;
  11378. $prev_epsposbeg = 0;
  11379. global $spacew;
  11380. while (preg_match('/([0-9\.\+\-]*)[\s](Td|cm|m|l|c|re)[\s]/x', $pmid, $strpiece, PREG_OFFSET_CAPTURE, $offset) == 1) {
  11381. // check if we are inside a string section '[( ... )]'
  11382. $stroffset = strpos($pmid, '[(', $offset);
  11383. if (($stroffset !== false) AND ($stroffset <= $strpiece[2][1])) {
  11384. // set offset to the end of string section
  11385. $offset = strpos($pmid, ')]', $stroffset);
  11386. while (($offset !== false) AND ($pmid{($offset - 1)} == '\\')) {
  11387. $offset = strpos($pmid, ')]', ($offset + 1));
  11388. }
  11389. if ($offset === false) {
  11390. $this->Error('HTML Justification: malformed PDF code.');
  11391. }
  11392. continue;
  11393. }
  11394. if ($this->rtl OR $this->tmprtl) {
  11395. $spacew = ($spacewidth * ($nsmax - $ns));
  11396. } else {
  11397. $spacew = ($spacewidth * $ns);
  11398. }
  11399. $offset = $strpiece[2][1] + strlen($strpiece[2][0]);
  11400. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, $offset);
  11401. $epsposend = strpos($pmid, $this->epsmarker.'Q', $offset) + strlen($this->epsmarker.'Q');
  11402. if ((($epsposbeg > 0) AND ($epsposend > 0) AND ($offset > $epsposbeg) AND ($offset < $epsposend))
  11403. OR (($epsposbeg === false) AND ($epsposend > 0) AND ($offset < $epsposend))) {
  11404. // shift EPS images
  11405. $trx = sprintf('1 0 0 1 %.3F 0 cm', $spacew);
  11406. $epsposbeg = strpos($pmid, 'q'.$this->epsmarker, ($prev_epsposbeg - 6));
  11407. $pmid_b = substr($pmid, 0, $epsposbeg);
  11408. $pmid_m = substr($pmid, $epsposbeg, ($epsposend - $epsposbeg));
  11409. $pmid_e = substr($pmid, $epsposend);
  11410. $pmid = $pmid_b."\nq\n".$trx."\n".$pmid_m."\nQ\n".$pmid_e;
  11411. $offset = $epsposend;
  11412. continue;
  11413. }
  11414. $prev_epsposbeg = $epsposbeg;
  11415. $currentxpos = 0;
  11416. // shift blocks of code
  11417. switch ($strpiece[2][0]) {
  11418. case 'Td':
  11419. case 'cm':
  11420. case 'm':
  11421. case 'l': {
  11422. // get current X position
  11423. preg_match('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  11424. $currentxpos = $xmatches[1];
  11425. if (($strcount <= $maxkk) AND ($strpiece[2][0] == 'Td')) {
  11426. if ($strcount == $maxkk) {
  11427. if ($this->rtl OR $this->tmprtl) {
  11428. $tvalue = $lnstring[1][$strcount];
  11429. } else {
  11430. $tvalue = rtrim($lnstring[1][$strcount]);
  11431. }
  11432. } else {
  11433. $tvalue = $lnstring[1][$strcount];
  11434. }
  11435. $ns += substr_count($tvalue, chr(32));
  11436. ++$strcount;
  11437. }
  11438. if ($this->rtl OR $this->tmprtl) {
  11439. $spacew = ($spacewidth * ($nsmax - $ns));
  11440. }
  11441. // justify block
  11442. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  11443. create_function('$matches', 'global $spacew;
  11444. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  11445. return "".$newx." ".$matches[2]." x*#!#*x".$matches[3].$matches[4];'), $pmid, 1);
  11446. break;
  11447. }
  11448. case 're': {
  11449. // get current X position
  11450. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  11451. $currentxpos = $xmatches[1];
  11452. // justify block
  11453. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  11454. create_function('$matches', 'global $spacew;
  11455. $newx = sprintf("%.2F",(floatval($matches[1]) + $spacew));
  11456. return "".$newx." ".$matches[2]." ".$matches[3]." ".$matches[4]." x*#!#*x".$matches[5].$matches[6];'), $pmid, 1);
  11457. break;
  11458. }
  11459. case 'c': {
  11460. // get current X position
  11461. preg_match('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x', $pmid, $xmatches);
  11462. $currentxpos = $xmatches[1];
  11463. // justify block
  11464. $pmid = preg_replace_callback('/([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]([0-9\.\+\-]*)[\s]('.$strpiece[1][0].')[\s]('.$strpiece[2][0].')([\s]*)/x',
  11465. create_function('$matches', 'global $spacew;
  11466. $newx1 = sprintf("%.3F",(floatval($matches[1]) + $spacew));
  11467. $newx2 = sprintf("%.3F",(floatval($matches[3]) + $spacew));
  11468. $newx3 = sprintf("%.3F",(floatval($matches[5]) + $spacew));
  11469. return "".$newx1." ".$matches[2]." ".$newx2." ".$matches[4]." ".$newx3." ".$matches[6]." x*#!#*x".$matches[7].$matches[8];'), $pmid, 1);
  11470. break;
  11471. }
  11472. }
  11473. // shift the annotations and links
  11474. if (isset($this->PageAnnots[$this->page])) {
  11475. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  11476. if (($pac['y'] >= $minstartliney) AND (($pac['x'] * $this->k) >= ($currentxpos - $this->feps)) AND (($pac['x'] * $this->k) <= ($currentxpos + $this->feps))) {
  11477. $this->PageAnnots[$this->page][$pak]['x'] += ($spacew / $this->k);
  11478. $this->PageAnnots[$this->page][$pak]['w'] += (($spacewidth * $pac['numspaces']) / $this->k);
  11479. break;
  11480. }
  11481. }
  11482. }
  11483. } // end of while
  11484. // remove markers
  11485. $pmid = str_replace('x*#!#*x', '', $pmid);
  11486. if (($this->CurrentFont['type'] == 'TrueTypeUnicode') OR ($this->CurrentFont['type'] == 'cidfont0')) {
  11487. // multibyte characters
  11488. $spacew = $spacewidthu;
  11489. $pmidtemp = $pmid;
  11490. // escape special characters
  11491. $pmidtemp = preg_replace('/[\\\][\(]/x', '\\#!#OP#!#', $pmidtemp);
  11492. $pmidtemp = preg_replace('/[\\\][\)]/x', '\\#!#CP#!#', $pmidtemp);
  11493. $pmid = preg_replace_callback("/\[\(([^\)]*)\)\]/x",
  11494. create_function('$matches', 'global $spacew;
  11495. $matches[1] = str_replace("#!#OP#!#", "(", $matches[1]);
  11496. $matches[1] = str_replace("#!#CP#!#", ")", $matches[1]);
  11497. return "[(".str_replace(chr(0).chr(32), ") ".($spacew)." (", $matches[1]).")]";'), $pmidtemp);
  11498. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\n".$pend);
  11499. $endlinepos = strlen($pstart."\n".$pmid."\n");
  11500. } else {
  11501. // non-unicode (single-byte characters)
  11502. $rs = sprintf("%.3F Tw", $spacewidth);
  11503. $pmid = preg_replace("/\[\(/x", $rs.' [(', $pmid);
  11504. $this->setPageBuffer($startlinepage, $pstart."\n".$pmid."\nBT 0 Tw ET\n".$pend);
  11505. $endlinepos = strlen($pstart."\n".$pmid."\nBT 0 Tw ET\n");
  11506. }
  11507. }
  11508. } // end of J
  11509. if (($t_x != 0) OR ($yshift < 0)) {
  11510. // shift the line
  11511. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  11512. $this->setPageBuffer($startlinepage, $pstart."\nq\n".$trx."\n".$pmid."\nQ\n".$pend);
  11513. $endlinepos = strlen($pstart."\nq\n".$trx."\n".$pmid."\nQ\n");
  11514. // shift the annotations and links
  11515. if (isset($this->PageAnnots[$this->page])) {
  11516. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  11517. if ($pak >= $pask) {
  11518. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  11519. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  11520. }
  11521. }
  11522. }
  11523. $this->y -= $yshift;
  11524. }
  11525. }
  11526. }
  11527. $this->newline = false;
  11528. $pbrk = $this->checkPageBreak($this->lasth);
  11529. $this->SetFont($fontname, $fontstyle, $fontsize);
  11530. if ($wfill) {
  11531. $this->SetFillColorArray($this->bgcolor);
  11532. }
  11533. $startlinex = $this->x;
  11534. $startliney = $this->y;
  11535. $minstartliney = $this->y;
  11536. $startlinepage = $this->page;
  11537. if (isset($endlinepos) AND (!$pbrk)) {
  11538. $startlinepos = $endlinepos;
  11539. unset($endlinepos);
  11540. } else {
  11541. if (isset($this->footerlen[$this->page])) {
  11542. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11543. } else {
  11544. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11545. }
  11546. $startlinepos = $this->footerpos[$this->page];
  11547. }
  11548. $plalign = $lalign;
  11549. if (isset($this->PageAnnots[$this->page])) {
  11550. $pask = count($this->PageAnnots[$this->page]);
  11551. } else {
  11552. $pask = 0;
  11553. }
  11554. }
  11555. if (isset($opentagpos)) {
  11556. unset($opentagpos);
  11557. }
  11558. if ($dom[$key]['tag']) {
  11559. if ($dom[$key]['opening']) {
  11560. if ($dom[$key]['value'] == 'table') {
  11561. if ($this->rtl) {
  11562. $wtmp = $this->x - $this->lMargin;
  11563. } else {
  11564. $wtmp = $this->w - $this->rMargin - $this->x;
  11565. }
  11566. $wtmp -= (2 * $this->cMargin);
  11567. // calculate cell width
  11568. if (isset($dom[$key]['width'])) {
  11569. $table_width = $this->getHTMLUnitToUnits($dom[$key]['width'], $wtmp, 'px');
  11570. } else {
  11571. $table_width = $wtmp;
  11572. }
  11573. }
  11574. // table content is handled in a special way
  11575. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  11576. $trid = $dom[$key]['parent'];
  11577. $table_el = $dom[$trid]['parent'];
  11578. if (!isset($dom[$table_el]['cols'])) {
  11579. $dom[$table_el]['cols'] = $trid['cols'];
  11580. }
  11581. $oldmargin = $this->cMargin;
  11582. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'])) {
  11583. $currentcmargin = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellpadding'], 1, 'px');
  11584. } else {
  11585. $currentcmargin = 0;
  11586. }
  11587. $this->cMargin = $currentcmargin;
  11588. if (isset($dom[($dom[$trid]['parent'])]['attribute']['cellspacing'])) {
  11589. $cellspacing = $this->getHTMLUnitToUnits($dom[($dom[$trid]['parent'])]['attribute']['cellspacing'], 1, 'px');
  11590. } else {
  11591. $cellspacing = 0;
  11592. }
  11593. if ($this->rtl) {
  11594. $cellspacingx = -$cellspacing;
  11595. } else {
  11596. $cellspacingx = $cellspacing;
  11597. }
  11598. $colspan = $dom[$key]['attribute']['colspan'];
  11599. $wtmp = ($colspan * ($table_width / $dom[$table_el]['cols']));
  11600. if (isset($dom[$key]['width'])) {
  11601. $cellw = $this->getHTMLUnitToUnits($dom[$key]['width'], $table_width, 'px');
  11602. } else {
  11603. $cellw = $wtmp;
  11604. }
  11605. if (isset($dom[$key]['height'])) {
  11606. // minimum cell height
  11607. $cellh = $this->getHTMLUnitToUnits($dom[$key]['height'], 0, 'px');
  11608. } else {
  11609. $cellh = 0;
  11610. }
  11611. $cellw -= $cellspacing;
  11612. if (isset($dom[$key]['content'])) {
  11613. $cell_content = $dom[$key]['content'];
  11614. } else {
  11615. $cell_content = '&nbsp;';
  11616. }
  11617. $tagtype = $dom[$key]['value'];
  11618. $parentid = $key;
  11619. while (($key < $maxel) AND (!(($dom[$key]['tag']) AND (!$dom[$key]['opening']) AND ($dom[$key]['value'] == $tagtype) AND ($dom[$key]['parent'] == $parentid)))) {
  11620. // move $key index forward
  11621. ++$key;
  11622. }
  11623. if (!isset($dom[$trid]['startpage'])) {
  11624. $dom[$trid]['startpage'] = $this->page;
  11625. } else {
  11626. $this->setPage($dom[$trid]['startpage']);
  11627. }
  11628. if (!isset($dom[$trid]['starty'])) {
  11629. $dom[$trid]['starty'] = $this->y;
  11630. } else {
  11631. $this->y = $dom[$trid]['starty'];
  11632. }
  11633. if (!isset($dom[$trid]['startx'])) {
  11634. $dom[$trid]['startx'] = $this->x;
  11635. }
  11636. $this->x += ($cellspacingx / 2);
  11637. if (isset($dom[$parentid]['attribute']['rowspan'])) {
  11638. $rowspan = intval($dom[$parentid]['attribute']['rowspan']);
  11639. } else {
  11640. $rowspan = 1;
  11641. }
  11642. // skip row-spanned cells started on the previous rows
  11643. if (isset($dom[$table_el]['rowspans'])) {
  11644. $rsk = 0;
  11645. $rskmax = count($dom[$table_el]['rowspans']);
  11646. while ($rsk < $rskmax) {
  11647. $trwsp = $dom[$table_el]['rowspans'][$rsk];
  11648. $rsstartx = $trwsp['startx'];
  11649. $rsendx = $trwsp['endx'];
  11650. // account for margin changes
  11651. if ($trwsp['startpage'] < $this->page) {
  11652. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$trwsp['startpage']]['orm'])) {
  11653. $dl = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$trwsp['startpage']]['orm']);
  11654. $rsstartx -= $dl;
  11655. $rsendx -= $dl;
  11656. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$trwsp['startpage']]['olm'])) {
  11657. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$trwsp['startpage']]['olm']);
  11658. $rsstartx += $dl;
  11659. $rsendx += $dl;
  11660. }
  11661. }
  11662. if (($trwsp['rowspan'] > 0)
  11663. AND ($rsstartx > ($this->x - $cellspacing - $currentcmargin - $this->feps))
  11664. AND ($rsstartx < ($this->x + $cellspacing + $currentcmargin + $this->feps))
  11665. AND (($trwsp['starty'] < ($this->y - $this->feps)) OR ($trwsp['startpage'] < $this->page))) {
  11666. // set the starting X position of the current cell
  11667. $this->x = $rsendx + $cellspacingx;
  11668. if (($trwsp['rowspan'] == 1)
  11669. AND (isset($dom[$trid]['endy']))
  11670. AND (isset($dom[$trid]['endpage']))
  11671. AND ($trwsp['endpage'] == $dom[$trid]['endpage'])) {
  11672. // set ending Y position for row
  11673. $dom[$table_el]['rowspans'][$rsk]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  11674. $dom[$trid]['endy'] = $dom[$table_el]['rowspans'][$rsk]['endy'];
  11675. }
  11676. $rsk = 0;
  11677. } else {
  11678. ++$rsk;
  11679. }
  11680. }
  11681. }
  11682. // add rowspan information to table element
  11683. if ($rowspan > 1) {
  11684. if (isset($this->footerlen[$this->page])) {
  11685. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11686. } else {
  11687. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11688. }
  11689. $trintmrkpos = $this->footerpos[$this->page];
  11690. $trsid = array_push($dom[$table_el]['rowspans'], array('trid' => $trid, 'rowspan' => $rowspan, 'mrowspan' => $rowspan, 'colspan' => $colspan, 'startpage' => $this->page, 'startx' => $this->x, 'starty' => $this->y, 'intmrkpos' => $trintmrkpos));
  11691. }
  11692. $cellid = array_push($dom[$trid]['cellpos'], array('startx' => $this->x));
  11693. if ($rowspan > 1) {
  11694. $dom[$trid]['cellpos'][($cellid - 1)]['rowspanid'] = ($trsid - 1);
  11695. }
  11696. // push background colors
  11697. if (isset($dom[$parentid]['bgcolor']) AND ($dom[$parentid]['bgcolor'] !== false)) {
  11698. $dom[$trid]['cellpos'][($cellid - 1)]['bgcolor'] = $dom[$parentid]['bgcolor'];
  11699. }
  11700. $prevLastH = $this->lasth;
  11701. // ****** write the cell content ******
  11702. $this->MultiCell($cellw, $cellh, $cell_content, false, $lalign, false, 2, '', '', true, 0, true);
  11703. $this->lasth = $prevLastH;
  11704. $this->cMargin = $oldmargin;
  11705. $dom[$trid]['cellpos'][($cellid - 1)]['endx'] = $this->x;
  11706. // update the end of row position
  11707. if ($rowspan <= 1) {
  11708. if (isset($dom[$trid]['endy'])) {
  11709. if ($this->page == $dom[$trid]['endpage']) {
  11710. $dom[$trid]['endy'] = max($this->y, $dom[$trid]['endy']);
  11711. } elseif ($this->page > $dom[$trid]['endpage']) {
  11712. $dom[$trid]['endy'] = $this->y;
  11713. }
  11714. } else {
  11715. $dom[$trid]['endy'] = $this->y;
  11716. }
  11717. if (isset($dom[$trid]['endpage'])) {
  11718. $dom[$trid]['endpage'] = max($this->page, $dom[$trid]['endpage']);
  11719. } else {
  11720. $dom[$trid]['endpage'] = $this->page;
  11721. }
  11722. } else {
  11723. // account for row-spanned cells
  11724. $dom[$table_el]['rowspans'][($trsid - 1)]['endx'] = $this->x;
  11725. $dom[$table_el]['rowspans'][($trsid - 1)]['endy'] = $this->y;
  11726. $dom[$table_el]['rowspans'][($trsid - 1)]['endpage'] = $this->page;
  11727. }
  11728. if (isset($dom[$table_el]['rowspans'])) {
  11729. // update endy and endpage on rowspanned cells
  11730. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  11731. if ($trwsp['rowspan'] > 0) {
  11732. if (isset($dom[$trid]['endpage'])) {
  11733. if ($trwsp['endpage'] == $dom[$trid]['endpage']) {
  11734. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$trid]['endy'], $trwsp['endy']);
  11735. } elseif ($trwsp['endpage'] < $dom[$trid]['endpage']) {
  11736. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[$trid]['endy'];
  11737. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[$trid]['endpage'];
  11738. } else {
  11739. $dom[$trid]['endy'] = $this->pagedim[$dom[$trid]['endpage']]['hk'] - $this->pagedim[$dom[$trid]['endpage']]['bm'];
  11740. }
  11741. }
  11742. }
  11743. }
  11744. }
  11745. $this->x += ($cellspacingx / 2);
  11746. } else {
  11747. // opening tag (or self-closing tag)
  11748. if (!isset($opentagpos)) {
  11749. if (!$this->InFooter) {
  11750. if (isset($this->footerlen[$this->page])) {
  11751. $this->footerpos[$this->page] = $this->pagelen[$this->page] - $this->footerlen[$this->page];
  11752. } else {
  11753. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  11754. }
  11755. $opentagpos = $this->footerpos[$this->page];
  11756. }
  11757. }
  11758. $this->openHTMLTagHandler($dom, $key, $cell);
  11759. }
  11760. } else {
  11761. // closing tag
  11762. $this->closeHTMLTagHandler($dom, $key, $cell);
  11763. }
  11764. } elseif (strlen($dom[$key]['value']) > 0) {
  11765. // print list-item
  11766. if (!$this->empty_string($this->lispacer)) {
  11767. $this->SetFont($pfontname, $pfontstyle, $pfontsize);
  11768. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  11769. $minstartliney = $this->y;
  11770. $this->putHtmlListBullet($this->listnum, $this->lispacer, $pfontsize);
  11771. $this->SetFont($curfontname, $curfontstyle, $curfontsize);
  11772. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  11773. if (is_numeric($pfontsize) AND ($pfontsize > 0) AND is_numeric($curfontsize) AND ($curfontsize > 0) AND ($pfontsize != $curfontsize)) {
  11774. $this->y += (($pfontsize - $curfontsize) / $this->k);
  11775. $minstartliney = min($this->y, $minstartliney);
  11776. }
  11777. }
  11778. // text
  11779. $this->htmlvspace = 0;
  11780. if ((!$this->premode) AND ($this->rtl OR $this->tmprtl)) {
  11781. // reverse spaces order
  11782. $len1 = strlen($dom[$key]['value']);
  11783. $lsp = $len1 - strlen(ltrim($dom[$key]['value']));
  11784. $rsp = $len1 - strlen(rtrim($dom[$key]['value']));
  11785. $tmpstr = '';
  11786. if ($rsp > 0) {
  11787. $tmpstr .= substr($dom[$key]['value'], -$rsp);
  11788. }
  11789. $tmpstr .= trim($dom[$key]['value']);
  11790. if ($lsp > 0) {
  11791. $tmpstr .= substr($dom[$key]['value'], 0, $lsp);
  11792. }
  11793. $dom[$key]['value'] = $tmpstr;
  11794. }
  11795. if ($newline) {
  11796. if (!$this->premode) {
  11797. if (($this->rtl OR $this->tmprtl)) {
  11798. $dom[$key]['value'] = rtrim($dom[$key]['value']);
  11799. } else {
  11800. $dom[$key]['value'] = ltrim($dom[$key]['value']);
  11801. }
  11802. }
  11803. $newline = false;
  11804. $firstblock = true;
  11805. } else {
  11806. $firstblock = false;
  11807. }
  11808. $strrest = '';
  11809. if (!empty($this->HREF) AND (isset($this->HREF['url']))) {
  11810. // HTML <a> Link
  11811. $strrest = $this->addHtmlLink($this->HREF['url'], $dom[$key]['value'], $wfill, true, $this->HREF['color'], $this->HREF['style']);
  11812. } else {
  11813. $ctmpmargin = $this->cMargin;
  11814. $this->cMargin = 0;
  11815. // ****** write only until the end of the line and get the rest ******
  11816. $strrest = $this->Write($this->lasth, $dom[$key]['value'], '', $wfill, '', false, 0, true, $firstblock);
  11817. $this->cMargin = $ctmpmargin;
  11818. }
  11819. if (strlen($strrest) > 0) {
  11820. // store the remaining string on the previous $key position
  11821. $this->newline = true;
  11822. if ($cell) {
  11823. if ($this->rtl) {
  11824. $this->x -= $this->cMargin;
  11825. } else {
  11826. $this->x += $this->cMargin;
  11827. }
  11828. }
  11829. if ($strrest == $dom[$key]['value']) {
  11830. // used to avoid infinite loop
  11831. ++$loop;
  11832. } else {
  11833. $loop = 0;
  11834. }
  11835. $dom[$key]['value'] = ltrim($strrest);
  11836. if ($loop < 3) {
  11837. --$key;
  11838. }
  11839. } else {
  11840. $loop = 0;
  11841. }
  11842. }
  11843. ++$key;
  11844. } // end for each $key
  11845. // align the last line
  11846. if (isset($startlinex)) {
  11847. $yshift = $minstartliney - $startliney;
  11848. if (($yshift > 0) OR ($this->page > $startlinepage)) {
  11849. $yshift = 0;
  11850. }
  11851. if ((isset($plalign) AND ((($plalign == 'C') OR ($plalign == 'J') OR (($plalign == 'R') AND (!$this->rtl)) OR (($plalign == 'L') AND ($this->rtl))))) OR ($yshift < 0)) {
  11852. // the last line must be shifted to be aligned as requested
  11853. $linew = abs($this->endlinex - $startlinex);
  11854. $pstart = substr($this->getPageBuffer($startlinepage), 0, $startlinepos);
  11855. if (isset($opentagpos) AND isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  11856. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11857. $midpos = min($opentagpos, $this->footerpos[$startlinepage]);
  11858. } elseif (isset($opentagpos)) {
  11859. $midpos = $opentagpos;
  11860. } elseif (isset($this->footerlen[$startlinepage]) AND (!$this->InFooter)) {
  11861. $this->footerpos[$startlinepage] = $this->pagelen[$startlinepage] - $this->footerlen[$startlinepage];
  11862. $midpos = $this->footerpos[$startlinepage];
  11863. } else {
  11864. $midpos = 0;
  11865. }
  11866. if ($midpos > 0) {
  11867. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos, ($midpos - $startlinepos));
  11868. $pend = substr($this->getPageBuffer($startlinepage), $midpos);
  11869. } else {
  11870. $pmid = substr($this->getPageBuffer($startlinepage), $startlinepos);
  11871. $pend = '';
  11872. }
  11873. // calculate shifting amount
  11874. $tw = $w;
  11875. if ($this->lMargin != $prevlMargin) {
  11876. $tw += ($prevlMargin - $this->lMargin);
  11877. }
  11878. if ($this->rMargin != $prevrMargin) {
  11879. $tw += ($prevrMargin - $this->rMargin);
  11880. }
  11881. $mdiff = abs($tw - $linew);
  11882. if ($plalign == 'C') {
  11883. if ($this->rtl) {
  11884. $t_x = -($mdiff / 2);
  11885. } else {
  11886. $t_x = ($mdiff / 2);
  11887. }
  11888. } elseif (($plalign == 'R') AND (!$this->rtl)) {
  11889. // right alignment on LTR document
  11890. $t_x = $mdiff;
  11891. } elseif (($plalign == 'L') AND ($this->rtl)) {
  11892. // left alignment on RTL document
  11893. $t_x = -$mdiff;
  11894. } else {
  11895. $t_x = 0;
  11896. }
  11897. if (($t_x != 0) OR ($yshift < 0)) {
  11898. // shift the line
  11899. $trx = sprintf('1 0 0 1 %.3F %.3F cm', ($t_x * $this->k), ($yshift * $this->k));
  11900. $this->setPageBuffer($startlinepage, $pstart."\nq\n".$trx."\n".$pmid."\nQ\n".$pend);
  11901. $endlinepos = strlen($pstart."\nq\n".$trx."\n".$pmid."\nQ\n");
  11902. // shift the annotations and links
  11903. if (isset($this->PageAnnots[$this->page])) {
  11904. foreach ($this->PageAnnots[$this->page] as $pak => $pac) {
  11905. if ($pak >= $pask) {
  11906. $this->PageAnnots[$this->page][$pak]['x'] += $t_x;
  11907. $this->PageAnnots[$this->page][$pak]['y'] -= $yshift;
  11908. }
  11909. }
  11910. }
  11911. $this->y -= $yshift;
  11912. }
  11913. }
  11914. }
  11915. if ($ln AND (!($cell AND ($dom[$key-1]['value'] == 'table')))) {
  11916. $this->Ln($this->lasth);
  11917. }
  11918. // restore previous values
  11919. $this->setGraphicVars($gvars);
  11920. if ($this->page > $prevPage) {
  11921. $this->lMargin = $this->pagedim[$this->page]['olm'];
  11922. $this->rMargin = $this->pagedim[$this->page]['orm'];
  11923. }
  11924. unset($dom);
  11925. }
  11926. /**
  11927. * Process opening tags.
  11928. * @param array $dom html dom array
  11929. * @param int $key current element id
  11930. * @param boolean $cell if true add the default cMargin space to each new line (default false).
  11931. * @access protected
  11932. */
  11933. protected function openHTMLTagHandler(&$dom, $key, $cell=false) {
  11934. $tag = $dom[$key];
  11935. $parent = $dom[($dom[$key]['parent'])];
  11936. $firstorlast = ($key == 1);
  11937. // check for text direction attribute
  11938. if (isset($tag['attribute']['dir'])) {
  11939. $this->tmprtl = $tag['attribute']['dir'] == 'rtl' ? 'R' : 'L';
  11940. } else {
  11941. $this->tmprtl = false;
  11942. }
  11943. //Opening tag
  11944. switch($tag['value']) {
  11945. case 'table': {
  11946. $cp = 0;
  11947. $cs = 0;
  11948. $dom[$key]['rowspans'] = array();
  11949. if (!$this->empty_string($dom[$key]['thead'])) {
  11950. // set table header
  11951. $this->thead = $dom[$key]['thead'];
  11952. if (!isset($this->theadMargins) OR (empty($this->theadMargins))) {
  11953. $this->theadMargins = array();
  11954. $this->theadMargins['cmargin'] = $this->cMargin;
  11955. }
  11956. }
  11957. if (isset($tag['attribute']['cellpadding'])) {
  11958. $cp = $this->getHTMLUnitToUnits($tag['attribute']['cellpadding'], 1, 'px');
  11959. $this->oldcMargin = $this->cMargin;
  11960. $this->cMargin = $cp;
  11961. }
  11962. if (isset($tag['attribute']['cellspacing'])) {
  11963. $cs = $this->getHTMLUnitToUnits($tag['attribute']['cellspacing'], 1, 'px');
  11964. }
  11965. $this->checkPageBreak((2 * $cp) + (2 * $cs) + $this->lasth);
  11966. break;
  11967. }
  11968. case 'tr': {
  11969. // array of columns positions
  11970. $dom[$key]['cellpos'] = array();
  11971. break;
  11972. }
  11973. case 'hr': {
  11974. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  11975. $this->htmlvspace = 0;
  11976. $wtmp = $this->w - $this->lMargin - $this->rMargin;
  11977. if ((isset($tag['attribute']['width'])) AND ($tag['attribute']['width'] != '')) {
  11978. $hrWidth = $this->getHTMLUnitToUnits($tag['attribute']['width'], $wtmp, 'px');
  11979. } else {
  11980. $hrWidth = $wtmp;
  11981. }
  11982. $x = $this->GetX();
  11983. $y = $this->GetY();
  11984. $prevlinewidth = $this->GetLineWidth();
  11985. $this->Line($x, $y, $x + $hrWidth, $y);
  11986. $this->SetLineWidth($prevlinewidth);
  11987. $this->addHTMLVertSpace(1, $cell, '', !isset($dom[($key + 1)]), $tag['value'], false);
  11988. break;
  11989. }
  11990. case 'a': {
  11991. if (array_key_exists('href', $tag['attribute'])) {
  11992. $this->HREF['url'] = $tag['attribute']['href'];
  11993. }
  11994. $this->HREF['color'] = $this->htmlLinkColorArray;
  11995. $this->HREF['style'] = $this->htmlLinkFontStyle;
  11996. if (array_key_exists('style', $tag['attribute'])) {
  11997. // get style attributes
  11998. preg_match_all('/([^;:\s]*):([^;]*)/', $tag['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  11999. $astyle = array();
  12000. while (list($id, $name) = each($style_array[1])) {
  12001. $name = strtolower($name);
  12002. $astyle[$name] = trim($style_array[2][$id]);
  12003. }
  12004. if (isset($astyle['color'])) {
  12005. $this->HREF['color'] = $this->convertHTMLColorToDec($astyle['color']);
  12006. }
  12007. if (isset($astyle['text-decoration'])) {
  12008. $this->HREF['style'] = '';
  12009. $decors = explode(' ', strtolower($astyle['text-decoration']));
  12010. foreach ($decors as $dec) {
  12011. $dec = trim($dec);
  12012. if (!$this->empty_string($dec)) {
  12013. if ($dec{0} == 'u') {
  12014. $this->HREF['style'] .= 'U';
  12015. } elseif ($dec{0} == 'l') {
  12016. $this->HREF['style'] .= 'D';
  12017. }
  12018. }
  12019. }
  12020. }
  12021. }
  12022. break;
  12023. }
  12024. case 'img': {
  12025. if (isset($tag['attribute']['src'])) {
  12026. // replace relative path with real server path
  12027. if ($tag['attribute']['src'][0] == '/') {
  12028. $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
  12029. }
  12030. $tag['attribute']['src'] = urldecode($tag['attribute']['src']);
  12031. $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
  12032. if (!isset($tag['attribute']['width'])) {
  12033. $tag['attribute']['width'] = 0;
  12034. }
  12035. if (!isset($tag['attribute']['height'])) {
  12036. $tag['attribute']['height'] = 0;
  12037. }
  12038. //if (!isset($tag['attribute']['align'])) {
  12039. // the only alignment supported is "bottom"
  12040. // further development is required for other modes.
  12041. $tag['attribute']['align'] = 'bottom';
  12042. //}
  12043. switch($tag['attribute']['align']) {
  12044. case 'top': {
  12045. $align = 'T';
  12046. break;
  12047. }
  12048. case 'middle': {
  12049. $align = 'M';
  12050. break;
  12051. }
  12052. case 'bottom': {
  12053. $align = 'B';
  12054. break;
  12055. }
  12056. default: {
  12057. $align = 'B';
  12058. break;
  12059. }
  12060. }
  12061. $fileinfo = pathinfo($tag['attribute']['src']);
  12062. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  12063. $type = strtolower($fileinfo['extension']);
  12064. }
  12065. $prevy = $this->y;
  12066. $xpos = $this->GetX();
  12067. if (isset($dom[($key - 1)]) AND ($dom[($key - 1)]['value'] == ' ')) {
  12068. if ($this->rtl) {
  12069. $xpos += $this->GetStringWidth(' ');
  12070. } else {
  12071. $xpos -= $this->GetStringWidth(' ');
  12072. }
  12073. }
  12074. $imglink = '';
  12075. if (isset($this->HREF['url']) AND !$this->empty_string($this->HREF['url'])) {
  12076. $imglink = $this->HREF['url'];
  12077. if ($imglink{0} == '#') {
  12078. // convert url to internal link
  12079. $page = intval(substr($imglink, 1));
  12080. $imglink = $this->AddLink();
  12081. $this->SetLink($imglink, 0, $page);
  12082. }
  12083. }
  12084. $border = 0;
  12085. if (isset($tag['attribute']['border']) AND !empty($tag['attribute']['border'])) {
  12086. // currently only support 1 (frame) or a combination of 'LTRB'
  12087. $border = $tag['attribute']['border'];
  12088. }
  12089. $iw = '';
  12090. if (isset($tag['attribute']['width'])) {
  12091. $iw = $this->getHTMLUnitToUnits($tag['attribute']['width'], 1, 'px', false);
  12092. }
  12093. $ih = '';
  12094. if (isset($tag['attribute']['height'])) {
  12095. $ih = $this->getHTMLUnitToUnits($tag['attribute']['height'], 1, 'px', false);
  12096. }
  12097. if (($type == 'eps') OR ($type == 'ai')) {
  12098. $this->ImageEps($tag['attribute']['src'], $xpos, $this->GetY(), $iw, $ih, $imglink, true, $align, '', $border);
  12099. } else {
  12100. $this->Image($tag['attribute']['src'], $xpos, $this->GetY(), $iw, $ih, '', $imglink, $align, false, 300, '', false, false, $border);
  12101. }
  12102. switch($align) {
  12103. case 'T': {
  12104. $this->y = $prevy;
  12105. break;
  12106. }
  12107. case 'M': {
  12108. $this->y = (($this->img_rb_y + $prevy - ($tag['fontsize'] / $this->k)) / 2) ;
  12109. break;
  12110. }
  12111. case 'B': {
  12112. $this->y = $this->img_rb_y - ($tag['fontsize'] / $this->k);
  12113. break;
  12114. }
  12115. }
  12116. }
  12117. break;
  12118. }
  12119. case 'dl': {
  12120. ++$this->listnum;
  12121. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
  12122. break;
  12123. }
  12124. case 'dt': {
  12125. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  12126. break;
  12127. }
  12128. case 'dd': {
  12129. if ($this->rtl) {
  12130. $this->rMargin += $this->listindent;
  12131. } else {
  12132. $this->lMargin += $this->listindent;
  12133. }
  12134. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  12135. break;
  12136. }
  12137. case 'ul':
  12138. case 'ol': {
  12139. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
  12140. $this->htmlvspace = 0;
  12141. ++$this->listnum;
  12142. if ($tag['value'] == 'ol') {
  12143. $this->listordered[$this->listnum] = true;
  12144. } else {
  12145. $this->listordered[$this->listnum] = false;
  12146. }
  12147. if (isset($tag['attribute']['start'])) {
  12148. $this->listcount[$this->listnum] = intval($tag['attribute']['start']) - 1;
  12149. } else {
  12150. $this->listcount[$this->listnum] = 0;
  12151. }
  12152. if ($this->rtl) {
  12153. $this->rMargin += $this->listindent;
  12154. } else {
  12155. $this->lMargin += $this->listindent;
  12156. }
  12157. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], false);
  12158. $this->htmlvspace = 0;
  12159. break;
  12160. }
  12161. case 'li': {
  12162. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  12163. if ($this->listordered[$this->listnum]) {
  12164. // ordered item
  12165. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  12166. $this->lispacer = $parent['attribute']['type'];
  12167. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  12168. $this->lispacer = $parent['listtype'];
  12169. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  12170. $this->lispacer = $this->lisymbol;
  12171. } else {
  12172. $this->lispacer = '#';
  12173. }
  12174. ++$this->listcount[$this->listnum];
  12175. if (isset($tag['attribute']['value'])) {
  12176. $this->listcount[$this->listnum] = intval($tag['attribute']['value']);
  12177. }
  12178. } else {
  12179. // unordered item
  12180. if (isset($parent['attribute']['type']) AND !$this->empty_string($parent['attribute']['type'])) {
  12181. $this->lispacer = $parent['attribute']['type'];
  12182. } elseif (isset($parent['listtype']) AND !$this->empty_string($parent['listtype'])) {
  12183. $this->lispacer = $parent['listtype'];
  12184. } elseif (isset($this->lisymbol) AND !$this->empty_string($this->lisymbol)) {
  12185. $this->lispacer = $this->lisymbol;
  12186. } else {
  12187. $this->lispacer = '!';
  12188. }
  12189. }
  12190. break;
  12191. }
  12192. case 'blockquote': {
  12193. if ($this->rtl) {
  12194. $this->rMargin += $this->listindent;
  12195. } else {
  12196. $this->lMargin += $this->listindent;
  12197. }
  12198. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], false);
  12199. break;
  12200. }
  12201. case 'br': {
  12202. $this->Ln('', $cell);
  12203. break;
  12204. }
  12205. case 'div': {
  12206. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  12207. break;
  12208. }
  12209. case 'p': {
  12210. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], false);
  12211. break;
  12212. }
  12213. case 'pre': {
  12214. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], false);
  12215. $this->premode = true;
  12216. break;
  12217. }
  12218. case 'sup': {
  12219. $this->SetXY($this->GetX(), $this->GetY() - ((0.7 * $this->FontSizePt) / $this->k));
  12220. break;
  12221. }
  12222. case 'sub': {
  12223. $this->SetXY($this->GetX(), $this->GetY() + ((0.3 * $this->FontSizePt) / $this->k));
  12224. break;
  12225. }
  12226. case 'h1':
  12227. case 'h2':
  12228. case 'h3':
  12229. case 'h4':
  12230. case 'h5':
  12231. case 'h6': {
  12232. $this->addHTMLVertSpace(1, $cell, ($tag['fontsize'] * 1.5) / $this->k, $firstorlast, $tag['value'], false);
  12233. break;
  12234. }
  12235. case 'tcpdf': {
  12236. // NOT HTML: used to call TCPDF methods
  12237. if (isset($tag['attribute']['method'])) {
  12238. $tcpdf_method = $tag['attribute']['method'];
  12239. if (method_exists($this, $tcpdf_method)) {
  12240. if (isset($tag['attribute']['params']) AND (!empty($tag['attribute']['params']))) {
  12241. eval('$params = array('.$tag['attribute']['params'].');');
  12242. call_user_func_array(array($this, $tcpdf_method), $params);
  12243. } else {
  12244. $this->$tcpdf_method();
  12245. }
  12246. $this->newline = true;
  12247. }
  12248. }
  12249. }
  12250. default: {
  12251. break;
  12252. }
  12253. }
  12254. }
  12255. /**
  12256. * Process closing tags.
  12257. * @param array $dom html dom array
  12258. * @param int $key current element id
  12259. * @param boolean $cell if true add the default cMargin space to each new line (default false).
  12260. * @access protected
  12261. */
  12262. protected function closeHTMLTagHandler(&$dom, $key, $cell=false) {
  12263. $tag = $dom[$key];
  12264. $parent = $dom[($dom[$key]['parent'])];
  12265. $firstorlast = ((!isset($dom[($key + 1)])) OR ((!isset($dom[($key + 2)])) AND ($dom[($key + 1)]['value'] == 'marker')));
  12266. $in_table_head = false;
  12267. //Closing tag
  12268. switch($tag['value']) {
  12269. case 'tr': {
  12270. $table_el = $dom[($dom[$key]['parent'])]['parent'];
  12271. if(!isset($parent['endy'])) {
  12272. $dom[($dom[$key]['parent'])]['endy'] = $this->y;
  12273. $parent['endy'] = $this->y;
  12274. }
  12275. if(!isset($parent['endpage'])) {
  12276. $dom[($dom[$key]['parent'])]['endpage'] = $this->page;
  12277. $parent['endpage'] = $this->page;
  12278. }
  12279. // update row-spanned cells
  12280. if (isset($dom[$table_el]['rowspans'])) {
  12281. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  12282. $dom[$table_el]['rowspans'][$k]['rowspan'] -= 1;
  12283. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  12284. if ($dom[$table_el]['rowspans'][$k]['endpage'] == $parent['endpage']) {
  12285. $dom[($dom[$key]['parent'])]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $parent['endy']);
  12286. } elseif ($dom[$table_el]['rowspans'][$k]['endpage'] > $parent['endpage']) {
  12287. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  12288. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  12289. }
  12290. }
  12291. }
  12292. // report new endy and endpage to the rowspanned cells
  12293. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  12294. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  12295. $dom[$table_el]['rowspans'][$k]['endpage'] = max($dom[$table_el]['rowspans'][$k]['endpage'], $dom[($dom[$key]['parent'])]['endpage']);
  12296. $dom[($dom[$key]['parent'])]['endpage'] = $dom[$table_el]['rowspans'][$k]['endpage'];
  12297. $dom[$table_el]['rowspans'][$k]['endy'] = max($dom[$table_el]['rowspans'][$k]['endy'], $dom[($dom[$key]['parent'])]['endy']);
  12298. $dom[($dom[$key]['parent'])]['endy'] = $dom[$table_el]['rowspans'][$k]['endy'];
  12299. }
  12300. }
  12301. // update remaining rowspanned cells
  12302. foreach ($dom[$table_el]['rowspans'] as $k => $trwsp) {
  12303. if ($dom[$table_el]['rowspans'][$k]['rowspan'] == 0) {
  12304. $dom[$table_el]['rowspans'][$k]['endpage'] = $dom[($dom[$key]['parent'])]['endpage'];
  12305. $dom[$table_el]['rowspans'][$k]['endy'] = $dom[($dom[$key]['parent'])]['endy'];
  12306. }
  12307. }
  12308. }
  12309. $this->setPage($dom[($dom[$key]['parent'])]['endpage']);
  12310. $this->y = $dom[($dom[$key]['parent'])]['endy'];
  12311. if (isset($dom[$table_el]['attribute']['cellspacing'])) {
  12312. $cellspacing = $this->getHTMLUnitToUnits($dom[$table_el]['attribute']['cellspacing'], 1, 'px');
  12313. $this->y += $cellspacing;
  12314. }
  12315. $this->Ln(0, $cell);
  12316. $this->x = $parent['startx'];
  12317. // account for booklet mode
  12318. if ($this->page > $parent['startpage']) {
  12319. if (($this->rtl) AND ($this->pagedim[$this->page]['orm'] != $this->pagedim[$parent['startpage']]['orm'])) {
  12320. $this->x += ($this->pagedim[$this->page]['orm'] - $this->pagedim[$parent['startpage']]['orm']);
  12321. } elseif ((!$this->rtl) AND ($this->pagedim[$this->page]['olm'] != $this->pagedim[$parent['startpage']]['olm'])) {
  12322. $this->x += ($this->pagedim[$this->page]['olm'] - $this->pagedim[$parent['startpage']]['olm']);
  12323. }
  12324. }
  12325. break;
  12326. }
  12327. case 'tablehead':
  12328. // closing tag used for the thead part
  12329. $in_table_head = true;
  12330. case 'table': {
  12331. // draw borders
  12332. $table_el = $parent;
  12333. if ((isset($table_el['attribute']['border']) AND ($table_el['attribute']['border'] > 0))
  12334. OR (isset($table_el['style']['border']) AND ($table_el['style']['border'] > 0))) {
  12335. $border = 1;
  12336. } else {
  12337. $border = 0;
  12338. }
  12339. // fix bottom line alignment of last line before page break
  12340. foreach ($dom[($dom[$key]['parent'])]['trids'] as $j => $trkey) {
  12341. // update row-spanned cells
  12342. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  12343. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  12344. if ($trwsp['trid'] == $trkey) {
  12345. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] -= 1;
  12346. }
  12347. if (isset($prevtrkey) AND ($trwsp['trid'] == $prevtrkey) AND ($trwsp['mrowspan'] >= 0)) {
  12348. $dom[($dom[$key]['parent'])]['rowspans'][$k]['trid'] = $trkey;
  12349. }
  12350. }
  12351. }
  12352. if (isset($prevtrkey) AND ($dom[$trkey]['startpage'] > $dom[$prevtrkey]['endpage'])) {
  12353. $pgendy = $this->pagedim[$dom[$prevtrkey]['endpage']]['hk'] - $this->pagedim[$dom[$prevtrkey]['endpage']]['bm'];
  12354. $dom[$prevtrkey]['endy'] = $pgendy;
  12355. // update row-spanned cells
  12356. if (isset($dom[($dom[$key]['parent'])]['rowspans'])) {
  12357. foreach ($dom[($dom[$key]['parent'])]['rowspans'] as $k => $trwsp) {
  12358. if (($trwsp['trid'] == $trkey) AND ($trwsp['mrowspan'] == 1) AND ($trwsp['endpage'] == $dom[$prevtrkey]['endpage'])) {
  12359. $dom[($dom[$key]['parent'])]['rowspans'][$k]['endy'] = $pgendy;
  12360. $dom[($dom[$key]['parent'])]['rowspans'][$k]['mrowspan'] = -1;
  12361. }
  12362. }
  12363. }
  12364. }
  12365. $prevtrkey = $trkey;
  12366. $table_el = $dom[($dom[$key]['parent'])];
  12367. }
  12368. // for each row
  12369. foreach ($table_el['trids'] as $j => $trkey) {
  12370. $parent = $dom[$trkey];
  12371. // for each cell on the row
  12372. foreach ($parent['cellpos'] as $k => $cellpos) {
  12373. if (isset($cellpos['rowspanid']) AND ($cellpos['rowspanid'] >= 0)) {
  12374. $cellpos['startx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['startx'];
  12375. $cellpos['endx'] = $table_el['rowspans'][($cellpos['rowspanid'])]['endx'];
  12376. $endy = $table_el['rowspans'][($cellpos['rowspanid'])]['endy'];
  12377. $startpage = $table_el['rowspans'][($cellpos['rowspanid'])]['startpage'];
  12378. $endpage = $table_el['rowspans'][($cellpos['rowspanid'])]['endpage'];
  12379. } else {
  12380. $endy = $parent['endy'];
  12381. $startpage = $parent['startpage'];
  12382. $endpage = $parent['endpage'];
  12383. }
  12384. if ($endpage > $startpage) {
  12385. // design borders around HTML cells.
  12386. for ($page=$startpage; $page <= $endpage; ++$page) {
  12387. $this->setPage($page);
  12388. if ($page == $startpage) {
  12389. $this->y = $parent['starty']; // put cursor at the beginning of row on the first page
  12390. $ch = $this->getPageHeight() - $parent['starty'] - $this->getBreakMargin();
  12391. $cborder = $this->getBorderMode($border, $position='start');
  12392. } elseif ($page == $endpage) {
  12393. $this->y = $this->tMargin; // put cursor at the beginning of last page
  12394. $ch = $endy - $this->tMargin;
  12395. $cborder = $this->getBorderMode($border, $position='end');
  12396. } else {
  12397. $this->y = $this->tMargin; // put cursor at the beginning of the current page
  12398. $ch = $this->getPageHeight() - $this->tMargin - $this->getBreakMargin();
  12399. $cborder = $this->getBorderMode($border, $position='middle');
  12400. }
  12401. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  12402. $this->SetFillColorArray($cellpos['bgcolor']);
  12403. $fill = true;
  12404. } else {
  12405. $fill = false;
  12406. }
  12407. $cw = abs($cellpos['endx'] - $cellpos['startx']);
  12408. $this->x = $cellpos['startx'];
  12409. // account for margin changes
  12410. if ($page > $startpage) {
  12411. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  12412. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  12413. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['lm'] != $this->pagedim[$startpage]['olm'])) {
  12414. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  12415. }
  12416. }
  12417. // design a cell around the text
  12418. $ccode = $this->FillColor."\n".$this->getCellCode($cw, $ch, '', $cborder, 1, '', $fill, '', 0, true);
  12419. if ($cborder OR $fill) {
  12420. $pagebuff = $this->getPageBuffer($this->page);
  12421. $pstart = substr($pagebuff, 0, $this->intmrk[$this->page]);
  12422. $pend = substr($pagebuff, $this->intmrk[$this->page]);
  12423. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  12424. $this->intmrk[$this->page] += strlen($ccode."\n");
  12425. }
  12426. }
  12427. } else {
  12428. $this->setPage($startpage);
  12429. if (isset($cellpos['bgcolor']) AND ($cellpos['bgcolor']) !== false) {
  12430. $this->SetFillColorArray($cellpos['bgcolor']);
  12431. $fill = true;
  12432. } else {
  12433. $fill = false;
  12434. }
  12435. $this->x = $cellpos['startx'];
  12436. $this->y = $parent['starty'];
  12437. $cw = abs($cellpos['endx'] - $cellpos['startx']);
  12438. $ch = $endy - $parent['starty'];
  12439. // design a cell around the text
  12440. $ccode = $this->FillColor."\n".$this->getCellCode($cw, $ch, '', $border, 1, '', $fill, '', 0, true);
  12441. if ($border OR $fill) {
  12442. if (end($this->transfmrk[$this->page]) !== false) {
  12443. $pagemarkkey = key($this->transfmrk[$this->page]);
  12444. $pagemark = &$this->transfmrk[$this->page][$pagemarkkey];
  12445. } elseif ($this->InFooter) {
  12446. $pagemark = &$this->footerpos[$this->page];
  12447. } else {
  12448. $pagemark = &$this->intmrk[$this->page];
  12449. }
  12450. $pagebuff = $this->getPageBuffer($this->page);
  12451. $pstart = substr($pagebuff, 0, $pagemark);
  12452. $pend = substr($pagebuff, $pagemark);
  12453. $this->setPageBuffer($this->page, $pstart.$ccode."\n".$pend);
  12454. $pagemark += strlen($ccode."\n");
  12455. }
  12456. }
  12457. }
  12458. if (isset($table_el['attribute']['cellspacing'])) {
  12459. $cellspacing = $this->getHTMLUnitToUnits($table_el['attribute']['cellspacing'], 1, 'px');
  12460. $this->y += $cellspacing;
  12461. }
  12462. $this->Ln(0, $cell);
  12463. $this->x = $parent['startx'];
  12464. if ($endpage > $startpage) {
  12465. if (($this->rtl) AND ($this->pagedim[$endpage]['orm'] != $this->pagedim[$startpage]['orm'])) {
  12466. $this->x += ($this->pagedim[$endpage]['orm'] - $this->pagedim[$startpage]['orm']);
  12467. } elseif ((!$this->rtl) AND ($this->pagedim[$endpage]['olm'] != $this->pagedim[$startpage]['olm'])) {
  12468. $this->x += ($this->pagedim[$endpage]['olm'] - $this->pagedim[$startpage]['olm']);
  12469. }
  12470. }
  12471. }
  12472. if (!$in_table_head) {
  12473. // we are not inside a thead section
  12474. if (isset($parent['cellpadding'])) {
  12475. $this->cMargin = $this->oldcMargin;
  12476. }
  12477. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  12478. if (isset($this->theadMargins['top'])) {
  12479. // restore top margin
  12480. $this->tMargin = $this->theadMargins['top'];
  12481. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  12482. }
  12483. // reset table header
  12484. $this->thead = '';
  12485. $this->theadMargins = array();
  12486. }
  12487. break;
  12488. }
  12489. case 'a': {
  12490. $this->HREF = '';
  12491. break;
  12492. }
  12493. case 'sup': {
  12494. $this->SetXY($this->GetX(), $this->GetY() + ((0.7 * $parent['fontsize']) / $this->k));
  12495. break;
  12496. }
  12497. case 'sub': {
  12498. $this->SetXY($this->GetX(), $this->GetY() - ((0.3 * $parent['fontsize'])/$this->k));
  12499. break;
  12500. }
  12501. case 'div': {
  12502. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], true);
  12503. break;
  12504. }
  12505. case 'blockquote': {
  12506. if ($this->rtl) {
  12507. $this->rMargin -= $this->listindent;
  12508. } else {
  12509. $this->lMargin -= $this->listindent;
  12510. }
  12511. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12512. break;
  12513. }
  12514. case 'p': {
  12515. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12516. break;
  12517. }
  12518. case 'pre': {
  12519. $this->addHTMLVertSpace(1, $cell, '', $firstorlast, $tag['value'], true);
  12520. $this->premode = false;
  12521. break;
  12522. }
  12523. case 'dl': {
  12524. --$this->listnum;
  12525. if ($this->listnum <= 0) {
  12526. $this->listnum = 0;
  12527. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12528. }
  12529. break;
  12530. }
  12531. case 'dt': {
  12532. $this->lispacer = '';
  12533. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], true);
  12534. break;
  12535. }
  12536. case 'dd': {
  12537. $this->lispacer = '';
  12538. if ($this->rtl) {
  12539. $this->rMargin -= $this->listindent;
  12540. } else {
  12541. $this->lMargin -= $this->listindent;
  12542. }
  12543. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], true);
  12544. break;
  12545. }
  12546. case 'ul':
  12547. case 'ol': {
  12548. --$this->listnum;
  12549. $this->lispacer = '';
  12550. if ($this->rtl) {
  12551. $this->rMargin -= $this->listindent;
  12552. } else {
  12553. $this->lMargin -= $this->listindent;
  12554. }
  12555. if ($this->listnum <= 0) {
  12556. $this->listnum = 0;
  12557. $this->addHTMLVertSpace(2, $cell, '', $firstorlast, $tag['value'], true);
  12558. }
  12559. $this->lasth = $this->FontSize * $this->cell_height_ratio;
  12560. break;
  12561. }
  12562. case 'li': {
  12563. $this->lispacer = '';
  12564. $this->addHTMLVertSpace(0, $cell, '', $firstorlast, $tag['value'], true);
  12565. break;
  12566. }
  12567. case 'h1':
  12568. case 'h2':
  12569. case 'h3':
  12570. case 'h4':
  12571. case 'h5':
  12572. case 'h6': {
  12573. $this->addHTMLVertSpace(1, $cell, ($parent['fontsize'] * 1.5) / $this->k, $firstorlast, $tag['value'], true);
  12574. break;
  12575. }
  12576. default : {
  12577. break;
  12578. }
  12579. }
  12580. $this->tmprtl = false;
  12581. }
  12582. /**
  12583. * Add vertical spaces if needed.
  12584. * @param int $n number of spaces to add
  12585. * @param boolean $cell if true add the default cMargin space to each new line (default false).
  12586. * @param string $h The height of the break. By default, the value equals the height of the last printed cell.
  12587. * @param boolean $firstorlast if true do not print additional empty lines.
  12588. * @param string $tag HTML tag to which this space will be applied
  12589. * @param boolean $closing true if this space will be applied to a closing tag, false otherwise
  12590. * @access protected
  12591. */
  12592. protected function addHTMLVertSpace($n, $cell=false, $h='', $firstorlast=false, $tag='', $closing=false) {
  12593. if ($firstorlast) {
  12594. $this->Ln(0, $cell);
  12595. $this->htmlvspace = 0;
  12596. return;
  12597. }
  12598. if (isset($this->tagvspaces[$tag][intval($closing)]['n'])) {
  12599. $n = $this->tagvspaces[$tag][intval($closing)]['n'];
  12600. }
  12601. if (isset($this->tagvspaces[$tag][intval($closing)]['h'])) {
  12602. $h = $this->tagvspaces[$tag][intval($closing)]['h'];
  12603. }
  12604. if (is_string($h)) {
  12605. $vsize = $n * $this->lasth;
  12606. } else {
  12607. $vsize = $n * $h;
  12608. }
  12609. if ($vsize > $this->htmlvspace) {
  12610. $this->Ln(($vsize - $this->htmlvspace), $cell);
  12611. $this->htmlvspace = $vsize;
  12612. }
  12613. }
  12614. /**
  12615. * Set the default bullet to be used as LI bullet symbol
  12616. * @param string $symbol character or string to be used (legal values are: '' = automatic, '!' = auto bullet, '#' = auto numbering, 'disc', 'disc', 'circle', 'square', '1', 'decimal', 'decimal-leading-zero', 'i', 'lower-roman', 'I', 'upper-roman', 'a', 'lower-alpha', 'lower-latin', 'A', 'upper-alpha', 'upper-latin', 'lower-greek')
  12617. * @access public
  12618. * @since 4.0.028 (2008-09-26)
  12619. */
  12620. public function setLIsymbol($symbol='!') {
  12621. $symbol = strtolower($symbol);
  12622. switch ($symbol) {
  12623. case '!' :
  12624. case '#' :
  12625. case 'disc' :
  12626. case 'disc' :
  12627. case 'circle' :
  12628. case 'square' :
  12629. case '1':
  12630. case 'decimal':
  12631. case 'decimal-leading-zero':
  12632. case 'i':
  12633. case 'lower-roman':
  12634. case 'I':
  12635. case 'upper-roman':
  12636. case 'a':
  12637. case 'lower-alpha':
  12638. case 'lower-latin':
  12639. case 'A':
  12640. case 'upper-alpha':
  12641. case 'upper-latin':
  12642. case 'lower-greek': {
  12643. $this->lisymbol = $symbol;
  12644. break;
  12645. }
  12646. default : {
  12647. $this->lisymbol = '';
  12648. }
  12649. }
  12650. }
  12651. /**
  12652. * Set the booklet mode for double-sided pages.
  12653. * @param boolean $booklet true set the booklet mode on, fals eotherwise.
  12654. * @param float $inner Inner page margin.
  12655. * @param float $outer Outer page margin.
  12656. * @access public
  12657. * @since 4.2.000 (2008-10-29)
  12658. */
  12659. public function SetBooklet($booklet=true, $inner=-1, $outer=-1) {
  12660. $this->booklet = $booklet;
  12661. if ($inner >= 0) {
  12662. $this->lMargin = $inner;
  12663. }
  12664. if ($outer >= 0) {
  12665. $this->rMargin = $outer;
  12666. }
  12667. }
  12668. /**
  12669. * Swap the left and right margins.
  12670. * @param boolean $reverse if true swap left and right margins.
  12671. * @access protected
  12672. * @since 4.2.000 (2008-10-29)
  12673. */
  12674. protected function swapMargins($reverse=true) {
  12675. if ($reverse) {
  12676. // swap left and right margins
  12677. $mtemp = $this->original_lMargin;
  12678. $this->original_lMargin = $this->original_rMargin;
  12679. $this->original_rMargin = $mtemp;
  12680. $deltam = $this->original_lMargin - $this->original_rMargin;
  12681. $this->lMargin += $deltam;
  12682. $this->rMargin -= $deltam;
  12683. }
  12684. }
  12685. /**
  12686. * Set the vertical spaces for HTML tags.
  12687. * The array must have the following structure (example):
  12688. * $tagvs = array('h1' => array(0 => array('h' => '', 'n' => 2), 1 => array('h' => 1.3, 'n' => 1)));
  12689. * The first array level contains the tag names,
  12690. * the second level contains 0 for opening tags or 1 for closing tags,
  12691. * the third level contains the vertical space unit (h) and the number spaces to add (n).
  12692. * If the h parameter is not specified, default values are used.
  12693. * @param array $tagvs array of tags and relative vertical spaces.
  12694. * @access public
  12695. * @since 4.2.001 (2008-10-30)
  12696. */
  12697. public function setHtmlVSpace($tagvs) {
  12698. $this->tagvspaces = $tagvs;
  12699. }
  12700. /**
  12701. * Set custom width for list indentation.
  12702. * @param float $width width of the indentation. Use negative value to disable it.
  12703. * @access public
  12704. * @since 4.2.007 (2008-11-12)
  12705. */
  12706. public function setListIndentWidth($width) {
  12707. return $this->customlistindent = floatval($width);
  12708. }
  12709. /**
  12710. * Set the top/bottom cell sides to be open or closed when the cell cross the page.
  12711. * @param boolean $isopen if true keeps the top/bottom border open for the cell sides that cross the page.
  12712. * @access public
  12713. * @since 4.2.010 (2008-11-14)
  12714. */
  12715. public function setOpenCell($isopen) {
  12716. $this->opencell = $isopen;
  12717. }
  12718. /**
  12719. * Set the color and font style for HTML links.
  12720. * @param array $color RGB array of colors
  12721. * @param string $fontstyle additional font styles to add
  12722. * @access public
  12723. * @since 4.4.003 (2008-12-09)
  12724. */
  12725. public function setHtmlLinksStyle($color=array(0,0,255), $fontstyle='U') {
  12726. $this->htmlLinkColorArray = $color;
  12727. $this->htmlLinkFontStyle = $fontstyle;
  12728. }
  12729. /**
  12730. * convert html string containing value and unit of measure to user's units or points.
  12731. * @param string $htmlval string containing values and unit
  12732. * @param string $refsize reference value in points
  12733. * @param string $defaultunit default unit (can be one of the following: %, em, ex, px, in, mm, pc, pt).
  12734. * @param boolean $point if true returns points, otherwise returns value in user's units
  12735. * @return float value in user's unit or point if $points=true
  12736. * @access public
  12737. * @since 4.4.004 (2008-12-10)
  12738. */
  12739. public function getHTMLUnitToUnits($htmlval, $refsize=1, $defaultunit='px', $points=false) {
  12740. $supportedunits = array('%', 'em', 'ex', 'px', 'in', 'cm', 'mm', 'pc', 'pt');
  12741. $retval = 0;
  12742. $value = 0;
  12743. $unit = 'px';
  12744. $k = $this->k;
  12745. if ($points) {
  12746. $k = 1;
  12747. }
  12748. if (in_array($defaultunit, $supportedunits)) {
  12749. $unit = $defaultunit;
  12750. }
  12751. if (is_numeric($htmlval)) {
  12752. $value = floatval($htmlval);
  12753. } elseif (preg_match('/([0-9\.]+)/', $htmlval, $mnum)) {
  12754. $value = floatval($mnum[1]);
  12755. if (preg_match('/([a-z%]+)/', $htmlval, $munit)) {
  12756. if (in_array($munit[1], $supportedunits)) {
  12757. $unit = $munit[1];
  12758. }
  12759. }
  12760. }
  12761. switch ($unit) {
  12762. // percentage
  12763. case '%': {
  12764. $retval = (($value * $refsize) / 100);
  12765. break;
  12766. }
  12767. // relative-size
  12768. case 'em': {
  12769. $retval = ($value * $refsize);
  12770. break;
  12771. }
  12772. case 'ex': {
  12773. $retval = $value * ($refsize / 2);
  12774. break;
  12775. }
  12776. // absolute-size
  12777. case 'in': {
  12778. $retval = ($value * $this->dpi) / $k;
  12779. break;
  12780. }
  12781. case 'cm': {
  12782. $retval = ($value / 2.54 * $this->dpi) / $k;
  12783. break;
  12784. }
  12785. case 'mm': {
  12786. $retval = ($value / 25.4 * $this->dpi) / $k;
  12787. break;
  12788. }
  12789. case 'pc': {
  12790. // one pica is 12 points
  12791. $retval = ($value * 12) / $k;
  12792. break;
  12793. }
  12794. case 'pt': {
  12795. $retval = $value / $k;
  12796. break;
  12797. }
  12798. case 'px': {
  12799. $retval = $this->pixelsToUnits($value);
  12800. break;
  12801. }
  12802. }
  12803. return $retval;
  12804. }
  12805. /**
  12806. * Returns the Roman representation of an integer number
  12807. * @param int number to convert
  12808. * @return string roman representation of the specified number
  12809. * @access public
  12810. * @since 4.4.004 (2008-12-10)
  12811. */
  12812. public function intToRoman($number) {
  12813. $roman = '';
  12814. while ($number >= 1000) {
  12815. $roman .= 'M';
  12816. $number -= 1000;
  12817. }
  12818. while ($number >= 900) {
  12819. $roman .= 'CM';
  12820. $number -= 900;
  12821. }
  12822. while ($number >= 500) {
  12823. $roman .= 'D';
  12824. $number -= 500;
  12825. }
  12826. while ($number >= 400) {
  12827. $roman .= 'CD';
  12828. $number -= 400;
  12829. }
  12830. while ($number >= 100) {
  12831. $roman .= 'C';
  12832. $number -= 100;
  12833. }
  12834. while ($number >= 90) {
  12835. $roman .= 'XC';
  12836. $number -= 90;
  12837. }
  12838. while ($number >= 50) {
  12839. $roman .= 'L';
  12840. $number -= 50;
  12841. }
  12842. while ($number >= 40) {
  12843. $roman .= 'XL';
  12844. $number -= 40;
  12845. }
  12846. while ($number >= 10) {
  12847. $roman .= 'X';
  12848. $number -= 10;
  12849. }
  12850. while ($number >= 9) {
  12851. $roman .= 'IX';
  12852. $number -= 9;
  12853. }
  12854. while ($number >= 5) {
  12855. $roman .= 'V';
  12856. $number -= 5;
  12857. }
  12858. while ($number >= 4) {
  12859. $roman .= 'IV';
  12860. $number -= 4;
  12861. }
  12862. while ($number >= 1) {
  12863. $roman .= 'I';
  12864. --$number;
  12865. }
  12866. return $roman;
  12867. }
  12868. /**
  12869. * Output an HTML list bullet or ordered item symbol
  12870. * @param int $listdepth list nesting level
  12871. * @param string $listtype type of list
  12872. * @param float $size current font size
  12873. * @access protected
  12874. * @since 4.4.004 (2008-12-10)
  12875. */
  12876. protected function putHtmlListBullet($listdepth, $listtype='', $size=10) {
  12877. $size /= $this->k;
  12878. $fill = '';
  12879. $color = $this->fgcolor;
  12880. $width = 0;
  12881. $textitem = '';
  12882. $tmpx = $this->x;
  12883. $lspace = $this->GetStringWidth(' ');
  12884. if ($listtype == '!') {
  12885. // set default list type for unordered list
  12886. $deftypes = array('disc', 'circle', 'square');
  12887. $listtype = $deftypes[($listdepth - 1) % 3];
  12888. } elseif ($listtype == '#') {
  12889. // set default list type for ordered list
  12890. $listtype = 'decimal';
  12891. }
  12892. switch ($listtype) {
  12893. // unordered types
  12894. case 'none': {
  12895. break;
  12896. }
  12897. case 'disc': {
  12898. $fill = 'F';
  12899. }
  12900. case 'circle': {
  12901. $fill .= 'D';
  12902. $r = $size / 6;
  12903. $lspace += (2 * $r);
  12904. if ($this->rtl) {
  12905. $this->x = $this->w - $this->x - $lspace;
  12906. } else {
  12907. $this->x -= $lspace;
  12908. }
  12909. $this->Circle(($this->x + $r), ($this->y + ($this->lasth / 2)), $r, 0, 360, $fill, array('color'=>$color), $color, 8);
  12910. break;
  12911. }
  12912. case 'square': {
  12913. $l = $size / 3;
  12914. $lspace += $l;
  12915. if ($this->rtl) {
  12916. $this->x = $this->w - $this->x - $lspace;
  12917. } else {
  12918. $this->x -= $lspace;
  12919. }
  12920. $this->Rect($this->x, ($this->y + (($this->lasth - $l)/ 2)), $l, $l, 'F', array(), $color);
  12921. break;
  12922. }
  12923. // ordered types
  12924. // $this->listcount[$this->listnum];
  12925. // $textitem
  12926. case '1':
  12927. case 'decimal': {
  12928. $textitem = $this->listcount[$this->listnum];
  12929. break;
  12930. }
  12931. case 'decimal-leading-zero': {
  12932. $textitem = sprintf("%02d", $this->listcount[$this->listnum]);
  12933. break;
  12934. }
  12935. case 'i':
  12936. case 'lower-roman': {
  12937. $textitem = strtolower($this->intToRoman($this->listcount[$this->listnum]));
  12938. break;
  12939. }
  12940. case 'I':
  12941. case 'upper-roman': {
  12942. $textitem = $this->intToRoman($this->listcount[$this->listnum]);
  12943. break;
  12944. }
  12945. case 'a':
  12946. case 'lower-alpha':
  12947. case 'lower-latin': {
  12948. $textitem = chr(97 + $this->listcount[$this->listnum] - 1);
  12949. break;
  12950. }
  12951. case 'A':
  12952. case 'upper-alpha':
  12953. case 'upper-latin': {
  12954. $textitem = chr(65 + $this->listcount[$this->listnum] - 1);
  12955. break;
  12956. }
  12957. case 'lower-greek': {
  12958. $textitem = $this->unichr(945 + $this->listcount[$this->listnum] - 1);
  12959. break;
  12960. }
  12961. /*
  12962. // Types to be implemented (special handling)
  12963. case 'hebrew': {
  12964. break;
  12965. }
  12966. case 'armenian': {
  12967. break;
  12968. }
  12969. case 'georgian': {
  12970. break;
  12971. }
  12972. case 'cjk-ideographic': {
  12973. break;
  12974. }
  12975. case 'hiragana': {
  12976. break;
  12977. }
  12978. case 'katakana': {
  12979. break;
  12980. }
  12981. case 'hiragana-iroha': {
  12982. break;
  12983. }
  12984. case 'katakana-iroha': {
  12985. break;
  12986. }
  12987. */
  12988. default: {
  12989. $textitem = $this->listcount[$this->listnum];
  12990. }
  12991. }
  12992. if (!$this->empty_string($textitem)) {
  12993. // print ordered item
  12994. if ($this->rtl) {
  12995. $textitem = '.'.$textitem;
  12996. } else {
  12997. $textitem = $textitem.'.';
  12998. }
  12999. $lspace += $this->GetStringWidth($textitem);
  13000. if ($this->rtl) {
  13001. $this->x += $lspace;
  13002. } else {
  13003. $this->x -= $lspace;
  13004. }
  13005. $this->Write($this->lasth, $textitem, '', false, '', false, 0, false);
  13006. }
  13007. $this->x = $tmpx;
  13008. $this->lispacer = '';
  13009. }
  13010. /**
  13011. * Returns current graphic variables as array.
  13012. * @return array graphic variables
  13013. * @access protected
  13014. * @since 4.2.010 (2008-11-14)
  13015. */
  13016. protected function getGraphicVars() {
  13017. $grapvars = array(
  13018. 'FontFamily' => $this->FontFamily,
  13019. 'FontStyle' => $this->FontStyle,
  13020. 'FontSizePt' => $this->FontSizePt,
  13021. 'rMargin' => $this->rMargin,
  13022. 'lMargin' => $this->lMargin,
  13023. 'cMargin' => $this->cMargin,
  13024. 'LineWidth' => $this->LineWidth,
  13025. 'linestyleWidth' => $this->linestyleWidth,
  13026. 'linestyleCap' => $this->linestyleCap,
  13027. 'linestyleJoin' => $this->linestyleJoin,
  13028. 'linestyleDash' => $this->linestyleDash,
  13029. 'DrawColor' => $this->DrawColor,
  13030. 'FillColor' => $this->FillColor,
  13031. 'TextColor' => $this->TextColor,
  13032. 'ColorFlag' => $this->ColorFlag,
  13033. 'bgcolor' => $this->bgcolor,
  13034. 'fgcolor' => $this->fgcolor,
  13035. 'htmlvspace' => $this->htmlvspace,
  13036. 'lasth' => $this->lasth
  13037. );
  13038. return $grapvars;
  13039. }
  13040. /**
  13041. * Set graphic variables.
  13042. * @param $gvars array graphic variables
  13043. * @access protected
  13044. * @since 4.2.010 (2008-11-14)
  13045. */
  13046. protected function setGraphicVars($gvars) {
  13047. $this->FontFamily = $gvars['FontFamily'];
  13048. $this->FontStyle = $gvars['FontStyle'];
  13049. $this->FontSizePt = $gvars['FontSizePt'];
  13050. $this->rMargin = $gvars['rMargin'];
  13051. $this->lMargin = $gvars['lMargin'];
  13052. $this->cMargin = $gvars['cMargin'];
  13053. $this->LineWidth = $gvars['LineWidth'];
  13054. $this->linestyleWidth = $gvars['linestyleWidth'];
  13055. $this->linestyleCap = $gvars['linestyleCap'];
  13056. $this->linestyleJoin = $gvars['linestyleJoin'];
  13057. $this->linestyleDash = $gvars['linestyleDash'];
  13058. $this->DrawColor = $gvars['DrawColor'];
  13059. $this->FillColor = $gvars['FillColor'];
  13060. $this->TextColor = $gvars['TextColor'];
  13061. $this->ColorFlag = $gvars['ColorFlag'];
  13062. $this->bgcolor = $gvars['bgcolor'];
  13063. $this->fgcolor = $gvars['fgcolor'];
  13064. $this->htmlvspace = $gvars['htmlvspace'];
  13065. //$this->lasth = $gvars['lasth'];
  13066. $this->_out(''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor.'');
  13067. if (!$this->empty_string($this->FontFamily)) {
  13068. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  13069. }
  13070. }
  13071. /**
  13072. * Returns a temporary filename for caching object on filesystem.
  13073. * @param string $prefix prefix to add to filename
  13074. * return string filename.
  13075. * @access protected
  13076. * @since 4.5.000 (2008-12-31)
  13077. */
  13078. protected function getObjFilename($name) {
  13079. return tempnam(K_PATH_CACHE, $name.'_');
  13080. }
  13081. /**
  13082. * Writes data to a temporary file on filesystem.
  13083. * @param string $file file name
  13084. * @param mixed $data data to write on file
  13085. * @param boolean $append if true append data, false replace.
  13086. * @access protected
  13087. * @since 4.5.000 (2008-12-31)
  13088. */
  13089. protected function writeDiskCache($filename, $data, $append=false) {
  13090. if ($append) {
  13091. $fmode = 'ab+';
  13092. } else {
  13093. $fmode = 'wb+';
  13094. }
  13095. $f = @fopen($filename, $fmode);
  13096. if (!$f) {
  13097. $this->Error('Unable to write cache file: '.$filename);
  13098. } else {
  13099. fwrite($f, $data);
  13100. fclose($f);
  13101. }
  13102. // update file lenght (needed for transactions)
  13103. if (!isset($this->cache_file_lenght['_'.$filename])) {
  13104. $this->cache_file_lenght['_'.$filename] = strlen($data);
  13105. } else {
  13106. $this->cache_file_lenght['_'.$filename] += strlen($data);
  13107. }
  13108. }
  13109. /**
  13110. * Read data from a temporary file on filesystem.
  13111. * @param string $file file name
  13112. * @return mixed retrieved data
  13113. * @access protected
  13114. * @since 4.5.000 (2008-12-31)
  13115. */
  13116. protected function readDiskCache($filename) {
  13117. return file_get_contents($filename);
  13118. }
  13119. /**
  13120. * Set buffer content (always append data).
  13121. * @param string $data data
  13122. * @access protected
  13123. * @since 4.5.000 (2009-01-02)
  13124. */
  13125. protected function setBuffer($data) {
  13126. $this->bufferlen += strlen($data);
  13127. if ($this->diskcache) {
  13128. if (!isset($this->buffer) OR $this->empty_string($this->buffer)) {
  13129. $this->buffer = $this->getObjFilename('buffer');
  13130. }
  13131. $this->writeDiskCache($this->buffer, $data, true);
  13132. } else {
  13133. $this->buffer .= $data;
  13134. }
  13135. }
  13136. /**
  13137. * Get buffer content.
  13138. * @return string buffer content
  13139. * @access protected
  13140. * @since 4.5.000 (2009-01-02)
  13141. */
  13142. protected function getBuffer() {
  13143. if ($this->diskcache) {
  13144. return $this->readDiskCache($this->buffer);
  13145. } else {
  13146. return $this->buffer;
  13147. }
  13148. }
  13149. /**
  13150. * Set page buffer content.
  13151. * @param int $page page number
  13152. * @param string $data page data
  13153. * @param boolean $append if true append data, false replace.
  13154. * @access protected
  13155. * @since 4.5.000 (2008-12-31)
  13156. */
  13157. protected function setPageBuffer($page, $data, $append=false) {
  13158. if ($this->diskcache) {
  13159. if (!isset($this->pages[$page])) {
  13160. $this->pages[$page] = $this->getObjFilename('page'.$page);
  13161. }
  13162. $this->writeDiskCache($this->pages[$page], $data, $append);
  13163. } else {
  13164. if ($append) {
  13165. $this->pages[$page] .= $data;
  13166. } else {
  13167. $this->pages[$page] = $data;
  13168. }
  13169. }
  13170. if ($append AND isset($this->pagelen[$page])) {
  13171. $this->pagelen[$page] += strlen($data);
  13172. } else {
  13173. $this->pagelen[$page] = strlen($data);
  13174. }
  13175. }
  13176. /**
  13177. * Get page buffer content.
  13178. * @param int $page page number
  13179. * @return string page buffer content or false in case of error
  13180. * @access protected
  13181. * @since 4.5.000 (2008-12-31)
  13182. */
  13183. protected function getPageBuffer($page) {
  13184. if ($this->diskcache) {
  13185. return $this->readDiskCache($this->pages[$page]);
  13186. } elseif (isset($this->pages[$page])) {
  13187. return $this->pages[$page];
  13188. }
  13189. return false;
  13190. }
  13191. /**
  13192. * Set image buffer content.
  13193. * @param string $image image key
  13194. * @param array $data image data
  13195. * @access protected
  13196. * @since 4.5.000 (2008-12-31)
  13197. */
  13198. protected function setImageBuffer($image, $data) {
  13199. if ($this->diskcache) {
  13200. if (!isset($this->images[$image])) {
  13201. $this->images[$image