/htdocs/includes/tcpdf/tcpdf.php

https://bitbucket.org/speedealing/speedealing · PHP · 22699 lines · 15053 code · 695 blank · 6951 comment · 2903 complexity · 9193fd2ec123c86131319bf50134a328 MD5 · raw file

  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.203
  5. // Begin : 2002-08-03
  6. // Last Update : 2013-01-22
  7. // Author : Nicola Asuni - Tecnick.com LTD - Manor Coach House, Church Hill, Aldershot, Hants, GU12 4RQ, UK - www.tecnick.com - info@tecnick.com
  8. // License : http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT GNU-LGPLv3
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2013 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the License
  25. // along with TCPDF. If not, see
  26. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  27. //
  28. // See LICENSE.TXT file for more information.
  29. // -------------------------------------------------------------------
  30. //
  31. // Description :
  32. // This is a PHP class for generating PDF documents without requiring external extensions.
  33. //
  34. // NOTE:
  35. // This class was originally derived in 2002 from the Public
  36. // Domain FPDF class by Olivier Plathey (http://www.fpdf.org),
  37. // but now is almost entirely rewritten and contains thousands of
  38. // new lines of code and hundreds new features.
  39. //
  40. // Main features:
  41. // * no external libraries are required for the basic functions;
  42. // * all standard page formats, custom page formats, custom margins and units of measure;
  43. // * UTF-8 Unicode and Right-To-Left languages;
  44. // * TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;
  45. // * font subsetting;
  46. // * methods to publish some XHTML + CSS code, Javascript and Forms;
  47. // * images, graphic (geometric figures) and transformation methods;
  48. // * supports JPEG, PNG and SVG 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)
  49. // * 1D and 2D barcodes: 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, Datamatrix, QR-Code, PDF417;
  50. // * JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;
  51. // * automatic page header and footer management;
  52. // * document encryption up to 256 bit and digital signature certifications;
  53. // * transactions to UNDO commands;
  54. // * PDF annotations, including links, text and file attachments;
  55. // * text rendering modes (fill, stroke and clipping);
  56. // * multiple columns mode;
  57. // * no-write page regions;
  58. // * bookmarks, named destinations and table of content;
  59. // * text hyphenation;
  60. // * text stretching and spacing (tracking);
  61. // * automatic page break, line break and text alignments including justification;
  62. // * automatic page numbering and page groups;
  63. // * move and delete pages;
  64. // * page compression (requires php-zlib extension);
  65. // * XOBject Templates;
  66. // * Layers and object visibility.
  67. // * PDF/A-1b support.
  68. //
  69. // -----------------------------------------------------------
  70. // THANKS TO:
  71. //
  72. // Olivier Plathey (http://www.fpdf.org) for original FPDF.
  73. // Efthimios Mavrogeorgiadis (emavro@yahoo.com) for suggestions on RTL language support.
  74. // Klemen Vodopivec (http://www.fpdf.de/downloads/addons/37/) for Encryption algorithm.
  75. // Warren Sherliker (wsherliker@gmail.com) for better image handling.
  76. // dullus for text Justification.
  77. // Bob Vincent (pillarsdotnet@users.sourceforge.net) for <li> value attribute.
  78. // Patrick Benny for text stretch suggestion on Cell().
  79. // Johannes G�ntert for JavaScript support.
  80. // Denis Van Nuffelen for Dynamic Form.
  81. // Jacek Czekaj for multibyte justification
  82. // Anthony Ferrara for the reintroduction of legacy image methods.
  83. // Sourceforge user 1707880 (hucste) for line-trough mode.
  84. // Larry Stanbery for page groups.
  85. // Martin Hall-May for transparency.
  86. // Aaron C. Spike for Polycurve method.
  87. // Mohamad Ali Golkar, Saleh AlMatrafe, Charles Abbott for Arabic and Persian support.
  88. // Moritz Wagner and Andreas Wurmser for graphic functions.
  89. // Andrew Whitehead for core fonts support.
  90. // Esteban Jo�l Mar�n for OpenType font conversion.
  91. // Teus Hagen for several suggestions and fixes.
  92. // Yukihiro Nakadaira for CID-0 CJK fonts fixes.
  93. // Kosmas Papachristos for some CSS improvements.
  94. // Marcel Partap for some fixes.
  95. // Won Kyu Park for several suggestions, fixes and patches.
  96. // Dominik Dzienia for QR-code support.
  97. // Laurent Minguet for some suggestions.
  98. // Christian Deligant for some suggestions and fixes.
  99. // Travis Harris for crop mark suggestion.
  100. // Aleksey Kuznetsov for some suggestions and text shadows.
  101. // Jim Hanlon for several suggestions and patches.
  102. // Anyone else that has reported a bug or sent a suggestion.
  103. //============================================================+
  104. /**
  105. * @file
  106. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  107. * 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>
  108. * <h3>TCPDF main features are:</h3>
  109. * <ul>
  110. * <li>no external libraries are required for the basic functions;</li>
  111. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  112. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  113. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  114. * <li>font subsetting;</li>
  115. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  116. * <li>images, graphic (geometric figures) and transformation methods;
  117. * <li>supports JPEG, PNG and SVG 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>
  118. * <li>1D and 2D barcodes: 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, Datamatrix, QR-Code, PDF417;</li>
  119. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  120. * <li>automatic page header and footer management;</li>
  121. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  122. * <li>transactions to UNDO commands;</li>
  123. * <li>PDF annotations, including links, text and file attachments;</li>
  124. * <li>text rendering modes (fill, stroke and clipping);</li>
  125. * <li>multiple columns mode;</li>
  126. * <li>no-write page regions;</li>
  127. * <li>bookmarks, named destinations and table of content;</li>
  128. * <li>text hyphenation;</li>
  129. * <li>text stretching and spacing (tracking);</li>
  130. * <li>automatic page break, line break and text alignments including justification;</li>
  131. * <li>automatic page numbering and page groups;</li>
  132. * <li>move and delete pages;</li>
  133. * <li>page compression (requires php-zlib extension);</li>
  134. * <li>XOBject Templates;</li>
  135. * <li>Layers and object visibility;</li>
  136. * <li>PDF/A-1b support.</li>
  137. * </ul>
  138. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  139. * @package com.tecnick.tcpdf
  140. * @author Nicola Asuni
  141. * @version 5.9.203
  142. */
  143. // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
  144. require_once(dirname(__FILE__) . '/config/tcpdf_config.php');
  145. /**
  146. * @class TCPDF
  147. * PHP class for generating PDF documents without requiring external extensions.
  148. * 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>
  149. * @package com.tecnick.tcpdf
  150. * @brief PHP class for generating PDF documents without requiring external extensions.
  151. * @version 5.9.203
  152. * @author Nicola Asuni - info@tecnick.com
  153. */
  154. class TCPDF {
  155. // private properties
  156. /**
  157. * Current TCPDF version.
  158. * @private
  159. */
  160. private $tcpdf_version = '5.9.203';
  161. // Protected properties
  162. /**
  163. * Current page number.
  164. * @protected
  165. */
  166. protected $page;
  167. /**
  168. * Current object number.
  169. * @protected
  170. */
  171. protected $n;
  172. /**
  173. * Array of object offsets.
  174. * @protected
  175. */
  176. protected $offsets = array();
  177. /**
  178. * Array of object IDs for each page.
  179. * @protected
  180. */
  181. protected $pageobjects = array();
  182. /**
  183. * Buffer holding in-memory PDF.
  184. * @protected
  185. */
  186. protected $buffer;
  187. /**
  188. * Array containing pages.
  189. * @protected
  190. */
  191. protected $pages = array();
  192. /**
  193. * Current document state.
  194. * @protected
  195. */
  196. protected $state;
  197. /**
  198. * Compression flag.
  199. * @protected
  200. */
  201. protected $compress;
  202. /**
  203. * Current page orientation (P = Portrait, L = Landscape).
  204. * @protected
  205. */
  206. protected $CurOrientation;
  207. /**
  208. * Page dimensions.
  209. * @protected
  210. */
  211. protected $pagedim = array();
  212. /**
  213. * Scale factor (number of points in user unit).
  214. * @protected
  215. */
  216. protected $k;
  217. /**
  218. * Width of page format in points.
  219. * @protected
  220. */
  221. protected $fwPt;
  222. /**
  223. * Height of page format in points.
  224. * @protected
  225. */
  226. protected $fhPt;
  227. /**
  228. * Current width of page in points.
  229. * @protected
  230. */
  231. protected $wPt;
  232. /**
  233. * Current height of page in points.
  234. * @protected
  235. */
  236. protected $hPt;
  237. /**
  238. * Current width of page in user unit.
  239. * @protected
  240. */
  241. protected $w;
  242. /**
  243. * Current height of page in user unit.
  244. * @protected
  245. */
  246. protected $h;
  247. /**
  248. * Left margin.
  249. * @protected
  250. */
  251. protected $lMargin;
  252. /**
  253. * Right margin.
  254. * @protected
  255. */
  256. protected $rMargin;
  257. /**
  258. * Cell left margin (used by regions).
  259. * @protected
  260. */
  261. protected $clMargin;
  262. /**
  263. * Cell right margin (used by regions).
  264. * @protected
  265. */
  266. protected $crMargin;
  267. /**
  268. * Top margin.
  269. * @protected
  270. */
  271. protected $tMargin;
  272. /**
  273. * Page break margin.
  274. * @protected
  275. */
  276. protected $bMargin;
  277. /**
  278. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  279. * @since 5.9.000 (2010-10-03)
  280. * @protected
  281. */
  282. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  283. /**
  284. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  285. * @since 5.9.000 (2010-10-04)
  286. * @protected
  287. */
  288. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  289. /**
  290. * Current horizontal position in user unit for cell positioning.
  291. * @protected
  292. */
  293. protected $x;
  294. /**
  295. * Current vertical position in user unit for cell positioning.
  296. * @protected
  297. */
  298. protected $y;
  299. /**
  300. * Height of last cell printed.
  301. * @protected
  302. */
  303. protected $lasth;
  304. /**
  305. * Line width in user unit.
  306. * @protected
  307. */
  308. protected $LineWidth;
  309. /**
  310. * Array of standard font names.
  311. * @protected
  312. */
  313. protected $CoreFonts;
  314. /**
  315. * Array of used fonts.
  316. * @protected
  317. */
  318. protected $fonts = array();
  319. /**
  320. * Array of font files.
  321. * @protected
  322. */
  323. protected $FontFiles = array();
  324. /**
  325. * Array of encoding differences.
  326. * @protected
  327. */
  328. protected $diffs = array();
  329. /**
  330. * Array of used images.
  331. * @protected
  332. */
  333. protected $images = array();
  334. /**
  335. * Array of cached files.
  336. * @protected
  337. */
  338. protected $cached_files = array();
  339. /**
  340. * Array of Annotations in pages.
  341. * @protected
  342. */
  343. protected $PageAnnots = array();
  344. /**
  345. * Array of internal links.
  346. * @protected
  347. */
  348. protected $links = array();
  349. /**
  350. * Current font family.
  351. * @protected
  352. */
  353. protected $FontFamily;
  354. /**
  355. * Current font style.
  356. * @protected
  357. */
  358. protected $FontStyle;
  359. /**
  360. * Current font ascent (distance between font top and baseline).
  361. * @protected
  362. * @since 2.8.000 (2007-03-29)
  363. */
  364. protected $FontAscent;
  365. /**
  366. * Current font descent (distance between font bottom and baseline).
  367. * @protected
  368. * @since 2.8.000 (2007-03-29)
  369. */
  370. protected $FontDescent;
  371. /**
  372. * Underlining flag.
  373. * @protected
  374. */
  375. protected $underline;
  376. /**
  377. * Overlining flag.
  378. * @protected
  379. */
  380. protected $overline;
  381. /**
  382. * Current font info.
  383. * @protected
  384. */
  385. protected $CurrentFont;
  386. /**
  387. * Current font size in points.
  388. * @protected
  389. */
  390. protected $FontSizePt;
  391. /**
  392. * Current font size in user unit.
  393. * @protected
  394. */
  395. protected $FontSize;
  396. /**
  397. * Commands for drawing color.
  398. * @protected
  399. */
  400. protected $DrawColor;
  401. /**
  402. * Commands for filling color.
  403. * @protected
  404. */
  405. protected $FillColor;
  406. /**
  407. * Commands for text color.
  408. * @protected
  409. */
  410. protected $TextColor;
  411. /**
  412. * Indicates whether fill and text colors are different.
  413. * @protected
  414. */
  415. protected $ColorFlag;
  416. /**
  417. * Automatic page breaking.
  418. * @protected
  419. */
  420. protected $AutoPageBreak;
  421. /**
  422. * Threshold used to trigger page breaks.
  423. * @protected
  424. */
  425. protected $PageBreakTrigger;
  426. /**
  427. * Flag set when processing page header.
  428. * @protected
  429. */
  430. protected $InHeader = false;
  431. /**
  432. * Flag set when processing page footer.
  433. * @protected
  434. */
  435. protected $InFooter = false;
  436. /**
  437. * Zoom display mode.
  438. * @protected
  439. */
  440. protected $ZoomMode;
  441. /**
  442. * Layout display mode.
  443. * @protected
  444. */
  445. protected $LayoutMode;
  446. /**
  447. * If true set the document information dictionary in Unicode.
  448. * @protected
  449. */
  450. protected $docinfounicode = true;
  451. /**
  452. * Document title.
  453. * @protected
  454. */
  455. protected $title = '';
  456. /**
  457. * Document subject.
  458. * @protected
  459. */
  460. protected $subject = '';
  461. /**
  462. * Document author.
  463. * @protected
  464. */
  465. protected $author = '';
  466. /**
  467. * Document keywords.
  468. * @protected
  469. */
  470. protected $keywords = '';
  471. /**
  472. * Document creator.
  473. * @protected
  474. */
  475. protected $creator = '';
  476. /**
  477. * Starting page number.
  478. * @protected
  479. */
  480. protected $starting_page_number = 1;
  481. /**
  482. * String alias for total number of pages.
  483. * @protected
  484. */
  485. protected $alias_tot_pages = '{:ptp:}';
  486. /**
  487. * String alias for page number.
  488. * @protected
  489. */
  490. protected $alias_num_page = '{:pnp:}';
  491. /**
  492. * String alias for total number of pages in a single group.
  493. * @protected
  494. */
  495. protected $alias_group_tot_pages = '{:ptg:}';
  496. /**
  497. * String alias for group page number.
  498. * @protected
  499. */
  500. protected $alias_group_num_page = '{:png:}';
  501. /**
  502. * String alias for right shift compensation used to correctly align page numbers on the right.
  503. * @protected
  504. */
  505. protected $alias_right_shift = '{rsc:';
  506. /**
  507. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  508. * @since 2002-07-31
  509. * @author Nicola Asuni
  510. * @protected
  511. */
  512. protected $img_rb_x;
  513. /**
  514. * The right-bottom corner Y coordinate of last inserted image.
  515. * @since 2002-07-31
  516. * @author Nicola Asuni
  517. * @protected
  518. */
  519. protected $img_rb_y;
  520. /**
  521. * Adjusting factor to convert pixels to user units.
  522. * @since 2004-06-14
  523. * @author Nicola Asuni
  524. * @protected
  525. */
  526. protected $imgscale = 1;
  527. /**
  528. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  529. * @since 2005-01-02
  530. * @author Nicola Asuni
  531. * @protected
  532. */
  533. protected $isunicode = false;
  534. /**
  535. * Object containing unicode data.
  536. * @since 5.9.004 (2010-10-18)
  537. * @author Nicola Asuni
  538. * @protected
  539. */
  540. protected $unicode;
  541. /**
  542. * Object containing font encoding maps.
  543. * @since 5.9.123 (2011-10-01)
  544. * @author Nicola Asuni
  545. * @protected
  546. */
  547. protected $encmaps;
  548. /**
  549. * PDF version.
  550. * @since 1.5.3
  551. * @protected
  552. */
  553. protected $PDFVersion = '1.7';
  554. /**
  555. * ID of the stored default header template (-1 = not set).
  556. * @protected
  557. */
  558. protected $header_xobjid = -1;
  559. /**
  560. * If true reset the Header Xobject template at each page
  561. * @protected
  562. */
  563. protected $header_xobj_autoreset = false;
  564. /**
  565. * Minimum distance between header and top page margin.
  566. * @protected
  567. */
  568. protected $header_margin;
  569. /**
  570. * Minimum distance between footer and bottom page margin.
  571. * @protected
  572. */
  573. protected $footer_margin;
  574. /**
  575. * Original left margin value.
  576. * @protected
  577. * @since 1.53.0.TC013
  578. */
  579. protected $original_lMargin;
  580. /**
  581. * Original right margin value.
  582. * @protected
  583. * @since 1.53.0.TC013
  584. */
  585. protected $original_rMargin;
  586. /**
  587. * Default font used on page header.
  588. * @protected
  589. */
  590. protected $header_font;
  591. /**
  592. * Default font used on page footer.
  593. * @protected
  594. */
  595. protected $footer_font;
  596. /**
  597. * Language templates.
  598. * @protected
  599. */
  600. protected $l;
  601. /**
  602. * Barcode to print on page footer (only if set).
  603. * @protected
  604. */
  605. protected $barcode = false;
  606. /**
  607. * Boolean flag to print/hide page header.
  608. * @protected
  609. */
  610. protected $print_header = true;
  611. /**
  612. * Boolean flag to print/hide page footer.
  613. * @protected
  614. */
  615. protected $print_footer = true;
  616. /**
  617. * Header image logo.
  618. * @protected
  619. */
  620. protected $header_logo = '';
  621. /**
  622. * Width of header image logo in user units.
  623. * @protected
  624. */
  625. protected $header_logo_width = 30;
  626. /**
  627. * Title to be printed on default page header.
  628. * @protected
  629. */
  630. protected $header_title = '';
  631. /**
  632. * String to pring on page header after title.
  633. * @protected
  634. */
  635. protected $header_string = '';
  636. /**
  637. * Color for header text (RGB array).
  638. * @since 5.9.174 (2012-07-25)
  639. * @protected
  640. */
  641. protected $header_text_color = array(0, 0, 0);
  642. /**
  643. * Color for header line (RGB array).
  644. * @since 5.9.174 (2012-07-25)
  645. * @protected
  646. */
  647. protected $header_line_color = array(0, 0, 0);
  648. /**
  649. * Color for footer text (RGB array).
  650. * @since 5.9.174 (2012-07-25)
  651. * @protected
  652. */
  653. protected $footer_text_color = array(0, 0, 0);
  654. /**
  655. * Color for footer line (RGB array).
  656. * @since 5.9.174 (2012-07-25)
  657. * @protected
  658. */
  659. protected $footer_line_color = array(0, 0, 0);
  660. /**
  661. * Text shadow data array.
  662. * @since 5.9.174 (2012-07-25)
  663. * @protected
  664. */
  665. protected $txtshadow = array('enabled' => false, 'depth_w' => 0, 'depth_h' => 0, 'color' => false, 'opacity' => 1, 'blend_mode' => 'Normal');
  666. /**
  667. * Default number of columns for html table.
  668. * @protected
  669. */
  670. protected $default_table_columns = 4;
  671. // variables for html parser
  672. /**
  673. * HTML PARSER: array to store current link and rendering styles.
  674. * @protected
  675. */
  676. protected $HREF = array();
  677. /**
  678. * List of available fonts on filesystem.
  679. * @protected
  680. */
  681. protected $fontlist = array();
  682. /**
  683. * Current foreground color.
  684. * @protected
  685. */
  686. protected $fgcolor;
  687. /**
  688. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  689. * @protected
  690. */
  691. protected $listordered = array();
  692. /**
  693. * HTML PARSER: array count list items on nested lists.
  694. * @protected
  695. */
  696. protected $listcount = array();
  697. /**
  698. * HTML PARSER: current list nesting level.
  699. * @protected
  700. */
  701. protected $listnum = 0;
  702. /**
  703. * HTML PARSER: indent amount for lists.
  704. * @protected
  705. */
  706. protected $listindent = 0;
  707. /**
  708. * HTML PARSER: current list indententation level.
  709. * @protected
  710. */
  711. protected $listindentlevel = 0;
  712. /**
  713. * Current background color.
  714. * @protected
  715. */
  716. protected $bgcolor;
  717. /**
  718. * Temporary font size in points.
  719. * @protected
  720. */
  721. protected $tempfontsize = 10;
  722. /**
  723. * Spacer string for LI tags.
  724. * @protected
  725. */
  726. protected $lispacer = '';
  727. /**
  728. * Default encoding.
  729. * @protected
  730. * @since 1.53.0.TC010
  731. */
  732. protected $encoding = 'UTF-8';
  733. /**
  734. * PHP internal encoding.
  735. * @protected
  736. * @since 1.53.0.TC016
  737. */
  738. protected $internal_encoding;
  739. /**
  740. * Boolean flag to indicate if the document language is Right-To-Left.
  741. * @protected
  742. * @since 2.0.000
  743. */
  744. protected $rtl = false;
  745. /**
  746. * Boolean flag used to force RTL or LTR string direction.
  747. * @protected
  748. * @since 2.0.000
  749. */
  750. protected $tmprtl = false;
  751. // --- Variables used for document encryption:
  752. /**
  753. * IBoolean flag indicating whether document is protected.
  754. * @protected
  755. * @since 2.0.000 (2008-01-02)
  756. */
  757. protected $encrypted;
  758. /**
  759. * Array containing encryption settings.
  760. * @protected
  761. * @since 5.0.005 (2010-05-11)
  762. */
  763. protected $encryptdata = array();
  764. /**
  765. * Last RC4 key encrypted (cached for optimisation).
  766. * @protected
  767. * @since 2.0.000 (2008-01-02)
  768. */
  769. protected $last_enc_key;
  770. /**
  771. * Last RC4 computed key.
  772. * @protected
  773. * @since 2.0.000 (2008-01-02)
  774. */
  775. protected $last_enc_key_c;
  776. /**
  777. * Encryption padding string.
  778. * @protected
  779. */
  780. protected $enc_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";
  781. /**
  782. * File ID (used on document trailer).
  783. * @protected
  784. * @since 5.0.005 (2010-05-12)
  785. */
  786. protected $file_id;
  787. // --- bookmark ---
  788. /**
  789. * Outlines for bookmark.
  790. * @protected
  791. * @since 2.1.002 (2008-02-12)
  792. */
  793. protected $outlines = array();
  794. /**
  795. * Outline root for bookmark.
  796. * @protected
  797. * @since 2.1.002 (2008-02-12)
  798. */
  799. protected $OutlineRoot;
  800. // --- javascript and form ---
  801. /**
  802. * Javascript code.
  803. * @protected
  804. * @since 2.1.002 (2008-02-12)
  805. */
  806. protected $javascript = '';
  807. /**
  808. * Javascript counter.
  809. * @protected
  810. * @since 2.1.002 (2008-02-12)
  811. */
  812. protected $n_js;
  813. /**
  814. * line trough state
  815. * @protected
  816. * @since 2.8.000 (2008-03-19)
  817. */
  818. protected $linethrough;
  819. /**
  820. * Array with additional document-wide usage rights for the document.
  821. * @protected
  822. * @since 5.8.014 (2010-08-23)
  823. */
  824. protected $ur = array();
  825. /**
  826. * DPI (Dot Per Inch) Document Resolution (do not change).
  827. * @protected
  828. * @since 3.0.000 (2008-03-27)
  829. */
  830. protected $dpi = 72;
  831. /**
  832. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  833. * @protected
  834. * @since 3.0.000 (2008-03-27)
  835. */
  836. protected $newpagegroup = array();
  837. /**
  838. * Array that contains the number of pages in each page group.
  839. * @protected
  840. * @since 3.0.000 (2008-03-27)
  841. */
  842. protected $pagegroups = array();
  843. /**
  844. * Current page group number.
  845. * @protected
  846. * @since 3.0.000 (2008-03-27)
  847. */
  848. protected $currpagegroup = 0;
  849. /**
  850. * Array of transparency objects and parameters.
  851. * @protected
  852. * @since 3.0.000 (2008-03-27)
  853. */
  854. protected $extgstates;
  855. /**
  856. * Set the default JPEG compression quality (1-100).
  857. * @protected
  858. * @since 3.0.000 (2008-03-27)
  859. */
  860. protected $jpeg_quality;
  861. /**
  862. * Default cell height ratio.
  863. * @protected
  864. * @since 3.0.014 (2008-05-23)
  865. */
  866. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  867. /**
  868. * PDF viewer preferences.
  869. * @protected
  870. * @since 3.1.000 (2008-06-09)
  871. */
  872. protected $viewer_preferences;
  873. /**
  874. * A name object specifying how the document should be displayed when opened.
  875. * @protected
  876. * @since 3.1.000 (2008-06-09)
  877. */
  878. protected $PageMode;
  879. /**
  880. * Array for storing gradient information.
  881. * @protected
  882. * @since 3.1.000 (2008-06-09)
  883. */
  884. protected $gradients = array();
  885. /**
  886. * Array used to store positions inside the pages buffer (keys are the page numbers).
  887. * @protected
  888. * @since 3.2.000 (2008-06-26)
  889. */
  890. protected $intmrk = array();
  891. /**
  892. * Array used to store positions inside the pages buffer (keys are the page numbers).
  893. * @protected
  894. * @since 5.7.000 (2010-08-03)
  895. */
  896. protected $bordermrk = array();
  897. /**
  898. * Array used to store page positions to track empty pages (keys are the page numbers).
  899. * @protected
  900. * @since 5.8.007 (2010-08-18)
  901. */
  902. protected $emptypagemrk = array();
  903. /**
  904. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  905. * @protected
  906. * @since 4.6.021 (2009-07-20)
  907. */
  908. protected $cntmrk = array();
  909. /**
  910. * Array used to store footer positions of each page.
  911. * @protected
  912. * @since 3.2.000 (2008-07-01)
  913. */
  914. protected $footerpos = array();
  915. /**
  916. * Array used to store footer length of each page.
  917. * @protected
  918. * @since 4.0.014 (2008-07-29)
  919. */
  920. protected $footerlen = array();
  921. /**
  922. * Boolean flag to indicate if a new line is created.
  923. * @protected
  924. * @since 3.2.000 (2008-07-01)
  925. */
  926. protected $newline = true;
  927. /**
  928. * End position of the latest inserted line.
  929. * @protected
  930. * @since 3.2.000 (2008-07-01)
  931. */
  932. protected $endlinex = 0;
  933. /**
  934. * PDF string for width value of the last line.
  935. * @protected
  936. * @since 4.0.006 (2008-07-16)
  937. */
  938. protected $linestyleWidth = '';
  939. /**
  940. * PDF string for CAP value of the last line.
  941. * @protected
  942. * @since 4.0.006 (2008-07-16)
  943. */
  944. protected $linestyleCap = '0 J';
  945. /**
  946. * PDF string for join value of the last line.
  947. * @protected
  948. * @since 4.0.006 (2008-07-16)
  949. */
  950. protected $linestyleJoin = '0 j';
  951. /**
  952. * PDF string for dash value of the last line.
  953. * @protected
  954. * @since 4.0.006 (2008-07-16)
  955. */
  956. protected $linestyleDash = '[] 0 d';
  957. /**
  958. * Boolean flag to indicate if marked-content sequence is open.
  959. * @protected
  960. * @since 4.0.013 (2008-07-28)
  961. */
  962. protected $openMarkedContent = false;
  963. /**
  964. * Count the latest inserted vertical spaces on HTML.
  965. * @protected
  966. * @since 4.0.021 (2008-08-24)
  967. */
  968. protected $htmlvspace = 0;
  969. /**
  970. * Array of Spot colors.
  971. * @protected
  972. * @since 4.0.024 (2008-09-12)
  973. */
  974. protected $spot_colors = array();
  975. /**
  976. * Symbol used for HTML unordered list items.
  977. * @protected
  978. * @since 4.0.028 (2008-09-26)
  979. */
  980. protected $lisymbol = '';
  981. /**
  982. * String used to mark the beginning and end of EPS image blocks.
  983. * @protected
  984. * @since 4.1.000 (2008-10-18)
  985. */
  986. protected $epsmarker = 'x#!#EPS#!#x';
  987. /**
  988. * Array of transformation matrix.
  989. * @protected
  990. * @since 4.2.000 (2008-10-29)
  991. */
  992. protected $transfmatrix = array();
  993. /**
  994. * Current key for transformation matrix.
  995. * @protected
  996. * @since 4.8.005 (2009-09-17)
  997. */
  998. protected $transfmatrix_key = 0;
  999. /**
  1000. * Booklet mode for double-sided pages.
  1001. * @protected
  1002. * @since 4.2.000 (2008-10-29)
  1003. */
  1004. protected $booklet = false;
  1005. /**
  1006. * Epsilon value used for float calculations.
  1007. * @protected
  1008. * @since 4.2.000 (2008-10-29)
  1009. */
  1010. protected $feps = 0.005;
  1011. /**
  1012. * Array used for custom vertical spaces for HTML tags.
  1013. * @protected
  1014. * @since 4.2.001 (2008-10-30)
  1015. */
  1016. protected $tagvspaces = array();
  1017. /**
  1018. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  1019. * @protected
  1020. * @since 4.2.007 (2008-11-12)
  1021. */
  1022. protected $customlistindent = -1;
  1023. /**
  1024. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  1025. * @protected
  1026. * @since 4.2.010 (2008-11-14)
  1027. */
  1028. protected $opencell = true;
  1029. /**
  1030. * Array of files to embedd.
  1031. * @protected
  1032. * @since 4.4.000 (2008-12-07)
  1033. */
  1034. protected $embeddedfiles = array();
  1035. /**
  1036. * Boolean flag to indicate if we are inside a PRE tag.
  1037. * @protected
  1038. * @since 4.4.001 (2008-12-08)
  1039. */
  1040. protected $premode = false;
  1041. /**
  1042. * Array used to store positions of graphics transformation blocks inside the page buffer.
  1043. * keys are the page numbers
  1044. * @protected
  1045. * @since 4.4.002 (2008-12-09)
  1046. */
  1047. protected $transfmrk = array();
  1048. /**
  1049. * Default color for html links.
  1050. * @protected
  1051. * @since 4.4.003 (2008-12-09)
  1052. */
  1053. protected $htmlLinkColorArray = array(0, 0, 255);
  1054. /**
  1055. * Default font style to add to html links.
  1056. * @protected
  1057. * @since 4.4.003 (2008-12-09)
  1058. */
  1059. protected $htmlLinkFontStyle = 'U';
  1060. /**
  1061. * Counts the number of pages.
  1062. * @protected
  1063. * @since 4.5.000 (2008-12-31)
  1064. */
  1065. protected $numpages = 0;
  1066. /**
  1067. * Array containing page lengths in bytes.
  1068. * @protected
  1069. * @since 4.5.000 (2008-12-31)
  1070. */
  1071. protected $pagelen = array();
  1072. /**
  1073. * Counts the number of pages.
  1074. * @protected
  1075. * @since 4.5.000 (2008-12-31)
  1076. */
  1077. protected $numimages = 0;
  1078. /**
  1079. * Store the image keys.
  1080. * @protected
  1081. * @since 4.5.000 (2008-12-31)
  1082. */
  1083. protected $imagekeys = array();
  1084. /**
  1085. * Length of the buffer in bytes.
  1086. * @protected
  1087. * @since 4.5.000 (2008-12-31)
  1088. */
  1089. protected $bufferlen = 0;
  1090. /**
  1091. * If true enables disk caching.
  1092. * @protected
  1093. * @since 4.5.000 (2008-12-31)
  1094. */
  1095. protected $diskcache = false;
  1096. /**
  1097. * Counts the number of fonts.
  1098. * @protected
  1099. * @since 4.5.000 (2009-01-02)
  1100. */
  1101. protected $numfonts = 0;
  1102. /**
  1103. * Store the font keys.
  1104. * @protected
  1105. * @since 4.5.000 (2009-01-02)
  1106. */
  1107. protected $fontkeys = array();
  1108. /**
  1109. * Store the font object IDs.
  1110. * @protected
  1111. * @since 4.8.001 (2009-09-09)
  1112. */
  1113. protected $font_obj_ids = array();
  1114. /**
  1115. * Store the fage status (true when opened, false when closed).
  1116. * @protected
  1117. * @since 4.5.000 (2009-01-02)
  1118. */
  1119. protected $pageopen = array();
  1120. /**
  1121. * Default monospace font.
  1122. * @protected
  1123. * @since 4.5.025 (2009-03-10)
  1124. */
  1125. protected $default_monospaced_font = 'courier';
  1126. /**
  1127. * Cloned copy of the current class object.
  1128. * @protected
  1129. * @since 4.5.029 (2009-03-19)
  1130. */
  1131. protected $objcopy;
  1132. /**
  1133. * Array used to store the lengths of cache files.
  1134. * @protected
  1135. * @since 4.5.029 (2009-03-19)
  1136. */
  1137. protected $cache_file_length = array();
  1138. /**
  1139. * Table header content to be repeated on each new page.
  1140. * @protected
  1141. * @since 4.5.030 (2009-03-20)
  1142. */
  1143. protected $thead = '';
  1144. /**
  1145. * Margins used for table header.
  1146. * @protected
  1147. * @since 4.5.030 (2009-03-20)
  1148. */
  1149. protected $theadMargins = array();
  1150. /**
  1151. * Cache array for UTF8StringToArray() method.
  1152. * @protected
  1153. * @since 4.5.037 (2009-04-07)
  1154. */
  1155. protected $cache_UTF8StringToArray = array();
  1156. /**
  1157. * Maximum size of cache array used for UTF8StringToArray() method.
  1158. * @protected
  1159. * @since 4.5.037 (2009-04-07)
  1160. */
  1161. protected $cache_maxsize_UTF8StringToArray = 8;
  1162. /**
  1163. * Current size of cache array used for UTF8StringToArray() method.
  1164. * @protected
  1165. * @since 4.5.037 (2009-04-07)
  1166. */
  1167. protected $cache_size_UTF8StringToArray = 0;
  1168. /**
  1169. * Boolean flag to enable document digital signature.
  1170. * @protected
  1171. * @since 4.6.005 (2009-04-24)
  1172. */
  1173. protected $sign = false;
  1174. /**
  1175. * Digital signature data.
  1176. * @protected
  1177. * @since 4.6.005 (2009-04-24)
  1178. */
  1179. protected $signature_data = array();
  1180. /**
  1181. * Digital signature max length.
  1182. * @protected
  1183. * @since 4.6.005 (2009-04-24)
  1184. */
  1185. protected $signature_max_length = 11742;
  1186. /**
  1187. * Data for digital signature appearance.
  1188. * @protected
  1189. * @since 5.3.011 (2010-06-16)
  1190. */
  1191. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1192. /**
  1193. * Array of empty digital signature appearances.
  1194. * @protected
  1195. * @since 5.9.101 (2011-07-06)
  1196. */
  1197. protected $empty_signature_appearance = array();
  1198. /**
  1199. * Regular expression used to find blank characters (required for word-wrapping).
  1200. * @protected
  1201. * @since 4.6.006 (2009-04-28)
  1202. */
  1203. protected $re_spaces = '/[^\S\xa0]/';
  1204. /**
  1205. * Array of $re_spaces parts.
  1206. * @protected
  1207. * @since 5.5.011 (2010-07-09)
  1208. */
  1209. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1210. /**
  1211. * Digital signature object ID.
  1212. * @protected
  1213. * @since 4.6.022 (2009-06-23)
  1214. */
  1215. protected $sig_obj_id = 0;
  1216. /**
  1217. * ByteRange placemark used during digital signature process.
  1218. * @protected
  1219. * @since 4.6.028 (2009-08-25)
  1220. */
  1221. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1222. /**
  1223. * Placemark used during digital signature process.
  1224. * @protected
  1225. * @since 4.6.028 (2009-08-25)
  1226. */
  1227. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1228. /**
  1229. * ID of page objects.
  1230. * @protected
  1231. * @since 4.7.000 (2009-08-29)
  1232. */
  1233. protected $page_obj_id = array();
  1234. /**
  1235. * List of form annotations IDs.
  1236. * @protected
  1237. * @since 4.8.000 (2009-09-07)
  1238. */
  1239. protected $form_obj_id = array();
  1240. /**
  1241. * Deafult Javascript field properties. Possible values are described on official Javascript for Acrobat API reference. Annotation options can be directly specified using the 'aopt' entry.
  1242. * @protected
  1243. * @since 4.8.000 (2009-09-07)
  1244. */
  1245. protected $default_form_prop = array('lineWidth' => 1, 'borderStyle' => 'solid', 'fillColor' => array(255, 255, 255), 'strokeColor' => array(128, 128, 128));
  1246. /**
  1247. * Javascript objects array.
  1248. * @protected
  1249. * @since 4.8.000 (2009-09-07)
  1250. */
  1251. protected $js_objects = array();
  1252. /**
  1253. * Current form action (used during XHTML rendering).
  1254. * @protected
  1255. * @since 4.8.000 (2009-09-07)
  1256. */
  1257. protected $form_action = '';
  1258. /**
  1259. * Current form encryption type (used during XHTML rendering).
  1260. * @protected
  1261. * @since 4.8.000 (2009-09-07)
  1262. */
  1263. protected $form_enctype = 'application/x-www-form-urlencoded';
  1264. /**
  1265. * Current method to submit forms.
  1266. * @protected
  1267. * @since 4.8.000 (2009-09-07)
  1268. */
  1269. protected $form_mode = 'post';
  1270. /**
  1271. * List of fonts used on form fields (fontname => fontkey).
  1272. * @protected
  1273. * @since 4.8.001 (2009-09-09)
  1274. */
  1275. protected $annotation_fonts = array();
  1276. /**
  1277. * List of radio buttons parent objects.
  1278. * @protected
  1279. * @since 4.8.001 (2009-09-09)
  1280. */
  1281. protected $radiobutton_groups = array();
  1282. /**
  1283. * List of radio group objects IDs.
  1284. * @protected
  1285. * @since 4.8.001 (2009-09-09)
  1286. */
  1287. protected $radio_groups = array();
  1288. /**
  1289. * Text indentation value (used for text-indent CSS attribute).
  1290. * @protected
  1291. * @since 4.8.006 (2009-09-23)
  1292. */
  1293. protected $textindent = 0;
  1294. /**
  1295. * Store page number when startTransaction() is called.
  1296. * @protected
  1297. * @since 4.8.006 (2009-09-23)
  1298. */
  1299. protected $start_transaction_page = 0;
  1300. /**
  1301. * Store Y position when startTransaction() is called.
  1302. * @protected
  1303. * @since 4.9.001 (2010-03-28)
  1304. */
  1305. protected $start_transaction_y = 0;
  1306. /**
  1307. * True when we are printing the thead section on a new page.
  1308. * @protected
  1309. * @since 4.8.027 (2010-01-25)
  1310. */
  1311. protected $inthead = false;
  1312. /**
  1313. * Array of column measures (width, space, starting Y position).
  1314. * @protected
  1315. * @since 4.9.001 (2010-03-28)
  1316. */
  1317. protected $columns = array();
  1318. /**
  1319. * Number of colums.
  1320. * @protected
  1321. * @since 4.9.001 (2010-03-28)
  1322. */
  1323. protected $num_columns = 1;
  1324. /**
  1325. * Current column number.
  1326. * @protected
  1327. * @since 4.9.001 (2010-03-28)
  1328. */
  1329. protected $current_column = 0;
  1330. /**
  1331. * Starting page for columns.
  1332. * @protected
  1333. * @since 4.9.001 (2010-03-28)
  1334. */
  1335. protected $column_start_page = 0;
  1336. /**
  1337. * Maximum page and column selected.
  1338. * @protected
  1339. * @since 5.8.000 (2010-08-11)
  1340. */
  1341. protected $maxselcol = array('page' => 0, 'column' => 0);
  1342. /**
  1343. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1344. * @protected
  1345. * @since 5.8.000 (2010-08-11)
  1346. */
  1347. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1348. /**
  1349. * Text rendering mode: 0 = Fill text; 1 = Stroke text; 2 = Fill, then stroke text; 3 = Neither fill nor stroke text (invisible); 4 = Fill text and add to path for clipping; 5 = Stroke text and add to path for clipping; 6 = Fill, then stroke text and add to path for clipping; 7 = Add text to path for clipping.
  1350. * @protected
  1351. * @since 4.9.008 (2010-04-03)
  1352. */
  1353. protected $textrendermode = 0;
  1354. /**
  1355. * Text stroke width in doc units.
  1356. * @protected
  1357. * @since 4.9.008 (2010-04-03)
  1358. */
  1359. protected $textstrokewidth = 0;
  1360. /**
  1361. * Current stroke color.
  1362. * @protected
  1363. * @since 4.9.008 (2010-04-03)
  1364. */
  1365. protected $strokecolor;
  1366. /**
  1367. * Default unit of measure for document.
  1368. * @protected
  1369. * @since 5.0.000 (2010-04-22)
  1370. */
  1371. protected $pdfunit = 'mm';
  1372. /**
  1373. * Boolean flag true when we are on TOC (Table Of Content) page.
  1374. * @protected
  1375. */
  1376. protected $tocpage = false;
  1377. /**
  1378. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1379. * @protected
  1380. * @since 5.0.000 (2010-04-26)
  1381. */
  1382. protected $rasterize_vector_images = false;
  1383. /**
  1384. * Boolean flag: if true enables font subsetting by default.
  1385. * @protected
  1386. * @since 5.3.002 (2010-06-07)
  1387. */
  1388. protected $font_subsetting = true;
  1389. /**
  1390. * Array of default graphic settings.
  1391. * @protected
  1392. * @since 5.5.008 (2010-07-02)
  1393. */
  1394. protected $default_graphic_vars = array();
  1395. /**
  1396. * Array of XObjects.
  1397. * @protected
  1398. * @since 5.8.014 (2010-08-23)
  1399. */
  1400. protected $xobjects = array();
  1401. /**
  1402. * Boolean value true when we are inside an XObject.
  1403. * @protected
  1404. * @since 5.8.017 (2010-08-24)
  1405. */
  1406. protected $inxobj = false;
  1407. /**
  1408. * Current XObject ID.
  1409. * @protected
  1410. * @since 5.8.017 (2010-08-24)
  1411. */
  1412. protected $xobjid = '';
  1413. /**
  1414. * Percentage of character stretching.
  1415. * @protected
  1416. * @since 5.9.000 (2010-09-29)
  1417. */
  1418. protected $font_stretching = 100;
  1419. /**
  1420. * Increases or decreases the space between characters in a text by the specified amount (tracking).
  1421. * @protected
  1422. * @since 5.9.000 (2010-09-29)
  1423. */
  1424. protected $font_spacing = 0;
  1425. /**
  1426. * Array of no-write regions.
  1427. * ('page' => page number or empy for current page, 'xt' => X top, 'yt' => Y top, 'xb' => X bottom, 'yb' => Y bottom, 'side' => page side 'L' = left or 'R' = right)
  1428. * @protected
  1429. * @since 5.9.003 (2010-10-14)
  1430. */
  1431. protected $page_regions = array();
  1432. /**
  1433. * Boolean value true when page region check is active.
  1434. * @protected
  1435. */
  1436. protected $check_page_regions = true;
  1437. /**
  1438. * Array containing HTML color names and values.
  1439. * @protected
  1440. * @since 5.9.004 (2010-10-18)
  1441. */
  1442. protected $webcolor = array();
  1443. /**
  1444. * Array containing spot color names and values.
  1445. * @protected
  1446. * @since 5.9.012 (2010-11-11)
  1447. */
  1448. protected $spotcolor = array();
  1449. /**
  1450. * Array of PDF layers data.
  1451. * @protected
  1452. * @since 5.9.102 (2011-07-13)
  1453. */
  1454. protected $pdflayers = array();
  1455. /**
  1456. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1457. * @protected
  1458. * @since 5.9.097 (2011-06-23)
  1459. */
  1460. protected $dests = array();
  1461. /**
  1462. * Object ID for Named Destinations
  1463. * @protected
  1464. * @since 5.9.097 (2011-06-23)
  1465. */
  1466. protected $n_dests;
  1467. /**
  1468. * Directory used for the last SVG image.
  1469. * @protected
  1470. * @since 5.0.000 (2010-05-05)
  1471. */
  1472. protected $svgdir = '';
  1473. /**
  1474. * Deafult unit of measure for SVG.
  1475. * @protected
  1476. * @since 5.0.000 (2010-05-02)
  1477. */
  1478. protected $svgunit = 'px';
  1479. /**
  1480. * Array of SVG gradients.
  1481. * @protected
  1482. * @since 5.0.000 (2010-05-02)
  1483. */
  1484. protected $svggradients = array();
  1485. /**
  1486. * ID of last SVG gradient.
  1487. * @protected
  1488. * @since 5.0.000 (2010-05-02)
  1489. */
  1490. protected $svggradientid = 0;
  1491. /**
  1492. * Boolean value true when in SVG defs group.
  1493. * @protected
  1494. * @since 5.0.000 (2010-05-02)
  1495. */
  1496. protected $svgdefsmode = false;
  1497. /**
  1498. * Array of SVG defs.
  1499. * @protected
  1500. * @since 5.0.000 (2010-05-02)
  1501. */
  1502. protected $svgdefs = array();
  1503. /**
  1504. * Boolean value true when in SVG clipPath tag.
  1505. * @protected
  1506. * @since 5.0.000 (2010-04-26)
  1507. */
  1508. protected $svgclipmode = false;
  1509. /**
  1510. * Array of SVG clipPath commands.
  1511. * @protected
  1512. * @since 5.0.000 (2010-05-02)
  1513. */
  1514. protected $svgclippaths = array();
  1515. /**
  1516. * Array of SVG clipPath tranformation matrix.
  1517. * @protected
  1518. * @since 5.8.022 (2010-08-31)
  1519. */
  1520. protected $svgcliptm = array();
  1521. /**
  1522. * ID of last SVG clipPath.
  1523. * @protected
  1524. * @since 5.0.000 (2010-05-02)
  1525. */
  1526. protected $svgclipid = 0;
  1527. /**
  1528. * SVG text.
  1529. * @protected
  1530. * @since 5.0.000 (2010-05-02)
  1531. */
  1532. protected $svgtext = '';
  1533. /**
  1534. * SVG text properties.
  1535. * @protected
  1536. * @since 5.8.013 (2010-08-23)
  1537. */
  1538. protected $svgtextmode = array();
  1539. /**
  1540. * Array of hinheritable SVG properties.
  1541. * @protected
  1542. * @since 5.0.000 (2010-05-02)
  1543. */
  1544. protected $svginheritprop = array('clip-rule', 'color', 'color-interpolation', 'color-interpolation-filters', 'color-profile', 'color-rendering', 'cursor', 'direction', 'fill', 'fill-opacity', 'fill-rule', 'font', 'font-family', 'font-size', 'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-weight', 'glyph-orientation-horizontal', 'glyph-orientation-vertical', 'image-rendering', 'kerning', 'letter-spacing', 'marker', 'marker-end', 'marker-mid', 'marker-start', 'pointer-events', 'shape-rendering', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'text-anchor', 'text-rendering', 'visibility', 'word-spacing', 'writing-mode');
  1545. /**
  1546. * Array of SVG properties.
  1547. * @protected
  1548. * @since 5.0.000 (2010-05-02)
  1549. */
  1550. protected $svgstyles = array(array(
  1551. 'alignment-baseline' => 'auto',
  1552. 'baseline-shift' => 'baseline',
  1553. 'clip' => 'auto',
  1554. 'clip-path' => 'none',
  1555. 'clip-rule' => 'nonzero',
  1556. 'color' => 'black',
  1557. 'color-interpolation' => 'sRGB',
  1558. 'color-interpolation-filters' => 'linearRGB',
  1559. 'color-profile' => 'auto',
  1560. 'color-rendering' => 'auto',
  1561. 'cursor' => 'auto',
  1562. 'direction' => 'ltr',
  1563. 'display' => 'inline',
  1564. 'dominant-baseline' => 'auto',
  1565. 'enable-background' => 'accumulate',
  1566. 'fill' => 'black',
  1567. 'fill-opacity' => 1,
  1568. 'fill-rule' => 'nonzero',
  1569. 'filter' => 'none',
  1570. 'flood-color' => 'black',
  1571. 'flood-opacity' => 1,
  1572. 'font' => '',
  1573. 'font-family' => 'helvetica',
  1574. 'font-size' => 'medium',
  1575. 'font-size-adjust' => 'none',
  1576. 'font-stretch' => 'normal',
  1577. 'font-style' => 'normal',
  1578. 'font-variant' => 'normal',
  1579. 'font-weight' => 'normal',
  1580. 'glyph-orientation-horizontal' => '0deg',
  1581. 'glyph-orientation-vertical' => 'auto',
  1582. 'image-rendering' => 'auto',
  1583. 'kerning' => 'auto',
  1584. 'letter-spacing' => 'normal',
  1585. 'lighting-color' => 'white',
  1586. 'marker' => '',
  1587. 'marker-end' => 'none',
  1588. 'marker-mid' => 'none',
  1589. 'marker-start' => 'none',
  1590. 'mask' => 'none',
  1591. 'opacity' => 1,
  1592. 'overflow' => 'auto',
  1593. 'pointer-events' => 'visiblePainted',
  1594. 'shape-rendering' => 'auto',
  1595. 'stop-color' => 'black',
  1596. 'stop-opacity' => 1,
  1597. 'stroke' => 'none',
  1598. 'stroke-dasharray' => 'none',
  1599. 'stroke-dashoffset' => 0,
  1600. 'stroke-linecap' => 'butt',
  1601. 'stroke-linejoin' => 'miter',
  1602. 'stroke-miterlimit' => 4,
  1603. 'stroke-opacity' => 1,
  1604. 'stroke-width' => 1,
  1605. 'text-anchor' => 'start',
  1606. 'text-decoration' => 'none',
  1607. 'text-rendering' => 'auto',
  1608. 'unicode-bidi' => 'normal',
  1609. 'visibility' => 'visible',
  1610. 'word-spacing' => 'normal',
  1611. 'writing-mode' => 'lr-tb',
  1612. 'text-color' => 'black',
  1613. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1614. ));
  1615. /**
  1616. * If true force sRGB color profile for all document.
  1617. * @protected
  1618. * @since 5.9.121 (2011-09-28)
  1619. */
  1620. protected $force_srgb = false;
  1621. /**
  1622. * If true set the document to PDF/A mode.
  1623. * @protected
  1624. * @since 5.9.121 (2011-09-27)
  1625. */
  1626. protected $pdfa_mode = false;
  1627. /**
  1628. * Document creation date-time
  1629. * @protected
  1630. * @since 5.9.152 (2012-03-22)
  1631. */
  1632. protected $doc_creation_timestamp;
  1633. /**
  1634. * Document modification date-time
  1635. * @protected
  1636. * @since 5.9.152 (2012-03-22)
  1637. */
  1638. protected $doc_modification_timestamp;
  1639. /**
  1640. * Custom XMP data.
  1641. * @protected
  1642. * @since 5.9.128 (2011-10-06)
  1643. */
  1644. protected $custom_xmp = '';
  1645. /**
  1646. * Overprint mode array.
  1647. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1648. * @protected
  1649. * @since 5.9.152 (2012-03-23)
  1650. */
  1651. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1652. /**
  1653. * Alpha mode array.
  1654. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1655. * @protected
  1656. * @since 5.9.152 (2012-03-23)
  1657. */
  1658. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1659. /**
  1660. * Define the page boundaries boxes to be set on document.
  1661. * @protected
  1662. * @since 5.9.152 (2012-03-23)
  1663. */
  1664. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1665. /**
  1666. * Set the document producer metadata.
  1667. * @protected
  1668. * @since 5.9.152 (2012-03-23)
  1669. */
  1670. protected $pdfproducer;
  1671. /**
  1672. * If true print TCPDF meta link.
  1673. * @protected
  1674. * @since 5.9.152 (2012-03-23)
  1675. */
  1676. protected $tcpdflink = true;
  1677. /**
  1678. * Cache array for computed GD gamma values.
  1679. * @protected
  1680. * @since 5.9.1632 (2012-06-05)
  1681. */
  1682. protected $gdgammacache = array();
  1683. //------------------------------------------------------------
  1684. // METHODS
  1685. //------------------------------------------------------------
  1686. /**
  1687. * This is the class constructor.
  1688. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1689. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  1690. * @param $unit (string) 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.
  1691. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  1692. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1693. * @param $encoding (string) Charset encoding; default is UTF-8.
  1694. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1695. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1696. * @public
  1697. * @see getPageSizeFromFormat(), setPageFormat()
  1698. */
  1699. public function __construct($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false) {
  1700. /* Set internal character encoding to ASCII */
  1701. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1702. $this->internal_encoding = mb_internal_encoding();
  1703. mb_internal_encoding('ASCII');
  1704. }
  1705. // get array of HTML colors
  1706. require(dirname(__FILE__) . '/htmlcolors.php');
  1707. $this->webcolor = $webcolor;
  1708. // get array of custom spot colors
  1709. if (file_exists(dirname(__FILE__) . '/spotcolors.php')) {
  1710. require(dirname(__FILE__) . '/spotcolors.php');
  1711. $this->spotcolor = $spotcolor;
  1712. } else {
  1713. $this->spotcolor = array();
  1714. }
  1715. require_once(dirname(__FILE__) . '/unicode_data.php');
  1716. $this->unicode = new TCPDF_UNICODE_DATA();
  1717. require_once(dirname(__FILE__) . '/encodings_maps.php');
  1718. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1719. $this->font_obj_ids = array();
  1720. $this->page_obj_id = array();
  1721. $this->form_obj_id = array();
  1722. // set pdf/a mode
  1723. $this->pdfa_mode = $pdfa;
  1724. $this->force_srgb = false;
  1725. // set disk caching
  1726. $this->diskcache = $diskcache ? true : false;
  1727. // set language direction
  1728. $this->rtl = false;
  1729. $this->tmprtl = false;
  1730. // some checks
  1731. $this->_dochecks();
  1732. // initialization of properties
  1733. $this->isunicode = $unicode;
  1734. $this->page = 0;
  1735. $this->transfmrk[0] = array();
  1736. $this->pagedim = array();
  1737. $this->n = 2;
  1738. $this->buffer = '';
  1739. $this->pages = array();
  1740. $this->state = 0;
  1741. $this->fonts = array();
  1742. $this->FontFiles = array();
  1743. $this->diffs = array();
  1744. $this->images = array();
  1745. $this->links = array();
  1746. $this->gradients = array();
  1747. $this->InFooter = false;
  1748. $this->lasth = 0;
  1749. $this->FontFamily = defined('PDF_FONT_NAME_MAIN') ? PDF_FONT_NAME_MAIN : 'helvetica';
  1750. $this->FontStyle = '';
  1751. $this->FontSizePt = 12;
  1752. $this->underline = false;
  1753. $this->overline = false;
  1754. $this->linethrough = false;
  1755. $this->DrawColor = '0 G';
  1756. $this->FillColor = '0 g';
  1757. $this->TextColor = '0 g';
  1758. $this->ColorFlag = false;
  1759. $this->pdflayers = array();
  1760. // encryption values
  1761. $this->encrypted = false;
  1762. $this->last_enc_key = '';
  1763. // standard Unicode fonts
  1764. $this->CoreFonts = array(
  1765. 'courier' => 'Courier',
  1766. 'courierB' => 'Courier-Bold',
  1767. 'courierI' => 'Courier-Oblique',
  1768. 'courierBI' => 'Courier-BoldOblique',
  1769. 'helvetica' => 'Helvetica',
  1770. 'helveticaB' => 'Helvetica-Bold',
  1771. 'helveticaI' => 'Helvetica-Oblique',
  1772. 'helveticaBI' => 'Helvetica-BoldOblique',
  1773. 'times' => 'Times-Roman',
  1774. 'timesB' => 'Times-Bold',
  1775. 'timesI' => 'Times-Italic',
  1776. 'timesBI' => 'Times-BoldItalic',
  1777. 'symbol' => 'Symbol',
  1778. 'zapfdingbats' => 'ZapfDingbats'
  1779. );
  1780. // set scale factor
  1781. $this->setPageUnit($unit);
  1782. // set page format and orientation
  1783. $this->setPageFormat($format, $orientation);
  1784. // page margins (1 cm)
  1785. $margin = 28.35 / $this->k;
  1786. $this->SetMargins($margin, $margin);
  1787. $this->clMargin = $this->lMargin;
  1788. $this->crMargin = $this->rMargin;
  1789. // internal cell padding
  1790. $cpadding = $margin / 10;
  1791. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1792. // cell margins
  1793. $this->setCellMargins(0, 0, 0, 0);
  1794. // line width (0.2 mm)
  1795. $this->LineWidth = 0.57 / $this->k;
  1796. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1797. $this->linestyleCap = '0 J';
  1798. $this->linestyleJoin = '0 j';
  1799. $this->linestyleDash = '[] 0 d';
  1800. // automatic page break
  1801. $this->SetAutoPageBreak(true, (2 * $margin));
  1802. // full width display mode
  1803. $this->SetDisplayMode('fullwidth');
  1804. // compression
  1805. $this->SetCompression();
  1806. // set default PDF version number
  1807. $this->setPDFVersion();
  1808. $this->pdfproducer = "\x54\x43\x50\x44\x46\x20" . $this->tcpdf_version . "\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
  1809. $this->tcpdflink = true;
  1810. $this->encoding = $encoding;
  1811. $this->HREF = array();
  1812. $this->getFontsList();
  1813. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1814. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1815. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1816. $this->extgstates = array();
  1817. $this->setTextShadow();
  1818. // user's rights
  1819. $this->sign = false;
  1820. $this->ur['enabled'] = false;
  1821. $this->ur['document'] = '/FullSave';
  1822. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1823. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1824. $this->ur['signature'] = '/Modify';
  1825. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1826. $this->ur['formex'] = '';
  1827. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1828. $this->empty_signature_appearance = array();
  1829. // set default JPEG quality
  1830. $this->jpeg_quality = 75;
  1831. // initialize some settings
  1832. $this->utf8Bidi(array(''), '');
  1833. // set default font
  1834. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1835. // check if PCRE Unicode support is enabled
  1836. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1837. // PCRE unicode support is turned ON
  1838. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1839. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1840. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1841. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1842. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1843. } else {
  1844. // PCRE unicode support is turned OFF
  1845. $this->setSpacesRE('/[^\S\xa0]/');
  1846. }
  1847. $this->default_form_prop = array('lineWidth' => 1, 'borderStyle' => 'solid', 'fillColor' => array(255, 255, 255), 'strokeColor' => array(128, 128, 128));
  1848. // set file ID for trailer
  1849. $serformat = (is_array($format) ? serialize($format) : $format);
  1850. $this->file_id = md5($this->getRandomSeed('TCPDF' . $orientation . $unit . $serformat . $encoding));
  1851. // set document creation and modification timestamp
  1852. $this->doc_creation_timestamp = time();
  1853. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1854. // get default graphic vars
  1855. $this->default_graphic_vars = $this->getGraphicVars();
  1856. $this->header_xobj_autoreset = false;
  1857. $this->custom_xmp = '';
  1858. }
  1859. /**
  1860. * Default destructor.
  1861. * @public
  1862. * @since 1.53.0.TC016
  1863. */
  1864. public function __destruct() {
  1865. // restore internal encoding
  1866. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1867. mb_internal_encoding($this->internal_encoding);
  1868. }
  1869. // unset all class variables
  1870. $this->_destroy(true);
  1871. }
  1872. /**
  1873. * Return the current TCPDF version.
  1874. * @return TCPDF version string
  1875. * @public
  1876. * @since 5.9.012 (2010-11-10)
  1877. */
  1878. public function getTCPDFVersion() {
  1879. return $this->tcpdf_version;
  1880. }
  1881. /**
  1882. * Set the units of measure for the document.
  1883. * @param $unit (string) 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.
  1884. * @public
  1885. * @since 3.0.015 (2008-06-06)
  1886. */
  1887. public function setPageUnit($unit) {
  1888. $unit = strtolower($unit);
  1889. //Set scale factor
  1890. switch ($unit) {
  1891. // points
  1892. case 'px':
  1893. case 'pt': {
  1894. $this->k = 1;
  1895. break;
  1896. }
  1897. // millimeters
  1898. case 'mm': {
  1899. $this->k = $this->dpi / 25.4;
  1900. break;
  1901. }
  1902. // centimeters
  1903. case 'cm': {
  1904. $this->k = $this->dpi / 2.54;
  1905. break;
  1906. }
  1907. // inches
  1908. case 'in': {
  1909. $this->k = $this->dpi;
  1910. break;
  1911. }
  1912. // unsupported unit
  1913. default : {
  1914. $this->Error('Incorrect unit: ' . $unit);
  1915. break;
  1916. }
  1917. }
  1918. $this->pdfunit = $unit;
  1919. if (isset($this->CurOrientation)) {
  1920. $this->setPageOrientation($this->CurOrientation);
  1921. }
  1922. }
  1923. /**
  1924. * Get page dimensions from format name.
  1925. * @param $format (mixed) The format name. It can be: <ul>
  1926. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1927. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1928. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1929. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1930. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1931. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1932. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1933. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1934. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1935. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1936. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1937. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1938. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1939. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1940. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1941. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1942. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1943. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1944. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1945. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1946. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1947. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1948. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1949. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1950. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1951. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1952. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1953. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1954. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1955. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1956. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1957. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1958. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1959. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1960. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1961. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1962. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1963. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1964. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1965. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1966. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1967. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1968. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1969. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1970. * <li><b>SIS 014711 E Series</b></li>
  1971. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1972. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1973. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1974. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1975. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1976. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1977. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1978. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1979. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1980. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1981. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1982. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1983. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1984. * <li><b>SIS 014711 G Series</b></li>
  1985. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1986. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1987. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1988. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1989. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1990. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1991. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1992. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1993. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1994. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1995. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1996. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1997. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1998. * <li><b>ISO Press</b></li>
  1999. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  2000. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  2001. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  2002. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  2003. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  2004. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  2005. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  2006. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  2007. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  2008. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  2009. * <li><b>German DIN 476</b></li>
  2010. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  2011. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  2012. * <li><b>Variations on the ISO Standard</b></li>
  2013. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  2014. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  2015. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  2016. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  2017. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  2018. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  2019. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  2020. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  2021. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  2022. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  2023. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  2024. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  2025. * <li><b>ANSI Series</b></li>
  2026. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  2027. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  2028. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  2029. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  2030. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  2031. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  2032. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  2033. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  2034. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  2035. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  2036. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  2037. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  2038. * <li><b>Other North American Paper Sizes</b></li>
  2039. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  2040. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  2041. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  2042. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  2043. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  2044. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  2045. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  2046. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  2047. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  2048. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  2049. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  2050. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  2051. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  2052. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  2053. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  2054. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  2055. * <li><b>North American Architectural Sizes</b></li>
  2056. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  2057. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  2058. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  2059. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  2060. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  2061. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  2062. * <li><b>Announcement Envelopes</b></li>
  2063. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  2064. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  2065. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  2066. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  2067. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  2068. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  2069. * <li><b>Commercial Envelopes</b></li>
  2070. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  2071. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  2072. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  2073. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  2074. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  2075. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  2076. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  2077. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  2078. * <li><b>Catalogue Envelopes</b></li>
  2079. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  2080. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  2081. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  2082. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  2083. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  2084. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  2085. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  2086. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  2087. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  2088. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  2089. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  2090. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  2091. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  2092. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  2093. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  2094. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  2095. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  2096. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  2097. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  2098. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  2099. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  2100. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  2101. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  2102. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  2103. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  2104. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  2105. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  2106. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  2107. * <li><b>PA Series</b></li>
  2108. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2109. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2110. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2111. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2112. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2113. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2114. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2115. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2116. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2117. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2118. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2119. * <li><b>Standard Photographic Print Sizes</b></li>
  2120. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2121. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2122. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2123. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2124. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2125. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2126. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2127. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2128. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2129. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2130. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2131. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2132. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2133. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2134. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2135. * <li><b>Common Newspaper Sizes</b></li>
  2136. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2137. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2138. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2139. * <li><b>Business Cards</b></li>
  2140. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2141. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2142. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2143. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2144. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2145. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2146. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2147. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2148. * <li><b>Billboards</b></li>
  2149. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2150. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2151. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2152. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2153. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2154. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2155. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2156. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2157. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2158. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2159. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2160. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2161. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2162. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2163. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2164. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2165. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2166. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2167. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2168. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2169. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2170. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2171. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2172. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2173. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2174. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2175. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2176. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2177. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2178. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2179. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2180. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2181. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2182. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2183. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2184. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2185. * <li><b>Old Imperial Belgian</b></li>
  2186. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2187. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2188. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2189. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2190. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2191. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2192. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2193. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2194. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2195. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2196. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2197. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2198. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2199. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2200. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2201. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2202. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2203. * <li><b>Old Imperial French</b></li>
  2204. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2205. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2206. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2207. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2208. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2209. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2210. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2211. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2212. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2213. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2214. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2215. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2216. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2217. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2218. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2219. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2220. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2221. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2222. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2223. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2224. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2225. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2226. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2227. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2228. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2229. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2230. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2231. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2232. * </ul>
  2233. * @return array containing page width and height in points
  2234. * @public
  2235. * @since 5.0.010 (2010-05-17)
  2236. */
  2237. public function getPageSizeFromFormat($format) {
  2238. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2239. switch (strtoupper($format)) {
  2240. // ISO 216 A Series + 2 SIS 014711 extensions
  2241. case 'A0' : {
  2242. $pf = array(2383.937, 3370.394);
  2243. break;
  2244. }
  2245. case 'A1' : {
  2246. $pf = array(1683.780, 2383.937);
  2247. break;
  2248. }
  2249. case 'A2' : {
  2250. $pf = array(1190.551, 1683.780);
  2251. break;
  2252. }
  2253. case 'A3' : {
  2254. $pf = array(841.890, 1190.551);
  2255. break;
  2256. }
  2257. case 'A4' : {
  2258. $pf = array(595.276, 841.890);
  2259. break;
  2260. }
  2261. case 'A5' : {
  2262. $pf = array(419.528, 595.276);
  2263. break;
  2264. }
  2265. case 'A6' : {
  2266. $pf = array(297.638, 419.528);
  2267. break;
  2268. }
  2269. case 'A7' : {
  2270. $pf = array(209.764, 297.638);
  2271. break;
  2272. }
  2273. case 'A8' : {
  2274. $pf = array(147.402, 209.764);
  2275. break;
  2276. }
  2277. case 'A9' : {
  2278. $pf = array(104.882, 147.402);
  2279. break;
  2280. }
  2281. case 'A10': {
  2282. $pf = array(73.701, 104.882);
  2283. break;
  2284. }
  2285. case 'A11': {
  2286. $pf = array(51.024, 73.701);
  2287. break;
  2288. }
  2289. case 'A12': {
  2290. $pf = array(36.850, 51.024);
  2291. break;
  2292. }
  2293. // ISO 216 B Series + 2 SIS 014711 extensions
  2294. case 'B0' : {
  2295. $pf = array(2834.646, 4008.189);
  2296. break;
  2297. }
  2298. case 'B1' : {
  2299. $pf = array(2004.094, 2834.646);
  2300. break;
  2301. }
  2302. case 'B2' : {
  2303. $pf = array(1417.323, 2004.094);
  2304. break;
  2305. }
  2306. case 'B3' : {
  2307. $pf = array(1000.630, 1417.323);
  2308. break;
  2309. }
  2310. case 'B4' : {
  2311. $pf = array(708.661, 1000.630);
  2312. break;
  2313. }
  2314. case 'B5' : {
  2315. $pf = array(498.898, 708.661);
  2316. break;
  2317. }
  2318. case 'B6' : {
  2319. $pf = array(354.331, 498.898);
  2320. break;
  2321. }
  2322. case 'B7' : {
  2323. $pf = array(249.449, 354.331);
  2324. break;
  2325. }
  2326. case 'B8' : {
  2327. $pf = array(175.748, 249.449);
  2328. break;
  2329. }
  2330. case 'B9' : {
  2331. $pf = array(124.724, 175.748);
  2332. break;
  2333. }
  2334. case 'B10': {
  2335. $pf = array(87.874, 124.724);
  2336. break;
  2337. }
  2338. case 'B11': {
  2339. $pf = array(62.362, 87.874);
  2340. break;
  2341. }
  2342. case 'B12': {
  2343. $pf = array(42.520, 62.362);
  2344. break;
  2345. }
  2346. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2347. case 'C0' : {
  2348. $pf = array(2599.370, 3676.535);
  2349. break;
  2350. }
  2351. case 'C1' : {
  2352. $pf = array(1836.850, 2599.370);
  2353. break;
  2354. }
  2355. case 'C2' : {
  2356. $pf = array(1298.268, 1836.850);
  2357. break;
  2358. }
  2359. case 'C3' : {
  2360. $pf = array(918.425, 1298.268);
  2361. break;
  2362. }
  2363. case 'C4' : {
  2364. $pf = array(649.134, 918.425);
  2365. break;
  2366. }
  2367. case 'C5' : {
  2368. $pf = array(459.213, 649.134);
  2369. break;
  2370. }
  2371. case 'C6' : {
  2372. $pf = array(323.150, 459.213);
  2373. break;
  2374. }
  2375. case 'C7' : {
  2376. $pf = array(229.606, 323.150);
  2377. break;
  2378. }
  2379. case 'C8' : {
  2380. $pf = array(161.575, 229.606);
  2381. break;
  2382. }
  2383. case 'C9' : {
  2384. $pf = array(113.386, 161.575);
  2385. break;
  2386. }
  2387. case 'C10': {
  2388. $pf = array(79.370, 113.386);
  2389. break;
  2390. }
  2391. case 'C11': {
  2392. $pf = array(56.693, 79.370);
  2393. break;
  2394. }
  2395. case 'C12': {
  2396. $pf = array(39.685, 56.693);
  2397. break;
  2398. }
  2399. case 'C76': {
  2400. $pf = array(229.606, 459.213);
  2401. break;
  2402. }
  2403. case 'DL' : {
  2404. $pf = array(311.811, 623.622);
  2405. break;
  2406. }
  2407. // SIS 014711 E Series
  2408. case 'E0' : {
  2409. $pf = array(2491.654, 3517.795);
  2410. break;
  2411. }
  2412. case 'E1' : {
  2413. $pf = array(1757.480, 2491.654);
  2414. break;
  2415. }
  2416. case 'E2' : {
  2417. $pf = array(1247.244, 1757.480);
  2418. break;
  2419. }
  2420. case 'E3' : {
  2421. $pf = array(878.740, 1247.244);
  2422. break;
  2423. }
  2424. case 'E4' : {
  2425. $pf = array(623.622, 878.740);
  2426. break;
  2427. }
  2428. case 'E5' : {
  2429. $pf = array(439.370, 623.622);
  2430. break;
  2431. }
  2432. case 'E6' : {
  2433. $pf = array(311.811, 439.370);
  2434. break;
  2435. }
  2436. case 'E7' : {
  2437. $pf = array(221.102, 311.811);
  2438. break;
  2439. }
  2440. case 'E8' : {
  2441. $pf = array(155.906, 221.102);
  2442. break;
  2443. }
  2444. case 'E9' : {
  2445. $pf = array(110.551, 155.906);
  2446. break;
  2447. }
  2448. case 'E10': {
  2449. $pf = array(76.535, 110.551);
  2450. break;
  2451. }
  2452. case 'E11': {
  2453. $pf = array(53.858, 76.535);
  2454. break;
  2455. }
  2456. case 'E12': {
  2457. $pf = array(36.850, 53.858);
  2458. break;
  2459. }
  2460. // SIS 014711 G Series
  2461. case 'G0' : {
  2462. $pf = array(2715.591, 3838.110);
  2463. break;
  2464. }
  2465. case 'G1' : {
  2466. $pf = array(1919.055, 2715.591);
  2467. break;
  2468. }
  2469. case 'G2' : {
  2470. $pf = array(1357.795, 1919.055);
  2471. break;
  2472. }
  2473. case 'G3' : {
  2474. $pf = array(958.110, 1357.795);
  2475. break;
  2476. }
  2477. case 'G4' : {
  2478. $pf = array(677.480, 958.110);
  2479. break;
  2480. }
  2481. case 'G5' : {
  2482. $pf = array(479.055, 677.480);
  2483. break;
  2484. }
  2485. case 'G6' : {
  2486. $pf = array(337.323, 479.055);
  2487. break;
  2488. }
  2489. case 'G7' : {
  2490. $pf = array(238.110, 337.323);
  2491. break;
  2492. }
  2493. case 'G8' : {
  2494. $pf = array(167.244, 238.110);
  2495. break;
  2496. }
  2497. case 'G9' : {
  2498. $pf = array(119.055, 167.244);
  2499. break;
  2500. }
  2501. case 'G10': {
  2502. $pf = array(82.205, 119.055);
  2503. break;
  2504. }
  2505. case 'G11': {
  2506. $pf = array(59.528, 82.205);
  2507. break;
  2508. }
  2509. case 'G12': {
  2510. $pf = array(39.685, 59.528);
  2511. break;
  2512. }
  2513. // ISO Press
  2514. case 'RA0': {
  2515. $pf = array(2437.795, 3458.268);
  2516. break;
  2517. }
  2518. case 'RA1': {
  2519. $pf = array(1729.134, 2437.795);
  2520. break;
  2521. }
  2522. case 'RA2': {
  2523. $pf = array(1218.898, 1729.134);
  2524. break;
  2525. }
  2526. case 'RA3': {
  2527. $pf = array(864.567, 1218.898);
  2528. break;
  2529. }
  2530. case 'RA4': {
  2531. $pf = array(609.449, 864.567);
  2532. break;
  2533. }
  2534. case 'SRA0': {
  2535. $pf = array(2551.181, 3628.346);
  2536. break;
  2537. }
  2538. case 'SRA1': {
  2539. $pf = array(1814.173, 2551.181);
  2540. break;
  2541. }
  2542. case 'SRA2': {
  2543. $pf = array(1275.591, 1814.173);
  2544. break;
  2545. }
  2546. case 'SRA3': {
  2547. $pf = array(907.087, 1275.591);
  2548. break;
  2549. }
  2550. case 'SRA4': {
  2551. $pf = array(637.795, 907.087);
  2552. break;
  2553. }
  2554. // German DIN 476
  2555. case '4A0': {
  2556. $pf = array(4767.874, 6740.787);
  2557. break;
  2558. }
  2559. case '2A0': {
  2560. $pf = array(3370.394, 4767.874);
  2561. break;
  2562. }
  2563. // Variations on the ISO Standard
  2564. case 'A2_EXTRA' : {
  2565. $pf = array(1261.417, 1754.646);
  2566. break;
  2567. }
  2568. case 'A3+' : {
  2569. $pf = array(932.598, 1369.134);
  2570. break;
  2571. }
  2572. case 'A3_EXTRA' : {
  2573. $pf = array(912.756, 1261.417);
  2574. break;
  2575. }
  2576. case 'A3_SUPER' : {
  2577. $pf = array(864.567, 1440.000);
  2578. break;
  2579. }
  2580. case 'SUPER_A3' : {
  2581. $pf = array(864.567, 1380.472);
  2582. break;
  2583. }
  2584. case 'A4_EXTRA' : {
  2585. $pf = array(666.142, 912.756);
  2586. break;
  2587. }
  2588. case 'A4_SUPER' : {
  2589. $pf = array(649.134, 912.756);
  2590. break;
  2591. }
  2592. case 'SUPER_A4' : {
  2593. $pf = array(643.465, 1009.134);
  2594. break;
  2595. }
  2596. case 'A4_LONG' : {
  2597. $pf = array(595.276, 986.457);
  2598. break;
  2599. }
  2600. case 'F4' : {
  2601. $pf = array(595.276, 935.433);
  2602. break;
  2603. }
  2604. case 'SO_B5_EXTRA': {
  2605. $pf = array(572.598, 782.362);
  2606. break;
  2607. }
  2608. case 'A5_EXTRA' : {
  2609. $pf = array(490.394, 666.142);
  2610. break;
  2611. }
  2612. // ANSI Series
  2613. case 'ANSI_E': {
  2614. $pf = array(2448.000, 3168.000);
  2615. break;
  2616. }
  2617. case 'ANSI_D': {
  2618. $pf = array(1584.000, 2448.000);
  2619. break;
  2620. }
  2621. case 'ANSI_C': {
  2622. $pf = array(1224.000, 1584.000);
  2623. break;
  2624. }
  2625. case 'ANSI_B': {
  2626. $pf = array(792.000, 1224.000);
  2627. break;
  2628. }
  2629. case 'ANSI_A': {
  2630. $pf = array(612.000, 792.000);
  2631. break;
  2632. }
  2633. // Traditional 'Loose' North American Paper Sizes
  2634. case 'USLEDGER':
  2635. case 'LEDGER' : {
  2636. $pf = array(1224.000, 792.000);
  2637. break;
  2638. }
  2639. case 'ORGANIZERK':
  2640. case 'BIBLE':
  2641. case 'USTABLOID':
  2642. case 'TABLOID': {
  2643. $pf = array(792.000, 1224.000);
  2644. break;
  2645. }
  2646. case 'ORGANIZERM':
  2647. case 'USLETTER':
  2648. case 'LETTER' : {
  2649. $pf = array(612.000, 792.000);
  2650. break;
  2651. }
  2652. case 'USLEGAL':
  2653. case 'LEGAL' : {
  2654. $pf = array(612.000, 1008.000);
  2655. break;
  2656. }
  2657. case 'GOVERNMENTLETTER':
  2658. case 'GLETTER': {
  2659. $pf = array(576.000, 756.000);
  2660. break;
  2661. }
  2662. case 'JUNIORLEGAL':
  2663. case 'JLEGAL' : {
  2664. $pf = array(576.000, 360.000);
  2665. break;
  2666. }
  2667. // Other North American Paper Sizes
  2668. case 'QUADDEMY': {
  2669. $pf = array(2520.000, 3240.000);
  2670. break;
  2671. }
  2672. case 'SUPER_B': {
  2673. $pf = array(936.000, 1368.000);
  2674. break;
  2675. }
  2676. case 'QUARTO': {
  2677. $pf = array(648.000, 792.000);
  2678. break;
  2679. }
  2680. case 'GOVERNMENTLEGAL':
  2681. case 'FOLIO': {
  2682. $pf = array(612.000, 936.000);
  2683. break;
  2684. }
  2685. case 'MONARCH':
  2686. case 'EXECUTIVE': {
  2687. $pf = array(522.000, 756.000);
  2688. break;
  2689. }
  2690. case 'ORGANIZERL':
  2691. case 'STATEMENT':
  2692. case 'MEMO': {
  2693. $pf = array(396.000, 612.000);
  2694. break;
  2695. }
  2696. case 'FOOLSCAP': {
  2697. $pf = array(595.440, 936.000);
  2698. break;
  2699. }
  2700. case 'COMPACT': {
  2701. $pf = array(306.000, 486.000);
  2702. break;
  2703. }
  2704. case 'ORGANIZERJ': {
  2705. $pf = array(198.000, 360.000);
  2706. break;
  2707. }
  2708. // Canadian standard CAN 2-9.60M
  2709. case 'P1': {
  2710. $pf = array(1587.402, 2437.795);
  2711. break;
  2712. }
  2713. case 'P2': {
  2714. $pf = array(1218.898, 1587.402);
  2715. break;
  2716. }
  2717. case 'P3': {
  2718. $pf = array(793.701, 1218.898);
  2719. break;
  2720. }
  2721. case 'P4': {
  2722. $pf = array(609.449, 793.701);
  2723. break;
  2724. }
  2725. case 'P5': {
  2726. $pf = array(396.850, 609.449);
  2727. break;
  2728. }
  2729. case 'P6': {
  2730. $pf = array(303.307, 396.850);
  2731. break;
  2732. }
  2733. // North American Architectural Sizes
  2734. case 'ARCH_E' : {
  2735. $pf = array(2592.000, 3456.000);
  2736. break;
  2737. }
  2738. case 'ARCH_E1': {
  2739. $pf = array(2160.000, 3024.000);
  2740. break;
  2741. }
  2742. case 'ARCH_D' : {
  2743. $pf = array(1728.000, 2592.000);
  2744. break;
  2745. }
  2746. case 'BROADSHEET':
  2747. case 'ARCH_C' : {
  2748. $pf = array(1296.000, 1728.000);
  2749. break;
  2750. }
  2751. case 'ARCH_B' : {
  2752. $pf = array(864.000, 1296.000);
  2753. break;
  2754. }
  2755. case 'ARCH_A' : {
  2756. $pf = array(648.000, 864.000);
  2757. break;
  2758. }
  2759. // --- North American Envelope Sizes ---
  2760. // - Announcement Envelopes
  2761. case 'ANNENV_A2' : {
  2762. $pf = array(314.640, 414.000);
  2763. break;
  2764. }
  2765. case 'ANNENV_A6' : {
  2766. $pf = array(342.000, 468.000);
  2767. break;
  2768. }
  2769. case 'ANNENV_A7' : {
  2770. $pf = array(378.000, 522.000);
  2771. break;
  2772. }
  2773. case 'ANNENV_A8' : {
  2774. $pf = array(396.000, 584.640);
  2775. break;
  2776. }
  2777. case 'ANNENV_A10' : {
  2778. $pf = array(450.000, 692.640);
  2779. break;
  2780. }
  2781. case 'ANNENV_SLIM': {
  2782. $pf = array(278.640, 638.640);
  2783. break;
  2784. }
  2785. // - Commercial Envelopes
  2786. case 'COMMENV_N6_1/4': {
  2787. $pf = array(252.000, 432.000);
  2788. break;
  2789. }
  2790. case 'COMMENV_N6_3/4': {
  2791. $pf = array(260.640, 468.000);
  2792. break;
  2793. }
  2794. case 'COMMENV_N8' : {
  2795. $pf = array(278.640, 540.000);
  2796. break;
  2797. }
  2798. case 'COMMENV_N9' : {
  2799. $pf = array(278.640, 638.640);
  2800. break;
  2801. }
  2802. case 'COMMENV_N10' : {
  2803. $pf = array(296.640, 684.000);
  2804. break;
  2805. }
  2806. case 'COMMENV_N11' : {
  2807. $pf = array(324.000, 746.640);
  2808. break;
  2809. }
  2810. case 'COMMENV_N12' : {
  2811. $pf = array(342.000, 792.000);
  2812. break;
  2813. }
  2814. case 'COMMENV_N14' : {
  2815. $pf = array(360.000, 828.000);
  2816. break;
  2817. }
  2818. // - Catalogue Envelopes
  2819. case 'CATENV_N1' : {
  2820. $pf = array(432.000, 648.000);
  2821. break;
  2822. }
  2823. case 'CATENV_N1_3/4' : {
  2824. $pf = array(468.000, 684.000);
  2825. break;
  2826. }
  2827. case 'CATENV_N2' : {
  2828. $pf = array(468.000, 720.000);
  2829. break;
  2830. }
  2831. case 'CATENV_N3' : {
  2832. $pf = array(504.000, 720.000);
  2833. break;
  2834. }
  2835. case 'CATENV_N6' : {
  2836. $pf = array(540.000, 756.000);
  2837. break;
  2838. }
  2839. case 'CATENV_N7' : {
  2840. $pf = array(576.000, 792.000);
  2841. break;
  2842. }
  2843. case 'CATENV_N8' : {
  2844. $pf = array(594.000, 810.000);
  2845. break;
  2846. }
  2847. case 'CATENV_N9_1/2' : {
  2848. $pf = array(612.000, 756.000);
  2849. break;
  2850. }
  2851. case 'CATENV_N9_3/4' : {
  2852. $pf = array(630.000, 810.000);
  2853. break;
  2854. }
  2855. case 'CATENV_N10_1/2': {
  2856. $pf = array(648.000, 864.000);
  2857. break;
  2858. }
  2859. case 'CATENV_N12_1/2': {
  2860. $pf = array(684.000, 900.000);
  2861. break;
  2862. }
  2863. case 'CATENV_N13_1/2': {
  2864. $pf = array(720.000, 936.000);
  2865. break;
  2866. }
  2867. case 'CATENV_N14_1/4': {
  2868. $pf = array(810.000, 882.000);
  2869. break;
  2870. }
  2871. case 'CATENV_N14_1/2': {
  2872. $pf = array(828.000, 1044.000);
  2873. break;
  2874. }
  2875. // Japanese (JIS P 0138-61) Standard B-Series
  2876. case 'JIS_B0' : {
  2877. $pf = array(2919.685, 4127.244);
  2878. break;
  2879. }
  2880. case 'JIS_B1' : {
  2881. $pf = array(2063.622, 2919.685);
  2882. break;
  2883. }
  2884. case 'JIS_B2' : {
  2885. $pf = array(1459.843, 2063.622);
  2886. break;
  2887. }
  2888. case 'JIS_B3' : {
  2889. $pf = array(1031.811, 1459.843);
  2890. break;
  2891. }
  2892. case 'JIS_B4' : {
  2893. $pf = array(728.504, 1031.811);
  2894. break;
  2895. }
  2896. case 'JIS_B5' : {
  2897. $pf = array(515.906, 728.504);
  2898. break;
  2899. }
  2900. case 'JIS_B6' : {
  2901. $pf = array(362.835, 515.906);
  2902. break;
  2903. }
  2904. case 'JIS_B7' : {
  2905. $pf = array(257.953, 362.835);
  2906. break;
  2907. }
  2908. case 'JIS_B8' : {
  2909. $pf = array(181.417, 257.953);
  2910. break;
  2911. }
  2912. case 'JIS_B9' : {
  2913. $pf = array(127.559, 181.417);
  2914. break;
  2915. }
  2916. case 'JIS_B10': {
  2917. $pf = array(90.709, 127.559);
  2918. break;
  2919. }
  2920. case 'JIS_B11': {
  2921. $pf = array(62.362, 90.709);
  2922. break;
  2923. }
  2924. case 'JIS_B12': {
  2925. $pf = array(45.354, 62.362);
  2926. break;
  2927. }
  2928. // PA Series
  2929. case 'PA0' : {
  2930. $pf = array(2381.102, 3174.803,);
  2931. break;
  2932. }
  2933. case 'PA1' : {
  2934. $pf = array(1587.402, 2381.102);
  2935. break;
  2936. }
  2937. case 'PA2' : {
  2938. $pf = array(1190.551, 1587.402);
  2939. break;
  2940. }
  2941. case 'PA3' : {
  2942. $pf = array(793.701, 1190.551);
  2943. break;
  2944. }
  2945. case 'PA4' : {
  2946. $pf = array(595.276, 793.701);
  2947. break;
  2948. }
  2949. case 'PA5' : {
  2950. $pf = array(396.850, 595.276);
  2951. break;
  2952. }
  2953. case 'PA6' : {
  2954. $pf = array(297.638, 396.850);
  2955. break;
  2956. }
  2957. case 'PA7' : {
  2958. $pf = array(198.425, 297.638);
  2959. break;
  2960. }
  2961. case 'PA8' : {
  2962. $pf = array(147.402, 198.425);
  2963. break;
  2964. }
  2965. case 'PA9' : {
  2966. $pf = array(99.213, 147.402);
  2967. break;
  2968. }
  2969. case 'PA10': {
  2970. $pf = array(73.701, 99.213);
  2971. break;
  2972. }
  2973. // Standard Photographic Print Sizes
  2974. case 'PASSPORT_PHOTO': {
  2975. $pf = array(99.213, 127.559);
  2976. break;
  2977. }
  2978. case 'E' : {
  2979. $pf = array(233.858, 340.157);
  2980. break;
  2981. }
  2982. case 'L':
  2983. case '3R' : {
  2984. $pf = array(252.283, 360.000);
  2985. break;
  2986. }
  2987. case 'KG':
  2988. case '4R' : {
  2989. $pf = array(289.134, 430.866);
  2990. break;
  2991. }
  2992. case '4D' : {
  2993. $pf = array(340.157, 430.866);
  2994. break;
  2995. }
  2996. case '2L':
  2997. case '5R' : {
  2998. $pf = array(360.000, 504.567);
  2999. break;
  3000. }
  3001. case '8P':
  3002. case '6R' : {
  3003. $pf = array(430.866, 575.433);
  3004. break;
  3005. }
  3006. case '6P':
  3007. case '8R' : {
  3008. $pf = array(575.433, 720.000);
  3009. break;
  3010. }
  3011. case '6PW':
  3012. case 'S8R' : {
  3013. $pf = array(575.433, 864.567);
  3014. break;
  3015. }
  3016. case '4P':
  3017. case '10R' : {
  3018. $pf = array(720.000, 864.567);
  3019. break;
  3020. }
  3021. case '4PW':
  3022. case 'S10R': {
  3023. $pf = array(720.000, 1080.000);
  3024. break;
  3025. }
  3026. case '11R' : {
  3027. $pf = array(790.866, 1009.134);
  3028. break;
  3029. }
  3030. case 'S11R': {
  3031. $pf = array(790.866, 1224.567);
  3032. break;
  3033. }
  3034. case '12R' : {
  3035. $pf = array(864.567, 1080.000);
  3036. break;
  3037. }
  3038. case 'S12R': {
  3039. $pf = array(864.567, 1292.598);
  3040. break;
  3041. }
  3042. // Common Newspaper Sizes
  3043. case 'NEWSPAPER_BROADSHEET': {
  3044. $pf = array(2125.984, 1700.787);
  3045. break;
  3046. }
  3047. case 'NEWSPAPER_BERLINER' : {
  3048. $pf = array(1332.283, 892.913);
  3049. break;
  3050. }
  3051. case 'NEWSPAPER_TABLOID':
  3052. case 'NEWSPAPER_COMPACT' : {
  3053. $pf = array(1218.898, 793.701);
  3054. break;
  3055. }
  3056. // Business Cards
  3057. case 'CREDIT_CARD':
  3058. case 'BUSINESS_CARD':
  3059. case 'BUSINESS_CARD_ISO7810': {
  3060. $pf = array(153.014, 242.646);
  3061. break;
  3062. }
  3063. case 'BUSINESS_CARD_ISO216' : {
  3064. $pf = array(147.402, 209.764);
  3065. break;
  3066. }
  3067. case 'BUSINESS_CARD_IT':
  3068. case 'BUSINESS_CARD_UK':
  3069. case 'BUSINESS_CARD_FR':
  3070. case 'BUSINESS_CARD_DE':
  3071. case 'BUSINESS_CARD_ES' : {
  3072. $pf = array(155.906, 240.945);
  3073. break;
  3074. }
  3075. case 'BUSINESS_CARD_CA':
  3076. case 'BUSINESS_CARD_US' : {
  3077. $pf = array(144.567, 252.283);
  3078. break;
  3079. }
  3080. case 'BUSINESS_CARD_JP' : {
  3081. $pf = array(155.906, 257.953);
  3082. break;
  3083. }
  3084. case 'BUSINESS_CARD_HK' : {
  3085. $pf = array(153.071, 255.118);
  3086. break;
  3087. }
  3088. case 'BUSINESS_CARD_AU':
  3089. case 'BUSINESS_CARD_DK':
  3090. case 'BUSINESS_CARD_SE' : {
  3091. $pf = array(155.906, 255.118);
  3092. break;
  3093. }
  3094. case 'BUSINESS_CARD_RU':
  3095. case 'BUSINESS_CARD_CZ':
  3096. case 'BUSINESS_CARD_FI':
  3097. case 'BUSINESS_CARD_HU':
  3098. case 'BUSINESS_CARD_IL' : {
  3099. $pf = array(141.732, 255.118);
  3100. break;
  3101. }
  3102. // Billboards
  3103. case '4SHEET' : {
  3104. $pf = array(2880.000, 4320.000);
  3105. break;
  3106. }
  3107. case '6SHEET' : {
  3108. $pf = array(3401.575, 5102.362);
  3109. break;
  3110. }
  3111. case '12SHEET': {
  3112. $pf = array(8640.000, 4320.000);
  3113. break;
  3114. }
  3115. case '16SHEET': {
  3116. $pf = array(5760.000, 8640.000);
  3117. break;
  3118. }
  3119. case '32SHEET': {
  3120. $pf = array(11520.000, 8640.000);
  3121. break;
  3122. }
  3123. case '48SHEET': {
  3124. $pf = array(17280.000, 8640.000);
  3125. break;
  3126. }
  3127. case '64SHEET': {
  3128. $pf = array(23040.000, 8640.000);
  3129. break;
  3130. }
  3131. case '96SHEET': {
  3132. $pf = array(34560.000, 8640.000);
  3133. break;
  3134. }
  3135. // Old European Sizes
  3136. // - Old Imperial English Sizes
  3137. case 'EN_EMPEROR' : {
  3138. $pf = array(3456.000, 5184.000);
  3139. break;
  3140. }
  3141. case 'EN_ANTIQUARIAN' : {
  3142. $pf = array(2232.000, 3816.000);
  3143. break;
  3144. }
  3145. case 'EN_GRAND_EAGLE' : {
  3146. $pf = array(2070.000, 3024.000);
  3147. break;
  3148. }
  3149. case 'EN_DOUBLE_ELEPHANT' : {
  3150. $pf = array(1926.000, 2880.000);
  3151. break;
  3152. }
  3153. case 'EN_ATLAS' : {
  3154. $pf = array(1872.000, 2448.000);
  3155. break;
  3156. }
  3157. case 'EN_COLOMBIER' : {
  3158. $pf = array(1692.000, 2484.000);
  3159. break;
  3160. }
  3161. case 'EN_ELEPHANT' : {
  3162. $pf = array(1656.000, 2016.000);
  3163. break;
  3164. }
  3165. case 'EN_DOUBLE_DEMY' : {
  3166. $pf = array(1620.000, 2556.000);
  3167. break;
  3168. }
  3169. case 'EN_IMPERIAL' : {
  3170. $pf = array(1584.000, 2160.000);
  3171. break;
  3172. }
  3173. case 'EN_PRINCESS' : {
  3174. $pf = array(1548.000, 2016.000);
  3175. break;
  3176. }
  3177. case 'EN_CARTRIDGE' : {
  3178. $pf = array(1512.000, 1872.000);
  3179. break;
  3180. }
  3181. case 'EN_DOUBLE_LARGE_POST': {
  3182. $pf = array(1512.000, 2376.000);
  3183. break;
  3184. }
  3185. case 'EN_ROYAL' : {
  3186. $pf = array(1440.000, 1800.000);
  3187. break;
  3188. }
  3189. case 'EN_SHEET':
  3190. case 'EN_HALF_POST' : {
  3191. $pf = array(1404.000, 1692.000);
  3192. break;
  3193. }
  3194. case 'EN_SUPER_ROYAL' : {
  3195. $pf = array(1368.000, 1944.000);
  3196. break;
  3197. }
  3198. case 'EN_DOUBLE_POST' : {
  3199. $pf = array(1368.000, 2196.000);
  3200. break;
  3201. }
  3202. case 'EN_MEDIUM' : {
  3203. $pf = array(1260.000, 1656.000);
  3204. break;
  3205. }
  3206. case 'EN_DEMY' : {
  3207. $pf = array(1260.000, 1620.000);
  3208. break;
  3209. }
  3210. case 'EN_LARGE_POST' : {
  3211. $pf = array(1188.000, 1512.000);
  3212. break;
  3213. }
  3214. case 'EN_COPY_DRAUGHT' : {
  3215. $pf = array(1152.000, 1440.000);
  3216. break;
  3217. }
  3218. case 'EN_POST' : {
  3219. $pf = array(1116.000, 1386.000);
  3220. break;
  3221. }
  3222. case 'EN_CROWN' : {
  3223. $pf = array(1080.000, 1440.000);
  3224. break;
  3225. }
  3226. case 'EN_PINCHED_POST' : {
  3227. $pf = array(1062.000, 1332.000);
  3228. break;
  3229. }
  3230. case 'EN_BRIEF' : {
  3231. $pf = array(972.000, 1152.000);
  3232. break;
  3233. }
  3234. case 'EN_FOOLSCAP' : {
  3235. $pf = array(972.000, 1224.000);
  3236. break;
  3237. }
  3238. case 'EN_SMALL_FOOLSCAP' : {
  3239. $pf = array(954.000, 1188.000);
  3240. break;
  3241. }
  3242. case 'EN_POTT' : {
  3243. $pf = array(900.000, 1080.000);
  3244. break;
  3245. }
  3246. // - Old Imperial Belgian Sizes
  3247. case 'BE_GRAND_AIGLE' : {
  3248. $pf = array(1984.252, 2948.031);
  3249. break;
  3250. }
  3251. case 'BE_COLOMBIER' : {
  3252. $pf = array(1757.480, 2409.449);
  3253. break;
  3254. }
  3255. case 'BE_DOUBLE_CARRE': {
  3256. $pf = array(1757.480, 2607.874);
  3257. break;
  3258. }
  3259. case 'BE_ELEPHANT' : {
  3260. $pf = array(1746.142, 2182.677);
  3261. break;
  3262. }
  3263. case 'BE_PETIT_AIGLE' : {
  3264. $pf = array(1700.787, 2381.102);
  3265. break;
  3266. }
  3267. case 'BE_GRAND_JESUS' : {
  3268. $pf = array(1559.055, 2069.291);
  3269. break;
  3270. }
  3271. case 'BE_JESUS' : {
  3272. $pf = array(1530.709, 2069.291);
  3273. break;
  3274. }
  3275. case 'BE_RAISIN' : {
  3276. $pf = array(1417.323, 1842.520);
  3277. break;
  3278. }
  3279. case 'BE_GRAND_MEDIAN': {
  3280. $pf = array(1303.937, 1714.961);
  3281. break;
  3282. }
  3283. case 'BE_DOUBLE_POSTE': {
  3284. $pf = array(1233.071, 1601.575);
  3285. break;
  3286. }
  3287. case 'BE_COQUILLE' : {
  3288. $pf = array(1218.898, 1587.402);
  3289. break;
  3290. }
  3291. case 'BE_PETIT_MEDIAN': {
  3292. $pf = array(1176.378, 1502.362);
  3293. break;
  3294. }
  3295. case 'BE_RUCHE' : {
  3296. $pf = array(1020.472, 1303.937);
  3297. break;
  3298. }
  3299. case 'BE_PROPATRIA' : {
  3300. $pf = array(977.953, 1218.898);
  3301. break;
  3302. }
  3303. case 'BE_LYS' : {
  3304. $pf = array(898.583, 1125.354);
  3305. break;
  3306. }
  3307. case 'BE_POT' : {
  3308. $pf = array(870.236, 1088.504);
  3309. break;
  3310. }
  3311. case 'BE_ROSETTE' : {
  3312. $pf = array(765.354, 983.622);
  3313. break;
  3314. }
  3315. // - Old Imperial French Sizes
  3316. case 'FR_UNIVERS' : {
  3317. $pf = array(2834.646, 3685.039);
  3318. break;
  3319. }
  3320. case 'FR_DOUBLE_COLOMBIER' : {
  3321. $pf = array(2551.181, 3571.654);
  3322. break;
  3323. }
  3324. case 'FR_GRANDE_MONDE' : {
  3325. $pf = array(2551.181, 3571.654);
  3326. break;
  3327. }
  3328. case 'FR_DOUBLE_SOLEIL' : {
  3329. $pf = array(2267.717, 3401.575);
  3330. break;
  3331. }
  3332. case 'FR_DOUBLE_JESUS' : {
  3333. $pf = array(2154.331, 3174.803);
  3334. break;
  3335. }
  3336. case 'FR_GRAND_AIGLE' : {
  3337. $pf = array(2125.984, 3004.724);
  3338. break;
  3339. }
  3340. case 'FR_PETIT_AIGLE' : {
  3341. $pf = array(1984.252, 2664.567);
  3342. break;
  3343. }
  3344. case 'FR_DOUBLE_RAISIN' : {
  3345. $pf = array(1842.520, 2834.646);
  3346. break;
  3347. }
  3348. case 'FR_JOURNAL' : {
  3349. $pf = array(1842.520, 2664.567);
  3350. break;
  3351. }
  3352. case 'FR_COLOMBIER_AFFICHE': {
  3353. $pf = array(1785.827, 2551.181);
  3354. break;
  3355. }
  3356. case 'FR_DOUBLE_CAVALIER' : {
  3357. $pf = array(1757.480, 2607.874);
  3358. break;
  3359. }
  3360. case 'FR_CLOCHE' : {
  3361. $pf = array(1700.787, 2267.717);
  3362. break;
  3363. }
  3364. case 'FR_SOLEIL' : {
  3365. $pf = array(1700.787, 2267.717);
  3366. break;
  3367. }
  3368. case 'FR_DOUBLE_CARRE' : {
  3369. $pf = array(1587.402, 2551.181);
  3370. break;
  3371. }
  3372. case 'FR_DOUBLE_COQUILLE' : {
  3373. $pf = array(1587.402, 2494.488);
  3374. break;
  3375. }
  3376. case 'FR_JESUS' : {
  3377. $pf = array(1587.402, 2154.331);
  3378. break;
  3379. }
  3380. case 'FR_RAISIN' : {
  3381. $pf = array(1417.323, 1842.520);
  3382. break;
  3383. }
  3384. case 'FR_CAVALIER' : {
  3385. $pf = array(1303.937, 1757.480);
  3386. break;
  3387. }
  3388. case 'FR_DOUBLE_COURONNE' : {
  3389. $pf = array(1303.937, 2040.945);
  3390. break;
  3391. }
  3392. case 'FR_CARRE' : {
  3393. $pf = array(1275.591, 1587.402);
  3394. break;
  3395. }
  3396. case 'FR_COQUILLE' : {
  3397. $pf = array(1247.244, 1587.402);
  3398. break;
  3399. }
  3400. case 'FR_DOUBLE_TELLIERE' : {
  3401. $pf = array(1247.244, 1927.559);
  3402. break;
  3403. }
  3404. case 'FR_DOUBLE_CLOCHE' : {
  3405. $pf = array(1133.858, 1700.787);
  3406. break;
  3407. }
  3408. case 'FR_DOUBLE_POT' : {
  3409. $pf = array(1133.858, 1757.480);
  3410. break;
  3411. }
  3412. case 'FR_ECU' : {
  3413. $pf = array(1133.858, 1474.016);
  3414. break;
  3415. }
  3416. case 'FR_COURONNE' : {
  3417. $pf = array(1020.472, 1303.937);
  3418. break;
  3419. }
  3420. case 'FR_TELLIERE' : {
  3421. $pf = array(963.780, 1247.244);
  3422. break;
  3423. }
  3424. case 'FR_POT' : {
  3425. $pf = array(878.740, 1133.858);
  3426. break;
  3427. }
  3428. // DEFAULT ISO A4
  3429. default: {
  3430. $pf = array(595.276, 841.890);
  3431. break;
  3432. }
  3433. }
  3434. return $pf;
  3435. }
  3436. /**
  3437. * Change the format of the current page
  3438. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() documentation or an array of two numners (width, height) or an array containing the following measures and options:<ul>
  3439. * <li>['format'] = page format name (one of the above);</li>
  3440. * <li>['Rotate'] : The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li>
  3441. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  3442. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  3443. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  3444. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  3445. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  3446. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  3447. * <li>['CropBox'] : the visible region of default user space:</li>
  3448. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  3449. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  3450. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  3451. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  3452. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  3453. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  3454. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  3455. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  3456. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  3457. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  3458. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  3459. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  3460. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  3461. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  3462. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  3463. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  3464. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  3465. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  3466. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  3467. * <li>['BoxColorInfo'] :specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for each of the possible page boundaries other than the MediaBox:</li>
  3468. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  3469. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  3470. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  3471. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  3472. * <li>['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation</li>
  3473. * <li>['trans']['Dur'] : The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li>
  3474. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  3475. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  3476. * <li>['trans']['Dm'] : (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li>
  3477. * <li>['trans']['M'] : (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li>
  3478. * <li>['trans']['Di'] : (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li>
  3479. * <li>['trans']['SS'] : (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0.</li>
  3480. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  3481. * </ul>
  3482. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  3483. * <li>P or Portrait (default)</li>
  3484. * <li>L or Landscape</li>
  3485. * <li>'' (empty string) for automatic orientation</li>
  3486. * </ul>
  3487. * @protected
  3488. * @since 3.0.015 (2008-06-06)
  3489. * @see getPageSizeFromFormat()
  3490. */
  3491. protected function setPageFormat($format, $orientation = 'P') {
  3492. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  3493. // remove inherited values
  3494. unset($this->pagedim[$this->page]);
  3495. }
  3496. if (is_string($format)) {
  3497. // get page measures from format name
  3498. $pf = $this->getPageSizeFromFormat($format);
  3499. $this->fwPt = $pf[0];
  3500. $this->fhPt = $pf[1];
  3501. } else {
  3502. // the boundaries of the physical medium on which the page shall be displayed or printed
  3503. if (isset($format['MediaBox'])) {
  3504. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  3505. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  3506. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  3507. } else {
  3508. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  3509. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  3510. } else {
  3511. if (!isset($format['format'])) {
  3512. // default value
  3513. $format['format'] = 'A4';
  3514. }
  3515. $pf = $this->getPageSizeFromFormat($format['format']);
  3516. }
  3517. $this->fwPt = $pf[0];
  3518. $this->fhPt = $pf[1];
  3519. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  3520. }
  3521. // the visible region of default user space
  3522. if (isset($format['CropBox'])) {
  3523. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  3524. }
  3525. // the region to which the contents of the page shall be clipped when output in a production environment
  3526. if (isset($format['BleedBox'])) {
  3527. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  3528. }
  3529. // the intended dimensions of the finished page after trimming
  3530. if (isset($format['TrimBox'])) {
  3531. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  3532. }
  3533. // the page's meaningful content (including potential white space)
  3534. if (isset($format['ArtBox'])) {
  3535. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  3536. }
  3537. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  3538. if (isset($format['BoxColorInfo'])) {
  3539. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  3540. }
  3541. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  3542. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  3543. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  3544. }
  3545. if (isset($format['PZ'])) {
  3546. // The page's preferred zoom (magnification) factor
  3547. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  3548. }
  3549. if (isset($format['trans'])) {
  3550. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  3551. if (isset($format['trans']['Dur'])) {
  3552. // The page's display duration
  3553. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  3554. }
  3555. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  3556. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  3557. // The transition style that shall be used when moving to this page from another during a presentation
  3558. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  3559. $valid_effect = array('Split', 'Blinds');
  3560. $valid_vals = array('H', 'V');
  3561. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  3562. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  3563. }
  3564. $valid_effect = array('Split', 'Box', 'Fly');
  3565. $valid_vals = array('I', 'O');
  3566. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  3567. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  3568. }
  3569. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  3570. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  3571. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  3572. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  3573. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  3574. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  3575. }
  3576. }
  3577. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  3578. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  3579. }
  3580. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  3581. $this->pagedim[$this->page]['trans']['B'] = 'true';
  3582. }
  3583. } else {
  3584. $this->pagedim[$this->page]['trans']['S'] = 'R';
  3585. }
  3586. if (isset($format['trans']['D'])) {
  3587. // The duration of the transition effect, in seconds
  3588. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  3589. } else {
  3590. $this->pagedim[$this->page]['trans']['D'] = 1;
  3591. }
  3592. }
  3593. }
  3594. $this->setPageOrientation($orientation);
  3595. }
  3596. /**
  3597. * Set page boundaries.
  3598. * @param $page (int) page number
  3599. * @param $type (string) valid values are: <ul><li>'MediaBox' : the boundaries of the physical medium on which the page shall be displayed or printed;</li><li>'CropBox' : the visible region of default user space;</li><li>'BleedBox' : the region to which the contents of the page shall be clipped when output in a production environment;</li><li>'TrimBox' : the intended dimensions of the finished page after trimming;</li><li>'ArtBox' : the page's meaningful content (including potential white space).</li></ul>
  3600. * @param $llx (float) lower-left x coordinate in user units
  3601. * @param $lly (float) lower-left y coordinate in user units
  3602. * @param $urx (float) upper-right x coordinate in user units
  3603. * @param $ury (float) upper-right y coordinate in user units
  3604. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  3605. * @public
  3606. * @since 5.0.010 (2010-05-17)
  3607. */
  3608. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points = false) {
  3609. if (!isset($this->pagedim[$page])) {
  3610. // initialize array
  3611. $this->pagedim[$page] = array();
  3612. }
  3613. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  3614. if (!in_array($type, $pageboxes)) {
  3615. return;
  3616. }
  3617. if ($points) {
  3618. $k = 1;
  3619. } else {
  3620. $k = $this->k;
  3621. }
  3622. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  3623. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  3624. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  3625. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  3626. }
  3627. /**
  3628. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  3629. * @param $page (int) page number
  3630. * @protected
  3631. * @since 5.0.010 (2010-05-17)
  3632. */
  3633. protected function swapPageBoxCoordinates($page) {
  3634. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  3635. foreach ($pageboxes as $type) {
  3636. // swap X and Y coordinates
  3637. if (isset($this->pagedim[$page][$type])) {
  3638. $tmp = $this->pagedim[$page][$type]['llx'];
  3639. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  3640. $this->pagedim[$page][$type]['lly'] = $tmp;
  3641. $tmp = $this->pagedim[$page][$type]['urx'];
  3642. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  3643. $this->pagedim[$page][$type]['ury'] = $tmp;
  3644. }
  3645. }
  3646. }
  3647. /**
  3648. * Set page orientation.
  3649. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or Portrait (default)</li><li>L or Landscape</li><li>'' (empty string) for automatic orientation</li></ul>
  3650. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  3651. * @param $bottommargin (float) bottom margin of the page.
  3652. * @public
  3653. * @since 3.0.015 (2008-06-06)
  3654. */
  3655. public function setPageOrientation($orientation, $autopagebreak = '', $bottommargin = '') {
  3656. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  3657. // the boundaries of the physical medium on which the page shall be displayed or printed
  3658. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  3659. }
  3660. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  3661. // the visible region of default user space
  3662. $this->setPageBoxes($this->page, 'CropBox', $this->pagedim[$this->page]['MediaBox']['llx'], $this->pagedim[$this->page]['MediaBox']['lly'], $this->pagedim[$this->page]['MediaBox']['urx'], $this->pagedim[$this->page]['MediaBox']['ury'], true);
  3663. }
  3664. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  3665. // the region to which the contents of the page shall be clipped when output in a production environment
  3666. $this->setPageBoxes($this->page, 'BleedBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
  3667. }
  3668. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  3669. // the intended dimensions of the finished page after trimming
  3670. $this->setPageBoxes($this->page, 'TrimBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
  3671. }
  3672. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  3673. // the page's meaningful content (including potential white space)
  3674. $this->setPageBoxes($this->page, 'ArtBox', $this->pagedim[$this->page]['CropBox']['llx'], $this->pagedim[$this->page]['CropBox']['lly'], $this->pagedim[$this->page]['CropBox']['urx'], $this->pagedim[$this->page]['CropBox']['ury'], true);
  3675. }
  3676. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  3677. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  3678. $this->pagedim[$this->page]['Rotate'] = 0;
  3679. }
  3680. if (!isset($this->pagedim[$this->page]['PZ'])) {
  3681. // The page's preferred zoom (magnification) factor
  3682. $this->pagedim[$this->page]['PZ'] = 1;
  3683. }
  3684. if ($this->fwPt > $this->fhPt) {
  3685. // landscape
  3686. $default_orientation = 'L';
  3687. } else {
  3688. // portrait
  3689. $default_orientation = 'P';
  3690. }
  3691. $valid_orientations = array('P', 'L');
  3692. if (empty($orientation)) {
  3693. $orientation = $default_orientation;
  3694. } else {
  3695. $orientation = strtoupper($orientation{0});
  3696. }
  3697. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  3698. $this->CurOrientation = $orientation;
  3699. $this->wPt = $this->fhPt;
  3700. $this->hPt = $this->fwPt;
  3701. } else {
  3702. $this->CurOrientation = $default_orientation;
  3703. $this->wPt = $this->fwPt;
  3704. $this->hPt = $this->fhPt;
  3705. }
  3706. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)) {
  3707. // swap X and Y coordinates (change page orientation)
  3708. $this->swapPageBoxCoordinates($this->page);
  3709. }
  3710. $this->w = ($this->wPt / $this->k);
  3711. $this->h = ($this->hPt / $this->k);
  3712. if ($this->empty_string($autopagebreak)) {
  3713. if (isset($this->AutoPageBreak)) {
  3714. $autopagebreak = $this->AutoPageBreak;
  3715. } else {
  3716. $autopagebreak = true;
  3717. }
  3718. }
  3719. if ($this->empty_string($bottommargin)) {
  3720. if (isset($this->bMargin)) {
  3721. $bottommargin = $this->bMargin;
  3722. } else {
  3723. // default value = 2 cm
  3724. $bottommargin = 2 * 28.35 / $this->k;
  3725. }
  3726. }
  3727. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  3728. // store page dimensions
  3729. $this->pagedim[$this->page]['w'] = $this->wPt;
  3730. $this->pagedim[$this->page]['h'] = $this->hPt;
  3731. $this->pagedim[$this->page]['wk'] = $this->w;
  3732. $this->pagedim[$this->page]['hk'] = $this->h;
  3733. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  3734. $this->pagedim[$this->page]['bm'] = $bottommargin;
  3735. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  3736. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  3737. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  3738. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  3739. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  3740. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  3741. }
  3742. /**
  3743. * Set regular expression to detect withespaces or word separators.
  3744. * The pattern delimiter must be the forward-slash character "/".
  3745. * Some example patterns are:
  3746. * <pre>
  3747. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  3748. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  3749. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  3750. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  3751. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  3752. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  3753. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  3754. * </pre>
  3755. * @param $re (string) regular expression (leave empty for default).
  3756. * @public
  3757. * @since 4.6.016 (2009-06-15)
  3758. */
  3759. public function setSpacesRE($re = '/[^\S\xa0]/') {
  3760. $this->re_spaces = $re;
  3761. $re_parts = explode('/', $re);
  3762. // get pattern parts
  3763. $this->re_space = array();
  3764. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  3765. $this->re_space['p'] = $re_parts[1];
  3766. } else {
  3767. $this->re_space['p'] = '[\s]';
  3768. }
  3769. // set pattern modifiers
  3770. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  3771. $this->re_space['m'] = $re_parts[2];
  3772. } else {
  3773. $this->re_space['m'] = '';
  3774. }
  3775. }
  3776. /**
  3777. * Enable or disable Right-To-Left language mode
  3778. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  3779. * @param $resetx (Boolean) if true reset the X position on direction change.
  3780. * @public
  3781. * @since 2.0.000 (2008-01-03)
  3782. */
  3783. public function setRTL($enable, $resetx = true) {
  3784. $enable = $enable ? true : false;
  3785. $resetx = ($resetx AND ($enable != $this->rtl));
  3786. $this->rtl = $enable;
  3787. $this->tmprtl = false;
  3788. if ($resetx) {
  3789. $this->Ln(0);
  3790. }
  3791. }
  3792. /**
  3793. * Return the RTL status
  3794. * @return boolean
  3795. * @public
  3796. * @since 4.0.012 (2008-07-24)
  3797. */
  3798. public function getRTL() {
  3799. return $this->rtl;
  3800. }
  3801. /**
  3802. * Force temporary RTL language direction
  3803. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  3804. * @public
  3805. * @since 2.1.000 (2008-01-09)
  3806. */
  3807. public function setTempRTL($mode) {
  3808. $newmode = false;
  3809. switch (strtoupper($mode)) {
  3810. case 'LTR':
  3811. case 'L': {
  3812. if ($this->rtl) {
  3813. $newmode = 'L';
  3814. }
  3815. break;
  3816. }
  3817. case 'RTL':
  3818. case 'R': {
  3819. if (!$this->rtl) {
  3820. $newmode = 'R';
  3821. }
  3822. break;
  3823. }
  3824. case false:
  3825. default: {
  3826. $newmode = false;
  3827. break;
  3828. }
  3829. }
  3830. $this->tmprtl = $newmode;
  3831. }
  3832. /**
  3833. * Return the current temporary RTL status
  3834. * @return boolean
  3835. * @public
  3836. * @since 4.8.014 (2009-11-04)
  3837. */
  3838. public function isRTLTextDir() {
  3839. return ($this->rtl OR ($this->tmprtl == 'R'));
  3840. }
  3841. /**
  3842. * Set the last cell height.
  3843. * @param $h (float) cell height.
  3844. * @author Nicola Asuni
  3845. * @public
  3846. * @since 1.53.0.TC034
  3847. */
  3848. public function setLastH($h) {
  3849. $this->lasth = $h;
  3850. }
  3851. /**
  3852. * Reset the last cell height.
  3853. * @public
  3854. * @since 5.9.000 (2010-10-03)
  3855. */
  3856. public function resetLastH() {
  3857. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  3858. }
  3859. /**
  3860. * Get the last cell height.
  3861. * @return last cell height
  3862. * @public
  3863. * @since 4.0.017 (2008-08-05)
  3864. */
  3865. public function getLastH() {
  3866. return $this->lasth;
  3867. }
  3868. /**
  3869. * Set the adjusting factor to convert pixels to user units.
  3870. * @param $scale (float) adjusting factor to convert pixels to user units.
  3871. * @author Nicola Asuni
  3872. * @public
  3873. * @since 1.5.2
  3874. */
  3875. public function setImageScale($scale) {
  3876. $this->imgscale = $scale;
  3877. }
  3878. /**
  3879. * Returns the adjusting factor to convert pixels to user units.
  3880. * @return float adjusting factor to convert pixels to user units.
  3881. * @author Nicola Asuni
  3882. * @public
  3883. * @since 1.5.2
  3884. */
  3885. public function getImageScale() {
  3886. return $this->imgscale;
  3887. }
  3888. /**
  3889. * Returns an array of page dimensions:
  3890. * <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 user units</li><li>$this->pagedim[$this->page]['hk'] = page height in user units</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><li>$this->pagedim[$this->page]['Rotate'] = The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.</li><li>$this->pagedim[$this->page]['PZ'] = The page's preferred zoom (magnification) factor.</li><li>$this->pagedim[$this->page]['trans'] : the style and duration of the visual transition to use when moving from another page to the given page during a presentation<ul><li>$this->pagedim[$this->page]['trans']['Dur'] = The page's display duration (also called its advance timing): the maximum length of time, in seconds, that the page shall be displayed during presentations before the viewer application shall automatically advance to the next page.</li><li>$this->pagedim[$this->page]['trans']['S'] = transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li><li>$this->pagedim[$this->page]['trans']['D'] = The duration of the transition effect, in seconds.</li><li>$this->pagedim[$this->page]['trans']['Dm'] = (Split and Blinds transition styles only) The dimension in which the specified transition effect shall occur: H = Horizontal, V = Vertical. Default value: H.</li><li>$this->pagedim[$this->page]['trans']['M'] = (Split, Box and Fly transition styles only) The direction of motion for the specified transition effect: I = Inward from the edges of the page, O = Outward from the center of the pageDefault value: I.</li><li>$this->pagedim[$this->page]['trans']['Di'] = (Wipe, Glitter, Fly, Cover, Uncover and Push transition styles only) The direction in which the specified transition effect shall moves, expressed in degrees counterclockwise starting from a left-to-right direction. If the value is a number, it shall be one of: 0 = Left to right, 90 = Bottom to top (Wipe only), 180 = Right to left (Wipe only), 270 = Top to bottom, 315 = Top-left to bottom-right (Glitter only). If the value is a name, it shall be None, which is relevant only for the Fly transition when the value of SS is not 1.0. Default value: 0.</li><li>$this->pagedim[$this->page]['trans']['SS'] = (Fly transition style only) The starting or ending scale at which the changes shall be drawn. If M specifies an inward transition, the scale of the changes drawn shall progress from SS to 1.0 over the course of the transition. If M specifies an outward transition, the scale of the changes drawn shall progress from 1.0 to SS over the course of the transition. Default: 1.0. </li><li>$this->pagedim[$this->page]['trans']['B'] = (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li></ul></li><li>$this->pagedim[$this->page]['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed<ul><li>$this->pagedim[$this->page]['MediaBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['MediaBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['CropBox'] : the visible region of default user space<ul><li>$this->pagedim[$this->page]['CropBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['CropBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment<ul><li>$this->pagedim[$this->page]['BleedBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['BleedBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['TrimBox'] : the intended dimensions of the finished page after trimming<ul><li>$this->pagedim[$this->page]['TrimBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['TrimBox']['ury'] = upper-right y coordinate in points</li></ul></li><li>$this->pagedim[$this->page]['ArtBox'] : the extent of the page's meaningful content<ul><li>$this->pagedim[$this->page]['ArtBox']['llx'] = lower-left x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['lly'] = lower-left y coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['urx'] = upper-right x coordinate in points</li><li>$this->pagedim[$this->page]['ArtBox']['ury'] = upper-right y coordinate in points</li></ul></li></ul>
  3891. * @param $pagenum (int) page number (empty = current page)
  3892. * @return array of page dimensions.
  3893. * @author Nicola Asuni
  3894. * @public
  3895. * @since 4.5.027 (2009-03-16)
  3896. */
  3897. public function getPageDimensions($pagenum = '') {
  3898. if (empty($pagenum)) {
  3899. $pagenum = $this->page;
  3900. }
  3901. return $this->pagedim[$pagenum];
  3902. }
  3903. /**
  3904. * Returns the page width in units.
  3905. * @param $pagenum (int) page number (empty = current page)
  3906. * @return int page width.
  3907. * @author Nicola Asuni
  3908. * @public
  3909. * @since 1.5.2
  3910. * @see getPageDimensions()
  3911. */
  3912. public function getPageWidth($pagenum = '') {
  3913. if (empty($pagenum)) {
  3914. return $this->w;
  3915. }
  3916. return $this->pagedim[$pagenum]['w'];
  3917. }
  3918. /**
  3919. * Returns the page height in units.
  3920. * @param $pagenum (int) page number (empty = current page)
  3921. * @return int page height.
  3922. * @author Nicola Asuni
  3923. * @public
  3924. * @since 1.5.2
  3925. * @see getPageDimensions()
  3926. */
  3927. public function getPageHeight($pagenum = '') {
  3928. if (empty($pagenum)) {
  3929. return $this->h;
  3930. }
  3931. return $this->pagedim[$pagenum]['h'];
  3932. }
  3933. /**
  3934. * Returns the page break margin.
  3935. * @param $pagenum (int) page number (empty = current page)
  3936. * @return int page break margin.
  3937. * @author Nicola Asuni
  3938. * @public
  3939. * @since 1.5.2
  3940. * @see getPageDimensions()
  3941. */
  3942. public function getBreakMargin($pagenum = '') {
  3943. if (empty($pagenum)) {
  3944. return $this->bMargin;
  3945. }
  3946. return $this->pagedim[$pagenum]['bm'];
  3947. }
  3948. /**
  3949. * Returns the scale factor (number of points in user unit).
  3950. * @return int scale factor.
  3951. * @author Nicola Asuni
  3952. * @public
  3953. * @since 1.5.2
  3954. */
  3955. public function getScaleFactor() {
  3956. return $this->k;
  3957. }
  3958. /**
  3959. * Defines the left, top and right margins.
  3960. * @param $left (float) Left margin.
  3961. * @param $top (float) Top margin.
  3962. * @param $right (float) Right margin. Default value is the left one.
  3963. * @param $keepmargins (boolean) if true overwrites the default page margins
  3964. * @public
  3965. * @since 1.0
  3966. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3967. */
  3968. public function SetMargins($left, $top, $right = -1, $keepmargins = false) {
  3969. //Set left, top and right margins
  3970. $this->lMargin = $left;
  3971. $this->tMargin = $top;
  3972. if ($right == -1) {
  3973. $right = $left;
  3974. }
  3975. $this->rMargin = $right;
  3976. if ($keepmargins) {
  3977. // overwrite original values
  3978. $this->original_lMargin = $this->lMargin;
  3979. $this->original_rMargin = $this->rMargin;
  3980. }
  3981. }
  3982. /**
  3983. * 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.
  3984. * @param $margin (float) The margin.
  3985. * @public
  3986. * @since 1.4
  3987. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3988. */
  3989. public function SetLeftMargin($margin) {
  3990. //Set left margin
  3991. $this->lMargin = $margin;
  3992. if (($this->page > 0) AND ($this->x < $margin)) {
  3993. $this->x = $margin;
  3994. }
  3995. }
  3996. /**
  3997. * Defines the top margin. The method can be called before creating the first page.
  3998. * @param $margin (float) The margin.
  3999. * @public
  4000. * @since 1.5
  4001. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  4002. */
  4003. public function SetTopMargin($margin) {
  4004. //Set top margin
  4005. $this->tMargin = $margin;
  4006. if (($this->page > 0) AND ($this->y < $margin)) {
  4007. $this->y = $margin;
  4008. }
  4009. }
  4010. /**
  4011. * Defines the right margin. The method can be called before creating the first page.
  4012. * @param $margin (float) The margin.
  4013. * @public
  4014. * @since 1.5
  4015. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  4016. */
  4017. public function SetRightMargin($margin) {
  4018. $this->rMargin = $margin;
  4019. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  4020. $this->x = $this->w - $margin;
  4021. }
  4022. }
  4023. /**
  4024. * Set the same internal Cell padding for top, right, bottom, left-
  4025. * @param $pad (float) internal padding.
  4026. * @public
  4027. * @since 2.1.000 (2008-01-09)
  4028. * @see getCellPaddings(), setCellPaddings()
  4029. */
  4030. public function SetCellPadding($pad) {
  4031. if ($pad >= 0) {
  4032. $this->cell_padding['L'] = $pad;
  4033. $this->cell_padding['T'] = $pad;
  4034. $this->cell_padding['R'] = $pad;
  4035. $this->cell_padding['B'] = $pad;
  4036. }
  4037. }
  4038. /**
  4039. * Set the internal Cell paddings.
  4040. * @param $left (float) left padding
  4041. * @param $top (float) top padding
  4042. * @param $right (float) right padding
  4043. * @param $bottom (float) bottom padding
  4044. * @public
  4045. * @since 5.9.000 (2010-10-03)
  4046. * @see getCellPaddings(), SetCellPadding()
  4047. */
  4048. public function setCellPaddings($left = '', $top = '', $right = '', $bottom = '') {
  4049. if (($left !== '') AND ($left >= 0)) {
  4050. $this->cell_padding['L'] = $left;
  4051. }
  4052. if (($top !== '') AND ($top >= 0)) {
  4053. $this->cell_padding['T'] = $top;
  4054. }
  4055. if (($right !== '') AND ($right >= 0)) {
  4056. $this->cell_padding['R'] = $right;
  4057. }
  4058. if (($bottom !== '') AND ($bottom >= 0)) {
  4059. $this->cell_padding['B'] = $bottom;
  4060. }
  4061. }
  4062. /**
  4063. * Get the internal Cell padding array.
  4064. * @return array of padding values
  4065. * @public
  4066. * @since 5.9.000 (2010-10-03)
  4067. * @see setCellPaddings(), SetCellPadding()
  4068. */
  4069. public function getCellPaddings() {
  4070. return $this->cell_padding;
  4071. }
  4072. /**
  4073. * Set the internal Cell margins.
  4074. * @param $left (float) left margin
  4075. * @param $top (float) top margin
  4076. * @param $right (float) right margin
  4077. * @param $bottom (float) bottom margin
  4078. * @public
  4079. * @since 5.9.000 (2010-10-03)
  4080. * @see getCellMargins()
  4081. */
  4082. public function setCellMargins($left = '', $top = '', $right = '', $bottom = '') {
  4083. if (($left !== '') AND ($left >= 0)) {
  4084. $this->cell_margin['L'] = $left;
  4085. }
  4086. if (($top !== '') AND ($top >= 0)) {
  4087. $this->cell_margin['T'] = $top;
  4088. }
  4089. if (($right !== '') AND ($right >= 0)) {
  4090. $this->cell_margin['R'] = $right;
  4091. }
  4092. if (($bottom !== '') AND ($bottom >= 0)) {
  4093. $this->cell_margin['B'] = $bottom;
  4094. }
  4095. }
  4096. /**
  4097. * Get the internal Cell margin array.
  4098. * @return array of margin values
  4099. * @public
  4100. * @since 5.9.000 (2010-10-03)
  4101. * @see setCellMargins()
  4102. */
  4103. public function getCellMargins() {
  4104. return $this->cell_margin;
  4105. }
  4106. /**
  4107. * Adjust the internal Cell padding array to take account of the line width.
  4108. * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  4109. * @return array of adjustments
  4110. * @public
  4111. * @since 5.9.000 (2010-10-03)
  4112. */
  4113. protected function adjustCellPadding($brd = 0) {
  4114. if (empty($brd)) {
  4115. return;
  4116. }
  4117. if (is_string($brd)) {
  4118. // convert string to array
  4119. $slen = strlen($brd);
  4120. $newbrd = array();
  4121. for ($i = 0; $i < $slen; ++$i) {
  4122. $newbrd[$brd[$i]] = true;
  4123. }
  4124. $brd = $newbrd;
  4125. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  4126. $brd = array('LRTB' => true);
  4127. }
  4128. if (!is_array($brd)) {
  4129. return;
  4130. }
  4131. // store current cell padding
  4132. $cp = $this->cell_padding;
  4133. // select border mode
  4134. if (isset($brd['mode'])) {
  4135. $mode = $brd['mode'];
  4136. unset($brd['mode']);
  4137. } else {
  4138. $mode = 'normal';
  4139. }
  4140. // process borders
  4141. foreach ($brd as $border => $style) {
  4142. $line_width = $this->LineWidth;
  4143. if (is_array($style) AND isset($style['width'])) {
  4144. // get border width
  4145. $line_width = $style['width'];
  4146. }
  4147. $adj = 0; // line width inside the cell
  4148. switch ($mode) {
  4149. case 'ext': {
  4150. $adj = 0;
  4151. break;
  4152. }
  4153. case 'int': {
  4154. $adj = $line_width;
  4155. break;
  4156. }
  4157. case 'normal':
  4158. default: {
  4159. $adj = ($line_width / 2);
  4160. break;
  4161. }
  4162. }
  4163. // correct internal cell padding if required to avoid overlap between text and lines
  4164. if ((strpos($border, 'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  4165. $this->cell_padding['T'] = $adj;
  4166. }
  4167. if ((strpos($border, 'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  4168. $this->cell_padding['R'] = $adj;
  4169. }
  4170. if ((strpos($border, 'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  4171. $this->cell_padding['B'] = $adj;
  4172. }
  4173. if ((strpos($border, 'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  4174. $this->cell_padding['L'] = $adj;
  4175. }
  4176. }
  4177. return array('T' => ($this->cell_padding['T'] - $cp['T']), 'R' => ($this->cell_padding['R'] - $cp['R']), 'B' => ($this->cell_padding['B'] - $cp['B']), 'L' => ($this->cell_padding['L'] - $cp['L']));
  4178. }
  4179. /**
  4180. * 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.
  4181. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  4182. * @param $margin (float) Distance from the bottom of the page.
  4183. * @public
  4184. * @since 1.0
  4185. * @see Cell(), MultiCell(), AcceptPageBreak()
  4186. */
  4187. public function SetAutoPageBreak($auto, $margin = 0) {
  4188. $this->AutoPageBreak = $auto ? true : false;
  4189. $this->bMargin = $margin;
  4190. $this->PageBreakTrigger = $this->h - $margin;
  4191. }
  4192. /**
  4193. * Return the auto-page-break mode (true or false).
  4194. * @return boolean auto-page-break mode
  4195. * @public
  4196. * @since 5.9.088
  4197. */
  4198. public function getAutoPageBreak() {
  4199. return $this->AutoPageBreak;
  4200. }
  4201. /**
  4202. * Defines the way the document is to be displayed by the viewer.
  4203. * @param $zoom (mixed) 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>
  4204. * @param $layout (string) 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>
  4205. * @param $mode (string) 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>
  4206. * @public
  4207. * @since 1.2
  4208. */
  4209. public function SetDisplayMode($zoom, $layout = 'SinglePage', $mode = 'UseNone') {
  4210. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  4211. $this->ZoomMode = $zoom;
  4212. } else {
  4213. $this->Error('Incorrect zoom display mode: ' . $zoom);
  4214. }
  4215. switch ($layout) {
  4216. case 'default':
  4217. case 'single':
  4218. case 'SinglePage': {
  4219. $this->LayoutMode = 'SinglePage';
  4220. break;
  4221. }
  4222. case 'continuous':
  4223. case 'OneColumn': {
  4224. $this->LayoutMode = 'OneColumn';
  4225. break;
  4226. }
  4227. case 'two':
  4228. case 'TwoColumnLeft': {
  4229. $this->LayoutMode = 'TwoColumnLeft';
  4230. break;
  4231. }
  4232. case 'TwoColumnRight': {
  4233. $this->LayoutMode = 'TwoColumnRight';
  4234. break;
  4235. }
  4236. case 'TwoPageLeft': {
  4237. $this->LayoutMode = 'TwoPageLeft';
  4238. break;
  4239. }
  4240. case 'TwoPageRight': {
  4241. $this->LayoutMode = 'TwoPageRight';
  4242. break;
  4243. }
  4244. default: {
  4245. $this->LayoutMode = 'SinglePage';
  4246. }
  4247. }
  4248. switch ($mode) {
  4249. case 'UseNone': {
  4250. $this->PageMode = 'UseNone';
  4251. break;
  4252. }
  4253. case 'UseOutlines': {
  4254. $this->PageMode = 'UseOutlines';
  4255. break;
  4256. }
  4257. case 'UseThumbs': {
  4258. $this->PageMode = 'UseThumbs';
  4259. break;
  4260. }
  4261. case 'FullScreen': {
  4262. $this->PageMode = 'FullScreen';
  4263. break;
  4264. }
  4265. case 'UseOC': {
  4266. $this->PageMode = 'UseOC';
  4267. break;
  4268. }
  4269. case '': {
  4270. $this->PageMode = 'UseAttachments';
  4271. break;
  4272. }
  4273. default: {
  4274. $this->PageMode = 'UseNone';
  4275. }
  4276. }
  4277. }
  4278. /**
  4279. * 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.
  4280. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  4281. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  4282. * @public
  4283. * @since 1.4
  4284. */
  4285. public function SetCompression($compress = true) {
  4286. if (function_exists('gzcompress')) {
  4287. $this->compress = $compress ? true : false;
  4288. } else {
  4289. $this->compress = false;
  4290. }
  4291. }
  4292. /**
  4293. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  4294. * @param $mode (boolean) If true force sRGB output intent.
  4295. * @public
  4296. * @since 5.9.121 (2011-09-28)
  4297. */
  4298. public function setSRGBmode($mode = false) {
  4299. $this->force_srgb = $mode ? true : false;
  4300. }
  4301. /**
  4302. * Turn on/off Unicode mode for document information dictionary (meta tags).
  4303. * This has effect only when unicode mode is set to false.
  4304. * @param $unicode (boolean) if true set the meta information in Unicode
  4305. * @since 5.9.027 (2010-12-01)
  4306. * @public
  4307. */
  4308. public function SetDocInfoUnicode($unicode = true) {
  4309. $this->docinfounicode = $unicode ? true : false;
  4310. }
  4311. /**
  4312. * Defines the title of the document.
  4313. * @param $title (string) The title.
  4314. * @public
  4315. * @since 1.2
  4316. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  4317. */
  4318. public function SetTitle($title) {
  4319. $this->title = $title;
  4320. }
  4321. /**
  4322. * Defines the subject of the document.
  4323. * @param $subject (string) The subject.
  4324. * @public
  4325. * @since 1.2
  4326. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  4327. */
  4328. public function SetSubject($subject) {
  4329. $this->subject = $subject;
  4330. }
  4331. /**
  4332. * Defines the author of the document.
  4333. * @param $author (string) The name of the author.
  4334. * @public
  4335. * @since 1.2
  4336. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  4337. */
  4338. public function SetAuthor($author) {
  4339. $this->author = $author;
  4340. }
  4341. /**
  4342. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  4343. * @param $keywords (string) The list of keywords.
  4344. * @public
  4345. * @since 1.2
  4346. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  4347. */
  4348. public function SetKeywords($keywords) {
  4349. $this->keywords = $keywords;
  4350. }
  4351. /**
  4352. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  4353. * @param $creator (string) The name of the creator.
  4354. * @public
  4355. * @since 1.2
  4356. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  4357. */
  4358. public function SetCreator($creator) {
  4359. $this->creator = $creator;
  4360. }
  4361. /**
  4362. * 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.
  4363. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  4364. * @param $msg (string) The error message
  4365. * @public
  4366. * @since 1.0
  4367. */
  4368. public function Error($msg) {
  4369. // unset all class variables
  4370. $this->_destroy(true);
  4371. // exit program and print error
  4372. die('<strong>TCPDF ERROR: </strong>' . $msg);
  4373. }
  4374. /**
  4375. * This method begins the generation of the PDF document.
  4376. * It is not necessary to call it explicitly because AddPage() does it automatically.
  4377. * Note: no page is created by this method
  4378. * @public
  4379. * @since 1.0
  4380. * @see AddPage(), Close()
  4381. */
  4382. public function Open() {
  4383. $this->state = 1;
  4384. }
  4385. /**
  4386. * Terminates the PDF document.
  4387. * It is not necessary to call this method explicitly because Output() does it automatically.
  4388. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  4389. * @public
  4390. * @since 1.0
  4391. * @see Open(), Output()
  4392. */
  4393. public function Close() {
  4394. if ($this->state == 3) {
  4395. return;
  4396. }
  4397. if ($this->page == 0) {
  4398. $this->AddPage();
  4399. }
  4400. $this->endLayer();
  4401. if ($this->tcpdflink) {
  4402. // save current graphic settings
  4403. $gvars = $this->getGraphicVars();
  4404. $this->setEqualColumns();
  4405. $this->lastpage(true);
  4406. $this->SetAutoPageBreak(false);
  4407. $this->x = 0;
  4408. $this->y = $this->h - (1 / $this->k);
  4409. $this->lMargin = 0;
  4410. $this->_out('q');
  4411. $font = defined('PDF_FONT_NAME_MAIN') ? PDF_FONT_NAME_MAIN : 'helvetica';
  4412. $this->SetFont($font, '', 1);
  4413. $this->setTextRenderingMode(0, false, false);
  4414. $msg = "\x50\x6f\x77\x65\x72\x65\x64\x20\x62\x79\x20\x54\x43\x50\x44\x46\x20\x28\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
  4415. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  4416. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  4417. $this->_out('Q');
  4418. // restore graphic settings
  4419. $this->setGraphicVars($gvars);
  4420. }
  4421. // close page
  4422. $this->endPage();
  4423. // close document
  4424. $this->_enddoc();
  4425. // unset all class variables (except critical ones)
  4426. $this->_destroy(false);
  4427. }
  4428. /**
  4429. * Move pointer at the specified document page and update page dimensions.
  4430. * @param $pnum (int) page number (1 ... numpages)
  4431. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  4432. * @public
  4433. * @since 2.1.000 (2008-01-07)
  4434. * @see getPage(), lastpage(), getNumPages()
  4435. */
  4436. public function setPage($pnum, $resetmargins = false) {
  4437. if (($pnum == $this->page) AND ($this->state == 2)) {
  4438. return;
  4439. }
  4440. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  4441. $this->state = 2;
  4442. // save current graphic settings
  4443. //$gvars = $this->getGraphicVars();
  4444. $oldpage = $this->page;
  4445. $this->page = $pnum;
  4446. $this->wPt = $this->pagedim[$this->page]['w'];
  4447. $this->hPt = $this->pagedim[$this->page]['h'];
  4448. $this->w = $this->pagedim[$this->page]['wk'];
  4449. $this->h = $this->pagedim[$this->page]['hk'];
  4450. $this->tMargin = $this->pagedim[$this->page]['tm'];
  4451. $this->bMargin = $this->pagedim[$this->page]['bm'];
  4452. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  4453. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  4454. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  4455. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  4456. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  4457. // restore graphic settings
  4458. //$this->setGraphicVars($gvars);
  4459. if ($resetmargins) {
  4460. $this->lMargin = $this->pagedim[$this->page]['olm'];
  4461. $this->rMargin = $this->pagedim[$this->page]['orm'];
  4462. $this->SetY($this->tMargin);
  4463. } else {
  4464. // account for booklet mode
  4465. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  4466. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  4467. $this->lMargin += $deltam;
  4468. $this->rMargin -= $deltam;
  4469. }
  4470. }
  4471. } else {
  4472. $this->Error('Wrong page number on setPage() function: ' . $pnum);
  4473. }
  4474. }
  4475. /**
  4476. * Reset pointer to the last document page.
  4477. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  4478. * @public
  4479. * @since 2.0.000 (2008-01-04)
  4480. * @see setPage(), getPage(), getNumPages()
  4481. */
  4482. public function lastPage($resetmargins = false) {
  4483. $this->setPage($this->getNumPages(), $resetmargins);
  4484. }
  4485. /**
  4486. * Get current document page number.
  4487. * @return int page number
  4488. * @public
  4489. * @since 2.1.000 (2008-01-07)
  4490. * @see setPage(), lastpage(), getNumPages()
  4491. */
  4492. public function getPage() {
  4493. return $this->page;
  4494. }
  4495. /**
  4496. * Get the total number of insered pages.
  4497. * @return int number of pages
  4498. * @public
  4499. * @since 2.1.000 (2008-01-07)
  4500. * @see setPage(), getPage(), lastpage()
  4501. */
  4502. public function getNumPages() {
  4503. return $this->numpages;
  4504. }
  4505. /**
  4506. * Adds a new TOC (Table Of Content) page to the document.
  4507. * @param $orientation (string) page orientation.
  4508. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  4509. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  4510. * @public
  4511. * @since 5.0.001 (2010-05-06)
  4512. * @see AddPage(), startPage(), endPage(), endTOCPage()
  4513. */
  4514. public function addTOCPage($orientation = '', $format = '', $keepmargins = false) {
  4515. $this->AddPage($orientation, $format, $keepmargins, true);
  4516. }
  4517. /**
  4518. * Terminate the current TOC (Table Of Content) page
  4519. * @public
  4520. * @since 5.0.001 (2010-05-06)
  4521. * @see AddPage(), startPage(), endPage(), addTOCPage()
  4522. */
  4523. public function endTOCPage() {
  4524. $this->endPage(true);
  4525. }
  4526. /**
  4527. * 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).
  4528. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  4529. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  4530. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  4531. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  4532. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  4533. * @public
  4534. * @since 1.0
  4535. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  4536. */
  4537. public function AddPage($orientation = '', $format = '', $keepmargins = false, $tocpage = false) {
  4538. if ($this->inxobj) {
  4539. // we are inside an XObject template
  4540. return;
  4541. }
  4542. if (!isset($this->original_lMargin) OR $keepmargins) {
  4543. $this->original_lMargin = $this->lMargin;
  4544. }
  4545. if (!isset($this->original_rMargin) OR $keepmargins) {
  4546. $this->original_rMargin = $this->rMargin;
  4547. }
  4548. // terminate previous page
  4549. $this->endPage();
  4550. // start new page
  4551. $this->startPage($orientation, $format, $tocpage);
  4552. }
  4553. /**
  4554. * Terminate the current page
  4555. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  4556. * @public
  4557. * @since 4.2.010 (2008-11-14)
  4558. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  4559. */
  4560. public function endPage($tocpage = false) {
  4561. // check if page is already closed
  4562. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  4563. return;
  4564. }
  4565. // print page footer
  4566. $this->setFooter();
  4567. // close page
  4568. $this->_endpage();
  4569. // mark page as closed
  4570. $this->pageopen[$this->page] = false;
  4571. if ($tocpage) {
  4572. $this->tocpage = false;
  4573. }
  4574. }
  4575. /**
  4576. * Starts a new page to the document. The page must be closed using the endPage() function.
  4577. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  4578. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  4579. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  4580. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  4581. * @since 4.2.010 (2008-11-14)
  4582. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  4583. * @public
  4584. */
  4585. public function startPage($orientation = '', $format = '', $tocpage = false) {
  4586. if ($tocpage) {
  4587. $this->tocpage = true;
  4588. }
  4589. // move page numbers of documents to be attached
  4590. if ($this->tocpage) {
  4591. // move reference to unexistent pages (used for page attachments)
  4592. // adjust outlines
  4593. $tmpoutlines = $this->outlines;
  4594. foreach ($tmpoutlines as $key => $outline) {
  4595. if ($outline['p'] > $this->numpages) {
  4596. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  4597. }
  4598. }
  4599. // adjust dests
  4600. $tmpdests = $this->dests;
  4601. foreach ($tmpdests as $key => $dest) {
  4602. if ($dest['p'] > $this->numpages) {
  4603. $this->dests[$key]['p'] = ($dest['p'] + 1);
  4604. }
  4605. }
  4606. // adjust links
  4607. $tmplinks = $this->links;
  4608. foreach ($tmplinks as $key => $link) {
  4609. if ($link[0] > $this->numpages) {
  4610. $this->links[$key][0] = ($link[0] + 1);
  4611. }
  4612. }
  4613. }
  4614. if ($this->numpages > $this->page) {
  4615. // this page has been already added
  4616. $this->setPage($this->page + 1);
  4617. $this->SetY($this->tMargin);
  4618. return;
  4619. }
  4620. // start a new page
  4621. if ($this->state == 0) {
  4622. $this->Open();
  4623. }
  4624. ++$this->numpages;
  4625. $this->swapMargins($this->booklet);
  4626. // save current graphic settings
  4627. $gvars = $this->getGraphicVars();
  4628. // start new page
  4629. $this->_beginpage($orientation, $format);
  4630. // mark page as open
  4631. $this->pageopen[$this->page] = true;
  4632. // restore graphic settings
  4633. $this->setGraphicVars($gvars);
  4634. // mark this point
  4635. $this->setPageMark();
  4636. // print page header
  4637. $this->setHeader();
  4638. // restore graphic settings
  4639. $this->setGraphicVars($gvars);
  4640. // mark this point
  4641. $this->setPageMark();
  4642. // print table header (if any)
  4643. $this->setTableHeader();
  4644. // set mark for empty page check
  4645. $this->emptypagemrk[$this->page] = $this->pagelen[$this->page];
  4646. }
  4647. /**
  4648. * Set start-writing mark on current page stream used to put borders and fills.
  4649. * Borders and fills are always created after content and inserted on the position marked by this method.
  4650. * This function must be called after calling Image() function for a background image.
  4651. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  4652. * @public
  4653. * @since 4.0.016 (2008-07-30)
  4654. */
  4655. public function setPageMark() {
  4656. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  4657. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  4658. $this->setContentMark();
  4659. }
  4660. /**
  4661. * Set start-writing mark on selected page.
  4662. * Borders and fills are always created after content and inserted on the position marked by this method.
  4663. * @param $page (int) page number (default is the current page)
  4664. * @protected
  4665. * @since 4.6.021 (2009-07-20)
  4666. */
  4667. protected function setContentMark($page = 0) {
  4668. if ($page <= 0) {
  4669. $page = $this->page;
  4670. }
  4671. if (isset($this->footerlen[$page])) {
  4672. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  4673. } else {
  4674. $this->cntmrk[$page] = $this->pagelen[$page];
  4675. }
  4676. }
  4677. /**
  4678. * Set header data.
  4679. * @param $ln (string) header image logo
  4680. * @param $lw (string) header image logo width in mm
  4681. * @param $ht (string) string to print as title on document header
  4682. * @param $hs (string) string to print on document header
  4683. * @param $tc (array) RGB array color for text.
  4684. * @param $lc (array) RGB array color for line.
  4685. * @public
  4686. */
  4687. public function setHeaderData($ln = '', $lw = 0, $ht = '', $hs = '', $tc = array(0, 0, 0), $lc = array(0, 0, 0)) {
  4688. $this->header_logo = $ln;
  4689. $this->header_logo_width = $lw;
  4690. $this->header_title = $ht;
  4691. $this->header_string = $hs;
  4692. $this->header_text_color = $tc;
  4693. $this->header_line_color = $lc;
  4694. }
  4695. /**
  4696. * Set footer data.
  4697. * @param $tc (array) RGB array color for text.
  4698. * @param $lc (array) RGB array color for line.
  4699. * @public
  4700. */
  4701. public function setFooterData($tc = array(0, 0, 0), $lc = array(0, 0, 0)) {
  4702. $this->footer_text_color = $tc;
  4703. $this->footer_line_color = $lc;
  4704. }
  4705. /**
  4706. * Returns header data:
  4707. * <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>
  4708. * @return array()
  4709. * @public
  4710. * @since 4.0.012 (2008-07-24)
  4711. */
  4712. public function getHeaderData() {
  4713. $ret = array();
  4714. $ret['logo'] = $this->header_logo;
  4715. $ret['logo_width'] = $this->header_logo_width;
  4716. $ret['title'] = $this->header_title;
  4717. $ret['string'] = $this->header_string;
  4718. $ret['text_color'] = $this->header_text_color;
  4719. $ret['line_color'] = $this->header_line_color;
  4720. return $ret;
  4721. }
  4722. /**
  4723. * Set header margin.
  4724. * (minimum distance between header and top page margin)
  4725. * @param $hm (int) distance in user units
  4726. * @public
  4727. */
  4728. public function setHeaderMargin($hm = 10) {
  4729. $this->header_margin = $hm;
  4730. }
  4731. /**
  4732. * Returns header margin in user units.
  4733. * @return float
  4734. * @since 4.0.012 (2008-07-24)
  4735. * @public
  4736. */
  4737. public function getHeaderMargin() {
  4738. return $this->header_margin;
  4739. }
  4740. /**
  4741. * Set footer margin.
  4742. * (minimum distance between footer and bottom page margin)
  4743. * @param $fm (int) distance in user units
  4744. * @public
  4745. */
  4746. public function setFooterMargin($fm = 10) {
  4747. $this->footer_margin = $fm;
  4748. }
  4749. /**
  4750. * Returns footer margin in user units.
  4751. * @return float
  4752. * @since 4.0.012 (2008-07-24)
  4753. * @public
  4754. */
  4755. public function getFooterMargin() {
  4756. return $this->footer_margin;
  4757. }
  4758. /**
  4759. * Set a flag to print page header.
  4760. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  4761. * @public
  4762. */
  4763. public function setPrintHeader($val = true) {
  4764. $this->print_header = $val ? true : false;
  4765. }
  4766. /**
  4767. * Set a flag to print page footer.
  4768. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  4769. * @public
  4770. */
  4771. public function setPrintFooter($val = true) {
  4772. $this->print_footer = $val ? true : false;
  4773. }
  4774. /**
  4775. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  4776. * @return float
  4777. * @public
  4778. */
  4779. public function getImageRBX() {
  4780. return $this->img_rb_x;
  4781. }
  4782. /**
  4783. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  4784. * @return float
  4785. * @public
  4786. */
  4787. public function getImageRBY() {
  4788. return $this->img_rb_y;
  4789. }
  4790. /**
  4791. * Reset the xobject template used by Header() method.
  4792. * @public
  4793. */
  4794. public function resetHeaderTemplate() {
  4795. $this->header_xobjid = -1;
  4796. }
  4797. /**
  4798. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  4799. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  4800. * @public
  4801. */
  4802. public function setHeaderTemplateAutoreset($val = true) {
  4803. $this->header_xobj_autoreset = $val ? true : false;
  4804. }
  4805. /**
  4806. * This method is used to render the page header.
  4807. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  4808. * @public
  4809. */
  4810. public function Header() {
  4811. if ($this->header_xobjid < 0) {
  4812. // start a new XObject Template
  4813. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  4814. $headerfont = $this->getHeaderFont();
  4815. $headerdata = $this->getHeaderData();
  4816. $this->y = $this->header_margin;
  4817. if ($this->rtl) {
  4818. $this->x = $this->w - $this->original_rMargin;
  4819. } else {
  4820. $this->x = $this->original_lMargin;
  4821. }
  4822. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  4823. $imgtype = $this->getImageFileType(K_PATH_IMAGES . $headerdata['logo']);
  4824. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  4825. $this->ImageEps(K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width']);
  4826. } elseif ($imgtype == 'svg') {
  4827. $this->ImageSVG(K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width']);
  4828. } else {
  4829. $this->Image(K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width']);
  4830. }
  4831. $imgy = $this->getImageRBY();
  4832. } else {
  4833. $imgy = $this->y;
  4834. }
  4835. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  4836. // set starting margin for text data cell
  4837. if ($this->getRTL()) {
  4838. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  4839. } else {
  4840. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  4841. }
  4842. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  4843. $this->SetTextColorArray($this->header_text_color);
  4844. // header title
  4845. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  4846. $this->SetX($header_x);
  4847. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  4848. // header string
  4849. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  4850. $this->SetX($header_x);
  4851. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  4852. // print an ending header line
  4853. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
  4854. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  4855. if ($this->rtl) {
  4856. $this->SetX($this->original_rMargin);
  4857. } else {
  4858. $this->SetX($this->original_lMargin);
  4859. }
  4860. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  4861. $this->endTemplate();
  4862. }
  4863. // print header template
  4864. $x = 0;
  4865. $dx = 0;
  4866. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  4867. // adjust margins for booklet mode
  4868. $dx = ($this->original_lMargin - $this->original_rMargin);
  4869. }
  4870. if ($this->rtl) {
  4871. $x = $this->w + $dx;
  4872. } else {
  4873. $x = 0 + $dx;
  4874. }
  4875. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  4876. if ($this->header_xobj_autoreset) {
  4877. // reset header xobject template at each page
  4878. $this->header_xobjid = -1;
  4879. }
  4880. }
  4881. /**
  4882. * This method is used to render the page footer.
  4883. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  4884. * @public
  4885. */
  4886. public function Footer() {
  4887. $cur_y = $this->y;
  4888. $this->SetTextColorArray($this->footer_text_color);
  4889. //set style for cell border
  4890. $line_width = (0.85 / $this->k);
  4891. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
  4892. //print document barcode
  4893. $barcode = $this->getBarcode();
  4894. if (!empty($barcode)) {
  4895. $this->Ln($line_width);
  4896. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  4897. $style = array(
  4898. 'position' => $this->rtl ? 'R' : 'L',
  4899. 'align' => $this->rtl ? 'R' : 'L',
  4900. 'stretch' => false,
  4901. 'fitwidth' => true,
  4902. 'cellfitalign' => '',
  4903. 'border' => false,
  4904. 'padding' => 0,
  4905. 'fgcolor' => array(0, 0, 0),
  4906. 'bgcolor' => false,
  4907. 'text' => false
  4908. );
  4909. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  4910. }
  4911. $w_page = isset($this->l['w_page']) ? $this->l['w_page'] . ' ' : '';
  4912. if (empty($this->pagegroups)) {
  4913. $pagenumtxt = $w_page . $this->getAliasNumPage() . ' / ' . $this->getAliasNbPages();
  4914. } else {
  4915. $pagenumtxt = $w_page . $this->getPageNumGroupAlias() . ' / ' . $this->getPageGroupAlias();
  4916. }
  4917. $this->SetY($cur_y);
  4918. //Print page number
  4919. if ($this->getRTL()) {
  4920. $this->SetX($this->original_rMargin);
  4921. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  4922. } else {
  4923. $this->SetX($this->original_lMargin);
  4924. $this->Cell(0, 0, $this->getAliasRightShift() . $pagenumtxt, 'T', 0, 'R');
  4925. }
  4926. }
  4927. /**
  4928. * This method is used to render the page header.
  4929. * @protected
  4930. * @since 4.0.012 (2008-07-24)
  4931. */
  4932. protected function setHeader() {
  4933. if (!$this->print_header OR ($this->state != 2)) {
  4934. return;
  4935. }
  4936. $this->InHeader = true;
  4937. $this->setGraphicVars($this->default_graphic_vars);
  4938. $temp_thead = $this->thead;
  4939. $temp_theadMargins = $this->theadMargins;
  4940. $lasth = $this->lasth;
  4941. $this->_out('q');
  4942. $this->rMargin = $this->original_rMargin;
  4943. $this->lMargin = $this->original_lMargin;
  4944. $this->SetCellPadding(0);
  4945. //set current position
  4946. if ($this->rtl) {
  4947. $this->SetXY($this->original_rMargin, $this->header_margin);
  4948. } else {
  4949. $this->SetXY($this->original_lMargin, $this->header_margin);
  4950. }
  4951. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  4952. $this->Header();
  4953. //restore position
  4954. if ($this->rtl) {
  4955. $this->SetXY($this->original_rMargin, $this->tMargin);
  4956. } else {
  4957. $this->SetXY($this->original_lMargin, $this->tMargin);
  4958. }
  4959. $this->_out('Q');
  4960. $this->lasth = $lasth;
  4961. $this->thead = $temp_thead;
  4962. $this->theadMargins = $temp_theadMargins;
  4963. $this->newline = false;
  4964. $this->InHeader = false;
  4965. }
  4966. /**
  4967. * This method is used to render the page footer.
  4968. * @protected
  4969. * @since 4.0.012 (2008-07-24)
  4970. */
  4971. protected function setFooter() {
  4972. if ($this->state != 2) {
  4973. return;
  4974. }
  4975. $this->InFooter = true;
  4976. // save current graphic settings
  4977. $gvars = $this->getGraphicVars();
  4978. // mark this point
  4979. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4980. $this->_out("\n");
  4981. if ($this->print_footer) {
  4982. $this->setGraphicVars($this->default_graphic_vars);
  4983. $this->current_column = 0;
  4984. $this->num_columns = 1;
  4985. $temp_thead = $this->thead;
  4986. $temp_theadMargins = $this->theadMargins;
  4987. $lasth = $this->lasth;
  4988. $this->_out('q');
  4989. $this->rMargin = $this->original_rMargin;
  4990. $this->lMargin = $this->original_lMargin;
  4991. $this->SetCellPadding(0);
  4992. //set current position
  4993. $footer_y = $this->h - $this->footer_margin;
  4994. if ($this->rtl) {
  4995. $this->SetXY($this->original_rMargin, $footer_y);
  4996. } else {
  4997. $this->SetXY($this->original_lMargin, $footer_y);
  4998. }
  4999. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  5000. $this->Footer();
  5001. //restore position
  5002. if ($this->rtl) {
  5003. $this->SetXY($this->original_rMargin, $this->tMargin);
  5004. } else {
  5005. $this->SetXY($this->original_lMargin, $this->tMargin);
  5006. }
  5007. $this->_out('Q');
  5008. $this->lasth = $lasth;
  5009. $this->thead = $temp_thead;
  5010. $this->theadMargins = $temp_theadMargins;
  5011. }
  5012. // restore graphic settings
  5013. $this->setGraphicVars($gvars);
  5014. $this->current_column = $gvars['current_column'];
  5015. $this->num_columns = $gvars['num_columns'];
  5016. // calculate footer length
  5017. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  5018. $this->InFooter = false;
  5019. }
  5020. /**
  5021. * Check if we are on the page body (excluding page header and footer).
  5022. * @return true if we are not in page header nor in page footer, false otherwise.
  5023. * @protected
  5024. * @since 5.9.091 (2011-06-15)
  5025. */
  5026. protected function inPageBody() {
  5027. return (($this->InHeader === false) AND ($this->InFooter === false));
  5028. }
  5029. /**
  5030. * This method is used to render the table header on new page (if any).
  5031. * @protected
  5032. * @since 4.5.030 (2009-03-25)
  5033. */
  5034. protected function setTableHeader() {
  5035. if ($this->num_columns > 1) {
  5036. // multi column mode
  5037. return;
  5038. }
  5039. if (isset($this->theadMargins['top'])) {
  5040. // restore the original top-margin
  5041. $this->tMargin = $this->theadMargins['top'];
  5042. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  5043. $this->y = $this->tMargin;
  5044. }
  5045. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  5046. // set margins
  5047. $prev_lMargin = $this->lMargin;
  5048. $prev_rMargin = $this->rMargin;
  5049. $prev_cell_padding = $this->cell_padding;
  5050. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  5051. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  5052. $this->cell_padding = $this->theadMargins['cell_padding'];
  5053. if ($this->rtl) {
  5054. $this->x = $this->w - $this->rMargin;
  5055. } else {
  5056. $this->x = $this->lMargin;
  5057. }
  5058. // account for special "cell" mode
  5059. if ($this->theadMargins['cell']) {
  5060. if ($this->rtl) {
  5061. $this->x -= $this->cell_padding['R'];
  5062. } else {
  5063. $this->x += $this->cell_padding['L'];
  5064. }
  5065. }
  5066. // print table header
  5067. $this->writeHTML($this->thead, false, false, false, false, '');
  5068. // set new top margin to skip the table headers
  5069. if (!isset($this->theadMargins['top'])) {
  5070. $this->theadMargins['top'] = $this->tMargin;
  5071. }
  5072. // store end of header position
  5073. if (!isset($this->columns[0]['th'])) {
  5074. $this->columns[0]['th'] = array();
  5075. }
  5076. $this->columns[0]['th']['\'' . $this->page . '\''] = $this->y;
  5077. $this->tMargin = $this->y;
  5078. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  5079. $this->lasth = 0;
  5080. $this->lMargin = $prev_lMargin;
  5081. $this->rMargin = $prev_rMargin;
  5082. $this->cell_padding = $prev_cell_padding;
  5083. }
  5084. }
  5085. /**
  5086. * Returns the current page number.
  5087. * @return int page number
  5088. * @public
  5089. * @since 1.0
  5090. * @see getAliasNbPages()
  5091. */
  5092. public function PageNo() {
  5093. return $this->page;
  5094. }
  5095. /**
  5096. * Defines a new spot color.
  5097. * It can be expressed in RGB components or gray scale.
  5098. * The method can be called before the first page is created and the value is retained from page to page.
  5099. * @param $name (string) Full name of the spot color.
  5100. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  5101. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  5102. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  5103. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  5104. * @public
  5105. * @since 4.0.024 (2008-09-12)
  5106. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  5107. */
  5108. public function AddSpotColor($name, $c, $m, $y, $k) {
  5109. if (!isset($this->spot_colors[$name])) {
  5110. $i = (1 + count($this->spot_colors));
  5111. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  5112. }
  5113. }
  5114. /**
  5115. * Return the Spot color array.
  5116. * @param $name (string) Name of the spot color.
  5117. * @return (array) Spot color array or false if not defined.
  5118. * @public
  5119. * @since 5.9.125 (2011-10-03)
  5120. */
  5121. public function getSpotColor($name) {
  5122. if (isset($this->spot_colors[$name])) {
  5123. return $this->spot_colors[$name];
  5124. }
  5125. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  5126. $color = strtolower($color);
  5127. if (isset($this->spotcolor[$color])) {
  5128. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  5129. return $this->spot_colors[$this->spotcolor[$color][4]];
  5130. }
  5131. return false;
  5132. }
  5133. /**
  5134. * Set the spot color for the specified type ('draw', 'fill', 'text').
  5135. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  5136. * @param $name (string) Name of the spot color.
  5137. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  5138. * @return (string) PDF color command.
  5139. * @public
  5140. * @since 5.9.125 (2011-10-03)
  5141. */
  5142. public function setSpotColor($type, $name, $tint = 100) {
  5143. $spotcolor = $this->getSpotColor($name);
  5144. if ($spotcolor === false) {
  5145. $this->Error('Undefined spot color: ' . $name . ', you must add it on the spotcolors.php file.');
  5146. }
  5147. $tint = (max(0, min(100, $tint)) / 100);
  5148. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  5149. switch ($type) {
  5150. case 'draw': {
  5151. $pdfcolor .= sprintf('CS %F SCN', $tint);
  5152. $this->DrawColor = $pdfcolor;
  5153. $this->strokecolor = $spotcolor;
  5154. break;
  5155. }
  5156. case 'fill': {
  5157. $pdfcolor .= sprintf('cs %F scn', $tint);
  5158. $this->FillColor = $pdfcolor;
  5159. $this->bgcolor = $spotcolor;
  5160. break;
  5161. }
  5162. case 'text': {
  5163. $pdfcolor .= sprintf('cs %F scn', $tint);
  5164. $this->TextColor = $pdfcolor;
  5165. $this->fgcolor = $spotcolor;
  5166. break;
  5167. }
  5168. }
  5169. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  5170. if ($this->state == 2) {
  5171. $this->_out($pdfcolor);
  5172. }
  5173. if ($this->inxobj) {
  5174. // we are inside an XObject template
  5175. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  5176. }
  5177. return $pdfcolor;
  5178. }
  5179. /**
  5180. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  5181. * @param $name (string) Name of the spot color.
  5182. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  5183. * @public
  5184. * @since 4.0.024 (2008-09-12)
  5185. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  5186. */
  5187. public function SetDrawSpotColor($name, $tint = 100) {
  5188. $this->setSpotColor('draw', $name, $tint);
  5189. }
  5190. /**
  5191. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  5192. * @param $name (string) Name of the spot color.
  5193. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  5194. * @public
  5195. * @since 4.0.024 (2008-09-12)
  5196. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  5197. */
  5198. public function SetFillSpotColor($name, $tint = 100) {
  5199. $this->setSpotColor('fill', $name, $tint);
  5200. }
  5201. /**
  5202. * Defines the spot color used for text.
  5203. * @param $name (string) Name of the spot color.
  5204. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  5205. * @public
  5206. * @since 4.0.024 (2008-09-12)
  5207. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  5208. */
  5209. public function SetTextSpotColor($name, $tint = 100) {
  5210. $this->setSpotColor('text', $name, $tint);
  5211. }
  5212. /**
  5213. * Set the color array for the specified type ('draw', 'fill', 'text').
  5214. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  5215. * The method can be called before the first page is created and the value is retained from page to page.
  5216. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  5217. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  5218. * @param $ret (boolean) If true do not send the PDF command.
  5219. * @return (string) The PDF command or empty string.
  5220. * @public
  5221. * @since 3.1.000 (2008-06-11)
  5222. */
  5223. public function setColorArray($type, $color, $ret = false) {
  5224. if (is_array($color)) {
  5225. $color = array_values($color);
  5226. // component: grey, RGB red or CMYK cyan
  5227. $c = isset($color[0]) ? $color[0] : -1;
  5228. // component: RGB green or CMYK magenta
  5229. $m = isset($color[1]) ? $color[1] : -1;
  5230. // component: RGB blue or CMYK yellow
  5231. $y = isset($color[2]) ? $color[2] : -1;
  5232. // component: CMYK black
  5233. $k = isset($color[3]) ? $color[3] : -1;
  5234. // color name
  5235. $name = isset($color[4]) ? $color[4] : '';
  5236. if ($c >= 0) {
  5237. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  5238. }
  5239. }
  5240. return '';
  5241. }
  5242. /**
  5243. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  5244. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  5245. * The method can be called before the first page is created and the value is retained from page to page.
  5246. * @param $color (array) Array of colors (1, 3 or 4 values).
  5247. * @param $ret (boolean) If true do not send the PDF command.
  5248. * @return string the PDF command
  5249. * @public
  5250. * @since 3.1.000 (2008-06-11)
  5251. * @see SetDrawColor()
  5252. */
  5253. public function SetDrawColorArray($color, $ret = false) {
  5254. return $this->setColorArray('draw', $color, $ret);
  5255. }
  5256. /**
  5257. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  5258. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  5259. * The method can be called before the first page is created and the value is retained from page to page.
  5260. * @param $color (array) Array of colors (1, 3 or 4 values).
  5261. * @param $ret (boolean) If true do not send the PDF command.
  5262. * @public
  5263. * @since 3.1.000 (2008-6-11)
  5264. * @see SetFillColor()
  5265. */
  5266. public function SetFillColorArray($color, $ret = false) {
  5267. return $this->setColorArray('fill', $color, $ret);
  5268. }
  5269. /**
  5270. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  5271. * The method can be called before the first page is created and the value is retained from page to page.
  5272. * @param $color (array) Array of colors (1, 3 or 4 values).
  5273. * @param $ret (boolean) If true do not send the PDF command.
  5274. * @public
  5275. * @since 3.1.000 (2008-6-11)
  5276. * @see SetFillColor()
  5277. */
  5278. public function SetTextColorArray($color, $ret = false) {
  5279. return $this->setColorArray('text', $color, $ret);
  5280. }
  5281. /**
  5282. * Defines the color used by the specified type ('draw', 'fill', 'text').
  5283. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  5284. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  5285. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  5286. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  5287. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  5288. * @param $ret (boolean) If true do not send the command.
  5289. * @param $name (string) spot color name (if any)
  5290. * @return (string) The PDF command or empty string.
  5291. * @public
  5292. * @since 5.9.125 (2011-10-03)
  5293. */
  5294. public function setColor($type, $col1 = 0, $col2 = -1, $col3 = -1, $col4 = -1, $ret = false, $name = '') {
  5295. // set default values
  5296. if (!is_numeric($col1)) {
  5297. $col1 = 0;
  5298. }
  5299. if (!is_numeric($col2)) {
  5300. $col2 = -1;
  5301. }
  5302. if (!is_numeric($col3)) {
  5303. $col3 = -1;
  5304. }
  5305. if (!is_numeric($col4)) {
  5306. $col4 = -1;
  5307. }
  5308. // set color by case
  5309. $suffix = '';
  5310. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  5311. // Grey scale
  5312. $col1 = max(0, min(255, $col1));
  5313. $intcolor = array('G' => $col1);
  5314. $pdfcolor = sprintf('%F ', ($col1 / 255));
  5315. $suffix = 'g';
  5316. } elseif ($col4 == -1) {
  5317. // RGB
  5318. $col1 = max(0, min(255, $col1));
  5319. $col2 = max(0, min(255, $col2));
  5320. $col3 = max(0, min(255, $col3));
  5321. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  5322. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  5323. $suffix = 'rg';
  5324. } else {
  5325. $col1 = max(0, min(100, $col1));
  5326. $col2 = max(0, min(100, $col2));
  5327. $col3 = max(0, min(100, $col3));
  5328. $col4 = max(0, min(100, $col4));
  5329. if (empty($name)) {
  5330. // CMYK
  5331. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  5332. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  5333. $suffix = 'k';
  5334. } else {
  5335. // SPOT COLOR
  5336. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  5337. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  5338. $pdfcolor = $this->setSpotColor($type, $name, 100);
  5339. }
  5340. }
  5341. switch ($type) {
  5342. case 'draw': {
  5343. $pdfcolor .= strtoupper($suffix);
  5344. $this->DrawColor = $pdfcolor;
  5345. $this->strokecolor = $intcolor;
  5346. break;
  5347. }
  5348. case 'fill': {
  5349. $pdfcolor .= $suffix;
  5350. $this->FillColor = $pdfcolor;
  5351. $this->bgcolor = $intcolor;
  5352. break;
  5353. }
  5354. case 'text': {
  5355. $pdfcolor .= $suffix;
  5356. $this->TextColor = $pdfcolor;
  5357. $this->fgcolor = $intcolor;
  5358. break;
  5359. }
  5360. }
  5361. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  5362. if (($type != 'text') AND ($this->state == 2)) {
  5363. if (!$ret) {
  5364. $this->_out($pdfcolor);
  5365. }
  5366. return $pdfcolor;
  5367. }
  5368. return '';
  5369. }
  5370. /**
  5371. * Convert a color array into a string representation.
  5372. * @param $c (array) Array of colors.
  5373. * @return (string) The color array representation.
  5374. * @protected
  5375. * @since 5.9.137 (2011-12-01)
  5376. */
  5377. protected function getColorStringFromArray($c) {
  5378. $c = array_values($c);
  5379. $color = '[';
  5380. switch (count($c)) {
  5381. case 4: {
  5382. // CMYK
  5383. $color .= sprintf('%F %F %F %F', (max(0, min(100, floatval($c[0]))) / 100), (max(0, min(100, floatval($c[1]))) / 100), (max(0, min(100, floatval($c[2]))) / 100), (max(0, min(100, floatval($c[3]))) / 100));
  5384. break;
  5385. }
  5386. case 3: {
  5387. // RGB
  5388. $color .= sprintf('%F %F %F', (max(0, min(255, floatval($c[0]))) / 255), (max(0, min(255, floatval($c[1]))) / 255), (max(0, min(255, floatval($c[2]))) / 255));
  5389. break;
  5390. }
  5391. case 1: {
  5392. // grayscale
  5393. $color .= sprintf('%F', (max(0, min(255, floatval($c[0]))) / 255));
  5394. break;
  5395. }
  5396. }
  5397. $color .= ']';
  5398. return $color;
  5399. }
  5400. /**
  5401. * 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.
  5402. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  5403. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  5404. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  5405. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  5406. * @param $ret (boolean) If true do not send the command.
  5407. * @param $name (string) spot color name (if any)
  5408. * @return string the PDF command
  5409. * @public
  5410. * @since 1.3
  5411. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  5412. */
  5413. public function SetDrawColor($col1 = 0, $col2 = -1, $col3 = -1, $col4 = -1, $ret = false, $name = '') {
  5414. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  5415. }
  5416. /**
  5417. * 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.
  5418. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  5419. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  5420. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  5421. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  5422. * @param $ret (boolean) If true do not send the command.
  5423. * @param $name (string) Spot color name (if any).
  5424. * @return (string) The PDF command.
  5425. * @public
  5426. * @since 1.3
  5427. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  5428. */
  5429. public function SetFillColor($col1 = 0, $col2 = -1, $col3 = -1, $col4 = -1, $ret = false, $name = '') {
  5430. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  5431. }
  5432. /**
  5433. * 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.
  5434. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  5435. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  5436. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  5437. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  5438. * @param $ret (boolean) If true do not send the command.
  5439. * @param $name (string) Spot color name (if any).
  5440. * @return (string) Empty string.
  5441. * @public
  5442. * @since 1.3
  5443. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  5444. */
  5445. public function SetTextColor($col1 = 0, $col2 = -1, $col3 = -1, $col4 = -1, $ret = false, $name = '') {
  5446. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  5447. }
  5448. /**
  5449. * Returns the length of a string in user unit. A font must be selected.<br>
  5450. * @param $s (string) The string whose length is to be computed
  5451. * @param $fontname (string) 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.
  5452. * @param $fontstyle (string) 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><li>O: overline</li></ul> or any combination. The default value is regular.
  5453. * @param $fontsize (float) Font size in points. The default value is the current size.
  5454. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  5455. * @return mixed int total string length or array of characted widths
  5456. * @author Nicola Asuni
  5457. * @public
  5458. * @since 1.2
  5459. */
  5460. public function GetStringWidth($s, $fontname = '', $fontstyle = '', $fontsize = 0, $getarray = false) {
  5461. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  5462. }
  5463. /**
  5464. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  5465. * @param $sa (string) The array of chars whose total length is to be computed
  5466. * @param $fontname (string) 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.
  5467. * @param $fontstyle (string) 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><li>O: overline</li></ul> or any combination. The default value is regular.
  5468. * @param $fontsize (float) Font size in points. The default value is the current size.
  5469. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  5470. * @return mixed int total string length or array of characted widths
  5471. * @author Nicola Asuni
  5472. * @public
  5473. * @since 2.4.000 (2008-03-06)
  5474. */
  5475. public function GetArrStringWidth($sa, $fontname = '', $fontstyle = '', $fontsize = 0, $getarray = false) {
  5476. // store current values
  5477. if (!$this->empty_string($fontname)) {
  5478. $prev_FontFamily = $this->FontFamily;
  5479. $prev_FontStyle = $this->FontStyle;
  5480. $prev_FontSizePt = $this->FontSizePt;
  5481. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  5482. }
  5483. // convert UTF-8 array to Latin1 if required
  5484. $sa = $this->UTF8ArrToLatin1($sa);
  5485. $w = 0; // total width
  5486. $wa = array(); // array of characters widths
  5487. foreach ($sa as $ck => $char) {
  5488. // character width
  5489. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  5490. $wa[] = $cw;
  5491. $w += $cw;
  5492. }
  5493. // restore previous values
  5494. if (!$this->empty_string($fontname)) {
  5495. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  5496. }
  5497. if ($getarray) {
  5498. return $wa;
  5499. }
  5500. return $w;
  5501. }
  5502. /**
  5503. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
  5504. * @param $char (int) The char code whose length is to be returned
  5505. * @param $notlast (boolean) If false ignore the font-spacing.
  5506. * @return float char width
  5507. * @author Nicola Asuni
  5508. * @public
  5509. * @since 2.4.000 (2008-03-06)
  5510. */
  5511. public function GetCharWidth($char, $notlast = true) {
  5512. // get raw width
  5513. $chw = $this->getRawCharWidth($char);
  5514. if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
  5515. // increase/decrease font spacing
  5516. $chw += $this->font_spacing;
  5517. }
  5518. if ($this->font_stretching != 100) {
  5519. // fixed stretching mode
  5520. $chw *= ($this->font_stretching / 100);
  5521. }
  5522. return $chw;
  5523. }
  5524. /**
  5525. * Returns the length of the char in user unit for the current font.
  5526. * @param $char (int) The char code whose length is to be returned
  5527. * @return float char width
  5528. * @author Nicola Asuni
  5529. * @public
  5530. * @since 5.9.000 (2010-09-28)
  5531. */
  5532. public function getRawCharWidth($char) {
  5533. if ($char == 173) {
  5534. // SHY character will not be printed
  5535. return (0);
  5536. }
  5537. if (isset($this->CurrentFont['cw'][$char])) {
  5538. $w = $this->CurrentFont['cw'][$char];
  5539. } elseif (isset($this->CurrentFont['dw'])) {
  5540. // default width
  5541. $w = $this->CurrentFont['dw'];
  5542. } elseif (isset($this->CurrentFont['cw'][32])) {
  5543. // default width
  5544. $w = $this->CurrentFont['cw'][32];
  5545. } else {
  5546. $w = 600;
  5547. }
  5548. return $this->getAbsFontMeasure($w);
  5549. }
  5550. /**
  5551. * Returns the numbero of characters in a string.
  5552. * @param $s (string) The input string.
  5553. * @return int number of characters
  5554. * @public
  5555. * @since 2.0.0001 (2008-01-07)
  5556. */
  5557. public function GetNumChars($s) {
  5558. if ($this->isUnicodeFont()) {
  5559. return count($this->UTF8StringToArray($s));
  5560. }
  5561. return strlen($s);
  5562. }
  5563. /**
  5564. * Fill the list of available fonts ($this->fontlist).
  5565. * @protected
  5566. * @since 4.0.013 (2008-07-28)
  5567. */
  5568. protected function getFontsList() {
  5569. $fontsdir = opendir($this->_getfontpath());
  5570. while (($file = readdir($fontsdir)) !== false) {
  5571. if (substr($file, -4) == '.php') {
  5572. array_push($this->fontlist, strtolower(basename($file, '.php')));
  5573. }
  5574. }
  5575. closedir($fontsdir);
  5576. }
  5577. /**
  5578. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  5579. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  5580. * 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.
  5581. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  5582. * @param $style (string) 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>
  5583. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  5584. * @return array containing the font data, or false in case of error.
  5585. * @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
  5586. * @public
  5587. * @since 1.5
  5588. * @see SetFont(), setFontSubsetting()
  5589. */
  5590. public function AddFont($family, $style = '', $fontfile = '', $subset = 'default') {
  5591. if ($subset === 'default') {
  5592. $subset = $this->font_subsetting;
  5593. }
  5594. if ($this->pdfa_mode) {
  5595. $subset = false;
  5596. }
  5597. if ($this->empty_string($family)) {
  5598. if (!$this->empty_string($this->FontFamily)) {
  5599. $family = $this->FontFamily;
  5600. } else {
  5601. $this->Error('Empty font family');
  5602. }
  5603. }
  5604. // move embedded styles on $style
  5605. if (substr($family, -1) == 'I') {
  5606. $style .= 'I';
  5607. $family = substr($family, 0, -1);
  5608. }
  5609. if (substr($family, -1) == 'B') {
  5610. $style .= 'B';
  5611. $family = substr($family, 0, -1);
  5612. }
  5613. // normalize family name
  5614. $family = strtolower($family);
  5615. if ((!$this->isunicode) AND ($family == 'arial')) {
  5616. $family = 'helvetica';
  5617. }
  5618. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  5619. $style = '';
  5620. }
  5621. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  5622. // all fonts must be embedded
  5623. $family = 'pdfa' . $family;
  5624. }
  5625. $tempstyle = strtoupper($style);
  5626. $style = '';
  5627. // underline
  5628. if (strpos($tempstyle, 'U') !== false) {
  5629. $this->underline = true;
  5630. } else {
  5631. $this->underline = false;
  5632. }
  5633. // line-through (deleted)
  5634. if (strpos($tempstyle, 'D') !== false) {
  5635. $this->linethrough = true;
  5636. } else {
  5637. $this->linethrough = false;
  5638. }
  5639. // overline
  5640. if (strpos($tempstyle, 'O') !== false) {
  5641. $this->overline = true;
  5642. } else {
  5643. $this->overline = false;
  5644. }
  5645. // bold
  5646. if (strpos($tempstyle, 'B') !== false) {
  5647. $style .= 'B';
  5648. }
  5649. // oblique
  5650. if (strpos($tempstyle, 'I') !== false) {
  5651. $style .= 'I';
  5652. }
  5653. $bistyle = $style;
  5654. $fontkey = $family . $style;
  5655. $font_style = $style . ($this->underline ? 'U' : '') . ($this->linethrough ? 'D' : '') . ($this->overline ? 'O' : '');
  5656. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  5657. // check if the font has been already added
  5658. $fb = $this->getFontBuffer($fontkey);
  5659. if ($fb !== false) {
  5660. if ($this->inxobj) {
  5661. // we are inside an XObject template
  5662. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  5663. }
  5664. return $fontdata;
  5665. }
  5666. if (isset($type)) {
  5667. unset($type);
  5668. }
  5669. if (isset($cw)) {
  5670. unset($cw);
  5671. }
  5672. // get specified font directory (if any)
  5673. $fontdir = false;
  5674. if (!$this->empty_string($fontfile)) {
  5675. $fontdir = dirname($fontfile);
  5676. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  5677. $fontdir = '';
  5678. } else {
  5679. $fontdir .= '/';
  5680. }
  5681. }
  5682. $missing_style = false; // true when the font style variation is missing
  5683. // search and include font file
  5684. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  5685. // build a standard filenames for specified font
  5686. $tmp_fontfile = str_replace(' ', '', $family) . strtolower($style) . '.php';
  5687. // search files on various directories
  5688. if (($fontdir !== false) AND file_exists($fontdir . $tmp_fontfile)) {
  5689. $fontfile = $fontdir . $tmp_fontfile;
  5690. } elseif (file_exists($this->_getfontpath() . $tmp_fontfile)) {
  5691. $fontfile = $this->_getfontpath() . $tmp_fontfile;
  5692. } elseif (file_exists($tmp_fontfile)) {
  5693. $fontfile = $tmp_fontfile;
  5694. } elseif (!$this->empty_string($style)) {
  5695. $missing_style = true;
  5696. // try to remove the style part
  5697. $tmp_fontfile = str_replace(' ', '', $family) . '.php';
  5698. if (($fontdir !== false) AND file_exists($fontdir . $tmp_fontfile)) {
  5699. $fontfile = $fontdir . $tmp_fontfile;
  5700. } elseif (file_exists($this->_getfontpath() . $tmp_fontfile)) {
  5701. $fontfile = $this->_getfontpath() . $tmp_fontfile;
  5702. } else {
  5703. $fontfile = $tmp_fontfile;
  5704. }
  5705. }
  5706. }
  5707. // include font file
  5708. if (file_exists($fontfile)) {
  5709. include($fontfile);
  5710. } else {
  5711. $this->Error('Could not include font definition file: ' . $family . '');
  5712. }
  5713. // check font parameters
  5714. if ((!isset($type)) OR (!isset($cw))) {
  5715. $this->Error('The font definition file has a bad format: ' . $fontfile . '');
  5716. }
  5717. // SET default parameters
  5718. if (!isset($file) OR $this->empty_string($file)) {
  5719. $file = '';
  5720. }
  5721. if (!isset($enc) OR $this->empty_string($enc)) {
  5722. $enc = '';
  5723. }
  5724. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  5725. $cidinfo = array('Registry' => 'Adobe', 'Ordering' => 'Identity', 'Supplement' => 0);
  5726. $cidinfo['uni2cid'] = array();
  5727. }
  5728. if (!isset($ctg) OR $this->empty_string($ctg)) {
  5729. $ctg = '';
  5730. }
  5731. if (!isset($desc) OR $this->empty_string($desc)) {
  5732. $desc = array();
  5733. }
  5734. if (!isset($up) OR $this->empty_string($up)) {
  5735. $up = -100;
  5736. }
  5737. if (!isset($ut) OR $this->empty_string($ut)) {
  5738. $ut = 50;
  5739. }
  5740. if (!isset($cw) OR $this->empty_string($cw)) {
  5741. $cw = array();
  5742. }
  5743. if (!isset($dw) OR $this->empty_string($dw)) {
  5744. // set default width
  5745. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  5746. $dw = $desc['MissingWidth'];
  5747. } elseif (isset($cw[32])) {
  5748. $dw = $cw[32];
  5749. } else {
  5750. $dw = 600;
  5751. }
  5752. }
  5753. ++$this->numfonts;
  5754. if ($type == 'core') {
  5755. $name = $this->CoreFonts[$fontkey];
  5756. $subset = false;
  5757. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  5758. $subset = false;
  5759. } elseif ($type == 'TrueTypeUnicode') {
  5760. $enc = 'Identity-H';
  5761. } elseif ($type == 'cidfont0') {
  5762. if ($this->pdfa_mode) {
  5763. $this->Error('All fonts must be embedded in PDF/A mode!');
  5764. }
  5765. } else {
  5766. $this->Error('Unknow font type: ' . $type . '');
  5767. }
  5768. // set name if unset
  5769. if (!isset($name) OR empty($name)) {
  5770. $name = $fontkey;
  5771. }
  5772. // create artificial font style variations if missing (only works with non-embedded fonts)
  5773. if (($type != 'core') AND $missing_style) {
  5774. // style variations
  5775. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  5776. $name .= $styles[$bistyle];
  5777. // artificial bold
  5778. if (strpos($bistyle, 'B') !== false) {
  5779. if (isset($desc['StemV'])) {
  5780. // from normal to bold
  5781. $desc['StemV'] = round($desc['StemV'] * 1.75);
  5782. } else {
  5783. // bold
  5784. $desc['StemV'] = 123;
  5785. }
  5786. }
  5787. // artificial italic
  5788. if (strpos($bistyle, 'I') !== false) {
  5789. if (isset($desc['ItalicAngle'])) {
  5790. $desc['ItalicAngle'] -= 11;
  5791. } else {
  5792. $desc['ItalicAngle'] = -11;
  5793. }
  5794. if (isset($desc['Flags'])) {
  5795. $desc['Flags'] |= 64; //bit 7
  5796. } else {
  5797. $desc['Flags'] = 64;
  5798. }
  5799. }
  5800. }
  5801. // check if the array of characters bounding boxes is defined
  5802. if (!isset($cbbox)) {
  5803. $cbbox = array();
  5804. }
  5805. // initialize subsetchars
  5806. $subsetchars = array_fill(0, 255, true);
  5807. $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'cbbox' => $cbbox, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
  5808. if ($this->inxobj) {
  5809. // we are inside an XObject template
  5810. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  5811. }
  5812. if (isset($diff) AND (!empty($diff))) {
  5813. //Search existing encodings
  5814. $d = 0;
  5815. $nb = count($this->diffs);
  5816. for ($i = 1; $i <= $nb; ++$i) {
  5817. if ($this->diffs[$i] == $diff) {
  5818. $d = $i;
  5819. break;
  5820. }
  5821. }
  5822. if ($d == 0) {
  5823. $d = $nb + 1;
  5824. $this->diffs[$d] = $diff;
  5825. }
  5826. $this->setFontSubBuffer($fontkey, 'diff', $d);
  5827. }
  5828. if (!$this->empty_string($file)) {
  5829. if (!isset($this->FontFiles[$file])) {
  5830. if ((strcasecmp($type, 'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  5831. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  5832. } elseif ($type != 'core') {
  5833. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  5834. }
  5835. } else {
  5836. // update fontkeys that are sharing this font file
  5837. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  5838. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  5839. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  5840. }
  5841. }
  5842. }
  5843. return $fontdata;
  5844. }
  5845. /**
  5846. * Sets the font used to print character strings.
  5847. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  5848. * The method can be called before the first page is created and the font is retained from page to page.
  5849. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  5850. * 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 />
  5851. * @param $family (string) 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.
  5852. * @param $style (string) 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><li>O: overline</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.
  5853. * @param $size (float) 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
  5854. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  5855. * @param $subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.
  5856. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  5857. * @author Nicola Asuni
  5858. * @public
  5859. * @since 1.0
  5860. * @see AddFont(), SetFontSize()
  5861. */
  5862. public function SetFont($family, $style = '', $size = null, $fontfile = '', $subset = 'default', $out = true) {
  5863. //Select a font; size given in points
  5864. if ($size === null) {
  5865. $size = $this->FontSizePt;
  5866. }
  5867. if ($size < 0) {
  5868. $size = 0;
  5869. }
  5870. // try to add font (if not already added)
  5871. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  5872. $this->FontFamily = $fontdata['family'];
  5873. $this->FontStyle = $fontdata['style'];
  5874. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  5875. $this->SetFontSize($size, $out);
  5876. }
  5877. /**
  5878. * Defines the size of the current font.
  5879. * @param $size (float) The font size in points.
  5880. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  5881. * @public
  5882. * @since 1.0
  5883. * @see SetFont()
  5884. */
  5885. public function SetFontSize($size, $out = true) {
  5886. // font size in points
  5887. $this->FontSizePt = $size;
  5888. // font size in user units
  5889. $this->FontSize = $size / $this->k;
  5890. // calculate some font metrics
  5891. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  5892. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  5893. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  5894. } else {
  5895. $font_height = $size * 1.219;
  5896. }
  5897. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  5898. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  5899. }
  5900. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  5901. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  5902. }
  5903. if (!isset($font_ascent) AND !isset($font_descent)) {
  5904. // core font
  5905. $font_ascent = 0.76 * $font_height;
  5906. $font_descent = $font_height - $font_ascent;
  5907. } elseif (!isset($font_descent)) {
  5908. $font_descent = $font_height - $font_ascent;
  5909. } elseif (!isset($font_ascent)) {
  5910. $font_ascent = $font_height - $font_descent;
  5911. }
  5912. $this->FontAscent = ($font_ascent / $this->k);
  5913. $this->FontDescent = ($font_descent / $this->k);
  5914. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i'])) AND ($this->state == 2)) {
  5915. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  5916. }
  5917. }
  5918. /**
  5919. * Returns the bounding box of the current font in user units.
  5920. * @return array
  5921. * @public
  5922. * @since 5.9.152 (2012-03-23)
  5923. */
  5924. public function getFontBBox() {
  5925. $fbbox = array();
  5926. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  5927. $tmpbbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  5928. $fbbox = array_map(array($this, 'getAbsFontMeasure'), $tmpbbox);
  5929. } else {
  5930. // Find max width
  5931. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  5932. $maxw = $this->getAbsFontMeasure(intval($this->CurrentFont['desc']['MaxWidth']));
  5933. } else {
  5934. $maxw = 0;
  5935. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  5936. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  5937. }
  5938. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  5939. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  5940. }
  5941. if (isset($this->CurrentFont['dw'])) {
  5942. $maxw = max($maxw, $this->CurrentFont['dw']);
  5943. }
  5944. foreach ($this->CurrentFont['cw'] as $char => $w) {
  5945. $maxw = max($maxw, $w);
  5946. }
  5947. if ($maxw == 0) {
  5948. $maxw = 600;
  5949. }
  5950. $maxw = $this->getAbsFontMeasure($maxw);
  5951. }
  5952. $fbbox = array(0, -$this->FontDescent, $maxw, $this->FontAscent);
  5953. }
  5954. return $fbbox;
  5955. }
  5956. /**
  5957. * Convert a relative font measure into absolute value.
  5958. * @param $s (int) Font measure.
  5959. * @return float Absolute measure.
  5960. * @since 5.9.186 (2012-09-13)
  5961. */
  5962. public function getAbsFontMeasure($s) {
  5963. return ($s * $this->FontSize / 1000);
  5964. }
  5965. /**
  5966. * Returns the glyph bounding box of the specified character in the current font in user units.
  5967. * @param $char (int) Input character code.
  5968. * @return mixed array(xMin, yMin, xMax, yMax) or FALSE if not defined.
  5969. * @since 5.9.186 (2012-09-13)
  5970. */
  5971. public function getCharBBox($char) {
  5972. if (isset($this->CurrentFont['cbbox'][$char])) {
  5973. return array_map(array($this, 'getAbsFontMeasure'), $this->CurrentFont['cbbox'][intval($char)]);
  5974. }
  5975. return false;
  5976. }
  5977. /**
  5978. * Return the font descent value
  5979. * @param $font (string) font name
  5980. * @param $style (string) font style
  5981. * @param $size (float) The size (in points)
  5982. * @return int font descent
  5983. * @public
  5984. * @author Nicola Asuni
  5985. * @since 4.9.003 (2010-03-30)
  5986. */
  5987. public function getFontDescent($font, $style = '', $size = 0) {
  5988. $fontdata = $this->AddFont($font, $style);
  5989. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5990. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  5991. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  5992. } else {
  5993. $descent = (1.219 * 0.24 * $size);
  5994. }
  5995. return ($descent / $this->k);
  5996. }
  5997. /**
  5998. * Return the font ascent value.
  5999. * @param $font (string) font name
  6000. * @param $style (string) font style
  6001. * @param $size (float) The size (in points)
  6002. * @return int font ascent
  6003. * @public
  6004. * @author Nicola Asuni
  6005. * @since 4.9.003 (2010-03-30)
  6006. */
  6007. public function getFontAscent($font, $style = '', $size = 0) {
  6008. $fontdata = $this->AddFont($font, $style);
  6009. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  6010. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  6011. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  6012. } else {
  6013. $ascent = 1.219 * 0.76 * $size;
  6014. }
  6015. return ($ascent / $this->k);
  6016. }
  6017. /**
  6018. * Return true in the character is present in the specified font.
  6019. * @param $char (mixed) Character to check (integer value or string)
  6020. * @param $font (string) Font name (family name).
  6021. * @param $style (string) Font style.
  6022. * @return (boolean) true if the char is defined, false otherwise.
  6023. * @public
  6024. * @since 5.9.153 (2012-03-28)
  6025. */
  6026. public function isCharDefined($char, $font = '', $style = '') {
  6027. if (is_string($char)) {
  6028. // get character code
  6029. $char = $this->UTF8StringToArray($char);
  6030. $char = $char[0];
  6031. }
  6032. if ($this->empty_string($font)) {
  6033. if ($this->empty_string($style)) {
  6034. return (isset($this->CurrentFont['cw'][intval($char)]));
  6035. }
  6036. $font = $this->FontFamily;
  6037. }
  6038. $fontdata = $this->AddFont($font, $style);
  6039. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  6040. return (isset($fontinfo['cw'][intval($char)]));
  6041. }
  6042. /**
  6043. * Replace missing font characters on selected font with specified substitutions.
  6044. * @param $text (string) Text to process.
  6045. * @param $font (string) Font name (family name).
  6046. * @param $style (string) Font style.
  6047. * @param $subs (array) Array of possible character substitutions. The key is the character to check (integer value) and the value is a single intege value or an array of possible substitutes.
  6048. * @return (string) Processed text.
  6049. * @public
  6050. * @since 5.9.153 (2012-03-28)
  6051. */
  6052. public function replaceMissingChars($text, $font = '', $style = '', $subs = array()) {
  6053. if (empty($subs)) {
  6054. return $text;
  6055. }
  6056. if ($this->empty_string($font)) {
  6057. $font = $this->FontFamily;
  6058. }
  6059. $fontdata = $this->AddFont($font, $style);
  6060. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  6061. $uniarr = $this->UTF8StringToArray($text);
  6062. foreach ($uniarr as $k => $chr) {
  6063. if (!isset($fontinfo['cw'][$chr])) {
  6064. // this character is missing on the selected font
  6065. if (isset($subs[$chr])) {
  6066. // we have available substitutions
  6067. if (is_array($subs[$chr])) {
  6068. foreach ($subs[$chr] as $s) {
  6069. if (isset($fontinfo['cw'][$s])) {
  6070. $uniarr[$k] = $s;
  6071. break;
  6072. }
  6073. }
  6074. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  6075. $uniarr[$k] = $subs[$chr];
  6076. }
  6077. }
  6078. }
  6079. }
  6080. return $this->UniArrSubString($this->UTF8ArrayToUniArray($uniarr));
  6081. }
  6082. /**
  6083. * Defines the default monospaced font.
  6084. * @param $font (string) Font name.
  6085. * @public
  6086. * @since 4.5.025
  6087. */
  6088. public function SetDefaultMonospacedFont($font) {
  6089. $this->default_monospaced_font = $font;
  6090. }
  6091. /**
  6092. * 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 />
  6093. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  6094. * @public
  6095. * @since 1.5
  6096. * @see Cell(), Write(), Image(), Link(), SetLink()
  6097. */
  6098. public function AddLink() {
  6099. //Create a new internal link
  6100. $n = count($this->links) + 1;
  6101. $this->links[$n] = array(0, 0);
  6102. return $n;
  6103. }
  6104. /**
  6105. * Defines the page and position a link points to.
  6106. * @param $link (int) The link identifier returned by AddLink()
  6107. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  6108. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  6109. * @public
  6110. * @since 1.5
  6111. * @see AddLink()
  6112. */
  6113. public function SetLink($link, $y = 0, $page = -1) {
  6114. if ($y == -1) {
  6115. $y = $this->y;
  6116. }
  6117. if ($page == -1) {
  6118. $page = $this->page;
  6119. }
  6120. $this->links[$link] = array($page, $y);
  6121. }
  6122. /**
  6123. * Puts a link on a rectangular area of the page.
  6124. * 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.
  6125. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  6126. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  6127. * @param $w (float) Width of the rectangle
  6128. * @param $h (float) Height of the rectangle
  6129. * @param $link (mixed) URL or identifier returned by AddLink()
  6130. * @param $spaces (int) number of spaces on the text to link
  6131. * @public
  6132. * @since 1.5
  6133. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  6134. */
  6135. public function Link($x, $y, $w, $h, $link, $spaces = 0) {
  6136. $this->Annotation($x, $y, $w, $h, $link, array('Subtype' => 'Link'), $spaces);
  6137. }
  6138. /**
  6139. * Puts a markup annotation on a rectangular area of the page.
  6140. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  6141. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  6142. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  6143. * @param $w (float) Width of the rectangle
  6144. * @param $h (float) Height of the rectangle
  6145. * @param $text (string) annotation text or alternate content
  6146. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  6147. * @param $spaces (int) number of spaces on the text to link
  6148. * @public
  6149. * @since 4.0.018 (2008-08-06)
  6150. */
  6151. public function Annotation($x, $y, $w, $h, $text, $opt = array('Subtype' => 'Text'), $spaces = 0) {
  6152. if ($this->inxobj) {
  6153. // store parameters for later use on template
  6154. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  6155. return;
  6156. }
  6157. if ($x === '') {
  6158. $x = $this->x;
  6159. }
  6160. if ($y === '') {
  6161. $y = $this->y;
  6162. }
  6163. // check page for no-write regions and adapt page margins if necessary
  6164. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  6165. // recalculate coordinates to account for graphic transformations
  6166. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  6167. for ($i = $this->transfmatrix_key; $i > 0; --$i) {
  6168. $maxid = count($this->transfmatrix[$i]) - 1;
  6169. for ($j = $maxid; $j >= 0; --$j) {
  6170. $ctm = $this->transfmatrix[$i][$j];
  6171. if (isset($ctm['a'])) {
  6172. $x = $x * $this->k;
  6173. $y = ($this->h - $y) * $this->k;
  6174. $w = $w * $this->k;
  6175. $h = $h * $this->k;
  6176. // top left
  6177. $xt = $x;
  6178. $yt = $y;
  6179. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  6180. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  6181. // top right
  6182. $xt = $x + $w;
  6183. $yt = $y;
  6184. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  6185. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  6186. // bottom left
  6187. $xt = $x;
  6188. $yt = $y - $h;
  6189. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  6190. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  6191. // bottom right
  6192. $xt = $x + $w;
  6193. $yt = $y - $h;
  6194. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  6195. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  6196. // new coordinates (rectangle area)
  6197. $x = min($x1, $x2, $x3, $x4);
  6198. $y = max($y1, $y2, $y3, $y4);
  6199. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  6200. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  6201. $x = $x / $this->k;
  6202. $y = $this->h - ($y / $this->k);
  6203. }
  6204. }
  6205. }
  6206. }
  6207. if ($this->page <= 0) {
  6208. $page = 1;
  6209. } else {
  6210. $page = $this->page;
  6211. }
  6212. if (!isset($this->PageAnnots[$page])) {
  6213. $this->PageAnnots[$page] = array();
  6214. }
  6215. ++$this->n;
  6216. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  6217. if (!$this->pdfa_mode) {
  6218. if ((($opt['Subtype'] == 'FileAttachment') OR ($opt['Subtype'] == 'Sound')) AND (!$this->empty_string($opt['FS'])) AND file_exists($opt['FS']) AND (!isset($this->embeddedfiles[basename($opt['FS'])]))) {
  6219. ++$this->n;
  6220. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  6221. }
  6222. }
  6223. // Add widgets annotation's icons
  6224. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  6225. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  6226. }
  6227. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  6228. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  6229. }
  6230. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  6231. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  6232. }
  6233. }
  6234. /**
  6235. * Embedd the attached files.
  6236. * @since 4.4.000 (2008-12-07)
  6237. * @protected
  6238. * @see Annotation()
  6239. */
  6240. protected function _putEmbeddedFiles() {
  6241. if ($this->pdfa_mode) {
  6242. // embedded files are not allowed in PDF/A mode
  6243. return;
  6244. }
  6245. reset($this->embeddedfiles);
  6246. foreach ($this->embeddedfiles as $filename => $filedata) {
  6247. $data = file_get_contents($filedata['file']);
  6248. $filter = '';
  6249. if ($this->compress) {
  6250. $data = gzcompress($data);
  6251. $filter = ' /Filter /FlateDecode';
  6252. }
  6253. $stream = $this->_getrawstream($data, $filedata['n']);
  6254. $out = $this->_getobj($filedata['n']) . "\n";
  6255. $out .= '<< /Type /EmbeddedFile' . $filter . ' /Length ' . strlen($stream) . ' >>';
  6256. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  6257. $out .= "\n" . 'endobj';
  6258. $this->_out($out);
  6259. }
  6260. }
  6261. /**
  6262. * Prints a text cell at the specified position.
  6263. * This method allows to place a string precisely on the page.
  6264. * @param $x (float) Abscissa of the cell origin
  6265. * @param $y (float) Ordinate of the cell origin
  6266. * @param $txt (string) String to print
  6267. * @param $fstroke (int) outline size in user units (false = disable)
  6268. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  6269. * @param $ffill (boolean) if true fills the text
  6270. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6271. * @param $ln (int) 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.
  6272. * @param $align (string) 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>
  6273. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6274. * @param $link (mixed) URL or identifier returned by AddLink().
  6275. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  6276. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  6277. * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li><li>B : cell bottom</li></ul>
  6278. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  6279. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  6280. * @public
  6281. * @since 1.0
  6282. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  6283. */
  6284. public function Text($x, $y, $txt, $fstroke = false, $fclip = false, $ffill = true, $border = 0, $ln = 0, $align = '', $fill = false, $link = '', $stretch = 0, $ignore_min_height = false, $calign = 'T', $valign = 'M', $rtloff = false) {
  6285. $textrendermode = $this->textrendermode;
  6286. $textstrokewidth = $this->textstrokewidth;
  6287. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  6288. $this->SetXY($x, $y, $rtloff);
  6289. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  6290. // restore previous rendering mode
  6291. $this->textrendermode = $textrendermode;
  6292. $this->textstrokewidth = $textstrokewidth;
  6293. }
  6294. /**
  6295. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  6296. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  6297. * This method is called automatically and should not be called directly by the application.
  6298. * @return boolean
  6299. * @public
  6300. * @since 1.4
  6301. * @see SetAutoPageBreak()
  6302. */
  6303. public function AcceptPageBreak() {
  6304. if ($this->num_columns > 1) {
  6305. // multi column mode
  6306. if ($this->current_column < ($this->num_columns - 1)) {
  6307. // go to next column
  6308. $this->selectColumn($this->current_column + 1);
  6309. } elseif ($this->AutoPageBreak) {
  6310. // add a new page
  6311. $this->AddPage();
  6312. // set first column
  6313. $this->selectColumn(0);
  6314. }
  6315. // avoid page breaking from checkPageBreak()
  6316. return false;
  6317. }
  6318. return $this->AutoPageBreak;
  6319. }
  6320. /**
  6321. * Add page if needed.
  6322. * @param $h (float) Cell height. Default value: 0.
  6323. * @param $y (mixed) starting y position, leave empty for current position.
  6324. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  6325. * @return boolean true in case of page break, false otherwise.
  6326. * @since 3.2.000 (2008-07-01)
  6327. * @protected
  6328. */
  6329. protected function checkPageBreak($h = 0, $y = '', $addpage = true) {
  6330. if ($this->empty_string($y)) {
  6331. $y = $this->y;
  6332. }
  6333. $current_page = $this->page;
  6334. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  6335. if ($addpage) {
  6336. //Automatic page break
  6337. $x = $this->x;
  6338. $this->AddPage($this->CurOrientation);
  6339. $this->y = $this->tMargin;
  6340. $oldpage = $this->page - 1;
  6341. if ($this->rtl) {
  6342. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  6343. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  6344. } else {
  6345. $this->x = $x;
  6346. }
  6347. } else {
  6348. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  6349. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  6350. } else {
  6351. $this->x = $x;
  6352. }
  6353. }
  6354. }
  6355. return true;
  6356. }
  6357. if ($current_page != $this->page) {
  6358. // account for columns mode
  6359. return true;
  6360. }
  6361. return false;
  6362. }
  6363. /**
  6364. * Removes SHY characters from text.
  6365. * Unicode Data:<ul>
  6366. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  6367. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  6368. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  6369. * <li>HTML Entity (named): "&amp;shy;"</li>
  6370. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  6371. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  6372. * <li>UTF-8 character: chr(194).chr(173)</li>
  6373. * </ul>
  6374. * @param $txt (string) input string
  6375. * @return string without SHY characters.
  6376. * @public
  6377. * @since (4.5.019) 2009-02-28
  6378. */
  6379. public function removeSHY($txt = '') {
  6380. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  6381. if (!$this->isunicode) {
  6382. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  6383. }
  6384. return $txt;
  6385. }
  6386. /**
  6387. * 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 />
  6388. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  6389. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  6390. * @param $h (float) Cell height. Default value: 0.
  6391. * @param $txt (string) String to print. Default value: empty string.
  6392. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6393. * @param $ln (int) 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.
  6394. * @param $align (string) 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>
  6395. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6396. * @param $link (mixed) URL or identifier returned by AddLink().
  6397. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  6398. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  6399. * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
  6400. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>C : center</li><li>B : bottom</li></ul>
  6401. * @public
  6402. * @since 1.0
  6403. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  6404. */
  6405. public function Cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '', $stretch = 0, $ignore_min_height = false, $calign = 'T', $valign = 'M') {
  6406. $prev_cell_margin = $this->cell_margin;
  6407. $prev_cell_padding = $this->cell_padding;
  6408. $this->adjustCellPadding($border);
  6409. if (!$ignore_min_height) {
  6410. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  6411. if ($h < $min_cell_height) {
  6412. $h = $min_cell_height;
  6413. }
  6414. }
  6415. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  6416. // apply text shadow if enabled
  6417. if ($this->txtshadow['enabled']) {
  6418. // save data
  6419. $x = $this->x;
  6420. $y = $this->y;
  6421. $bc = $this->bgcolor;
  6422. $fc = $this->fgcolor;
  6423. $sc = $this->strokecolor;
  6424. $alpha = $this->alpha;
  6425. // print shadow
  6426. $this->x += $this->txtshadow['depth_w'];
  6427. $this->y += $this->txtshadow['depth_h'];
  6428. $this->SetFillColorArray($this->txtshadow['color']);
  6429. $this->SetTextColorArray($this->txtshadow['color']);
  6430. $this->SetDrawColorArray($this->txtshadow['color']);
  6431. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  6432. $this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
  6433. }
  6434. if ($this->state == 2) {
  6435. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  6436. }
  6437. //restore data
  6438. $this->x = $x;
  6439. $this->y = $y;
  6440. $this->SetFillColorArray($bc);
  6441. $this->SetTextColorArray($fc);
  6442. $this->SetDrawColorArray($sc);
  6443. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  6444. $this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
  6445. }
  6446. }
  6447. if ($this->state == 2) {
  6448. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  6449. }
  6450. $this->cell_padding = $prev_cell_padding;
  6451. $this->cell_margin = $prev_cell_margin;
  6452. }
  6453. /**
  6454. * 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 />
  6455. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  6456. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  6457. * @param $h (float) Cell height. Default value: 0.
  6458. * @param $txt (string) String to print. Default value: empty string.
  6459. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6460. * @param $ln (int) 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.
  6461. * @param $align (string) 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>
  6462. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6463. * @param $link (mixed) URL or identifier returned by AddLink().
  6464. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  6465. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  6466. * @param $calign (string) cell vertical alignment relative to the specified Y value. Possible values are:<ul><li>T : cell top</li><li>C : center</li><li>B : cell bottom</li><li>A : font top</li><li>L : font baseline</li><li>D : font bottom</li></ul>
  6467. * @param $valign (string) text vertical alignment inside the cell. Possible values are:<ul><li>T : top</li><li>M : middle</li><li>B : bottom</li></ul>
  6468. * @return string containing cell code
  6469. * @protected
  6470. * @since 1.0
  6471. * @see Cell()
  6472. */
  6473. protected function getCellCode($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '', $stretch = 0, $ignore_min_height = false, $calign = 'T', $valign = 'M') {
  6474. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  6475. $txt = str_replace($this->unichr(160), ' ', $txt);
  6476. $prev_cell_margin = $this->cell_margin;
  6477. $prev_cell_padding = $this->cell_padding;
  6478. $txt = $this->removeSHY($txt);
  6479. $rs = ''; //string to be returned
  6480. $this->adjustCellPadding($border);
  6481. if (!$ignore_min_height) {
  6482. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  6483. if ($h < $min_cell_height) {
  6484. $h = $min_cell_height;
  6485. }
  6486. }
  6487. $k = $this->k;
  6488. // check page for no-write regions and adapt page margins if necessary
  6489. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6490. if ($this->rtl) {
  6491. $x = $this->x - $this->cell_margin['R'];
  6492. } else {
  6493. $x = $this->x + $this->cell_margin['L'];
  6494. }
  6495. $y = $this->y + $this->cell_margin['T'];
  6496. $prev_font_stretching = $this->font_stretching;
  6497. $prev_font_spacing = $this->font_spacing;
  6498. // cell vertical alignment
  6499. switch ($calign) {
  6500. case 'A': {
  6501. // font top
  6502. switch ($valign) {
  6503. case 'T': {
  6504. // top
  6505. $y -= $this->cell_padding['T'];
  6506. break;
  6507. }
  6508. case 'B': {
  6509. // bottom
  6510. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  6511. break;
  6512. }
  6513. default:
  6514. case 'C':
  6515. case 'M': {
  6516. // center
  6517. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  6518. break;
  6519. }
  6520. }
  6521. break;
  6522. }
  6523. case 'L': {
  6524. // font baseline
  6525. switch ($valign) {
  6526. case 'T': {
  6527. // top
  6528. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  6529. break;
  6530. }
  6531. case 'B': {
  6532. // bottom
  6533. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  6534. break;
  6535. }
  6536. default:
  6537. case 'C':
  6538. case 'M': {
  6539. // center
  6540. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  6541. break;
  6542. }
  6543. }
  6544. break;
  6545. }
  6546. case 'D': {
  6547. // font bottom
  6548. switch ($valign) {
  6549. case 'T': {
  6550. // top
  6551. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  6552. break;
  6553. }
  6554. case 'B': {
  6555. // bottom
  6556. $y -= ($h - $this->cell_padding['B']);
  6557. break;
  6558. }
  6559. default:
  6560. case 'C':
  6561. case 'M': {
  6562. // center
  6563. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  6564. break;
  6565. }
  6566. }
  6567. break;
  6568. }
  6569. case 'B': {
  6570. // cell bottom
  6571. $y -= $h;
  6572. break;
  6573. }
  6574. case 'C':
  6575. case 'M': {
  6576. // cell center
  6577. $y -= ($h / 2);
  6578. break;
  6579. }
  6580. default:
  6581. case 'T': {
  6582. // cell top
  6583. break;
  6584. }
  6585. }
  6586. // text vertical alignment
  6587. switch ($valign) {
  6588. case 'T': {
  6589. // top
  6590. $yt = $y + $this->cell_padding['T'];
  6591. break;
  6592. }
  6593. case 'B': {
  6594. // bottom
  6595. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  6596. break;
  6597. }
  6598. default:
  6599. case 'C':
  6600. case 'M': {
  6601. // center
  6602. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  6603. break;
  6604. }
  6605. }
  6606. $basefonty = $yt + $this->FontAscent;
  6607. if ($this->empty_string($w) OR ($w <= 0)) {
  6608. if ($this->rtl) {
  6609. $w = $x - $this->lMargin;
  6610. } else {
  6611. $w = $this->w - $this->rMargin - $x;
  6612. }
  6613. }
  6614. $s = '';
  6615. // fill and borders
  6616. if (is_string($border) AND (strlen($border) == 4)) {
  6617. // full border
  6618. $border = 1;
  6619. }
  6620. if ($fill OR ($border == 1)) {
  6621. if ($fill) {
  6622. $op = ($border == 1) ? 'B' : 'f';
  6623. } else {
  6624. $op = 'S';
  6625. }
  6626. if ($this->rtl) {
  6627. $xk = (($x - $w) * $k);
  6628. } else {
  6629. $xk = ($x * $k);
  6630. }
  6631. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  6632. }
  6633. // draw borders
  6634. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  6635. if ($txt != '') {
  6636. $txt2 = $txt;
  6637. if ($this->isunicode) {
  6638. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  6639. $txt2 = $this->UTF8ToLatin1($txt2);
  6640. } else {
  6641. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  6642. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  6643. // replace thai chars (if any)
  6644. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  6645. // number of chars
  6646. $numchars = count($unicode);
  6647. // po pla, for far, for fan
  6648. $longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
  6649. // do chada, to patak
  6650. $lowtail = array(0x0e0e, 0x0e0f);
  6651. // mai hun arkad, sara i, sara ii, sara ue, sara uee
  6652. $upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
  6653. // mai ek, mai tho, mai tri, mai chattawa, karan
  6654. $tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
  6655. // sara u, sara uu, pinthu
  6656. $lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
  6657. $output = array();
  6658. for ($i = 0; $i < $numchars; $i++) {
  6659. if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
  6660. $ch0 = $unicode[$i];
  6661. $ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
  6662. $ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
  6663. $chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
  6664. if (in_array($ch0, $tonemark)) {
  6665. if ($chn == 0x0e33) {
  6666. // sara um
  6667. if (in_array($ch1, $longtail)) {
  6668. // tonemark at upper left
  6669. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  6670. } else {
  6671. // tonemark at upper right (normal position)
  6672. $output[] = $ch0;
  6673. }
  6674. } elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
  6675. // tonemark at lower left
  6676. $output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
  6677. } elseif (in_array($ch1, $upvowel)) {
  6678. if (in_array($ch2, $longtail)) {
  6679. // tonemark at upper left
  6680. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  6681. } else {
  6682. // tonemark at upper right (normal position)
  6683. $output[] = $ch0;
  6684. }
  6685. } else {
  6686. // tonemark at lower right
  6687. $output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
  6688. }
  6689. } elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
  6690. // add lower left nikhahit and sara aa
  6691. if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
  6692. $output[] = 0xf711;
  6693. $this->CurrentFont['subsetchars'][0xf711] = true;
  6694. $output[] = 0x0e32;
  6695. $this->CurrentFont['subsetchars'][0x0e32] = true;
  6696. } else {
  6697. $output[] = $ch0;
  6698. }
  6699. } elseif (in_array($ch1, $longtail)) {
  6700. if ($ch0 == 0x0e31) {
  6701. // lower left mai hun arkad
  6702. $output[] = $this->replaceChar($ch0, 0xf710);
  6703. } elseif (in_array($ch0, $upvowel)) {
  6704. // lower left
  6705. $output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
  6706. } elseif ($ch0 == 0x0e47) {
  6707. // lower left mai tai koo
  6708. $output[] = $this->replaceChar($ch0, 0xf712);
  6709. } else {
  6710. // normal character
  6711. $output[] = $ch0;
  6712. }
  6713. } elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
  6714. // lower vowel
  6715. $output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
  6716. } elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
  6717. // yo ying without lower part
  6718. $output[] = $this->replaceChar($ch0, 0xf70f);
  6719. } elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
  6720. // tho santan without lower part
  6721. $output[] = $this->replaceChar($ch0, 0xf700);
  6722. } else {
  6723. $output[] = $ch0;
  6724. }
  6725. } else {
  6726. // non-thai character
  6727. $output[] = $unicode[$i];
  6728. }
  6729. }
  6730. $unicode = $output;
  6731. // update font subsetchars
  6732. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  6733. } // end of K_THAI_TOPCHARS
  6734. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  6735. }
  6736. }
  6737. $txt2 = $this->_escape($txt2);
  6738. // get current text width (considering general font stretching and spacing)
  6739. $txwidth = $this->GetStringWidth($txt);
  6740. $width = $txwidth;
  6741. // check for stretch mode
  6742. if ($stretch > 0) {
  6743. // calculate ratio between cell width and text width
  6744. if ($width <= 0) {
  6745. $ratio = 1;
  6746. } else {
  6747. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  6748. }
  6749. // check if stretching is required
  6750. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  6751. // the text will be stretched to fit cell width
  6752. if ($stretch > 2) {
  6753. // set new character spacing
  6754. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  6755. } else {
  6756. // set new horizontal stretching
  6757. $this->font_stretching *= $ratio;
  6758. }
  6759. // recalculate text width (the text fills the entire cell)
  6760. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6761. // reset alignment
  6762. $align = '';
  6763. }
  6764. }
  6765. if ($this->font_stretching != 100) {
  6766. // apply font stretching
  6767. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  6768. }
  6769. if ($this->font_spacing != 0) {
  6770. // increase/decrease font spacing
  6771. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  6772. }
  6773. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  6774. $s .= 'q ' . $this->TextColor . ' ';
  6775. }
  6776. // rendering mode
  6777. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
  6778. // count number of spaces
  6779. $ns = substr_count($txt, chr(32));
  6780. // Justification
  6781. $spacewidth = 0;
  6782. if (($align == 'J') AND ($ns > 0)) {
  6783. if ($this->isUnicodeFont()) {
  6784. // get string width without spaces
  6785. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  6786. // calculate average space width
  6787. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns ? $ns : 1) / $this->FontSize;
  6788. if ($this->font_stretching != 100) {
  6789. // word spacing is affected by stretching
  6790. $spacewidth /= ($this->font_stretching / 100);
  6791. }
  6792. // set word position to be used with TJ operator
  6793. $txt2 = str_replace(chr(0) . chr(32), ') ' . sprintf('%F', $spacewidth) . ' (', $txt2);
  6794. $unicode_justification = true;
  6795. } else {
  6796. // get string width
  6797. $width = $txwidth;
  6798. // new space width
  6799. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns ? $ns : 1)) * $this->k;
  6800. if ($this->font_stretching != 100) {
  6801. // word spacing (Tw) is affected by stretching
  6802. $spacewidth /= ($this->font_stretching / 100);
  6803. }
  6804. // set word spacing
  6805. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  6806. }
  6807. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6808. }
  6809. // replace carriage return characters
  6810. $txt2 = str_replace("\r", ' ', $txt2);
  6811. switch ($align) {
  6812. case 'C': {
  6813. $dx = ($w - $width) / 2;
  6814. break;
  6815. }
  6816. case 'R': {
  6817. if ($this->rtl) {
  6818. $dx = $this->cell_padding['R'];
  6819. } else {
  6820. $dx = $w - $width - $this->cell_padding['R'];
  6821. }
  6822. break;
  6823. }
  6824. case 'L': {
  6825. if ($this->rtl) {
  6826. $dx = $w - $width - $this->cell_padding['L'];
  6827. } else {
  6828. $dx = $this->cell_padding['L'];
  6829. }
  6830. break;
  6831. }
  6832. case 'J':
  6833. default: {
  6834. if ($this->rtl) {
  6835. $dx = $this->cell_padding['R'];
  6836. } else {
  6837. $dx = $this->cell_padding['L'];
  6838. }
  6839. break;
  6840. }
  6841. }
  6842. if ($this->rtl) {
  6843. $xdx = $x - $dx - $width;
  6844. } else {
  6845. $xdx = $x + $dx;
  6846. }
  6847. $xdk = $xdx * $k;
  6848. // print text
  6849. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  6850. if (isset($uniblock)) {
  6851. // print overlapping characters as separate string
  6852. $xshift = 0; // horizontal shift
  6853. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  6854. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns ? $ns : 1));
  6855. foreach ($uniblock as $uk => $uniarr) {
  6856. if (($uk % 2) == 0) {
  6857. // x space to skip
  6858. if ($spacewidth != 0) {
  6859. // justification shift
  6860. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  6861. }
  6862. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  6863. } else {
  6864. // character to print
  6865. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  6866. $topchr = $this->_escape($topchr);
  6867. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  6868. }
  6869. }
  6870. }
  6871. if ($this->underline) {
  6872. $s .= ' ' . $this->_dounderlinew($xdx, $basefonty, $width);
  6873. }
  6874. if ($this->linethrough) {
  6875. $s .= ' ' . $this->_dolinethroughw($xdx, $basefonty, $width);
  6876. }
  6877. if ($this->overline) {
  6878. $s .= ' ' . $this->_dooverlinew($xdx, $basefonty, $width);
  6879. }
  6880. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  6881. $s .= ' Q';
  6882. }
  6883. if ($link) {
  6884. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  6885. }
  6886. }
  6887. // output cell
  6888. if ($s) {
  6889. // output cell
  6890. $rs .= $s;
  6891. if ($this->font_spacing != 0) {
  6892. // reset font spacing mode
  6893. $rs .= ' BT 0 Tc ET';
  6894. }
  6895. if ($this->font_stretching != 100) {
  6896. // reset font stretching mode
  6897. $rs .= ' BT 100 Tz ET';
  6898. }
  6899. }
  6900. // reset word spacing
  6901. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  6902. $rs .= ' BT 0 Tw ET';
  6903. }
  6904. // reset stretching and spacing
  6905. $this->font_stretching = $prev_font_stretching;
  6906. $this->font_spacing = $prev_font_spacing;
  6907. $this->lasth = $h;
  6908. if ($ln > 0) {
  6909. //Go to the beginning of the next line
  6910. $this->y = $y + $h + $this->cell_margin['B'];
  6911. if ($ln == 1) {
  6912. if ($this->rtl) {
  6913. $this->x = $this->w - $this->rMargin;
  6914. } else {
  6915. $this->x = $this->lMargin;
  6916. }
  6917. }
  6918. } else {
  6919. // go left or right by case
  6920. if ($this->rtl) {
  6921. $this->x = $x - $w - $this->cell_margin['L'];
  6922. } else {
  6923. $this->x = $x + $w + $this->cell_margin['R'];
  6924. }
  6925. }
  6926. $gstyles = '' . $this->linestyleWidth . ' ' . $this->linestyleCap . ' ' . $this->linestyleJoin . ' ' . $this->linestyleDash . ' ' . $this->DrawColor . ' ' . $this->FillColor . "\n";
  6927. $rs = $gstyles . $rs;
  6928. $this->cell_padding = $prev_cell_padding;
  6929. $this->cell_margin = $prev_cell_margin;
  6930. return $rs;
  6931. }
  6932. /**
  6933. * Replace a char if is defined on the current font.
  6934. * @param $oldchar (int) Integer code (unicode) of the character to replace.
  6935. * @param $newchar (int) Integer code (unicode) of the new character.
  6936. * @return int the replaced char or the old char in case the new char i not defined
  6937. * @protected
  6938. * @since 5.9.167 (2012-06-22)
  6939. */
  6940. protected function replaceChar($oldchar, $newchar) {
  6941. if ($this->isCharDefined($newchar)) {
  6942. // add the new char on the subset list
  6943. $this->CurrentFont['subsetchars'][$newchar] = true;
  6944. // return the new character
  6945. return $newchar;
  6946. }
  6947. // return the old char
  6948. return $oldchar;
  6949. }
  6950. /**
  6951. * Returns the code to draw the cell border
  6952. * @param $x (float) X coordinate.
  6953. * @param $y (float) Y coordinate.
  6954. * @param $w (float) Cell width.
  6955. * @param $h (float) Cell height.
  6956. * @param $brd (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  6957. * @return string containing cell border code
  6958. * @protected
  6959. * @see SetLineStyle()
  6960. * @since 5.7.000 (2010-08-02)
  6961. */
  6962. protected function getCellBorder($x, $y, $w, $h, $brd) {
  6963. $s = ''; // string to be returned
  6964. if (empty($brd)) {
  6965. return $s;
  6966. }
  6967. if ($brd == 1) {
  6968. $brd = array('LRTB' => true);
  6969. }
  6970. // calculate coordinates for border
  6971. $k = $this->k;
  6972. if ($this->rtl) {
  6973. $xeL = ($x - $w) * $k;
  6974. $xeR = $x * $k;
  6975. } else {
  6976. $xeL = $x * $k;
  6977. $xeR = ($x + $w) * $k;
  6978. }
  6979. $yeL = (($this->h - ($y + $h)) * $k);
  6980. $yeT = (($this->h - $y) * $k);
  6981. $xeT = $xeL;
  6982. $xeB = $xeR;
  6983. $yeR = $yeT;
  6984. $yeB = $yeL;
  6985. if (is_string($brd)) {
  6986. // convert string to array
  6987. $slen = strlen($brd);
  6988. $newbrd = array();
  6989. for ($i = 0; $i < $slen; ++$i) {
  6990. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6991. }
  6992. $brd = $newbrd;
  6993. }
  6994. if (isset($brd['mode'])) {
  6995. $mode = $brd['mode'];
  6996. unset($brd['mode']);
  6997. } else {
  6998. $mode = 'normal';
  6999. }
  7000. foreach ($brd as $border => $style) {
  7001. if (is_array($style) AND !empty($style)) {
  7002. // apply border style
  7003. $prev_style = $this->linestyleWidth . ' ' . $this->linestyleCap . ' ' . $this->linestyleJoin . ' ' . $this->linestyleDash . ' ' . $this->DrawColor . ' ';
  7004. $s .= $this->SetLineStyle($style, true) . "\n";
  7005. }
  7006. switch ($mode) {
  7007. case 'ext': {
  7008. $off = (($this->LineWidth / 2) * $k);
  7009. $xL = $xeL - $off;
  7010. $xR = $xeR + $off;
  7011. $yT = $yeT + $off;
  7012. $yL = $yeL - $off;
  7013. $xT = $xL;
  7014. $xB = $xR;
  7015. $yR = $yT;
  7016. $yB = $yL;
  7017. $w += $this->LineWidth;
  7018. $h += $this->LineWidth;
  7019. break;
  7020. }
  7021. case 'int': {
  7022. $off = ($this->LineWidth / 2) * $k;
  7023. $xL = $xeL + $off;
  7024. $xR = $xeR - $off;
  7025. $yT = $yeT - $off;
  7026. $yL = $yeL + $off;
  7027. $xT = $xL;
  7028. $xB = $xR;
  7029. $yR = $yT;
  7030. $yB = $yL;
  7031. $w -= $this->LineWidth;
  7032. $h -= $this->LineWidth;
  7033. break;
  7034. }
  7035. case 'normal':
  7036. default: {
  7037. $xL = $xeL;
  7038. $xT = $xeT;
  7039. $xB = $xeB;
  7040. $xR = $xeR;
  7041. $yL = $yeL;
  7042. $yT = $yeT;
  7043. $yB = $yeB;
  7044. $yR = $yeR;
  7045. break;
  7046. }
  7047. }
  7048. // draw borders by case
  7049. if (strlen($border) == 4) {
  7050. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  7051. } elseif (strlen($border) == 3) {
  7052. if (strpos($border, 'B') === false) { // LTR
  7053. $s .= sprintf('%F %F m ', $xL, $yL);
  7054. $s .= sprintf('%F %F l ', $xT, $yT);
  7055. $s .= sprintf('%F %F l ', $xR, $yR);
  7056. $s .= sprintf('%F %F l ', $xB, $yB);
  7057. $s .= 'S ';
  7058. } elseif (strpos($border, 'L') === false) { // TRB
  7059. $s .= sprintf('%F %F m ', $xT, $yT);
  7060. $s .= sprintf('%F %F l ', $xR, $yR);
  7061. $s .= sprintf('%F %F l ', $xB, $yB);
  7062. $s .= sprintf('%F %F l ', $xL, $yL);
  7063. $s .= 'S ';
  7064. } elseif (strpos($border, 'T') === false) { // RBL
  7065. $s .= sprintf('%F %F m ', $xR, $yR);
  7066. $s .= sprintf('%F %F l ', $xB, $yB);
  7067. $s .= sprintf('%F %F l ', $xL, $yL);
  7068. $s .= sprintf('%F %F l ', $xT, $yT);
  7069. $s .= 'S ';
  7070. } elseif (strpos($border, 'R') === false) { // BLT
  7071. $s .= sprintf('%F %F m ', $xB, $yB);
  7072. $s .= sprintf('%F %F l ', $xL, $yL);
  7073. $s .= sprintf('%F %F l ', $xT, $yT);
  7074. $s .= sprintf('%F %F l ', $xR, $yR);
  7075. $s .= 'S ';
  7076. }
  7077. } elseif (strlen($border) == 2) {
  7078. if ((strpos($border, 'L') !== false) AND (strpos($border, 'T') !== false)) { // LT
  7079. $s .= sprintf('%F %F m ', $xL, $yL);
  7080. $s .= sprintf('%F %F l ', $xT, $yT);
  7081. $s .= sprintf('%F %F l ', $xR, $yR);
  7082. $s .= 'S ';
  7083. } elseif ((strpos($border, 'T') !== false) AND (strpos($border, 'R') !== false)) { // TR
  7084. $s .= sprintf('%F %F m ', $xT, $yT);
  7085. $s .= sprintf('%F %F l ', $xR, $yR);
  7086. $s .= sprintf('%F %F l ', $xB, $yB);
  7087. $s .= 'S ';
  7088. } elseif ((strpos($border, 'R') !== false) AND (strpos($border, 'B') !== false)) { // RB
  7089. $s .= sprintf('%F %F m ', $xR, $yR);
  7090. $s .= sprintf('%F %F l ', $xB, $yB);
  7091. $s .= sprintf('%F %F l ', $xL, $yL);
  7092. $s .= 'S ';
  7093. } elseif ((strpos($border, 'B') !== false) AND (strpos($border, 'L') !== false)) { // BL
  7094. $s .= sprintf('%F %F m ', $xB, $yB);
  7095. $s .= sprintf('%F %F l ', $xL, $yL);
  7096. $s .= sprintf('%F %F l ', $xT, $yT);
  7097. $s .= 'S ';
  7098. } elseif ((strpos($border, 'L') !== false) AND (strpos($border, 'R') !== false)) { // LR
  7099. $s .= sprintf('%F %F m ', $xL, $yL);
  7100. $s .= sprintf('%F %F l ', $xT, $yT);
  7101. $s .= 'S ';
  7102. $s .= sprintf('%F %F m ', $xR, $yR);
  7103. $s .= sprintf('%F %F l ', $xB, $yB);
  7104. $s .= 'S ';
  7105. } elseif ((strpos($border, 'T') !== false) AND (strpos($border, 'B') !== false)) { // TB
  7106. $s .= sprintf('%F %F m ', $xT, $yT);
  7107. $s .= sprintf('%F %F l ', $xR, $yR);
  7108. $s .= 'S ';
  7109. $s .= sprintf('%F %F m ', $xB, $yB);
  7110. $s .= sprintf('%F %F l ', $xL, $yL);
  7111. $s .= 'S ';
  7112. }
  7113. } else { // strlen($border) == 1
  7114. if (strpos($border, 'L') !== false) { // L
  7115. $s .= sprintf('%F %F m ', $xL, $yL);
  7116. $s .= sprintf('%F %F l ', $xT, $yT);
  7117. $s .= 'S ';
  7118. } elseif (strpos($border, 'T') !== false) { // T
  7119. $s .= sprintf('%F %F m ', $xT, $yT);
  7120. $s .= sprintf('%F %F l ', $xR, $yR);
  7121. $s .= 'S ';
  7122. } elseif (strpos($border, 'R') !== false) { // R
  7123. $s .= sprintf('%F %F m ', $xR, $yR);
  7124. $s .= sprintf('%F %F l ', $xB, $yB);
  7125. $s .= 'S ';
  7126. } elseif (strpos($border, 'B') !== false) { // B
  7127. $s .= sprintf('%F %F m ', $xB, $yB);
  7128. $s .= sprintf('%F %F l ', $xL, $yL);
  7129. $s .= 'S ';
  7130. }
  7131. }
  7132. if (is_array($style) AND !empty($style)) {
  7133. // reset border style to previous value
  7134. $s .= "\n" . $this->linestyleWidth . ' ' . $this->linestyleCap . ' ' . $this->linestyleJoin . ' ' . $this->linestyleDash . ' ' . $this->DrawColor . "\n";
  7135. }
  7136. }
  7137. return $s;
  7138. }
  7139. /**
  7140. * This method allows printing text with line breaks.
  7141. * 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 />
  7142. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  7143. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  7144. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  7145. * @param $txt (string) String to print
  7146. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  7147. * @param $align (string) 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>
  7148. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  7149. * @param $ln (int) 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>
  7150. * @param $x (float) x position in user units
  7151. * @param $y (float) y position in user units
  7152. * @param $reseth (boolean) if true reset the last cell height (default true).
  7153. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  7154. * @param $ishtml (boolean) INTERNAL USE ONLY -- set to true if $txt is HTML content (default = false). Never set this parameter to true, use instead writeHTMLCell() or writeHTML() methods.
  7155. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  7156. * @param $maxh (float) 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.
  7157. * @param $valign (string) Vertical alignment of text (requires $maxh = $h > 0). Possible values are:<ul><li>T: TOP</li><li>M: middle</li><li>B: bottom</li></ul>. This feature works only when $ishtml=false and the cell must fit in a single page.
  7158. * @param $fitcell (boolean) if true attempt to fit all the text within the cell by reducing the font size (do not work in HTML mode).
  7159. * @return int Return the number of cells or 1 for html mode.
  7160. * @public
  7161. * @since 1.3
  7162. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  7163. */
  7164. public function MultiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false, $ln = 1, $x = '', $y = '', $reseth = true, $stretch = 0, $ishtml = false, $autopadding = true, $maxh = 0, $valign = 'T', $fitcell = false) {
  7165. $prev_cell_margin = $this->cell_margin;
  7166. $prev_cell_padding = $this->cell_padding;
  7167. // adjust internal padding
  7168. $this->adjustCellPadding($border);
  7169. $mc_padding = $this->cell_padding;
  7170. $mc_margin = $this->cell_margin;
  7171. $this->cell_padding['T'] = 0;
  7172. $this->cell_padding['B'] = 0;
  7173. $this->setCellMargins(0, 0, 0, 0);
  7174. if ($this->empty_string($this->lasth) OR $reseth) {
  7175. // reset row height
  7176. $this->resetLastH();
  7177. }
  7178. if (!$this->empty_string($y)) {
  7179. $this->SetY($y);
  7180. } else {
  7181. $y = $this->GetY();
  7182. }
  7183. $resth = 0;
  7184. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  7185. // spit cell in more pages/columns
  7186. $newh = ($this->PageBreakTrigger - $y);
  7187. $resth = ($h - $newh); // cell to be printed on the next page/column
  7188. $h = $newh;
  7189. }
  7190. // get current page number
  7191. $startpage = $this->page;
  7192. // get current column
  7193. $startcolumn = $this->current_column;
  7194. if (!$this->empty_string($x)) {
  7195. $this->SetX($x);
  7196. } else {
  7197. $x = $this->GetX();
  7198. }
  7199. // check page for no-write regions and adapt page margins if necessary
  7200. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  7201. // apply margins
  7202. $oy = $y + $mc_margin['T'];
  7203. if ($this->rtl) {
  7204. $ox = ($this->w - $x - $mc_margin['R']);
  7205. } else {
  7206. $ox = ($x + $mc_margin['L']);
  7207. }
  7208. $this->x = $ox;
  7209. $this->y = $oy;
  7210. // set width
  7211. if ($this->empty_string($w) OR ($w <= 0)) {
  7212. if ($this->rtl) {
  7213. $w = ($this->x - $this->lMargin - $mc_margin['L']);
  7214. } else {
  7215. $w = ($this->w - $this->x - $this->rMargin - $mc_margin['R']);
  7216. }
  7217. }
  7218. // store original margin values
  7219. $lMargin = $this->lMargin;
  7220. $rMargin = $this->rMargin;
  7221. if ($this->rtl) {
  7222. $this->rMargin = ($this->w - $this->x);
  7223. $this->lMargin = ($this->x - $w);
  7224. } else {
  7225. $this->lMargin = ($this->x);
  7226. $this->rMargin = ($this->w - $this->x - $w);
  7227. }
  7228. $this->clMargin = $this->lMargin;
  7229. $this->crMargin = $this->rMargin;
  7230. if ($autopadding) {
  7231. // add top padding
  7232. $this->y += $mc_padding['T'];
  7233. }
  7234. if ($ishtml) { // ******* Write HTML text
  7235. $this->writeHTML($txt, true, false, $reseth, true, $align);
  7236. $nl = 1;
  7237. } else { // ******* Write simple text
  7238. $prev_FontSizePt = $this->FontSizePt;
  7239. // vertical alignment
  7240. if ($maxh > 0) {
  7241. // get text height
  7242. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  7243. if ($fitcell) {
  7244. // try to reduce font size to fit text on cell (use a quick search algorithm)
  7245. $fmin = 1;
  7246. $fmax = $this->FontSizePt;
  7247. $prev_text_height = $text_height;
  7248. $maxit = 100; // max number of iterations
  7249. while ($maxit > 0) {
  7250. $fmid = (($fmax + $fmin) / 2);
  7251. $this->SetFontSize($fmid, false);
  7252. $this->resetLastH();
  7253. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  7254. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  7255. break;
  7256. } elseif ($text_height < $maxh) {
  7257. $fmin = $fmid;
  7258. } else {
  7259. $fmax = $fmid;
  7260. }
  7261. --$maxit;
  7262. }
  7263. $this->SetFontSize($this->FontSizePt);
  7264. }
  7265. if ($text_height < $maxh) {
  7266. if ($valign == 'M') {
  7267. // text vertically centered
  7268. $this->y += (($maxh - $text_height) / 2);
  7269. } elseif ($valign == 'B') {
  7270. // text vertically aligned on bottom
  7271. $this->y += ($maxh - $text_height);
  7272. }
  7273. }
  7274. }
  7275. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  7276. if ($fitcell) {
  7277. // restore font size
  7278. $this->SetFontSize($prev_FontSizePt);
  7279. }
  7280. }
  7281. if ($autopadding) {
  7282. // add bottom padding
  7283. $this->y += $mc_padding['B'];
  7284. }
  7285. // Get end-of-text Y position
  7286. $currentY = $this->y;
  7287. // get latest page number
  7288. $endpage = $this->page;
  7289. if ($resth > 0) {
  7290. $skip = ($endpage - $startpage);
  7291. $tmpresth = $resth;
  7292. while ($tmpresth > 0) {
  7293. if ($skip <= 0) {
  7294. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  7295. $this->checkPageBreak($this->PageBreakTrigger + 1);
  7296. }
  7297. if ($this->num_columns > 1) {
  7298. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  7299. } else {
  7300. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  7301. }
  7302. --$skip;
  7303. }
  7304. $currentY = $this->y;
  7305. $endpage = $this->page;
  7306. }
  7307. // get latest column
  7308. $endcolumn = $this->current_column;
  7309. if ($this->num_columns == 0) {
  7310. $this->num_columns = 1;
  7311. }
  7312. // disable page regions check
  7313. $check_page_regions = $this->check_page_regions;
  7314. $this->check_page_regions = false;
  7315. // get border modes
  7316. $border_start = $this->getBorderMode($border, $position = 'start');
  7317. $border_end = $this->getBorderMode($border, $position = 'end');
  7318. $border_middle = $this->getBorderMode($border, $position = 'middle');
  7319. // design borders around HTML cells.
  7320. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  7321. $ccode = '';
  7322. $this->setPage($page);
  7323. if ($this->num_columns < 2) {
  7324. // single-column mode
  7325. $this->SetX($x);
  7326. $this->y = $this->tMargin;
  7327. }
  7328. // account for margin changes
  7329. if ($page > $startpage) {
  7330. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  7331. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  7332. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  7333. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  7334. }
  7335. }
  7336. if ($startpage == $endpage) {
  7337. // single page
  7338. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  7339. $this->selectColumn($column);
  7340. if ($this->rtl) {
  7341. $this->x -= $mc_margin['R'];
  7342. } else {
  7343. $this->x += $mc_margin['L'];
  7344. }
  7345. if ($startcolumn == $endcolumn) { // single column
  7346. $cborder = $border;
  7347. $h = max($h, ($currentY - $oy));
  7348. $this->y = $oy;
  7349. } elseif ($column == $startcolumn) { // first column
  7350. $cborder = $border_start;
  7351. $this->y = $oy;
  7352. $h = $this->h - $this->y - $this->bMargin;
  7353. } elseif ($column == $endcolumn) { // end column
  7354. $cborder = $border_end;
  7355. $h = $currentY - $this->y;
  7356. if ($resth > $h) {
  7357. $h = $resth;
  7358. }
  7359. } else { // middle column
  7360. $cborder = $border_middle;
  7361. $h = $this->h - $this->y - $this->bMargin;
  7362. $resth -= $h;
  7363. }
  7364. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true) . "\n";
  7365. } // end for each column
  7366. } elseif ($page == $startpage) { // first page
  7367. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  7368. $this->selectColumn($column);
  7369. if ($this->rtl) {
  7370. $this->x -= $mc_margin['R'];
  7371. } else {
  7372. $this->x += $mc_margin['L'];
  7373. }
  7374. if ($column == $startcolumn) { // first column
  7375. $cborder = $border_start;
  7376. $this->y = $oy;
  7377. $h = $this->h - $this->y - $this->bMargin;
  7378. } else { // middle column
  7379. $cborder = $border_middle;
  7380. $h = $this->h - $this->y - $this->bMargin;
  7381. $resth -= $h;
  7382. }
  7383. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true) . "\n";
  7384. } // end for each column
  7385. } elseif ($page == $endpage) { // last page
  7386. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  7387. $this->selectColumn($column);
  7388. if ($this->rtl) {
  7389. $this->x -= $mc_margin['R'];
  7390. } else {
  7391. $this->x += $mc_margin['L'];
  7392. }
  7393. if ($column == $endcolumn) {
  7394. // end column
  7395. $cborder = $border_end;
  7396. $h = $currentY - $this->y;
  7397. if ($resth > $h) {
  7398. $h = $resth;
  7399. }
  7400. } else {
  7401. // middle column
  7402. $cborder = $border_middle;
  7403. $h = $this->h - $this->y - $this->bMargin;
  7404. $resth -= $h;
  7405. }
  7406. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true) . "\n";
  7407. } // end for each column
  7408. } else { // middle page
  7409. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  7410. $this->selectColumn($column);
  7411. if ($this->rtl) {
  7412. $this->x -= $mc_margin['R'];
  7413. } else {
  7414. $this->x += $mc_margin['L'];
  7415. }
  7416. $cborder = $border_middle;
  7417. $h = $this->h - $this->y - $this->bMargin;
  7418. $resth -= $h;
  7419. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true) . "\n";
  7420. } // end for each column
  7421. }
  7422. if ($cborder OR $fill) {
  7423. $offsetlen = strlen($ccode);
  7424. // draw border and fill
  7425. if ($this->inxobj) {
  7426. // we are inside an XObject template
  7427. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  7428. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  7429. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  7430. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  7431. } else {
  7432. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  7433. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  7434. }
  7435. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  7436. $pstart = substr($pagebuff, 0, $pagemark);
  7437. $pend = substr($pagebuff, $pagemark);
  7438. $this->xobjects[$this->xobjid]['outdata'] = $pstart . $ccode . $pend;
  7439. } else {
  7440. if (end($this->transfmrk[$this->page]) !== false) {
  7441. $pagemarkkey = key($this->transfmrk[$this->page]);
  7442. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  7443. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  7444. } elseif ($this->InFooter) {
  7445. $pagemark = $this->footerpos[$this->page];
  7446. $this->footerpos[$this->page] += $offsetlen;
  7447. } else {
  7448. $pagemark = $this->intmrk[$this->page];
  7449. $this->intmrk[$this->page] += $offsetlen;
  7450. }
  7451. $pagebuff = $this->getPageBuffer($this->page);
  7452. $pstart = substr($pagebuff, 0, $pagemark);
  7453. $pend = substr($pagebuff, $pagemark);
  7454. $this->setPageBuffer($this->page, $pstart . $ccode . $pend);
  7455. }
  7456. }
  7457. } // end for each page
  7458. // restore page regions check
  7459. $this->check_page_regions = $check_page_regions;
  7460. // Get end-of-cell Y position
  7461. $currentY = $this->GetY();
  7462. // restore previous values
  7463. if ($this->num_columns > 1) {
  7464. $this->selectColumn();
  7465. } else {
  7466. // restore original margins
  7467. $this->lMargin = $lMargin;
  7468. $this->rMargin = $rMargin;
  7469. if ($this->page > $startpage) {
  7470. // check for margin variations between pages (i.e. booklet mode)
  7471. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  7472. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  7473. if (($dl != 0) OR ($dr != 0)) {
  7474. $this->lMargin += $dl;
  7475. $this->rMargin += $dr;
  7476. }
  7477. }
  7478. }
  7479. if ($ln > 0) {
  7480. //Go to the beginning of the next line
  7481. $this->SetY($currentY + $mc_margin['B']);
  7482. if ($ln == 2) {
  7483. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  7484. }
  7485. } else {
  7486. // go left or right by case
  7487. $this->setPage($startpage);
  7488. $this->y = $y;
  7489. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  7490. }
  7491. $this->setContentMark();
  7492. $this->cell_padding = $prev_cell_padding;
  7493. $this->cell_margin = $prev_cell_margin;
  7494. $this->clMargin = $this->lMargin;
  7495. $this->crMargin = $this->rMargin;
  7496. return $nl;
  7497. }
  7498. /**
  7499. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  7500. * @param $brd (mixed) Indicates if borders must be drawn around the cell block. The value can be 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> or an array of line styles for each border group: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  7501. * @param $position (string) multicell position: 'start', 'middle', 'end'
  7502. * @return border mode array
  7503. * @protected
  7504. * @since 4.4.002 (2008-12-09)
  7505. */
  7506. protected function getBorderMode($brd, $position = 'start') {
  7507. if ((!$this->opencell) OR empty($brd)) {
  7508. return $brd;
  7509. }
  7510. if ($brd == 1) {
  7511. $brd = 'LTRB';
  7512. }
  7513. if (is_string($brd)) {
  7514. // convert string to array
  7515. $slen = strlen($brd);
  7516. $newbrd = array();
  7517. for ($i = 0; $i < $slen; ++$i) {
  7518. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  7519. }
  7520. $brd = $newbrd;
  7521. }
  7522. foreach ($brd as $border => $style) {
  7523. switch ($position) {
  7524. case 'start': {
  7525. if (strpos($border, 'B') !== false) {
  7526. // remove bottom line
  7527. $newkey = str_replace('B', '', $border);
  7528. if (strlen($newkey) > 0) {
  7529. $brd[$newkey] = $style;
  7530. }
  7531. unset($brd[$border]);
  7532. }
  7533. break;
  7534. }
  7535. case 'middle': {
  7536. if (strpos($border, 'B') !== false) {
  7537. // remove bottom line
  7538. $newkey = str_replace('B', '', $border);
  7539. if (strlen($newkey) > 0) {
  7540. $brd[$newkey] = $style;
  7541. }
  7542. unset($brd[$border]);
  7543. $border = $newkey;
  7544. }
  7545. if (strpos($border, 'T') !== false) {
  7546. // remove bottom line
  7547. $newkey = str_replace('T', '', $border);
  7548. if (strlen($newkey) > 0) {
  7549. $brd[$newkey] = $style;
  7550. }
  7551. unset($brd[$border]);
  7552. }
  7553. break;
  7554. }
  7555. case 'end': {
  7556. if (strpos($border, 'T') !== false) {
  7557. // remove bottom line
  7558. $newkey = str_replace('T', '', $border);
  7559. if (strlen($newkey) > 0) {
  7560. $brd[$newkey] = $style;
  7561. }
  7562. unset($brd[$border]);
  7563. }
  7564. break;
  7565. }
  7566. }
  7567. }
  7568. return $brd;
  7569. }
  7570. /**
  7571. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  7572. * @param $txt (string) String for calculating his height
  7573. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  7574. * @param $reseth (boolean) if true reset the last cell height (default false).
  7575. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  7576. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  7577. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  7578. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  7579. * @author Alexander Escalona Fern�ndez, Nicola Asuni
  7580. * @public
  7581. * @since 4.5.011
  7582. */
  7583. public function getNumLines($txt, $w = 0, $reseth = false, $autopadding = true, $cellpadding = '', $border = 0) {
  7584. if ($txt === '') {
  7585. // empty string
  7586. return 1;
  7587. }
  7588. // adjust internal padding
  7589. $prev_cell_padding = $this->cell_padding;
  7590. $prev_lasth = $this->lasth;
  7591. if (is_array($cellpadding)) {
  7592. $this->cell_padding = $cellpadding;
  7593. }
  7594. $this->adjustCellPadding($border);
  7595. if ($this->empty_string($w) OR ($w <= 0)) {
  7596. if ($this->rtl) {
  7597. $w = $this->x - $this->lMargin;
  7598. } else {
  7599. $w = $this->w - $this->rMargin - $this->x;
  7600. }
  7601. }
  7602. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  7603. if ($reseth) {
  7604. // reset row height
  7605. $this->resetLastH();
  7606. }
  7607. $lines = 1;
  7608. $sum = 0;
  7609. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  7610. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  7611. $length = count($chars);
  7612. $lastSeparator = -1;
  7613. for ($i = 0; $i < $length; ++$i) {
  7614. $charWidth = $charsWidth[$i];
  7615. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  7616. $lastSeparator = $i;
  7617. }
  7618. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  7619. ++$lines;
  7620. if ($chars[$i] == 10) {
  7621. $lastSeparator = -1;
  7622. $sum = 0;
  7623. } elseif ($lastSeparator != -1) {
  7624. $i = $lastSeparator;
  7625. $lastSeparator = -1;
  7626. $sum = 0;
  7627. } else {
  7628. $sum = $charWidth;
  7629. }
  7630. } else {
  7631. $sum += $charWidth;
  7632. }
  7633. }
  7634. if ($chars[($length - 1)] == 10) {
  7635. --$lines;
  7636. }
  7637. $this->cell_padding = $prev_cell_padding;
  7638. $this->lasth = $prev_lasth;
  7639. return $lines;
  7640. }
  7641. /**
  7642. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  7643. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  7644. * @pre
  7645. * // store current object
  7646. * $pdf->startTransaction();
  7647. * // store starting values
  7648. * $start_y = $pdf->GetY();
  7649. * $start_page = $pdf->getPage();
  7650. * // call your printing functions with your parameters
  7651. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7652. * $pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
  7653. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7654. * // get the new Y
  7655. * $end_y = $pdf->GetY();
  7656. * $end_page = $pdf->getPage();
  7657. * // calculate height
  7658. * $height = 0;
  7659. * if ($end_page == $start_page) {
  7660. * $height = $end_y - $start_y;
  7661. * } else {
  7662. * for ($page=$start_page; $page <= $end_page; ++$page) {
  7663. * $this->setPage($page);
  7664. * if ($page == $start_page) {
  7665. * // first page
  7666. * $height = $this->h - $start_y - $this->bMargin;
  7667. * } elseif ($page == $end_page) {
  7668. * // last page
  7669. * $height = $end_y - $this->tMargin;
  7670. * } else {
  7671. * $height = $this->h - $this->tMargin - $this->bMargin;
  7672. * }
  7673. * }
  7674. * }
  7675. * // restore previous object
  7676. * $pdf = $pdf->rollbackTransaction();
  7677. *
  7678. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  7679. * @param $txt (string) String for calculating his height
  7680. * @param $reseth (boolean) if true reset the last cell height (default false).
  7681. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  7682. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  7683. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  7684. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  7685. * @author Nicola Asuni, Alexander Escalona Fern�ndez
  7686. * @public
  7687. */
  7688. public function getStringHeight($w, $txt, $reseth = false, $autopadding = true, $cellpadding = '', $border = 0) {
  7689. // adjust internal padding
  7690. $prev_cell_padding = $this->cell_padding;
  7691. $prev_lasth = $this->lasth;
  7692. if (is_array($cellpadding)) {
  7693. $this->cell_padding = $cellpadding;
  7694. }
  7695. $this->adjustCellPadding($border);
  7696. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  7697. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  7698. if ($autopadding) {
  7699. // add top and bottom padding
  7700. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  7701. }
  7702. $this->cell_padding = $prev_cell_padding;
  7703. $this->lasth = $prev_lasth;
  7704. return $height;
  7705. }
  7706. /**
  7707. * This method prints text from the current position.<br />
  7708. * @param $h (float) Line height
  7709. * @param $txt (string) String to print
  7710. * @param $link (mixed) URL or identifier returned by AddLink()
  7711. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  7712. * @param $align (string) 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>
  7713. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  7714. * @param $stretch (int) font stretch mode: <ul><li>0 = disabled</li><li>1 = horizontal scaling only if text is larger than cell width</li><li>2 = forced horizontal scaling to fit cell width</li><li>3 = character spacing only if text is larger than cell width</li><li>4 = forced character spacing to fit cell width</li></ul> General font stretching and scaling values will be preserved when possible.
  7715. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  7716. * @param $firstblock (boolean) if true the string is the starting of a line.
  7717. * @param $maxh (float) 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.
  7718. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  7719. * @param $margin (array) margin array of the parent container
  7720. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  7721. * @public
  7722. * @since 1.5
  7723. */
  7724. public function Write($h, $txt, $link = '', $fill = false, $align = '', $ln = false, $stretch = 0, $firstline = false, $firstblock = false, $maxh = 0, $wadj = 0, $margin = '') {
  7725. // check page for no-write regions and adapt page margins if necessary
  7726. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  7727. if (strlen($txt) == 0) {
  7728. // fix empty text
  7729. $txt = ' ';
  7730. }
  7731. if ($margin === '') {
  7732. // set default margins
  7733. $margin = $this->cell_margin;
  7734. }
  7735. // remove carriage returns
  7736. $s = str_replace("\r", '', $txt);
  7737. // check if string contains arabic text
  7738. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  7739. $arabic = true;
  7740. } else {
  7741. $arabic = false;
  7742. }
  7743. // check if string contains RTL text
  7744. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  7745. $rtlmode = true;
  7746. } else {
  7747. $rtlmode = false;
  7748. }
  7749. // get a char width
  7750. $chrwidth = $this->GetCharWidth(46); // dot character
  7751. // get array of unicode values
  7752. $chars = $this->UTF8StringToArray($s);
  7753. // calculate maximum width for a single character on string
  7754. $chrw = $this->GetArrStringWidth($chars, '', '', 0, true);
  7755. array_walk($chrw, array($this, 'getRawCharWidth'));
  7756. $maxchwidth = max($chrw);
  7757. // get array of chars
  7758. $uchars = $this->UTF8ArrayToUniArray($chars);
  7759. // get the number of characters
  7760. $nb = count($chars);
  7761. // replacement for SHY character (minus symbol)
  7762. $shy_replacement = 45;
  7763. $shy_replacement_char = $this->unichr($shy_replacement);
  7764. // widht for SHY replacement
  7765. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  7766. // max Y
  7767. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  7768. // page width
  7769. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  7770. // calculate remaining line width ($w)
  7771. if ($this->rtl) {
  7772. $w = $this->x - $this->lMargin;
  7773. } else {
  7774. $w = $this->w - $this->rMargin - $this->x;
  7775. }
  7776. // max column width
  7777. $wmax = ($w - $wadj);
  7778. if (!$firstline) {
  7779. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  7780. }
  7781. if ((!$firstline) AND (($chrwidth > $wmax) OR ($maxchwidth > $wmax))) {
  7782. // the maximum width character do not fit on column
  7783. return '';
  7784. }
  7785. // minimum row height
  7786. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  7787. $start_page = $this->page;
  7788. $i = 0; // character position
  7789. $j = 0; // current starting position
  7790. $sep = -1; // position of the last blank space
  7791. $shy = false; // true if the last blank is a soft hypen (SHY)
  7792. $l = 0; // current string length
  7793. $nl = 0; //number of lines
  7794. $linebreak = false;
  7795. $pc = 0; // previous character
  7796. // for each character
  7797. while ($i < $nb) {
  7798. if (($maxh > 0) AND ($this->y >= $maxy)) {
  7799. break;
  7800. }
  7801. //Get the current character
  7802. $c = $chars[$i];
  7803. if ($c == 10) { // 10 = "\n" = new line
  7804. //Explicit line break
  7805. if ($align == 'J') {
  7806. if ($this->rtl) {
  7807. $talign = 'R';
  7808. } else {
  7809. $talign = 'L';
  7810. }
  7811. } else {
  7812. $talign = $align;
  7813. }
  7814. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  7815. if ($firstline) {
  7816. $startx = $this->x;
  7817. $tmparr = array_slice($chars, $j, ($i - $j));
  7818. if ($rtlmode) {
  7819. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7820. }
  7821. $linew = $this->GetArrStringWidth($tmparr);
  7822. unset($tmparr);
  7823. if ($this->rtl) {
  7824. $this->endlinex = $startx - $linew;
  7825. } else {
  7826. $this->endlinex = $startx + $linew;
  7827. }
  7828. $w = $linew;
  7829. $tmpcellpadding = $this->cell_padding;
  7830. if ($maxh == 0) {
  7831. $this->SetCellPadding(0);
  7832. }
  7833. }
  7834. if ($firstblock AND $this->isRTLTextDir()) {
  7835. $tmpstr = $this->stringRightTrim($tmpstr);
  7836. }
  7837. // Skip newlines at the begining of a page or column
  7838. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  7839. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  7840. }
  7841. unset($tmpstr);
  7842. if ($firstline) {
  7843. $this->cell_padding = $tmpcellpadding;
  7844. return ($this->UniArrSubString($uchars, $i));
  7845. }
  7846. ++$nl;
  7847. $j = $i + 1;
  7848. $l = 0;
  7849. $sep = -1;
  7850. $shy = false;
  7851. // account for margin changes
  7852. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  7853. $this->AcceptPageBreak();
  7854. if ($this->rtl) {
  7855. $this->x -= $margin['R'];
  7856. } else {
  7857. $this->x += $margin['L'];
  7858. }
  7859. $this->lMargin += $margin['L'];
  7860. $this->rMargin += $margin['R'];
  7861. }
  7862. $w = $this->getRemainingWidth();
  7863. $wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
  7864. } else {
  7865. // 160 is the non-breaking space.
  7866. // 173 is SHY (Soft Hypen).
  7867. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  7868. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  7869. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  7870. if (($c != 160)
  7871. AND (($c == 173)
  7872. OR preg_match($this->re_spaces, $this->unichr($c))
  7873. OR (($c == 45)
  7874. AND ($i < ($nb - 1))
  7875. AND @preg_match('/[\p{L}]/' . $this->re_space['m'], $this->unichr($pc))
  7876. AND @preg_match('/[\p{L}]/' . $this->re_space['m'], $this->unichr($chars[($i + 1)]))
  7877. )
  7878. )
  7879. ) {
  7880. // update last blank space position
  7881. $sep = $i;
  7882. // check if is a SHY
  7883. if (($c == 173) OR ($c == 45)) {
  7884. $shy = true;
  7885. if ($pc == 45) {
  7886. $tmp_shy_replacement_width = 0;
  7887. $tmp_shy_replacement_char = '';
  7888. } else {
  7889. $tmp_shy_replacement_width = $shy_replacement_width;
  7890. $tmp_shy_replacement_char = $shy_replacement_char;
  7891. }
  7892. } else {
  7893. $shy = false;
  7894. }
  7895. }
  7896. // update string length
  7897. if ($this->isUnicodeFont() AND ($arabic)) {
  7898. // with bidirectional algorithm some chars may be changed affecting the line length
  7899. // *** very slow ***
  7900. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  7901. } else {
  7902. $l += $this->GetCharWidth($c);
  7903. }
  7904. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax))) {
  7905. // we have reached the end of column
  7906. if ($sep == -1) {
  7907. // check if the line was already started
  7908. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
  7909. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
  7910. // print a void cell and go to next line
  7911. $this->Cell($w, $h, '', 0, 1);
  7912. $linebreak = true;
  7913. if ($firstline) {
  7914. return ($this->UniArrSubString($uchars, $j));
  7915. }
  7916. } else {
  7917. // truncate the word because do not fit on column
  7918. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  7919. if ($firstline) {
  7920. $startx = $this->x;
  7921. $tmparr = array_slice($chars, $j, ($i - $j));
  7922. if ($rtlmode) {
  7923. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7924. }
  7925. $linew = $this->GetArrStringWidth($tmparr);
  7926. unset($tmparr);
  7927. if ($this->rtl) {
  7928. $this->endlinex = $startx - $linew;
  7929. } else {
  7930. $this->endlinex = $startx + $linew;
  7931. }
  7932. $w = $linew;
  7933. $tmpcellpadding = $this->cell_padding;
  7934. if ($maxh == 0) {
  7935. $this->SetCellPadding(0);
  7936. }
  7937. }
  7938. if ($firstblock AND $this->isRTLTextDir()) {
  7939. $tmpstr = $this->stringRightTrim($tmpstr);
  7940. }
  7941. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  7942. unset($tmpstr);
  7943. if ($firstline) {
  7944. $this->cell_padding = $tmpcellpadding;
  7945. return ($this->UniArrSubString($uchars, $i));
  7946. }
  7947. $j = $i;
  7948. --$i;
  7949. }
  7950. } else {
  7951. // word wrapping
  7952. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  7953. $endspace = 1;
  7954. } else {
  7955. $endspace = 0;
  7956. }
  7957. // check the length of the next string
  7958. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  7959. $nextstr = preg_split('/' . $this->re_space['p'] . '/' . $this->re_space['m'], $this->stringTrim($strrest));
  7960. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  7961. // truncate the word because do not fit on a full page width
  7962. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  7963. if ($firstline) {
  7964. $startx = $this->x;
  7965. $tmparr = array_slice($chars, $j, ($i - $j));
  7966. if ($rtlmode) {
  7967. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7968. }
  7969. $linew = $this->GetArrStringWidth($tmparr);
  7970. unset($tmparr);
  7971. if ($this->rtl) {
  7972. $this->endlinex = ($startx - $linew);
  7973. } else {
  7974. $this->endlinex = ($startx + $linew);
  7975. }
  7976. $w = $linew;
  7977. $tmpcellpadding = $this->cell_padding;
  7978. if ($maxh == 0) {
  7979. $this->SetCellPadding(0);
  7980. }
  7981. }
  7982. if ($firstblock AND $this->isRTLTextDir()) {
  7983. $tmpstr = $this->stringRightTrim($tmpstr);
  7984. }
  7985. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  7986. unset($tmpstr);
  7987. if ($firstline) {
  7988. $this->cell_padding = $tmpcellpadding;
  7989. return ($this->UniArrSubString($uchars, $i));
  7990. }
  7991. $j = $i;
  7992. --$i;
  7993. } else {
  7994. // word wrapping
  7995. if ($shy) {
  7996. // add hypen (minus symbol) at the end of the line
  7997. $shy_width = $tmp_shy_replacement_width;
  7998. if ($this->rtl) {
  7999. $shy_char_left = $tmp_shy_replacement_char;
  8000. $shy_char_right = '';
  8001. } else {
  8002. $shy_char_left = '';
  8003. $shy_char_right = $tmp_shy_replacement_char;
  8004. }
  8005. } else {
  8006. $shy_width = 0;
  8007. $shy_char_left = '';
  8008. $shy_char_right = '';
  8009. }
  8010. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  8011. if ($firstline) {
  8012. $startx = $this->x;
  8013. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  8014. if ($rtlmode) {
  8015. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  8016. }
  8017. $linew = $this->GetArrStringWidth($tmparr);
  8018. unset($tmparr);
  8019. if ($this->rtl) {
  8020. $this->endlinex = $startx - $linew - $shy_width;
  8021. } else {
  8022. $this->endlinex = $startx + $linew + $shy_width;
  8023. }
  8024. $w = $linew;
  8025. $tmpcellpadding = $this->cell_padding;
  8026. if ($maxh == 0) {
  8027. $this->SetCellPadding(0);
  8028. }
  8029. }
  8030. // print the line
  8031. if ($firstblock AND $this->isRTLTextDir()) {
  8032. $tmpstr = $this->stringRightTrim($tmpstr);
  8033. }
  8034. $this->Cell($w, $h, $shy_char_left . $tmpstr . $shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  8035. unset($tmpstr);
  8036. if ($firstline) {
  8037. if ($chars[$sep] == 45) {
  8038. $endspace += 1;
  8039. }
  8040. // return the remaining text
  8041. $this->cell_padding = $tmpcellpadding;
  8042. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  8043. }
  8044. $i = $sep;
  8045. $sep = -1;
  8046. $shy = false;
  8047. $j = ($i + 1);
  8048. }
  8049. }
  8050. // account for margin changes
  8051. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  8052. $this->AcceptPageBreak();
  8053. if ($this->rtl) {
  8054. $this->x -= $margin['R'];
  8055. } else {
  8056. $this->x += $margin['L'];
  8057. }
  8058. $this->lMargin += $margin['L'];
  8059. $this->rMargin += $margin['R'];
  8060. }
  8061. $w = $this->getRemainingWidth();
  8062. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  8063. if ($linebreak) {
  8064. $linebreak = false;
  8065. } else {
  8066. ++$nl;
  8067. $l = 0;
  8068. }
  8069. }
  8070. }
  8071. // save last character
  8072. $pc = $c;
  8073. ++$i;
  8074. } // end while i < nb
  8075. // print last substring (if any)
  8076. if ($l > 0) {
  8077. switch ($align) {
  8078. case 'J':
  8079. case 'C': {
  8080. $w = $w;
  8081. break;
  8082. }
  8083. case 'L': {
  8084. if ($this->rtl) {
  8085. $w = $w;
  8086. } else {
  8087. $w = $l;
  8088. }
  8089. break;
  8090. }
  8091. case 'R': {
  8092. if ($this->rtl) {
  8093. $w = $l;
  8094. } else {
  8095. $w = $w;
  8096. }
  8097. break;
  8098. }
  8099. default: {
  8100. $w = $l;
  8101. break;
  8102. }
  8103. }
  8104. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  8105. if ($firstline) {
  8106. $startx = $this->x;
  8107. $tmparr = array_slice($chars, $j, ($nb - $j));
  8108. if ($rtlmode) {
  8109. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  8110. }
  8111. $linew = $this->GetArrStringWidth($tmparr);
  8112. unset($tmparr);
  8113. if ($this->rtl) {
  8114. $this->endlinex = $startx - $linew;
  8115. } else {
  8116. $this->endlinex = $startx + $linew;
  8117. }
  8118. $w = $linew;
  8119. $tmpcellpadding = $this->cell_padding;
  8120. if ($maxh == 0) {
  8121. $this->SetCellPadding(0);
  8122. }
  8123. }
  8124. if ($firstblock AND $this->isRTLTextDir()) {
  8125. $tmpstr = $this->stringRightTrim($tmpstr);
  8126. }
  8127. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  8128. unset($tmpstr);
  8129. if ($firstline) {
  8130. $this->cell_padding = $tmpcellpadding;
  8131. return ($this->UniArrSubString($uchars, $nb));
  8132. }
  8133. ++$nl;
  8134. }
  8135. if ($firstline) {
  8136. return '';
  8137. }
  8138. return $nl;
  8139. }
  8140. /**
  8141. * Returns the remaining width between the current position and margins.
  8142. * @return int Return the remaining width
  8143. * @protected
  8144. */
  8145. protected function getRemainingWidth() {
  8146. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  8147. if ($this->rtl) {
  8148. return ($this->x - $this->lMargin);
  8149. } else {
  8150. return ($this->w - $this->rMargin - $this->x);
  8151. }
  8152. }
  8153. /**
  8154. * Extract a slice of the $strarr array and return it as string.
  8155. * @param $strarr (string) The input array of characters.
  8156. * @param $start (int) the starting element of $strarr.
  8157. * @param $end (int) first element that will not be returned.
  8158. * @return Return part of a string
  8159. * @public
  8160. */
  8161. public function UTF8ArrSubString($strarr, $start = '', $end = '') {
  8162. if (strlen($start) == 0) {
  8163. $start = 0;
  8164. }
  8165. if (strlen($end) == 0) {
  8166. $end = count($strarr);
  8167. }
  8168. $string = '';
  8169. for ($i = $start; $i < $end; ++$i) {
  8170. $string .= $this->unichr($strarr[$i]);
  8171. }
  8172. return $string;
  8173. }
  8174. /**
  8175. * Extract a slice of the $uniarr array and return it as string.
  8176. * @param $uniarr (string) The input array of characters.
  8177. * @param $start (int) the starting element of $strarr.
  8178. * @param $end (int) first element that will not be returned.
  8179. * @return Return part of a string
  8180. * @public
  8181. * @since 4.5.037 (2009-04-07)
  8182. */
  8183. public function UniArrSubString($uniarr, $start = '', $end = '') {
  8184. if (strlen($start) == 0) {
  8185. $start = 0;
  8186. }
  8187. if (strlen($end) == 0) {
  8188. $end = count($uniarr);
  8189. }
  8190. $string = '';
  8191. for ($i = $start; $i < $end; ++$i) {
  8192. $string .= $uniarr[$i];
  8193. }
  8194. return $string;
  8195. }
  8196. /**
  8197. * Convert an array of UTF8 values to array of unicode characters
  8198. * @param $ta (string) The input array of UTF8 values.
  8199. * @return Return array of unicode characters
  8200. * @public
  8201. * @since 4.5.037 (2009-04-07)
  8202. */
  8203. public function UTF8ArrayToUniArray($ta) {
  8204. return array_map(array($this, 'unichr'), $ta);
  8205. }
  8206. /**
  8207. * Returns the unicode caracter specified by UTF-8 value
  8208. * @param $c (int) UTF-8 value
  8209. * @return Returns the specified character.
  8210. * @author Miguel Perez, Nicola Asuni
  8211. * @public
  8212. * @since 2.3.000 (2008-03-05)
  8213. */
  8214. public function unichr($c) {
  8215. if (!$this->isunicode) {
  8216. return chr($c);
  8217. } elseif ($c <= 0x7F) {
  8218. // one byte
  8219. return chr($c);
  8220. } elseif ($c <= 0x7FF) {
  8221. // two bytes
  8222. return chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F);
  8223. } elseif ($c <= 0xFFFF) {
  8224. // three bytes
  8225. return chr(0xE0 | $c >> 12) . chr(0x80 | $c >> 6 & 0x3F) . chr(0x80 | $c & 0x3F);
  8226. } elseif ($c <= 0x10FFFF) {
  8227. // four bytes
  8228. return chr(0xF0 | $c >> 18) . chr(0x80 | $c >> 12 & 0x3F) . chr(0x80 | $c >> 6 & 0x3F) . chr(0x80 | $c & 0x3F);
  8229. } else {
  8230. return '';
  8231. }
  8232. }
  8233. /**
  8234. * Return the image type given the file name or array returned by getimagesize() function.
  8235. * @param $imgfile (string) image file name
  8236. * @param $iminfo (array) array of image information returned by getimagesize() function.
  8237. * @return string image type
  8238. * @since 4.8.017 (2009-11-27)
  8239. */
  8240. public function getImageFileType($imgfile, $iminfo = array()) {
  8241. $type = '';
  8242. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  8243. $mime = explode('/', $iminfo['mime']);
  8244. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  8245. $type = strtolower(trim($mime[1]));
  8246. }
  8247. }
  8248. if (empty($type)) {
  8249. $fileinfo = pathinfo($imgfile);
  8250. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  8251. $type = strtolower(trim($fileinfo['extension']));
  8252. }
  8253. }
  8254. if ($type == 'jpg') {
  8255. $type = 'jpeg';
  8256. }
  8257. return $type;
  8258. }
  8259. /**
  8260. * Set the block dimensions accounting for page breaks and page/column fitting
  8261. * @param $w (float) width
  8262. * @param $h (float) height
  8263. * @param $x (float) X coordinate
  8264. * @param $y (float) Y coodiante
  8265. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  8266. * @return array($w, $h, $x, $y)
  8267. * @protected
  8268. * @since 5.5.009 (2010-07-05)
  8269. */
  8270. protected function fitBlock($w, $h, $x, $y, $fitonpage = false) {
  8271. if ($w <= 0) {
  8272. // set maximum width
  8273. $w = ($this->w - $this->lMargin - $this->rMargin);
  8274. }
  8275. if ($h <= 0) {
  8276. // set maximum height
  8277. $h = ($this->PageBreakTrigger - $this->tMargin);
  8278. }
  8279. // resize the block to be vertically contained on a single page or single column
  8280. if ($fitonpage OR $this->AutoPageBreak) {
  8281. $ratio_wh = ($w / $h);
  8282. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  8283. $h = $this->PageBreakTrigger - $this->tMargin;
  8284. $w = ($h * $ratio_wh);
  8285. }
  8286. // resize the block to be horizontally contained on a single page or single column
  8287. if ($fitonpage) {
  8288. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  8289. if ($w > $maxw) {
  8290. $w = $maxw;
  8291. $h = ($w / $ratio_wh);
  8292. }
  8293. }
  8294. }
  8295. // Check whether we need a new page or new column first as this does not fit
  8296. $prev_x = $this->x;
  8297. $prev_y = $this->y;
  8298. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  8299. $y = $this->y;
  8300. if ($this->rtl) {
  8301. $x += ($prev_x - $this->x);
  8302. } else {
  8303. $x += ($this->x - $prev_x);
  8304. }
  8305. $this->newline = true;
  8306. }
  8307. // resize the block to be contained on the remaining available page or column space
  8308. if ($fitonpage) {
  8309. $ratio_wh = ($w / $h);
  8310. if (($y + $h) > $this->PageBreakTrigger) {
  8311. $h = $this->PageBreakTrigger - $y;
  8312. $w = ($h * $ratio_wh);
  8313. }
  8314. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  8315. $w = $this->w - $this->rMargin - $x;
  8316. $h = ($w / $ratio_wh);
  8317. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  8318. $w = $x - $this->lMargin;
  8319. $h = ($w / $ratio_wh);
  8320. }
  8321. }
  8322. return array($w, $h, $x, $y);
  8323. }
  8324. /**
  8325. * Puts an image in the page.
  8326. * The upper-left corner must be given.
  8327. * The dimensions can be specified in different ways:<ul>
  8328. * <li>explicit width and height (expressed in user unit)</li>
  8329. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  8330. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  8331. * 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;
  8332. * The format can be specified explicitly or inferred from the file extension.<br />
  8333. * It is possible to put a link on the image.<br />
  8334. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  8335. * @param $file (string) Name of the file containing the image or a '@' character followed by the image data string. To link an image without embedding it on the document, set an asterisk character before the URL (i.e.: '*http://www.example.com/image.jpg').
  8336. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  8337. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  8338. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  8339. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  8340. * @param $type (string) 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.
  8341. * @param $link (mixed) URL or identifier returned by AddLink().
  8342. * @param $align (string) 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>
  8343. * @param $resize (mixed) If true resize (reduce) the image to fit $w and $h (requires GD or ImageMagick library); if false do not resize; if 2 force resize in all cases (upscaling and downscaling).
  8344. * @param $dpi (int) dot-per-inch resolution used on resize
  8345. * @param $palign (string) 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>
  8346. * @param $ismask (boolean) true if this image is a mask, false otherwise
  8347. * @param $imgmask (mixed) image object returned by this function or false
  8348. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  8349. * @param $fitbox (mixed) If not false scale image dimensions proportionally to fit within the ($w, $h) box. $fitbox can be true or a 2 characters string indicating the image alignment inside the box. The first character indicate the horizontal alignment (L = left, C = center, R = right) the second character indicate the vertical algnment (T = top, M = middle, B = bottom).
  8350. * @param $hidden (boolean) If true do not display the image.
  8351. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  8352. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  8353. * @param $altimgs (array) Array of alternate images IDs. Each alternative image must be an array with two values: an integer representing the image ID (the value returned by the Image method) and a boolean value to indicate if the image is the default for printing.
  8354. * @return image information
  8355. * @public
  8356. * @since 1.1
  8357. */
  8358. 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, $hidden = false, $fitonpage = false, $alt = false, $altimgs = array()) {
  8359. if ($this->state != 2) {
  8360. return;
  8361. }
  8362. if ($x === '') {
  8363. $x = $this->x;
  8364. }
  8365. if ($y === '') {
  8366. $y = $this->y;
  8367. }
  8368. // check page for no-write regions and adapt page margins if necessary
  8369. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  8370. $exurl = ''; // external streams
  8371. // check if we are passing an image as file or string
  8372. if ($file[0] === '@') {
  8373. // image from string
  8374. $imgdata = substr($file, 1);
  8375. $file = $this->getObjFilename('img');
  8376. $fp = fopen($file, 'w');
  8377. fwrite($fp, $imgdata);
  8378. fclose($fp);
  8379. unset($imgdata);
  8380. $imsize = @getimagesize($file);
  8381. if ($imsize === FALSE) {
  8382. unlink($file);
  8383. } else {
  8384. $this->cached_files[] = $file;
  8385. }
  8386. } else { // image file
  8387. if ($file{0} === '*') {
  8388. // image as external stream
  8389. $file = substr($file, 1);
  8390. $exurl = $file;
  8391. }
  8392. // check if is local file
  8393. if (!@file_exists($file)) {
  8394. // encode spaces on filename (file is probably an URL)
  8395. $file = str_replace(' ', '%20', $file);
  8396. }
  8397. if (@file_exists($file)) {
  8398. // get image dimensions
  8399. $imsize = @getimagesize($file);
  8400. } else {
  8401. $imsize = false;
  8402. }
  8403. if ($imsize === FALSE) {
  8404. if (function_exists('curl_init')) {
  8405. // try to get remote file data using cURL
  8406. $cs = curl_init(); // curl session
  8407. curl_setopt($cs, CURLOPT_URL, $file);
  8408. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  8409. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  8410. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  8411. if ((ini_get('open_basedir') == '') AND (!ini_get('safe_mode'))) {
  8412. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  8413. }
  8414. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  8415. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  8416. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  8417. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  8418. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  8419. $imgdata = curl_exec($cs);
  8420. curl_close($cs);
  8421. if ($imgdata !== FALSE) {
  8422. // copy image to cache
  8423. $file = $this->getObjFilename('img');
  8424. $fp = fopen($file, 'w');
  8425. fwrite($fp, $imgdata);
  8426. fclose($fp);
  8427. unset($imgdata);
  8428. $imsize = @getimagesize($file);
  8429. if ($imsize === FALSE) {
  8430. unlink($file);
  8431. } else {
  8432. $this->cached_files[] = $file;
  8433. }
  8434. }
  8435. } elseif (($w > 0) AND ($h > 0)) {
  8436. // get measures from specified data
  8437. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  8438. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  8439. $imsize = array($pw, $ph);
  8440. }
  8441. }
  8442. }
  8443. if ($imsize === FALSE) {
  8444. if (substr($file, 0, -34) == K_PATH_CACHE . 'msk') { // mask file
  8445. // get measures from specified data
  8446. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  8447. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  8448. $imsize = array($pw, $ph);
  8449. } else {
  8450. $this->Error('[Image] Unable to get image: ' . $file);
  8451. }
  8452. }
  8453. // file hash
  8454. $filehash = md5($this->file_id . $file);
  8455. // get original image width and height in pixels
  8456. list($pixw, $pixh) = $imsize;
  8457. // calculate image width and height on document
  8458. if (($w <= 0) AND ($h <= 0)) {
  8459. // convert image size to document unit
  8460. $w = $this->pixelsToUnits($pixw);
  8461. $h = $this->pixelsToUnits($pixh);
  8462. } elseif ($w <= 0) {
  8463. $w = $h * $pixw / $pixh;
  8464. } elseif ($h <= 0) {
  8465. $h = $w * $pixh / $pixw;
  8466. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  8467. if (strlen($fitbox) !== 2) {
  8468. // set default alignment
  8469. $fitbox = '--';
  8470. }
  8471. // scale image dimensions proportionally to fit within the ($w, $h) box
  8472. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  8473. // store current height
  8474. $oldh = $h;
  8475. // calculate new height
  8476. $h = $w * $pixh / $pixw;
  8477. // height difference
  8478. $hdiff = ($oldh - $h);
  8479. // vertical alignment
  8480. switch (strtoupper($fitbox{1})) {
  8481. case 'T': {
  8482. break;
  8483. }
  8484. case 'M': {
  8485. $y += ($hdiff / 2);
  8486. break;
  8487. }
  8488. case 'B': {
  8489. $y += $hdiff;
  8490. break;
  8491. }
  8492. }
  8493. } else {
  8494. // store current width
  8495. $oldw = $w;
  8496. // calculate new width
  8497. $w = $h * $pixw / $pixh;
  8498. // width difference
  8499. $wdiff = ($oldw - $w);
  8500. // horizontal alignment
  8501. switch (strtoupper($fitbox{0})) {
  8502. case 'L': {
  8503. if ($this->rtl) {
  8504. $x -= $wdiff;
  8505. }
  8506. break;
  8507. }
  8508. case 'C': {
  8509. if ($this->rtl) {
  8510. $x -= ($wdiff / 2);
  8511. } else {
  8512. $x += ($wdiff / 2);
  8513. }
  8514. break;
  8515. }
  8516. case 'R': {
  8517. if (!$this->rtl) {
  8518. $x += $wdiff;
  8519. }
  8520. break;
  8521. }
  8522. }
  8523. }
  8524. }
  8525. // fit the image on available space
  8526. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  8527. // calculate new minimum dimensions in pixels
  8528. $neww = round($w * $this->k * $dpi / $this->dpi);
  8529. $newh = round($h * $this->k * $dpi / $this->dpi);
  8530. // check if resize is necessary (resize is used only to reduce the image)
  8531. $newsize = ($neww * $newh);
  8532. $pixsize = ($pixw * $pixh);
  8533. if (intval($resize) == 2) {
  8534. $resize = true;
  8535. } elseif ($newsize >= $pixsize) {
  8536. $resize = false;
  8537. }
  8538. // check if image has been already added on document
  8539. $newimage = true;
  8540. if (in_array($file, $this->imagekeys)) {
  8541. $newimage = false;
  8542. // get existing image data
  8543. $info = $this->getImageBuffer($file);
  8544. if (substr($file, 0, -34) != K_PATH_CACHE . 'msk') {
  8545. // check if the newer image is larger
  8546. $oldsize = ($info['w'] * $info['h']);
  8547. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  8548. $newimage = true;
  8549. }
  8550. }
  8551. } elseif (substr($file, 0, -34) != K_PATH_CACHE . 'msk') {
  8552. // check for cached images with alpha channel
  8553. $tempfile_plain = K_PATH_CACHE . 'mskp_' . $filehash;
  8554. $tempfile_alpha = K_PATH_CACHE . 'mska_' . $filehash;
  8555. if (in_array($tempfile_plain, $this->imagekeys)) {
  8556. // get existing image data
  8557. $info = $this->getImageBuffer($tempfile_plain);
  8558. // check if the newer image is larger
  8559. $oldsize = ($info['w'] * $info['h']);
  8560. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  8561. $newimage = true;
  8562. } else {
  8563. $newimage = false;
  8564. // embed mask image
  8565. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  8566. // embed image, masked with previously embedded mask
  8567. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  8568. }
  8569. }
  8570. }
  8571. if ($newimage) {
  8572. //First use of image, get info
  8573. $type = strtolower($type);
  8574. if ($type == '') {
  8575. $type = $this->getImageFileType($file, $imsize);
  8576. } elseif ($type == 'jpg') {
  8577. $type = 'jpeg';
  8578. }
  8579. $mqr = $this->get_mqr();
  8580. $this->set_mqr(false);
  8581. // Specific image handlers
  8582. $mtd = '_parse' . $type;
  8583. // GD image handler function
  8584. $gdfunction = 'imagecreatefrom' . $type;
  8585. $info = false;
  8586. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  8587. // TCPDF image functions
  8588. $info = $this->$mtd($file);
  8589. if ($info == 'pngalpha') {
  8590. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  8591. }
  8592. }
  8593. if (!$info) {
  8594. if (function_exists($gdfunction)) {
  8595. // GD library
  8596. $img = $gdfunction($file);
  8597. if ($resize) {
  8598. $imgr = imagecreatetruecolor($neww, $newh);
  8599. if (($type == 'gif') OR ($type == 'png')) {
  8600. $imgr = $this->_setGDImageTransparency($imgr, $img);
  8601. }
  8602. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  8603. if (($type == 'gif') OR ($type == 'png')) {
  8604. $info = $this->_toPNG($imgr);
  8605. } else {
  8606. $info = $this->_toJPEG($imgr);
  8607. }
  8608. } else {
  8609. if (($type == 'gif') OR ($type == 'png')) {
  8610. $info = $this->_toPNG($img);
  8611. } else {
  8612. $info = $this->_toJPEG($img);
  8613. }
  8614. }
  8615. } elseif (extension_loaded('imagick')) {
  8616. // ImageMagick library
  8617. $img = new Imagick();
  8618. if ($type == 'SVG') {
  8619. // get SVG file content
  8620. $svgimg = file_get_contents($file);
  8621. // get width and height
  8622. $regs = array();
  8623. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  8624. $svgtag = $regs[1];
  8625. $tmp = array();
  8626. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  8627. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  8628. $owu = sprintf('%F', ($ow * $dpi / 72)) . $this->pdfunit;
  8629. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="' . $owu . '"', $svgtag, 1);
  8630. } else {
  8631. $ow = $w;
  8632. }
  8633. $tmp = array();
  8634. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  8635. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  8636. $ohu = sprintf('%F', ($oh * $dpi / 72)) . $this->pdfunit;
  8637. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="' . $ohu . '"', $svgtag, 1);
  8638. } else {
  8639. $oh = $h;
  8640. }
  8641. $tmp = array();
  8642. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  8643. $vbw = ($ow * $this->imgscale * $this->k);
  8644. $vbh = ($oh * $this->imgscale * $this->k);
  8645. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  8646. $svgtag = $vbox . $svgtag;
  8647. }
  8648. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg' . $svgtag . '>', $svgimg, 1);
  8649. }
  8650. $img->readImageBlob($svgimg);
  8651. } else {
  8652. $img->readImage($file);
  8653. }
  8654. if ($resize) {
  8655. $img->resizeImage($neww, $newh, 10, 1, false);
  8656. }
  8657. $img->setCompressionQuality($this->jpeg_quality);
  8658. $img->setImageFormat('jpeg');
  8659. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  8660. $img->writeImage($tempname);
  8661. $info = $this->_parsejpeg($tempname);
  8662. unlink($tempname);
  8663. $img->destroy();
  8664. } else {
  8665. return;
  8666. }
  8667. }
  8668. if ($info === false) {
  8669. //If false, we cannot process image
  8670. return;
  8671. }
  8672. $this->set_mqr($mqr);
  8673. if ($ismask) {
  8674. // force grayscale
  8675. $info['cs'] = 'DeviceGray';
  8676. }
  8677. if ($imgmask !== false) {
  8678. $info['masked'] = $imgmask;
  8679. }
  8680. if (!empty($exurl)) {
  8681. $info['exurl'] = $exurl;
  8682. }
  8683. // array of alternative images
  8684. $info['altimgs'] = $altimgs;
  8685. // add image to document
  8686. $info['i'] = $this->setImageBuffer($file, $info);
  8687. }
  8688. // set alignment
  8689. $this->img_rb_y = $y + $h;
  8690. // set alignment
  8691. if ($this->rtl) {
  8692. if ($palign == 'L') {
  8693. $ximg = $this->lMargin;
  8694. } elseif ($palign == 'C') {
  8695. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  8696. } elseif ($palign == 'R') {
  8697. $ximg = $this->w - $this->rMargin - $w;
  8698. } else {
  8699. $ximg = $x - $w;
  8700. }
  8701. $this->img_rb_x = $ximg;
  8702. } else {
  8703. if ($palign == 'L') {
  8704. $ximg = $this->lMargin;
  8705. } elseif ($palign == 'C') {
  8706. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  8707. } elseif ($palign == 'R') {
  8708. $ximg = $this->w - $this->rMargin - $w;
  8709. } else {
  8710. $ximg = $x;
  8711. }
  8712. $this->img_rb_x = $ximg + $w;
  8713. }
  8714. if ($ismask OR $hidden) {
  8715. // image is not displayed
  8716. return $info['i'];
  8717. }
  8718. $xkimg = $ximg * $this->k;
  8719. if (!$alt) {
  8720. // only non-alternative immages will be set
  8721. $this->_out(sprintf('q %F 0 0 %F %F %F cm /I%u Do Q', ($w * $this->k), ($h * $this->k), $xkimg, (($this->h - ($y + $h)) * $this->k), $info['i']));
  8722. }
  8723. if (!empty($border)) {
  8724. $bx = $this->x;
  8725. $by = $this->y;
  8726. $this->x = $ximg;
  8727. if ($this->rtl) {
  8728. $this->x += $w;
  8729. }
  8730. $this->y = $y;
  8731. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  8732. $this->x = $bx;
  8733. $this->y = $by;
  8734. }
  8735. if ($link) {
  8736. $this->Link($ximg, $y, $w, $h, $link, 0);
  8737. }
  8738. // set pointer to align the next text/objects
  8739. switch ($align) {
  8740. case 'T': {
  8741. $this->y = $y;
  8742. $this->x = $this->img_rb_x;
  8743. break;
  8744. }
  8745. case 'M': {
  8746. $this->y = $y + round($h / 2);
  8747. $this->x = $this->img_rb_x;
  8748. break;
  8749. }
  8750. case 'B': {
  8751. $this->y = $this->img_rb_y;
  8752. $this->x = $this->img_rb_x;
  8753. break;
  8754. }
  8755. case 'N': {
  8756. $this->SetY($this->img_rb_y);
  8757. break;
  8758. }
  8759. default: {
  8760. break;
  8761. }
  8762. }
  8763. $this->endlinex = $this->img_rb_x;
  8764. if ($this->inxobj) {
  8765. // we are inside an XObject template
  8766. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  8767. }
  8768. return $info['i'];
  8769. }
  8770. /**
  8771. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  8772. * @param $mqr (boolean) FALSE for off, TRUE for on.
  8773. * @since 4.6.025 (2009-08-17)
  8774. */
  8775. public function set_mqr($mqr) {
  8776. if (!defined('PHP_VERSION_ID')) {
  8777. $version = PHP_VERSION;
  8778. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  8779. }
  8780. if (PHP_VERSION_ID < 50300) {
  8781. @set_magic_quotes_runtime($mqr);
  8782. }
  8783. }
  8784. /**
  8785. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  8786. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  8787. * @since 4.6.025 (2009-08-17)
  8788. */
  8789. public function get_mqr() {
  8790. if (!defined('PHP_VERSION_ID')) {
  8791. $version = PHP_VERSION;
  8792. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  8793. }
  8794. if (PHP_VERSION_ID < 50300) {
  8795. return @get_magic_quotes_runtime();
  8796. }
  8797. return 0;
  8798. }
  8799. /**
  8800. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  8801. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  8802. * @param $image (image) Image object.
  8803. * return image JPEG image object.
  8804. * @protected
  8805. */
  8806. protected function _toJPEG($image) {
  8807. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  8808. imagejpeg($image, $tempname, $this->jpeg_quality);
  8809. imagedestroy($image);
  8810. $retvars = $this->_parsejpeg($tempname);
  8811. // tidy up by removing temporary image
  8812. unlink($tempname);
  8813. return $retvars;
  8814. }
  8815. /**
  8816. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  8817. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  8818. * @param $image (image) Image object.
  8819. * return image PNG image object.
  8820. * @protected
  8821. * @since 4.9.016 (2010-04-20)
  8822. */
  8823. protected function _toPNG($image) {
  8824. // set temporary image file name
  8825. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  8826. // turn off interlaced mode
  8827. imageinterlace($image, 0);
  8828. // create temporary PNG image
  8829. imagepng($image, $tempname);
  8830. // remove image from memory
  8831. imagedestroy($image);
  8832. // get PNG image data
  8833. $retvars = $this->_parsepng($tempname);
  8834. // tidy up by removing temporary image
  8835. unlink($tempname);
  8836. return $retvars;
  8837. }
  8838. /**
  8839. * Set the transparency for the given GD image.
  8840. * @param $new_image (image) GD image object
  8841. * @param $image (image) GD image object.
  8842. * return GD image object.
  8843. * @protected
  8844. * @since 4.9.016 (2010-04-20)
  8845. */
  8846. protected function _setGDImageTransparency($new_image, $image) {
  8847. // transparency index
  8848. $tid = imagecolortransparent($image);
  8849. // default transparency color
  8850. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  8851. if ($tid >= 0) {
  8852. // get the colors for the transparency index
  8853. $tcol = imagecolorsforindex($image, $tid);
  8854. }
  8855. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  8856. imagefill($new_image, 0, 0, $tid);
  8857. imagecolortransparent($new_image, $tid);
  8858. return $new_image;
  8859. }
  8860. /**
  8861. * Extract info from a JPEG file without using the GD library.
  8862. * @param $file (string) image file to parse
  8863. * @return array structure containing the image data
  8864. * @protected
  8865. */
  8866. protected function _parsejpeg($file) {
  8867. $a = getimagesize($file);
  8868. if (empty($a)) {
  8869. $this->Error('Missing or incorrect image file: ' . $file);
  8870. }
  8871. if ($a[2] != 2) {
  8872. $this->Error('Not a JPEG file: ' . $file);
  8873. }
  8874. // bits per pixel
  8875. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  8876. // number of image channels
  8877. if (!isset($a['channels'])) {
  8878. $channels = 3;
  8879. } else {
  8880. $channels = intval($a['channels']);
  8881. }
  8882. // default colour space
  8883. switch ($channels) {
  8884. case 1: {
  8885. $colspace = 'DeviceGray';
  8886. break;
  8887. }
  8888. case 3: {
  8889. $colspace = 'DeviceRGB';
  8890. break;
  8891. }
  8892. case 4: {
  8893. $colspace = 'DeviceCMYK';
  8894. break;
  8895. }
  8896. default: {
  8897. $channels = 3;
  8898. $colspace = 'DeviceRGB';
  8899. break;
  8900. }
  8901. }
  8902. // get file content
  8903. $data = file_get_contents($file);
  8904. // check for embedded ICC profile
  8905. $icc = array();
  8906. $offset = 0;
  8907. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  8908. // get ICC sequence length
  8909. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  8910. // marker sequence number
  8911. $msn = max(1, ord($data[($pos + 12)]));
  8912. // number of markers (total of APP2 used)
  8913. $nom = max(1, ord($data[($pos + 13)]));
  8914. // get sequence segment
  8915. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  8916. // move forward to next sequence
  8917. $offset = ($pos + 14 + $length);
  8918. }
  8919. // order and compact ICC segments
  8920. if (count($icc) > 0) {
  8921. ksort($icc);
  8922. $icc = implode('', $icc);
  8923. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  8924. // invalid ICC profile
  8925. $icc = false;
  8926. }
  8927. } else {
  8928. $icc = false;
  8929. }
  8930. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  8931. }
  8932. /**
  8933. * Extract info from a PNG file without using the GD library.
  8934. * @param $file (string) image file to parse
  8935. * @return array structure containing the image data
  8936. * @protected
  8937. */
  8938. protected function _parsepng($file) {
  8939. $f = fopen($file, 'rb');
  8940. if ($f === false) {
  8941. $this->Error('Can\'t open image file: ' . $file);
  8942. }
  8943. //Check signature
  8944. if (fread($f, 8) != chr(137) . 'PNG' . chr(13) . chr(10) . chr(26) . chr(10)) {
  8945. $this->Error('Not a PNG file: ' . $file);
  8946. }
  8947. //Read header chunk
  8948. fread($f, 4);
  8949. if (fread($f, 4) != 'IHDR') {
  8950. $this->Error('Incorrect PNG file: ' . $file);
  8951. }
  8952. $w = $this->_freadint($f);
  8953. $h = $this->_freadint($f);
  8954. $bpc = ord(fread($f, 1));
  8955. if ($bpc > 8) {
  8956. //$this->Error('16-bit depth not supported: '.$file);
  8957. fclose($f);
  8958. return false;
  8959. }
  8960. $ct = ord(fread($f, 1));
  8961. if ($ct == 0) {
  8962. $colspace = 'DeviceGray';
  8963. } elseif ($ct == 2) {
  8964. $colspace = 'DeviceRGB';
  8965. } elseif ($ct == 3) {
  8966. $colspace = 'Indexed';
  8967. } else {
  8968. // alpha channel
  8969. fclose($f);
  8970. return 'pngalpha';
  8971. }
  8972. if (ord(fread($f, 1)) != 0) {
  8973. //$this->Error('Unknown compression method: '.$file);
  8974. fclose($f);
  8975. return false;
  8976. }
  8977. if (ord(fread($f, 1)) != 0) {
  8978. //$this->Error('Unknown filter method: '.$file);
  8979. fclose($f);
  8980. return false;
  8981. }
  8982. if (ord(fread($f, 1)) != 0) {
  8983. //$this->Error('Interlacing not supported: '.$file);
  8984. fclose($f);
  8985. return false;
  8986. }
  8987. fread($f, 4);
  8988. $channels = ($ct == 2 ? 3 : 1);
  8989. $parms = '/DecodeParms << /Predictor 15 /Colors ' . $channels . ' /BitsPerComponent ' . $bpc . ' /Columns ' . $w . ' >>';
  8990. //Scan chunks looking for palette, transparency and image data
  8991. $pal = '';
  8992. $trns = '';
  8993. $data = '';
  8994. $icc = false;
  8995. do {
  8996. $n = $this->_freadint($f);
  8997. $type = fread($f, 4);
  8998. if ($type == 'PLTE') {
  8999. // read palette
  9000. $pal = $this->rfread($f, $n);
  9001. fread($f, 4);
  9002. } elseif ($type == 'tRNS') {
  9003. // read transparency info
  9004. $t = $this->rfread($f, $n);
  9005. if ($ct == 0) {
  9006. $trns = array(ord($t{1}));
  9007. } elseif ($ct == 2) {
  9008. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  9009. } else {
  9010. $pos = strpos($t, chr(0));
  9011. if ($pos !== false) {
  9012. $trns = array($pos);
  9013. }
  9014. }
  9015. fread($f, 4);
  9016. } elseif ($type == 'IDAT') {
  9017. // read image data block
  9018. $data .= $this->rfread($f, $n);
  9019. fread($f, 4);
  9020. } elseif ($type == 'iCCP') {
  9021. // skip profile name
  9022. $len = 0;
  9023. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  9024. ++$len;
  9025. }
  9026. // skip null separator
  9027. fread($f, 1);
  9028. // get compression method
  9029. if (ord(fread($f, 1)) != 0) {
  9030. //$this->Error('Unknown filter method: '.$file);
  9031. fclose($f);
  9032. return false;
  9033. }
  9034. // read ICC Color Profile
  9035. $icc = $this->rfread($f, ($n - $len - 2));
  9036. // decompress profile
  9037. $icc = gzuncompress($icc);
  9038. fread($f, 4);
  9039. } elseif ($type == 'IEND') {
  9040. break;
  9041. } else {
  9042. $this->rfread($f, $n + 4);
  9043. }
  9044. } while ($n);
  9045. if (($colspace == 'Indexed') AND (empty($pal))) {
  9046. //$this->Error('Missing palette in '.$file);
  9047. fclose($f);
  9048. return false;
  9049. }
  9050. fclose($f);
  9051. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  9052. }
  9053. /**
  9054. * Binary-safe and URL-safe file read.
  9055. * 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.
  9056. * @param $handle (resource)
  9057. * @param $length (int)
  9058. * @return Returns the read string or FALSE in case of error.
  9059. * @author Nicola Asuni
  9060. * @protected
  9061. * @since 4.5.027 (2009-03-16)
  9062. */
  9063. protected function rfread($handle, $length) {
  9064. $data = fread($handle, $length);
  9065. if ($data === false) {
  9066. return false;
  9067. }
  9068. $rest = $length - strlen($data);
  9069. if ($rest > 0) {
  9070. $data .= $this->rfread($handle, $rest);
  9071. }
  9072. return $data;
  9073. }
  9074. /**
  9075. * Extract info from a PNG image with alpha channel using the GD library.
  9076. * @param $file (string) Name of the file containing the image.
  9077. * @param $x (float) Abscissa of the upper-left corner.
  9078. * @param $y (float) Ordinate of the upper-left corner.
  9079. * @param $wpx (float) Original width of the image in pixels.
  9080. * @param $hpx (float) original height of the image in pixels.
  9081. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  9082. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  9083. * @param $type (string) 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.
  9084. * @param $link (mixed) URL or identifier returned by AddLink().
  9085. * @param $align (string) 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>
  9086. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  9087. * @param $dpi (int) dot-per-inch resolution used on resize
  9088. * @param $palign (string) 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>
  9089. * @param $filehash (string) File hash used to build unique file names.
  9090. * @author Nicola Asuni
  9091. * @protected
  9092. * @since 4.3.007 (2008-12-04)
  9093. * @see Image()
  9094. */
  9095. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash = '') {
  9096. if (empty($filehash)) {
  9097. $filehash = md5($this->file_id . $file);
  9098. }
  9099. // create temp image file (without alpha channel)
  9100. $tempfile_plain = K_PATH_CACHE . 'mskp_' . $filehash;
  9101. // create temp alpha file
  9102. $tempfile_alpha = K_PATH_CACHE . 'mska_' . $filehash;
  9103. if (extension_loaded('imagick')) { // ImageMagick extension
  9104. // ImageMagick library
  9105. $img = new Imagick();
  9106. $img->readImage($file);
  9107. // clone image object
  9108. $imga = $this->objclone($img);
  9109. // extract alpha channel
  9110. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  9111. $img->negateImage(true);
  9112. $img->setImageFormat('png');
  9113. $img->writeImage($tempfile_alpha);
  9114. // remove alpha channel
  9115. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  9116. $imga->setImageFormat('png');
  9117. $imga->writeImage($tempfile_plain);
  9118. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  9119. // generate images
  9120. $img = imagecreatefrompng($file);
  9121. $imgalpha = imagecreate($wpx, $hpx);
  9122. // generate gray scale palette (0 -> 255)
  9123. for ($c = 0; $c < 256; ++$c) {
  9124. ImageColorAllocate($imgalpha, $c, $c, $c);
  9125. }
  9126. // extract alpha channel
  9127. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  9128. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  9129. $color = imagecolorat($img, $xpx, $ypx);
  9130. $alpha = $this->getGDgamma($color); // correct gamma
  9131. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  9132. }
  9133. }
  9134. imagepng($imgalpha, $tempfile_alpha);
  9135. imagedestroy($imgalpha);
  9136. // extract image without alpha channel
  9137. $imgplain = imagecreatetruecolor($wpx, $hpx);
  9138. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  9139. imagepng($imgplain, $tempfile_plain);
  9140. imagedestroy($imgplain);
  9141. } else {
  9142. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  9143. }
  9144. // embed mask image
  9145. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  9146. // embed image, masked with previously embedded mask
  9147. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  9148. // remove temp files
  9149. unlink($tempfile_alpha);
  9150. unlink($tempfile_plain);
  9151. }
  9152. /**
  9153. * Get the GD-corrected PNG gamma value from alpha color
  9154. * @param $c (int) alpha color
  9155. * @protected
  9156. * @since 4.3.007 (2008-12-04)
  9157. */
  9158. protected function getGDgamma($c) {
  9159. if (!isset($this->gdgammacache["'" . $c . "'"])) {
  9160. // shifts off the first 24 bits (where 8x3 are used for each color),
  9161. // and returns the remaining 7 allocated bits (commonly used for alpha)
  9162. $alpha = ($c >> 24);
  9163. // GD alpha is only 7 bit (0 -> 127)
  9164. $alpha = (((127 - $alpha) / 127) * 255);
  9165. // correct gamma
  9166. $this->gdgammacache["'" . $c . "'"] = (pow(($alpha / 255), 2.2) * 255);
  9167. // store the latest values on cache to improve performances
  9168. if (count($this->gdgammacache) > 8) {
  9169. // remove one element from the cache array
  9170. array_shift($this->gdgammacache);
  9171. }
  9172. }
  9173. return $this->gdgammacache["'" . $c . "'"];
  9174. }
  9175. /**
  9176. * Performs a line break.
  9177. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  9178. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  9179. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  9180. * @public
  9181. * @since 1.0
  9182. * @see Cell()
  9183. */
  9184. public function Ln($h = '', $cell = false) {
  9185. if (($this->num_columns > 1) AND ($this->y == $this->columns[$this->current_column]['y']) AND isset($this->columns[$this->current_column]['x']) AND ($this->x == $this->columns[$this->current_column]['x'])) {
  9186. // revove vertical space from the top of the column
  9187. return;
  9188. }
  9189. if ($cell) {
  9190. if ($this->rtl) {
  9191. $cellpadding = $this->cell_padding['R'];
  9192. } else {
  9193. $cellpadding = $this->cell_padding['L'];
  9194. }
  9195. } else {
  9196. $cellpadding = 0;
  9197. }
  9198. if ($this->rtl) {
  9199. $this->x = $this->w - $this->rMargin - $cellpadding;
  9200. } else {
  9201. $this->x = $this->lMargin + $cellpadding;
  9202. }
  9203. if (is_string($h)) {
  9204. $this->y += $this->lasth;
  9205. } else {
  9206. $this->y += $h;
  9207. }
  9208. $this->newline = true;
  9209. }
  9210. /**
  9211. * Returns the relative X value of current position.
  9212. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  9213. * @return float
  9214. * @public
  9215. * @since 1.2
  9216. * @see SetX(), GetY(), SetY()
  9217. */
  9218. public function GetX() {
  9219. //Get x position
  9220. if ($this->rtl) {
  9221. return ($this->w - $this->x);
  9222. } else {
  9223. return $this->x;
  9224. }
  9225. }
  9226. /**
  9227. * Returns the absolute X value of current position.
  9228. * @return float
  9229. * @public
  9230. * @since 1.2
  9231. * @see SetX(), GetY(), SetY()
  9232. */
  9233. public function GetAbsX() {
  9234. return $this->x;
  9235. }
  9236. /**
  9237. * Returns the ordinate of the current position.
  9238. * @return float
  9239. * @public
  9240. * @since 1.0
  9241. * @see SetY(), GetX(), SetX()
  9242. */
  9243. public function GetY() {
  9244. return $this->y;
  9245. }
  9246. /**
  9247. * Defines the abscissa of the current position.
  9248. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  9249. * @param $x (float) The value of the abscissa in user units.
  9250. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  9251. * @public
  9252. * @since 1.2
  9253. * @see GetX(), GetY(), SetY(), SetXY()
  9254. */
  9255. public function SetX($x, $rtloff = false) {
  9256. $x = floatval($x);
  9257. if (!$rtloff AND $this->rtl) {
  9258. if ($x >= 0) {
  9259. $this->x = $this->w - $x;
  9260. } else {
  9261. $this->x = abs($x);
  9262. }
  9263. } else {
  9264. if ($x >= 0) {
  9265. $this->x = $x;
  9266. } else {
  9267. $this->x = $this->w + $x;
  9268. }
  9269. }
  9270. if ($this->x < 0) {
  9271. $this->x = 0;
  9272. }
  9273. if ($this->x > $this->w) {
  9274. $this->x = $this->w;
  9275. }
  9276. }
  9277. /**
  9278. * Moves the current abscissa back to the left margin and sets the ordinate.
  9279. * If the passed value is negative, it is relative to the bottom of the page.
  9280. * @param $y (float) The value of the ordinate in user units.
  9281. * @param $resetx (bool) if true (default) reset the X position.
  9282. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  9283. * @public
  9284. * @since 1.0
  9285. * @see GetX(), GetY(), SetY(), SetXY()
  9286. */
  9287. public function SetY($y, $resetx = true, $rtloff = false) {
  9288. $y = floatval($y);
  9289. if ($resetx) {
  9290. //reset x
  9291. if (!$rtloff AND $this->rtl) {
  9292. $this->x = $this->w - $this->rMargin;
  9293. } else {
  9294. $this->x = $this->lMargin;
  9295. }
  9296. }
  9297. if ($y >= 0) {
  9298. $this->y = $y;
  9299. } else {
  9300. $this->y = $this->h + $y;
  9301. }
  9302. if ($this->y < 0) {
  9303. $this->y = 0;
  9304. }
  9305. if ($this->y > $this->h) {
  9306. $this->y = $this->h;
  9307. }
  9308. }
  9309. /**
  9310. * Defines the abscissa and ordinate of the current position.
  9311. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  9312. * @param $x (float) The value of the abscissa.
  9313. * @param $y (float) The value of the ordinate.
  9314. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  9315. * @public
  9316. * @since 1.2
  9317. * @see SetX(), SetY()
  9318. */
  9319. public function SetXY($x, $y, $rtloff = false) {
  9320. $this->SetY($y, false, $rtloff);
  9321. $this->SetX($x, $rtloff);
  9322. }
  9323. /**
  9324. * Set the absolute X coordinate of the current pointer.
  9325. * @param $x (float) The value of the abscissa in user units.
  9326. * @public
  9327. * @since 5.9.186 (2012-09-13)
  9328. * @see setAbsX(), setAbsY(), SetAbsXY()
  9329. */
  9330. public function SetAbsX($x) {
  9331. $this->x = floatval($x);
  9332. }
  9333. /**
  9334. * Set the absolute Y coordinate of the current pointer.
  9335. * @param $y (float) (float) The value of the ordinate in user units.
  9336. * @public
  9337. * @since 5.9.186 (2012-09-13)
  9338. * @see setAbsX(), setAbsY(), SetAbsXY()
  9339. */
  9340. public function SetAbsY($y) {
  9341. $this->y = floatval($y);
  9342. }
  9343. /**
  9344. * Set the absolute X and Y coordinates of the current pointer.
  9345. * @param $x (float) The value of the abscissa in user units.
  9346. * @param $y (float) (float) The value of the ordinate in user units.
  9347. * @public
  9348. * @since 5.9.186 (2012-09-13)
  9349. * @see setAbsX(), setAbsY(), SetAbsXY()
  9350. */
  9351. public function SetAbsXY($x, $y) {
  9352. $this->SetAbsX($x);
  9353. $this->SetAbsY($y);
  9354. }
  9355. /**
  9356. * Ouput input data and compress it if possible.
  9357. * @param $data (string) Data to output.
  9358. * @param $length (int) Data length in bytes.
  9359. * @protected
  9360. * @since 5.9.086
  9361. */
  9362. protected function sendOutputData($data, $length) {
  9363. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  9364. // the content length may vary if the server is using compression
  9365. header('Content-Length: ' . $length);
  9366. }
  9367. echo $data;
  9368. }
  9369. /**
  9370. * Send the document to a given destination: string, local file or browser.
  9371. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  9372. * The method first calls Close() if necessary to terminate the document.
  9373. * @param $name (string) The name of the file when saved. Note that special characters are removed and blanks characters are replaced with the underscore character.
  9374. * @param $dest (string) 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 server file with the name given by name.</li><li>S: return the document as a string (name is ignored).</li><li>FI: equivalent to F + I option</li><li>FD: equivalent to F + D option</li><li>E: return the document as base64 mime multi-part email attachment (RFC 2045)</li></ul>
  9375. * @public
  9376. * @since 1.0
  9377. * @see Close()
  9378. */
  9379. public function Output($name = 'doc.pdf', $dest = 'I') {
  9380. //Output PDF to some destination
  9381. //Finish document if necessary
  9382. if ($this->state < 3) {
  9383. $this->Close();
  9384. }
  9385. //Normalize parameters
  9386. if (is_bool($dest)) {
  9387. $dest = $dest ? 'D' : 'F';
  9388. }
  9389. $dest = strtoupper($dest);
  9390. if ($dest{0} != 'F') {
  9391. $name = preg_replace('/[\s]+/', '_', $name);
  9392. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  9393. }
  9394. if ($this->sign) {
  9395. // *** apply digital signature to the document ***
  9396. // get the document content
  9397. $pdfdoc = $this->getBuffer();
  9398. // remove last newline
  9399. $pdfdoc = substr($pdfdoc, 0, -1);
  9400. // Remove the original buffer
  9401. if (isset($this->diskcache) AND $this->diskcache) {
  9402. // remove buffer file from cache
  9403. unlink($this->buffer);
  9404. }
  9405. unset($this->buffer);
  9406. // remove filler space
  9407. $byterange_string_len = strlen($this->byterange_string);
  9408. // define the ByteRange
  9409. $byte_range = array();
  9410. $byte_range[0] = 0;
  9411. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  9412. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  9413. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  9414. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]) . substr($pdfdoc, $byte_range[2]);
  9415. // replace the ByteRange
  9416. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  9417. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  9418. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  9419. // write the document to a temporary folder
  9420. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  9421. $f = fopen($tempdoc, 'wb');
  9422. if (!$f) {
  9423. $this->Error('Unable to create temporary file: ' . $tempdoc);
  9424. }
  9425. $pdfdoc_length = strlen($pdfdoc);
  9426. fwrite($f, $pdfdoc, $pdfdoc_length);
  9427. fclose($f);
  9428. // get digital signature via openssl library
  9429. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  9430. if (empty($this->signature_data['extracerts'])) {
  9431. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  9432. } else {
  9433. 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']);
  9434. }
  9435. unlink($tempdoc);
  9436. // read signature
  9437. $signature = file_get_contents($tempsign);
  9438. unlink($tempsign);
  9439. // extract signature
  9440. $signature = substr($signature, $pdfdoc_length);
  9441. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  9442. $tmparr = explode("\n\n", $signature);
  9443. $signature = $tmparr[1];
  9444. unset($tmparr);
  9445. // decode signature
  9446. $signature = base64_decode(trim($signature));
  9447. // convert signature to hex
  9448. $signature = current(unpack('H*', $signature));
  9449. $signature = str_pad($signature, $this->signature_max_length, '0');
  9450. // disable disk caching
  9451. $this->diskcache = false;
  9452. // Add signature to the document
  9453. $this->buffer = substr($pdfdoc, 0, $byte_range[1]) . '<' . $signature . '>' . substr($pdfdoc, $byte_range[1]);
  9454. $this->bufferlen = strlen($this->buffer);
  9455. }
  9456. switch ($dest) {
  9457. case 'I': {
  9458. // Send PDF to the standard output
  9459. if (ob_get_contents()) {
  9460. $this->Error('Some data has already been output, can\'t send PDF file');
  9461. }
  9462. if (php_sapi_name() != 'cli') {
  9463. // send output to a browser
  9464. header('Content-Type: application/pdf');
  9465. if (headers_sent()) {
  9466. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  9467. }
  9468. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  9469. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  9470. header('Pragma: public');
  9471. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  9472. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  9473. header('Content-Disposition: inline; filename="' . basename($name) . '"');
  9474. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  9475. } else {
  9476. echo $this->getBuffer();
  9477. }
  9478. break;
  9479. }
  9480. case 'D': {
  9481. // download PDF as file
  9482. if (ob_get_contents()) {
  9483. $this->Error('Some data has already been output, can\'t send PDF file');
  9484. }
  9485. header('Content-Description: File Transfer');
  9486. if (headers_sent()) {
  9487. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  9488. }
  9489. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  9490. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  9491. header('Pragma: public');
  9492. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  9493. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  9494. // force download dialog
  9495. if (strpos(php_sapi_name(), 'cgi') === false) {
  9496. header('Content-Type: application/force-download');
  9497. header('Content-Type: application/octet-stream', false);
  9498. header('Content-Type: application/download', false);
  9499. header('Content-Type: application/pdf', false);
  9500. } else {
  9501. header('Content-Type: application/pdf');
  9502. }
  9503. // use the Content-Disposition header to supply a recommended filename
  9504. header('Content-Disposition: attachment; filename="' . basename($name) . '"');
  9505. header('Content-Transfer-Encoding: binary');
  9506. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  9507. break;
  9508. }
  9509. case 'F':
  9510. case 'FI':
  9511. case 'FD': {
  9512. // save PDF to a local file
  9513. if ($this->diskcache) {
  9514. copy($this->buffer, $name);
  9515. } else {
  9516. $f = fopen($name, 'wb');
  9517. if (!$f) {
  9518. $this->Error('Unable to create output file: ' . $name);
  9519. }
  9520. fwrite($f, $this->getBuffer(), $this->bufferlen);
  9521. fclose($f);
  9522. }
  9523. if ($dest == 'FI') {
  9524. // send headers to browser
  9525. header('Content-Type: application/pdf');
  9526. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  9527. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  9528. header('Pragma: public');
  9529. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  9530. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  9531. header('Content-Disposition: inline; filename="' . basename($name) . '"');
  9532. $this->sendOutputData(file_get_contents($name), filesize($name));
  9533. } elseif ($dest == 'FD') {
  9534. // send headers to browser
  9535. if (ob_get_contents()) {
  9536. $this->Error('Some data has already been output, can\'t send PDF file');
  9537. }
  9538. header('Content-Description: File Transfer');
  9539. if (headers_sent()) {
  9540. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  9541. }
  9542. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  9543. header('Pragma: public');
  9544. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  9545. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  9546. // force download dialog
  9547. if (strpos(php_sapi_name(), 'cgi') === false) {
  9548. header('Content-Type: application/force-download');
  9549. header('Content-Type: application/octet-stream', false);
  9550. header('Content-Type: application/download', false);
  9551. header('Content-Type: application/pdf', false);
  9552. } else {
  9553. header('Content-Type: application/pdf');
  9554. }
  9555. // use the Content-Disposition header to supply a recommended filename
  9556. header('Content-Disposition: attachment; filename="' . basename($name) . '"');
  9557. header('Content-Transfer-Encoding: binary');
  9558. $this->sendOutputData(file_get_contents($name), filesize($name));
  9559. }
  9560. break;
  9561. }
  9562. case 'E': {
  9563. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  9564. $retval = 'Content-Type: application/pdf;' . "\r\n";
  9565. $retval .= ' name="' . $name . '"' . "\r\n";
  9566. $retval .= 'Content-Transfer-Encoding: base64' . "\r\n";
  9567. $retval .= 'Content-Disposition: attachment;' . "\r\n";
  9568. $retval .= ' filename="' . $name . '"' . "\r\n\r\n";
  9569. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  9570. return $retval;
  9571. }
  9572. case 'S': {
  9573. // returns PDF as a string
  9574. return $this->getBuffer();
  9575. }
  9576. default: {
  9577. $this->Error('Incorrect output destination: ' . $dest);
  9578. }
  9579. }
  9580. return '';
  9581. }
  9582. /**
  9583. * Unset all class variables except the following critical variables.
  9584. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  9585. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  9586. * @public
  9587. * @since 4.5.016 (2009-02-24)
  9588. */
  9589. public function _destroy($destroyall = false, $preserve_objcopy = false) {
  9590. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  9591. // remove buffer file from cache
  9592. unlink($this->buffer);
  9593. }
  9594. if ($destroyall AND isset($this->cached_files) AND !empty($this->cached_files)) {
  9595. // remove cached files
  9596. foreach ($this->cached_files as $cachefile) {
  9597. if (is_file($cachefile)) {
  9598. unlink($cachefile);
  9599. }
  9600. }
  9601. unset($this->cached_files);
  9602. }
  9603. foreach (array_keys(get_object_vars($this)) as $val) {
  9604. if ($destroyall OR (
  9605. ($val != 'internal_encoding')
  9606. AND ($val != 'state')
  9607. AND ($val != 'bufferlen')
  9608. AND ($val != 'buffer')
  9609. AND ($val != 'diskcache')
  9610. AND ($val != 'cached_files')
  9611. AND ($val != 'sign')
  9612. AND ($val != 'signature_data')
  9613. AND ($val != 'signature_max_length')
  9614. AND ($val != 'byterange_string')
  9615. )) {
  9616. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  9617. unset($this->$val);
  9618. }
  9619. }
  9620. }
  9621. }
  9622. /**
  9623. * Check for locale-related bug
  9624. * @protected
  9625. */
  9626. protected function _dochecks() {
  9627. //Check for locale-related bug
  9628. if (1.1 == 1) {
  9629. $this->Error('Don\'t alter the locale before including class file');
  9630. }
  9631. //Check for decimal separator
  9632. if (sprintf('%.1F', 1.0) != '1.0') {
  9633. setlocale(LC_NUMERIC, 'C');
  9634. }
  9635. }
  9636. /**
  9637. * Return fonts path
  9638. * @return string
  9639. * @protected
  9640. */
  9641. protected function _getfontpath() {
  9642. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__) . '/fonts')) {
  9643. define('K_PATH_FONTS', dirname(__FILE__) . '/fonts/');
  9644. }
  9645. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  9646. }
  9647. /**
  9648. * Return an array containing variations for the basic page number alias.
  9649. * @param $a (string) Base alias.
  9650. * @return array of page number aliases
  9651. * @protected
  9652. */
  9653. protected function getInternalPageNumberAliases($a = '') {
  9654. $alias = array();
  9655. // build array of Unicode + ASCII variants (the order is important)
  9656. $alias = array('u' => array(), 'a' => array());
  9657. $u = '{' . $a . '}';
  9658. $alias['u'][] = $this->_escape($u);
  9659. if ($this->isunicode) {
  9660. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  9661. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  9662. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  9663. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  9664. }
  9665. $alias['a'][] = $this->_escape($a);
  9666. return $alias;
  9667. }
  9668. /**
  9669. * Return an array containing all internal page aliases.
  9670. * @return array of page number aliases
  9671. * @protected
  9672. */
  9673. protected function getAllInternalPageNumberAliases() {
  9674. $basic_alias = array($this->alias_tot_pages, $this->alias_num_page, $this->alias_group_tot_pages, $this->alias_group_num_page, $this->alias_right_shift);
  9675. $pnalias = array();
  9676. foreach ($basic_alias as $k => $a) {
  9677. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  9678. }
  9679. return $pnalias;
  9680. }
  9681. /**
  9682. * Replace page number aliases with number.
  9683. * @param $page (string) Page content.
  9684. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  9685. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  9686. * @return replaced page content and updated $diff parameter as array.
  9687. * @protected
  9688. */
  9689. protected function replacePageNumAliases($page, $replace, $diff = 0) {
  9690. foreach ($replace as $rep) {
  9691. foreach ($rep[3] as $a) {
  9692. if (strpos($page, $a) !== false) {
  9693. $page = str_replace($a, $rep[0], $page);
  9694. $diff += ($rep[2] - $rep[1]);
  9695. }
  9696. }
  9697. }
  9698. return array($page, $diff);
  9699. }
  9700. /**
  9701. * Replace right shift page number aliases with spaces to correct right alignment.
  9702. * This works perfectly only when using monospaced fonts.
  9703. * @param $page (string) Page content.
  9704. * @param $aliases (array) Array of page aliases.
  9705. * @param $diff (int) initial difference to add.
  9706. * @return replaced page content.
  9707. * @protected
  9708. */
  9709. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  9710. foreach ($aliases as $type => $alias) {
  9711. foreach ($alias as $a) {
  9712. // find position of compensation factor
  9713. $startnum = (strpos($a, ':') + 1);
  9714. $a = substr($a, 0, $startnum);
  9715. if (($pos = strpos($page, $a)) !== false) {
  9716. // end of alias
  9717. $endnum = strpos($page, '}', $pos);
  9718. // string to be replaced
  9719. $aa = substr($page, $pos, ($endnum - $pos + 1));
  9720. // get compensation factor
  9721. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  9722. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  9723. $ratio = floatval($ratio);
  9724. if ($type == 'u') {
  9725. $chrdiff = floor(($diff + 12) * $ratio);
  9726. $shift = str_repeat(' ', $chrdiff);
  9727. $shift = $this->UTF8ToUTF16BE($shift, false);
  9728. } else {
  9729. $chrdiff = floor(($diff + 11) * $ratio);
  9730. $shift = str_repeat(' ', $chrdiff);
  9731. }
  9732. $page = str_replace($aa, $shift, $page);
  9733. }
  9734. }
  9735. }
  9736. return $page;
  9737. }
  9738. /**
  9739. * Set page boxes to be included on page descriptions.
  9740. * @param $boxes (array) Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  9741. * @protected
  9742. */
  9743. protected function setPageBoxTypes($boxes) {
  9744. $validboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  9745. $this->page_boxes = array();
  9746. foreach ($boxes as $box) {
  9747. if (in_array($box, $validboxes)) {
  9748. $this->page_boxes[] = $box;
  9749. }
  9750. }
  9751. }
  9752. /**
  9753. * Output pages (and replace page number aliases).
  9754. * @protected
  9755. */
  9756. protected function _putpages() {
  9757. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  9758. // get internal aliases for page numbers
  9759. $pnalias = $this->getAllInternalPageNumberAliases();
  9760. $num_pages = $this->numpages;
  9761. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  9762. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  9763. $ptp_num_chars = $this->GetNumChars($ptpa);
  9764. $pagegroupnum = 0;
  9765. $groupnum = 0;
  9766. $ptgu = 1;
  9767. $ptga = 1;
  9768. for ($n = 1; $n <= $num_pages; ++$n) {
  9769. // get current page
  9770. $temppage = $this->getPageBuffer($n);
  9771. $pagelen = strlen($temppage);
  9772. // set replacements for total pages number
  9773. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  9774. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  9775. $pnp_num_chars = $this->GetNumChars($pnpa);
  9776. $pdiff = 0; // difference used for right shift alignment of page numbers
  9777. $gdiff = 0; // difference used for right shift alignment of page group numbers
  9778. if (!empty($this->pagegroups)) {
  9779. if (isset($this->newpagegroup[$n])) {
  9780. $pagegroupnum = 0;
  9781. ++$groupnum;
  9782. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  9783. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  9784. $ptg_num_chars = $this->GetNumChars($ptga);
  9785. }
  9786. ++$pagegroupnum;
  9787. $pnga = $this->formatPageNumber($pagegroupnum);
  9788. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  9789. $png_num_chars = $this->GetNumChars($pnga);
  9790. // replace page numbers
  9791. $replace = array();
  9792. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  9793. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  9794. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  9795. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  9796. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  9797. }
  9798. // replace page numbers
  9799. $replace = array();
  9800. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  9801. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  9802. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  9803. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  9804. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  9805. // replace right shift alias
  9806. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  9807. // replace EPS marker
  9808. $temppage = str_replace($this->epsmarker, '', $temppage);
  9809. //Page
  9810. $this->page_obj_id[$n] = $this->_newobj();
  9811. $out = '<<';
  9812. $out .= ' /Type /Page';
  9813. $out .= ' /Parent 1 0 R';
  9814. $out .= ' /LastModified ' . $this->_datestring(0, $this->doc_modification_timestamp);
  9815. $out .= ' /Resources 2 0 R';
  9816. foreach ($this->page_boxes as $box) {
  9817. $out .= ' /' . $box;
  9818. $out .= sprintf(' [%F %F %F %F]', $this->pagedim[$n][$box]['llx'], $this->pagedim[$n][$box]['lly'], $this->pagedim[$n][$box]['urx'], $this->pagedim[$n][$box]['ury']);
  9819. }
  9820. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  9821. $out .= ' /BoxColorInfo <<';
  9822. foreach ($this->page_boxes as $box) {
  9823. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  9824. $out .= ' /' . $box . ' <<';
  9825. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  9826. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  9827. $out .= ' /C [';
  9828. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  9829. $out .= ' ]';
  9830. }
  9831. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  9832. $out .= ' /W ' . ($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  9833. }
  9834. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  9835. $out .= ' /S /' . $this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  9836. }
  9837. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  9838. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  9839. $out .= ' /D [';
  9840. foreach ($dashes as $dash) {
  9841. $out .= sprintf(' %F', ($dash * $this->k));
  9842. }
  9843. $out .= ' ]';
  9844. }
  9845. $out .= ' >>';
  9846. }
  9847. }
  9848. $out .= ' >>';
  9849. }
  9850. $out .= ' /Contents ' . ($this->n + 1) . ' 0 R';
  9851. $out .= ' /Rotate ' . $this->pagedim[$n]['Rotate'];
  9852. if (!$this->pdfa_mode) {
  9853. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  9854. }
  9855. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  9856. // page transitions
  9857. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  9858. $out .= ' /Dur ' . $this->pagedim[$n]['trans']['Dur'];
  9859. }
  9860. $out .= ' /Trans <<';
  9861. $out .= ' /Type /Trans';
  9862. if (isset($this->pagedim[$n]['trans']['S'])) {
  9863. $out .= ' /S /' . $this->pagedim[$n]['trans']['S'];
  9864. }
  9865. if (isset($this->pagedim[$n]['trans']['D'])) {
  9866. $out .= ' /D ' . $this->pagedim[$n]['trans']['D'];
  9867. }
  9868. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  9869. $out .= ' /Dm /' . $this->pagedim[$n]['trans']['Dm'];
  9870. }
  9871. if (isset($this->pagedim[$n]['trans']['M'])) {
  9872. $out .= ' /M /' . $this->pagedim[$n]['trans']['M'];
  9873. }
  9874. if (isset($this->pagedim[$n]['trans']['Di'])) {
  9875. $out .= ' /Di ' . $this->pagedim[$n]['trans']['Di'];
  9876. }
  9877. if (isset($this->pagedim[$n]['trans']['SS'])) {
  9878. $out .= ' /SS ' . $this->pagedim[$n]['trans']['SS'];
  9879. }
  9880. if (isset($this->pagedim[$n]['trans']['B'])) {
  9881. $out .= ' /B ' . $this->pagedim[$n]['trans']['B'];
  9882. }
  9883. $out .= ' >>';
  9884. }
  9885. $out .= $this->_getannotsrefs($n);
  9886. $out .= ' /PZ ' . $this->pagedim[$n]['PZ'];
  9887. $out .= ' >>';
  9888. $out .= "\n" . 'endobj';
  9889. $this->_out($out);
  9890. //Page content
  9891. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  9892. $this->_newobj();
  9893. $p = $this->_getrawstream($p);
  9894. $this->_out('<<' . $filter . '/Length ' . strlen($p) . '>> stream' . "\n" . $p . "\n" . 'endstream' . "\n" . 'endobj');
  9895. if ($this->diskcache) {
  9896. // remove temporary files
  9897. unlink($this->pages[$n]);
  9898. }
  9899. }
  9900. //Pages root
  9901. $out = $this->_getobj(1) . "\n";
  9902. $out .= '<< /Type /Pages /Kids [';
  9903. foreach ($this->page_obj_id as $page_obj) {
  9904. $out .= ' ' . $page_obj . ' 0 R';
  9905. }
  9906. $out .= ' ] /Count ' . $num_pages . ' >>';
  9907. $out .= "\n" . 'endobj';
  9908. $this->_out($out);
  9909. }
  9910. /**
  9911. * Output references to page annotations
  9912. * @param $n (int) page number
  9913. * @protected
  9914. * @author Nicola Asuni
  9915. * @since 4.7.000 (2008-08-29)
  9916. * @deprecated
  9917. */
  9918. protected function _putannotsrefs($n) {
  9919. $this->_out($this->_getannotsrefs($n));
  9920. }
  9921. /**
  9922. * Get references to page annotations.
  9923. * @param $n (int) page number
  9924. * @return string
  9925. * @protected
  9926. * @author Nicola Asuni
  9927. * @since 5.0.010 (2010-05-17)
  9928. */
  9929. protected function _getannotsrefs($n) {
  9930. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  9931. return '';
  9932. }
  9933. $out = ' /Annots [';
  9934. if (isset($this->PageAnnots[$n])) {
  9935. foreach ($this->PageAnnots[$n] as $key => $val) {
  9936. if (!in_array($val['n'], $this->radio_groups)) {
  9937. $out .= ' ' . $val['n'] . ' 0 R';
  9938. }
  9939. }
  9940. // add radiobutton groups
  9941. if (isset($this->radiobutton_groups[$n])) {
  9942. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  9943. if (isset($data['n'])) {
  9944. $out .= ' ' . $data['n'] . ' 0 R';
  9945. }
  9946. }
  9947. }
  9948. }
  9949. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  9950. // set reference for signature object
  9951. $out .= ' ' . $this->sig_obj_id . ' 0 R';
  9952. }
  9953. if (!empty($this->empty_signature_appearance)) {
  9954. foreach ($this->empty_signature_appearance as $esa) {
  9955. if ($esa['page'] == $n) {
  9956. // set reference for empty signature objects
  9957. $out .= ' ' . $esa['objid'] . ' 0 R';
  9958. }
  9959. }
  9960. }
  9961. $out .= ' ]';
  9962. return $out;
  9963. }
  9964. /**
  9965. * Output annotations objects for all pages.
  9966. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  9967. * See section 12.5 of PDF 32000_2008 reference.
  9968. * @protected
  9969. * @author Nicola Asuni
  9970. * @since 4.0.018 (2008-08-06)
  9971. */
  9972. protected function _putannotsobjs() {
  9973. // reset object counter
  9974. for ($n = 1; $n <= $this->numpages; ++$n) {
  9975. if (isset($this->PageAnnots[$n])) {
  9976. // set page annotations
  9977. foreach ($this->PageAnnots[$n] as $key => $pl) {
  9978. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  9979. // create annotation object for grouping radiobuttons
  9980. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  9981. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  9982. $annots = '<<';
  9983. $annots .= ' /Type /Annot';
  9984. $annots .= ' /Subtype /Widget';
  9985. $annots .= ' /Rect [0 0 0 0]';
  9986. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  9987. // read only
  9988. $annots .= ' /F 68';
  9989. $annots .= ' /Ff 49153';
  9990. } else {
  9991. $annots .= ' /F 4'; // default print for PDF/A
  9992. $annots .= ' /Ff 49152';
  9993. }
  9994. $annots .= ' /T ' . $this->_datastring($pl['txt'], $radio_button_obj_id);
  9995. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9996. $annots .= ' /TU ' . $this->_datastring($pl['opt']['tu'], $radio_button_obj_id);
  9997. }
  9998. $annots .= ' /FT /Btn';
  9999. $annots .= ' /Kids [';
  10000. $defval = '';
  10001. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  10002. if (isset($data['kid'])) {
  10003. $annots .= ' ' . $data['kid'] . ' 0 R';
  10004. if ($data['def'] !== 'Off') {
  10005. $defval = $data['def'];
  10006. }
  10007. }
  10008. }
  10009. $annots .= ' ]';
  10010. if (!empty($defval)) {
  10011. $annots .= ' /V /' . $defval;
  10012. }
  10013. $annots .= ' >>';
  10014. $this->_out($this->_getobj($radio_button_obj_id) . "\n" . $annots . "\n" . 'endobj');
  10015. $this->form_obj_id[] = $radio_button_obj_id;
  10016. // store object id to be used on Parent entry of Kids
  10017. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  10018. }
  10019. $formfield = false;
  10020. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  10021. $a = $pl['x'] * $this->k;
  10022. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  10023. $c = $pl['w'] * $this->k;
  10024. $d = $pl['h'] * $this->k;
  10025. $rect = sprintf('%F %F %F %F', $a, $b, $a + $c, $b + $d);
  10026. // create new annotation object
  10027. $annots = '<</Type /Annot';
  10028. $annots .= ' /Subtype /' . $pl['opt']['subtype'];
  10029. $annots .= ' /Rect [' . $rect . ']';
  10030. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  10031. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  10032. $annots .= ' /FT /' . $pl['opt']['ft'];
  10033. $formfield = true;
  10034. }
  10035. $annots .= ' /Contents ' . $this->_textstring($pl['txt'], $annot_obj_id);
  10036. $annots .= ' /P ' . $this->page_obj_id[$n] . ' 0 R';
  10037. $annots .= ' /NM ' . $this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  10038. $annots .= ' /M ' . $this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  10039. if (isset($pl['opt']['f'])) {
  10040. $fval = 0;
  10041. if (is_array($pl['opt']['f'])) {
  10042. foreach ($pl['opt']['f'] as $f) {
  10043. switch (strtolower($f)) {
  10044. case 'invisible': {
  10045. $fval += 1 << 0;
  10046. break;
  10047. }
  10048. case 'hidden': {
  10049. $fval += 1 << 1;
  10050. break;
  10051. }
  10052. case 'print': {
  10053. $fval += 1 << 2;
  10054. break;
  10055. }
  10056. case 'nozoom': {
  10057. $fval += 1 << 3;
  10058. break;
  10059. }
  10060. case 'norotate': {
  10061. $fval += 1 << 4;
  10062. break;
  10063. }
  10064. case 'noview': {
  10065. $fval += 1 << 5;
  10066. break;
  10067. }
  10068. case 'readonly': {
  10069. $fval += 1 << 6;
  10070. break;
  10071. }
  10072. case 'locked': {
  10073. $fval += 1 << 8;
  10074. break;
  10075. }
  10076. case 'togglenoview': {
  10077. $fval += 1 << 9;
  10078. break;
  10079. }
  10080. case 'lockedcontents': {
  10081. $fval += 1 << 10;
  10082. break;
  10083. }
  10084. default: {
  10085. break;
  10086. }
  10087. }
  10088. }
  10089. } else {
  10090. $fval = intval($pl['opt']['f']);
  10091. }
  10092. } else {
  10093. $fval = 4;
  10094. }
  10095. if ($this->pdfa_mode) {
  10096. // force print flag for PDF/A mode
  10097. $fval |= 4;
  10098. }
  10099. $annots .= ' /F ' . intval($fval);
  10100. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  10101. $annots .= ' /AS /' . $pl['opt']['as'];
  10102. }
  10103. if (isset($pl['opt']['ap'])) {
  10104. // appearance stream
  10105. $annots .= ' /AP <<';
  10106. if (is_array($pl['opt']['ap'])) {
  10107. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  10108. // $apmode can be: n = normal; r = rollover; d = down;
  10109. $annots .= ' /' . strtoupper($apmode);
  10110. if (is_array($apdef)) {
  10111. $annots .= ' <<';
  10112. foreach ($apdef as $apstate => $stream) {
  10113. // reference to XObject that define the appearance for this mode-state
  10114. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  10115. $annots .= ' /' . $apstate . ' ' . $apsobjid . ' 0 R';
  10116. }
  10117. $annots .= ' >>';
  10118. } else {
  10119. // reference to XObject that define the appearance for this mode
  10120. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  10121. $annots .= ' ' . $apsobjid . ' 0 R';
  10122. }
  10123. }
  10124. } else {
  10125. $annots .= $pl['opt']['ap'];
  10126. }
  10127. $annots .= ' >>';
  10128. }
  10129. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  10130. $annots .= ' /BS <<';
  10131. $annots .= ' /Type /Border';
  10132. if (isset($pl['opt']['bs']['w'])) {
  10133. $annots .= ' /W ' . intval($pl['opt']['bs']['w']);
  10134. }
  10135. $bstyles = array('S', 'D', 'B', 'I', 'U');
  10136. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  10137. $annots .= ' /S /' . $pl['opt']['bs']['s'];
  10138. }
  10139. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  10140. $annots .= ' /D [';
  10141. foreach ($pl['opt']['bs']['d'] as $cord) {
  10142. $annots .= ' ' . intval($cord);
  10143. }
  10144. $annots .= ']';
  10145. }
  10146. $annots .= ' >>';
  10147. } else {
  10148. $annots .= ' /Border [';
  10149. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  10150. $annots .= intval($pl['opt']['border'][0]) . ' ';
  10151. $annots .= intval($pl['opt']['border'][1]) . ' ';
  10152. $annots .= intval($pl['opt']['border'][2]);
  10153. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  10154. $annots .= ' [';
  10155. foreach ($pl['opt']['border'][3] as $dash) {
  10156. $annots .= intval($dash) . ' ';
  10157. }
  10158. $annots .= ']';
  10159. }
  10160. } else {
  10161. $annots .= '0 0 0';
  10162. }
  10163. $annots .= ']';
  10164. }
  10165. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  10166. $annots .= ' /BE <<';
  10167. $bstyles = array('S', 'C');
  10168. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $bstyles)) {
  10169. $annots .= ' /S /' . $pl['opt']['bs']['s'];
  10170. } else {
  10171. $annots .= ' /S /S';
  10172. }
  10173. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  10174. $annots .= ' /I ' . sprintf(' %F', $pl['opt']['be']['i']);
  10175. }
  10176. $annots .= '>>';
  10177. }
  10178. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  10179. $annots .= ' /C ' . $this->getColorStringFromArray($pl['opt']['c']);
  10180. }
  10181. //$annots .= ' /StructParent ';
  10182. //$annots .= ' /OC ';
  10183. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  10184. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  10185. // this is a markup type
  10186. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  10187. $annots .= ' /T ' . $this->_textstring($pl['opt']['t'], $annot_obj_id);
  10188. }
  10189. //$annots .= ' /Popup ';
  10190. if (isset($pl['opt']['ca'])) {
  10191. $annots .= ' /CA ' . sprintf('%F', floatval($pl['opt']['ca']));
  10192. }
  10193. if (isset($pl['opt']['rc'])) {
  10194. $annots .= ' /RC ' . $this->_textstring($pl['opt']['rc'], $annot_obj_id);
  10195. }
  10196. $annots .= ' /CreationDate ' . $this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  10197. //$annots .= ' /IRT ';
  10198. if (isset($pl['opt']['subj'])) {
  10199. $annots .= ' /Subj ' . $this->_textstring($pl['opt']['subj'], $annot_obj_id);
  10200. }
  10201. //$annots .= ' /RT ';
  10202. //$annots .= ' /IT ';
  10203. //$annots .= ' /ExData ';
  10204. }
  10205. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  10206. // Annotation types
  10207. switch (strtolower($pl['opt']['subtype'])) {
  10208. case 'text': {
  10209. if (isset($pl['opt']['open'])) {
  10210. $annots .= ' /Open ' . (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  10211. }
  10212. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  10213. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  10214. $annots .= ' /Name /' . $pl['opt']['name'];
  10215. } else {
  10216. $annots .= ' /Name /Note';
  10217. }
  10218. $statemodels = array('Marked', 'Review');
  10219. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  10220. $annots .= ' /StateModel /' . $pl['opt']['statemodel'];
  10221. } else {
  10222. $pl['opt']['statemodel'] = 'Marked';
  10223. $annots .= ' /StateModel /' . $pl['opt']['statemodel'];
  10224. }
  10225. if ($pl['opt']['statemodel'] == 'Marked') {
  10226. $states = array('Accepted', 'Unmarked');
  10227. } else {
  10228. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  10229. }
  10230. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  10231. $annots .= ' /State /' . $pl['opt']['state'];
  10232. } else {
  10233. if ($pl['opt']['statemodel'] == 'Marked') {
  10234. $annots .= ' /State /Unmarked';
  10235. } else {
  10236. $annots .= ' /State /None';
  10237. }
  10238. }
  10239. break;
  10240. }
  10241. case 'link': {
  10242. if (is_string($pl['txt'])) {
  10243. if ($pl['txt'][0] == '#') {
  10244. // internal destination
  10245. $annots .= ' /Dest /' . $this->encodeNameObject(substr($pl['txt'], 1));
  10246. } else {
  10247. // external URI link
  10248. $annots .= ' /A <</S /URI /URI ' . $this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id) . '>>';
  10249. }
  10250. } else {
  10251. // internal link
  10252. if (isset($this->links[$pl['txt']])) {
  10253. $l = $this->links[$pl['txt']];
  10254. if (isset($this->page_obj_id[($l[0])])) {
  10255. $annots .= sprintf(' /Dest [%u 0 R /XYZ 0 %F null]', $this->page_obj_id[($l[0])], ($this->pagedim[$l[0]]['h'] - ($l[1] * $this->k)));
  10256. }
  10257. }
  10258. }
  10259. $hmodes = array('N', 'I', 'O', 'P');
  10260. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  10261. $annots .= ' /H /' . $pl['opt']['h'];
  10262. } else {
  10263. $annots .= ' /H /I';
  10264. }
  10265. //$annots .= ' /PA ';
  10266. //$annots .= ' /Quadpoints ';
  10267. break;
  10268. }
  10269. case 'freetext': {
  10270. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  10271. $annots .= ' /DA (' . $pl['opt']['da'] . ')';
  10272. }
  10273. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  10274. $annots .= ' /Q ' . intval($pl['opt']['q']);
  10275. }
  10276. if (isset($pl['opt']['rc'])) {
  10277. $annots .= ' /RC ' . $this->_textstring($pl['opt']['rc'], $annot_obj_id);
  10278. }
  10279. if (isset($pl['opt']['ds'])) {
  10280. $annots .= ' /DS ' . $this->_textstring($pl['opt']['ds'], $annot_obj_id);
  10281. }
  10282. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  10283. $annots .= ' /CL [';
  10284. foreach ($pl['opt']['cl'] as $cl) {
  10285. $annots .= sprintf('%F ', $cl * $this->k);
  10286. }
  10287. $annots .= ']';
  10288. }
  10289. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  10290. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  10291. $annots .= ' /IT /' . $pl['opt']['it'];
  10292. }
  10293. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  10294. $l = $pl['opt']['rd'][0] * $this->k;
  10295. $r = $pl['opt']['rd'][1] * $this->k;
  10296. $t = $pl['opt']['rd'][2] * $this->k;
  10297. $b = $pl['opt']['rd'][3] * $this->k;
  10298. $annots .= ' /RD [' . sprintf('%F %F %F %F', $l, $r, $t, $b) . ']';
  10299. }
  10300. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  10301. $annots .= ' /LE /' . $pl['opt']['le'];
  10302. }
  10303. break;
  10304. }
  10305. case 'line': {
  10306. break;
  10307. }
  10308. case 'square': {
  10309. break;
  10310. }
  10311. case 'circle': {
  10312. break;
  10313. }
  10314. case 'polygon': {
  10315. break;
  10316. }
  10317. case 'polyline': {
  10318. break;
  10319. }
  10320. case 'highlight': {
  10321. break;
  10322. }
  10323. case 'underline': {
  10324. break;
  10325. }
  10326. case 'squiggly': {
  10327. break;
  10328. }
  10329. case 'strikeout': {
  10330. break;
  10331. }
  10332. case 'stamp': {
  10333. break;
  10334. }
  10335. case 'caret': {
  10336. break;
  10337. }
  10338. case 'ink': {
  10339. break;
  10340. }
  10341. case 'popup': {
  10342. break;
  10343. }
  10344. case 'fileattachment': {
  10345. if ($this->pdfa_mode) {
  10346. // embedded files are not allowed in PDF/A mode
  10347. break;
  10348. }
  10349. if (!isset($pl['opt']['fs'])) {
  10350. break;
  10351. }
  10352. $filename = basename($pl['opt']['fs']);
  10353. if (isset($this->embeddedfiles[$filename]['n'])) {
  10354. $annots .= ' /FS <</Type /Filespec /F ' . $this->_datastring($filename, $annot_obj_id) . ' /EF <</F ' . $this->embeddedfiles[$filename]['n'] . ' 0 R>> >>';
  10355. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  10356. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  10357. $annots .= ' /Name /' . $pl['opt']['name'];
  10358. } else {
  10359. $annots .= ' /Name /PushPin';
  10360. }
  10361. }
  10362. break;
  10363. }
  10364. case 'sound': {
  10365. if (!isset($pl['opt']['fs'])) {
  10366. break;
  10367. }
  10368. $filename = basename($pl['opt']['fs']);
  10369. if (isset($this->embeddedfiles[$filename]['n'])) {
  10370. // ... TO BE COMPLETED ...
  10371. // /R /C /B /E /CO /CP
  10372. $annots .= ' /Sound <</Type /Filespec /F ' . $this->_datastring($filename, $annot_obj_id) . ' /EF <</F ' . $this->embeddedfiles[$filename]['n'] . ' 0 R>> >>';
  10373. $iconsapp = array('Speaker', 'Mic');
  10374. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  10375. $annots .= ' /Name /' . $pl['opt']['name'];
  10376. } else {
  10377. $annots .= ' /Name /Speaker';
  10378. }
  10379. }
  10380. break;
  10381. }
  10382. case 'movie': {
  10383. break;
  10384. }
  10385. case 'widget': {
  10386. $hmode = array('N', 'I', 'O', 'P', 'T');
  10387. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  10388. $annots .= ' /H /' . $pl['opt']['h'];
  10389. }
  10390. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  10391. $annots .= ' /MK <<';
  10392. if (isset($pl['opt']['mk']['r'])) {
  10393. $annots .= ' /R ' . $pl['opt']['mk']['r'];
  10394. }
  10395. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  10396. $annots .= ' /BC ' . $this->getColorStringFromArray($pl['opt']['mk']['bc']);
  10397. }
  10398. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  10399. $annots .= ' /BG ' . $this->getColorStringFromArray($pl['opt']['mk']['bg']);
  10400. }
  10401. if (isset($pl['opt']['mk']['ca'])) {
  10402. $annots .= ' /CA ' . $pl['opt']['mk']['ca'];
  10403. }
  10404. if (isset($pl['opt']['mk']['rc'])) {
  10405. $annots .= ' /RC ' . $pl['opt']['mk']['rc'];
  10406. }
  10407. if (isset($pl['opt']['mk']['ac'])) {
  10408. $annots .= ' /AC ' . $pl['opt']['mk']['ac'];
  10409. }
  10410. if (isset($pl['opt']['mk']['i'])) {
  10411. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  10412. if ($info !== false) {
  10413. $annots .= ' /I ' . $info['n'] . ' 0 R';
  10414. }
  10415. }
  10416. if (isset($pl['opt']['mk']['ri'])) {
  10417. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  10418. if ($info !== false) {
  10419. $annots .= ' /RI ' . $info['n'] . ' 0 R';
  10420. }
  10421. }
  10422. if (isset($pl['opt']['mk']['ix'])) {
  10423. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  10424. if ($info !== false) {
  10425. $annots .= ' /IX ' . $info['n'] . ' 0 R';
  10426. }
  10427. }
  10428. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  10429. $annots .= ' /IF <<';
  10430. $if_sw = array('A', 'B', 'S', 'N');
  10431. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  10432. $annots .= ' /SW /' . $pl['opt']['mk']['if']['sw'];
  10433. }
  10434. $if_s = array('A', 'P');
  10435. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  10436. $annots .= ' /S /' . $pl['opt']['mk']['if']['s'];
  10437. }
  10438. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  10439. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  10440. }
  10441. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  10442. $annots .= ' /FB true';
  10443. }
  10444. $annots .= '>>';
  10445. }
  10446. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  10447. $annots .= ' /TP ' . intval($pl['opt']['mk']['tp']);
  10448. }
  10449. $annots .= '>>';
  10450. } // end MK
  10451. // --- Entries for field dictionaries ---
  10452. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  10453. // set parent
  10454. $annots .= ' /Parent ' . $this->radiobutton_groups[$n][$pl['txt']] . ' 0 R';
  10455. }
  10456. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  10457. $annots .= ' /T ' . $this->_datastring($pl['opt']['t'], $annot_obj_id);
  10458. }
  10459. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  10460. $annots .= ' /TU ' . $this->_datastring($pl['opt']['tu'], $annot_obj_id);
  10461. }
  10462. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  10463. $annots .= ' /TM ' . $this->_datastring($pl['opt']['tm'], $annot_obj_id);
  10464. }
  10465. if (isset($pl['opt']['ff'])) {
  10466. if (is_array($pl['opt']['ff'])) {
  10467. // array of bit settings
  10468. $flag = 0;
  10469. foreach ($pl['opt']['ff'] as $val) {
  10470. $flag += 1 << ($val - 1);
  10471. }
  10472. } else {
  10473. $flag = intval($pl['opt']['ff']);
  10474. }
  10475. $annots .= ' /Ff ' . $flag;
  10476. }
  10477. if (isset($pl['opt']['maxlen'])) {
  10478. $annots .= ' /MaxLen ' . intval($pl['opt']['maxlen']);
  10479. }
  10480. if (isset($pl['opt']['v'])) {
  10481. $annots .= ' /V';
  10482. if (is_array($pl['opt']['v'])) {
  10483. foreach ($pl['opt']['v'] AS $optval) {
  10484. if (is_float($optval)) {
  10485. $optval = sprintf('%F', $optval);
  10486. }
  10487. $annots .= ' ' . $optval;
  10488. }
  10489. } else {
  10490. $annots .= ' ' . $this->_textstring($pl['opt']['v'], $annot_obj_id);
  10491. }
  10492. }
  10493. if (isset($pl['opt']['dv'])) {
  10494. $annots .= ' /DV';
  10495. if (is_array($pl['opt']['dv'])) {
  10496. foreach ($pl['opt']['dv'] AS $optval) {
  10497. if (is_float($optval)) {
  10498. $optval = sprintf('%F', $optval);
  10499. }
  10500. $annots .= ' ' . $optval;
  10501. }
  10502. } else {
  10503. $annots .= ' ' . $this->_textstring($pl['opt']['dv'], $annot_obj_id);
  10504. }
  10505. }
  10506. if (isset($pl['opt']['rv'])) {
  10507. $annots .= ' /RV';
  10508. if (is_array($pl['opt']['rv'])) {
  10509. foreach ($pl['opt']['rv'] AS $optval) {
  10510. if (is_float($optval)) {
  10511. $optval = sprintf('%F', $optval);
  10512. }
  10513. $annots .= ' ' . $optval;
  10514. }
  10515. } else {
  10516. $annots .= ' ' . $this->_textstring($pl['opt']['rv'], $annot_obj_id);
  10517. }
  10518. }
  10519. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  10520. $annots .= ' /A << ' . $pl['opt']['a'] . ' >>';
  10521. }
  10522. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  10523. $annots .= ' /AA << ' . $pl['opt']['aa'] . ' >>';
  10524. }
  10525. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  10526. $annots .= ' /DA (' . $pl['opt']['da'] . ')';
  10527. }
  10528. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  10529. $annots .= ' /Q ' . intval($pl['opt']['q']);
  10530. }
  10531. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  10532. $annots .= ' /Opt [';
  10533. foreach ($pl['opt']['opt'] AS $copt) {
  10534. if (is_array($copt)) {
  10535. $annots .= ' [' . $this->_textstring($copt[0], $annot_obj_id) . ' ' . $this->_textstring($copt[1], $annot_obj_id) . ']';
  10536. } else {
  10537. $annots .= ' ' . $this->_textstring($copt, $annot_obj_id);
  10538. }
  10539. }
  10540. $annots .= ']';
  10541. }
  10542. if (isset($pl['opt']['ti'])) {
  10543. $annots .= ' /TI ' . intval($pl['opt']['ti']);
  10544. }
  10545. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  10546. $annots .= ' /I [';
  10547. foreach ($pl['opt']['i'] AS $copt) {
  10548. $annots .= intval($copt) . ' ';
  10549. }
  10550. $annots .= ']';
  10551. }
  10552. break;
  10553. }
  10554. case 'screen': {
  10555. break;
  10556. }
  10557. case 'printermark': {
  10558. break;
  10559. }
  10560. case 'trapnet': {
  10561. break;
  10562. }
  10563. case 'watermark': {
  10564. break;
  10565. }
  10566. case '3d': {
  10567. break;
  10568. }
  10569. default: {
  10570. break;
  10571. }
  10572. }
  10573. $annots .= '>>';
  10574. // create new annotation object
  10575. $this->_out($this->_getobj($annot_obj_id) . "\n" . $annots . "\n" . 'endobj');
  10576. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  10577. // store reference of form object
  10578. $this->form_obj_id[] = $annot_obj_id;
  10579. }
  10580. }
  10581. }
  10582. } // end for each page
  10583. }
  10584. /**
  10585. * Put appearance streams XObject used to define annotation's appearance states.
  10586. * @param $w (int) annotation width
  10587. * @param $h (int) annotation height
  10588. * @param $stream (string) appearance stream
  10589. * @return int object ID
  10590. * @protected
  10591. * @since 4.8.001 (2009-09-09)
  10592. */
  10593. protected function _putAPXObject($w = 0, $h = 0, $stream = '') {
  10594. $stream = trim($stream);
  10595. $out = $this->_getobj() . "\n";
  10596. $this->xobjects['AX' . $this->n] = array('n' => $this->n);
  10597. $out .= '<<';
  10598. $out .= ' /Type /XObject';
  10599. $out .= ' /Subtype /Form';
  10600. $out .= ' /FormType 1';
  10601. if ($this->compress) {
  10602. $stream = gzcompress($stream);
  10603. $out .= ' /Filter /FlateDecode';
  10604. }
  10605. $rect = sprintf('%F %F', $w, $h);
  10606. $out .= ' /BBox [0 0 ' . $rect . ']';
  10607. $out .= ' /Matrix [1 0 0 1 0 0]';
  10608. $out .= ' /Resources 2 0 R';
  10609. $stream = $this->_getrawstream($stream);
  10610. $out .= ' /Length ' . strlen($stream);
  10611. $out .= ' >>';
  10612. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  10613. $out .= "\n" . 'endobj';
  10614. $this->_out($out);
  10615. return $this->n;
  10616. }
  10617. /**
  10618. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  10619. * @param $str (string) string from where to extract value
  10620. * @param $offset (int) point from where to read the data
  10621. * @return int 32 bit value
  10622. * @author Nicola Asuni
  10623. * @protected
  10624. * @since 5.2.000 (2010-06-02)
  10625. */
  10626. protected function _getULONG($str, $offset) {
  10627. $v = unpack('Ni', substr($str, $offset, 4));
  10628. return $v['i'];
  10629. }
  10630. /**
  10631. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  10632. * @param $str (string) string from where to extract value
  10633. * @param $offset (int) point from where to read the data
  10634. * @return int 16 bit value
  10635. * @author Nicola Asuni
  10636. * @protected
  10637. * @since 5.2.000 (2010-06-02)
  10638. */
  10639. protected function _getUSHORT($str, $offset) {
  10640. $v = unpack('ni', substr($str, $offset, 2));
  10641. return $v['i'];
  10642. }
  10643. /**
  10644. * Get SHORT from string (Big Endian 16-bit signed integer).
  10645. * @param $str (string) String from where to extract value.
  10646. * @param $offset (int) Point from where to read the data.
  10647. * @return int 16 bit value
  10648. * @author Nicola Asuni
  10649. * @protected
  10650. * @since 5.2.000 (2010-06-02)
  10651. */
  10652. protected function _getSHORT($str, $offset) {
  10653. $v = unpack('si', substr($str, $offset, 2));
  10654. return $v['i'];
  10655. }
  10656. /**
  10657. * Get FWORD from string (Big Endian 16-bit signed integer).
  10658. * @param $str (string) String from where to extract value.
  10659. * @param $offset (int) Point from where to read the data.
  10660. * @return int 16 bit value
  10661. * @author Nicola Asuni
  10662. * @protected
  10663. * @since 5.9.123 (2011-09-30)
  10664. */
  10665. protected function _getFWORD($str, $offset) {
  10666. $v = $this->_getUSHORT($str, $offset);
  10667. if ($v > 0x7fff) {
  10668. $v -= 0x10000;
  10669. }
  10670. return $v;
  10671. }
  10672. /**
  10673. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  10674. * @param $str (string) string from where to extract value
  10675. * @param $offset (int) point from where to read the data
  10676. * @return int 16 bit value
  10677. * @author Nicola Asuni
  10678. * @protected
  10679. * @since 5.9.123 (2011-09-30)
  10680. */
  10681. protected function _getUFWORD($str, $offset) {
  10682. $v = $this->_getUSHORT($str, $offset);
  10683. return $v;
  10684. }
  10685. /**
  10686. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  10687. * @param $str (string) string from where to extract value
  10688. * @param $offset (int) point from where to read the data
  10689. * @return int 16 bit value
  10690. * @author Nicola Asuni
  10691. * @protected
  10692. * @since 5.9.123 (2011-09-30)
  10693. */
  10694. protected function _getFIXED($str, $offset) {
  10695. // mantissa
  10696. $m = $this->_getFWORD($str, $offset);
  10697. // fraction
  10698. $f = $this->_getUSHORT($str, ($offset + 2));
  10699. $v = floatval('' . $m . '.' . $f . '');
  10700. return $v;
  10701. }
  10702. /**
  10703. * Get BYTE from string (8-bit unsigned integer).
  10704. * @param $str (string) String from where to extract value.
  10705. * @param $offset (int) Point from where to read the data.
  10706. * @return int 8 bit value
  10707. * @author Nicola Asuni
  10708. * @protected
  10709. * @since 5.2.000 (2010-06-02)
  10710. */
  10711. protected function _getBYTE($str, $offset) {
  10712. $v = unpack('Ci', substr($str, $offset, 1));
  10713. return $v['i'];
  10714. }
  10715. /**
  10716. * Update the CIDToGIDMap string with a new value.
  10717. * @param $map (string) CIDToGIDMap.
  10718. * @param $cid (int) CID value.
  10719. * @param $gid (int) GID value.
  10720. * @return (string) CIDToGIDMap.
  10721. * @author Nicola Asuni
  10722. * @protected
  10723. * @since 5.9.123 (2011-09-29)
  10724. */
  10725. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  10726. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  10727. if ($gid > 0xFFFF) {
  10728. $gid -= 0x10000;
  10729. }
  10730. $map[($cid * 2)] = chr($gid >> 8);
  10731. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  10732. }
  10733. return $map;
  10734. }
  10735. /**
  10736. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  10737. * @param $fontfile (string) Font file (full path).
  10738. * @param $fonttype (string) Font type. Leave empty for autodetect mode. Valid values are: TrueTypeUnicode, TrueType, Type1, CID0JP = CID-0 Japanese, CID0KR = CID-0 Korean, CID0CS = CID-0 Chinese Simplified, CID0CT = CID-0 Chinese Traditional.
  10739. * @param $enc (string) Name of the encoding table to use. Leave empty for default mode. Omit this parameter for TrueType Unicode and symbolic fonts like Symbol or ZapfDingBats.
  10740. * @param $flags (int) Unsigned 32-bit integer containing flags specifying various characteristics of the font (PDF32000:2008 - 9.8.2 Font Descriptor Flags): +1 for fixed font; +4 for symbol or +32 for non-symbol; +64 for italic. Fixed and Italic mode are generally autodetected so you have to set it to 32 = non-symbolic font (default) or 4 = symbolic font.
  10741. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  10742. * @param $platid (int) Platform ID for CMAP table to extract (when building a Unicode font for Windows this value should be 3, for Macintosh should be 1).
  10743. * @param $encid (int) Encoding ID for CMAP table to extract (when building a Unicode font for Windows this value should be 1, for Macintosh should be 0). When Platform ID is 3, legal values for Encoding ID are: 0=Symbol, 1=Unicode, 2=ShiftJIS, 3=PRC, 4=Big5, 5=Wansung, 6=Johab, 7=Reserved, 8=Reserved, 9=Reserved, 10=UCS-4.
  10744. * @param $addcbbox (boolean) If true includes the character bounding box information on the php font file.
  10745. * @return (string) TCPDF font name.
  10746. * @author Nicola Asuni
  10747. * @public
  10748. * @since 5.9.123 (2010-09-30)
  10749. */
  10750. public function addTTFfont($fontfile, $fonttype = '', $enc = '', $flags = 32, $outpath = '', $platid = 3, $encid = 1, $addcbbox = false) {
  10751. if (!file_exists($fontfile)) {
  10752. $this->Error('Could not find file: ' . $fontfile . '');
  10753. }
  10754. // font metrics
  10755. $fmetric = array();
  10756. // build new font name for TCPDF compatibility
  10757. $font_path_parts = pathinfo($fontfile);
  10758. if (!isset($font_path_parts['filename'])) {
  10759. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  10760. }
  10761. $font_name = strtolower($font_path_parts['filename']);
  10762. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  10763. $search = array('bold', 'oblique', 'italic', 'regular');
  10764. $replace = array('b', 'i', 'i', '');
  10765. $font_name = str_replace($search, $replace, $font_name);
  10766. if (empty($font_name)) {
  10767. // set generic name
  10768. $font_name = 'tcpdffont';
  10769. }
  10770. // set output path
  10771. if (empty($outpath)) {
  10772. $outpath = $this->_getfontpath();
  10773. }
  10774. // check if this font already exist
  10775. if (file_exists($outpath . $font_name . '.php')) {
  10776. // this font already exist (delete it from fonts folder to rebuild it)
  10777. return $font_name;
  10778. }
  10779. $fmetric['file'] = $font_name . '.z';
  10780. $fmetric['ctg'] = $font_name . '.ctg.z';
  10781. // get font data
  10782. $font = file_get_contents($fontfile);
  10783. $fmetric['originalsize'] = strlen($font);
  10784. // autodetect font type
  10785. if (empty($fonttype)) {
  10786. if ($this->_getULONG($font, 0) == 0x10000) {
  10787. // True Type (Unicode or not)
  10788. $fonttype = 'TrueTypeUnicode';
  10789. } elseif (substr($font, 0, 4) == 'OTTO') {
  10790. // Open Type (Unicode or not)
  10791. $this->Error('Unsupported font format: OpenType with CFF data.');
  10792. } else {
  10793. // Type 1
  10794. $fonttype = 'Type1';
  10795. }
  10796. }
  10797. // set font type
  10798. switch ($fonttype) {
  10799. case 'CID0CT':
  10800. case 'CID0CS':
  10801. case 'CID0KR':
  10802. case 'CID0JP': {
  10803. $fmetric['type'] = 'cidfont0';
  10804. break;
  10805. }
  10806. case 'Type1': {
  10807. $fmetric['type'] = 'Type1';
  10808. if (empty($enc) AND (($flags & 4) == 0)) {
  10809. $enc = 'cp1252';
  10810. }
  10811. break;
  10812. }
  10813. case 'TrueType': {
  10814. $fmetric['type'] = 'TrueType';
  10815. break;
  10816. }
  10817. case 'TrueTypeUnicode':
  10818. default: {
  10819. $fmetric['type'] = 'TrueTypeUnicode';
  10820. break;
  10821. }
  10822. }
  10823. // set encoding maps (if any)
  10824. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  10825. $fmetric['diff'] = '';
  10826. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  10827. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  10828. // build differences from reference encoding
  10829. $enc_ref = $this->encmaps->encmap['cp1252'];
  10830. $enc_target = $this->encmaps->encmap[$enc];
  10831. $last = 0;
  10832. for ($i = 32; $i <= 255; ++$i) {
  10833. if ($enc_target != $enc_ref[$i]) {
  10834. if ($i != ($last + 1)) {
  10835. $fmetric['diff'] .= $i . ' ';
  10836. }
  10837. $last = $i;
  10838. $fmetric['diff'] .= '/' . $enc_target[$i] . ' ';
  10839. }
  10840. }
  10841. }
  10842. }
  10843. // parse the font by type
  10844. if ($fmetric['type'] == 'Type1') {
  10845. // ---------- TYPE 1 ----------
  10846. // read first segment
  10847. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  10848. if ($a['marker'] != 128) {
  10849. $this->Error('Font file is not a valid binary Type1');
  10850. }
  10851. $fmetric['size1'] = $a['size'];
  10852. $data = substr($font, 6, $fmetric['size1']);
  10853. // read second segment
  10854. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  10855. if ($a['marker'] != 128) {
  10856. $this->Error('Font file is not a valid binary Type1');
  10857. }
  10858. $fmetric['size2'] = $a['size'];
  10859. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  10860. $data .= $encrypted;
  10861. // store compressed font
  10862. $fp = fopen($outpath . $fmetric['file'], 'wb');
  10863. fwrite($fp, gzcompress($data));
  10864. fclose($fp);
  10865. // get font info
  10866. $fmetric['Flags'] = $flags;
  10867. preg_match('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  10868. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  10869. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  10870. $fmetric['bbox'] = trim($matches[1]);
  10871. $bv = explode(' ', $fmetric['bbox']);
  10872. $fmetric['Ascent'] = intval($bv[3]);
  10873. $fmetric['Descent'] = intval($bv[1]);
  10874. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  10875. $fmetric['italicAngle'] = intval($matches[1]);
  10876. if ($fmetric['italicAngle'] != 0) {
  10877. $fmetric['Flags'] |= 64;
  10878. }
  10879. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  10880. $fmetric['underlinePosition'] = intval($matches[1]);
  10881. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  10882. $fmetric['underlineThickness'] = intval($matches[1]);
  10883. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  10884. if ($matches[1] == 'true') {
  10885. $fmetric['Flags'] |= 1;
  10886. }
  10887. // get internal map
  10888. $imap = array();
  10889. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  10890. foreach ($fmap as $v) {
  10891. $imap[$v[2]] = $v[1];
  10892. }
  10893. }
  10894. // decrypt eexec encrypted part
  10895. $r = 55665; // eexec encryption constant
  10896. $c1 = 52845;
  10897. $c2 = 22719;
  10898. $elen = strlen($encrypted);
  10899. $eplain = '';
  10900. for ($i = 0; $i < $elen; ++$i) {
  10901. $chr = ord($encrypted[$i]);
  10902. $eplain .= chr($chr ^ ($r >> 8));
  10903. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  10904. }
  10905. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  10906. if ($matches[1] == 'true') {
  10907. $fmetric['Flags'] |= 0x40000;
  10908. }
  10909. }
  10910. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  10911. $fmetric['StemV'] = intval($matches[1]);
  10912. } else {
  10913. $fmetric['StemV'] = 70;
  10914. }
  10915. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  10916. $fmetric['StemH'] = intval($matches[1]);
  10917. } else {
  10918. $fmetric['StemH'] = 30;
  10919. }
  10920. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  10921. $bv = explode(' ', $matches[1]);
  10922. if (count($bv) >= 6) {
  10923. $v1 = intval($bv[2]);
  10924. $v2 = intval($bv[4]);
  10925. if ($v1 <= $v2) {
  10926. $fmetric['XHeight'] = $v1;
  10927. $fmetric['CapHeight'] = $v2;
  10928. } else {
  10929. $fmetric['XHeight'] = $v2;
  10930. $fmetric['CapHeight'] = $v1;
  10931. }
  10932. } else {
  10933. $fmetric['XHeight'] = 450;
  10934. $fmetric['CapHeight'] = 700;
  10935. }
  10936. } else {
  10937. $fmetric['XHeight'] = 450;
  10938. $fmetric['CapHeight'] = 700;
  10939. }
  10940. // get the number of random bytes at the beginning of charstrings
  10941. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  10942. $lenIV = intval($matches[1]);
  10943. } else {
  10944. $lenIV = 4;
  10945. }
  10946. $fmetric['Leading'] = 0;
  10947. // get charstring data
  10948. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  10949. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  10950. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  10951. $enc_map = $this->encmaps->encmap[$enc];
  10952. } else {
  10953. $enc_map = false;
  10954. }
  10955. $fmetric['cw'] = '';
  10956. $fmetric['MaxWidth'] = 0;
  10957. $cwidths = array();
  10958. foreach ($matches as $k => $v) {
  10959. $cid = 0;
  10960. if (isset($imap[$v[1]])) {
  10961. $cid = $imap[$v[1]];
  10962. } elseif ($enc_map !== false) {
  10963. $cid = array_search($v[1], $enc_map);
  10964. if ($cid === false) {
  10965. $cid = 0;
  10966. } elseif ($cid > 1000) {
  10967. $cid -= 1000;
  10968. }
  10969. }
  10970. // decrypt charstring encrypted part
  10971. $r = 4330; // charstring encryption constant
  10972. $c1 = 52845;
  10973. $c2 = 22719;
  10974. $cd = $v[2];
  10975. $clen = strlen($cd);
  10976. $ccom = array();
  10977. for ($i = 0; $i < $clen; ++$i) {
  10978. $chr = ord($cd[$i]);
  10979. $ccom[] = ($chr ^ ($r >> 8));
  10980. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  10981. }
  10982. // decode numbers
  10983. $cdec = array();
  10984. $ck = 0;
  10985. $i = $lenIV;
  10986. while ($i < $clen) {
  10987. if ($ccom[$i] < 32) {
  10988. $cdec[$ck] = $ccom[$i];
  10989. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  10990. // hsbw command: update width
  10991. $cwidths[$cid] = $cdec[($ck - 1)];
  10992. }
  10993. ++$i;
  10994. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  10995. $cdec[$ck] = ($ccom[$i] - 139);
  10996. ++$i;
  10997. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  10998. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  10999. $i += 2;
  11000. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  11001. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  11002. $i += 2;
  11003. } elseif ($ccom[$i] == 255) {
  11004. $sval = chr($ccom[($i + 1)]) . chr($ccom[($i + 2)]) . chr($ccom[($i + 3)]) . chr($ccom[($i + 4)]);
  11005. $vsval = unpack('li', $sval);
  11006. $cdec[$ck] = $vsval['i'];
  11007. $i += 5;
  11008. }
  11009. ++$ck;
  11010. }
  11011. } // end for each matches
  11012. $fmetric['MissingWidth'] = $cwidths[0];
  11013. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  11014. $fmetric['AvgWidth'] = 0;
  11015. // set chars widths
  11016. for ($cid = 0; $cid <= 255; ++$cid) {
  11017. if (isset($cwidths[$cid])) {
  11018. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  11019. $fmetric['MaxWidth'] = $cwidths[$cid];
  11020. }
  11021. $fmetric['AvgWidth'] += $cwidths[$cid];
  11022. $fmetric['cw'] .= ',' . $cid . '=>' . $cwidths[$cid];
  11023. } else {
  11024. $fmetric['cw'] .= ',' . $cid . '=>' . $fmetric['MissingWidth'];
  11025. }
  11026. }
  11027. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  11028. } else {
  11029. // ---------- TRUE TYPE ----------
  11030. if ($fmetric['type'] != 'cidfont0') {
  11031. // store compressed font
  11032. $fp = fopen($outpath . $fmetric['file'], 'wb');
  11033. fwrite($fp, gzcompress($font));
  11034. fclose($fp);
  11035. }
  11036. $offset = 0; // offset position of the font data
  11037. if ($this->_getULONG($font, $offset) != 0x10000) {
  11038. // sfnt version must be 0x00010000 for TrueType version 1.0.
  11039. return $font;
  11040. }
  11041. $offset += 4;
  11042. // get number of tables
  11043. $numTables = $this->_getUSHORT($font, $offset);
  11044. $offset += 2;
  11045. // skip searchRange, entrySelector and rangeShift
  11046. $offset += 6;
  11047. // tables array
  11048. $table = array();
  11049. // ---------- get tables ----------
  11050. for ($i = 0; $i < $numTables; ++$i) {
  11051. // get table info
  11052. $tag = substr($font, $offset, 4);
  11053. $offset += 4;
  11054. $table[$tag] = array();
  11055. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  11056. $offset += 4;
  11057. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  11058. $offset += 4;
  11059. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  11060. $offset += 4;
  11061. }
  11062. // check magicNumber
  11063. $offset = $table['head']['offset'] + 12;
  11064. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  11065. // magicNumber must be 0x5F0F3CF5
  11066. return $font;
  11067. }
  11068. $offset += 4;
  11069. $offset += 2; // skip flags
  11070. // get FUnits
  11071. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  11072. $offset += 2;
  11073. // units ratio constant
  11074. $urk = (1000 / $fmetric['unitsPerEm']);
  11075. $offset += 16; // skip created, modified
  11076. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  11077. $offset += 2;
  11078. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  11079. $offset += 2;
  11080. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  11081. $offset += 2;
  11082. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  11083. $offset += 2;
  11084. $fmetric['bbox'] = '' . $xMin . ' ' . $yMin . ' ' . $xMax . ' ' . $yMax . '';
  11085. $macStyle = $this->_getUSHORT($font, $offset);
  11086. $offset += 2;
  11087. // PDF font flags
  11088. $fmetric['Flags'] = $flags;
  11089. if (($macStyle & 2) == 2) {
  11090. // italic flag
  11091. $fmetric['Flags'] |= 64;
  11092. }
  11093. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  11094. $offset = $table['head']['offset'] + 50;
  11095. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  11096. $offset += 2;
  11097. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  11098. $indexToLoc = array();
  11099. $offset = $table['loca']['offset'];
  11100. if ($short_offset) {
  11101. // short version
  11102. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  11103. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  11104. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  11105. $offset += 2;
  11106. }
  11107. } else {
  11108. // long version
  11109. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  11110. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  11111. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  11112. $offset += 4;
  11113. }
  11114. }
  11115. // get glyphs indexes of chars from cmap table
  11116. $offset = $table['cmap']['offset'] + 2;
  11117. $numEncodingTables = $this->_getUSHORT($font, $offset);
  11118. $offset += 2;
  11119. $encodingTables = array();
  11120. for ($i = 0; $i < $numEncodingTables; ++$i) {
  11121. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  11122. $offset += 2;
  11123. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  11124. $offset += 2;
  11125. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  11126. $offset += 4;
  11127. }
  11128. // ---------- get os/2 metrics ----------
  11129. $offset = $table['OS/2']['offset'];
  11130. $offset += 2; // skip version
  11131. // xAvgCharWidth
  11132. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  11133. $offset += 2;
  11134. // usWeightClass
  11135. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  11136. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  11137. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  11138. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  11139. $offset += 2;
  11140. $offset += 2; // usWidthClass
  11141. $fsType = $this->_getSHORT($font, $offset);
  11142. $offset += 2;
  11143. if ($fsType == 2) {
  11144. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  11145. }
  11146. // ---------- get font name ----------
  11147. $fmetric['name'] = '';
  11148. $offset = $table['name']['offset'];
  11149. $offset += 2; // skip Format selector (=0).
  11150. // Number of NameRecords that follow n.
  11151. $numNameRecords = $this->_getUSHORT($font, $offset);
  11152. $offset += 2;
  11153. // Offset to start of string storage (from start of table).
  11154. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  11155. $offset += 2;
  11156. for ($i = 0; $i < $numNameRecords; ++$i) {
  11157. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  11158. // Name ID.
  11159. $nameID = $this->_getUSHORT($font, $offset);
  11160. $offset += 2;
  11161. if ($nameID == 6) {
  11162. // String length (in bytes).
  11163. $stringLength = $this->_getUSHORT($font, $offset);
  11164. $offset += 2;
  11165. // String offset from start of storage area (in bytes).
  11166. $stringOffset = $this->_getUSHORT($font, $offset);
  11167. $offset += 2;
  11168. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  11169. $fmetric['name'] = substr($font, $offset, $stringLength);
  11170. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  11171. break;
  11172. } else {
  11173. $offset += 4; // skip String length, String offset
  11174. }
  11175. }
  11176. if (empty($fmetric['name'])) {
  11177. $fmetric['name'] = $font_name;
  11178. }
  11179. // ---------- get post data ----------
  11180. $offset = $table['post']['offset'];
  11181. $offset += 4; // skip Format Type
  11182. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  11183. $offset += 4;
  11184. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  11185. $offset += 2;
  11186. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  11187. $offset += 2;
  11188. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  11189. $offset += 2;
  11190. if ($isFixedPitch) {
  11191. $fmetric['Flags'] |= 1;
  11192. }
  11193. // ---------- get hhea data ----------
  11194. $offset = $table['hhea']['offset'];
  11195. $offset += 4; // skip Table version number
  11196. // Ascender
  11197. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  11198. $offset += 2;
  11199. // Descender
  11200. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  11201. $offset += 2;
  11202. // LineGap
  11203. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  11204. $offset += 2;
  11205. // advanceWidthMax
  11206. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  11207. $offset += 2;
  11208. $offset += 22; // skip some values
  11209. // get the number of hMetric entries in hmtx table
  11210. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  11211. // ---------- get maxp data ----------
  11212. $offset = $table['maxp']['offset'];
  11213. $offset += 4; // skip Table version number
  11214. // get the the number of glyphs in the font.
  11215. $numGlyphs = $this->_getUSHORT($font, $offset);
  11216. // ---------- get CIDToGIDMap ----------
  11217. $ctg = array();
  11218. foreach ($encodingTables as $enctable) {
  11219. // get only specified Platform ID and Encoding ID
  11220. if (($enctable['platformID'] == $platid) AND ($enctable['encodingID'] == $encid)) {
  11221. $offset = $table['cmap']['offset'] + $enctable['offset'];
  11222. $format = $this->_getUSHORT($font, $offset);
  11223. $offset += 2;
  11224. switch ($format) {
  11225. case 0: { // Format 0: Byte encoding table
  11226. $offset += 4; // skip length and version/language
  11227. for ($c = 0; $c < 256; ++$c) {
  11228. $g = $this->_getBYTE($font, $offset);
  11229. $ctg[$c] = $g;
  11230. ++$offset;
  11231. }
  11232. break;
  11233. }
  11234. case 2: { // Format 2: High-byte mapping through table
  11235. $offset += 4; // skip length and version/language
  11236. $numSubHeaders = 0;
  11237. for ($i = 0; $i < 256; ++$i) {
  11238. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  11239. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  11240. $offset += 2;
  11241. if ($numSubHeaders < $subHeaderKeys[$i]) {
  11242. $numSubHeaders = $subHeaderKeys[$i];
  11243. }
  11244. }
  11245. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  11246. ++$numSubHeaders;
  11247. // read subHeader structures
  11248. $subHeaders = array();
  11249. $numGlyphIndexArray = 0;
  11250. for ($k = 0; $k < $numSubHeaders; ++$k) {
  11251. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  11252. $offset += 2;
  11253. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  11254. $offset += 2;
  11255. $subHeaders[$k]['idDelta'] = $this->_getUSHORT($font, $offset);
  11256. $offset += 2;
  11257. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  11258. $offset += 2;
  11259. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  11260. $subHeaders[$k]['idRangeOffset'] /= 2;
  11261. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  11262. }
  11263. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  11264. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  11265. $offset += 2;
  11266. }
  11267. for ($i = 0; $i < 256; ++$i) {
  11268. $k = $subHeaderKeys[$i];
  11269. if ($k == 0) {
  11270. // one byte code
  11271. $c = $i;
  11272. $g = $glyphIndexArray[0];
  11273. $ctg[$c] = $g;
  11274. } else {
  11275. // two bytes code
  11276. $start_byte = $subHeaders[$k]['firstCode'];
  11277. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  11278. for ($j = $start_byte; $j < $end_byte; ++$j) {
  11279. // combine high and low bytes
  11280. $c = (($i << 8) + $j);
  11281. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  11282. $g = ($glyphIndexArray[$idRangeOffset] + $idDelta[$k]) % 65536;
  11283. if ($g < 0) {
  11284. $g = 0;
  11285. }
  11286. $ctg[$c] = $g;
  11287. }
  11288. }
  11289. }
  11290. break;
  11291. }
  11292. case 4: { // Format 4: Segment mapping to delta values
  11293. $length = $this->_getUSHORT($font, $offset);
  11294. $offset += 2;
  11295. $offset += 2; // skip version/language
  11296. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  11297. $offset += 2;
  11298. $offset += 6; // skip searchRange, entrySelector, rangeShift
  11299. $endCount = array(); // array of end character codes for each segment
  11300. for ($k = 0; $k < $segCount; ++$k) {
  11301. $endCount[$k] = $this->_getUSHORT($font, $offset);
  11302. $offset += 2;
  11303. }
  11304. $offset += 2; // skip reservedPad
  11305. $startCount = array(); // array of start character codes for each segment
  11306. for ($k = 0; $k < $segCount; ++$k) {
  11307. $startCount[$k] = $this->_getUSHORT($font, $offset);
  11308. $offset += 2;
  11309. }
  11310. $idDelta = array(); // delta for all character codes in segment
  11311. for ($k = 0; $k < $segCount; ++$k) {
  11312. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  11313. $offset += 2;
  11314. }
  11315. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  11316. for ($k = 0; $k < $segCount; ++$k) {
  11317. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  11318. $offset += 2;
  11319. }
  11320. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  11321. $glyphIdArray = array(); // glyph index array
  11322. for ($k = 0; $k < $gidlen; ++$k) {
  11323. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  11324. $offset += 2;
  11325. }
  11326. for ($k = 0; $k < $segCount; ++$k) {
  11327. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  11328. if ($idRangeOffset[$k] == 0) {
  11329. $g = ($idDelta[$k] + $c) % 65536;
  11330. } else {
  11331. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  11332. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  11333. }
  11334. if ($g < 0) {
  11335. $g = 0;
  11336. }
  11337. $ctg[$c] = $g;
  11338. }
  11339. }
  11340. break;
  11341. }
  11342. case 6: { // Format 6: Trimmed table mapping
  11343. $offset += 4; // skip length and version/language
  11344. $firstCode = $this->_getUSHORT($font, $offset);
  11345. $offset += 2;
  11346. $entryCount = $this->_getUSHORT($font, $offset);
  11347. $offset += 2;
  11348. for ($k = 0; $k < $entryCount; ++$k) {
  11349. $c = ($k + $firstCode);
  11350. $g = $this->_getUSHORT($font, $offset);
  11351. $offset += 2;
  11352. $ctg[$c] = $g;
  11353. }
  11354. break;
  11355. }
  11356. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  11357. $offset += 10; // skip reserved, length and version/language
  11358. for ($k = 0; $k < 8192; ++$k) {
  11359. $is32[$k] = $this->_getBYTE($font, $offset);
  11360. ++$offset;
  11361. }
  11362. $nGroups = $this->_getULONG($font, $offset);
  11363. $offset += 4;
  11364. for ($i = 0; $i < $nGroups; ++$i) {
  11365. $startCharCode = $this->_getULONG($font, $offset);
  11366. $offset += 4;
  11367. $endCharCode = $this->_getULONG($font, $offset);
  11368. $offset += 4;
  11369. $startGlyphID = $this->_getULONG($font, $offset);
  11370. $offset += 4;
  11371. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  11372. $is32idx = floor($c / 8);
  11373. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  11374. $c = $k;
  11375. } else {
  11376. // 32 bit format
  11377. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  11378. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  11379. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  11380. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) - 56613888;
  11381. }
  11382. $ctg[$c] = 0;
  11383. ++$startGlyphID;
  11384. }
  11385. }
  11386. break;
  11387. }
  11388. case 10: { // Format 10: Trimmed array
  11389. $offset += 10; // skip reserved, length and version/language
  11390. $startCharCode = $this->_getULONG($font, $offset);
  11391. $offset += 4;
  11392. $numChars = $this->_getULONG($font, $offset);
  11393. $offset += 4;
  11394. for ($k = 0; $k < $numChars; ++$k) {
  11395. $c = ($k + $startCharCode);
  11396. $g = $this->_getUSHORT($font, $offset);
  11397. $ctg[$c] = $g;
  11398. $offset += 2;
  11399. }
  11400. break;
  11401. }
  11402. case 12: { // Format 12: Segmented coverage
  11403. $offset += 10; // skip length and version/language
  11404. $nGroups = $this->_getULONG($font, $offset);
  11405. $offset += 4;
  11406. for ($k = 0; $k < $nGroups; ++$k) {
  11407. $startCharCode = $this->_getULONG($font, $offset);
  11408. $offset += 4;
  11409. $endCharCode = $this->_getULONG($font, $offset);
  11410. $offset += 4;
  11411. $startGlyphCode = $this->_getULONG($font, $offset);
  11412. $offset += 4;
  11413. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  11414. $ctg[$c] = $startGlyphCode;
  11415. ++$startGlyphCode;
  11416. }
  11417. }
  11418. break;
  11419. }
  11420. case 13: { // Format 13: Many-to-one range mappings
  11421. // to be implemented ...
  11422. break;
  11423. }
  11424. case 14: { // Format 14: Unicode Variation Sequences
  11425. // to be implemented ...
  11426. break;
  11427. }
  11428. }
  11429. }
  11430. }
  11431. if (!isset($ctg[0])) {
  11432. $ctg[0] = 0;
  11433. }
  11434. // get xHeight (height of x)
  11435. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  11436. $yMin = $this->_getFWORD($font, $offset);
  11437. $offset += 4;
  11438. $yMax = $this->_getFWORD($font, $offset);
  11439. $offset += 2;
  11440. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  11441. // get CapHeight (height of H)
  11442. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  11443. $yMin = $this->_getFWORD($font, $offset);
  11444. $offset += 4;
  11445. $yMax = $this->_getFWORD($font, $offset);
  11446. $offset += 2;
  11447. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  11448. // ceate widths array
  11449. $cw = array();
  11450. $offset = $table['hmtx']['offset'];
  11451. for ($i = 0; $i < $numberOfHMetrics; ++$i) {
  11452. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  11453. $offset += 4; // skip lsb
  11454. }
  11455. if ($numberOfHMetrics < $numGlyphs) {
  11456. // fill missing widths with the last value
  11457. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  11458. }
  11459. $fmetric['MissingWidth'] = $cw[0];
  11460. $fmetric['cw'] = '';
  11461. for ($cid = 0; $cid <= 65535; ++$cid) {
  11462. if (isset($ctg[$cid])) {
  11463. if (isset($cw[$ctg[$cid]])) {
  11464. $fmetric['cw'] .= ',' . $cid . '=>' . $cw[$ctg[$cid]];
  11465. }
  11466. if ($addcbbox AND isset($indexToLoc[$ctg[$cid]])) {
  11467. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[$cid]]);
  11468. $xMin = round($this->_getFWORD($font, $offset + 2)) * $urk;
  11469. $yMin = round($this->_getFWORD($font, $offset + 4)) * $urk;
  11470. $xMax = round($this->_getFWORD($font, $offset + 6)) * $urk;
  11471. $yMax = round($this->_getFWORD($font, $offset + 8)) * $urk;
  11472. $fmetric['cbbox'] .= ',' . $cid . '=>array(' . $xMin . ',' . $yMin . ',' . $xMax . ',' . $yMax . ')';
  11473. }
  11474. }
  11475. }
  11476. } // end of true type
  11477. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  11478. $fmetric['type'] == 'TrueType';
  11479. }
  11480. // ---------- create php font file ----------
  11481. $pfile = '<' . '?' . 'php' . "\n";
  11482. $pfile .= '// TCPDF FONT FILE DESCRIPTION' . "\n";
  11483. $pfile .= '$type=\'' . $fmetric['type'] . '\';' . "\n";
  11484. $pfile .= '$name=\'' . $fmetric['name'] . '\';' . "\n";
  11485. $pfile .= '$up=' . $fmetric['underlinePosition'] . ';' . "\n";
  11486. $pfile .= '$ut=' . $fmetric['underlineThickness'] . ';' . "\n";
  11487. if ($fmetric['MissingWidth'] > 0) {
  11488. $pfile .= '$dw=' . $fmetric['MissingWidth'] . ';' . "\n";
  11489. } else {
  11490. $pfile .= '$dw=' . $fmetric['AvgWidth'] . ';' . "\n";
  11491. }
  11492. $pfile .= '$diff=\'' . $fmetric['diff'] . '\';' . "\n";
  11493. if ($fmetric['type'] == 'Type1') {
  11494. // Type 1
  11495. $pfile .= '$enc=\'' . $fmetric['enc'] . '\';' . "\n";
  11496. $pfile .= '$file=\'' . $fmetric['file'] . '\';' . "\n";
  11497. $pfile .= '$size1=' . $fmetric['size1'] . ';' . "\n";
  11498. $pfile .= '$size2=' . $fmetric['size2'] . ';' . "\n";
  11499. } else {
  11500. $pfile .= '$originalsize=' . $fmetric['originalsize'] . ';' . "\n";
  11501. if ($fmetric['type'] == 'cidfont0') {
  11502. // CID-0
  11503. switch ($fonttype) {
  11504. case 'CID0JP': {
  11505. $pfile .= '// Japanese' . "\n";
  11506. $pfile .= '$enc=\'UniJIS-UTF16-H\';' . "\n";
  11507. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);' . "\n";
  11508. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');' . "\n";
  11509. break;
  11510. }
  11511. case 'CID0KR': {
  11512. $pfile .= '// Korean' . "\n";
  11513. $pfile .= '$enc=\'UniKS-UTF16-H\';' . "\n";
  11514. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);' . "\n";
  11515. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');' . "\n";
  11516. break;
  11517. }
  11518. case 'CID0CS': {
  11519. $pfile .= '// Chinese Simplified' . "\n";
  11520. $pfile .= '$enc=\'UniGB-UTF16-H\';' . "\n";
  11521. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);' . "\n";
  11522. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');' . "\n";
  11523. break;
  11524. }
  11525. case 'CID0CT':
  11526. default: {
  11527. $pfile .= '// Chinese Traditional' . "\n";
  11528. $pfile .= '$enc=\'UniCNS-UTF16-H\';' . "\n";
  11529. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);' . "\n";
  11530. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');' . "\n";
  11531. break;
  11532. }
  11533. }
  11534. } else {
  11535. // TrueType
  11536. $pfile .= '$enc=\'' . $fmetric['enc'] . '\';' . "\n";
  11537. $pfile .= '$file=\'' . $fmetric['file'] . '\';' . "\n";
  11538. $pfile .= '$ctg=\'' . $fmetric['ctg'] . '\';' . "\n";
  11539. // create CIDToGIDMap
  11540. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  11541. foreach ($ctg as $cid => $gid) {
  11542. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  11543. }
  11544. // store compressed CIDToGIDMap
  11545. $fp = fopen($outpath . $fmetric['ctg'], 'wb');
  11546. fwrite($fp, gzcompress($cidtogidmap));
  11547. fclose($fp);
  11548. }
  11549. }
  11550. $pfile .= '$desc=array(';
  11551. $pfile .= '\'Flags\'=>' . $fmetric['Flags'] . ',';
  11552. $pfile .= '\'FontBBox\'=>\'[' . $fmetric['bbox'] . ']\',';
  11553. $pfile .= '\'ItalicAngle\'=>' . $fmetric['italicAngle'] . ',';
  11554. $pfile .= '\'Ascent\'=>' . $fmetric['Ascent'] . ',';
  11555. $pfile .= '\'Descent\'=>' . $fmetric['Descent'] . ',';
  11556. $pfile .= '\'Leading\'=>' . $fmetric['Leading'] . ',';
  11557. $pfile .= '\'CapHeight\'=>' . $fmetric['CapHeight'] . ',';
  11558. $pfile .= '\'XHeight\'=>' . $fmetric['XHeight'] . ',';
  11559. $pfile .= '\'StemV\'=>' . $fmetric['StemV'] . ',';
  11560. $pfile .= '\'StemH\'=>' . $fmetric['StemH'] . ',';
  11561. $pfile .= '\'AvgWidth\'=>' . $fmetric['AvgWidth'] . ',';
  11562. $pfile .= '\'MaxWidth\'=>' . $fmetric['MaxWidth'] . ',';
  11563. $pfile .= '\'MissingWidth\'=>' . $fmetric['MissingWidth'] . '';
  11564. $pfile .= ');' . "\n";
  11565. if (isset($fmetric['cbbox'])) {
  11566. $pfile .= '$cbbox=array(' . substr($fmetric['cbbox'], 1) . ');' . "\n";
  11567. }
  11568. $pfile .= '$cw=array(' . substr($fmetric['cw'], 1) . ');' . "\n";
  11569. $pfile .= '// --- EOF ---' . "\n";
  11570. // store file
  11571. $fp = fopen($outpath . $font_name . '.php', 'w');
  11572. fwrite($fp, $pfile);
  11573. fclose($fp);
  11574. // return TCPDF font name
  11575. return $font_name;
  11576. }
  11577. /**
  11578. * Returns a subset of the TrueType font data without the unused glyphs.
  11579. * @param $font (string) TrueType font data.
  11580. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  11581. * @return (string) A subset of TrueType font data without the unused glyphs.
  11582. * @author Nicola Asuni
  11583. * @protected
  11584. * @since 5.2.000 (2010-06-02)
  11585. */
  11586. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  11587. ksort($subsetchars);
  11588. $offset = 0; // offset position of the font data
  11589. if ($this->_getULONG($font, $offset) != 0x10000) {
  11590. // sfnt version must be 0x00010000 for TrueType version 1.0.
  11591. return $font;
  11592. }
  11593. $offset += 4;
  11594. // get number of tables
  11595. $numTables = $this->_getUSHORT($font, $offset);
  11596. $offset += 2;
  11597. // skip searchRange, entrySelector and rangeShift
  11598. $offset += 6;
  11599. // tables array
  11600. $table = array();
  11601. // for each table
  11602. for ($i = 0; $i < $numTables; ++$i) {
  11603. // get table info
  11604. $tag = substr($font, $offset, 4);
  11605. $offset += 4;
  11606. $table[$tag] = array();
  11607. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  11608. $offset += 4;
  11609. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  11610. $offset += 4;
  11611. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  11612. $offset += 4;
  11613. }
  11614. // check magicNumber
  11615. $offset = $table['head']['offset'] + 12;
  11616. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  11617. // magicNumber must be 0x5F0F3CF5
  11618. return $font;
  11619. }
  11620. $offset += 4;
  11621. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  11622. $offset = $table['head']['offset'] + 50;
  11623. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  11624. $offset += 2;
  11625. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  11626. $indexToLoc = array();
  11627. $offset = $table['loca']['offset'];
  11628. if ($short_offset) {
  11629. // short version
  11630. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  11631. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  11632. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  11633. $offset += 2;
  11634. }
  11635. } else {
  11636. // long version
  11637. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  11638. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  11639. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  11640. $offset += 4;
  11641. }
  11642. }
  11643. // get glyphs indexes of chars from cmap table
  11644. $subsetglyphs = array(); // glyph IDs on key
  11645. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  11646. $offset = $table['cmap']['offset'] + 2;
  11647. $numEncodingTables = $this->_getUSHORT($font, $offset);
  11648. $offset += 2;
  11649. $encodingTables = array();
  11650. for ($i = 0; $i < $numEncodingTables; ++$i) {
  11651. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  11652. $offset += 2;
  11653. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  11654. $offset += 2;
  11655. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  11656. $offset += 4;
  11657. }
  11658. foreach ($encodingTables as $enctable) {
  11659. // get all platforms and encodings
  11660. $offset = $table['cmap']['offset'] + $enctable['offset'];
  11661. $format = $this->_getUSHORT($font, $offset);
  11662. $offset += 2;
  11663. switch ($format) {
  11664. case 0: { // Format 0: Byte encoding table
  11665. $offset += 4; // skip length and version/language
  11666. for ($c = 0; $c < 256; ++$c) {
  11667. if (isset($subsetchars[$c])) {
  11668. $g = $this->_getBYTE($font, $offset);
  11669. $subsetglyphs[$g] = true;
  11670. }
  11671. ++$offset;
  11672. }
  11673. break;
  11674. }
  11675. case 2: { // Format 2: High-byte mapping through table
  11676. $offset += 4; // skip length and version/language
  11677. $numSubHeaders = 0;
  11678. for ($i = 0; $i < 256; ++$i) {
  11679. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  11680. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  11681. $offset += 2;
  11682. if ($numSubHeaders < $subHeaderKeys[$i]) {
  11683. $numSubHeaders = $subHeaderKeys[$i];
  11684. }
  11685. }
  11686. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  11687. ++$numSubHeaders;
  11688. // read subHeader structures
  11689. $subHeaders = array();
  11690. $numGlyphIndexArray = 0;
  11691. for ($k = 0; $k < $numSubHeaders; ++$k) {
  11692. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  11693. $offset += 2;
  11694. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  11695. $offset += 2;
  11696. $subHeaders[$k]['idDelta'] = $this->_getUSHORT($font, $offset);
  11697. $offset += 2;
  11698. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  11699. $offset += 2;
  11700. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  11701. $subHeaders[$k]['idRangeOffset'] /= 2;
  11702. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  11703. }
  11704. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  11705. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  11706. $offset += 2;
  11707. }
  11708. for ($i = 0; $i < 256; ++$i) {
  11709. $k = $subHeaderKeys[$i];
  11710. if ($k == 0) {
  11711. // one byte code
  11712. $c = $i;
  11713. if (isset($subsetchars[$c])) {
  11714. $g = $glyphIndexArray[0];
  11715. $subsetglyphs[$g] = true;
  11716. }
  11717. } else {
  11718. // two bytes code
  11719. $start_byte = $subHeaders[$k]['firstCode'];
  11720. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  11721. for ($j = $start_byte; $j < $end_byte; ++$j) {
  11722. // combine high and low bytes
  11723. $c = (($i << 8) + $j);
  11724. if (isset($subsetchars[$c])) {
  11725. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  11726. $g = ($glyphIndexArray[$idRangeOffset] + $idDelta[$k]) % 65536;
  11727. if ($g < 0) {
  11728. $g = 0;
  11729. }
  11730. $subsetglyphs[$g] = true;
  11731. }
  11732. }
  11733. }
  11734. }
  11735. break;
  11736. }
  11737. case 4: { // Format 4: Segment mapping to delta values
  11738. $length = $this->_getUSHORT($font, $offset);
  11739. $offset += 2;
  11740. $offset += 2; // skip version/language
  11741. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  11742. $offset += 2;
  11743. $offset += 6; // skip searchRange, entrySelector, rangeShift
  11744. $endCount = array(); // array of end character codes for each segment
  11745. for ($k = 0; $k < $segCount; ++$k) {
  11746. $endCount[$k] = $this->_getUSHORT($font, $offset);
  11747. $offset += 2;
  11748. }
  11749. $offset += 2; // skip reservedPad
  11750. $startCount = array(); // array of start character codes for each segment
  11751. for ($k = 0; $k < $segCount; ++$k) {
  11752. $startCount[$k] = $this->_getUSHORT($font, $offset);
  11753. $offset += 2;
  11754. }
  11755. $idDelta = array(); // delta for all character codes in segment
  11756. for ($k = 0; $k < $segCount; ++$k) {
  11757. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  11758. $offset += 2;
  11759. }
  11760. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  11761. for ($k = 0; $k < $segCount; ++$k) {
  11762. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  11763. $offset += 2;
  11764. }
  11765. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  11766. $glyphIdArray = array(); // glyph index array
  11767. for ($k = 0; $k < $gidlen; ++$k) {
  11768. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  11769. $offset += 2;
  11770. }
  11771. for ($k = 0; $k < $segCount; ++$k) {
  11772. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  11773. if (isset($subsetchars[$c])) {
  11774. if ($idRangeOffset[$k] == 0) {
  11775. $g = ($idDelta[$k] + $c) % 65536;
  11776. } else {
  11777. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  11778. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  11779. }
  11780. if ($g < 0) {
  11781. $g = 0;
  11782. }
  11783. $subsetglyphs[$g] = true;
  11784. }
  11785. }
  11786. }
  11787. break;
  11788. }
  11789. case 6: { // Format 6: Trimmed table mapping
  11790. $offset += 4; // skip length and version/language
  11791. $firstCode = $this->_getUSHORT($font, $offset);
  11792. $offset += 2;
  11793. $entryCount = $this->_getUSHORT($font, $offset);
  11794. $offset += 2;
  11795. for ($k = 0; $k < $entryCount; ++$k) {
  11796. $c = ($k + $firstCode);
  11797. if (isset($subsetchars[$c])) {
  11798. $g = $this->_getUSHORT($font, $offset);
  11799. $subsetglyphs[$g] = true;
  11800. }
  11801. $offset += 2;
  11802. }
  11803. break;
  11804. }
  11805. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  11806. $offset += 10; // skip reserved, length and version/language
  11807. for ($k = 0; $k < 8192; ++$k) {
  11808. $is32[$k] = $this->_getBYTE($font, $offset);
  11809. ++$offset;
  11810. }
  11811. $nGroups = $this->_getULONG($font, $offset);
  11812. $offset += 4;
  11813. for ($i = 0; $i < $nGroups; ++$i) {
  11814. $startCharCode = $this->_getULONG($font, $offset);
  11815. $offset += 4;
  11816. $endCharCode = $this->_getULONG($font, $offset);
  11817. $offset += 4;
  11818. $startGlyphID = $this->_getULONG($font, $offset);
  11819. $offset += 4;
  11820. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  11821. $is32idx = floor($c / 8);
  11822. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  11823. $c = $k;
  11824. } else {
  11825. // 32 bit format
  11826. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  11827. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  11828. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  11829. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) - 56613888;
  11830. }
  11831. if (isset($subsetchars[$c])) {
  11832. $subsetglyphs[$startGlyphID] = true;
  11833. }
  11834. ++$startGlyphID;
  11835. }
  11836. }
  11837. break;
  11838. }
  11839. case 10: { // Format 10: Trimmed array
  11840. $offset += 10; // skip reserved, length and version/language
  11841. $startCharCode = $this->_getULONG($font, $offset);
  11842. $offset += 4;
  11843. $numChars = $this->_getULONG($font, $offset);
  11844. $offset += 4;
  11845. for ($k = 0; $k < $numChars; ++$k) {
  11846. $c = ($k + $startCharCode);
  11847. if (isset($subsetchars[$c])) {
  11848. $g = $this->_getUSHORT($font, $offset);
  11849. $subsetglyphs[$g] = true;
  11850. }
  11851. $offset += 2;
  11852. }
  11853. break;
  11854. }
  11855. case 12: { // Format 12: Segmented coverage
  11856. $offset += 10; // skip length and version/language
  11857. $nGroups = $this->_getULONG($font, $offset);
  11858. $offset += 4;
  11859. for ($k = 0; $k < $nGroups; ++$k) {
  11860. $startCharCode = $this->_getULONG($font, $offset);
  11861. $offset += 4;
  11862. $endCharCode = $this->_getULONG($font, $offset);
  11863. $offset += 4;
  11864. $startGlyphCode = $this->_getULONG($font, $offset);
  11865. $offset += 4;
  11866. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  11867. if (isset($subsetchars[$c])) {
  11868. $subsetglyphs[$startGlyphCode] = true;
  11869. }
  11870. ++$startGlyphCode;
  11871. }
  11872. }
  11873. break;
  11874. }
  11875. case 13: { // Format 13: Many-to-one range mappings
  11876. // to be implemented ...
  11877. break;
  11878. }
  11879. case 14: { // Format 14: Unicode Variation Sequences
  11880. // to be implemented ...
  11881. break;
  11882. }
  11883. }
  11884. }
  11885. // include all parts of composite glyphs
  11886. $new_sga = $subsetglyphs;
  11887. while (!empty($new_sga)) {
  11888. $sga = $new_sga;
  11889. $new_sga = array();
  11890. foreach ($sga as $key => $val) {
  11891. if (isset($indexToLoc[$key])) {
  11892. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  11893. $numberOfContours = $this->_getSHORT($font, $offset);
  11894. $offset += 2;
  11895. if ($numberOfContours < 0) { // composite glyph
  11896. $offset += 8; // skip xMin, yMin, xMax, yMax
  11897. do {
  11898. $flags = $this->_getUSHORT($font, $offset);
  11899. $offset += 2;
  11900. $glyphIndex = $this->_getUSHORT($font, $offset);
  11901. $offset += 2;
  11902. if (!isset($subsetglyphs[$glyphIndex])) {
  11903. // add missing glyphs
  11904. $new_sga[$glyphIndex] = true;
  11905. }
  11906. // skip some bytes by case
  11907. if ($flags & 1) {
  11908. $offset += 4;
  11909. } else {
  11910. $offset += 2;
  11911. }
  11912. if ($flags & 8) {
  11913. $offset += 2;
  11914. } elseif ($flags & 64) {
  11915. $offset += 4;
  11916. } elseif ($flags & 128) {
  11917. $offset += 8;
  11918. }
  11919. } while ($flags & 32);
  11920. }
  11921. }
  11922. }
  11923. $subsetglyphs += $new_sga;
  11924. }
  11925. // sort glyphs by key (and remove duplicates)
  11926. ksort($subsetglyphs);
  11927. // build new glyf and loca tables
  11928. $glyf = '';
  11929. $loca = '';
  11930. $offset = 0;
  11931. $glyf_offset = $table['glyf']['offset'];
  11932. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  11933. if (isset($subsetglyphs[$i])) {
  11934. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  11935. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  11936. } else {
  11937. $length = 0;
  11938. }
  11939. if ($short_offset) {
  11940. $loca .= pack('n', ($offset / 2));
  11941. } else {
  11942. $loca .= pack('N', $offset);
  11943. }
  11944. $offset += $length;
  11945. }
  11946. // array of table names to preserve (loca and glyf tables will be added later)
  11947. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  11948. $table_names = array('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  11949. // get the tables to preserve
  11950. $offset = 12;
  11951. foreach ($table as $tag => $val) {
  11952. if (in_array($tag, $table_names)) {
  11953. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  11954. if ($tag == 'head') {
  11955. // set the checkSumAdjustment to 0
  11956. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8) . "\x0\x0\x0\x0" . substr($table[$tag]['data'], 12);
  11957. }
  11958. $pad = 4 - ($table[$tag]['length'] % 4);
  11959. if ($pad != 4) {
  11960. // the length of a table must be a multiple of four bytes
  11961. $table[$tag]['length'] += $pad;
  11962. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  11963. }
  11964. $table[$tag]['offset'] = $offset;
  11965. $offset += $table[$tag]['length'];
  11966. // check sum is not changed (so keep the following line commented)
  11967. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  11968. } else {
  11969. unset($table[$tag]);
  11970. }
  11971. }
  11972. // add loca
  11973. $table['loca']['data'] = $loca;
  11974. $table['loca']['length'] = strlen($loca);
  11975. $pad = 4 - ($table['loca']['length'] % 4);
  11976. if ($pad != 4) {
  11977. // the length of a table must be a multiple of four bytes
  11978. $table['loca']['length'] += $pad;
  11979. $table['loca']['data'] .= str_repeat("\x0", $pad);
  11980. }
  11981. $table['loca']['offset'] = $offset;
  11982. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  11983. $offset += $table['loca']['length'];
  11984. // add glyf
  11985. $table['glyf']['data'] = $glyf;
  11986. $table['glyf']['length'] = strlen($glyf);
  11987. $pad = 4 - ($table['glyf']['length'] % 4);
  11988. if ($pad != 4) {
  11989. // the length of a table must be a multiple of four bytes
  11990. $table['glyf']['length'] += $pad;
  11991. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  11992. }
  11993. $table['glyf']['offset'] = $offset;
  11994. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  11995. // rebuild font
  11996. $font = '';
  11997. $font .= pack('N', 0x10000); // sfnt version
  11998. $numTables = count($table);
  11999. $font .= pack('n', $numTables); // numTables
  12000. $entrySelector = floor(log($numTables, 2));
  12001. $searchRange = pow(2, $entrySelector) * 16;
  12002. $rangeShift = ($numTables * 16) - $searchRange;
  12003. $font .= pack('n', $searchRange); // searchRange
  12004. $font .= pack('n', $entrySelector); // entrySelector
  12005. $font .= pack('n', $rangeShift); // rangeShift
  12006. $offset = ($numTables * 16);
  12007. foreach ($table as $tag => $data) {
  12008. $font .= $tag; // tag
  12009. $font .= pack('N', $data['checkSum']); // checkSum
  12010. $font .= pack('N', ($data['offset'] + $offset)); // offset
  12011. $font .= pack('N', $data['length']); // length
  12012. }
  12013. foreach ($table as $data) {
  12014. $font .= $data['data'];
  12015. }
  12016. // set checkSumAdjustment on head table
  12017. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  12018. $font = substr($font, 0, $table['head']['offset'] + 8) . pack('N', $checkSumAdjustment) . substr($font, $table['head']['offset'] + 12);
  12019. return $font;
  12020. }
  12021. /**
  12022. * Returs the checksum of a TTF table.
  12023. * @param $table (string) table to check
  12024. * @param $length (int) length of table in bytes
  12025. * @return int checksum
  12026. * @author Nicola Asuni
  12027. * @protected
  12028. * @since 5.2.000 (2010-06-02)
  12029. */
  12030. protected function _getTTFtableChecksum($table, $length) {
  12031. $sum = 0;
  12032. $tlen = ($length / 4);
  12033. $offset = 0;
  12034. for ($i = 0; $i < $tlen; ++$i) {
  12035. $v = unpack('Ni', substr($table, $offset, 4));
  12036. $sum += $v['i'];
  12037. $offset += 4;
  12038. }
  12039. $sum = unpack('Ni', pack('N', $sum));
  12040. return $sum['i'];
  12041. }
  12042. /**
  12043. * Outputs font widths
  12044. * @param $font (array) font data
  12045. * @param $cidoffset (int) offset for CID values
  12046. * @return PDF command string for font widths
  12047. * @author Nicola Asuni
  12048. * @protected
  12049. * @since 4.4.000 (2008-12-07)
  12050. */
  12051. protected function _putfontwidths($font, $cidoffset = 0) {
  12052. ksort($font['cw']);
  12053. $rangeid = 0;
  12054. $range = array();
  12055. $prevcid = -2;
  12056. $prevwidth = -1;
  12057. $interval = false;
  12058. // for each character
  12059. foreach ($font['cw'] as $cid => $width) {
  12060. $cid -= $cidoffset;
  12061. if ($font['subset'] AND (!isset($font['subsetchars'][$cid]))) {
  12062. // ignore the unused characters (font subsetting)
  12063. continue;
  12064. }
  12065. if ($width != $font['dw']) {
  12066. if ($cid == ($prevcid + 1)) {
  12067. // consecutive CID
  12068. if ($width == $prevwidth) {
  12069. if ($width == $range[$rangeid][0]) {
  12070. $range[$rangeid][] = $width;
  12071. } else {
  12072. array_pop($range[$rangeid]);
  12073. // new range
  12074. $rangeid = $prevcid;
  12075. $range[$rangeid] = array();
  12076. $range[$rangeid][] = $prevwidth;
  12077. $range[$rangeid][] = $width;
  12078. }
  12079. $interval = true;
  12080. $range[$rangeid]['interval'] = true;
  12081. } else {
  12082. if ($interval) {
  12083. // new range
  12084. $rangeid = $cid;
  12085. $range[$rangeid] = array();
  12086. $range[$rangeid][] = $width;
  12087. } else {
  12088. $range[$rangeid][] = $width;
  12089. }
  12090. $interval = false;
  12091. }
  12092. } else {
  12093. // new range
  12094. $rangeid = $cid;
  12095. $range[$rangeid] = array();
  12096. $range[$rangeid][] = $width;
  12097. $interval = false;
  12098. }
  12099. $prevcid = $cid;
  12100. $prevwidth = $width;
  12101. }
  12102. }
  12103. // optimize ranges
  12104. $prevk = -1;
  12105. $nextk = -1;
  12106. $prevint = false;
  12107. foreach ($range as $k => $ws) {
  12108. $cws = count($ws);
  12109. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  12110. if (isset($range[$k]['interval'])) {
  12111. unset($range[$k]['interval']);
  12112. }
  12113. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  12114. unset($range[$k]);
  12115. } else {
  12116. $prevk = $k;
  12117. }
  12118. $nextk = $k + $cws;
  12119. if (isset($ws['interval'])) {
  12120. if ($cws > 3) {
  12121. $prevint = true;
  12122. } else {
  12123. $prevint = false;
  12124. }
  12125. if (isset($range[$k]['interval'])) {
  12126. unset($range[$k]['interval']);
  12127. }
  12128. --$nextk;
  12129. } else {
  12130. $prevint = false;
  12131. }
  12132. }
  12133. // output data
  12134. $w = '';
  12135. foreach ($range as $k => $ws) {
  12136. if (count(array_count_values($ws)) == 1) {
  12137. // interval mode is more compact
  12138. $w .= ' ' . $k . ' ' . ($k + count($ws) - 1) . ' ' . $ws[0];
  12139. } else {
  12140. // range mode
  12141. $w .= ' ' . $k . ' [ ' . implode(' ', $ws) . ' ]';
  12142. }
  12143. }
  12144. return '/W [' . $w . ' ]';
  12145. }
  12146. /**
  12147. * Output fonts.
  12148. * @author Nicola Asuni
  12149. * @protected
  12150. */
  12151. protected function _putfonts() {
  12152. $nf = $this->n;
  12153. foreach ($this->diffs as $diff) {
  12154. //Encodings
  12155. $this->_newobj();
  12156. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences [' . $diff . '] >>' . "\n" . 'endobj');
  12157. }
  12158. $mqr = $this->get_mqr();
  12159. $this->set_mqr(false);
  12160. foreach ($this->FontFiles as $file => $info) {
  12161. // search and get font file to embedd
  12162. $fontdir = $info['fontdir'];
  12163. $file = strtolower($file);
  12164. $fontfile = '';
  12165. // search files on various directories
  12166. if (($fontdir !== false) AND file_exists($fontdir . $file)) {
  12167. $fontfile = $fontdir . $file;
  12168. } elseif (file_exists($this->_getfontpath() . $file)) {
  12169. $fontfile = $this->_getfontpath() . $file;
  12170. } elseif (file_exists($file)) {
  12171. $fontfile = $file;
  12172. }
  12173. if (!$this->empty_string($fontfile)) {
  12174. $font = file_get_contents($fontfile);
  12175. $compressed = (substr($file, -2) == '.z');
  12176. if ((!$compressed) AND (isset($info['length2']))) {
  12177. $header = (ord($font{0}) == 128);
  12178. if ($header) {
  12179. // strip first binary header
  12180. $font = substr($font, 6);
  12181. }
  12182. if ($header AND (ord($font[$info['length1']]) == 128)) {
  12183. // strip second binary header
  12184. $font = substr($font, 0, $info['length1']) . substr($font, ($info['length1'] + 6));
  12185. }
  12186. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  12187. if ($compressed) {
  12188. // uncompress font
  12189. $font = gzuncompress($font);
  12190. }
  12191. // merge subset characters
  12192. $subsetchars = array(); // used chars
  12193. foreach ($info['fontkeys'] as $fontkey) {
  12194. $fontinfo = $this->getFontBuffer($fontkey);
  12195. $subsetchars += $fontinfo['subsetchars'];
  12196. }
  12197. // rebuild a font subset
  12198. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  12199. // calculate new font length
  12200. $info['length1'] = strlen($font);
  12201. if ($compressed) {
  12202. // recompress font
  12203. $font = gzcompress($font);
  12204. }
  12205. }
  12206. $this->_newobj();
  12207. $this->FontFiles[$file]['n'] = $this->n;
  12208. $stream = $this->_getrawstream($font);
  12209. $out = '<< /Length ' . strlen($stream);
  12210. if ($compressed) {
  12211. $out .= ' /Filter /FlateDecode';
  12212. }
  12213. $out .= ' /Length1 ' . $info['length1'];
  12214. if (isset($info['length2'])) {
  12215. $out .= ' /Length2 ' . $info['length2'] . ' /Length3 0';
  12216. }
  12217. $out .= ' >>';
  12218. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  12219. $out .= "\n" . 'endobj';
  12220. $this->_out($out);
  12221. }
  12222. }
  12223. $this->set_mqr($mqr);
  12224. foreach ($this->fontkeys as $k) {
  12225. //Font objects
  12226. $font = $this->getFontBuffer($k);
  12227. $type = $font['type'];
  12228. $name = $font['name'];
  12229. if ($type == 'core') {
  12230. // standard core font
  12231. $out = $this->_getobj($this->font_obj_ids[$k]) . "\n";
  12232. $out .= '<</Type /Font';
  12233. $out .= ' /Subtype /Type1';
  12234. $out .= ' /BaseFont /' . $name;
  12235. $out .= ' /Name /F' . $font['i'];
  12236. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  12237. $out .= ' /Encoding /WinAnsiEncoding';
  12238. }
  12239. if ($k == 'helvetica') {
  12240. // add default font for annotations
  12241. $this->annotation_fonts[$k] = $font['i'];
  12242. }
  12243. $out .= ' >>';
  12244. $out .= "\n" . 'endobj';
  12245. $this->_out($out);
  12246. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  12247. // additional Type1 or TrueType font
  12248. $out = $this->_getobj($this->font_obj_ids[$k]) . "\n";
  12249. $out .= '<</Type /Font';
  12250. $out .= ' /Subtype /' . $type;
  12251. $out .= ' /BaseFont /' . $name;
  12252. $out .= ' /Name /F' . $font['i'];
  12253. $out .= ' /FirstChar 32 /LastChar 255';
  12254. $out .= ' /Widths ' . ($this->n + 1) . ' 0 R';
  12255. $out .= ' /FontDescriptor ' . ($this->n + 2) . ' 0 R';
  12256. if ($font['enc']) {
  12257. if (isset($font['diff'])) {
  12258. $out .= ' /Encoding ' . ($nf + $font['diff']) . ' 0 R';
  12259. } else {
  12260. $out .= ' /Encoding /WinAnsiEncoding';
  12261. }
  12262. }
  12263. $out .= ' >>';
  12264. $out .= "\n" . 'endobj';
  12265. $this->_out($out);
  12266. // Widths
  12267. $this->_newobj();
  12268. $s = '[';
  12269. for ($i = 32; $i < 256; ++$i) {
  12270. if (isset($font['cw'][$i])) {
  12271. $s .= $font['cw'][$i] . ' ';
  12272. } else {
  12273. $s .= $font['dw'] . ' ';
  12274. }
  12275. }
  12276. $s .= ']';
  12277. $s .= "\n" . 'endobj';
  12278. $this->_out($s);
  12279. //Descriptor
  12280. $this->_newobj();
  12281. $s = '<</Type /FontDescriptor /FontName /' . $name;
  12282. foreach ($font['desc'] as $fdk => $fdv) {
  12283. if (is_float($fdv)) {
  12284. $fdv = sprintf('%F', $fdv);
  12285. }
  12286. $s .= ' /' . $fdk . ' ' . $fdv . '';
  12287. }
  12288. if (!$this->empty_string($font['file'])) {
  12289. $s .= ' /FontFile' . ($type == 'Type1' ? '' : '2') . ' ' . $this->FontFiles[$font['file']]['n'] . ' 0 R';
  12290. }
  12291. $s .= '>>';
  12292. $s .= "\n" . 'endobj';
  12293. $this->_out($s);
  12294. } else {
  12295. // additional types
  12296. $mtd = '_put' . strtolower($type);
  12297. if (!method_exists($this, $mtd)) {
  12298. $this->Error('Unsupported font type: ' . $type);
  12299. }
  12300. $this->$mtd($font);
  12301. }
  12302. }
  12303. }
  12304. /**
  12305. * Adds unicode fonts.<br>
  12306. * Based on PDF Reference 1.3 (section 5)
  12307. * @param $font (array) font data
  12308. * @protected
  12309. * @author Nicola Asuni
  12310. * @since 1.52.0.TC005 (2005-01-05)
  12311. */
  12312. protected function _puttruetypeunicode($font) {
  12313. $fontname = '';
  12314. if ($font['subset']) {
  12315. // change name for font subsetting
  12316. $subtag = sprintf('%06u', $font['i']);
  12317. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  12318. $fontname .= $subtag . '+';
  12319. }
  12320. $fontname .= $font['name'];
  12321. // Type0 Font
  12322. // A composite font composed of other fonts, organized hierarchically
  12323. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']]) . "\n";
  12324. $out .= '<< /Type /Font';
  12325. $out .= ' /Subtype /Type0';
  12326. $out .= ' /BaseFont /' . $fontname;
  12327. $out .= ' /Name /F' . $font['i'];
  12328. $out .= ' /Encoding /' . $font['enc'];
  12329. $out .= ' /ToUnicode ' . ($this->n + 1) . ' 0 R';
  12330. $out .= ' /DescendantFonts [' . ($this->n + 2) . ' 0 R]';
  12331. $out .= ' >>';
  12332. $out .= "\n" . 'endobj';
  12333. $this->_out($out);
  12334. // ToUnicode map for Identity-H
  12335. $stream = "/CIDInit /ProcSet findresource begin\n";
  12336. $stream .= "12 dict begin\n";
  12337. $stream .= "begincmap\n";
  12338. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  12339. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  12340. $stream .= "/CMapType 2 def\n";
  12341. $stream .= "/WMode 0 def\n";
  12342. $stream .= "1 begincodespacerange\n";
  12343. $stream .= "<0000> <FFFF>\n";
  12344. $stream .= "endcodespacerange\n";
  12345. $stream .= "100 beginbfrange\n";
  12346. $stream .= "<0000> <00ff> <0000>\n";
  12347. $stream .= "<0100> <01ff> <0100>\n";
  12348. $stream .= "<0200> <02ff> <0200>\n";
  12349. $stream .= "<0300> <03ff> <0300>\n";
  12350. $stream .= "<0400> <04ff> <0400>\n";
  12351. $stream .= "<0500> <05ff> <0500>\n";
  12352. $stream .= "<0600> <06ff> <0600>\n";
  12353. $stream .= "<0700> <07ff> <0700>\n";
  12354. $stream .= "<0800> <08ff> <0800>\n";
  12355. $stream .= "<0900> <09ff> <0900>\n";
  12356. $stream .= "<0a00> <0aff> <0a00>\n";
  12357. $stream .= "<0b00> <0bff> <0b00>\n";
  12358. $stream .= "<0c00> <0cff> <0c00>\n";
  12359. $stream .= "<0d00> <0dff> <0d00>\n";
  12360. $stream .= "<0e00> <0eff> <0e00>\n";
  12361. $stream .= "<0f00> <0fff> <0f00>\n";
  12362. $stream .= "<1000> <10ff> <1000>\n";
  12363. $stream .= "<1100> <11ff> <1100>\n";
  12364. $stream .= "<1200> <12ff> <1200>\n";
  12365. $stream .= "<1300> <13ff> <1300>\n";
  12366. $stream .= "<1400> <14ff> <1400>\n";
  12367. $stream .= "<1500> <15ff> <1500>\n";
  12368. $stream .= "<1600> <16ff> <1600>\n";
  12369. $stream .= "<1700> <17ff> <1700>\n";
  12370. $stream .= "<1800> <18ff> <1800>\n";
  12371. $stream .= "<1900> <19ff> <1900>\n";
  12372. $stream .= "<1a00> <1aff> <1a00>\n";
  12373. $stream .= "<1b00> <1bff> <1b00>\n";
  12374. $stream .= "<1c00> <1cff> <1c00>\n";
  12375. $stream .= "<1d00> <1dff> <1d00>\n";
  12376. $stream .= "<1e00> <1eff> <1e00>\n";
  12377. $stream .= "<1f00> <1fff> <1f00>\n";
  12378. $stream .= "<2000> <20ff> <2000>\n";
  12379. $stream .= "<2100> <21ff> <2100>\n";
  12380. $stream .= "<2200> <22ff> <2200>\n";
  12381. $stream .= "<2300> <23ff> <2300>\n";
  12382. $stream .= "<2400> <24ff> <2400>\n";
  12383. $stream .= "<2500> <25ff> <2500>\n";
  12384. $stream .= "<2600> <26ff> <2600>\n";
  12385. $stream .= "<2700> <27ff> <2700>\n";
  12386. $stream .= "<2800> <28ff> <2800>\n";
  12387. $stream .= "<2900> <29ff> <2900>\n";
  12388. $stream .= "<2a00> <2aff> <2a00>\n";
  12389. $stream .= "<2b00> <2bff> <2b00>\n";
  12390. $stream .= "<2c00> <2cff> <2c00>\n";
  12391. $stream .= "<2d00> <2dff> <2d00>\n";
  12392. $stream .= "<2e00> <2eff> <2e00>\n";
  12393. $stream .= "<2f00> <2fff> <2f00>\n";
  12394. $stream .= "<3000> <30ff> <3000>\n";
  12395. $stream .= "<3100> <31ff> <3100>\n";
  12396. $stream .= "<3200> <32ff> <3200>\n";
  12397. $stream .= "<3300> <33ff> <3300>\n";
  12398. $stream .= "<3400> <34ff> <3400>\n";
  12399. $stream .= "<3500> <35ff> <3500>\n";
  12400. $stream .= "<3600> <36ff> <3600>\n";
  12401. $stream .= "<3700> <37ff> <3700>\n";
  12402. $stream .= "<3800> <38ff> <3800>\n";
  12403. $stream .= "<3900> <39ff> <3900>\n";
  12404. $stream .= "<3a00> <3aff> <3a00>\n";
  12405. $stream .= "<3b00> <3bff> <3b00>\n";
  12406. $stream .= "<3c00> <3cff> <3c00>\n";
  12407. $stream .= "<3d00> <3dff> <3d00>\n";
  12408. $stream .= "<3e00> <3eff> <3e00>\n";
  12409. $stream .= "<3f00> <3fff> <3f00>\n";
  12410. $stream .= "<4000> <40ff> <4000>\n";
  12411. $stream .= "<4100> <41ff> <4100>\n";
  12412. $stream .= "<4200> <42ff> <4200>\n";
  12413. $stream .= "<4300> <43ff> <4300>\n";
  12414. $stream .= "<4400> <44ff> <4400>\n";
  12415. $stream .= "<4500> <45ff> <4500>\n";
  12416. $stream .= "<4600> <46ff> <4600>\n";
  12417. $stream .= "<4700> <47ff> <4700>\n";
  12418. $stream .= "<4800> <48ff> <4800>\n";
  12419. $stream .= "<4900> <49ff> <4900>\n";
  12420. $stream .= "<4a00> <4aff> <4a00>\n";
  12421. $stream .= "<4b00> <4bff> <4b00>\n";
  12422. $stream .= "<4c00> <4cff> <4c00>\n";
  12423. $stream .= "<4d00> <4dff> <4d00>\n";
  12424. $stream .= "<4e00> <4eff> <4e00>\n";
  12425. $stream .= "<4f00> <4fff> <4f00>\n";
  12426. $stream .= "<5000> <50ff> <5000>\n";
  12427. $stream .= "<5100> <51ff> <5100>\n";
  12428. $stream .= "<5200> <52ff> <5200>\n";
  12429. $stream .= "<5300> <53ff> <5300>\n";
  12430. $stream .= "<5400> <54ff> <5400>\n";
  12431. $stream .= "<5500> <55ff> <5500>\n";
  12432. $stream .= "<5600> <56ff> <5600>\n";
  12433. $stream .= "<5700> <57ff> <5700>\n";
  12434. $stream .= "<5800> <58ff> <5800>\n";
  12435. $stream .= "<5900> <59ff> <5900>\n";
  12436. $stream .= "<5a00> <5aff> <5a00>\n";
  12437. $stream .= "<5b00> <5bff> <5b00>\n";
  12438. $stream .= "<5c00> <5cff> <5c00>\n";
  12439. $stream .= "<5d00> <5dff> <5d00>\n";
  12440. $stream .= "<5e00> <5eff> <5e00>\n";
  12441. $stream .= "<5f00> <5fff> <5f00>\n";
  12442. $stream .= "<6000> <60ff> <6000>\n";
  12443. $stream .= "<6100> <61ff> <6100>\n";
  12444. $stream .= "<6200> <62ff> <6200>\n";
  12445. $stream .= "<6300> <63ff> <6300>\n";
  12446. $stream .= "endbfrange\n";
  12447. $stream .= "100 beginbfrange\n";
  12448. $stream .= "<6400> <64ff> <6400>\n";
  12449. $stream .= "<6500> <65ff> <6500>\n";
  12450. $stream .= "<6600> <66ff> <6600>\n";
  12451. $stream .= "<6700> <67ff> <6700>\n";
  12452. $stream .= "<6800> <68ff> <6800>\n";
  12453. $stream .= "<6900> <69ff> <6900>\n";
  12454. $stream .= "<6a00> <6aff> <6a00>\n";
  12455. $stream .= "<6b00> <6bff> <6b00>\n";
  12456. $stream .= "<6c00> <6cff> <6c00>\n";
  12457. $stream .= "<6d00> <6dff> <6d00>\n";
  12458. $stream .= "<6e00> <6eff> <6e00>\n";
  12459. $stream .= "<6f00> <6fff> <6f00>\n";
  12460. $stream .= "<7000> <70ff> <7000>\n";
  12461. $stream .= "<7100> <71ff> <7100>\n";
  12462. $stream .= "<7200> <72ff> <7200>\n";
  12463. $stream .= "<7300> <73ff> <7300>\n";
  12464. $stream .= "<7400> <74ff> <7400>\n";
  12465. $stream .= "<7500> <75ff> <7500>\n";
  12466. $stream .= "<7600> <76ff> <7600>\n";
  12467. $stream .= "<7700> <77ff> <7700>\n";
  12468. $stream .= "<7800> <78ff> <7800>\n";
  12469. $stream .= "<7900> <79ff> <7900>\n";
  12470. $stream .= "<7a00> <7aff> <7a00>\n";
  12471. $stream .= "<7b00> <7bff> <7b00>\n";
  12472. $stream .= "<7c00> <7cff> <7c00>\n";
  12473. $stream .= "<7d00> <7dff> <7d00>\n";
  12474. $stream .= "<7e00> <7eff> <7e00>\n";
  12475. $stream .= "<7f00> <7fff> <7f00>\n";
  12476. $stream .= "<8000> <80ff> <8000>\n";
  12477. $stream .= "<8100> <81ff> <8100>\n";
  12478. $stream .= "<8200> <82ff> <8200>\n";
  12479. $stream .= "<8300> <83ff> <8300>\n";
  12480. $stream .= "<8400> <84ff> <8400>\n";
  12481. $stream .= "<8500> <85ff> <8500>\n";
  12482. $stream .= "<8600> <86ff> <8600>\n";
  12483. $stream .= "<8700> <87ff> <8700>\n";
  12484. $stream .= "<8800> <88ff> <8800>\n";
  12485. $stream .= "<8900> <89ff> <8900>\n";
  12486. $stream .= "<8a00> <8aff> <8a00>\n";
  12487. $stream .= "<8b00> <8bff> <8b00>\n";
  12488. $stream .= "<8c00> <8cff> <8c00>\n";
  12489. $stream .= "<8d00> <8dff> <8d00>\n";
  12490. $stream .= "<8e00> <8eff> <8e00>\n";
  12491. $stream .= "<8f00> <8fff> <8f00>\n";
  12492. $stream .= "<9000> <90ff> <9000>\n";
  12493. $stream .= "<9100> <91ff> <9100>\n";
  12494. $stream .= "<9200> <92ff> <9200>\n";
  12495. $stream .= "<9300> <93ff> <9300>\n";
  12496. $stream .= "<9400> <94ff> <9400>\n";
  12497. $stream .= "<9500> <95ff> <9500>\n";
  12498. $stream .= "<9600> <96ff> <9600>\n";
  12499. $stream .= "<9700> <97ff> <9700>\n";
  12500. $stream .= "<9800> <98ff> <9800>\n";
  12501. $stream .= "<9900> <99ff> <9900>\n";
  12502. $stream .= "<9a00> <9aff> <9a00>\n";
  12503. $stream .= "<9b00> <9bff> <9b00>\n";
  12504. $stream .= "<9c00> <9cff> <9c00>\n";
  12505. $stream .= "<9d00> <9dff> <9d00>\n";
  12506. $stream .= "<9e00> <9eff> <9e00>\n";
  12507. $stream .= "<9f00> <9fff> <9f00>\n";
  12508. $stream .= "<a000> <a0ff> <a000>\n";
  12509. $stream .= "<a100> <a1ff> <a100>\n";
  12510. $stream .= "<a200> <a2ff> <a200>\n";
  12511. $stream .= "<a300> <a3ff> <a300>\n";
  12512. $stream .= "<a400> <a4ff> <a400>\n";
  12513. $stream .= "<a500> <a5ff> <a500>\n";
  12514. $stream .= "<a600> <a6ff> <a600>\n";
  12515. $stream .= "<a700> <a7ff> <a700>\n";
  12516. $stream .= "<a800> <a8ff> <a800>\n";
  12517. $stream .= "<a900> <a9ff> <a900>\n";
  12518. $stream .= "<aa00> <aaff> <aa00>\n";
  12519. $stream .= "<ab00> <abff> <ab00>\n";
  12520. $stream .= "<ac00> <acff> <ac00>\n";
  12521. $stream .= "<ad00> <adff> <ad00>\n";
  12522. $stream .= "<ae00> <aeff> <ae00>\n";
  12523. $stream .= "<af00> <afff> <af00>\n";
  12524. $stream .= "<b000> <b0ff> <b000>\n";
  12525. $stream .= "<b100> <b1ff> <b100>\n";
  12526. $stream .= "<b200> <b2ff> <b200>\n";
  12527. $stream .= "<b300> <b3ff> <b300>\n";
  12528. $stream .= "<b400> <b4ff> <b400>\n";
  12529. $stream .= "<b500> <b5ff> <b500>\n";
  12530. $stream .= "<b600> <b6ff> <b600>\n";
  12531. $stream .= "<b700> <b7ff> <b700>\n";
  12532. $stream .= "<b800> <b8ff> <b800>\n";
  12533. $stream .= "<b900> <b9ff> <b900>\n";
  12534. $stream .= "<ba00> <baff> <ba00>\n";
  12535. $stream .= "<bb00> <bbff> <bb00>\n";
  12536. $stream .= "<bc00> <bcff> <bc00>\n";
  12537. $stream .= "<bd00> <bdff> <bd00>\n";
  12538. $stream .= "<be00> <beff> <be00>\n";
  12539. $stream .= "<bf00> <bfff> <bf00>\n";
  12540. $stream .= "<c000> <c0ff> <c000>\n";
  12541. $stream .= "<c100> <c1ff> <c100>\n";
  12542. $stream .= "<c200> <c2ff> <c200>\n";
  12543. $stream .= "<c300> <c3ff> <c300>\n";
  12544. $stream .= "<c400> <c4ff> <c400>\n";
  12545. $stream .= "<c500> <c5ff> <c500>\n";
  12546. $stream .= "<c600> <c6ff> <c600>\n";
  12547. $stream .= "<c700> <c7ff> <c700>\n";
  12548. $stream .= "endbfrange\n";
  12549. $stream .= "56 beginbfrange\n";
  12550. $stream .= "<c800> <c8ff> <c800>\n";
  12551. $stream .= "<c900> <c9ff> <c900>\n";
  12552. $stream .= "<ca00> <caff> <ca00>\n";
  12553. $stream .= "<cb00> <cbff> <cb00>\n";
  12554. $stream .= "<cc00> <ccff> <cc00>\n";
  12555. $stream .= "<cd00> <cdff> <cd00>\n";
  12556. $stream .= "<ce00> <ceff> <ce00>\n";
  12557. $stream .= "<cf00> <cfff> <cf00>\n";
  12558. $stream .= "<d000> <d0ff> <d000>\n";
  12559. $stream .= "<d100> <d1ff> <d100>\n";
  12560. $stream .= "<d200> <d2ff> <d200>\n";
  12561. $stream .= "<d300> <d3ff> <d300>\n";
  12562. $stream .= "<d400> <d4ff> <d400>\n";
  12563. $stream .= "<d500> <d5ff> <d500>\n";
  12564. $stream .= "<d600> <d6ff> <d600>\n";
  12565. $stream .= "<d700> <d7ff> <d700>\n";
  12566. $stream .= "<d800> <d8ff> <d800>\n";
  12567. $stream .= "<d900> <d9ff> <d900>\n";
  12568. $stream .= "<da00> <daff> <da00>\n";
  12569. $stream .= "<db00> <dbff> <db00>\n";
  12570. $stream .= "<dc00> <dcff> <dc00>\n";
  12571. $stream .= "<dd00> <ddff> <dd00>\n";
  12572. $stream .= "<de00> <deff> <de00>\n";
  12573. $stream .= "<df00> <dfff> <df00>\n";
  12574. $stream .= "<e000> <e0ff> <e000>\n";
  12575. $stream .= "<e100> <e1ff> <e100>\n";
  12576. $stream .= "<e200> <e2ff> <e200>\n";
  12577. $stream .= "<e300> <e3ff> <e300>\n";
  12578. $stream .= "<e400> <e4ff> <e400>\n";
  12579. $stream .= "<e500> <e5ff> <e500>\n";
  12580. $stream .= "<e600> <e6ff> <e600>\n";
  12581. $stream .= "<e700> <e7ff> <e700>\n";
  12582. $stream .= "<e800> <e8ff> <e800>\n";
  12583. $stream .= "<e900> <e9ff> <e900>\n";
  12584. $stream .= "<ea00> <eaff> <ea00>\n";
  12585. $stream .= "<eb00> <ebff> <eb00>\n";
  12586. $stream .= "<ec00> <ecff> <ec00>\n";
  12587. $stream .= "<ed00> <edff> <ed00>\n";
  12588. $stream .= "<ee00> <eeff> <ee00>\n";
  12589. $stream .= "<ef00> <efff> <ef00>\n";
  12590. $stream .= "<f000> <f0ff> <f000>\n";
  12591. $stream .= "<f100> <f1ff> <f100>\n";
  12592. $stream .= "<f200> <f2ff> <f200>\n";
  12593. $stream .= "<f300> <f3ff> <f300>\n";
  12594. $stream .= "<f400> <f4ff> <f400>\n";
  12595. $stream .= "<f500> <f5ff> <f500>\n";
  12596. $stream .= "<f600> <f6ff> <f600>\n";
  12597. $stream .= "<f700> <f7ff> <f700>\n";
  12598. $stream .= "<f800> <f8ff> <f800>\n";
  12599. $stream .= "<f900> <f9ff> <f900>\n";
  12600. $stream .= "<fa00> <faff> <fa00>\n";
  12601. $stream .= "<fb00> <fbff> <fb00>\n";
  12602. $stream .= "<fc00> <fcff> <fc00>\n";
  12603. $stream .= "<fd00> <fdff> <fd00>\n";
  12604. $stream .= "<fe00> <feff> <fe00>\n";
  12605. $stream .= "<ff00> <ffff> <ff00>\n";
  12606. $stream .= "endbfrange\n";
  12607. $stream .= "endcmap\n";
  12608. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  12609. $stream .= "end\n";
  12610. $stream .= "end";
  12611. // ToUnicode Object
  12612. $this->_newobj();
  12613. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  12614. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  12615. $stream = $this->_getrawstream($stream);
  12616. $this->_out('<<' . $filter . '/Length ' . strlen($stream) . '>> stream' . "\n" . $stream . "\n" . 'endstream' . "\n" . 'endobj');
  12617. // CIDFontType2
  12618. // A CIDFont whose glyph descriptions are based on TrueType font technology
  12619. $oid = $this->_newobj();
  12620. $out = '<< /Type /Font';
  12621. $out .= ' /Subtype /CIDFontType2';
  12622. $out .= ' /BaseFont /' . $fontname;
  12623. // A dictionary containing entries that define the character collection of the CIDFont.
  12624. $cidinfo = '/Registry ' . $this->_datastring($font['cidinfo']['Registry'], $oid);
  12625. $cidinfo .= ' /Ordering ' . $this->_datastring($font['cidinfo']['Ordering'], $oid);
  12626. $cidinfo .= ' /Supplement ' . $font['cidinfo']['Supplement'];
  12627. $out .= ' /CIDSystemInfo << ' . $cidinfo . ' >>';
  12628. $out .= ' /FontDescriptor ' . ($this->n + 1) . ' 0 R';
  12629. $out .= ' /DW ' . $font['dw']; // default width
  12630. $out .= "\n" . $this->_putfontwidths($font, 0);
  12631. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  12632. $out .= "\n" . '/CIDToGIDMap ' . ($this->n + 2) . ' 0 R';
  12633. }
  12634. $out .= ' >>';
  12635. $out .= "\n" . 'endobj';
  12636. $this->_out($out);
  12637. // Font descriptor
  12638. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  12639. $this->_newobj();
  12640. $out = '<< /Type /FontDescriptor';
  12641. $out .= ' /FontName /' . $fontname;
  12642. foreach ($font['desc'] as $key => $value) {
  12643. if (is_float($value)) {
  12644. $value = sprintf('%F', $value);
  12645. }
  12646. $out .= ' /' . $key . ' ' . $value;
  12647. }
  12648. $fontdir = false;
  12649. if (!$this->empty_string($font['file'])) {
  12650. // A stream containing a TrueType font
  12651. $out .= ' /FontFile2 ' . $this->FontFiles[$font['file']]['n'] . ' 0 R';
  12652. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  12653. }
  12654. $out .= ' >>';
  12655. $out .= "\n" . 'endobj';
  12656. $this->_out($out);
  12657. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  12658. $this->_newobj();
  12659. // Embed CIDToGIDMap
  12660. // A specification of the mapping from CIDs to glyph indices
  12661. // search and get CTG font file to embedd
  12662. $ctgfile = strtolower($font['ctg']);
  12663. // search and get ctg font file to embedd
  12664. $fontfile = '';
  12665. // search files on various directories
  12666. if (($fontdir !== false) AND file_exists($fontdir . $ctgfile)) {
  12667. $fontfile = $fontdir . $ctgfile;
  12668. } elseif (file_exists($this->_getfontpath() . $ctgfile)) {
  12669. $fontfile = $this->_getfontpath() . $ctgfile;
  12670. } elseif (file_exists($ctgfile)) {
  12671. $fontfile = $ctgfile;
  12672. }
  12673. if ($this->empty_string($fontfile)) {
  12674. $this->Error('Font file not found: ' . $ctgfile);
  12675. }
  12676. $stream = $this->_getrawstream(file_get_contents($fontfile));
  12677. $out = '<< /Length ' . strlen($stream) . '';
  12678. if (substr($fontfile, -2) == '.z') { // check file extension
  12679. // Decompresses data encoded using the public-domain
  12680. // zlib/deflate compression method, reproducing the
  12681. // original text or binary data
  12682. $out .= ' /Filter /FlateDecode';
  12683. }
  12684. $out .= ' >>';
  12685. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  12686. $out .= "\n" . 'endobj';
  12687. $this->_out($out);
  12688. }
  12689. }
  12690. /**
  12691. * Output CID-0 fonts.
  12692. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  12693. * @param $font (array) font data
  12694. * @protected
  12695. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  12696. * @since 3.2.000 (2008-06-23)
  12697. */
  12698. protected function _putcidfont0($font) {
  12699. $cidoffset = 0;
  12700. if (!isset($font['cw'][1])) {
  12701. $cidoffset = 31;
  12702. }
  12703. if (isset($font['cidinfo']['uni2cid'])) {
  12704. // convert unicode to cid.
  12705. $uni2cid = $font['cidinfo']['uni2cid'];
  12706. $cw = array();
  12707. foreach ($font['cw'] as $uni => $width) {
  12708. if (isset($uni2cid[$uni])) {
  12709. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  12710. } elseif ($uni < 256) {
  12711. $cw[$uni] = $width;
  12712. } // else unknown character
  12713. }
  12714. $font = array_merge($font, array('cw' => $cw));
  12715. }
  12716. $name = $font['name'];
  12717. $enc = $font['enc'];
  12718. if ($enc) {
  12719. $longname = $name . '-' . $enc;
  12720. } else {
  12721. $longname = $name;
  12722. }
  12723. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']]) . "\n";
  12724. $out .= '<</Type /Font';
  12725. $out .= ' /Subtype /Type0';
  12726. $out .= ' /BaseFont /' . $longname;
  12727. $out .= ' /Name /F' . $font['i'];
  12728. if ($enc) {
  12729. $out .= ' /Encoding /' . $enc;
  12730. }
  12731. $out .= ' /DescendantFonts [' . ($this->n + 1) . ' 0 R]';
  12732. $out .= ' >>';
  12733. $out .= "\n" . 'endobj';
  12734. $this->_out($out);
  12735. $oid = $this->_newobj();
  12736. $out = '<</Type /Font';
  12737. $out .= ' /Subtype /CIDFontType0';
  12738. $out .= ' /BaseFont /' . $name;
  12739. $cidinfo = '/Registry ' . $this->_datastring($font['cidinfo']['Registry'], $oid);
  12740. $cidinfo .= ' /Ordering ' . $this->_datastring($font['cidinfo']['Ordering'], $oid);
  12741. $cidinfo .= ' /Supplement ' . $font['cidinfo']['Supplement'];
  12742. $out .= ' /CIDSystemInfo <<' . $cidinfo . '>>';
  12743. $out .= ' /FontDescriptor ' . ($this->n + 1) . ' 0 R';
  12744. $out .= ' /DW ' . $font['dw'];
  12745. $out .= "\n" . $this->_putfontwidths($font, $cidoffset);
  12746. $out .= ' >>';
  12747. $out .= "\n" . 'endobj';
  12748. $this->_out($out);
  12749. $this->_newobj();
  12750. $s = '<</Type /FontDescriptor /FontName /' . $name;
  12751. foreach ($font['desc'] as $k => $v) {
  12752. if ($k != 'Style') {
  12753. if (is_float($v)) {
  12754. $v = sprintf('%F', $v);
  12755. }
  12756. $s .= ' /' . $k . ' ' . $v . '';
  12757. }
  12758. }
  12759. $s .= '>>';
  12760. $s .= "\n" . 'endobj';
  12761. $this->_out($s);
  12762. }
  12763. /**
  12764. * Output images.
  12765. * @protected
  12766. */
  12767. protected function _putimages() {
  12768. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  12769. foreach ($this->imagekeys as $file) {
  12770. $info = $this->getImageBuffer($file);
  12771. // set object for alternate images array
  12772. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  12773. $altoid = $this->_newobj();
  12774. $out = '[';
  12775. foreach ($info['altimgs'] as $altimage) {
  12776. if (isset($this->xobjects['I' . $altimage[0]]['n'])) {
  12777. $out .= ' << /Image ' . $this->xobjects['I' . $altimage[0]]['n'] . ' 0 R';
  12778. $out .= ' /DefaultForPrinting';
  12779. if ($altimage[1] === true) {
  12780. $out .= ' true';
  12781. } else {
  12782. $out .= ' false';
  12783. }
  12784. $out .= ' >>';
  12785. }
  12786. }
  12787. $out .= ' ]';
  12788. $out .= "\n" . 'endobj';
  12789. $this->_out($out);
  12790. }
  12791. // set image object
  12792. $oid = $this->_newobj();
  12793. $this->xobjects['I' . $info['i']] = array('n' => $oid);
  12794. $this->setImageSubBuffer($file, 'n', $this->n);
  12795. $out = '<</Type /XObject';
  12796. $out .= ' /Subtype /Image';
  12797. $out .= ' /Width ' . $info['w'];
  12798. $out .= ' /Height ' . $info['h'];
  12799. if (array_key_exists('masked', $info)) {
  12800. $out .= ' /SMask ' . ($this->n - 1) . ' 0 R';
  12801. }
  12802. // set color space
  12803. $icc = false;
  12804. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  12805. // ICC Colour Space
  12806. $icc = true;
  12807. $out .= ' /ColorSpace [/ICCBased ' . ($this->n + 1) . ' 0 R]';
  12808. } elseif ($info['cs'] == 'Indexed') {
  12809. // Indexed Colour Space
  12810. $out .= ' /ColorSpace [/Indexed /DeviceRGB ' . ((strlen($info['pal']) / 3) - 1) . ' ' . ($this->n + 1) . ' 0 R]';
  12811. } else {
  12812. // Device Colour Space
  12813. $out .= ' /ColorSpace /' . $info['cs'];
  12814. }
  12815. if ($info['cs'] == 'DeviceCMYK') {
  12816. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  12817. }
  12818. $out .= ' /BitsPerComponent ' . $info['bpc'];
  12819. if (isset($altoid) AND ($altoid > 0)) {
  12820. // reference to alternate images dictionary
  12821. $out .= ' /Alternates ' . $altoid . ' 0 R';
  12822. }
  12823. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  12824. // external stream
  12825. $out .= ' /Length 0';
  12826. $out .= ' /F << /FS /URL /F ' . $this->_datastring($info['exurl'], $oid) . ' >>';
  12827. if (isset($info['f'])) {
  12828. $out .= ' /FFilter /' . $info['f'];
  12829. }
  12830. $out .= ' >>';
  12831. $out .= ' stream' . "\n" . 'endstream';
  12832. } else {
  12833. if (isset($info['f'])) {
  12834. $out .= ' /Filter /' . $info['f'];
  12835. }
  12836. if (isset($info['parms'])) {
  12837. $out .= ' ' . $info['parms'];
  12838. }
  12839. if (isset($info['trns']) AND is_array($info['trns'])) {
  12840. $trns = '';
  12841. $count_info = count($info['trns']);
  12842. for ($i = 0; $i < $count_info; ++$i) {
  12843. $trns .= $info['trns'][$i] . ' ' . $info['trns'][$i] . ' ';
  12844. }
  12845. $out .= ' /Mask [' . $trns . ']';
  12846. }
  12847. $stream = $this->_getrawstream($info['data']);
  12848. $out .= ' /Length ' . strlen($stream) . ' >>';
  12849. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  12850. }
  12851. $out .= "\n" . 'endobj';
  12852. $this->_out($out);
  12853. if ($icc) {
  12854. // ICC colour profile
  12855. $this->_newobj();
  12856. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  12857. $icc = $this->_getrawstream($icc);
  12858. $this->_out('<</N ' . $info['ch'] . ' /Alternate /' . $info['cs'] . ' ' . $filter . '/Length ' . strlen($icc) . '>> stream' . "\n" . $icc . "\n" . 'endstream' . "\n" . 'endobj');
  12859. } elseif ($info['cs'] == 'Indexed') {
  12860. // colour palette
  12861. $this->_newobj();
  12862. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  12863. $pal = $this->_getrawstream($pal);
  12864. $this->_out('<<' . $filter . '/Length ' . strlen($pal) . '>> stream' . "\n" . $pal . "\n" . 'endstream' . "\n" . 'endobj');
  12865. }
  12866. }
  12867. }
  12868. /**
  12869. * Output Form XObjects Templates.
  12870. * @author Nicola Asuni
  12871. * @since 5.8.017 (2010-08-24)
  12872. * @protected
  12873. * @see startTemplate(), endTemplate(), printTemplate()
  12874. */
  12875. protected function _putxobjects() {
  12876. foreach ($this->xobjects as $key => $data) {
  12877. if (isset($data['outdata'])) {
  12878. $stream = trim($data['outdata']);
  12879. $out = $this->_getobj($data['n']) . "\n";
  12880. $out .= '<<';
  12881. $out .= ' /Type /XObject';
  12882. $out .= ' /Subtype /Form';
  12883. $out .= ' /FormType 1';
  12884. if ($this->compress) {
  12885. $stream = gzcompress($stream);
  12886. $out .= ' /Filter /FlateDecode';
  12887. }
  12888. $out .= sprintf(' /BBox [%F %F %F %F]', ($data['x'] * $this->k), (-$data['y'] * $this->k), (($data['w'] + $data['x']) * $this->k), (($data['h'] - $data['y']) * $this->k));
  12889. $out .= ' /Matrix [1 0 0 1 0 0]';
  12890. $out .= ' /Resources <<';
  12891. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  12892. if (!$this->pdfa_mode) {
  12893. // transparency
  12894. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  12895. $out .= ' /ExtGState <<';
  12896. foreach ($data['extgstates'] as $k => $extgstate) {
  12897. if (isset($this->extgstates[$k]['name'])) {
  12898. $out .= ' /' . $this->extgstates[$k]['name'];
  12899. } else {
  12900. $out .= ' /GS' . $k;
  12901. }
  12902. $out .= ' ' . $this->extgstates[$k]['n'] . ' 0 R';
  12903. }
  12904. $out .= ' >>';
  12905. }
  12906. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  12907. $gp = '';
  12908. $gs = '';
  12909. foreach ($data['gradients'] as $id => $grad) {
  12910. // gradient patterns
  12911. $gp .= ' /p' . $id . ' ' . $this->gradients[$id]['pattern'] . ' 0 R';
  12912. // gradient shadings
  12913. $gs .= ' /Sh' . $id . ' ' . $this->gradients[$id]['id'] . ' 0 R';
  12914. }
  12915. $out .= ' /Pattern <<' . $gp . ' >>';
  12916. $out .= ' /Shading <<' . $gs . ' >>';
  12917. }
  12918. }
  12919. // spot colors
  12920. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  12921. $out .= ' /ColorSpace <<';
  12922. foreach ($data['spot_colors'] as $name => $color) {
  12923. $out .= ' /CS' . $color['i'] . ' ' . $this->spot_colors[$name]['n'] . ' 0 R';
  12924. }
  12925. $out .= ' >>';
  12926. }
  12927. // fonts
  12928. if (!empty($data['fonts'])) {
  12929. $out .= ' /Font <<';
  12930. foreach ($data['fonts'] as $fontkey => $fontid) {
  12931. $out .= ' /F' . $fontid . ' ' . $this->font_obj_ids[$fontkey] . ' 0 R';
  12932. }
  12933. $out .= ' >>';
  12934. }
  12935. // images or nested xobjects
  12936. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  12937. $out .= ' /XObject <<';
  12938. foreach ($data['images'] as $imgid) {
  12939. $out .= ' /I' . $imgid . ' ' . $this->xobjects['I' . $imgid]['n'] . ' 0 R';
  12940. }
  12941. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  12942. $out .= ' /' . $sub_id . ' ' . $sub_objid['n'] . ' 0 R';
  12943. }
  12944. $out .= ' >>';
  12945. }
  12946. $out .= ' >>'; //end resources
  12947. if (isset($data['group']) AND ($data['group'] !== false)) {
  12948. // set transparency group
  12949. $out .= ' /Group << /Type /Group /S /Transparency';
  12950. if (is_array($data['group'])) {
  12951. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  12952. $out .= ' /CS /' . $data['group']['CS'];
  12953. }
  12954. if (isset($data['group']['I'])) {
  12955. $out .= ' /I /' . ($data['group']['I'] === true ? 'true' : 'false');
  12956. }
  12957. if (isset($data['group']['K'])) {
  12958. $out .= ' /K /' . ($data['group']['K'] === true ? 'true' : 'false');
  12959. }
  12960. }
  12961. $out .= ' >>';
  12962. }
  12963. $stream = $this->_getrawstream($stream, $data['n']);
  12964. $out .= ' /Length ' . strlen($stream);
  12965. $out .= ' >>';
  12966. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  12967. $out .= "\n" . 'endobj';
  12968. $this->_out($out);
  12969. }
  12970. }
  12971. }
  12972. /**
  12973. * Output Spot Colors Resources.
  12974. * @protected
  12975. * @since 4.0.024 (2008-09-12)
  12976. */
  12977. protected function _putspotcolors() {
  12978. foreach ($this->spot_colors as $name => $color) {
  12979. $this->_newobj();
  12980. $this->spot_colors[$name]['n'] = $this->n;
  12981. $out = '[/Separation /' . str_replace(' ', '#20', $name);
  12982. $out .= ' /DeviceCMYK <<';
  12983. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  12984. $out .= ' ' . sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  12985. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  12986. $out .= "\n" . 'endobj';
  12987. $this->_out($out);
  12988. }
  12989. }
  12990. /**
  12991. * Return XObjects Dictionary.
  12992. * @return string XObjects dictionary
  12993. * @protected
  12994. * @since 5.8.014 (2010-08-23)
  12995. */
  12996. protected function _getxobjectdict() {
  12997. $out = '';
  12998. foreach ($this->xobjects as $id => $objid) {
  12999. $out .= ' /' . $id . ' ' . $objid['n'] . ' 0 R';
  13000. }
  13001. return $out;
  13002. }
  13003. /**
  13004. * Output Resources Dictionary.
  13005. * @protected
  13006. */
  13007. protected function _putresourcedict() {
  13008. $out = $this->_getobj(2) . "\n";
  13009. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  13010. $out .= ' /Font <<';
  13011. foreach ($this->fontkeys as $fontkey) {
  13012. $font = $this->getFontBuffer($fontkey);
  13013. $out .= ' /F' . $font['i'] . ' ' . $font['n'] . ' 0 R';
  13014. }
  13015. $out .= ' >>';
  13016. $out .= ' /XObject <<';
  13017. $out .= $this->_getxobjectdict();
  13018. $out .= ' >>';
  13019. // layers
  13020. if (!empty($this->pdflayers)) {
  13021. $out .= ' /Properties <<';
  13022. foreach ($this->pdflayers as $layer) {
  13023. $out .= ' /' . $layer['layer'] . ' ' . $layer['objid'] . ' 0 R';
  13024. }
  13025. $out .= ' >>';
  13026. }
  13027. if (!$this->pdfa_mode) {
  13028. // transparency
  13029. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  13030. $out .= ' /ExtGState <<';
  13031. foreach ($this->extgstates as $k => $extgstate) {
  13032. if (isset($extgstate['name'])) {
  13033. $out .= ' /' . $extgstate['name'];
  13034. } else {
  13035. $out .= ' /GS' . $k;
  13036. }
  13037. $out .= ' ' . $extgstate['n'] . ' 0 R';
  13038. }
  13039. $out .= ' >>';
  13040. }
  13041. if (isset($this->gradients) AND !empty($this->gradients)) {
  13042. $gp = '';
  13043. $gs = '';
  13044. foreach ($this->gradients as $id => $grad) {
  13045. // gradient patterns
  13046. $gp .= ' /p' . $id . ' ' . $grad['pattern'] . ' 0 R';
  13047. // gradient shadings
  13048. $gs .= ' /Sh' . $id . ' ' . $grad['id'] . ' 0 R';
  13049. }
  13050. $out .= ' /Pattern <<' . $gp . ' >>';
  13051. $out .= ' /Shading <<' . $gs . ' >>';
  13052. }
  13053. }
  13054. // spot colors
  13055. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  13056. $out .= ' /ColorSpace <<';
  13057. foreach ($this->spot_colors as $color) {
  13058. $out .= ' /CS' . $color['i'] . ' ' . $color['n'] . ' 0 R';
  13059. }
  13060. $out .= ' >>';
  13061. }
  13062. $out .= ' >>';
  13063. $out .= "\n" . 'endobj';
  13064. $this->_out($out);
  13065. }
  13066. /**
  13067. * Output Resources.
  13068. * @protected
  13069. */
  13070. protected function _putresources() {
  13071. $this->_putextgstates();
  13072. $this->_putocg();
  13073. $this->_putfonts();
  13074. $this->_putimages();
  13075. $this->_putspotcolors();
  13076. $this->_putshaders();
  13077. $this->_putxobjects();
  13078. $this->_putresourcedict();
  13079. $this->_putdests();
  13080. $this->_putbookmarks();
  13081. $this->_putEmbeddedFiles();
  13082. $this->_putannotsobjs();
  13083. $this->_putjavascript();
  13084. $this->_putencryption();
  13085. }
  13086. /**
  13087. * Adds some Metadata information (Document Information Dictionary)
  13088. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  13089. * @return int object id
  13090. * @protected
  13091. */
  13092. protected function _putinfo() {
  13093. $oid = $this->_newobj();
  13094. $out = '<<';
  13095. // store current isunicode value
  13096. $prev_isunicode = $this->isunicode;
  13097. if ($this->docinfounicode) {
  13098. $this->isunicode = true;
  13099. }
  13100. if (!$this->empty_string($this->title)) {
  13101. // The document's title.
  13102. $out .= ' /Title ' . $this->_textstring($this->title, $oid);
  13103. }
  13104. if (!$this->empty_string($this->author)) {
  13105. // The name of the person who created the document.
  13106. $out .= ' /Author ' . $this->_textstring($this->author, $oid);
  13107. }
  13108. if (!$this->empty_string($this->subject)) {
  13109. // The subject of the document.
  13110. $out .= ' /Subject ' . $this->_textstring($this->subject, $oid);
  13111. }
  13112. if (!$this->empty_string($this->keywords)) {
  13113. // Keywords associated with the document.
  13114. $out .= ' /Keywords ' . $this->_textstring($this->keywords . ' TCPDF', $oid);
  13115. }
  13116. if (!$this->empty_string($this->creator)) {
  13117. // If the document was converted to PDF from another format, the name of the conforming product that created the original document from which it was converted.
  13118. $out .= ' /Creator ' . $this->_textstring($this->creator, $oid);
  13119. }
  13120. // restore previous isunicode value
  13121. $this->isunicode = $prev_isunicode;
  13122. // default producer
  13123. $out .= ' /Producer ' . $this->_textstring($this->pdfproducer, $oid);
  13124. // The date and time the document was created, in human-readable form
  13125. $out .= ' /CreationDate ' . $this->_datestring(0, $this->doc_creation_timestamp);
  13126. // The date and time the document was most recently modified, in human-readable form
  13127. $out .= ' /ModDate ' . $this->_datestring(0, $this->doc_modification_timestamp);
  13128. // A name object indicating whether the document has been modified to include trapping information
  13129. $out .= ' /Trapped /False';
  13130. $out .= ' >>';
  13131. $out .= "\n" . 'endobj';
  13132. $this->_out($out);
  13133. return $oid;
  13134. }
  13135. /**
  13136. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  13137. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  13138. * @param $xmp (string) Custom XMP data.
  13139. * @since 5.9.128 (2011-10-06)
  13140. * @public
  13141. */
  13142. public function setExtraXMP($xmp) {
  13143. $this->custom_xmp = $xmp;
  13144. }
  13145. /**
  13146. * Put XMP data object and return ID.
  13147. * @return (int) The object ID.
  13148. * @since 5.9.121 (2011-09-28)
  13149. * @protected
  13150. */
  13151. protected function _putXMP() {
  13152. $oid = $this->_newobj();
  13153. // store current isunicode value
  13154. $prev_isunicode = $this->isunicode;
  13155. $this->isunicode = true;
  13156. $prev_encrypted = $this->encrypted;
  13157. $this->encrypted = false;
  13158. // set XMP data
  13159. $xmp = '<?xpacket begin="' . $this->unichr(0xfeff) . '" id="W5M0MpCehiHzreSzNTczkc9d"?>' . "\n";
  13160. $xmp .= '<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:08:04">' . "\n";
  13161. $xmp .= "\t" . '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' . "\n";
  13162. $xmp .= "\t\t" . '<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">' . "\n";
  13163. $xmp .= "\t\t\t" . '<dc:format>application/pdf</dc:format>' . "\n";
  13164. $xmp .= "\t\t\t" . '<dc:title>' . "\n";
  13165. $xmp .= "\t\t\t\t" . '<rdf:Alt>' . "\n";
  13166. $xmp .= "\t\t\t\t\t" . '<rdf:li xml:lang="x-default">' . $this->_escapeXML($this->title) . '</rdf:li>' . "\n";
  13167. $xmp .= "\t\t\t\t" . '</rdf:Alt>' . "\n";
  13168. $xmp .= "\t\t\t" . '</dc:title>' . "\n";
  13169. $xmp .= "\t\t\t" . '<dc:creator>' . "\n";
  13170. $xmp .= "\t\t\t\t" . '<rdf:Seq>' . "\n";
  13171. $xmp .= "\t\t\t\t\t" . '<rdf:li>' . $this->_escapeXML($this->author) . '</rdf:li>' . "\n";
  13172. $xmp .= "\t\t\t\t" . '</rdf:Seq>' . "\n";
  13173. $xmp .= "\t\t\t" . '</dc:creator>' . "\n";
  13174. $xmp .= "\t\t\t" . '<dc:description>' . "\n";
  13175. $xmp .= "\t\t\t\t" . '<rdf:Alt>' . "\n";
  13176. $xmp .= "\t\t\t\t\t" . '<rdf:li xml:lang="x-default">' . $this->_escapeXML($this->subject) . '</rdf:li>' . "\n";
  13177. $xmp .= "\t\t\t\t" . '</rdf:Alt>' . "\n";
  13178. $xmp .= "\t\t\t" . '</dc:description>' . "\n";
  13179. $xmp .= "\t\t\t" . '<dc:subject>' . "\n";
  13180. $xmp .= "\t\t\t\t" . '<rdf:Bag>' . "\n";
  13181. $xmp .= "\t\t\t\t\t" . '<rdf:li>' . $this->_escapeXML($this->keywords) . ' TCPDF</rdf:li>' . "\n";
  13182. $xmp .= "\t\t\t\t" . '</rdf:Bag>' . "\n";
  13183. $xmp .= "\t\t\t" . '</dc:subject>' . "\n";
  13184. $xmp .= "\t\t" . '</rdf:Description>' . "\n";
  13185. // convert doc creation date format
  13186. $dcdate = $this->getFormattedDate($this->doc_creation_timestamp);
  13187. $doccreationdate = substr($dcdate, 0, 4) . '-' . substr($dcdate, 4, 2) . '-' . substr($dcdate, 6, 2);
  13188. $doccreationdate .= 'T' . substr($dcdate, 8, 2) . ':' . substr($dcdate, 10, 2) . ':' . substr($dcdate, 12, 2);
  13189. $doccreationdate .= '+' . substr($dcdate, 15, 2) . ':' . substr($dcdate, 18, 2);
  13190. $doccreationdate = $this->_escapeXML($doccreationdate);
  13191. // convert doc modification date format
  13192. $dmdate = $this->getFormattedDate($this->doc_modification_timestamp);
  13193. $docmoddate = substr($dmdate, 0, 4) . '-' . substr($dmdate, 4, 2) . '-' . substr($dmdate, 6, 2);
  13194. $docmoddate .= 'T' . substr($dmdate, 8, 2) . ':' . substr($dmdate, 10, 2) . ':' . substr($dmdate, 12, 2);
  13195. $docmoddate .= '+' . substr($dmdate, 15, 2) . ':' . substr($dmdate, 18, 2);
  13196. $docmoddate = $this->_escapeXML($docmoddate);
  13197. $xmp .= "\t\t" . '<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">' . "\n";
  13198. $xmp .= "\t\t\t" . '<xmp:CreateDate>' . $doccreationdate . '</xmp:CreateDate>' . "\n";
  13199. $xmp .= "\t\t\t" . '<xmp:CreatorTool>' . $this->creator . '</xmp:CreatorTool>' . "\n";
  13200. $xmp .= "\t\t\t" . '<xmp:ModifyDate>' . $docmoddate . '</xmp:ModifyDate>' . "\n";
  13201. $xmp .= "\t\t\t" . '<xmp:MetadataDate>' . $doccreationdate . '</xmp:MetadataDate>' . "\n";
  13202. $xmp .= "\t\t" . '</rdf:Description>' . "\n";
  13203. $xmp .= "\t\t" . '<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">' . "\n";
  13204. $xmp .= "\t\t\t" . '<pdf:Keywords>' . $this->_escapeXML($this->keywords) . ' TCPDF</pdf:Keywords>' . "\n";
  13205. $xmp .= "\t\t\t" . '<pdf:Producer>' . $this->_escapeXML($this->pdfproducer) . '</pdf:Producer>' . "\n";
  13206. $xmp .= "\t\t" . '</rdf:Description>' . "\n";
  13207. $xmp .= "\t\t" . '<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">' . "\n";
  13208. $uuid = 'uuid:' . substr($this->file_id, 0, 8) . '-' . substr($this->file_id, 8, 4) . '-' . substr($this->file_id, 12, 4) . '-' . substr($this->file_id, 16, 4) . '-' . substr($this->file_id, 20, 12);
  13209. $xmp .= "\t\t\t" . '<xmpMM:DocumentID>' . $uuid . '</xmpMM:DocumentID>' . "\n";
  13210. $xmp .= "\t\t\t" . '<xmpMM:InstanceID>' . $uuid . '</xmpMM:InstanceID>' . "\n";
  13211. $xmp .= "\t\t" . '</rdf:Description>' . "\n";
  13212. if ($this->pdfa_mode) {
  13213. $xmp .= "\t\t" . '<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">' . "\n";
  13214. $xmp .= "\t\t\t" . '<pdfaid:part>1</pdfaid:part>' . "\n";
  13215. $xmp .= "\t\t\t" . '<pdfaid:conformance>B</pdfaid:conformance>' . "\n";
  13216. $xmp .= "\t\t" . '</rdf:Description>' . "\n";
  13217. }
  13218. // XMP extension schemas
  13219. $xmp .= "\t\t" . '<rdf:Description rdf:about="" xmlns:pdfaExtension="http://www.aiim.org/pdfa/ns/extension/" xmlns:pdfaSchema="http://www.aiim.org/pdfa/ns/schema#" xmlns:pdfaProperty="http://www.aiim.org/pdfa/ns/property#">' . "\n";
  13220. $xmp .= "\t\t\t" . '<pdfaExtension:schemas>' . "\n";
  13221. $xmp .= "\t\t\t\t" . '<rdf:Bag>' . "\n";
  13222. $xmp .= "\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13223. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>' . "\n";
  13224. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:prefix>pdf</pdfaSchema:prefix>' . "\n";
  13225. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>' . "\n";
  13226. $xmp .= "\t\t\t\t\t" . '</rdf:li>' . "\n";
  13227. $xmp .= "\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13228. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>' . "\n";
  13229. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>' . "\n";
  13230. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>' . "\n";
  13231. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:property>' . "\n";
  13232. $xmp .= "\t\t\t\t\t\t\t" . '<rdf:Seq>' . "\n";
  13233. $xmp .= "\t\t\t\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13234. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:category>internal</pdfaProperty:category>' . "\n";
  13235. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>' . "\n";
  13236. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:name>InstanceID</pdfaProperty:name>' . "\n";
  13237. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:valueType>URI</pdfaProperty:valueType>' . "\n";
  13238. $xmp .= "\t\t\t\t\t\t\t\t" . '</rdf:li>' . "\n";
  13239. $xmp .= "\t\t\t\t\t\t\t" . '</rdf:Seq>' . "\n";
  13240. $xmp .= "\t\t\t\t\t\t" . '</pdfaSchema:property>' . "\n";
  13241. $xmp .= "\t\t\t\t\t" . '</rdf:li>' . "\n";
  13242. $xmp .= "\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13243. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>' . "\n";
  13244. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>' . "\n";
  13245. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>' . "\n";
  13246. $xmp .= "\t\t\t\t\t\t" . '<pdfaSchema:property>' . "\n";
  13247. $xmp .= "\t\t\t\t\t\t\t" . '<rdf:Seq>' . "\n";
  13248. $xmp .= "\t\t\t\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13249. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:category>internal</pdfaProperty:category>' . "\n";
  13250. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>' . "\n";
  13251. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:name>part</pdfaProperty:name>' . "\n";
  13252. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:valueType>Integer</pdfaProperty:valueType>' . "\n";
  13253. $xmp .= "\t\t\t\t\t\t\t\t" . '</rdf:li>' . "\n";
  13254. $xmp .= "\t\t\t\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13255. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:category>internal</pdfaProperty:category>' . "\n";
  13256. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>' . "\n";
  13257. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:name>amd</pdfaProperty:name>' . "\n";
  13258. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:valueType>Text</pdfaProperty:valueType>' . "\n";
  13259. $xmp .= "\t\t\t\t\t\t\t\t" . '</rdf:li>' . "\n";
  13260. $xmp .= "\t\t\t\t\t\t\t\t" . '<rdf:li rdf:parseType="Resource">' . "\n";
  13261. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:category>internal</pdfaProperty:category>' . "\n";
  13262. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>' . "\n";
  13263. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:name>conformance</pdfaProperty:name>' . "\n";
  13264. $xmp .= "\t\t\t\t\t\t\t\t\t" . '<pdfaProperty:valueType>Text</pdfaProperty:valueType>' . "\n";
  13265. $xmp .= "\t\t\t\t\t\t\t\t" . '</rdf:li>' . "\n";
  13266. $xmp .= "\t\t\t\t\t\t\t" . '</rdf:Seq>' . "\n";
  13267. $xmp .= "\t\t\t\t\t\t" . '</pdfaSchema:property>' . "\n";
  13268. $xmp .= "\t\t\t\t\t" . '</rdf:li>' . "\n";
  13269. $xmp .= "\t\t\t\t" . '</rdf:Bag>' . "\n";
  13270. $xmp .= "\t\t\t" . '</pdfaExtension:schemas>' . "\n";
  13271. $xmp .= "\t\t" . '</rdf:Description>' . "\n";
  13272. $xmp .= "\t" . '</rdf:RDF>' . "\n";
  13273. $xmp .= $this->custom_xmp;
  13274. $xmp .= '</x:xmpmeta>' . "\n";
  13275. $xmp .= '<?xpacket end="w"?>';
  13276. $out = '<< /Type /Metadata /Subtype /XML /Length ' . strlen($xmp) . ' >> stream' . "\n" . $xmp . "\n" . 'endstream' . "\n" . 'endobj';
  13277. // restore previous isunicode value
  13278. $this->isunicode = $prev_isunicode;
  13279. $this->encrypted = $prev_encrypted;
  13280. $this->_out($out);
  13281. return $oid;
  13282. }
  13283. /**
  13284. * Output Catalog.
  13285. * @return int object id
  13286. * @protected
  13287. */
  13288. protected function _putcatalog() {
  13289. // put XMP
  13290. $xmpobj = $this->_putXMP();
  13291. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  13292. if ($this->pdfa_mode OR $this->force_srgb) {
  13293. $iccobj = $this->_newobj();
  13294. $icc = file_get_contents(dirname(__FILE__) . '/sRGB.icc');
  13295. $filter = '';
  13296. if ($this->compress) {
  13297. $filter = ' /Filter /FlateDecode';
  13298. $icc = gzcompress($icc);
  13299. }
  13300. $icc = $this->_getrawstream($icc);
  13301. $this->_out('<</N 3 ' . $filter . '/Length ' . strlen($icc) . '>> stream' . "\n" . $icc . "\n" . 'endstream' . "\n" . 'endobj');
  13302. }
  13303. // start catalog
  13304. $oid = $this->_newobj();
  13305. $out = '<< /Type /Catalog';
  13306. $out .= ' /Version /' . $this->PDFVersion;
  13307. //$out .= ' /Extensions <<>>';
  13308. $out .= ' /Pages 1 0 R';
  13309. //$out .= ' /PageLabels ' //...;
  13310. $out .= ' /Names <<';
  13311. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  13312. $out .= ' /JavaScript ' . ($this->n_js) . ' 0 R';
  13313. }
  13314. $out .= ' >>';
  13315. if (!empty($this->dests)) {
  13316. $out .= ' /Dests ' . $this->n_dests . ' 0 R';
  13317. }
  13318. $out .= $this->_putviewerpreferences();
  13319. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  13320. $out .= ' /PageLayout /' . $this->LayoutMode;
  13321. }
  13322. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  13323. $out .= ' /PageMode /' . $this->PageMode;
  13324. }
  13325. if (count($this->outlines) > 0) {
  13326. $out .= ' /Outlines ' . $this->OutlineRoot . ' 0 R';
  13327. $out .= ' /PageMode /UseOutlines';
  13328. }
  13329. //$out .= ' /Threads []';
  13330. if ($this->ZoomMode == 'fullpage') {
  13331. $out .= ' /OpenAction [' . $this->page_obj_id[1] . ' 0 R /Fit]';
  13332. } elseif ($this->ZoomMode == 'fullwidth') {
  13333. $out .= ' /OpenAction [' . $this->page_obj_id[1] . ' 0 R /FitH null]';
  13334. } elseif ($this->ZoomMode == 'real') {
  13335. $out .= ' /OpenAction [' . $this->page_obj_id[1] . ' 0 R /XYZ null null 1]';
  13336. } elseif (!is_string($this->ZoomMode)) {
  13337. $out .= sprintf(' /OpenAction [' . $this->page_obj_id[1] . ' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  13338. }
  13339. //$out .= ' /AA <<>>';
  13340. //$out .= ' /URI <<>>';
  13341. $out .= ' /Metadata ' . $xmpobj . ' 0 R';
  13342. //$out .= ' /StructTreeRoot <<>>';
  13343. //$out .= ' /MarkInfo <<>>';
  13344. if (isset($this->l['a_meta_language'])) {
  13345. $out .= ' /Lang ' . $this->_textstring($this->l['a_meta_language'], $oid);
  13346. }
  13347. //$out .= ' /SpiderInfo <<>>';
  13348. // set OutputIntent to sRGB IEC61966-2.1 if required
  13349. if ($this->pdfa_mode OR $this->force_srgb) {
  13350. $out .= ' /OutputIntents [<<';
  13351. $out .= ' /Type /OutputIntent';
  13352. $out .= ' /S /GTS_PDFA1';
  13353. $out .= ' /OutputCondition ' . $this->_textstring('sRGB IEC61966-2.1', $oid);
  13354. $out .= ' /OutputConditionIdentifier ' . $this->_textstring('sRGB IEC61966-2.1', $oid);
  13355. $out .= ' /RegistryName ' . $this->_textstring('http://www.color.org', $oid);
  13356. $out .= ' /Info ' . $this->_textstring('sRGB IEC61966-2.1', $oid);
  13357. $out .= ' /DestOutputProfile ' . $iccobj . ' 0 R';
  13358. $out .= ' >>]';
  13359. }
  13360. //$out .= ' /PieceInfo <<>>';
  13361. if (!empty($this->pdflayers)) {
  13362. $lyrobjs = '';
  13363. $lyrobjs_print = '';
  13364. $lyrobjs_view = '';
  13365. foreach ($this->pdflayers as $layer) {
  13366. $lyrobjs .= ' ' . $layer['objid'] . ' 0 R';
  13367. if ($layer['print']) {
  13368. $lyrobjs_print .= ' ' . $layer['objid'] . ' 0 R';
  13369. }
  13370. if ($layer['view']) {
  13371. $lyrobjs_view .= ' ' . $layer['objid'] . ' 0 R';
  13372. }
  13373. }
  13374. $out .= ' /OCProperties << /OCGs [' . $lyrobjs . ']';
  13375. $out .= ' /D <<';
  13376. $out .= ' /Name ' . $this->_textstring('Layers', $oid);
  13377. $out .= ' /Creator ' . $this->_textstring('TCPDF', $oid);
  13378. $out .= ' /BaseState /ON';
  13379. $out .= ' /ON [' . $lyrobjs_print . ']';
  13380. $out .= ' /OFF [' . $lyrobjs_view . ']';
  13381. $out .= ' /Intent /View';
  13382. $out .= ' /AS [';
  13383. $out .= ' << /Event /Print /OCGs [' . $lyrobjs . '] /Category [/Print] >>';
  13384. $out .= ' << /Event /View /OCGs [' . $lyrobjs . '] /Category [/View] >>';
  13385. $out .= ' ]';
  13386. $out .= ' /Order [' . $lyrobjs . ']';
  13387. $out .= ' /ListMode /AllPages';
  13388. //$out .= ' /RBGroups ['..']';
  13389. //$out .= ' /Locked ['..']';
  13390. $out .= ' >>';
  13391. $out .= ' >>';
  13392. }
  13393. // AcroForm
  13394. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  13395. $out .= ' /AcroForm <<';
  13396. $objrefs = '';
  13397. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  13398. // set reference for signature object
  13399. $objrefs .= $this->sig_obj_id . ' 0 R';
  13400. }
  13401. if (!empty($this->empty_signature_appearance)) {
  13402. foreach ($this->empty_signature_appearance as $esa) {
  13403. // set reference for empty signature objects
  13404. $objrefs .= ' ' . $esa['objid'] . ' 0 R';
  13405. }
  13406. }
  13407. if (!empty($this->form_obj_id)) {
  13408. foreach ($this->form_obj_id as $objid) {
  13409. $objrefs .= ' ' . $objid . ' 0 R';
  13410. }
  13411. }
  13412. $out .= ' /Fields [' . $objrefs . ']';
  13413. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  13414. $out .= ' /NeedAppearances false';
  13415. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  13416. if ($this->signature_data['cert_type'] > 0) {
  13417. $out .= ' /SigFlags 3';
  13418. } else {
  13419. $out .= ' /SigFlags 1';
  13420. }
  13421. }
  13422. //$out .= ' /CO ';
  13423. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  13424. $out .= ' /DR <<';
  13425. $out .= ' /Font <<';
  13426. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  13427. $out .= ' /F' . $fontid . ' ' . $this->font_obj_ids[$fontkey] . ' 0 R';
  13428. }
  13429. $out .= ' >> >>';
  13430. }
  13431. $font = $this->getFontBuffer('helvetica');
  13432. $out .= ' /DA (/F' . $font['i'] . ' 0 Tf 0 g)';
  13433. $out .= ' /Q ' . (($this->rtl) ? '2' : '0');
  13434. //$out .= ' /XFA ';
  13435. $out .= ' >>';
  13436. // signatures
  13437. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  13438. if ($this->signature_data['cert_type'] > 0) {
  13439. $out .= ' /Perms << /DocMDP ' . ($this->sig_obj_id + 1) . ' 0 R >>';
  13440. } else {
  13441. $out .= ' /Perms << /UR3 ' . ($this->sig_obj_id + 1) . ' 0 R >>';
  13442. }
  13443. }
  13444. }
  13445. //$out .= ' /Legal <<>>';
  13446. //$out .= ' /Requirements []';
  13447. //$out .= ' /Collection <<>>';
  13448. //$out .= ' /NeedsRendering true';
  13449. $out .= ' >>';
  13450. $out .= "\n" . 'endobj';
  13451. $this->_out($out);
  13452. return $oid;
  13453. }
  13454. /**
  13455. * Output viewer preferences.
  13456. * @return string for viewer preferences
  13457. * @author Nicola asuni
  13458. * @since 3.1.000 (2008-06-09)
  13459. * @protected
  13460. */
  13461. protected function _putviewerpreferences() {
  13462. $out = ' /ViewerPreferences <<';
  13463. if ($this->rtl) {
  13464. $out .= ' /Direction /R2L';
  13465. } else {
  13466. $out .= ' /Direction /L2R';
  13467. }
  13468. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  13469. $out .= ' /HideToolbar true';
  13470. }
  13471. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  13472. $out .= ' /HideMenubar true';
  13473. }
  13474. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  13475. $out .= ' /HideWindowUI true';
  13476. }
  13477. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  13478. $out .= ' /FitWindow true';
  13479. }
  13480. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  13481. $out .= ' /CenterWindow true';
  13482. }
  13483. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  13484. $out .= ' /DisplayDocTitle true';
  13485. }
  13486. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  13487. $out .= ' /NonFullScreenPageMode /' . $this->viewer_preferences['NonFullScreenPageMode'];
  13488. }
  13489. if (isset($this->viewer_preferences['ViewArea'])) {
  13490. $out .= ' /ViewArea /' . $this->viewer_preferences['ViewArea'];
  13491. }
  13492. if (isset($this->viewer_preferences['ViewClip'])) {
  13493. $out .= ' /ViewClip /' . $this->viewer_preferences['ViewClip'];
  13494. }
  13495. if (isset($this->viewer_preferences['PrintArea'])) {
  13496. $out .= ' /PrintArea /' . $this->viewer_preferences['PrintArea'];
  13497. }
  13498. if (isset($this->viewer_preferences['PrintClip'])) {
  13499. $out .= ' /PrintClip /' . $this->viewer_preferences['PrintClip'];
  13500. }
  13501. if (isset($this->viewer_preferences['PrintScaling'])) {
  13502. $out .= ' /PrintScaling /' . $this->viewer_preferences['PrintScaling'];
  13503. }
  13504. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  13505. $out .= ' /Duplex /' . $this->viewer_preferences['Duplex'];
  13506. }
  13507. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  13508. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  13509. $out .= ' /PickTrayByPDFSize true';
  13510. } else {
  13511. $out .= ' /PickTrayByPDFSize false';
  13512. }
  13513. }
  13514. if (isset($this->viewer_preferences['PrintPageRange'])) {
  13515. $PrintPageRangeNum = '';
  13516. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  13517. $PrintPageRangeNum .= ' ' . ($v - 1) . '';
  13518. }
  13519. $out .= ' /PrintPageRange [' . substr($PrintPageRangeNum, 1) . ']';
  13520. }
  13521. if (isset($this->viewer_preferences['NumCopies'])) {
  13522. $out .= ' /NumCopies ' . intval($this->viewer_preferences['NumCopies']);
  13523. }
  13524. $out .= ' >>';
  13525. return $out;
  13526. }
  13527. /**
  13528. * Output PDF File Header (7.5.2).
  13529. * @protected
  13530. */
  13531. protected function _putheader() {
  13532. $this->_out('%PDF-' . $this->PDFVersion);
  13533. $this->_out('%' . chr(0xe2) . chr(0xe3) . chr(0xcf) . chr(0xd3));
  13534. }
  13535. /**
  13536. * Output end of document (EOF).
  13537. * @protected
  13538. */
  13539. protected function _enddoc() {
  13540. $this->state = 1;
  13541. $this->_putheader();
  13542. $this->_putpages();
  13543. $this->_putresources();
  13544. // empty signature fields
  13545. if (!empty($this->empty_signature_appearance)) {
  13546. foreach ($this->empty_signature_appearance as $key => $esa) {
  13547. // widget annotation for empty signature
  13548. $out = $this->_getobj($esa['objid']) . "\n";
  13549. $out .= '<< /Type /Annot';
  13550. $out .= ' /Subtype /Widget';
  13551. $out .= ' /Rect [' . $esa['rect'] . ']';
  13552. $out .= ' /P ' . $this->page_obj_id[($esa['page'])] . ' 0 R'; // link to signature appearance page
  13553. $out .= ' /F 4';
  13554. $out .= ' /FT /Sig';
  13555. $signame = sprintf('Signature_%03d', ($key + 1));
  13556. $out .= ' /T ' . $this->_textstring($signame, $esa['objid']);
  13557. $out .= ' /Ff 0';
  13558. $out .= ' >>';
  13559. $out .= "\n" . 'endobj';
  13560. $this->_out($out);
  13561. }
  13562. }
  13563. // Signature
  13564. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  13565. // widget annotation for signature
  13566. $out = $this->_getobj($this->sig_obj_id) . "\n";
  13567. $out .= '<< /Type /Annot';
  13568. $out .= ' /Subtype /Widget';
  13569. $out .= ' /Rect [' . $this->signature_appearance['rect'] . ']';
  13570. $out .= ' /P ' . $this->page_obj_id[($this->signature_appearance['page'])] . ' 0 R'; // link to signature appearance page
  13571. $out .= ' /F 4';
  13572. $out .= ' /FT /Sig';
  13573. $out .= ' /T ' . $this->_textstring('Signature_000', $this->sig_obj_id);
  13574. $out .= ' /Ff 0';
  13575. $out .= ' /V ' . ($this->sig_obj_id + 1) . ' 0 R';
  13576. $out .= ' >>';
  13577. $out .= "\n" . 'endobj';
  13578. $this->_out($out);
  13579. // signature
  13580. $this->_putsignature();
  13581. }
  13582. // Info
  13583. $objid_info = $this->_putinfo();
  13584. // Catalog
  13585. $objid_catalog = $this->_putcatalog();
  13586. // Cross-ref
  13587. $o = $this->bufferlen;
  13588. // XREF section
  13589. $this->_out('xref');
  13590. $this->_out('0 ' . ($this->n + 1));
  13591. $this->_out('0000000000 65535 f ');
  13592. $freegen = ($this->n + 2);
  13593. for ($i = 1; $i <= $this->n; ++$i) {
  13594. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  13595. $this->_out(sprintf('0000000000 %05d f ', $freegen));
  13596. ++$freegen;
  13597. } else {
  13598. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  13599. }
  13600. }
  13601. // TRAILER
  13602. $out = 'trailer' . "\n";
  13603. $out .= '<<';
  13604. $out .= ' /Size ' . ($this->n + 1);
  13605. $out .= ' /Root ' . $objid_catalog . ' 0 R';
  13606. $out .= ' /Info ' . $objid_info . ' 0 R';
  13607. if ($this->encrypted) {
  13608. $out .= ' /Encrypt ' . $this->encryptdata['objid'] . ' 0 R';
  13609. }
  13610. $out .= ' /ID [ <' . $this->file_id . '> <' . $this->file_id . '> ]';
  13611. $out .= ' >>';
  13612. $this->_out($out);
  13613. $this->_out('startxref');
  13614. $this->_out($o);
  13615. $this->_out('%%EOF');
  13616. $this->state = 3; // end-of-doc
  13617. if ($this->diskcache) {
  13618. // remove temporary files used for images
  13619. foreach ($this->imagekeys as $key) {
  13620. // remove temporary files
  13621. unlink($this->images[$key]);
  13622. }
  13623. foreach ($this->fontkeys as $key) {
  13624. // remove temporary files
  13625. unlink($this->fonts[$key]);
  13626. }
  13627. }
  13628. }
  13629. /**
  13630. * Initialize a new page.
  13631. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  13632. * @param $format (mixed) The format used for pages. It can be either: one of the string values specified at getPageSizeFromFormat() or an array of parameters specified at setPageFormat().
  13633. * @protected
  13634. * @see getPageSizeFromFormat(), setPageFormat()
  13635. */
  13636. protected function _beginpage($orientation = '', $format = '') {
  13637. ++$this->page;
  13638. $this->pageobjects[$this->page] = array();
  13639. $this->setPageBuffer($this->page, '');
  13640. // initialize array for graphics tranformation positions inside a page buffer
  13641. $this->transfmrk[$this->page] = array();
  13642. $this->state = 2;
  13643. if ($this->empty_string($orientation)) {
  13644. if (isset($this->CurOrientation)) {
  13645. $orientation = $this->CurOrientation;
  13646. } elseif ($this->fwPt > $this->fhPt) {
  13647. // landscape
  13648. $orientation = 'L';
  13649. } else {
  13650. // portrait
  13651. $orientation = 'P';
  13652. }
  13653. }
  13654. if ($this->empty_string($format)) {
  13655. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  13656. $this->setPageOrientation($orientation);
  13657. } else {
  13658. $this->setPageFormat($format, $orientation);
  13659. }
  13660. if ($this->rtl) {
  13661. $this->x = $this->w - $this->rMargin;
  13662. } else {
  13663. $this->x = $this->lMargin;
  13664. }
  13665. $this->y = $this->tMargin;
  13666. if (isset($this->newpagegroup[$this->page])) {
  13667. // start a new group
  13668. $this->currpagegroup = $this->newpagegroup[$this->page];
  13669. $this->pagegroups[$this->currpagegroup] = 1;
  13670. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  13671. ++$this->pagegroups[$this->currpagegroup];
  13672. }
  13673. }
  13674. /**
  13675. * Mark end of page.
  13676. * @protected
  13677. */
  13678. protected function _endpage() {
  13679. $this->setVisibility('all');
  13680. $this->state = 1;
  13681. }
  13682. /**
  13683. * Begin a new object and return the object number.
  13684. * @return int object number
  13685. * @protected
  13686. */
  13687. protected function _newobj() {
  13688. $this->_out($this->_getobj());
  13689. return $this->n;
  13690. }
  13691. /**
  13692. * Return the starting object string for the selected object ID.
  13693. * @param $objid (int) Object ID (leave empty to get a new ID).
  13694. * @return string the starting object string
  13695. * @protected
  13696. * @since 5.8.009 (2010-08-20)
  13697. */
  13698. protected function _getobj($objid = '') {
  13699. if ($objid === '') {
  13700. ++$this->n;
  13701. $objid = $this->n;
  13702. }
  13703. $this->offsets[$objid] = $this->bufferlen;
  13704. $this->pageobjects[$this->page][] = $objid;
  13705. return $objid . ' 0 obj';
  13706. }
  13707. /**
  13708. * Underline text.
  13709. * @param $x (int) X coordinate
  13710. * @param $y (int) Y coordinate
  13711. * @param $txt (string) text to underline
  13712. * @protected
  13713. */
  13714. protected function _dounderline($x, $y, $txt) {
  13715. $w = $this->GetStringWidth($txt);
  13716. return $this->_dounderlinew($x, $y, $w);
  13717. }
  13718. /**
  13719. * Underline for rectangular text area.
  13720. * @param $x (int) X coordinate
  13721. * @param $y (int) Y coordinate
  13722. * @param $w (int) width to underline
  13723. * @protected
  13724. * @since 4.8.008 (2009-09-29)
  13725. */
  13726. protected function _dounderlinew($x, $y, $w) {
  13727. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  13728. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  13729. }
  13730. /**
  13731. * Line through text.
  13732. * @param $x (int) X coordinate
  13733. * @param $y (int) Y coordinate
  13734. * @param $txt (string) text to linethrough
  13735. * @protected
  13736. */
  13737. protected function _dolinethrough($x, $y, $txt) {
  13738. $w = $this->GetStringWidth($txt);
  13739. return $this->_dolinethroughw($x, $y, $w);
  13740. }
  13741. /**
  13742. * Line through for rectangular text area.
  13743. * @param $x (int) X coordinate
  13744. * @param $y (int) Y coordinate
  13745. * @param $w (int) line length (width)
  13746. * @protected
  13747. * @since 4.9.008 (2009-09-29)
  13748. */
  13749. protected function _dolinethroughw($x, $y, $w) {
  13750. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  13751. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  13752. }
  13753. /**
  13754. * Overline text.
  13755. * @param $x (int) X coordinate
  13756. * @param $y (int) Y coordinate
  13757. * @param $txt (string) text to overline
  13758. * @protected
  13759. * @since 4.9.015 (2010-04-19)
  13760. */
  13761. protected function _dooverline($x, $y, $txt) {
  13762. $w = $this->GetStringWidth($txt);
  13763. return $this->_dooverlinew($x, $y, $w);
  13764. }
  13765. /**
  13766. * Overline for rectangular text area.
  13767. * @param $x (int) X coordinate
  13768. * @param $y (int) Y coordinate
  13769. * @param $w (int) width to overline
  13770. * @protected
  13771. * @since 4.9.015 (2010-04-19)
  13772. */
  13773. protected function _dooverlinew($x, $y, $w) {
  13774. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  13775. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  13776. }
  13777. /**
  13778. * Read a 4-byte (32 bit) integer from file.
  13779. * @param $f (string) file name.
  13780. * @return 4-byte integer
  13781. * @protected
  13782. */
  13783. protected function _freadint($f) {
  13784. $a = unpack('Ni', fread($f, 4));
  13785. return $a['i'];
  13786. }
  13787. /**
  13788. * Add "\" before "\", "(" and ")"
  13789. * @param $s (string) string to escape.
  13790. * @return string escaped string.
  13791. * @protected
  13792. */
  13793. protected function _escape($s) {
  13794. // the chr(13) substitution fixes the Bugs item #1421290.
  13795. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  13796. }
  13797. /**
  13798. * Format a data string for meta information
  13799. * @param $s (string) data string to escape.
  13800. * @param $n (int) object ID
  13801. * @return string escaped string.
  13802. * @protected
  13803. */
  13804. protected function _datastring($s, $n = 0) {
  13805. if ($n == 0) {
  13806. $n = $this->n;
  13807. }
  13808. $s = $this->_encrypt_data($n, $s);
  13809. return '(' . $this->_escape($s) . ')';
  13810. }
  13811. /**
  13812. * Set the document creation timestamp
  13813. * @param $time (mixed) Document creation timestamp in seconds or date-time string.
  13814. * @public
  13815. * @since 5.9.152 (2012-03-23)
  13816. */
  13817. public function setDocCreationTimestamp($time) {
  13818. if (is_string($time)) {
  13819. $time = getTimestamp($time);
  13820. }
  13821. $this->doc_creation_timestamp = intval($time);
  13822. }
  13823. /**
  13824. * Set the document modification timestamp
  13825. * @param $time (mixed) Document modification timestamp in seconds or date-time string.
  13826. * @public
  13827. * @since 5.9.152 (2012-03-23)
  13828. */
  13829. public function setDocModificationTimestamp($time) {
  13830. if (is_string($time)) {
  13831. $time = getTimestamp($time);
  13832. }
  13833. $this->doc_modification_timestamp = intval($time);
  13834. }
  13835. /**
  13836. * Returns document creation timestamp in seconds.
  13837. * @return (int) Creation timestamp in seconds.
  13838. * @public
  13839. * @since 5.9.152 (2012-03-23)
  13840. */
  13841. public function getDocCreationTimestamp() {
  13842. return $this->doc_creation_timestamp;
  13843. }
  13844. /**
  13845. * Returns document modification timestamp in seconds.
  13846. * @return (int) Modfication timestamp in seconds.
  13847. * @public
  13848. * @since 5.9.152 (2012-03-23)
  13849. */
  13850. public function getDocModificationTimestamp() {
  13851. return $this->doc_modification_timestamp;
  13852. }
  13853. /**
  13854. * Returns timestamp in seconds from formatted date-time.
  13855. * @param $date (string) Formatted date-time.
  13856. * @return int seconds.
  13857. * @public
  13858. * @since 5.9.152 (2012-03-23)
  13859. */
  13860. public function getTimestamp($date) {
  13861. if (($date[0] == 'D') AND ($date[1] == ':')) {
  13862. // remove date prefix if present
  13863. $date = substr($date, 2);
  13864. }
  13865. return strtotime($date);
  13866. }
  13867. /**
  13868. * Returns a formatted date-time.
  13869. * @param $time (int) Time in seconds.
  13870. * @return string escaped date string.
  13871. * @public
  13872. * @since 5.9.152 (2012-03-23)
  13873. */
  13874. public function getFormattedDate($time) {
  13875. return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0) . '\'';
  13876. }
  13877. /**
  13878. * Returns a formatted date for meta information
  13879. * @param $n (int) Object ID.
  13880. * @param $timestamp (int) Timestamp to convert.
  13881. * @return string escaped date string.
  13882. * @protected
  13883. * @since 4.6.028 (2009-08-25)
  13884. */
  13885. protected function _datestring($n = 0, $timestamp = 0) {
  13886. if ((empty($timestamp)) OR ($timestamp < 0)) {
  13887. $timestamp = $this->doc_creation_timestamp;
  13888. }
  13889. return $this->_datastring('D:' . $this->getFormattedDate($timestamp), $n);
  13890. }
  13891. /**
  13892. * Format a text string for meta information
  13893. * @param $s (string) string to escape.
  13894. * @param $n (int) object ID
  13895. * @return string escaped string.
  13896. * @protected
  13897. */
  13898. protected function _textstring($s, $n = 0) {
  13899. if ($this->isunicode) {
  13900. //Convert string to UTF-16BE
  13901. $s = $this->UTF8ToUTF16BE($s, true);
  13902. }
  13903. return $this->_datastring($s, $n);
  13904. }
  13905. /**
  13906. * THIS METHOD IS DEPRECATED
  13907. * Format a text string
  13908. * @param $s (string) string to escape.
  13909. * @return string escaped string.
  13910. * @protected
  13911. * @deprecated
  13912. */
  13913. protected function _escapetext($s) {
  13914. if ($this->isunicode) {
  13915. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  13916. $s = $this->UTF8ToLatin1($s);
  13917. } else {
  13918. //Convert string to UTF-16BE and reverse RTL language
  13919. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  13920. }
  13921. }
  13922. return $this->_escape($s);
  13923. }
  13924. /**
  13925. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  13926. * @param $str (string) Input string to convert.
  13927. * @return converted string
  13928. * @since 5.9.121 (2011-09-28)
  13929. * @protected
  13930. */
  13931. protected function _escapeXML($str) {
  13932. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  13933. $str = strtr($str, $replaceTable);
  13934. return $str;
  13935. }
  13936. /**
  13937. * get raw output stream.
  13938. * @param $s (string) string to output.
  13939. * @param $n (int) object reference for encryption mode
  13940. * @protected
  13941. * @author Nicola Asuni
  13942. * @since 5.5.000 (2010-06-22)
  13943. */
  13944. protected function _getrawstream($s, $n = 0) {
  13945. if ($n <= 0) {
  13946. // default to current object
  13947. $n = $this->n;
  13948. }
  13949. return $this->_encrypt_data($n, $s);
  13950. }
  13951. /**
  13952. * Format output stream (DEPRECATED).
  13953. * @param $s (string) string to output.
  13954. * @param $n (int) object reference for encryption mode
  13955. * @protected
  13956. * @deprecated
  13957. */
  13958. protected function _getstream($s, $n = 0) {
  13959. return 'stream' . "\n" . $this->_getrawstream($s, $n) . "\n" . 'endstream';
  13960. }
  13961. /**
  13962. * Output a stream (DEPRECATED).
  13963. * @param $s (string) string to output.
  13964. * @param $n (int) object reference for encryption mode
  13965. * @protected
  13966. * @deprecated
  13967. */
  13968. protected function _putstream($s, $n = 0) {
  13969. $this->_out($this->_getstream($s, $n));
  13970. }
  13971. /**
  13972. * Output a string to the document.
  13973. * @param $s (string) string to output.
  13974. * @protected
  13975. */
  13976. protected function _out($s) {
  13977. if ($this->state == 2) {
  13978. if ($this->inxobj) {
  13979. // we are inside an XObject template
  13980. $this->xobjects[$this->xobjid]['outdata'] .= $s . "\n";
  13981. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  13982. // puts data before page footer
  13983. $pagebuff = $this->getPageBuffer($this->page);
  13984. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  13985. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  13986. $this->setPageBuffer($this->page, $page . $s . "\n" . $footer);
  13987. // update footer position
  13988. $this->footerpos[$this->page] += strlen($s . "\n");
  13989. } else {
  13990. // set page data
  13991. $this->setPageBuffer($this->page, $s . "\n", true);
  13992. }
  13993. } elseif ($this->state > 0) {
  13994. // set general data
  13995. $this->setBuffer($s . "\n");
  13996. }
  13997. }
  13998. /**
  13999. * Converts UTF-8 strings to codepoints array.<br>
  14000. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  14001. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  14002. * <pre>
  14003. * Char. number range | UTF-8 octet sequence
  14004. * (hexadecimal) | (binary)
  14005. * --------------------+-----------------------------------------------
  14006. * 0000 0000-0000 007F | 0xxxxxxx
  14007. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  14008. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  14009. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  14010. * ---------------------------------------------------------------------
  14011. *
  14012. * ABFN notation:
  14013. * ---------------------------------------------------------------------
  14014. * UTF8-octets = *( UTF8-char )
  14015. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  14016. * UTF8-1 = %x00-7F
  14017. * UTF8-2 = %xC2-DF UTF8-tail
  14018. *
  14019. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  14020. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  14021. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  14022. * %xF4 %x80-8F 2( UTF8-tail )
  14023. * UTF8-tail = %x80-BF
  14024. * ---------------------------------------------------------------------
  14025. * </pre>
  14026. * @param $str (string) string to process.
  14027. * @return array containing codepoints (UTF-8 characters values)
  14028. * @protected
  14029. * @author Nicola Asuni
  14030. * @since 1.53.0.TC005 (2005-01-05)
  14031. */
  14032. protected function UTF8StringToArray($str) {
  14033. // build a unique string key
  14034. $strkey = md5($str);
  14035. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  14036. // return cached value
  14037. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  14038. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  14039. if ($this->isunicode) {
  14040. foreach ($chrarray as $chr) {
  14041. // store this char for font subsetting
  14042. $this->CurrentFont['subsetchars'][$chr] = true;
  14043. }
  14044. // update font subsetchars
  14045. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  14046. }
  14047. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  14048. }
  14049. return $chrarray;
  14050. }
  14051. // check cache size
  14052. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  14053. // remove first element
  14054. array_shift($this->cache_UTF8StringToArray);
  14055. }
  14056. // new cache array for selected string
  14057. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  14058. ++$this->cache_size_UTF8StringToArray;
  14059. if (!$this->isunicode) {
  14060. // split string into array of equivalent codes
  14061. $strarr = array();
  14062. $strlen = strlen($str);
  14063. for ($i = 0; $i < $strlen; ++$i) {
  14064. $strarr[] = ord($str[$i]);
  14065. }
  14066. // insert new value on cache
  14067. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  14068. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  14069. return $strarr;
  14070. }
  14071. $unichar = -1; // last unicode char
  14072. $unicode = array(); // array containing unicode values
  14073. $bytes = array(); // array containing single character byte sequences
  14074. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  14075. $str .= ''; // force $str to be a string
  14076. $length = strlen($str);
  14077. for ($i = 0; $i < $length; ++$i) {
  14078. $char = ord($str[$i]); // get one string character at time
  14079. if (count($bytes) == 0) { // get starting octect
  14080. if ($char <= 0x7F) {
  14081. $unichar = $char; // use the character "as is" because is ASCII
  14082. $numbytes = 1;
  14083. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  14084. $bytes[] = ($char - 0xC0) << 0x06;
  14085. $numbytes = 2;
  14086. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  14087. $bytes[] = ($char - 0xE0) << 0x0C;
  14088. $numbytes = 3;
  14089. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  14090. $bytes[] = ($char - 0xF0) << 0x12;
  14091. $numbytes = 4;
  14092. } else {
  14093. // use replacement character for other invalid sequences
  14094. $unichar = 0xFFFD;
  14095. $bytes = array();
  14096. $numbytes = 1;
  14097. }
  14098. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  14099. $bytes[] = $char - 0x80;
  14100. if (count($bytes) == $numbytes) {
  14101. // compose UTF-8 bytes to a single unicode value
  14102. $char = $bytes[0];
  14103. for ($j = 1; $j < $numbytes; ++$j) {
  14104. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  14105. }
  14106. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  14107. /* The definition of UTF-8 prohibits encoding character numbers between
  14108. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  14109. encoding form (as surrogate pairs) and do not directly represent
  14110. characters. */
  14111. $unichar = 0xFFFD; // use replacement character
  14112. } else {
  14113. $unichar = $char; // add char to array
  14114. }
  14115. // reset data for next char
  14116. $bytes = array();
  14117. $numbytes = 1;
  14118. }
  14119. } else {
  14120. // use replacement character for other invalid sequences
  14121. $unichar = 0xFFFD;
  14122. $bytes = array();
  14123. $numbytes = 1;
  14124. }
  14125. if ($unichar >= 0) {
  14126. // insert unicode value into array
  14127. $unicode[] = $unichar;
  14128. // store this char for font subsetting
  14129. $this->CurrentFont['subsetchars'][$unichar] = true;
  14130. $unichar = -1;
  14131. }
  14132. }
  14133. // update font subsetchars
  14134. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  14135. // insert new value on cache
  14136. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  14137. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  14138. return $unicode;
  14139. }
  14140. /**
  14141. * Converts UTF-8 strings to UTF16-BE.<br>
  14142. * @param $str (string) string to process.
  14143. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  14144. * @return string
  14145. * @author Nicola Asuni
  14146. * @since 1.53.0.TC005 (2005-01-05)
  14147. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  14148. * @protected
  14149. */
  14150. protected function UTF8ToUTF16BE($str, $setbom = false) {
  14151. if (!$this->isunicode) {
  14152. return $str; // string is not in unicode
  14153. }
  14154. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  14155. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  14156. }
  14157. /**
  14158. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  14159. * @param $str (string) string to process.
  14160. * @return string
  14161. * @author Andrew Whitehead, Nicola Asuni
  14162. * @protected
  14163. * @since 3.2.000 (2008-06-23)
  14164. */
  14165. protected function UTF8ToLatin1($str) {
  14166. if (!$this->isunicode) {
  14167. return $str; // string is not in unicode
  14168. }
  14169. $outstr = ''; // string to be returned
  14170. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  14171. foreach ($unicode as $char) {
  14172. if ($char < 256) {
  14173. $outstr .= chr($char);
  14174. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  14175. // map from UTF-8
  14176. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  14177. } elseif ($char == 0xFFFD) {
  14178. // skip
  14179. } else {
  14180. $outstr .= '?';
  14181. }
  14182. }
  14183. return $outstr;
  14184. }
  14185. /**
  14186. * Converts UTF-8 characters array to array of Latin1 characters<br>
  14187. * @param $unicode (array) array containing UTF-8 unicode values
  14188. * @return array
  14189. * @author Nicola Asuni
  14190. * @protected
  14191. * @since 4.8.023 (2010-01-15)
  14192. */
  14193. protected function UTF8ArrToLatin1($unicode) {
  14194. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  14195. return $unicode;
  14196. }
  14197. $outarr = array(); // array to be returned
  14198. foreach ($unicode as $char) {
  14199. if ($char < 256) {
  14200. $outarr[] = $char;
  14201. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  14202. // map from UTF-8
  14203. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  14204. } elseif ($char == 0xFFFD) {
  14205. // skip
  14206. } else {
  14207. $outarr[] = 63; // '?' character
  14208. }
  14209. }
  14210. return $outarr;
  14211. }
  14212. /**
  14213. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  14214. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  14215. * <pre>
  14216. * Encoding UTF-16:
  14217. *
  14218. * Encoding of a single character from an ISO 10646 character value to
  14219. * UTF-16 proceeds as follows. Let U be the character number, no greater
  14220. * than 0x10FFFF.
  14221. *
  14222. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  14223. * terminate.
  14224. *
  14225. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  14226. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  14227. * represented in 20 bits.
  14228. *
  14229. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  14230. * 0xDC00, respectively. These integers each have 10 bits free to
  14231. * encode the character value, for a total of 20 bits.
  14232. *
  14233. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  14234. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  14235. * bits of W2. Terminate.
  14236. *
  14237. * Graphically, steps 2 through 4 look like:
  14238. * U' = yyyyyyyyyyxxxxxxxxxx
  14239. * W1 = 110110yyyyyyyyyy
  14240. * W2 = 110111xxxxxxxxxx
  14241. * </pre>
  14242. * @param $unicode (array) array containing UTF-8 unicode values
  14243. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  14244. * @return string
  14245. * @protected
  14246. * @author Nicola Asuni
  14247. * @since 2.1.000 (2008-01-08)
  14248. * @see UTF8ToUTF16BE()
  14249. */
  14250. protected function arrUTF8ToUTF16BE($unicode, $setbom = false) {
  14251. $outstr = ''; // string to be returned
  14252. if ($setbom) {
  14253. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  14254. }
  14255. foreach ($unicode as $char) {
  14256. if ($char == 0x200b) {
  14257. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  14258. } elseif ($char == 0xFFFD) {
  14259. $outstr .= "\xFF\xFD"; // replacement character
  14260. } elseif ($char < 0x10000) {
  14261. $outstr .= chr($char >> 0x08);
  14262. $outstr .= chr($char & 0xFF);
  14263. } else {
  14264. $char -= 0x10000;
  14265. $w1 = 0xD800 | ($char >> 0x0a);
  14266. $w2 = 0xDC00 | ($char & 0x3FF);
  14267. $outstr .= chr($w1 >> 0x08);
  14268. $outstr .= chr($w1 & 0xFF);
  14269. $outstr .= chr($w2 >> 0x08);
  14270. $outstr .= chr($w2 & 0xFF);
  14271. }
  14272. }
  14273. return $outstr;
  14274. }
  14275. // ====================================================
  14276. /**
  14277. * Set header font.
  14278. * @param $font (array) font
  14279. * @public
  14280. * @since 1.1
  14281. */
  14282. public function setHeaderFont($font) {
  14283. $this->header_font = $font;
  14284. }
  14285. /**
  14286. * Get header font.
  14287. * @return array()
  14288. * @public
  14289. * @since 4.0.012 (2008-07-24)
  14290. */
  14291. public function getHeaderFont() {
  14292. return $this->header_font;
  14293. }
  14294. /**
  14295. * Set footer font.
  14296. * @param $font (array) font
  14297. * @public
  14298. * @since 1.1
  14299. */
  14300. public function setFooterFont($font) {
  14301. $this->footer_font = $font;
  14302. }
  14303. /**
  14304. * Get Footer font.
  14305. * @return array()
  14306. * @public
  14307. * @since 4.0.012 (2008-07-24)
  14308. */
  14309. public function getFooterFont() {
  14310. return $this->footer_font;
  14311. }
  14312. /**
  14313. * Set language array.
  14314. * @param $language (array)
  14315. * @public
  14316. * @since 1.1
  14317. */
  14318. public function setLanguageArray($language) {
  14319. $this->l = $language;
  14320. if (isset($this->l['a_meta_dir'])) {
  14321. $this->rtl = $this->l['a_meta_dir'] == 'rtl' ? true : false;
  14322. } else {
  14323. $this->rtl = false;
  14324. }
  14325. }
  14326. /**
  14327. * Returns the PDF data.
  14328. * @public
  14329. */
  14330. public function getPDFData() {
  14331. if ($this->state < 3) {
  14332. $this->Close();
  14333. }
  14334. return $this->buffer;
  14335. }
  14336. /**
  14337. * Output anchor link.
  14338. * @param $url (string) link URL or internal link (i.e.: &lt;a href="#23,4.5"&gt;link to page 23 at 4.5 Y position&lt;/a&gt;)
  14339. * @param $name (string) link name
  14340. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  14341. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  14342. * @param $color (array) array of RGB text color
  14343. * @param $style (string) font style (U, D, B, I)
  14344. * @param $firstblock (boolean) if true the string is the starting of a line.
  14345. * @return the number of cells used or the remaining text if $firstline = true;
  14346. * @public
  14347. */
  14348. public function addHtmlLink($url, $name, $fill = false, $firstline = false, $color = '', $style = -1, $firstblock = false) {
  14349. if (!$this->empty_string($url) AND ($url[0] == '#') AND is_numeric($url[1])) {
  14350. // convert url to internal link
  14351. $lnkdata = explode(',', $url);
  14352. if (isset($lnkdata[0])) {
  14353. $page = intval(substr($lnkdata[0], 1));
  14354. if (empty($page) OR ($page <= 0)) {
  14355. $page = $this->page;
  14356. }
  14357. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  14358. $lnky = floatval($lnkdata[1]);
  14359. } else {
  14360. $lnky = 0;
  14361. }
  14362. $url = $this->AddLink();
  14363. $this->SetLink($url, $lnky, $page);
  14364. }
  14365. }
  14366. // store current settings
  14367. $prevcolor = $this->fgcolor;
  14368. $prevstyle = $this->FontStyle;
  14369. if (empty($color)) {
  14370. $this->SetTextColorArray($this->htmlLinkColorArray);
  14371. } else {
  14372. $this->SetTextColorArray($color);
  14373. }
  14374. if ($style == -1) {
  14375. $this->SetFont('', $this->FontStyle . $this->htmlLinkFontStyle);
  14376. } else {
  14377. $this->SetFont('', $this->FontStyle . $style);
  14378. }
  14379. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  14380. // restore settings
  14381. $this->SetFont('', $prevstyle);
  14382. $this->SetTextColorArray($prevcolor);
  14383. return $ret;
  14384. }
  14385. /**
  14386. * Returns an array (RGB or CMYK) from an html color name, or a six-digit (i.e. #3FE5AA), or three-digit (i.e. #7FF) hexadecimal color, or a javascript color array, or javascript color name.
  14387. * @param $hcolor (string) HTML color.
  14388. * @param $defcol (array) Color to return in case of error.
  14389. * @return array RGB or CMYK color, or false in case of error.
  14390. * @public
  14391. */
  14392. public function convertHTMLColorToDec($hcolor = '#FFFFFF', $defcol = array('R' => 128, 'G' => 128, 'B' => 128)) {
  14393. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  14394. $color = strtolower($color);
  14395. // check for javascript color array syntax
  14396. if (strpos($color, '[') !== false) {
  14397. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  14398. $returncolor = array();
  14399. switch ($m[1]) {
  14400. case 'cmyk': {
  14401. // RGB
  14402. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  14403. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  14404. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  14405. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  14406. break;
  14407. }
  14408. case 'rgb': {
  14409. // RGB
  14410. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  14411. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  14412. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  14413. break;
  14414. }
  14415. case 'g': {
  14416. // grayscale
  14417. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  14418. break;
  14419. }
  14420. case 't':
  14421. default: {
  14422. // transparent (empty array)
  14423. break;
  14424. }
  14425. }
  14426. return $returncolor;
  14427. }
  14428. } elseif (($dotpos = strpos($color, '.')) !== false) {
  14429. // remove class parent (i.e.: color.red)
  14430. $color = substr($color, ($dotpos + 1));
  14431. if ($color == 'transparent') {
  14432. // transparent (empty array)
  14433. return array();
  14434. }
  14435. }
  14436. if (strlen($color) == 0) {
  14437. return $defcol;
  14438. }
  14439. // RGB ARRAY
  14440. if (substr($color, 0, 3) == 'rgb') {
  14441. $codes = substr($color, 4);
  14442. $codes = str_replace(')', '', $codes);
  14443. $returncolor = explode(',', $codes);
  14444. foreach ($returncolor as $key => $val) {
  14445. if (strpos($val, '%') > 0) {
  14446. // percentage
  14447. $returncolor[$key] = (255 * intval($val) / 100);
  14448. } else {
  14449. $returncolor[$key] = intval($val);
  14450. }
  14451. // normalize value
  14452. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  14453. }
  14454. return $returncolor;
  14455. }
  14456. // CMYK ARRAY
  14457. if (substr($color, 0, 4) == 'cmyk') {
  14458. $codes = substr($color, 5);
  14459. $codes = str_replace(')', '', $codes);
  14460. $returncolor = explode(',', $codes);
  14461. foreach ($returncolor as $key => $val) {
  14462. if (strpos($val, '%') !== false) {
  14463. // percentage
  14464. $returncolor[$key] = (100 * intval($val) / 100);
  14465. } else {
  14466. $returncolor[$key] = intval($val);
  14467. }
  14468. // normalize value
  14469. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  14470. }
  14471. return $returncolor;
  14472. }
  14473. if ($color{0} != '#') {
  14474. // COLOR NAME
  14475. if (isset($this->webcolor[$color])) {
  14476. // web color
  14477. $color_code = $this->webcolor[$color];
  14478. } else {
  14479. // spot color
  14480. $returncolor = $this->getSpotColor($color);
  14481. if ($returncolor === false) {
  14482. $returncolor = $defcol;
  14483. }
  14484. return $returncolor;
  14485. }
  14486. } else {
  14487. $color_code = substr($color, 1);
  14488. }
  14489. // HEXADECIMAL REPRESENTATION
  14490. switch (strlen($color_code)) {
  14491. case 3: {
  14492. // 3-digit RGB hexadecimal representation
  14493. $r = substr($color_code, 0, 1);
  14494. $g = substr($color_code, 1, 1);
  14495. $b = substr($color_code, 2, 1);
  14496. $returncolor = array();
  14497. $returncolor['R'] = max(0, min(255, hexdec($r . $r)));
  14498. $returncolor['G'] = max(0, min(255, hexdec($g . $g)));
  14499. $returncolor['B'] = max(0, min(255, hexdec($b . $b)));
  14500. break;
  14501. }
  14502. case 6: {
  14503. // 6-digit RGB hexadecimal representation
  14504. $returncolor = array();
  14505. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  14506. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  14507. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  14508. break;
  14509. }
  14510. case 8: {
  14511. // 8-digit CMYK hexadecimal representation
  14512. $returncolor = array();
  14513. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  14514. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  14515. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  14516. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  14517. break;
  14518. }
  14519. default: {
  14520. $returncolor = $defcol;
  14521. break;
  14522. }
  14523. }
  14524. return $returncolor;
  14525. }
  14526. /**
  14527. * Converts pixels to User's Units.
  14528. * @param $px (int) pixels
  14529. * @return float value in user's unit
  14530. * @public
  14531. * @see setImageScale(), getImageScale()
  14532. */
  14533. public function pixelsToUnits($px) {
  14534. return ($px / ($this->imgscale * $this->k));
  14535. }
  14536. /**
  14537. * Reverse function for htmlentities.
  14538. * Convert entities in UTF-8.
  14539. * @param $text_to_convert (string) Text to convert.
  14540. * @return string converted text string
  14541. * @public
  14542. */
  14543. public function unhtmlentities($text_to_convert) {
  14544. return @html_entity_decode($text_to_convert, ENT_QUOTES, $this->encoding);
  14545. }
  14546. // ENCRYPTION METHODS ----------------------------------
  14547. /**
  14548. * Returns a string containing random data to be used as a seed for encryption methods.
  14549. * @param $seed (string) starting seed value
  14550. * @return string containing random data
  14551. * @author Nicola Asuni
  14552. * @since 5.9.006 (2010-10-19)
  14553. * @protected
  14554. */
  14555. protected function getRandomSeed($seed = '') {
  14556. $seed .= microtime();
  14557. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  14558. // this is not used on windows systems because it is very slow for a know bug
  14559. $seed .= openssl_random_pseudo_bytes(512);
  14560. } else {
  14561. for ($i = 0; $i < 23; ++$i) {
  14562. $seed .= uniqid('', true);
  14563. }
  14564. }
  14565. $seed .= uniqid('', true);
  14566. $seed .= rand();
  14567. $seed .= getmypid();
  14568. $seed .= __FILE__;
  14569. $seed .= $this->bufferlen;
  14570. if (isset($_SERVER['REMOTE_ADDR'])) {
  14571. $seed .= $_SERVER['REMOTE_ADDR'];
  14572. }
  14573. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  14574. $seed .= $_SERVER['HTTP_USER_AGENT'];
  14575. }
  14576. if (isset($_SERVER['HTTP_ACCEPT'])) {
  14577. $seed .= $_SERVER['HTTP_ACCEPT'];
  14578. }
  14579. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  14580. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  14581. }
  14582. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  14583. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  14584. }
  14585. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  14586. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  14587. }
  14588. $seed .= rand();
  14589. $seed .= uniqid('', true);
  14590. $seed .= microtime();
  14591. return $seed;
  14592. }
  14593. /**
  14594. * Compute encryption key depending on object number where the encrypted data is stored.
  14595. * This is used for all strings and streams without crypt filter specifier.
  14596. * @param $n (int) object number
  14597. * @return int object key
  14598. * @protected
  14599. * @author Nicola Asuni
  14600. * @since 2.0.000 (2008-01-02)
  14601. */
  14602. protected function _objectkey($n) {
  14603. $objkey = $this->encryptdata['key'] . pack('VXxx', $n);
  14604. if ($this->encryptdata['mode'] == 2) { // AES-128
  14605. // AES padding
  14606. $objkey .= "\x73\x41\x6C\x54"; // sAlT
  14607. }
  14608. $objkey = substr($this->_md5_16($objkey), 0, (($this->encryptdata['Length'] / 8) + 5));
  14609. $objkey = substr($objkey, 0, 16);
  14610. return $objkey;
  14611. }
  14612. /**
  14613. * Encrypt the input string.
  14614. * @param $n (int) object number
  14615. * @param $s (string) data string to encrypt
  14616. * @return encrypted string
  14617. * @protected
  14618. * @author Nicola Asuni
  14619. * @since 5.0.005 (2010-05-11)
  14620. */
  14621. protected function _encrypt_data($n, $s) {
  14622. if (!$this->encrypted) {
  14623. return $s;
  14624. }
  14625. switch ($this->encryptdata['mode']) {
  14626. case 0: // RC4-40
  14627. case 1: { // RC4-128
  14628. $s = $this->_RC4($this->_objectkey($n), $s);
  14629. break;
  14630. }
  14631. case 2: { // AES-128
  14632. $s = $this->_AES($this->_objectkey($n), $s);
  14633. break;
  14634. }
  14635. case 3: { // AES-256
  14636. $s = $this->_AES($this->encryptdata['key'], $s);
  14637. break;
  14638. }
  14639. }
  14640. return $s;
  14641. }
  14642. /**
  14643. * Put encryption on PDF document.
  14644. * @protected
  14645. * @author Nicola Asuni
  14646. * @since 2.0.000 (2008-01-02)
  14647. */
  14648. protected function _putencryption() {
  14649. if (!$this->encrypted) {
  14650. return;
  14651. }
  14652. $this->encryptdata['objid'] = $this->_newobj();
  14653. $out = '<<';
  14654. if (!isset($this->encryptdata['Filter']) OR empty($this->encryptdata['Filter'])) {
  14655. $this->encryptdata['Filter'] = 'Standard';
  14656. }
  14657. $out .= ' /Filter /' . $this->encryptdata['Filter'];
  14658. if (isset($this->encryptdata['SubFilter']) AND !empty($this->encryptdata['SubFilter'])) {
  14659. $out .= ' /SubFilter /' . $this->encryptdata['SubFilter'];
  14660. }
  14661. if (!isset($this->encryptdata['V']) OR empty($this->encryptdata['V'])) {
  14662. $this->encryptdata['V'] = 1;
  14663. }
  14664. // V is a code specifying the algorithm to be used in encrypting and decrypting the document
  14665. $out .= ' /V ' . $this->encryptdata['V'];
  14666. if (isset($this->encryptdata['Length']) AND !empty($this->encryptdata['Length'])) {
  14667. // The length of the encryption key, in bits. The value shall be a multiple of 8, in the range 40 to 256
  14668. $out .= ' /Length ' . $this->encryptdata['Length'];
  14669. } else {
  14670. $out .= ' /Length 40';
  14671. }
  14672. if ($this->encryptdata['V'] >= 4) {
  14673. if (!isset($this->encryptdata['StmF']) OR empty($this->encryptdata['StmF'])) {
  14674. $this->encryptdata['StmF'] = 'Identity';
  14675. }
  14676. if (!isset($this->encryptdata['StrF']) OR empty($this->encryptdata['StrF'])) {
  14677. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  14678. $this->encryptdata['StrF'] = 'Identity';
  14679. }
  14680. // A dictionary whose keys shall be crypt filter names and whose values shall be the corresponding crypt filter dictionaries.
  14681. if (isset($this->encryptdata['CF']) AND !empty($this->encryptdata['CF'])) {
  14682. $out .= ' /CF <<';
  14683. $out .= ' /' . $this->encryptdata['StmF'] . ' <<';
  14684. $out .= ' /Type /CryptFilter';
  14685. if (isset($this->encryptdata['CF']['CFM']) AND !empty($this->encryptdata['CF']['CFM'])) {
  14686. // The method used
  14687. $out .= ' /CFM /' . $this->encryptdata['CF']['CFM'];
  14688. if ($this->encryptdata['pubkey']) {
  14689. $out .= ' /Recipients [';
  14690. foreach ($this->encryptdata['Recipients'] as $rec) {
  14691. $out .= ' <' . $rec . '>';
  14692. }
  14693. $out .= ' ]';
  14694. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) {
  14695. $out .= ' /EncryptMetadata false';
  14696. } else {
  14697. $out .= ' /EncryptMetadata true';
  14698. }
  14699. }
  14700. } else {
  14701. $out .= ' /CFM /None';
  14702. }
  14703. if (isset($this->encryptdata['CF']['AuthEvent']) AND !empty($this->encryptdata['CF']['AuthEvent'])) {
  14704. // The event to be used to trigger the authorization that is required to access encryption keys used by this filter.
  14705. $out .= ' /AuthEvent /' . $this->encryptdata['CF']['AuthEvent'];
  14706. } else {
  14707. $out .= ' /AuthEvent /DocOpen';
  14708. }
  14709. if (isset($this->encryptdata['CF']['Length']) AND !empty($this->encryptdata['CF']['Length'])) {
  14710. // The bit length of the encryption key.
  14711. $out .= ' /Length ' . $this->encryptdata['CF']['Length'];
  14712. }
  14713. $out .= ' >> >>';
  14714. }
  14715. // The name of the crypt filter that shall be used by default when decrypting streams.
  14716. $out .= ' /StmF /' . $this->encryptdata['StmF'];
  14717. // The name of the crypt filter that shall be used when decrypting all strings in the document.
  14718. $out .= ' /StrF /' . $this->encryptdata['StrF'];
  14719. if (isset($this->encryptdata['EFF']) AND !empty($this->encryptdata['EFF'])) {
  14720. // The name of the crypt filter that shall be used when encrypting embedded file streams that do not have their own crypt filter specifier.
  14721. $out .= ' /EFF /' . $this->encryptdata[''];
  14722. }
  14723. }
  14724. // Additional encryption dictionary entries for the standard security handler
  14725. if ($this->encryptdata['pubkey']) {
  14726. if (($this->encryptdata['V'] < 4) AND isset($this->encryptdata['Recipients']) AND !empty($this->encryptdata['Recipients'])) {
  14727. $out .= ' /Recipients [';
  14728. foreach ($this->encryptdata['Recipients'] as $rec) {
  14729. $out .= ' <' . $rec . '>';
  14730. }
  14731. $out .= ' ]';
  14732. }
  14733. } else {
  14734. $out .= ' /R';
  14735. if ($this->encryptdata['V'] == 5) { // AES-256
  14736. $out .= ' 5';
  14737. $out .= ' /OE (' . $this->_escape($this->encryptdata['OE']) . ')';
  14738. $out .= ' /UE (' . $this->_escape($this->encryptdata['UE']) . ')';
  14739. $out .= ' /Perms (' . $this->_escape($this->encryptdata['perms']) . ')';
  14740. } elseif ($this->encryptdata['V'] == 4) { // AES-128
  14741. $out .= ' 4';
  14742. } elseif ($this->encryptdata['V'] < 2) { // RC-40
  14743. $out .= ' 2';
  14744. } else { // RC-128
  14745. $out .= ' 3';
  14746. }
  14747. $out .= ' /O (' . $this->_escape($this->encryptdata['O']) . ')';
  14748. $out .= ' /U (' . $this->_escape($this->encryptdata['U']) . ')';
  14749. $out .= ' /P ' . $this->encryptdata['P'];
  14750. if (isset($this->encryptdata['EncryptMetadata']) AND (!$this->encryptdata['EncryptMetadata'])) {
  14751. $out .= ' /EncryptMetadata false';
  14752. } else {
  14753. $out .= ' /EncryptMetadata true';
  14754. }
  14755. }
  14756. $out .= ' >>';
  14757. $out .= "\n" . 'endobj';
  14758. $this->_out($out);
  14759. }
  14760. /**
  14761. * Returns the input text encrypted using RC4 algorithm and the specified key.
  14762. * RC4 is the standard encryption algorithm used in PDF format
  14763. * @param $key (string) encryption key
  14764. * @param $text (String) input text to be encrypted
  14765. * @return String encrypted text
  14766. * @protected
  14767. * @since 2.0.000 (2008-01-02)
  14768. * @author Klemen Vodopivec, Nicola Asuni
  14769. */
  14770. protected function _RC4($key, $text) {
  14771. if (function_exists('mcrypt_decrypt') AND ($out = @mcrypt_decrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  14772. // try to use mcrypt function if exist
  14773. return $out;
  14774. }
  14775. if ($this->last_enc_key != $key) {
  14776. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  14777. $rc4 = range(0, 255);
  14778. $j = 0;
  14779. for ($i = 0; $i < 256; ++$i) {
  14780. $t = $rc4[$i];
  14781. $j = ($j + $t + ord($k[$i])) % 256;
  14782. $rc4[$i] = $rc4[$j];
  14783. $rc4[$j] = $t;
  14784. }
  14785. $this->last_enc_key = $key;
  14786. $this->last_enc_key_c = $rc4;
  14787. } else {
  14788. $rc4 = $this->last_enc_key_c;
  14789. }
  14790. $len = strlen($text);
  14791. $a = 0;
  14792. $b = 0;
  14793. $out = '';
  14794. for ($i = 0; $i < $len; ++$i) {
  14795. $a = ($a + 1) % 256;
  14796. $t = $rc4[$a];
  14797. $b = ($b + $t) % 256;
  14798. $rc4[$a] = $rc4[$b];
  14799. $rc4[$b] = $t;
  14800. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  14801. $out .= chr(ord($text[$i]) ^ $k);
  14802. }
  14803. return $out;
  14804. }
  14805. /**
  14806. * Returns the input text exrypted using AES algorithm and the specified key.
  14807. * This method requires mcrypt.
  14808. * @param $key (string) encryption key
  14809. * @param $text (String) input text to be encrypted
  14810. * @return String encrypted text
  14811. * @protected
  14812. * @author Nicola Asuni
  14813. * @since 5.0.005 (2010-05-11)
  14814. */
  14815. protected function _AES($key, $text) {
  14816. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  14817. $padding = 16 - (strlen($text) % 16);
  14818. $text .= str_repeat(chr($padding), $padding);
  14819. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  14820. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  14821. $text = $iv . $text;
  14822. return $text;
  14823. }
  14824. /**
  14825. * Encrypts a string using MD5 and returns it's value as a binary string.
  14826. * @param $str (string) input string
  14827. * @return String MD5 encrypted binary string
  14828. * @protected
  14829. * @since 2.0.000 (2008-01-02)
  14830. * @author Klemen Vodopivec
  14831. */
  14832. protected function _md5_16($str) {
  14833. return pack('H*', md5($str));
  14834. }
  14835. /**
  14836. * Compute U value (used for encryption)
  14837. * @return string U value
  14838. * @protected
  14839. * @since 2.0.000 (2008-01-02)
  14840. * @author Nicola Asuni
  14841. */
  14842. protected function _Uvalue() {
  14843. if ($this->encryptdata['mode'] == 0) { // RC4-40
  14844. return $this->_RC4($this->encryptdata['key'], $this->enc_padding);
  14845. } elseif ($this->encryptdata['mode'] < 3) { // RC4-128, AES-128
  14846. $tmp = $this->_md5_16($this->enc_padding . $this->encryptdata['fileid']);
  14847. $enc = $this->_RC4($this->encryptdata['key'], $tmp);
  14848. $len = strlen($tmp);
  14849. for ($i = 1; $i <= 19; ++$i) {
  14850. $ek = '';
  14851. for ($j = 0; $j < $len; ++$j) {
  14852. $ek .= chr(ord($this->encryptdata['key'][$j]) ^ $i);
  14853. }
  14854. $enc = $this->_RC4($ek, $enc);
  14855. }
  14856. $enc .= str_repeat("\x00", 16);
  14857. return substr($enc, 0, 32);
  14858. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  14859. $seed = $this->_md5_16($this->getRandomSeed());
  14860. // User Validation Salt
  14861. $this->encryptdata['UVS'] = substr($seed, 0, 8);
  14862. // User Key Salt
  14863. $this->encryptdata['UKS'] = substr($seed, 8, 16);
  14864. return hash('sha256', $this->encryptdata['user_password'] . $this->encryptdata['UVS'], true) . $this->encryptdata['UVS'] . $this->encryptdata['UKS'];
  14865. }
  14866. }
  14867. /**
  14868. * Compute UE value (used for encryption)
  14869. * @return string UE value
  14870. * @protected
  14871. * @since 5.9.006 (2010-10-19)
  14872. * @author Nicola Asuni
  14873. */
  14874. protected function _UEvalue() {
  14875. $hashkey = hash('sha256', $this->encryptdata['user_password'] . $this->encryptdata['UKS'], true);
  14876. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  14877. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  14878. }
  14879. /**
  14880. * Compute O value (used for encryption)
  14881. * @return string O value
  14882. * @protected
  14883. * @since 2.0.000 (2008-01-02)
  14884. * @author Nicola Asuni
  14885. */
  14886. protected function _Ovalue() {
  14887. if ($this->encryptdata['mode'] < 3) { // RC4-40, RC4-128, AES-128
  14888. $tmp = $this->_md5_16($this->encryptdata['owner_password']);
  14889. if ($this->encryptdata['mode'] > 0) {
  14890. for ($i = 0; $i < 50; ++$i) {
  14891. $tmp = $this->_md5_16($tmp);
  14892. }
  14893. }
  14894. $owner_key = substr($tmp, 0, ($this->encryptdata['Length'] / 8));
  14895. $enc = $this->_RC4($owner_key, $this->encryptdata['user_password']);
  14896. if ($this->encryptdata['mode'] > 0) {
  14897. $len = strlen($owner_key);
  14898. for ($i = 1; $i <= 19; ++$i) {
  14899. $ek = '';
  14900. for ($j = 0; $j < $len; ++$j) {
  14901. $ek .= chr(ord($owner_key[$j]) ^ $i);
  14902. }
  14903. $enc = $this->_RC4($ek, $enc);
  14904. }
  14905. }
  14906. return $enc;
  14907. } elseif ($this->encryptdata['mode'] == 3) { // AES-256
  14908. $seed = $this->_md5_16($this->getRandomSeed());
  14909. // Owner Validation Salt
  14910. $this->encryptdata['OVS'] = substr($seed, 0, 8);
  14911. // Owner Key Salt
  14912. $this->encryptdata['OKS'] = substr($seed, 8, 16);
  14913. return hash('sha256', $this->encryptdata['owner_password'] . $this->encryptdata['OVS'] . $this->encryptdata['U'], true) . $this->encryptdata['OVS'] . $this->encryptdata['OKS'];
  14914. }
  14915. }
  14916. /**
  14917. * Compute OE value (used for encryption)
  14918. * @return string OE value
  14919. * @protected
  14920. * @since 5.9.006 (2010-10-19)
  14921. * @author Nicola Asuni
  14922. */
  14923. protected function _OEvalue() {
  14924. $hashkey = hash('sha256', $this->encryptdata['owner_password'] . $this->encryptdata['OKS'] . $this->encryptdata['U'], true);
  14925. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
  14926. return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $hashkey, $this->encryptdata['key'], MCRYPT_MODE_CBC, $iv);
  14927. }
  14928. /**
  14929. * Convert password for AES-256 encryption mode
  14930. * @param $password (string) password
  14931. * @return string password
  14932. * @protected
  14933. * @since 5.9.006 (2010-10-19)
  14934. * @author Nicola Asuni
  14935. */
  14936. protected function _fixAES256Password($password) {
  14937. $psw = ''; // password to be returned
  14938. $psw_array = $this->utf8Bidi($this->UTF8StringToArray($password), $password, $this->rtl);
  14939. foreach ($psw_array as $c) {
  14940. $psw .= $this->unichr($c);
  14941. }
  14942. return substr($psw, 0, 127);
  14943. }
  14944. /**
  14945. * Compute encryption key
  14946. * @protected
  14947. * @since 2.0.000 (2008-01-02)
  14948. * @author Nicola Asuni
  14949. */
  14950. protected function _generateencryptionkey() {
  14951. $keybytelen = ($this->encryptdata['Length'] / 8);
  14952. if (!$this->encryptdata['pubkey']) { // standard mode
  14953. if ($this->encryptdata['mode'] == 3) { // AES-256
  14954. // generate 256 bit random key
  14955. $this->encryptdata['key'] = substr(hash('sha256', $this->getRandomSeed(), true), 0, $keybytelen);
  14956. // truncate passwords
  14957. $this->encryptdata['user_password'] = $this->_fixAES256Password($this->encryptdata['user_password']);
  14958. $this->encryptdata['owner_password'] = $this->_fixAES256Password($this->encryptdata['owner_password']);
  14959. // Compute U value
  14960. $this->encryptdata['U'] = $this->_Uvalue();
  14961. // Compute UE value
  14962. $this->encryptdata['UE'] = $this->_UEvalue();
  14963. // Compute O value
  14964. $this->encryptdata['O'] = $this->_Ovalue();
  14965. // Compute OE value
  14966. $this->encryptdata['OE'] = $this->_OEvalue();
  14967. // Compute P value
  14968. $this->encryptdata['P'] = $this->encryptdata['protection'];
  14969. // Computing the encryption dictionary's Perms (permissions) value
  14970. $perms = $this->getEncPermissionsString($this->encryptdata['protection']); // bytes 0-3
  14971. $perms .= chr(255) . chr(255) . chr(255) . chr(255); // bytes 4-7
  14972. if (isset($this->encryptdata['CF']['EncryptMetadata']) AND (!$this->encryptdata['CF']['EncryptMetadata'])) { // byte 8
  14973. $perms .= 'F';
  14974. } else {
  14975. $perms .= 'T';
  14976. }
  14977. $perms .= 'adb'; // bytes 9-11
  14978. $perms .= 'nick'; // bytes 12-15
  14979. $iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB));
  14980. $this->encryptdata['perms'] = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->encryptdata['key'], $perms, MCRYPT_MODE_ECB, $iv);
  14981. } else { // RC4-40, RC4-128, AES-128
  14982. // Pad passwords
  14983. $this->encryptdata['user_password'] = substr($this->encryptdata['user_password'] . $this->enc_padding, 0, 32);
  14984. $this->encryptdata['owner_password'] = substr($this->encryptdata['owner_password'] . $this->enc_padding, 0, 32);
  14985. // Compute O value
  14986. $this->encryptdata['O'] = $this->_Ovalue();
  14987. // get default permissions (reverse byte order)
  14988. $permissions = $this->getEncPermissionsString($this->encryptdata['protection']);
  14989. // Compute encryption key
  14990. $tmp = $this->_md5_16($this->encryptdata['user_password'] . $this->encryptdata['O'] . $permissions . $this->encryptdata['fileid']);
  14991. if ($this->encryptdata['mode'] > 0) {
  14992. for ($i = 0; $i < 50; ++$i) {
  14993. $tmp = $this->_md5_16(substr($tmp, 0, $keybytelen));
  14994. }
  14995. }
  14996. $this->encryptdata['key'] = substr($tmp, 0, $keybytelen);
  14997. // Compute U value
  14998. $this->encryptdata['U'] = $this->_Uvalue();
  14999. // Compute P value
  15000. $this->encryptdata['P'] = $this->encryptdata['protection'];
  15001. }
  15002. } else { // Public-Key mode
  15003. // random 20-byte seed
  15004. $seed = sha1($this->getRandomSeed(), true);
  15005. $recipient_bytes = '';
  15006. foreach ($this->encryptdata['pubkeys'] as $pubkey) {
  15007. // for each public certificate
  15008. if (isset($pubkey['p'])) {
  15009. $pkprotection = $this->getUserPermissionCode($pubkey['p'], $this->encryptdata['mode']);
  15010. } else {
  15011. $pkprotection = $this->encryptdata['protection'];
  15012. }
  15013. // get default permissions (reverse byte order)
  15014. $pkpermissions = $this->getEncPermissionsString($pkprotection);
  15015. // envelope data
  15016. $envelope = $seed . $pkpermissions;
  15017. // write the envelope data to a temporary file
  15018. $tempkeyfile = tempnam(K_PATH_CACHE, 'tmpkey_');
  15019. $f = fopen($tempkeyfile, 'wb');
  15020. if (!$f) {
  15021. $this->Error('Unable to create temporary key file: ' . $tempkeyfile);
  15022. }
  15023. $envelope_length = strlen($envelope);
  15024. fwrite($f, $envelope, $envelope_length);
  15025. fclose($f);
  15026. $tempencfile = tempnam(K_PATH_CACHE, 'tmpenc_');
  15027. if (!openssl_pkcs7_encrypt($tempkeyfile, $tempencfile, $pubkey['c'], array(), PKCS7_BINARY | PKCS7_DETACHED)) {
  15028. $this->Error('Unable to encrypt the file: ' . $tempkeyfile);
  15029. }
  15030. unlink($tempkeyfile);
  15031. // read encryption signature
  15032. $signature = file_get_contents($tempencfile, false, null, $envelope_length);
  15033. unlink($tempencfile);
  15034. // extract signature
  15035. $signature = substr($signature, strpos($signature, 'Content-Disposition'));
  15036. $tmparr = explode("\n\n", $signature);
  15037. $signature = trim($tmparr[1]);
  15038. unset($tmparr);
  15039. // decode signature
  15040. $signature = base64_decode($signature);
  15041. // convert signature to hex
  15042. $hexsignature = current(unpack('H*', $signature));
  15043. // store signature on recipients array
  15044. $this->encryptdata['Recipients'][] = $hexsignature;
  15045. // The bytes of each item in the Recipients array of PKCS#7 objects in the order in which they appear in the array
  15046. $recipient_bytes .= $signature;
  15047. }
  15048. // calculate encryption key
  15049. if ($this->encryptdata['mode'] == 3) { // AES-256
  15050. $this->encryptdata['key'] = substr(hash('sha256', $seed . $recipient_bytes, true), 0, $keybytelen);
  15051. } else { // RC4-40, RC4-128, AES-128
  15052. $this->encryptdata['key'] = substr(sha1($seed . $recipient_bytes, true), 0, $keybytelen);
  15053. }
  15054. }
  15055. }
  15056. /**
  15057. * Return the premission code used on encryption (P value).
  15058. * @param $permissions (Array) the set of permissions (specify the ones you want to block).
  15059. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  15060. * @protected
  15061. * @since 5.0.005 (2010-05-12)
  15062. * @author Nicola Asuni
  15063. */
  15064. protected function getUserPermissionCode($permissions, $mode = 0) {
  15065. $options = array(
  15066. 'owner' => 2, // bit 2 -- inverted logic: cleared by default
  15067. 'print' => 4, // bit 3
  15068. 'modify' => 8, // bit 4
  15069. 'copy' => 16, // bit 5
  15070. 'annot-forms' => 32, // bit 6
  15071. 'fill-forms' => 256, // bit 9
  15072. 'extract' => 512, // bit 10
  15073. 'assemble' => 1024, // bit 11
  15074. 'print-high' => 2048 // bit 12
  15075. );
  15076. $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
  15077. foreach ($permissions as $permission) {
  15078. if (!isset($options[$permission])) {
  15079. $this->Error('Incorrect permission: ' . $permission);
  15080. }
  15081. if (($mode > 0) OR ($options[$permission] <= 32)) {
  15082. // set only valid permissions
  15083. if ($options[$permission] == 2) {
  15084. // the logic for bit 2 is inverted (cleared by default)
  15085. $protection += $options[$permission];
  15086. } else {
  15087. $protection -= $options[$permission];
  15088. }
  15089. }
  15090. }
  15091. return $protection;
  15092. }
  15093. /**
  15094. * Set document protection
  15095. * Remark: the protection against modification is for people who have the full Acrobat product.
  15096. * 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.
  15097. * 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.
  15098. * @param $permissions (Array) the set of permissions (specify the ones you want to block):<ul><li>print : Print the document;</li><li>modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';</li><li>copy : Copy or otherwise extract text and graphics from the document;</li><li>annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);</li><li>fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;</li><li>extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);</li><li>assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;</li><li>print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.</li><li>owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.</li></ul>
  15099. * @param $user_pass (String) user password. Empty by default.
  15100. * @param $owner_pass (String) owner password. If not specified, a random value is used.
  15101. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  15102. * @param $pubkeys (String) array of recipients containing public-key certificates ('c') and permissions ('p'). For example: array(array('c' => 'file://../tcpdf.crt', 'p' => array('print')))
  15103. * @public
  15104. * @since 2.0.000 (2008-01-02)
  15105. * @author Nicola Asuni
  15106. */
  15107. public function SetProtection($permissions = array('print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble', 'print-high'), $user_pass = '', $owner_pass = null, $mode = 0, $pubkeys = null) {
  15108. if ($this->pdfa_mode) {
  15109. // encryption is not allowed in PDF/A mode
  15110. return;
  15111. }
  15112. $this->encryptdata['protection'] = $this->getUserPermissionCode($permissions, $mode);
  15113. if (($pubkeys !== null) AND (is_array($pubkeys))) {
  15114. // public-key mode
  15115. $this->encryptdata['pubkeys'] = $pubkeys;
  15116. if ($mode == 0) {
  15117. // public-Key Security requires at least 128 bit
  15118. $mode = 1;
  15119. }
  15120. if (!function_exists('openssl_pkcs7_encrypt')) {
  15121. $this->Error('Public-Key Security requires openssl library.');
  15122. }
  15123. // Set Public-Key filter (availabe are: Entrust.PPKEF, Adobe.PPKLite, Adobe.PubSec)
  15124. $this->encryptdata['pubkey'] = true;
  15125. $this->encryptdata['Filter'] = 'Adobe.PubSec';
  15126. $this->encryptdata['StmF'] = 'DefaultCryptFilter';
  15127. $this->encryptdata['StrF'] = 'DefaultCryptFilter';
  15128. } else {
  15129. // standard mode (password mode)
  15130. $this->encryptdata['pubkey'] = false;
  15131. $this->encryptdata['Filter'] = 'Standard';
  15132. $this->encryptdata['StmF'] = 'StdCF';
  15133. $this->encryptdata['StrF'] = 'StdCF';
  15134. }
  15135. if ($mode > 1) { // AES
  15136. if (!extension_loaded('mcrypt')) {
  15137. $this->Error('AES encryption requires mcrypt library (http://www.php.net/manual/en/mcrypt.requirements.php).');
  15138. }
  15139. if (mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) === false) {
  15140. $this->Error('AES encryption requires MCRYPT_RIJNDAEL_128 cypher.');
  15141. }
  15142. if (($mode == 3) AND !function_exists('hash')) {
  15143. // the Hash extension requires no external libraries and is enabled by default as of PHP 5.1.2.
  15144. $this->Error('AES 256 encryption requires HASH Message Digest Framework (http://www.php.net/manual/en/book.hash.php).');
  15145. }
  15146. }
  15147. if ($owner_pass === null) {
  15148. $owner_pass = md5($this->getRandomSeed());
  15149. }
  15150. $this->encryptdata['user_password'] = $user_pass;
  15151. $this->encryptdata['owner_password'] = $owner_pass;
  15152. $this->encryptdata['mode'] = $mode;
  15153. switch ($mode) {
  15154. case 0: { // RC4 40 bit
  15155. $this->encryptdata['V'] = 1;
  15156. $this->encryptdata['Length'] = 40;
  15157. $this->encryptdata['CF']['CFM'] = 'V2';
  15158. break;
  15159. }
  15160. case 1: { // RC4 128 bit
  15161. $this->encryptdata['V'] = 2;
  15162. $this->encryptdata['Length'] = 128;
  15163. $this->encryptdata['CF']['CFM'] = 'V2';
  15164. if ($this->encryptdata['pubkey']) {
  15165. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s4';
  15166. $this->encryptdata['Recipients'] = array();
  15167. }
  15168. break;
  15169. }
  15170. case 2: { // AES 128 bit
  15171. $this->encryptdata['V'] = 4;
  15172. $this->encryptdata['Length'] = 128;
  15173. $this->encryptdata['CF']['CFM'] = 'AESV2';
  15174. $this->encryptdata['CF']['Length'] = 128;
  15175. if ($this->encryptdata['pubkey']) {
  15176. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  15177. $this->encryptdata['Recipients'] = array();
  15178. }
  15179. break;
  15180. }
  15181. case 3: { // AES 256 bit
  15182. $this->encryptdata['V'] = 5;
  15183. $this->encryptdata['Length'] = 256;
  15184. $this->encryptdata['CF']['CFM'] = 'AESV3';
  15185. $this->encryptdata['CF']['Length'] = 256;
  15186. if ($this->encryptdata['pubkey']) {
  15187. $this->encryptdata['SubFilter'] = 'adbe.pkcs7.s5';
  15188. $this->encryptdata['Recipients'] = array();
  15189. }
  15190. break;
  15191. }
  15192. }
  15193. $this->encrypted = true;
  15194. $this->encryptdata['fileid'] = $this->convertHexStringToString($this->file_id);
  15195. $this->_generateencryptionkey();
  15196. }
  15197. /**
  15198. * Convert hexadecimal string to string
  15199. * @param $bs (string) byte-string to convert
  15200. * @return String
  15201. * @protected
  15202. * @since 5.0.005 (2010-05-12)
  15203. * @author Nicola Asuni
  15204. */
  15205. protected function convertHexStringToString($bs) {
  15206. $string = ''; // string to be returned
  15207. $bslength = strlen($bs);
  15208. if (($bslength % 2) != 0) {
  15209. // padding
  15210. $bs .= '0';
  15211. ++$bslength;
  15212. }
  15213. for ($i = 0; $i < $bslength; $i += 2) {
  15214. $string .= chr(hexdec($bs[$i] . $bs[($i + 1)]));
  15215. }
  15216. return $string;
  15217. }
  15218. /**
  15219. * Convert string to hexadecimal string (byte string)
  15220. * @param $s (string) string to convert
  15221. * @return byte string
  15222. * @protected
  15223. * @since 5.0.010 (2010-05-17)
  15224. * @author Nicola Asuni
  15225. */
  15226. protected function convertStringToHexString($s) {
  15227. $bs = '';
  15228. $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  15229. foreach ($chars as $c) {
  15230. $bs .= sprintf('%02s', dechex(ord($c)));
  15231. }
  15232. return $bs;
  15233. }
  15234. /**
  15235. * Convert encryption P value to a string of bytes, low-order byte first.
  15236. * @param $protection (string) 32bit encryption permission value (P value)
  15237. * @return String
  15238. * @protected
  15239. * @since 5.0.005 (2010-05-12)
  15240. * @author Nicola Asuni
  15241. */
  15242. protected function getEncPermissionsString($protection) {
  15243. $binprot = sprintf('%032b', $protection);
  15244. $str = chr(bindec(substr($binprot, 24, 8)));
  15245. $str .= chr(bindec(substr($binprot, 16, 8)));
  15246. $str .= chr(bindec(substr($binprot, 8, 8)));
  15247. $str .= chr(bindec(substr($binprot, 0, 8)));
  15248. return $str;
  15249. }
  15250. // END OF ENCRYPTION FUNCTIONS -------------------------
  15251. // START TRANSFORMATIONS SECTION -----------------------
  15252. /**
  15253. * Starts a 2D tranformation saving current graphic state.
  15254. * This function must be called before scaling, mirroring, translation, rotation and skewing.
  15255. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  15256. * @public
  15257. * @since 2.1.000 (2008-01-07)
  15258. * @see StartTransform(), StopTransform()
  15259. */
  15260. public function StartTransform() {
  15261. if ($this->state != 2) {
  15262. return;
  15263. }
  15264. $this->_out('q');
  15265. if ($this->inxobj) {
  15266. // we are inside an XObject template
  15267. $this->xobjects[$this->xobjid]['transfmrk'][] = strlen($this->xobjects[$this->xobjid]['outdata']);
  15268. } else {
  15269. $this->transfmrk[$this->page][] = $this->pagelen[$this->page];
  15270. }
  15271. ++$this->transfmatrix_key;
  15272. $this->transfmatrix[$this->transfmatrix_key] = array();
  15273. }
  15274. /**
  15275. * Stops a 2D tranformation restoring previous graphic state.
  15276. * This function must be called after scaling, mirroring, translation, rotation and skewing.
  15277. * Use StartTransform() before, and StopTransform() after the transformations to restore the normal behavior.
  15278. * @public
  15279. * @since 2.1.000 (2008-01-07)
  15280. * @see StartTransform(), StopTransform()
  15281. */
  15282. public function StopTransform() {
  15283. if ($this->state != 2) {
  15284. return;
  15285. }
  15286. $this->_out('Q');
  15287. if (isset($this->transfmatrix[$this->transfmatrix_key])) {
  15288. array_pop($this->transfmatrix[$this->transfmatrix_key]);
  15289. --$this->transfmatrix_key;
  15290. }
  15291. if ($this->inxobj) {
  15292. // we are inside an XObject template
  15293. array_pop($this->xobjects[$this->xobjid]['transfmrk']);
  15294. } else {
  15295. array_pop($this->transfmrk[$this->page]);
  15296. }
  15297. }
  15298. /**
  15299. * Horizontal Scaling.
  15300. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  15301. * @param $x (int) abscissa of the scaling center. Default is current x position
  15302. * @param $y (int) ordinate of the scaling center. Default is current y position
  15303. * @public
  15304. * @since 2.1.000 (2008-01-07)
  15305. * @see StartTransform(), StopTransform()
  15306. */
  15307. public function ScaleX($s_x, $x = '', $y = '') {
  15308. $this->Scale($s_x, 100, $x, $y);
  15309. }
  15310. /**
  15311. * Vertical Scaling.
  15312. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  15313. * @param $x (int) abscissa of the scaling center. Default is current x position
  15314. * @param $y (int) ordinate of the scaling center. Default is current y position
  15315. * @public
  15316. * @since 2.1.000 (2008-01-07)
  15317. * @see StartTransform(), StopTransform()
  15318. */
  15319. public function ScaleY($s_y, $x = '', $y = '') {
  15320. $this->Scale(100, $s_y, $x, $y);
  15321. }
  15322. /**
  15323. * Vertical and horizontal proportional Scaling.
  15324. * @param $s (float) scaling factor for width and height as percent. 0 is not allowed.
  15325. * @param $x (int) abscissa of the scaling center. Default is current x position
  15326. * @param $y (int) ordinate of the scaling center. Default is current y position
  15327. * @public
  15328. * @since 2.1.000 (2008-01-07)
  15329. * @see StartTransform(), StopTransform()
  15330. */
  15331. public function ScaleXY($s, $x = '', $y = '') {
  15332. $this->Scale($s, $s, $x, $y);
  15333. }
  15334. /**
  15335. * Vertical and horizontal non-proportional Scaling.
  15336. * @param $s_x (float) scaling factor for width as percent. 0 is not allowed.
  15337. * @param $s_y (float) scaling factor for height as percent. 0 is not allowed.
  15338. * @param $x (int) abscissa of the scaling center. Default is current x position
  15339. * @param $y (int) ordinate of the scaling center. Default is current y position
  15340. * @public
  15341. * @since 2.1.000 (2008-01-07)
  15342. * @see StartTransform(), StopTransform()
  15343. */
  15344. public function Scale($s_x, $s_y, $x = '', $y = '') {
  15345. if ($x === '') {
  15346. $x = $this->x;
  15347. }
  15348. if ($y === '') {
  15349. $y = $this->y;
  15350. }
  15351. if (($s_x == 0) OR ($s_y == 0)) {
  15352. $this->Error('Please do not use values equal to zero for scaling');
  15353. }
  15354. $y = ($this->h - $y) * $this->k;
  15355. $x *= $this->k;
  15356. //calculate elements of transformation matrix
  15357. $s_x /= 100;
  15358. $s_y /= 100;
  15359. $tm = array();
  15360. $tm[0] = $s_x;
  15361. $tm[1] = 0;
  15362. $tm[2] = 0;
  15363. $tm[3] = $s_y;
  15364. $tm[4] = $x * (1 - $s_x);
  15365. $tm[5] = $y * (1 - $s_y);
  15366. //scale the coordinate system
  15367. $this->Transform($tm);
  15368. }
  15369. /**
  15370. * Horizontal Mirroring.
  15371. * @param $x (int) abscissa of the point. Default is current x position
  15372. * @public
  15373. * @since 2.1.000 (2008-01-07)
  15374. * @see StartTransform(), StopTransform()
  15375. */
  15376. public function MirrorH($x = '') {
  15377. $this->Scale(-100, 100, $x);
  15378. }
  15379. /**
  15380. * Verical Mirroring.
  15381. * @param $y (int) ordinate of the point. Default is current y position
  15382. * @public
  15383. * @since 2.1.000 (2008-01-07)
  15384. * @see StartTransform(), StopTransform()
  15385. */
  15386. public function MirrorV($y = '') {
  15387. $this->Scale(100, -100, '', $y);
  15388. }
  15389. /**
  15390. * Point reflection mirroring.
  15391. * @param $x (int) abscissa of the point. Default is current x position
  15392. * @param $y (int) ordinate of the point. Default is current y position
  15393. * @public
  15394. * @since 2.1.000 (2008-01-07)
  15395. * @see StartTransform(), StopTransform()
  15396. */
  15397. public function MirrorP($x = '', $y = '') {
  15398. $this->Scale(-100, -100, $x, $y);
  15399. }
  15400. /**
  15401. * Reflection against a straight line through point (x, y) with the gradient angle (angle).
  15402. * @param $angle (float) gradient angle of the straight line. Default is 0 (horizontal line).
  15403. * @param $x (int) abscissa of the point. Default is current x position
  15404. * @param $y (int) ordinate of the point. Default is current y position
  15405. * @public
  15406. * @since 2.1.000 (2008-01-07)
  15407. * @see StartTransform(), StopTransform()
  15408. */
  15409. public function MirrorL($angle = 0, $x = '', $y = '') {
  15410. $this->Scale(-100, 100, $x, $y);
  15411. $this->Rotate(-2 * ($angle - 90), $x, $y);
  15412. }
  15413. /**
  15414. * Translate graphic object horizontally.
  15415. * @param $t_x (int) movement to the right (or left for RTL)
  15416. * @public
  15417. * @since 2.1.000 (2008-01-07)
  15418. * @see StartTransform(), StopTransform()
  15419. */
  15420. public function TranslateX($t_x) {
  15421. $this->Translate($t_x, 0);
  15422. }
  15423. /**
  15424. * Translate graphic object vertically.
  15425. * @param $t_y (int) movement to the bottom
  15426. * @public
  15427. * @since 2.1.000 (2008-01-07)
  15428. * @see StartTransform(), StopTransform()
  15429. */
  15430. public function TranslateY($t_y) {
  15431. $this->Translate(0, $t_y);
  15432. }
  15433. /**
  15434. * Translate graphic object horizontally and vertically.
  15435. * @param $t_x (int) movement to the right
  15436. * @param $t_y (int) movement to the bottom
  15437. * @public
  15438. * @since 2.1.000 (2008-01-07)
  15439. * @see StartTransform(), StopTransform()
  15440. */
  15441. public function Translate($t_x, $t_y) {
  15442. //calculate elements of transformation matrix
  15443. $tm = array();
  15444. $tm[0] = 1;
  15445. $tm[1] = 0;
  15446. $tm[2] = 0;
  15447. $tm[3] = 1;
  15448. $tm[4] = $t_x * $this->k;
  15449. $tm[5] = -$t_y * $this->k;
  15450. //translate the coordinate system
  15451. $this->Transform($tm);
  15452. }
  15453. /**
  15454. * Rotate object.
  15455. * @param $angle (float) angle in degrees for counter-clockwise rotation
  15456. * @param $x (int) abscissa of the rotation center. Default is current x position
  15457. * @param $y (int) ordinate of the rotation center. Default is current y position
  15458. * @public
  15459. * @since 2.1.000 (2008-01-07)
  15460. * @see StartTransform(), StopTransform()
  15461. */
  15462. public function Rotate($angle, $x = '', $y = '') {
  15463. if ($x === '') {
  15464. $x = $this->x;
  15465. }
  15466. if ($y === '') {
  15467. $y = $this->y;
  15468. }
  15469. $y = ($this->h - $y) * $this->k;
  15470. $x *= $this->k;
  15471. //calculate elements of transformation matrix
  15472. $tm = array();
  15473. $tm[0] = cos(deg2rad($angle));
  15474. $tm[1] = sin(deg2rad($angle));
  15475. $tm[2] = -$tm[1];
  15476. $tm[3] = $tm[0];
  15477. $tm[4] = $x + ($tm[1] * $y) - ($tm[0] * $x);
  15478. $tm[5] = $y - ($tm[0] * $y) - ($tm[1] * $x);
  15479. //rotate the coordinate system around ($x,$y)
  15480. $this->Transform($tm);
  15481. }
  15482. /**
  15483. * Skew horizontally.
  15484. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  15485. * @param $x (int) abscissa of the skewing center. default is current x position
  15486. * @param $y (int) ordinate of the skewing center. default is current y position
  15487. * @public
  15488. * @since 2.1.000 (2008-01-07)
  15489. * @see StartTransform(), StopTransform()
  15490. */
  15491. public function SkewX($angle_x, $x = '', $y = '') {
  15492. $this->Skew($angle_x, 0, $x, $y);
  15493. }
  15494. /**
  15495. * Skew vertically.
  15496. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  15497. * @param $x (int) abscissa of the skewing center. default is current x position
  15498. * @param $y (int) ordinate of the skewing center. default is current y position
  15499. * @public
  15500. * @since 2.1.000 (2008-01-07)
  15501. * @see StartTransform(), StopTransform()
  15502. */
  15503. public function SkewY($angle_y, $x = '', $y = '') {
  15504. $this->Skew(0, $angle_y, $x, $y);
  15505. }
  15506. /**
  15507. * Skew.
  15508. * @param $angle_x (float) angle in degrees between -90 (skew to the left) and 90 (skew to the right)
  15509. * @param $angle_y (float) angle in degrees between -90 (skew to the bottom) and 90 (skew to the top)
  15510. * @param $x (int) abscissa of the skewing center. default is current x position
  15511. * @param $y (int) ordinate of the skewing center. default is current y position
  15512. * @public
  15513. * @since 2.1.000 (2008-01-07)
  15514. * @see StartTransform(), StopTransform()
  15515. */
  15516. public function Skew($angle_x, $angle_y, $x = '', $y = '') {
  15517. if ($x === '') {
  15518. $x = $this->x;
  15519. }
  15520. if ($y === '') {
  15521. $y = $this->y;
  15522. }
  15523. if (($angle_x <= -90) OR ($angle_x >= 90) OR ($angle_y <= -90) OR ($angle_y >= 90)) {
  15524. $this->Error('Please use values between -90 and +90 degrees for Skewing.');
  15525. }
  15526. $x *= $this->k;
  15527. $y = ($this->h - $y) * $this->k;
  15528. //calculate elements of transformation matrix
  15529. $tm = array();
  15530. $tm[0] = 1;
  15531. $tm[1] = tan(deg2rad($angle_y));
  15532. $tm[2] = tan(deg2rad($angle_x));
  15533. $tm[3] = 1;
  15534. $tm[4] = -$tm[2] * $y;
  15535. $tm[5] = -$tm[1] * $x;
  15536. //skew the coordinate system
  15537. $this->Transform($tm);
  15538. }
  15539. /**
  15540. * Apply graphic transformations.
  15541. * @param $tm (array) transformation matrix
  15542. * @protected
  15543. * @since 2.1.000 (2008-01-07)
  15544. * @see StartTransform(), StopTransform()
  15545. */
  15546. protected function Transform($tm) {
  15547. if ($this->state != 2) {
  15548. return;
  15549. }
  15550. $this->_out(sprintf('%F %F %F %F %F %F cm', $tm[0], $tm[1], $tm[2], $tm[3], $tm[4], $tm[5]));
  15551. // add tranformation matrix
  15552. $this->transfmatrix[$this->transfmatrix_key][] = array('a' => $tm[0], 'b' => $tm[1], 'c' => $tm[2], 'd' => $tm[3], 'e' => $tm[4], 'f' => $tm[5]);
  15553. // update transformation mark
  15554. if ($this->inxobj) {
  15555. // we are inside an XObject template
  15556. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  15557. $key = key($this->xobjects[$this->xobjid]['transfmrk']);
  15558. $this->xobjects[$this->xobjid]['transfmrk'][$key] = strlen($this->xobjects[$this->xobjid]['outdata']);
  15559. }
  15560. } elseif (end($this->transfmrk[$this->page]) !== false) {
  15561. $key = key($this->transfmrk[$this->page]);
  15562. $this->transfmrk[$this->page][$key] = $this->pagelen[$this->page];
  15563. }
  15564. }
  15565. // END TRANSFORMATIONS SECTION -------------------------
  15566. // START GRAPHIC FUNCTIONS SECTION ---------------------
  15567. // The following section is based on the code provided by David Hernandez Sanz
  15568. /**
  15569. * 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.
  15570. * @param $width (float) The width.
  15571. * @public
  15572. * @since 1.0
  15573. * @see Line(), Rect(), Cell(), MultiCell()
  15574. */
  15575. public function SetLineWidth($width) {
  15576. //Set line width
  15577. $this->LineWidth = $width;
  15578. $this->linestyleWidth = sprintf('%F w', ($width * $this->k));
  15579. if ($this->state == 2) {
  15580. $this->_out($this->linestyleWidth);
  15581. }
  15582. }
  15583. /**
  15584. * Returns the current the line width.
  15585. * @return int Line width
  15586. * @public
  15587. * @since 2.1.000 (2008-01-07)
  15588. * @see Line(), SetLineWidth()
  15589. */
  15590. public function GetLineWidth() {
  15591. return $this->LineWidth;
  15592. }
  15593. /**
  15594. * Set line style.
  15595. * @param $style (array) Line style. Array with keys among the following:
  15596. * <ul>
  15597. * <li>width (float): Width of the line in user units.</li>
  15598. * <li>cap (string): Type of cap to put on the line. Possible values are:
  15599. * butt, round, square. The difference between "square" and "butt" is that
  15600. * "square" projects a flat end past the end of the line.</li>
  15601. * <li>join (string): Type of join. Possible values are: miter, round,
  15602. * bevel.</li>
  15603. * <li>dash (mixed): Dash pattern. Is 0 (without dash) or string with
  15604. * series of length values, which are the lengths of the on and off dashes.
  15605. * For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; "2,1" is 2 on,
  15606. * 1 off, 2 on, 1 off, ...</li>
  15607. * <li>phase (integer): Modifier on the dash pattern which is used to shift
  15608. * the point at which the pattern starts.</li>
  15609. * <li>color (array): Draw color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName).</li>
  15610. * </ul>
  15611. * @param $ret (boolean) if true do not send the command.
  15612. * @return string the PDF command
  15613. * @public
  15614. * @since 2.1.000 (2008-01-08)
  15615. */
  15616. public function SetLineStyle($style, $ret = false) {
  15617. $s = ''; // string to be returned
  15618. if (!is_array($style)) {
  15619. return;
  15620. }
  15621. if (isset($style['width'])) {
  15622. $this->LineWidth = $style['width'];
  15623. $this->linestyleWidth = sprintf('%F w', ($style['width'] * $this->k));
  15624. $s .= $this->linestyleWidth . ' ';
  15625. }
  15626. if (isset($style['cap'])) {
  15627. $ca = array('butt' => 0, 'round' => 1, 'square' => 2);
  15628. if (isset($ca[$style['cap']])) {
  15629. $this->linestyleCap = $ca[$style['cap']] . ' J';
  15630. $s .= $this->linestyleCap . ' ';
  15631. }
  15632. }
  15633. if (isset($style['join'])) {
  15634. $ja = array('miter' => 0, 'round' => 1, 'bevel' => 2);
  15635. if (isset($ja[$style['join']])) {
  15636. $this->linestyleJoin = $ja[$style['join']] . ' j';
  15637. $s .= $this->linestyleJoin . ' ';
  15638. }
  15639. }
  15640. if (isset($style['dash'])) {
  15641. $dash_string = '';
  15642. if ($style['dash']) {
  15643. if (preg_match('/^.+,/', $style['dash']) > 0) {
  15644. $tab = explode(',', $style['dash']);
  15645. } else {
  15646. $tab = array($style['dash']);
  15647. }
  15648. $dash_string = '';
  15649. foreach ($tab as $i => $v) {
  15650. if ($i) {
  15651. $dash_string .= ' ';
  15652. }
  15653. $dash_string .= sprintf('%F', $v);
  15654. }
  15655. }
  15656. if (!isset($style['phase']) OR !$style['dash']) {
  15657. $style['phase'] = 0;
  15658. }
  15659. $this->linestyleDash = sprintf('[%s] %F d', $dash_string, $style['phase']);
  15660. $s .= $this->linestyleDash . ' ';
  15661. }
  15662. if (isset($style['color'])) {
  15663. $s .= $this->SetDrawColorArray($style['color'], true) . ' ';
  15664. }
  15665. if (!$ret AND ($this->state == 2)) {
  15666. $this->_out($s);
  15667. }
  15668. return $s;
  15669. }
  15670. /**
  15671. * Begin a new subpath by moving the current point to coordinates (x, y), omitting any connecting line segment.
  15672. * @param $x (float) Abscissa of point.
  15673. * @param $y (float) Ordinate of point.
  15674. * @protected
  15675. * @since 2.1.000 (2008-01-08)
  15676. */
  15677. protected function _outPoint($x, $y) {
  15678. if ($this->state == 2) {
  15679. $this->_out(sprintf('%F %F m', ($x * $this->k), (($this->h - $y) * $this->k)));
  15680. }
  15681. }
  15682. /**
  15683. * Append a straight line segment from the current point to the point (x, y).
  15684. * The new current point shall be (x, y).
  15685. * @param $x (float) Abscissa of end point.
  15686. * @param $y (float) Ordinate of end point.
  15687. * @protected
  15688. * @since 2.1.000 (2008-01-08)
  15689. */
  15690. protected function _outLine($x, $y) {
  15691. if ($this->state == 2) {
  15692. $this->_out(sprintf('%F %F l', ($x * $this->k), (($this->h - $y) * $this->k)));
  15693. }
  15694. }
  15695. /**
  15696. * Append a rectangle to the current path as a complete subpath, with lower-left corner (x, y) and dimensions widthand height in user space.
  15697. * @param $x (float) Abscissa of upper-left corner.
  15698. * @param $y (float) Ordinate of upper-left corner.
  15699. * @param $w (float) Width.
  15700. * @param $h (float) Height.
  15701. * @param $op (string) options
  15702. * @protected
  15703. * @since 2.1.000 (2008-01-08)
  15704. */
  15705. protected function _outRect($x, $y, $w, $h, $op) {
  15706. if ($this->state == 2) {
  15707. $this->_out(sprintf('%F %F %F %F re %s', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k, $op));
  15708. }
  15709. }
  15710. /**
  15711. * Append a cubic B�zier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x2, y2) as the B�zier control points.
  15712. * The new current point shall be (x3, y3).
  15713. * @param $x1 (float) Abscissa of control point 1.
  15714. * @param $y1 (float) Ordinate of control point 1.
  15715. * @param $x2 (float) Abscissa of control point 2.
  15716. * @param $y2 (float) Ordinate of control point 2.
  15717. * @param $x3 (float) Abscissa of end point.
  15718. * @param $y3 (float) Ordinate of end point.
  15719. * @protected
  15720. * @since 2.1.000 (2008-01-08)
  15721. */
  15722. protected function _outCurve($x1, $y1, $x2, $y2, $x3, $y3) {
  15723. if ($this->state == 2) {
  15724. $this->_out(sprintf('%F %F %F %F %F %F 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));
  15725. }
  15726. }
  15727. /**
  15728. * Append a cubic B�zier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using the current point and (x2, y2) as the B�zier control points.
  15729. * The new current point shall be (x3, y3).
  15730. * @param $x2 (float) Abscissa of control point 2.
  15731. * @param $y2 (float) Ordinate of control point 2.
  15732. * @param $x3 (float) Abscissa of end point.
  15733. * @param $y3 (float) Ordinate of end point.
  15734. * @protected
  15735. * @since 4.9.019 (2010-04-26)
  15736. */
  15737. protected function _outCurveV($x2, $y2, $x3, $y3) {
  15738. if ($this->state == 2) {
  15739. $this->_out(sprintf('%F %F %F %F v', $x2 * $this->k, ($this->h - $y2) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  15740. }
  15741. }
  15742. /**
  15743. * Append a cubic B�zier curve to the current path. The curve shall extend from the current point to the point (x3, y3), using (x1, y1) and (x3, y3) as the B�zier control points.
  15744. * The new current point shall be (x3, y3).
  15745. * @param $x1 (float) Abscissa of control point 1.
  15746. * @param $y1 (float) Ordinate of control point 1.
  15747. * @param $x3 (float) Abscissa of end point.
  15748. * @param $y3 (float) Ordinate of end point.
  15749. * @protected
  15750. * @since 2.1.000 (2008-01-08)
  15751. */
  15752. protected function _outCurveY($x1, $y1, $x3, $y3) {
  15753. if ($this->state == 2) {
  15754. $this->_out(sprintf('%F %F %F %F y', $x1 * $this->k, ($this->h - $y1) * $this->k, $x3 * $this->k, ($this->h - $y3) * $this->k));
  15755. }
  15756. }
  15757. /**
  15758. * Draws a line between two points.
  15759. * @param $x1 (float) Abscissa of first point.
  15760. * @param $y1 (float) Ordinate of first point.
  15761. * @param $x2 (float) Abscissa of second point.
  15762. * @param $y2 (float) Ordinate of second point.
  15763. * @param $style (array) Line style. Array like for SetLineStyle(). Default value: default line style (empty array).
  15764. * @public
  15765. * @since 1.0
  15766. * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
  15767. */
  15768. public function Line($x1, $y1, $x2, $y2, $style = array()) {
  15769. if ($this->state != 2) {
  15770. return;
  15771. }
  15772. if (is_array($style)) {
  15773. $this->SetLineStyle($style);
  15774. }
  15775. $this->_outPoint($x1, $y1);
  15776. $this->_outLine($x2, $y2);
  15777. $this->_out('S');
  15778. }
  15779. /**
  15780. * Draws a rectangle.
  15781. * @param $x (float) Abscissa of upper-left corner.
  15782. * @param $y (float) Ordinate of upper-left corner.
  15783. * @param $w (float) Width.
  15784. * @param $h (float) Height.
  15785. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  15786. * @param $border_style (array) Border style of rectangle. Array with keys among the following:
  15787. * <ul>
  15788. * <li>all: Line style of all borders. Array like for SetLineStyle().</li>
  15789. * <li>L, T, R, B or combinations: Line style of left, top, right or bottom border. Array like for SetLineStyle().</li>
  15790. * </ul>
  15791. * If a key is not present or is null, not draws the border. Default value: default line style (empty array).
  15792. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  15793. * @public
  15794. * @since 1.0
  15795. * @see SetLineStyle()
  15796. */
  15797. public function Rect($x, $y, $w, $h, $style = '', $border_style = array(), $fill_color = array()) {
  15798. if ($this->state != 2) {
  15799. return;
  15800. }
  15801. if (!(false === strpos($style, 'F')) AND !empty($fill_color)) {
  15802. $this->SetFillColorArray($fill_color);
  15803. }
  15804. $op = $this->getPathPaintOperator($style);
  15805. if ((!$border_style) OR (isset($border_style['all']))) {
  15806. if (isset($border_style['all']) AND $border_style['all']) {
  15807. $this->SetLineStyle($border_style['all']);
  15808. $border_style = array();
  15809. }
  15810. }
  15811. $this->_outRect($x, $y, $w, $h, $op);
  15812. if ($border_style) {
  15813. $border_style2 = array();
  15814. foreach ($border_style as $line => $value) {
  15815. $length = strlen($line);
  15816. for ($i = 0; $i < $length; ++$i) {
  15817. $border_style2[$line[$i]] = $value;
  15818. }
  15819. }
  15820. $border_style = $border_style2;
  15821. if (isset($border_style['L']) AND $border_style['L']) {
  15822. $this->Line($x, $y, $x, $y + $h, $border_style['L']);
  15823. }
  15824. if (isset($border_style['T']) AND $border_style['T']) {
  15825. $this->Line($x, $y, $x + $w, $y, $border_style['T']);
  15826. }
  15827. if (isset($border_style['R']) AND $border_style['R']) {
  15828. $this->Line($x + $w, $y, $x + $w, $y + $h, $border_style['R']);
  15829. }
  15830. if (isset($border_style['B']) AND $border_style['B']) {
  15831. $this->Line($x, $y + $h, $x + $w, $y + $h, $border_style['B']);
  15832. }
  15833. }
  15834. }
  15835. /**
  15836. * Draws a Bezier curve.
  15837. * The Bezier curve is a tangent to the line between the control points at
  15838. * either end of the curve.
  15839. * @param $x0 (float) Abscissa of start point.
  15840. * @param $y0 (float) Ordinate of start point.
  15841. * @param $x1 (float) Abscissa of control point 1.
  15842. * @param $y1 (float) Ordinate of control point 1.
  15843. * @param $x2 (float) Abscissa of control point 2.
  15844. * @param $y2 (float) Ordinate of control point 2.
  15845. * @param $x3 (float) Abscissa of end point.
  15846. * @param $y3 (float) Ordinate of end point.
  15847. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  15848. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  15849. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  15850. * @public
  15851. * @see SetLineStyle()
  15852. * @since 2.1.000 (2008-01-08)
  15853. */
  15854. public function Curve($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3, $style = '', $line_style = array(), $fill_color = array()) {
  15855. if ($this->state != 2) {
  15856. return;
  15857. }
  15858. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  15859. $this->SetFillColorArray($fill_color);
  15860. }
  15861. $op = $this->getPathPaintOperator($style);
  15862. if ($line_style) {
  15863. $this->SetLineStyle($line_style);
  15864. }
  15865. $this->_outPoint($x0, $y0);
  15866. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  15867. $this->_out($op);
  15868. }
  15869. /**
  15870. * Draws a poly-Bezier curve.
  15871. * Each Bezier curve segment is a tangent to the line between the control points at
  15872. * either end of the curve.
  15873. * @param $x0 (float) Abscissa of start point.
  15874. * @param $y0 (float) Ordinate of start point.
  15875. * @param $segments (float) An array of bezier descriptions. Format: array(x1, y1, x2, y2, x3, y3).
  15876. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  15877. * @param $line_style (array) Line style of curve. Array like for SetLineStyle(). Default value: default line style (empty array).
  15878. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  15879. * @public
  15880. * @see SetLineStyle()
  15881. * @since 3.0008 (2008-05-12)
  15882. */
  15883. public function Polycurve($x0, $y0, $segments, $style = '', $line_style = array(), $fill_color = array()) {
  15884. if ($this->state != 2) {
  15885. return;
  15886. }
  15887. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  15888. $this->SetFillColorArray($fill_color);
  15889. }
  15890. $op = $this->getPathPaintOperator($style);
  15891. if ($op == 'f') {
  15892. $line_style = array();
  15893. }
  15894. if ($line_style) {
  15895. $this->SetLineStyle($line_style);
  15896. }
  15897. $this->_outPoint($x0, $y0);
  15898. foreach ($segments as $segment) {
  15899. list($x1, $y1, $x2, $y2, $x3, $y3) = $segment;
  15900. $this->_outCurve($x1, $y1, $x2, $y2, $x3, $y3);
  15901. }
  15902. $this->_out($op);
  15903. }
  15904. /**
  15905. * Draws an ellipse.
  15906. * An ellipse is formed from n Bezier curves.
  15907. * @param $x0 (float) Abscissa of center point.
  15908. * @param $y0 (float) Ordinate of center point.
  15909. * @param $rx (float) Horizontal radius.
  15910. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  15911. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  15912. * @param $astart: (float) Angle start of draw line. Default value: 0.
  15913. * @param $afinish: (float) Angle finish of draw line. Default value: 360.
  15914. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  15915. * @param $line_style (array) Line style of ellipse. Array like for SetLineStyle(). Default value: default line style (empty array).
  15916. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  15917. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  15918. * @author Nicola Asuni
  15919. * @public
  15920. * @since 2.1.000 (2008-01-08)
  15921. */
  15922. public function Ellipse($x0, $y0, $rx, $ry = '', $angle = 0, $astart = 0, $afinish = 360, $style = '', $line_style = array(), $fill_color = array(), $nc = 2) {
  15923. if ($this->state != 2) {
  15924. return;
  15925. }
  15926. if ($this->empty_string($ry) OR ($ry == 0)) {
  15927. $ry = $rx;
  15928. }
  15929. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  15930. $this->SetFillColorArray($fill_color);
  15931. }
  15932. $op = $this->getPathPaintOperator($style);
  15933. if ($op == 'f') {
  15934. $line_style = array();
  15935. }
  15936. if ($line_style) {
  15937. $this->SetLineStyle($line_style);
  15938. }
  15939. $this->_outellipticalarc($x0, $y0, $rx, $ry, $angle, $astart, $afinish, false, $nc, true, true, false);
  15940. $this->_out($op);
  15941. }
  15942. /**
  15943. * Append an elliptical arc to the current path.
  15944. * An ellipse is formed from n Bezier curves.
  15945. * @param $xc (float) Abscissa of center point.
  15946. * @param $yc (float) Ordinate of center point.
  15947. * @param $rx (float) Horizontal radius.
  15948. * @param $ry (float) Vertical radius (if ry = 0 then is a circle, see Circle()). Default value: 0.
  15949. * @param $xang: (float) Angle between the X-axis and the major axis of the ellipse. Default value: 0.
  15950. * @param $angs: (float) Angle start of draw line. Default value: 0.
  15951. * @param $angf: (float) Angle finish of draw line. Default value: 360.
  15952. * @param $pie (boolean) if true do not mark the border point (used to draw pie sectors).
  15953. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of ellipse.
  15954. * @param $startpoint (boolean) if true output a starting point.
  15955. * @param $ccw (boolean) if true draws in counter-clockwise.
  15956. * @param $svg (boolean) if true the angles are in svg mode (already calculated).
  15957. * @return array bounding box coordinates (x min, y min, x max, y max)
  15958. * @author Nicola Asuni
  15959. * @protected
  15960. * @since 4.9.019 (2010-04-26)
  15961. */
  15962. protected function _outellipticalarc($xc, $yc, $rx, $ry, $xang = 0, $angs = 0, $angf = 360, $pie = false, $nc = 2, $startpoint = true, $ccw = true, $svg = false) {
  15963. $k = $this->k;
  15964. if ($nc < 2) {
  15965. $nc = 2;
  15966. }
  15967. $xmin = 2147483647;
  15968. $ymin = 2147483647;
  15969. $xmax = 0;
  15970. $ymax = 0;
  15971. if ($pie) {
  15972. // center of the arc
  15973. $this->_outPoint($xc, $yc);
  15974. }
  15975. $xang = deg2rad((float) $xang);
  15976. $angs = deg2rad((float) $angs);
  15977. $angf = deg2rad((float) $angf);
  15978. if ($svg) {
  15979. $as = $angs;
  15980. $af = $angf;
  15981. } else {
  15982. $as = atan2((sin($angs) / $ry), (cos($angs) / $rx));
  15983. $af = atan2((sin($angf) / $ry), (cos($angf) / $rx));
  15984. }
  15985. if ($as < 0) {
  15986. $as += (2 * M_PI);
  15987. }
  15988. if ($af < 0) {
  15989. $af += (2 * M_PI);
  15990. }
  15991. if ($ccw AND ($as > $af)) {
  15992. // reverse rotation
  15993. $as -= (2 * M_PI);
  15994. } elseif (!$ccw AND ($as < $af)) {
  15995. // reverse rotation
  15996. $af -= (2 * M_PI);
  15997. }
  15998. $total_angle = ($af - $as);
  15999. if ($nc < 2) {
  16000. $nc = 2;
  16001. }
  16002. // total arcs to draw
  16003. $nc *= (2 * abs($total_angle) / M_PI);
  16004. $nc = round($nc) + 1;
  16005. // angle of each arc
  16006. $arcang = ($total_angle / $nc);
  16007. // center point in PDF coordinates
  16008. $x0 = $xc;
  16009. $y0 = ($this->h - $yc);
  16010. // starting angle
  16011. $ang = $as;
  16012. $alpha = sin($arcang) * ((sqrt(4 + (3 * pow(tan(($arcang) / 2), 2))) - 1) / 3);
  16013. $cos_xang = cos($xang);
  16014. $sin_xang = sin($xang);
  16015. $cos_ang = cos($ang);
  16016. $sin_ang = sin($ang);
  16017. // first arc point
  16018. $px1 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  16019. $py1 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  16020. // first Bezier control point
  16021. $qx1 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  16022. $qy1 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  16023. if ($pie) {
  16024. // line from center to arc starting point
  16025. $this->_outLine($px1, $this->h - $py1);
  16026. } elseif ($startpoint) {
  16027. // arc starting point
  16028. $this->_outPoint($px1, $this->h - $py1);
  16029. }
  16030. // draw arcs
  16031. for ($i = 1; $i <= $nc; ++$i) {
  16032. // starting angle
  16033. $ang = $as + ($i * $arcang);
  16034. if ($i == $nc) {
  16035. $ang = $af;
  16036. }
  16037. $cos_ang = cos($ang);
  16038. $sin_ang = sin($ang);
  16039. // second arc point
  16040. $px2 = $x0 + ($rx * $cos_xang * $cos_ang) - ($ry * $sin_xang * $sin_ang);
  16041. $py2 = $y0 + ($rx * $sin_xang * $cos_ang) + ($ry * $cos_xang * $sin_ang);
  16042. // second Bezier control point
  16043. $qx2 = ($alpha * ((-$rx * $cos_xang * $sin_ang) - ($ry * $sin_xang * $cos_ang)));
  16044. $qy2 = ($alpha * ((-$rx * $sin_xang * $sin_ang) + ($ry * $cos_xang * $cos_ang)));
  16045. // draw arc
  16046. $cx1 = ($px1 + $qx1);
  16047. $cy1 = ($this->h - ($py1 + $qy1));
  16048. $cx2 = ($px2 - $qx2);
  16049. $cy2 = ($this->h - ($py2 - $qy2));
  16050. $cx3 = $px2;
  16051. $cy3 = ($this->h - $py2);
  16052. $this->_outCurve($cx1, $cy1, $cx2, $cy2, $cx3, $cy3);
  16053. // get bounding box coordinates
  16054. $xmin = min($xmin, $cx1, $cx2, $cx3);
  16055. $ymin = min($ymin, $cy1, $cy2, $cy3);
  16056. $xmax = max($xmax, $cx1, $cx2, $cx3);
  16057. $ymax = max($ymax, $cy1, $cy2, $cy3);
  16058. // move to next point
  16059. $px1 = $px2;
  16060. $py1 = $py2;
  16061. $qx1 = $qx2;
  16062. $qy1 = $qy2;
  16063. }
  16064. if ($pie) {
  16065. $this->_outLine($xc, $yc);
  16066. // get bounding box coordinates
  16067. $xmin = min($xmin, $xc);
  16068. $ymin = min($ymin, $yc);
  16069. $xmax = max($xmax, $xc);
  16070. $ymax = max($ymax, $yc);
  16071. }
  16072. return array($xmin, $ymin, $xmax, $ymax);
  16073. }
  16074. /**
  16075. * Draws a circle.
  16076. * A circle is formed from n Bezier curves.
  16077. * @param $x0 (float) Abscissa of center point.
  16078. * @param $y0 (float) Ordinate of center point.
  16079. * @param $r (float) Radius.
  16080. * @param $angstr: (float) Angle start of draw line. Default value: 0.
  16081. * @param $angend: (float) Angle finish of draw line. Default value: 360.
  16082. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16083. * @param $line_style (array) Line style of circle. Array like for SetLineStyle(). Default value: default line style (empty array).
  16084. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  16085. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of circle.
  16086. * @public
  16087. * @since 2.1.000 (2008-01-08)
  16088. */
  16089. public function Circle($x0, $y0, $r, $angstr = 0, $angend = 360, $style = '', $line_style = array(), $fill_color = array(), $nc = 2) {
  16090. $this->Ellipse($x0, $y0, $r, $r, 0, $angstr, $angend, $style, $line_style, $fill_color, $nc);
  16091. }
  16092. /**
  16093. * Draws a polygonal line
  16094. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  16095. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16096. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  16097. * <ul>
  16098. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  16099. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  16100. * </ul>
  16101. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  16102. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  16103. * @since 4.8.003 (2009-09-15)
  16104. * @public
  16105. */
  16106. public function PolyLine($p, $style = '', $line_style = array(), $fill_color = array()) {
  16107. $this->Polygon($p, $style, $line_style, $fill_color, false);
  16108. }
  16109. /**
  16110. * Draws a polygon.
  16111. * @param $p (array) Points 0 to ($np - 1). Array with values (x0, y0, x1, y1,..., x(np-1), y(np - 1))
  16112. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16113. * @param $line_style (array) Line style of polygon. Array with keys among the following:
  16114. * <ul>
  16115. * <li>all: Line style of all lines. Array like for SetLineStyle().</li>
  16116. * <li>0 to ($np - 1): Line style of each line. Array like for SetLineStyle().</li>
  16117. * </ul>
  16118. * If a key is not present or is null, not draws the line. Default value is default line style (empty array).
  16119. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  16120. * @param $closed (boolean) if true the polygon is closes, otherwise will remain open
  16121. * @public
  16122. * @since 2.1.000 (2008-01-08)
  16123. */
  16124. public function Polygon($p, $style = '', $line_style = array(), $fill_color = array(), $closed = true) {
  16125. if ($this->state != 2) {
  16126. return;
  16127. }
  16128. $nc = count($p); // number of coordinates
  16129. $np = $nc / 2; // number of points
  16130. if ($closed) {
  16131. // close polygon by adding the first 2 points at the end (one line)
  16132. for ($i = 0; $i < 4; ++$i) {
  16133. $p[$nc + $i] = $p[$i];
  16134. }
  16135. // copy style for the last added line
  16136. if (isset($line_style[0])) {
  16137. $line_style[$np] = $line_style[0];
  16138. }
  16139. $nc += 4;
  16140. }
  16141. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  16142. $this->SetFillColorArray($fill_color);
  16143. }
  16144. $op = $this->getPathPaintOperator($style);
  16145. if ($op == 'f') {
  16146. $line_style = array();
  16147. }
  16148. $draw = true;
  16149. if ($line_style) {
  16150. if (isset($line_style['all'])) {
  16151. $this->SetLineStyle($line_style['all']);
  16152. } else {
  16153. $draw = false;
  16154. if ($op == 'B') {
  16155. // draw fill
  16156. $op = 'f';
  16157. $this->_outPoint($p[0], $p[1]);
  16158. for ($i = 2; $i < $nc; $i = $i + 2) {
  16159. $this->_outLine($p[$i], $p[$i + 1]);
  16160. }
  16161. $this->_out($op);
  16162. }
  16163. // draw outline
  16164. $this->_outPoint($p[0], $p[1]);
  16165. for ($i = 2; $i < $nc; $i = $i + 2) {
  16166. $line_num = ($i / 2) - 1;
  16167. if (isset($line_style[$line_num])) {
  16168. if ($line_style[$line_num] != 0) {
  16169. if (is_array($line_style[$line_num])) {
  16170. $this->_out('S');
  16171. $this->SetLineStyle($line_style[$line_num]);
  16172. $this->_outPoint($p[$i - 2], $p[$i - 1]);
  16173. $this->_outLine($p[$i], $p[$i + 1]);
  16174. $this->_out('S');
  16175. $this->_outPoint($p[$i], $p[$i + 1]);
  16176. } else {
  16177. $this->_outLine($p[$i], $p[$i + 1]);
  16178. }
  16179. }
  16180. } else {
  16181. $this->_outLine($p[$i], $p[$i + 1]);
  16182. }
  16183. }
  16184. $this->_out($op);
  16185. }
  16186. }
  16187. if ($draw) {
  16188. $this->_outPoint($p[0], $p[1]);
  16189. for ($i = 2; $i < $nc; $i = $i + 2) {
  16190. $this->_outLine($p[$i], $p[$i + 1]);
  16191. }
  16192. $this->_out($op);
  16193. }
  16194. }
  16195. /**
  16196. * Draws a regular polygon.
  16197. * @param $x0 (float) Abscissa of center point.
  16198. * @param $y0 (float) Ordinate of center point.
  16199. * @param $r: (float) Radius of inscribed circle.
  16200. * @param $ns (integer) Number of sides.
  16201. * @param $angle (float) Angle oriented (anti-clockwise). Default value: 0.
  16202. * @param $draw_circle (boolean) Draw inscribed circle or not. Default value: false.
  16203. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16204. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  16205. * <ul>
  16206. * <li>all: Line style of all sides. Array like for SetLineStyle().</li>
  16207. * <li>0 to ($ns - 1): Line style of each side. Array like for SetLineStyle().</li>
  16208. * </ul>
  16209. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  16210. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  16211. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  16212. * <ul>
  16213. * <li>D or empty string: Draw (default).</li>
  16214. * <li>F: Fill.</li>
  16215. * <li>DF or FD: Draw and fill.</li>
  16216. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  16217. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  16218. * </ul>
  16219. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  16220. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  16221. * @public
  16222. * @since 2.1.000 (2008-01-08)
  16223. */
  16224. 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()) {
  16225. if (3 > $ns) {
  16226. $ns = 3;
  16227. }
  16228. if ($draw_circle) {
  16229. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  16230. }
  16231. $p = array();
  16232. for ($i = 0; $i < $ns; ++$i) {
  16233. $a = $angle + ($i * 360 / $ns);
  16234. $a_rad = deg2rad((float) $a);
  16235. $p[] = $x0 + ($r * sin($a_rad));
  16236. $p[] = $y0 + ($r * cos($a_rad));
  16237. }
  16238. $this->Polygon($p, $style, $line_style, $fill_color);
  16239. }
  16240. /**
  16241. * Draws a star polygon
  16242. * @param $x0 (float) Abscissa of center point.
  16243. * @param $y0 (float) Ordinate of center point.
  16244. * @param $r (float) Radius of inscribed circle.
  16245. * @param $nv (integer) Number of vertices.
  16246. * @param $ng (integer) Number of gap (if ($ng % $nv = 1) then is a regular polygon).
  16247. * @param $angle: (float) Angle oriented (anti-clockwise). Default value: 0.
  16248. * @param $draw_circle: (boolean) Draw inscribed circle or not. Default value is false.
  16249. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16250. * @param $line_style (array) Line style of polygon sides. Array with keys among the following:
  16251. * <ul>
  16252. * <li>all: Line style of all sides. Array like for
  16253. * SetLineStyle().</li>
  16254. * <li>0 to (n - 1): Line style of each side. Array like for SetLineStyle().</li>
  16255. * </ul>
  16256. * If a key is not present or is null, not draws the side. Default value is default line style (empty array).
  16257. * @param $fill_color (array) Fill color. Format: array(red, green, blue). Default value: default color (empty array).
  16258. * @param $circle_style (string) Style of rendering of inscribed circle (if draws). Possible values are:
  16259. * <ul>
  16260. * <li>D or empty string: Draw (default).</li>
  16261. * <li>F: Fill.</li>
  16262. * <li>DF or FD: Draw and fill.</li>
  16263. * <li>CNZ: Clipping mode (using the even-odd rule to determine which regions lie inside the clipping path).</li>
  16264. * <li>CEO: Clipping mode (using the nonzero winding number rule to determine which regions lie inside the clipping path).</li>
  16265. * </ul>
  16266. * @param $circle_outLine_style (array) Line style of inscribed circle (if draws). Array like for SetLineStyle(). Default value: default line style (empty array).
  16267. * @param $circle_fill_color (array) Fill color of inscribed circle (if draws). Format: array(red, green, blue). Default value: default color (empty array).
  16268. * @public
  16269. * @since 2.1.000 (2008-01-08)
  16270. */
  16271. 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()) {
  16272. if ($nv < 2) {
  16273. $nv = 2;
  16274. }
  16275. if ($draw_circle) {
  16276. $this->Circle($x0, $y0, $r, 0, 360, $circle_style, $circle_outLine_style, $circle_fill_color);
  16277. }
  16278. $p2 = array();
  16279. $visited = array();
  16280. for ($i = 0; $i < $nv; ++$i) {
  16281. $a = $angle + ($i * 360 / $nv);
  16282. $a_rad = deg2rad((float) $a);
  16283. $p2[] = $x0 + ($r * sin($a_rad));
  16284. $p2[] = $y0 + ($r * cos($a_rad));
  16285. $visited[] = false;
  16286. }
  16287. $p = array();
  16288. $i = 0;
  16289. do {
  16290. $p[] = $p2[$i * 2];
  16291. $p[] = $p2[($i * 2) + 1];
  16292. $visited[$i] = true;
  16293. $i += $ng;
  16294. $i %= $nv;
  16295. } while (!$visited[$i]);
  16296. $this->Polygon($p, $style, $line_style, $fill_color);
  16297. }
  16298. /**
  16299. * Draws a rounded rectangle.
  16300. * @param $x (float) Abscissa of upper-left corner.
  16301. * @param $y (float) Ordinate of upper-left corner.
  16302. * @param $w (float) Width.
  16303. * @param $h (float) Height.
  16304. * @param $r (float) the radius of the circle used to round off the corners of the rectangle.
  16305. * @param $round_corner (string) 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 right, bottom right, bottom left and top left. Default value: all rounded corner ("1111").
  16306. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16307. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  16308. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  16309. * @public
  16310. * @since 2.1.000 (2008-01-08)
  16311. */
  16312. public function RoundedRect($x, $y, $w, $h, $r, $round_corner = '1111', $style = '', $border_style = array(), $fill_color = array()) {
  16313. $this->RoundedRectXY($x, $y, $w, $h, $r, $r, $round_corner, $style, $border_style, $fill_color);
  16314. }
  16315. /**
  16316. * Draws a rounded rectangle.
  16317. * @param $x (float) Abscissa of upper-left corner.
  16318. * @param $y (float) Ordinate of upper-left corner.
  16319. * @param $w (float) Width.
  16320. * @param $h (float) Height.
  16321. * @param $rx (float) the x-axis radius of the ellipse used to round off the corners of the rectangle.
  16322. * @param $ry (float) the y-axis radius of the ellipse used to round off the corners of the rectangle.
  16323. * @param $round_corner (string) 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 right, bottom right, bottom left and top left. Default value: all rounded corner ("1111").
  16324. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  16325. * @param $border_style (array) Border style of rectangle. Array like for SetLineStyle(). Default value: default line style (empty array).
  16326. * @param $fill_color (array) Fill color. Format: array(GREY) or array(R,G,B) or array(C,M,Y,K) or array(C,M,Y,K,SpotColorName). Default value: default color (empty array).
  16327. * @public
  16328. * @since 4.9.019 (2010-04-22)
  16329. */
  16330. public function RoundedRectXY($x, $y, $w, $h, $rx, $ry, $round_corner = '1111', $style = '', $border_style = array(), $fill_color = array()) {
  16331. if ($this->state != 2) {
  16332. return;
  16333. }
  16334. if (($round_corner == '0000') OR (($rx == $ry) AND ($rx == 0))) {
  16335. // Not rounded
  16336. $this->Rect($x, $y, $w, $h, $style, $border_style, $fill_color);
  16337. return;
  16338. }
  16339. // Rounded
  16340. if (!(false === strpos($style, 'F')) AND isset($fill_color)) {
  16341. $this->SetFillColorArray($fill_color);
  16342. }
  16343. $op = $this->getPathPaintOperator($style);
  16344. if ($op == 'f') {
  16345. $border_style = array();
  16346. }
  16347. if ($border_style) {
  16348. $this->SetLineStyle($border_style);
  16349. }
  16350. $MyArc = 4 / 3 * (sqrt(2) - 1);
  16351. $this->_outPoint($x + $rx, $y);
  16352. $xc = $x + $w - $rx;
  16353. $yc = $y + $ry;
  16354. $this->_outLine($xc, $y);
  16355. if ($round_corner[0]) {
  16356. $this->_outCurve($xc + ($rx * $MyArc), $yc - $ry, $xc + $rx, $yc - ($ry * $MyArc), $xc + $rx, $yc);
  16357. } else {
  16358. $this->_outLine($x + $w, $y);
  16359. }
  16360. $xc = $x + $w - $rx;
  16361. $yc = $y + $h - $ry;
  16362. $this->_outLine($x + $w, $yc);
  16363. if ($round_corner[1]) {
  16364. $this->_outCurve($xc + $rx, $yc + ($ry * $MyArc), $xc + ($rx * $MyArc), $yc + $ry, $xc, $yc + $ry);
  16365. } else {
  16366. $this->_outLine($x + $w, $y + $h);
  16367. }
  16368. $xc = $x + $rx;
  16369. $yc = $y + $h - $ry;
  16370. $this->_outLine($xc, $y + $h);
  16371. if ($round_corner[2]) {
  16372. $this->_outCurve($xc - ($rx * $MyArc), $yc + $ry, $xc - $rx, $yc + ($ry * $MyArc), $xc - $rx, $yc);
  16373. } else {
  16374. $this->_outLine($x, $y + $h);
  16375. }
  16376. $xc = $x + $rx;
  16377. $yc = $y + $ry;
  16378. $this->_outLine($x, $yc);
  16379. if ($round_corner[3]) {
  16380. $this->_outCurve($xc - $rx, $yc - ($ry * $MyArc), $xc - ($rx * $MyArc), $yc - $ry, $xc, $yc - $ry);
  16381. } else {
  16382. $this->_outLine($x, $y);
  16383. $this->_outLine($x + $rx, $y);
  16384. }
  16385. $this->_out($op);
  16386. }
  16387. /**
  16388. * Draws a grahic arrow.
  16389. * @param $x0 (float) Abscissa of first point.
  16390. * @param $y0 (float) Ordinate of first point.
  16391. * @param $x1 (float) Abscissa of second point.
  16392. * @param $y1 (float) Ordinate of second point.
  16393. * @param $head_style (int) (0 = draw only arrowhead arms, 1 = draw closed arrowhead, but no fill, 2 = closed and filled arrowhead, 3 = filled arrowhead)
  16394. * @param $arm_size (float) length of arrowhead arms
  16395. * @param $arm_angle (int) angle between an arm and the shaft
  16396. * @author Piotr Galecki, Nicola Asuni, Andy Meier
  16397. * @since 4.6.018 (2009-07-10)
  16398. */
  16399. public function Arrow($x0, $y0, $x1, $y1, $head_style = 0, $arm_size = 5, $arm_angle = 15) {
  16400. // getting arrow direction angle
  16401. // 0 deg angle is when both arms go along X axis. angle grows clockwise.
  16402. $dir_angle = atan2(($y0 - $y1), ($x0 - $x1));
  16403. if ($dir_angle < 0) {
  16404. $dir_angle += (2 * M_PI);
  16405. }
  16406. $arm_angle = deg2rad($arm_angle);
  16407. $sx1 = $x1;
  16408. $sy1 = $y1;
  16409. if ($head_style > 0) {
  16410. // calculate the stopping point for the arrow shaft
  16411. $sx1 = $x1 + (($arm_size - $this->LineWidth) * cos($dir_angle));
  16412. $sy1 = $y1 + (($arm_size - $this->LineWidth) * sin($dir_angle));
  16413. }
  16414. // main arrow line / shaft
  16415. $this->Line($x0, $y0, $sx1, $sy1);
  16416. // left arrowhead arm tip
  16417. $x2L = $x1 + ($arm_size * cos($dir_angle + $arm_angle));
  16418. $y2L = $y1 + ($arm_size * sin($dir_angle + $arm_angle));
  16419. // right arrowhead arm tip
  16420. $x2R = $x1 + ($arm_size * cos($dir_angle - $arm_angle));
  16421. $y2R = $y1 + ($arm_size * sin($dir_angle - $arm_angle));
  16422. $mode = 'D';
  16423. $style = array();
  16424. switch ($head_style) {
  16425. case 0: {
  16426. // draw only arrowhead arms
  16427. $mode = 'D';
  16428. $style = array(1, 1, 0);
  16429. break;
  16430. }
  16431. case 1: {
  16432. // draw closed arrowhead, but no fill
  16433. $mode = 'D';
  16434. break;
  16435. }
  16436. case 2: {
  16437. // closed and filled arrowhead
  16438. $mode = 'DF';
  16439. break;
  16440. }
  16441. case 3: {
  16442. // filled arrowhead
  16443. $mode = 'F';
  16444. break;
  16445. }
  16446. }
  16447. $this->Polygon(array($x2L, $y2L, $x1, $y1, $x2R, $y2R), $mode, $style, array());
  16448. }
  16449. // END GRAPHIC FUNCTIONS SECTION -----------------------
  16450. // BIDIRECTIONAL TEXT SECTION --------------------------
  16451. /**
  16452. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  16453. * @param $str (string) string to manipulate.
  16454. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  16455. * @param $forcertl (bool) if true forces RTL text direction
  16456. * @return string
  16457. * @protected
  16458. * @author Nicola Asuni
  16459. * @since 2.1.000 (2008-01-08)
  16460. */
  16461. protected function utf8StrRev($str, $setbom = false, $forcertl = false) {
  16462. return $this->utf8StrArrRev($this->UTF8StringToArray($str), $str, $setbom, $forcertl);
  16463. }
  16464. /**
  16465. * Reverse the RLT substrings array using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  16466. * @param $arr (array) array of unicode values.
  16467. * @param $str (string) string to manipulate (or empty value).
  16468. * @param $setbom (bool) if true set the Byte Order Mark (BOM = 0xFEFF)
  16469. * @param $forcertl (bool) if true forces RTL text direction
  16470. * @return string
  16471. * @protected
  16472. * @author Nicola Asuni
  16473. * @since 4.9.000 (2010-03-27)
  16474. */
  16475. protected function utf8StrArrRev($arr, $str = '', $setbom = false, $forcertl = false) {
  16476. return $this->arrUTF8ToUTF16BE($this->utf8Bidi($arr, $str, $forcertl), $setbom);
  16477. }
  16478. /**
  16479. * Reverse the RLT substrings using the Bidirectional Algorithm (http://unicode.org/reports/tr9/).
  16480. * @param $ta (array) array of characters composing the string.
  16481. * @param $str (string) string to process
  16482. * @param $forcertl (bool) if 'R' forces RTL, if 'L' forces LTR
  16483. * @return array of unicode chars
  16484. * @author Nicola Asuni
  16485. * @protected
  16486. * @since 2.4.000 (2008-03-06)
  16487. */
  16488. protected function utf8Bidi($ta, $str = '', $forcertl = false) {
  16489. // paragraph embedding level
  16490. $pel = 0;
  16491. // max level
  16492. $maxlevel = 0;
  16493. if ($this->empty_string($str)) {
  16494. // create string from array
  16495. $str = $this->UTF8ArrSubString($ta);
  16496. }
  16497. // check if string contains arabic text
  16498. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $str)) {
  16499. $arabic = true;
  16500. } else {
  16501. $arabic = false;
  16502. }
  16503. // check if string contains RTL text
  16504. if (!($forcertl OR $arabic OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $str))) {
  16505. return $ta;
  16506. }
  16507. // get number of chars
  16508. $numchars = count($ta);
  16509. if ($forcertl == 'R') {
  16510. $pel = 1;
  16511. } elseif ($forcertl == 'L') {
  16512. $pel = 0;
  16513. } else {
  16514. // P2. In each paragraph, find the first character of type L, AL, or R.
  16515. // 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.
  16516. for ($i = 0; $i < $numchars; ++$i) {
  16517. $type = $this->unicode->uni_type[$ta[$i]];
  16518. if ($type == 'L') {
  16519. $pel = 0;
  16520. break;
  16521. } elseif (($type == 'AL') OR ($type == 'R')) {
  16522. $pel = 1;
  16523. break;
  16524. }
  16525. }
  16526. }
  16527. // Current Embedding Level
  16528. $cel = $pel;
  16529. // directional override status
  16530. $dos = 'N';
  16531. $remember = array();
  16532. // start-of-level-run
  16533. $sor = $pel % 2 ? 'R' : 'L';
  16534. $eor = $sor;
  16535. // Array of characters data
  16536. $chardata = Array();
  16537. // 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.
  16538. // In the resolution of levels in rules I1 and I2, the maximum embedding level of 62 can be reached.
  16539. for ($i = 0; $i < $numchars; ++$i) {
  16540. if ($ta[$i] == $this->unicode->uni_RLE) {
  16541. // X2. With each RLE, compute the least greater odd embedding level.
  16542. // 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.
  16543. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  16544. $next_level = $cel + ($cel % 2) + 1;
  16545. if ($next_level < 62) {
  16546. $remember[] = array('num' => $this->unicode->uni_RLE, 'cel' => $cel, 'dos' => $dos);
  16547. $cel = $next_level;
  16548. $dos = 'N';
  16549. $sor = $eor;
  16550. $eor = $cel % 2 ? 'R' : 'L';
  16551. }
  16552. } elseif ($ta[$i] == $this->unicode->uni_LRE) {
  16553. // X3. With each LRE, compute the least greater even embedding level.
  16554. // 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.
  16555. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  16556. $next_level = $cel + 2 - ($cel % 2);
  16557. if ($next_level < 62) {
  16558. $remember[] = array('num' => $this->unicode->uni_LRE, 'cel' => $cel, 'dos' => $dos);
  16559. $cel = $next_level;
  16560. $dos = 'N';
  16561. $sor = $eor;
  16562. $eor = $cel % 2 ? 'R' : 'L';
  16563. }
  16564. } elseif ($ta[$i] == $this->unicode->uni_RLO) {
  16565. // X4. With each RLO, compute the least greater odd embedding level.
  16566. // 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.
  16567. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  16568. $next_level = $cel + ($cel % 2) + 1;
  16569. if ($next_level < 62) {
  16570. $remember[] = array('num' => $this->unicode->uni_RLO, 'cel' => $cel, 'dos' => $dos);
  16571. $cel = $next_level;
  16572. $dos = 'R';
  16573. $sor = $eor;
  16574. $eor = $cel % 2 ? 'R' : 'L';
  16575. }
  16576. } elseif ($ta[$i] == $this->unicode->uni_LRO) {
  16577. // X5. With each LRO, compute the least greater even embedding level.
  16578. // 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.
  16579. // b. If the new level would not be valid, then this code is invalid. Do not change the current level or override status.
  16580. $next_level = $cel + 2 - ($cel % 2);
  16581. if ($next_level < 62) {
  16582. $remember[] = array('num' => $this->unicode->uni_LRO, 'cel' => $cel, 'dos' => $dos);
  16583. $cel = $next_level;
  16584. $dos = 'L';
  16585. $sor = $eor;
  16586. $eor = $cel % 2 ? 'R' : 'L';
  16587. }
  16588. } elseif ($ta[$i] == $this->unicode->uni_PDF) {
  16589. // 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.
  16590. if (count($remember)) {
  16591. $last = count($remember) - 1;
  16592. if (($remember[$last]['num'] == $this->unicode->uni_RLE) OR
  16593. ($remember[$last]['num'] == $this->unicode->uni_LRE) OR
  16594. ($remember[$last]['num'] == $this->unicode->uni_RLO) OR
  16595. ($remember[$last]['num'] == $this->unicode->uni_LRO)) {
  16596. $match = array_pop($remember);
  16597. $cel = $match['cel'];
  16598. $dos = $match['dos'];
  16599. $sor = $eor;
  16600. $eor = ($cel > $match['cel'] ? $cel : $match['cel']) % 2 ? 'R' : 'L';
  16601. }
  16602. }
  16603. } elseif (($ta[$i] != $this->unicode->uni_RLE) AND
  16604. ($ta[$i] != $this->unicode->uni_LRE) AND
  16605. ($ta[$i] != $this->unicode->uni_RLO) AND
  16606. ($ta[$i] != $this->unicode->uni_LRO) AND
  16607. ($ta[$i] != $this->unicode->uni_PDF)) {
  16608. // X6. For all types besides RLE, LRE, RLO, LRO, and PDF:
  16609. // a. Set the level of the current character to the current embedding level.
  16610. // b. Whenever the directional override status is not neutral, reset the current character type to the directional override status.
  16611. if ($dos != 'N') {
  16612. $chardir = $dos;
  16613. } else {
  16614. if (isset($this->unicode->uni_type[$ta[$i]])) {
  16615. $chardir = $this->unicode->uni_type[$ta[$i]];
  16616. } else {
  16617. $chardir = 'L';
  16618. }
  16619. }
  16620. // stores string characters and other information
  16621. $chardata[] = array('char' => $ta[$i], 'level' => $cel, 'type' => $chardir, 'sor' => $sor, 'eor' => $eor);
  16622. }
  16623. } // end for each char
  16624. // X8. All explicit directional embeddings and overrides are completely terminated at the end of each paragraph. Paragraph separators are not included in the embedding.
  16625. // X9. Remove all RLE, LRE, RLO, LRO, PDF, and BN codes.
  16626. // 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.
  16627. // 3.3.3 Resolving Weak Types
  16628. // 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.
  16629. // Nonspacing marks are now resolved based on the previous characters.
  16630. $numchars = count($chardata);
  16631. // 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.
  16632. $prevlevel = -1; // track level changes
  16633. $levcount = 0; // counts consecutive chars at the same level
  16634. for ($i = 0; $i < $numchars; ++$i) {
  16635. if ($chardata[$i]['type'] == 'NSM') {
  16636. if ($levcount) {
  16637. $chardata[$i]['type'] = $chardata[$i]['sor'];
  16638. } elseif ($i > 0) {
  16639. $chardata[$i]['type'] = $chardata[($i - 1)]['type'];
  16640. }
  16641. }
  16642. if ($chardata[$i]['level'] != $prevlevel) {
  16643. $levcount = 0;
  16644. } else {
  16645. ++$levcount;
  16646. }
  16647. $prevlevel = $chardata[$i]['level'];
  16648. }
  16649. // 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.
  16650. $prevlevel = -1;
  16651. $levcount = 0;
  16652. for ($i = 0; $i < $numchars; ++$i) {
  16653. if ($chardata[$i]['char'] == 'EN') {
  16654. for ($j = $levcount; $j >= 0; $j--) {
  16655. if ($chardata[$j]['type'] == 'AL') {
  16656. $chardata[$i]['type'] = 'AN';
  16657. } elseif (($chardata[$j]['type'] == 'L') OR ($chardata[$j]['type'] == 'R')) {
  16658. break;
  16659. }
  16660. }
  16661. }
  16662. if ($chardata[$i]['level'] != $prevlevel) {
  16663. $levcount = 0;
  16664. } else {
  16665. ++$levcount;
  16666. }
  16667. $prevlevel = $chardata[$i]['level'];
  16668. }
  16669. // W3. Change all ALs to R.
  16670. for ($i = 0; $i < $numchars; ++$i) {
  16671. if ($chardata[$i]['type'] == 'AL') {
  16672. $chardata[$i]['type'] = 'R';
  16673. }
  16674. }
  16675. // 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.
  16676. $prevlevel = -1;
  16677. $levcount = 0;
  16678. for ($i = 0; $i < $numchars; ++$i) {
  16679. if (($levcount > 0) AND (($i + 1) < $numchars) AND ($chardata[($i + 1)]['level'] == $prevlevel)) {
  16680. if (($chardata[$i]['type'] == 'ES') AND ($chardata[($i - 1)]['type'] == 'EN') AND ($chardata[($i + 1)]['type'] == 'EN')) {
  16681. $chardata[$i]['type'] = 'EN';
  16682. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i - 1)]['type'] == 'EN') AND ($chardata[($i + 1)]['type'] == 'EN')) {
  16683. $chardata[$i]['type'] = 'EN';
  16684. } elseif (($chardata[$i]['type'] == 'CS') AND ($chardata[($i - 1)]['type'] == 'AN') AND ($chardata[($i + 1)]['type'] == 'AN')) {
  16685. $chardata[$i]['type'] = 'AN';
  16686. }
  16687. }
  16688. if ($chardata[$i]['level'] != $prevlevel) {
  16689. $levcount = 0;
  16690. } else {
  16691. ++$levcount;
  16692. }
  16693. $prevlevel = $chardata[$i]['level'];
  16694. }
  16695. // W5. A sequence of European terminators adjacent to European numbers changes to all European numbers.
  16696. $prevlevel = -1;
  16697. $levcount = 0;
  16698. for ($i = 0; $i < $numchars; ++$i) {
  16699. if ($chardata[$i]['type'] == 'ET') {
  16700. if (($levcount > 0) AND ($chardata[($i - 1)]['type'] == 'EN')) {
  16701. $chardata[$i]['type'] = 'EN';
  16702. } else {
  16703. $j = $i + 1;
  16704. while (($j < $numchars) AND ($chardata[$j]['level'] == $prevlevel)) {
  16705. if ($chardata[$j]['type'] == 'EN') {
  16706. $chardata[$i]['type'] = 'EN';
  16707. break;
  16708. } elseif ($chardata[$j]['type'] != 'ET') {
  16709. break;
  16710. }
  16711. ++$j;
  16712. }
  16713. }
  16714. }
  16715. if ($chardata[$i]['level'] != $prevlevel) {
  16716. $levcount = 0;
  16717. } else {
  16718. ++$levcount;
  16719. }
  16720. $prevlevel = $chardata[$i]['level'];
  16721. }
  16722. // W6. Otherwise, separators and terminators change to Other Neutral.
  16723. $prevlevel = -1;
  16724. $levcount = 0;
  16725. for ($i = 0; $i < $numchars; ++$i) {
  16726. if (($chardata[$i]['type'] == 'ET') OR ($chardata[$i]['type'] == 'ES') OR ($chardata[$i]['type'] == 'CS')) {
  16727. $chardata[$i]['type'] = 'ON';
  16728. }
  16729. if ($chardata[$i]['level'] != $prevlevel) {
  16730. $levcount = 0;
  16731. } else {
  16732. ++$levcount;
  16733. }
  16734. $prevlevel = $chardata[$i]['level'];
  16735. }
  16736. //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.
  16737. $prevlevel = -1;
  16738. $levcount = 0;
  16739. for ($i = 0; $i < $numchars; ++$i) {
  16740. if ($chardata[$i]['char'] == 'EN') {
  16741. for ($j = $levcount; $j >= 0; $j--) {
  16742. if ($chardata[$j]['type'] == 'L') {
  16743. $chardata[$i]['type'] = 'L';
  16744. } elseif ($chardata[$j]['type'] == 'R') {
  16745. break;
  16746. }
  16747. }
  16748. }
  16749. if ($chardata[$i]['level'] != $prevlevel) {
  16750. $levcount = 0;
  16751. } else {
  16752. ++$levcount;
  16753. }
  16754. $prevlevel = $chardata[$i]['level'];
  16755. }
  16756. // 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.
  16757. $prevlevel = -1;
  16758. $levcount = 0;
  16759. for ($i = 0; $i < $numchars; ++$i) {
  16760. if (($levcount > 0) AND (($i + 1) < $numchars) AND ($chardata[($i + 1)]['level'] == $prevlevel)) {
  16761. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i - 1)]['type'] == 'L') AND ($chardata[($i + 1)]['type'] == 'L')) {
  16762. $chardata[$i]['type'] = 'L';
  16763. } elseif (($chardata[$i]['type'] == 'N') AND
  16764. (($chardata[($i - 1)]['type'] == 'R') OR ($chardata[($i - 1)]['type'] == 'EN') OR ($chardata[($i - 1)]['type'] == 'AN')) AND
  16765. (($chardata[($i + 1)]['type'] == 'R') OR ($chardata[($i + 1)]['type'] == 'EN') OR ($chardata[($i + 1)]['type'] == 'AN'))) {
  16766. $chardata[$i]['type'] = 'R';
  16767. } elseif ($chardata[$i]['type'] == 'N') {
  16768. // N2. Any remaining neutrals take the embedding direction
  16769. $chardata[$i]['type'] = $chardata[$i]['sor'];
  16770. }
  16771. } elseif (($levcount == 0) AND (($i + 1) < $numchars) AND ($chardata[($i + 1)]['level'] == $prevlevel)) {
  16772. // first char
  16773. if (($chardata[$i]['type'] == 'N') AND ($chardata[$i]['sor'] == 'L') AND ($chardata[($i + 1)]['type'] == 'L')) {
  16774. $chardata[$i]['type'] = 'L';
  16775. } elseif (($chardata[$i]['type'] == 'N') AND
  16776. (($chardata[$i]['sor'] == 'R') OR ($chardata[$i]['sor'] == 'EN') OR ($chardata[$i]['sor'] == 'AN')) AND
  16777. (($chardata[($i + 1)]['type'] == 'R') OR ($chardata[($i + 1)]['type'] == 'EN') OR ($chardata[($i + 1)]['type'] == 'AN'))) {
  16778. $chardata[$i]['type'] = 'R';
  16779. } elseif ($chardata[$i]['type'] == 'N') {
  16780. // N2. Any remaining neutrals take the embedding direction
  16781. $chardata[$i]['type'] = $chardata[$i]['sor'];
  16782. }
  16783. } elseif (($levcount > 0) AND ((($i + 1) == $numchars) OR (($i + 1) < $numchars) AND ($chardata[($i + 1)]['level'] != $prevlevel))) {
  16784. //last char
  16785. if (($chardata[$i]['type'] == 'N') AND ($chardata[($i - 1)]['type'] == 'L') AND ($chardata[$i]['eor'] == 'L')) {
  16786. $chardata[$i]['type'] = 'L';
  16787. } elseif (($chardata[$i]['type'] == 'N') AND
  16788. (($chardata[($i - 1)]['type'] == 'R') OR ($chardata[($i - 1)]['type'] == 'EN') OR ($chardata[($i - 1)]['type'] == 'AN')) AND
  16789. (($chardata[$i]['eor'] == 'R') OR ($chardata[$i]['eor'] == 'EN') OR ($chardata[$i]['eor'] == 'AN'))) {
  16790. $chardata[$i]['type'] = 'R';
  16791. } elseif ($chardata[$i]['type'] == 'N') {
  16792. // N2. Any remaining neutrals take the embedding direction
  16793. $chardata[$i]['type'] = $chardata[$i]['sor'];
  16794. }
  16795. } elseif ($chardata[$i]['type'] == 'N') {
  16796. // N2. Any remaining neutrals take the embedding direction
  16797. $chardata[$i]['type'] = $chardata[$i]['sor'];
  16798. }
  16799. if ($chardata[$i]['level'] != $prevlevel) {
  16800. $levcount = 0;
  16801. } else {
  16802. ++$levcount;
  16803. }
  16804. $prevlevel = $chardata[$i]['level'];
  16805. }
  16806. // 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.
  16807. // I2. For all characters with an odd (right-to-left) embedding direction, those of type L, EN or AN go up one level.
  16808. for ($i = 0; $i < $numchars; ++$i) {
  16809. $odd = $chardata[$i]['level'] % 2;
  16810. if ($odd) {
  16811. if (($chardata[$i]['type'] == 'L') OR ($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  16812. $chardata[$i]['level'] += 1;
  16813. }
  16814. } else {
  16815. if ($chardata[$i]['type'] == 'R') {
  16816. $chardata[$i]['level'] += 1;
  16817. } elseif (($chardata[$i]['type'] == 'AN') OR ($chardata[$i]['type'] == 'EN')) {
  16818. $chardata[$i]['level'] += 2;
  16819. }
  16820. }
  16821. $maxlevel = max($chardata[$i]['level'], $maxlevel);
  16822. }
  16823. // L1. On each line, reset the embedding level of the following characters to the paragraph embedding level:
  16824. // 1. Segment separators,
  16825. // 2. Paragraph separators,
  16826. // 3. Any sequence of whitespace characters preceding a segment separator or paragraph separator, and
  16827. // 4. Any sequence of white space characters at the end of the line.
  16828. for ($i = 0; $i < $numchars; ++$i) {
  16829. if (($chardata[$i]['type'] == 'B') OR ($chardata[$i]['type'] == 'S')) {
  16830. $chardata[$i]['level'] = $pel;
  16831. } elseif ($chardata[$i]['type'] == 'WS') {
  16832. $j = $i + 1;
  16833. while ($j < $numchars) {
  16834. if ((($chardata[$j]['type'] == 'B') OR ($chardata[$j]['type'] == 'S')) OR
  16835. (($j == ($numchars - 1)) AND ($chardata[$j]['type'] == 'WS'))) {
  16836. $chardata[$i]['level'] = $pel;
  16837. break;
  16838. } elseif ($chardata[$j]['type'] != 'WS') {
  16839. break;
  16840. }
  16841. ++$j;
  16842. }
  16843. }
  16844. }
  16845. // Arabic Shaping
  16846. // 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.
  16847. if ($arabic) {
  16848. $endedletter = array(1569, 1570, 1571, 1572, 1573, 1575, 1577, 1583, 1584, 1585, 1586, 1608, 1688);
  16849. $alfletter = array(1570, 1571, 1573, 1575);
  16850. $chardata2 = $chardata;
  16851. $laaletter = false;
  16852. $charAL = array();
  16853. $x = 0;
  16854. for ($i = 0; $i < $numchars; ++$i) {
  16855. if (($this->unicode->uni_type[$chardata[$i]['char']] == 'AL') OR ($chardata[$i]['char'] == 32) OR ($chardata[$i]['char'] == 8204)) {
  16856. $charAL[$x] = $chardata[$i];
  16857. $charAL[$x]['i'] = $i;
  16858. $chardata[$i]['x'] = $x;
  16859. ++$x;
  16860. }
  16861. }
  16862. $numAL = $x;
  16863. for ($i = 0; $i < $numchars; ++$i) {
  16864. $thischar = $chardata[$i];
  16865. if ($i > 0) {
  16866. $prevchar = $chardata[($i - 1)];
  16867. } else {
  16868. $prevchar = false;
  16869. }
  16870. if (($i + 1) < $numchars) {
  16871. $nextchar = $chardata[($i + 1)];
  16872. } else {
  16873. $nextchar = false;
  16874. }
  16875. if ($this->unicode->uni_type[$thischar['char']] == 'AL') {
  16876. $x = $thischar['x'];
  16877. if ($x > 0) {
  16878. $prevchar = $charAL[($x - 1)];
  16879. } else {
  16880. $prevchar = false;
  16881. }
  16882. if (($x + 1) < $numAL) {
  16883. $nextchar = $charAL[($x + 1)];
  16884. } else {
  16885. $nextchar = false;
  16886. }
  16887. // if laa letter
  16888. if (($prevchar !== false) AND ($prevchar['char'] == 1604) AND (in_array($thischar['char'], $alfletter))) {
  16889. $arabicarr = $this->unicode->uni_laa_array;
  16890. $laaletter = true;
  16891. if ($x > 1) {
  16892. $prevchar = $charAL[($x - 2)];
  16893. } else {
  16894. $prevchar = false;
  16895. }
  16896. } else {
  16897. $arabicarr = $this->unicode->uni_arabicsubst;
  16898. $laaletter = false;
  16899. }
  16900. if (($prevchar !== false) AND ($nextchar !== false) AND
  16901. (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
  16902. (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
  16903. ($prevchar['type'] == $thischar['type']) AND
  16904. ($nextchar['type'] == $thischar['type']) AND
  16905. ($nextchar['char'] != 1567)) {
  16906. if (in_array($prevchar['char'], $endedletter)) {
  16907. if (isset($arabicarr[$thischar['char']][2])) {
  16908. // initial
  16909. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  16910. }
  16911. } else {
  16912. if (isset($arabicarr[$thischar['char']][3])) {
  16913. // medial
  16914. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][3];
  16915. }
  16916. }
  16917. } elseif (($nextchar !== false) AND
  16918. (($this->unicode->uni_type[$nextchar['char']] == 'AL') OR ($this->unicode->uni_type[$nextchar['char']] == 'NSM')) AND
  16919. ($nextchar['type'] == $thischar['type']) AND
  16920. ($nextchar['char'] != 1567)) {
  16921. if (isset($arabicarr[$chardata[$i]['char']][2])) {
  16922. // initial
  16923. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][2];
  16924. }
  16925. } elseif ((($prevchar !== false) AND
  16926. (($this->unicode->uni_type[$prevchar['char']] == 'AL') OR ($this->unicode->uni_type[$prevchar['char']] == 'NSM')) AND
  16927. ($prevchar['type'] == $thischar['type'])) OR
  16928. (($nextchar !== false) AND ($nextchar['char'] == 1567))) {
  16929. // final
  16930. if (($i > 1) AND ($thischar['char'] == 1607) AND
  16931. ($chardata[$i - 1]['char'] == 1604) AND
  16932. ($chardata[$i - 2]['char'] == 1604)) {
  16933. //Allah Word
  16934. // mark characters to delete with false
  16935. $chardata2[$i - 2]['char'] = false;
  16936. $chardata2[$i - 1]['char'] = false;
  16937. $chardata2[$i]['char'] = 65010;
  16938. } else {
  16939. if (($prevchar !== false) AND in_array($prevchar['char'], $endedletter)) {
  16940. if (isset($arabicarr[$thischar['char']][0])) {
  16941. // isolated
  16942. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  16943. }
  16944. } else {
  16945. if (isset($arabicarr[$thischar['char']][1])) {
  16946. // final
  16947. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][1];
  16948. }
  16949. }
  16950. }
  16951. } elseif (isset($arabicarr[$thischar['char']][0])) {
  16952. // isolated
  16953. $chardata2[$i]['char'] = $arabicarr[$thischar['char']][0];
  16954. }
  16955. // if laa letter
  16956. if ($laaletter) {
  16957. // mark characters to delete with false
  16958. $chardata2[($charAL[($x - 1)]['i'])]['char'] = false;
  16959. }
  16960. } // end if AL (Arabic Letter)
  16961. } // end for each char
  16962. /*
  16963. * Combining characters that can occur with Arabic Shadda (0651 HEX, 1617 DEC) are replaced.
  16964. * Putting the combining mark and shadda in the same glyph allows us to avoid the two marks overlapping each other in an illegible manner.
  16965. */
  16966. for ($i = 0; $i < ($numchars - 1); ++$i) {
  16967. if (($chardata2[$i]['char'] == 1617) AND (isset($this->unicode->uni_diacritics[($chardata2[$i + 1]['char'])]))) {
  16968. // check if the subtitution font is defined on current font
  16969. if (isset($this->CurrentFont['cw'][($this->unicode->uni_diacritics[($chardata2[$i + 1]['char'])])])) {
  16970. $chardata2[$i]['char'] = false;
  16971. $chardata2[$i + 1]['char'] = $this->unicode->uni_diacritics[($chardata2[$i + 1]['char'])];
  16972. }
  16973. }
  16974. }
  16975. // remove marked characters
  16976. foreach ($chardata2 as $key => $value) {
  16977. if ($value['char'] === false) {
  16978. unset($chardata2[$key]);
  16979. }
  16980. }
  16981. $chardata = array_values($chardata2);
  16982. $numchars = count($chardata);
  16983. unset($chardata2);
  16984. unset($arabicarr);
  16985. unset($laaletter);
  16986. unset($charAL);
  16987. }
  16988. // 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.
  16989. for ($j = $maxlevel; $j > 0; $j--) {
  16990. $ordarray = Array();
  16991. $revarr = Array();
  16992. $onlevel = false;
  16993. for ($i = 0; $i < $numchars; ++$i) {
  16994. if ($chardata[$i]['level'] >= $j) {
  16995. $onlevel = true;
  16996. if (isset($this->unicode->uni_mirror[$chardata[$i]['char']])) {
  16997. // 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.
  16998. $chardata[$i]['char'] = $this->unicode->uni_mirror[$chardata[$i]['char']];
  16999. }
  17000. $revarr[] = $chardata[$i];
  17001. } else {
  17002. if ($onlevel) {
  17003. $revarr = array_reverse($revarr);
  17004. $ordarray = array_merge($ordarray, $revarr);
  17005. $revarr = Array();
  17006. $onlevel = false;
  17007. }
  17008. $ordarray[] = $chardata[$i];
  17009. }
  17010. }
  17011. if ($onlevel) {
  17012. $revarr = array_reverse($revarr);
  17013. $ordarray = array_merge($ordarray, $revarr);
  17014. }
  17015. $chardata = $ordarray;
  17016. }
  17017. $ordarray = array();
  17018. for ($i = 0; $i < $numchars; ++$i) {
  17019. $ordarray[] = $chardata[$i]['char'];
  17020. // store char values for subsetting
  17021. $this->CurrentFont['subsetchars'][$chardata[$i]['char']] = true;
  17022. }
  17023. // update font subsetchars
  17024. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  17025. return $ordarray;
  17026. }
  17027. // END OF BIDIRECTIONAL TEXT SECTION -------------------
  17028. /**
  17029. * Encode a name object.
  17030. * @param $name (string) Name object to encode.
  17031. * @return (string) Encoded name object.
  17032. * @protected
  17033. * @author Nicola Asuni
  17034. * @since 5.9.097 (2011-06-23)
  17035. */
  17036. protected function encodeNameObject($name) {
  17037. $escname = '';
  17038. $length = strlen($name);
  17039. for ($i = 0; $i < $length; ++$i) {
  17040. $chr = $name[$i];
  17041. if (preg_match('/[0-9a-zA-Z]/', $chr) == 1) {
  17042. $escname .= $chr;
  17043. } else {
  17044. $escname .= sprintf('#%02X', ord($chr));
  17045. }
  17046. }
  17047. return $escname;
  17048. }
  17049. /**
  17050. * Add a Named Destination.
  17051. * NOTE: destination names are unique, so only last entry will be saved.
  17052. * @param $name (string) Destination name.
  17053. * @param $y (float) Y position in user units of the destiantion on the selected page (default = -1 = current position; 0 = page start;).
  17054. * @param $page (int) Target page number (leave empty for current page).
  17055. * @param $x (float) X position in user units of the destiantion on the selected page (default = -1 = current position;).
  17056. * @return (string) Stripped named destination identifier or false in case of error.
  17057. * @public
  17058. * @author Christian Deligant, Nicola Asuni
  17059. * @since 5.9.097 (2011-06-23)
  17060. */
  17061. public function setDestination($name, $y = -1, $page = '', $x = -1) {
  17062. // remove unsupported characters
  17063. $name = $this->encodeNameObject($name);
  17064. if ($this->empty_string($name)) {
  17065. return false;
  17066. }
  17067. if ($y == -1) {
  17068. $y = $this->GetY();
  17069. } elseif ($y < 0) {
  17070. $y = 0;
  17071. } elseif ($y > $this->h) {
  17072. $y = $this->h;
  17073. }
  17074. if ($x == -1) {
  17075. $x = $this->GetX();
  17076. } elseif ($x < 0) {
  17077. $x = 0;
  17078. } elseif ($x > $this->w) {
  17079. $x = $this->w;
  17080. }
  17081. if (empty($page)) {
  17082. $page = $this->PageNo();
  17083. if (empty($page)) {
  17084. return;
  17085. }
  17086. }
  17087. $this->dests[$name] = array('x' => $x, 'y' => $y, 'p' => $page);
  17088. return $name;
  17089. }
  17090. /**
  17091. * Return the Named Destination array.
  17092. * @return (array) Named Destination array.
  17093. * @public
  17094. * @author Nicola Asuni
  17095. * @since 5.9.097 (2011-06-23)
  17096. */
  17097. public function getDestination() {
  17098. return $this->dests;
  17099. }
  17100. /**
  17101. * Insert Named Destinations.
  17102. * @protected
  17103. * @author Johannes G�ntert, Nicola Asuni
  17104. * @since 5.9.098 (2011-06-23)
  17105. */
  17106. protected function _putdests() {
  17107. if (empty($this->dests)) {
  17108. return;
  17109. }
  17110. $this->n_dests = $this->_newobj();
  17111. $out = ' <<';
  17112. foreach ($this->dests as $name => $o) {
  17113. $out .= ' /' . $name . ' ' . sprintf('[%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  17114. }
  17115. $out .= ' >>';
  17116. $out .= "\n" . 'endobj';
  17117. $this->_out($out);
  17118. }
  17119. /**
  17120. * Adds a bookmark - alias for Bookmark().
  17121. * @param $txt (string) Bookmark description.
  17122. * @param $level (int) Bookmark level (minimum value is 0).
  17123. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  17124. * @param $page (int) Target page number (leave empty for current page).
  17125. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  17126. * @param $color (array) RGB color array (values from 0 to 255).
  17127. * @public
  17128. */
  17129. public function setBookmark($txt, $level = 0, $y = -1, $page = '', $style = '', $color = array(0, 0, 0)) {
  17130. $this->Bookmark($txt, $level, $y, $page, $style, $color);
  17131. }
  17132. /**
  17133. * Adds a bookmark.
  17134. * @param $txt (string) Bookmark description.
  17135. * @param $level (int) Bookmark level (minimum value is 0).
  17136. * @param $y (float) Y position in user units of the bookmark on the selected page (default = -1 = current position; 0 = page start;).
  17137. * @param $page (int) Target page number (leave empty for current page).
  17138. * @param $style (string) Font style: B = Bold, I = Italic, BI = Bold + Italic.
  17139. * @param $color (array) RGB color array (values from 0 to 255).
  17140. * @param $x (float) X position in user units of the bookmark on the selected page (default = -1 = current position;).
  17141. * @public
  17142. * @author Olivier Plathey, Nicola Asuni
  17143. * @since 2.1.002 (2008-02-12)
  17144. */
  17145. public function Bookmark($txt, $level = 0, $y = -1, $page = '', $style = '', $color = array(0, 0, 0), $x = -1) {
  17146. if ($level < 0) {
  17147. $level = 0;
  17148. }
  17149. if (isset($this->outlines[0])) {
  17150. $lastoutline = end($this->outlines);
  17151. $maxlevel = $lastoutline['l'] + 1;
  17152. } else {
  17153. $maxlevel = 0;
  17154. }
  17155. if ($level > $maxlevel) {
  17156. $level = $maxlevel;
  17157. }
  17158. if ($y == -1) {
  17159. $y = $this->GetY();
  17160. } elseif ($y < 0) {
  17161. $y = 0;
  17162. } elseif ($y > $this->h) {
  17163. $y = $this->h;
  17164. }
  17165. if ($x == -1) {
  17166. $x = $this->GetX();
  17167. } elseif ($x < 0) {
  17168. $x = 0;
  17169. } elseif ($x > $this->w) {
  17170. $x = $this->w;
  17171. }
  17172. if (empty($page)) {
  17173. $page = $this->PageNo();
  17174. if (empty($page)) {
  17175. return;
  17176. }
  17177. }
  17178. $this->outlines[] = array('t' => $txt, 'l' => $level, 'x' => $x, 'y' => $y, 'p' => $page, 's' => strtoupper($style), 'c' => $color);
  17179. }
  17180. /**
  17181. * Sort bookmarks for page and key.
  17182. * @protected
  17183. * @since 5.9.119 (2011-09-19)
  17184. */
  17185. protected function sortBookmarks() {
  17186. // get sorting columns
  17187. $outline_p = array();
  17188. $outline_y = array();
  17189. foreach ($this->outlines as $key => $row) {
  17190. $outline_p[$key] = $row['p'];
  17191. $outline_k[$key] = $key;
  17192. }
  17193. // sort outlines by page and original position
  17194. array_multisort($outline_p, SORT_NUMERIC, SORT_ASC, $outline_k, SORT_NUMERIC, SORT_ASC, $this->outlines);
  17195. }
  17196. /**
  17197. * Create a bookmark PDF string.
  17198. * @protected
  17199. * @author Olivier Plathey, Nicola Asuni
  17200. * @since 2.1.002 (2008-02-12)
  17201. */
  17202. protected function _putbookmarks() {
  17203. $nb = count($this->outlines);
  17204. if ($nb == 0) {
  17205. return;
  17206. }
  17207. // sort bookmarks
  17208. $this->sortBookmarks();
  17209. $lru = array();
  17210. $level = 0;
  17211. foreach ($this->outlines as $i => $o) {
  17212. if ($o['l'] > 0) {
  17213. $parent = $lru[($o['l'] - 1)];
  17214. //Set parent and last pointers
  17215. $this->outlines[$i]['parent'] = $parent;
  17216. $this->outlines[$parent]['last'] = $i;
  17217. if ($o['l'] > $level) {
  17218. //Level increasing: set first pointer
  17219. $this->outlines[$parent]['first'] = $i;
  17220. }
  17221. } else {
  17222. $this->outlines[$i]['parent'] = $nb;
  17223. }
  17224. if (($o['l'] <= $level) AND ($i > 0)) {
  17225. //Set prev and next pointers
  17226. $prev = $lru[$o['l']];
  17227. $this->outlines[$prev]['next'] = $i;
  17228. $this->outlines[$i]['prev'] = $prev;
  17229. }
  17230. $lru[$o['l']] = $i;
  17231. $level = $o['l'];
  17232. }
  17233. //Outline items
  17234. $n = $this->n + 1;
  17235. $nltags = '/<br[\s]?\/>|<\/(blockquote|dd|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|p|pre|ul|tcpdf|table|tr|td)>/si';
  17236. foreach ($this->outlines as $i => $o) {
  17237. $oid = $this->_newobj();
  17238. // covert HTML title to string
  17239. $title = preg_replace($nltags, "\n", $o['t']);
  17240. $title = preg_replace("/[\r]+/si", '', $title);
  17241. $title = preg_replace("/[\n]+/si", "\n", $title);
  17242. $title = strip_tags($title);
  17243. $title = $this->stringTrim($title);
  17244. $out = '<</Title ' . $this->_textstring($title, $oid);
  17245. $out .= ' /Parent ' . ($n + $o['parent']) . ' 0 R';
  17246. if (isset($o['prev'])) {
  17247. $out .= ' /Prev ' . ($n + $o['prev']) . ' 0 R';
  17248. }
  17249. if (isset($o['next'])) {
  17250. $out .= ' /Next ' . ($n + $o['next']) . ' 0 R';
  17251. }
  17252. if (isset($o['first'])) {
  17253. $out .= ' /First ' . ($n + $o['first']) . ' 0 R';
  17254. }
  17255. if (isset($o['last'])) {
  17256. $out .= ' /Last ' . ($n + $o['last']) . ' 0 R';
  17257. }
  17258. if (isset($this->page_obj_id[($o['p'])])) {
  17259. $out .= ' ' . sprintf('/Dest [%u 0 R /XYZ %F %F null]', $this->page_obj_id[($o['p'])], ($o['x'] * $this->k), ($this->pagedim[$o['p']]['h'] - ($o['y'] * $this->k)));
  17260. }
  17261. // set font style
  17262. $style = 0;
  17263. if (!empty($o['s'])) {
  17264. // bold
  17265. if (strpos($o['s'], 'B') !== false) {
  17266. $style |= 2;
  17267. }
  17268. // oblique
  17269. if (strpos($o['s'], 'I') !== false) {
  17270. $style |= 1;
  17271. }
  17272. }
  17273. $out .= sprintf(' /F %d', $style);
  17274. // set bookmark color
  17275. if (isset($o['c']) AND is_array($o['c']) AND (count($o['c']) == 3)) {
  17276. $color = array_values($o['c']);
  17277. $out .= sprintf(' /C [%F %F %F]', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  17278. } else {
  17279. // black
  17280. $out .= ' /C [0.0 0.0 0.0]';
  17281. }
  17282. $out .= ' /Count 0'; // normally closed item
  17283. $out .= ' >>';
  17284. $out .= "\n" . 'endobj';
  17285. $this->_out($out);
  17286. }
  17287. //Outline root
  17288. $this->OutlineRoot = $this->_newobj();
  17289. $this->_out('<< /Type /Outlines /First ' . $n . ' 0 R /Last ' . ($n + $lru[0]) . ' 0 R >>' . "\n" . 'endobj');
  17290. }
  17291. // --- JAVASCRIPT ------------------------------------------------------
  17292. /**
  17293. * Adds a javascript
  17294. * @param $script (string) Javascript code
  17295. * @public
  17296. * @author Johannes G�ntert, Nicola Asuni
  17297. * @since 2.1.002 (2008-02-12)
  17298. */
  17299. public function IncludeJS($script) {
  17300. $this->javascript .= $script;
  17301. }
  17302. /**
  17303. * Adds a javascript object and return object ID
  17304. * @param $script (string) Javascript code
  17305. * @param $onload (boolean) if true executes this object when opening the document
  17306. * @return int internal object ID
  17307. * @public
  17308. * @author Nicola Asuni
  17309. * @since 4.8.000 (2009-09-07)
  17310. */
  17311. public function addJavascriptObject($script, $onload = false) {
  17312. if ($this->pdfa_mode) {
  17313. // javascript is not allowed in PDF/A mode
  17314. return false;
  17315. }
  17316. ++$this->n;
  17317. $this->js_objects[$this->n] = array('n' => $this->n, 'js' => $script, 'onload' => $onload);
  17318. return $this->n;
  17319. }
  17320. /**
  17321. * Create a javascript PDF string.
  17322. * @protected
  17323. * @author Johannes G�ntert, Nicola Asuni
  17324. * @since 2.1.002 (2008-02-12)
  17325. */
  17326. protected function _putjavascript() {
  17327. if ($this->pdfa_mode OR (empty($this->javascript) AND empty($this->js_objects))) {
  17328. return;
  17329. }
  17330. if (strpos($this->javascript, 'this.addField') > 0) {
  17331. if (!$this->ur['enabled']) {
  17332. //$this->setUserRights();
  17333. }
  17334. // the following two lines are used to avoid form fields duplication after saving
  17335. // The addField method only works when releasing user rights (UR3)
  17336. $jsa = sprintf("ftcpdfdocsaved=this.addField('%s','%s',%d,[%F,%F,%F,%F]);", 'tcpdfdocsaved', 'text', 0, 0, 1, 0, 1);
  17337. $jsb = "getField('tcpdfdocsaved').value='saved';";
  17338. $this->javascript = $jsa . "\n" . $this->javascript . "\n" . $jsb;
  17339. }
  17340. $this->n_js = $this->_newobj();
  17341. $out = ' << /Names [';
  17342. if (!empty($this->javascript)) {
  17343. $out .= ' (EmbeddedJS) ' . ($this->n + 1) . ' 0 R';
  17344. }
  17345. if (!empty($this->js_objects)) {
  17346. foreach ($this->js_objects as $key => $val) {
  17347. if ($val['onload']) {
  17348. $out .= ' (JS' . $key . ') ' . $key . ' 0 R';
  17349. }
  17350. }
  17351. }
  17352. $out .= ' ] >>';
  17353. $out .= "\n" . 'endobj';
  17354. $this->_out($out);
  17355. // default Javascript object
  17356. if (!empty($this->javascript)) {
  17357. $obj_id = $this->_newobj();
  17358. $out = '<< /S /JavaScript';
  17359. $out .= ' /JS ' . $this->_textstring($this->javascript, $obj_id);
  17360. $out .= ' >>';
  17361. $out .= "\n" . 'endobj';
  17362. $this->_out($out);
  17363. }
  17364. // additional Javascript objects
  17365. if (!empty($this->js_objects)) {
  17366. foreach ($this->js_objects as $key => $val) {
  17367. $out = $this->_getobj($key) . "\n" . ' << /S /JavaScript /JS ' . $this->_textstring($val['js'], $key) . ' >>' . "\n" . 'endobj';
  17368. $this->_out($out);
  17369. }
  17370. }
  17371. }
  17372. /**
  17373. * Convert color to javascript color.
  17374. * @param $color (string) color name or "#RRGGBB"
  17375. * @protected
  17376. * @author Denis Van Nuffelen, Nicola Asuni
  17377. * @since 2.1.002 (2008-02-12)
  17378. */
  17379. protected function _JScolor($color) {
  17380. static $aColors = array('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  17381. if (substr($color, 0, 1) == '#') {
  17382. return sprintf("['RGB',%F,%F,%F]", hexdec(substr($color, 1, 2)) / 255, hexdec(substr($color, 3, 2)) / 255, hexdec(substr($color, 5, 2)) / 255);
  17383. }
  17384. if (!in_array($color, $aColors)) {
  17385. $this->Error('Invalid color: ' . $color);
  17386. }
  17387. return 'color.' . $color;
  17388. }
  17389. /**
  17390. * Adds a javascript form field.
  17391. * @param $type (string) field type
  17392. * @param $name (string) field name
  17393. * @param $x (int) horizontal position
  17394. * @param $y (int) vertical position
  17395. * @param $w (int) width
  17396. * @param $h (int) height
  17397. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  17398. * @protected
  17399. * @author Denis Van Nuffelen, Nicola Asuni
  17400. * @since 2.1.002 (2008-02-12)
  17401. */
  17402. protected function _addfield($type, $name, $x, $y, $w, $h, $prop) {
  17403. if ($this->rtl) {
  17404. $x = $x - $w;
  17405. }
  17406. // the followind avoid fields duplication after saving the document
  17407. $this->javascript .= "if (getField('tcpdfdocsaved').value != 'saved') {";
  17408. $k = $this->k;
  17409. $this->javascript .= sprintf("f" . $name . "=this.addField('%s','%s',%u,[%F,%F,%F,%F]);", $name, $type, $this->PageNo() - 1, $x * $k, ($this->h - $y) * $k + 1, ($x + $w) * $k, ($this->h - $y - $h) * $k + 1) . "\n";
  17410. $this->javascript .= 'f' . $name . '.textSize=' . $this->FontSizePt . ";\n";
  17411. while (list($key, $val) = each($prop)) {
  17412. if (strcmp(substr($key, -5), 'Color') == 0) {
  17413. $val = $this->_JScolor($val);
  17414. } else {
  17415. $val = "'" . $val . "'";
  17416. }
  17417. $this->javascript .= 'f' . $name . '.' . $key . '=' . $val . ";\n";
  17418. }
  17419. if ($this->rtl) {
  17420. $this->x -= $w;
  17421. } else {
  17422. $this->x += $w;
  17423. }
  17424. $this->javascript .= '}';
  17425. }
  17426. // --- FORM FIELDS -----------------------------------------------------
  17427. /**
  17428. * Convert JavaScript form fields properties array to Annotation Properties array.
  17429. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  17430. * @return array of annotation properties
  17431. * @protected
  17432. * @author Nicola Asuni
  17433. * @since 4.8.000 (2009-09-06)
  17434. */
  17435. protected function getAnnotOptFromJSProp($prop) {
  17436. if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
  17437. // the annotation options area lready defined
  17438. return $prop['aopt'];
  17439. }
  17440. $opt = array(); // value to be returned
  17441. // alignment: Controls how the text is laid out within the text field.
  17442. if (isset($prop['alignment'])) {
  17443. switch ($prop['alignment']) {
  17444. case 'left': {
  17445. $opt['q'] = 0;
  17446. break;
  17447. }
  17448. case 'center': {
  17449. $opt['q'] = 1;
  17450. break;
  17451. }
  17452. case 'right': {
  17453. $opt['q'] = 2;
  17454. break;
  17455. }
  17456. default: {
  17457. $opt['q'] = ($this->rtl) ? 2 : 0;
  17458. break;
  17459. }
  17460. }
  17461. }
  17462. // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
  17463. if (isset($prop['lineWidth'])) {
  17464. $linewidth = intval($prop['lineWidth']);
  17465. } else {
  17466. $linewidth = 1;
  17467. }
  17468. // borderStyle: The border style for a field.
  17469. if (isset($prop['borderStyle'])) {
  17470. switch ($prop['borderStyle']) {
  17471. case 'border.d':
  17472. case 'dashed': {
  17473. $opt['border'] = array(0, 0, $linewidth, array(3, 2));
  17474. $opt['bs'] = array('w' => $linewidth, 's' => 'D', 'd' => array(3, 2));
  17475. break;
  17476. }
  17477. case 'border.b':
  17478. case 'beveled': {
  17479. $opt['border'] = array(0, 0, $linewidth);
  17480. $opt['bs'] = array('w' => $linewidth, 's' => 'B');
  17481. break;
  17482. }
  17483. case 'border.i':
  17484. case 'inset': {
  17485. $opt['border'] = array(0, 0, $linewidth);
  17486. $opt['bs'] = array('w' => $linewidth, 's' => 'I');
  17487. break;
  17488. }
  17489. case 'border.u':
  17490. case 'underline': {
  17491. $opt['border'] = array(0, 0, $linewidth);
  17492. $opt['bs'] = array('w' => $linewidth, 's' => 'U');
  17493. break;
  17494. }
  17495. case 'border.s':
  17496. case 'solid': {
  17497. $opt['border'] = array(0, 0, $linewidth);
  17498. $opt['bs'] = array('w' => $linewidth, 's' => 'S');
  17499. break;
  17500. }
  17501. default: {
  17502. break;
  17503. }
  17504. }
  17505. }
  17506. if (isset($prop['border']) AND is_array($prop['border'])) {
  17507. $opt['border'] = $prop['border'];
  17508. }
  17509. if (!isset($opt['mk'])) {
  17510. $opt['mk'] = array();
  17511. }
  17512. if (!isset($opt['mk']['if'])) {
  17513. $opt['mk']['if'] = array();
  17514. }
  17515. $opt['mk']['if']['a'] = array(0.5, 0.5);
  17516. // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
  17517. if (isset($prop['buttonAlignX'])) {
  17518. $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
  17519. }
  17520. // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
  17521. if (isset($prop['buttonAlignY'])) {
  17522. $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
  17523. }
  17524. // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
  17525. if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
  17526. $opt['mk']['if']['fb'] = true;
  17527. }
  17528. // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
  17529. if (isset($prop['buttonScaleHow'])) {
  17530. switch ($prop['buttonScaleHow']) {
  17531. case 'scaleHow.proportional': {
  17532. $opt['mk']['if']['s'] = 'P';
  17533. break;
  17534. }
  17535. case 'scaleHow.anamorphic': {
  17536. $opt['mk']['if']['s'] = 'A';
  17537. break;
  17538. }
  17539. }
  17540. }
  17541. // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
  17542. if (isset($prop['buttonScaleWhen'])) {
  17543. switch ($prop['buttonScaleWhen']) {
  17544. case 'scaleWhen.always': {
  17545. $opt['mk']['if']['sw'] = 'A';
  17546. break;
  17547. }
  17548. case 'scaleWhen.never': {
  17549. $opt['mk']['if']['sw'] = 'N';
  17550. break;
  17551. }
  17552. case 'scaleWhen.tooBig': {
  17553. $opt['mk']['if']['sw'] = 'B';
  17554. break;
  17555. }
  17556. case 'scaleWhen.tooSmall': {
  17557. $opt['mk']['if']['sw'] = 'S';
  17558. break;
  17559. }
  17560. }
  17561. }
  17562. // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
  17563. if (isset($prop['buttonPosition'])) {
  17564. switch ($prop['buttonPosition']) {
  17565. case 0:
  17566. case 'position.textOnly': {
  17567. $opt['mk']['tp'] = 0;
  17568. break;
  17569. }
  17570. case 1:
  17571. case 'position.iconOnly': {
  17572. $opt['mk']['tp'] = 1;
  17573. break;
  17574. }
  17575. case 2:
  17576. case 'position.iconTextV': {
  17577. $opt['mk']['tp'] = 2;
  17578. break;
  17579. }
  17580. case 3:
  17581. case 'position.textIconV': {
  17582. $opt['mk']['tp'] = 3;
  17583. break;
  17584. }
  17585. case 4:
  17586. case 'position.iconTextH': {
  17587. $opt['mk']['tp'] = 4;
  17588. break;
  17589. }
  17590. case 5:
  17591. case 'position.textIconH': {
  17592. $opt['mk']['tp'] = 5;
  17593. break;
  17594. }
  17595. case 6:
  17596. case 'position.overlay': {
  17597. $opt['mk']['tp'] = 6;
  17598. break;
  17599. }
  17600. }
  17601. }
  17602. // fillColor: Specifies the background color for a field.
  17603. if (isset($prop['fillColor'])) {
  17604. if (is_array($prop['fillColor'])) {
  17605. $opt['mk']['bg'] = $prop['fillColor'];
  17606. } else {
  17607. $opt['mk']['bg'] = $this->convertHTMLColorToDec($prop['fillColor']);
  17608. }
  17609. }
  17610. // strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
  17611. if (isset($prop['strokeColor'])) {
  17612. if (is_array($prop['strokeColor'])) {
  17613. $opt['mk']['bc'] = $prop['strokeColor'];
  17614. } else {
  17615. $opt['mk']['bc'] = $this->convertHTMLColorToDec($prop['strokeColor']);
  17616. }
  17617. }
  17618. // rotation: The rotation of a widget in counterclockwise increments.
  17619. if (isset($prop['rotation'])) {
  17620. $opt['mk']['r'] = $prop['rotation'];
  17621. }
  17622. // charLimit: Limits the number of characters that a user can type into a text field.
  17623. if (isset($prop['charLimit'])) {
  17624. $opt['maxlen'] = intval($prop['charLimit']);
  17625. }
  17626. if (!isset($ff)) {
  17627. $ff = 0; // default value
  17628. }
  17629. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  17630. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  17631. $ff += 1 << 0;
  17632. }
  17633. // required: Specifies whether a field requires a value.
  17634. if (isset($prop['required']) AND ($prop['required'] == 'true')) {
  17635. $ff += 1 << 1;
  17636. }
  17637. // multiline: Controls how text is wrapped within the field.
  17638. if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
  17639. $ff += 1 << 12;
  17640. }
  17641. // password: Specifies whether the field should display asterisks when data is entered in the field.
  17642. if (isset($prop['password']) AND ($prop['password'] == 'true')) {
  17643. $ff += 1 << 13;
  17644. }
  17645. // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
  17646. if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
  17647. $ff += 1 << 14;
  17648. }
  17649. // Radio: If set, the field is a set of radio buttons.
  17650. if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
  17651. $ff += 1 << 15;
  17652. }
  17653. // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
  17654. if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
  17655. $ff += 1 << 16;
  17656. }
  17657. // Combo: If set, the field is a combo box; if clear, the field is a list box.
  17658. if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
  17659. $ff += 1 << 17;
  17660. }
  17661. // editable: Controls whether a combo box is editable.
  17662. if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
  17663. $ff += 1 << 18;
  17664. }
  17665. // Sort: If set, the field's option items shall be sorted alphabetically.
  17666. if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
  17667. $ff += 1 << 19;
  17668. }
  17669. // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
  17670. if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
  17671. $ff += 1 << 20;
  17672. }
  17673. // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
  17674. if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
  17675. $ff += 1 << 21;
  17676. }
  17677. // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
  17678. if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
  17679. $ff += 1 << 22;
  17680. }
  17681. // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
  17682. if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
  17683. $ff += 1 << 23;
  17684. }
  17685. // comb: If set to true, the field background is drawn as series of boxes (one for each character in the value of the field) and each character of the content is drawn within those boxes. The number of boxes drawn is determined from the charLimit property. It applies only to text fields. The setter will also raise if any of the following field properties are also set multiline, password, and fileSelect. A side-effect of setting this property is that the doNotScroll property is also set.
  17686. if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
  17687. $ff += 1 << 24;
  17688. }
  17689. // radiosInUnison: If false, even if a group of radio buttons have the same name and export value, they behave in a mutually exclusive fashion, like HTML radio buttons.
  17690. if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
  17691. $ff += 1 << 25;
  17692. }
  17693. // richText: If true, the field allows rich text formatting.
  17694. if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
  17695. $ff += 1 << 25;
  17696. }
  17697. // commitOnSelChange: Controls whether a field value is committed after a selection change.
  17698. if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
  17699. $ff += 1 << 26;
  17700. }
  17701. $opt['ff'] = $ff;
  17702. // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
  17703. if (isset($prop['defaultValue'])) {
  17704. $opt['dv'] = $prop['defaultValue'];
  17705. }
  17706. $f = 4; // default value for annotation flags
  17707. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  17708. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  17709. $f += 1 << 6;
  17710. }
  17711. // display: Controls whether the field is hidden or visible on screen and in print.
  17712. if (isset($prop['display'])) {
  17713. if ($prop['display'] == 'display.visible') {
  17714. //
  17715. } elseif ($prop['display'] == 'display.hidden') {
  17716. $f += 1 << 1;
  17717. } elseif ($prop['display'] == 'display.noPrint') {
  17718. $f -= 1 << 2;
  17719. } elseif ($prop['display'] == 'display.noView') {
  17720. $f += 1 << 5;
  17721. }
  17722. }
  17723. $opt['f'] = $f;
  17724. // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
  17725. if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
  17726. $opt['i'] = $prop['currentValueIndices'];
  17727. }
  17728. // value: The value of the field data that the user has entered.
  17729. if (isset($prop['value'])) {
  17730. if (is_array($prop['value'])) {
  17731. $opt['opt'] = array();
  17732. foreach ($prop['value'] AS $key => $optval) {
  17733. // exportValues: An array of strings representing the export values for the field.
  17734. if (isset($prop['exportValues'][$key])) {
  17735. $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
  17736. } else {
  17737. $opt['opt'][$key] = $prop['value'][$key];
  17738. }
  17739. }
  17740. } else {
  17741. $opt['v'] = $prop['value'];
  17742. }
  17743. }
  17744. // richValue: This property specifies the text contents and formatting of a rich text field.
  17745. if (isset($prop['richValue'])) {
  17746. $opt['rv'] = $prop['richValue'];
  17747. }
  17748. // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
  17749. if (isset($prop['submitName'])) {
  17750. $opt['tm'] = $prop['submitName'];
  17751. }
  17752. // name: Fully qualified field name.
  17753. if (isset($prop['name'])) {
  17754. $opt['t'] = $prop['name'];
  17755. }
  17756. // userName: The user name (short description string) of the field.
  17757. if (isset($prop['userName'])) {
  17758. $opt['tu'] = $prop['userName'];
  17759. }
  17760. // highlight: Defines how a button reacts when a user clicks it.
  17761. if (isset($prop['highlight'])) {
  17762. switch ($prop['highlight']) {
  17763. case 'none':
  17764. case 'highlight.n': {
  17765. $opt['h'] = 'N';
  17766. break;
  17767. }
  17768. case 'invert':
  17769. case 'highlight.i': {
  17770. $opt['h'] = 'i';
  17771. break;
  17772. }
  17773. case 'push':
  17774. case 'highlight.p': {
  17775. $opt['h'] = 'P';
  17776. break;
  17777. }
  17778. case 'outline':
  17779. case 'highlight.o': {
  17780. $opt['h'] = 'O';
  17781. break;
  17782. }
  17783. }
  17784. }
  17785. // Unsupported options:
  17786. // - calcOrderIndex: Changes the calculation order of fields in the document.
  17787. // - delay: Delays the redrawing of a field's appearance.
  17788. // - defaultStyle: This property defines the default style attributes for the form field.
  17789. // - style: Allows the user to set the glyph style of a check box or radio button.
  17790. // - textColor, textFont, textSize
  17791. return $opt;
  17792. }
  17793. /**
  17794. * Set default properties for form fields.
  17795. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  17796. * @public
  17797. * @author Nicola Asuni
  17798. * @since 4.8.000 (2009-09-06)
  17799. */
  17800. public function setFormDefaultProp($prop = array()) {
  17801. $this->default_form_prop = $prop;
  17802. }
  17803. /**
  17804. * Return the default properties for form fields.
  17805. * @return array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  17806. * @public
  17807. * @author Nicola Asuni
  17808. * @since 4.8.000 (2009-09-06)
  17809. */
  17810. public function getFormDefaultProp() {
  17811. return $this->default_form_prop;
  17812. }
  17813. /**
  17814. * Creates a text field
  17815. * @param $name (string) field name
  17816. * @param $w (float) Width of the rectangle
  17817. * @param $h (float) Height of the rectangle
  17818. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  17819. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  17820. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  17821. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  17822. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  17823. * @public
  17824. * @author Nicola Asuni
  17825. * @since 4.8.000 (2009-09-07)
  17826. */
  17827. public function TextField($name, $w, $h, $prop = array(), $opt = array(), $x = '', $y = '', $js = false) {
  17828. if ($x === '') {
  17829. $x = $this->x;
  17830. }
  17831. if ($y === '') {
  17832. $y = $this->y;
  17833. }
  17834. // check page for no-write regions and adapt page margins if necessary
  17835. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  17836. if ($js) {
  17837. $this->_addfield('text', $name, $x, $y, $w, $h, $prop);
  17838. return;
  17839. }
  17840. // get default style
  17841. $prop = array_merge($this->getFormDefaultProp(), $prop);
  17842. // get annotation data
  17843. $popt = $this->getAnnotOptFromJSProp($prop);
  17844. // set default appearance stream
  17845. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  17846. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  17847. $popt['da'] = $fontstyle;
  17848. // build appearance stream
  17849. $popt['ap'] = array();
  17850. $popt['ap']['n'] = '/Tx BMC q ' . $fontstyle . ' ';
  17851. $text = '';
  17852. if (isset($prop['value']) AND !empty($prop['value'])) {
  17853. $text = $prop['value'];
  17854. } elseif (isset($opt['v']) AND !empty($opt['v'])) {
  17855. $text = $opt['v'];
  17856. }
  17857. $tmpid = $this->startTemplate($w, $h, false);
  17858. $align = '';
  17859. if (isset($popt['q'])) {
  17860. switch ($popt['q']) {
  17861. case 0: {
  17862. $align = 'L';
  17863. break;
  17864. }
  17865. case 1: {
  17866. $align = 'C';
  17867. break;
  17868. }
  17869. case 2: {
  17870. $align = 'R';
  17871. break;
  17872. }
  17873. default: {
  17874. $align = '';
  17875. break;
  17876. }
  17877. }
  17878. }
  17879. $this->MultiCell($w, $h, $text, 0, $align, false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  17880. $this->endTemplate();
  17881. --$this->n;
  17882. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  17883. unset($this->xobjects[$tmpid]);
  17884. $popt['ap']['n'] .= 'Q EMC';
  17885. // merge options
  17886. $opt = array_merge($popt, $opt);
  17887. // remove some conflicting options
  17888. unset($opt['bs']);
  17889. // set remaining annotation data
  17890. $opt['Subtype'] = 'Widget';
  17891. $opt['ft'] = 'Tx';
  17892. $opt['t'] = $name;
  17893. // Additional annotation's parameters (check _putannotsobj() method):
  17894. //$opt['f']
  17895. //$opt['as']
  17896. //$opt['bs']
  17897. //$opt['be']
  17898. //$opt['c']
  17899. //$opt['border']
  17900. //$opt['h']
  17901. //$opt['mk'];
  17902. //$opt['mk']['r']
  17903. //$opt['mk']['bc'];
  17904. //$opt['mk']['bg'];
  17905. unset($opt['mk']['ca']);
  17906. unset($opt['mk']['rc']);
  17907. unset($opt['mk']['ac']);
  17908. unset($opt['mk']['i']);
  17909. unset($opt['mk']['ri']);
  17910. unset($opt['mk']['ix']);
  17911. unset($opt['mk']['if']);
  17912. //$opt['mk']['if']['sw'];
  17913. //$opt['mk']['if']['s'];
  17914. //$opt['mk']['if']['a'];
  17915. //$opt['mk']['if']['fb'];
  17916. unset($opt['mk']['tp']);
  17917. //$opt['tu']
  17918. //$opt['tm']
  17919. //$opt['ff']
  17920. //$opt['v']
  17921. //$opt['dv']
  17922. //$opt['a']
  17923. //$opt['aa']
  17924. //$opt['q']
  17925. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  17926. if ($this->rtl) {
  17927. $this->x -= $w;
  17928. } else {
  17929. $this->x += $w;
  17930. }
  17931. }
  17932. /**
  17933. * Creates a RadioButton field.
  17934. * @param $name (string) Field name.
  17935. * @param $w (int) Width of the radio button.
  17936. * @param $prop (array) Javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  17937. * @param $opt (array) Annotation parameters. Possible values are described on official PDF32000_2008 reference.
  17938. * @param $onvalue (string) Value to be returned if selected.
  17939. * @param $checked (boolean) Define the initial state.
  17940. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  17941. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  17942. * @param $js (boolean) If true put the field using JavaScript (requires Acrobat Writer to be rendered).
  17943. * @public
  17944. * @author Nicola Asuni
  17945. * @since 4.8.000 (2009-09-07)
  17946. */
  17947. public function RadioButton($name, $w, $prop = array(), $opt = array(), $onvalue = 'On', $checked = false, $x = '', $y = '', $js = false) {
  17948. if ($x === '') {
  17949. $x = $this->x;
  17950. }
  17951. if ($y === '') {
  17952. $y = $this->y;
  17953. }
  17954. // check page for no-write regions and adapt page margins if necessary
  17955. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  17956. if ($js) {
  17957. $this->_addfield('radiobutton', $name, $x, $y, $w, $w, $prop);
  17958. return;
  17959. }
  17960. if ($this->empty_string($onvalue)) {
  17961. $onvalue = 'On';
  17962. }
  17963. if ($checked) {
  17964. $defval = $onvalue;
  17965. } else {
  17966. $defval = 'Off';
  17967. }
  17968. // set font
  17969. $font = 'zapfdingbats';
  17970. if ($this->pdfa_mode) {
  17971. // all fonts must be embedded
  17972. $font = 'pdfa' . $font;
  17973. }
  17974. $this->AddFont($font);
  17975. $tmpfont = $this->getFontBuffer($font);
  17976. // set data for parent group
  17977. if (!isset($this->radiobutton_groups[$this->page])) {
  17978. $this->radiobutton_groups[$this->page] = array();
  17979. }
  17980. if (!isset($this->radiobutton_groups[$this->page][$name])) {
  17981. $this->radiobutton_groups[$this->page][$name] = array();
  17982. ++$this->n;
  17983. $this->radiobutton_groups[$this->page][$name]['n'] = $this->n;
  17984. $this->radio_groups[] = $this->n;
  17985. }
  17986. $kid = ($this->n + 1);
  17987. // save object ID to be added on Kids entry on parent object
  17988. $this->radiobutton_groups[$this->page][$name][] = array('kid' => $kid, 'def' => $defval);
  17989. // get default style
  17990. $prop = array_merge($this->getFormDefaultProp(), $prop);
  17991. $prop['NoToggleToOff'] = 'true';
  17992. $prop['Radio'] = 'true';
  17993. $prop['borderStyle'] = 'inset';
  17994. // get annotation data
  17995. $popt = $this->getAnnotOptFromJSProp($prop);
  17996. // set additional default options
  17997. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  17998. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  17999. $popt['da'] = $fontstyle;
  18000. // build appearance stream
  18001. $popt['ap'] = array();
  18002. $popt['ap']['n'] = array();
  18003. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][108])) / 2) * $this->k);
  18004. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  18005. $popt['ap']['n'][$onvalue] = sprintf('q %s BT /F%d %F Tf %F %F Td (' . chr(108) . ') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  18006. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td (' . chr(109) . ') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  18007. if (!isset($popt['mk'])) {
  18008. $popt['mk'] = array();
  18009. }
  18010. $popt['mk']['ca'] = '(l)';
  18011. // merge options
  18012. $opt = array_merge($popt, $opt);
  18013. // set remaining annotation data
  18014. $opt['Subtype'] = 'Widget';
  18015. $opt['ft'] = 'Btn';
  18016. if ($checked) {
  18017. $opt['v'] = array('/' . $onvalue);
  18018. $opt['as'] = $onvalue;
  18019. } else {
  18020. $opt['as'] = 'Off';
  18021. }
  18022. // store readonly flag
  18023. if (!isset($this->radiobutton_groups[$this->page][$name]['#readonly#'])) {
  18024. $this->radiobutton_groups[$this->page][$name]['#readonly#'] = false;
  18025. }
  18026. $this->radiobutton_groups[$this->page][$name]['#readonly#'] |= ($opt['f'] & 64);
  18027. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  18028. if ($this->rtl) {
  18029. $this->x -= $w;
  18030. } else {
  18031. $this->x += $w;
  18032. }
  18033. }
  18034. /**
  18035. * Creates a List-box field
  18036. * @param $name (string) field name
  18037. * @param $w (int) width
  18038. * @param $h (int) height
  18039. * @param $values (array) array containing the list of values.
  18040. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  18041. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  18042. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  18043. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  18044. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  18045. * @public
  18046. * @author Nicola Asuni
  18047. * @since 4.8.000 (2009-09-07)
  18048. */
  18049. public function ListBox($name, $w, $h, $values, $prop = array(), $opt = array(), $x = '', $y = '', $js = false) {
  18050. if ($x === '') {
  18051. $x = $this->x;
  18052. }
  18053. if ($y === '') {
  18054. $y = $this->y;
  18055. }
  18056. // check page for no-write regions and adapt page margins if necessary
  18057. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18058. if ($js) {
  18059. $this->_addfield('listbox', $name, $x, $y, $w, $h, $prop);
  18060. $s = '';
  18061. foreach ($values as $value) {
  18062. if (is_array($value)) {
  18063. $s .= ',[\'' . addslashes($value[1]) . '\',\'' . addslashes($value[0]) . '\']';
  18064. } else {
  18065. $s .= ',[\'' . addslashes($value) . '\',\'' . addslashes($value) . '\']';
  18066. }
  18067. }
  18068. $this->javascript .= 'f' . $name . '.setItems(' . substr($s, 1) . ');' . "\n";
  18069. return;
  18070. }
  18071. // get default style
  18072. $prop = array_merge($this->getFormDefaultProp(), $prop);
  18073. // get annotation data
  18074. $popt = $this->getAnnotOptFromJSProp($prop);
  18075. // set additional default values
  18076. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  18077. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  18078. $popt['da'] = $fontstyle;
  18079. // build appearance stream
  18080. $popt['ap'] = array();
  18081. $popt['ap']['n'] = '/Tx BMC q ' . $fontstyle . ' ';
  18082. $text = '';
  18083. foreach ($values as $item) {
  18084. if (is_array($item)) {
  18085. $text .= $item[1] . "\n";
  18086. } else {
  18087. $text .= $item . "\n";
  18088. }
  18089. }
  18090. $tmpid = $this->startTemplate($w, $h, false);
  18091. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  18092. $this->endTemplate();
  18093. --$this->n;
  18094. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  18095. unset($this->xobjects[$tmpid]);
  18096. $popt['ap']['n'] .= 'Q EMC';
  18097. // merge options
  18098. $opt = array_merge($popt, $opt);
  18099. // set remaining annotation data
  18100. $opt['Subtype'] = 'Widget';
  18101. $opt['ft'] = 'Ch';
  18102. $opt['t'] = $name;
  18103. $opt['opt'] = $values;
  18104. unset($opt['mk']['ca']);
  18105. unset($opt['mk']['rc']);
  18106. unset($opt['mk']['ac']);
  18107. unset($opt['mk']['i']);
  18108. unset($opt['mk']['ri']);
  18109. unset($opt['mk']['ix']);
  18110. unset($opt['mk']['if']);
  18111. unset($opt['mk']['tp']);
  18112. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  18113. if ($this->rtl) {
  18114. $this->x -= $w;
  18115. } else {
  18116. $this->x += $w;
  18117. }
  18118. }
  18119. /**
  18120. * Creates a Combo-box field
  18121. * @param $name (string) field name
  18122. * @param $w (int) width
  18123. * @param $h (int) height
  18124. * @param $values (array) array containing the list of values.
  18125. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  18126. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  18127. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  18128. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  18129. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  18130. * @public
  18131. * @author Nicola Asuni
  18132. * @since 4.8.000 (2009-09-07)
  18133. */
  18134. public function ComboBox($name, $w, $h, $values, $prop = array(), $opt = array(), $x = '', $y = '', $js = false) {
  18135. if ($x === '') {
  18136. $x = $this->x;
  18137. }
  18138. if ($y === '') {
  18139. $y = $this->y;
  18140. }
  18141. // check page for no-write regions and adapt page margins if necessary
  18142. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18143. if ($js) {
  18144. $this->_addfield('combobox', $name, $x, $y, $w, $h, $prop);
  18145. $s = '';
  18146. foreach ($values as $value) {
  18147. if (is_array($value)) {
  18148. $s .= ',[\'' . addslashes($value[1]) . '\',\'' . addslashes($value[0]) . '\']';
  18149. } else {
  18150. $s .= ',[\'' . addslashes($value) . '\',\'' . addslashes($value) . '\']';
  18151. }
  18152. }
  18153. $this->javascript .= 'f' . $name . '.setItems(' . substr($s, 1) . ');' . "\n";
  18154. return;
  18155. }
  18156. // get default style
  18157. $prop = array_merge($this->getFormDefaultProp(), $prop);
  18158. $prop['Combo'] = true;
  18159. // get annotation data
  18160. $popt = $this->getAnnotOptFromJSProp($prop);
  18161. // set additional default options
  18162. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  18163. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  18164. $popt['da'] = $fontstyle;
  18165. // build appearance stream
  18166. $popt['ap'] = array();
  18167. $popt['ap']['n'] = '/Tx BMC q ' . $fontstyle . ' ';
  18168. $text = '';
  18169. foreach ($values as $item) {
  18170. if (is_array($item)) {
  18171. $text .= $item[1] . "\n";
  18172. } else {
  18173. $text .= $item . "\n";
  18174. }
  18175. }
  18176. $tmpid = $this->startTemplate($w, $h, false);
  18177. $this->MultiCell($w, $h, $text, 0, '', false, 0, 0, 0, true, 0, false, true, 0, 'T', false);
  18178. $this->endTemplate();
  18179. --$this->n;
  18180. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  18181. unset($this->xobjects[$tmpid]);
  18182. $popt['ap']['n'] .= 'Q EMC';
  18183. // merge options
  18184. $opt = array_merge($popt, $opt);
  18185. // set remaining annotation data
  18186. $opt['Subtype'] = 'Widget';
  18187. $opt['ft'] = 'Ch';
  18188. $opt['t'] = $name;
  18189. $opt['opt'] = $values;
  18190. unset($opt['mk']['ca']);
  18191. unset($opt['mk']['rc']);
  18192. unset($opt['mk']['ac']);
  18193. unset($opt['mk']['i']);
  18194. unset($opt['mk']['ri']);
  18195. unset($opt['mk']['ix']);
  18196. unset($opt['mk']['if']);
  18197. unset($opt['mk']['tp']);
  18198. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  18199. if ($this->rtl) {
  18200. $this->x -= $w;
  18201. } else {
  18202. $this->x += $w;
  18203. }
  18204. }
  18205. /**
  18206. * Creates a CheckBox field
  18207. * @param $name (string) field name
  18208. * @param $w (int) width
  18209. * @param $checked (boolean) define the initial state.
  18210. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  18211. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  18212. * @param $onvalue (string) value to be returned if selected.
  18213. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  18214. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  18215. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  18216. * @public
  18217. * @author Nicola Asuni
  18218. * @since 4.8.000 (2009-09-07)
  18219. */
  18220. public function CheckBox($name, $w, $checked = false, $prop = array(), $opt = array(), $onvalue = 'Yes', $x = '', $y = '', $js = false) {
  18221. if ($x === '') {
  18222. $x = $this->x;
  18223. }
  18224. if ($y === '') {
  18225. $y = $this->y;
  18226. }
  18227. // check page for no-write regions and adapt page margins if necessary
  18228. list($x, $y) = $this->checkPageRegions($w, $x, $y);
  18229. if ($js) {
  18230. $this->_addfield('checkbox', $name, $x, $y, $w, $w, $prop);
  18231. return;
  18232. }
  18233. if (!isset($prop['value'])) {
  18234. $prop['value'] = array('Yes');
  18235. }
  18236. // get default style
  18237. $prop = array_merge($this->getFormDefaultProp(), $prop);
  18238. $prop['borderStyle'] = 'inset';
  18239. // get annotation data
  18240. $popt = $this->getAnnotOptFromJSProp($prop);
  18241. // set additional default options
  18242. $font = 'zapfdingbats';
  18243. if ($this->pdfa_mode) {
  18244. // all fonts must be embedded
  18245. $font = 'pdfa' . $font;
  18246. }
  18247. $this->AddFont($font);
  18248. $tmpfont = $this->getFontBuffer($font);
  18249. $this->annotation_fonts[$tmpfont['fontkey']] = $tmpfont['i'];
  18250. $fontstyle = sprintf('/F%d %F Tf %s', $tmpfont['i'], $this->FontSizePt, $this->TextColor);
  18251. $popt['da'] = $fontstyle;
  18252. // build appearance stream
  18253. $popt['ap'] = array();
  18254. $popt['ap']['n'] = array();
  18255. $fx = ((($w - $this->getAbsFontMeasure($tmpfont['cw'][110])) / 2) * $this->k);
  18256. $fy = (($w - ((($tmpfont['desc']['Ascent'] - $tmpfont['desc']['Descent']) * $this->FontSizePt / 1000) / $this->k)) * $this->k);
  18257. $popt['ap']['n']['Yes'] = sprintf('q %s BT /F%d %F Tf %F %F Td (' . chr(110) . ') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  18258. $popt['ap']['n']['Off'] = sprintf('q %s BT /F%d %F Tf %F %F Td (' . chr(111) . ') Tj ET Q', $this->TextColor, $tmpfont['i'], $this->FontSizePt, $fx, $fy);
  18259. // merge options
  18260. $opt = array_merge($popt, $opt);
  18261. // set remaining annotation data
  18262. $opt['Subtype'] = 'Widget';
  18263. $opt['ft'] = 'Btn';
  18264. $opt['t'] = $name;
  18265. if ($this->empty_string($onvalue)) {
  18266. $onvalue = 'Yes';
  18267. }
  18268. $opt['opt'] = array($onvalue);
  18269. if ($checked) {
  18270. $opt['v'] = array('/Yes');
  18271. $opt['as'] = 'Yes';
  18272. } else {
  18273. $opt['v'] = array('/Off');
  18274. $opt['as'] = 'Off';
  18275. }
  18276. $this->Annotation($x, $y, $w, $w, $name, $opt, 0);
  18277. if ($this->rtl) {
  18278. $this->x -= $w;
  18279. } else {
  18280. $this->x += $w;
  18281. }
  18282. }
  18283. /**
  18284. * Creates a button field
  18285. * @param $name (string) field name
  18286. * @param $w (int) width
  18287. * @param $h (int) height
  18288. * @param $caption (string) caption.
  18289. * @param $action (mixed) action triggered by pressing the button. Use a string to specify a javascript action. Use an array to specify a form action options as on section 12.7.5 of PDF32000_2008.
  18290. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  18291. * @param $opt (array) annotation parameters. Possible values are described on official PDF32000_2008 reference.
  18292. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  18293. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  18294. * @param $js (boolean) if true put the field using JavaScript (requires Acrobat Writer to be rendered).
  18295. * @public
  18296. * @author Nicola Asuni
  18297. * @since 4.8.000 (2009-09-07)
  18298. */
  18299. public function Button($name, $w, $h, $caption, $action, $prop = array(), $opt = array(), $x = '', $y = '', $js = false) {
  18300. if ($x === '') {
  18301. $x = $this->x;
  18302. }
  18303. if ($y === '') {
  18304. $y = $this->y;
  18305. }
  18306. // check page for no-write regions and adapt page margins if necessary
  18307. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  18308. if ($js) {
  18309. $this->_addfield('button', $name, $this->x, $this->y, $w, $h, $prop);
  18310. $this->javascript .= 'f' . $name . ".buttonSetCaption('" . addslashes($caption) . "');\n";
  18311. $this->javascript .= 'f' . $name . ".setAction('MouseUp','" . addslashes($action) . "');\n";
  18312. $this->javascript .= 'f' . $name . ".highlight='push';\n";
  18313. $this->javascript .= 'f' . $name . ".print=false;\n";
  18314. return;
  18315. }
  18316. // get default style
  18317. $prop = array_merge($this->getFormDefaultProp(), $prop);
  18318. $prop['Pushbutton'] = 'true';
  18319. $prop['highlight'] = 'push';
  18320. $prop['display'] = 'display.noPrint';
  18321. // get annotation data
  18322. $popt = $this->getAnnotOptFromJSProp($prop);
  18323. $this->annotation_fonts[$this->CurrentFont['fontkey']] = $this->CurrentFont['i'];
  18324. $fontstyle = sprintf('/F%d %F Tf %s', $this->CurrentFont['i'], $this->FontSizePt, $this->TextColor);
  18325. $popt['da'] = $fontstyle;
  18326. // build appearance stream
  18327. $popt['ap'] = array();
  18328. $popt['ap']['n'] = '/Tx BMC q ' . $fontstyle . ' ';
  18329. $tmpid = $this->startTemplate($w, $h, false);
  18330. $bw = (2 / $this->k); // border width
  18331. $border = array(
  18332. 'L' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  18333. 'R' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)),
  18334. 'T' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(231)),
  18335. 'B' => array('width' => $bw, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(51)));
  18336. $this->SetFillColor(204);
  18337. $this->Cell($w, $h, $caption, $border, 0, 'C', true, '', 1, false, 'T', 'M');
  18338. $this->endTemplate();
  18339. --$this->n;
  18340. $popt['ap']['n'] .= $this->xobjects[$tmpid]['outdata'];
  18341. unset($this->xobjects[$tmpid]);
  18342. $popt['ap']['n'] .= 'Q EMC';
  18343. // set additional default options
  18344. if (!isset($popt['mk'])) {
  18345. $popt['mk'] = array();
  18346. }
  18347. $ann_obj_id = ($this->n + 1);
  18348. if (!empty($action) AND !is_array($action)) {
  18349. $ann_obj_id = ($this->n + 2);
  18350. }
  18351. $popt['mk']['ca'] = $this->_textstring($caption, $ann_obj_id);
  18352. $popt['mk']['rc'] = $this->_textstring($caption, $ann_obj_id);
  18353. $popt['mk']['ac'] = $this->_textstring($caption, $ann_obj_id);
  18354. // merge options
  18355. $opt = array_merge($popt, $opt);
  18356. // set remaining annotation data
  18357. $opt['Subtype'] = 'Widget';
  18358. $opt['ft'] = 'Btn';
  18359. $opt['t'] = $caption;
  18360. $opt['v'] = $name;
  18361. if (!empty($action)) {
  18362. if (is_array($action)) {
  18363. // form action options as on section 12.7.5 of PDF32000_2008.
  18364. $opt['aa'] = '/D <<';
  18365. $bmode = array('SubmitForm', 'ResetForm', 'ImportData');
  18366. foreach ($action AS $key => $val) {
  18367. if (($key == 'S') AND in_array($val, $bmode)) {
  18368. $opt['aa'] .= ' /S /' . $val;
  18369. } elseif (($key == 'F') AND (!empty($val))) {
  18370. $opt['aa'] .= ' /F ' . $this->_datastring($val, $ann_obj_id);
  18371. } elseif (($key == 'Fields') AND is_array($val) AND !empty($val)) {
  18372. $opt['aa'] .= ' /Fields [';
  18373. foreach ($val AS $field) {
  18374. $opt['aa'] .= ' ' . $this->_textstring($field, $ann_obj_id);
  18375. }
  18376. $opt['aa'] .= ']';
  18377. } elseif (($key == 'Flags')) {
  18378. $ff = 0;
  18379. if (is_array($val)) {
  18380. foreach ($val AS $flag) {
  18381. switch ($flag) {
  18382. case 'Include/Exclude': {
  18383. $ff += 1 << 0;
  18384. break;
  18385. }
  18386. case 'IncludeNoValueFields': {
  18387. $ff += 1 << 1;
  18388. break;
  18389. }
  18390. case 'ExportFormat': {
  18391. $ff += 1 << 2;
  18392. break;
  18393. }
  18394. case 'GetMethod': {
  18395. $ff += 1 << 3;
  18396. break;
  18397. }
  18398. case 'SubmitCoordinates': {
  18399. $ff += 1 << 4;
  18400. break;
  18401. }
  18402. case 'XFDF': {
  18403. $ff += 1 << 5;
  18404. break;
  18405. }
  18406. case 'IncludeAppendSaves': {
  18407. $ff += 1 << 6;
  18408. break;
  18409. }
  18410. case 'IncludeAnnotations': {
  18411. $ff += 1 << 7;
  18412. break;
  18413. }
  18414. case 'SubmitPDF': {
  18415. $ff += 1 << 8;
  18416. break;
  18417. }
  18418. case 'CanonicalFormat': {
  18419. $ff += 1 << 9;
  18420. break;
  18421. }
  18422. case 'ExclNonUserAnnots': {
  18423. $ff += 1 << 10;
  18424. break;
  18425. }
  18426. case 'ExclFKey': {
  18427. $ff += 1 << 11;
  18428. break;
  18429. }
  18430. case 'EmbedForm': {
  18431. $ff += 1 << 13;
  18432. break;
  18433. }
  18434. }
  18435. }
  18436. } else {
  18437. $ff = intval($val);
  18438. }
  18439. $opt['aa'] .= ' /Flags ' . $ff;
  18440. }
  18441. }
  18442. $opt['aa'] .= ' >>';
  18443. } else {
  18444. // Javascript action or raw action command
  18445. $js_obj_id = $this->addJavascriptObject($action);
  18446. $opt['aa'] = '/D ' . $js_obj_id . ' 0 R';
  18447. }
  18448. }
  18449. $this->Annotation($x, $y, $w, $h, $name, $opt, 0);
  18450. if ($this->rtl) {
  18451. $this->x -= $w;
  18452. } else {
  18453. $this->x += $w;
  18454. }
  18455. }
  18456. // --- END FORMS FIELDS ------------------------------------------------
  18457. /**
  18458. * Add certification signature (DocMDP or UR3)
  18459. * You can set only one signature type
  18460. * @protected
  18461. * @author Nicola Asuni
  18462. * @since 4.6.008 (2009-05-07)
  18463. */
  18464. protected function _putsignature() {
  18465. if ((!$this->sign) OR (!isset($this->signature_data['cert_type']))) {
  18466. return;
  18467. }
  18468. $sigobjid = ($this->sig_obj_id + 1);
  18469. $out = $this->_getobj($sigobjid) . "\n";
  18470. $out .= '<< /Type /Sig';
  18471. $out .= ' /Filter /Adobe.PPKLite';
  18472. $out .= ' /SubFilter /adbe.pkcs7.detached';
  18473. $out .= ' ' . $this->byterange_string;
  18474. $out .= ' /Contents<' . str_repeat('0', $this->signature_max_length) . '>';
  18475. $out .= ' /Reference ['; // array of signature reference dictionaries
  18476. $out .= ' << /Type /SigRef';
  18477. if ($this->signature_data['cert_type'] > 0) {
  18478. $out .= ' /TransformMethod /DocMDP';
  18479. $out .= ' /TransformParams <<';
  18480. $out .= ' /Type /TransformParams';
  18481. $out .= ' /P ' . $this->signature_data['cert_type'];
  18482. $out .= ' /V /1.2';
  18483. } else {
  18484. $out .= ' /TransformMethod /UR3';
  18485. $out .= ' /TransformParams <<';
  18486. $out .= ' /Type /TransformParams';
  18487. $out .= ' /V /2.2';
  18488. if (!$this->empty_string($this->ur['document'])) {
  18489. $out .= ' /Document[' . $this->ur['document'] . ']';
  18490. }
  18491. if (!$this->empty_string($this->ur['form'])) {
  18492. $out .= ' /Form[' . $this->ur['form'] . ']';
  18493. }
  18494. if (!$this->empty_string($this->ur['signature'])) {
  18495. $out .= ' /Signature[' . $this->ur['signature'] . ']';
  18496. }
  18497. if (!$this->empty_string($this->ur['annots'])) {
  18498. $out .= ' /Annots[' . $this->ur['annots'] . ']';
  18499. }
  18500. if (!$this->empty_string($this->ur['ef'])) {
  18501. $out .= ' /EF[' . $this->ur['ef'] . ']';
  18502. }
  18503. if (!$this->empty_string($this->ur['formex'])) {
  18504. $out .= ' /FormEX[' . $this->ur['formex'] . ']';
  18505. }
  18506. }
  18507. $out .= ' >>'; // close TransformParams
  18508. // optional digest data (values must be calculated and replaced later)
  18509. //$out .= ' /Data ********** 0 R';
  18510. //$out .= ' /DigestMethod/MD5';
  18511. //$out .= ' /DigestLocation[********** 34]';
  18512. //$out .= ' /DigestValue<********************************>';
  18513. $out .= ' >>';
  18514. $out .= ' ]'; // end of reference
  18515. if (isset($this->signature_data['info']['Name']) AND !$this->empty_string($this->signature_data['info']['Name'])) {
  18516. $out .= ' /Name ' . $this->_textstring($this->signature_data['info']['Name'], $sigobjid);
  18517. }
  18518. if (isset($this->signature_data['info']['Location']) AND !$this->empty_string($this->signature_data['info']['Location'])) {
  18519. $out .= ' /Location ' . $this->_textstring($this->signature_data['info']['Location'], $sigobjid);
  18520. }
  18521. if (isset($this->signature_data['info']['Reason']) AND !$this->empty_string($this->signature_data['info']['Reason'])) {
  18522. $out .= ' /Reason ' . $this->_textstring($this->signature_data['info']['Reason'], $sigobjid);
  18523. }
  18524. if (isset($this->signature_data['info']['ContactInfo']) AND !$this->empty_string($this->signature_data['info']['ContactInfo'])) {
  18525. $out .= ' /ContactInfo ' . $this->_textstring($this->signature_data['info']['ContactInfo'], $sigobjid);
  18526. }
  18527. $out .= ' /M ' . $this->_datestring($sigobjid, $this->doc_modification_timestamp);
  18528. $out .= ' >>';
  18529. $out .= "\n" . 'endobj';
  18530. $this->_out($out);
  18531. }
  18532. /**
  18533. * Set User's Rights for PDF Reader
  18534. * WARNING: This is experimental and currently do not work.
  18535. * Check the PDF Reference 8.7.1 Transform Methods,
  18536. * Table 8.105 Entries in the UR transform parameters dictionary
  18537. * @param $enable (boolean) if true enable user's rights on PDF reader
  18538. * @param $document (string) 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.
  18539. * @param $annots (string) 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.
  18540. * @param $form (string) Names specifying additional form-field-related usage rights for the document. Valid names are: /Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate
  18541. * @param $signature (string) 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.
  18542. * @param $ef (string) Names specifying additional usage rights for named embedded files in the document. Valid names are /Create/Delete/Modify/Import, which permit the user to perform the named operation on named embedded files
  18543. Names specifying additional embedded-files-related usage rights for the document.
  18544. * @param $formex (string) Names specifying additional form-field-related usage rights. The only valid name is BarcodePlaintext, which permits text form field data to be encoded as a plaintext two-dimensional barcode.
  18545. * @public
  18546. * @author Nicola Asuni
  18547. * @since 2.9.000 (2008-03-26)
  18548. */
  18549. public function setUserRights(
  18550. $enable = true, $document = '/FullSave', $annots = '/Create/Delete/Modify/Copy/Import/Export', $form = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate', $signature = '/Modify', $ef = '/Create/Delete/Modify/Import', $formex = '') {
  18551. $this->ur['enabled'] = $enable;
  18552. $this->ur['document'] = $document;
  18553. $this->ur['annots'] = $annots;
  18554. $this->ur['form'] = $form;
  18555. $this->ur['signature'] = $signature;
  18556. $this->ur['ef'] = $ef;
  18557. $this->ur['formex'] = $formex;
  18558. if (!$this->sign) {
  18559. $this->setSignature('', '', '', '', 0, array());
  18560. }
  18561. }
  18562. /**
  18563. * Enable document signature (requires the OpenSSL Library).
  18564. * The digital signature improve document authenticity and integrity and allows o enable extra features on Acrobat Reader.
  18565. * To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  18566. * To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  18567. * To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  18568. * @param $signing_cert (mixed) signing certificate (string or filename prefixed with 'file://')
  18569. * @param $private_key (mixed) private key (string or filename prefixed with 'file://')
  18570. * @param $private_key_password (string) password
  18571. * @param $extracerts (string) 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.
  18572. * @param $cert_type (int) 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.
  18573. * @param $info (array) array of option information: Name, Location, Reason, ContactInfo.
  18574. * @public
  18575. * @author Nicola Asuni
  18576. * @since 4.6.005 (2009-04-24)
  18577. */
  18578. public function setSignature($signing_cert = '', $private_key = '', $private_key_password = '', $extracerts = '', $cert_type = 2, $info = array()) {
  18579. // to create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  18580. // to export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  18581. // to convert pfx certificate to pem: openssl
  18582. // OpenSSL> pkcs12 -in <cert.pfx> -out <cert.crt> -nodes
  18583. $this->sign = true;
  18584. ++$this->n;
  18585. $this->sig_obj_id = $this->n; // signature widget
  18586. ++$this->n; // signature object ($this->sig_obj_id + 1)
  18587. $this->signature_data = array();
  18588. if (strlen($signing_cert) == 0) {
  18589. $signing_cert = 'file://' . dirname(__FILE__) . '/tcpdf.crt';
  18590. $private_key_password = 'tcpdfdemo';
  18591. }
  18592. if (strlen($private_key) == 0) {
  18593. $private_key = $signing_cert;
  18594. }
  18595. $this->signature_data['signcert'] = $signing_cert;
  18596. $this->signature_data['privkey'] = $private_key;
  18597. $this->signature_data['password'] = $private_key_password;
  18598. $this->signature_data['extracerts'] = $extracerts;
  18599. $this->signature_data['cert_type'] = $cert_type;
  18600. $this->signature_data['info'] = $info;
  18601. }
  18602. /**
  18603. * Set the digital signature appearance (a cliccable rectangle area to get signature properties)
  18604. * @param $x (float) Abscissa of the upper-left corner.
  18605. * @param $y (float) Ordinate of the upper-left corner.
  18606. * @param $w (float) Width of the signature area.
  18607. * @param $h (float) Height of the signature area.
  18608. * @param $page (int) option page number (if < 0 the current page is used).
  18609. * @public
  18610. * @author Nicola Asuni
  18611. * @since 5.3.011 (2010-06-17)
  18612. */
  18613. public function setSignatureAppearance($x = 0, $y = 0, $w = 0, $h = 0, $page = -1) {
  18614. $this->signature_appearance = $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
  18615. }
  18616. /**
  18617. * Add an empty digital signature appearance (a cliccable rectangle area to get signature properties)
  18618. * @param $x (float) Abscissa of the upper-left corner.
  18619. * @param $y (float) Ordinate of the upper-left corner.
  18620. * @param $w (float) Width of the signature area.
  18621. * @param $h (float) Height of the signature area.
  18622. * @param $page (int) option page number (if < 0 the current page is used).
  18623. * @public
  18624. * @author Nicola Asuni
  18625. * @since 5.9.101 (2011-07-06)
  18626. */
  18627. public function addEmptySignatureAppearance($x = 0, $y = 0, $w = 0, $h = 0, $page = -1) {
  18628. ++$this->n;
  18629. $this->empty_signature_appearance[] = array('objid' => $this->n) + $this->getSignatureAppearanceArray($x, $y, $w, $h, $page);
  18630. }
  18631. /**
  18632. * Get the array that defines the signature appearance (page and rectangle coordinates).
  18633. * @param $x (float) Abscissa of the upper-left corner.
  18634. * @param $y (float) Ordinate of the upper-left corner.
  18635. * @param $w (float) Width of the signature area.
  18636. * @param $h (float) Height of the signature area.
  18637. * @param $page (int) option page number (if < 0 the current page is used).
  18638. * @return (array) Array defining page and rectangle coordinates of signature appearance.
  18639. * @protected
  18640. * @author Nicola Asuni
  18641. * @since 5.9.101 (2011-07-06)
  18642. */
  18643. protected function getSignatureAppearanceArray($x = 0, $y = 0, $w = 0, $h = 0, $page = -1) {
  18644. $sigapp = array();
  18645. if (($page < 1) OR ($page > $this->numpages)) {
  18646. $sigapp['page'] = $this->page;
  18647. } else {
  18648. $sigapp['page'] = intval($page);
  18649. }
  18650. $a = $x * $this->k;
  18651. $b = $this->pagedim[($sigapp['page'])]['h'] - (($y + $h) * $this->k);
  18652. $c = $w * $this->k;
  18653. $d = $h * $this->k;
  18654. $sigapp['rect'] = sprintf('%F %F %F %F', $a, $b, ($a + $c), ($b + $d));
  18655. return $sigapp;
  18656. }
  18657. /**
  18658. * Create a new page group.
  18659. * NOTE: call this function before calling AddPage()
  18660. * @param $page (int) starting group page (leave empty for next page).
  18661. * @public
  18662. * @since 3.0.000 (2008-03-27)
  18663. */
  18664. public function startPageGroup($page = '') {
  18665. if (empty($page)) {
  18666. $page = $this->page + 1;
  18667. }
  18668. $this->newpagegroup[$page] = sizeof($this->newpagegroup) + 1;
  18669. }
  18670. /**
  18671. * This method is DEPRECATED and doesn't have any effect.
  18672. * Please remove any reference to this method.
  18673. * @param $s (string) Empty parameter.
  18674. * @deprecated deprecated since version 5.9.089 (2011-06-13)
  18675. * @public
  18676. */
  18677. public function AliasNbPages($s = '') {
  18678. }
  18679. /**
  18680. * This method is DEPRECATED and doesn't have any effect.
  18681. * Please remove any reference to this method.
  18682. * @param $s (string) Empty parameter.
  18683. * @deprecated deprecated since version 5.9.089 (2011-06-13)
  18684. * @public
  18685. */
  18686. public function AliasNumPage($s = '') {
  18687. }
  18688. /**
  18689. * Set the starting page number.
  18690. * @param $num (int) Starting page number.
  18691. * @since 5.9.093 (2011-06-16)
  18692. * @public
  18693. */
  18694. public function setStartingPageNumber($num = 1) {
  18695. $this->starting_page_number = max(0, intval($num));
  18696. }
  18697. /**
  18698. * Returns the string alias used right align page numbers.
  18699. * If the current font is unicode type, the returned string wil contain an additional open curly brace.
  18700. * @return string
  18701. * @since 5.9.099 (2011-06-27)
  18702. * @public
  18703. */
  18704. public function getAliasRightShift() {
  18705. // calculate aproximatively the ratio between widths of aliases and replacements.
  18706. $ref = '{' . $this->alias_right_shift . '}{' . $this->alias_tot_pages . '}{' . $this->alias_num_page . '}';
  18707. $rep = str_repeat(' ', $this->GetNumChars($ref));
  18708. $wdiff = max(1, ($this->GetStringWidth($ref) / $this->GetStringWidth($rep)));
  18709. $sdiff = sprintf('%F', $wdiff);
  18710. $alias = $this->alias_right_shift . $sdiff . '}';
  18711. if ($this->isUnicodeFont()) {
  18712. $alias = '{' . $alias;
  18713. }
  18714. return $alias;
  18715. }
  18716. /**
  18717. * Returns the string alias used for the total number of pages.
  18718. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  18719. * This alias will be replaced by the total number of pages in the document.
  18720. * @return string
  18721. * @since 4.0.018 (2008-08-08)
  18722. * @public
  18723. */
  18724. public function getAliasNbPages() {
  18725. if ($this->isUnicodeFont()) {
  18726. return '{' . $this->alias_tot_pages . '}';
  18727. }
  18728. return $this->alias_tot_pages;
  18729. }
  18730. /**
  18731. * Returns the string alias used for the page number.
  18732. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  18733. * This alias will be replaced by the page number.
  18734. * @return string
  18735. * @since 4.5.000 (2009-01-02)
  18736. * @public
  18737. */
  18738. public function getAliasNumPage() {
  18739. if ($this->isUnicodeFont()) {
  18740. return '{' . $this->alias_num_page . '}';
  18741. }
  18742. return $this->alias_num_page;
  18743. }
  18744. /**
  18745. * Return the alias for the total number of pages in the current page group.
  18746. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  18747. * This alias will be replaced by the total number of pages in this group.
  18748. * @return alias of the current page group
  18749. * @public
  18750. * @since 3.0.000 (2008-03-27)
  18751. */
  18752. public function getPageGroupAlias() {
  18753. if ($this->isUnicodeFont()) {
  18754. return '{' . $this->alias_group_tot_pages . '}';
  18755. }
  18756. return $this->alias_group_tot_pages;
  18757. }
  18758. /**
  18759. * Return the alias for the page number on the current page group.
  18760. * If the current font is unicode type, the returned string is surrounded by additional curly braces.
  18761. * This alias will be replaced by the page number (relative to the belonging group).
  18762. * @return alias of the current page group
  18763. * @public
  18764. * @since 4.5.000 (2009-01-02)
  18765. */
  18766. public function getPageNumGroupAlias() {
  18767. if ($this->isUnicodeFont()) {
  18768. return '{' . $this->alias_group_num_page . '}';
  18769. }
  18770. return $this->alias_group_num_page;
  18771. }
  18772. /**
  18773. * Return the current page in the group.
  18774. * @return current page in the group
  18775. * @public
  18776. * @since 3.0.000 (2008-03-27)
  18777. */
  18778. public function getGroupPageNo() {
  18779. return $this->pagegroups[$this->currpagegroup];
  18780. }
  18781. /**
  18782. * Returns the current group page number formatted as a string.
  18783. * @public
  18784. * @since 4.3.003 (2008-11-18)
  18785. * @see PaneNo(), formatPageNumber()
  18786. */
  18787. public function getGroupPageNoFormatted() {
  18788. return $this->formatPageNumber($this->getGroupPageNo());
  18789. }
  18790. /**
  18791. * Format the page numbers.
  18792. * This method can be overriden for custom formats.
  18793. * @param $num (int) page number
  18794. * @protected
  18795. * @since 4.2.005 (2008-11-06)
  18796. */
  18797. protected function formatPageNumber($num) {
  18798. return number_format((float) $num, 0, '', '.');
  18799. }
  18800. /**
  18801. * Format the page numbers on the Table Of Content.
  18802. * This method can be overriden for custom formats.
  18803. * @param $num (int) page number
  18804. * @protected
  18805. * @since 4.5.001 (2009-01-04)
  18806. * @see addTOC(), addHTMLTOC()
  18807. */
  18808. protected function formatTOCPageNumber($num) {
  18809. return number_format((float) $num, 0, '', '.');
  18810. }
  18811. /**
  18812. * Returns the current page number formatted as a string.
  18813. * @public
  18814. * @since 4.2.005 (2008-11-06)
  18815. * @see PaneNo(), formatPageNumber()
  18816. */
  18817. public function PageNoFormatted() {
  18818. return $this->formatPageNumber($this->PageNo());
  18819. }
  18820. /**
  18821. * Put pdf layers.
  18822. * @protected
  18823. * @since 3.0.000 (2008-03-27)
  18824. */
  18825. protected function _putocg() {
  18826. if (empty($this->pdflayers)) {
  18827. return;
  18828. }
  18829. foreach ($this->pdflayers as $key => $layer) {
  18830. $this->pdflayers[$key]['objid'] = $this->_newobj();
  18831. $out = '<< /Type /OCG';
  18832. $out .= ' /Name ' . $this->_textstring($layer['name'], $this->pdflayers[$key]['objid']);
  18833. $out .= ' /Usage <<';
  18834. $out .= ' /Print <</PrintState /' . ($layer['print'] ? 'ON' : 'OFF') . '>>';
  18835. $out .= ' /View <</ViewState /' . ($layer['view'] ? 'ON' : 'OFF') . '>>';
  18836. $out .= ' >> >>';
  18837. $out .= "\n" . 'endobj';
  18838. $this->_out($out);
  18839. }
  18840. }
  18841. /**
  18842. * Start a new pdf layer.
  18843. * @param $name (string) Layer name (only a-z letters and numbers). Leave empty for automatic name.
  18844. * @param $print (boolean) Set to true to print this layer.
  18845. * @param $view (boolean) Set to true to view this layer.
  18846. * @public
  18847. * @since 5.9.102 (2011-07-13)
  18848. */
  18849. public function startLayer($name = '', $print = true, $view = true) {
  18850. if ($this->state != 2) {
  18851. return;
  18852. }
  18853. $layer = sprintf('LYR%03d', (count($this->pdflayers) + 1));
  18854. if (empty($name)) {
  18855. $name = $layer;
  18856. } else {
  18857. $name = preg_replace('/[^a-zA-Z0-9_\-]/', '', $name);
  18858. }
  18859. $this->pdflayers[] = array('layer' => $layer, 'name' => $name, 'print' => $print, 'view' => $view);
  18860. $this->openMarkedContent = true;
  18861. $this->_out('/OC /' . $layer . ' BDC');
  18862. }
  18863. /**
  18864. * End the current PDF layer.
  18865. * @public
  18866. * @since 5.9.102 (2011-07-13)
  18867. */
  18868. public function endLayer() {
  18869. if ($this->state != 2) {
  18870. return;
  18871. }
  18872. if ($this->openMarkedContent) {
  18873. // close existing open marked-content layer
  18874. $this->_out('EMC');
  18875. $this->openMarkedContent = false;
  18876. }
  18877. }
  18878. /**
  18879. * Set the visibility of the successive elements.
  18880. * This can be useful, for instance, to put a background
  18881. * image or color that will show on screen but won't print.
  18882. * @param $v (string) visibility mode. Legal values are: all, print, screen or view.
  18883. * @public
  18884. * @since 3.0.000 (2008-03-27)
  18885. */
  18886. public function setVisibility($v) {
  18887. if ($this->state != 2) {
  18888. return;
  18889. }
  18890. $this->endLayer();
  18891. switch ($v) {
  18892. case 'print': {
  18893. $this->startLayer('Print', true, false);
  18894. break;
  18895. }
  18896. case 'view':
  18897. case 'screen': {
  18898. $this->startLayer('View', false, true);
  18899. break;
  18900. }
  18901. case 'all': {
  18902. $this->_out('');
  18903. break;
  18904. }
  18905. default: {
  18906. $this->Error('Incorrect visibility: ' . $v);
  18907. break;
  18908. }
  18909. }
  18910. }
  18911. /**
  18912. * Add transparency parameters to the current extgstate
  18913. * @param $parms (array) parameters
  18914. * @return the number of extgstates
  18915. * @protected
  18916. * @since 3.0.000 (2008-03-27)
  18917. */
  18918. protected function addExtGState($parms) {
  18919. if ($this->pdfa_mode) {
  18920. // transparencies are not allowed in PDF/A mode
  18921. return;
  18922. }
  18923. // check if this ExtGState already exist
  18924. foreach ($this->extgstates as $i => $ext) {
  18925. if ($ext['parms'] == $parms) {
  18926. if ($this->inxobj) {
  18927. // we are inside an XObject template
  18928. $this->xobjects[$this->xobjid]['extgstates'][$i] = $ext;
  18929. }
  18930. // return reference to existing ExtGState
  18931. return $i;
  18932. }
  18933. }
  18934. $n = (count($this->extgstates) + 1);
  18935. $this->extgstates[$n] = array('parms' => $parms);
  18936. if ($this->inxobj) {
  18937. // we are inside an XObject template
  18938. $this->xobjects[$this->xobjid]['extgstates'][$n] = $this->extgstates[$n];
  18939. }
  18940. return $n;
  18941. }
  18942. /**
  18943. * Add an extgstate
  18944. * @param $gs (array) extgstate
  18945. * @protected
  18946. * @since 3.0.000 (2008-03-27)
  18947. */
  18948. protected function setExtGState($gs) {
  18949. if ($this->pdfa_mode OR ($this->state != 2)) {
  18950. // transparency is not allowed in PDF/A mode
  18951. return;
  18952. }
  18953. $this->_out(sprintf('/GS%d gs', $gs));
  18954. }
  18955. /**
  18956. * Put extgstates for object transparency
  18957. * @protected
  18958. * @since 3.0.000 (2008-03-27)
  18959. */
  18960. protected function _putextgstates() {
  18961. foreach ($this->extgstates as $i => $ext) {
  18962. $this->extgstates[$i]['n'] = $this->_newobj();
  18963. $out = '<< /Type /ExtGState';
  18964. foreach ($ext['parms'] as $k => $v) {
  18965. if (is_float($v)) {
  18966. $v = sprintf('%F', $v);
  18967. } elseif ($v === true) {
  18968. $v = 'true';
  18969. } elseif ($v === false) {
  18970. $v = 'false';
  18971. }
  18972. $out .= ' /' . $k . ' ' . $v;
  18973. }
  18974. $out .= ' >>';
  18975. $out .= "\n" . 'endobj';
  18976. $this->_out($out);
  18977. }
  18978. }
  18979. /**
  18980. * Set overprint mode for stroking (OP) and non-stroking (op) painting operations.
  18981. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  18982. * @param $stroking (boolean) If true apply overprint for stroking operations.
  18983. * @param $nonstroking (boolean) If true apply overprint for painting operations other than stroking.
  18984. * @param $mode (integer) Overprint mode: (0 = each source colour component value replaces the value previously painted for the corresponding device colorant; 1 = a tint value of 0.0 for a source colour component shall leave the corresponding component of the previously painted colour unchanged).
  18985. * @public
  18986. * @since 5.9.152 (2012-03-23)
  18987. */
  18988. public function setOverprint($stroking = true, $nonstroking = '', $mode = 0) {
  18989. if ($this->state != 2) {
  18990. return;
  18991. }
  18992. $stroking = $stroking ? true : false;
  18993. if ($this->empty_string($nonstroking)) {
  18994. // default value if not set
  18995. $nonstroking = $stroking;
  18996. } else {
  18997. $nonstroking = $nonstroking ? true : false;
  18998. }
  18999. if (($mode != 0) AND ($mode != 1)) {
  19000. $mode = 0;
  19001. }
  19002. $this->overprint = array('OP' => $stroking, 'op' => $nonstroking, 'OPM' => $mode);
  19003. $gs = $this->addExtGState($this->overprint);
  19004. $this->setExtGState($gs);
  19005. }
  19006. /**
  19007. * Get the overprint mode array (OP, op, OPM).
  19008. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  19009. * @return array.
  19010. * @public
  19011. * @since 5.9.152 (2012-03-23)
  19012. */
  19013. public function getOverprint() {
  19014. return $this->overprint;
  19015. }
  19016. /**
  19017. * Set alpha for stroking (CA) and non-stroking (ca) operations.
  19018. * @param $stroking (float) Alpha value for stroking operations: real value from 0 (transparent) to 1 (opaque).
  19019. * @param $bm (string) blend mode, one of the following: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
  19020. * @param $nonstroking (float) Alpha value for non-stroking operations: real value from 0 (transparent) to 1 (opaque).
  19021. * @param $ais (boolean)
  19022. * @public
  19023. * @since 3.0.000 (2008-03-27)
  19024. */
  19025. public function setAlpha($stroking = 1, $bm = 'Normal', $nonstroking = '', $ais = false) {
  19026. if ($this->pdfa_mode) {
  19027. // transparency is not allowed in PDF/A mode
  19028. return;
  19029. }
  19030. $stroking = floatval($stroking);
  19031. if ($this->empty_string($nonstroking)) {
  19032. // default value if not set
  19033. $nonstroking = $stroking;
  19034. } else {
  19035. $nonstroking = floatval($nonstroking);
  19036. }
  19037. if ($bm[0] == '/') {
  19038. // remove trailing slash
  19039. $bm = substr($bm, 1);
  19040. }
  19041. if (!in_array($bm, array('Normal', 'Multiply', 'Screen', 'Overlay', 'Darken', 'Lighten', 'ColorDodge', 'ColorBurn', 'HardLight', 'SoftLight', 'Difference', 'Exclusion', 'Hue', 'Saturation', 'Color', 'Luminosity'))) {
  19042. $bm = 'Normal';
  19043. }
  19044. $ais = $ais ? true : false;
  19045. $this->alpha = array('CA' => $stroking, 'ca' => $nonstroking, 'BM' => '/' . $bm, 'AIS' => $ais);
  19046. $gs = $this->addExtGState($this->alpha);
  19047. $this->setExtGState($gs);
  19048. }
  19049. /**
  19050. * Get the alpha mode array (CA, ca, BM, AIS).
  19051. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  19052. * @return array.
  19053. * @public
  19054. * @since 5.9.152 (2012-03-23)
  19055. */
  19056. public function getAlpha() {
  19057. return $this->alpha;
  19058. }
  19059. /**
  19060. * Set the default JPEG compression quality (1-100)
  19061. * @param $quality (int) JPEG quality, integer between 1 and 100
  19062. * @public
  19063. * @since 3.0.000 (2008-03-27)
  19064. */
  19065. public function setJPEGQuality($quality) {
  19066. if (($quality < 1) OR ($quality > 100)) {
  19067. $quality = 75;
  19068. }
  19069. $this->jpeg_quality = intval($quality);
  19070. }
  19071. /**
  19072. * Set the default number of columns in a row for HTML tables.
  19073. * @param $cols (int) number of columns
  19074. * @public
  19075. * @since 3.0.014 (2008-06-04)
  19076. */
  19077. public function setDefaultTableColumns($cols = 4) {
  19078. $this->default_table_columns = intval($cols);
  19079. }
  19080. /**
  19081. * Set the height of the cell (line height) respect the font height.
  19082. * @param $h (int) cell proportion respect font height (typical value = 1.25).
  19083. * @public
  19084. * @since 3.0.014 (2008-06-04)
  19085. */
  19086. public function setCellHeightRatio($h) {
  19087. $this->cell_height_ratio = $h;
  19088. }
  19089. /**
  19090. * return the height of cell repect font height.
  19091. * @public
  19092. * @since 4.0.012 (2008-07-24)
  19093. */
  19094. public function getCellHeightRatio() {
  19095. return $this->cell_height_ratio;
  19096. }
  19097. /**
  19098. * Set the PDF version (check PDF reference for valid values).
  19099. * @param $version (string) PDF document version.
  19100. * @public
  19101. * @since 3.1.000 (2008-06-09)
  19102. */
  19103. public function setPDFVersion($version = '1.7') {
  19104. if ($this->pdfa_mode) {
  19105. // PDF/A mode
  19106. $this->PDFVersion = '1.4';
  19107. } else {
  19108. $this->PDFVersion = $version;
  19109. }
  19110. }
  19111. /**
  19112. * Set the viewer preferences dictionary controlling the way the document is to be presented on the screen or in print.
  19113. * (see Section 8.1 of PDF reference, "Viewer Preferences").
  19114. * <ul><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><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><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><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><li>CenterWindow boolean (Optional) A flag specifying whether to position the document's window in the center of the screen. Default value: false.</li><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><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><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><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><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><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><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><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><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><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><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></ul>
  19115. * @param $preferences (array) array of options.
  19116. * @author Nicola Asuni
  19117. * @public
  19118. * @since 3.1.000 (2008-06-09)
  19119. */
  19120. public function setViewerPreferences($preferences) {
  19121. $this->viewer_preferences = $preferences;
  19122. }
  19123. /**
  19124. * Paints color transition registration bars
  19125. * @param $x (float) abscissa of the top left corner of the rectangle.
  19126. * @param $y (float) ordinate of the top left corner of the rectangle.
  19127. * @param $w (float) width of the rectangle.
  19128. * @param $h (float) height of the rectangle.
  19129. * @param $transition (boolean) if true prints tcolor transitions to white.
  19130. * @param $vertical (boolean) if true prints bar vertically.
  19131. * @param $colors (string) colors to print, one letter per color separated by comma (for example 'A,W,R,G,B,C,M,Y,K'): A=black, W=white, R=red, G=green, B=blue, C=cyan, M=magenta, Y=yellow, K=black.
  19132. * @author Nicola Asuni
  19133. * @since 4.9.000 (2010-03-26)
  19134. * @public
  19135. */
  19136. public function colorRegistrationBar($x, $y, $w, $h, $transition = true, $vertical = false, $colors = 'A,R,G,B,C,M,Y,K') {
  19137. $bars = explode(',', $colors);
  19138. $numbars = count($bars); // number of bars to print
  19139. // set bar measures
  19140. if ($vertical) {
  19141. $coords = array(0, 0, 0, 1);
  19142. $wb = $w / $numbars; // bar width
  19143. $hb = $h; // bar height
  19144. $xd = $wb; // delta x
  19145. $yd = 0; // delta y
  19146. } else {
  19147. $coords = array(1, 0, 0, 0);
  19148. $wb = $w; // bar width
  19149. $hb = $h / $numbars; // bar height
  19150. $xd = 0; // delta x
  19151. $yd = $hb; // delta y
  19152. }
  19153. $xb = $x;
  19154. $yb = $y;
  19155. foreach ($bars as $col) {
  19156. switch ($col) {
  19157. // set transition colors
  19158. case 'A': { // BLACK
  19159. $col_a = array(255);
  19160. $col_b = array(0);
  19161. break;
  19162. }
  19163. case 'W': { // WHITE
  19164. $col_a = array(0);
  19165. $col_b = array(255);
  19166. break;
  19167. }
  19168. case 'R': { // R
  19169. $col_a = array(255, 255, 255);
  19170. $col_b = array(255, 0, 0);
  19171. break;
  19172. }
  19173. case 'G': { // G
  19174. $col_a = array(255, 255, 255);
  19175. $col_b = array(0, 255, 0);
  19176. break;
  19177. }
  19178. case 'B': { // B
  19179. $col_a = array(255, 255, 255);
  19180. $col_b = array(0, 0, 255);
  19181. break;
  19182. }
  19183. case 'C': { // C
  19184. $col_a = array(0, 0, 0, 0);
  19185. $col_b = array(100, 0, 0, 0);
  19186. break;
  19187. }
  19188. case 'M': { // M
  19189. $col_a = array(0, 0, 0, 0);
  19190. $col_b = array(0, 100, 0, 0);
  19191. break;
  19192. }
  19193. case 'Y': { // Y
  19194. $col_a = array(0, 0, 0, 0);
  19195. $col_b = array(0, 0, 100, 0);
  19196. break;
  19197. }
  19198. case 'K': { // K
  19199. $col_a = array(0, 0, 0, 0);
  19200. $col_b = array(0, 0, 0, 100);
  19201. break;
  19202. }
  19203. default: { // GRAY
  19204. $col_a = array(255);
  19205. $col_b = array(0);
  19206. break;
  19207. }
  19208. }
  19209. if ($transition) {
  19210. // color gradient
  19211. $this->LinearGradient($xb, $yb, $wb, $hb, $col_a, $col_b, $coords);
  19212. } else {
  19213. // color rectangle
  19214. $this->SetFillColorArray($col_b);
  19215. $this->Rect($xb, $yb, $wb, $hb, 'F', array());
  19216. }
  19217. $xb += $xd;
  19218. $yb += $yd;
  19219. }
  19220. }
  19221. /**
  19222. * Paints crop marks.
  19223. * @param $x (float) abscissa of the crop mark center.
  19224. * @param $y (float) ordinate of the crop mark center.
  19225. * @param $w (float) width of the crop mark.
  19226. * @param $h (float) height of the crop mark.
  19227. * @param $type (string) type of crop mark, one symbol per type separated by comma: T = TOP, F = BOTTOM, L = LEFT, R = RIGHT, TL = A = TOP-LEFT, TR = B = TOP-RIGHT, BL = C = BOTTOM-LEFT, BR = D = BOTTOM-RIGHT.
  19228. * @param $color (array) crop mark color (default black).
  19229. * @author Nicola Asuni
  19230. * @since 4.9.000 (2010-03-26)
  19231. * @public
  19232. */
  19233. public function cropMark($x, $y, $w, $h, $type = 'T,R,B,L', $color = array(0, 0, 0)) {
  19234. $this->SetLineStyle(array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $color));
  19235. $type = strtoupper($type);
  19236. $type = preg_replace('/[^A-Z\-\,]*/', '', $type);
  19237. // split type in single components
  19238. $type = str_replace('-', ',', $type);
  19239. $type = str_replace('TL', 'T,L', $type);
  19240. $type = str_replace('TR', 'T,R', $type);
  19241. $type = str_replace('BL', 'F,L', $type);
  19242. $type = str_replace('BR', 'F,R', $type);
  19243. $type = str_replace('A', 'T,L', $type);
  19244. $type = str_replace('B', 'T,R', $type);
  19245. $type = str_replace('T,RO', 'BO', $type);
  19246. $type = str_replace('C', 'F,L', $type);
  19247. $type = str_replace('D', 'F,R', $type);
  19248. $crops = explode(',', strtoupper($type));
  19249. // remove duplicates
  19250. $crops = array_unique($crops);
  19251. $dw = ($w / 4); // horizontal space to leave before the intersection point
  19252. $dh = ($h / 4); // vertical space to leave before the intersection point
  19253. foreach ($crops as $crop) {
  19254. switch ($crop) {
  19255. case 'T':
  19256. case 'TOP': {
  19257. $x1 = $x;
  19258. $y1 = ($y - $h);
  19259. $x2 = $x;
  19260. $y2 = ($y - $dh);
  19261. break;
  19262. }
  19263. case 'F':
  19264. case 'BOTTOM': {
  19265. $x1 = $x;
  19266. $y1 = ($y + $dh);
  19267. $x2 = $x;
  19268. $y2 = ($y + $h);
  19269. break;
  19270. }
  19271. case 'L':
  19272. case 'LEFT': {
  19273. $x1 = ($x - $w);
  19274. $y1 = $y;
  19275. $x2 = ($x - $dw);
  19276. $y2 = $y;
  19277. break;
  19278. }
  19279. case 'R':
  19280. case 'RIGHT': {
  19281. $x1 = ($x + $dw);
  19282. $y1 = $y;
  19283. $x2 = ($x + $w);
  19284. $y2 = $y;
  19285. break;
  19286. }
  19287. }
  19288. $this->Line($x1, $y1, $x2, $y2);
  19289. }
  19290. }
  19291. /**
  19292. * Paints a registration mark
  19293. * @param $x (float) abscissa of the registration mark center.
  19294. * @param $y (float) ordinate of the registration mark center.
  19295. * @param $r (float) radius of the crop mark.
  19296. * @param $double (boolean) if true print two concentric crop marks.
  19297. * @param $cola (array) crop mark color (default black).
  19298. * @param $colb (array) second crop mark color.
  19299. * @author Nicola Asuni
  19300. * @since 4.9.000 (2010-03-26)
  19301. * @public
  19302. */
  19303. public function registrationMark($x, $y, $r, $double = false, $cola = array(0, 0, 0), $colb = array(255, 255, 255)) {
  19304. $line_style = array('width' => (0.5 / $this->k), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $cola);
  19305. $this->SetFillColorArray($cola);
  19306. $this->PieSector($x, $y, $r, 90, 180, 'F');
  19307. $this->PieSector($x, $y, $r, 270, 360, 'F');
  19308. $this->Circle($x, $y, $r, 0, 360, 'C', $line_style, array(), 8);
  19309. if ($double) {
  19310. $r2 = $r * 0.5;
  19311. $this->SetFillColorArray($colb);
  19312. $this->PieSector($x, $y, $r2, 90, 180, 'F');
  19313. $this->PieSector($x, $y, $r2, 270, 360, 'F');
  19314. $this->SetFillColorArray($cola);
  19315. $this->PieSector($x, $y, $r2, 0, 90, 'F');
  19316. $this->PieSector($x, $y, $r2, 180, 270, 'F');
  19317. $this->Circle($x, $y, $r2, 0, 360, 'C', $line_style, array(), 8);
  19318. }
  19319. }
  19320. /**
  19321. * Paints a linear colour gradient.
  19322. * @param $x (float) abscissa of the top left corner of the rectangle.
  19323. * @param $y (float) ordinate of the top left corner of the rectangle.
  19324. * @param $w (float) width of the rectangle.
  19325. * @param $h (float) height of the rectangle.
  19326. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  19327. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  19328. * @param $coords (array) 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).
  19329. * @author Andreas W�rmser, Nicola Asuni
  19330. * @since 3.1.000 (2008-06-09)
  19331. * @public
  19332. */
  19333. public function LinearGradient($x, $y, $w, $h, $col1 = array(), $col2 = array(), $coords = array(0, 0, 1, 0)) {
  19334. $this->Clip($x, $y, $w, $h);
  19335. $this->Gradient(2, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  19336. }
  19337. /**
  19338. * Paints a radial colour gradient.
  19339. * @param $x (float) abscissa of the top left corner of the rectangle.
  19340. * @param $y (float) ordinate of the top left corner of the rectangle.
  19341. * @param $w (float) width of the rectangle.
  19342. * @param $h (float) height of the rectangle.
  19343. * @param $col1 (array) first color (Grayscale, RGB or CMYK components).
  19344. * @param $col2 (array) second color (Grayscale, RGB or CMYK components).
  19345. * @param $coords (array) 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.
  19346. * @author Andreas W�rmser, Nicola Asuni
  19347. * @since 3.1.000 (2008-06-09)
  19348. * @public
  19349. */
  19350. public function RadialGradient($x, $y, $w, $h, $col1 = array(), $col2 = array(), $coords = array(0.5, 0.5, 0.5, 0.5, 1)) {
  19351. $this->Clip($x, $y, $w, $h);
  19352. $this->Gradient(3, $coords, array(array('color' => $col1, 'offset' => 0, 'exponent' => 1), array('color' => $col2, 'offset' => 1, 'exponent' => 1)), array(), false);
  19353. }
  19354. /**
  19355. * Paints a coons patch mesh.
  19356. * @param $x (float) abscissa of the top left corner of the rectangle.
  19357. * @param $y (float) ordinate of the top left corner of the rectangle.
  19358. * @param $w (float) width of the rectangle.
  19359. * @param $h (float) height of the rectangle.
  19360. * @param $col1 (array) first color (lower left corner) (RGB components).
  19361. * @param $col2 (array) second color (lower right corner) (RGB components).
  19362. * @param $col3 (array) third color (upper right corner) (RGB components).
  19363. * @param $col4 (array) fourth color (upper left corner) (RGB components).
  19364. * @param $coords (array) <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>
  19365. * @param $coords_min (array) minimum value used by the coordinates. If a coordinate's value is smaller than this it will be cut to coords_min. default: 0
  19366. * @param $coords_max (array) maximum value used by the coordinates. If a coordinate's value is greater than this it will be cut to coords_max. default: 1
  19367. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  19368. * @author Andreas W�rmser, Nicola Asuni
  19369. * @since 3.1.000 (2008-06-09)
  19370. * @public
  19371. */
  19372. 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, $antialias = false) {
  19373. if ($this->pdfa_mode OR ($this->state != 2)) {
  19374. return;
  19375. }
  19376. $this->Clip($x, $y, $w, $h);
  19377. $n = count($this->gradients) + 1;
  19378. $this->gradients[$n] = array();
  19379. $this->gradients[$n]['type'] = 6; //coons patch mesh
  19380. $this->gradients[$n]['coords'] = array();
  19381. $this->gradients[$n]['antialias'] = $antialias;
  19382. $this->gradients[$n]['colors'] = array();
  19383. $this->gradients[$n]['transparency'] = false;
  19384. //check the coords array if it is the simple array or the multi patch array
  19385. if (!isset($coords[0]['f'])) {
  19386. //simple array -> convert to multi patch array
  19387. if (!isset($col1[1])) {
  19388. $col1[1] = $col1[2] = $col1[0];
  19389. }
  19390. if (!isset($col2[1])) {
  19391. $col2[1] = $col2[2] = $col2[0];
  19392. }
  19393. if (!isset($col3[1])) {
  19394. $col3[1] = $col3[2] = $col3[0];
  19395. }
  19396. if (!isset($col4[1])) {
  19397. $col4[1] = $col4[2] = $col4[0];
  19398. }
  19399. $patch_array[0]['f'] = 0;
  19400. $patch_array[0]['points'] = $coords;
  19401. $patch_array[0]['colors'][0]['r'] = $col1[0];
  19402. $patch_array[0]['colors'][0]['g'] = $col1[1];
  19403. $patch_array[0]['colors'][0]['b'] = $col1[2];
  19404. $patch_array[0]['colors'][1]['r'] = $col2[0];
  19405. $patch_array[0]['colors'][1]['g'] = $col2[1];
  19406. $patch_array[0]['colors'][1]['b'] = $col2[2];
  19407. $patch_array[0]['colors'][2]['r'] = $col3[0];
  19408. $patch_array[0]['colors'][2]['g'] = $col3[1];
  19409. $patch_array[0]['colors'][2]['b'] = $col3[2];
  19410. $patch_array[0]['colors'][3]['r'] = $col4[0];
  19411. $patch_array[0]['colors'][3]['g'] = $col4[1];
  19412. $patch_array[0]['colors'][3]['b'] = $col4[2];
  19413. } else {
  19414. //multi patch array
  19415. $patch_array = $coords;
  19416. }
  19417. $bpcd = 65535; //16 bits per coordinate
  19418. //build the data stream
  19419. $this->gradients[$n]['stream'] = '';
  19420. $count_patch = count($patch_array);
  19421. for ($i = 0; $i < $count_patch; ++$i) {
  19422. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['f']); //start with the edge flag as 8 bit
  19423. $count_points = count($patch_array[$i]['points']);
  19424. for ($j = 0; $j < $count_points; ++$j) {
  19425. //each point as 16 bit
  19426. $patch_array[$i]['points'][$j] = (($patch_array[$i]['points'][$j] - $coords_min) / ($coords_max - $coords_min)) * $bpcd;
  19427. if ($patch_array[$i]['points'][$j] < 0) {
  19428. $patch_array[$i]['points'][$j] = 0;
  19429. }
  19430. if ($patch_array[$i]['points'][$j] > $bpcd) {
  19431. $patch_array[$i]['points'][$j] = $bpcd;
  19432. }
  19433. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] / 256));
  19434. $this->gradients[$n]['stream'] .= chr(floor($patch_array[$i]['points'][$j] % 256));
  19435. }
  19436. $count_cols = count($patch_array[$i]['colors']);
  19437. for ($j = 0; $j < $count_cols; ++$j) {
  19438. //each color component as 8 bit
  19439. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['r']);
  19440. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['g']);
  19441. $this->gradients[$n]['stream'] .= chr($patch_array[$i]['colors'][$j]['b']);
  19442. }
  19443. }
  19444. //paint the gradient
  19445. $this->_out('/Sh' . $n . ' sh');
  19446. //restore previous Graphic State
  19447. $this->_out('Q');
  19448. if ($this->inxobj) {
  19449. // we are inside an XObject template
  19450. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  19451. }
  19452. }
  19453. /**
  19454. * Set a rectangular clipping area.
  19455. * @param $x (float) abscissa of the top left corner of the rectangle (or top right corner for RTL mode).
  19456. * @param $y (float) ordinate of the top left corner of the rectangle.
  19457. * @param $w (float) width of the rectangle.
  19458. * @param $h (float) height of the rectangle.
  19459. * @author Andreas W�rmser, Nicola Asuni
  19460. * @since 3.1.000 (2008-06-09)
  19461. * @protected
  19462. */
  19463. protected function Clip($x, $y, $w, $h) {
  19464. if ($this->state != 2) {
  19465. return;
  19466. }
  19467. if ($this->rtl) {
  19468. $x = $this->w - $x - $w;
  19469. }
  19470. //save current Graphic State
  19471. $s = 'q';
  19472. //set clipping area
  19473. $s .= sprintf(' %F %F %F %F re W n', $x * $this->k, ($this->h - $y) * $this->k, $w * $this->k, -$h * $this->k);
  19474. //set up transformation matrix for gradient
  19475. $s .= sprintf(' %F 0 0 %F %F %F cm', $w * $this->k, $h * $this->k, $x * $this->k, ($this->h - ($y + $h)) * $this->k);
  19476. $this->_out($s);
  19477. }
  19478. /**
  19479. * Output gradient.
  19480. * @param $type (int) type of gradient (1 Function-based shading; 2 Axial shading; 3 Radial shading; 4 Free-form Gouraud-shaded triangle mesh; 5 Lattice-form Gouraud-shaded triangle mesh; 6 Coons patch mesh; 7 Tensor-product patch mesh). (Not all types are currently supported)
  19481. * @param $coords (array) array of coordinates.
  19482. * @param $stops (array) array gradient color components: color = array of GRAY, RGB or CMYK color components; offset = (0 to 1) represents a location along the gradient vector; exponent = exponent of the exponential interpolation function (default = 1).
  19483. * @param $background (array) An array of colour components appropriate to the colour space, specifying a single background colour value.
  19484. * @param $antialias (boolean) A flag indicating whether to filter the shading function to prevent aliasing artifacts.
  19485. * @author Nicola Asuni
  19486. * @since 3.1.000 (2008-06-09)
  19487. * @public
  19488. */
  19489. public function Gradient($type, $coords, $stops, $background = array(), $antialias = false) {
  19490. if ($this->pdfa_mode OR ($this->state != 2)) {
  19491. return;
  19492. }
  19493. $n = count($this->gradients) + 1;
  19494. $this->gradients[$n] = array();
  19495. $this->gradients[$n]['type'] = $type;
  19496. $this->gradients[$n]['coords'] = $coords;
  19497. $this->gradients[$n]['antialias'] = $antialias;
  19498. $this->gradients[$n]['colors'] = array();
  19499. $this->gradients[$n]['transparency'] = false;
  19500. // color space
  19501. $numcolspace = count($stops[0]['color']);
  19502. $bcolor = array_values($background);
  19503. switch ($numcolspace) {
  19504. case 4: { // CMYK
  19505. $this->gradients[$n]['colspace'] = 'DeviceCMYK';
  19506. if (!empty($background)) {
  19507. $this->gradients[$n]['background'] = sprintf('%F %F %F %F', $bcolor[0] / 100, $bcolor[1] / 100, $bcolor[2] / 100, $bcolor[3] / 100);
  19508. }
  19509. break;
  19510. }
  19511. case 3: { // RGB
  19512. $this->gradients[$n]['colspace'] = 'DeviceRGB';
  19513. if (!empty($background)) {
  19514. $this->gradients[$n]['background'] = sprintf('%F %F %F', $bcolor[0] / 255, $bcolor[1] / 255, $bcolor[2] / 255);
  19515. }
  19516. break;
  19517. }
  19518. case 1: { // Gray scale
  19519. $this->gradients[$n]['colspace'] = 'DeviceGray';
  19520. if (!empty($background)) {
  19521. $this->gradients[$n]['background'] = sprintf('%F', $bcolor[0] / 255);
  19522. }
  19523. break;
  19524. }
  19525. }
  19526. $num_stops = count($stops);
  19527. $last_stop_id = $num_stops - 1;
  19528. foreach ($stops as $key => $stop) {
  19529. $this->gradients[$n]['colors'][$key] = array();
  19530. // offset represents a location along the gradient vector
  19531. if (isset($stop['offset'])) {
  19532. $this->gradients[$n]['colors'][$key]['offset'] = $stop['offset'];
  19533. } else {
  19534. if ($key == 0) {
  19535. $this->gradients[$n]['colors'][$key]['offset'] = 0;
  19536. } elseif ($key == $last_stop_id) {
  19537. $this->gradients[$n]['colors'][$key]['offset'] = 1;
  19538. } else {
  19539. $offsetstep = (1 - $this->gradients[$n]['colors'][($key - 1)]['offset']) / ($num_stops - $key);
  19540. $this->gradients[$n]['colors'][$key]['offset'] = $this->gradients[$n]['colors'][($key - 1)]['offset'] + $offsetstep;
  19541. }
  19542. }
  19543. if (isset($stop['opacity'])) {
  19544. $this->gradients[$n]['colors'][$key]['opacity'] = $stop['opacity'];
  19545. if ((!$this->pdfa_mode) AND ($stop['opacity'] < 1)) {
  19546. $this->gradients[$n]['transparency'] = true;
  19547. }
  19548. } else {
  19549. $this->gradients[$n]['colors'][$key]['opacity'] = 1;
  19550. }
  19551. // exponent for the exponential interpolation function
  19552. if (isset($stop['exponent'])) {
  19553. $this->gradients[$n]['colors'][$key]['exponent'] = $stop['exponent'];
  19554. } else {
  19555. $this->gradients[$n]['colors'][$key]['exponent'] = 1;
  19556. }
  19557. // set colors
  19558. $color = array_values($stop['color']);
  19559. switch ($numcolspace) {
  19560. case 4: { // CMYK
  19561. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F %F', $color[0] / 100, $color[1] / 100, $color[2] / 100, $color[3] / 100);
  19562. break;
  19563. }
  19564. case 3: { // RGB
  19565. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F %F %F', $color[0] / 255, $color[1] / 255, $color[2] / 255);
  19566. break;
  19567. }
  19568. case 1: { // Gray scale
  19569. $this->gradients[$n]['colors'][$key]['color'] = sprintf('%F', $color[0] / 255);
  19570. break;
  19571. }
  19572. }
  19573. }
  19574. if ($this->gradients[$n]['transparency']) {
  19575. // paint luminosity gradient
  19576. $this->_out('/TGS' . $n . ' gs');
  19577. }
  19578. //paint the gradient
  19579. $this->_out('/Sh' . $n . ' sh');
  19580. //restore previous Graphic State
  19581. $this->_out('Q');
  19582. if ($this->inxobj) {
  19583. // we are inside an XObject template
  19584. $this->xobjects[$this->xobjid]['gradients'][$n] = $this->gradients[$n];
  19585. }
  19586. }
  19587. /**
  19588. * Output gradient shaders.
  19589. * @author Nicola Asuni
  19590. * @since 3.1.000 (2008-06-09)
  19591. * @protected
  19592. */
  19593. function _putshaders() {
  19594. if ($this->pdfa_mode) {
  19595. return;
  19596. }
  19597. $idt = count($this->gradients); //index for transparency gradients
  19598. foreach ($this->gradients as $id => $grad) {
  19599. if (($grad['type'] == 2) OR ($grad['type'] == 3)) {
  19600. $fc = $this->_newobj();
  19601. $out = '<<';
  19602. $out .= ' /FunctionType 3';
  19603. $out .= ' /Domain [0 1]';
  19604. $functions = '';
  19605. $bounds = '';
  19606. $encode = '';
  19607. $i = 1;
  19608. $num_cols = count($grad['colors']);
  19609. $lastcols = $num_cols - 1;
  19610. for ($i = 1; $i < $num_cols; ++$i) {
  19611. $functions .= ($fc + $i) . ' 0 R ';
  19612. if ($i < $lastcols) {
  19613. $bounds .= sprintf('%F ', $grad['colors'][$i]['offset']);
  19614. }
  19615. $encode .= '0 1 ';
  19616. }
  19617. $out .= ' /Functions [' . trim($functions) . ']';
  19618. $out .= ' /Bounds [' . trim($bounds) . ']';
  19619. $out .= ' /Encode [' . trim($encode) . ']';
  19620. $out .= ' >>';
  19621. $out .= "\n" . 'endobj';
  19622. $this->_out($out);
  19623. for ($i = 1; $i < $num_cols; ++$i) {
  19624. $this->_newobj();
  19625. $out = '<<';
  19626. $out .= ' /FunctionType 2';
  19627. $out .= ' /Domain [0 1]';
  19628. $out .= ' /C0 [' . $grad['colors'][($i - 1)]['color'] . ']';
  19629. $out .= ' /C1 [' . $grad['colors'][$i]['color'] . ']';
  19630. $out .= ' /N ' . $grad['colors'][$i]['exponent'];
  19631. $out .= ' >>';
  19632. $out .= "\n" . 'endobj';
  19633. $this->_out($out);
  19634. }
  19635. // set transparency fuctions
  19636. if ($grad['transparency']) {
  19637. $ft = $this->_newobj();
  19638. $out = '<<';
  19639. $out .= ' /FunctionType 3';
  19640. $out .= ' /Domain [0 1]';
  19641. $functions = '';
  19642. $i = 1;
  19643. $num_cols = count($grad['colors']);
  19644. for ($i = 1; $i < $num_cols; ++$i) {
  19645. $functions .= ($ft + $i) . ' 0 R ';
  19646. }
  19647. $out .= ' /Functions [' . trim($functions) . ']';
  19648. $out .= ' /Bounds [' . trim($bounds) . ']';
  19649. $out .= ' /Encode [' . trim($encode) . ']';
  19650. $out .= ' >>';
  19651. $out .= "\n" . 'endobj';
  19652. $this->_out($out);
  19653. for ($i = 1; $i < $num_cols; ++$i) {
  19654. $this->_newobj();
  19655. $out = '<<';
  19656. $out .= ' /FunctionType 2';
  19657. $out .= ' /Domain [0 1]';
  19658. $out .= ' /C0 [' . $grad['colors'][($i - 1)]['opacity'] . ']';
  19659. $out .= ' /C1 [' . $grad['colors'][$i]['opacity'] . ']';
  19660. $out .= ' /N ' . $grad['colors'][$i]['exponent'];
  19661. $out .= ' >>';
  19662. $out .= "\n" . 'endobj';
  19663. $this->_out($out);
  19664. }
  19665. }
  19666. }
  19667. // set shading object
  19668. $this->_newobj();
  19669. $out = '<< /ShadingType ' . $grad['type'];
  19670. if (isset($grad['colspace'])) {
  19671. $out .= ' /ColorSpace /' . $grad['colspace'];
  19672. } else {
  19673. $out .= ' /ColorSpace /DeviceRGB';
  19674. }
  19675. if (isset($grad['background']) AND !empty($grad['background'])) {
  19676. $out .= ' /Background [' . $grad['background'] . ']';
  19677. }
  19678. if (isset($grad['antialias']) AND ($grad['antialias'] === true)) {
  19679. $out .= ' /AntiAlias true';
  19680. }
  19681. if ($grad['type'] == 2) {
  19682. $out .= ' ' . sprintf('/Coords [%F %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3]);
  19683. $out .= ' /Domain [0 1]';
  19684. $out .= ' /Function ' . $fc . ' 0 R';
  19685. $out .= ' /Extend [true true]';
  19686. $out .= ' >>';
  19687. } elseif ($grad['type'] == 3) {
  19688. //x0, y0, r0, x1, y1, r1
  19689. //at this this time radius of inner circle is 0
  19690. $out .= ' ' . sprintf('/Coords [%F %F 0 %F %F %F]', $grad['coords'][0], $grad['coords'][1], $grad['coords'][2], $grad['coords'][3], $grad['coords'][4]);
  19691. $out .= ' /Domain [0 1]';
  19692. $out .= ' /Function ' . $fc . ' 0 R';
  19693. $out .= ' /Extend [true true]';
  19694. $out .= ' >>';
  19695. } elseif ($grad['type'] == 6) {
  19696. $out .= ' /BitsPerCoordinate 16';
  19697. $out .= ' /BitsPerComponent 8';
  19698. $out .= ' /Decode[0 1 0 1 0 1 0 1 0 1]';
  19699. $out .= ' /BitsPerFlag 8';
  19700. $stream = $this->_getrawstream($grad['stream']);
  19701. $out .= ' /Length ' . strlen($stream);
  19702. $out .= ' >>';
  19703. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  19704. }
  19705. $out .= "\n" . 'endobj';
  19706. $this->_out($out);
  19707. if ($grad['transparency']) {
  19708. $shading_transparency = preg_replace('/\/ColorSpace \/[^\s]+/si', '/ColorSpace /DeviceGray', $out);
  19709. $shading_transparency = preg_replace('/\/Function [0-9]+ /si', '/Function ' . $ft . ' ', $shading_transparency);
  19710. }
  19711. $this->gradients[$id]['id'] = $this->n;
  19712. // set pattern object
  19713. $this->_newobj();
  19714. $out = '<< /Type /Pattern /PatternType 2';
  19715. $out .= ' /Shading ' . $this->gradients[$id]['id'] . ' 0 R';
  19716. $out .= ' >>';
  19717. $out .= "\n" . 'endobj';
  19718. $this->_out($out);
  19719. $this->gradients[$id]['pattern'] = $this->n;
  19720. // set shading and pattern for transparency mask
  19721. if ($grad['transparency']) {
  19722. // luminosity pattern
  19723. $idgs = $id + $idt;
  19724. $this->_newobj();
  19725. $this->_out($shading_transparency);
  19726. $this->gradients[$idgs]['id'] = $this->n;
  19727. $this->_newobj();
  19728. $out = '<< /Type /Pattern /PatternType 2';
  19729. $out .= ' /Shading ' . $this->gradients[$idgs]['id'] . ' 0 R';
  19730. $out .= ' >>';
  19731. $out .= "\n" . 'endobj';
  19732. $this->_out($out);
  19733. $this->gradients[$idgs]['pattern'] = $this->n;
  19734. // luminosity XObject
  19735. $oid = $this->_newobj();
  19736. $this->xobjects['LX' . $oid] = array('n' => $oid);
  19737. $filter = '';
  19738. $stream = 'q /a0 gs /Pattern cs /p' . $idgs . ' scn 0 0 ' . $this->wPt . ' ' . $this->hPt . ' re f Q';
  19739. if ($this->compress) {
  19740. $filter = ' /Filter /FlateDecode';
  19741. $stream = gzcompress($stream);
  19742. }
  19743. $stream = $this->_getrawstream($stream);
  19744. $out = '<< /Type /XObject /Subtype /Form /FormType 1' . $filter;
  19745. $out .= ' /Length ' . strlen($stream);
  19746. $rect = sprintf('%F %F', $this->wPt, $this->hPt);
  19747. $out .= ' /BBox [0 0 ' . $rect . ']';
  19748. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceGray >>';
  19749. $out .= ' /Resources <<';
  19750. $out .= ' /ExtGState << /a0 << /ca 1 /CA 1 >> >>';
  19751. $out .= ' /Pattern << /p' . $idgs . ' ' . $this->gradients[$idgs]['pattern'] . ' 0 R >>';
  19752. $out .= ' >>';
  19753. $out .= ' >> ';
  19754. $out .= ' stream' . "\n" . $stream . "\n" . 'endstream';
  19755. $out .= "\n" . 'endobj';
  19756. $this->_out($out);
  19757. // SMask
  19758. $this->_newobj();
  19759. $out = '<< /Type /Mask /S /Luminosity /G ' . ($this->n - 1) . ' 0 R >>' . "\n" . 'endobj';
  19760. $this->_out($out);
  19761. // ExtGState
  19762. $this->_newobj();
  19763. $out = '<< /Type /ExtGState /SMask ' . ($this->n - 1) . ' 0 R /AIS false >>' . "\n" . 'endobj';
  19764. $this->_out($out);
  19765. $this->extgstates[] = array('n' => $this->n, 'name' => 'TGS' . $id);
  19766. }
  19767. }
  19768. }
  19769. /**
  19770. * Draw the sector of a circle.
  19771. * It can be used for instance to render pie charts.
  19772. * @param $xc (float) abscissa of the center.
  19773. * @param $yc (float) ordinate of the center.
  19774. * @param $r (float) radius.
  19775. * @param $a (float) start angle (in degrees).
  19776. * @param $b (float) end angle (in degrees).
  19777. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  19778. * @param $cw: (float) indicates whether to go clockwise (default: true).
  19779. * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock). Default: 90.
  19780. * @author Maxime Delorme, Nicola Asuni
  19781. * @since 3.1.000 (2008-06-09)
  19782. * @public
  19783. */
  19784. public function PieSector($xc, $yc, $r, $a, $b, $style = 'FD', $cw = true, $o = 90) {
  19785. $this->PieSectorXY($xc, $yc, $r, $r, $a, $b, $style, $cw, $o);
  19786. }
  19787. /**
  19788. * Draw the sector of an ellipse.
  19789. * It can be used for instance to render pie charts.
  19790. * @param $xc (float) abscissa of the center.
  19791. * @param $yc (float) ordinate of the center.
  19792. * @param $rx (float) the x-axis radius.
  19793. * @param $ry (float) the y-axis radius.
  19794. * @param $a (float) start angle (in degrees).
  19795. * @param $b (float) end angle (in degrees).
  19796. * @param $style (string) Style of rendering. See the getPathPaintOperator() function for more information.
  19797. * @param $cw: (float) indicates whether to go clockwise.
  19798. * @param $o: (float) origin of angles (0 for 3 o'clock, 90 for noon, 180 for 9 o'clock, 270 for 6 o'clock).
  19799. * @param $nc (integer) Number of curves used to draw a 90 degrees portion of arc.
  19800. * @author Maxime Delorme, Nicola Asuni
  19801. * @since 3.1.000 (2008-06-09)
  19802. * @public
  19803. */
  19804. public function PieSectorXY($xc, $yc, $rx, $ry, $a, $b, $style = 'FD', $cw = false, $o = 0, $nc = 2) {
  19805. if ($this->state != 2) {
  19806. return;
  19807. }
  19808. if ($this->rtl) {
  19809. $xc = ($this->w - $xc);
  19810. }
  19811. $op = $this->getPathPaintOperator($style);
  19812. if ($op == 'f') {
  19813. $line_style = array();
  19814. }
  19815. if ($cw) {
  19816. $d = $b;
  19817. $b = (360 - $a + $o);
  19818. $a = (360 - $d + $o);
  19819. } else {
  19820. $b += $o;
  19821. $a += $o;
  19822. }
  19823. $this->_outellipticalarc($xc, $yc, $rx, $ry, 0, $a, $b, true, $nc);
  19824. $this->_out($op);
  19825. }
  19826. /**
  19827. * Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
  19828. * NOTE: EPS is not yet fully implemented, use the setRasterizeVectorImages() method to enable/disable rasterization of vector images using ImageMagick library.
  19829. * Only vector drawing is supported, not text or bitmap.
  19830. * 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).
  19831. * @param $file (string) Name of the file containing the image or a '@' character followed by the EPS/AI data string.
  19832. * @param $x (float) Abscissa of the upper-left corner.
  19833. * @param $y (float) Ordinate of the upper-left corner.
  19834. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  19835. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  19836. * @param $link (mixed) URL or identifier returned by AddLink().
  19837. * @param $useBoundingBox (boolean) specifies whether to position the bounding box (true) or the complete canvas (false) at location (x,y). Default value is true.
  19838. * @param $align (string) 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>
  19839. * @param $palign (string) 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>
  19840. * @param $border (mixed) Indicates if borders must be drawn around the cell. The value can be 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> or an array of line styles for each border group - for example: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
  19841. * @param $fitonpage (boolean) if true the image is resized to not exceed page dimensions.
  19842. * @param $fixoutvals (boolean) if true remove values outside the bounding box.
  19843. * @author Valentin Schmidt, Nicola Asuni
  19844. * @since 3.1.000 (2008-06-09)
  19845. * @public
  19846. */
  19847. public function ImageEps($file, $x = '', $y = '', $w = 0, $h = 0, $link = '', $useBoundingBox = true, $align = '', $palign = '', $border = 0, $fitonpage = false, $fixoutvals = false) {
  19848. if ($this->state != 2) {
  19849. return;
  19850. }
  19851. if ($this->rasterize_vector_images AND ($w > 0) AND ($h > 0)) {
  19852. // convert EPS to raster image using GD or ImageMagick libraries
  19853. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  19854. }
  19855. if ($x === '') {
  19856. $x = $this->x;
  19857. }
  19858. if ($y === '') {
  19859. $y = $this->y;
  19860. }
  19861. // check page for no-write regions and adapt page margins if necessary
  19862. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  19863. $k = $this->k;
  19864. if ($file{0} === '@') { // image from string
  19865. $data = substr($file, 1);
  19866. } else { // EPS/AI file
  19867. $data = file_get_contents($file);
  19868. }
  19869. if ($data === false) {
  19870. $this->Error('EPS file not found: ' . $file);
  19871. }
  19872. $regs = array();
  19873. // EPS/AI compatibility check (only checks files created by Adobe Illustrator!)
  19874. preg_match("/%%Creator:([^\r\n]+)/", $data, $regs); # find Creator
  19875. if (count($regs) > 1) {
  19876. $version_str = trim($regs[1]); # e.g. "Adobe Illustrator(R) 8.0"
  19877. if (strpos($version_str, 'Adobe Illustrator') !== false) {
  19878. $versexp = explode(' ', $version_str);
  19879. $version = (float) array_pop($versexp);
  19880. if ($version >= 9) {
  19881. $this->Error('This version of Adobe Illustrator file is not supported: ' . $file);
  19882. }
  19883. }
  19884. }
  19885. // strip binary bytes in front of PS-header
  19886. $start = strpos($data, '%!PS-Adobe');
  19887. if ($start > 0) {
  19888. $data = substr($data, $start);
  19889. }
  19890. // find BoundingBox params
  19891. preg_match("/%%BoundingBox:([^\r\n]+)/", $data, $regs);
  19892. if (count($regs) > 1) {
  19893. list($x1, $y1, $x2, $y2) = explode(' ', trim($regs[1]));
  19894. } else {
  19895. $this->Error('No BoundingBox found in EPS/AI file: ' . $file);
  19896. }
  19897. $start = strpos($data, '%%EndSetup');
  19898. if ($start === false) {
  19899. $start = strpos($data, '%%EndProlog');
  19900. }
  19901. if ($start === false) {
  19902. $start = strpos($data, '%%BoundingBox');
  19903. }
  19904. $data = substr($data, $start);
  19905. $end = strpos($data, '%%PageTrailer');
  19906. if ($end === false) {
  19907. $end = strpos($data, 'showpage');
  19908. }
  19909. if ($end) {
  19910. $data = substr($data, 0, $end);
  19911. }
  19912. // calculate image width and height on document
  19913. if (($w <= 0) AND ($h <= 0)) {
  19914. $w = ($x2 - $x1) / $k;
  19915. $h = ($y2 - $y1) / $k;
  19916. } elseif ($w <= 0) {
  19917. $w = ($x2 - $x1) / $k * ($h / (($y2 - $y1) / $k));
  19918. } elseif ($h <= 0) {
  19919. $h = ($y2 - $y1) / $k * ($w / (($x2 - $x1) / $k));
  19920. }
  19921. // fit the image on available space
  19922. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  19923. if ($this->rasterize_vector_images) {
  19924. // convert EPS to raster image using GD or ImageMagick libraries
  19925. return $this->Image($file, $x, $y, $w, $h, 'EPS', $link, $align, true, 300, $palign, false, false, $border, false, false, $fitonpage);
  19926. }
  19927. // set scaling factors
  19928. $scale_x = $w / (($x2 - $x1) / $k);
  19929. $scale_y = $h / (($y2 - $y1) / $k);
  19930. // set alignment
  19931. $this->img_rb_y = $y + $h;
  19932. // set alignment
  19933. if ($this->rtl) {
  19934. if ($palign == 'L') {
  19935. $ximg = $this->lMargin;
  19936. } elseif ($palign == 'C') {
  19937. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  19938. } elseif ($palign == 'R') {
  19939. $ximg = $this->w - $this->rMargin - $w;
  19940. } else {
  19941. $ximg = $x - $w;
  19942. }
  19943. $this->img_rb_x = $ximg;
  19944. } else {
  19945. if ($palign == 'L') {
  19946. $ximg = $this->lMargin;
  19947. } elseif ($palign == 'C') {
  19948. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  19949. } elseif ($palign == 'R') {
  19950. $ximg = $this->w - $this->rMargin - $w;
  19951. } else {
  19952. $ximg = $x;
  19953. }
  19954. $this->img_rb_x = $ximg + $w;
  19955. }
  19956. if ($useBoundingBox) {
  19957. $dx = $ximg * $k - $x1;
  19958. $dy = $y * $k - $y1;
  19959. } else {
  19960. $dx = $ximg * $k;
  19961. $dy = $y * $k;
  19962. }
  19963. // save the current graphic state
  19964. $this->_out('q' . $this->epsmarker);
  19965. // translate
  19966. $this->_out(sprintf('%F %F %F %F %F %F cm', 1, 0, 0, 1, $dx, $dy + ($this->hPt - (2 * $y * $k) - ($y2 - $y1))));
  19967. // scale
  19968. if (isset($scale_x)) {
  19969. $this->_out(sprintf('%F %F %F %F %F %F cm', $scale_x, 0, 0, $scale_y, $x1 * (1 - $scale_x), $y2 * (1 - $scale_y)));
  19970. }
  19971. // handle pc/unix/mac line endings
  19972. $lines = preg_split('/[\r\n]+/si', $data, -1, PREG_SPLIT_NO_EMPTY);
  19973. $u = 0;
  19974. $cnt = count($lines);
  19975. for ($i = 0; $i < $cnt; ++$i) {
  19976. $line = $lines[$i];
  19977. if (($line == '') OR ($line{0} == '%')) {
  19978. continue;
  19979. }
  19980. $len = strlen($line);
  19981. // check for spot color names
  19982. $color_name = '';
  19983. if (strcasecmp('x', substr(trim($line), -1)) == 0) {
  19984. if (preg_match('/\([^\)]*\)/', $line, $matches) > 0) {
  19985. // extract spot color name
  19986. $color_name = $matches[0];
  19987. // remove color name from string
  19988. $line = str_replace(' ' . $color_name, '', $line);
  19989. // remove pharentesis from color name
  19990. $color_name = substr($color_name, 1, -1);
  19991. }
  19992. }
  19993. $chunks = explode(' ', $line);
  19994. $cmd = trim(array_pop($chunks));
  19995. // RGB
  19996. if (($cmd == 'Xa') OR ($cmd == 'XA')) {
  19997. $b = array_pop($chunks);
  19998. $g = array_pop($chunks);
  19999. $r = array_pop($chunks);
  20000. $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!
  20001. continue;
  20002. }
  20003. $skip = false;
  20004. if ($fixoutvals) {
  20005. // check for values outside the bounding box
  20006. switch ($cmd) {
  20007. case 'm':
  20008. case 'l':
  20009. case 'L': {
  20010. // skip values outside bounding box
  20011. foreach ($chunks as $key => $val) {
  20012. if ((($key % 2) == 0) AND (($val < $x1) OR ($val > $x2))) {
  20013. $skip = true;
  20014. } elseif ((($key % 2) != 0) AND (($val < $y1) OR ($val > $y2))) {
  20015. $skip = true;
  20016. }
  20017. }
  20018. }
  20019. }
  20020. }
  20021. switch ($cmd) {
  20022. case 'm':
  20023. case 'l':
  20024. case 'v':
  20025. case 'y':
  20026. case 'c':
  20027. case 'k':
  20028. case 'K':
  20029. case 'g':
  20030. case 'G':
  20031. case 's':
  20032. case 'S':
  20033. case 'J':
  20034. case 'j':
  20035. case 'w':
  20036. case 'M':
  20037. case 'd':
  20038. case 'n': {
  20039. if ($skip) {
  20040. break;
  20041. }
  20042. $this->_out($line);
  20043. break;
  20044. }
  20045. case 'x': {// custom fill color
  20046. if (empty($color_name)) {
  20047. // CMYK color
  20048. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  20049. $this->_out('' . $col_c . ' ' . $col_m . ' ' . $col_y . ' ' . $col_k . ' k');
  20050. } else {
  20051. // Spot Color (CMYK + tint)
  20052. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  20053. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  20054. $color_cmd = sprintf('/CS%d cs %F scn', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  20055. $this->_out($color_cmd);
  20056. }
  20057. break;
  20058. }
  20059. case 'X': { // custom stroke color
  20060. if (empty($color_name)) {
  20061. // CMYK color
  20062. list($col_c, $col_m, $col_y, $col_k) = $chunks;
  20063. $this->_out('' . $col_c . ' ' . $col_m . ' ' . $col_y . ' ' . $col_k . ' K');
  20064. } else {
  20065. // Spot Color (CMYK + tint)
  20066. list($col_c, $col_m, $col_y, $col_k, $col_t) = $chunks;
  20067. $this->AddSpotColor($color_name, ($col_c * 100), ($col_m * 100), ($col_y * 100), ($col_k * 100));
  20068. $color_cmd = sprintf('/CS%d CS %F SCN', $this->spot_colors[$color_name]['i'], (1 - $col_t));
  20069. $this->_out($color_cmd);
  20070. }
  20071. break;
  20072. }
  20073. case 'Y':
  20074. case 'N':
  20075. case 'V':
  20076. case 'L':
  20077. case 'C': {
  20078. if ($skip) {
  20079. break;
  20080. }
  20081. $line[($len - 1)] = strtolower($cmd);
  20082. $this->_out($line);
  20083. break;
  20084. }
  20085. case 'b':
  20086. case 'B': {
  20087. $this->_out($cmd . '*');
  20088. break;
  20089. }
  20090. case 'f':
  20091. case 'F': {
  20092. if ($u > 0) {
  20093. $isU = false;
  20094. $max = min(($i + 5), $cnt);
  20095. for ($j = ($i + 1); $j < $max; ++$j) {
  20096. $isU = ($isU OR (($lines[$j] == 'U') OR ($lines[$j] == '*U')));
  20097. }
  20098. if ($isU) {
  20099. $this->_out('f*');
  20100. }
  20101. } else {
  20102. $this->_out('f*');
  20103. }
  20104. break;
  20105. }
  20106. case '*u': {
  20107. ++$u;
  20108. break;
  20109. }
  20110. case '*U': {
  20111. --$u;
  20112. break;
  20113. }
  20114. }
  20115. }
  20116. // restore previous graphic state
  20117. $this->_out($this->epsmarker . 'Q');
  20118. if (!empty($border)) {
  20119. $bx = $this->x;
  20120. $by = $this->y;
  20121. $this->x = $ximg;
  20122. if ($this->rtl) {
  20123. $this->x += $w;
  20124. }
  20125. $this->y = $y;
  20126. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  20127. $this->x = $bx;
  20128. $this->y = $by;
  20129. }
  20130. if ($link) {
  20131. $this->Link($ximg, $y, $w, $h, $link, 0);
  20132. }
  20133. // set pointer to align the next text/objects
  20134. switch ($align) {
  20135. case 'T': {
  20136. $this->y = $y;
  20137. $this->x = $this->img_rb_x;
  20138. break;
  20139. }
  20140. case 'M': {
  20141. $this->y = $y + round($h / 2);
  20142. $this->x = $this->img_rb_x;
  20143. break;
  20144. }
  20145. case 'B': {
  20146. $this->y = $this->img_rb_y;
  20147. $this->x = $this->img_rb_x;
  20148. break;
  20149. }
  20150. case 'N': {
  20151. $this->SetY($this->img_rb_y);
  20152. break;
  20153. }
  20154. default: {
  20155. break;
  20156. }
  20157. }
  20158. $this->endlinex = $this->img_rb_x;
  20159. }
  20160. /**
  20161. * Set document barcode.
  20162. * @param $bc (string) barcode
  20163. * @public
  20164. */
  20165. public function setBarcode($bc = '') {
  20166. $this->barcode = $bc;
  20167. }
  20168. /**
  20169. * Get current barcode.
  20170. * @return string
  20171. * @public
  20172. * @since 4.0.012 (2008-07-24)
  20173. */
  20174. public function getBarcode() {
  20175. return $this->barcode;
  20176. }
  20177. /**
  20178. * Print a Linear Barcode.
  20179. * @param $code (string) code to print
  20180. * @param $type (string) type of barcode (see barcodes.php for supported formats).
  20181. * @param $x (int) x position in user units (empty string = current x position)
  20182. * @param $y (int) y position in user units (empty string = current y position)
  20183. * @param $w (int) width in user units (empty string = remaining page width)
  20184. * @param $h (int) height in user units (empty string = remaining page height)
  20185. * @param $xres (float) width of the smallest bar in user units (empty string = default value = 0.4mm)
  20186. * @param $style (array) array of options:<ul>
  20187. * <li>boolean $style['border'] if true prints a border</li>
  20188. * <li>int $style['padding'] padding to leave around the barcode in user units (set to 'auto' for automatic padding)</li>
  20189. * <li>int $style['hpadding'] horizontal padding in user units (set to 'auto' for automatic padding)</li>
  20190. * <li>int $style['vpadding'] vertical padding in user units (set to 'auto' for automatic padding)</li>
  20191. * <li>array $style['fgcolor'] color array for bars and text</li>
  20192. * <li>mixed $style['bgcolor'] color array for background (set to false for transparent)</li>
  20193. * <li>boolean $style['text'] if true prints text below the barcode</li>
  20194. * <li>string $style['label'] override default label</li>
  20195. * <li>string $style['font'] font name for text</li><li>int $style['fontsize'] font size for text</li>
  20196. * <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>
  20197. * <li>string $style['position'] horizontal position of the containing barcode cell on the page: L = left margin; C = center; R = right margin.</li>
  20198. * <li>string $style['align'] horizontal position of the barcode on the containing rectangle: L = left; C = center; R = right.</li>
  20199. * <li>string $style['stretch'] if true stretch the barcode to best fit the available width, otherwise uses $xres resolution for a single bar.</li>
  20200. * <li>string $style['fitwidth'] if true reduce the width to fit the barcode width + padding. When this option is enabled the 'stretch' option is automatically disabled.</li>
  20201. * <li>string $style['cellfitalign'] this option works only when 'fitwidth' is true and 'position' is unset or empty. Set the horizontal position of the containing barcode cell inside the specified rectangle: L = left; C = center; R = right.</li></ul>
  20202. * @param $align (string) 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>
  20203. * @author Nicola Asuni
  20204. * @since 3.1.000 (2008-06-09)
  20205. * @public
  20206. */
  20207. public function write1DBarcode($code, $type, $x = '', $y = '', $w = '', $h = '', $xres = '', $style = '', $align = '') {
  20208. if ($this->empty_string(trim($code))) {
  20209. return;
  20210. }
  20211. require_once(dirname(__FILE__) . '/barcodes.php');
  20212. // save current graphic settings
  20213. $gvars = $this->getGraphicVars();
  20214. // create new barcode object
  20215. $barcodeobj = new TCPDFBarcode($code, $type);
  20216. $arrcode = $barcodeobj->getBarcodeArray();
  20217. if (($arrcode === false) OR empty($arrcode) OR ($arrcode['maxw'] == 0)) {
  20218. $this->Error('Error in 1D barcode string');
  20219. }
  20220. // set default values
  20221. if (!isset($style['position'])) {
  20222. $style['position'] = '';
  20223. } elseif ($style['position'] == 'S') {
  20224. // keep this for backward compatibility
  20225. $style['position'] = '';
  20226. $style['stretch'] = true;
  20227. }
  20228. if (!isset($style['fitwidth'])) {
  20229. if (!isset($style['stretch'])) {
  20230. $style['fitwidth'] = true;
  20231. } else {
  20232. $style['fitwidth'] = false;
  20233. }
  20234. }
  20235. if ($style['fitwidth']) {
  20236. // disable stretch
  20237. $style['stretch'] = false;
  20238. }
  20239. if (!isset($style['stretch'])) {
  20240. if (($w === '') OR ($w <= 0)) {
  20241. $style['stretch'] = false;
  20242. } else {
  20243. $style['stretch'] = true;
  20244. }
  20245. }
  20246. if (!isset($style['fgcolor'])) {
  20247. $style['fgcolor'] = array(0, 0, 0); // default black
  20248. }
  20249. if (!isset($style['bgcolor'])) {
  20250. $style['bgcolor'] = false; // default transparent
  20251. }
  20252. if (!isset($style['border'])) {
  20253. $style['border'] = false;
  20254. }
  20255. $fontsize = 0;
  20256. if (!isset($style['text'])) {
  20257. $style['text'] = false;
  20258. }
  20259. if ($style['text'] AND isset($style['font'])) {
  20260. if (isset($style['fontsize'])) {
  20261. $fontsize = $style['fontsize'];
  20262. }
  20263. $this->SetFont($style['font'], '', $fontsize);
  20264. }
  20265. if (!isset($style['stretchtext'])) {
  20266. $style['stretchtext'] = 4;
  20267. }
  20268. if ($x === '') {
  20269. $x = $this->x;
  20270. }
  20271. if ($y === '') {
  20272. $y = $this->y;
  20273. }
  20274. // check page for no-write regions and adapt page margins if necessary
  20275. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  20276. if (($w === '') OR ($w <= 0)) {
  20277. if ($this->rtl) {
  20278. $w = $x - $this->lMargin;
  20279. } else {
  20280. $w = $this->w - $this->rMargin - $x;
  20281. }
  20282. }
  20283. // padding
  20284. if (!isset($style['padding'])) {
  20285. $padding = 0;
  20286. } elseif ($style['padding'] === 'auto') {
  20287. $padding = 10 * ($w / ($arrcode['maxw'] + 20));
  20288. } else {
  20289. $padding = floatval($style['padding']);
  20290. }
  20291. // horizontal padding
  20292. if (!isset($style['hpadding'])) {
  20293. $hpadding = $padding;
  20294. } elseif ($style['hpadding'] === 'auto') {
  20295. $hpadding = 10 * ($w / ($arrcode['maxw'] + 20));
  20296. } else {
  20297. $hpadding = floatval($style['hpadding']);
  20298. }
  20299. // vertical padding
  20300. if (!isset($style['vpadding'])) {
  20301. $vpadding = $padding;
  20302. } elseif ($style['vpadding'] === 'auto') {
  20303. $vpadding = ($hpadding / 2);
  20304. } else {
  20305. $vpadding = floatval($style['vpadding']);
  20306. }
  20307. // calculate xres (single bar width)
  20308. $max_xres = ($w - (2 * $hpadding)) / $arrcode['maxw'];
  20309. if ($style['stretch']) {
  20310. $xres = $max_xres;
  20311. } else {
  20312. if ($this->empty_string($xres)) {
  20313. $xres = (0.141 * $this->k); // default bar width = 0.4 mm
  20314. }
  20315. if ($xres > $max_xres) {
  20316. // correct xres to fit on $w
  20317. $xres = $max_xres;
  20318. }
  20319. if ((isset($style['padding']) AND ($style['padding'] === 'auto'))
  20320. OR (isset($style['hpadding']) AND ($style['hpadding'] === 'auto'))) {
  20321. $hpadding = 10 * $xres;
  20322. if (isset($style['vpadding']) AND ($style['vpadding'] === 'auto')) {
  20323. $vpadding = ($hpadding / 2);
  20324. }
  20325. }
  20326. }
  20327. if ($style['fitwidth']) {
  20328. $wold = $w;
  20329. $w = (($arrcode['maxw'] * $xres) + (2 * $hpadding));
  20330. if (isset($style['cellfitalign'])) {
  20331. switch ($style['cellfitalign']) {
  20332. case 'L': {
  20333. if ($this->rtl) {
  20334. $x -= ($wold - $w);
  20335. }
  20336. break;
  20337. }
  20338. case 'R': {
  20339. if (!$this->rtl) {
  20340. $x += ($wold - $w);
  20341. }
  20342. break;
  20343. }
  20344. case 'C': {
  20345. if ($this->rtl) {
  20346. $x -= (($wold - $w) / 2);
  20347. } else {
  20348. $x += (($wold - $w) / 2);
  20349. }
  20350. break;
  20351. }
  20352. default : {
  20353. break;
  20354. }
  20355. }
  20356. }
  20357. }
  20358. $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
  20359. // height
  20360. if (($h === '') OR ($h <= 0)) {
  20361. // set default height
  20362. $h = (($arrcode['maxw'] * $xres) / 3) + (2 * $vpadding) + $text_height;
  20363. }
  20364. $barh = $h - $text_height - (2 * $vpadding);
  20365. if ($barh <= 0) {
  20366. // try to reduce font or padding to fit barcode on available height
  20367. if ($text_height > $h) {
  20368. $fontsize = (($h * $this->k) / (4 * $this->cell_height_ratio));
  20369. $text_height = ($this->cell_height_ratio * $fontsize / $this->k);
  20370. $this->SetFont($style['font'], '', $fontsize);
  20371. }
  20372. if ($vpadding > 0) {
  20373. $vpadding = (($h - $text_height) / 4);
  20374. }
  20375. $barh = $h - $text_height - (2 * $vpadding);
  20376. }
  20377. // fit the barcode on available space
  20378. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  20379. // set alignment
  20380. $this->img_rb_y = $y + $h;
  20381. // set alignment
  20382. if ($this->rtl) {
  20383. if ($style['position'] == 'L') {
  20384. $xpos = $this->lMargin;
  20385. } elseif ($style['position'] == 'C') {
  20386. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  20387. } elseif ($style['position'] == 'R') {
  20388. $xpos = $this->w - $this->rMargin - $w;
  20389. } else {
  20390. $xpos = $x - $w;
  20391. }
  20392. $this->img_rb_x = $xpos;
  20393. } else {
  20394. if ($style['position'] == 'L') {
  20395. $xpos = $this->lMargin;
  20396. } elseif ($style['position'] == 'C') {
  20397. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  20398. } elseif ($style['position'] == 'R') {
  20399. $xpos = $this->w - $this->rMargin - $w;
  20400. } else {
  20401. $xpos = $x;
  20402. }
  20403. $this->img_rb_x = $xpos + $w;
  20404. }
  20405. $xpos_rect = $xpos;
  20406. if (!isset($style['align'])) {
  20407. $style['align'] = 'C';
  20408. }
  20409. switch ($style['align']) {
  20410. case 'L': {
  20411. $xpos = $xpos_rect + $hpadding;
  20412. break;
  20413. }
  20414. case 'R': {
  20415. $xpos = $xpos_rect + ($w - ($arrcode['maxw'] * $xres)) - $hpadding;
  20416. break;
  20417. }
  20418. case 'C':
  20419. default : {
  20420. $xpos = $xpos_rect + (($w - ($arrcode['maxw'] * $xres)) / 2);
  20421. break;
  20422. }
  20423. }
  20424. $xpos_text = $xpos;
  20425. // barcode is always printed in LTR direction
  20426. $tempRTL = $this->rtl;
  20427. $this->rtl = false;
  20428. // print background color
  20429. if ($style['bgcolor']) {
  20430. $this->Rect($xpos_rect, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  20431. } elseif ($style['border']) {
  20432. $this->Rect($xpos_rect, $y, $w, $h, 'D');
  20433. }
  20434. // set foreground color
  20435. $this->SetDrawColorArray($style['fgcolor']);
  20436. $this->SetTextColorArray($style['fgcolor']);
  20437. // print bars
  20438. foreach ($arrcode['bcode'] as $k => $v) {
  20439. $bw = ($v['w'] * $xres);
  20440. if ($v['t']) {
  20441. // draw a vertical bar
  20442. $ypos = $y + $vpadding + ($v['p'] * $barh / $arrcode['maxh']);
  20443. $this->Rect($xpos, $ypos, $bw, ($v['h'] * $barh / $arrcode['maxh']), 'F', array(), $style['fgcolor']);
  20444. }
  20445. $xpos += $bw;
  20446. }
  20447. // print text
  20448. if ($style['text']) {
  20449. if (isset($style['label']) AND !$this->empty_string($style['label'])) {
  20450. $label = $style['label'];
  20451. } else {
  20452. $label = $code;
  20453. }
  20454. $txtwidth = ($arrcode['maxw'] * $xres);
  20455. if ($this->GetStringWidth($label) > $txtwidth) {
  20456. $style['stretchtext'] = 2;
  20457. }
  20458. // print text
  20459. $this->x = $xpos_text;
  20460. $this->y = $y + $vpadding + $barh;
  20461. $cellpadding = $this->cell_padding;
  20462. $this->SetCellPadding(0);
  20463. $this->Cell($txtwidth, '', $label, 0, 0, 'C', false, '', $style['stretchtext'], false, 'T', 'T');
  20464. $this->cell_padding = $cellpadding;
  20465. }
  20466. // restore original direction
  20467. $this->rtl = $tempRTL;
  20468. // restore previous settings
  20469. $this->setGraphicVars($gvars);
  20470. // set pointer to align the next text/objects
  20471. switch ($align) {
  20472. case 'T': {
  20473. $this->y = $y;
  20474. $this->x = $this->img_rb_x;
  20475. break;
  20476. }
  20477. case 'M': {
  20478. $this->y = $y + round($h / 2);
  20479. $this->x = $this->img_rb_x;
  20480. break;
  20481. }
  20482. case 'B': {
  20483. $this->y = $this->img_rb_y;
  20484. $this->x = $this->img_rb_x;
  20485. break;
  20486. }
  20487. case 'N': {
  20488. $this->SetY($this->img_rb_y);
  20489. break;
  20490. }
  20491. default: {
  20492. break;
  20493. }
  20494. }
  20495. $this->endlinex = $this->img_rb_x;
  20496. }
  20497. /**
  20498. * This function is DEPRECATED, please use the new write1DBarcode() function.
  20499. * @param $x (int) x position in user units
  20500. * @param $y (int) y position in user units
  20501. * @param $w (int) width in user units
  20502. * @param $h (int) height position in user units
  20503. * @param $type (string) type of barcode
  20504. * @param $style (string) barcode style
  20505. * @param $font (string) font for text
  20506. * @param $xres (int) x resolution
  20507. * @param $code (string) code to print
  20508. * @deprecated deprecated since version 3.1.000 (2008-06-10)
  20509. * @public
  20510. * @see write1DBarcode()
  20511. */
  20512. public function writeBarcode($x, $y, $w, $h, $type, $style, $font, $xres, $code) {
  20513. // convert old settings for the new write1DBarcode() function.
  20514. $xres = 1 / $xres;
  20515. $newstyle = array(
  20516. 'position' => '',
  20517. 'align' => '',
  20518. 'stretch' => false,
  20519. 'fitwidth' => false,
  20520. 'cellfitalign' => '',
  20521. 'border' => false,
  20522. 'padding' => 0,
  20523. 'fgcolor' => array(0, 0, 0),
  20524. 'bgcolor' => false,
  20525. 'text' => true,
  20526. 'font' => $font,
  20527. 'fontsize' => 8,
  20528. 'stretchtext' => 4
  20529. );
  20530. if ($style & 1) {
  20531. $newstyle['border'] = true;
  20532. }
  20533. if ($style & 2) {
  20534. $newstyle['bgcolor'] = false;
  20535. }
  20536. if ($style & 4) {
  20537. $newstyle['position'] = 'C';
  20538. } elseif ($style & 8) {
  20539. $newstyle['position'] = 'L';
  20540. } elseif ($style & 16) {
  20541. $newstyle['position'] = 'R';
  20542. }
  20543. if ($style & 128) {
  20544. $newstyle['text'] = true;
  20545. }
  20546. if ($style & 256) {
  20547. $newstyle['stretchtext'] = 4;
  20548. }
  20549. $this->write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
  20550. }
  20551. /**
  20552. * Print 2D Barcode.
  20553. * @param $code (string) code to print
  20554. * @param $type (string) type of barcode (see 2dbarcodes.php for supported formats).
  20555. * @param $x (int) x position in user units
  20556. * @param $y (int) y position in user units
  20557. * @param $w (int) width in user units
  20558. * @param $h (int) height in user units
  20559. * @param $style (array) array of options:<ul>
  20560. * <li>boolean $style['border'] if true prints a border around the barcode</li>
  20561. * <li>int $style['padding'] padding to leave around the barcode in barcode units (set to 'auto' for automatic padding)</li>
  20562. * <li>int $style['hpadding'] horizontal padding in barcode units (set to 'auto' for automatic padding)</li>
  20563. * <li>int $style['vpadding'] vertical padding in barcode units (set to 'auto' for automatic padding)</li>
  20564. * <li>int $style['module_width'] width of a single module in points</li>
  20565. * <li>int $style['module_height'] height of a single module in points</li>
  20566. * <li>array $style['fgcolor'] color array for bars and text</li>
  20567. * <li>mixed $style['bgcolor'] color array for background or false for transparent</li>
  20568. * <li>string $style['position'] barcode position on the page: L = left margin; C = center; R = right margin; S = stretch</li><li>$style['module_width'] width of a single module in points</li>
  20569. * <li>$style['module_height'] height of a single module in points</li></ul>
  20570. * @param $align (string) 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>
  20571. * @param $distort (boolean) if true distort the barcode to fit width and height, otherwise preserve aspect ratio
  20572. * @author Nicola Asuni
  20573. * @since 4.5.037 (2009-04-07)
  20574. * @public
  20575. */
  20576. public function write2DBarcode($code, $type, $x = '', $y = '', $w = '', $h = '', $style = '', $align = '', $distort = false) {
  20577. if ($this->empty_string(trim($code))) {
  20578. return;
  20579. }
  20580. require_once(dirname(__FILE__) . '/2dbarcodes.php');
  20581. // save current graphic settings
  20582. $gvars = $this->getGraphicVars();
  20583. // create new barcode object
  20584. $barcodeobj = new TCPDF2DBarcode($code, $type);
  20585. $arrcode = $barcodeobj->getBarcodeArray();
  20586. if (($arrcode === false) OR empty($arrcode) OR !isset($arrcode['num_rows']) OR ($arrcode['num_rows'] == 0) OR !isset($arrcode['num_cols']) OR ($arrcode['num_cols'] == 0)) {
  20587. $this->Error('Error in 2D barcode string');
  20588. }
  20589. // set default values
  20590. if (!isset($style['position'])) {
  20591. $style['position'] = '';
  20592. }
  20593. if (!isset($style['fgcolor'])) {
  20594. $style['fgcolor'] = array(0, 0, 0); // default black
  20595. }
  20596. if (!isset($style['bgcolor'])) {
  20597. $style['bgcolor'] = false; // default transparent
  20598. }
  20599. if (!isset($style['border'])) {
  20600. $style['border'] = false;
  20601. }
  20602. // padding
  20603. if (!isset($style['padding'])) {
  20604. $style['padding'] = 0;
  20605. } elseif ($style['padding'] === 'auto') {
  20606. $style['padding'] = 4;
  20607. }
  20608. if (!isset($style['hpadding'])) {
  20609. $style['hpadding'] = $style['padding'];
  20610. } elseif ($style['hpadding'] === 'auto') {
  20611. $style['hpadding'] = 4;
  20612. }
  20613. if (!isset($style['vpadding'])) {
  20614. $style['vpadding'] = $style['padding'];
  20615. } elseif ($style['vpadding'] === 'auto') {
  20616. $style['vpadding'] = 4;
  20617. }
  20618. $hpad = (2 * $style['hpadding']);
  20619. $vpad = (2 * $style['vpadding']);
  20620. // cell (module) dimension
  20621. if (!isset($style['module_width'])) {
  20622. $style['module_width'] = 1; // width of a single module in points
  20623. }
  20624. if (!isset($style['module_height'])) {
  20625. $style['module_height'] = 1; // height of a single module in points
  20626. }
  20627. if ($x === '') {
  20628. $x = $this->x;
  20629. }
  20630. if ($y === '') {
  20631. $y = $this->y;
  20632. }
  20633. // check page for no-write regions and adapt page margins if necessary
  20634. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  20635. // number of barcode columns and rows
  20636. $rows = $arrcode['num_rows'];
  20637. $cols = $arrcode['num_cols'];
  20638. // module width and height
  20639. $mw = $style['module_width'];
  20640. $mh = $style['module_height'];
  20641. if (($mw == 0) OR ($mh == 0)) {
  20642. $this->Error('Error in 2D barcode string');
  20643. }
  20644. // get max dimensions
  20645. if ($this->rtl) {
  20646. $maxw = $x - $this->lMargin;
  20647. } else {
  20648. $maxw = $this->w - $this->rMargin - $x;
  20649. }
  20650. $maxh = ($this->h - $this->tMargin - $this->bMargin);
  20651. $ratioHW = ((($rows * $mh) + $hpad) / (($cols * $mw) + $vpad));
  20652. $ratioWH = ((($cols * $mw) + $vpad) / (($rows * $mh) + $hpad));
  20653. if (!$distort) {
  20654. if (($maxw * $ratioHW) > $maxh) {
  20655. $maxw = $maxh * $ratioWH;
  20656. }
  20657. if (($maxh * $ratioWH) > $maxw) {
  20658. $maxh = $maxw * $ratioHW;
  20659. }
  20660. }
  20661. // set maximum dimesions
  20662. if ($w > $maxw) {
  20663. $w = $maxw;
  20664. }
  20665. if ($h > $maxh) {
  20666. $h = $maxh;
  20667. }
  20668. // set dimensions
  20669. if ((($w === '') OR ($w <= 0)) AND (($h === '') OR ($h <= 0))) {
  20670. $w = ($cols + $hpad) * ($mw / $this->k);
  20671. $h = ($rows + $vpad) * ($mh / $this->k);
  20672. } elseif (($w === '') OR ($w <= 0)) {
  20673. $w = $h * $ratioWH;
  20674. } elseif (($h === '') OR ($h <= 0)) {
  20675. $h = $w * $ratioHW;
  20676. }
  20677. // barcode size (excluding padding)
  20678. $bw = ($w * $cols) / ($cols + $hpad);
  20679. $bh = ($h * $rows) / ($rows + $vpad);
  20680. // dimension of single barcode cell unit
  20681. $cw = $bw / $cols;
  20682. $ch = $bh / $rows;
  20683. if (!$distort) {
  20684. if (($cw / $ch) > ($mw / $mh)) {
  20685. // correct horizontal distortion
  20686. $cw = $ch * $mw / $mh;
  20687. $bw = $cw * $cols;
  20688. $style['hpadding'] = ($w - $bw) / (2 * $cw);
  20689. } else {
  20690. // correct vertical distortion
  20691. $ch = $cw * $mh / $mw;
  20692. $bh = $ch * $rows;
  20693. $style['vpadding'] = ($h - $bh) / (2 * $ch);
  20694. }
  20695. }
  20696. // fit the barcode on available space
  20697. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, false);
  20698. // set alignment
  20699. $this->img_rb_y = $y + $h;
  20700. // set alignment
  20701. if ($this->rtl) {
  20702. if ($style['position'] == 'L') {
  20703. $xpos = $this->lMargin;
  20704. } elseif ($style['position'] == 'C') {
  20705. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  20706. } elseif ($style['position'] == 'R') {
  20707. $xpos = $this->w - $this->rMargin - $w;
  20708. } else {
  20709. $xpos = $x - $w;
  20710. }
  20711. $this->img_rb_x = $xpos;
  20712. } else {
  20713. if ($style['position'] == 'L') {
  20714. $xpos = $this->lMargin;
  20715. } elseif ($style['position'] == 'C') {
  20716. $xpos = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  20717. } elseif ($style['position'] == 'R') {
  20718. $xpos = $this->w - $this->rMargin - $w;
  20719. } else {
  20720. $xpos = $x;
  20721. }
  20722. $this->img_rb_x = $xpos + $w;
  20723. }
  20724. $xstart = $xpos + ($style['hpadding'] * $cw);
  20725. $ystart = $y + ($style['vpadding'] * $ch);
  20726. // barcode is always printed in LTR direction
  20727. $tempRTL = $this->rtl;
  20728. $this->rtl = false;
  20729. // print background color
  20730. if ($style['bgcolor']) {
  20731. $this->Rect($xpos, $y, $w, $h, $style['border'] ? 'DF' : 'F', '', $style['bgcolor']);
  20732. } elseif ($style['border']) {
  20733. $this->Rect($xpos, $y, $w, $h, 'D');
  20734. }
  20735. // set foreground color
  20736. $this->SetDrawColorArray($style['fgcolor']);
  20737. // print barcode cells
  20738. // for each row
  20739. for ($r = 0; $r < $rows; ++$r) {
  20740. $xr = $xstart;
  20741. // for each column
  20742. for ($c = 0; $c < $cols; ++$c) {
  20743. if ($arrcode['bcode'][$r][$c] == 1) {
  20744. // draw a single barcode cell
  20745. $this->Rect($xr, $ystart, $cw, $ch, 'F', array(), $style['fgcolor']);
  20746. }
  20747. $xr += $cw;
  20748. }
  20749. $ystart += $ch;
  20750. }
  20751. // restore original direction
  20752. $this->rtl = $tempRTL;
  20753. // restore previous settings
  20754. $this->setGraphicVars($gvars);
  20755. // set pointer to align the next text/objects
  20756. switch ($align) {
  20757. case 'T': {
  20758. $this->y = $y;
  20759. $this->x = $this->img_rb_x;
  20760. break;
  20761. }
  20762. case 'M': {
  20763. $this->y = $y + round($h / 2);
  20764. $this->x = $this->img_rb_x;
  20765. break;
  20766. }
  20767. case 'B': {
  20768. $this->y = $this->img_rb_y;
  20769. $this->x = $this->img_rb_x;
  20770. break;
  20771. }
  20772. case 'N': {
  20773. $this->SetY($this->img_rb_y);
  20774. break;
  20775. }
  20776. default: {
  20777. break;
  20778. }
  20779. }
  20780. $this->endlinex = $this->img_rb_x;
  20781. }
  20782. /**
  20783. * Returns an array containing current margins:
  20784. * <ul>
  20785. <li>$ret['left'] = left margin</li>
  20786. <li>$ret['right'] = right margin</li>
  20787. <li>$ret['top'] = top margin</li>
  20788. <li>$ret['bottom'] = bottom margin</li>
  20789. <li>$ret['header'] = header margin</li>
  20790. <li>$ret['footer'] = footer margin</li>
  20791. <li>$ret['cell'] = cell padding array</li>
  20792. <li>$ret['padding_left'] = cell left padding</li>
  20793. <li>$ret['padding_top'] = cell top padding</li>
  20794. <li>$ret['padding_right'] = cell right padding</li>
  20795. <li>$ret['padding_bottom'] = cell bottom padding</li>
  20796. * </ul>
  20797. * @return array containing all margins measures
  20798. * @public
  20799. * @since 3.2.000 (2008-06-23)
  20800. */
  20801. public function getMargins() {
  20802. $ret = array(
  20803. 'left' => $this->lMargin,
  20804. 'right' => $this->rMargin,
  20805. 'top' => $this->tMargin,
  20806. 'bottom' => $this->bMargin,
  20807. 'header' => $this->header_margin,
  20808. 'footer' => $this->footer_margin,
  20809. 'cell' => $this->cell_padding,
  20810. 'padding_left' => $this->cell_padding['L'],
  20811. 'padding_top' => $this->cell_padding['T'],
  20812. 'padding_right' => $this->cell_padding['R'],
  20813. 'padding_bottom' => $this->cell_padding['B']
  20814. );
  20815. return $ret;
  20816. }
  20817. /**
  20818. * Returns an array containing original margins:
  20819. * <ul>
  20820. <li>$ret['left'] = left margin</li>
  20821. <li>$ret['right'] = right margin</li>
  20822. * </ul>
  20823. * @return array containing all margins measures
  20824. * @public
  20825. * @since 4.0.012 (2008-07-24)
  20826. */
  20827. public function getOriginalMargins() {
  20828. $ret = array(
  20829. 'left' => $this->original_lMargin,
  20830. 'right' => $this->original_rMargin
  20831. );
  20832. return $ret;
  20833. }
  20834. /**
  20835. * Returns the current font size.
  20836. * @return current font size
  20837. * @public
  20838. * @since 3.2.000 (2008-06-23)
  20839. */
  20840. public function getFontSize() {
  20841. return $this->FontSize;
  20842. }
  20843. /**
  20844. * Returns the current font size in points unit.
  20845. * @return current font size in points unit
  20846. * @public
  20847. * @since 3.2.000 (2008-06-23)
  20848. */
  20849. public function getFontSizePt() {
  20850. return $this->FontSizePt;
  20851. }
  20852. /**
  20853. * Returns the current font family name.
  20854. * @return string current font family name
  20855. * @public
  20856. * @since 4.3.008 (2008-12-05)
  20857. */
  20858. public function getFontFamily() {
  20859. return $this->FontFamily;
  20860. }
  20861. /**
  20862. * Returns the current font style.
  20863. * @return string current font style
  20864. * @public
  20865. * @since 4.3.008 (2008-12-05)
  20866. */
  20867. public function getFontStyle() {
  20868. return $this->FontStyle;
  20869. }
  20870. /**
  20871. * Cleanup HTML code (requires HTML Tidy library).
  20872. * @param $html (string) htmlcode to fix
  20873. * @param $default_css (string) CSS commands to add
  20874. * @param $tagvs (array) parameters for setHtmlVSpace method
  20875. * @param $tidy_options (array) options for tidy_parse_string function
  20876. * @return string XHTML code cleaned up
  20877. * @author Nicola Asuni
  20878. * @public
  20879. * @since 5.9.017 (2010-11-16)
  20880. * @see setHtmlVSpace()
  20881. */
  20882. public function fixHTMLCode($html, $default_css = '', $tagvs = '', $tidy_options = '') {
  20883. // configure parameters for HTML Tidy
  20884. if ($tidy_options === '') {
  20885. $tidy_options = array(
  20886. 'clean' => 1,
  20887. 'drop-empty-paras' => 0,
  20888. 'drop-proprietary-attributes' => 1,
  20889. 'fix-backslash' => 1,
  20890. 'hide-comments' => 1,
  20891. 'join-styles' => 1,
  20892. 'lower-literals' => 1,
  20893. 'merge-divs' => 1,
  20894. 'merge-spans' => 1,
  20895. 'output-xhtml' => 1,
  20896. 'word-2000' => 1,
  20897. 'wrap' => 0,
  20898. 'output-bom' => 0,
  20899. //'char-encoding' => 'utf8',
  20900. //'input-encoding' => 'utf8',
  20901. //'output-encoding' => 'utf8'
  20902. );
  20903. }
  20904. // clean up the HTML code
  20905. $tidy = tidy_parse_string($html, $tidy_options);
  20906. // fix the HTML
  20907. $tidy->cleanRepair();
  20908. // get the CSS part
  20909. $tidy_head = tidy_get_head($tidy);
  20910. $css = $tidy_head->value;
  20911. $css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
  20912. $css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
  20913. $css = str_replace('/*<![CDATA[*/', '', $css);
  20914. $css = str_replace('/*]]>*/', '', $css);
  20915. preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
  20916. if (isset($matches[1])) {
  20917. $css = strtolower($matches[1]);
  20918. } else {
  20919. $css = '';
  20920. }
  20921. // include default css
  20922. $css = '<style>' . $default_css . $css . '</style>';
  20923. // get the body part
  20924. $tidy_body = tidy_get_body($tidy);
  20925. $html = $tidy_body->value;
  20926. // fix some self-closing tags
  20927. $html = str_replace('<br>', '<br />', $html);
  20928. // remove some empty tag blocks
  20929. $html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
  20930. $html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
  20931. if ($tagvs !== '') {
  20932. // set vertical space for some XHTML tags
  20933. $this->setHtmlVSpace($tagvs);
  20934. }
  20935. // return the cleaned XHTML code + CSS
  20936. return $css . $html;
  20937. }
  20938. /**
  20939. * Extracts the CSS properties from a CSS string.
  20940. * @param $cssdata (string) string containing CSS definitions.
  20941. * @return An array where the keys are the CSS selectors and the values are the CSS properties.
  20942. * @author Nicola Asuni
  20943. * @since 5.1.000 (2010-05-25)
  20944. * @protected
  20945. */
  20946. protected function extractCSSproperties($cssdata) {
  20947. if (empty($cssdata)) {
  20948. return array();
  20949. }
  20950. // remove comments
  20951. $cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
  20952. // remove newlines and multiple spaces
  20953. $cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
  20954. // remove some spaces
  20955. $cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
  20956. // remove empty blocks
  20957. $cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
  20958. // replace media type parenthesis
  20959. $cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\1�', $cssdata);
  20960. $cssdata = preg_replace('/\}\}/si', '}�', $cssdata);
  20961. // trim string
  20962. $cssdata = trim($cssdata);
  20963. // find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  20964. $cssblocks = array();
  20965. $matches = array();
  20966. if (preg_match_all('/@media[\s]+([^\�]*)�([^�]*)�/i', $cssdata, $matches) > 0) {
  20967. foreach ($matches[1] as $key => $type) {
  20968. $cssblocks[$type] = $matches[2][$key];
  20969. }
  20970. // remove media blocks
  20971. $cssdata = preg_replace('/@media[\s]+([^\�]*)�([^�]*)�/i', '', $cssdata);
  20972. }
  20973. // keep 'all' and 'print' media, other media types are discarded
  20974. if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
  20975. $cssdata .= $cssblocks['all'];
  20976. }
  20977. if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
  20978. $cssdata .= $cssblocks['print'];
  20979. }
  20980. // reset css blocks array
  20981. $cssblocks = array();
  20982. $matches = array();
  20983. // explode css data string into array
  20984. if (substr($cssdata, -1) == '}') {
  20985. // remove last parethesis
  20986. $cssdata = substr($cssdata, 0, -1);
  20987. }
  20988. $matches = explode('}', $cssdata);
  20989. foreach ($matches as $key => $block) {
  20990. // index 0 contains the CSS selector, index 1 contains CSS properties
  20991. $cssblocks[$key] = explode('{', $block);
  20992. if (!isset($cssblocks[$key][1])) {
  20993. // remove empty definitions
  20994. unset($cssblocks[$key]);
  20995. }
  20996. }
  20997. // split groups of selectors (comma-separated list of selectors)
  20998. foreach ($cssblocks as $key => $block) {
  20999. if (strpos($block[0], ',') > 0) {
  21000. $selectors = explode(',', $block[0]);
  21001. foreach ($selectors as $sel) {
  21002. $cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
  21003. }
  21004. unset($cssblocks[$key]);
  21005. }
  21006. }
  21007. // covert array to selector => properties
  21008. $cssdata = array();
  21009. foreach ($cssblocks as $block) {
  21010. $selector = $block[0];
  21011. // calculate selector's specificity
  21012. $matches = array();
  21013. $a = 0; // the declaration is not from is a 'style' attribute
  21014. $b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
  21015. $c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
  21016. $c += intval(preg_match_all('/[\:]link|visited|hover|active|focus|target|lang|enabled|disabled|checked|indeterminate|root|nth|first|last|only|empty|contains|not/i', $selector, $matches)); // number of pseudo-classes
  21017. $d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' ' . $selector, $matches)); // number of element names
  21018. $d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
  21019. $specificity = $a . $b . $c . $d;
  21020. // add specificity to the beginning of the selector
  21021. $cssdata[$specificity . ' ' . $selector] = $block[1];
  21022. }
  21023. // sort selectors alphabetically to account for specificity
  21024. ksort($cssdata, SORT_STRING);
  21025. // return array
  21026. return $cssdata;
  21027. }
  21028. /**
  21029. * Returns true if the CSS selector is valid for the selected HTML tag
  21030. * @param $dom (array) array of HTML tags and properties
  21031. * @param $key (int) key of the current HTML tag
  21032. * @param $selector (string) CSS selector string
  21033. * @return true if the selector is valid, false otherwise
  21034. * @protected
  21035. * @since 5.1.000 (2010-05-25)
  21036. */
  21037. protected function isValidCSSSelectorForTag($dom, $key, $selector) {
  21038. $valid = false; // value to be returned
  21039. $tag = $dom[$key]['value'];
  21040. $class = array();
  21041. if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
  21042. $class = explode(' ', strtolower($dom[$key]['attribute']['class']));
  21043. }
  21044. $id = '';
  21045. if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
  21046. $id = strtolower($dom[$key]['attribute']['id']);
  21047. }
  21048. $selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
  21049. $matches = array();
  21050. if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
  21051. $parentop = array_pop($matches[1]);
  21052. $operator = $parentop[0];
  21053. $offset = $parentop[1];
  21054. $lasttag = array_pop($matches[2]);
  21055. $lasttag = strtolower(trim($lasttag[0]));
  21056. if (($lasttag == '*') OR ($lasttag == $tag)) {
  21057. // the last element on selector is our tag or 'any tag'
  21058. $attrib = array_pop($matches[3]);
  21059. $attrib = strtolower(trim($attrib[0]));
  21060. if (!empty($attrib)) {
  21061. // check if matches class, id, attribute, pseudo-class or pseudo-element
  21062. switch ($attrib{0}) {
  21063. case '.': { // class
  21064. if (in_array(substr($attrib, 1), $class)) {
  21065. $valid = true;
  21066. }
  21067. break;
  21068. }
  21069. case '#': { // ID
  21070. if (substr($attrib, 1) == $id) {
  21071. $valid = true;
  21072. }
  21073. break;
  21074. }
  21075. case '[': { // attribute
  21076. $attrmatch = array();
  21077. if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
  21078. $att = strtolower($attrmatch[1]);
  21079. $val = $attrmatch[3];
  21080. if (isset($dom[$key]['attribute'][$att])) {
  21081. switch ($attrmatch[2]) {
  21082. case '=': {
  21083. if ($dom[$key]['attribute'][$att] == $val) {
  21084. $valid = true;
  21085. }
  21086. break;
  21087. }
  21088. case '~=': {
  21089. if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
  21090. $valid = true;
  21091. }
  21092. break;
  21093. }
  21094. case '^=': {
  21095. if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
  21096. $valid = true;
  21097. }
  21098. break;
  21099. }
  21100. case '$=': {
  21101. if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
  21102. $valid = true;
  21103. }
  21104. break;
  21105. }
  21106. case '*=': {
  21107. if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
  21108. $valid = true;
  21109. }
  21110. break;
  21111. }
  21112. case '|=': {
  21113. if ($dom[$key]['attribute'][$att] == $val) {
  21114. $valid = true;
  21115. } elseif (preg_match('/' . $val . '[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
  21116. $valid = true;
  21117. }
  21118. break;
  21119. }
  21120. default: {
  21121. $valid = true;
  21122. }
  21123. }
  21124. }
  21125. }
  21126. break;
  21127. }
  21128. case ':': { // pseudo-class or pseudo-element
  21129. if ($attrib{1} == ':') { // pseudo-element
  21130. // pseudo-elements are not supported!
  21131. // (::first-line, ::first-letter, ::before, ::after)
  21132. } else { // pseudo-class
  21133. // pseudo-classes are not supported!
  21134. // (:root, :nth-child(n), :nth-last-child(n), :nth-of-type(n), :nth-last-of-type(n), :first-child, :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty, :link, :visited, :active, :hover, :focus, :target, :lang(fr), :enabled, :disabled, :checked)
  21135. }
  21136. break;
  21137. }
  21138. } // end of switch
  21139. } else {
  21140. $valid = true;
  21141. }
  21142. if ($valid AND ($offset > 0)) {
  21143. $valid = false;
  21144. // check remaining selector part
  21145. $selector = substr($selector, 0, $offset);
  21146. switch ($operator) {
  21147. case ' ': { // descendant of an element
  21148. while ($dom[$key]['parent'] > 0) {
  21149. if ($this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
  21150. $valid = true;
  21151. break;
  21152. } else {
  21153. $key = $dom[$key]['parent'];
  21154. }
  21155. }
  21156. break;
  21157. }
  21158. case '>': { // child of an element
  21159. $valid = $this->isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
  21160. break;
  21161. }
  21162. case '+': { // immediately preceded by an element
  21163. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  21164. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  21165. $valid = $this->isValidCSSSelectorForTag($dom, $i, $selector);
  21166. break;
  21167. }
  21168. }
  21169. break;
  21170. }
  21171. case '~': { // preceded by an element
  21172. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  21173. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  21174. if ($this->isValidCSSSelectorForTag($dom, $i, $selector)) {
  21175. break;
  21176. }
  21177. }
  21178. }
  21179. break;
  21180. }
  21181. }
  21182. }
  21183. }
  21184. }
  21185. return $valid;
  21186. }
  21187. /**
  21188. * Returns the styles array that apply for the selected HTML tag.
  21189. * @param $dom (array) array of HTML tags and properties
  21190. * @param $key (int) key of the current HTML tag
  21191. * @param $css (array) array of CSS properties
  21192. * @return array containing CSS properties
  21193. * @protected
  21194. * @since 5.1.000 (2010-05-25)
  21195. */
  21196. protected function getCSSdataArray($dom, $key, $css) {
  21197. $cssarray = array(); // style to be returned
  21198. // get parent CSS selectors
  21199. $selectors = array();
  21200. if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
  21201. $selectors = $dom[($dom[$key]['parent'])]['csssel'];
  21202. }
  21203. // get all styles that apply
  21204. foreach ($css as $selector => $style) {
  21205. $pos = strpos($selector, ' ');
  21206. // get specificity
  21207. $specificity = substr($selector, 0, $pos);
  21208. // remove specificity
  21209. $selector = substr($selector, $pos);
  21210. // check if this selector apply to current tag
  21211. if ($this->isValidCSSSelectorForTag($dom, $key, $selector)) {
  21212. if (!in_array($selector, $selectors)) {
  21213. // add style if not already added on parent selector
  21214. $cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
  21215. $selectors[] = $selector;
  21216. }
  21217. }
  21218. }
  21219. if (isset($dom[$key]['attribute']['style'])) {
  21220. // attach inline style (latest properties have high priority)
  21221. $cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
  21222. }
  21223. // order the css array to account for specificity
  21224. $cssordered = array();
  21225. foreach ($cssarray as $key => $val) {
  21226. $skey = sprintf('%04d', $key);
  21227. $cssordered[$val['s'] . '_' . $skey] = $val;
  21228. }
  21229. // sort selectors alphabetically to account for specificity
  21230. ksort($cssordered, SORT_STRING);
  21231. return array($selectors, $cssordered);
  21232. }
  21233. /**
  21234. * Compact CSS data array into single string.
  21235. * @param $css (array) array of CSS properties
  21236. * @return string containing merged CSS properties
  21237. * @protected
  21238. * @since 5.9.070 (2011-04-19)
  21239. */
  21240. protected function getTagStyleFromCSSarray($css) {
  21241. $tagstyle = ''; // value to be returned
  21242. foreach ($css as $style) {
  21243. // split single css commands
  21244. $csscmds = explode(';', $style['c']);
  21245. foreach ($csscmds as $cmd) {
  21246. if (!empty($cmd)) {
  21247. $pos = strpos($cmd, ':');
  21248. if ($pos !== false) {
  21249. $cmd = substr($cmd, 0, ($pos + 1));
  21250. if (strpos($tagstyle, $cmd) !== false) {
  21251. // remove duplicate commands (last commands have high priority)
  21252. $tagstyle = preg_replace('/' . $cmd . '[^;]+/i', '', $tagstyle);
  21253. }
  21254. }
  21255. }
  21256. }
  21257. $tagstyle .= ';' . $style['c'];
  21258. }
  21259. // remove multiple semicolons
  21260. $tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
  21261. return $tagstyle;
  21262. }
  21263. /**
  21264. * Returns the border width from CSS property
  21265. * @param $width (string) border width
  21266. * @return int with in user units
  21267. * @protected
  21268. * @since 5.7.000 (2010-08-02)
  21269. */
  21270. protected function getCSSBorderWidth($width) {
  21271. if ($width == 'thin') {
  21272. $width = (2 / $this->k);
  21273. } elseif ($width == 'medium') {
  21274. $width = (4 / $this->k);
  21275. } elseif ($width == 'thick') {
  21276. $width = (6 / $this->k);
  21277. } else {
  21278. $width = $this->getHTMLUnitToUnits($width, 1, 'px', false);
  21279. }
  21280. return $width;
  21281. }
  21282. /**
  21283. * Returns the border dash style from CSS property
  21284. * @param $style (string) border style to convert
  21285. * @return int sash style (return -1 in case of none or hidden border)
  21286. * @protected
  21287. * @since 5.7.000 (2010-08-02)
  21288. */
  21289. protected function getCSSBorderDashStyle($style) {
  21290. switch (strtolower($style)) {
  21291. case 'none':
  21292. case 'hidden': {
  21293. $dash = -1;
  21294. break;
  21295. }
  21296. case 'dotted': {
  21297. $dash = 1;
  21298. break;
  21299. }
  21300. case 'dashed': {
  21301. $dash = 3;
  21302. break;
  21303. }
  21304. case 'double':
  21305. case 'groove':
  21306. case 'ridge':
  21307. case 'inset':
  21308. case 'outset':
  21309. case 'solid':
  21310. default: {
  21311. $dash = 0;
  21312. break;
  21313. }
  21314. }
  21315. return $dash;
  21316. }
  21317. /**
  21318. * Returns the border style array from CSS border properties
  21319. * @param $cssborder (string) border properties
  21320. * @return array containing border properties
  21321. * @protected
  21322. * @since 5.7.000 (2010-08-02)
  21323. */
  21324. protected function getCSSBorderStyle($cssborder) {
  21325. $bprop = preg_split('/[\s]+/', trim($cssborder));
  21326. $border = array(); // value to be returned
  21327. switch (count($bprop)) {
  21328. case 3: {
  21329. $width = $bprop[0];
  21330. $style = $bprop[1];
  21331. $color = $bprop[2];
  21332. break;
  21333. }
  21334. case 2: {
  21335. $width = 'medium';
  21336. $style = $bprop[0];
  21337. $color = $bprop[1];
  21338. break;
  21339. }
  21340. case 1: {
  21341. $width = 'medium';
  21342. $style = $bprop[0];
  21343. $color = 'black';
  21344. break;
  21345. }
  21346. default: {
  21347. $width = 'medium';
  21348. $style = 'solid';
  21349. $color = 'black';
  21350. break;
  21351. }
  21352. }
  21353. if ($style == 'none') {
  21354. return array();
  21355. }
  21356. $border['cap'] = 'square';
  21357. $border['join'] = 'miter';
  21358. $border['dash'] = $this->getCSSBorderDashStyle($style);
  21359. if ($border['dash'] < 0) {
  21360. return array();
  21361. }
  21362. $border['width'] = $this->getCSSBorderWidth($width);
  21363. $border['color'] = $this->convertHTMLColorToDec($color);
  21364. return $border;
  21365. }
  21366. /**
  21367. * Get the internal Cell padding from CSS attribute.
  21368. * @param $csspadding (string) padding properties
  21369. * @param $width (float) width of the containing element
  21370. * @return array of cell paddings
  21371. * @public
  21372. * @since 5.9.000 (2010-10-04)
  21373. */
  21374. public function getCSSPadding($csspadding, $width = 0) {
  21375. $padding = preg_split('/[\s]+/', trim($csspadding));
  21376. $cell_padding = array(); // value to be returned
  21377. switch (count($padding)) {
  21378. case 4: {
  21379. $cell_padding['T'] = $padding[0];
  21380. $cell_padding['R'] = $padding[1];
  21381. $cell_padding['B'] = $padding[2];
  21382. $cell_padding['L'] = $padding[3];
  21383. break;
  21384. }
  21385. case 3: {
  21386. $cell_padding['T'] = $padding[0];
  21387. $cell_padding['R'] = $padding[1];
  21388. $cell_padding['B'] = $padding[2];
  21389. $cell_padding['L'] = $padding[1];
  21390. break;
  21391. }
  21392. case 2: {
  21393. $cell_padding['T'] = $padding[0];
  21394. $cell_padding['R'] = $padding[1];
  21395. $cell_padding['B'] = $padding[0];
  21396. $cell_padding['L'] = $padding[1];
  21397. break;
  21398. }
  21399. case 1: {
  21400. $cell_padding['T'] = $padding[0];
  21401. $cell_padding['R'] = $padding[0];
  21402. $cell_padding['B'] = $padding[0];
  21403. $cell_padding['L'] = $padding[0];
  21404. break;
  21405. }
  21406. default: {
  21407. return $this->cell_padding;
  21408. }
  21409. }
  21410. if ($width == 0) {
  21411. $width = $this->w - $this->lMargin - $this->rMargin;
  21412. }
  21413. $cell_padding['T'] = $this->getHTMLUnitToUnits($cell_padding['T'], $width, 'px', false);
  21414. $cell_padding['R'] = $this->getHTMLUnitToUnits($cell_padding['R'], $width, 'px', false);
  21415. $cell_padding['B'] = $this->getHTMLUnitToUnits($cell_padding['B'], $width, 'px', false);
  21416. $cell_padding['L'] = $this->getHTMLUnitToUnits($cell_padding['L'], $width, 'px', false);
  21417. return $cell_padding;
  21418. }
  21419. /**
  21420. * Get the internal Cell margin from CSS attribute.
  21421. * @param $cssmargin (string) margin properties
  21422. * @param $width (float) width of the containing element
  21423. * @return array of cell margins
  21424. * @public
  21425. * @since 5.9.000 (2010-10-04)
  21426. */
  21427. public function getCSSMargin($cssmargin, $width = 0) {
  21428. $margin = preg_split('/[\s]+/', trim($cssmargin));
  21429. $cell_margin = array(); // value to be returned
  21430. switch (count($margin)) {
  21431. case 4: {
  21432. $cell_margin['T'] = $margin[0];
  21433. $cell_margin['R'] = $margin[1];
  21434. $cell_margin['B'] = $margin[2];
  21435. $cell_margin['L'] = $margin[3];
  21436. break;
  21437. }
  21438. case 3: {
  21439. $cell_margin['T'] = $margin[0];
  21440. $cell_margin['R'] = $margin[1];
  21441. $cell_margin['B'] = $margin[2];
  21442. $cell_margin['L'] = $margin[1];
  21443. break;
  21444. }
  21445. case 2: {
  21446. $cell_margin['T'] = $margin[0];
  21447. $cell_margin['R'] = $margin[1];
  21448. $cell_margin['B'] = $margin[0];
  21449. $cell_margin['L'] = $margin[1];
  21450. break;
  21451. }
  21452. case 1: {
  21453. $cell_margin['T'] = $margin[0];
  21454. $cell_margin['R'] = $margin[0];
  21455. $cell_margin['B'] = $margin[0];
  21456. $cell_margin['L'] = $margin[0];
  21457. break;
  21458. }
  21459. default: {
  21460. return $this->cell_margin;
  21461. }
  21462. }
  21463. if ($width == 0) {
  21464. $width = $this->w - $this->lMargin - $this->rMargin;
  21465. }
  21466. $cell_margin['T'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['T']), $width, 'px', false);
  21467. $cell_margin['R'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['R']), $width, 'px', false);
  21468. $cell_margin['B'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['B']), $width, 'px', false);
  21469. $cell_margin['L'] = $this->getHTMLUnitToUnits(str_replace('auto', '0', $cell_margin['L']), $width, 'px', false);
  21470. return $cell_margin;
  21471. }
  21472. /**
  21473. * Get the border-spacing from CSS attribute.
  21474. * @param $cssbspace (string) border-spacing CSS properties
  21475. * @param $width (float) width of the containing element
  21476. * @return array of border spacings
  21477. * @public
  21478. * @since 5.9.010 (2010-10-27)
  21479. */
  21480. public function getCSSBorderMargin($cssbspace, $width = 0) {
  21481. $space = preg_split('/[\s]+/', trim($cssbspace));
  21482. $border_spacing = array(); // value to be returned
  21483. switch (count($space)) {
  21484. case 2: {
  21485. $border_spacing['H'] = $space[0];
  21486. $border_spacing['V'] = $space[1];
  21487. break;
  21488. }
  21489. case 1: {
  21490. $border_spacing['H'] = $space[0];
  21491. $border_spacing['V'] = $space[0];
  21492. break;
  21493. }
  21494. default: {
  21495. return array('H' => 0, 'V' => 0);
  21496. }
  21497. }
  21498. if ($width == 0) {
  21499. $width = $this->w - $this->lMargin - $this->rMargin;
  21500. }
  21501. $border_spacing['H'] = $this->getHTMLUnitToUnits($border_spacing['H'], $width, 'px', false);
  21502. $border_spacing['V'] = $this->getHTMLUnitToUnits($border_spacing['V'], $width, 'px', false);
  21503. return $border_spacing;
  21504. }
  21505. /**
  21506. * Returns the letter-spacing value from CSS value
  21507. * @param $spacing (string) letter-spacing value
  21508. * @param $parent (float) font spacing (tracking) value of the parent element
  21509. * @return float quantity to increases or decreases the space between characters in a text.
  21510. * @protected
  21511. * @since 5.9.000 (2010-10-02)
  21512. */
  21513. protected function getCSSFontSpacing($spacing, $parent = 0) {
  21514. $val = 0; // value to be returned
  21515. $spacing = trim($spacing);
  21516. switch ($spacing) {
  21517. case 'normal': {
  21518. $val = 0;
  21519. break;
  21520. }
  21521. case 'inherit': {
  21522. if ($parent == 'normal') {
  21523. $val = 0;
  21524. } else {
  21525. $val = $parent;
  21526. }
  21527. break;
  21528. }
  21529. default: {
  21530. $val = $this->getHTMLUnitToUnits($spacing, 0, 'px', false);
  21531. }
  21532. }
  21533. return $val;
  21534. }
  21535. /**
  21536. * Returns the percentage of font stretching from CSS value
  21537. * @param $stretch (string) stretch mode
  21538. * @param $parent (float) stretch value of the parent element
  21539. * @return float font stretching percentage
  21540. * @protected
  21541. * @since 5.9.000 (2010-10-02)
  21542. */
  21543. protected function getCSSFontStretching($stretch, $parent = 100) {
  21544. $val = 100; // value to be returned
  21545. $stretch = trim($stretch);
  21546. switch ($stretch) {
  21547. case 'ultra-condensed': {
  21548. $val = 40;
  21549. break;
  21550. }
  21551. case 'extra-condensed': {
  21552. $val = 55;
  21553. break;
  21554. }
  21555. case 'condensed': {
  21556. $val = 70;
  21557. break;
  21558. }
  21559. case 'semi-condensed': {
  21560. $val = 85;
  21561. break;
  21562. }
  21563. case 'normal': {
  21564. $val = 100;
  21565. break;
  21566. }
  21567. case 'semi-expanded': {
  21568. $val = 115;
  21569. break;
  21570. }
  21571. case 'expanded': {
  21572. $val = 130;
  21573. break;
  21574. }
  21575. case 'extra-expanded': {
  21576. $val = 145;
  21577. break;
  21578. }
  21579. case 'ultra-expanded': {
  21580. $val = 160;
  21581. break;
  21582. }
  21583. case 'wider': {
  21584. $val = $parent + 10;
  21585. break;
  21586. }
  21587. case 'narrower': {
  21588. $val = $parent - 10;
  21589. break;
  21590. }
  21591. case 'inherit': {
  21592. if ($parent == 'normal') {
  21593. $val = 100;
  21594. } else {
  21595. $val = $parent;
  21596. }
  21597. break;
  21598. }
  21599. default: {
  21600. $val = $this->getHTMLUnitToUnits($stretch, 100, '%', false);
  21601. }
  21602. }
  21603. return $val;
  21604. }
  21605. /**
  21606. * Returns the HTML DOM array.
  21607. * @param $html (string) html code
  21608. * @return array
  21609. * @protected
  21610. * @since 3.2.000 (2008-06-20)
  21611. */
  21612. protected function getHtmlDomArray($html) {
  21613. // array of CSS styles ( selector => properties).
  21614. $css = array();
  21615. // get CSS array defined at previous call
  21616. $matches = array();
  21617. if (preg_match_all('/<cssarray>([^\<]*)<\/cssarray>/isU', $html, $matches) > 0) {
  21618. if (isset($matches[1][0])) {
  21619. $css = array_merge($css, unserialize($this->unhtmlentities($matches[1][0])));
  21620. }
  21621. $html = preg_replace('/<cssarray>(.*?)<\/cssarray>/isU', '', $html);
  21622. }
  21623. // extract external CSS files
  21624. $matches = array();
  21625. if (preg_match_all('/<link([^\>]*)>/isU', $html, $matches) > 0) {
  21626. foreach ($matches[1] as $key => $link) {
  21627. $type = array();
  21628. if (preg_match('/type[\s]*=[\s]*"text\/css"/', $link, $type)) {
  21629. $type = array();
  21630. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $link, $type);
  21631. // get 'all' and 'print' media, other media types are discarded
  21632. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  21633. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  21634. $type = array();
  21635. if (preg_match('/href[\s]*=[\s]*"([^"]*)"/', $link, $type) > 0) {
  21636. // read CSS data file
  21637. $cssdata = file_get_contents(trim($type[1]));
  21638. $css = array_merge($css, $this->extractCSSproperties($cssdata));
  21639. }
  21640. }
  21641. }
  21642. }
  21643. }
  21644. // extract style tags
  21645. $matches = array();
  21646. if (preg_match_all('/<style([^\>]*)>([^\<]*)<\/style>/isU', $html, $matches) > 0) {
  21647. foreach ($matches[1] as $key => $media) {
  21648. $type = array();
  21649. preg_match('/media[\s]*=[\s]*"([^"]*)"/', $media, $type);
  21650. // get 'all' and 'print' media, other media types are discarded
  21651. // (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  21652. if (empty($type) OR (isset($type[1]) AND (($type[1] == 'all') OR ($type[1] == 'print')))) {
  21653. $cssdata = $matches[2][$key];
  21654. $css = array_merge($css, $this->extractCSSproperties($cssdata));
  21655. }
  21656. }
  21657. }
  21658. // create a special tag to contain the CSS array (used for table content)
  21659. $csstagarray = '<cssarray>' . htmlentities(serialize($css)) . '</cssarray>';
  21660. // remove head and style blocks
  21661. $html = preg_replace('/<head([^\>]*)>(.*?)<\/head>/siU', '', $html);
  21662. $html = preg_replace('/<style([^\>]*)>([^\<]*)<\/style>/isU', '', $html);
  21663. // define block tags
  21664. $blocktags = array('blockquote', 'br', 'dd', 'dl', 'div', 'dt', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'li', 'ol', 'p', 'pre', 'ul', 'tcpdf', 'table', 'tr', 'td');
  21665. // define self-closing tags
  21666. $selfclosingtags = array('area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta');
  21667. // remove all unsupported tags (the line below lists all supported tags)
  21668. $html = strip_tags($html, '<marker/><a><b><blockquote><body><br><br/><dd><del><div><dl><dt><em><font><form><h1><h2><h3><h4><h5><h6><hr><hr/><i><img><input><label><li><ol><option><p><pre><s><select><small><span><strike><strong><sub><sup><table><tablehead><tcpdf><td><textarea><th><thead><tr><tt><u><ul>');
  21669. //replace some blank characters
  21670. $html = preg_replace('/<pre/', '<xre', $html); // preserve pre tag
  21671. $html = preg_replace('/<(table|tr|td|th|tcpdf|blockquote|dd|div|dl|dt|form|h1|h2|h3|h4|h5|h6|br|hr|li|ol|ul|p)([^\>]*)>[\n\r\t]+/', '<\\1\\2>', $html);
  21672. $html = preg_replace('@(\r\n|\r)@', "\n", $html);
  21673. $repTable = array("\t" => ' ', "\0" => ' ', "\x0B" => ' ', "\\" => "\\\\");
  21674. $html = strtr($html, $repTable);
  21675. $offset = 0;
  21676. while (($offset < strlen($html)) AND ($pos = strpos($html, '</pre>', $offset)) !== false) {
  21677. $html_a = substr($html, 0, $offset);
  21678. $html_b = substr($html, $offset, ($pos - $offset + 6));
  21679. while (preg_match("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", $html_b)) {
  21680. // preserve newlines on <pre> tag
  21681. $html_b = preg_replace("'<xre([^\>]*)>(.*?)\n(.*?)</pre>'si", "<xre\\1>\\2<br />\\3</pre>", $html_b);
  21682. }
  21683. while (preg_match("'<xre([^\>]*)>(.*?)" . $this->re_space['p'] . "(.*?)</pre>'" . $this->re_space['m'], $html_b)) {
  21684. // preserve spaces on <pre> tag
  21685. $html_b = preg_replace("'<xre([^\>]*)>(.*?)" . $this->re_space['p'] . "(.*?)</pre>'" . $this->re_space['m'], "<xre\\1>\\2&nbsp;\\3</pre>", $html_b);
  21686. }
  21687. $html = $html_a . $html_b . substr($html, $pos + 6);
  21688. $offset = strlen($html_a . $html_b);
  21689. }
  21690. $offset = 0;
  21691. while (($offset < strlen($html)) AND ($pos = strpos($html, '</textarea>', $offset)) !== false) {
  21692. $html_a = substr($html, 0, $offset);
  21693. $html_b = substr($html, $offset, ($pos - $offset + 11));
  21694. while (preg_match("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", $html_b)) {
  21695. // preserve newlines on <textarea> tag
  21696. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)\n(.*?)</textarea>'si", "<textarea\\1>\\2<TBR>\\3</textarea>", $html_b);
  21697. $html_b = preg_replace("'<textarea([^\>]*)>(.*?)[\"](.*?)</textarea>'si", "<textarea\\1>\\2''\\3</textarea>", $html_b);
  21698. }
  21699. $html = $html_a . $html_b . substr($html, $pos + 11);
  21700. $offset = strlen($html_a . $html_b);
  21701. }
  21702. $html = preg_replace('/([\s]*)<option/si', '<option', $html);
  21703. $html = preg_replace('/<\/option>([\s]*)/si', '</option>', $html);
  21704. $offset = 0;
  21705. while (($offset < strlen($html)) AND ($pos = strpos($html, '</option>', $offset)) !== false) {
  21706. $html_a = substr($html, 0, $offset);
  21707. $html_b = substr($html, $offset, ($pos - $offset + 9));
  21708. while (preg_match("'<option([^\>]*)>(.*?)</option>'si", $html_b)) {
  21709. $html_b = preg_replace("'<option([\s]+)value=\"([^\"]*)\"([^\>]*)>(.*?)</option>'si", "\\2#!TaB!#\\4#!NwL!#", $html_b);
  21710. $html_b = preg_replace("'<option([^\>]*)>(.*?)</option>'si", "\\2#!NwL!#", $html_b);
  21711. }
  21712. $html = $html_a . $html_b . substr($html, $pos + 9);
  21713. $offset = strlen($html_a . $html_b);
  21714. }
  21715. if (preg_match("'</select'si", $html)) {
  21716. $html = preg_replace("'<select([^\>]*)>'si", "<select\\1 opt=\"", $html);
  21717. $html = preg_replace("'#!NwL!#</select>'si", "\" />", $html);
  21718. }
  21719. $html = str_replace("\n", ' ', $html);
  21720. // restore textarea newlines
  21721. $html = str_replace('<TBR>', "\n", $html);
  21722. // remove extra spaces from code
  21723. $html = preg_replace('/[\s]+<\/(table|tr|ul|ol|dl)>/', '</\\1>', $html);
  21724. $html = preg_replace('/' . $this->re_space['p'] . '+<\/(td|th|li|dt|dd)>/' . $this->re_space['m'], '</\\1>', $html);
  21725. $html = preg_replace('/[\s]+<(tr|td|th|li|dt|dd)/', '<\\1', $html);
  21726. $html = preg_replace('/' . $this->re_space['p'] . '+<(ul|ol|dl|br)/' . $this->re_space['m'], '<\\1', $html);
  21727. $html = preg_replace('/<\/(table|tr|td|th|blockquote|dd|dt|dl|div|dt|h1|h2|h3|h4|h5|h6|hr|li|ol|ul|p)>[\s]+</', '</\\1><', $html);
  21728. $html = preg_replace('/<\/(td|th)>/', '<marker style="font-size:0"/></\\1>', $html);
  21729. $html = preg_replace('/<\/table>([\s]*)<marker style="font-size:0"\/>/', '</table>', $html);
  21730. $html = preg_replace('/' . $this->re_space['p'] . '+<img/' . $this->re_space['m'], chr(32) . '<img', $html);
  21731. $html = preg_replace('/<img([^\>]*)>[\s]+([^\<])/xi', '<img\\1>&nbsp;\\2', $html);
  21732. $html = preg_replace('/<img([^\>]*)>/xi', '<img\\1><span><marker style="font-size:0"/></span>', $html);
  21733. $html = preg_replace('/<xre/', '<pre', $html); // restore pre tag
  21734. $html = preg_replace('/<textarea([^\>]*)>([^\<]*)<\/textarea>/xi', '<textarea\\1 value="\\2" />', $html);
  21735. $html = preg_replace('/<li([^\>]*)><\/li>/', '<li\\1>&nbsp;</li>', $html);
  21736. $html = preg_replace('/<li([^\>]*)>' . $this->re_space['p'] . '*<img/' . $this->re_space['m'], '<li\\1><font size="1">&nbsp;</font><img', $html);
  21737. $html = preg_replace('/<([^\>\/]*)>[\s]/', '<\\1>&nbsp;', $html); // preserve some spaces
  21738. $html = preg_replace('/[\s]<\/([^\>]*)>/', '&nbsp;</\\1>', $html); // preserve some spaces
  21739. $html = preg_replace('/<su([bp])/', '<zws/><su\\1', $html); // fix sub/sup alignment
  21740. $html = preg_replace('/<\/su([bp])>/', '</su\\1><zws/>', $html); // fix sub/sup alignment
  21741. $html = preg_replace('/' . $this->re_space['p'] . '+/' . $this->re_space['m'], chr(32), $html); // replace multiple spaces with a single space
  21742. // trim string
  21743. $html = $this->stringTrim($html);
  21744. // fix first image tag alignment
  21745. $html = preg_replace('/^<img/', '<span style="font-size:0"><br /></span> <img', $html, 1);
  21746. // pattern for generic tag
  21747. $tagpattern = '/(<[^>]+>)/';
  21748. // explodes the string
  21749. $a = preg_split($tagpattern, $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  21750. // count elements
  21751. $maxel = count($a);
  21752. $elkey = 0;
  21753. $key = 0;
  21754. // create an array of elements
  21755. $dom = array();
  21756. $dom[$key] = array();
  21757. // set inheritable properties fot the first void element
  21758. // possible inheritable properties are: azimuth, border-collapse, border-spacing, caption-side, color, cursor, direction, empty-cells, font, font-family, font-stretch, font-size, font-size-adjust, font-style, font-variant, font-weight, letter-spacing, line-height, list-style, list-style-image, list-style-position, list-style-type, orphans, page, page-break-inside, quotes, speak, speak-header, text-align, text-indent, text-transform, volume, white-space, widows, word-spacing
  21759. $dom[$key]['tag'] = false;
  21760. $dom[$key]['block'] = false;
  21761. $dom[$key]['value'] = '';
  21762. $dom[$key]['parent'] = 0;
  21763. $dom[$key]['hide'] = false;
  21764. $dom[$key]['fontname'] = $this->FontFamily;
  21765. $dom[$key]['fontstyle'] = $this->FontStyle;
  21766. $dom[$key]['fontsize'] = $this->FontSizePt;
  21767. $dom[$key]['font-stretch'] = $this->font_stretching;
  21768. $dom[$key]['letter-spacing'] = $this->font_spacing;
  21769. $dom[$key]['stroke'] = $this->textstrokewidth;
  21770. $dom[$key]['fill'] = (($this->textrendermode % 2) == 0);
  21771. $dom[$key]['clip'] = ($this->textrendermode > 3);
  21772. $dom[$key]['line-height'] = $this->cell_height_ratio;
  21773. $dom[$key]['bgcolor'] = false;
  21774. $dom[$key]['fgcolor'] = $this->fgcolor; // color
  21775. $dom[$key]['strokecolor'] = $this->strokecolor;
  21776. $dom[$key]['align'] = '';
  21777. $dom[$key]['listtype'] = '';
  21778. $dom[$key]['text-indent'] = 0;
  21779. $dom[$key]['border'] = array();
  21780. $dom[$key]['dir'] = $this->rtl ? 'rtl' : 'ltr';
  21781. $thead = false; // true when we are inside the THEAD tag
  21782. ++$key;
  21783. $level = array();
  21784. array_push($level, 0); // root
  21785. while ($elkey < $maxel) {
  21786. $dom[$key] = array();
  21787. $element = $a[$elkey];
  21788. $dom[$key]['elkey'] = $elkey;
  21789. if (preg_match($tagpattern, $element)) {
  21790. // html tag
  21791. $element = substr($element, 1, -1);
  21792. // get tag name
  21793. preg_match('/[\/]?([a-zA-Z0-9]*)/', $element, $tag);
  21794. $tagname = strtolower($tag[1]);
  21795. // check if we are inside a table header
  21796. if ($tagname == 'thead') {
  21797. if ($element{0} == '/') {
  21798. $thead = false;
  21799. } else {
  21800. $thead = true;
  21801. }
  21802. ++$elkey;
  21803. continue;
  21804. }
  21805. $dom[$key]['tag'] = true;
  21806. $dom[$key]['value'] = $tagname;
  21807. if (in_array($dom[$key]['value'], $blocktags)) {
  21808. $dom[$key]['block'] = true;
  21809. } else {
  21810. $dom[$key]['block'] = false;
  21811. }
  21812. if ($element{0} == '/') {
  21813. // *** closing html tag
  21814. $dom[$key]['opening'] = false;
  21815. $dom[$key]['parent'] = end($level);
  21816. array_pop($level);
  21817. $dom[$key]['hide'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['hide'];
  21818. $dom[$key]['fontname'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontname'];
  21819. $dom[$key]['fontstyle'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontstyle'];
  21820. $dom[$key]['fontsize'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fontsize'];
  21821. $dom[$key]['font-stretch'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['font-stretch'];
  21822. $dom[$key]['letter-spacing'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['letter-spacing'];
  21823. $dom[$key]['stroke'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['stroke'];
  21824. $dom[$key]['fill'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fill'];
  21825. $dom[$key]['clip'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['clip'];
  21826. $dom[$key]['line-height'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['line-height'];
  21827. $dom[$key]['bgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['bgcolor'];
  21828. $dom[$key]['fgcolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['fgcolor'];
  21829. $dom[$key]['strokecolor'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['strokecolor'];
  21830. $dom[$key]['align'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['align'];
  21831. $dom[$key]['dir'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['dir'];
  21832. if (isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'])) {
  21833. $dom[$key]['listtype'] = $dom[($dom[($dom[$key]['parent'])]['parent'])]['listtype'];
  21834. }
  21835. // set the number of columns in table tag
  21836. if (($dom[$key]['value'] == 'tr') AND (!isset($dom[($dom[($dom[$key]['parent'])]['parent'])]['cols']))) {
  21837. $dom[($dom[($dom[$key]['parent'])]['parent'])]['cols'] = $dom[($dom[$key]['parent'])]['cols'];
  21838. }
  21839. if (($dom[$key]['value'] == 'td') OR ($dom[$key]['value'] == 'th')) {
  21840. $dom[($dom[$key]['parent'])]['content'] = $csstagarray;
  21841. for ($i = ($dom[$key]['parent'] + 1); $i < $key; ++$i) {
  21842. $dom[($dom[$key]['parent'])]['content'] .= $a[$dom[$i]['elkey']];
  21843. }
  21844. $key = $i;
  21845. // mark nested tables
  21846. $dom[($dom[$key]['parent'])]['content'] = str_replace('<table', '<table nested="true"', $dom[($dom[$key]['parent'])]['content']);
  21847. // remove thead sections from nested tables
  21848. $dom[($dom[$key]['parent'])]['content'] = str_replace('<thead>', '', $dom[($dom[$key]['parent'])]['content']);
  21849. $dom[($dom[$key]['parent'])]['content'] = str_replace('</thead>', '', $dom[($dom[$key]['parent'])]['content']);
  21850. }
  21851. // store header rows on a new table
  21852. if (($dom[$key]['value'] == 'tr') AND ($dom[($dom[$key]['parent'])]['thead'] === true)) {
  21853. if ($this->empty_string($dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'])) {
  21854. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] = $csstagarray . $a[$dom[($dom[($dom[$key]['parent'])]['parent'])]['elkey']];
  21855. }
  21856. for ($i = $dom[$key]['parent']; $i <= $key; ++$i) {
  21857. $dom[($dom[($dom[$key]['parent'])]['parent'])]['thead'] .= $a[$dom[$i]['elkey']];
  21858. }
  21859. if (!isset($dom[($dom[$key]['parent'])]['attribute'])) {
  21860. $dom[($dom[$key]['parent'])]['attribute'] = array();
  21861. }
  21862. // header elements must be always contained in a single page
  21863. $dom[($dom[$key]['parent'])]['attribute']['nobr'] = 'true';
  21864. }
  21865. if (($dom[$key]['value'] == 'table') AND (!$this->empty_string($dom[($dom[$key]['parent'])]['thead']))) {
  21866. // remove the nobr attributes from the table header
  21867. $dom[($dom[$key]['parent'])]['thead'] = str_replace(' nobr="true"', '', $dom[($dom[$key]['parent'])]['thead']);
  21868. $dom[($dom[$key]['parent'])]['thead'] .= '</tablehead>';
  21869. }
  21870. } else {
  21871. // *** opening or self-closing html tag
  21872. $dom[$key]['opening'] = true;
  21873. $dom[$key]['parent'] = end($level);
  21874. if ((substr($element, -1, 1) == '/') OR (in_array($dom[$key]['value'], $selfclosingtags))) {
  21875. // self-closing tag
  21876. $dom[$key]['self'] = true;
  21877. } else {
  21878. // opening tag
  21879. array_push($level, $key);
  21880. $dom[$key]['self'] = false;
  21881. }
  21882. // copy some values from parent
  21883. $parentkey = 0;
  21884. if ($key > 0) {
  21885. $parentkey = $dom[$key]['parent'];
  21886. $dom[$key]['hide'] = $dom[$parentkey]['hide'];
  21887. $dom[$key]['fontname'] = $dom[$parentkey]['fontname'];
  21888. $dom[$key]['fontstyle'] = $dom[$parentkey]['fontstyle'];
  21889. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'];
  21890. $dom[$key]['font-stretch'] = $dom[$parentkey]['font-stretch'];
  21891. $dom[$key]['letter-spacing'] = $dom[$parentkey]['letter-spacing'];
  21892. $dom[$key]['stroke'] = $dom[$parentkey]['stroke'];
  21893. $dom[$key]['fill'] = $dom[$parentkey]['fill'];
  21894. $dom[$key]['clip'] = $dom[$parentkey]['clip'];
  21895. $dom[$key]['line-height'] = $dom[$parentkey]['line-height'];
  21896. $dom[$key]['bgcolor'] = $dom[$parentkey]['bgcolor'];
  21897. $dom[$key]['fgcolor'] = $dom[$parentkey]['fgcolor'];
  21898. $dom[$key]['strokecolor'] = $dom[$parentkey]['strokecolor'];
  21899. $dom[$key]['align'] = $dom[$parentkey]['align'];
  21900. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  21901. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  21902. $dom[$key]['border'] = array();
  21903. $dom[$key]['dir'] = $dom[$parentkey]['dir'];
  21904. }
  21905. // get attributes
  21906. preg_match_all('/([^=\s]*)[\s]*=[\s]*"([^"]*)"/', $element, $attr_array, PREG_PATTERN_ORDER);
  21907. $dom[$key]['attribute'] = array(); // reset attribute array
  21908. while (list($id, $name) = each($attr_array[1])) {
  21909. $dom[$key]['attribute'][strtolower($name)] = $attr_array[2][$id];
  21910. }
  21911. if (!empty($css)) {
  21912. // merge CSS style to current style
  21913. list($dom[$key]['csssel'], $dom[$key]['cssdata']) = $this->getCSSdataArray($dom, $key, $css);
  21914. $dom[$key]['attribute']['style'] = $this->getTagStyleFromCSSarray($dom[$key]['cssdata']);
  21915. }
  21916. // split style attributes
  21917. if (isset($dom[$key]['attribute']['style']) AND !empty($dom[$key]['attribute']['style'])) {
  21918. // get style attributes
  21919. preg_match_all('/([^;:\s]*):([^;]*)/', $dom[$key]['attribute']['style'], $style_array, PREG_PATTERN_ORDER);
  21920. $dom[$key]['style'] = array(); // reset style attribute array
  21921. while (list($id, $name) = each($style_array[1])) {
  21922. // in case of duplicate attribute the last replace the previous
  21923. $dom[$key]['style'][strtolower($name)] = trim($style_array[2][$id]);
  21924. }
  21925. // --- get some style attributes ---
  21926. // text direction
  21927. if (isset($dom[$key]['style']['direction'])) {
  21928. $dom[$key]['dir'] = $dom[$key]['style']['direction'];
  21929. }
  21930. // display
  21931. if (isset($dom[$key]['style']['display'])) {
  21932. $dom[$key]['hide'] = (trim(strtolower($dom[$key]['style']['display'])) == 'none');
  21933. }
  21934. // font family
  21935. if (isset($dom[$key]['style']['font-family'])) {
  21936. $dom[$key]['fontname'] = $this->getFontFamilyName($dom[$key]['style']['font-family']);
  21937. }
  21938. // list-style-type
  21939. if (isset($dom[$key]['style']['list-style-type'])) {
  21940. $dom[$key]['listtype'] = trim(strtolower($dom[$key]['style']['list-style-type']));
  21941. if ($dom[$key]['listtype'] == 'inherit') {
  21942. $dom[$key]['listtype'] = $dom[$parentkey]['listtype'];
  21943. }
  21944. }
  21945. // text-indent
  21946. if (isset($dom[$key]['style']['text-indent'])) {
  21947. $dom[$key]['text-indent'] = $this->getHTMLUnitToUnits($dom[$key]['style']['text-indent']);
  21948. if ($dom[$key]['text-indent'] == 'inherit') {
  21949. $dom[$key]['text-indent'] = $dom[$parentkey]['text-indent'];
  21950. }
  21951. }
  21952. // font size
  21953. if (isset($dom[$key]['style']['font-size'])) {
  21954. $fsize = trim($dom[$key]['style']['font-size']);
  21955. switch ($fsize) {
  21956. // absolute-size
  21957. case 'xx-small': {
  21958. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 4;
  21959. break;
  21960. }
  21961. case 'x-small': {
  21962. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 3;
  21963. break;
  21964. }
  21965. case 'small': {
  21966. $dom[$key]['fontsize'] = $dom[0]['fontsize'] - 2;
  21967. break;
  21968. }
  21969. case 'medium': {
  21970. $dom[$key]['fontsize'] = $dom[0]['fontsize'];
  21971. break;
  21972. }
  21973. case 'large': {
  21974. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 2;
  21975. break;
  21976. }
  21977. case 'x-large': {
  21978. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 4;
  21979. break;
  21980. }
  21981. case 'xx-large': {
  21982. $dom[$key]['fontsize'] = $dom[0]['fontsize'] + 6;
  21983. break;
  21984. }
  21985. // relative-size
  21986. case 'smaller': {
  21987. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] - 3;
  21988. break;
  21989. }
  21990. case 'larger': {
  21991. $dom[$key]['fontsize'] = $dom[$parentkey]['fontsize'] + 3;
  21992. break;
  21993. }
  21994. default: {
  21995. $dom[$key]['fontsize'] = $this->getHTMLUnitToUnits($fsize, $dom[$parentkey]['fontsize'], 'pt', true);
  21996. }
  21997. }
  21998. }
  21999. // font-stretch
  22000. if (isset($dom[$key]['style']['font-stretch'])) {
  22001. $dom[$key]['font-stretch'] = $this->getCSSFontStretching($dom[$key]['style']['font-stretch'], $dom[$parentkey]['font-stretch']);
  22002. }
  22003. // letter-spacing
  22004. if (isset($dom[$key]['style']['letter-spacing