PageRenderTime 68ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 3ms

/tools/tcpdf/tcpdf.php

https://bitbucket.org/enurkov/prestashop
PHP | 13950 lines | 8767 code | 497 blank | 4686 comment | 1678 complexity | 7aa19c022eb2a4c7b2b08d21381d1762 MD5 | raw file
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf.php
  4. // Version : 5.9.179
  5. // Begin : 2002-08-03
  6. // Last Update : 2012-08-04
  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-2012 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. // Anyone else that has reported a bug or sent a suggestion.
  102. //============================================================+
  103. /**
  104. * @file
  105. * This is a PHP class for generating PDF documents without requiring external extensions.<br>
  106. * 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>
  107. * <h3>TCPDF main features are:</h3>
  108. * <ul>
  109. * <li>no external libraries are required for the basic functions;</li>
  110. * <li>all standard page formats, custom page formats, custom margins and units of measure;</li>
  111. * <li>UTF-8 Unicode and Right-To-Left languages;</li>
  112. * <li>TrueTypeUnicode, TrueType, Type1 and CID-0 fonts;</li>
  113. * <li>font subsetting;</li>
  114. * <li>methods to publish some XHTML + CSS code, Javascript and Forms;</li>
  115. * <li>images, graphic (geometric figures) and transformation methods;
  116. * <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>
  117. * <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>
  118. * <li>JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors and Transparencies;</li>
  119. * <li>automatic page header and footer management;</li>
  120. * <li>document encryption up to 256 bit and digital signature certifications;</li>
  121. * <li>transactions to UNDO commands;</li>
  122. * <li>PDF annotations, including links, text and file attachments;</li>
  123. * <li>text rendering modes (fill, stroke and clipping);</li>
  124. * <li>multiple columns mode;</li>
  125. * <li>no-write page regions;</li>
  126. * <li>bookmarks, named destinations and table of content;</li>
  127. * <li>text hyphenation;</li>
  128. * <li>text stretching and spacing (tracking);</li>
  129. * <li>automatic page break, line break and text alignments including justification;</li>
  130. * <li>automatic page numbering and page groups;</li>
  131. * <li>move and delete pages;</li>
  132. * <li>page compression (requires php-zlib extension);</li>
  133. * <li>XOBject Templates;</li>
  134. * <li>Layers and object visibility;</li>
  135. * <li>PDF/A-1b support.</li>
  136. * </ul>
  137. * Tools to encode your unicode fonts are on fonts/utils directory.</p>
  138. * @package com.tecnick.tcpdf
  139. * @author Nicola Asuni
  140. * @version 5.9.179
  141. */
  142. // Main configuration file. Define the K_TCPDF_EXTERNAL_CONFIG constant to skip this file.
  143. require_once(dirname(__FILE__).'/config/tcpdf_config.php');
  144. /**
  145. * @class TCPDF
  146. * PHP class for generating PDF documents without requiring external extensions.
  147. * 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>
  148. * @package com.tecnick.tcpdf
  149. * @brief PHP class for generating PDF documents without requiring external extensions.
  150. * @version 5.9.179
  151. * @author Nicola Asuni - info@tecnick.com
  152. */
  153. class TCPDF {
  154. // private properties
  155. /**
  156. * Current TCPDF version.
  157. * @private
  158. */
  159. private $tcpdf_version = '5.9.179';
  160. // Protected properties
  161. /**
  162. * Current page number.
  163. * @protected
  164. */
  165. protected $page;
  166. /**
  167. * Current object number.
  168. * @protected
  169. */
  170. protected $n;
  171. /**
  172. * Array of object offsets.
  173. * @protected
  174. */
  175. protected $offsets = array();
  176. /**
  177. * Array of object IDs for each page.
  178. * @protected
  179. */
  180. protected $pageobjects = array();
  181. /**
  182. * Buffer holding in-memory PDF.
  183. * @protected
  184. */
  185. protected $buffer;
  186. /**
  187. * Array containing pages.
  188. * @protected
  189. */
  190. protected $pages = array();
  191. /**
  192. * Current document state.
  193. * @protected
  194. */
  195. protected $state;
  196. /**
  197. * Compression flag.
  198. * @protected
  199. */
  200. protected $compress;
  201. /**
  202. * Current page orientation (P = Portrait, L = Landscape).
  203. * @protected
  204. */
  205. protected $CurOrientation;
  206. /**
  207. * Page dimensions.
  208. * @protected
  209. */
  210. protected $pagedim = array();
  211. /**
  212. * Scale factor (number of points in user unit).
  213. * @protected
  214. */
  215. protected $k;
  216. /**
  217. * Width of page format in points.
  218. * @protected
  219. */
  220. protected $fwPt;
  221. /**
  222. * Height of page format in points.
  223. * @protected
  224. */
  225. protected $fhPt;
  226. /**
  227. * Current width of page in points.
  228. * @protected
  229. */
  230. protected $wPt;
  231. /**
  232. * Current height of page in points.
  233. * @protected
  234. */
  235. protected $hPt;
  236. /**
  237. * Current width of page in user unit.
  238. * @protected
  239. */
  240. protected $w;
  241. /**
  242. * Current height of page in user unit.
  243. * @protected
  244. */
  245. protected $h;
  246. /**
  247. * Left margin.
  248. * @protected
  249. */
  250. protected $lMargin;
  251. /**
  252. * Top margin.
  253. * @protected
  254. */
  255. protected $tMargin;
  256. /**
  257. * Right margin.
  258. * @protected
  259. */
  260. protected $rMargin;
  261. /**
  262. * Page break margin.
  263. * @protected
  264. */
  265. protected $bMargin;
  266. /**
  267. * Array of cell internal paddings ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  268. * @since 5.9.000 (2010-10-03)
  269. * @protected
  270. */
  271. protected $cell_padding = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  272. /**
  273. * Array of cell margins ('T' => top, 'R' => right, 'B' => bottom, 'L' => left).
  274. * @since 5.9.000 (2010-10-04)
  275. * @protected
  276. */
  277. protected $cell_margin = array('T' => 0, 'R' => 0, 'B' => 0, 'L' => 0);
  278. /**
  279. * Current horizontal position in user unit for cell positioning.
  280. * @protected
  281. */
  282. protected $x;
  283. /**
  284. * Current vertical position in user unit for cell positioning.
  285. * @protected
  286. */
  287. protected $y;
  288. /**
  289. * Height of last cell printed.
  290. * @protected
  291. */
  292. protected $lasth;
  293. /**
  294. * Line width in user unit.
  295. * @protected
  296. */
  297. protected $LineWidth;
  298. /**
  299. * Array of standard font names.
  300. * @protected
  301. */
  302. protected $CoreFonts;
  303. /**
  304. * Array of used fonts.
  305. * @protected
  306. */
  307. protected $fonts = array();
  308. /**
  309. * Array of font files.
  310. * @protected
  311. */
  312. protected $FontFiles = array();
  313. /**
  314. * Array of encoding differences.
  315. * @protected
  316. */
  317. protected $diffs = array();
  318. /**
  319. * Array of used images.
  320. * @protected
  321. */
  322. protected $images = array();
  323. /**
  324. * Array of Annotations in pages.
  325. * @protected
  326. */
  327. protected $PageAnnots = array();
  328. /**
  329. * Array of internal links.
  330. * @protected
  331. */
  332. protected $links = array();
  333. /**
  334. * Current font family.
  335. * @protected
  336. */
  337. protected $FontFamily;
  338. /**
  339. * Current font style.
  340. * @protected
  341. */
  342. protected $FontStyle;
  343. /**
  344. * Current font ascent (distance between font top and baseline).
  345. * @protected
  346. * @since 2.8.000 (2007-03-29)
  347. */
  348. protected $FontAscent;
  349. /**
  350. * Current font descent (distance between font bottom and baseline).
  351. * @protected
  352. * @since 2.8.000 (2007-03-29)
  353. */
  354. protected $FontDescent;
  355. /**
  356. * Underlining flag.
  357. * @protected
  358. */
  359. protected $underline;
  360. /**
  361. * Overlining flag.
  362. * @protected
  363. */
  364. protected $overline;
  365. /**
  366. * Current font info.
  367. * @protected
  368. */
  369. protected $CurrentFont;
  370. /**
  371. * Current font size in points.
  372. * @protected
  373. */
  374. protected $FontSizePt;
  375. /**
  376. * Current font size in user unit.
  377. * @protected
  378. */
  379. protected $FontSize;
  380. /**
  381. * Commands for drawing color.
  382. * @protected
  383. */
  384. protected $DrawColor;
  385. /**
  386. * Commands for filling color.
  387. * @protected
  388. */
  389. protected $FillColor;
  390. /**
  391. * Commands for text color.
  392. * @protected
  393. */
  394. protected $TextColor;
  395. /**
  396. * Indicates whether fill and text colors are different.
  397. * @protected
  398. */
  399. protected $ColorFlag;
  400. /**
  401. * Automatic page breaking.
  402. * @protected
  403. */
  404. protected $AutoPageBreak;
  405. /**
  406. * Threshold used to trigger page breaks.
  407. * @protected
  408. */
  409. protected $PageBreakTrigger;
  410. /**
  411. * Flag set when processing page header.
  412. * @protected
  413. */
  414. protected $InHeader = false;
  415. /**
  416. * Flag set when processing page footer.
  417. * @protected
  418. */
  419. protected $InFooter = false;
  420. /**
  421. * Zoom display mode.
  422. * @protected
  423. */
  424. protected $ZoomMode;
  425. /**
  426. * Layout display mode.
  427. * @protected
  428. */
  429. protected $LayoutMode;
  430. /**
  431. * If true set the document information dictionary in Unicode.
  432. * @protected
  433. */
  434. protected $docinfounicode = true;
  435. /**
  436. * Document title.
  437. * @protected
  438. */
  439. protected $title = '';
  440. /**
  441. * Document subject.
  442. * @protected
  443. */
  444. protected $subject = '';
  445. /**
  446. * Document author.
  447. * @protected
  448. */
  449. protected $author = '';
  450. /**
  451. * Document keywords.
  452. * @protected
  453. */
  454. protected $keywords = '';
  455. /**
  456. * Document creator.
  457. * @protected
  458. */
  459. protected $creator = '';
  460. /**
  461. * Starting page number.
  462. * @protected
  463. */
  464. protected $starting_page_number = 1;
  465. /**
  466. * String alias for total number of pages.
  467. * @protected
  468. */
  469. protected $alias_tot_pages = '{:ptp:}';
  470. /**
  471. * String alias for page number.
  472. * @protected
  473. */
  474. protected $alias_num_page = '{:pnp:}';
  475. /**
  476. * String alias for total number of pages in a single group.
  477. * @protected
  478. */
  479. protected $alias_group_tot_pages = '{:ptg:}';
  480. /**
  481. * String alias for group page number.
  482. * @protected
  483. */
  484. protected $alias_group_num_page = '{:png:}';
  485. /**
  486. * String alias for right shift compensation used to correctly align page numbers on the right.
  487. * @protected
  488. */
  489. protected $alias_right_shift = '{rsc:';
  490. /**
  491. * The right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image.
  492. * @since 2002-07-31
  493. * @author Nicola Asuni
  494. * @protected
  495. */
  496. protected $img_rb_x;
  497. /**
  498. * The right-bottom corner Y coordinate of last inserted image.
  499. * @since 2002-07-31
  500. * @author Nicola Asuni
  501. * @protected
  502. */
  503. protected $img_rb_y;
  504. /**
  505. * Adjusting factor to convert pixels to user units.
  506. * @since 2004-06-14
  507. * @author Nicola Asuni
  508. * @protected
  509. */
  510. protected $imgscale = 1;
  511. /**
  512. * Boolean flag set to true when the input text is unicode (require unicode fonts).
  513. * @since 2005-01-02
  514. * @author Nicola Asuni
  515. * @protected
  516. */
  517. protected $isunicode = false;
  518. /**
  519. * Object containing unicode data.
  520. * @since 5.9.004 (2010-10-18)
  521. * @author Nicola Asuni
  522. * @protected
  523. */
  524. protected $unicode;
  525. /**
  526. * Object containing font encoding maps.
  527. * @since 5.9.123 (2011-10-01)
  528. * @author Nicola Asuni
  529. * @protected
  530. */
  531. protected $encmaps;
  532. /**
  533. * PDF version.
  534. * @since 1.5.3
  535. * @protected
  536. */
  537. protected $PDFVersion = '1.7';
  538. /**
  539. * ID of the stored default header template (-1 = not set).
  540. * @protected
  541. */
  542. protected $header_xobjid = -1;
  543. /**
  544. * If true reset the Header Xobject template at each page
  545. * @protected
  546. */
  547. protected $header_xobj_autoreset = false;
  548. /**
  549. * Minimum distance between header and top page margin.
  550. * @protected
  551. */
  552. protected $header_margin;
  553. /**
  554. * Minimum distance between footer and bottom page margin.
  555. * @protected
  556. */
  557. protected $footer_margin;
  558. /**
  559. * Original left margin value.
  560. * @protected
  561. * @since 1.53.0.TC013
  562. */
  563. protected $original_lMargin;
  564. /**
  565. * Original right margin value.
  566. * @protected
  567. * @since 1.53.0.TC013
  568. */
  569. protected $original_rMargin;
  570. /**
  571. * Default font used on page header.
  572. * @protected
  573. */
  574. protected $header_font;
  575. /**
  576. * Default font used on page footer.
  577. * @protected
  578. */
  579. protected $footer_font;
  580. /**
  581. * Language templates.
  582. * @protected
  583. */
  584. protected $l;
  585. /**
  586. * Barcode to print on page footer (only if set).
  587. * @protected
  588. */
  589. protected $barcode = false;
  590. /**
  591. * Boolean flag to print/hide page header.
  592. * @protected
  593. */
  594. protected $print_header = true;
  595. /**
  596. * Boolean flag to print/hide page footer.
  597. * @protected
  598. */
  599. protected $print_footer = true;
  600. /**
  601. * Header image logo.
  602. * @protected
  603. */
  604. protected $header_logo = '';
  605. /**
  606. * Width of header image logo in user units.
  607. * @protected
  608. */
  609. protected $header_logo_width = 30;
  610. /**
  611. * Title to be printed on default page header.
  612. * @protected
  613. */
  614. protected $header_title = '';
  615. /**
  616. * String to pring on page header after title.
  617. * @protected
  618. */
  619. protected $header_string = '';
  620. /**
  621. * Color for header text (RGB array).
  622. * @since 5.9.174 (2012-07-25)
  623. * @protected
  624. */
  625. protected $header_text_color = array(0,0,0);
  626. /**
  627. * Color for header line (RGB array).
  628. * @since 5.9.174 (2012-07-25)
  629. * @protected
  630. */
  631. protected $header_line_color = array(0,0,0);
  632. /**
  633. * Color for footer text (RGB array).
  634. * @since 5.9.174 (2012-07-25)
  635. * @protected
  636. */
  637. protected $footer_text_color = array(0,0,0);
  638. /**
  639. * Color for footer line (RGB array).
  640. * @since 5.9.174 (2012-07-25)
  641. * @protected
  642. */
  643. protected $footer_line_color = array(0,0,0);
  644. /**
  645. * Text shadow data array.
  646. * @since 5.9.174 (2012-07-25)
  647. * @protected
  648. */
  649. protected $txtshadow = array('enabled'=>false, 'depth_w'=>0, 'depth_h'=>0, 'color'=>false, 'opacity'=>1, 'blend_mode'=>'Normal');
  650. /**
  651. * Default number of columns for html table.
  652. * @protected
  653. */
  654. protected $default_table_columns = 4;
  655. // variables for html parser
  656. /**
  657. * HTML PARSER: array to store current link and rendering styles.
  658. * @protected
  659. */
  660. protected $HREF = array();
  661. /**
  662. * List of available fonts on filesystem.
  663. * @protected
  664. */
  665. protected $fontlist = array();
  666. /**
  667. * Current foreground color.
  668. * @protected
  669. */
  670. protected $fgcolor;
  671. /**
  672. * HTML PARSER: array of boolean values, true in case of ordered list (OL), false otherwise.
  673. * @protected
  674. */
  675. protected $listordered = array();
  676. /**
  677. * HTML PARSER: array count list items on nested lists.
  678. * @protected
  679. */
  680. protected $listcount = array();
  681. /**
  682. * HTML PARSER: current list nesting level.
  683. * @protected
  684. */
  685. protected $listnum = 0;
  686. /**
  687. * HTML PARSER: indent amount for lists.
  688. * @protected
  689. */
  690. protected $listindent = 0;
  691. /**
  692. * HTML PARSER: current list indententation level.
  693. * @protected
  694. */
  695. protected $listindentlevel = 0;
  696. /**
  697. * Current background color.
  698. * @protected
  699. */
  700. protected $bgcolor;
  701. /**
  702. * Temporary font size in points.
  703. * @protected
  704. */
  705. protected $tempfontsize = 10;
  706. /**
  707. * Spacer string for LI tags.
  708. * @protected
  709. */
  710. protected $lispacer = '';
  711. /**
  712. * Default encoding.
  713. * @protected
  714. * @since 1.53.0.TC010
  715. */
  716. protected $encoding = 'UTF-8';
  717. /**
  718. * PHP internal encoding.
  719. * @protected
  720. * @since 1.53.0.TC016
  721. */
  722. protected $internal_encoding;
  723. /**
  724. * Boolean flag to indicate if the document language is Right-To-Left.
  725. * @protected
  726. * @since 2.0.000
  727. */
  728. protected $rtl = false;
  729. /**
  730. * Boolean flag used to force RTL or LTR string direction.
  731. * @protected
  732. * @since 2.0.000
  733. */
  734. protected $tmprtl = false;
  735. // --- Variables used for document encryption:
  736. /**
  737. * IBoolean flag indicating whether document is protected.
  738. * @protected
  739. * @since 2.0.000 (2008-01-02)
  740. */
  741. protected $encrypted;
  742. /**
  743. * Array containing encryption settings.
  744. * @protected
  745. * @since 5.0.005 (2010-05-11)
  746. */
  747. protected $encryptdata = array();
  748. /**
  749. * Last RC4 key encrypted (cached for optimisation).
  750. * @protected
  751. * @since 2.0.000 (2008-01-02)
  752. */
  753. protected $last_enc_key;
  754. /**
  755. * Last RC4 computed key.
  756. * @protected
  757. * @since 2.0.000 (2008-01-02)
  758. */
  759. protected $last_enc_key_c;
  760. /**
  761. * Encryption padding string.
  762. * @protected
  763. */
  764. 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";
  765. /**
  766. * File ID (used on document trailer).
  767. * @protected
  768. * @since 5.0.005 (2010-05-12)
  769. */
  770. protected $file_id;
  771. // --- bookmark ---
  772. /**
  773. * Outlines for bookmark.
  774. * @protected
  775. * @since 2.1.002 (2008-02-12)
  776. */
  777. protected $outlines = array();
  778. /**
  779. * Outline root for bookmark.
  780. * @protected
  781. * @since 2.1.002 (2008-02-12)
  782. */
  783. protected $OutlineRoot;
  784. // --- javascript and form ---
  785. /**
  786. * Javascript code.
  787. * @protected
  788. * @since 2.1.002 (2008-02-12)
  789. */
  790. protected $javascript = '';
  791. /**
  792. * Javascript counter.
  793. * @protected
  794. * @since 2.1.002 (2008-02-12)
  795. */
  796. protected $n_js;
  797. /**
  798. * line trough state
  799. * @protected
  800. * @since 2.8.000 (2008-03-19)
  801. */
  802. protected $linethrough;
  803. /**
  804. * Array with additional document-wide usage rights for the document.
  805. * @protected
  806. * @since 5.8.014 (2010-08-23)
  807. */
  808. protected $ur = array();
  809. /**
  810. * DPI (Dot Per Inch) Document Resolution (do not change).
  811. * @protected
  812. * @since 3.0.000 (2008-03-27)
  813. */
  814. protected $dpi = 72;
  815. /**
  816. * Array of page numbers were a new page group was started (the page numbers are the keys of the array).
  817. * @protected
  818. * @since 3.0.000 (2008-03-27)
  819. */
  820. protected $newpagegroup = array();
  821. /**
  822. * Array that contains the number of pages in each page group.
  823. * @protected
  824. * @since 3.0.000 (2008-03-27)
  825. */
  826. protected $pagegroups = array();
  827. /**
  828. * Current page group number.
  829. * @protected
  830. * @since 3.0.000 (2008-03-27)
  831. */
  832. protected $currpagegroup = 0;
  833. /**
  834. * Array of transparency objects and parameters.
  835. * @protected
  836. * @since 3.0.000 (2008-03-27)
  837. */
  838. protected $extgstates;
  839. /**
  840. * Set the default JPEG compression quality (1-100).
  841. * @protected
  842. * @since 3.0.000 (2008-03-27)
  843. */
  844. protected $jpeg_quality;
  845. /**
  846. * Default cell height ratio.
  847. * @protected
  848. * @since 3.0.014 (2008-05-23)
  849. */
  850. protected $cell_height_ratio = K_CELL_HEIGHT_RATIO;
  851. /**
  852. * PDF viewer preferences.
  853. * @protected
  854. * @since 3.1.000 (2008-06-09)
  855. */
  856. protected $viewer_preferences;
  857. /**
  858. * A name object specifying how the document should be displayed when opened.
  859. * @protected
  860. * @since 3.1.000 (2008-06-09)
  861. */
  862. protected $PageMode;
  863. /**
  864. * Array for storing gradient information.
  865. * @protected
  866. * @since 3.1.000 (2008-06-09)
  867. */
  868. protected $gradients = array();
  869. /**
  870. * Array used to store positions inside the pages buffer (keys are the page numbers).
  871. * @protected
  872. * @since 3.2.000 (2008-06-26)
  873. */
  874. protected $intmrk = array();
  875. /**
  876. * Array used to store positions inside the pages buffer (keys are the page numbers).
  877. * @protected
  878. * @since 5.7.000 (2010-08-03)
  879. */
  880. protected $bordermrk = array();
  881. /**
  882. * Array used to store page positions to track empty pages (keys are the page numbers).
  883. * @protected
  884. * @since 5.8.007 (2010-08-18)
  885. */
  886. protected $emptypagemrk = array();
  887. /**
  888. * Array used to store content positions inside the pages buffer (keys are the page numbers).
  889. * @protected
  890. * @since 4.6.021 (2009-07-20)
  891. */
  892. protected $cntmrk = array();
  893. /**
  894. * Array used to store footer positions of each page.
  895. * @protected
  896. * @since 3.2.000 (2008-07-01)
  897. */
  898. protected $footerpos = array();
  899. /**
  900. * Array used to store footer length of each page.
  901. * @protected
  902. * @since 4.0.014 (2008-07-29)
  903. */
  904. protected $footerlen = array();
  905. /**
  906. * Boolean flag to indicate if a new line is created.
  907. * @protected
  908. * @since 3.2.000 (2008-07-01)
  909. */
  910. protected $newline = true;
  911. /**
  912. * End position of the latest inserted line.
  913. * @protected
  914. * @since 3.2.000 (2008-07-01)
  915. */
  916. protected $endlinex = 0;
  917. /**
  918. * PDF string for width value of the last line.
  919. * @protected
  920. * @since 4.0.006 (2008-07-16)
  921. */
  922. protected $linestyleWidth = '';
  923. /**
  924. * PDF string for CAP value of the last line.
  925. * @protected
  926. * @since 4.0.006 (2008-07-16)
  927. */
  928. protected $linestyleCap = '0 J';
  929. /**
  930. * PDF string for join value of the last line.
  931. * @protected
  932. * @since 4.0.006 (2008-07-16)
  933. */
  934. protected $linestyleJoin = '0 j';
  935. /**
  936. * PDF string for dash value of the last line.
  937. * @protected
  938. * @since 4.0.006 (2008-07-16)
  939. */
  940. protected $linestyleDash = '[] 0 d';
  941. /**
  942. * Boolean flag to indicate if marked-content sequence is open.
  943. * @protected
  944. * @since 4.0.013 (2008-07-28)
  945. */
  946. protected $openMarkedContent = false;
  947. /**
  948. * Count the latest inserted vertical spaces on HTML.
  949. * @protected
  950. * @since 4.0.021 (2008-08-24)
  951. */
  952. protected $htmlvspace = 0;
  953. /**
  954. * Array of Spot colors.
  955. * @protected
  956. * @since 4.0.024 (2008-09-12)
  957. */
  958. protected $spot_colors = array();
  959. /**
  960. * Symbol used for HTML unordered list items.
  961. * @protected
  962. * @since 4.0.028 (2008-09-26)
  963. */
  964. protected $lisymbol = '';
  965. /**
  966. * String used to mark the beginning and end of EPS image blocks.
  967. * @protected
  968. * @since 4.1.000 (2008-10-18)
  969. */
  970. protected $epsmarker = 'x#!#EPS#!#x';
  971. /**
  972. * Array of transformation matrix.
  973. * @protected
  974. * @since 4.2.000 (2008-10-29)
  975. */
  976. protected $transfmatrix = array();
  977. /**
  978. * Current key for transformation matrix.
  979. * @protected
  980. * @since 4.8.005 (2009-09-17)
  981. */
  982. protected $transfmatrix_key = 0;
  983. /**
  984. * Booklet mode for double-sided pages.
  985. * @protected
  986. * @since 4.2.000 (2008-10-29)
  987. */
  988. protected $booklet = false;
  989. /**
  990. * Epsilon value used for float calculations.
  991. * @protected
  992. * @since 4.2.000 (2008-10-29)
  993. */
  994. protected $feps = 0.005;
  995. /**
  996. * Array used for custom vertical spaces for HTML tags.
  997. * @protected
  998. * @since 4.2.001 (2008-10-30)
  999. */
  1000. protected $tagvspaces = array();
  1001. /**
  1002. * HTML PARSER: custom indent amount for lists. Negative value means disabled.
  1003. * @protected
  1004. * @since 4.2.007 (2008-11-12)
  1005. */
  1006. protected $customlistindent = -1;
  1007. /**
  1008. * Boolean flag to indicate if the border of the cell sides that cross the page should be removed.
  1009. * @protected
  1010. * @since 4.2.010 (2008-11-14)
  1011. */
  1012. protected $opencell = true;
  1013. /**
  1014. * Array of files to embedd.
  1015. * @protected
  1016. * @since 4.4.000 (2008-12-07)
  1017. */
  1018. protected $embeddedfiles = array();
  1019. /**
  1020. * Boolean flag to indicate if we are inside a PRE tag.
  1021. * @protected
  1022. * @since 4.4.001 (2008-12-08)
  1023. */
  1024. protected $premode = false;
  1025. /**
  1026. * Array used to store positions of graphics transformation blocks inside the page buffer.
  1027. * keys are the page numbers
  1028. * @protected
  1029. * @since 4.4.002 (2008-12-09)
  1030. */
  1031. protected $transfmrk = array();
  1032. /**
  1033. * Default color for html links.
  1034. * @protected
  1035. * @since 4.4.003 (2008-12-09)
  1036. */
  1037. protected $htmlLinkColorArray = array(0, 0, 255);
  1038. /**
  1039. * Default font style to add to html links.
  1040. * @protected
  1041. * @since 4.4.003 (2008-12-09)
  1042. */
  1043. protected $htmlLinkFontStyle = 'U';
  1044. /**
  1045. * Counts the number of pages.
  1046. * @protected
  1047. * @since 4.5.000 (2008-12-31)
  1048. */
  1049. protected $numpages = 0;
  1050. /**
  1051. * Array containing page lengths in bytes.
  1052. * @protected
  1053. * @since 4.5.000 (2008-12-31)
  1054. */
  1055. protected $pagelen = array();
  1056. /**
  1057. * Counts the number of pages.
  1058. * @protected
  1059. * @since 4.5.000 (2008-12-31)
  1060. */
  1061. protected $numimages = 0;
  1062. /**
  1063. * Store the image keys.
  1064. * @protected
  1065. * @since 4.5.000 (2008-12-31)
  1066. */
  1067. protected $imagekeys = array();
  1068. /**
  1069. * Length of the buffer in bytes.
  1070. * @protected
  1071. * @since 4.5.000 (2008-12-31)
  1072. */
  1073. protected $bufferlen = 0;
  1074. /**
  1075. * If true enables disk caching.
  1076. * @protected
  1077. * @since 4.5.000 (2008-12-31)
  1078. */
  1079. protected $diskcache = false;
  1080. /**
  1081. * Counts the number of fonts.
  1082. * @protected
  1083. * @since 4.5.000 (2009-01-02)
  1084. */
  1085. protected $numfonts = 0;
  1086. /**
  1087. * Store the font keys.
  1088. * @protected
  1089. * @since 4.5.000 (2009-01-02)
  1090. */
  1091. protected $fontkeys = array();
  1092. /**
  1093. * Store the font object IDs.
  1094. * @protected
  1095. * @since 4.8.001 (2009-09-09)
  1096. */
  1097. protected $font_obj_ids = array();
  1098. /**
  1099. * Store the fage status (true when opened, false when closed).
  1100. * @protected
  1101. * @since 4.5.000 (2009-01-02)
  1102. */
  1103. protected $pageopen = array();
  1104. /**
  1105. * Default monospace font.
  1106. * @protected
  1107. * @since 4.5.025 (2009-03-10)
  1108. */
  1109. protected $default_monospaced_font = 'courier';
  1110. /**
  1111. * Cloned copy of the current class object.
  1112. * @protected
  1113. * @since 4.5.029 (2009-03-19)
  1114. */
  1115. protected $objcopy;
  1116. /**
  1117. * Array used to store the lengths of cache files.
  1118. * @protected
  1119. * @since 4.5.029 (2009-03-19)
  1120. */
  1121. protected $cache_file_length = array();
  1122. /**
  1123. * Table header content to be repeated on each new page.
  1124. * @protected
  1125. * @since 4.5.030 (2009-03-20)
  1126. */
  1127. protected $thead = '';
  1128. /**
  1129. * Margins used for table header.
  1130. * @protected
  1131. * @since 4.5.030 (2009-03-20)
  1132. */
  1133. protected $theadMargins = array();
  1134. /**
  1135. * Cache array for UTF8StringToArray() method.
  1136. * @protected
  1137. * @since 4.5.037 (2009-04-07)
  1138. */
  1139. protected $cache_UTF8StringToArray = array();
  1140. /**
  1141. * Maximum size of cache array used for UTF8StringToArray() method.
  1142. * @protected
  1143. * @since 4.5.037 (2009-04-07)
  1144. */
  1145. protected $cache_maxsize_UTF8StringToArray = 8;
  1146. /**
  1147. * Current size of cache array used for UTF8StringToArray() method.
  1148. * @protected
  1149. * @since 4.5.037 (2009-04-07)
  1150. */
  1151. protected $cache_size_UTF8StringToArray = 0;
  1152. /**
  1153. * Boolean flag to enable document digital signature.
  1154. * @protected
  1155. * @since 4.6.005 (2009-04-24)
  1156. */
  1157. protected $sign = false;
  1158. /**
  1159. * Digital signature data.
  1160. * @protected
  1161. * @since 4.6.005 (2009-04-24)
  1162. */
  1163. protected $signature_data = array();
  1164. /**
  1165. * Digital signature max length.
  1166. * @protected
  1167. * @since 4.6.005 (2009-04-24)
  1168. */
  1169. protected $signature_max_length = 11742;
  1170. /**
  1171. * Data for digital signature appearance.
  1172. * @protected
  1173. * @since 5.3.011 (2010-06-16)
  1174. */
  1175. protected $signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1176. /**
  1177. * Array of empty digital signature appearances.
  1178. * @protected
  1179. * @since 5.9.101 (2011-07-06)
  1180. */
  1181. protected $empty_signature_appearance = array();
  1182. /**
  1183. * Regular expression used to find blank characters (required for word-wrapping).
  1184. * @protected
  1185. * @since 4.6.006 (2009-04-28)
  1186. */
  1187. protected $re_spaces = '/[^\S\xa0]/';
  1188. /**
  1189. * Array of $re_spaces parts.
  1190. * @protected
  1191. * @since 5.5.011 (2010-07-09)
  1192. */
  1193. protected $re_space = array('p' => '[^\S\xa0]', 'm' => '');
  1194. /**
  1195. * Digital signature object ID.
  1196. * @protected
  1197. * @since 4.6.022 (2009-06-23)
  1198. */
  1199. protected $sig_obj_id = 0;
  1200. /**
  1201. * ByteRange placemark used during digital signature process.
  1202. * @protected
  1203. * @since 4.6.028 (2009-08-25)
  1204. */
  1205. protected $byterange_string = '/ByteRange[0 ********** ********** **********]';
  1206. /**
  1207. * Placemark used during digital signature process.
  1208. * @protected
  1209. * @since 4.6.028 (2009-08-25)
  1210. */
  1211. protected $sig_annot_ref = '***SIGANNREF*** 0 R';
  1212. /**
  1213. * ID of page objects.
  1214. * @protected
  1215. * @since 4.7.000 (2009-08-29)
  1216. */
  1217. protected $page_obj_id = array();
  1218. /**
  1219. * List of form annotations IDs.
  1220. * @protected
  1221. * @since 4.8.000 (2009-09-07)
  1222. */
  1223. protected $form_obj_id = array();
  1224. /**
  1225. * 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.
  1226. * @protected
  1227. * @since 4.8.000 (2009-09-07)
  1228. */
  1229. protected $default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1230. /**
  1231. * Javascript objects array.
  1232. * @protected
  1233. * @since 4.8.000 (2009-09-07)
  1234. */
  1235. protected $js_objects = array();
  1236. /**
  1237. * Current form action (used during XHTML rendering).
  1238. * @protected
  1239. * @since 4.8.000 (2009-09-07)
  1240. */
  1241. protected $form_action = '';
  1242. /**
  1243. * Current form encryption type (used during XHTML rendering).
  1244. * @protected
  1245. * @since 4.8.000 (2009-09-07)
  1246. */
  1247. protected $form_enctype = 'application/x-www-form-urlencoded';
  1248. /**
  1249. * Current method to submit forms.
  1250. * @protected
  1251. * @since 4.8.000 (2009-09-07)
  1252. */
  1253. protected $form_mode = 'post';
  1254. /**
  1255. * List of fonts used on form fields (fontname => fontkey).
  1256. * @protected
  1257. * @since 4.8.001 (2009-09-09)
  1258. */
  1259. protected $annotation_fonts = array();
  1260. /**
  1261. * List of radio buttons parent objects.
  1262. * @protected
  1263. * @since 4.8.001 (2009-09-09)
  1264. */
  1265. protected $radiobutton_groups = array();
  1266. /**
  1267. * List of radio group objects IDs.
  1268. * @protected
  1269. * @since 4.8.001 (2009-09-09)
  1270. */
  1271. protected $radio_groups = array();
  1272. /**
  1273. * Text indentation value (used for text-indent CSS attribute).
  1274. * @protected
  1275. * @since 4.8.006 (2009-09-23)
  1276. */
  1277. protected $textindent = 0;
  1278. /**
  1279. * Store page number when startTransaction() is called.
  1280. * @protected
  1281. * @since 4.8.006 (2009-09-23)
  1282. */
  1283. protected $start_transaction_page = 0;
  1284. /**
  1285. * Store Y position when startTransaction() is called.
  1286. * @protected
  1287. * @since 4.9.001 (2010-03-28)
  1288. */
  1289. protected $start_transaction_y = 0;
  1290. /**
  1291. * True when we are printing the thead section on a new page.
  1292. * @protected
  1293. * @since 4.8.027 (2010-01-25)
  1294. */
  1295. protected $inthead = false;
  1296. /**
  1297. * Array of column measures (width, space, starting Y position).
  1298. * @protected
  1299. * @since 4.9.001 (2010-03-28)
  1300. */
  1301. protected $columns = array();
  1302. /**
  1303. * Number of colums.
  1304. * @protected
  1305. * @since 4.9.001 (2010-03-28)
  1306. */
  1307. protected $num_columns = 1;
  1308. /**
  1309. * Current column number.
  1310. * @protected
  1311. * @since 4.9.001 (2010-03-28)
  1312. */
  1313. protected $current_column = 0;
  1314. /**
  1315. * Starting page for columns.
  1316. * @protected
  1317. * @since 4.9.001 (2010-03-28)
  1318. */
  1319. protected $column_start_page = 0;
  1320. /**
  1321. * Maximum page and column selected.
  1322. * @protected
  1323. * @since 5.8.000 (2010-08-11)
  1324. */
  1325. protected $maxselcol = array('page' => 0, 'column' => 0);
  1326. /**
  1327. * Array of: X difference between table cell x start and starting page margin, cellspacing, cellpadding.
  1328. * @protected
  1329. * @since 5.8.000 (2010-08-11)
  1330. */
  1331. protected $colxshift = array('x' => 0, 's' => array('H' => 0, 'V' => 0), 'p' => array('L' => 0, 'T' => 0, 'R' => 0, 'B' => 0));
  1332. /**
  1333. * 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.
  1334. * @protected
  1335. * @since 4.9.008 (2010-04-03)
  1336. */
  1337. protected $textrendermode = 0;
  1338. /**
  1339. * Text stroke width in doc units.
  1340. * @protected
  1341. * @since 4.9.008 (2010-04-03)
  1342. */
  1343. protected $textstrokewidth = 0;
  1344. /**
  1345. * Current stroke color.
  1346. * @protected
  1347. * @since 4.9.008 (2010-04-03)
  1348. */
  1349. protected $strokecolor;
  1350. /**
  1351. * Default unit of measure for document.
  1352. * @protected
  1353. * @since 5.0.000 (2010-04-22)
  1354. */
  1355. protected $pdfunit = 'mm';
  1356. /**
  1357. * Boolean flag true when we are on TOC (Table Of Content) page.
  1358. * @protected
  1359. */
  1360. protected $tocpage = false;
  1361. /**
  1362. * Boolean flag: if true convert vector images (SVG, EPS) to raster image using GD or ImageMagick library.
  1363. * @protected
  1364. * @since 5.0.000 (2010-04-26)
  1365. */
  1366. protected $rasterize_vector_images = false;
  1367. /**
  1368. * Boolean flag: if true enables font subsetting by default.
  1369. * @protected
  1370. * @since 5.3.002 (2010-06-07)
  1371. */
  1372. protected $font_subsetting = true;
  1373. /**
  1374. * Array of default graphic settings.
  1375. * @protected
  1376. * @since 5.5.008 (2010-07-02)
  1377. */
  1378. protected $default_graphic_vars = array();
  1379. /**
  1380. * Array of XObjects.
  1381. * @protected
  1382. * @since 5.8.014 (2010-08-23)
  1383. */
  1384. protected $xobjects = array();
  1385. /**
  1386. * Boolean value true when we are inside an XObject.
  1387. * @protected
  1388. * @since 5.8.017 (2010-08-24)
  1389. */
  1390. protected $inxobj = false;
  1391. /**
  1392. * Current XObject ID.
  1393. * @protected
  1394. * @since 5.8.017 (2010-08-24)
  1395. */
  1396. protected $xobjid = '';
  1397. /**
  1398. * Percentage of character stretching.
  1399. * @protected
  1400. * @since 5.9.000 (2010-09-29)
  1401. */
  1402. protected $font_stretching = 100;
  1403. /**
  1404. * Increases or decreases the space between characters in a text by the specified amount (tracking).
  1405. * @protected
  1406. * @since 5.9.000 (2010-09-29)
  1407. */
  1408. protected $font_spacing = 0;
  1409. /**
  1410. * Array of no-write regions.
  1411. * ('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)
  1412. * @protected
  1413. * @since 5.9.003 (2010-10-14)
  1414. */
  1415. protected $page_regions = array();
  1416. /**
  1417. * Array containing HTML color names and values.
  1418. * @protected
  1419. * @since 5.9.004 (2010-10-18)
  1420. */
  1421. protected $webcolor = array();
  1422. /**
  1423. * Array containing spot color names and values.
  1424. * @protected
  1425. * @since 5.9.012 (2010-11-11)
  1426. */
  1427. protected $spotcolor = array();
  1428. /**
  1429. * Array of PDF layers data.
  1430. * @protected
  1431. * @since 5.9.102 (2011-07-13)
  1432. */
  1433. protected $pdflayers = array();
  1434. /**
  1435. * A dictionary of names and corresponding destinations (Dests key on document Catalog).
  1436. * @protected
  1437. * @since 5.9.097 (2011-06-23)
  1438. */
  1439. protected $dests = array();
  1440. /**
  1441. * Object ID for Named Destinations
  1442. * @protected
  1443. * @since 5.9.097 (2011-06-23)
  1444. */
  1445. protected $n_dests;
  1446. /**
  1447. * Directory used for the last SVG image.
  1448. * @protected
  1449. * @since 5.0.000 (2010-05-05)
  1450. */
  1451. protected $svgdir = '';
  1452. /**
  1453. * Deafult unit of measure for SVG.
  1454. * @protected
  1455. * @since 5.0.000 (2010-05-02)
  1456. */
  1457. protected $svgunit = 'px';
  1458. /**
  1459. * Array of SVG gradients.
  1460. * @protected
  1461. * @since 5.0.000 (2010-05-02)
  1462. */
  1463. protected $svggradients = array();
  1464. /**
  1465. * ID of last SVG gradient.
  1466. * @protected
  1467. * @since 5.0.000 (2010-05-02)
  1468. */
  1469. protected $svggradientid = 0;
  1470. /**
  1471. * Boolean value true when in SVG defs group.
  1472. * @protected
  1473. * @since 5.0.000 (2010-05-02)
  1474. */
  1475. protected $svgdefsmode = false;
  1476. /**
  1477. * Array of SVG defs.
  1478. * @protected
  1479. * @since 5.0.000 (2010-05-02)
  1480. */
  1481. protected $svgdefs = array();
  1482. /**
  1483. * Boolean value true when in SVG clipPath tag.
  1484. * @protected
  1485. * @since 5.0.000 (2010-04-26)
  1486. */
  1487. protected $svgclipmode = false;
  1488. /**
  1489. * Array of SVG clipPath commands.
  1490. * @protected
  1491. * @since 5.0.000 (2010-05-02)
  1492. */
  1493. protected $svgclippaths = array();
  1494. /**
  1495. * Array of SVG clipPath tranformation matrix.
  1496. * @protected
  1497. * @since 5.8.022 (2010-08-31)
  1498. */
  1499. protected $svgcliptm = array();
  1500. /**
  1501. * ID of last SVG clipPath.
  1502. * @protected
  1503. * @since 5.0.000 (2010-05-02)
  1504. */
  1505. protected $svgclipid = 0;
  1506. /**
  1507. * SVG text.
  1508. * @protected
  1509. * @since 5.0.000 (2010-05-02)
  1510. */
  1511. protected $svgtext = '';
  1512. /**
  1513. * SVG text properties.
  1514. * @protected
  1515. * @since 5.8.013 (2010-08-23)
  1516. */
  1517. protected $svgtextmode = array();
  1518. /**
  1519. * Array of hinheritable SVG properties.
  1520. * @protected
  1521. * @since 5.0.000 (2010-05-02)
  1522. */
  1523. 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');
  1524. /**
  1525. * Array of SVG properties.
  1526. * @protected
  1527. * @since 5.0.000 (2010-05-02)
  1528. */
  1529. protected $svgstyles = array(array(
  1530. 'alignment-baseline' => 'auto',
  1531. 'baseline-shift' => 'baseline',
  1532. 'clip' => 'auto',
  1533. 'clip-path' => 'none',
  1534. 'clip-rule' => 'nonzero',
  1535. 'color' => 'black',
  1536. 'color-interpolation' => 'sRGB',
  1537. 'color-interpolation-filters' => 'linearRGB',
  1538. 'color-profile' => 'auto',
  1539. 'color-rendering' => 'auto',
  1540. 'cursor' => 'auto',
  1541. 'direction' => 'ltr',
  1542. 'display' => 'inline',
  1543. 'dominant-baseline' => 'auto',
  1544. 'enable-background' => 'accumulate',
  1545. 'fill' => 'black',
  1546. 'fill-opacity' => 1,
  1547. 'fill-rule' => 'nonzero',
  1548. 'filter' => 'none',
  1549. 'flood-color' => 'black',
  1550. 'flood-opacity' => 1,
  1551. 'font' => '',
  1552. 'font-family' => 'helvetica',
  1553. 'font-size' => 'medium',
  1554. 'font-size-adjust' => 'none',
  1555. 'font-stretch' => 'normal',
  1556. 'font-style' => 'normal',
  1557. 'font-variant' => 'normal',
  1558. 'font-weight' => 'normal',
  1559. 'glyph-orientation-horizontal' => '0deg',
  1560. 'glyph-orientation-vertical' => 'auto',
  1561. 'image-rendering' => 'auto',
  1562. 'kerning' => 'auto',
  1563. 'letter-spacing' => 'normal',
  1564. 'lighting-color' => 'white',
  1565. 'marker' => '',
  1566. 'marker-end' => 'none',
  1567. 'marker-mid' => 'none',
  1568. 'marker-start' => 'none',
  1569. 'mask' => 'none',
  1570. 'opacity' => 1,
  1571. 'overflow' => 'auto',
  1572. 'pointer-events' => 'visiblePainted',
  1573. 'shape-rendering' => 'auto',
  1574. 'stop-color' => 'black',
  1575. 'stop-opacity' => 1,
  1576. 'stroke' => 'none',
  1577. 'stroke-dasharray' => 'none',
  1578. 'stroke-dashoffset' => 0,
  1579. 'stroke-linecap' => 'butt',
  1580. 'stroke-linejoin' => 'miter',
  1581. 'stroke-miterlimit' => 4,
  1582. 'stroke-opacity' => 1,
  1583. 'stroke-width' => 1,
  1584. 'text-anchor' => 'start',
  1585. 'text-decoration' => 'none',
  1586. 'text-rendering' => 'auto',
  1587. 'unicode-bidi' => 'normal',
  1588. 'visibility' => 'visible',
  1589. 'word-spacing' => 'normal',
  1590. 'writing-mode' => 'lr-tb',
  1591. 'text-color' => 'black',
  1592. 'transfmatrix' => array(1, 0, 0, 1, 0, 0)
  1593. ));
  1594. /**
  1595. * If true force sRGB color profile for all document.
  1596. * @protected
  1597. * @since 5.9.121 (2011-09-28)
  1598. */
  1599. protected $force_srgb = false;
  1600. /**
  1601. * If true set the document to PDF/A mode.
  1602. * @protected
  1603. * @since 5.9.121 (2011-09-27)
  1604. */
  1605. protected $pdfa_mode = false;
  1606. /**
  1607. * Document creation date-time
  1608. * @protected
  1609. * @since 5.9.152 (2012-03-22)
  1610. */
  1611. protected $doc_creation_timestamp;
  1612. /**
  1613. * Document modification date-time
  1614. * @protected
  1615. * @since 5.9.152 (2012-03-22)
  1616. */
  1617. protected $doc_modification_timestamp;
  1618. /**
  1619. * Custom XMP data.
  1620. * @protected
  1621. * @since 5.9.128 (2011-10-06)
  1622. */
  1623. protected $custom_xmp = '';
  1624. /**
  1625. * Overprint mode array.
  1626. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1627. * @protected
  1628. * @since 5.9.152 (2012-03-23)
  1629. */
  1630. protected $overprint = array('OP' => false, 'op' => false, 'OPM' => 0);
  1631. /**
  1632. * Alpha mode array.
  1633. * (Check the "Entries in a Graphics State Parameter Dictionary" on PDF 32000-1:2008).
  1634. * @protected
  1635. * @since 5.9.152 (2012-03-23)
  1636. */
  1637. protected $alpha = array('CA' => 1, 'ca' => 1, 'BM' => '/Normal', 'AIS' => false);
  1638. /**
  1639. * Define the page boundaries boxes to be set on document.
  1640. * @protected
  1641. * @since 5.9.152 (2012-03-23)
  1642. */
  1643. protected $page_boxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  1644. /**
  1645. * Set the document producer metadata.
  1646. * @protected
  1647. * @since 5.9.152 (2012-03-23)
  1648. */
  1649. protected $pdfproducer;
  1650. /**
  1651. * If true print TCPDF meta link.
  1652. * @protected
  1653. * @since 5.9.152 (2012-03-23)
  1654. */
  1655. protected $tcpdflink = true;
  1656. /**
  1657. * Cache array for computed GD gamma values.
  1658. * @protected
  1659. * @since 5.9.1632 (2012-06-05)
  1660. */
  1661. protected $gdgammacache = array();
  1662. //------------------------------------------------------------
  1663. // METHODS
  1664. //------------------------------------------------------------
  1665. /**
  1666. * This is the class constructor.
  1667. * It allows to set up the page format, the orientation and the measure unit used in all the methods (except for the font sizes).
  1668. * @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>
  1669. * @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.
  1670. * @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().
  1671. * @param $unicode (boolean) TRUE means that the input text is unicode (default = true)
  1672. * @param $encoding (string) Charset encoding; default is UTF-8.
  1673. * @param $diskcache (boolean) If TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower).
  1674. * @param $pdfa (boolean) If TRUE set the document to PDF/A mode.
  1675. * @public
  1676. * @see getPageSizeFromFormat(), setPageFormat()
  1677. */
  1678. public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
  1679. /* Set internal character encoding to ASCII */
  1680. if (function_exists('mb_internal_encoding') AND mb_internal_encoding()) {
  1681. $this->internal_encoding = mb_internal_encoding();
  1682. mb_internal_encoding('ASCII');
  1683. }
  1684. // get array of HTML colors
  1685. require(dirname(__FILE__).'/htmlcolors.php');
  1686. $this->webcolor = $webcolor;
  1687. // get array of custom spot colors
  1688. if (file_exists(dirname(__FILE__).'/spotcolors.php')) {
  1689. require(dirname(__FILE__).'/spotcolors.php');
  1690. $this->spotcolor = $spotcolor;
  1691. } else {
  1692. $this->spotcolor = array();
  1693. }
  1694. require_once(dirname(__FILE__).'/unicode_data.php');
  1695. $this->unicode = new TCPDF_UNICODE_DATA();
  1696. require_once(dirname(__FILE__).'/encodings_maps.php');
  1697. $this->encmaps = new TCPDF_ENCODING_MAPS();
  1698. $this->font_obj_ids = array();
  1699. $this->page_obj_id = array();
  1700. $this->form_obj_id = array();
  1701. // set pdf/a mode
  1702. $this->pdfa_mode = $pdfa;
  1703. $this->force_srgb = false;
  1704. // set disk caching
  1705. $this->diskcache = $diskcache ? true : false;
  1706. // set language direction
  1707. $this->rtl = false;
  1708. $this->tmprtl = false;
  1709. // some checks
  1710. $this->_dochecks();
  1711. // initialization of properties
  1712. $this->isunicode = $unicode;
  1713. $this->page = 0;
  1714. $this->transfmrk[0] = array();
  1715. $this->pagedim = array();
  1716. $this->n = 2;
  1717. $this->buffer = '';
  1718. $this->pages = array();
  1719. $this->state = 0;
  1720. $this->fonts = array();
  1721. $this->FontFiles = array();
  1722. $this->diffs = array();
  1723. $this->images = array();
  1724. $this->links = array();
  1725. $this->gradients = array();
  1726. $this->InFooter = false;
  1727. $this->lasth = 0;
  1728. $this->FontFamily = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  1729. $this->FontStyle = '';
  1730. $this->FontSizePt = 12;
  1731. $this->underline = false;
  1732. $this->overline = false;
  1733. $this->linethrough = false;
  1734. $this->DrawColor = '0 G';
  1735. $this->FillColor = '0 g';
  1736. $this->TextColor = '0 g';
  1737. $this->ColorFlag = false;
  1738. $this->pdflayers = array();
  1739. // encryption values
  1740. $this->encrypted = false;
  1741. $this->last_enc_key = '';
  1742. // standard Unicode fonts
  1743. $this->CoreFonts = array(
  1744. 'courier'=>'Courier',
  1745. 'courierB'=>'Courier-Bold',
  1746. 'courierI'=>'Courier-Oblique',
  1747. 'courierBI'=>'Courier-BoldOblique',
  1748. 'helvetica'=>'Helvetica',
  1749. 'helveticaB'=>'Helvetica-Bold',
  1750. 'helveticaI'=>'Helvetica-Oblique',
  1751. 'helveticaBI'=>'Helvetica-BoldOblique',
  1752. 'times'=>'Times-Roman',
  1753. 'timesB'=>'Times-Bold',
  1754. 'timesI'=>'Times-Italic',
  1755. 'timesBI'=>'Times-BoldItalic',
  1756. 'symbol'=>'Symbol',
  1757. 'zapfdingbats'=>'ZapfDingbats'
  1758. );
  1759. // set scale factor
  1760. $this->setPageUnit($unit);
  1761. // set page format and orientation
  1762. $this->setPageFormat($format, $orientation);
  1763. // page margins (1 cm)
  1764. $margin = 28.35 / $this->k;
  1765. $this->SetMargins($margin, $margin);
  1766. // internal cell padding
  1767. $cpadding = $margin / 10;
  1768. $this->setCellPaddings($cpadding, 0, $cpadding, 0);
  1769. // cell margins
  1770. $this->setCellMargins(0, 0, 0, 0);
  1771. // line width (0.2 mm)
  1772. $this->LineWidth = 0.57 / $this->k;
  1773. $this->linestyleWidth = sprintf('%F w', ($this->LineWidth * $this->k));
  1774. $this->linestyleCap = '0 J';
  1775. $this->linestyleJoin = '0 j';
  1776. $this->linestyleDash = '[] 0 d';
  1777. // automatic page break
  1778. $this->SetAutoPageBreak(true, (2 * $margin));
  1779. // full width display mode
  1780. $this->SetDisplayMode('fullwidth');
  1781. // compression
  1782. $this->SetCompression();
  1783. // set default PDF version number
  1784. $this->setPDFVersion();
  1785. $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";
  1786. $this->tcpdflink = true;
  1787. $this->encoding = $encoding;
  1788. $this->HREF = array();
  1789. $this->getFontsList();
  1790. $this->fgcolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1791. $this->strokecolor = array('R' => 0, 'G' => 0, 'B' => 0);
  1792. $this->bgcolor = array('R' => 255, 'G' => 255, 'B' => 255);
  1793. $this->extgstates = array();
  1794. $this->setTextShadow();
  1795. // user's rights
  1796. $this->sign = false;
  1797. $this->ur['enabled'] = false;
  1798. $this->ur['document'] = '/FullSave';
  1799. $this->ur['annots'] = '/Create/Delete/Modify/Copy/Import/Export';
  1800. $this->ur['form'] = '/Add/Delete/FillIn/Import/Export/SubmitStandalone/SpawnTemplate';
  1801. $this->ur['signature'] = '/Modify';
  1802. $this->ur['ef'] = '/Create/Delete/Modify/Import';
  1803. $this->ur['formex'] = '';
  1804. $this->signature_appearance = array('page' => 1, 'rect' => '0 0 0 0');
  1805. $this->empty_signature_appearance = array();
  1806. // set default JPEG quality
  1807. $this->jpeg_quality = 75;
  1808. // initialize some settings
  1809. $this->utf8Bidi(array(''), '');
  1810. // set default font
  1811. $this->SetFont($this->FontFamily, $this->FontStyle, $this->FontSizePt);
  1812. // check if PCRE Unicode support is enabled
  1813. if ($this->isunicode AND (@preg_match('/\pL/u', 'a') == 1)) {
  1814. // PCRE unicode support is turned ON
  1815. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  1816. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  1817. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  1818. //$this->setSpacesRE('/[^\S\P{Z}\P{Lo}\xa0]/u');
  1819. $this->setSpacesRE('/[^\S\P{Z}\xa0]/u');
  1820. } else {
  1821. // PCRE unicode support is turned OFF
  1822. $this->setSpacesRE('/[^\S\xa0]/');
  1823. }
  1824. $this->default_form_prop = array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 255), 'strokeColor'=>array(128, 128, 128));
  1825. // set file ID for trailer
  1826. $serformat = (is_array($format) ? serialize($format) : $format);
  1827. $this->file_id = md5($this->getRandomSeed('TCPDF'.$orientation.$unit.$serformat.$encoding));
  1828. // set document creation and modification timestamp
  1829. $this->doc_creation_timestamp = time();
  1830. $this->doc_modification_timestamp = $this->doc_creation_timestamp;
  1831. // get default graphic vars
  1832. $this->default_graphic_vars = $this->getGraphicVars();
  1833. $this->header_xobj_autoreset = false;
  1834. $this->custom_xmp = '';
  1835. }
  1836. /**
  1837. * Default destructor.
  1838. * @public
  1839. * @since 1.53.0.TC016
  1840. */
  1841. public function __destruct() {
  1842. // restore internal encoding
  1843. if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) {
  1844. mb_internal_encoding($this->internal_encoding);
  1845. }
  1846. // unset all class variables
  1847. $this->_destroy(true);
  1848. }
  1849. /**
  1850. * Return the current TCPDF version.
  1851. * @return TCPDF version string
  1852. * @public
  1853. * @since 5.9.012 (2010-11-10)
  1854. */
  1855. public function getTCPDFVersion() {
  1856. return $this->tcpdf_version;
  1857. }
  1858. /**
  1859. * Set the units of measure for the document.
  1860. * @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.
  1861. * @public
  1862. * @since 3.0.015 (2008-06-06)
  1863. */
  1864. public function setPageUnit($unit) {
  1865. $unit = strtolower($unit);
  1866. //Set scale factor
  1867. switch ($unit) {
  1868. // points
  1869. case 'px':
  1870. case 'pt': {
  1871. $this->k = 1;
  1872. break;
  1873. }
  1874. // millimeters
  1875. case 'mm': {
  1876. $this->k = $this->dpi / 25.4;
  1877. break;
  1878. }
  1879. // centimeters
  1880. case 'cm': {
  1881. $this->k = $this->dpi / 2.54;
  1882. break;
  1883. }
  1884. // inches
  1885. case 'in': {
  1886. $this->k = $this->dpi;
  1887. break;
  1888. }
  1889. // unsupported unit
  1890. default : {
  1891. $this->Error('Incorrect unit: '.$unit);
  1892. break;
  1893. }
  1894. }
  1895. $this->pdfunit = $unit;
  1896. if (isset($this->CurOrientation)) {
  1897. $this->setPageOrientation($this->CurOrientation);
  1898. }
  1899. }
  1900. /**
  1901. * Get page dimensions from format name.
  1902. * @param $format (mixed) The format name. It can be: <ul>
  1903. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  1904. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  1905. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  1906. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  1907. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  1908. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  1909. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  1910. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  1911. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  1912. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  1913. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  1914. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  1915. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  1916. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  1917. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  1918. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  1919. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  1920. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  1921. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  1922. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  1923. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  1924. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  1925. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  1926. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  1927. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  1928. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  1929. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  1930. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  1931. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  1932. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  1933. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  1934. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  1935. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  1936. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  1937. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  1938. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  1939. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  1940. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  1941. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  1942. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  1943. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  1944. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  1945. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  1946. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  1947. * <li><b>SIS 014711 E Series</b></li>
  1948. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  1949. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  1950. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  1951. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  1952. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  1953. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  1954. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  1955. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  1956. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  1957. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  1958. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  1959. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  1960. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  1961. * <li><b>SIS 014711 G Series</b></li>
  1962. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  1963. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  1964. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  1965. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  1966. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  1967. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  1968. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  1969. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  1970. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  1971. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  1972. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  1973. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  1974. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  1975. * <li><b>ISO Press</b></li>
  1976. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  1977. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  1978. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  1979. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  1980. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  1981. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  1982. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  1983. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  1984. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  1985. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  1986. * <li><b>German DIN 476</b></li>
  1987. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  1988. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  1989. * <li><b>Variations on the ISO Standard</b></li>
  1990. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  1991. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  1992. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  1993. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  1994. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  1995. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  1996. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  1997. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  1998. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  1999. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  2000. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  2001. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  2002. * <li><b>ANSI Series</b></li>
  2003. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  2004. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  2005. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  2006. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  2007. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  2008. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  2009. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  2010. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  2011. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  2012. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  2013. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  2014. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  2015. * <li><b>Other North American Paper Sizes</b></li>
  2016. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  2017. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  2018. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  2019. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  2020. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  2021. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  2022. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  2023. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  2024. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  2025. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  2026. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  2027. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  2028. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  2029. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  2030. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  2031. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  2032. * <li><b>North American Architectural Sizes</b></li>
  2033. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  2034. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  2035. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  2036. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  2037. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  2038. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  2039. * <li><b>Announcement Envelopes</b></li>
  2040. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  2041. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  2042. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  2043. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  2044. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  2045. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  2046. * <li><b>Commercial Envelopes</b></li>
  2047. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  2048. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  2049. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  2050. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  2051. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  2052. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  2053. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  2054. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  2055. * <li><b>Catalogue Envelopes</b></li>
  2056. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  2057. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  2058. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  2059. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  2060. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  2061. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  2062. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  2063. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  2064. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  2065. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  2066. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  2067. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  2068. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  2069. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  2070. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  2071. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  2072. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  2073. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  2074. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  2075. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  2076. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  2077. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  2078. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  2079. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  2080. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  2081. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  2082. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  2083. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  2084. * <li><b>PA Series</b></li>
  2085. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  2086. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  2087. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  2088. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  2089. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  2090. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  2091. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  2092. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  2093. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  2094. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  2095. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  2096. * <li><b>Standard Photographic Print Sizes</b></li>
  2097. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  2098. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  2099. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  2100. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  2101. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  2102. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  2103. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  2104. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  2105. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  2106. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  2107. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  2108. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  2109. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  2110. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  2111. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  2112. * <li><b>Common Newspaper Sizes</b></li>
  2113. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  2114. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  2115. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  2116. * <li><b>Business Cards</b></li>
  2117. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  2118. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  2119. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  2120. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  2121. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  2122. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  2123. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  2124. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  2125. * <li><b>Billboards</b></li>
  2126. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  2127. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  2128. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  2129. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  2130. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  2131. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  2132. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  2133. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  2134. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  2135. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  2136. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  2137. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  2138. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  2139. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  2140. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  2141. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  2142. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  2143. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  2144. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  2145. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  2146. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  2147. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  2148. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  2149. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  2150. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  2151. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  2152. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  2153. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  2154. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  2155. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  2156. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  2157. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  2158. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  2159. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  2160. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  2161. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  2162. * <li><b>Old Imperial Belgian</b></li>
  2163. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  2164. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  2165. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  2166. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  2167. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  2168. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  2169. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  2170. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2171. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  2172. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  2173. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  2174. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  2175. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  2176. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  2177. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  2178. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  2179. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  2180. * <li><b>Old Imperial French</b></li>
  2181. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  2182. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  2183. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  2184. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  2185. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  2186. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  2187. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  2188. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  2189. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  2190. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  2191. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  2192. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  2193. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  2194. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  2195. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  2196. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  2197. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  2198. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  2199. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  2200. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  2201. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  2202. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  2203. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  2204. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  2205. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  2206. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  2207. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  2208. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  2209. * </ul>
  2210. * @return array containing page width and height in points
  2211. * @public
  2212. * @since 5.0.010 (2010-05-17)
  2213. */
  2214. public function getPageSizeFromFormat($format) {
  2215. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  2216. switch (strtoupper($format)) {
  2217. // ISO 216 A Series + 2 SIS 014711 extensions
  2218. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  2219. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  2220. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  2221. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  2222. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  2223. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  2224. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  2225. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  2226. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  2227. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  2228. case 'A10': {$pf = array( 73.701, 104.882); break;}
  2229. case 'A11': {$pf = array( 51.024, 73.701); break;}
  2230. case 'A12': {$pf = array( 36.850, 51.024); break;}
  2231. // ISO 216 B Series + 2 SIS 014711 extensions
  2232. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  2233. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  2234. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  2235. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  2236. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  2237. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  2238. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  2239. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  2240. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  2241. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  2242. case 'B10': {$pf = array( 87.874, 124.724); break;}
  2243. case 'B11': {$pf = array( 62.362, 87.874); break;}
  2244. case 'B12': {$pf = array( 42.520, 62.362); break;}
  2245. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  2246. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  2247. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  2248. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  2249. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  2250. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  2251. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  2252. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  2253. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  2254. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  2255. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  2256. case 'C10': {$pf = array( 79.370, 113.386); break;}
  2257. case 'C11': {$pf = array( 56.693, 79.370); break;}
  2258. case 'C12': {$pf = array( 39.685, 56.693); break;}
  2259. case 'C76': {$pf = array( 229.606, 459.213); break;}
  2260. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  2261. // SIS 014711 E Series
  2262. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  2263. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  2264. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  2265. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  2266. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  2267. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  2268. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  2269. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  2270. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  2271. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  2272. case 'E10': {$pf = array( 76.535, 110.551); break;}
  2273. case 'E11': {$pf = array( 53.858, 76.535); break;}
  2274. case 'E12': {$pf = array( 36.850, 53.858); break;}
  2275. // SIS 014711 G Series
  2276. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  2277. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  2278. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  2279. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  2280. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  2281. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  2282. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  2283. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  2284. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  2285. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  2286. case 'G10': {$pf = array( 82.205, 119.055); break;}
  2287. case 'G11': {$pf = array( 59.528, 82.205); break;}
  2288. case 'G12': {$pf = array( 39.685, 59.528); break;}
  2289. // ISO Press
  2290. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  2291. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  2292. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  2293. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  2294. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  2295. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  2296. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  2297. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  2298. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  2299. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  2300. // German DIN 476
  2301. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  2302. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  2303. // Variations on the ISO Standard
  2304. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  2305. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  2306. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  2307. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  2308. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  2309. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  2310. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  2311. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  2312. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  2313. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  2314. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  2315. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  2316. // ANSI Series
  2317. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  2318. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  2319. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  2320. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  2321. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  2322. // Traditional 'Loose' North American Paper Sizes
  2323. case 'USLEDGER':
  2324. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  2325. case 'ORGANIZERK':
  2326. case 'BIBLE':
  2327. case 'USTABLOID':
  2328. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  2329. case 'ORGANIZERM':
  2330. case 'USLETTER':
  2331. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  2332. case 'USLEGAL':
  2333. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  2334. case 'GOVERNMENTLETTER':
  2335. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  2336. case 'JUNIORLEGAL':
  2337. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  2338. // Other North American Paper Sizes
  2339. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  2340. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  2341. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  2342. case 'GOVERNMENTLEGAL':
  2343. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  2344. case 'MONARCH':
  2345. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  2346. case 'ORGANIZERL':
  2347. case 'STATEMENT':
  2348. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  2349. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  2350. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  2351. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  2352. // Canadian standard CAN 2-9.60M
  2353. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  2354. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  2355. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  2356. case 'P4': {$pf = array( 609.449, 793.701); break;}
  2357. case 'P5': {$pf = array( 396.850, 609.449); break;}
  2358. case 'P6': {$pf = array( 303.307, 396.850); break;}
  2359. // North American Architectural Sizes
  2360. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  2361. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  2362. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  2363. case 'BROADSHEET':
  2364. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  2365. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  2366. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  2367. // --- North American Envelope Sizes ---
  2368. // - Announcement Envelopes
  2369. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  2370. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  2371. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  2372. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  2373. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  2374. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  2375. // - Commercial Envelopes
  2376. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  2377. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  2378. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  2379. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  2380. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  2381. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  2382. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  2383. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  2384. // - Catalogue Envelopes
  2385. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  2386. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  2387. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  2388. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  2389. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  2390. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  2391. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  2392. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  2393. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  2394. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  2395. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  2396. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  2397. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  2398. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  2399. // Japanese (JIS P 0138-61) Standard B-Series
  2400. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  2401. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  2402. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  2403. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  2404. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  2405. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  2406. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  2407. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  2408. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  2409. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  2410. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  2411. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  2412. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  2413. // PA Series
  2414. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  2415. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  2416. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  2417. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  2418. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  2419. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  2420. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  2421. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  2422. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  2423. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  2424. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  2425. // Standard Photographic Print Sizes
  2426. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  2427. case 'E' : {$pf = array( 233.858, 340.157); break;}
  2428. case 'L':
  2429. case '3R' : {$pf = array( 252.283, 360.000); break;}
  2430. case 'KG':
  2431. case '4R' : {$pf = array( 289.134, 430.866); break;}
  2432. case '4D' : {$pf = array( 340.157, 430.866); break;}
  2433. case '2L':
  2434. case '5R' : {$pf = array( 360.000, 504.567); break;}
  2435. case '8P':
  2436. case '6R' : {$pf = array( 430.866, 575.433); break;}
  2437. case '6P':
  2438. case '8R' : {$pf = array( 575.433, 720.000); break;}
  2439. case '6PW':
  2440. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  2441. case '4P':
  2442. case '10R' : {$pf = array( 720.000, 864.567); break;}
  2443. case '4PW':
  2444. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  2445. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  2446. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  2447. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  2448. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  2449. // Common Newspaper Sizes
  2450. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  2451. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  2452. case 'NEWSPAPER_TABLOID':
  2453. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  2454. // Business Cards
  2455. case 'CREDIT_CARD':
  2456. case 'BUSINESS_CARD':
  2457. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  2458. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  2459. case 'BUSINESS_CARD_IT':
  2460. case 'BUSINESS_CARD_UK':
  2461. case 'BUSINESS_CARD_FR':
  2462. case 'BUSINESS_CARD_DE':
  2463. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  2464. case 'BUSINESS_CARD_CA':
  2465. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  2466. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  2467. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  2468. case 'BUSINESS_CARD_AU':
  2469. case 'BUSINESS_CARD_DK':
  2470. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  2471. case 'BUSINESS_CARD_RU':
  2472. case 'BUSINESS_CARD_CZ':
  2473. case 'BUSINESS_CARD_FI':
  2474. case 'BUSINESS_CARD_HU':
  2475. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  2476. // Billboards
  2477. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  2478. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  2479. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  2480. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  2481. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  2482. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  2483. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  2484. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  2485. // Old European Sizes
  2486. // - Old Imperial English Sizes
  2487. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  2488. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  2489. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  2490. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  2491. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  2492. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  2493. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  2494. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  2495. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  2496. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  2497. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  2498. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  2499. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  2500. case 'EN_SHEET':
  2501. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  2502. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  2503. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  2504. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  2505. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  2506. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  2507. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  2508. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  2509. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  2510. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  2511. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  2512. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  2513. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  2514. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  2515. // - Old Imperial Belgian Sizes
  2516. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  2517. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  2518. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  2519. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  2520. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  2521. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  2522. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  2523. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2524. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  2525. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  2526. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  2527. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  2528. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  2529. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  2530. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  2531. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  2532. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  2533. // - Old Imperial French Sizes
  2534. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  2535. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  2536. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  2537. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  2538. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  2539. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  2540. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  2541. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  2542. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  2543. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  2544. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  2545. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  2546. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  2547. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  2548. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  2549. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  2550. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  2551. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  2552. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  2553. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  2554. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  2555. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  2556. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  2557. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  2558. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  2559. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  2560. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  2561. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  2562. // DEFAULT ISO A4
  2563. default: {$pf = array( 595.276, 841.890); break;}
  2564. }
  2565. return $pf;
  2566. }
  2567. /**
  2568. * Change the format of the current page
  2569. * @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>
  2570. * <li>['format'] = page format name (one of the above);</li>
  2571. * <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>
  2572. * <li>['PZ'] : The page's preferred zoom (magnification) factor.</li>
  2573. * <li>['MediaBox'] : the boundaries of the physical medium on which the page shall be displayed or printed:</li>
  2574. * <li>['MediaBox']['llx'] : lower-left x coordinate in points</li>
  2575. * <li>['MediaBox']['lly'] : lower-left y coordinate in points</li>
  2576. * <li>['MediaBox']['urx'] : upper-right x coordinate in points</li>
  2577. * <li>['MediaBox']['ury'] : upper-right y coordinate in points</li>
  2578. * <li>['CropBox'] : the visible region of default user space:</li>
  2579. * <li>['CropBox']['llx'] : lower-left x coordinate in points</li>
  2580. * <li>['CropBox']['lly'] : lower-left y coordinate in points</li>
  2581. * <li>['CropBox']['urx'] : upper-right x coordinate in points</li>
  2582. * <li>['CropBox']['ury'] : upper-right y coordinate in points</li>
  2583. * <li>['BleedBox'] : the region to which the contents of the page shall be clipped when output in a production environment:</li>
  2584. * <li>['BleedBox']['llx'] : lower-left x coordinate in points</li>
  2585. * <li>['BleedBox']['lly'] : lower-left y coordinate in points</li>
  2586. * <li>['BleedBox']['urx'] : upper-right x coordinate in points</li>
  2587. * <li>['BleedBox']['ury'] : upper-right y coordinate in points</li>
  2588. * <li>['TrimBox'] : the intended dimensions of the finished page after trimming:</li>
  2589. * <li>['TrimBox']['llx'] : lower-left x coordinate in points</li>
  2590. * <li>['TrimBox']['lly'] : lower-left y coordinate in points</li>
  2591. * <li>['TrimBox']['urx'] : upper-right x coordinate in points</li>
  2592. * <li>['TrimBox']['ury'] : upper-right y coordinate in points</li>
  2593. * <li>['ArtBox'] : the extent of the page's meaningful content:</li>
  2594. * <li>['ArtBox']['llx'] : lower-left x coordinate in points</li>
  2595. * <li>['ArtBox']['lly'] : lower-left y coordinate in points</li>
  2596. * <li>['ArtBox']['urx'] : upper-right x coordinate in points</li>
  2597. * <li>['ArtBox']['ury'] : upper-right y coordinate in points</li>
  2598. * <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>
  2599. * <li>['BoxColorInfo'][BOXTYPE]['C'] : an array of three numbers in the range 0-255, representing the components in the DeviceRGB colour space.</li>
  2600. * <li>['BoxColorInfo'][BOXTYPE]['W'] : the guideline width in default user units</li>
  2601. * <li>['BoxColorInfo'][BOXTYPE]['S'] : the guideline style: S = Solid; D = Dashed</li>
  2602. * <li>['BoxColorInfo'][BOXTYPE]['D'] : dash array defining a pattern of dashes and gaps to be used in drawing dashed guidelines</li>
  2603. * <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>
  2604. * <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>
  2605. * <li>['trans']['S'] : transition style : Split, Blinds, Box, Wipe, Dissolve, Glitter, R, Fly, Push, Cover, Uncover, Fade</li>
  2606. * <li>['trans']['D'] : The duration of the transition effect, in seconds.</li>
  2607. * <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>
  2608. * <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>
  2609. * <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>
  2610. * <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>
  2611. * <li>['trans']['B'] : (Fly transition style only) If true, the area that shall be flown in is rectangular and opaque. Default: false.</li>
  2612. * </ul>
  2613. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul>
  2614. * <li>P or Portrait (default)</li>
  2615. * <li>L or Landscape</li>
  2616. * <li>'' (empty string) for automatic orientation</li>
  2617. * </ul>
  2618. * @protected
  2619. * @since 3.0.015 (2008-06-06)
  2620. * @see getPageSizeFromFormat()
  2621. */
  2622. protected function setPageFormat($format, $orientation='P') {
  2623. if (!empty($format) AND isset($this->pagedim[$this->page])) {
  2624. // remove inherited values
  2625. unset($this->pagedim[$this->page]);
  2626. }
  2627. if (is_string($format)) {
  2628. // get page measures from format name
  2629. $pf = $this->getPageSizeFromFormat($format);
  2630. $this->fwPt = $pf[0];
  2631. $this->fhPt = $pf[1];
  2632. } else {
  2633. // the boundaries of the physical medium on which the page shall be displayed or printed
  2634. if (isset($format['MediaBox'])) {
  2635. $this->setPageBoxes($this->page, 'MediaBox', $format['MediaBox']['llx'], $format['MediaBox']['lly'], $format['MediaBox']['urx'], $format['MediaBox']['ury'], false);
  2636. $this->fwPt = (($format['MediaBox']['urx'] - $format['MediaBox']['llx']) * $this->k);
  2637. $this->fhPt = (($format['MediaBox']['ury'] - $format['MediaBox']['lly']) * $this->k);
  2638. } else {
  2639. if (isset($format[0]) AND is_numeric($format[0]) AND isset($format[1]) AND is_numeric($format[1])) {
  2640. $pf = array(($format[0] * $this->k), ($format[1] * $this->k));
  2641. } else {
  2642. if (!isset($format['format'])) {
  2643. // default value
  2644. $format['format'] = 'A4';
  2645. }
  2646. $pf = $this->getPageSizeFromFormat($format['format']);
  2647. }
  2648. $this->fwPt = $pf[0];
  2649. $this->fhPt = $pf[1];
  2650. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2651. }
  2652. // the visible region of default user space
  2653. if (isset($format['CropBox'])) {
  2654. $this->setPageBoxes($this->page, 'CropBox', $format['CropBox']['llx'], $format['CropBox']['lly'], $format['CropBox']['urx'], $format['CropBox']['ury'], false);
  2655. }
  2656. // the region to which the contents of the page shall be clipped when output in a production environment
  2657. if (isset($format['BleedBox'])) {
  2658. $this->setPageBoxes($this->page, 'BleedBox', $format['BleedBox']['llx'], $format['BleedBox']['lly'], $format['BleedBox']['urx'], $format['BleedBox']['ury'], false);
  2659. }
  2660. // the intended dimensions of the finished page after trimming
  2661. if (isset($format['TrimBox'])) {
  2662. $this->setPageBoxes($this->page, 'TrimBox', $format['TrimBox']['llx'], $format['TrimBox']['lly'], $format['TrimBox']['urx'], $format['TrimBox']['ury'], false);
  2663. }
  2664. // the page's meaningful content (including potential white space)
  2665. if (isset($format['ArtBox'])) {
  2666. $this->setPageBoxes($this->page, 'ArtBox', $format['ArtBox']['llx'], $format['ArtBox']['lly'], $format['ArtBox']['urx'], $format['ArtBox']['ury'], false);
  2667. }
  2668. // specify the colours and other visual characteristics that should be used in displaying guidelines on the screen for the various page boundaries
  2669. if (isset($format['BoxColorInfo'])) {
  2670. $this->pagedim[$this->page]['BoxColorInfo'] = $format['BoxColorInfo'];
  2671. }
  2672. if (isset($format['Rotate']) AND (($format['Rotate'] % 90) == 0)) {
  2673. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2674. $this->pagedim[$this->page]['Rotate'] = intval($format['Rotate']);
  2675. }
  2676. if (isset($format['PZ'])) {
  2677. // The page's preferred zoom (magnification) factor
  2678. $this->pagedim[$this->page]['PZ'] = floatval($format['PZ']);
  2679. }
  2680. if (isset($format['trans'])) {
  2681. // The style and duration of the visual transition to use when moving from another page to the given page during a presentation
  2682. if (isset($format['trans']['Dur'])) {
  2683. // The page's display duration
  2684. $this->pagedim[$this->page]['trans']['Dur'] = floatval($format['trans']['Dur']);
  2685. }
  2686. $stansition_styles = array('Split', 'Blinds', 'Box', 'Wipe', 'Dissolve', 'Glitter', 'R', 'Fly', 'Push', 'Cover', 'Uncover', 'Fade');
  2687. if (isset($format['trans']['S']) AND in_array($format['trans']['S'], $stansition_styles)) {
  2688. // The transition style that shall be used when moving to this page from another during a presentation
  2689. $this->pagedim[$this->page]['trans']['S'] = $format['trans']['S'];
  2690. $valid_effect = array('Split', 'Blinds');
  2691. $valid_vals = array('H', 'V');
  2692. if (isset($format['trans']['Dm']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['Dm'], $valid_vals)) {
  2693. $this->pagedim[$this->page]['trans']['Dm'] = $format['trans']['Dm'];
  2694. }
  2695. $valid_effect = array('Split', 'Box', 'Fly');
  2696. $valid_vals = array('I', 'O');
  2697. if (isset($format['trans']['M']) AND in_array($format['trans']['S'], $valid_effect) AND in_array($format['trans']['M'], $valid_vals)) {
  2698. $this->pagedim[$this->page]['trans']['M'] = $format['trans']['M'];
  2699. }
  2700. $valid_effect = array('Wipe', 'Glitter', 'Fly', 'Cover', 'Uncover', 'Push');
  2701. if (isset($format['trans']['Di']) AND in_array($format['trans']['S'], $valid_effect)) {
  2702. if (((($format['trans']['Di'] == 90) OR ($format['trans']['Di'] == 180)) AND ($format['trans']['S'] == 'Wipe'))
  2703. OR (($format['trans']['Di'] == 315) AND ($format['trans']['S'] == 'Glitter'))
  2704. OR (($format['trans']['Di'] == 0) OR ($format['trans']['Di'] == 270))) {
  2705. $this->pagedim[$this->page]['trans']['Di'] = intval($format['trans']['Di']);
  2706. }
  2707. }
  2708. if (isset($format['trans']['SS']) AND ($format['trans']['S'] == 'Fly')) {
  2709. $this->pagedim[$this->page]['trans']['SS'] = floatval($format['trans']['SS']);
  2710. }
  2711. if (isset($format['trans']['B']) AND ($format['trans']['B'] === true) AND ($format['trans']['S'] == 'Fly')) {
  2712. $this->pagedim[$this->page]['trans']['B'] = 'true';
  2713. }
  2714. } else {
  2715. $this->pagedim[$this->page]['trans']['S'] = 'R';
  2716. }
  2717. if (isset($format['trans']['D'])) {
  2718. // The duration of the transition effect, in seconds
  2719. $this->pagedim[$this->page]['trans']['D'] = floatval($format['trans']['D']);
  2720. } else {
  2721. $this->pagedim[$this->page]['trans']['D'] = 1;
  2722. }
  2723. }
  2724. }
  2725. $this->setPageOrientation($orientation);
  2726. }
  2727. /**
  2728. * Set page boundaries.
  2729. * @param $page (int) page number
  2730. * @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>
  2731. * @param $llx (float) lower-left x coordinate in user units
  2732. * @param $lly (float) lower-left y coordinate in user units
  2733. * @param $urx (float) upper-right x coordinate in user units
  2734. * @param $ury (float) upper-right y coordinate in user units
  2735. * @param $points (boolean) if true uses user units as unit of measure, otherwise uses PDF points
  2736. * @public
  2737. * @since 5.0.010 (2010-05-17)
  2738. */
  2739. public function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false) {
  2740. if (!isset($this->pagedim[$page])) {
  2741. // initialize array
  2742. $this->pagedim[$page] = array();
  2743. }
  2744. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2745. if (!in_array($type, $pageboxes)) {
  2746. return;
  2747. }
  2748. if ($points) {
  2749. $k = 1;
  2750. } else {
  2751. $k = $this->k;
  2752. }
  2753. $this->pagedim[$page][$type]['llx'] = ($llx * $k);
  2754. $this->pagedim[$page][$type]['lly'] = ($lly * $k);
  2755. $this->pagedim[$page][$type]['urx'] = ($urx * $k);
  2756. $this->pagedim[$page][$type]['ury'] = ($ury * $k);
  2757. }
  2758. /**
  2759. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  2760. * @param $page (int) page number
  2761. * @protected
  2762. * @since 5.0.010 (2010-05-17)
  2763. */
  2764. protected function swapPageBoxCoordinates($page) {
  2765. $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  2766. foreach ($pageboxes as $type) {
  2767. // swap X and Y coordinates
  2768. if (isset($this->pagedim[$page][$type])) {
  2769. $tmp = $this->pagedim[$page][$type]['llx'];
  2770. $this->pagedim[$page][$type]['llx'] = $this->pagedim[$page][$type]['lly'];
  2771. $this->pagedim[$page][$type]['lly'] = $tmp;
  2772. $tmp = $this->pagedim[$page][$type]['urx'];
  2773. $this->pagedim[$page][$type]['urx'] = $this->pagedim[$page][$type]['ury'];
  2774. $this->pagedim[$page][$type]['ury'] = $tmp;
  2775. }
  2776. }
  2777. }
  2778. /**
  2779. * Set page orientation.
  2780. * @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>
  2781. * @param $autopagebreak (boolean) Boolean indicating if auto-page-break mode should be on or off.
  2782. * @param $bottommargin (float) bottom margin of the page.
  2783. * @public
  2784. * @since 3.0.015 (2008-06-06)
  2785. */
  2786. public function setPageOrientation($orientation, $autopagebreak='', $bottommargin='') {
  2787. if (!isset($this->pagedim[$this->page]['MediaBox'])) {
  2788. // the boundaries of the physical medium on which the page shall be displayed or printed
  2789. $this->setPageBoxes($this->page, 'MediaBox', 0, 0, $this->fwPt, $this->fhPt, true);
  2790. }
  2791. if (!isset($this->pagedim[$this->page]['CropBox'])) {
  2792. // the visible region of default user space
  2793. $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);
  2794. }
  2795. if (!isset($this->pagedim[$this->page]['BleedBox'])) {
  2796. // the region to which the contents of the page shall be clipped when output in a production environment
  2797. $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);
  2798. }
  2799. if (!isset($this->pagedim[$this->page]['TrimBox'])) {
  2800. // the intended dimensions of the finished page after trimming
  2801. $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);
  2802. }
  2803. if (!isset($this->pagedim[$this->page]['ArtBox'])) {
  2804. // the page's meaningful content (including potential white space)
  2805. $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);
  2806. }
  2807. if (!isset($this->pagedim[$this->page]['Rotate'])) {
  2808. // The number of degrees by which the page shall be rotated clockwise when displayed or printed. The value shall be a multiple of 90.
  2809. $this->pagedim[$this->page]['Rotate'] = 0;
  2810. }
  2811. if (!isset($this->pagedim[$this->page]['PZ'])) {
  2812. // The page's preferred zoom (magnification) factor
  2813. $this->pagedim[$this->page]['PZ'] = 1;
  2814. }
  2815. if ($this->fwPt > $this->fhPt) {
  2816. // landscape
  2817. $default_orientation = 'L';
  2818. } else {
  2819. // portrait
  2820. $default_orientation = 'P';
  2821. }
  2822. $valid_orientations = array('P', 'L');
  2823. if (empty($orientation)) {
  2824. $orientation = $default_orientation;
  2825. } else {
  2826. $orientation = strtoupper($orientation{0});
  2827. }
  2828. if (in_array($orientation, $valid_orientations) AND ($orientation != $default_orientation)) {
  2829. $this->CurOrientation = $orientation;
  2830. $this->wPt = $this->fhPt;
  2831. $this->hPt = $this->fwPt;
  2832. } else {
  2833. $this->CurOrientation = $default_orientation;
  2834. $this->wPt = $this->fwPt;
  2835. $this->hPt = $this->fhPt;
  2836. }
  2837. if ((abs($this->pagedim[$this->page]['MediaBox']['urx'] - $this->hPt) < $this->feps) AND (abs($this->pagedim[$this->page]['MediaBox']['ury'] - $this->wPt) < $this->feps)){
  2838. // swap X and Y coordinates (change page orientation)
  2839. $this->swapPageBoxCoordinates($this->page);
  2840. }
  2841. $this->w = ($this->wPt / $this->k);
  2842. $this->h = ($this->hPt / $this->k);
  2843. if ($this->empty_string($autopagebreak)) {
  2844. if (isset($this->AutoPageBreak)) {
  2845. $autopagebreak = $this->AutoPageBreak;
  2846. } else {
  2847. $autopagebreak = true;
  2848. }
  2849. }
  2850. if ($this->empty_string($bottommargin)) {
  2851. if (isset($this->bMargin)) {
  2852. $bottommargin = $this->bMargin;
  2853. } else {
  2854. // default value = 2 cm
  2855. $bottommargin = 2 * 28.35 / $this->k;
  2856. }
  2857. }
  2858. $this->SetAutoPageBreak($autopagebreak, $bottommargin);
  2859. // store page dimensions
  2860. $this->pagedim[$this->page]['w'] = $this->wPt;
  2861. $this->pagedim[$this->page]['h'] = $this->hPt;
  2862. $this->pagedim[$this->page]['wk'] = $this->w;
  2863. $this->pagedim[$this->page]['hk'] = $this->h;
  2864. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  2865. $this->pagedim[$this->page]['bm'] = $bottommargin;
  2866. $this->pagedim[$this->page]['lm'] = $this->lMargin;
  2867. $this->pagedim[$this->page]['rm'] = $this->rMargin;
  2868. $this->pagedim[$this->page]['pb'] = $autopagebreak;
  2869. $this->pagedim[$this->page]['or'] = $this->CurOrientation;
  2870. $this->pagedim[$this->page]['olm'] = $this->original_lMargin;
  2871. $this->pagedim[$this->page]['orm'] = $this->original_rMargin;
  2872. }
  2873. /**
  2874. * Set regular expression to detect withespaces or word separators.
  2875. * The pattern delimiter must be the forward-slash character "/".
  2876. * Some example patterns are:
  2877. * <pre>
  2878. * Non-Unicode or missing PCRE unicode support: "/[^\S\xa0]/"
  2879. * Unicode and PCRE unicode support: "/[^\S\P{Z}\xa0]/u"
  2880. * Unicode and PCRE unicode support in Chinese mode: "/[^\S\P{Z}\P{Lo}\xa0]/u"
  2881. * if PCRE unicode support is turned ON ("\P" is the negate class of "\p"):
  2882. * "\p{Z}" or "\p{Separator}": any kind of Unicode whitespace or invisible separator.
  2883. * "\p{Lo}" or "\p{Other_Letter}": a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  2884. * "\p{Lo}" is needed for Chinese characters because are packed next to each other without spaces in between.
  2885. * </pre>
  2886. * @param $re (string) regular expression (leave empty for default).
  2887. * @public
  2888. * @since 4.6.016 (2009-06-15)
  2889. */
  2890. public function setSpacesRE($re='/[^\S\xa0]/') {
  2891. $this->re_spaces = $re;
  2892. $re_parts = explode('/', $re);
  2893. // get pattern parts
  2894. $this->re_space = array();
  2895. if (isset($re_parts[1]) AND !empty($re_parts[1])) {
  2896. $this->re_space['p'] = $re_parts[1];
  2897. } else {
  2898. $this->re_space['p'] = '[\s]';
  2899. }
  2900. // set pattern modifiers
  2901. if (isset($re_parts[2]) AND !empty($re_parts[2])) {
  2902. $this->re_space['m'] = $re_parts[2];
  2903. } else {
  2904. $this->re_space['m'] = '';
  2905. }
  2906. }
  2907. /**
  2908. * Enable or disable Right-To-Left language mode
  2909. * @param $enable (Boolean) if true enable Right-To-Left language mode.
  2910. * @param $resetx (Boolean) if true reset the X position on direction change.
  2911. * @public
  2912. * @since 2.0.000 (2008-01-03)
  2913. */
  2914. public function setRTL($enable, $resetx=true) {
  2915. $enable = $enable ? true : false;
  2916. $resetx = ($resetx AND ($enable != $this->rtl));
  2917. $this->rtl = $enable;
  2918. $this->tmprtl = false;
  2919. if ($resetx) {
  2920. $this->Ln(0);
  2921. }
  2922. }
  2923. /**
  2924. * Return the RTL status
  2925. * @return boolean
  2926. * @public
  2927. * @since 4.0.012 (2008-07-24)
  2928. */
  2929. public function getRTL() {
  2930. return $this->rtl;
  2931. }
  2932. /**
  2933. * Force temporary RTL language direction
  2934. * @param $mode (mixed) can be false, 'L' for LTR or 'R' for RTL
  2935. * @public
  2936. * @since 2.1.000 (2008-01-09)
  2937. */
  2938. public function setTempRTL($mode) {
  2939. $newmode = false;
  2940. switch (strtoupper($mode)) {
  2941. case 'LTR':
  2942. case 'L': {
  2943. if ($this->rtl) {
  2944. $newmode = 'L';
  2945. }
  2946. break;
  2947. }
  2948. case 'RTL':
  2949. case 'R': {
  2950. if (!$this->rtl) {
  2951. $newmode = 'R';
  2952. }
  2953. break;
  2954. }
  2955. case false:
  2956. default: {
  2957. $newmode = false;
  2958. break;
  2959. }
  2960. }
  2961. $this->tmprtl = $newmode;
  2962. }
  2963. /**
  2964. * Return the current temporary RTL status
  2965. * @return boolean
  2966. * @public
  2967. * @since 4.8.014 (2009-11-04)
  2968. */
  2969. public function isRTLTextDir() {
  2970. return ($this->rtl OR ($this->tmprtl == 'R'));
  2971. }
  2972. /**
  2973. * Set the last cell height.
  2974. * @param $h (float) cell height.
  2975. * @author Nicola Asuni
  2976. * @public
  2977. * @since 1.53.0.TC034
  2978. */
  2979. public function setLastH($h) {
  2980. $this->lasth = $h;
  2981. }
  2982. /**
  2983. * Reset the last cell height.
  2984. * @public
  2985. * @since 5.9.000 (2010-10-03)
  2986. */
  2987. public function resetLastH() {
  2988. $this->lasth = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  2989. }
  2990. /**
  2991. * Get the last cell height.
  2992. * @return last cell height
  2993. * @public
  2994. * @since 4.0.017 (2008-08-05)
  2995. */
  2996. public function getLastH() {
  2997. return $this->lasth;
  2998. }
  2999. /**
  3000. * Set the adjusting factor to convert pixels to user units.
  3001. * @param $scale (float) adjusting factor to convert pixels to user units.
  3002. * @author Nicola Asuni
  3003. * @public
  3004. * @since 1.5.2
  3005. */
  3006. public function setImageScale($scale) {
  3007. $this->imgscale = $scale;
  3008. }
  3009. /**
  3010. * Returns the adjusting factor to convert pixels to user units.
  3011. * @return float adjusting factor to convert pixels to user units.
  3012. * @author Nicola Asuni
  3013. * @public
  3014. * @since 1.5.2
  3015. */
  3016. public function getImageScale() {
  3017. return $this->imgscale;
  3018. }
  3019. /**
  3020. * Returns an array of page dimensions:
  3021. * <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>
  3022. * @param $pagenum (int) page number (empty = current page)
  3023. * @return array of page dimensions.
  3024. * @author Nicola Asuni
  3025. * @public
  3026. * @since 4.5.027 (2009-03-16)
  3027. */
  3028. public function getPageDimensions($pagenum='') {
  3029. if (empty($pagenum)) {
  3030. $pagenum = $this->page;
  3031. }
  3032. return $this->pagedim[$pagenum];
  3033. }
  3034. /**
  3035. * Returns the page width in units.
  3036. * @param $pagenum (int) page number (empty = current page)
  3037. * @return int page width.
  3038. * @author Nicola Asuni
  3039. * @public
  3040. * @since 1.5.2
  3041. * @see getPageDimensions()
  3042. */
  3043. public function getPageWidth($pagenum='') {
  3044. if (empty($pagenum)) {
  3045. return $this->w;
  3046. }
  3047. return $this->pagedim[$pagenum]['w'];
  3048. }
  3049. /**
  3050. * Returns the page height in units.
  3051. * @param $pagenum (int) page number (empty = current page)
  3052. * @return int page height.
  3053. * @author Nicola Asuni
  3054. * @public
  3055. * @since 1.5.2
  3056. * @see getPageDimensions()
  3057. */
  3058. public function getPageHeight($pagenum='') {
  3059. if (empty($pagenum)) {
  3060. return $this->h;
  3061. }
  3062. return $this->pagedim[$pagenum]['h'];
  3063. }
  3064. /**
  3065. * Returns the page break margin.
  3066. * @param $pagenum (int) page number (empty = current page)
  3067. * @return int page break margin.
  3068. * @author Nicola Asuni
  3069. * @public
  3070. * @since 1.5.2
  3071. * @see getPageDimensions()
  3072. */
  3073. public function getBreakMargin($pagenum='') {
  3074. if (empty($pagenum)) {
  3075. return $this->bMargin;
  3076. }
  3077. return $this->pagedim[$pagenum]['bm'];
  3078. }
  3079. /**
  3080. * Returns the scale factor (number of points in user unit).
  3081. * @return int scale factor.
  3082. * @author Nicola Asuni
  3083. * @public
  3084. * @since 1.5.2
  3085. */
  3086. public function getScaleFactor() {
  3087. return $this->k;
  3088. }
  3089. /**
  3090. * Defines the left, top and right margins.
  3091. * @param $left (float) Left margin.
  3092. * @param $top (float) Top margin.
  3093. * @param $right (float) Right margin. Default value is the left one.
  3094. * @param $keepmargins (boolean) if true overwrites the default page margins
  3095. * @public
  3096. * @since 1.0
  3097. * @see SetLeftMargin(), SetTopMargin(), SetRightMargin(), SetAutoPageBreak()
  3098. */
  3099. public function SetMargins($left, $top, $right=-1, $keepmargins=false) {
  3100. //Set left, top and right margins
  3101. $this->lMargin = $left;
  3102. $this->tMargin = $top;
  3103. if ($right == -1) {
  3104. $right = $left;
  3105. }
  3106. $this->rMargin = $right;
  3107. if ($keepmargins) {
  3108. // overwrite original values
  3109. $this->original_lMargin = $this->lMargin;
  3110. $this->original_rMargin = $this->rMargin;
  3111. }
  3112. }
  3113. /**
  3114. * 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.
  3115. * @param $margin (float) The margin.
  3116. * @public
  3117. * @since 1.4
  3118. * @see SetTopMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3119. */
  3120. public function SetLeftMargin($margin) {
  3121. //Set left margin
  3122. $this->lMargin = $margin;
  3123. if (($this->page > 0) AND ($this->x < $margin)) {
  3124. $this->x = $margin;
  3125. }
  3126. }
  3127. /**
  3128. * Defines the top margin. The method can be called before creating the first page.
  3129. * @param $margin (float) The margin.
  3130. * @public
  3131. * @since 1.5
  3132. * @see SetLeftMargin(), SetRightMargin(), SetAutoPageBreak(), SetMargins()
  3133. */
  3134. public function SetTopMargin($margin) {
  3135. //Set top margin
  3136. $this->tMargin = $margin;
  3137. if (($this->page > 0) AND ($this->y < $margin)) {
  3138. $this->y = $margin;
  3139. }
  3140. }
  3141. /**
  3142. * Defines the right margin. The method can be called before creating the first page.
  3143. * @param $margin (float) The margin.
  3144. * @public
  3145. * @since 1.5
  3146. * @see SetLeftMargin(), SetTopMargin(), SetAutoPageBreak(), SetMargins()
  3147. */
  3148. public function SetRightMargin($margin) {
  3149. $this->rMargin = $margin;
  3150. if (($this->page > 0) AND ($this->x > ($this->w - $margin))) {
  3151. $this->x = $this->w - $margin;
  3152. }
  3153. }
  3154. /**
  3155. * Set the same internal Cell padding for top, right, bottom, left-
  3156. * @param $pad (float) internal padding.
  3157. * @public
  3158. * @since 2.1.000 (2008-01-09)
  3159. * @see getCellPaddings(), setCellPaddings()
  3160. */
  3161. public function SetCellPadding($pad) {
  3162. if ($pad >= 0) {
  3163. $this->cell_padding['L'] = $pad;
  3164. $this->cell_padding['T'] = $pad;
  3165. $this->cell_padding['R'] = $pad;
  3166. $this->cell_padding['B'] = $pad;
  3167. }
  3168. }
  3169. /**
  3170. * Set the internal Cell paddings.
  3171. * @param $left (float) left padding
  3172. * @param $top (float) top padding
  3173. * @param $right (float) right padding
  3174. * @param $bottom (float) bottom padding
  3175. * @public
  3176. * @since 5.9.000 (2010-10-03)
  3177. * @see getCellPaddings(), SetCellPadding()
  3178. */
  3179. public function setCellPaddings($left='', $top='', $right='', $bottom='') {
  3180. if (($left !== '') AND ($left >= 0)) {
  3181. $this->cell_padding['L'] = $left;
  3182. }
  3183. if (($top !== '') AND ($top >= 0)) {
  3184. $this->cell_padding['T'] = $top;
  3185. }
  3186. if (($right !== '') AND ($right >= 0)) {
  3187. $this->cell_padding['R'] = $right;
  3188. }
  3189. if (($bottom !== '') AND ($bottom >= 0)) {
  3190. $this->cell_padding['B'] = $bottom;
  3191. }
  3192. }
  3193. /**
  3194. * Get the internal Cell padding array.
  3195. * @return array of padding values
  3196. * @public
  3197. * @since 5.9.000 (2010-10-03)
  3198. * @see setCellPaddings(), SetCellPadding()
  3199. */
  3200. public function getCellPaddings() {
  3201. return $this->cell_padding;
  3202. }
  3203. /**
  3204. * Set the internal Cell margins.
  3205. * @param $left (float) left margin
  3206. * @param $top (float) top margin
  3207. * @param $right (float) right margin
  3208. * @param $bottom (float) bottom margin
  3209. * @public
  3210. * @since 5.9.000 (2010-10-03)
  3211. * @see getCellMargins()
  3212. */
  3213. public function setCellMargins($left='', $top='', $right='', $bottom='') {
  3214. if (($left !== '') AND ($left >= 0)) {
  3215. $this->cell_margin['L'] = $left;
  3216. }
  3217. if (($top !== '') AND ($top >= 0)) {
  3218. $this->cell_margin['T'] = $top;
  3219. }
  3220. if (($right !== '') AND ($right >= 0)) {
  3221. $this->cell_margin['R'] = $right;
  3222. }
  3223. if (($bottom !== '') AND ($bottom >= 0)) {
  3224. $this->cell_margin['B'] = $bottom;
  3225. }
  3226. }
  3227. /**
  3228. * Get the internal Cell margin array.
  3229. * @return array of margin values
  3230. * @public
  3231. * @since 5.9.000 (2010-10-03)
  3232. * @see setCellMargins()
  3233. */
  3234. public function getCellMargins() {
  3235. return $this->cell_margin;
  3236. }
  3237. /**
  3238. * Adjust the internal Cell padding array to take account of the line width.
  3239. * @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)))
  3240. * @return array of adjustments
  3241. * @public
  3242. * @since 5.9.000 (2010-10-03)
  3243. */
  3244. protected function adjustCellPadding($brd=0) {
  3245. if (empty($brd)) {
  3246. return;
  3247. }
  3248. if (is_string($brd)) {
  3249. // convert string to array
  3250. $slen = strlen($brd);
  3251. $newbrd = array();
  3252. for ($i = 0; $i < $slen; ++$i) {
  3253. $newbrd[$brd[$i]] = true;
  3254. }
  3255. $brd = $newbrd;
  3256. } elseif (($brd === 1) OR ($brd === true) OR (is_numeric($brd) AND (intval($brd) > 0))) {
  3257. $brd = array('LRTB' => true);
  3258. }
  3259. if (!is_array($brd)) {
  3260. return;
  3261. }
  3262. // store current cell padding
  3263. $cp = $this->cell_padding;
  3264. // select border mode
  3265. if (isset($brd['mode'])) {
  3266. $mode = $brd['mode'];
  3267. unset($brd['mode']);
  3268. } else {
  3269. $mode = 'normal';
  3270. }
  3271. // process borders
  3272. foreach ($brd as $border => $style) {
  3273. $line_width = $this->LineWidth;
  3274. if (is_array($style) AND isset($style['width'])) {
  3275. // get border width
  3276. $line_width = $style['width'];
  3277. }
  3278. $adj = 0; // line width inside the cell
  3279. switch ($mode) {
  3280. case 'ext': {
  3281. $adj = 0;
  3282. break;
  3283. }
  3284. case 'int': {
  3285. $adj = $line_width;
  3286. break;
  3287. }
  3288. case 'normal':
  3289. default: {
  3290. $adj = ($line_width / 2);
  3291. break;
  3292. }
  3293. }
  3294. // correct internal cell padding if required to avoid overlap between text and lines
  3295. if ((strpos($border,'T') !== false) AND ($this->cell_padding['T'] < $adj)) {
  3296. $this->cell_padding['T'] = $adj;
  3297. }
  3298. if ((strpos($border,'R') !== false) AND ($this->cell_padding['R'] < $adj)) {
  3299. $this->cell_padding['R'] = $adj;
  3300. }
  3301. if ((strpos($border,'B') !== false) AND ($this->cell_padding['B'] < $adj)) {
  3302. $this->cell_padding['B'] = $adj;
  3303. }
  3304. if ((strpos($border,'L') !== false) AND ($this->cell_padding['L'] < $adj)) {
  3305. $this->cell_padding['L'] = $adj;
  3306. }
  3307. }
  3308. 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']));
  3309. }
  3310. /**
  3311. * 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.
  3312. * @param $auto (boolean) Boolean indicating if mode should be on or off.
  3313. * @param $margin (float) Distance from the bottom of the page.
  3314. * @public
  3315. * @since 1.0
  3316. * @see Cell(), MultiCell(), AcceptPageBreak()
  3317. */
  3318. public function SetAutoPageBreak($auto, $margin=0) {
  3319. $this->AutoPageBreak = $auto ? true : false;
  3320. $this->bMargin = $margin;
  3321. $this->PageBreakTrigger = $this->h - $margin;
  3322. }
  3323. /**
  3324. * Return the auto-page-break mode (true or false).
  3325. * @return boolean auto-page-break mode
  3326. * @public
  3327. * @since 5.9.088
  3328. */
  3329. public function getAutoPageBreak() {
  3330. return $this->AutoPageBreak;
  3331. }
  3332. /**
  3333. * Defines the way the document is to be displayed by the viewer.
  3334. * @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>
  3335. * @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>
  3336. * @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>
  3337. * @public
  3338. * @since 1.2
  3339. */
  3340. public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
  3341. if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
  3342. $this->ZoomMode = $zoom;
  3343. } else {
  3344. $this->Error('Incorrect zoom display mode: '.$zoom);
  3345. }
  3346. switch ($layout) {
  3347. case 'default':
  3348. case 'single':
  3349. case 'SinglePage': {
  3350. $this->LayoutMode = 'SinglePage';
  3351. break;
  3352. }
  3353. case 'continuous':
  3354. case 'OneColumn': {
  3355. $this->LayoutMode = 'OneColumn';
  3356. break;
  3357. }
  3358. case 'two':
  3359. case 'TwoColumnLeft': {
  3360. $this->LayoutMode = 'TwoColumnLeft';
  3361. break;
  3362. }
  3363. case 'TwoColumnRight': {
  3364. $this->LayoutMode = 'TwoColumnRight';
  3365. break;
  3366. }
  3367. case 'TwoPageLeft': {
  3368. $this->LayoutMode = 'TwoPageLeft';
  3369. break;
  3370. }
  3371. case 'TwoPageRight': {
  3372. $this->LayoutMode = 'TwoPageRight';
  3373. break;
  3374. }
  3375. default: {
  3376. $this->LayoutMode = 'SinglePage';
  3377. }
  3378. }
  3379. switch ($mode) {
  3380. case 'UseNone': {
  3381. $this->PageMode = 'UseNone';
  3382. break;
  3383. }
  3384. case 'UseOutlines': {
  3385. $this->PageMode = 'UseOutlines';
  3386. break;
  3387. }
  3388. case 'UseThumbs': {
  3389. $this->PageMode = 'UseThumbs';
  3390. break;
  3391. }
  3392. case 'FullScreen': {
  3393. $this->PageMode = 'FullScreen';
  3394. break;
  3395. }
  3396. case 'UseOC': {
  3397. $this->PageMode = 'UseOC';
  3398. break;
  3399. }
  3400. case '': {
  3401. $this->PageMode = 'UseAttachments';
  3402. break;
  3403. }
  3404. default: {
  3405. $this->PageMode = 'UseNone';
  3406. }
  3407. }
  3408. }
  3409. /**
  3410. * 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.
  3411. * Note: the Zlib extension is required for this feature. If not present, compression will be turned off.
  3412. * @param $compress (boolean) Boolean indicating if compression must be enabled.
  3413. * @public
  3414. * @since 1.4
  3415. */
  3416. public function SetCompression($compress=true) {
  3417. if (function_exists('gzcompress')) {
  3418. $this->compress = $compress ? true : false;
  3419. } else {
  3420. $this->compress = false;
  3421. }
  3422. }
  3423. /**
  3424. * Set flag to force sRGB_IEC61966-2.1 black scaled ICC color profile for the whole document.
  3425. * @param $mode (boolean) If true force sRGB output intent.
  3426. * @public
  3427. * @since 5.9.121 (2011-09-28)
  3428. */
  3429. public function setSRGBmode($mode=false) {
  3430. $this->force_srgb = $mode ? true : false;
  3431. }
  3432. /**
  3433. * Turn on/off Unicode mode for document information dictionary (meta tags).
  3434. * This has effect only when unicode mode is set to false.
  3435. * @param $unicode (boolean) if true set the meta information in Unicode
  3436. * @since 5.9.027 (2010-12-01)
  3437. * @public
  3438. */
  3439. public function SetDocInfoUnicode($unicode=true) {
  3440. $this->docinfounicode = $unicode ? true : false;
  3441. }
  3442. /**
  3443. * Defines the title of the document.
  3444. * @param $title (string) The title.
  3445. * @public
  3446. * @since 1.2
  3447. * @see SetAuthor(), SetCreator(), SetKeywords(), SetSubject()
  3448. */
  3449. public function SetTitle($title) {
  3450. $this->title = $title;
  3451. }
  3452. /**
  3453. * Defines the subject of the document.
  3454. * @param $subject (string) The subject.
  3455. * @public
  3456. * @since 1.2
  3457. * @see SetAuthor(), SetCreator(), SetKeywords(), SetTitle()
  3458. */
  3459. public function SetSubject($subject) {
  3460. $this->subject = $subject;
  3461. }
  3462. /**
  3463. * Defines the author of the document.
  3464. * @param $author (string) The name of the author.
  3465. * @public
  3466. * @since 1.2
  3467. * @see SetCreator(), SetKeywords(), SetSubject(), SetTitle()
  3468. */
  3469. public function SetAuthor($author) {
  3470. $this->author = $author;
  3471. }
  3472. /**
  3473. * Associates keywords with the document, generally in the form 'keyword1 keyword2 ...'.
  3474. * @param $keywords (string) The list of keywords.
  3475. * @public
  3476. * @since 1.2
  3477. * @see SetAuthor(), SetCreator(), SetSubject(), SetTitle()
  3478. */
  3479. public function SetKeywords($keywords) {
  3480. $this->keywords = $keywords;
  3481. }
  3482. /**
  3483. * Defines the creator of the document. This is typically the name of the application that generates the PDF.
  3484. * @param $creator (string) The name of the creator.
  3485. * @public
  3486. * @since 1.2
  3487. * @see SetAuthor(), SetKeywords(), SetSubject(), SetTitle()
  3488. */
  3489. public function SetCreator($creator) {
  3490. $this->creator = $creator;
  3491. }
  3492. /**
  3493. * 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.
  3494. * 2004-06-11 :: Nicola Asuni : changed bold tag with strong
  3495. * @param $msg (string) The error message
  3496. * @public
  3497. * @since 1.0
  3498. */
  3499. public function Error($msg) {
  3500. // unset all class variables
  3501. $this->_destroy(true);
  3502. // exit program and print error
  3503. die('<strong>TCPDF ERROR: </strong>'.$msg);
  3504. }
  3505. /**
  3506. * This method begins the generation of the PDF document.
  3507. * It is not necessary to call it explicitly because AddPage() does it automatically.
  3508. * Note: no page is created by this method
  3509. * @public
  3510. * @since 1.0
  3511. * @see AddPage(), Close()
  3512. */
  3513. public function Open() {
  3514. $this->state = 1;
  3515. }
  3516. /**
  3517. * Terminates the PDF document.
  3518. * It is not necessary to call this method explicitly because Output() does it automatically.
  3519. * If the document contains no page, AddPage() is called to prevent from getting an invalid document.
  3520. * @public
  3521. * @since 1.0
  3522. * @see Open(), Output()
  3523. */
  3524. public function Close() {
  3525. if ($this->state == 3) {
  3526. return;
  3527. }
  3528. if ($this->page == 0) {
  3529. $this->AddPage();
  3530. }
  3531. $this->endLayer();
  3532. if ($this->tcpdflink) {
  3533. // save current graphic settings
  3534. $gvars = $this->getGraphicVars();
  3535. $this->setEqualColumns();
  3536. $this->lastpage(true);
  3537. $this->SetAutoPageBreak(false);
  3538. $this->x = 0;
  3539. $this->y = $this->h - (1 / $this->k);
  3540. $this->lMargin = 0;
  3541. $this->_out('q');
  3542. $font = defined('PDF_FONT_NAME_MAIN')?PDF_FONT_NAME_MAIN:'helvetica';
  3543. $this->SetFont($font, '', 1);
  3544. $this->setTextRenderingMode(0, false, false);
  3545. $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";
  3546. $lnk = "\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67";
  3547. $this->Cell(0, 0, $msg, 0, 0, 'L', 0, $lnk, 0, false, 'D', 'B');
  3548. $this->_out('Q');
  3549. // restore graphic settings
  3550. $this->setGraphicVars($gvars);
  3551. }
  3552. // close page
  3553. $this->endPage();
  3554. // close document
  3555. $this->_enddoc();
  3556. // unset all class variables (except critical ones)
  3557. $this->_destroy(false);
  3558. }
  3559. /**
  3560. * Move pointer at the specified document page and update page dimensions.
  3561. * @param $pnum (int) page number (1 ... numpages)
  3562. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3563. * @public
  3564. * @since 2.1.000 (2008-01-07)
  3565. * @see getPage(), lastpage(), getNumPages()
  3566. */
  3567. public function setPage($pnum, $resetmargins=false) {
  3568. if (($pnum == $this->page) AND ($this->state == 2)) {
  3569. return;
  3570. }
  3571. if (($pnum > 0) AND ($pnum <= $this->numpages)) {
  3572. $this->state = 2;
  3573. // save current graphic settings
  3574. //$gvars = $this->getGraphicVars();
  3575. $oldpage = $this->page;
  3576. $this->page = $pnum;
  3577. $this->wPt = $this->pagedim[$this->page]['w'];
  3578. $this->hPt = $this->pagedim[$this->page]['h'];
  3579. $this->w = $this->pagedim[$this->page]['wk'];
  3580. $this->h = $this->pagedim[$this->page]['hk'];
  3581. $this->tMargin = $this->pagedim[$this->page]['tm'];
  3582. $this->bMargin = $this->pagedim[$this->page]['bm'];
  3583. $this->original_lMargin = $this->pagedim[$this->page]['olm'];
  3584. $this->original_rMargin = $this->pagedim[$this->page]['orm'];
  3585. $this->AutoPageBreak = $this->pagedim[$this->page]['pb'];
  3586. $this->CurOrientation = $this->pagedim[$this->page]['or'];
  3587. $this->SetAutoPageBreak($this->AutoPageBreak, $this->bMargin);
  3588. // restore graphic settings
  3589. //$this->setGraphicVars($gvars);
  3590. if ($resetmargins) {
  3591. $this->lMargin = $this->pagedim[$this->page]['olm'];
  3592. $this->rMargin = $this->pagedim[$this->page]['orm'];
  3593. $this->SetY($this->tMargin);
  3594. } else {
  3595. // account for booklet mode
  3596. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  3597. $deltam = $this->pagedim[$this->page]['olm'] - $this->pagedim[$this->page]['orm'];
  3598. $this->lMargin += $deltam;
  3599. $this->rMargin -= $deltam;
  3600. }
  3601. }
  3602. } else {
  3603. $this->Error('Wrong page number on setPage() function: '.$pnum);
  3604. }
  3605. }
  3606. /**
  3607. * Reset pointer to the last document page.
  3608. * @param $resetmargins (boolean) if true reset left, right, top margins and Y position.
  3609. * @public
  3610. * @since 2.0.000 (2008-01-04)
  3611. * @see setPage(), getPage(), getNumPages()
  3612. */
  3613. public function lastPage($resetmargins=false) {
  3614. $this->setPage($this->getNumPages(), $resetmargins);
  3615. }
  3616. /**
  3617. * Get current document page number.
  3618. * @return int page number
  3619. * @public
  3620. * @since 2.1.000 (2008-01-07)
  3621. * @see setPage(), lastpage(), getNumPages()
  3622. */
  3623. public function getPage() {
  3624. return $this->page;
  3625. }
  3626. /**
  3627. * Get the total number of insered pages.
  3628. * @return int number of pages
  3629. * @public
  3630. * @since 2.1.000 (2008-01-07)
  3631. * @see setPage(), getPage(), lastpage()
  3632. */
  3633. public function getNumPages() {
  3634. return $this->numpages;
  3635. }
  3636. /**
  3637. * Adds a new TOC (Table Of Content) page to the document.
  3638. * @param $orientation (string) page orientation.
  3639. * @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().
  3640. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3641. * @public
  3642. * @since 5.0.001 (2010-05-06)
  3643. * @see AddPage(), startPage(), endPage(), endTOCPage()
  3644. */
  3645. public function addTOCPage($orientation='', $format='', $keepmargins=false) {
  3646. $this->AddPage($orientation, $format, $keepmargins, true);
  3647. }
  3648. /**
  3649. * Terminate the current TOC (Table Of Content) page
  3650. * @public
  3651. * @since 5.0.001 (2010-05-06)
  3652. * @see AddPage(), startPage(), endPage(), addTOCPage()
  3653. */
  3654. public function endTOCPage() {
  3655. $this->endPage(true);
  3656. }
  3657. /**
  3658. * 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).
  3659. * The origin of the coordinate system is at the top-left corner (or top-right for RTL) and increasing ordinates go downwards.
  3660. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3661. * @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().
  3662. * @param $keepmargins (boolean) if true overwrites the default page margins with the current margins
  3663. * @param $tocpage (boolean) if true set the tocpage state to true (the added page will be used to display Table Of Content).
  3664. * @public
  3665. * @since 1.0
  3666. * @see startPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3667. */
  3668. public function AddPage($orientation='', $format='', $keepmargins=false, $tocpage=false) {
  3669. if ($this->inxobj) {
  3670. // we are inside an XObject template
  3671. return;
  3672. }
  3673. if (!isset($this->original_lMargin) OR $keepmargins) {
  3674. $this->original_lMargin = $this->lMargin;
  3675. }
  3676. if (!isset($this->original_rMargin) OR $keepmargins) {
  3677. $this->original_rMargin = $this->rMargin;
  3678. }
  3679. // terminate previous page
  3680. $this->endPage();
  3681. // start new page
  3682. $this->startPage($orientation, $format, $tocpage);
  3683. }
  3684. /**
  3685. * Terminate the current page
  3686. * @param $tocpage (boolean) if true set the tocpage state to false (end the page used to display Table Of Content).
  3687. * @public
  3688. * @since 4.2.010 (2008-11-14)
  3689. * @see AddPage(), startPage(), addTOCPage(), endTOCPage()
  3690. */
  3691. public function endPage($tocpage=false) {
  3692. // check if page is already closed
  3693. if (($this->page == 0) OR ($this->numpages > $this->page) OR (!$this->pageopen[$this->page])) {
  3694. return;
  3695. }
  3696. // print page footer
  3697. $this->setFooter();
  3698. // close page
  3699. $this->_endpage();
  3700. // mark page as closed
  3701. $this->pageopen[$this->page] = false;
  3702. if ($tocpage) {
  3703. $this->tocpage = false;
  3704. }
  3705. }
  3706. /**
  3707. * Starts a new page to the document. The page must be closed using the endPage() function.
  3708. * The origin of the coordinate system is at the top-left corner and increasing ordinates go downwards.
  3709. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  3710. * @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().
  3711. * @param $tocpage (boolean) if true the page is designated to contain the Table-Of-Content.
  3712. * @since 4.2.010 (2008-11-14)
  3713. * @see AddPage(), endPage(), addTOCPage(), endTOCPage(), getPageSizeFromFormat(), setPageFormat()
  3714. * @public
  3715. */
  3716. public function startPage($orientation='', $format='', $tocpage=false) {
  3717. if ($tocpage) {
  3718. $this->tocpage = true;
  3719. }
  3720. // move page numbers of documents to be attached
  3721. if ($this->tocpage) {
  3722. // move reference to unexistent pages (used for page attachments)
  3723. // adjust outlines
  3724. $tmpoutlines = $this->outlines;
  3725. foreach ($tmpoutlines as $key => $outline) {
  3726. if ($outline['p'] > $this->numpages) {
  3727. $this->outlines[$key]['p'] = ($outline['p'] + 1);
  3728. }
  3729. }
  3730. // adjust dests
  3731. $tmpdests = $this->dests;
  3732. foreach ($tmpdests as $key => $dest) {
  3733. if ($dest['p'] > $this->numpages) {
  3734. $this->dests[$key]['p'] = ($dest['p'] + 1);
  3735. }
  3736. }
  3737. // adjust links
  3738. $tmplinks = $this->links;
  3739. foreach ($tmplinks as $key => $link) {
  3740. if ($link[0] > $this->numpages) {
  3741. $this->links[$key][0] = ($link[0] + 1);
  3742. }
  3743. }
  3744. }
  3745. if ($this->numpages > $this->page) {
  3746. // this page has been already added
  3747. $this->setPage($this->page + 1);
  3748. $this->SetY($this->tMargin);
  3749. return;
  3750. }
  3751. // start a new page
  3752. if ($this->state == 0) {
  3753. $this->Open();
  3754. }
  3755. ++$this->numpages;
  3756. $this->swapMargins($this->booklet);
  3757. // save current graphic settings
  3758. $gvars = $this->getGraphicVars();
  3759. // start new page
  3760. $this->_beginpage($orientation, $format);
  3761. // mark page as open
  3762. $this->pageopen[$this->page] = true;
  3763. // restore graphic settings
  3764. $this->setGraphicVars($gvars);
  3765. // mark this point
  3766. $this->setPageMark();
  3767. // print page header
  3768. $this->setHeader();
  3769. // restore graphic settings
  3770. $this->setGraphicVars($gvars);
  3771. // mark this point
  3772. $this->setPageMark();
  3773. // print table header (if any)
  3774. $this->setTableHeader();
  3775. // set mark for empty page check
  3776. $this->emptypagemrk[$this->page]= $this->pagelen[$this->page];
  3777. }
  3778. /**
  3779. * Set start-writing mark on current page stream used to put borders and fills.
  3780. * Borders and fills are always created after content and inserted on the position marked by this method.
  3781. * This function must be called after calling Image() function for a background image.
  3782. * Background images must be always inserted before calling Multicell() or WriteHTMLCell() or WriteHTML() functions.
  3783. * @public
  3784. * @since 4.0.016 (2008-07-30)
  3785. */
  3786. public function setPageMark() {
  3787. $this->intmrk[$this->page] = $this->pagelen[$this->page];
  3788. $this->bordermrk[$this->page] = $this->intmrk[$this->page];
  3789. $this->setContentMark();
  3790. }
  3791. /**
  3792. * Set start-writing mark on selected page.
  3793. * Borders and fills are always created after content and inserted on the position marked by this method.
  3794. * @param $page (int) page number (default is the current page)
  3795. * @protected
  3796. * @since 4.6.021 (2009-07-20)
  3797. */
  3798. protected function setContentMark($page=0) {
  3799. if ($page <= 0) {
  3800. $page = $this->page;
  3801. }
  3802. if (isset($this->footerlen[$page])) {
  3803. $this->cntmrk[$page] = $this->pagelen[$page] - $this->footerlen[$page];
  3804. } else {
  3805. $this->cntmrk[$page] = $this->pagelen[$page];
  3806. }
  3807. }
  3808. /**
  3809. * Set header data.
  3810. * @param $ln (string) header image logo
  3811. * @param $lw (string) header image logo width in mm
  3812. * @param $ht (string) string to print as title on document header
  3813. * @param $hs (string) string to print on document header
  3814. * @param $tc (array) RGB array color for text.
  3815. * @param $lc (array) RGB array color for line.
  3816. * @public
  3817. */
  3818. public function setHeaderData($ln='', $lw=0, $ht='', $hs='', $tc=array(0,0,0), $lc=array(0,0,0)) {
  3819. $this->header_logo = $ln;
  3820. $this->header_logo_width = $lw;
  3821. $this->header_title = $ht;
  3822. $this->header_string = $hs;
  3823. $this->header_text_color = $tc;
  3824. $this->header_line_color = $lc;
  3825. }
  3826. /**
  3827. * Set footer data.
  3828. * @param $tc (array) RGB array color for text.
  3829. * @param $lc (array) RGB array color for line.
  3830. * @public
  3831. */
  3832. public function setFooterData($tc=array(0,0,0), $lc=array(0,0,0)) {
  3833. $this->footer_text_color = $tc;
  3834. $this->footer_line_color = $lc;
  3835. }
  3836. /**
  3837. * Returns header data:
  3838. * <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>
  3839. * @return array()
  3840. * @public
  3841. * @since 4.0.012 (2008-07-24)
  3842. */
  3843. public function getHeaderData() {
  3844. $ret = array();
  3845. $ret['logo'] = $this->header_logo;
  3846. $ret['logo_width'] = $this->header_logo_width;
  3847. $ret['title'] = $this->header_title;
  3848. $ret['string'] = $this->header_string;
  3849. $ret['text_color'] = $this->header_text_color;
  3850. $ret['line_color'] = $this->header_line_color;
  3851. return $ret;
  3852. }
  3853. /**
  3854. * Set header margin.
  3855. * (minimum distance between header and top page margin)
  3856. * @param $hm (int) distance in user units
  3857. * @public
  3858. */
  3859. public function setHeaderMargin($hm=10) {
  3860. $this->header_margin = $hm;
  3861. }
  3862. /**
  3863. * Returns header margin in user units.
  3864. * @return float
  3865. * @since 4.0.012 (2008-07-24)
  3866. * @public
  3867. */
  3868. public function getHeaderMargin() {
  3869. return $this->header_margin;
  3870. }
  3871. /**
  3872. * Set footer margin.
  3873. * (minimum distance between footer and bottom page margin)
  3874. * @param $fm (int) distance in user units
  3875. * @public
  3876. */
  3877. public function setFooterMargin($fm=10) {
  3878. $this->footer_margin = $fm;
  3879. }
  3880. /**
  3881. * Returns footer margin in user units.
  3882. * @return float
  3883. * @since 4.0.012 (2008-07-24)
  3884. * @public
  3885. */
  3886. public function getFooterMargin() {
  3887. return $this->footer_margin;
  3888. }
  3889. /**
  3890. * Set a flag to print page header.
  3891. * @param $val (boolean) set to true to print the page header (default), false otherwise.
  3892. * @public
  3893. */
  3894. public function setPrintHeader($val=true) {
  3895. $this->print_header = $val ? true : false;
  3896. }
  3897. /**
  3898. * Set a flag to print page footer.
  3899. * @param $val (boolean) set to true to print the page footer (default), false otherwise.
  3900. * @public
  3901. */
  3902. public function setPrintFooter($val=true) {
  3903. $this->print_footer = $val ? true : false;
  3904. }
  3905. /**
  3906. * Return the right-bottom (or left-bottom for RTL) corner X coordinate of last inserted image
  3907. * @return float
  3908. * @public
  3909. */
  3910. public function getImageRBX() {
  3911. return $this->img_rb_x;
  3912. }
  3913. /**
  3914. * Return the right-bottom (or left-bottom for RTL) corner Y coordinate of last inserted image
  3915. * @return float
  3916. * @public
  3917. */
  3918. public function getImageRBY() {
  3919. return $this->img_rb_y;
  3920. }
  3921. /**
  3922. * Reset the xobject template used by Header() method.
  3923. * @public
  3924. */
  3925. public function resetHeaderTemplate() {
  3926. $this->header_xobjid = -1;
  3927. }
  3928. /**
  3929. * Set a flag to automatically reset the xobject template used by Header() method at each page.
  3930. * @param $val (boolean) set to true to reset Header xobject template at each page, false otherwise.
  3931. * @public
  3932. */
  3933. public function setHeaderTemplateAutoreset($val=true) {
  3934. $this->header_xobj_autoreset = $val ? true : false;
  3935. }
  3936. /**
  3937. * This method is used to render the page header.
  3938. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  3939. * @public
  3940. */
  3941. public function Header() {
  3942. if ($this->header_xobjid < 0) {
  3943. // start a new XObject Template
  3944. $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
  3945. $headerfont = $this->getHeaderFont();
  3946. $headerdata = $this->getHeaderData();
  3947. $this->y = $this->header_margin;
  3948. if ($this->rtl) {
  3949. $this->x = $this->w - $this->original_rMargin;
  3950. } else {
  3951. $this->x = $this->original_lMargin;
  3952. }
  3953. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  3954. $imgtype = $this->getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
  3955. if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
  3956. $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3957. } elseif ($imgtype == 'svg') {
  3958. $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3959. } else {
  3960. $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
  3961. }
  3962. $imgy = $this->getImageRBY();
  3963. } else {
  3964. $imgy = $this->y;
  3965. }
  3966. $cell_height = round(($this->cell_height_ratio * $headerfont[2]) / $this->k, 2);
  3967. // set starting margin for text data cell
  3968. if ($this->getRTL()) {
  3969. $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
  3970. } else {
  3971. $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
  3972. }
  3973. $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
  3974. $this->SetTextColorArray($this->header_text_color);
  3975. // header title
  3976. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  3977. $this->SetX($header_x);
  3978. $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  3979. // header string
  3980. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  3981. $this->SetX($header_x);
  3982. $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
  3983. // print an ending header line
  3984. $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
  3985. $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
  3986. if ($this->rtl) {
  3987. $this->SetX($this->original_rMargin);
  3988. } else {
  3989. $this->SetX($this->original_lMargin);
  3990. }
  3991. $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', 'T', 0, 'C');
  3992. $this->endTemplate();
  3993. }
  3994. // print header template
  3995. $x = 0;
  3996. $dx = 0;
  3997. if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
  3998. // adjust margins for booklet mode
  3999. $dx = ($this->original_lMargin - $this->original_rMargin);
  4000. }
  4001. if ($this->rtl) {
  4002. $x = $this->w + $dx;
  4003. } else {
  4004. $x = 0 + $dx;
  4005. }
  4006. $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
  4007. if ($this->header_xobj_autoreset) {
  4008. // reset header xobject template at each page
  4009. $this->header_xobjid = -1;
  4010. }
  4011. }
  4012. /**
  4013. * This method is used to render the page footer.
  4014. * It is automatically called by AddPage() and could be overwritten in your own inherited class.
  4015. * @public
  4016. */
  4017. public function Footer() {
  4018. $cur_y = $this->y;
  4019. $this->SetTextColorArray($this->footer_text_color);
  4020. //set style for cell border
  4021. $line_width = (0.85 / $this->k);
  4022. $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
  4023. //print document barcode
  4024. $barcode = $this->getBarcode();
  4025. if (!empty($barcode)) {
  4026. $this->Ln($line_width);
  4027. $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
  4028. $style = array(
  4029. 'position' => $this->rtl?'R':'L',
  4030. 'align' => $this->rtl?'R':'L',
  4031. 'stretch' => false,
  4032. 'fitwidth' => true,
  4033. 'cellfitalign' => '',
  4034. 'border' => false,
  4035. 'padding' => 0,
  4036. 'fgcolor' => array(0,0,0),
  4037. 'bgcolor' => false,
  4038. 'text' => false
  4039. );
  4040. $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
  4041. }
  4042. if (empty($this->pagegroups)) {
  4043. $pagenumtxt = $this->l['w_page'].' '.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
  4044. } else {
  4045. $pagenumtxt = $this->l['w_page'].' '.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
  4046. }
  4047. $this->SetY($cur_y);
  4048. //Print page number
  4049. if ($this->getRTL()) {
  4050. $this->SetX($this->original_rMargin);
  4051. $this->Cell(0, 0, $pagenumtxt, 'T', 0, 'L');
  4052. } else {
  4053. $this->SetX($this->original_lMargin);
  4054. $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, 'T', 0, 'R');
  4055. }
  4056. }
  4057. /**
  4058. * This method is used to render the page header.
  4059. * @protected
  4060. * @since 4.0.012 (2008-07-24)
  4061. */
  4062. protected function setHeader() {
  4063. if (!$this->print_header) {
  4064. return;
  4065. }
  4066. $this->InHeader = true;
  4067. $this->setGraphicVars($this->default_graphic_vars);
  4068. $temp_thead = $this->thead;
  4069. $temp_theadMargins = $this->theadMargins;
  4070. $lasth = $this->lasth;
  4071. $this->_out('q');
  4072. $this->rMargin = $this->original_rMargin;
  4073. $this->lMargin = $this->original_lMargin;
  4074. $this->SetCellPadding(0);
  4075. //set current position
  4076. if ($this->rtl) {
  4077. $this->SetXY($this->original_rMargin, $this->header_margin);
  4078. } else {
  4079. $this->SetXY($this->original_lMargin, $this->header_margin);
  4080. }
  4081. $this->SetFont($this->header_font[0], $this->header_font[1], $this->header_font[2]);
  4082. $this->Header();
  4083. //restore position
  4084. if ($this->rtl) {
  4085. $this->SetXY($this->original_rMargin, $this->tMargin);
  4086. } else {
  4087. $this->SetXY($this->original_lMargin, $this->tMargin);
  4088. }
  4089. $this->_out('Q');
  4090. $this->lasth = $lasth;
  4091. $this->thead = $temp_thead;
  4092. $this->theadMargins = $temp_theadMargins;
  4093. $this->newline = false;
  4094. $this->InHeader = false;
  4095. }
  4096. /**
  4097. * This method is used to render the page footer.
  4098. * @protected
  4099. * @since 4.0.012 (2008-07-24)
  4100. */
  4101. protected function setFooter() {
  4102. //Page footer
  4103. $this->InFooter = true;
  4104. // save current graphic settings
  4105. $gvars = $this->getGraphicVars();
  4106. // mark this point
  4107. $this->footerpos[$this->page] = $this->pagelen[$this->page];
  4108. $this->_out("\n");
  4109. if ($this->print_footer) {
  4110. $this->setGraphicVars($this->default_graphic_vars);
  4111. $this->current_column = 0;
  4112. $this->num_columns = 1;
  4113. $temp_thead = $this->thead;
  4114. $temp_theadMargins = $this->theadMargins;
  4115. $lasth = $this->lasth;
  4116. $this->_out('q');
  4117. $this->rMargin = $this->original_rMargin;
  4118. $this->lMargin = $this->original_lMargin;
  4119. $this->SetCellPadding(0);
  4120. //set current position
  4121. $footer_y = $this->h - $this->footer_margin;
  4122. if ($this->rtl) {
  4123. $this->SetXY($this->original_rMargin, $footer_y);
  4124. } else {
  4125. $this->SetXY($this->original_lMargin, $footer_y);
  4126. }
  4127. $this->SetFont($this->footer_font[0], $this->footer_font[1], $this->footer_font[2]);
  4128. $this->Footer();
  4129. //restore position
  4130. if ($this->rtl) {
  4131. $this->SetXY($this->original_rMargin, $this->tMargin);
  4132. } else {
  4133. $this->SetXY($this->original_lMargin, $this->tMargin);
  4134. }
  4135. $this->_out('Q');
  4136. $this->lasth = $lasth;
  4137. $this->thead = $temp_thead;
  4138. $this->theadMargins = $temp_theadMargins;
  4139. }
  4140. // restore graphic settings
  4141. $this->setGraphicVars($gvars);
  4142. $this->current_column = $gvars['current_column'];
  4143. $this->num_columns = $gvars['num_columns'];
  4144. // calculate footer length
  4145. $this->footerlen[$this->page] = $this->pagelen[$this->page] - $this->footerpos[$this->page] + 1;
  4146. $this->InFooter = false;
  4147. }
  4148. /**
  4149. * Check if we are on the page body (excluding page header and footer).
  4150. * @return true if we are not in page header nor in page footer, false otherwise.
  4151. * @protected
  4152. * @since 5.9.091 (2011-06-15)
  4153. */
  4154. protected function inPageBody() {
  4155. return (($this->InHeader === false) AND ($this->InFooter === false));
  4156. }
  4157. /**
  4158. * This method is used to render the table header on new page (if any).
  4159. * @protected
  4160. * @since 4.5.030 (2009-03-25)
  4161. */
  4162. protected function setTableHeader() {
  4163. if ($this->num_columns > 1) {
  4164. // multi column mode
  4165. return;
  4166. }
  4167. if (isset($this->theadMargins['top'])) {
  4168. // restore the original top-margin
  4169. $this->tMargin = $this->theadMargins['top'];
  4170. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4171. $this->y = $this->tMargin;
  4172. }
  4173. if (!$this->empty_string($this->thead) AND (!$this->inthead)) {
  4174. // set margins
  4175. $prev_lMargin = $this->lMargin;
  4176. $prev_rMargin = $this->rMargin;
  4177. $prev_cell_padding = $this->cell_padding;
  4178. $this->lMargin = $this->theadMargins['lmargin'] + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$this->theadMargins['page']]['olm']);
  4179. $this->rMargin = $this->theadMargins['rmargin'] + ($this->pagedim[$this->page]['orm'] - $this->pagedim[$this->theadMargins['page']]['orm']);
  4180. $this->cell_padding = $this->theadMargins['cell_padding'];
  4181. if ($this->rtl) {
  4182. $this->x = $this->w - $this->rMargin;
  4183. } else {
  4184. $this->x = $this->lMargin;
  4185. }
  4186. // account for special "cell" mode
  4187. if ($this->theadMargins['cell']) {
  4188. if ($this->rtl) {
  4189. $this->x -= $this->cell_padding['R'];
  4190. } else {
  4191. $this->x += $this->cell_padding['L'];
  4192. }
  4193. }
  4194. // print table header
  4195. $this->writeHTML($this->thead, false, false, false, false, '');
  4196. // set new top margin to skip the table headers
  4197. if (!isset($this->theadMargins['top'])) {
  4198. $this->theadMargins['top'] = $this->tMargin;
  4199. }
  4200. // store end of header position
  4201. if (!isset($this->columns[0]['th'])) {
  4202. $this->columns[0]['th'] = array();
  4203. }
  4204. $this->columns[0]['th']['\''.$this->page.'\''] = $this->y;
  4205. $this->tMargin = $this->y;
  4206. $this->pagedim[$this->page]['tm'] = $this->tMargin;
  4207. $this->lasth = 0;
  4208. $this->lMargin = $prev_lMargin;
  4209. $this->rMargin = $prev_rMargin;
  4210. $this->cell_padding = $prev_cell_padding;
  4211. }
  4212. }
  4213. /**
  4214. * Returns the current page number.
  4215. * @return int page number
  4216. * @public
  4217. * @since 1.0
  4218. * @see getAliasNbPages()
  4219. */
  4220. public function PageNo() {
  4221. return $this->page;
  4222. }
  4223. /**
  4224. * Defines a new spot color.
  4225. * It can be expressed in RGB components or gray scale.
  4226. * The method can be called before the first page is created and the value is retained from page to page.
  4227. * @param $name (string) Full name of the spot color.
  4228. * @param $c (float) Cyan color for CMYK. Value between 0 and 100.
  4229. * @param $m (float) Magenta color for CMYK. Value between 0 and 100.
  4230. * @param $y (float) Yellow color for CMYK. Value between 0 and 100.
  4231. * @param $k (float) Key (Black) color for CMYK. Value between 0 and 100.
  4232. * @public
  4233. * @since 4.0.024 (2008-09-12)
  4234. * @see SetDrawSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4235. */
  4236. public function AddSpotColor($name, $c, $m, $y, $k) {
  4237. if (!isset($this->spot_colors[$name])) {
  4238. $i = (1 + count($this->spot_colors));
  4239. $this->spot_colors[$name] = array('C' => $c, 'M' => $m, 'Y' => $y, 'K' => $k, 'name' => $name, 'i' => $i);
  4240. }
  4241. }
  4242. /**
  4243. * Return the Spot color array.
  4244. * @param $name (string) Name of the spot color.
  4245. * @return (array) Spot color array or false if not defined.
  4246. * @public
  4247. * @since 5.9.125 (2011-10-03)
  4248. */
  4249. public function getSpotColor($name) {
  4250. if (isset($this->spot_colors[$name])) {
  4251. return $this->spot_colors[$name];
  4252. }
  4253. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  4254. $color = strtolower($color);
  4255. if (isset($this->spotcolor[$color])) {
  4256. $this->AddSpotColor($this->spotcolor[$color][4], $this->spotcolor[$color][0], $this->spotcolor[$color][1], $this->spotcolor[$color][2], $this->spotcolor[$color][3]);
  4257. return $this->spot_colors[$this->spotcolor[$color][4]];
  4258. }
  4259. return false;
  4260. }
  4261. /**
  4262. * Set the spot color for the specified type ('draw', 'fill', 'text').
  4263. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4264. * @param $name (string) Name of the spot color.
  4265. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4266. * @return (string) PDF color command.
  4267. * @public
  4268. * @since 5.9.125 (2011-10-03)
  4269. */
  4270. public function setSpotColor($type, $name, $tint=100) {
  4271. $spotcolor = $this->getSpotColor($name);
  4272. if ($spotcolor === false) {
  4273. $this->Error('Undefined spot color: '.$name.', you must add it on the spotcolors.php file.');
  4274. }
  4275. $tint = (max(0, min(100, $tint)) / 100);
  4276. $pdfcolor = sprintf('/CS%d ', $this->spot_colors[$name]['i']);
  4277. switch ($type) {
  4278. case 'draw': {
  4279. $pdfcolor .= sprintf('CS %F SCN', $tint);
  4280. $this->DrawColor = $pdfcolor;
  4281. $this->strokecolor = $spotcolor;
  4282. break;
  4283. }
  4284. case 'fill': {
  4285. $pdfcolor .= sprintf('cs %F scn', $tint);
  4286. $this->FillColor = $pdfcolor;
  4287. $this->bgcolor = $spotcolor;
  4288. break;
  4289. }
  4290. case 'text': {
  4291. $pdfcolor .= sprintf('cs %F scn', $tint);
  4292. $this->TextColor = $pdfcolor;
  4293. $this->fgcolor = $spotcolor;
  4294. break;
  4295. }
  4296. }
  4297. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4298. if ($this->page > 0) {
  4299. $this->_out($pdfcolor);
  4300. }
  4301. if ($this->inxobj) {
  4302. // we are inside an XObject template
  4303. $this->xobjects[$this->xobjid]['spot_colors'][$name] = $this->spot_colors[$name];
  4304. }
  4305. return $pdfcolor;
  4306. }
  4307. /**
  4308. * Defines the spot color used for all drawing operations (lines, rectangles and cell borders).
  4309. * @param $name (string) Name of the spot color.
  4310. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4311. * @public
  4312. * @since 4.0.024 (2008-09-12)
  4313. * @see AddSpotColor(), SetFillSpotColor(), SetTextSpotColor()
  4314. */
  4315. public function SetDrawSpotColor($name, $tint=100) {
  4316. $this->setSpotColor('draw', $name, $tint);
  4317. }
  4318. /**
  4319. * Defines the spot color used for all filling operations (filled rectangles and cell backgrounds).
  4320. * @param $name (string) Name of the spot color.
  4321. * @param $tint (float) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4322. * @public
  4323. * @since 4.0.024 (2008-09-12)
  4324. * @see AddSpotColor(), SetDrawSpotColor(), SetTextSpotColor()
  4325. */
  4326. public function SetFillSpotColor($name, $tint=100) {
  4327. $this->setSpotColor('fill', $name, $tint);
  4328. }
  4329. /**
  4330. * Defines the spot color used for text.
  4331. * @param $name (string) Name of the spot color.
  4332. * @param $tint (int) Intensity of the color (from 0 to 100 ; 100 = full intensity by default).
  4333. * @public
  4334. * @since 4.0.024 (2008-09-12)
  4335. * @see AddSpotColor(), SetDrawSpotColor(), SetFillSpotColor()
  4336. */
  4337. public function SetTextSpotColor($name, $tint=100) {
  4338. $this->setSpotColor('text', $name, $tint);
  4339. }
  4340. /**
  4341. * Set the color array for the specified type ('draw', 'fill', 'text').
  4342. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4343. * The method can be called before the first page is created and the value is retained from page to page.
  4344. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4345. * @param $color (array) Array of colors (1=gray, 3=RGB, 4=CMYK or 5=spotcolor=CMYK+name values).
  4346. * @param $ret (boolean) If true do not send the PDF command.
  4347. * @return (string) The PDF command or empty string.
  4348. * @public
  4349. * @since 3.1.000 (2008-06-11)
  4350. */
  4351. public function setColorArray($type, $color, $ret=false) {
  4352. if (is_array($color)) {
  4353. $color = array_values($color);
  4354. // component: grey, RGB red or CMYK cyan
  4355. $c = isset($color[0]) ? $color[0] : -1;
  4356. // component: RGB green or CMYK magenta
  4357. $m = isset($color[1]) ? $color[1] : -1;
  4358. // component: RGB blue or CMYK yellow
  4359. $y = isset($color[2]) ? $color[2] : -1;
  4360. // component: CMYK black
  4361. $k = isset($color[3]) ? $color[3] : -1;
  4362. // color name
  4363. $name = isset($color[4]) ? $color[4] : '';
  4364. if ($c >= 0) {
  4365. return $this->setColor($type, $c, $m, $y, $k, $ret, $name);
  4366. }
  4367. }
  4368. return '';
  4369. }
  4370. /**
  4371. * Defines the color used for all drawing operations (lines, rectangles and cell borders).
  4372. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4373. * The method can be called before the first page is created and the value is retained from page to page.
  4374. * @param $color (array) Array of colors (1, 3 or 4 values).
  4375. * @param $ret (boolean) If true do not send the PDF command.
  4376. * @return string the PDF command
  4377. * @public
  4378. * @since 3.1.000 (2008-06-11)
  4379. * @see SetDrawColor()
  4380. */
  4381. public function SetDrawColorArray($color, $ret=false) {
  4382. return $this->setColorArray('draw', $color, $ret);
  4383. }
  4384. /**
  4385. * Defines the color used for all filling operations (filled rectangles and cell backgrounds).
  4386. * It can be expressed in RGB, CMYK or GRAY SCALE components.
  4387. * The method can be called before the first page is created and the value is retained from page to page.
  4388. * @param $color (array) Array of colors (1, 3 or 4 values).
  4389. * @param $ret (boolean) If true do not send the PDF command.
  4390. * @public
  4391. * @since 3.1.000 (2008-6-11)
  4392. * @see SetFillColor()
  4393. */
  4394. public function SetFillColorArray($color, $ret=false) {
  4395. return $this->setColorArray('fill', $color, $ret);
  4396. }
  4397. /**
  4398. * Defines the color used for text. It can be expressed in RGB components or gray scale.
  4399. * The method can be called before the first page is created and the value is retained from page to page.
  4400. * @param $color (array) Array of colors (1, 3 or 4 values).
  4401. * @param $ret (boolean) If true do not send the PDF command.
  4402. * @public
  4403. * @since 3.1.000 (2008-6-11)
  4404. * @see SetFillColor()
  4405. */
  4406. public function SetTextColorArray($color, $ret=false) {
  4407. return $this->setColorArray('text', $color, $ret);
  4408. }
  4409. /**
  4410. * Defines the color used by the specified type ('draw', 'fill', 'text').
  4411. * @param $type (string) Type of object affected by this color: ('draw', 'fill', 'text').
  4412. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4413. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4414. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4415. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4416. * @param $ret (boolean) If true do not send the command.
  4417. * @param $name (string) spot color name (if any)
  4418. * @return (string) The PDF command or empty string.
  4419. * @public
  4420. * @since 5.9.125 (2011-10-03)
  4421. */
  4422. public function setColor($type, $col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4423. // set default values
  4424. if (!is_numeric($col1)) {
  4425. $col1 = 0;
  4426. }
  4427. if (!is_numeric($col2)) {
  4428. $col2 = -1;
  4429. }
  4430. if (!is_numeric($col3)) {
  4431. $col3 = -1;
  4432. }
  4433. if (!is_numeric($col4)) {
  4434. $col4 = -1;
  4435. }
  4436. // set color by case
  4437. $suffix = '';
  4438. if (($col2 == -1) AND ($col3 == -1) AND ($col4 == -1)) {
  4439. // Grey scale
  4440. $col1 = max(0, min(255, $col1));
  4441. $intcolor = array('G' => $col1);
  4442. $pdfcolor = sprintf('%F ', ($col1 / 255));
  4443. $suffix = 'g';
  4444. } elseif ($col4 == -1) {
  4445. // RGB
  4446. $col1 = max(0, min(255, $col1));
  4447. $col2 = max(0, min(255, $col2));
  4448. $col3 = max(0, min(255, $col3));
  4449. $intcolor = array('R' => $col1, 'G' => $col2, 'B' => $col3);
  4450. $pdfcolor = sprintf('%F %F %F ', ($col1 / 255), ($col2 / 255), ($col3 / 255));
  4451. $suffix = 'rg';
  4452. } else {
  4453. $col1 = max(0, min(100, $col1));
  4454. $col2 = max(0, min(100, $col2));
  4455. $col3 = max(0, min(100, $col3));
  4456. $col4 = max(0, min(100, $col4));
  4457. if (empty($name)) {
  4458. // CMYK
  4459. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4);
  4460. $pdfcolor = sprintf('%F %F %F %F ', ($col1 / 100), ($col2 / 100), ($col3 / 100), ($col4 / 100));
  4461. $suffix = 'k';
  4462. } else {
  4463. // SPOT COLOR
  4464. $intcolor = array('C' => $col1, 'M' => $col2, 'Y' => $col3, 'K' => $col4, 'name' => $name);
  4465. $this->AddSpotColor($name, $col1, $col2, $col3, $col4);
  4466. $pdfcolor = $this->setSpotColor($type, $name, 100);
  4467. }
  4468. }
  4469. switch ($type) {
  4470. case 'draw': {
  4471. $pdfcolor .= strtoupper($suffix);
  4472. $this->DrawColor = $pdfcolor;
  4473. $this->strokecolor = $intcolor;
  4474. break;
  4475. }
  4476. case 'fill': {
  4477. $pdfcolor .= $suffix;
  4478. $this->FillColor = $pdfcolor;
  4479. $this->bgcolor = $intcolor;
  4480. break;
  4481. }
  4482. case 'text': {
  4483. $pdfcolor .= $suffix;
  4484. $this->TextColor = $pdfcolor;
  4485. $this->fgcolor = $intcolor;
  4486. break;
  4487. }
  4488. }
  4489. $this->ColorFlag = ($this->FillColor != $this->TextColor);
  4490. if (($type != 'text') AND ($this->page > 0)) {
  4491. if (!$ret) {
  4492. $this->_out($pdfcolor);
  4493. }
  4494. return $pdfcolor;
  4495. }
  4496. return '';
  4497. }
  4498. /**
  4499. * Convert a color array into a string representation.
  4500. * @param $c (array) Array of colors.
  4501. * @return (string) The color array representation.
  4502. * @protected
  4503. * @since 5.9.137 (2011-12-01)
  4504. */
  4505. protected function getColorStringFromArray($c) {
  4506. $c = array_values($c);
  4507. $color = '[';
  4508. switch (count($c)) {
  4509. case 4: {
  4510. // CMYK
  4511. $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));
  4512. break;
  4513. }
  4514. case 3: {
  4515. // RGB
  4516. $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));
  4517. break;
  4518. }
  4519. case 1: {
  4520. // grayscale
  4521. $color .= sprintf('%F', (max(0, min(255, floatval($c[0]))) / 255));
  4522. break;
  4523. }
  4524. }
  4525. $color .= ']';
  4526. return $color;
  4527. }
  4528. /**
  4529. * 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.
  4530. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4531. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4532. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4533. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4534. * @param $ret (boolean) If true do not send the command.
  4535. * @param $name (string) spot color name (if any)
  4536. * @return string the PDF command
  4537. * @public
  4538. * @since 1.3
  4539. * @see SetDrawColorArray(), SetFillColor(), SetTextColor(), Line(), Rect(), Cell(), MultiCell()
  4540. */
  4541. public function SetDrawColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4542. return $this->setColor('draw', $col1, $col2, $col3, $col4, $ret, $name);
  4543. }
  4544. /**
  4545. * 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.
  4546. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4547. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4548. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4549. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4550. * @param $ret (boolean) If true do not send the command.
  4551. * @param $name (string) Spot color name (if any).
  4552. * @return (string) The PDF command.
  4553. * @public
  4554. * @since 1.3
  4555. * @see SetFillColorArray(), SetDrawColor(), SetTextColor(), Rect(), Cell(), MultiCell()
  4556. */
  4557. public function SetFillColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4558. return $this->setColor('fill', $col1, $col2, $col3, $col4, $ret, $name);
  4559. }
  4560. /**
  4561. * 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.
  4562. * @param $col1 (float) GRAY level for single color, or Red color for RGB (0-255), or CYAN color for CMYK (0-100).
  4563. * @param $col2 (float) GREEN color for RGB (0-255), or MAGENTA color for CMYK (0-100).
  4564. * @param $col3 (float) BLUE color for RGB (0-255), or YELLOW color for CMYK (0-100).
  4565. * @param $col4 (float) KEY (BLACK) color for CMYK (0-100).
  4566. * @param $ret (boolean) If true do not send the command.
  4567. * @param $name (string) Spot color name (if any).
  4568. * @return (string) Empty string.
  4569. * @public
  4570. * @since 1.3
  4571. * @see SetTextColorArray(), SetDrawColor(), SetFillColor(), Text(), Cell(), MultiCell()
  4572. */
  4573. public function SetTextColor($col1=0, $col2=-1, $col3=-1, $col4=-1, $ret=false, $name='') {
  4574. return $this->setColor('text', $col1, $col2, $col3, $col4, $ret, $name);
  4575. }
  4576. /**
  4577. * Returns the length of a string in user unit. A font must be selected.<br>
  4578. * @param $s (string) The string whose length is to be computed
  4579. * @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.
  4580. * @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.
  4581. * @param $fontsize (float) Font size in points. The default value is the current size.
  4582. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4583. * @return mixed int total string length or array of characted widths
  4584. * @author Nicola Asuni
  4585. * @public
  4586. * @since 1.2
  4587. */
  4588. public function GetStringWidth($s, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4589. return $this->GetArrStringWidth($this->utf8Bidi($this->UTF8StringToArray($s), $s, $this->tmprtl), $fontname, $fontstyle, $fontsize, $getarray);
  4590. }
  4591. /**
  4592. * Returns the string length of an array of chars in user unit or an array of characters widths. A font must be selected.<br>
  4593. * @param $sa (string) The array of chars whose total length is to be computed
  4594. * @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.
  4595. * @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.
  4596. * @param $fontsize (float) Font size in points. The default value is the current size.
  4597. * @param $getarray (boolean) if true returns an array of characters widths, if false returns the total length.
  4598. * @return mixed int total string length or array of characted widths
  4599. * @author Nicola Asuni
  4600. * @public
  4601. * @since 2.4.000 (2008-03-06)
  4602. */
  4603. public function GetArrStringWidth($sa, $fontname='', $fontstyle='', $fontsize=0, $getarray=false) {
  4604. // store current values
  4605. if (!$this->empty_string($fontname)) {
  4606. $prev_FontFamily = $this->FontFamily;
  4607. $prev_FontStyle = $this->FontStyle;
  4608. $prev_FontSizePt = $this->FontSizePt;
  4609. $this->SetFont($fontname, $fontstyle, $fontsize, '', 'default', false);
  4610. }
  4611. // convert UTF-8 array to Latin1 if required
  4612. $sa = $this->UTF8ArrToLatin1($sa);
  4613. $w = 0; // total width
  4614. $wa = array(); // array of characters widths
  4615. foreach ($sa as $ck => $char) {
  4616. // character width
  4617. $cw = $this->GetCharWidth($char, isset($sa[($ck + 1)]));
  4618. $wa[] = $cw;
  4619. $w += $cw;
  4620. }
  4621. // restore previous values
  4622. if (!$this->empty_string($fontname)) {
  4623. $this->SetFont($prev_FontFamily, $prev_FontStyle, $prev_FontSizePt, '', 'default', false);
  4624. }
  4625. if ($getarray) {
  4626. return $wa;
  4627. }
  4628. return $w;
  4629. }
  4630. /**
  4631. * Returns the length of the char in user unit for the current font considering current stretching and spacing (tracking).
  4632. * @param $char (int) The char code whose length is to be returned
  4633. * @param $notlast (boolean) If false ignore the font-spacing.
  4634. * @return float char width
  4635. * @author Nicola Asuni
  4636. * @public
  4637. * @since 2.4.000 (2008-03-06)
  4638. */
  4639. public function GetCharWidth($char, $notlast=true) {
  4640. // get raw width
  4641. $chw = $this->getRawCharWidth($char);
  4642. if (($this->font_spacing < 0) OR (($this->font_spacing > 0) AND $notlast)) {
  4643. // increase/decrease font spacing
  4644. $chw += $this->font_spacing;
  4645. }
  4646. if ($this->font_stretching != 100) {
  4647. // fixed stretching mode
  4648. $chw *= ($this->font_stretching / 100);
  4649. }
  4650. return $chw;
  4651. }
  4652. /**
  4653. * Returns the length of the char in user unit for the current font.
  4654. * @param $char (int) The char code whose length is to be returned
  4655. * @return float char width
  4656. * @author Nicola Asuni
  4657. * @public
  4658. * @since 5.9.000 (2010-09-28)
  4659. */
  4660. public function getRawCharWidth($char) {
  4661. if ($char == 173) {
  4662. // SHY character will not be printed
  4663. return (0);
  4664. }
  4665. if (isset($this->CurrentFont['cw'][$char])) {
  4666. $w = $this->CurrentFont['cw'][$char];
  4667. } elseif (isset($this->CurrentFont['dw'])) {
  4668. // default width
  4669. $w = $this->CurrentFont['dw'];
  4670. } elseif (isset($this->CurrentFont['cw'][32])) {
  4671. // default width
  4672. $w = $this->CurrentFont['cw'][32];
  4673. } else {
  4674. $w = 600;
  4675. }
  4676. return ($w * $this->FontSize / 1000);
  4677. }
  4678. /**
  4679. * Returns the numbero of characters in a string.
  4680. * @param $s (string) The input string.
  4681. * @return int number of characters
  4682. * @public
  4683. * @since 2.0.0001 (2008-01-07)
  4684. */
  4685. public function GetNumChars($s) {
  4686. if ($this->isUnicodeFont()) {
  4687. return count($this->UTF8StringToArray($s));
  4688. }
  4689. return strlen($s);
  4690. }
  4691. /**
  4692. * Fill the list of available fonts ($this->fontlist).
  4693. * @protected
  4694. * @since 4.0.013 (2008-07-28)
  4695. */
  4696. protected function getFontsList() {
  4697. $fontsdir = opendir($this->_getfontpath());
  4698. while (($file = readdir($fontsdir)) !== false) {
  4699. if (substr($file, -4) == '.php') {
  4700. array_push($this->fontlist, strtolower(basename($file, '.php')));
  4701. }
  4702. }
  4703. closedir($fontsdir);
  4704. }
  4705. /**
  4706. * Imports a TrueType, Type1, core, or CID0 font and makes it available.
  4707. * It is necessary to generate a font definition file first (read /fonts/utils/README.TXT).
  4708. * 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.
  4709. * @param $family (string) Font family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
  4710. * @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>
  4711. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4712. * @return array containing the font data, or false in case of error.
  4713. * @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.
  4714. * @public
  4715. * @since 1.5
  4716. * @see SetFont(), setFontSubsetting()
  4717. */
  4718. public function AddFont($family, $style='', $fontfile='', $subset='default') {
  4719. if ($subset === 'default') {
  4720. $subset = $this->font_subsetting;
  4721. }
  4722. if ($this->pdfa_mode) {
  4723. $subset = false;
  4724. }
  4725. if ($this->empty_string($family)) {
  4726. if (!$this->empty_string($this->FontFamily)) {
  4727. $family = $this->FontFamily;
  4728. } else {
  4729. $this->Error('Empty font family');
  4730. }
  4731. }
  4732. // move embedded styles on $style
  4733. if (substr($family, -1) == 'I') {
  4734. $style .= 'I';
  4735. $family = substr($family, 0, -1);
  4736. }
  4737. if (substr($family, -1) == 'B') {
  4738. $style .= 'B';
  4739. $family = substr($family, 0, -1);
  4740. }
  4741. // normalize family name
  4742. $family = strtolower($family);
  4743. if ((!$this->isunicode) AND ($family == 'arial')) {
  4744. $family = 'helvetica';
  4745. }
  4746. if (($family == 'symbol') OR ($family == 'zapfdingbats')) {
  4747. $style = '';
  4748. }
  4749. if ($this->pdfa_mode AND (isset($this->CoreFonts[$family]))) {
  4750. // all fonts must be embedded
  4751. $family = 'pdfa'.$family;
  4752. }
  4753. $tempstyle = strtoupper($style);
  4754. $style = '';
  4755. // underline
  4756. if (strpos($tempstyle, 'U') !== false) {
  4757. $this->underline = true;
  4758. } else {
  4759. $this->underline = false;
  4760. }
  4761. // line-through (deleted)
  4762. if (strpos($tempstyle, 'D') !== false) {
  4763. $this->linethrough = true;
  4764. } else {
  4765. $this->linethrough = false;
  4766. }
  4767. // overline
  4768. if (strpos($tempstyle, 'O') !== false) {
  4769. $this->overline = true;
  4770. } else {
  4771. $this->overline = false;
  4772. }
  4773. // bold
  4774. if (strpos($tempstyle, 'B') !== false) {
  4775. $style .= 'B';
  4776. }
  4777. // oblique
  4778. if (strpos($tempstyle, 'I') !== false) {
  4779. $style .= 'I';
  4780. }
  4781. $bistyle = $style;
  4782. $fontkey = $family.$style;
  4783. $font_style = $style.($this->underline ? 'U' : '').($this->linethrough ? 'D' : '').($this->overline ? 'O' : '');
  4784. $fontdata = array('fontkey' => $fontkey, 'family' => $family, 'style' => $font_style);
  4785. // check if the font has been already added
  4786. $fb = $this->getFontBuffer($fontkey);
  4787. if ($fb !== false) {
  4788. if ($this->inxobj) {
  4789. // we are inside an XObject template
  4790. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $fb['i'];
  4791. }
  4792. return $fontdata;
  4793. }
  4794. if (isset($type)) {
  4795. unset($type);
  4796. }
  4797. if (isset($cw)) {
  4798. unset($cw);
  4799. }
  4800. // get specified font directory (if any)
  4801. $fontdir = false;
  4802. if (!$this->empty_string($fontfile)) {
  4803. $fontdir = dirname($fontfile);
  4804. if ($this->empty_string($fontdir) OR ($fontdir == '.')) {
  4805. $fontdir = '';
  4806. } else {
  4807. $fontdir .= '/';
  4808. }
  4809. }
  4810. $missing_style = false; // true when the font style variation is missing
  4811. // search and include font file
  4812. if ($this->empty_string($fontfile) OR (!file_exists($fontfile))) {
  4813. // build a standard filenames for specified font
  4814. $tmp_fontfile = str_replace(' ', '', $family).strtolower($style).'.php';
  4815. // search files on various directories
  4816. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4817. $fontfile = $fontdir.$tmp_fontfile;
  4818. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4819. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4820. } elseif (file_exists($tmp_fontfile)) {
  4821. $fontfile = $tmp_fontfile;
  4822. } elseif (!$this->empty_string($style)) {
  4823. $missing_style = true;
  4824. // try to remove the style part
  4825. $tmp_fontfile = str_replace(' ', '', $family).'.php';
  4826. if (($fontdir !== false) AND file_exists($fontdir.$tmp_fontfile)) {
  4827. $fontfile = $fontdir.$tmp_fontfile;
  4828. } elseif (file_exists($this->_getfontpath().$tmp_fontfile)) {
  4829. $fontfile = $this->_getfontpath().$tmp_fontfile;
  4830. } else {
  4831. $fontfile = $tmp_fontfile;
  4832. }
  4833. }
  4834. }
  4835. // include font file
  4836. if (file_exists($fontfile)) {
  4837. include($fontfile);
  4838. } else {
  4839. $this->Error('Could not include font definition file: '.$family.'');
  4840. }
  4841. // check font parameters
  4842. if ((!isset($type)) OR (!isset($cw))) {
  4843. $this->Error('The font definition file has a bad format: '.$fontfile.'');
  4844. }
  4845. // SET default parameters
  4846. if (!isset($file) OR $this->empty_string($file)) {
  4847. $file = '';
  4848. }
  4849. if (!isset($enc) OR $this->empty_string($enc)) {
  4850. $enc = '';
  4851. }
  4852. if (!isset($cidinfo) OR $this->empty_string($cidinfo)) {
  4853. $cidinfo = array('Registry'=>'Adobe', 'Ordering'=>'Identity', 'Supplement'=>0);
  4854. $cidinfo['uni2cid'] = array();
  4855. }
  4856. if (!isset($ctg) OR $this->empty_string($ctg)) {
  4857. $ctg = '';
  4858. }
  4859. if (!isset($desc) OR $this->empty_string($desc)) {
  4860. $desc = array();
  4861. }
  4862. if (!isset($up) OR $this->empty_string($up)) {
  4863. $up = -100;
  4864. }
  4865. if (!isset($ut) OR $this->empty_string($ut)) {
  4866. $ut = 50;
  4867. }
  4868. if (!isset($cw) OR $this->empty_string($cw)) {
  4869. $cw = array();
  4870. }
  4871. if (!isset($dw) OR $this->empty_string($dw)) {
  4872. // set default width
  4873. if (isset($desc['MissingWidth']) AND ($desc['MissingWidth'] > 0)) {
  4874. $dw = $desc['MissingWidth'];
  4875. } elseif (isset($cw[32])) {
  4876. $dw = $cw[32];
  4877. } else {
  4878. $dw = 600;
  4879. }
  4880. }
  4881. ++$this->numfonts;
  4882. if ($type == 'core') {
  4883. $name = $this->CoreFonts[$fontkey];
  4884. $subset = false;
  4885. } elseif (($type == 'TrueType') OR ($type == 'Type1')) {
  4886. $subset = false;
  4887. } elseif ($type == 'TrueTypeUnicode') {
  4888. $enc = 'Identity-H';
  4889. } elseif ($type == 'cidfont0') {
  4890. if ($this->pdfa_mode) {
  4891. $this->Error('All fonts must be embedded in PDF/A mode!');
  4892. }
  4893. } else {
  4894. $this->Error('Unknow font type: '.$type.'');
  4895. }
  4896. // set name if unset
  4897. if (!isset($name) OR empty($name)) {
  4898. $name = $fontkey;
  4899. }
  4900. // create artificial font style variations if missing (only works with non-embedded fonts)
  4901. if (($type != 'core') AND $missing_style) {
  4902. // style variations
  4903. $styles = array('' => '', 'B' => ',Bold', 'I' => ',Italic', 'BI' => ',BoldItalic');
  4904. $name .= $styles[$bistyle];
  4905. // artificial bold
  4906. if (strpos($bistyle, 'B') !== false) {
  4907. if (isset($desc['StemV'])) {
  4908. // from normal to bold
  4909. $desc['StemV'] = round($desc['StemV'] * 1.75);
  4910. } else {
  4911. // bold
  4912. $desc['StemV'] = 123;
  4913. }
  4914. }
  4915. // artificial italic
  4916. if (strpos($bistyle, 'I') !== false) {
  4917. if (isset($desc['ItalicAngle'])) {
  4918. $desc['ItalicAngle'] -= 11;
  4919. } else {
  4920. $desc['ItalicAngle'] = -11;
  4921. }
  4922. if (isset($desc['Flags'])) {
  4923. $desc['Flags'] |= 64; //bit 7
  4924. } else {
  4925. $desc['Flags'] = 64;
  4926. }
  4927. }
  4928. }
  4929. // initialize subsetchars to contain default ASCII values (0-255)
  4930. $subsetchars = array_fill(0, 256, true);
  4931. $this->setFontBuffer($fontkey, array('fontkey' => $fontkey, 'i' => $this->numfonts, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'dw' => $dw, 'enc' => $enc, 'cidinfo' => $cidinfo, 'file' => $file, 'ctg' => $ctg, 'subset' => $subset, 'subsetchars' => $subsetchars));
  4932. if ($this->inxobj) {
  4933. // we are inside an XObject template
  4934. $this->xobjects[$this->xobjid]['fonts'][$fontkey] = $this->numfonts;
  4935. }
  4936. if (isset($diff) AND (!empty($diff))) {
  4937. //Search existing encodings
  4938. $d = 0;
  4939. $nb = count($this->diffs);
  4940. for ($i=1; $i <= $nb; ++$i) {
  4941. if ($this->diffs[$i] == $diff) {
  4942. $d = $i;
  4943. break;
  4944. }
  4945. }
  4946. if ($d == 0) {
  4947. $d = $nb + 1;
  4948. $this->diffs[$d] = $diff;
  4949. }
  4950. $this->setFontSubBuffer($fontkey, 'diff', $d);
  4951. }
  4952. if (!$this->empty_string($file)) {
  4953. if (!isset($this->FontFiles[$file])) {
  4954. if ((strcasecmp($type,'TrueType') == 0) OR (strcasecmp($type, 'TrueTypeUnicode') == 0)) {
  4955. $this->FontFiles[$file] = array('length1' => $originalsize, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4956. } elseif ($type != 'core') {
  4957. $this->FontFiles[$file] = array('length1' => $size1, 'length2' => $size2, 'fontdir' => $fontdir, 'subset' => $subset, 'fontkeys' => array($fontkey));
  4958. }
  4959. } else {
  4960. // update fontkeys that are sharing this font file
  4961. $this->FontFiles[$file]['subset'] = ($this->FontFiles[$file]['subset'] AND $subset);
  4962. if (!in_array($fontkey, $this->FontFiles[$file]['fontkeys'])) {
  4963. $this->FontFiles[$file]['fontkeys'][] = $fontkey;
  4964. }
  4965. }
  4966. }
  4967. return $fontdata;
  4968. }
  4969. /**
  4970. * Sets the font used to print character strings.
  4971. * The font can be either a standard one or a font added via the AddFont() method. Standard fonts use Windows encoding cp1252 (Western Europe).
  4972. * The method can be called before the first page is created and the font is retained from page to page.
  4973. * If you just wish to change the current font size, it is simpler to call SetFontSize().
  4974. * 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 />
  4975. * @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.
  4976. * @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.
  4977. * @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
  4978. * @param $fontfile (string) The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  4979. * @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.
  4980. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  4981. * @author Nicola Asuni
  4982. * @public
  4983. * @since 1.0
  4984. * @see AddFont(), SetFontSize()
  4985. */
  4986. public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {
  4987. //Select a font; size given in points
  4988. if ($size === null) {
  4989. $size = $this->FontSizePt;
  4990. }
  4991. if ($size < 0) {
  4992. $size = 0;
  4993. }
  4994. // try to add font (if not already added)
  4995. $fontdata = $this->AddFont($family, $style, $fontfile, $subset);
  4996. $this->FontFamily = $fontdata['family'];
  4997. $this->FontStyle = $fontdata['style'];
  4998. $this->CurrentFont = $this->getFontBuffer($fontdata['fontkey']);
  4999. $this->SetFontSize($size, $out);
  5000. }
  5001. /**
  5002. * Defines the size of the current font.
  5003. * @param $size (float) The font size in points.
  5004. * @param $out (boolean) if true output the font size command, otherwise only set the font properties.
  5005. * @public
  5006. * @since 1.0
  5007. * @see SetFont()
  5008. */
  5009. public function SetFontSize($size, $out=true) {
  5010. // font size in points
  5011. $this->FontSizePt = $size;
  5012. // font size in user units
  5013. $this->FontSize = $size / $this->k;
  5014. // calculate some font metrics
  5015. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  5016. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  5017. $font_height = ((intval($bbox[3]) - intval($bbox[1])) * $size / 1000);
  5018. } else {
  5019. $font_height = $size * 1.219;
  5020. }
  5021. if (isset($this->CurrentFont['desc']['Ascent']) AND ($this->CurrentFont['desc']['Ascent'] > 0)) {
  5022. $font_ascent = ($this->CurrentFont['desc']['Ascent'] * $size / 1000);
  5023. }
  5024. if (isset($this->CurrentFont['desc']['Descent']) AND ($this->CurrentFont['desc']['Descent'] <= 0)) {
  5025. $font_descent = (- $this->CurrentFont['desc']['Descent'] * $size / 1000);
  5026. }
  5027. if (!isset($font_ascent) AND !isset($font_descent)) {
  5028. // core font
  5029. $font_ascent = 0.76 * $font_height;
  5030. $font_descent = $font_height - $font_ascent;
  5031. } elseif (!isset($font_descent)) {
  5032. $font_descent = $font_height - $font_ascent;
  5033. } elseif (!isset($font_ascent)) {
  5034. $font_ascent = $font_height - $font_descent;
  5035. }
  5036. $this->FontAscent = ($font_ascent / $this->k);
  5037. $this->FontDescent = ($font_descent / $this->k);
  5038. if ($out AND ($this->page > 0) AND (isset($this->CurrentFont['i']))) {
  5039. $this->_out(sprintf('BT /F%d %F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
  5040. }
  5041. }
  5042. /**
  5043. * Returns the bounding box of the current font in user units.
  5044. * @return array
  5045. * @public
  5046. * @since 5.9.152 (2012-03-23)
  5047. */
  5048. public function getFontBBox() {
  5049. $result = array();
  5050. if (isset($this->CurrentFont['desc']['FontBBox'])) {
  5051. $bbox = explode(' ', substr($this->CurrentFont['desc']['FontBBox'], 1, -1));
  5052. foreach ($bbox as $v) {
  5053. $result[] = (intval($v) * $this->FontSize / 1000);
  5054. }
  5055. } else {
  5056. // Find max width
  5057. if (isset($this->CurrentFont['desc']['MaxWidth'])) {
  5058. $maxw = (intval($this->CurrentFont['desc']['MaxWidth']) * $this->FontSize / 1000);
  5059. } else {
  5060. $maxw = 0;
  5061. if (isset($this->CurrentFont['desc']['MissingWidth'])) {
  5062. $maxw = max($maxw, $this->CurrentFont['desc']['MissingWidth']);
  5063. }
  5064. if (isset($this->CurrentFont['desc']['AvgWidth'])) {
  5065. $maxw = max($maxw, $this->CurrentFont['desc']['AvgWidth']);
  5066. }
  5067. if (isset($this->CurrentFont['dw'])) {
  5068. $maxw = max($maxw, $this->CurrentFont['dw']);
  5069. }
  5070. foreach ($this->CurrentFont['cw'] as $char => $w) {
  5071. $maxw = max($maxw, $w);
  5072. }
  5073. if ($maxw == 0) {
  5074. $maxw = 600;
  5075. }
  5076. $maxw = ($maxw * $this->FontSize / 1000);
  5077. }
  5078. $result = array(0, -$this->FontDescent, $maxw, $this->FontAscent);
  5079. }
  5080. return $result;
  5081. }
  5082. /**
  5083. * Return the font descent value
  5084. * @param $font (string) font name
  5085. * @param $style (string) font style
  5086. * @param $size (float) The size (in points)
  5087. * @return int font descent
  5088. * @public
  5089. * @author Nicola Asuni
  5090. * @since 4.9.003 (2010-03-30)
  5091. */
  5092. public function getFontDescent($font, $style='', $size=0) {
  5093. $fontdata = $this->AddFont($font, $style);
  5094. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5095. if (isset($fontinfo['desc']['Descent']) AND ($fontinfo['desc']['Descent'] <= 0)) {
  5096. $descent = (- $fontinfo['desc']['Descent'] * $size / 1000);
  5097. } else {
  5098. $descent = 1.219 * 0.24 * $size;
  5099. }
  5100. return ($descent / $this->k);
  5101. }
  5102. /**
  5103. * Return the font ascent value.
  5104. * @param $font (string) font name
  5105. * @param $style (string) font style
  5106. * @param $size (float) The size (in points)
  5107. * @return int font ascent
  5108. * @public
  5109. * @author Nicola Asuni
  5110. * @since 4.9.003 (2010-03-30)
  5111. */
  5112. public function getFontAscent($font, $style='', $size=0) {
  5113. $fontdata = $this->AddFont($font, $style);
  5114. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5115. if (isset($fontinfo['desc']['Ascent']) AND ($fontinfo['desc']['Ascent'] > 0)) {
  5116. $ascent = ($fontinfo['desc']['Ascent'] * $size / 1000);
  5117. } else {
  5118. $ascent = 1.219 * 0.76 * $size;
  5119. }
  5120. return ($ascent / $this->k);
  5121. }
  5122. /**
  5123. * Return true in the character is present in the specified font.
  5124. * @param $char (mixed) Character to check (integer value or string)
  5125. * @param $font (string) Font name (family name).
  5126. * @param $style (string) Font style.
  5127. * @return (boolean) true if the char is defined, false otherwise.
  5128. * @public
  5129. * @since 5.9.153 (2012-03-28)
  5130. */
  5131. public function isCharDefined($char, $font='', $style='') {
  5132. if (is_string($char)) {
  5133. // get character code
  5134. $char = $this->UTF8StringToArray($char);
  5135. $char = $char[0];
  5136. }
  5137. if ($this->empty_string($font)) {
  5138. if ($this->empty_string($style)) {
  5139. return (isset($this->CurrentFont['cw'][intval($char)]));
  5140. }
  5141. $font = $this->FontFamily;
  5142. }
  5143. $fontdata = $this->AddFont($font, $style);
  5144. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5145. return (isset($fontinfo['cw'][intval($char)]));
  5146. }
  5147. /**
  5148. * Replace missing font characters on selected font with specified substitutions.
  5149. * @param $text (string) Text to process.
  5150. * @param $font (string) Font name (family name).
  5151. * @param $style (string) Font style.
  5152. * @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.
  5153. * @return (string) Processed text.
  5154. * @public
  5155. * @since 5.9.153 (2012-03-28)
  5156. */
  5157. public function replaceMissingChars($text, $font='', $style='', $subs=array()) {
  5158. if (empty($subs)) {
  5159. return $text;
  5160. }
  5161. if ($this->empty_string($font)) {
  5162. $font = $this->FontFamily;
  5163. }
  5164. $fontdata = $this->AddFont($font, $style);
  5165. $fontinfo = $this->getFontBuffer($fontdata['fontkey']);
  5166. $uniarr = $this->UTF8StringToArray($text);
  5167. foreach ($uniarr as $k => $chr) {
  5168. if (!isset($fontinfo['cw'][$chr])) {
  5169. // this character is missing on the selected font
  5170. if (isset($subs[$chr])) {
  5171. // we have available substitutions
  5172. if (is_array($subs[$chr])) {
  5173. foreach($subs[$chr] as $s) {
  5174. if (isset($fontinfo['cw'][$s])) {
  5175. $uniarr[$k] = $s;
  5176. break;
  5177. }
  5178. }
  5179. } elseif (isset($fontinfo['cw'][$subs[$chr]])) {
  5180. $uniarr[$k] = $subs[$chr];
  5181. }
  5182. }
  5183. }
  5184. }
  5185. return $this->UniArrSubString($this->UTF8ArrayToUniArray($uniarr));
  5186. }
  5187. /**
  5188. * Defines the default monospaced font.
  5189. * @param $font (string) Font name.
  5190. * @public
  5191. * @since 4.5.025
  5192. */
  5193. public function SetDefaultMonospacedFont($font) {
  5194. $this->default_monospaced_font = $font;
  5195. }
  5196. /**
  5197. * 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 />
  5198. * The identifier can then be passed to Cell(), Write(), Image() or Link(). The destination is defined with SetLink().
  5199. * @public
  5200. * @since 1.5
  5201. * @see Cell(), Write(), Image(), Link(), SetLink()
  5202. */
  5203. public function AddLink() {
  5204. //Create a new internal link
  5205. $n = count($this->links) + 1;
  5206. $this->links[$n] = array(0, 0);
  5207. return $n;
  5208. }
  5209. /**
  5210. * Defines the page and position a link points to.
  5211. * @param $link (int) The link identifier returned by AddLink()
  5212. * @param $y (float) Ordinate of target position; -1 indicates the current position. The default value is 0 (top of page)
  5213. * @param $page (int) Number of target page; -1 indicates the current page. This is the default value
  5214. * @public
  5215. * @since 1.5
  5216. * @see AddLink()
  5217. */
  5218. public function SetLink($link, $y=0, $page=-1) {
  5219. if ($y == -1) {
  5220. $y = $this->y;
  5221. }
  5222. if ($page == -1) {
  5223. $page = $this->page;
  5224. }
  5225. $this->links[$link] = array($page, $y);
  5226. }
  5227. /**
  5228. * Puts a link on a rectangular area of the page.
  5229. * 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.
  5230. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5231. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5232. * @param $w (float) Width of the rectangle
  5233. * @param $h (float) Height of the rectangle
  5234. * @param $link (mixed) URL or identifier returned by AddLink()
  5235. * @param $spaces (int) number of spaces on the text to link
  5236. * @public
  5237. * @since 1.5
  5238. * @see AddLink(), Annotation(), Cell(), Write(), Image()
  5239. */
  5240. public function Link($x, $y, $w, $h, $link, $spaces=0) {
  5241. $this->Annotation($x, $y, $w, $h, $link, array('Subtype'=>'Link'), $spaces);
  5242. }
  5243. /**
  5244. * Puts a markup annotation on a rectangular area of the page.
  5245. * !!!!THE ANNOTATION SUPPORT IS NOT YET FULLY IMPLEMENTED !!!!
  5246. * @param $x (float) Abscissa of the upper-left corner of the rectangle
  5247. * @param $y (float) Ordinate of the upper-left corner of the rectangle
  5248. * @param $w (float) Width of the rectangle
  5249. * @param $h (float) Height of the rectangle
  5250. * @param $text (string) annotation text or alternate content
  5251. * @param $opt (array) array of options (see section 8.4 of PDF reference 1.7).
  5252. * @param $spaces (int) number of spaces on the text to link
  5253. * @public
  5254. * @since 4.0.018 (2008-08-06)
  5255. */
  5256. public function Annotation($x, $y, $w, $h, $text, $opt=array('Subtype'=>'Text'), $spaces=0) {
  5257. if ($this->inxobj) {
  5258. // store parameters for later use on template
  5259. $this->xobjects[$this->xobjid]['annotations'][] = array('x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'text' => $text, 'opt' => $opt, 'spaces' => $spaces);
  5260. return;
  5261. }
  5262. if ($x === '') {
  5263. $x = $this->x;
  5264. }
  5265. if ($y === '') {
  5266. $y = $this->y;
  5267. }
  5268. // check page for no-write regions and adapt page margins if necessary
  5269. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  5270. // recalculate coordinates to account for graphic transformations
  5271. if (isset($this->transfmatrix) AND !empty($this->transfmatrix)) {
  5272. for ($i=$this->transfmatrix_key; $i > 0; --$i) {
  5273. $maxid = count($this->transfmatrix[$i]) - 1;
  5274. for ($j=$maxid; $j >= 0; --$j) {
  5275. $ctm = $this->transfmatrix[$i][$j];
  5276. if (isset($ctm['a'])) {
  5277. $x = $x * $this->k;
  5278. $y = ($this->h - $y) * $this->k;
  5279. $w = $w * $this->k;
  5280. $h = $h * $this->k;
  5281. // top left
  5282. $xt = $x;
  5283. $yt = $y;
  5284. $x1 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5285. $y1 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5286. // top right
  5287. $xt = $x + $w;
  5288. $yt = $y;
  5289. $x2 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5290. $y2 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5291. // bottom left
  5292. $xt = $x;
  5293. $yt = $y - $h;
  5294. $x3 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5295. $y3 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5296. // bottom right
  5297. $xt = $x + $w;
  5298. $yt = $y - $h;
  5299. $x4 = ($ctm['a'] * $xt) + ($ctm['c'] * $yt) + $ctm['e'];
  5300. $y4 = ($ctm['b'] * $xt) + ($ctm['d'] * $yt) + $ctm['f'];
  5301. // new coordinates (rectangle area)
  5302. $x = min($x1, $x2, $x3, $x4);
  5303. $y = max($y1, $y2, $y3, $y4);
  5304. $w = (max($x1, $x2, $x3, $x4) - $x) / $this->k;
  5305. $h = ($y - min($y1, $y2, $y3, $y4)) / $this->k;
  5306. $x = $x / $this->k;
  5307. $y = $this->h - ($y / $this->k);
  5308. }
  5309. }
  5310. }
  5311. }
  5312. if ($this->page <= 0) {
  5313. $page = 1;
  5314. } else {
  5315. $page = $this->page;
  5316. }
  5317. if (!isset($this->PageAnnots[$page])) {
  5318. $this->PageAnnots[$page] = array();
  5319. }
  5320. ++$this->n;
  5321. $this->PageAnnots[$page][] = array('n' => $this->n, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'txt' => $text, 'opt' => $opt, 'numspaces' => $spaces);
  5322. if (!$this->pdfa_mode) {
  5323. 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'])]))) {
  5324. ++$this->n;
  5325. $this->embeddedfiles[basename($opt['FS'])] = array('n' => $this->n, 'file' => $opt['FS']);
  5326. }
  5327. }
  5328. // Add widgets annotation's icons
  5329. if (isset($opt['mk']['i']) AND file_exists($opt['mk']['i'])) {
  5330. $this->Image($opt['mk']['i'], '', '', 10, 10, '', '', '', false, 300, '', false, false, 0, false, true);
  5331. }
  5332. if (isset($opt['mk']['ri']) AND file_exists($opt['mk']['ri'])) {
  5333. $this->Image($opt['mk']['ri'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5334. }
  5335. if (isset($opt['mk']['ix']) AND file_exists($opt['mk']['ix'])) {
  5336. $this->Image($opt['mk']['ix'], '', '', 0, 0, '', '', '', false, 300, '', false, false, 0, false, true);
  5337. }
  5338. }
  5339. /**
  5340. * Embedd the attached files.
  5341. * @since 4.4.000 (2008-12-07)
  5342. * @protected
  5343. * @see Annotation()
  5344. */
  5345. protected function _putEmbeddedFiles() {
  5346. if ($this->pdfa_mode) {
  5347. // embedded files are not allowed in PDF/A mode
  5348. return;
  5349. }
  5350. reset($this->embeddedfiles);
  5351. foreach ($this->embeddedfiles as $filename => $filedata) {
  5352. $data = file_get_contents($filedata['file']);
  5353. $filter = '';
  5354. if ($this->compress) {
  5355. $data = gzcompress($data);
  5356. $filter = ' /Filter /FlateDecode';
  5357. }
  5358. $stream = $this->_getrawstream($data, $filedata['n']);
  5359. $out = $this->_getobj($filedata['n'])."\n";
  5360. $out .= '<< /Type /EmbeddedFile'.$filter.' /Length '.strlen($stream).' >>';
  5361. $out .= ' stream'."\n".$stream."\n".'endstream';
  5362. $out .= "\n".'endobj';
  5363. $this->_out($out);
  5364. }
  5365. }
  5366. /**
  5367. * Prints a text cell at the specified position.
  5368. * This method allows to place a string precisely on the page.
  5369. * @param $x (float) Abscissa of the cell origin
  5370. * @param $y (float) Ordinate of the cell origin
  5371. * @param $txt (string) String to print
  5372. * @param $fstroke (int) outline size in user units (false = disable)
  5373. * @param $fclip (boolean) if true activate clipping mode (you must call StartTransform() before this function and StopTransform() to stop the clipping tranformation).
  5374. * @param $ffill (boolean) if true fills the text
  5375. * @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)))
  5376. * @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.
  5377. * @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>
  5378. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5379. * @param $link (mixed) URL or identifier returned by AddLink().
  5380. * @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.
  5381. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5382. * @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>
  5383. * @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>
  5384. * @param $rtloff (boolean) if true uses the page top-left corner as origin of axis for $x and $y initial position.
  5385. * @public
  5386. * @since 1.0
  5387. * @see Cell(), Write(), MultiCell(), WriteHTML(), WriteHTMLCell()
  5388. */
  5389. 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) {
  5390. $textrendermode = $this->textrendermode;
  5391. $textstrokewidth = $this->textstrokewidth;
  5392. $this->setTextRenderingMode($fstroke, $ffill, $fclip);
  5393. $this->SetXY($x, $y, $rtloff);
  5394. $this->Cell(0, 0, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
  5395. // restore previous rendering mode
  5396. $this->textrendermode = $textrendermode;
  5397. $this->textstrokewidth = $textstrokewidth;
  5398. }
  5399. /**
  5400. * Whenever a page break condition is met, the method is called, and the break is issued or not depending on the returned value.
  5401. * The default implementation returns a value according to the mode selected by SetAutoPageBreak().<br />
  5402. * This method is called automatically and should not be called directly by the application.
  5403. * @return boolean
  5404. * @public
  5405. * @since 1.4
  5406. * @see SetAutoPageBreak()
  5407. */
  5408. public function AcceptPageBreak() {
  5409. if ($this->num_columns > 1) {
  5410. // multi column mode
  5411. if ($this->current_column < ($this->num_columns - 1)) {
  5412. // go to next column
  5413. $this->selectColumn($this->current_column + 1);
  5414. } elseif ($this->AutoPageBreak) {
  5415. // add a new page
  5416. $this->AddPage();
  5417. // set first column
  5418. $this->selectColumn(0);
  5419. }
  5420. // avoid page breaking from checkPageBreak()
  5421. return false;
  5422. }
  5423. return $this->AutoPageBreak;
  5424. }
  5425. /**
  5426. * Add page if needed.
  5427. * @param $h (float) Cell height. Default value: 0.
  5428. * @param $y (mixed) starting y position, leave empty for current position.
  5429. * @param $addpage (boolean) if true add a page, otherwise only return the true/false state
  5430. * @return boolean true in case of page break, false otherwise.
  5431. * @since 3.2.000 (2008-07-01)
  5432. * @protected
  5433. */
  5434. protected function checkPageBreak($h=0, $y='', $addpage=true) {
  5435. if ($this->empty_string($y)) {
  5436. $y = $this->y;
  5437. }
  5438. $current_page = $this->page;
  5439. if ((($y + $h) > $this->PageBreakTrigger) AND ($this->inPageBody()) AND ($this->AcceptPageBreak())) {
  5440. if ($addpage) {
  5441. //Automatic page break
  5442. $x = $this->x;
  5443. $this->AddPage($this->CurOrientation);
  5444. $this->y = $this->tMargin;
  5445. $oldpage = $this->page - 1;
  5446. if ($this->rtl) {
  5447. if ($this->pagedim[$this->page]['orm'] != $this->pagedim[$oldpage]['orm']) {
  5448. $this->x = $x - ($this->pagedim[$this->page]['orm'] - $this->pagedim[$oldpage]['orm']);
  5449. } else {
  5450. $this->x = $x;
  5451. }
  5452. } else {
  5453. if ($this->pagedim[$this->page]['olm'] != $this->pagedim[$oldpage]['olm']) {
  5454. $this->x = $x + ($this->pagedim[$this->page]['olm'] - $this->pagedim[$oldpage]['olm']);
  5455. } else {
  5456. $this->x = $x;
  5457. }
  5458. }
  5459. }
  5460. return true;
  5461. }
  5462. if ($current_page != $this->page) {
  5463. // account for columns mode
  5464. return true;
  5465. }
  5466. return false;
  5467. }
  5468. /**
  5469. * Removes SHY characters from text.
  5470. * Unicode Data:<ul>
  5471. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  5472. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  5473. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  5474. * <li>HTML Entity (named): "&amp;shy;"</li>
  5475. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  5476. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  5477. * <li>UTF-8 character: chr(194).chr(173)</li>
  5478. * </ul>
  5479. * @param $txt (string) input string
  5480. * @return string without SHY characters.
  5481. * @public
  5482. * @since (4.5.019) 2009-02-28
  5483. */
  5484. public function removeSHY($txt='') {
  5485. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  5486. if (!$this->isunicode) {
  5487. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  5488. }
  5489. return $txt;
  5490. }
  5491. /**
  5492. * 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 />
  5493. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5494. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5495. * @param $h (float) Cell height. Default value: 0.
  5496. * @param $txt (string) String to print. Default value: empty string.
  5497. * @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)))
  5498. * @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.
  5499. * @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>
  5500. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5501. * @param $link (mixed) URL or identifier returned by AddLink().
  5502. * @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.
  5503. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5504. * @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>
  5505. * @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>
  5506. * @public
  5507. * @since 1.0
  5508. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), AddLink(), Ln(), MultiCell(), Write(), SetAutoPageBreak()
  5509. */
  5510. 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') {
  5511. $prev_cell_margin = $this->cell_margin;
  5512. $prev_cell_padding = $this->cell_padding;
  5513. $this->adjustCellPadding($border);
  5514. if (!$ignore_min_height) {
  5515. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5516. if ($h < $min_cell_height) {
  5517. $h = $min_cell_height;
  5518. }
  5519. }
  5520. $this->checkPageBreak($h + $this->cell_margin['T'] + $this->cell_margin['B']);
  5521. // apply text shadow if enabled
  5522. if ($this->txtshadow['enabled']) {
  5523. // save data
  5524. $x = $this->x;
  5525. $y = $this->y;
  5526. $bc = $this->bgcolor;
  5527. $fc = $this->fgcolor;
  5528. $sc = $this->strokecolor;
  5529. $alpha = $this->alpha;
  5530. // print shadow
  5531. $this->x += $this->txtshadow['depth_w'];
  5532. $this->y += $this->txtshadow['depth_h'];
  5533. $this->SetFillColorArray($this->txtshadow['color']);
  5534. $this->SetTextColorArray($this->txtshadow['color']);
  5535. $this->SetDrawColorArray($this->txtshadow['color']);
  5536. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  5537. $this->setAlpha($this->txtshadow['opacity'], $this->txtshadow['blend_mode']);
  5538. }
  5539. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5540. //restore data
  5541. $this->x = $x;
  5542. $this->y = $y;
  5543. $this->SetFillColorArray($bc);
  5544. $this->SetTextColorArray($fc);
  5545. $this->SetDrawColorArray($sc);
  5546. if ($this->txtshadow['opacity'] != $alpha['CA']) {
  5547. $this->setAlpha($alpha['CA'], $alpha['BM'], $alpha['ca'], $alpha['AIS']);
  5548. }
  5549. }
  5550. $this->_out($this->getCellCode($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, true, $calign, $valign));
  5551. $this->cell_padding = $prev_cell_padding;
  5552. $this->cell_margin = $prev_cell_margin;
  5553. }
  5554. /**
  5555. * 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 />
  5556. * If automatic page breaking is enabled and the cell goes beyond the limit, a page break is done before outputting.
  5557. * @param $w (float) Cell width. If 0, the cell extends up to the right margin.
  5558. * @param $h (float) Cell height. Default value: 0.
  5559. * @param $txt (string) String to print. Default value: empty string.
  5560. * @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)))
  5561. * @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.
  5562. * @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>
  5563. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  5564. * @param $link (mixed) URL or identifier returned by AddLink().
  5565. * @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.
  5566. * @param $ignore_min_height (boolean) if true ignore automatic minimum height value.
  5567. * @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>
  5568. * @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>
  5569. * @return string containing cell code
  5570. * @protected
  5571. * @since 1.0
  5572. * @see Cell()
  5573. */
  5574. 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') {
  5575. // replace 'NO-BREAK SPACE' (U+00A0) character with a simple space
  5576. $txt = str_replace($this->unichr(160), ' ', $txt);
  5577. $prev_cell_margin = $this->cell_margin;
  5578. $prev_cell_padding = $this->cell_padding;
  5579. $txt = $this->removeSHY($txt);
  5580. $rs = ''; //string to be returned
  5581. $this->adjustCellPadding($border);
  5582. if (!$ignore_min_height) {
  5583. $min_cell_height = ($this->FontSize * $this->cell_height_ratio) + $this->cell_padding['T'] + $this->cell_padding['B'];
  5584. if ($h < $min_cell_height) {
  5585. $h = $min_cell_height;
  5586. }
  5587. }
  5588. $k = $this->k;
  5589. // check page for no-write regions and adapt page margins if necessary
  5590. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  5591. if ($this->rtl) {
  5592. $x = $this->x - $this->cell_margin['R'];
  5593. } else {
  5594. $x = $this->x + $this->cell_margin['L'];
  5595. }
  5596. $y = $this->y + $this->cell_margin['T'];
  5597. $prev_font_stretching = $this->font_stretching;
  5598. $prev_font_spacing = $this->font_spacing;
  5599. // cell vertical alignment
  5600. switch ($calign) {
  5601. case 'A': {
  5602. // font top
  5603. switch ($valign) {
  5604. case 'T': {
  5605. // top
  5606. $y -= $this->cell_padding['T'];
  5607. break;
  5608. }
  5609. case 'B': {
  5610. // bottom
  5611. $y -= ($h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent);
  5612. break;
  5613. }
  5614. default:
  5615. case 'C':
  5616. case 'M': {
  5617. // center
  5618. $y -= (($h - $this->FontAscent - $this->FontDescent) / 2);
  5619. break;
  5620. }
  5621. }
  5622. break;
  5623. }
  5624. case 'L': {
  5625. // font baseline
  5626. switch ($valign) {
  5627. case 'T': {
  5628. // top
  5629. $y -= ($this->cell_padding['T'] + $this->FontAscent);
  5630. break;
  5631. }
  5632. case 'B': {
  5633. // bottom
  5634. $y -= ($h - $this->cell_padding['B'] - $this->FontDescent);
  5635. break;
  5636. }
  5637. default:
  5638. case 'C':
  5639. case 'M': {
  5640. // center
  5641. $y -= (($h + $this->FontAscent - $this->FontDescent) / 2);
  5642. break;
  5643. }
  5644. }
  5645. break;
  5646. }
  5647. case 'D': {
  5648. // font bottom
  5649. switch ($valign) {
  5650. case 'T': {
  5651. // top
  5652. $y -= ($this->cell_padding['T'] + $this->FontAscent + $this->FontDescent);
  5653. break;
  5654. }
  5655. case 'B': {
  5656. // bottom
  5657. $y -= ($h - $this->cell_padding['B']);
  5658. break;
  5659. }
  5660. default:
  5661. case 'C':
  5662. case 'M': {
  5663. // center
  5664. $y -= (($h + $this->FontAscent + $this->FontDescent) / 2);
  5665. break;
  5666. }
  5667. }
  5668. break;
  5669. }
  5670. case 'B': {
  5671. // cell bottom
  5672. $y -= $h;
  5673. break;
  5674. }
  5675. case 'C':
  5676. case 'M': {
  5677. // cell center
  5678. $y -= ($h / 2);
  5679. break;
  5680. }
  5681. default:
  5682. case 'T': {
  5683. // cell top
  5684. break;
  5685. }
  5686. }
  5687. // text vertical alignment
  5688. switch ($valign) {
  5689. case 'T': {
  5690. // top
  5691. $yt = $y + $this->cell_padding['T'];
  5692. break;
  5693. }
  5694. case 'B': {
  5695. // bottom
  5696. $yt = $y + $h - $this->cell_padding['B'] - $this->FontAscent - $this->FontDescent;
  5697. break;
  5698. }
  5699. default:
  5700. case 'C':
  5701. case 'M': {
  5702. // center
  5703. $yt = $y + (($h - $this->FontAscent - $this->FontDescent) / 2);
  5704. break;
  5705. }
  5706. }
  5707. $basefonty = $yt + $this->FontAscent;
  5708. if ($this->empty_string($w) OR ($w <= 0)) {
  5709. if ($this->rtl) {
  5710. $w = $x - $this->lMargin;
  5711. } else {
  5712. $w = $this->w - $this->rMargin - $x;
  5713. }
  5714. }
  5715. $s = '';
  5716. // fill and borders
  5717. if (is_string($border) AND (strlen($border) == 4)) {
  5718. // full border
  5719. $border = 1;
  5720. }
  5721. if ($fill OR ($border == 1)) {
  5722. if ($fill) {
  5723. $op = ($border == 1) ? 'B' : 'f';
  5724. } else {
  5725. $op = 'S';
  5726. }
  5727. if ($this->rtl) {
  5728. $xk = (($x - $w) * $k);
  5729. } else {
  5730. $xk = ($x * $k);
  5731. }
  5732. $s .= sprintf('%F %F %F %F re %s ', $xk, (($this->h - $y) * $k), ($w * $k), (-$h * $k), $op);
  5733. }
  5734. // draw borders
  5735. $s .= $this->getCellBorder($x, $y, $w, $h, $border);
  5736. if ($txt != '') {
  5737. $txt2 = $txt;
  5738. if ($this->isunicode) {
  5739. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  5740. $txt2 = $this->UTF8ToLatin1($txt2);
  5741. } else {
  5742. $unicode = $this->UTF8StringToArray($txt); // array of UTF-8 unicode values
  5743. $unicode = $this->utf8Bidi($unicode, '', $this->tmprtl);
  5744. // replace thai chars (if any)
  5745. if (defined('K_THAI_TOPCHARS') AND (K_THAI_TOPCHARS == true)) {
  5746. // number of chars
  5747. $numchars = count($unicode);
  5748. // po pla, for far, for fan
  5749. $longtail = array(0x0e1b, 0x0e1d, 0x0e1f);
  5750. // do chada, to patak
  5751. $lowtail = array(0x0e0e, 0x0e0f);
  5752. // mai hun arkad, sara i, sara ii, sara ue, sara uee
  5753. $upvowel = array(0x0e31, 0x0e34, 0x0e35, 0x0e36, 0x0e37);
  5754. // mai ek, mai tho, mai tri, mai chattawa, karan
  5755. $tonemark = array(0x0e48, 0x0e49, 0x0e4a, 0x0e4b, 0x0e4c);
  5756. // sara u, sara uu, pinthu
  5757. $lowvowel = array(0x0e38, 0x0e39, 0x0e3a);
  5758. $output = array();
  5759. for ($i = 0; $i < $numchars; $i++) {
  5760. if (($unicode[$i] >= 0x0e00) && ($unicode[$i] <= 0x0e5b)) {
  5761. $ch0 = $unicode[$i];
  5762. $ch1 = ($i > 0) ? $unicode[($i - 1)] : 0;
  5763. $ch2 = ($i > 1) ? $unicode[($i - 2)] : 0;
  5764. $chn = ($i < ($numchars - 1)) ? $unicode[($i + 1)] : 0;
  5765. if (in_array($ch0, $tonemark)) {
  5766. if ($chn == 0x0e33) {
  5767. // sara um
  5768. if (in_array($ch1, $longtail)) {
  5769. // tonemark at upper left
  5770. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  5771. } else {
  5772. // tonemark at upper right (normal position)
  5773. $output[] = $ch0;
  5774. }
  5775. } elseif (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $lowvowel))) {
  5776. // tonemark at lower left
  5777. $output[] = $this->replaceChar($ch0, (0xf705 + $ch0 - 0x0e48));
  5778. } elseif (in_array($ch1, $upvowel)) {
  5779. if (in_array($ch2, $longtail)) {
  5780. // tonemark at upper left
  5781. $output[] = $this->replaceChar($ch0, (0xf713 + $ch0 - 0x0e48));
  5782. } else {
  5783. // tonemark at upper right (normal position)
  5784. $output[] = $ch0;
  5785. }
  5786. } else {
  5787. // tonemark at lower right
  5788. $output[] = $this->replaceChar($ch0, (0xf70a + $ch0 - 0x0e48));
  5789. }
  5790. } elseif (($ch0 == 0x0e33) AND (in_array($ch1, $longtail) OR (in_array($ch2, $longtail) AND in_array($ch1, $tonemark)))) {
  5791. // add lower left nikhahit and sara aa
  5792. if ($this->isCharDefined(0xf711) AND $this->isCharDefined(0x0e32)) {
  5793. $output[] = 0xf711;
  5794. $this->CurrentFont['subsetchars'][0xf711] = true;
  5795. $output[] = 0x0e32;
  5796. $this->CurrentFont['subsetchars'][0x0e32] = true;
  5797. } else {
  5798. $output[] = $ch0;
  5799. }
  5800. } elseif (in_array($ch1, $longtail)) {
  5801. if ($ch0 == 0x0e31) {
  5802. // lower left mai hun arkad
  5803. $output[] = $this->replaceChar($ch0, 0xf710);
  5804. } elseif (in_array($ch0, $upvowel)) {
  5805. // lower left
  5806. $output[] = $this->replaceChar($ch0, (0xf701 + $ch0 - 0x0e34));
  5807. } elseif ($ch0 == 0x0e47) {
  5808. // lower left mai tai koo
  5809. $output[] = $this->replaceChar($ch0, 0xf712);
  5810. } else {
  5811. // normal character
  5812. $output[] = $ch0;
  5813. }
  5814. } elseif (in_array($ch1, $lowtail) AND in_array($ch0, $lowvowel)) {
  5815. // lower vowel
  5816. $output[] = $this->replaceChar($ch0, (0xf718 + $ch0 - 0x0e38));
  5817. } elseif (($ch0 == 0x0e0d) AND in_array($chn, $lowvowel)) {
  5818. // yo ying without lower part
  5819. $output[] = $this->replaceChar($ch0, 0xf70f);
  5820. } elseif (($ch0 == 0x0e10) AND in_array($chn, $lowvowel)) {
  5821. // tho santan without lower part
  5822. $output[] = $this->replaceChar($ch0, 0xf700);
  5823. } else {
  5824. $output[] = $ch0;
  5825. }
  5826. } else {
  5827. // non-thai character
  5828. $output[] = $unicode[$i];
  5829. }
  5830. }
  5831. $unicode = $output;
  5832. // update font subsetchars
  5833. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  5834. } // end of K_THAI_TOPCHARS
  5835. $txt2 = $this->arrUTF8ToUTF16BE($unicode, false);
  5836. }
  5837. }
  5838. $txt2 = $this->_escape($txt2);
  5839. // get current text width (considering general font stretching and spacing)
  5840. $txwidth = $this->GetStringWidth($txt);
  5841. $width = $txwidth;
  5842. // check for stretch mode
  5843. if ($stretch > 0) {
  5844. // calculate ratio between cell width and text width
  5845. if ($width <= 0) {
  5846. $ratio = 1;
  5847. } else {
  5848. $ratio = (($w - $this->cell_padding['L'] - $this->cell_padding['R']) / $width);
  5849. }
  5850. // check if stretching is required
  5851. if (($ratio < 1) OR (($ratio > 1) AND (($stretch % 2) == 0))) {
  5852. // the text will be stretched to fit cell width
  5853. if ($stretch > 2) {
  5854. // set new character spacing
  5855. $this->font_spacing += ($w - $this->cell_padding['L'] - $this->cell_padding['R'] - $width) / (max(($this->GetNumChars($txt) - 1), 1) * ($this->font_stretching / 100));
  5856. } else {
  5857. // set new horizontal stretching
  5858. $this->font_stretching *= $ratio;
  5859. }
  5860. // recalculate text width (the text fills the entire cell)
  5861. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5862. // reset alignment
  5863. $align = '';
  5864. }
  5865. }
  5866. if ($this->font_stretching != 100) {
  5867. // apply font stretching
  5868. $rs .= sprintf('BT %F Tz ET ', $this->font_stretching);
  5869. }
  5870. if ($this->font_spacing != 0) {
  5871. // increase/decrease font spacing
  5872. $rs .= sprintf('BT %F Tc ET ', ($this->font_spacing * $this->k));
  5873. }
  5874. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5875. $s .= 'q '.$this->TextColor.' ';
  5876. }
  5877. // rendering mode
  5878. $s .= sprintf('BT %d Tr %F w ET ', $this->textrendermode, ($this->textstrokewidth * $this->k));
  5879. // count number of spaces
  5880. $ns = substr_count($txt, chr(32));
  5881. // Justification
  5882. $spacewidth = 0;
  5883. if (($align == 'J') AND ($ns > 0)) {
  5884. if ($this->isUnicodeFont()) {
  5885. // get string width without spaces
  5886. $width = $this->GetStringWidth(str_replace(' ', '', $txt));
  5887. // calculate average space width
  5888. $spacewidth = -1000 * ($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1) / $this->FontSize;
  5889. if ($this->font_stretching != 100) {
  5890. // word spacing is affected by stretching
  5891. $spacewidth /= ($this->font_stretching / 100);
  5892. }
  5893. // set word position to be used with TJ operator
  5894. $txt2 = str_replace(chr(0).chr(32), ') '.sprintf('%F', $spacewidth).' (', $txt2);
  5895. $unicode_justification = true;
  5896. } else {
  5897. // get string width
  5898. $width = $txwidth;
  5899. // new space width
  5900. $spacewidth = (($w - $width - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1)) * $this->k;
  5901. if ($this->font_stretching != 100) {
  5902. // word spacing (Tw) is affected by stretching
  5903. $spacewidth /= ($this->font_stretching / 100);
  5904. }
  5905. // set word spacing
  5906. $rs .= sprintf('BT %F Tw ET ', $spacewidth);
  5907. }
  5908. $width = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  5909. }
  5910. // replace carriage return characters
  5911. $txt2 = str_replace("\r", ' ', $txt2);
  5912. switch ($align) {
  5913. case 'C': {
  5914. $dx = ($w - $width) / 2;
  5915. break;
  5916. }
  5917. case 'R': {
  5918. if ($this->rtl) {
  5919. $dx = $this->cell_padding['R'];
  5920. } else {
  5921. $dx = $w - $width - $this->cell_padding['R'];
  5922. }
  5923. break;
  5924. }
  5925. case 'L': {
  5926. if ($this->rtl) {
  5927. $dx = $w - $width - $this->cell_padding['L'];
  5928. } else {
  5929. $dx = $this->cell_padding['L'];
  5930. }
  5931. break;
  5932. }
  5933. case 'J':
  5934. default: {
  5935. if ($this->rtl) {
  5936. $dx = $this->cell_padding['R'];
  5937. } else {
  5938. $dx = $this->cell_padding['L'];
  5939. }
  5940. break;
  5941. }
  5942. }
  5943. if ($this->rtl) {
  5944. $xdx = $x - $dx - $width;
  5945. } else {
  5946. $xdx = $x + $dx;
  5947. }
  5948. $xdk = $xdx * $k;
  5949. // print text
  5950. $s .= sprintf('BT %F %F Td [(%s)] TJ ET', $xdk, (($this->h - $basefonty) * $k), $txt2);
  5951. if (isset($uniblock)) {
  5952. // print overlapping characters as separate string
  5953. $xshift = 0; // horizontal shift
  5954. $ty = (($this->h - $basefonty + (0.2 * $this->FontSize)) * $k);
  5955. $spw = (($w - $txwidth - $this->cell_padding['L'] - $this->cell_padding['R']) / ($ns?$ns:1));
  5956. foreach ($uniblock as $uk => $uniarr) {
  5957. if (($uk % 2) == 0) {
  5958. // x space to skip
  5959. if ($spacewidth != 0) {
  5960. // justification shift
  5961. $xshift += (count(array_keys($uniarr, 32)) * $spw);
  5962. }
  5963. $xshift += $this->GetArrStringWidth($uniarr); // + shift justification
  5964. } else {
  5965. // character to print
  5966. $topchr = $this->arrUTF8ToUTF16BE($uniarr, false);
  5967. $topchr = $this->_escape($topchr);
  5968. $s .= sprintf(' BT %F %F Td [(%s)] TJ ET', ($xdk + ($xshift * $k)), $ty, $topchr);
  5969. }
  5970. }
  5971. }
  5972. if ($this->underline) {
  5973. $s .= ' '.$this->_dounderlinew($xdx, $basefonty, $width);
  5974. }
  5975. if ($this->linethrough) {
  5976. $s .= ' '.$this->_dolinethroughw($xdx, $basefonty, $width);
  5977. }
  5978. if ($this->overline) {
  5979. $s .= ' '.$this->_dooverlinew($xdx, $basefonty, $width);
  5980. }
  5981. if ($this->ColorFlag AND ($this->textrendermode < 4)) {
  5982. $s .= ' Q';
  5983. }
  5984. if ($link) {
  5985. $this->Link($xdx, $yt, $width, ($this->FontAscent + $this->FontDescent), $link, $ns);
  5986. }
  5987. }
  5988. // output cell
  5989. if ($s) {
  5990. // output cell
  5991. $rs .= $s;
  5992. if ($this->font_spacing != 0) {
  5993. // reset font spacing mode
  5994. $rs .= ' BT 0 Tc ET';
  5995. }
  5996. if ($this->font_stretching != 100) {
  5997. // reset font stretching mode
  5998. $rs .= ' BT 100 Tz ET';
  5999. }
  6000. }
  6001. // reset word spacing
  6002. if (!$this->isUnicodeFont() AND ($align == 'J')) {
  6003. $rs .= ' BT 0 Tw ET';
  6004. }
  6005. // reset stretching and spacing
  6006. $this->font_stretching = $prev_font_stretching;
  6007. $this->font_spacing = $prev_font_spacing;
  6008. $this->lasth = $h;
  6009. if ($ln > 0) {
  6010. //Go to the beginning of the next line
  6011. $this->y = $y + $h + $this->cell_margin['B'];
  6012. if ($ln == 1) {
  6013. if ($this->rtl) {
  6014. $this->x = $this->w - $this->rMargin;
  6015. } else {
  6016. $this->x = $this->lMargin;
  6017. }
  6018. }
  6019. } else {
  6020. // go left or right by case
  6021. if ($this->rtl) {
  6022. $this->x = $x - $w - $this->cell_margin['L'];
  6023. } else {
  6024. $this->x = $x + $w + $this->cell_margin['R'];
  6025. }
  6026. }
  6027. $gstyles = ''.$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' '.$this->FillColor."\n";
  6028. $rs = $gstyles.$rs;
  6029. $this->cell_padding = $prev_cell_padding;
  6030. $this->cell_margin = $prev_cell_margin;
  6031. return $rs;
  6032. }
  6033. /**
  6034. * Replace a char if is defined on the current font.
  6035. * @param $oldchar (int) Integer code (unicode) of the character to replace.
  6036. * @param $newchar (int) Integer code (unicode) of the new character.
  6037. * @return int the replaced char or the old char in case the new char i not defined
  6038. * @protected
  6039. * @since 5.9.167 (2012-06-22)
  6040. */
  6041. protected function replaceChar($oldchar, $newchar) {
  6042. if ($this->isCharDefined($newchar)) {
  6043. // add the new char on the subset list
  6044. $this->CurrentFont['subsetchars'][$newchar] = true;
  6045. // return the new character
  6046. return $newchar;
  6047. }
  6048. // return the old char
  6049. return $oldchar;
  6050. }
  6051. /**
  6052. * Returns the code to draw the cell border
  6053. * @param $x (float) X coordinate.
  6054. * @param $y (float) Y coordinate.
  6055. * @param $w (float) Cell width.
  6056. * @param $h (float) Cell height.
  6057. * @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)))
  6058. * @return string containing cell border code
  6059. * @protected
  6060. * @see SetLineStyle()
  6061. * @since 5.7.000 (2010-08-02)
  6062. */
  6063. protected function getCellBorder($x, $y, $w, $h, $brd) {
  6064. $s = ''; // string to be returned
  6065. if (empty($brd)) {
  6066. return $s;
  6067. }
  6068. if ($brd == 1) {
  6069. $brd = array('LRTB' => true);
  6070. }
  6071. // calculate coordinates for border
  6072. $k = $this->k;
  6073. if ($this->rtl) {
  6074. $xeL = ($x - $w) * $k;
  6075. $xeR = $x * $k;
  6076. } else {
  6077. $xeL = $x * $k;
  6078. $xeR = ($x + $w) * $k;
  6079. }
  6080. $yeL = (($this->h - ($y + $h)) * $k);
  6081. $yeT = (($this->h - $y) * $k);
  6082. $xeT = $xeL;
  6083. $xeB = $xeR;
  6084. $yeR = $yeT;
  6085. $yeB = $yeL;
  6086. if (is_string($brd)) {
  6087. // convert string to array
  6088. $slen = strlen($brd);
  6089. $newbrd = array();
  6090. for ($i = 0; $i < $slen; ++$i) {
  6091. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6092. }
  6093. $brd = $newbrd;
  6094. }
  6095. if (isset($brd['mode'])) {
  6096. $mode = $brd['mode'];
  6097. unset($brd['mode']);
  6098. } else {
  6099. $mode = 'normal';
  6100. }
  6101. foreach ($brd as $border => $style) {
  6102. if (is_array($style) AND !empty($style)) {
  6103. // apply border style
  6104. $prev_style = $this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor.' ';
  6105. $s .= $this->SetLineStyle($style, true)."\n";
  6106. }
  6107. switch ($mode) {
  6108. case 'ext': {
  6109. $off = (($this->LineWidth / 2) * $k);
  6110. $xL = $xeL - $off;
  6111. $xR = $xeR + $off;
  6112. $yT = $yeT + $off;
  6113. $yL = $yeL - $off;
  6114. $xT = $xL;
  6115. $xB = $xR;
  6116. $yR = $yT;
  6117. $yB = $yL;
  6118. $w += $this->LineWidth;
  6119. $h += $this->LineWidth;
  6120. break;
  6121. }
  6122. case 'int': {
  6123. $off = ($this->LineWidth / 2) * $k;
  6124. $xL = $xeL + $off;
  6125. $xR = $xeR - $off;
  6126. $yT = $yeT - $off;
  6127. $yL = $yeL + $off;
  6128. $xT = $xL;
  6129. $xB = $xR;
  6130. $yR = $yT;
  6131. $yB = $yL;
  6132. $w -= $this->LineWidth;
  6133. $h -= $this->LineWidth;
  6134. break;
  6135. }
  6136. case 'normal':
  6137. default: {
  6138. $xL = $xeL;
  6139. $xT = $xeT;
  6140. $xB = $xeB;
  6141. $xR = $xeR;
  6142. $yL = $yeL;
  6143. $yT = $yeT;
  6144. $yB = $yeB;
  6145. $yR = $yeR;
  6146. break;
  6147. }
  6148. }
  6149. // draw borders by case
  6150. if (strlen($border) == 4) {
  6151. $s .= sprintf('%F %F %F %F re S ', $xT, $yT, ($w * $k), (-$h * $k));
  6152. } elseif (strlen($border) == 3) {
  6153. if (strpos($border,'B') === false) { // LTR
  6154. $s .= sprintf('%F %F m ', $xL, $yL);
  6155. $s .= sprintf('%F %F l ', $xT, $yT);
  6156. $s .= sprintf('%F %F l ', $xR, $yR);
  6157. $s .= sprintf('%F %F l ', $xB, $yB);
  6158. $s .= 'S ';
  6159. } elseif (strpos($border,'L') === false) { // TRB
  6160. $s .= sprintf('%F %F m ', $xT, $yT);
  6161. $s .= sprintf('%F %F l ', $xR, $yR);
  6162. $s .= sprintf('%F %F l ', $xB, $yB);
  6163. $s .= sprintf('%F %F l ', $xL, $yL);
  6164. $s .= 'S ';
  6165. } elseif (strpos($border,'T') === false) { // RBL
  6166. $s .= sprintf('%F %F m ', $xR, $yR);
  6167. $s .= sprintf('%F %F l ', $xB, $yB);
  6168. $s .= sprintf('%F %F l ', $xL, $yL);
  6169. $s .= sprintf('%F %F l ', $xT, $yT);
  6170. $s .= 'S ';
  6171. } elseif (strpos($border,'R') === false) { // BLT
  6172. $s .= sprintf('%F %F m ', $xB, $yB);
  6173. $s .= sprintf('%F %F l ', $xL, $yL);
  6174. $s .= sprintf('%F %F l ', $xT, $yT);
  6175. $s .= sprintf('%F %F l ', $xR, $yR);
  6176. $s .= 'S ';
  6177. }
  6178. } elseif (strlen($border) == 2) {
  6179. if ((strpos($border,'L') !== false) AND (strpos($border,'T') !== false)) { // LT
  6180. $s .= sprintf('%F %F m ', $xL, $yL);
  6181. $s .= sprintf('%F %F l ', $xT, $yT);
  6182. $s .= sprintf('%F %F l ', $xR, $yR);
  6183. $s .= 'S ';
  6184. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'R') !== false)) { // TR
  6185. $s .= sprintf('%F %F m ', $xT, $yT);
  6186. $s .= sprintf('%F %F l ', $xR, $yR);
  6187. $s .= sprintf('%F %F l ', $xB, $yB);
  6188. $s .= 'S ';
  6189. } elseif ((strpos($border,'R') !== false) AND (strpos($border,'B') !== false)) { // RB
  6190. $s .= sprintf('%F %F m ', $xR, $yR);
  6191. $s .= sprintf('%F %F l ', $xB, $yB);
  6192. $s .= sprintf('%F %F l ', $xL, $yL);
  6193. $s .= 'S ';
  6194. } elseif ((strpos($border,'B') !== false) AND (strpos($border,'L') !== false)) { // BL
  6195. $s .= sprintf('%F %F m ', $xB, $yB);
  6196. $s .= sprintf('%F %F l ', $xL, $yL);
  6197. $s .= sprintf('%F %F l ', $xT, $yT);
  6198. $s .= 'S ';
  6199. } elseif ((strpos($border,'L') !== false) AND (strpos($border,'R') !== false)) { // LR
  6200. $s .= sprintf('%F %F m ', $xL, $yL);
  6201. $s .= sprintf('%F %F l ', $xT, $yT);
  6202. $s .= 'S ';
  6203. $s .= sprintf('%F %F m ', $xR, $yR);
  6204. $s .= sprintf('%F %F l ', $xB, $yB);
  6205. $s .= 'S ';
  6206. } elseif ((strpos($border,'T') !== false) AND (strpos($border,'B') !== false)) { // TB
  6207. $s .= sprintf('%F %F m ', $xT, $yT);
  6208. $s .= sprintf('%F %F l ', $xR, $yR);
  6209. $s .= 'S ';
  6210. $s .= sprintf('%F %F m ', $xB, $yB);
  6211. $s .= sprintf('%F %F l ', $xL, $yL);
  6212. $s .= 'S ';
  6213. }
  6214. } else { // strlen($border) == 1
  6215. if (strpos($border,'L') !== false) { // L
  6216. $s .= sprintf('%F %F m ', $xL, $yL);
  6217. $s .= sprintf('%F %F l ', $xT, $yT);
  6218. $s .= 'S ';
  6219. } elseif (strpos($border,'T') !== false) { // T
  6220. $s .= sprintf('%F %F m ', $xT, $yT);
  6221. $s .= sprintf('%F %F l ', $xR, $yR);
  6222. $s .= 'S ';
  6223. } elseif (strpos($border,'R') !== false) { // R
  6224. $s .= sprintf('%F %F m ', $xR, $yR);
  6225. $s .= sprintf('%F %F l ', $xB, $yB);
  6226. $s .= 'S ';
  6227. } elseif (strpos($border,'B') !== false) { // B
  6228. $s .= sprintf('%F %F m ', $xB, $yB);
  6229. $s .= sprintf('%F %F l ', $xL, $yL);
  6230. $s .= 'S ';
  6231. }
  6232. }
  6233. if (is_array($style) AND !empty($style)) {
  6234. // reset border style to previous value
  6235. $s .= "\n".$this->linestyleWidth.' '.$this->linestyleCap.' '.$this->linestyleJoin.' '.$this->linestyleDash.' '.$this->DrawColor."\n";
  6236. }
  6237. }
  6238. return $s;
  6239. }
  6240. /**
  6241. * This method allows printing text with line breaks.
  6242. * 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 />
  6243. * Text can be aligned, centered or justified. The cell block can be framed and the background painted.
  6244. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6245. * @param $h (float) Cell minimum height. The cell extends automatically if needed.
  6246. * @param $txt (string) String to print
  6247. * @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)))
  6248. * @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>
  6249. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6250. * @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>
  6251. * @param $x (float) x position in user units
  6252. * @param $y (float) y position in user units
  6253. * @param $reseth (boolean) if true reset the last cell height (default true).
  6254. * @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.
  6255. * @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.
  6256. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width.
  6257. * @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.
  6258. * @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.
  6259. * @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).
  6260. * @return int Return the number of cells or 1 for html mode.
  6261. * @public
  6262. * @since 1.3
  6263. * @see SetFont(), SetDrawColor(), SetFillColor(), SetTextColor(), SetLineWidth(), Cell(), Write(), SetAutoPageBreak()
  6264. */
  6265. 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) {
  6266. $prev_cell_margin = $this->cell_margin;
  6267. $prev_cell_padding = $this->cell_padding;
  6268. // adjust internal padding
  6269. $this->adjustCellPadding($border);
  6270. $mc_padding = $this->cell_padding;
  6271. $mc_margin = $this->cell_margin;
  6272. $this->cell_padding['T'] = 0;
  6273. $this->cell_padding['B'] = 0;
  6274. $this->setCellMargins(0, 0, 0, 0);
  6275. if ($this->empty_string($this->lasth) OR $reseth) {
  6276. // reset row height
  6277. $this->resetLastH();
  6278. }
  6279. if (!$this->empty_string($y)) {
  6280. $this->SetY($y);
  6281. } else {
  6282. $y = $this->GetY();
  6283. }
  6284. $resth = 0;
  6285. if (($h > 0) AND $this->inPageBody() AND (($y + $h + $mc_margin['T'] + $mc_margin['B']) > $this->PageBreakTrigger)) {
  6286. // spit cell in more pages/columns
  6287. $newh = ($this->PageBreakTrigger - $y);
  6288. $resth = ($h - $newh); // cell to be printed on the next page/column
  6289. $h = $newh;
  6290. }
  6291. // get current page number
  6292. $startpage = $this->page;
  6293. // get current column
  6294. $startcolumn = $this->current_column;
  6295. if (!$this->empty_string($x)) {
  6296. $this->SetX($x);
  6297. } else {
  6298. $x = $this->GetX();
  6299. }
  6300. // check page for no-write regions and adapt page margins if necessary
  6301. list($x, $y) = $this->checkPageRegions(0, $x, $y);
  6302. // apply margins
  6303. $oy = $y + $mc_margin['T'];
  6304. if ($this->rtl) {
  6305. $ox = $this->w - $x - $mc_margin['R'];
  6306. } else {
  6307. $ox = $x + $mc_margin['L'];
  6308. }
  6309. $this->x = $ox;
  6310. $this->y = $oy;
  6311. // set width
  6312. if ($this->empty_string($w) OR ($w <= 0)) {
  6313. if ($this->rtl) {
  6314. $w = $this->x - $this->lMargin - $mc_margin['L'];
  6315. } else {
  6316. $w = $this->w - $this->x - $this->rMargin - $mc_margin['R'];
  6317. }
  6318. }
  6319. // store original margin values
  6320. $lMargin = $this->lMargin;
  6321. $rMargin = $this->rMargin;
  6322. if ($this->rtl) {
  6323. $this->rMargin = $this->w - $this->x;
  6324. $this->lMargin = $this->x - $w;
  6325. } else {
  6326. $this->lMargin = $this->x;
  6327. $this->rMargin = $this->w - $this->x - $w;
  6328. }
  6329. if ($autopadding) {
  6330. // add top padding
  6331. $this->y += $mc_padding['T'];
  6332. }
  6333. if ($ishtml) { // ******* Write HTML text
  6334. $this->writeHTML($txt, true, false, $reseth, true, $align);
  6335. $nl = 1;
  6336. } else { // ******* Write simple text
  6337. $prev_FontSizePt = $this->FontSizePt;
  6338. // vertical alignment
  6339. if ($maxh > 0) {
  6340. // get text height
  6341. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6342. if ($fitcell) {
  6343. // try to reduce font size to fit text on cell (use a quick search algorithm)
  6344. $fmin = 1;
  6345. $fmax = $this->FontSizePt;
  6346. $prev_text_height = $text_height;
  6347. $maxit = 100; // max number of iterations
  6348. while ($maxit > 0) {
  6349. $fmid = (($fmax + $fmin) / 2);
  6350. $this->SetFontSize($fmid, false);
  6351. $this->resetLastH();
  6352. $text_height = $this->getStringHeight($w, $txt, $reseth, $autopadding, $mc_padding, $border);
  6353. if (($text_height == $maxh) OR (($text_height < $maxh) AND ($fmin >= ($fmax - 0.01)))) {
  6354. break;
  6355. } elseif ($text_height < $maxh) {
  6356. $fmin = $fmid;
  6357. } else {
  6358. $fmax = $fmid;
  6359. }
  6360. --$maxit;
  6361. }
  6362. $this->SetFontSize($this->FontSizePt);
  6363. }
  6364. if ($text_height < $maxh) {
  6365. if ($valign == 'M') {
  6366. // text vertically centered
  6367. $this->y += (($maxh - $text_height) / 2);
  6368. } elseif ($valign == 'B') {
  6369. // text vertically aligned on bottom
  6370. $this->y += ($maxh - $text_height);
  6371. }
  6372. }
  6373. }
  6374. $nl = $this->Write($this->lasth, $txt, '', 0, $align, true, $stretch, false, true, $maxh, 0, $mc_margin);
  6375. if ($fitcell) {
  6376. // restore font size
  6377. $this->SetFontSize($prev_FontSizePt);
  6378. }
  6379. }
  6380. if ($autopadding) {
  6381. // add bottom padding
  6382. $this->y += $mc_padding['B'];
  6383. }
  6384. // Get end-of-text Y position
  6385. $currentY = $this->y;
  6386. // get latest page number
  6387. $endpage = $this->page;
  6388. if ($resth > 0) {
  6389. $skip = ($endpage - $startpage);
  6390. $tmpresth = $resth;
  6391. while ($tmpresth > 0) {
  6392. if ($skip <= 0) {
  6393. // add a page (or trig AcceptPageBreak() for multicolumn mode)
  6394. $this->checkPageBreak($this->PageBreakTrigger + 1);
  6395. }
  6396. if ($this->num_columns > 1) {
  6397. $tmpresth -= ($this->h - $this->y - $this->bMargin);
  6398. } else {
  6399. $tmpresth -= ($this->h - $this->tMargin - $this->bMargin);
  6400. }
  6401. --$skip;
  6402. }
  6403. $currentY = $this->y;
  6404. $endpage = $this->page;
  6405. }
  6406. // get latest column
  6407. $endcolumn = $this->current_column;
  6408. if ($this->num_columns == 0) {
  6409. $this->num_columns = 1;
  6410. }
  6411. // get border modes
  6412. $border_start = $this->getBorderMode($border, $position='start');
  6413. $border_end = $this->getBorderMode($border, $position='end');
  6414. $border_middle = $this->getBorderMode($border, $position='middle');
  6415. // design borders around HTML cells.
  6416. for ($page = $startpage; $page <= $endpage; ++$page) { // for each page
  6417. $ccode = '';
  6418. $this->setPage($page);
  6419. if ($this->num_columns < 2) {
  6420. // single-column mode
  6421. $this->SetX($x);
  6422. $this->y = $this->tMargin;
  6423. }
  6424. // account for margin changes
  6425. if ($page > $startpage) {
  6426. if (($this->rtl) AND ($this->pagedim[$page]['orm'] != $this->pagedim[$startpage]['orm'])) {
  6427. $this->x -= ($this->pagedim[$page]['orm'] - $this->pagedim[$startpage]['orm']);
  6428. } elseif ((!$this->rtl) AND ($this->pagedim[$page]['olm'] != $this->pagedim[$startpage]['olm'])) {
  6429. $this->x += ($this->pagedim[$page]['olm'] - $this->pagedim[$startpage]['olm']);
  6430. }
  6431. }
  6432. if ($startpage == $endpage) {
  6433. // single page
  6434. for ($column = $startcolumn; $column <= $endcolumn; ++$column) { // for each column
  6435. $this->selectColumn($column);
  6436. if ($this->rtl) {
  6437. $this->x -= $mc_margin['R'];
  6438. } else {
  6439. $this->x += $mc_margin['L'];
  6440. }
  6441. if ($startcolumn == $endcolumn) { // single column
  6442. $cborder = $border;
  6443. $h = max($h, ($currentY - $oy));
  6444. $this->y = $oy;
  6445. } elseif ($column == $startcolumn) { // first column
  6446. $cborder = $border_start;
  6447. $this->y = $oy;
  6448. $h = $this->h - $this->y - $this->bMargin;
  6449. } elseif ($column == $endcolumn) { // end column
  6450. $cborder = $border_end;
  6451. $h = $currentY - $this->y;
  6452. if ($resth > $h) {
  6453. $h = $resth;
  6454. }
  6455. } else { // middle column
  6456. $cborder = $border_middle;
  6457. $h = $this->h - $this->y - $this->bMargin;
  6458. $resth -= $h;
  6459. }
  6460. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6461. } // end for each column
  6462. } elseif ($page == $startpage) { // first page
  6463. for ($column = $startcolumn; $column < $this->num_columns; ++$column) { // for each column
  6464. $this->selectColumn($column);
  6465. if ($this->rtl) {
  6466. $this->x -= $mc_margin['R'];
  6467. } else {
  6468. $this->x += $mc_margin['L'];
  6469. }
  6470. if ($column == $startcolumn) { // first column
  6471. $cborder = $border_start;
  6472. $this->y = $oy;
  6473. $h = $this->h - $this->y - $this->bMargin;
  6474. } else { // middle column
  6475. $cborder = $border_middle;
  6476. $h = $this->h - $this->y - $this->bMargin;
  6477. $resth -= $h;
  6478. }
  6479. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6480. } // end for each column
  6481. } elseif ($page == $endpage) { // last page
  6482. for ($column = 0; $column <= $endcolumn; ++$column) { // for each column
  6483. $this->selectColumn($column);
  6484. if ($this->rtl) {
  6485. $this->x -= $mc_margin['R'];
  6486. } else {
  6487. $this->x += $mc_margin['L'];
  6488. }
  6489. if ($column == $endcolumn) {
  6490. // end column
  6491. $cborder = $border_end;
  6492. $h = $currentY - $this->y;
  6493. if ($resth > $h) {
  6494. $h = $resth;
  6495. }
  6496. } else {
  6497. // middle column
  6498. $cborder = $border_middle;
  6499. $h = $this->h - $this->y - $this->bMargin;
  6500. $resth -= $h;
  6501. }
  6502. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6503. } // end for each column
  6504. } else { // middle page
  6505. for ($column = 0; $column < $this->num_columns; ++$column) { // for each column
  6506. $this->selectColumn($column);
  6507. if ($this->rtl) {
  6508. $this->x -= $mc_margin['R'];
  6509. } else {
  6510. $this->x += $mc_margin['L'];
  6511. }
  6512. $cborder = $border_middle;
  6513. $h = $this->h - $this->y - $this->bMargin;
  6514. $resth -= $h;
  6515. $ccode .= $this->getCellCode($w, $h, '', $cborder, 1, '', $fill, '', 0, true)."\n";
  6516. } // end for each column
  6517. }
  6518. if ($cborder OR $fill) {
  6519. $offsetlen = strlen($ccode);
  6520. // draw border and fill
  6521. if ($this->inxobj) {
  6522. // we are inside an XObject template
  6523. if (end($this->xobjects[$this->xobjid]['transfmrk']) !== false) {
  6524. $pagemarkkey = key($this->xobjects[$this->xobjid]['transfmrk']);
  6525. $pagemark = $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey];
  6526. $this->xobjects[$this->xobjid]['transfmrk'][$pagemarkkey] += $offsetlen;
  6527. } else {
  6528. $pagemark = $this->xobjects[$this->xobjid]['intmrk'];
  6529. $this->xobjects[$this->xobjid]['intmrk'] += $offsetlen;
  6530. }
  6531. $pagebuff = $this->xobjects[$this->xobjid]['outdata'];
  6532. $pstart = substr($pagebuff, 0, $pagemark);
  6533. $pend = substr($pagebuff, $pagemark);
  6534. $this->xobjects[$this->xobjid]['outdata'] = $pstart.$ccode.$pend;
  6535. } else {
  6536. if (end($this->transfmrk[$this->page]) !== false) {
  6537. $pagemarkkey = key($this->transfmrk[$this->page]);
  6538. $pagemark = $this->transfmrk[$this->page][$pagemarkkey];
  6539. $this->transfmrk[$this->page][$pagemarkkey] += $offsetlen;
  6540. } elseif ($this->InFooter) {
  6541. $pagemark = $this->footerpos[$this->page];
  6542. $this->footerpos[$this->page] += $offsetlen;
  6543. } else {
  6544. $pagemark = $this->intmrk[$this->page];
  6545. $this->intmrk[$this->page] += $offsetlen;
  6546. }
  6547. $pagebuff = $this->getPageBuffer($this->page);
  6548. $pstart = substr($pagebuff, 0, $pagemark);
  6549. $pend = substr($pagebuff, $pagemark);
  6550. $this->setPageBuffer($this->page, $pstart.$ccode.$pend);
  6551. }
  6552. }
  6553. } // end for each page
  6554. // Get end-of-cell Y position
  6555. $currentY = $this->GetY();
  6556. // restore previous values
  6557. if ($this->num_columns > 1) {
  6558. $this->selectColumn();
  6559. } else {
  6560. // restore original margins
  6561. $this->lMargin = $lMargin;
  6562. $this->rMargin = $rMargin;
  6563. if ($this->page > $startpage) {
  6564. // check for margin variations between pages (i.e. booklet mode)
  6565. $dl = ($this->pagedim[$this->page]['olm'] - $this->pagedim[$startpage]['olm']);
  6566. $dr = ($this->pagedim[$this->page]['orm'] - $this->pagedim[$startpage]['orm']);
  6567. if (($dl != 0) OR ($dr != 0)) {
  6568. $this->lMargin += $dl;
  6569. $this->rMargin += $dr;
  6570. }
  6571. }
  6572. }
  6573. if ($ln > 0) {
  6574. //Go to the beginning of the next line
  6575. $this->SetY($currentY + $mc_margin['B']);
  6576. if ($ln == 2) {
  6577. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6578. }
  6579. } else {
  6580. // go left or right by case
  6581. $this->setPage($startpage);
  6582. $this->y = $y;
  6583. $this->SetX($x + $w + $mc_margin['L'] + $mc_margin['R']);
  6584. }
  6585. $this->setContentMark();
  6586. $this->cell_padding = $prev_cell_padding;
  6587. $this->cell_margin = $prev_cell_margin;
  6588. return $nl;
  6589. }
  6590. /**
  6591. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  6592. * @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)))
  6593. * @param $position (string) multicell position: 'start', 'middle', 'end'
  6594. * @return border mode array
  6595. * @protected
  6596. * @since 4.4.002 (2008-12-09)
  6597. */
  6598. protected function getBorderMode($brd, $position='start') {
  6599. if ((!$this->opencell) OR empty($brd)) {
  6600. return $brd;
  6601. }
  6602. if ($brd == 1) {
  6603. $brd = 'LTRB';
  6604. }
  6605. if (is_string($brd)) {
  6606. // convert string to array
  6607. $slen = strlen($brd);
  6608. $newbrd = array();
  6609. for ($i = 0; $i < $slen; ++$i) {
  6610. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  6611. }
  6612. $brd = $newbrd;
  6613. }
  6614. foreach ($brd as $border => $style) {
  6615. switch ($position) {
  6616. case 'start': {
  6617. if (strpos($border, 'B') !== false) {
  6618. // remove bottom line
  6619. $newkey = str_replace('B', '', $border);
  6620. if (strlen($newkey) > 0) {
  6621. $brd[$newkey] = $style;
  6622. }
  6623. unset($brd[$border]);
  6624. }
  6625. break;
  6626. }
  6627. case 'middle': {
  6628. if (strpos($border, 'B') !== false) {
  6629. // remove bottom line
  6630. $newkey = str_replace('B', '', $border);
  6631. if (strlen($newkey) > 0) {
  6632. $brd[$newkey] = $style;
  6633. }
  6634. unset($brd[$border]);
  6635. $border = $newkey;
  6636. }
  6637. if (strpos($border, 'T') !== false) {
  6638. // remove bottom line
  6639. $newkey = str_replace('T', '', $border);
  6640. if (strlen($newkey) > 0) {
  6641. $brd[$newkey] = $style;
  6642. }
  6643. unset($brd[$border]);
  6644. }
  6645. break;
  6646. }
  6647. case 'end': {
  6648. if (strpos($border, 'T') !== false) {
  6649. // remove bottom line
  6650. $newkey = str_replace('T', '', $border);
  6651. if (strlen($newkey) > 0) {
  6652. $brd[$newkey] = $style;
  6653. }
  6654. unset($brd[$border]);
  6655. }
  6656. break;
  6657. }
  6658. }
  6659. }
  6660. return $brd;
  6661. }
  6662. /**
  6663. * This method return the estimated number of lines for print a simple text string using Multicell() method.
  6664. * @param $txt (string) String for calculating his height
  6665. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6666. * @param $reseth (boolean) if true reset the last cell height (default false).
  6667. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6668. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6669. * @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)))
  6670. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6671. * @author Alexander Escalona Fern�ndez, Nicola Asuni
  6672. * @public
  6673. * @since 4.5.011
  6674. */
  6675. public function getNumLines($txt, $w=0, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6676. if ($txt === '') {
  6677. // empty string
  6678. return 1;
  6679. }
  6680. // adjust internal padding
  6681. $prev_cell_padding = $this->cell_padding;
  6682. $prev_lasth = $this->lasth;
  6683. if (is_array($cellpadding)) {
  6684. $this->cell_padding = $cellpadding;
  6685. }
  6686. $this->adjustCellPadding($border);
  6687. if ($this->empty_string($w) OR ($w <= 0)) {
  6688. if ($this->rtl) {
  6689. $w = $this->x - $this->lMargin;
  6690. } else {
  6691. $w = $this->w - $this->rMargin - $this->x;
  6692. }
  6693. }
  6694. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  6695. if ($reseth) {
  6696. // reset row height
  6697. $this->resetLastH();
  6698. }
  6699. $lines = 1;
  6700. $sum = 0;
  6701. $chars = $this->utf8Bidi($this->UTF8StringToArray($txt), $txt, $this->tmprtl);
  6702. $charsWidth = $this->GetArrStringWidth($chars, '', '', 0, true);
  6703. $length = count($chars);
  6704. $lastSeparator = -1;
  6705. for ($i = 0; $i < $length; ++$i) {
  6706. $charWidth = $charsWidth[$i];
  6707. if (preg_match($this->re_spaces, $this->unichr($chars[$i]))) {
  6708. $lastSeparator = $i;
  6709. }
  6710. if ((($sum + $charWidth) > $wmax) OR ($chars[$i] == 10)) {
  6711. ++$lines;
  6712. if ($chars[$i] == 10) {
  6713. $lastSeparator = -1;
  6714. $sum = 0;
  6715. } elseif ($lastSeparator != -1) {
  6716. $i = $lastSeparator;
  6717. $lastSeparator = -1;
  6718. $sum = 0;
  6719. } else {
  6720. $sum = $charWidth;
  6721. }
  6722. } else {
  6723. $sum += $charWidth;
  6724. }
  6725. }
  6726. if ($chars[($length - 1)] == 10) {
  6727. --$lines;
  6728. }
  6729. $this->cell_padding = $prev_cell_padding;
  6730. $this->lasth = $prev_lasth;
  6731. return $lines;
  6732. }
  6733. /**
  6734. * This method return the estimated height needed for printing a simple text string using the Multicell() method.
  6735. * Generally, if you want to know the exact height for a block of content you can use the following alternative technique:
  6736. * @pre
  6737. * // store current object
  6738. * $pdf->startTransaction();
  6739. * // store starting values
  6740. * $start_y = $pdf->GetY();
  6741. * $start_page = $pdf->getPage();
  6742. * // call your printing functions with your parameters
  6743. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6744. * $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);
  6745. * // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  6746. * // get the new Y
  6747. * $end_y = $pdf->GetY();
  6748. * $end_page = $pdf->getPage();
  6749. * // calculate height
  6750. * $height = 0;
  6751. * if ($end_page == $start_page) {
  6752. * $height = $end_y - $start_y;
  6753. * } else {
  6754. * for ($page=$start_page; $page <= $end_page; ++$page) {
  6755. * $this->setPage($page);
  6756. * if ($page == $start_page) {
  6757. * // first page
  6758. * $height = $this->h - $start_y - $this->bMargin;
  6759. * } elseif ($page == $end_page) {
  6760. * // last page
  6761. * $height = $end_y - $this->tMargin;
  6762. * } else {
  6763. * $height = $this->h - $this->tMargin - $this->bMargin;
  6764. * }
  6765. * }
  6766. * }
  6767. * // restore previous object
  6768. * $pdf = $pdf->rollbackTransaction();
  6769. *
  6770. * @param $w (float) Width of cells. If 0, they extend up to the right margin of the page.
  6771. * @param $txt (string) String for calculating his height
  6772. * @param $reseth (boolean) if true reset the last cell height (default false).
  6773. * @param $autopadding (boolean) if true, uses internal padding and automatically adjust it to account for line width (default true).
  6774. * @param $cellpadding (float) Internal cell padding, if empty uses default cell padding.
  6775. * @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)))
  6776. * @return float Return the minimal height needed for multicell method for printing the $txt param.
  6777. * @author Nicola Asuni, Alexander Escalona Fern�ndez
  6778. * @public
  6779. */
  6780. public function getStringHeight($w, $txt, $reseth=false, $autopadding=true, $cellpadding='', $border=0) {
  6781. // adjust internal padding
  6782. $prev_cell_padding = $this->cell_padding;
  6783. $prev_lasth = $this->lasth;
  6784. if (is_array($cellpadding)) {
  6785. $this->cell_padding = $cellpadding;
  6786. }
  6787. $this->adjustCellPadding($border);
  6788. $lines = $this->getNumLines($txt, $w, $reseth, $autopadding, $cellpadding, $border);
  6789. $height = $lines * ($this->FontSize * $this->cell_height_ratio);
  6790. if ($autopadding) {
  6791. // add top and bottom padding
  6792. $height += ($this->cell_padding['T'] + $this->cell_padding['B']);
  6793. }
  6794. $this->cell_padding = $prev_cell_padding;
  6795. $this->lasth = $prev_lasth;
  6796. return $height;
  6797. }
  6798. /**
  6799. * This method prints text from the current position.<br />
  6800. * @param $h (float) Line height
  6801. * @param $txt (string) String to print
  6802. * @param $link (mixed) URL or identifier returned by AddLink()
  6803. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  6804. * @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>
  6805. * @param $ln (boolean) if true set cursor at the bottom of the line, otherwise set cursor at the top of the line.
  6806. * @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.
  6807. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  6808. * @param $firstblock (boolean) if true the string is the starting of a line.
  6809. * @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.
  6810. * @param $wadj (float) first line width will be reduced by this amount (used in HTML mode).
  6811. * @param $margin (array) margin array of the parent container
  6812. * @return mixed Return the number of cells or the remaining string if $firstline = true.
  6813. * @public
  6814. * @since 1.5
  6815. */
  6816. public function Write($h, $txt, $link='', $fill=false, $align='', $ln=false, $stretch=0, $firstline=false, $firstblock=false, $maxh=0, $wadj=0, $margin='') {
  6817. // check page for no-write regions and adapt page margins if necessary
  6818. list($this->x, $this->y) = $this->checkPageRegions($h, $this->x, $this->y);
  6819. if (strlen($txt) == 0) {
  6820. // fix empty text
  6821. $txt = ' ';
  6822. }
  6823. if ($margin === '') {
  6824. // set default margins
  6825. $margin = $this->cell_margin;
  6826. }
  6827. // remove carriage returns
  6828. $s = str_replace("\r", '', $txt);
  6829. // check if string contains arabic text
  6830. if (preg_match($this->unicode->uni_RE_PATTERN_ARABIC, $s)) {
  6831. $arabic = true;
  6832. } else {
  6833. $arabic = false;
  6834. }
  6835. // check if string contains RTL text
  6836. if ($arabic OR ($this->tmprtl == 'R') OR preg_match($this->unicode->uni_RE_PATTERN_RTL, $s)) {
  6837. $rtlmode = true;
  6838. } else {
  6839. $rtlmode = false;
  6840. }
  6841. // get a char width
  6842. $chrwidth = $this->GetCharWidth(46); // dot character
  6843. // get array of unicode values
  6844. $chars = $this->UTF8StringToArray($s);
  6845. // get array of chars
  6846. $uchars = $this->UTF8ArrayToUniArray($chars);
  6847. // get the number of characters
  6848. $nb = count($chars);
  6849. // replacement for SHY character (minus symbol)
  6850. $shy_replacement = 45;
  6851. $shy_replacement_char = $this->unichr($shy_replacement);
  6852. // widht for SHY replacement
  6853. $shy_replacement_width = $this->GetCharWidth($shy_replacement);
  6854. // max Y
  6855. $maxy = $this->y + $maxh - $h - $this->cell_padding['T'] - $this->cell_padding['B'];
  6856. // page width
  6857. $pw = $w = $this->w - $this->lMargin - $this->rMargin;
  6858. // calculate remaining line width ($w)
  6859. if ($this->rtl) {
  6860. $w = $this->x - $this->lMargin;
  6861. } else {
  6862. $w = $this->w - $this->rMargin - $this->x;
  6863. }
  6864. // max column width
  6865. $wmax = ($w - $wadj);
  6866. if (!$firstline) {
  6867. $wmax -= ($this->cell_padding['L'] + $this->cell_padding['R']);
  6868. }
  6869. if ((!$firstline) AND (($chrwidth > $wmax) OR ($this->GetCharWidth($chars[0]) > $wmax))) {
  6870. // a single character do not fit on column
  6871. return '';
  6872. }
  6873. // minimum row height
  6874. $row_height = max($h, $this->FontSize * $this->cell_height_ratio);
  6875. $start_page = $this->page;
  6876. $i = 0; // character position
  6877. $j = 0; // current starting position
  6878. $sep = -1; // position of the last blank space
  6879. $shy = false; // true if the last blank is a soft hypen (SHY)
  6880. $l = 0; // current string length
  6881. $nl = 0; //number of lines
  6882. $linebreak = false;
  6883. $pc = 0; // previous character
  6884. // for each character
  6885. while ($i < $nb) {
  6886. if (($maxh > 0) AND ($this->y >= $maxy) ) {
  6887. break;
  6888. }
  6889. //Get the current character
  6890. $c = $chars[$i];
  6891. if ($c == 10) { // 10 = "\n" = new line
  6892. //Explicit line break
  6893. if ($align == 'J') {
  6894. if ($this->rtl) {
  6895. $talign = 'R';
  6896. } else {
  6897. $talign = 'L';
  6898. }
  6899. } else {
  6900. $talign = $align;
  6901. }
  6902. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  6903. if ($firstline) {
  6904. $startx = $this->x;
  6905. $tmparr = array_slice($chars, $j, ($i - $j));
  6906. if ($rtlmode) {
  6907. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  6908. }
  6909. $linew = $this->GetArrStringWidth($tmparr);
  6910. unset($tmparr);
  6911. if ($this->rtl) {
  6912. $this->endlinex = $startx - $linew;
  6913. } else {
  6914. $this->endlinex = $startx + $linew;
  6915. }
  6916. $w = $linew;
  6917. $tmpcellpadding = $this->cell_padding;
  6918. if ($maxh == 0) {
  6919. $this->SetCellPadding(0);
  6920. }
  6921. }
  6922. if ($firstblock AND $this->isRTLTextDir()) {
  6923. $tmpstr = $this->stringRightTrim($tmpstr);
  6924. }
  6925. // Skip newlines at the begining of a page or column
  6926. if (!empty($tmpstr) OR ($this->y < ($this->PageBreakTrigger - $row_height))) {
  6927. $this->Cell($w, $h, $tmpstr, 0, 1, $talign, $fill, $link, $stretch);
  6928. }
  6929. unset($tmpstr);
  6930. if ($firstline) {
  6931. $this->cell_padding = $tmpcellpadding;
  6932. return ($this->UniArrSubString($uchars, $i));
  6933. }
  6934. ++$nl;
  6935. $j = $i + 1;
  6936. $l = 0;
  6937. $sep = -1;
  6938. $shy = false;
  6939. // account for margin changes
  6940. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  6941. $this->AcceptPageBreak();
  6942. if ($this->rtl) {
  6943. $this->x -= $margin['R'];
  6944. } else {
  6945. $this->x += $margin['L'];
  6946. }
  6947. $this->lMargin += $margin['L'];
  6948. $this->rMargin += $margin['R'];
  6949. }
  6950. $w = $this->getRemainingWidth();
  6951. $wmax = ($w - $this->cell_padding['L'] - $this->cell_padding['R']);
  6952. } else {
  6953. // 160 is the non-breaking space.
  6954. // 173 is SHY (Soft Hypen).
  6955. // \p{Z} or \p{Separator}: any kind of Unicode whitespace or invisible separator.
  6956. // \p{Lo} or \p{Other_Letter}: a Unicode letter or ideograph that does not have lowercase and uppercase variants.
  6957. // \p{Lo} is needed because Chinese characters are packed next to each other without spaces in between.
  6958. if (($c != 160)
  6959. AND (($c == 173)
  6960. OR preg_match($this->re_spaces, $this->unichr($c))
  6961. OR (($c == 45)
  6962. AND ($i < ($nb - 1))
  6963. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], $this->unichr($pc))
  6964. AND @preg_match('/[\p{L}]/'.$this->re_space['m'], $this->unichr($chars[($i + 1)]))
  6965. )
  6966. )
  6967. ) {
  6968. // update last blank space position
  6969. $sep = $i;
  6970. // check if is a SHY
  6971. if (($c == 173) OR ($c == 45)) {
  6972. $shy = true;
  6973. if ($pc == 45) {
  6974. $tmp_shy_replacement_width = 0;
  6975. $tmp_shy_replacement_char = '';
  6976. } else {
  6977. $tmp_shy_replacement_width = $shy_replacement_width;
  6978. $tmp_shy_replacement_char = $shy_replacement_char;
  6979. }
  6980. } else {
  6981. $shy = false;
  6982. }
  6983. }
  6984. // update string length
  6985. if ($this->isUnicodeFont() AND ($arabic)) {
  6986. // with bidirectional algorithm some chars may be changed affecting the line length
  6987. // *** very slow ***
  6988. $l = $this->GetArrStringWidth($this->utf8Bidi(array_slice($chars, $j, ($i - $j)), '', $this->tmprtl));
  6989. } else {
  6990. $l += $this->GetCharWidth($c);
  6991. }
  6992. if (($l > $wmax) OR (($c == 173) AND (($l + $tmp_shy_replacement_width) > $wmax)) ) {
  6993. // we have reached the end of column
  6994. if ($sep == -1) {
  6995. // check if the line was already started
  6996. if (($this->rtl AND ($this->x <= ($this->w - $this->rMargin - $this->cell_padding['R'] - $margin['R'] - $chrwidth)))
  6997. OR ((!$this->rtl) AND ($this->x >= ($this->lMargin + $this->cell_padding['L'] + $margin['L'] + $chrwidth)))) {
  6998. // print a void cell and go to next line
  6999. $this->Cell($w, $h, '', 0, 1);
  7000. $linebreak = true;
  7001. if ($firstline) {
  7002. return ($this->UniArrSubString($uchars, $j));
  7003. }
  7004. } else {
  7005. // truncate the word because do not fit on column
  7006. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  7007. if ($firstline) {
  7008. $startx = $this->x;
  7009. $tmparr = array_slice($chars, $j, ($i - $j));
  7010. if ($rtlmode) {
  7011. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7012. }
  7013. $linew = $this->GetArrStringWidth($tmparr);
  7014. unset($tmparr);
  7015. if ($this->rtl) {
  7016. $this->endlinex = $startx - $linew;
  7017. } else {
  7018. $this->endlinex = $startx + $linew;
  7019. }
  7020. $w = $linew;
  7021. $tmpcellpadding = $this->cell_padding;
  7022. if ($maxh == 0) {
  7023. $this->SetCellPadding(0);
  7024. }
  7025. }
  7026. if ($firstblock AND $this->isRTLTextDir()) {
  7027. $tmpstr = $this->stringRightTrim($tmpstr);
  7028. }
  7029. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  7030. unset($tmpstr);
  7031. if ($firstline) {
  7032. $this->cell_padding = $tmpcellpadding;
  7033. return ($this->UniArrSubString($uchars, $i));
  7034. }
  7035. $j = $i;
  7036. --$i;
  7037. }
  7038. } else {
  7039. // word wrapping
  7040. if ($this->rtl AND (!$firstblock) AND ($sep < $i)) {
  7041. $endspace = 1;
  7042. } else {
  7043. $endspace = 0;
  7044. }
  7045. // check the length of the next string
  7046. $strrest = $this->UniArrSubString($uchars, ($sep + $endspace));
  7047. $nextstr = preg_split('/'.$this->re_space['p'].'/'.$this->re_space['m'], $this->stringTrim($strrest));
  7048. if (isset($nextstr[0]) AND ($this->GetStringWidth($nextstr[0]) > $pw)) {
  7049. // truncate the word because do not fit on a full page width
  7050. $tmpstr = $this->UniArrSubString($uchars, $j, $i);
  7051. if ($firstline) {
  7052. $startx = $this->x;
  7053. $tmparr = array_slice($chars, $j, ($i - $j));
  7054. if ($rtlmode) {
  7055. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7056. }
  7057. $linew = $this->GetArrStringWidth($tmparr);
  7058. unset($tmparr);
  7059. if ($this->rtl) {
  7060. $this->endlinex = ($startx - $linew);
  7061. } else {
  7062. $this->endlinex = ($startx + $linew);
  7063. }
  7064. $w = $linew;
  7065. $tmpcellpadding = $this->cell_padding;
  7066. if ($maxh == 0) {
  7067. $this->SetCellPadding(0);
  7068. }
  7069. }
  7070. if ($firstblock AND $this->isRTLTextDir()) {
  7071. $tmpstr = $this->stringRightTrim($tmpstr);
  7072. }
  7073. $this->Cell($w, $h, $tmpstr, 0, 1, $align, $fill, $link, $stretch);
  7074. unset($tmpstr);
  7075. if ($firstline) {
  7076. $this->cell_padding = $tmpcellpadding;
  7077. return ($this->UniArrSubString($uchars, $i));
  7078. }
  7079. $j = $i;
  7080. --$i;
  7081. } else {
  7082. // word wrapping
  7083. if ($shy) {
  7084. // add hypen (minus symbol) at the end of the line
  7085. $shy_width = $tmp_shy_replacement_width;
  7086. if ($this->rtl) {
  7087. $shy_char_left = $tmp_shy_replacement_char;
  7088. $shy_char_right = '';
  7089. } else {
  7090. $shy_char_left = '';
  7091. $shy_char_right = $tmp_shy_replacement_char;
  7092. }
  7093. } else {
  7094. $shy_width = 0;
  7095. $shy_char_left = '';
  7096. $shy_char_right = '';
  7097. }
  7098. $tmpstr = $this->UniArrSubString($uchars, $j, ($sep + $endspace));
  7099. if ($firstline) {
  7100. $startx = $this->x;
  7101. $tmparr = array_slice($chars, $j, (($sep + $endspace) - $j));
  7102. if ($rtlmode) {
  7103. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7104. }
  7105. $linew = $this->GetArrStringWidth($tmparr);
  7106. unset($tmparr);
  7107. if ($this->rtl) {
  7108. $this->endlinex = $startx - $linew - $shy_width;
  7109. } else {
  7110. $this->endlinex = $startx + $linew + $shy_width;
  7111. }
  7112. $w = $linew;
  7113. $tmpcellpadding = $this->cell_padding;
  7114. if ($maxh == 0) {
  7115. $this->SetCellPadding(0);
  7116. }
  7117. }
  7118. // print the line
  7119. if ($firstblock AND $this->isRTLTextDir()) {
  7120. $tmpstr = $this->stringRightTrim($tmpstr);
  7121. }
  7122. $this->Cell($w, $h, $shy_char_left.$tmpstr.$shy_char_right, 0, 1, $align, $fill, $link, $stretch);
  7123. unset($tmpstr);
  7124. if ($firstline) {
  7125. if ($chars[$sep] == 45) {
  7126. $endspace += 1;
  7127. }
  7128. // return the remaining text
  7129. $this->cell_padding = $tmpcellpadding;
  7130. return ($this->UniArrSubString($uchars, ($sep + $endspace)));
  7131. }
  7132. $i = $sep;
  7133. $sep = -1;
  7134. $shy = false;
  7135. $j = ($i + 1);
  7136. }
  7137. }
  7138. // account for margin changes
  7139. if ((($this->y + $this->lasth) > $this->PageBreakTrigger) AND ($this->inPageBody())) {
  7140. $this->AcceptPageBreak();
  7141. if ($this->rtl) {
  7142. $this->x -= $margin['R'];
  7143. } else {
  7144. $this->x += $margin['L'];
  7145. }
  7146. $this->lMargin += $margin['L'];
  7147. $this->rMargin += $margin['R'];
  7148. }
  7149. $w = $this->getRemainingWidth();
  7150. $wmax = $w - $this->cell_padding['L'] - $this->cell_padding['R'];
  7151. if ($linebreak) {
  7152. $linebreak = false;
  7153. } else {
  7154. ++$nl;
  7155. $l = 0;
  7156. }
  7157. }
  7158. }
  7159. // save last character
  7160. $pc = $c;
  7161. ++$i;
  7162. } // end while i < nb
  7163. // print last substring (if any)
  7164. if ($l > 0) {
  7165. switch ($align) {
  7166. case 'J':
  7167. case 'C': {
  7168. $w = $w;
  7169. break;
  7170. }
  7171. case 'L': {
  7172. if ($this->rtl) {
  7173. $w = $w;
  7174. } else {
  7175. $w = $l;
  7176. }
  7177. break;
  7178. }
  7179. case 'R': {
  7180. if ($this->rtl) {
  7181. $w = $l;
  7182. } else {
  7183. $w = $w;
  7184. }
  7185. break;
  7186. }
  7187. default: {
  7188. $w = $l;
  7189. break;
  7190. }
  7191. }
  7192. $tmpstr = $this->UniArrSubString($uchars, $j, $nb);
  7193. if ($firstline) {
  7194. $startx = $this->x;
  7195. $tmparr = array_slice($chars, $j, ($nb - $j));
  7196. if ($rtlmode) {
  7197. $tmparr = $this->utf8Bidi($tmparr, $tmpstr, $this->tmprtl);
  7198. }
  7199. $linew = $this->GetArrStringWidth($tmparr);
  7200. unset($tmparr);
  7201. if ($this->rtl) {
  7202. $this->endlinex = $startx - $linew;
  7203. } else {
  7204. $this->endlinex = $startx + $linew;
  7205. }
  7206. $w = $linew;
  7207. $tmpcellpadding = $this->cell_padding;
  7208. if ($maxh == 0) {
  7209. $this->SetCellPadding(0);
  7210. }
  7211. }
  7212. if ($firstblock AND $this->isRTLTextDir()) {
  7213. $tmpstr = $this->stringRightTrim($tmpstr);
  7214. }
  7215. $this->Cell($w, $h, $tmpstr, 0, $ln, $align, $fill, $link, $stretch);
  7216. unset($tmpstr);
  7217. if ($firstline) {
  7218. $this->cell_padding = $tmpcellpadding;
  7219. return ($this->UniArrSubString($uchars, $nb));
  7220. }
  7221. ++$nl;
  7222. }
  7223. if ($firstline) {
  7224. return '';
  7225. }
  7226. return $nl;
  7227. }
  7228. /**
  7229. * Returns the remaining width between the current position and margins.
  7230. * @return int Return the remaining width
  7231. * @protected
  7232. */
  7233. protected function getRemainingWidth() {
  7234. list($this->x, $this->y) = $this->checkPageRegions(0, $this->x, $this->y);
  7235. if ($this->rtl) {
  7236. return ($this->x - $this->lMargin);
  7237. } else {
  7238. return ($this->w - $this->rMargin - $this->x);
  7239. }
  7240. }
  7241. /**
  7242. * Extract a slice of the $strarr array and return it as string.
  7243. * @param $strarr (string) The input array of characters.
  7244. * @param $start (int) the starting element of $strarr.
  7245. * @param $end (int) first element that will not be returned.
  7246. * @return Return part of a string
  7247. * @public
  7248. */
  7249. public function UTF8ArrSubString($strarr, $start='', $end='') {
  7250. if (strlen($start) == 0) {
  7251. $start = 0;
  7252. }
  7253. if (strlen($end) == 0) {
  7254. $end = count($strarr);
  7255. }
  7256. $string = '';
  7257. for ($i=$start; $i < $end; ++$i) {
  7258. $string .= $this->unichr($strarr[$i]);
  7259. }
  7260. return $string;
  7261. }
  7262. /**
  7263. * Extract a slice of the $uniarr array and return it as string.
  7264. * @param $uniarr (string) The input array of characters.
  7265. * @param $start (int) the starting element of $strarr.
  7266. * @param $end (int) first element that will not be returned.
  7267. * @return Return part of a string
  7268. * @public
  7269. * @since 4.5.037 (2009-04-07)
  7270. */
  7271. public function UniArrSubString($uniarr, $start='', $end='') {
  7272. if (strlen($start) == 0) {
  7273. $start = 0;
  7274. }
  7275. if (strlen($end) == 0) {
  7276. $end = count($uniarr);
  7277. }
  7278. $string = '';
  7279. for ($i=$start; $i < $end; ++$i) {
  7280. $string .= $uniarr[$i];
  7281. }
  7282. return $string;
  7283. }
  7284. /**
  7285. * Convert an array of UTF8 values to array of unicode characters
  7286. * @param $ta (string) The input array of UTF8 values.
  7287. * @return Return array of unicode characters
  7288. * @public
  7289. * @since 4.5.037 (2009-04-07)
  7290. */
  7291. public function UTF8ArrayToUniArray($ta) {
  7292. return array_map(array($this, 'unichr'), $ta);
  7293. }
  7294. /**
  7295. * Returns the unicode caracter specified by UTF-8 value
  7296. * @param $c (int) UTF-8 value
  7297. * @return Returns the specified character.
  7298. * @author Miguel Perez, Nicola Asuni
  7299. * @public
  7300. * @since 2.3.000 (2008-03-05)
  7301. */
  7302. public function unichr($c) {
  7303. if (!$this->isunicode) {
  7304. return chr($c);
  7305. } elseif ($c <= 0x7F) {
  7306. // one byte
  7307. return chr($c);
  7308. } elseif ($c <= 0x7FF) {
  7309. // two bytes
  7310. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  7311. } elseif ($c <= 0xFFFF) {
  7312. // three bytes
  7313. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  7314. } elseif ($c <= 0x10FFFF) {
  7315. // four bytes
  7316. return chr(0xF0 | $c >> 18).chr(0x80 | $c >> 12 & 0x3F).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  7317. } else {
  7318. return '';
  7319. }
  7320. }
  7321. /**
  7322. * Return the image type given the file name or array returned by getimagesize() function.
  7323. * @param $imgfile (string) image file name
  7324. * @param $iminfo (array) array of image information returned by getimagesize() function.
  7325. * @return string image type
  7326. * @since 4.8.017 (2009-11-27)
  7327. */
  7328. public function getImageFileType($imgfile, $iminfo=array()) {
  7329. $type = '';
  7330. if (isset($iminfo['mime']) AND !empty($iminfo['mime'])) {
  7331. $mime = explode('/', $iminfo['mime']);
  7332. if ((count($mime) > 1) AND ($mime[0] == 'image') AND (!empty($mime[1]))) {
  7333. $type = strtolower(trim($mime[1]));
  7334. }
  7335. }
  7336. if (empty($type)) {
  7337. $fileinfo = pathinfo($imgfile);
  7338. if (isset($fileinfo['extension']) AND (!$this->empty_string($fileinfo['extension']))) {
  7339. $type = strtolower(trim($fileinfo['extension']));
  7340. }
  7341. }
  7342. if ($type == 'jpg') {
  7343. $type = 'jpeg';
  7344. }
  7345. return $type;
  7346. }
  7347. /**
  7348. * Set the block dimensions accounting for page breaks and page/column fitting
  7349. * @param $w (float) width
  7350. * @param $h (float) height
  7351. * @param $x (float) X coordinate
  7352. * @param $y (float) Y coodiante
  7353. * @param $fitonpage (boolean) if true the block is resized to not exceed page dimensions.
  7354. * @return array($w, $h, $x, $y)
  7355. * @protected
  7356. * @since 5.5.009 (2010-07-05)
  7357. */
  7358. protected function fitBlock($w, $h, $x, $y, $fitonpage=false) {
  7359. if ($w <= 0) {
  7360. // set maximum width
  7361. $w = ($this->w - $this->lMargin - $this->rMargin);
  7362. }
  7363. if ($h <= 0) {
  7364. // set maximum height
  7365. $h = ($this->PageBreakTrigger - $this->tMargin);
  7366. }
  7367. // resize the block to be vertically contained on a single page or single column
  7368. if ($fitonpage OR $this->AutoPageBreak) {
  7369. $ratio_wh = ($w / $h);
  7370. if ($h > ($this->PageBreakTrigger - $this->tMargin)) {
  7371. $h = $this->PageBreakTrigger - $this->tMargin;
  7372. $w = ($h * $ratio_wh);
  7373. }
  7374. // resize the block to be horizontally contained on a single page or single column
  7375. if ($fitonpage) {
  7376. $maxw = ($this->w - $this->lMargin - $this->rMargin);
  7377. if ($w > $maxw) {
  7378. $w = $maxw;
  7379. $h = ($w / $ratio_wh);
  7380. }
  7381. }
  7382. }
  7383. // Check whether we need a new page or new column first as this does not fit
  7384. $prev_x = $this->x;
  7385. $prev_y = $this->y;
  7386. if ($this->checkPageBreak($h, $y) OR ($this->y < $prev_y)) {
  7387. $y = $this->y;
  7388. if ($this->rtl) {
  7389. $x += ($prev_x - $this->x);
  7390. } else {
  7391. $x += ($this->x - $prev_x);
  7392. }
  7393. $this->newline = true;
  7394. }
  7395. // resize the block to be contained on the remaining available page or column space
  7396. if ($fitonpage) {
  7397. $ratio_wh = ($w / $h);
  7398. if (($y + $h) > $this->PageBreakTrigger) {
  7399. $h = $this->PageBreakTrigger - $y;
  7400. $w = ($h * $ratio_wh);
  7401. }
  7402. if ((!$this->rtl) AND (($x + $w) > ($this->w - $this->rMargin))) {
  7403. $w = $this->w - $this->rMargin - $x;
  7404. $h = ($w / $ratio_wh);
  7405. } elseif (($this->rtl) AND (($x - $w) < ($this->lMargin))) {
  7406. $w = $x - $this->lMargin;
  7407. $h = ($w / $ratio_wh);
  7408. }
  7409. }
  7410. return array($w, $h, $x, $y);
  7411. }
  7412. /**
  7413. * Puts an image in the page.
  7414. * The upper-left corner must be given.
  7415. * The dimensions can be specified in different ways:<ul>
  7416. * <li>explicit width and height (expressed in user unit)</li>
  7417. * <li>one explicit dimension, the other being calculated automatically in order to keep the original proportions</li>
  7418. * <li>no explicit dimension, in which case the image is put at 72 dpi</li></ul>
  7419. * 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;
  7420. * The format can be specified explicitly or inferred from the file extension.<br />
  7421. * It is possible to put a link on the image.<br />
  7422. * Remark: if an image is used several times, only one copy will be embedded in the file.<br />
  7423. * @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').
  7424. * @param $x (float) Abscissa of the upper-left corner (LTR) or upper-right corner (RTL).
  7425. * @param $y (float) Ordinate of the upper-left corner (LTR) or upper-right corner (RTL).
  7426. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7427. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  7428. * @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.
  7429. * @param $link (mixed) URL or identifier returned by AddLink().
  7430. * @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>
  7431. * @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).
  7432. * @param $dpi (int) dot-per-inch resolution used on resize
  7433. * @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>
  7434. * @param $ismask (boolean) true if this image is a mask, false otherwise
  7435. * @param $imgmask (mixed) image object returned by this function or false
  7436. * @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)))
  7437. * @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).
  7438. * @param $hidden (boolean) If true do not display the image.
  7439. * @param $fitonpage (boolean) If true the image is resized to not exceed page dimensions.
  7440. * @param $alt (boolean) If true the image will be added as alternative and not directly printed (the ID of the image will be returned).
  7441. * @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.
  7442. * @return image information
  7443. * @public
  7444. * @since 1.1
  7445. */
  7446. 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()) {
  7447. if ($x === '') {
  7448. $x = $this->x;
  7449. }
  7450. if ($y === '') {
  7451. $y = $this->y;
  7452. }
  7453. // check page for no-write regions and adapt page margins if necessary
  7454. list($x, $y) = $this->checkPageRegions($h, $x, $y);
  7455. $cached_file = false; // true when the file is cached
  7456. $exurl = ''; // external streams
  7457. // check if we are passing an image as file or string
  7458. if ($file[0] === '@') {
  7459. // image from string
  7460. $imgdata = substr($file, 1);
  7461. $file = $this->getObjFilename('img');
  7462. $fp = fopen($file, 'w');
  7463. fwrite($fp, $imgdata);
  7464. fclose($fp);
  7465. unset($imgdata);
  7466. $cached_file = true;
  7467. $imsize = @getimagesize($file);
  7468. if ($imsize === FALSE) {
  7469. unlink($file);
  7470. $cached_file = false;
  7471. }
  7472. } else { // image file
  7473. if ($file{0} === '*') {
  7474. // image as external stream
  7475. $file = substr($file, 1);
  7476. $exurl = $file;
  7477. }
  7478. // check if is local file
  7479. if (!@file_exists($file)) {
  7480. // encode spaces on filename (file is probably an URL)
  7481. $file = str_replace(' ', '%20', $file);
  7482. }
  7483. if (@file_exists($file)) {
  7484. // get image dimensions
  7485. $imsize = @getimagesize($file);
  7486. } else {
  7487. $imsize = false;
  7488. }
  7489. if ($imsize === FALSE) {
  7490. if (function_exists('curl_init')) {
  7491. // try to get remote file data using cURL
  7492. $cs = curl_init(); // curl session
  7493. curl_setopt($cs, CURLOPT_URL, $file);
  7494. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  7495. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  7496. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  7497. if ((ini_get('open_basedir') == '') AND (ini_get('safe_mode') == 'Off')) {
  7498. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  7499. }
  7500. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  7501. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  7502. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  7503. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  7504. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  7505. $imgdata = curl_exec($cs);
  7506. curl_close($cs);
  7507. if ($imgdata !== FALSE) {
  7508. // copy image to cache
  7509. $file = $this->getObjFilename('img');
  7510. $fp = fopen($file, 'w');
  7511. fwrite($fp, $imgdata);
  7512. fclose($fp);
  7513. unset($imgdata);
  7514. $cached_file = true;
  7515. $imsize = @getimagesize($file);
  7516. if ($imsize === FALSE) {
  7517. unlink($file);
  7518. $cached_file = false;
  7519. }
  7520. }
  7521. } elseif (($w > 0) AND ($h > 0)) {
  7522. // get measures from specified data
  7523. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7524. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7525. $imsize = array($pw, $ph);
  7526. }
  7527. }
  7528. }
  7529. if ($imsize === FALSE) {
  7530. if (substr($file, 0, -34) == K_PATH_CACHE.'msk') { // mask file
  7531. // get measures from specified data
  7532. $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7533. $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
  7534. $imsize = array($pw, $ph);
  7535. } else {
  7536. $this->Error('[Image] Unable to get image: '.$file);
  7537. }
  7538. }
  7539. // file hash
  7540. $filehash = md5($this->file_id.$file);
  7541. // get original image width and height in pixels
  7542. list($pixw, $pixh) = $imsize;
  7543. // calculate image width and height on document
  7544. if (($w <= 0) AND ($h <= 0)) {
  7545. // convert image size to document unit
  7546. $w = $this->pixelsToUnits($pixw);
  7547. $h = $this->pixelsToUnits($pixh);
  7548. } elseif ($w <= 0) {
  7549. $w = $h * $pixw / $pixh;
  7550. } elseif ($h <= 0) {
  7551. $h = $w * $pixh / $pixw;
  7552. } elseif (($fitbox !== false) AND ($w > 0) AND ($h > 0)) {
  7553. if (strlen($fitbox) !== 2) {
  7554. // set default alignment
  7555. $fitbox = '--';
  7556. }
  7557. // scale image dimensions proportionally to fit within the ($w, $h) box
  7558. if ((($w * $pixh) / ($h * $pixw)) < 1) {
  7559. // store current height
  7560. $oldh = $h;
  7561. // calculate new height
  7562. $h = $w * $pixh / $pixw;
  7563. // height difference
  7564. $hdiff = ($oldh - $h);
  7565. // vertical alignment
  7566. switch (strtoupper($fitbox{1})) {
  7567. case 'T': {
  7568. break;
  7569. }
  7570. case 'M': {
  7571. $y += ($hdiff / 2);
  7572. break;
  7573. }
  7574. case 'B': {
  7575. $y += $hdiff;
  7576. break;
  7577. }
  7578. }
  7579. } else {
  7580. // store current width
  7581. $oldw = $w;
  7582. // calculate new width
  7583. $w = $h * $pixw / $pixh;
  7584. // width difference
  7585. $wdiff = ($oldw - $w);
  7586. // horizontal alignment
  7587. switch (strtoupper($fitbox{0})) {
  7588. case 'L': {
  7589. if ($this->rtl) {
  7590. $x -= $wdiff;
  7591. }
  7592. break;
  7593. }
  7594. case 'C': {
  7595. if ($this->rtl) {
  7596. $x -= ($wdiff / 2);
  7597. } else {
  7598. $x += ($wdiff / 2);
  7599. }
  7600. break;
  7601. }
  7602. case 'R': {
  7603. if (!$this->rtl) {
  7604. $x += $wdiff;
  7605. }
  7606. break;
  7607. }
  7608. }
  7609. }
  7610. }
  7611. // fit the image on available space
  7612. list($w, $h, $x, $y) = $this->fitBlock($w, $h, $x, $y, $fitonpage);
  7613. // calculate new minimum dimensions in pixels
  7614. $neww = round($w * $this->k * $dpi / $this->dpi);
  7615. $newh = round($h * $this->k * $dpi / $this->dpi);
  7616. // check if resize is necessary (resize is used only to reduce the image)
  7617. $newsize = ($neww * $newh);
  7618. $pixsize = ($pixw * $pixh);
  7619. if (intval($resize) == 2) {
  7620. $resize = true;
  7621. } elseif ($newsize >= $pixsize) {
  7622. $resize = false;
  7623. }
  7624. // check if image has been already added on document
  7625. $newimage = true;
  7626. if (in_array($file, $this->imagekeys)) {
  7627. $newimage = false;
  7628. // get existing image data
  7629. $info = $this->getImageBuffer($file);
  7630. if (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7631. // check if the newer image is larger
  7632. $oldsize = ($info['w'] * $info['h']);
  7633. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7634. $newimage = true;
  7635. }
  7636. }
  7637. } elseif (substr($file, 0, -34) != K_PATH_CACHE.'msk') {
  7638. // check for cached images with alpha channel
  7639. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  7640. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  7641. if (in_array($tempfile_plain, $this->imagekeys)) {
  7642. // get existing image data
  7643. $info = $this->getImageBuffer($tempfile_plain);
  7644. // check if the newer image is larger
  7645. $oldsize = ($info['w'] * $info['h']);
  7646. if ((($oldsize < $newsize) AND ($resize)) OR (($oldsize < $pixsize) AND (!$resize))) {
  7647. $newimage = true;
  7648. } else {
  7649. $newimage = false;
  7650. // embed mask image
  7651. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  7652. // embed image, masked with previously embedded mask
  7653. return $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  7654. }
  7655. }
  7656. }
  7657. if ($newimage) {
  7658. //First use of image, get info
  7659. $type = strtolower($type);
  7660. if ($type == '') {
  7661. $type = $this->getImageFileType($file, $imsize);
  7662. } elseif ($type == 'jpg') {
  7663. $type = 'jpeg';
  7664. }
  7665. $mqr = $this->get_mqr();
  7666. $this->set_mqr(false);
  7667. // Specific image handlers
  7668. $mtd = '_parse'.$type;
  7669. // GD image handler function
  7670. $gdfunction = 'imagecreatefrom'.$type;
  7671. $info = false;
  7672. if ((method_exists($this, $mtd)) AND (!($resize AND (function_exists($gdfunction) OR extension_loaded('imagick'))))) {
  7673. // TCPDF image functions
  7674. $info = $this->$mtd($file);
  7675. if ($info == 'pngalpha') {
  7676. return $this->ImagePngAlpha($file, $x, $y, $pixw, $pixh, $w, $h, 'PNG', $link, $align, $resize, $dpi, $palign, $filehash);
  7677. }
  7678. }
  7679. if (!$info) {
  7680. if (function_exists($gdfunction)) {
  7681. // GD library
  7682. $img = $gdfunction($file);
  7683. if ($resize) {
  7684. $imgr = imagecreatetruecolor($neww, $newh);
  7685. if (($type == 'gif') OR ($type == 'png')) {
  7686. $imgr = $this->_setGDImageTransparency($imgr, $img);
  7687. }
  7688. imagecopyresampled($imgr, $img, 0, 0, 0, 0, $neww, $newh, $pixw, $pixh);
  7689. if (($type == 'gif') OR ($type == 'png')) {
  7690. $info = $this->_toPNG($imgr);
  7691. } else {
  7692. $info = $this->_toJPEG($imgr);
  7693. }
  7694. } else {
  7695. if (($type == 'gif') OR ($type == 'png')) {
  7696. $info = $this->_toPNG($img);
  7697. } else {
  7698. $info = $this->_toJPEG($img);
  7699. }
  7700. }
  7701. } elseif (extension_loaded('imagick')) {
  7702. // ImageMagick library
  7703. $img = new Imagick();
  7704. if ($type == 'SVG') {
  7705. // get SVG file content
  7706. $svgimg = file_get_contents($file);
  7707. // get width and height
  7708. $regs = array();
  7709. if (preg_match('/<svg([^\>]*)>/si', $svgimg, $regs)) {
  7710. $svgtag = $regs[1];
  7711. $tmp = array();
  7712. if (preg_match('/[\s]+width[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7713. $ow = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7714. $owu = sprintf('%F', ($ow * $dpi / 72)).$this->pdfunit;
  7715. $svgtag = preg_replace('/[\s]+width[\s]*=[\s]*"[^"]*"/si', ' width="'.$owu.'"', $svgtag, 1);
  7716. } else {
  7717. $ow = $w;
  7718. }
  7719. $tmp = array();
  7720. if (preg_match('/[\s]+height[\s]*=[\s]*"([^"]*)"/si', $svgtag, $tmp)) {
  7721. $oh = $this->getHTMLUnitToUnits($tmp[1], 1, $this->svgunit, false);
  7722. $ohu = sprintf('%F', ($oh * $dpi / 72)).$this->pdfunit;
  7723. $svgtag = preg_replace('/[\s]+height[\s]*=[\s]*"[^"]*"/si', ' height="'.$ohu.'"', $svgtag, 1);
  7724. } else {
  7725. $oh = $h;
  7726. }
  7727. $tmp = array();
  7728. if (!preg_match('/[\s]+viewBox[\s]*=[\s]*"[\s]*([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]+([0-9\.]+)[\s]*"/si', $svgtag, $tmp)) {
  7729. $vbw = ($ow * $this->imgscale * $this->k);
  7730. $vbh = ($oh * $this->imgscale * $this->k);
  7731. $vbox = sprintf(' viewBox="0 0 %F %F" ', $vbw, $vbh);
  7732. $svgtag = $vbox.$svgtag;
  7733. }
  7734. $svgimg = preg_replace('/<svg([^\>]*)>/si', '<svg'.$svgtag.'>', $svgimg, 1);
  7735. }
  7736. $img->readImageBlob($svgimg);
  7737. } else {
  7738. $img->readImage($file);
  7739. }
  7740. if ($resize) {
  7741. $img->resizeImage($neww, $newh, 10, 1, false);
  7742. }
  7743. $img->setCompressionQuality($this->jpeg_quality);
  7744. $img->setImageFormat('jpeg');
  7745. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7746. $img->writeImage($tempname);
  7747. $info = $this->_parsejpeg($tempname);
  7748. unlink($tempname);
  7749. $img->destroy();
  7750. } else {
  7751. return;
  7752. }
  7753. }
  7754. if ($info === false) {
  7755. //If false, we cannot process image
  7756. return;
  7757. }
  7758. $this->set_mqr($mqr);
  7759. if ($ismask) {
  7760. // force grayscale
  7761. $info['cs'] = 'DeviceGray';
  7762. }
  7763. $info['i'] = $this->numimages;
  7764. if (!in_array($file, $this->imagekeys)) {
  7765. ++$info['i'];
  7766. }
  7767. if ($imgmask !== false) {
  7768. $info['masked'] = $imgmask;
  7769. }
  7770. if (!empty($exurl)) {
  7771. $info['exurl'] = $exurl;
  7772. }
  7773. // array of alternative images
  7774. $info['altimgs'] = $altimgs;
  7775. // add image to document
  7776. $this->setImageBuffer($file, $info);
  7777. }
  7778. if ($cached_file) {
  7779. // remove cached file
  7780. unlink($file);
  7781. }
  7782. // set alignment
  7783. $this->img_rb_y = $y + $h;
  7784. // set alignment
  7785. if ($this->rtl) {
  7786. if ($palign == 'L') {
  7787. $ximg = $this->lMargin;
  7788. } elseif ($palign == 'C') {
  7789. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7790. } elseif ($palign == 'R') {
  7791. $ximg = $this->w - $this->rMargin - $w;
  7792. } else {
  7793. $ximg = $x - $w;
  7794. }
  7795. $this->img_rb_x = $ximg;
  7796. } else {
  7797. if ($palign == 'L') {
  7798. $ximg = $this->lMargin;
  7799. } elseif ($palign == 'C') {
  7800. $ximg = ($this->w + $this->lMargin - $this->rMargin - $w) / 2;
  7801. } elseif ($palign == 'R') {
  7802. $ximg = $this->w - $this->rMargin - $w;
  7803. } else {
  7804. $ximg = $x;
  7805. }
  7806. $this->img_rb_x = $ximg + $w;
  7807. }
  7808. if ($ismask OR $hidden) {
  7809. // image is not displayed
  7810. return $info['i'];
  7811. }
  7812. $xkimg = $ximg * $this->k;
  7813. if (!$alt) {
  7814. // only non-alternative immages will be set
  7815. $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']));
  7816. }
  7817. if (!empty($border)) {
  7818. $bx = $this->x;
  7819. $by = $this->y;
  7820. $this->x = $ximg;
  7821. if ($this->rtl) {
  7822. $this->x += $w;
  7823. }
  7824. $this->y = $y;
  7825. $this->Cell($w, $h, '', $border, 0, '', 0, '', 0, true);
  7826. $this->x = $bx;
  7827. $this->y = $by;
  7828. }
  7829. if ($link) {
  7830. $this->Link($ximg, $y, $w, $h, $link, 0);
  7831. }
  7832. // set pointer to align the next text/objects
  7833. switch($align) {
  7834. case 'T': {
  7835. $this->y = $y;
  7836. $this->x = $this->img_rb_x;
  7837. break;
  7838. }
  7839. case 'M': {
  7840. $this->y = $y + round($h/2);
  7841. $this->x = $this->img_rb_x;
  7842. break;
  7843. }
  7844. case 'B': {
  7845. $this->y = $this->img_rb_y;
  7846. $this->x = $this->img_rb_x;
  7847. break;
  7848. }
  7849. case 'N': {
  7850. $this->SetY($this->img_rb_y);
  7851. break;
  7852. }
  7853. default:{
  7854. break;
  7855. }
  7856. }
  7857. $this->endlinex = $this->img_rb_x;
  7858. if ($this->inxobj) {
  7859. // we are inside an XObject template
  7860. $this->xobjects[$this->xobjid]['images'][] = $info['i'];
  7861. }
  7862. return $info['i'];
  7863. }
  7864. /**
  7865. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  7866. * @param $mqr (boolean) FALSE for off, TRUE for on.
  7867. * @since 4.6.025 (2009-08-17)
  7868. */
  7869. public function set_mqr($mqr) {
  7870. if (!defined('PHP_VERSION_ID')) {
  7871. $version = PHP_VERSION;
  7872. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7873. }
  7874. if (PHP_VERSION_ID < 50300) {
  7875. @set_magic_quotes_runtime($mqr);
  7876. }
  7877. }
  7878. /**
  7879. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  7880. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  7881. * @since 4.6.025 (2009-08-17)
  7882. */
  7883. public function get_mqr() {
  7884. if (!defined('PHP_VERSION_ID')) {
  7885. $version = PHP_VERSION;
  7886. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  7887. }
  7888. if (PHP_VERSION_ID < 50300) {
  7889. return @get_magic_quotes_runtime();
  7890. }
  7891. return 0;
  7892. }
  7893. /**
  7894. * Convert the loaded image to a JPEG and then return a structure for the PDF creator.
  7895. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7896. * @param $image (image) Image object.
  7897. * return image JPEG image object.
  7898. * @protected
  7899. */
  7900. protected function _toJPEG($image) {
  7901. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7902. imagejpeg($image, $tempname, $this->jpeg_quality);
  7903. imagedestroy($image);
  7904. $retvars = $this->_parsejpeg($tempname);
  7905. // tidy up by removing temporary image
  7906. unlink($tempname);
  7907. return $retvars;
  7908. }
  7909. /**
  7910. * Convert the loaded image to a PNG and then return a structure for the PDF creator.
  7911. * This function requires GD library and write access to the directory defined on K_PATH_CACHE constant.
  7912. * @param $image (image) Image object.
  7913. * return image PNG image object.
  7914. * @protected
  7915. * @since 4.9.016 (2010-04-20)
  7916. */
  7917. protected function _toPNG($image) {
  7918. // set temporary image file name
  7919. $tempname = tempnam(K_PATH_CACHE, 'jpg_');
  7920. // turn off interlaced mode
  7921. imageinterlace($image, 0);
  7922. // create temporary PNG image
  7923. imagepng($image, $tempname);
  7924. // remove image from memory
  7925. imagedestroy($image);
  7926. // get PNG image data
  7927. $retvars = $this->_parsepng($tempname);
  7928. // tidy up by removing temporary image
  7929. unlink($tempname);
  7930. return $retvars;
  7931. }
  7932. /**
  7933. * Set the transparency for the given GD image.
  7934. * @param $new_image (image) GD image object
  7935. * @param $image (image) GD image object.
  7936. * return GD image object.
  7937. * @protected
  7938. * @since 4.9.016 (2010-04-20)
  7939. */
  7940. protected function _setGDImageTransparency($new_image, $image) {
  7941. // transparency index
  7942. $tid = imagecolortransparent($image);
  7943. // default transparency color
  7944. $tcol = array('red' => 255, 'green' => 255, 'blue' => 255);
  7945. if ($tid >= 0) {
  7946. // get the colors for the transparency index
  7947. $tcol = imagecolorsforindex($image, $tid);
  7948. }
  7949. $tid = imagecolorallocate($new_image, $tcol['red'], $tcol['green'], $tcol['blue']);
  7950. imagefill($new_image, 0, 0, $tid);
  7951. imagecolortransparent($new_image, $tid);
  7952. return $new_image;
  7953. }
  7954. /**
  7955. * Extract info from a JPEG file without using the GD library.
  7956. * @param $file (string) image file to parse
  7957. * @return array structure containing the image data
  7958. * @protected
  7959. */
  7960. protected function _parsejpeg($file) {
  7961. $a = getimagesize($file);
  7962. if (empty($a)) {
  7963. $this->Error('Missing or incorrect image file: '.$file);
  7964. }
  7965. if ($a[2] != 2) {
  7966. $this->Error('Not a JPEG file: '.$file);
  7967. }
  7968. // bits per pixel
  7969. $bpc = isset($a['bits']) ? intval($a['bits']) : 8;
  7970. // number of image channels
  7971. if (!isset($a['channels'])) {
  7972. $channels = 3;
  7973. } else {
  7974. $channels = intval($a['channels']);
  7975. }
  7976. // default colour space
  7977. switch ($channels) {
  7978. case 1: {
  7979. $colspace = 'DeviceGray';
  7980. break;
  7981. }
  7982. case 3: {
  7983. $colspace = 'DeviceRGB';
  7984. break;
  7985. }
  7986. case 4: {
  7987. $colspace = 'DeviceCMYK';
  7988. break;
  7989. }
  7990. default: {
  7991. $channels = 3;
  7992. $colspace = 'DeviceRGB';
  7993. break;
  7994. }
  7995. }
  7996. // get file content
  7997. $data = file_get_contents($file);
  7998. // check for embedded ICC profile
  7999. $icc = array();
  8000. $offset = 0;
  8001. while (($pos = strpos($data, "ICC_PROFILE\0", $offset)) !== false) {
  8002. // get ICC sequence length
  8003. $length = ($this->_getUSHORT($data, ($pos - 2)) - 16);
  8004. // marker sequence number
  8005. $msn = max(1, ord($data[($pos + 12)]));
  8006. // number of markers (total of APP2 used)
  8007. $nom = max(1, ord($data[($pos + 13)]));
  8008. // get sequence segment
  8009. $icc[($msn - 1)] = substr($data, ($pos + 14), $length);
  8010. // move forward to next sequence
  8011. $offset = ($pos + 14 + $length);
  8012. }
  8013. // order and compact ICC segments
  8014. if (count($icc) > 0) {
  8015. ksort($icc);
  8016. $icc = implode('', $icc);
  8017. if ((ord($icc{36}) != 0x61) OR (ord($icc{37}) != 0x63) OR (ord($icc{38}) != 0x73) OR (ord($icc{39}) != 0x70)) {
  8018. // invalid ICC profile
  8019. $icc = false;
  8020. }
  8021. } else {
  8022. $icc = false;
  8023. }
  8024. return array('w' => $a[0], 'h' => $a[1], 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'DCTDecode', 'data' => $data);
  8025. }
  8026. /**
  8027. * Extract info from a PNG file without using the GD library.
  8028. * @param $file (string) image file to parse
  8029. * @return array structure containing the image data
  8030. * @protected
  8031. */
  8032. protected function _parsepng($file) {
  8033. $f = fopen($file, 'rb');
  8034. if ($f === false) {
  8035. $this->Error('Can\'t open image file: '.$file);
  8036. }
  8037. //Check signature
  8038. if (fread($f, 8) != chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10)) {
  8039. $this->Error('Not a PNG file: '.$file);
  8040. }
  8041. //Read header chunk
  8042. fread($f, 4);
  8043. if (fread($f, 4) != 'IHDR') {
  8044. $this->Error('Incorrect PNG file: '.$file);
  8045. }
  8046. $w = $this->_freadint($f);
  8047. $h = $this->_freadint($f);
  8048. $bpc = ord(fread($f, 1));
  8049. if ($bpc > 8) {
  8050. //$this->Error('16-bit depth not supported: '.$file);
  8051. fclose($f);
  8052. return false;
  8053. }
  8054. $ct = ord(fread($f, 1));
  8055. if ($ct == 0) {
  8056. $colspace = 'DeviceGray';
  8057. } elseif ($ct == 2) {
  8058. $colspace = 'DeviceRGB';
  8059. } elseif ($ct == 3) {
  8060. $colspace = 'Indexed';
  8061. } else {
  8062. // alpha channel
  8063. fclose($f);
  8064. return 'pngalpha';
  8065. }
  8066. if (ord(fread($f, 1)) != 0) {
  8067. //$this->Error('Unknown compression method: '.$file);
  8068. fclose($f);
  8069. return false;
  8070. }
  8071. if (ord(fread($f, 1)) != 0) {
  8072. //$this->Error('Unknown filter method: '.$file);
  8073. fclose($f);
  8074. return false;
  8075. }
  8076. if (ord(fread($f, 1)) != 0) {
  8077. //$this->Error('Interlacing not supported: '.$file);
  8078. fclose($f);
  8079. return false;
  8080. }
  8081. fread($f, 4);
  8082. $channels = ($ct == 2 ? 3 : 1);
  8083. $parms = '/DecodeParms << /Predictor 15 /Colors '.$channels.' /BitsPerComponent '.$bpc.' /Columns '.$w.' >>';
  8084. //Scan chunks looking for palette, transparency and image data
  8085. $pal = '';
  8086. $trns = '';
  8087. $data = '';
  8088. $icc = false;
  8089. do {
  8090. $n = $this->_freadint($f);
  8091. $type = fread($f, 4);
  8092. if ($type == 'PLTE') {
  8093. // read palette
  8094. $pal = $this->rfread($f, $n);
  8095. fread($f, 4);
  8096. } elseif ($type == 'tRNS') {
  8097. // read transparency info
  8098. $t = $this->rfread($f, $n);
  8099. if ($ct == 0) {
  8100. $trns = array(ord($t{1}));
  8101. } elseif ($ct == 2) {
  8102. $trns = array(ord($t{1}), ord($t{3}), ord($t{5}));
  8103. } else {
  8104. $pos = strpos($t, chr(0));
  8105. if ($pos !== false) {
  8106. $trns = array($pos);
  8107. }
  8108. }
  8109. fread($f, 4);
  8110. } elseif ($type == 'IDAT') {
  8111. // read image data block
  8112. $data .= $this->rfread($f, $n);
  8113. fread($f, 4);
  8114. } elseif ($type == 'iCCP') {
  8115. // skip profile name
  8116. $len = 0;
  8117. while ((ord(fread($f, 1)) > 0) AND ($len < 80)) {
  8118. ++$len;
  8119. }
  8120. // skip null separator
  8121. fread($f, 1);
  8122. // get compression method
  8123. if (ord(fread($f, 1)) != 0) {
  8124. //$this->Error('Unknown filter method: '.$file);
  8125. fclose($f);
  8126. return false;
  8127. }
  8128. // read ICC Color Profile
  8129. $icc = $this->rfread($f, ($n - $len - 2));
  8130. // decompress profile
  8131. $icc = gzuncompress($icc);
  8132. fread($f, 4);
  8133. } elseif ($type == 'IEND') {
  8134. break;
  8135. } else {
  8136. $this->rfread($f, $n + 4);
  8137. }
  8138. } while ($n);
  8139. if (($colspace == 'Indexed') AND (empty($pal))) {
  8140. //$this->Error('Missing palette in '.$file);
  8141. fclose($f);
  8142. return false;
  8143. }
  8144. fclose($f);
  8145. return array('w' => $w, 'h' => $h, 'ch' => $channels, 'icc' => $icc, 'cs' => $colspace, 'bpc' => $bpc, 'f' => 'FlateDecode', 'parms' => $parms, 'pal' => $pal, 'trns' => $trns, 'data' => $data);
  8146. }
  8147. /**
  8148. * Binary-safe and URL-safe file read.
  8149. * 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.
  8150. * @param $handle (resource)
  8151. * @param $length (int)
  8152. * @return Returns the read string or FALSE in case of error.
  8153. * @author Nicola Asuni
  8154. * @protected
  8155. * @since 4.5.027 (2009-03-16)
  8156. */
  8157. protected function rfread($handle, $length) {
  8158. $data = fread($handle, $length);
  8159. if ($data === false) {
  8160. return false;
  8161. }
  8162. $rest = $length - strlen($data);
  8163. if ($rest > 0) {
  8164. $data .= $this->rfread($handle, $rest);
  8165. }
  8166. return $data;
  8167. }
  8168. /**
  8169. * Extract info from a PNG image with alpha channel using the GD library.
  8170. * @param $file (string) Name of the file containing the image.
  8171. * @param $x (float) Abscissa of the upper-left corner.
  8172. * @param $y (float) Ordinate of the upper-left corner.
  8173. * @param $wpx (float) Original width of the image in pixels.
  8174. * @param $hpx (float) original height of the image in pixels.
  8175. * @param $w (float) Width of the image in the page. If not specified or equal to zero, it is automatically calculated.
  8176. * @param $h (float) Height of the image in the page. If not specified or equal to zero, it is automatically calculated.
  8177. * @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.
  8178. * @param $link (mixed) URL or identifier returned by AddLink().
  8179. * @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>
  8180. * @param $resize (boolean) If true resize (reduce) the image to fit $w and $h (requires GD library).
  8181. * @param $dpi (int) dot-per-inch resolution used on resize
  8182. * @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>
  8183. * @param $filehash (string) File hash used to build unique file names.
  8184. * @author Nicola Asuni
  8185. * @protected
  8186. * @since 4.3.007 (2008-12-04)
  8187. * @see Image()
  8188. */
  8189. protected function ImagePngAlpha($file, $x, $y, $wpx, $hpx, $w, $h, $type, $link, $align, $resize, $dpi, $palign, $filehash='') {
  8190. if (empty($filehash)) {
  8191. $filehash = md5($this->file_id.$file);
  8192. }
  8193. // create temp image file (without alpha channel)
  8194. $tempfile_plain = K_PATH_CACHE.'mskp_'.$filehash;
  8195. // create temp alpha file
  8196. $tempfile_alpha = K_PATH_CACHE.'mska_'.$filehash;
  8197. if (extension_loaded('imagick')) { // ImageMagick extension
  8198. // ImageMagick library
  8199. $img = new Imagick();
  8200. $img->readImage($file);
  8201. // clone image object
  8202. $imga = $this->objclone($img);
  8203. // extract alpha channel
  8204. $img->separateImageChannel(8); // 8 = (imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE);
  8205. $img->negateImage(true);
  8206. $img->setImageFormat('png');
  8207. $img->writeImage($tempfile_alpha);
  8208. // remove alpha channel
  8209. $imga->separateImageChannel(39); // 39 = (imagick::CHANNEL_ALL & ~(imagick::CHANNEL_ALPHA | imagick::CHANNEL_OPACITY | imagick::CHANNEL_MATTE));
  8210. $imga->setImageFormat('png');
  8211. $imga->writeImage($tempfile_plain);
  8212. } elseif (function_exists('imagecreatefrompng')) { // GD extension
  8213. // generate images
  8214. $img = imagecreatefrompng($file);
  8215. $imgalpha = imagecreate($wpx, $hpx);
  8216. // generate gray scale palette (0 -> 255)
  8217. for ($c = 0; $c < 256; ++$c) {
  8218. ImageColorAllocate($imgalpha, $c, $c, $c);
  8219. }
  8220. // extract alpha channel
  8221. for ($xpx = 0; $xpx < $wpx; ++$xpx) {
  8222. for ($ypx = 0; $ypx < $hpx; ++$ypx) {
  8223. $color = imagecolorat($img, $xpx, $ypx);
  8224. $alpha = $this->getGDgamma($color); // correct gamma
  8225. imagesetpixel($imgalpha, $xpx, $ypx, $alpha);
  8226. }
  8227. }
  8228. imagepng($imgalpha, $tempfile_alpha);
  8229. imagedestroy($imgalpha);
  8230. // extract image without alpha channel
  8231. $imgplain = imagecreatetruecolor($wpx, $hpx);
  8232. imagecopy($imgplain, $img, 0, 0, 0, 0, $wpx, $hpx);
  8233. imagepng($imgplain, $tempfile_plain);
  8234. imagedestroy($imgplain);
  8235. } else {
  8236. $this->Error('TCPDF requires the Imagick or GD extension to handle PNG images with alpha channel.');
  8237. }
  8238. // embed mask image
  8239. $imgmask = $this->Image($tempfile_alpha, $x, $y, $w, $h, 'PNG', '', '', $resize, $dpi, '', true, false);
  8240. // embed image, masked with previously embedded mask
  8241. $this->Image($tempfile_plain, $x, $y, $w, $h, $type, $link, $align, $resize, $dpi, $palign, false, $imgmask);
  8242. // remove temp files
  8243. unlink($tempfile_alpha);
  8244. unlink($tempfile_plain);
  8245. }
  8246. /**
  8247. * Get the GD-corrected PNG gamma value from alpha color
  8248. * @param $c (int) alpha color
  8249. * @protected
  8250. * @since 4.3.007 (2008-12-04)
  8251. */
  8252. protected function getGDgamma($c) {
  8253. if (!isset($this->gdgammacache["'".$c."'"])) {
  8254. // shifts off the first 24 bits (where 8x3 are used for each color),
  8255. // and returns the remaining 7 allocated bits (commonly used for alpha)
  8256. $alpha = ($c >> 24);
  8257. // GD alpha is only 7 bit (0 -> 127)
  8258. $alpha = (((127 - $alpha) / 127) * 255);
  8259. // correct gamma
  8260. $this->gdgammacache["'".$c."'"] = (pow(($alpha / 255), 2.2) * 255);
  8261. // store the latest values on cache to improve performances
  8262. if (count($this->gdgammacache) > 8) {
  8263. // remove one element from the cache array
  8264. array_shift($this->gdgammacache);
  8265. }
  8266. }
  8267. return $this->gdgammacache["'".$c."'"];
  8268. }
  8269. /**
  8270. * Performs a line break.
  8271. * The current abscissa goes back to the left margin and the ordinate increases by the amount passed in parameter.
  8272. * @param $h (float) The height of the break. By default, the value equals the height of the last printed cell.
  8273. * @param $cell (boolean) if true add the current left (or right o for RTL) padding to the X coordinate
  8274. * @public
  8275. * @since 1.0
  8276. * @see Cell()
  8277. */
  8278. public function Ln($h='', $cell=false) {
  8279. 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'])) {
  8280. // revove vertical space from the top of the column
  8281. return;
  8282. }
  8283. if ($cell) {
  8284. if ($this->rtl) {
  8285. $cellpadding = $this->cell_padding['R'];
  8286. } else {
  8287. $cellpadding = $this->cell_padding['L'];
  8288. }
  8289. } else {
  8290. $cellpadding = 0;
  8291. }
  8292. if ($this->rtl) {
  8293. $this->x = $this->w - $this->rMargin - $cellpadding;
  8294. } else {
  8295. $this->x = $this->lMargin + $cellpadding;
  8296. }
  8297. if (is_string($h)) {
  8298. $this->y += $this->lasth;
  8299. } else {
  8300. $this->y += $h;
  8301. }
  8302. $this->newline = true;
  8303. }
  8304. /**
  8305. * Returns the relative X value of current position.
  8306. * The value is relative to the left border for LTR languages and to the right border for RTL languages.
  8307. * @return float
  8308. * @public
  8309. * @since 1.2
  8310. * @see SetX(), GetY(), SetY()
  8311. */
  8312. public function GetX() {
  8313. //Get x position
  8314. if ($this->rtl) {
  8315. return ($this->w - $this->x);
  8316. } else {
  8317. return $this->x;
  8318. }
  8319. }
  8320. /**
  8321. * Returns the absolute X value of current position.
  8322. * @return float
  8323. * @public
  8324. * @since 1.2
  8325. * @see SetX(), GetY(), SetY()
  8326. */
  8327. public function GetAbsX() {
  8328. return $this->x;
  8329. }
  8330. /**
  8331. * Returns the ordinate of the current position.
  8332. * @return float
  8333. * @public
  8334. * @since 1.0
  8335. * @see SetY(), GetX(), SetX()
  8336. */
  8337. public function GetY() {
  8338. return $this->y;
  8339. }
  8340. /**
  8341. * Defines the abscissa of the current position.
  8342. * If the passed value is negative, it is relative to the right of the page (or left if language is RTL).
  8343. * @param $x (float) The value of the abscissa.
  8344. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8345. * @public
  8346. * @since 1.2
  8347. * @see GetX(), GetY(), SetY(), SetXY()
  8348. */
  8349. public function SetX($x, $rtloff=false) {
  8350. if (!$rtloff AND $this->rtl) {
  8351. if ($x >= 0) {
  8352. $this->x = $this->w - $x;
  8353. } else {
  8354. $this->x = abs($x);
  8355. }
  8356. } else {
  8357. if ($x >= 0) {
  8358. $this->x = $x;
  8359. } else {
  8360. $this->x = $this->w + $x;
  8361. }
  8362. }
  8363. if ($this->x < 0) {
  8364. $this->x = 0;
  8365. }
  8366. if ($this->x > $this->w) {
  8367. $this->x = $this->w;
  8368. }
  8369. }
  8370. /**
  8371. * Moves the current abscissa back to the left margin and sets the ordinate.
  8372. * If the passed value is negative, it is relative to the bottom of the page.
  8373. * @param $y (float) The value of the ordinate.
  8374. * @param $resetx (bool) if true (default) reset the X position.
  8375. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8376. * @public
  8377. * @since 1.0
  8378. * @see GetX(), GetY(), SetY(), SetXY()
  8379. */
  8380. public function SetY($y, $resetx=true, $rtloff=false) {
  8381. if ($resetx) {
  8382. //reset x
  8383. if (!$rtloff AND $this->rtl) {
  8384. $this->x = $this->w - $this->rMargin;
  8385. } else {
  8386. $this->x = $this->lMargin;
  8387. }
  8388. }
  8389. if ($y >= 0) {
  8390. $this->y = $y;
  8391. } else {
  8392. $this->y = $this->h + $y;
  8393. }
  8394. if ($this->y < 0) {
  8395. $this->y = 0;
  8396. }
  8397. if ($this->y > $this->h) {
  8398. $this->y = $this->h;
  8399. }
  8400. }
  8401. /**
  8402. * Defines the abscissa and ordinate of the current position.
  8403. * If the passed values are negative, they are relative respectively to the right and bottom of the page.
  8404. * @param $x (float) The value of the abscissa.
  8405. * @param $y (float) The value of the ordinate.
  8406. * @param $rtloff (boolean) if true always uses the page top-left corner as origin of axis.
  8407. * @public
  8408. * @since 1.2
  8409. * @see SetX(), SetY()
  8410. */
  8411. public function SetXY($x, $y, $rtloff=false) {
  8412. $this->SetY($y, false, $rtloff);
  8413. $this->SetX($x, $rtloff);
  8414. }
  8415. /**
  8416. * Ouput input data and compress it if possible.
  8417. * @param $data (string) Data to output.
  8418. * @param $length (int) Data length in bytes.
  8419. * @protected
  8420. * @since 5.9.086
  8421. */
  8422. protected function sendOutputData($data, $length) {
  8423. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  8424. // the content length may vary if the server is using compression
  8425. header('Content-Length: '.$length);
  8426. }
  8427. echo $data;
  8428. }
  8429. /**
  8430. * Send the document to a given destination: string, local file or browser.
  8431. * In the last case, the plug-in may be used (if present) or a download ("Save as" dialog box) may be forced.<br />
  8432. * The method first calls Close() if necessary to terminate the document.
  8433. * @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.
  8434. * @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>
  8435. * @public
  8436. * @since 1.0
  8437. * @see Close()
  8438. */
  8439. public function Output($name='doc.pdf', $dest='I') {
  8440. //Output PDF to some destination
  8441. //Finish document if necessary
  8442. if ($this->state < 3) {
  8443. $this->Close();
  8444. }
  8445. //Normalize parameters
  8446. if (is_bool($dest)) {
  8447. $dest = $dest ? 'D' : 'F';
  8448. }
  8449. $dest = strtoupper($dest);
  8450. if ($dest{0} != 'F') {
  8451. $name = preg_replace('/[\s]+/', '_', $name);
  8452. $name = preg_replace('/[^a-zA-Z0-9_\.-]/', '', $name);
  8453. }
  8454. if ($this->sign) {
  8455. // *** apply digital signature to the document ***
  8456. // get the document content
  8457. $pdfdoc = $this->getBuffer();
  8458. // remove last newline
  8459. $pdfdoc = substr($pdfdoc, 0, -1);
  8460. // Remove the original buffer
  8461. if (isset($this->diskcache) AND $this->diskcache) {
  8462. // remove buffer file from cache
  8463. unlink($this->buffer);
  8464. }
  8465. unset($this->buffer);
  8466. // remove filler space
  8467. $byterange_string_len = strlen($this->byterange_string);
  8468. // define the ByteRange
  8469. $byte_range = array();
  8470. $byte_range[0] = 0;
  8471. $byte_range[1] = strpos($pdfdoc, $this->byterange_string) + $byterange_string_len + 10;
  8472. $byte_range[2] = $byte_range[1] + $this->signature_max_length + 2;
  8473. $byte_range[3] = strlen($pdfdoc) - $byte_range[2];
  8474. $pdfdoc = substr($pdfdoc, 0, $byte_range[1]).substr($pdfdoc, $byte_range[2]);
  8475. // replace the ByteRange
  8476. $byterange = sprintf('/ByteRange[0 %u %u %u]', $byte_range[1], $byte_range[2], $byte_range[3]);
  8477. $byterange .= str_repeat(' ', ($byterange_string_len - strlen($byterange)));
  8478. $pdfdoc = str_replace($this->byterange_string, $byterange, $pdfdoc);
  8479. // write the document to a temporary folder
  8480. $tempdoc = tempnam(K_PATH_CACHE, 'tmppdf_');
  8481. $f = fopen($tempdoc, 'wb');
  8482. if (!$f) {
  8483. $this->Error('Unable to create temporary file: '.$tempdoc);
  8484. }
  8485. $pdfdoc_length = strlen($pdfdoc);
  8486. fwrite($f, $pdfdoc, $pdfdoc_length);
  8487. fclose($f);
  8488. // get digital signature via openssl library
  8489. $tempsign = tempnam(K_PATH_CACHE, 'tmpsig_');
  8490. if (empty($this->signature_data['extracerts'])) {
  8491. openssl_pkcs7_sign($tempdoc, $tempsign, $this->signature_data['signcert'], array($this->signature_data['privkey'], $this->signature_data['password']), array(), PKCS7_BINARY | PKCS7_DETACHED);
  8492. } else {
  8493. 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']);
  8494. }
  8495. unlink($tempdoc);
  8496. // read signature
  8497. $signature = file_get_contents($tempsign);
  8498. unlink($tempsign);
  8499. // extract signature
  8500. $signature = substr($signature, $pdfdoc_length);
  8501. $signature = substr($signature, (strpos($signature, "%%EOF\n\n------") + 13));
  8502. $tmparr = explode("\n\n", $signature);
  8503. $signature = $tmparr[1];
  8504. unset($tmparr);
  8505. // decode signature
  8506. $signature = base64_decode(trim($signature));
  8507. // convert signature to hex
  8508. $signature = current(unpack('H*', $signature));
  8509. $signature = str_pad($signature, $this->signature_max_length, '0');
  8510. // disable disk caching
  8511. $this->diskcache = false;
  8512. // Add signature to the document
  8513. $this->buffer = substr($pdfdoc, 0, $byte_range[1]).'<'.$signature.'>'.substr($pdfdoc, $byte_range[1]);
  8514. $this->bufferlen = strlen($this->buffer);
  8515. }
  8516. switch($dest) {
  8517. case 'I': {
  8518. // Send PDF to the standard output
  8519. if (ob_get_contents()) {
  8520. $this->Error('Some data has already been output, can\'t send PDF file');
  8521. }
  8522. if (php_sapi_name() != 'cli') {
  8523. // send output to a browser
  8524. header('Content-Type: application/pdf');
  8525. if (headers_sent()) {
  8526. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8527. }
  8528. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8529. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8530. header('Pragma: public');
  8531. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8532. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8533. header('Content-Disposition: inline; filename="'.basename($name).'"');
  8534. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8535. } else {
  8536. echo $this->getBuffer();
  8537. }
  8538. break;
  8539. }
  8540. case 'D': {
  8541. // download PDF as file
  8542. if (ob_get_contents()) {
  8543. $this->Error('Some data has already been output, can\'t send PDF file');
  8544. }
  8545. header('Content-Description: File Transfer');
  8546. if (headers_sent()) {
  8547. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8548. }
  8549. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8550. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8551. header('Pragma: public');
  8552. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8553. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8554. // force download dialog
  8555. if (strpos(php_sapi_name(), 'cgi') === false) {
  8556. header('Content-Type: application/force-download');
  8557. header('Content-Type: application/octet-stream', false);
  8558. header('Content-Type: application/download', false);
  8559. header('Content-Type: application/pdf', false);
  8560. } else {
  8561. header('Content-Type: application/pdf');
  8562. }
  8563. // use the Content-Disposition header to supply a recommended filename
  8564. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  8565. header('Content-Transfer-Encoding: binary');
  8566. $this->sendOutputData($this->getBuffer(), $this->bufferlen);
  8567. break;
  8568. }
  8569. case 'F':
  8570. case 'FI':
  8571. case 'FD': {
  8572. // save PDF to a local file
  8573. if ($this->diskcache) {
  8574. copy($this->buffer, $name);
  8575. } else {
  8576. $f = fopen($name, 'wb');
  8577. if (!$f) {
  8578. $this->Error('Unable to create output file: '.$name);
  8579. }
  8580. fwrite($f, $this->getBuffer(), $this->bufferlen);
  8581. fclose($f);
  8582. }
  8583. if ($dest == 'FI') {
  8584. // send headers to browser
  8585. header('Content-Type: application/pdf');
  8586. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8587. //header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
  8588. header('Pragma: public');
  8589. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8590. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8591. header('Content-Disposition: inline; filename="'.basename($name).'"');
  8592. $this->sendOutputData(file_get_contents($name), filesize($name));
  8593. } elseif ($dest == 'FD') {
  8594. // send headers to browser
  8595. if (ob_get_contents()) {
  8596. $this->Error('Some data has already been output, can\'t send PDF file');
  8597. }
  8598. header('Content-Description: File Transfer');
  8599. if (headers_sent()) {
  8600. $this->Error('Some data has already been output to browser, can\'t send PDF file');
  8601. }
  8602. header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
  8603. header('Pragma: public');
  8604. header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  8605. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  8606. // force download dialog
  8607. if (strpos(php_sapi_name(), 'cgi') === false) {
  8608. header('Content-Type: application/force-download');
  8609. header('Content-Type: application/octet-stream', false);
  8610. header('Content-Type: application/download', false);
  8611. header('Content-Type: application/pdf', false);
  8612. } else {
  8613. header('Content-Type: application/pdf');
  8614. }
  8615. // use the Content-Disposition header to supply a recommended filename
  8616. header('Content-Disposition: attachment; filename="'.basename($name).'"');
  8617. header('Content-Transfer-Encoding: binary');
  8618. $this->sendOutputData(file_get_contents($name), filesize($name));
  8619. }
  8620. break;
  8621. }
  8622. case 'E': {
  8623. // return PDF as base64 mime multi-part email attachment (RFC 2045)
  8624. $retval = 'Content-Type: application/pdf;'."\r\n";
  8625. $retval .= ' name="'.$name.'"'."\r\n";
  8626. $retval .= 'Content-Transfer-Encoding: base64'."\r\n";
  8627. $retval .= 'Content-Disposition: attachment;'."\r\n";
  8628. $retval .= ' filename="'.$name.'"'."\r\n\r\n";
  8629. $retval .= chunk_split(base64_encode($this->getBuffer()), 76, "\r\n");
  8630. return $retval;
  8631. }
  8632. case 'S': {
  8633. // returns PDF as a string
  8634. return $this->getBuffer();
  8635. }
  8636. default: {
  8637. $this->Error('Incorrect output destination: '.$dest);
  8638. }
  8639. }
  8640. return '';
  8641. }
  8642. /**
  8643. * Unset all class variables except the following critical variables: internal_encoding, state, bufferlen, buffer and diskcache.
  8644. * @param $destroyall (boolean) if true destroys all class variables, otherwise preserves critical variables.
  8645. * @param $preserve_objcopy (boolean) if true preserves the objcopy variable
  8646. * @public
  8647. * @since 4.5.016 (2009-02-24)
  8648. */
  8649. public function _destroy($destroyall=false, $preserve_objcopy=false) {
  8650. if ($destroyall AND isset($this->diskcache) AND $this->diskcache AND (!$preserve_objcopy) AND (!$this->empty_string($this->buffer))) {
  8651. // remove buffer file from cache
  8652. unlink($this->buffer);
  8653. }
  8654. foreach (array_keys(get_object_vars($this)) as $val) {
  8655. if ($destroyall OR (
  8656. ($val != 'internal_encoding')
  8657. AND ($val != 'state')
  8658. AND ($val != 'bufferlen')
  8659. AND ($val != 'buffer')
  8660. AND ($val != 'diskcache')
  8661. AND ($val != 'sign')
  8662. AND ($val != 'signature_data')
  8663. AND ($val != 'signature_max_length')
  8664. AND ($val != 'byterange_string')
  8665. )) {
  8666. if ((!$preserve_objcopy OR ($val != 'objcopy')) AND isset($this->$val)) {
  8667. unset($this->$val);
  8668. }
  8669. }
  8670. }
  8671. }
  8672. /**
  8673. * Check for locale-related bug
  8674. * @protected
  8675. */
  8676. protected function _dochecks() {
  8677. //Check for locale-related bug
  8678. if (1.1 == 1) {
  8679. $this->Error('Don\'t alter the locale before including class file');
  8680. }
  8681. //Check for decimal separator
  8682. if (sprintf('%.1F', 1.0) != '1.0') {
  8683. setlocale(LC_NUMERIC, 'C');
  8684. }
  8685. }
  8686. /**
  8687. * Return fonts path
  8688. * @return string
  8689. * @protected
  8690. */
  8691. protected function _getfontpath() {
  8692. if (!defined('K_PATH_FONTS') AND is_dir(dirname(__FILE__).'/fonts')) {
  8693. define('K_PATH_FONTS', dirname(__FILE__).'/fonts/');
  8694. }
  8695. return defined('K_PATH_FONTS') ? K_PATH_FONTS : '';
  8696. }
  8697. /**
  8698. * Return an array containing variations for the basic page number alias.
  8699. * @param $a (string) Base alias.
  8700. * @return array of page number aliases
  8701. * @protected
  8702. */
  8703. protected function getInternalPageNumberAliases($a= '') {
  8704. $alias = array();
  8705. // build array of Unicode + ASCII variants (the order is important)
  8706. $alias = array('u' => array(), 'a' => array());
  8707. $u = '{'.$a.'}';
  8708. $alias['u'][] = $this->_escape($u);
  8709. if ($this->isunicode) {
  8710. $alias['u'][] = $this->_escape($this->UTF8ToLatin1($u));
  8711. $alias['u'][] = $this->_escape($this->utf8StrRev($u, false, $this->tmprtl));
  8712. $alias['a'][] = $this->_escape($this->UTF8ToLatin1($a));
  8713. $alias['a'][] = $this->_escape($this->utf8StrRev($a, false, $this->tmprtl));
  8714. }
  8715. $alias['a'][] = $this->_escape($a);
  8716. return $alias;
  8717. }
  8718. /**
  8719. * Return an array containing all internal page aliases.
  8720. * @return array of page number aliases
  8721. * @protected
  8722. */
  8723. protected function getAllInternalPageNumberAliases() {
  8724. $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);
  8725. $pnalias = array();
  8726. foreach($basic_alias as $k => $a) {
  8727. $pnalias[$k] = $this->getInternalPageNumberAliases($a);
  8728. }
  8729. return $pnalias;
  8730. }
  8731. /**
  8732. * Replace page number aliases with number.
  8733. * @param $page (string) Page content.
  8734. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  8735. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  8736. * @return replaced page content and updated $diff parameter as array.
  8737. * @protected
  8738. */
  8739. protected function replacePageNumAliases($page, $replace, $diff=0) {
  8740. foreach ($replace as $rep) {
  8741. foreach ($rep[3] as $a) {
  8742. if (strpos($page, $a) !== false) {
  8743. $page = str_replace($a, $rep[0], $page);
  8744. $diff += ($rep[2] - $rep[1]);
  8745. }
  8746. }
  8747. }
  8748. return array($page, $diff);
  8749. }
  8750. /**
  8751. * Replace right shift page number aliases with spaces to correct right alignment.
  8752. * This works perfectly only when using monospaced fonts.
  8753. * @param $page (string) Page content.
  8754. * @param $aliases (array) Array of page aliases.
  8755. * @param $diff (int) initial difference to add.
  8756. * @return replaced page content.
  8757. * @protected
  8758. */
  8759. protected function replaceRightShiftPageNumAliases($page, $aliases, $diff) {
  8760. foreach ($aliases as $type => $alias) {
  8761. foreach ($alias as $a) {
  8762. // find position of compensation factor
  8763. $startnum = (strpos($a, ':') + 1);
  8764. $a = substr($a, 0, $startnum);
  8765. if (($pos = strpos($page, $a)) !== false) {
  8766. // end of alias
  8767. $endnum = strpos($page, '}', $pos);
  8768. // string to be replaced
  8769. $aa = substr($page, $pos, ($endnum - $pos + 1));
  8770. // get compensation factor
  8771. $ratio = substr($page, ($pos + $startnum), ($endnum - $pos - $startnum));
  8772. $ratio = preg_replace('/[^0-9\.]/', '', $ratio);
  8773. $ratio = floatval($ratio);
  8774. if ($type == 'u') {
  8775. $chrdiff = floor(($diff + 12) * $ratio);
  8776. $shift = str_repeat(' ', $chrdiff);
  8777. $shift = $this->UTF8ToUTF16BE($shift, false);
  8778. } else {
  8779. $chrdiff = floor(($diff + 11) * $ratio);
  8780. $shift = str_repeat(' ', $chrdiff);
  8781. }
  8782. $page = str_replace($aa, $shift, $page);
  8783. }
  8784. }
  8785. }
  8786. return $page;
  8787. }
  8788. /**
  8789. * Set page boxes to be included on page descriptions.
  8790. * @param $boxes (array) Array of page boxes to set on document: ('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox').
  8791. * @protected
  8792. */
  8793. protected function setPageBoxTypes($boxes) {
  8794. $validboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  8795. $this->page_boxes = array();
  8796. foreach ($boxes as $box) {
  8797. if (in_array($box, $validboxes)) {
  8798. $this->page_boxes[] = $box;
  8799. }
  8800. }
  8801. }
  8802. /**
  8803. * Output pages (and replace page number aliases).
  8804. * @protected
  8805. */
  8806. protected function _putpages() {
  8807. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  8808. // get internal aliases for page numbers
  8809. $pnalias = $this->getAllInternalPageNumberAliases();
  8810. $num_pages = $this->numpages;
  8811. $ptpa = $this->formatPageNumber(($this->starting_page_number + $num_pages - 1));
  8812. $ptpu = $this->UTF8ToUTF16BE($ptpa, false);
  8813. $ptp_num_chars = $this->GetNumChars($ptpa);
  8814. $pagegroupnum = 0;
  8815. $groupnum = 0;
  8816. $ptgu = 1;
  8817. $ptga = 1;
  8818. for ($n = 1; $n <= $num_pages; ++$n) {
  8819. // get current page
  8820. $temppage = $this->getPageBuffer($n);
  8821. $pagelen = strlen($temppage);
  8822. // set replacements for total pages number
  8823. $pnpa = $this->formatPageNumber(($this->starting_page_number + $n - 1));
  8824. $pnpu = $this->UTF8ToUTF16BE($pnpa, false);
  8825. $pnp_num_chars = $this->GetNumChars($pnpa);
  8826. $pdiff = 0; // difference used for right shift alignment of page numbers
  8827. $gdiff = 0; // difference used for right shift alignment of page group numbers
  8828. if (!empty($this->pagegroups)) {
  8829. if (isset($this->newpagegroup[$n])) {
  8830. $pagegroupnum = 0;
  8831. ++$groupnum;
  8832. $ptga = $this->formatPageNumber($this->pagegroups[$groupnum]);
  8833. $ptgu = $this->UTF8ToUTF16BE($ptga, false);
  8834. $ptg_num_chars = $this->GetNumChars($ptga);
  8835. }
  8836. ++$pagegroupnum;
  8837. $pnga = $this->formatPageNumber($pagegroupnum);
  8838. $pngu = $this->UTF8ToUTF16BE($pnga, false);
  8839. $png_num_chars = $this->GetNumChars($pnga);
  8840. // replace page numbers
  8841. $replace = array();
  8842. $replace[] = array($ptgu, $ptg_num_chars, 9, $pnalias[2]['u']);
  8843. $replace[] = array($ptga, $ptg_num_chars, 7, $pnalias[2]['a']);
  8844. $replace[] = array($pngu, $png_num_chars, 9, $pnalias[3]['u']);
  8845. $replace[] = array($pnga, $png_num_chars, 7, $pnalias[3]['a']);
  8846. list($temppage, $gdiff) = $this->replacePageNumAliases($temppage, $replace, $gdiff);
  8847. }
  8848. // replace page numbers
  8849. $replace = array();
  8850. $replace[] = array($ptpu, $ptp_num_chars, 9, $pnalias[0]['u']);
  8851. $replace[] = array($ptpa, $ptp_num_chars, 7, $pnalias[0]['a']);
  8852. $replace[] = array($pnpu, $pnp_num_chars, 9, $pnalias[1]['u']);
  8853. $replace[] = array($pnpa, $pnp_num_chars, 7, $pnalias[1]['a']);
  8854. list($temppage, $pdiff) = $this->replacePageNumAliases($temppage, $replace, $pdiff);
  8855. // replace right shift alias
  8856. $temppage = $this->replaceRightShiftPageNumAliases($temppage, $pnalias[4], max($pdiff, $gdiff));
  8857. // replace EPS marker
  8858. $temppage = str_replace($this->epsmarker, '', $temppage);
  8859. //Page
  8860. $this->page_obj_id[$n] = $this->_newobj();
  8861. $out = '<<';
  8862. $out .= ' /Type /Page';
  8863. $out .= ' /Parent 1 0 R';
  8864. $out .= ' /LastModified '.$this->_datestring(0, $this->doc_modification_timestamp);
  8865. $out .= ' /Resources 2 0 R';
  8866. foreach ($this->page_boxes as $box) {
  8867. $out .= ' /'.$box;
  8868. $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']);
  8869. }
  8870. if (isset($this->pagedim[$n]['BoxColorInfo']) AND !empty($this->pagedim[$n]['BoxColorInfo'])) {
  8871. $out .= ' /BoxColorInfo <<';
  8872. foreach ($this->page_boxes as $box) {
  8873. if (isset($this->pagedim[$n]['BoxColorInfo'][$box])) {
  8874. $out .= ' /'.$box.' <<';
  8875. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['C'])) {
  8876. $color = $this->pagedim[$n]['BoxColorInfo'][$box]['C'];
  8877. $out .= ' /C [';
  8878. $out .= sprintf(' %F %F %F', ($color[0] / 255), ($color[1] / 255), ($color[2] / 255));
  8879. $out .= ' ]';
  8880. }
  8881. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['W'])) {
  8882. $out .= ' /W '.($this->pagedim[$n]['BoxColorInfo'][$box]['W'] * $this->k);
  8883. }
  8884. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['S'])) {
  8885. $out .= ' /S /'.$this->pagedim[$n]['BoxColorInfo'][$box]['S'];
  8886. }
  8887. if (isset($this->pagedim[$n]['BoxColorInfo'][$box]['D'])) {
  8888. $dashes = $this->pagedim[$n]['BoxColorInfo'][$box]['D'];
  8889. $out .= ' /D [';
  8890. foreach ($dashes as $dash) {
  8891. $out .= sprintf(' %F', ($dash * $this->k));
  8892. }
  8893. $out .= ' ]';
  8894. }
  8895. $out .= ' >>';
  8896. }
  8897. }
  8898. $out .= ' >>';
  8899. }
  8900. $out .= ' /Contents '.($this->n + 1).' 0 R';
  8901. $out .= ' /Rotate '.$this->pagedim[$n]['Rotate'];
  8902. if (!$this->pdfa_mode) {
  8903. $out .= ' /Group << /Type /Group /S /Transparency /CS /DeviceRGB >>';
  8904. }
  8905. if (isset($this->pagedim[$n]['trans']) AND !empty($this->pagedim[$n]['trans'])) {
  8906. // page transitions
  8907. if (isset($this->pagedim[$n]['trans']['Dur'])) {
  8908. $out .= ' /Dur '.$this->pagedim[$n]['trans']['Dur'];
  8909. }
  8910. $out .= ' /Trans <<';
  8911. $out .= ' /Type /Trans';
  8912. if (isset($this->pagedim[$n]['trans']['S'])) {
  8913. $out .= ' /S /'.$this->pagedim[$n]['trans']['S'];
  8914. }
  8915. if (isset($this->pagedim[$n]['trans']['D'])) {
  8916. $out .= ' /D '.$this->pagedim[$n]['trans']['D'];
  8917. }
  8918. if (isset($this->pagedim[$n]['trans']['Dm'])) {
  8919. $out .= ' /Dm /'.$this->pagedim[$n]['trans']['Dm'];
  8920. }
  8921. if (isset($this->pagedim[$n]['trans']['M'])) {
  8922. $out .= ' /M /'.$this->pagedim[$n]['trans']['M'];
  8923. }
  8924. if (isset($this->pagedim[$n]['trans']['Di'])) {
  8925. $out .= ' /Di '.$this->pagedim[$n]['trans']['Di'];
  8926. }
  8927. if (isset($this->pagedim[$n]['trans']['SS'])) {
  8928. $out .= ' /SS '.$this->pagedim[$n]['trans']['SS'];
  8929. }
  8930. if (isset($this->pagedim[$n]['trans']['B'])) {
  8931. $out .= ' /B '.$this->pagedim[$n]['trans']['B'];
  8932. }
  8933. $out .= ' >>';
  8934. }
  8935. $out .= $this->_getannotsrefs($n);
  8936. $out .= ' /PZ '.$this->pagedim[$n]['PZ'];
  8937. $out .= ' >>';
  8938. $out .= "\n".'endobj';
  8939. $this->_out($out);
  8940. //Page content
  8941. $p = ($this->compress) ? gzcompress($temppage) : $temppage;
  8942. $this->_newobj();
  8943. $p = $this->_getrawstream($p);
  8944. $this->_out('<<'.$filter.'/Length '.strlen($p).'>> stream'."\n".$p."\n".'endstream'."\n".'endobj');
  8945. if ($this->diskcache) {
  8946. // remove temporary files
  8947. unlink($this->pages[$n]);
  8948. }
  8949. }
  8950. //Pages root
  8951. $out = $this->_getobj(1)."\n";
  8952. $out .= '<< /Type /Pages /Kids [';
  8953. foreach($this->page_obj_id as $page_obj) {
  8954. $out .= ' '.$page_obj.' 0 R';
  8955. }
  8956. $out .= ' ] /Count '.$num_pages.' >>';
  8957. $out .= "\n".'endobj';
  8958. $this->_out($out);
  8959. }
  8960. /**
  8961. * Output references to page annotations
  8962. * @param $n (int) page number
  8963. * @protected
  8964. * @author Nicola Asuni
  8965. * @since 4.7.000 (2008-08-29)
  8966. * @deprecated
  8967. */
  8968. protected function _putannotsrefs($n) {
  8969. $this->_out($this->_getannotsrefs($n));
  8970. }
  8971. /**
  8972. * Get references to page annotations.
  8973. * @param $n (int) page number
  8974. * @return string
  8975. * @protected
  8976. * @author Nicola Asuni
  8977. * @since 5.0.010 (2010-05-17)
  8978. */
  8979. protected function _getannotsrefs($n) {
  8980. if (!(isset($this->PageAnnots[$n]) OR ($this->sign AND isset($this->signature_data['cert_type'])))) {
  8981. return '';
  8982. }
  8983. $out = ' /Annots [';
  8984. if (isset($this->PageAnnots[$n])) {
  8985. foreach ($this->PageAnnots[$n] as $key => $val) {
  8986. if (!in_array($val['n'], $this->radio_groups)) {
  8987. $out .= ' '.$val['n'].' 0 R';
  8988. }
  8989. }
  8990. // add radiobutton groups
  8991. if (isset($this->radiobutton_groups[$n])) {
  8992. foreach ($this->radiobutton_groups[$n] as $key => $data) {
  8993. if (isset($data['n'])) {
  8994. $out .= ' '.$data['n'].' 0 R';
  8995. }
  8996. }
  8997. }
  8998. }
  8999. if ($this->sign AND ($n == $this->signature_appearance['page']) AND isset($this->signature_data['cert_type'])) {
  9000. // set reference for signature object
  9001. $out .= ' '.$this->sig_obj_id.' 0 R';
  9002. }
  9003. if (!empty($this->empty_signature_appearance)) {
  9004. foreach ($this->empty_signature_appearance as $esa) {
  9005. if ($esa['page'] == $n) {
  9006. // set reference for empty signature objects
  9007. $out .= ' '.$esa['objid'].' 0 R';
  9008. }
  9009. }
  9010. }
  9011. $out .= ' ]';
  9012. return $out;
  9013. }
  9014. /**
  9015. * Output annotations objects for all pages.
  9016. * !!! THIS METHOD IS NOT YET COMPLETED !!!
  9017. * See section 12.5 of PDF 32000_2008 reference.
  9018. * @protected
  9019. * @author Nicola Asuni
  9020. * @since 4.0.018 (2008-08-06)
  9021. */
  9022. protected function _putannotsobjs() {
  9023. // reset object counter
  9024. for ($n=1; $n <= $this->numpages; ++$n) {
  9025. if (isset($this->PageAnnots[$n])) {
  9026. // set page annotations
  9027. foreach ($this->PageAnnots[$n] as $key => $pl) {
  9028. $annot_obj_id = $this->PageAnnots[$n][$key]['n'];
  9029. // create annotation object for grouping radiobuttons
  9030. if (isset($this->radiobutton_groups[$n][$pl['txt']]) AND is_array($this->radiobutton_groups[$n][$pl['txt']])) {
  9031. $radio_button_obj_id = $this->radiobutton_groups[$n][$pl['txt']]['n'];
  9032. $annots = '<<';
  9033. $annots .= ' /Type /Annot';
  9034. $annots .= ' /Subtype /Widget';
  9035. $annots .= ' /Rect [0 0 0 0]';
  9036. if ($this->radiobutton_groups[$n][$pl['txt']]['#readonly#']) {
  9037. // read only
  9038. $annots .= ' /F 68';
  9039. $annots .= ' /Ff 49153';
  9040. } else {
  9041. $annots .= ' /F 4'; // default print for PDF/A
  9042. $annots .= ' /Ff 49152';
  9043. }
  9044. $annots .= ' /T '.$this->_datastring($pl['txt'], $radio_button_obj_id);
  9045. $annots .= ' /FT /Btn';
  9046. $annots .= ' /Kids [';
  9047. $defval = '';
  9048. foreach ($this->radiobutton_groups[$n][$pl['txt']] as $key => $data) {
  9049. if (isset($data['kid'])) {
  9050. $annots .= ' '.$data['kid'].' 0 R';
  9051. if ($data['def'] !== 'Off') {
  9052. $defval = $data['def'];
  9053. }
  9054. }
  9055. }
  9056. $annots .= ' ]';
  9057. if (!empty($defval)) {
  9058. $annots .= ' /V /'.$defval;
  9059. }
  9060. $annots .= ' >>';
  9061. $this->_out($this->_getobj($radio_button_obj_id)."\n".$annots."\n".'endobj');
  9062. $this->form_obj_id[] = $radio_button_obj_id;
  9063. // store object id to be used on Parent entry of Kids
  9064. $this->radiobutton_groups[$n][$pl['txt']] = $radio_button_obj_id;
  9065. }
  9066. $formfield = false;
  9067. $pl['opt'] = array_change_key_case($pl['opt'], CASE_LOWER);
  9068. $a = $pl['x'] * $this->k;
  9069. $b = $this->pagedim[$n]['h'] - (($pl['y'] + $pl['h']) * $this->k);
  9070. $c = $pl['w'] * $this->k;
  9071. $d = $pl['h'] * $this->k;
  9072. $rect = sprintf('%F %F %F %F', $a, $b, $a+$c, $b+$d);
  9073. // create new annotation object
  9074. $annots = '<</Type /Annot';
  9075. $annots .= ' /Subtype /'.$pl['opt']['subtype'];
  9076. $annots .= ' /Rect ['.$rect.']';
  9077. $ft = array('Btn', 'Tx', 'Ch', 'Sig');
  9078. if (isset($pl['opt']['ft']) AND in_array($pl['opt']['ft'], $ft)) {
  9079. $annots .= ' /FT /'.$pl['opt']['ft'];
  9080. $formfield = true;
  9081. }
  9082. $annots .= ' /Contents '.$this->_textstring($pl['txt'], $annot_obj_id);
  9083. $annots .= ' /P '.$this->page_obj_id[$n].' 0 R';
  9084. $annots .= ' /NM '.$this->_datastring(sprintf('%04u-%04u', $n, $key), $annot_obj_id);
  9085. $annots .= ' /M '.$this->_datestring($annot_obj_id, $this->doc_modification_timestamp);
  9086. if (isset($pl['opt']['f'])) {
  9087. $fval = 0;
  9088. if (is_array($pl['opt']['f'])) {
  9089. foreach ($pl['opt']['f'] as $f) {
  9090. switch (strtolower($f)) {
  9091. case 'invisible': {
  9092. $fval += 1 << 0;
  9093. break;
  9094. }
  9095. case 'hidden': {
  9096. $fval += 1 << 1;
  9097. break;
  9098. }
  9099. case 'print': {
  9100. $fval += 1 << 2;
  9101. break;
  9102. }
  9103. case 'nozoom': {
  9104. $fval += 1 << 3;
  9105. break;
  9106. }
  9107. case 'norotate': {
  9108. $fval += 1 << 4;
  9109. break;
  9110. }
  9111. case 'noview': {
  9112. $fval += 1 << 5;
  9113. break;
  9114. }
  9115. case 'readonly': {
  9116. $fval += 1 << 6;
  9117. break;
  9118. }
  9119. case 'locked': {
  9120. $fval += 1 << 8;
  9121. break;
  9122. }
  9123. case 'togglenoview': {
  9124. $fval += 1 << 9;
  9125. break;
  9126. }
  9127. case 'lockedcontents': {
  9128. $fval += 1 << 10;
  9129. break;
  9130. }
  9131. default: {
  9132. break;
  9133. }
  9134. }
  9135. }
  9136. } else {
  9137. $fval = intval($pl['opt']['f']);
  9138. }
  9139. } else {
  9140. $fval = 4;
  9141. }
  9142. if ($this->pdfa_mode) {
  9143. // force print flag for PDF/A mode
  9144. $fval |= 4;
  9145. }
  9146. $annots .= ' /F '.intval($fval);
  9147. if (isset($pl['opt']['as']) AND is_string($pl['opt']['as'])) {
  9148. $annots .= ' /AS /'.$pl['opt']['as'];
  9149. }
  9150. if (isset($pl['opt']['ap'])) {
  9151. // appearance stream
  9152. $annots .= ' /AP <<';
  9153. if (is_array($pl['opt']['ap'])) {
  9154. foreach ($pl['opt']['ap'] as $apmode => $apdef) {
  9155. // $apmode can be: n = normal; r = rollover; d = down;
  9156. $annots .= ' /'.strtoupper($apmode);
  9157. if (is_array($apdef)) {
  9158. $annots .= ' <<';
  9159. foreach ($apdef as $apstate => $stream) {
  9160. // reference to XObject that define the appearance for this mode-state
  9161. $apsobjid = $this->_putAPXObject($c, $d, $stream);
  9162. $annots .= ' /'.$apstate.' '.$apsobjid.' 0 R';
  9163. }
  9164. $annots .= ' >>';
  9165. } else {
  9166. // reference to XObject that define the appearance for this mode
  9167. $apsobjid = $this->_putAPXObject($c, $d, $apdef);
  9168. $annots .= ' '.$apsobjid.' 0 R';
  9169. }
  9170. }
  9171. } else {
  9172. $annots .= $pl['opt']['ap'];
  9173. }
  9174. $annots .= ' >>';
  9175. }
  9176. if (isset($pl['opt']['bs']) AND (is_array($pl['opt']['bs']))) {
  9177. $annots .= ' /BS <<';
  9178. $annots .= ' /Type /Border';
  9179. if (isset($pl['opt']['bs']['w'])) {
  9180. $annots .= ' /W '.intval($pl['opt']['bs']['w']);
  9181. }
  9182. $bstyles = array('S', 'D', 'B', 'I', 'U');
  9183. if (isset($pl['opt']['bs']['s']) AND in_array($pl['opt']['bs']['s'], $bstyles)) {
  9184. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  9185. }
  9186. if (isset($pl['opt']['bs']['d']) AND (is_array($pl['opt']['bs']['d']))) {
  9187. $annots .= ' /D [';
  9188. foreach ($pl['opt']['bs']['d'] as $cord) {
  9189. $annots .= ' '.intval($cord);
  9190. }
  9191. $annots .= ']';
  9192. }
  9193. $annots .= ' >>';
  9194. } else {
  9195. $annots .= ' /Border [';
  9196. if (isset($pl['opt']['border']) AND (count($pl['opt']['border']) >= 3)) {
  9197. $annots .= intval($pl['opt']['border'][0]).' ';
  9198. $annots .= intval($pl['opt']['border'][1]).' ';
  9199. $annots .= intval($pl['opt']['border'][2]);
  9200. if (isset($pl['opt']['border'][3]) AND is_array($pl['opt']['border'][3])) {
  9201. $annots .= ' [';
  9202. foreach ($pl['opt']['border'][3] as $dash) {
  9203. $annots .= intval($dash).' ';
  9204. }
  9205. $annots .= ']';
  9206. }
  9207. } else {
  9208. $annots .= '0 0 0';
  9209. }
  9210. $annots .= ']';
  9211. }
  9212. if (isset($pl['opt']['be']) AND (is_array($pl['opt']['be']))) {
  9213. $annots .= ' /BE <<';
  9214. $bstyles = array('S', 'C');
  9215. if (isset($pl['opt']['be']['s']) AND in_array($pl['opt']['be']['s'], $bstyles)) {
  9216. $annots .= ' /S /'.$pl['opt']['bs']['s'];
  9217. } else {
  9218. $annots .= ' /S /S';
  9219. }
  9220. if (isset($pl['opt']['be']['i']) AND ($pl['opt']['be']['i'] >= 0) AND ($pl['opt']['be']['i'] <= 2)) {
  9221. $annots .= ' /I '.sprintf(' %F', $pl['opt']['be']['i']);
  9222. }
  9223. $annots .= '>>';
  9224. }
  9225. if (isset($pl['opt']['c']) AND (is_array($pl['opt']['c'])) AND !empty($pl['opt']['c'])) {
  9226. $annots .= ' /C '.$this->getColorStringFromArray($pl['opt']['c']);
  9227. }
  9228. //$annots .= ' /StructParent ';
  9229. //$annots .= ' /OC ';
  9230. $markups = array('text', 'freetext', 'line', 'square', 'circle', 'polygon', 'polyline', 'highlight', 'underline', 'squiggly', 'strikeout', 'stamp', 'caret', 'ink', 'fileattachment', 'sound');
  9231. if (in_array(strtolower($pl['opt']['subtype']), $markups)) {
  9232. // this is a markup type
  9233. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9234. $annots .= ' /T '.$this->_textstring($pl['opt']['t'], $annot_obj_id);
  9235. }
  9236. //$annots .= ' /Popup ';
  9237. if (isset($pl['opt']['ca'])) {
  9238. $annots .= ' /CA '.sprintf('%F', floatval($pl['opt']['ca']));
  9239. }
  9240. if (isset($pl['opt']['rc'])) {
  9241. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  9242. }
  9243. $annots .= ' /CreationDate '.$this->_datestring($annot_obj_id, $this->doc_creation_timestamp);
  9244. //$annots .= ' /IRT ';
  9245. if (isset($pl['opt']['subj'])) {
  9246. $annots .= ' /Subj '.$this->_textstring($pl['opt']['subj'], $annot_obj_id);
  9247. }
  9248. //$annots .= ' /RT ';
  9249. //$annots .= ' /IT ';
  9250. //$annots .= ' /ExData ';
  9251. }
  9252. $lineendings = array('Square', 'Circle', 'Diamond', 'OpenArrow', 'ClosedArrow', 'None', 'Butt', 'ROpenArrow', 'RClosedArrow', 'Slash');
  9253. // Annotation types
  9254. switch (strtolower($pl['opt']['subtype'])) {
  9255. case 'text': {
  9256. if (isset($pl['opt']['open'])) {
  9257. $annots .= ' /Open '. (strtolower($pl['opt']['open']) == 'true' ? 'true' : 'false');
  9258. }
  9259. $iconsapp = array('Comment', 'Help', 'Insert', 'Key', 'NewParagraph', 'Note', 'Paragraph');
  9260. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9261. $annots .= ' /Name /'.$pl['opt']['name'];
  9262. } else {
  9263. $annots .= ' /Name /Note';
  9264. }
  9265. $statemodels = array('Marked', 'Review');
  9266. if (isset($pl['opt']['statemodel']) AND in_array($pl['opt']['statemodel'], $statemodels)) {
  9267. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  9268. } else {
  9269. $pl['opt']['statemodel'] = 'Marked';
  9270. $annots .= ' /StateModel /'.$pl['opt']['statemodel'];
  9271. }
  9272. if ($pl['opt']['statemodel'] == 'Marked') {
  9273. $states = array('Accepted', 'Unmarked');
  9274. } else {
  9275. $states = array('Accepted', 'Rejected', 'Cancelled', 'Completed', 'None');
  9276. }
  9277. if (isset($pl['opt']['state']) AND in_array($pl['opt']['state'], $states)) {
  9278. $annots .= ' /State /'.$pl['opt']['state'];
  9279. } else {
  9280. if ($pl['opt']['statemodel'] == 'Marked') {
  9281. $annots .= ' /State /Unmarked';
  9282. } else {
  9283. $annots .= ' /State /None';
  9284. }
  9285. }
  9286. break;
  9287. }
  9288. case 'link': {
  9289. if (is_string($pl['txt'])) {
  9290. // external URI link
  9291. $annots .= ' /A <</S /URI /URI '.$this->_datastring($this->unhtmlentities($pl['txt']), $annot_obj_id).'>>';
  9292. } else {
  9293. // internal link
  9294. if (isset($this->links[$pl['txt']])) {
  9295. $l = $this->links[$pl['txt']];
  9296. if (isset($this->page_obj_id[($l[0])])) {
  9297. $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)));
  9298. }
  9299. }
  9300. }
  9301. $hmodes = array('N', 'I', 'O', 'P');
  9302. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmodes)) {
  9303. $annots .= ' /H /'.$pl['opt']['h'];
  9304. } else {
  9305. $annots .= ' /H /I';
  9306. }
  9307. //$annots .= ' /PA ';
  9308. //$annots .= ' /Quadpoints ';
  9309. break;
  9310. }
  9311. case 'freetext': {
  9312. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9313. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9314. }
  9315. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9316. $annots .= ' /Q '.intval($pl['opt']['q']);
  9317. }
  9318. if (isset($pl['opt']['rc'])) {
  9319. $annots .= ' /RC '.$this->_textstring($pl['opt']['rc'], $annot_obj_id);
  9320. }
  9321. if (isset($pl['opt']['ds'])) {
  9322. $annots .= ' /DS '.$this->_textstring($pl['opt']['ds'], $annot_obj_id);
  9323. }
  9324. if (isset($pl['opt']['cl']) AND is_array($pl['opt']['cl'])) {
  9325. $annots .= ' /CL [';
  9326. foreach ($pl['opt']['cl'] as $cl) {
  9327. $annots .= sprintf('%F ', $cl * $this->k);
  9328. }
  9329. $annots .= ']';
  9330. }
  9331. $tfit = array('FreeText', 'FreeTextCallout', 'FreeTextTypeWriter');
  9332. if (isset($pl['opt']['it']) AND in_array($pl['opt']['it'], $tfit)) {
  9333. $annots .= ' /IT /'.$pl['opt']['it'];
  9334. }
  9335. if (isset($pl['opt']['rd']) AND is_array($pl['opt']['rd'])) {
  9336. $l = $pl['opt']['rd'][0] * $this->k;
  9337. $r = $pl['opt']['rd'][1] * $this->k;
  9338. $t = $pl['opt']['rd'][2] * $this->k;
  9339. $b = $pl['opt']['rd'][3] * $this->k;
  9340. $annots .= ' /RD ['.sprintf('%F %F %F %F', $l, $r, $t, $b).']';
  9341. }
  9342. if (isset($pl['opt']['le']) AND in_array($pl['opt']['le'], $lineendings)) {
  9343. $annots .= ' /LE /'.$pl['opt']['le'];
  9344. }
  9345. break;
  9346. }
  9347. case 'line': {
  9348. break;
  9349. }
  9350. case 'square': {
  9351. break;
  9352. }
  9353. case 'circle': {
  9354. break;
  9355. }
  9356. case 'polygon': {
  9357. break;
  9358. }
  9359. case 'polyline': {
  9360. break;
  9361. }
  9362. case 'highlight': {
  9363. break;
  9364. }
  9365. case 'underline': {
  9366. break;
  9367. }
  9368. case 'squiggly': {
  9369. break;
  9370. }
  9371. case 'strikeout': {
  9372. break;
  9373. }
  9374. case 'stamp': {
  9375. break;
  9376. }
  9377. case 'caret': {
  9378. break;
  9379. }
  9380. case 'ink': {
  9381. break;
  9382. }
  9383. case 'popup': {
  9384. break;
  9385. }
  9386. case 'fileattachment': {
  9387. if ($this->pdfa_mode) {
  9388. // embedded files are not allowed in PDF/A mode
  9389. break;
  9390. }
  9391. if (!isset($pl['opt']['fs'])) {
  9392. break;
  9393. }
  9394. $filename = basename($pl['opt']['fs']);
  9395. if (isset($this->embeddedfiles[$filename]['n'])) {
  9396. $annots .= ' /FS <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9397. $iconsapp = array('Graph', 'Paperclip', 'PushPin', 'Tag');
  9398. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9399. $annots .= ' /Name /'.$pl['opt']['name'];
  9400. } else {
  9401. $annots .= ' /Name /PushPin';
  9402. }
  9403. }
  9404. break;
  9405. }
  9406. case 'sound': {
  9407. if (!isset($pl['opt']['fs'])) {
  9408. break;
  9409. }
  9410. $filename = basename($pl['opt']['fs']);
  9411. if (isset($this->embeddedfiles[$filename]['n'])) {
  9412. // ... TO BE COMPLETED ...
  9413. // /R /C /B /E /CO /CP
  9414. $annots .= ' /Sound <</Type /Filespec /F '.$this->_datastring($filename, $annot_obj_id).' /EF <</F '.$this->embeddedfiles[$filename]['n'].' 0 R>> >>';
  9415. $iconsapp = array('Speaker', 'Mic');
  9416. if (isset($pl['opt']['name']) AND in_array($pl['opt']['name'], $iconsapp)) {
  9417. $annots .= ' /Name /'.$pl['opt']['name'];
  9418. } else {
  9419. $annots .= ' /Name /Speaker';
  9420. }
  9421. }
  9422. break;
  9423. }
  9424. case 'movie': {
  9425. break;
  9426. }
  9427. case 'widget': {
  9428. $hmode = array('N', 'I', 'O', 'P', 'T');
  9429. if (isset($pl['opt']['h']) AND in_array($pl['opt']['h'], $hmode)) {
  9430. $annots .= ' /H /'.$pl['opt']['h'];
  9431. }
  9432. if (isset($pl['opt']['mk']) AND (is_array($pl['opt']['mk'])) AND !empty($pl['opt']['mk'])) {
  9433. $annots .= ' /MK <<';
  9434. if (isset($pl['opt']['mk']['r'])) {
  9435. $annots .= ' /R '.$pl['opt']['mk']['r'];
  9436. }
  9437. if (isset($pl['opt']['mk']['bc']) AND (is_array($pl['opt']['mk']['bc']))) {
  9438. $annots .= ' /BC '.$this->getColorStringFromArray($pl['opt']['mk']['bc']);
  9439. }
  9440. if (isset($pl['opt']['mk']['bg']) AND (is_array($pl['opt']['mk']['bg']))) {
  9441. $annots .= ' /BG '.$this->getColorStringFromArray($pl['opt']['mk']['bg']);
  9442. }
  9443. if (isset($pl['opt']['mk']['ca'])) {
  9444. $annots .= ' /CA '.$pl['opt']['mk']['ca'];
  9445. }
  9446. if (isset($pl['opt']['mk']['rc'])) {
  9447. $annots .= ' /RC '.$pl['opt']['mk']['rc'];
  9448. }
  9449. if (isset($pl['opt']['mk']['ac'])) {
  9450. $annots .= ' /AC '.$pl['opt']['mk']['ac'];
  9451. }
  9452. if (isset($pl['opt']['mk']['i'])) {
  9453. $info = $this->getImageBuffer($pl['opt']['mk']['i']);
  9454. if ($info !== false) {
  9455. $annots .= ' /I '.$info['n'].' 0 R';
  9456. }
  9457. }
  9458. if (isset($pl['opt']['mk']['ri'])) {
  9459. $info = $this->getImageBuffer($pl['opt']['mk']['ri']);
  9460. if ($info !== false) {
  9461. $annots .= ' /RI '.$info['n'].' 0 R';
  9462. }
  9463. }
  9464. if (isset($pl['opt']['mk']['ix'])) {
  9465. $info = $this->getImageBuffer($pl['opt']['mk']['ix']);
  9466. if ($info !== false) {
  9467. $annots .= ' /IX '.$info['n'].' 0 R';
  9468. }
  9469. }
  9470. if (isset($pl['opt']['mk']['if']) AND (is_array($pl['opt']['mk']['if'])) AND !empty($pl['opt']['mk']['if'])) {
  9471. $annots .= ' /IF <<';
  9472. $if_sw = array('A', 'B', 'S', 'N');
  9473. if (isset($pl['opt']['mk']['if']['sw']) AND in_array($pl['opt']['mk']['if']['sw'], $if_sw)) {
  9474. $annots .= ' /SW /'.$pl['opt']['mk']['if']['sw'];
  9475. }
  9476. $if_s = array('A', 'P');
  9477. if (isset($pl['opt']['mk']['if']['s']) AND in_array($pl['opt']['mk']['if']['s'], $if_s)) {
  9478. $annots .= ' /S /'.$pl['opt']['mk']['if']['s'];
  9479. }
  9480. if (isset($pl['opt']['mk']['if']['a']) AND (is_array($pl['opt']['mk']['if']['a'])) AND !empty($pl['opt']['mk']['if']['a'])) {
  9481. $annots .= sprintf(' /A [%F %F]', $pl['opt']['mk']['if']['a'][0], $pl['opt']['mk']['if']['a'][1]);
  9482. }
  9483. if (isset($pl['opt']['mk']['if']['fb']) AND ($pl['opt']['mk']['if']['fb'])) {
  9484. $annots .= ' /FB true';
  9485. }
  9486. $annots .= '>>';
  9487. }
  9488. if (isset($pl['opt']['mk']['tp']) AND ($pl['opt']['mk']['tp'] >= 0) AND ($pl['opt']['mk']['tp'] <= 6)) {
  9489. $annots .= ' /TP '.intval($pl['opt']['mk']['tp']);
  9490. }
  9491. $annots .= '>>';
  9492. } // end MK
  9493. // --- Entries for field dictionaries ---
  9494. if (isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9495. // set parent
  9496. $annots .= ' /Parent '.$this->radiobutton_groups[$n][$pl['txt']].' 0 R';
  9497. }
  9498. if (isset($pl['opt']['t']) AND is_string($pl['opt']['t'])) {
  9499. $annots .= ' /T '.$this->_datastring($pl['opt']['t'], $annot_obj_id);
  9500. }
  9501. if (isset($pl['opt']['tu']) AND is_string($pl['opt']['tu'])) {
  9502. $annots .= ' /TU '.$this->_datastring($pl['opt']['tu'], $annot_obj_id);
  9503. }
  9504. if (isset($pl['opt']['tm']) AND is_string($pl['opt']['tm'])) {
  9505. $annots .= ' /TM '.$this->_datastring($pl['opt']['tm'], $annot_obj_id);
  9506. }
  9507. if (isset($pl['opt']['ff'])) {
  9508. if (is_array($pl['opt']['ff'])) {
  9509. // array of bit settings
  9510. $flag = 0;
  9511. foreach($pl['opt']['ff'] as $val) {
  9512. $flag += 1 << ($val - 1);
  9513. }
  9514. } else {
  9515. $flag = intval($pl['opt']['ff']);
  9516. }
  9517. $annots .= ' /Ff '.$flag;
  9518. }
  9519. if (isset($pl['opt']['maxlen'])) {
  9520. $annots .= ' /MaxLen '.intval($pl['opt']['maxlen']);
  9521. }
  9522. if (isset($pl['opt']['v'])) {
  9523. $annots .= ' /V';
  9524. if (is_array($pl['opt']['v'])) {
  9525. foreach ($pl['opt']['v'] AS $optval) {
  9526. if (is_float($optval)) {
  9527. $optval = sprintf('%F', $optval);
  9528. }
  9529. $annots .= ' '.$optval;
  9530. }
  9531. } else {
  9532. $annots .= ' '.$this->_textstring($pl['opt']['v'], $annot_obj_id);
  9533. }
  9534. }
  9535. if (isset($pl['opt']['dv'])) {
  9536. $annots .= ' /DV';
  9537. if (is_array($pl['opt']['dv'])) {
  9538. foreach ($pl['opt']['dv'] AS $optval) {
  9539. if (is_float($optval)) {
  9540. $optval = sprintf('%F', $optval);
  9541. }
  9542. $annots .= ' '.$optval;
  9543. }
  9544. } else {
  9545. $annots .= ' '.$this->_textstring($pl['opt']['dv'], $annot_obj_id);
  9546. }
  9547. }
  9548. if (isset($pl['opt']['rv'])) {
  9549. $annots .= ' /RV';
  9550. if (is_array($pl['opt']['rv'])) {
  9551. foreach ($pl['opt']['rv'] AS $optval) {
  9552. if (is_float($optval)) {
  9553. $optval = sprintf('%F', $optval);
  9554. }
  9555. $annots .= ' '.$optval;
  9556. }
  9557. } else {
  9558. $annots .= ' '.$this->_textstring($pl['opt']['rv'], $annot_obj_id);
  9559. }
  9560. }
  9561. if (isset($pl['opt']['a']) AND !empty($pl['opt']['a'])) {
  9562. $annots .= ' /A << '.$pl['opt']['a'].' >>';
  9563. }
  9564. if (isset($pl['opt']['aa']) AND !empty($pl['opt']['aa'])) {
  9565. $annots .= ' /AA << '.$pl['opt']['aa'].' >>';
  9566. }
  9567. if (isset($pl['opt']['da']) AND !empty($pl['opt']['da'])) {
  9568. $annots .= ' /DA ('.$pl['opt']['da'].')';
  9569. }
  9570. if (isset($pl['opt']['q']) AND ($pl['opt']['q'] >= 0) AND ($pl['opt']['q'] <= 2)) {
  9571. $annots .= ' /Q '.intval($pl['opt']['q']);
  9572. }
  9573. if (isset($pl['opt']['opt']) AND (is_array($pl['opt']['opt'])) AND !empty($pl['opt']['opt'])) {
  9574. $annots .= ' /Opt [';
  9575. foreach($pl['opt']['opt'] AS $copt) {
  9576. if (is_array($copt)) {
  9577. $annots .= ' ['.$this->_textstring($copt[0], $annot_obj_id).' '.$this->_textstring($copt[1], $annot_obj_id).']';
  9578. } else {
  9579. $annots .= ' '.$this->_textstring($copt, $annot_obj_id);
  9580. }
  9581. }
  9582. $annots .= ']';
  9583. }
  9584. if (isset($pl['opt']['ti'])) {
  9585. $annots .= ' /TI '.intval($pl['opt']['ti']);
  9586. }
  9587. if (isset($pl['opt']['i']) AND (is_array($pl['opt']['i'])) AND !empty($pl['opt']['i'])) {
  9588. $annots .= ' /I [';
  9589. foreach($pl['opt']['i'] AS $copt) {
  9590. $annots .= intval($copt).' ';
  9591. }
  9592. $annots .= ']';
  9593. }
  9594. break;
  9595. }
  9596. case 'screen': {
  9597. break;
  9598. }
  9599. case 'printermark': {
  9600. break;
  9601. }
  9602. case 'trapnet': {
  9603. break;
  9604. }
  9605. case 'watermark': {
  9606. break;
  9607. }
  9608. case '3d': {
  9609. break;
  9610. }
  9611. default: {
  9612. break;
  9613. }
  9614. }
  9615. $annots .= '>>';
  9616. // create new annotation object
  9617. $this->_out($this->_getobj($annot_obj_id)."\n".$annots."\n".'endobj');
  9618. if ($formfield AND !isset($this->radiobutton_groups[$n][$pl['txt']])) {
  9619. // store reference of form object
  9620. $this->form_obj_id[] = $annot_obj_id;
  9621. }
  9622. }
  9623. }
  9624. } // end for each page
  9625. }
  9626. /**
  9627. * Put appearance streams XObject used to define annotation's appearance states.
  9628. * @param $w (int) annotation width
  9629. * @param $h (int) annotation height
  9630. * @param $stream (string) appearance stream
  9631. * @return int object ID
  9632. * @protected
  9633. * @since 4.8.001 (2009-09-09)
  9634. */
  9635. protected function _putAPXObject($w=0, $h=0, $stream='') {
  9636. $stream = trim($stream);
  9637. $out = $this->_getobj()."\n";
  9638. $this->xobjects['AX'.$this->n] = array('n' => $this->n);
  9639. $out .= '<<';
  9640. $out .= ' /Type /XObject';
  9641. $out .= ' /Subtype /Form';
  9642. $out .= ' /FormType 1';
  9643. if ($this->compress) {
  9644. $stream = gzcompress($stream);
  9645. $out .= ' /Filter /FlateDecode';
  9646. }
  9647. $rect = sprintf('%F %F', $w, $h);
  9648. $out .= ' /BBox [0 0 '.$rect.']';
  9649. $out .= ' /Matrix [1 0 0 1 0 0]';
  9650. $out .= ' /Resources 2 0 R';
  9651. $stream = $this->_getrawstream($stream);
  9652. $out .= ' /Length '.strlen($stream);
  9653. $out .= ' >>';
  9654. $out .= ' stream'."\n".$stream."\n".'endstream';
  9655. $out .= "\n".'endobj';
  9656. $this->_out($out);
  9657. return $this->n;
  9658. }
  9659. /**
  9660. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  9661. * @param $str (string) string from where to extract value
  9662. * @param $offset (int) point from where to read the data
  9663. * @return int 32 bit value
  9664. * @author Nicola Asuni
  9665. * @protected
  9666. * @since 5.2.000 (2010-06-02)
  9667. */
  9668. protected function _getULONG($str, $offset) {
  9669. $v = unpack('Ni', substr($str, $offset, 4));
  9670. return $v['i'];
  9671. }
  9672. /**
  9673. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  9674. * @param $str (string) string from where to extract value
  9675. * @param $offset (int) point from where to read the data
  9676. * @return int 16 bit value
  9677. * @author Nicola Asuni
  9678. * @protected
  9679. * @since 5.2.000 (2010-06-02)
  9680. */
  9681. protected function _getUSHORT($str, $offset) {
  9682. $v = unpack('ni', substr($str, $offset, 2));
  9683. return $v['i'];
  9684. }
  9685. /**
  9686. * Get SHORT from string (Big Endian 16-bit signed integer).
  9687. * @param $str (string) String from where to extract value.
  9688. * @param $offset (int) Point from where to read the data.
  9689. * @return int 16 bit value
  9690. * @author Nicola Asuni
  9691. * @protected
  9692. * @since 5.2.000 (2010-06-02)
  9693. */
  9694. protected function _getSHORT($str, $offset) {
  9695. $v = unpack('si', substr($str, $offset, 2));
  9696. return $v['i'];
  9697. }
  9698. /**
  9699. * Get FWORD from string (Big Endian 16-bit signed integer).
  9700. * @param $str (string) String from where to extract value.
  9701. * @param $offset (int) Point from where to read the data.
  9702. * @return int 16 bit value
  9703. * @author Nicola Asuni
  9704. * @protected
  9705. * @since 5.9.123 (2011-09-30)
  9706. */
  9707. protected function _getFWORD($str, $offset) {
  9708. $v = $this->_getUSHORT($str, $offset);
  9709. if ($v > 0x7fff) {
  9710. $v -= 0x10000;
  9711. }
  9712. return $v;
  9713. }
  9714. /**
  9715. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  9716. * @param $str (string) string from where to extract value
  9717. * @param $offset (int) point from where to read the data
  9718. * @return int 16 bit value
  9719. * @author Nicola Asuni
  9720. * @protected
  9721. * @since 5.9.123 (2011-09-30)
  9722. */
  9723. protected function _getUFWORD($str, $offset) {
  9724. $v = $this->_getUSHORT($str, $offset);
  9725. return $v;
  9726. }
  9727. /**
  9728. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  9729. * @param $str (string) string from where to extract value
  9730. * @param $offset (int) point from where to read the data
  9731. * @return int 16 bit value
  9732. * @author Nicola Asuni
  9733. * @protected
  9734. * @since 5.9.123 (2011-09-30)
  9735. */
  9736. protected function _getFIXED($str, $offset) {
  9737. // mantissa
  9738. $m = $this->_getFWORD($str, $offset);
  9739. // fraction
  9740. $f = $this->_getUSHORT($str, ($offset + 2));
  9741. $v = floatval(''.$m.'.'.$f.'');
  9742. return $v;
  9743. }
  9744. /**
  9745. * Get BYTE from string (8-bit unsigned integer).
  9746. * @param $str (string) String from where to extract value.
  9747. * @param $offset (int) Point from where to read the data.
  9748. * @return int 8 bit value
  9749. * @author Nicola Asuni
  9750. * @protected
  9751. * @since 5.2.000 (2010-06-02)
  9752. */
  9753. protected function _getBYTE($str, $offset) {
  9754. $v = unpack('Ci', substr($str, $offset, 1));
  9755. return $v['i'];
  9756. }
  9757. /**
  9758. * Update the CIDToGIDMap string with a new value.
  9759. * @param $map (string) CIDToGIDMap.
  9760. * @param $cid (int) CID value.
  9761. * @param $gid (int) GID value.
  9762. * @return (string) CIDToGIDMap.
  9763. * @author Nicola Asuni
  9764. * @protected
  9765. * @since 5.9.123 (2011-09-29)
  9766. */
  9767. protected function updateCIDtoGIDmap($map, $cid, $gid) {
  9768. if (($cid >= 0) AND ($cid <= 0xFFFF) AND ($gid >= 0)) {
  9769. if ($gid > 0xFFFF) {
  9770. $gid -= 0x10000;
  9771. }
  9772. $map[($cid * 2)] = chr($gid >> 8);
  9773. $map[(($cid * 2) + 1)] = chr($gid & 0xFF);
  9774. }
  9775. return $map;
  9776. }
  9777. /**
  9778. * Convert and add the selected TrueType or Type1 font to the fonts folder (that must be writeable).
  9779. * @param $fontfile (string) Font file (full path).
  9780. * @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.
  9781. * @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.
  9782. * @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.
  9783. * @param $outpath (string) Output path for generated font files (must be writeable by the web server). Leave empty for default font folder.
  9784. * @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).
  9785. * @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.
  9786. * @return (string) TCPDF font name.
  9787. * @author Nicola Asuni
  9788. * @public
  9789. * @since 5.9.123 (2010-09-30)
  9790. */
  9791. public function addTTFfont($fontfile, $fonttype='', $enc='', $flags=32, $outpath='', $platid=3, $encid=1) {
  9792. if (!file_exists($fontfile)) {
  9793. $this->Error('Could not find file: '.$fontfile.'');
  9794. }
  9795. // font metrics
  9796. $fmetric = array();
  9797. // build new font name for TCPDF compatibility
  9798. $font_path_parts = pathinfo($fontfile);
  9799. if (!isset($font_path_parts['filename'])) {
  9800. $font_path_parts['filename'] = substr($font_path_parts['basename'], 0, -(strlen($font_path_parts['extension']) + 1));
  9801. }
  9802. $font_name = strtolower($font_path_parts['filename']);
  9803. $font_name = preg_replace('/[^a-z0-9_]/', '', $font_name);
  9804. $search = array('bold', 'oblique', 'italic', 'regular');
  9805. $replace = array('b', 'i', 'i', '');
  9806. $font_name = str_replace($search, $replace, $font_name);
  9807. if (empty($font_name)) {
  9808. // set generic name
  9809. $font_name = 'tcpdffont';
  9810. }
  9811. // set output path
  9812. if (empty($outpath)) {
  9813. $outpath = $this->_getfontpath();
  9814. }
  9815. // check if this font already exist
  9816. if (file_exists($outpath.$font_name.'.php')) {
  9817. // this font already exist (delete it from fonts folder to rebuild it)
  9818. return $font_name;
  9819. }
  9820. $fmetric['file'] = $font_name.'.z';
  9821. $fmetric['ctg'] = $font_name.'.ctg.z';
  9822. // get font data
  9823. $font = file_get_contents($fontfile);
  9824. $fmetric['originalsize'] = strlen($font);
  9825. // autodetect font type
  9826. if (empty($fonttype)) {
  9827. if ($this->_getULONG($font, 0) == 0x10000) {
  9828. // True Type (Unicode or not)
  9829. $fonttype = 'TrueTypeUnicode';
  9830. } elseif (substr($font, 0, 4) == 'OTTO') {
  9831. // Open Type (Unicode or not)
  9832. $this->Error('Unsupported font format: OpenType with CFF data.');
  9833. } else {
  9834. // Type 1
  9835. $fonttype = 'Type1';
  9836. }
  9837. }
  9838. // set font type
  9839. switch ($fonttype) {
  9840. case 'CID0CT':
  9841. case 'CID0CS':
  9842. case 'CID0KR':
  9843. case 'CID0JP': {
  9844. $fmetric['type'] = 'cidfont0';
  9845. break;
  9846. }
  9847. case 'Type1': {
  9848. $fmetric['type'] = 'Type1';
  9849. if (empty($enc) AND (($flags & 4) == 0)) {
  9850. $enc = 'cp1252';
  9851. }
  9852. break;
  9853. }
  9854. case 'TrueType': {
  9855. $fmetric['type'] = 'TrueType';
  9856. break;
  9857. }
  9858. case 'TrueTypeUnicode':
  9859. default: {
  9860. $fmetric['type'] = 'TrueTypeUnicode';
  9861. break;
  9862. }
  9863. }
  9864. // set encoding maps (if any)
  9865. $fmetric['enc'] = preg_replace('/[^A-Za-z0-9_\-]/', '', $enc);
  9866. $fmetric['diff'] = '';
  9867. if (($fmetric['type'] == 'TrueType') OR ($fmetric['type'] == 'Type1')) {
  9868. if (!empty($enc) AND ($enc != 'cp1252') AND isset($this->encmaps->encmap[$enc])) {
  9869. // build differences from reference encoding
  9870. $enc_ref = $this->encmaps->encmap['cp1252'];
  9871. $enc_target = $this->encmaps->encmap[$enc];
  9872. $last = 0;
  9873. for ($i = 32; $i <= 255; ++$i) {
  9874. if ($enc_target != $enc_ref[$i]) {
  9875. if ($i != ($last + 1)) {
  9876. $fmetric['diff'] .= $i.' ';
  9877. }
  9878. $last = $i;
  9879. $fmetric['diff'] .= '/'.$enc_target[$i].' ';
  9880. }
  9881. }
  9882. }
  9883. }
  9884. // parse the font by type
  9885. if ($fmetric['type'] == 'Type1') {
  9886. // ---------- TYPE 1 ----------
  9887. // read first segment
  9888. $a = unpack('Cmarker/Ctype/Vsize', substr($font, 0, 6));
  9889. if ($a['marker'] != 128) {
  9890. $this->Error('Font file is not a valid binary Type1');
  9891. }
  9892. $fmetric['size1'] = $a['size'];
  9893. $data = substr($font, 6, $fmetric['size1']);
  9894. // read second segment
  9895. $a = unpack('Cmarker/Ctype/Vsize', substr($font, (6 + $fmetric['size1']), 6));
  9896. if ($a['marker'] != 128) {
  9897. $this->Error('Font file is not a valid binary Type1');
  9898. }
  9899. $fmetric['size2'] = $a['size'];
  9900. $encrypted = substr($font, (12 + $fmetric['size1']), $fmetric['size2']);
  9901. $data .= $encrypted;
  9902. // store compressed font
  9903. $fp = fopen($outpath.$fmetric['file'], 'wb');
  9904. fwrite($fp, gzcompress($data));
  9905. fclose($fp);
  9906. // get font info
  9907. $fmetric['Flags'] = $flags;
  9908. preg_match ('#/FullName[\s]*\(([^\)]*)#', $font, $matches);
  9909. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $matches[1]);
  9910. preg_match('#/FontBBox[\s]*{([^}]*)#', $font, $matches);
  9911. $fmetric['bbox'] = trim($matches[1]);
  9912. $bv = explode(' ', $fmetric['bbox']);
  9913. $fmetric['Ascent'] = intval($bv[3]);
  9914. $fmetric['Descent'] = intval($bv[1]);
  9915. preg_match('#/ItalicAngle[\s]*([0-9\+\-]*)#', $font, $matches);
  9916. $fmetric['italicAngle'] = intval($matches[1]);
  9917. if ($fmetric['italicAngle'] != 0) {
  9918. $fmetric['Flags'] |= 64;
  9919. }
  9920. preg_match('#/UnderlinePosition[\s]*([0-9\+\-]*)#', $font, $matches);
  9921. $fmetric['underlinePosition'] = intval($matches[1]);
  9922. preg_match('#/UnderlineThickness[\s]*([0-9\+\-]*)#', $font, $matches);
  9923. $fmetric['underlineThickness'] = intval($matches[1]);
  9924. preg_match('#/isFixedPitch[\s]*([^\s]*)#', $font, $matches);
  9925. if ($matches[1] == 'true') {
  9926. $fmetric['Flags'] |= 1;
  9927. }
  9928. // get internal map
  9929. $imap = array();
  9930. if (preg_match_all('#dup[\s]([0-9]+)[\s]*/([^\s]*)[\s]put#sU', $font, $fmap, PREG_SET_ORDER) > 0) {
  9931. foreach ($fmap as $v) {
  9932. $imap[$v[2]] = $v[1];
  9933. }
  9934. }
  9935. // decrypt eexec encrypted part
  9936. $r = 55665; // eexec encryption constant
  9937. $c1 = 52845;
  9938. $c2 = 22719;
  9939. $elen = strlen($encrypted);
  9940. $eplain = '';
  9941. for ($i = 0; $i < $elen; ++$i) {
  9942. $chr = ord($encrypted[$i]);
  9943. $eplain .= chr($chr ^ ($r >> 8));
  9944. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  9945. }
  9946. if (preg_match('#/ForceBold[\s]*([^\s]*)#', $eplain, $matches) > 0) {
  9947. if ($matches[1] == 'true') {
  9948. $fmetric['Flags'] |= 0x40000;
  9949. }
  9950. }
  9951. if (preg_match('#/StdVW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9952. $fmetric['StemV'] = intval($matches[1]);
  9953. } else {
  9954. $fmetric['StemV'] = 70;
  9955. }
  9956. if (preg_match('#/StdHW[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9957. $fmetric['StemH'] = intval($matches[1]);
  9958. } else {
  9959. $fmetric['StemH'] = 30;
  9960. }
  9961. if (preg_match('#/BlueValues[\s]*\[([^\]]*)#', $eplain, $matches) > 0) {
  9962. $bv = explode(' ', $matches[1]);
  9963. if (count($bv) >= 6) {
  9964. $v1 = intval($bv[2]);
  9965. $v2 = intval($bv[4]);
  9966. if ($v1 <= $v2) {
  9967. $fmetric['XHeight'] = $v1;
  9968. $fmetric['CapHeight'] = $v2;
  9969. } else {
  9970. $fmetric['XHeight'] = $v2;
  9971. $fmetric['CapHeight'] = $v1;
  9972. }
  9973. } else {
  9974. $fmetric['XHeight'] = 450;
  9975. $fmetric['CapHeight'] = 700;
  9976. }
  9977. } else {
  9978. $fmetric['XHeight'] = 450;
  9979. $fmetric['CapHeight'] = 700;
  9980. }
  9981. // get the number of random bytes at the beginning of charstrings
  9982. if (preg_match('#/lenIV[\s]*([0-9]*)#', $eplain, $matches) > 0) {
  9983. $lenIV = intval($matches[1]);
  9984. } else {
  9985. $lenIV = 4;
  9986. }
  9987. $fmetric['Leading'] = 0;
  9988. // get charstring data
  9989. $eplain = substr($eplain, (strpos($eplain, '/CharStrings') + 1));
  9990. preg_match_all('#/([A-Za-z0-9\.]*)[\s][0-9]+[\s]RD[\s](.*)[\s]ND#sU', $eplain, $matches, PREG_SET_ORDER);
  9991. if (!empty($enc) AND isset($this->encmaps->encmap[$enc])) {
  9992. $enc_map = $this->encmaps->encmap[$enc];
  9993. } else {
  9994. $enc_map = false;
  9995. }
  9996. $fmetric['cw'] = '';
  9997. $fmetric['MaxWidth'] = 0;
  9998. $cwidths = array();
  9999. foreach ($matches as $k => $v) {
  10000. $cid = 0;
  10001. if (isset($imap[$v[1]])) {
  10002. $cid = $imap[$v[1]];
  10003. } elseif ($enc_map !== false) {
  10004. $cid = array_search($v[1], $enc_map);
  10005. if ($cid === false) {
  10006. $cid = 0;
  10007. } elseif ($cid > 1000) {
  10008. $cid -= 1000;
  10009. }
  10010. }
  10011. // decrypt charstring encrypted part
  10012. $r = 4330; // charstring encryption constant
  10013. $c1 = 52845;
  10014. $c2 = 22719;
  10015. $cd = $v[2];
  10016. $clen = strlen($cd);
  10017. $ccom = array();
  10018. for ($i = 0; $i < $clen; ++$i) {
  10019. $chr = ord($cd[$i]);
  10020. $ccom[] = ($chr ^ ($r >> 8));
  10021. $r = ((($chr + $r) * $c1 + $c2) % 65536);
  10022. }
  10023. // decode numbers
  10024. $cdec = array();
  10025. $ck = 0;
  10026. $i = $lenIV;
  10027. while ($i < $clen) {
  10028. if ($ccom[$i] < 32) {
  10029. $cdec[$ck] = $ccom[$i];
  10030. if (($ck > 0) AND ($cdec[$ck] == 13)) {
  10031. // hsbw command: update width
  10032. $cwidths[$cid] = $cdec[($ck - 1)];
  10033. }
  10034. ++$i;
  10035. } elseif (($ccom[$i] >= 32) AND ($ccom[$i] <= 246)) {
  10036. $cdec[$ck] = ($ccom[$i] - 139);
  10037. ++$i;
  10038. } elseif (($ccom[$i] >= 247) AND ($ccom[$i] <= 250)) {
  10039. $cdec[$ck] = ((($ccom[$i] - 247) * 256) + $ccom[($i + 1)] + 108);
  10040. $i += 2;
  10041. } elseif (($ccom[$i] >= 251) AND ($ccom[$i] <= 254)) {
  10042. $cdec[$ck] = ((-($ccom[$i] - 251) * 256) - $ccom[($i + 1)] - 108);
  10043. $i += 2;
  10044. } elseif ($ccom[$i] == 255) {
  10045. $sval = chr($ccom[($i + 1)]).chr($ccom[($i + 2)]).chr($ccom[($i + 3)]).chr($ccom[($i + 4)]);
  10046. $vsval = unpack('li', $sval);
  10047. $cdec[$ck] = $vsval['i'];
  10048. $i += 5;
  10049. }
  10050. ++$ck;
  10051. }
  10052. } // end for each matches
  10053. $fmetric['MissingWidth'] = $cwidths[0];
  10054. $fmetric['MaxWidth'] = $fmetric['MissingWidth'];
  10055. $fmetric['AvgWidth'] = 0;
  10056. // set chars widths
  10057. for ($cid = 0; $cid <= 255; ++$cid) {
  10058. if (isset($cwidths[$cid])) {
  10059. if ($cwidths[$cid] > $fmetric['MaxWidth']) {
  10060. $fmetric['MaxWidth'] = $cwidths[$cid];
  10061. }
  10062. $fmetric['AvgWidth'] += $cwidths[$cid];
  10063. $fmetric['cw'] .= ','.$cid.'=>'.$cwidths[$cid];
  10064. } else {
  10065. $fmetric['cw'] .= ','.$cid.'=>'.$fmetric['MissingWidth'];
  10066. }
  10067. }
  10068. $fmetric['AvgWidth'] = round($fmetric['AvgWidth'] / count($cwidths));
  10069. } else {
  10070. // ---------- TRUE TYPE ----------
  10071. if ($fmetric['type'] != 'cidfont0') {
  10072. // store compressed font
  10073. $fp = fopen($outpath.$fmetric['file'], 'wb');
  10074. fwrite($fp, gzcompress($font));
  10075. fclose($fp);
  10076. }
  10077. $offset = 0; // offset position of the font data
  10078. if ($this->_getULONG($font, $offset) != 0x10000) {
  10079. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10080. return $font;
  10081. }
  10082. $offset += 4;
  10083. // get number of tables
  10084. $numTables = $this->_getUSHORT($font, $offset);
  10085. $offset += 2;
  10086. // skip searchRange, entrySelector and rangeShift
  10087. $offset += 6;
  10088. // tables array
  10089. $table = array();
  10090. // ---------- get tables ----------
  10091. for ($i = 0; $i < $numTables; ++$i) {
  10092. // get table info
  10093. $tag = substr($font, $offset, 4);
  10094. $offset += 4;
  10095. $table[$tag] = array();
  10096. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10097. $offset += 4;
  10098. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10099. $offset += 4;
  10100. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10101. $offset += 4;
  10102. }
  10103. // check magicNumber
  10104. $offset = $table['head']['offset'] + 12;
  10105. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10106. // magicNumber must be 0x5F0F3CF5
  10107. return $font;
  10108. }
  10109. $offset += 4;
  10110. $offset += 2; // skip flags
  10111. // get FUnits
  10112. $fmetric['unitsPerEm'] = $this->_getUSHORT($font, $offset);
  10113. $offset += 2;
  10114. // units ratio constant
  10115. $urk = (1000 / $fmetric['unitsPerEm']);
  10116. $offset += 16; // skip created, modified
  10117. $xMin = round($this->_getFWORD($font, $offset) * $urk);
  10118. $offset += 2;
  10119. $yMin = round($this->_getFWORD($font, $offset) * $urk);
  10120. $offset += 2;
  10121. $xMax = round($this->_getFWORD($font, $offset) * $urk);
  10122. $offset += 2;
  10123. $yMax = round($this->_getFWORD($font, $offset) * $urk);
  10124. $offset += 2;
  10125. $fmetric['bbox'] = ''.$xMin.' '.$yMin.' '.$xMax.' '.$yMax.'';
  10126. $macStyle = $this->_getUSHORT($font, $offset);
  10127. $offset += 2;
  10128. // PDF font flags
  10129. $fmetric['Flags'] = $flags;
  10130. if (($macStyle & 2) == 2) {
  10131. // italic flag
  10132. $fmetric['Flags'] |= 64;
  10133. }
  10134. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10135. $offset = $table['head']['offset'] + 50;
  10136. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10137. $offset += 2;
  10138. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10139. $indexToLoc = array();
  10140. $offset = $table['loca']['offset'];
  10141. if ($short_offset) {
  10142. // short version
  10143. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10144. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10145. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10146. $offset += 2;
  10147. }
  10148. } else {
  10149. // long version
  10150. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10151. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10152. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10153. $offset += 4;
  10154. }
  10155. }
  10156. // get glyphs indexes of chars from cmap table
  10157. $offset = $table['cmap']['offset'] + 2;
  10158. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10159. $offset += 2;
  10160. $encodingTables = array();
  10161. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10162. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10163. $offset += 2;
  10164. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10165. $offset += 2;
  10166. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10167. $offset += 4;
  10168. }
  10169. // ---------- get os/2 metrics ----------
  10170. $offset = $table['OS/2']['offset'];
  10171. $offset += 2; // skip version
  10172. // xAvgCharWidth
  10173. $fmetric['AvgWidth'] = round($this->_getFWORD($font, $offset) * $urk);
  10174. $offset += 2;
  10175. // usWeightClass
  10176. $usWeightClass = round($this->_getUFWORD($font, $offset) * $urk);
  10177. // estimate StemV and StemH (400 = usWeightClass for Normal - Regular font)
  10178. $fmetric['StemV'] = round((70 * $usWeightClass) / 400);
  10179. $fmetric['StemH'] = round((30 * $usWeightClass) / 400);
  10180. $offset += 2;
  10181. $offset += 2; // usWidthClass
  10182. $fsType = $this->_getSHORT($font, $offset);
  10183. $offset += 2;
  10184. if ($fsType == 2) {
  10185. $this->Error('This Font cannot be modified, embedded or exchanged in any manner without first obtaining permission of the legal owner.');
  10186. }
  10187. // ---------- get font name ----------
  10188. $fmetric['name'] = '';
  10189. $offset = $table['name']['offset'];
  10190. $offset += 2; // skip Format selector (=0).
  10191. // Number of NameRecords that follow n.
  10192. $numNameRecords = $this->_getUSHORT($font, $offset);
  10193. $offset += 2;
  10194. // Offset to start of string storage (from start of table).
  10195. $stringStorageOffset = $this->_getUSHORT($font, $offset);
  10196. $offset += 2;
  10197. for ($i = 0; $i < $numNameRecords; ++$i) {
  10198. $offset += 6; // skip Platform ID, Platform-specific encoding ID, Language ID.
  10199. // Name ID.
  10200. $nameID = $this->_getUSHORT($font, $offset);
  10201. $offset += 2;
  10202. if ($nameID == 6) {
  10203. // String length (in bytes).
  10204. $stringLength = $this->_getUSHORT($font, $offset);
  10205. $offset += 2;
  10206. // String offset from start of storage area (in bytes).
  10207. $stringOffset = $this->_getUSHORT($font, $offset);
  10208. $offset += 2;
  10209. $offset = ($table['name']['offset'] + $stringStorageOffset + $stringOffset);
  10210. $fmetric['name'] = substr($font, $offset, $stringLength);
  10211. $fmetric['name'] = preg_replace('/[^a-zA-Z0-9_\-]/', '', $fmetric['name']);
  10212. break;
  10213. } else {
  10214. $offset += 4; // skip String length, String offset
  10215. }
  10216. }
  10217. if (empty($fmetric['name'])) {
  10218. $fmetric['name'] = $font_name;
  10219. }
  10220. // ---------- get post data ----------
  10221. $offset = $table['post']['offset'];
  10222. $offset += 4; // skip Format Type
  10223. $fmetric['italicAngle'] = $this->_getFIXED($font, $offset);
  10224. $offset += 4;
  10225. $fmetric['underlinePosition'] = round($this->_getFWORD($font, $offset) * $urk);
  10226. $offset += 2;
  10227. $fmetric['underlineThickness'] = round($this->_getFWORD($font, $offset) * $urk);
  10228. $offset += 2;
  10229. $isFixedPitch = ($this->_getULONG($font, $offset) == 0) ? false : true;
  10230. $offset += 2;
  10231. if ($isFixedPitch) {
  10232. $fmetric['Flags'] |= 1;
  10233. }
  10234. // ---------- get hhea data ----------
  10235. $offset = $table['hhea']['offset'];
  10236. $offset += 4; // skip Table version number
  10237. // Ascender
  10238. $fmetric['Ascent'] = round($this->_getFWORD($font, $offset) * $urk);
  10239. $offset += 2;
  10240. // Descender
  10241. $fmetric['Descent'] = round($this->_getFWORD($font, $offset) * $urk);
  10242. $offset += 2;
  10243. // LineGap
  10244. $fmetric['Leading'] = round($this->_getFWORD($font, $offset) * $urk);
  10245. $offset += 2;
  10246. // advanceWidthMax
  10247. $fmetric['MaxWidth'] = round($this->_getUFWORD($font, $offset) * $urk);
  10248. $offset += 2;
  10249. $offset += 22; // skip some values
  10250. // get the number of hMetric entries in hmtx table
  10251. $numberOfHMetrics = $this->_getUSHORT($font, $offset);
  10252. // ---------- get maxp data ----------
  10253. $offset = $table['maxp']['offset'];
  10254. $offset += 4; // skip Table version number
  10255. // get the the number of glyphs in the font.
  10256. $numGlyphs = $this->_getUSHORT($font, $offset);
  10257. // ---------- get CIDToGIDMap ----------
  10258. $ctg = array();
  10259. foreach ($encodingTables as $enctable) {
  10260. // get only specified Platform ID and Encoding ID
  10261. if (($enctable['platformID'] == $platid) AND ($enctable['encodingID'] == $encid)) {
  10262. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10263. $format = $this->_getUSHORT($font, $offset);
  10264. $offset += 2;
  10265. switch ($format) {
  10266. case 0: { // Format 0: Byte encoding table
  10267. $offset += 4; // skip length and version/language
  10268. for ($c = 0; $c < 256; ++$c) {
  10269. $g = $this->_getBYTE($font, $offset);
  10270. $ctg[$c] = $g;
  10271. ++$offset;
  10272. }
  10273. break;
  10274. }
  10275. case 2: { // Format 2: High-byte mapping through table
  10276. $offset += 4; // skip length and version/language
  10277. $numSubHeaders = 0;
  10278. for ($i = 0; $i < 256; ++$i) {
  10279. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10280. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10281. $offset += 2;
  10282. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10283. $numSubHeaders = $subHeaderKeys[$i];
  10284. }
  10285. }
  10286. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10287. ++$numSubHeaders;
  10288. // read subHeader structures
  10289. $subHeaders = array();
  10290. $numGlyphIndexArray = 0;
  10291. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10292. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10293. $offset += 2;
  10294. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10295. $offset += 2;
  10296. $subHeaders[$k]['idDelta'] = $this->_getUSHORT($font, $offset);
  10297. $offset += 2;
  10298. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10299. $offset += 2;
  10300. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10301. $subHeaders[$k]['idRangeOffset'] /= 2;
  10302. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10303. }
  10304. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10305. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10306. $offset += 2;
  10307. }
  10308. for ($i = 0; $i < 256; ++$i) {
  10309. $k = $subHeaderKeys[$i];
  10310. if ($k == 0) {
  10311. // one byte code
  10312. $c = $i;
  10313. $g = $glyphIndexArray[0];
  10314. $ctg[$c] = $g;
  10315. } else {
  10316. // two bytes code
  10317. $start_byte = $subHeaders[$k]['firstCode'];
  10318. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10319. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10320. // combine high and low bytes
  10321. $c = (($i << 8) + $j);
  10322. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10323. $g = ($glyphIndexArray[$idRangeOffset] + $idDelta[$k]) % 65536;
  10324. if ($g < 0) {
  10325. $g = 0;
  10326. }
  10327. $ctg[$c] = $g;
  10328. }
  10329. }
  10330. }
  10331. break;
  10332. }
  10333. case 4: { // Format 4: Segment mapping to delta values
  10334. $length = $this->_getUSHORT($font, $offset);
  10335. $offset += 2;
  10336. $offset += 2; // skip version/language
  10337. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10338. $offset += 2;
  10339. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10340. $endCount = array(); // array of end character codes for each segment
  10341. for ($k = 0; $k < $segCount; ++$k) {
  10342. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10343. $offset += 2;
  10344. }
  10345. $offset += 2; // skip reservedPad
  10346. $startCount = array(); // array of start character codes for each segment
  10347. for ($k = 0; $k < $segCount; ++$k) {
  10348. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10349. $offset += 2;
  10350. }
  10351. $idDelta = array(); // delta for all character codes in segment
  10352. for ($k = 0; $k < $segCount; ++$k) {
  10353. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10354. $offset += 2;
  10355. }
  10356. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10357. for ($k = 0; $k < $segCount; ++$k) {
  10358. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10359. $offset += 2;
  10360. }
  10361. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10362. $glyphIdArray = array(); // glyph index array
  10363. for ($k = 0; $k < $gidlen; ++$k) {
  10364. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10365. $offset += 2;
  10366. }
  10367. for ($k = 0; $k < $segCount; ++$k) {
  10368. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10369. if ($idRangeOffset[$k] == 0) {
  10370. $g = ($idDelta[$k] + $c) % 65536;
  10371. } else {
  10372. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10373. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  10374. }
  10375. if ($g < 0) {
  10376. $g = 0;
  10377. }
  10378. $ctg[$c] = $g;
  10379. }
  10380. }
  10381. break;
  10382. }
  10383. case 6: { // Format 6: Trimmed table mapping
  10384. $offset += 4; // skip length and version/language
  10385. $firstCode = $this->_getUSHORT($font, $offset);
  10386. $offset += 2;
  10387. $entryCount = $this->_getUSHORT($font, $offset);
  10388. $offset += 2;
  10389. for ($k = 0; $k < $entryCount; ++$k) {
  10390. $c = ($k + $firstCode);
  10391. $g = $this->_getUSHORT($font, $offset);
  10392. $offset += 2;
  10393. $ctg[$c] = $g;
  10394. }
  10395. break;
  10396. }
  10397. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10398. $offset += 10; // skip reserved, length and version/language
  10399. for ($k = 0; $k < 8192; ++$k) {
  10400. $is32[$k] = $this->_getBYTE($font, $offset);
  10401. ++$offset;
  10402. }
  10403. $nGroups = $this->_getULONG($font, $offset);
  10404. $offset += 4;
  10405. for ($i = 0; $i < $nGroups; ++$i) {
  10406. $startCharCode = $this->_getULONG($font, $offset);
  10407. $offset += 4;
  10408. $endCharCode = $this->_getULONG($font, $offset);
  10409. $offset += 4;
  10410. $startGlyphID = $this->_getULONG($font, $offset);
  10411. $offset += 4;
  10412. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10413. $is32idx = floor($c / 8);
  10414. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10415. $c = $k;
  10416. } else {
  10417. // 32 bit format
  10418. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10419. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10420. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10421. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10422. }
  10423. $ctg[$c] = 0;
  10424. ++$startGlyphID;
  10425. }
  10426. }
  10427. break;
  10428. }
  10429. case 10: { // Format 10: Trimmed array
  10430. $offset += 10; // skip reserved, length and version/language
  10431. $startCharCode = $this->_getULONG($font, $offset);
  10432. $offset += 4;
  10433. $numChars = $this->_getULONG($font, $offset);
  10434. $offset += 4;
  10435. for ($k = 0; $k < $numChars; ++$k) {
  10436. $c = ($k + $startCharCode);
  10437. $g = $this->_getUSHORT($font, $offset);
  10438. $ctg[$c] = $g;
  10439. $offset += 2;
  10440. }
  10441. break;
  10442. }
  10443. case 12: { // Format 12: Segmented coverage
  10444. $offset += 10; // skip length and version/language
  10445. $nGroups = $this->_getULONG($font, $offset);
  10446. $offset += 4;
  10447. for ($k = 0; $k < $nGroups; ++$k) {
  10448. $startCharCode = $this->_getULONG($font, $offset);
  10449. $offset += 4;
  10450. $endCharCode = $this->_getULONG($font, $offset);
  10451. $offset += 4;
  10452. $startGlyphCode = $this->_getULONG($font, $offset);
  10453. $offset += 4;
  10454. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10455. $ctg[$c] = $startGlyphCode;
  10456. ++$startGlyphCode;
  10457. }
  10458. }
  10459. break;
  10460. }
  10461. case 13: { // Format 13: Many-to-one range mappings
  10462. // to be implemented ...
  10463. break;
  10464. }
  10465. case 14: { // Format 14: Unicode Variation Sequences
  10466. // to be implemented ...
  10467. break;
  10468. }
  10469. }
  10470. }
  10471. }
  10472. if (!isset($ctg[0])) {
  10473. $ctg[0] = 0;
  10474. }
  10475. // get xHeight (height of x)
  10476. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[120]] + 4);
  10477. $yMin = $this->_getFWORD($font, $offset);
  10478. $offset += 4;
  10479. $yMax = $this->_getFWORD($font, $offset);
  10480. $offset += 2;
  10481. $fmetric['XHeight'] = round(($yMax - $yMin) * $urk);
  10482. // get CapHeight (height of H)
  10483. $offset = ($table['glyf']['offset'] + $indexToLoc[$ctg[72]] + 4);
  10484. $yMin = $this->_getFWORD($font, $offset);
  10485. $offset += 4;
  10486. $yMax = $this->_getFWORD($font, $offset);
  10487. $offset += 2;
  10488. $fmetric['CapHeight'] = round(($yMax - $yMin) * $urk);
  10489. // ceate widths array
  10490. $cw = array();
  10491. $offset = $table['hmtx']['offset'];
  10492. for ($i = 0 ; $i < $numberOfHMetrics; ++$i) {
  10493. $cw[$i] = round($this->_getUFWORD($font, $offset) * $urk);
  10494. $offset += 4; // skip lsb
  10495. }
  10496. if ($numberOfHMetrics < $numGlyphs) {
  10497. // fill missing widths with the last value
  10498. $cw = array_pad($cw, $numGlyphs, $cw[($numberOfHMetrics - 1)]);
  10499. }
  10500. $fmetric['MissingWidth'] = $cw[0];
  10501. $fmetric['cw'] = '';
  10502. for ($cid = 0; $cid <= 65535; ++$cid) {
  10503. if (isset($ctg[$cid]) AND isset($cw[$ctg[$cid]])) {
  10504. $fmetric['cw'] .= ','.$cid.'=>'.$cw[$ctg[$cid]];
  10505. }
  10506. }
  10507. } // end of true type
  10508. if (($fmetric['type'] == 'TrueTypeUnicode') AND (count($ctg) == 256)) {
  10509. $fmetric['type'] == 'TrueType';
  10510. }
  10511. // ---------- create php font file ----------
  10512. $pfile = '<'.'?'.'php'."\n";
  10513. $pfile .= '// TCPDF FONT FILE DESCRIPTION'."\n";
  10514. $pfile .= '$type=\''.$fmetric['type'].'\';'."\n";
  10515. $pfile .= '$name=\''.$fmetric['name'].'\';'."\n";
  10516. $pfile .= '$up='.$fmetric['underlinePosition'].';'."\n";
  10517. $pfile .= '$ut='.$fmetric['underlineThickness'].';'."\n";
  10518. if ($fmetric['MissingWidth'] > 0) {
  10519. $pfile .= '$dw='.$fmetric['MissingWidth'].';'."\n";
  10520. } else {
  10521. $pfile .= '$dw='.$fmetric['AvgWidth'].';'."\n";
  10522. }
  10523. $pfile .= '$diff=\''.$fmetric['diff'].'\';'."\n";
  10524. if ($fmetric['type'] == 'Type1') {
  10525. // Type 1
  10526. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10527. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10528. $pfile .= '$size1='.$fmetric['size1'].';'."\n";
  10529. $pfile .= '$size2='.$fmetric['size2'].';'."\n";
  10530. } else {
  10531. $pfile .= '$originalsize='.$fmetric['originalsize'].';'."\n";
  10532. if ($fmetric['type'] == 'cidfont0') {
  10533. // CID-0
  10534. switch ($fonttype) {
  10535. case 'CID0JP': {
  10536. $pfile .= '// Japanese'."\n";
  10537. $pfile .= '$enc=\'UniJIS-UTF16-H\';'."\n";
  10538. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Japan1\',\'Supplement\'=>5);'."\n";
  10539. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10540. break;
  10541. }
  10542. case 'CID0KR': {
  10543. $pfile .= '// Korean'."\n";
  10544. $pfile .= '$enc=\'UniKS-UTF16-H\';'."\n";
  10545. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'Korea1\',\'Supplement\'=>0);'."\n";
  10546. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ak12.php\');'."\n";
  10547. break;
  10548. }
  10549. case 'CID0CS': {
  10550. $pfile .= '// Chinese Simplified'."\n";
  10551. $pfile .= '$enc=\'UniGB-UTF16-H\';'."\n";
  10552. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'GB1\',\'Supplement\'=>2);'."\n";
  10553. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_ag15.php\');'."\n";
  10554. break;
  10555. }
  10556. case 'CID0CT':
  10557. default: {
  10558. $pfile .= '// Chinese Traditional'."\n";
  10559. $pfile .= '$enc=\'UniCNS-UTF16-H\';'."\n";
  10560. $pfile .= '$cidinfo=array(\'Registry\'=>\'Adobe\', \'Ordering\'=>\'CNS1\',\'Supplement\'=>0);'."\n";
  10561. $pfile .= 'include(dirname(__FILE__).\'/uni2cid_aj16.php\');'."\n";
  10562. break;
  10563. }
  10564. }
  10565. } else {
  10566. // TrueType
  10567. $pfile .= '$enc=\''.$fmetric['enc'].'\';'."\n";
  10568. $pfile .= '$file=\''.$fmetric['file'].'\';'."\n";
  10569. $pfile .= '$ctg=\''.$fmetric['ctg'].'\';'."\n";
  10570. // create CIDToGIDMap
  10571. $cidtogidmap = str_pad('', 131072, "\x00"); // (256 * 256 * 2) = 131072
  10572. foreach ($ctg as $cid => $gid) {
  10573. $cidtogidmap = $this->updateCIDtoGIDmap($cidtogidmap, $cid, $ctg[$cid]);
  10574. }
  10575. // store compressed CIDToGIDMap
  10576. $fp = fopen($outpath.$fmetric['ctg'], 'wb');
  10577. fwrite($fp, gzcompress($cidtogidmap));
  10578. fclose($fp);
  10579. }
  10580. }
  10581. $pfile .= '$desc=array(';
  10582. $pfile .= '\'Flags\'=>'.$fmetric['Flags'].',';
  10583. $pfile .= '\'FontBBox\'=>\'['.$fmetric['bbox'].']\',';
  10584. $pfile .= '\'ItalicAngle\'=>'.$fmetric['italicAngle'].',';
  10585. $pfile .= '\'Ascent\'=>'.$fmetric['Ascent'].',';
  10586. $pfile .= '\'Descent\'=>'.$fmetric['Descent'].',';
  10587. $pfile .= '\'Leading\'=>'.$fmetric['Leading'].',';
  10588. $pfile .= '\'CapHeight\'=>'.$fmetric['CapHeight'].',';
  10589. $pfile .= '\'XHeight\'=>'.$fmetric['XHeight'].',';
  10590. $pfile .= '\'StemV\'=>'.$fmetric['StemV'].',';
  10591. $pfile .= '\'StemH\'=>'.$fmetric['StemH'].',';
  10592. $pfile .= '\'AvgWidth\'=>'.$fmetric['AvgWidth'].',';
  10593. $pfile .= '\'MaxWidth\'=>'.$fmetric['MaxWidth'].',';
  10594. $pfile .= '\'MissingWidth\'=>'.$fmetric['MissingWidth'].'';
  10595. $pfile .= ');'."\n";
  10596. $pfile .= '$cw=array('.substr($fmetric['cw'], 1).');'."\n";
  10597. $pfile .= '// --- EOF ---'."\n";
  10598. // store file
  10599. $fp = fopen($outpath.$font_name.'.php', 'w');
  10600. fwrite($fp, $pfile);
  10601. fclose($fp);
  10602. // return TCPDF font name
  10603. return $font_name;
  10604. }
  10605. /**
  10606. * Returns a subset of the TrueType font data without the unused glyphs.
  10607. * @param $font (string) TrueType font data.
  10608. * @param $subsetchars (array) Array of used characters (the glyphs to keep).
  10609. * @return (string) A subset of TrueType font data without the unused glyphs.
  10610. * @author Nicola Asuni
  10611. * @protected
  10612. * @since 5.2.000 (2010-06-02)
  10613. */
  10614. protected function _getTrueTypeFontSubset($font, $subsetchars) {
  10615. ksort($subsetchars);
  10616. $offset = 0; // offset position of the font data
  10617. if ($this->_getULONG($font, $offset) != 0x10000) {
  10618. // sfnt version must be 0x00010000 for TrueType version 1.0.
  10619. return $font;
  10620. }
  10621. $offset += 4;
  10622. // get number of tables
  10623. $numTables = $this->_getUSHORT($font, $offset);
  10624. $offset += 2;
  10625. // skip searchRange, entrySelector and rangeShift
  10626. $offset += 6;
  10627. // tables array
  10628. $table = array();
  10629. // for each table
  10630. for ($i = 0; $i < $numTables; ++$i) {
  10631. // get table info
  10632. $tag = substr($font, $offset, 4);
  10633. $offset += 4;
  10634. $table[$tag] = array();
  10635. $table[$tag]['checkSum'] = $this->_getULONG($font, $offset);
  10636. $offset += 4;
  10637. $table[$tag]['offset'] = $this->_getULONG($font, $offset);
  10638. $offset += 4;
  10639. $table[$tag]['length'] = $this->_getULONG($font, $offset);
  10640. $offset += 4;
  10641. }
  10642. // check magicNumber
  10643. $offset = $table['head']['offset'] + 12;
  10644. if ($this->_getULONG($font, $offset) != 0x5F0F3CF5) {
  10645. // magicNumber must be 0x5F0F3CF5
  10646. return $font;
  10647. }
  10648. $offset += 4;
  10649. // get offset mode (indexToLocFormat : 0 = short, 1 = long)
  10650. $offset = $table['head']['offset'] + 50;
  10651. $short_offset = ($this->_getSHORT($font, $offset) == 0);
  10652. $offset += 2;
  10653. // get the offsets to the locations of the glyphs in the font, relative to the beginning of the glyphData table
  10654. $indexToLoc = array();
  10655. $offset = $table['loca']['offset'];
  10656. if ($short_offset) {
  10657. // short version
  10658. $tot_num_glyphs = ($table['loca']['length'] / 2); // numGlyphs + 1
  10659. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10660. $indexToLoc[$i] = $this->_getUSHORT($font, $offset) * 2;
  10661. $offset += 2;
  10662. }
  10663. } else {
  10664. // long version
  10665. $tot_num_glyphs = ($table['loca']['length'] / 4); // numGlyphs + 1
  10666. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10667. $indexToLoc[$i] = $this->_getULONG($font, $offset);
  10668. $offset += 4;
  10669. }
  10670. }
  10671. // get glyphs indexes of chars from cmap table
  10672. $subsetglyphs = array(); // glyph IDs on key
  10673. $subsetglyphs[0] = true; // character codes that do not correspond to any glyph in the font should be mapped to glyph index 0
  10674. $offset = $table['cmap']['offset'] + 2;
  10675. $numEncodingTables = $this->_getUSHORT($font, $offset);
  10676. $offset += 2;
  10677. $encodingTables = array();
  10678. for ($i = 0; $i < $numEncodingTables; ++$i) {
  10679. $encodingTables[$i]['platformID'] = $this->_getUSHORT($font, $offset);
  10680. $offset += 2;
  10681. $encodingTables[$i]['encodingID'] = $this->_getUSHORT($font, $offset);
  10682. $offset += 2;
  10683. $encodingTables[$i]['offset'] = $this->_getULONG($font, $offset);
  10684. $offset += 4;
  10685. }
  10686. foreach ($encodingTables as $enctable) {
  10687. // get all platforms and encodings
  10688. $offset = $table['cmap']['offset'] + $enctable['offset'];
  10689. $format = $this->_getUSHORT($font, $offset);
  10690. $offset += 2;
  10691. switch ($format) {
  10692. case 0: { // Format 0: Byte encoding table
  10693. $offset += 4; // skip length and version/language
  10694. for ($c = 0; $c < 256; ++$c) {
  10695. if (isset($subsetchars[$c])) {
  10696. $g = $this->_getBYTE($font, $offset);
  10697. $subsetglyphs[$g] = true;
  10698. }
  10699. ++$offset;
  10700. }
  10701. break;
  10702. }
  10703. case 2: { // Format 2: High-byte mapping through table
  10704. $offset += 4; // skip length and version/language
  10705. $numSubHeaders = 0;
  10706. for ($i = 0; $i < 256; ++$i) {
  10707. // Array that maps high bytes to subHeaders: value is subHeader index * 8.
  10708. $subHeaderKeys[$i] = ($this->_getUSHORT($font, $offset) / 8);
  10709. $offset += 2;
  10710. if ($numSubHeaders < $subHeaderKeys[$i]) {
  10711. $numSubHeaders = $subHeaderKeys[$i];
  10712. }
  10713. }
  10714. // the number of subHeaders is equal to the max of subHeaderKeys + 1
  10715. ++$numSubHeaders;
  10716. // read subHeader structures
  10717. $subHeaders = array();
  10718. $numGlyphIndexArray = 0;
  10719. for ($k = 0; $k < $numSubHeaders; ++$k) {
  10720. $subHeaders[$k]['firstCode'] = $this->_getUSHORT($font, $offset);
  10721. $offset += 2;
  10722. $subHeaders[$k]['entryCount'] = $this->_getUSHORT($font, $offset);
  10723. $offset += 2;
  10724. $subHeaders[$k]['idDelta'] = $this->_getUSHORT($font, $offset);
  10725. $offset += 2;
  10726. $subHeaders[$k]['idRangeOffset'] = $this->_getUSHORT($font, $offset);
  10727. $offset += 2;
  10728. $subHeaders[$k]['idRangeOffset'] -= (2 + (($numSubHeaders - $k - 1) * 8));
  10729. $subHeaders[$k]['idRangeOffset'] /= 2;
  10730. $numGlyphIndexArray += $subHeaders[$k]['entryCount'];
  10731. }
  10732. for ($k = 0; $k < $numGlyphIndexArray; ++$k) {
  10733. $glyphIndexArray[$k] = $this->_getUSHORT($font, $offset);
  10734. $offset += 2;
  10735. }
  10736. for ($i = 0; $i < 256; ++$i) {
  10737. $k = $subHeaderKeys[$i];
  10738. if ($k == 0) {
  10739. // one byte code
  10740. $c = $i;
  10741. if (isset($subsetchars[$c])) {
  10742. $g = $glyphIndexArray[0];
  10743. $subsetglyphs[$g] = true;
  10744. }
  10745. } else {
  10746. // two bytes code
  10747. $start_byte = $subHeaders[$k]['firstCode'];
  10748. $end_byte = $start_byte + $subHeaders[$k]['entryCount'];
  10749. for ($j = $start_byte; $j < $end_byte; ++$j) {
  10750. // combine high and low bytes
  10751. $c = (($i << 8) + $j);
  10752. if (isset($subsetchars[$c])) {
  10753. $idRangeOffset = ($subHeaders[$k]['idRangeOffset'] + $j - $subHeaders[$k]['firstCode']);
  10754. $g = ($glyphIndexArray[$idRangeOffset] + $idDelta[$k]) % 65536;
  10755. if ($g < 0) {
  10756. $g = 0;
  10757. }
  10758. $subsetglyphs[$g] = true;
  10759. }
  10760. }
  10761. }
  10762. }
  10763. break;
  10764. }
  10765. case 4: { // Format 4: Segment mapping to delta values
  10766. $length = $this->_getUSHORT($font, $offset);
  10767. $offset += 2;
  10768. $offset += 2; // skip version/language
  10769. $segCount = ($this->_getUSHORT($font, $offset) / 2);
  10770. $offset += 2;
  10771. $offset += 6; // skip searchRange, entrySelector, rangeShift
  10772. $endCount = array(); // array of end character codes for each segment
  10773. for ($k = 0; $k < $segCount; ++$k) {
  10774. $endCount[$k] = $this->_getUSHORT($font, $offset);
  10775. $offset += 2;
  10776. }
  10777. $offset += 2; // skip reservedPad
  10778. $startCount = array(); // array of start character codes for each segment
  10779. for ($k = 0; $k < $segCount; ++$k) {
  10780. $startCount[$k] = $this->_getUSHORT($font, $offset);
  10781. $offset += 2;
  10782. }
  10783. $idDelta = array(); // delta for all character codes in segment
  10784. for ($k = 0; $k < $segCount; ++$k) {
  10785. $idDelta[$k] = $this->_getUSHORT($font, $offset);
  10786. $offset += 2;
  10787. }
  10788. $idRangeOffset = array(); // Offsets into glyphIdArray or 0
  10789. for ($k = 0; $k < $segCount; ++$k) {
  10790. $idRangeOffset[$k] = $this->_getUSHORT($font, $offset);
  10791. $offset += 2;
  10792. }
  10793. $gidlen = ($length / 2) - 8 - (4 * $segCount);
  10794. $glyphIdArray = array(); // glyph index array
  10795. for ($k = 0; $k < $gidlen; ++$k) {
  10796. $glyphIdArray[$k] = $this->_getUSHORT($font, $offset);
  10797. $offset += 2;
  10798. }
  10799. for ($k = 0; $k < $segCount; ++$k) {
  10800. for ($c = $startCount[$k]; $c <= $endCount[$k]; ++$c) {
  10801. if (isset($subsetchars[$c])) {
  10802. if ($idRangeOffset[$k] == 0) {
  10803. $g = ($idDelta[$k] + $c) % 65536;
  10804. } else {
  10805. $gid = (($idRangeOffset[$k] / 2) + ($c - $startCount[$k]) - ($segCount - $k));
  10806. $g = ($glyphIdArray[$gid] + $idDelta[$k]) % 65536;
  10807. }
  10808. if ($g < 0) {
  10809. $g = 0;
  10810. }
  10811. $subsetglyphs[$g] = true;
  10812. }
  10813. }
  10814. }
  10815. break;
  10816. }
  10817. case 6: { // Format 6: Trimmed table mapping
  10818. $offset += 4; // skip length and version/language
  10819. $firstCode = $this->_getUSHORT($font, $offset);
  10820. $offset += 2;
  10821. $entryCount = $this->_getUSHORT($font, $offset);
  10822. $offset += 2;
  10823. for ($k = 0; $k < $entryCount; ++$k) {
  10824. $c = ($k + $firstCode);
  10825. if (isset($subsetchars[$c])) {
  10826. $g = $this->_getUSHORT($font, $offset);
  10827. $subsetglyphs[$g] = true;
  10828. }
  10829. $offset += 2;
  10830. }
  10831. break;
  10832. }
  10833. case 8: { // Format 8: Mixed 16-bit and 32-bit coverage
  10834. $offset += 10; // skip reserved, length and version/language
  10835. for ($k = 0; $k < 8192; ++$k) {
  10836. $is32[$k] = $this->_getBYTE($font, $offset);
  10837. ++$offset;
  10838. }
  10839. $nGroups = $this->_getULONG($font, $offset);
  10840. $offset += 4;
  10841. for ($i = 0; $i < $nGroups; ++$i) {
  10842. $startCharCode = $this->_getULONG($font, $offset);
  10843. $offset += 4;
  10844. $endCharCode = $this->_getULONG($font, $offset);
  10845. $offset += 4;
  10846. $startGlyphID = $this->_getULONG($font, $offset);
  10847. $offset += 4;
  10848. for ($k = $startCharCode; $k <= $endCharCode; ++$k) {
  10849. $is32idx = floor($c / 8);
  10850. if ((isset($is32[$is32idx])) AND (($is32[$is32idx] & (1 << (7 - ($c % 8)))) == 0)) {
  10851. $c = $k;
  10852. } else {
  10853. // 32 bit format
  10854. // convert to decimal (http://www.unicode.org/faq//utf_bom.html#utf16-4)
  10855. //LEAD_OFFSET = (0xD800 - (0x10000 >> 10)) = 55232
  10856. //SURROGATE_OFFSET = (0x10000 - (0xD800 << 10) - 0xDC00) = -56613888
  10857. $c = ((55232 + ($k >> 10)) << 10) + (0xDC00 + ($k & 0x3FF)) -56613888;
  10858. }
  10859. if (isset($subsetchars[$c])) {
  10860. $subsetglyphs[$startGlyphID] = true;
  10861. }
  10862. ++$startGlyphID;
  10863. }
  10864. }
  10865. break;
  10866. }
  10867. case 10: { // Format 10: Trimmed array
  10868. $offset += 10; // skip reserved, length and version/language
  10869. $startCharCode = $this->_getULONG($font, $offset);
  10870. $offset += 4;
  10871. $numChars = $this->_getULONG($font, $offset);
  10872. $offset += 4;
  10873. for ($k = 0; $k < $numChars; ++$k) {
  10874. $c = ($k + $startCharCode);
  10875. if (isset($subsetchars[$c])) {
  10876. $g = $this->_getUSHORT($font, $offset);
  10877. $subsetglyphs[$g] = true;
  10878. }
  10879. $offset += 2;
  10880. }
  10881. break;
  10882. }
  10883. case 12: { // Format 12: Segmented coverage
  10884. $offset += 10; // skip length and version/language
  10885. $nGroups = $this->_getULONG($font, $offset);
  10886. $offset += 4;
  10887. for ($k = 0; $k < $nGroups; ++$k) {
  10888. $startCharCode = $this->_getULONG($font, $offset);
  10889. $offset += 4;
  10890. $endCharCode = $this->_getULONG($font, $offset);
  10891. $offset += 4;
  10892. $startGlyphCode = $this->_getULONG($font, $offset);
  10893. $offset += 4;
  10894. for ($c = $startCharCode; $c <= $endCharCode; ++$c) {
  10895. if (isset($subsetchars[$c])) {
  10896. $subsetglyphs[$startGlyphCode] = true;
  10897. }
  10898. ++$startGlyphCode;
  10899. }
  10900. }
  10901. break;
  10902. }
  10903. case 13: { // Format 13: Many-to-one range mappings
  10904. // to be implemented ...
  10905. break;
  10906. }
  10907. case 14: { // Format 14: Unicode Variation Sequences
  10908. // to be implemented ...
  10909. break;
  10910. }
  10911. }
  10912. }
  10913. // include all parts of composite glyphs
  10914. $new_sga = $subsetglyphs;
  10915. while (!empty($new_sga)) {
  10916. $sga = $new_sga;
  10917. $new_sga = array();
  10918. foreach ($sga as $key => $val) {
  10919. if (isset($indexToLoc[$key])) {
  10920. $offset = ($table['glyf']['offset'] + $indexToLoc[$key]);
  10921. $numberOfContours = $this->_getSHORT($font, $offset);
  10922. $offset += 2;
  10923. if ($numberOfContours < 0) { // composite glyph
  10924. $offset += 8; // skip xMin, yMin, xMax, yMax
  10925. do {
  10926. $flags = $this->_getUSHORT($font, $offset);
  10927. $offset += 2;
  10928. $glyphIndex = $this->_getUSHORT($font, $offset);
  10929. $offset += 2;
  10930. if (!isset($subsetglyphs[$glyphIndex])) {
  10931. // add missing glyphs
  10932. $new_sga[$glyphIndex] = true;
  10933. }
  10934. // skip some bytes by case
  10935. if ($flags & 1) {
  10936. $offset += 4;
  10937. } else {
  10938. $offset += 2;
  10939. }
  10940. if ($flags & 8) {
  10941. $offset += 2;
  10942. } elseif ($flags & 64) {
  10943. $offset += 4;
  10944. } elseif ($flags & 128) {
  10945. $offset += 8;
  10946. }
  10947. } while ($flags & 32);
  10948. }
  10949. }
  10950. }
  10951. $subsetglyphs += $new_sga;
  10952. }
  10953. // sort glyphs by key (and remove duplicates)
  10954. ksort($subsetglyphs);
  10955. // build new glyf and loca tables
  10956. $glyf = '';
  10957. $loca = '';
  10958. $offset = 0;
  10959. $glyf_offset = $table['glyf']['offset'];
  10960. for ($i = 0; $i < $tot_num_glyphs; ++$i) {
  10961. if (isset($subsetglyphs[$i])) {
  10962. $length = ($indexToLoc[($i + 1)] - $indexToLoc[$i]);
  10963. $glyf .= substr($font, ($glyf_offset + $indexToLoc[$i]), $length);
  10964. } else {
  10965. $length = 0;
  10966. }
  10967. if ($short_offset) {
  10968. $loca .= pack('n', ($offset / 2));
  10969. } else {
  10970. $loca .= pack('N', $offset);
  10971. }
  10972. $offset += $length;
  10973. }
  10974. // array of table names to preserve (loca and glyf tables will be added later)
  10975. // the cmap table is not needed and shall not be present, since the mapping from character codes to glyph descriptions is provided separately
  10976. $table_names = array ('head', 'hhea', 'hmtx', 'maxp', 'cvt ', 'fpgm', 'prep'); // minimum required table names
  10977. // get the tables to preserve
  10978. $offset = 12;
  10979. foreach ($table as $tag => $val) {
  10980. if (in_array($tag, $table_names)) {
  10981. $table[$tag]['data'] = substr($font, $table[$tag]['offset'], $table[$tag]['length']);
  10982. if ($tag == 'head') {
  10983. // set the checkSumAdjustment to 0
  10984. $table[$tag]['data'] = substr($table[$tag]['data'], 0, 8)."\x0\x0\x0\x0".substr($table[$tag]['data'], 12);
  10985. }
  10986. $pad = 4 - ($table[$tag]['length'] % 4);
  10987. if ($pad != 4) {
  10988. // the length of a table must be a multiple of four bytes
  10989. $table[$tag]['length'] += $pad;
  10990. $table[$tag]['data'] .= str_repeat("\x0", $pad);
  10991. }
  10992. $table[$tag]['offset'] = $offset;
  10993. $offset += $table[$tag]['length'];
  10994. // check sum is not changed (so keep the following line commented)
  10995. //$table[$tag]['checkSum'] = $this->_getTTFtableChecksum($table[$tag]['data'], $table[$tag]['length']);
  10996. } else {
  10997. unset($table[$tag]);
  10998. }
  10999. }
  11000. // add loca
  11001. $table['loca']['data'] = $loca;
  11002. $table['loca']['length'] = strlen($loca);
  11003. $pad = 4 - ($table['loca']['length'] % 4);
  11004. if ($pad != 4) {
  11005. // the length of a table must be a multiple of four bytes
  11006. $table['loca']['length'] += $pad;
  11007. $table['loca']['data'] .= str_repeat("\x0", $pad);
  11008. }
  11009. $table['loca']['offset'] = $offset;
  11010. $table['loca']['checkSum'] = $this->_getTTFtableChecksum($table['loca']['data'], $table['loca']['length']);
  11011. $offset += $table['loca']['length'];
  11012. // add glyf
  11013. $table['glyf']['data'] = $glyf;
  11014. $table['glyf']['length'] = strlen($glyf);
  11015. $pad = 4 - ($table['glyf']['length'] % 4);
  11016. if ($pad != 4) {
  11017. // the length of a table must be a multiple of four bytes
  11018. $table['glyf']['length'] += $pad;
  11019. $table['glyf']['data'] .= str_repeat("\x0", $pad);
  11020. }
  11021. $table['glyf']['offset'] = $offset;
  11022. $table['glyf']['checkSum'] = $this->_getTTFtableChecksum($table['glyf']['data'], $table['glyf']['length']);
  11023. // rebuild font
  11024. $font = '';
  11025. $font .= pack('N', 0x10000); // sfnt version
  11026. $numTables = count($table);
  11027. $font .= pack('n', $numTables); // numTables
  11028. $entrySelector = floor(log($numTables, 2));
  11029. $searchRange = pow(2, $entrySelector) * 16;
  11030. $rangeShift = ($numTables * 16) - $searchRange;
  11031. $font .= pack('n', $searchRange); // searchRange
  11032. $font .= pack('n', $entrySelector); // entrySelector
  11033. $font .= pack('n', $rangeShift); // rangeShift
  11034. $offset = ($numTables * 16);
  11035. foreach ($table as $tag => $data) {
  11036. $font .= $tag; // tag
  11037. $font .= pack('N', $data['checkSum']); // checkSum
  11038. $font .= pack('N', ($data['offset'] + $offset)); // offset
  11039. $font .= pack('N', $data['length']); // length
  11040. }
  11041. foreach ($table as $data) {
  11042. $font .= $data['data'];
  11043. }
  11044. // set checkSumAdjustment on head table
  11045. $checkSumAdjustment = 0xB1B0AFBA - $this->_getTTFtableChecksum($font, strlen($font));
  11046. $font = substr($font, 0, $table['head']['offset'] + 8).pack('N', $checkSumAdjustment).substr($font, $table['head']['offset'] + 12);
  11047. return $font;
  11048. }
  11049. /**
  11050. * Returs the checksum of a TTF table.
  11051. * @param $table (string) table to check
  11052. * @param $length (int) length of table in bytes
  11053. * @return int checksum
  11054. * @author Nicola Asuni
  11055. * @protected
  11056. * @since 5.2.000 (2010-06-02)
  11057. */
  11058. protected function _getTTFtableChecksum($table, $length) {
  11059. $sum = 0;
  11060. $tlen = ($length / 4);
  11061. $offset = 0;
  11062. for ($i = 0; $i < $tlen; ++$i) {
  11063. $v = unpack('Ni', substr($table, $offset, 4));
  11064. $sum += $v['i'];
  11065. $offset += 4;
  11066. }
  11067. $sum = unpack('Ni', pack('N', $sum));
  11068. return $sum['i'];
  11069. }
  11070. /**
  11071. * Outputs font widths
  11072. * @param $font (array) font data
  11073. * @param $cidoffset (int) offset for CID values
  11074. * @return PDF command string for font widths
  11075. * @author Nicola Asuni
  11076. * @protected
  11077. * @since 4.4.000 (2008-12-07)
  11078. */
  11079. protected function _putfontwidths($font, $cidoffset=0) {
  11080. ksort($font['cw']);
  11081. $rangeid = 0;
  11082. $range = array();
  11083. $prevcid = -2;
  11084. $prevwidth = -1;
  11085. $interval = false;
  11086. // for each character
  11087. foreach ($font['cw'] as $cid => $width) {
  11088. $cid -= $cidoffset;
  11089. if ($font['subset'] AND ($cid > 255) AND (!isset($font['subsetchars'][$cid]))) {
  11090. // ignore the unused characters (font subsetting)
  11091. continue;
  11092. }
  11093. if ($width != $font['dw']) {
  11094. if ($cid == ($prevcid + 1)) {
  11095. // consecutive CID
  11096. if ($width == $prevwidth) {
  11097. if ($width == $range[$rangeid][0]) {
  11098. $range[$rangeid][] = $width;
  11099. } else {
  11100. array_pop($range[$rangeid]);
  11101. // new range
  11102. $rangeid = $prevcid;
  11103. $range[$rangeid] = array();
  11104. $range[$rangeid][] = $prevwidth;
  11105. $range[$rangeid][] = $width;
  11106. }
  11107. $interval = true;
  11108. $range[$rangeid]['interval'] = true;
  11109. } else {
  11110. if ($interval) {
  11111. // new range
  11112. $rangeid = $cid;
  11113. $range[$rangeid] = array();
  11114. $range[$rangeid][] = $width;
  11115. } else {
  11116. $range[$rangeid][] = $width;
  11117. }
  11118. $interval = false;
  11119. }
  11120. } else {
  11121. // new range
  11122. $rangeid = $cid;
  11123. $range[$rangeid] = array();
  11124. $range[$rangeid][] = $width;
  11125. $interval = false;
  11126. }
  11127. $prevcid = $cid;
  11128. $prevwidth = $width;
  11129. }
  11130. }
  11131. // optimize ranges
  11132. $prevk = -1;
  11133. $nextk = -1;
  11134. $prevint = false;
  11135. foreach ($range as $k => $ws) {
  11136. $cws = count($ws);
  11137. if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
  11138. if (isset($range[$k]['interval'])) {
  11139. unset($range[$k]['interval']);
  11140. }
  11141. $range[$prevk] = array_merge($range[$prevk], $range[$k]);
  11142. unset($range[$k]);
  11143. } else {
  11144. $prevk = $k;
  11145. }
  11146. $nextk = $k + $cws;
  11147. if (isset($ws['interval'])) {
  11148. if ($cws > 3) {
  11149. $prevint = true;
  11150. } else {
  11151. $prevint = false;
  11152. }
  11153. unset($range[$k]['interval']);
  11154. --$nextk;
  11155. } else {
  11156. $prevint = false;
  11157. }
  11158. }
  11159. // output data
  11160. $w = '';
  11161. foreach ($range as $k => $ws) {
  11162. if (count(array_count_values($ws)) == 1) {
  11163. // interval mode is more compact
  11164. $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0];
  11165. } else {
  11166. // range mode
  11167. $w .= ' '.$k.' [ '.implode(' ', $ws).' ]';
  11168. }
  11169. }
  11170. return '/W ['.$w.' ]';
  11171. }
  11172. /**
  11173. * Output fonts.
  11174. * @author Nicola Asuni
  11175. * @protected
  11176. */
  11177. protected function _putfonts() {
  11178. $nf = $this->n;
  11179. foreach ($this->diffs as $diff) {
  11180. //Encodings
  11181. $this->_newobj();
  11182. $this->_out('<< /Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$diff.'] >>'."\n".'endobj');
  11183. }
  11184. $mqr = $this->get_mqr();
  11185. $this->set_mqr(false);
  11186. foreach ($this->FontFiles as $file => $info) {
  11187. // search and get font file to embedd
  11188. $fontdir = $info['fontdir'];
  11189. $file = strtolower($file);
  11190. $fontfile = '';
  11191. // search files on various directories
  11192. if (($fontdir !== false) AND file_exists($fontdir.$file)) {
  11193. $fontfile = $fontdir.$file;
  11194. } elseif (file_exists($this->_getfontpath().$file)) {
  11195. $fontfile = $this->_getfontpath().$file;
  11196. } elseif (file_exists($file)) {
  11197. $fontfile = $file;
  11198. }
  11199. if (!$this->empty_string($fontfile)) {
  11200. $font = file_get_contents($fontfile);
  11201. $compressed = (substr($file, -2) == '.z');
  11202. if ((!$compressed) AND (isset($info['length2']))) {
  11203. $header = (ord($font{0}) == 128);
  11204. if ($header) {
  11205. // strip first binary header
  11206. $font = substr($font, 6);
  11207. }
  11208. if ($header AND (ord($font[$info['length1']]) == 128)) {
  11209. // strip second binary header
  11210. $font = substr($font, 0, $info['length1']).substr($font, ($info['length1'] + 6));
  11211. }
  11212. } elseif ($info['subset'] AND ((!$compressed) OR ($compressed AND function_exists('gzcompress')))) {
  11213. if ($compressed) {
  11214. // uncompress font
  11215. $font = gzuncompress($font);
  11216. }
  11217. // merge subset characters
  11218. $subsetchars = array(); // used chars
  11219. foreach ($info['fontkeys'] as $fontkey) {
  11220. $fontinfo = $this->getFontBuffer($fontkey);
  11221. $subsetchars += $fontinfo['subsetchars'];
  11222. }
  11223. // rebuild a font subset
  11224. $font = $this->_getTrueTypeFontSubset($font, $subsetchars);
  11225. // calculate new font length
  11226. $info['length1'] = strlen($font);
  11227. if ($compressed) {
  11228. // recompress font
  11229. $font = gzcompress($font);
  11230. }
  11231. }
  11232. $this->_newobj();
  11233. $this->FontFiles[$file]['n'] = $this->n;
  11234. $stream = $this->_getrawstream($font);
  11235. $out = '<< /Length '.strlen($stream);
  11236. if ($compressed) {
  11237. $out .= ' /Filter /FlateDecode';
  11238. }
  11239. $out .= ' /Length1 '.$info['length1'];
  11240. if (isset($info['length2'])) {
  11241. $out .= ' /Length2 '.$info['length2'].' /Length3 0';
  11242. }
  11243. $out .= ' >>';
  11244. $out .= ' stream'."\n".$stream."\n".'endstream';
  11245. $out .= "\n".'endobj';
  11246. $this->_out($out);
  11247. }
  11248. }
  11249. $this->set_mqr($mqr);
  11250. foreach ($this->fontkeys as $k) {
  11251. //Font objects
  11252. $font = $this->getFontBuffer($k);
  11253. $type = $font['type'];
  11254. $name = $font['name'];
  11255. if ($type == 'core') {
  11256. // standard core font
  11257. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  11258. $out .= '<</Type /Font';
  11259. $out .= ' /Subtype /Type1';
  11260. $out .= ' /BaseFont /'.$name;
  11261. $out .= ' /Name /F'.$font['i'];
  11262. if ((strtolower($name) != 'symbol') AND (strtolower($name) != 'zapfdingbats')) {
  11263. $out .= ' /Encoding /WinAnsiEncoding';
  11264. }
  11265. if ($k == 'helvetica') {
  11266. // add default font for annotations
  11267. $this->annotation_fonts[$k] = $font['i'];
  11268. }
  11269. $out .= ' >>';
  11270. $out .= "\n".'endobj';
  11271. $this->_out($out);
  11272. } elseif (($type == 'Type1') OR ($type == 'TrueType')) {
  11273. // additional Type1 or TrueType font
  11274. $out = $this->_getobj($this->font_obj_ids[$k])."\n";
  11275. $out .= '<</Type /Font';
  11276. $out .= ' /Subtype /'.$type;
  11277. $out .= ' /BaseFont /'.$name;
  11278. $out .= ' /Name /F'.$font['i'];
  11279. $out .= ' /FirstChar 32 /LastChar 255';
  11280. $out .= ' /Widths '.($this->n + 1).' 0 R';
  11281. $out .= ' /FontDescriptor '.($this->n + 2).' 0 R';
  11282. if ($font['enc']) {
  11283. if (isset($font['diff'])) {
  11284. $out .= ' /Encoding '.($nf + $font['diff']).' 0 R';
  11285. } else {
  11286. $out .= ' /Encoding /WinAnsiEncoding';
  11287. }
  11288. }
  11289. $out .= ' >>';
  11290. $out .= "\n".'endobj';
  11291. $this->_out($out);
  11292. // Widths
  11293. $this->_newobj();
  11294. $s = '[';
  11295. for ($i = 32; $i < 256; ++$i) {
  11296. $s .= $font['cw'][$i].' ';
  11297. }
  11298. $s .= ']';
  11299. $s .= "\n".'endobj';
  11300. $this->_out($s);
  11301. //Descriptor
  11302. $this->_newobj();
  11303. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11304. foreach ($font['desc'] as $fdk => $fdv) {
  11305. if (is_float($fdv)) {
  11306. $fdv = sprintf('%F', $fdv);
  11307. }
  11308. $s .= ' /'.$fdk.' '.$fdv.'';
  11309. }
  11310. if (!$this->empty_string($font['file'])) {
  11311. $s .= ' /FontFile'.($type == 'Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11312. }
  11313. $s .= '>>';
  11314. $s .= "\n".'endobj';
  11315. $this->_out($s);
  11316. } else {
  11317. // additional types
  11318. $mtd = '_put'.strtolower($type);
  11319. if (!method_exists($this, $mtd)) {
  11320. $this->Error('Unsupported font type: '.$type);
  11321. }
  11322. $this->$mtd($font);
  11323. }
  11324. }
  11325. }
  11326. /**
  11327. * Adds unicode fonts.<br>
  11328. * Based on PDF Reference 1.3 (section 5)
  11329. * @param $font (array) font data
  11330. * @protected
  11331. * @author Nicola Asuni
  11332. * @since 1.52.0.TC005 (2005-01-05)
  11333. */
  11334. protected function _puttruetypeunicode($font) {
  11335. $fontname = '';
  11336. if ($font['subset']) {
  11337. // change name for font subsetting
  11338. $subtag = sprintf('%06u', $font['i']);
  11339. $subtag = strtr($subtag, '0123456789', 'ABCDEFGHIJ');
  11340. $fontname .= $subtag.'+';
  11341. }
  11342. $fontname .= $font['name'];
  11343. // Type0 Font
  11344. // A composite font composed of other fonts, organized hierarchically
  11345. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11346. $out .= '<< /Type /Font';
  11347. $out .= ' /Subtype /Type0';
  11348. $out .= ' /BaseFont /'.$fontname;
  11349. $out .= ' /Name /F'.$font['i'];
  11350. $out .= ' /Encoding /'.$font['enc'];
  11351. $out .= ' /ToUnicode '.($this->n + 1).' 0 R';
  11352. $out .= ' /DescendantFonts ['.($this->n + 2).' 0 R]';
  11353. $out .= ' >>';
  11354. $out .= "\n".'endobj';
  11355. $this->_out($out);
  11356. // ToUnicode map for Identity-H
  11357. $stream = "/CIDInit /ProcSet findresource begin\n";
  11358. $stream .= "12 dict begin\n";
  11359. $stream .= "begincmap\n";
  11360. $stream .= "/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n";
  11361. $stream .= "/CMapName /Adobe-Identity-UCS def\n";
  11362. $stream .= "/CMapType 2 def\n";
  11363. $stream .= "/WMode 0 def\n";
  11364. $stream .= "1 begincodespacerange\n";
  11365. $stream .= "<0000> <FFFF>\n";
  11366. $stream .= "endcodespacerange\n";
  11367. $stream .= "100 beginbfrange\n";
  11368. $stream .= "<0000> <00ff> <0000>\n";
  11369. $stream .= "<0100> <01ff> <0100>\n";
  11370. $stream .= "<0200> <02ff> <0200>\n";
  11371. $stream .= "<0300> <03ff> <0300>\n";
  11372. $stream .= "<0400> <04ff> <0400>\n";
  11373. $stream .= "<0500> <05ff> <0500>\n";
  11374. $stream .= "<0600> <06ff> <0600>\n";
  11375. $stream .= "<0700> <07ff> <0700>\n";
  11376. $stream .= "<0800> <08ff> <0800>\n";
  11377. $stream .= "<0900> <09ff> <0900>\n";
  11378. $stream .= "<0a00> <0aff> <0a00>\n";
  11379. $stream .= "<0b00> <0bff> <0b00>\n";
  11380. $stream .= "<0c00> <0cff> <0c00>\n";
  11381. $stream .= "<0d00> <0dff> <0d00>\n";
  11382. $stream .= "<0e00> <0eff> <0e00>\n";
  11383. $stream .= "<0f00> <0fff> <0f00>\n";
  11384. $stream .= "<1000> <10ff> <1000>\n";
  11385. $stream .= "<1100> <11ff> <1100>\n";
  11386. $stream .= "<1200> <12ff> <1200>\n";
  11387. $stream .= "<1300> <13ff> <1300>\n";
  11388. $stream .= "<1400> <14ff> <1400>\n";
  11389. $stream .= "<1500> <15ff> <1500>\n";
  11390. $stream .= "<1600> <16ff> <1600>\n";
  11391. $stream .= "<1700> <17ff> <1700>\n";
  11392. $stream .= "<1800> <18ff> <1800>\n";
  11393. $stream .= "<1900> <19ff> <1900>\n";
  11394. $stream .= "<1a00> <1aff> <1a00>\n";
  11395. $stream .= "<1b00> <1bff> <1b00>\n";
  11396. $stream .= "<1c00> <1cff> <1c00>\n";
  11397. $stream .= "<1d00> <1dff> <1d00>\n";
  11398. $stream .= "<1e00> <1eff> <1e00>\n";
  11399. $stream .= "<1f00> <1fff> <1f00>\n";
  11400. $stream .= "<2000> <20ff> <2000>\n";
  11401. $stream .= "<2100> <21ff> <2100>\n";
  11402. $stream .= "<2200> <22ff> <2200>\n";
  11403. $stream .= "<2300> <23ff> <2300>\n";
  11404. $stream .= "<2400> <24ff> <2400>\n";
  11405. $stream .= "<2500> <25ff> <2500>\n";
  11406. $stream .= "<2600> <26ff> <2600>\n";
  11407. $stream .= "<2700> <27ff> <2700>\n";
  11408. $stream .= "<2800> <28ff> <2800>\n";
  11409. $stream .= "<2900> <29ff> <2900>\n";
  11410. $stream .= "<2a00> <2aff> <2a00>\n";
  11411. $stream .= "<2b00> <2bff> <2b00>\n";
  11412. $stream .= "<2c00> <2cff> <2c00>\n";
  11413. $stream .= "<2d00> <2dff> <2d00>\n";
  11414. $stream .= "<2e00> <2eff> <2e00>\n";
  11415. $stream .= "<2f00> <2fff> <2f00>\n";
  11416. $stream .= "<3000> <30ff> <3000>\n";
  11417. $stream .= "<3100> <31ff> <3100>\n";
  11418. $stream .= "<3200> <32ff> <3200>\n";
  11419. $stream .= "<3300> <33ff> <3300>\n";
  11420. $stream .= "<3400> <34ff> <3400>\n";
  11421. $stream .= "<3500> <35ff> <3500>\n";
  11422. $stream .= "<3600> <36ff> <3600>\n";
  11423. $stream .= "<3700> <37ff> <3700>\n";
  11424. $stream .= "<3800> <38ff> <3800>\n";
  11425. $stream .= "<3900> <39ff> <3900>\n";
  11426. $stream .= "<3a00> <3aff> <3a00>\n";
  11427. $stream .= "<3b00> <3bff> <3b00>\n";
  11428. $stream .= "<3c00> <3cff> <3c00>\n";
  11429. $stream .= "<3d00> <3dff> <3d00>\n";
  11430. $stream .= "<3e00> <3eff> <3e00>\n";
  11431. $stream .= "<3f00> <3fff> <3f00>\n";
  11432. $stream .= "<4000> <40ff> <4000>\n";
  11433. $stream .= "<4100> <41ff> <4100>\n";
  11434. $stream .= "<4200> <42ff> <4200>\n";
  11435. $stream .= "<4300> <43ff> <4300>\n";
  11436. $stream .= "<4400> <44ff> <4400>\n";
  11437. $stream .= "<4500> <45ff> <4500>\n";
  11438. $stream .= "<4600> <46ff> <4600>\n";
  11439. $stream .= "<4700> <47ff> <4700>\n";
  11440. $stream .= "<4800> <48ff> <4800>\n";
  11441. $stream .= "<4900> <49ff> <4900>\n";
  11442. $stream .= "<4a00> <4aff> <4a00>\n";
  11443. $stream .= "<4b00> <4bff> <4b00>\n";
  11444. $stream .= "<4c00> <4cff> <4c00>\n";
  11445. $stream .= "<4d00> <4dff> <4d00>\n";
  11446. $stream .= "<4e00> <4eff> <4e00>\n";
  11447. $stream .= "<4f00> <4fff> <4f00>\n";
  11448. $stream .= "<5000> <50ff> <5000>\n";
  11449. $stream .= "<5100> <51ff> <5100>\n";
  11450. $stream .= "<5200> <52ff> <5200>\n";
  11451. $stream .= "<5300> <53ff> <5300>\n";
  11452. $stream .= "<5400> <54ff> <5400>\n";
  11453. $stream .= "<5500> <55ff> <5500>\n";
  11454. $stream .= "<5600> <56ff> <5600>\n";
  11455. $stream .= "<5700> <57ff> <5700>\n";
  11456. $stream .= "<5800> <58ff> <5800>\n";
  11457. $stream .= "<5900> <59ff> <5900>\n";
  11458. $stream .= "<5a00> <5aff> <5a00>\n";
  11459. $stream .= "<5b00> <5bff> <5b00>\n";
  11460. $stream .= "<5c00> <5cff> <5c00>\n";
  11461. $stream .= "<5d00> <5dff> <5d00>\n";
  11462. $stream .= "<5e00> <5eff> <5e00>\n";
  11463. $stream .= "<5f00> <5fff> <5f00>\n";
  11464. $stream .= "<6000> <60ff> <6000>\n";
  11465. $stream .= "<6100> <61ff> <6100>\n";
  11466. $stream .= "<6200> <62ff> <6200>\n";
  11467. $stream .= "<6300> <63ff> <6300>\n";
  11468. $stream .= "endbfrange\n";
  11469. $stream .= "100 beginbfrange\n";
  11470. $stream .= "<6400> <64ff> <6400>\n";
  11471. $stream .= "<6500> <65ff> <6500>\n";
  11472. $stream .= "<6600> <66ff> <6600>\n";
  11473. $stream .= "<6700> <67ff> <6700>\n";
  11474. $stream .= "<6800> <68ff> <6800>\n";
  11475. $stream .= "<6900> <69ff> <6900>\n";
  11476. $stream .= "<6a00> <6aff> <6a00>\n";
  11477. $stream .= "<6b00> <6bff> <6b00>\n";
  11478. $stream .= "<6c00> <6cff> <6c00>\n";
  11479. $stream .= "<6d00> <6dff> <6d00>\n";
  11480. $stream .= "<6e00> <6eff> <6e00>\n";
  11481. $stream .= "<6f00> <6fff> <6f00>\n";
  11482. $stream .= "<7000> <70ff> <7000>\n";
  11483. $stream .= "<7100> <71ff> <7100>\n";
  11484. $stream .= "<7200> <72ff> <7200>\n";
  11485. $stream .= "<7300> <73ff> <7300>\n";
  11486. $stream .= "<7400> <74ff> <7400>\n";
  11487. $stream .= "<7500> <75ff> <7500>\n";
  11488. $stream .= "<7600> <76ff> <7600>\n";
  11489. $stream .= "<7700> <77ff> <7700>\n";
  11490. $stream .= "<7800> <78ff> <7800>\n";
  11491. $stream .= "<7900> <79ff> <7900>\n";
  11492. $stream .= "<7a00> <7aff> <7a00>\n";
  11493. $stream .= "<7b00> <7bff> <7b00>\n";
  11494. $stream .= "<7c00> <7cff> <7c00>\n";
  11495. $stream .= "<7d00> <7dff> <7d00>\n";
  11496. $stream .= "<7e00> <7eff> <7e00>\n";
  11497. $stream .= "<7f00> <7fff> <7f00>\n";
  11498. $stream .= "<8000> <80ff> <8000>\n";
  11499. $stream .= "<8100> <81ff> <8100>\n";
  11500. $stream .= "<8200> <82ff> <8200>\n";
  11501. $stream .= "<8300> <83ff> <8300>\n";
  11502. $stream .= "<8400> <84ff> <8400>\n";
  11503. $stream .= "<8500> <85ff> <8500>\n";
  11504. $stream .= "<8600> <86ff> <8600>\n";
  11505. $stream .= "<8700> <87ff> <8700>\n";
  11506. $stream .= "<8800> <88ff> <8800>\n";
  11507. $stream .= "<8900> <89ff> <8900>\n";
  11508. $stream .= "<8a00> <8aff> <8a00>\n";
  11509. $stream .= "<8b00> <8bff> <8b00>\n";
  11510. $stream .= "<8c00> <8cff> <8c00>\n";
  11511. $stream .= "<8d00> <8dff> <8d00>\n";
  11512. $stream .= "<8e00> <8eff> <8e00>\n";
  11513. $stream .= "<8f00> <8fff> <8f00>\n";
  11514. $stream .= "<9000> <90ff> <9000>\n";
  11515. $stream .= "<9100> <91ff> <9100>\n";
  11516. $stream .= "<9200> <92ff> <9200>\n";
  11517. $stream .= "<9300> <93ff> <9300>\n";
  11518. $stream .= "<9400> <94ff> <9400>\n";
  11519. $stream .= "<9500> <95ff> <9500>\n";
  11520. $stream .= "<9600> <96ff> <9600>\n";
  11521. $stream .= "<9700> <97ff> <9700>\n";
  11522. $stream .= "<9800> <98ff> <9800>\n";
  11523. $stream .= "<9900> <99ff> <9900>\n";
  11524. $stream .= "<9a00> <9aff> <9a00>\n";
  11525. $stream .= "<9b00> <9bff> <9b00>\n";
  11526. $stream .= "<9c00> <9cff> <9c00>\n";
  11527. $stream .= "<9d00> <9dff> <9d00>\n";
  11528. $stream .= "<9e00> <9eff> <9e00>\n";
  11529. $stream .= "<9f00> <9fff> <9f00>\n";
  11530. $stream .= "<a000> <a0ff> <a000>\n";
  11531. $stream .= "<a100> <a1ff> <a100>\n";
  11532. $stream .= "<a200> <a2ff> <a200>\n";
  11533. $stream .= "<a300> <a3ff> <a300>\n";
  11534. $stream .= "<a400> <a4ff> <a400>\n";
  11535. $stream .= "<a500> <a5ff> <a500>\n";
  11536. $stream .= "<a600> <a6ff> <a600>\n";
  11537. $stream .= "<a700> <a7ff> <a700>\n";
  11538. $stream .= "<a800> <a8ff> <a800>\n";
  11539. $stream .= "<a900> <a9ff> <a900>\n";
  11540. $stream .= "<aa00> <aaff> <aa00>\n";
  11541. $stream .= "<ab00> <abff> <ab00>\n";
  11542. $stream .= "<ac00> <acff> <ac00>\n";
  11543. $stream .= "<ad00> <adff> <ad00>\n";
  11544. $stream .= "<ae00> <aeff> <ae00>\n";
  11545. $stream .= "<af00> <afff> <af00>\n";
  11546. $stream .= "<b000> <b0ff> <b000>\n";
  11547. $stream .= "<b100> <b1ff> <b100>\n";
  11548. $stream .= "<b200> <b2ff> <b200>\n";
  11549. $stream .= "<b300> <b3ff> <b300>\n";
  11550. $stream .= "<b400> <b4ff> <b400>\n";
  11551. $stream .= "<b500> <b5ff> <b500>\n";
  11552. $stream .= "<b600> <b6ff> <b600>\n";
  11553. $stream .= "<b700> <b7ff> <b700>\n";
  11554. $stream .= "<b800> <b8ff> <b800>\n";
  11555. $stream .= "<b900> <b9ff> <b900>\n";
  11556. $stream .= "<ba00> <baff> <ba00>\n";
  11557. $stream .= "<bb00> <bbff> <bb00>\n";
  11558. $stream .= "<bc00> <bcff> <bc00>\n";
  11559. $stream .= "<bd00> <bdff> <bd00>\n";
  11560. $stream .= "<be00> <beff> <be00>\n";
  11561. $stream .= "<bf00> <bfff> <bf00>\n";
  11562. $stream .= "<c000> <c0ff> <c000>\n";
  11563. $stream .= "<c100> <c1ff> <c100>\n";
  11564. $stream .= "<c200> <c2ff> <c200>\n";
  11565. $stream .= "<c300> <c3ff> <c300>\n";
  11566. $stream .= "<c400> <c4ff> <c400>\n";
  11567. $stream .= "<c500> <c5ff> <c500>\n";
  11568. $stream .= "<c600> <c6ff> <c600>\n";
  11569. $stream .= "<c700> <c7ff> <c700>\n";
  11570. $stream .= "endbfrange\n";
  11571. $stream .= "56 beginbfrange\n";
  11572. $stream .= "<c800> <c8ff> <c800>\n";
  11573. $stream .= "<c900> <c9ff> <c900>\n";
  11574. $stream .= "<ca00> <caff> <ca00>\n";
  11575. $stream .= "<cb00> <cbff> <cb00>\n";
  11576. $stream .= "<cc00> <ccff> <cc00>\n";
  11577. $stream .= "<cd00> <cdff> <cd00>\n";
  11578. $stream .= "<ce00> <ceff> <ce00>\n";
  11579. $stream .= "<cf00> <cfff> <cf00>\n";
  11580. $stream .= "<d000> <d0ff> <d000>\n";
  11581. $stream .= "<d100> <d1ff> <d100>\n";
  11582. $stream .= "<d200> <d2ff> <d200>\n";
  11583. $stream .= "<d300> <d3ff> <d300>\n";
  11584. $stream .= "<d400> <d4ff> <d400>\n";
  11585. $stream .= "<d500> <d5ff> <d500>\n";
  11586. $stream .= "<d600> <d6ff> <d600>\n";
  11587. $stream .= "<d700> <d7ff> <d700>\n";
  11588. $stream .= "<d800> <d8ff> <d800>\n";
  11589. $stream .= "<d900> <d9ff> <d900>\n";
  11590. $stream .= "<da00> <daff> <da00>\n";
  11591. $stream .= "<db00> <dbff> <db00>\n";
  11592. $stream .= "<dc00> <dcff> <dc00>\n";
  11593. $stream .= "<dd00> <ddff> <dd00>\n";
  11594. $stream .= "<de00> <deff> <de00>\n";
  11595. $stream .= "<df00> <dfff> <df00>\n";
  11596. $stream .= "<e000> <e0ff> <e000>\n";
  11597. $stream .= "<e100> <e1ff> <e100>\n";
  11598. $stream .= "<e200> <e2ff> <e200>\n";
  11599. $stream .= "<e300> <e3ff> <e300>\n";
  11600. $stream .= "<e400> <e4ff> <e400>\n";
  11601. $stream .= "<e500> <e5ff> <e500>\n";
  11602. $stream .= "<e600> <e6ff> <e600>\n";
  11603. $stream .= "<e700> <e7ff> <e700>\n";
  11604. $stream .= "<e800> <e8ff> <e800>\n";
  11605. $stream .= "<e900> <e9ff> <e900>\n";
  11606. $stream .= "<ea00> <eaff> <ea00>\n";
  11607. $stream .= "<eb00> <ebff> <eb00>\n";
  11608. $stream .= "<ec00> <ecff> <ec00>\n";
  11609. $stream .= "<ed00> <edff> <ed00>\n";
  11610. $stream .= "<ee00> <eeff> <ee00>\n";
  11611. $stream .= "<ef00> <efff> <ef00>\n";
  11612. $stream .= "<f000> <f0ff> <f000>\n";
  11613. $stream .= "<f100> <f1ff> <f100>\n";
  11614. $stream .= "<f200> <f2ff> <f200>\n";
  11615. $stream .= "<f300> <f3ff> <f300>\n";
  11616. $stream .= "<f400> <f4ff> <f400>\n";
  11617. $stream .= "<f500> <f5ff> <f500>\n";
  11618. $stream .= "<f600> <f6ff> <f600>\n";
  11619. $stream .= "<f700> <f7ff> <f700>\n";
  11620. $stream .= "<f800> <f8ff> <f800>\n";
  11621. $stream .= "<f900> <f9ff> <f900>\n";
  11622. $stream .= "<fa00> <faff> <fa00>\n";
  11623. $stream .= "<fb00> <fbff> <fb00>\n";
  11624. $stream .= "<fc00> <fcff> <fc00>\n";
  11625. $stream .= "<fd00> <fdff> <fd00>\n";
  11626. $stream .= "<fe00> <feff> <fe00>\n";
  11627. $stream .= "<ff00> <ffff> <ff00>\n";
  11628. $stream .= "endbfrange\n";
  11629. $stream .= "endcmap\n";
  11630. $stream .= "CMapName currentdict /CMap defineresource pop\n";
  11631. $stream .= "end\n";
  11632. $stream .= "end";
  11633. // ToUnicode Object
  11634. $this->_newobj();
  11635. $stream = ($this->compress) ? gzcompress($stream) : $stream;
  11636. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11637. $stream = $this->_getrawstream($stream);
  11638. $this->_out('<<'.$filter.'/Length '.strlen($stream).'>> stream'."\n".$stream."\n".'endstream'."\n".'endobj');
  11639. // CIDFontType2
  11640. // A CIDFont whose glyph descriptions are based on TrueType font technology
  11641. $oid = $this->_newobj();
  11642. $out = '<< /Type /Font';
  11643. $out .= ' /Subtype /CIDFontType2';
  11644. $out .= ' /BaseFont /'.$fontname;
  11645. // A dictionary containing entries that define the character collection of the CIDFont.
  11646. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11647. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11648. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11649. $out .= ' /CIDSystemInfo << '.$cidinfo.' >>';
  11650. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11651. $out .= ' /DW '.$font['dw']; // default width
  11652. $out .= "\n".$this->_putfontwidths($font, 0);
  11653. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11654. $out .= "\n".'/CIDToGIDMap '.($this->n + 2).' 0 R';
  11655. }
  11656. $out .= ' >>';
  11657. $out .= "\n".'endobj';
  11658. $this->_out($out);
  11659. // Font descriptor
  11660. // A font descriptor describing the CIDFont default metrics other than its glyph widths
  11661. $this->_newobj();
  11662. $out = '<< /Type /FontDescriptor';
  11663. $out .= ' /FontName /'.$fontname;
  11664. foreach ($font['desc'] as $key => $value) {
  11665. if (is_float($value)) {
  11666. $value = sprintf('%F', $value);
  11667. }
  11668. $out .= ' /'.$key.' '.$value;
  11669. }
  11670. $fontdir = false;
  11671. if (!$this->empty_string($font['file'])) {
  11672. // A stream containing a TrueType font
  11673. $out .= ' /FontFile2 '.$this->FontFiles[$font['file']]['n'].' 0 R';
  11674. $fontdir = $this->FontFiles[$font['file']]['fontdir'];
  11675. }
  11676. $out .= ' >>';
  11677. $out .= "\n".'endobj';
  11678. $this->_out($out);
  11679. if (isset($font['ctg']) AND (!$this->empty_string($font['ctg']))) {
  11680. $this->_newobj();
  11681. // Embed CIDToGIDMap
  11682. // A specification of the mapping from CIDs to glyph indices
  11683. // search and get CTG font file to embedd
  11684. $ctgfile = strtolower($font['ctg']);
  11685. // search and get ctg font file to embedd
  11686. $fontfile = '';
  11687. // search files on various directories
  11688. if (($fontdir !== false) AND file_exists($fontdir.$ctgfile)) {
  11689. $fontfile = $fontdir.$ctgfile;
  11690. } elseif (file_exists($this->_getfontpath().$ctgfile)) {
  11691. $fontfile = $this->_getfontpath().$ctgfile;
  11692. } elseif (file_exists($ctgfile)) {
  11693. $fontfile = $ctgfile;
  11694. }
  11695. if ($this->empty_string($fontfile)) {
  11696. $this->Error('Font file not found: '.$ctgfile);
  11697. }
  11698. $stream = $this->_getrawstream(file_get_contents($fontfile));
  11699. $out = '<< /Length '.strlen($stream).'';
  11700. if (substr($fontfile, -2) == '.z') { // check file extension
  11701. // Decompresses data encoded using the public-domain
  11702. // zlib/deflate compression method, reproducing the
  11703. // original text or binary data
  11704. $out .= ' /Filter /FlateDecode';
  11705. }
  11706. $out .= ' >>';
  11707. $out .= ' stream'."\n".$stream."\n".'endstream';
  11708. $out .= "\n".'endobj';
  11709. $this->_out($out);
  11710. }
  11711. }
  11712. /**
  11713. * Output CID-0 fonts.
  11714. * A Type 0 CIDFont contains glyph descriptions based on the Adobe Type 1 font format
  11715. * @param $font (array) font data
  11716. * @protected
  11717. * @author Andrew Whitehead, Nicola Asuni, Yukihiro Nakadaira
  11718. * @since 3.2.000 (2008-06-23)
  11719. */
  11720. protected function _putcidfont0($font) {
  11721. $cidoffset = 0;
  11722. if (!isset($font['cw'][1])) {
  11723. $cidoffset = 31;
  11724. }
  11725. if (isset($font['cidinfo']['uni2cid'])) {
  11726. // convert unicode to cid.
  11727. $uni2cid = $font['cidinfo']['uni2cid'];
  11728. $cw = array();
  11729. foreach ($font['cw'] as $uni => $width) {
  11730. if (isset($uni2cid[$uni])) {
  11731. $cw[($uni2cid[$uni] + $cidoffset)] = $width;
  11732. } elseif ($uni < 256) {
  11733. $cw[$uni] = $width;
  11734. } // else unknown character
  11735. }
  11736. $font = array_merge($font, array('cw' => $cw));
  11737. }
  11738. $name = $font['name'];
  11739. $enc = $font['enc'];
  11740. if ($enc) {
  11741. $longname = $name.'-'.$enc;
  11742. } else {
  11743. $longname = $name;
  11744. }
  11745. $out = $this->_getobj($this->font_obj_ids[$font['fontkey']])."\n";
  11746. $out .= '<</Type /Font';
  11747. $out .= ' /Subtype /Type0';
  11748. $out .= ' /BaseFont /'.$longname;
  11749. $out .= ' /Name /F'.$font['i'];
  11750. if ($enc) {
  11751. $out .= ' /Encoding /'.$enc;
  11752. }
  11753. $out .= ' /DescendantFonts ['.($this->n + 1).' 0 R]';
  11754. $out .= ' >>';
  11755. $out .= "\n".'endobj';
  11756. $this->_out($out);
  11757. $oid = $this->_newobj();
  11758. $out = '<</Type /Font';
  11759. $out .= ' /Subtype /CIDFontType0';
  11760. $out .= ' /BaseFont /'.$name;
  11761. $cidinfo = '/Registry '.$this->_datastring($font['cidinfo']['Registry'], $oid);
  11762. $cidinfo .= ' /Ordering '.$this->_datastring($font['cidinfo']['Ordering'], $oid);
  11763. $cidinfo .= ' /Supplement '.$font['cidinfo']['Supplement'];
  11764. $out .= ' /CIDSystemInfo <<'.$cidinfo.'>>';
  11765. $out .= ' /FontDescriptor '.($this->n + 1).' 0 R';
  11766. $out .= ' /DW '.$font['dw'];
  11767. $out .= "\n".$this->_putfontwidths($font, $cidoffset);
  11768. $out .= ' >>';
  11769. $out .= "\n".'endobj';
  11770. $this->_out($out);
  11771. $this->_newobj();
  11772. $s = '<</Type /FontDescriptor /FontName /'.$name;
  11773. foreach ($font['desc'] as $k => $v) {
  11774. if ($k != 'Style') {
  11775. if (is_float($v)) {
  11776. $v = sprintf('%F', $v);
  11777. }
  11778. $s .= ' /'.$k.' '.$v.'';
  11779. }
  11780. }
  11781. $s .= '>>';
  11782. $s .= "\n".'endobj';
  11783. $this->_out($s);
  11784. }
  11785. /**
  11786. * Output images.
  11787. * @protected
  11788. */
  11789. protected function _putimages() {
  11790. $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';
  11791. foreach ($this->imagekeys as $file) {
  11792. $info = $this->getImageBuffer($file);
  11793. // set object for alternate images array
  11794. if ((!$this->pdfa_mode) AND isset($info['altimgs']) AND !empty($info['altimgs'])) {
  11795. $altoid = $this->_newobj();
  11796. $out = '[';
  11797. foreach ($info['altimgs'] as $altimage) {
  11798. if (isset($this->xobjects['I'.$altimage[0]]['n'])) {
  11799. $out .= ' << /Image '.$this->xobjects['I'.$altimage[0]]['n'].' 0 R';
  11800. $out .= ' /DefaultForPrinting';
  11801. if ($altimage[1] === true) {
  11802. $out .= ' true';
  11803. } else {
  11804. $out .= ' false';
  11805. }
  11806. $out .= ' >>';
  11807. }
  11808. }
  11809. $out .= ' ]';
  11810. $out .= "\n".'endobj';
  11811. $this->_out($out);
  11812. }
  11813. // set image object
  11814. $oid = $this->_newobj();
  11815. $this->xobjects['I'.$info['i']] = array('n' => $oid);
  11816. $this->setImageSubBuffer($file, 'n', $this->n);
  11817. $out = '<</Type /XObject';
  11818. $out .= ' /Subtype /Image';
  11819. $out .= ' /Width '.$info['w'];
  11820. $out .= ' /Height '.$info['h'];
  11821. if (array_key_exists('masked', $info)) {
  11822. $out .= ' /SMask '.($this->n - 1).' 0 R';
  11823. }
  11824. // set color space
  11825. $icc = false;
  11826. if (isset($info['icc']) AND ($info['icc'] !== false)) {
  11827. // ICC Colour Space
  11828. $icc = true;
  11829. $out .= ' /ColorSpace [/ICCBased '.($this->n + 1).' 0 R]';
  11830. } elseif ($info['cs'] == 'Indexed') {
  11831. // Indexed Colour Space
  11832. $out .= ' /ColorSpace [/Indexed /DeviceRGB '.((strlen($info['pal']) / 3) - 1).' '.($this->n + 1).' 0 R]';
  11833. } else {
  11834. // Device Colour Space
  11835. $out .= ' /ColorSpace /'.$info['cs'];
  11836. }
  11837. if ($info['cs'] == 'DeviceCMYK') {
  11838. $out .= ' /Decode [1 0 1 0 1 0 1 0]';
  11839. }
  11840. $out .= ' /BitsPerComponent '.$info['bpc'];
  11841. if (isset($altoid) AND ($altoid > 0)) {
  11842. // reference to alternate images dictionary
  11843. $out .= ' /Alternates '.$altoid.' 0 R';
  11844. }
  11845. if (isset($info['exurl']) AND !empty($info['exurl'])) {
  11846. // external stream
  11847. $out .= ' /Length 0';
  11848. $out .= ' /F << /FS /URL /F '.$this->_datastring($info['exurl'], $oid).' >>';
  11849. if (isset($info['f'])) {
  11850. $out .= ' /FFilter /'.$info['f'];
  11851. }
  11852. $out .= ' >>';
  11853. $out .= ' stream'."\n".'endstream';
  11854. } else {
  11855. if (isset($info['f'])) {
  11856. $out .= ' /Filter /'.$info['f'];
  11857. }
  11858. if (isset($info['parms'])) {
  11859. $out .= ' '.$info['parms'];
  11860. }
  11861. if (isset($info['trns']) AND is_array($info['trns'])) {
  11862. $trns = '';
  11863. $count_info = count($info['trns']);
  11864. for ($i=0; $i < $count_info; ++$i) {
  11865. $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
  11866. }
  11867. $out .= ' /Mask ['.$trns.']';
  11868. }
  11869. $stream = $this->_getrawstream($info['data']);
  11870. $out .= ' /Length '.strlen($stream).' >>';
  11871. $out .= ' stream'."\n".$stream."\n".'endstream';
  11872. }
  11873. $out .= "\n".'endobj';
  11874. $this->_out($out);
  11875. if ($icc) {
  11876. // ICC colour profile
  11877. $this->_newobj();
  11878. $icc = ($this->compress) ? gzcompress($info['icc']) : $info['icc'];
  11879. $icc = $this->_getrawstream($icc);
  11880. $this->_out('<</N '.$info['ch'].' /Alternate /'.$info['cs'].' '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  11881. } elseif ($info['cs'] == 'Indexed') {
  11882. // colour palette
  11883. $this->_newobj();
  11884. $pal = ($this->compress) ? gzcompress($info['pal']) : $info['pal'];
  11885. $pal = $this->_getrawstream($pal);
  11886. $this->_out('<<'.$filter.'/Length '.strlen($pal).'>> stream'."\n".$pal."\n".'endstream'."\n".'endobj');
  11887. }
  11888. }
  11889. }
  11890. /**
  11891. * Output Form XObjects Templates.
  11892. * @author Nicola Asuni
  11893. * @since 5.8.017 (2010-08-24)
  11894. * @protected
  11895. * @see startTemplate(), endTemplate(), printTemplate()
  11896. */
  11897. protected function _putxobjects() {
  11898. foreach ($this->xobjects as $key => $data) {
  11899. if (isset($data['outdata'])) {
  11900. $stream = trim($data['outdata']);
  11901. $out = $this->_getobj($data['n'])."\n";
  11902. $out .= '<<';
  11903. $out .= ' /Type /XObject';
  11904. $out .= ' /Subtype /Form';
  11905. $out .= ' /FormType 1';
  11906. if ($this->compress) {
  11907. $stream = gzcompress($stream);
  11908. $out .= ' /Filter /FlateDecode';
  11909. }
  11910. $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));
  11911. $out .= ' /Matrix [1 0 0 1 0 0]';
  11912. $out .= ' /Resources <<';
  11913. $out .= ' /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  11914. if (!$this->pdfa_mode) {
  11915. // transparency
  11916. if (isset($data['extgstates']) AND !empty($data['extgstates'])) {
  11917. $out .= ' /ExtGState <<';
  11918. foreach ($data['extgstates'] as $k => $extgstate) {
  11919. if (isset($this->extgstates[$k]['name'])) {
  11920. $out .= ' /'.$this->extgstates[$k]['name'];
  11921. } else {
  11922. $out .= ' /GS'.$k;
  11923. }
  11924. $out .= ' '.$this->extgstates[$k]['n'].' 0 R';
  11925. }
  11926. $out .= ' >>';
  11927. }
  11928. if (isset($data['gradients']) AND !empty($data['gradients'])) {
  11929. $gp = '';
  11930. $gs = '';
  11931. foreach ($data['gradients'] as $id => $grad) {
  11932. // gradient patterns
  11933. $gp .= ' /p'.$id.' '.$this->gradients[$id]['pattern'].' 0 R';
  11934. // gradient shadings
  11935. $gs .= ' /Sh'.$id.' '.$this->gradients[$id]['id'].' 0 R';
  11936. }
  11937. $out .= ' /Pattern <<'.$gp.' >>';
  11938. $out .= ' /Shading <<'.$gs.' >>';
  11939. }
  11940. }
  11941. // spot colors
  11942. if (isset($data['spot_colors']) AND !empty($data['spot_colors'])) {
  11943. $out .= ' /ColorSpace <<';
  11944. foreach ($data['spot_colors'] as $name => $color) {
  11945. $out .= ' /CS'.$color['i'].' '.$this->spot_colors[$name]['n'].' 0 R';
  11946. }
  11947. $out .= ' >>';
  11948. }
  11949. // fonts
  11950. if (!empty($data['fonts'])) {
  11951. $out .= ' /Font <<';
  11952. foreach ($data['fonts'] as $fontkey => $fontid) {
  11953. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  11954. }
  11955. $out .= ' >>';
  11956. }
  11957. // images or nested xobjects
  11958. if (!empty($data['images']) OR !empty($data['xobjects'])) {
  11959. $out .= ' /XObject <<';
  11960. foreach ($data['images'] as $imgid) {
  11961. $out .= ' /I'.$imgid.' '.$this->xobjects['I'.$imgid]['n'].' 0 R';
  11962. }
  11963. foreach ($data['xobjects'] as $sub_id => $sub_objid) {
  11964. $out .= ' /'.$sub_id.' '.$sub_objid['n'].' 0 R';
  11965. }
  11966. $out .= ' >>';
  11967. }
  11968. $out .= ' >>'; //end resources
  11969. if (isset($data['group']) AND ($data['group'] !== false)) {
  11970. // set transparency group
  11971. $out .= ' /Group << /Type /Group /S /Transparency';
  11972. if (is_array($data['group'])) {
  11973. if (isset($data['group']['CS']) AND !empty($data['group']['CS'])) {
  11974. $out .= ' /CS /'.$data['group']['CS'];
  11975. }
  11976. if (isset($data['group']['I'])) {
  11977. $out .= ' /I /'.($data['group']['I']===true?'true':'false');
  11978. }
  11979. if (isset($data['group']['K'])) {
  11980. $out .= ' /K /'.($data['group']['K']===true?'true':'false');
  11981. }
  11982. }
  11983. $out .= ' >>';
  11984. }
  11985. $stream = $this->_getrawstream($stream, $data['n']);
  11986. $out .= ' /Length '.strlen($stream);
  11987. $out .= ' >>';
  11988. $out .= ' stream'."\n".$stream."\n".'endstream';
  11989. $out .= "\n".'endobj';
  11990. $this->_out($out);
  11991. }
  11992. }
  11993. }
  11994. /**
  11995. * Output Spot Colors Resources.
  11996. * @protected
  11997. * @since 4.0.024 (2008-09-12)
  11998. */
  11999. protected function _putspotcolors() {
  12000. foreach ($this->spot_colors as $name => $color) {
  12001. $this->_newobj();
  12002. $this->spot_colors[$name]['n'] = $this->n;
  12003. $out = '[/Separation /'.str_replace(' ', '#20', $name);
  12004. $out .= ' /DeviceCMYK <<';
  12005. $out .= ' /Range [0 1 0 1 0 1 0 1] /C0 [0 0 0 0]';
  12006. $out .= ' '.sprintf('/C1 [%F %F %F %F] ', ($color['C'] / 100), ($color['M'] / 100), ($color['Y'] / 100), ($color['K'] / 100));
  12007. $out .= ' /FunctionType 2 /Domain [0 1] /N 1>>]';
  12008. $out .= "\n".'endobj';
  12009. $this->_out($out);
  12010. }
  12011. }
  12012. /**
  12013. * Return XObjects Dictionary.
  12014. * @return string XObjects dictionary
  12015. * @protected
  12016. * @since 5.8.014 (2010-08-23)
  12017. */
  12018. protected function _getxobjectdict() {
  12019. $out = '';
  12020. foreach ($this->xobjects as $id => $objid) {
  12021. $out .= ' /'.$id.' '.$objid['n'].' 0 R';
  12022. }
  12023. return $out;
  12024. }
  12025. /**
  12026. * Output Resources Dictionary.
  12027. * @protected
  12028. */
  12029. protected function _putresourcedict() {
  12030. $out = $this->_getobj(2)."\n";
  12031. $out .= '<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]';
  12032. $out .= ' /Font <<';
  12033. foreach ($this->fontkeys as $fontkey) {
  12034. $font = $this->getFontBuffer($fontkey);
  12035. $out .= ' /F'.$font['i'].' '.$font['n'].' 0 R';
  12036. }
  12037. $out .= ' >>';
  12038. $out .= ' /XObject <<';
  12039. $out .= $this->_getxobjectdict();
  12040. $out .= ' >>';
  12041. // layers
  12042. if (!empty($this->pdflayers)) {
  12043. $out .= ' /Properties <<';
  12044. foreach ($this->pdflayers as $layer) {
  12045. $out .= ' /'.$layer['layer'].' '.$layer['objid'].' 0 R';
  12046. }
  12047. $out .= ' >>';
  12048. }
  12049. if (!$this->pdfa_mode) {
  12050. // transparency
  12051. if (isset($this->extgstates) AND !empty($this->extgstates)) {
  12052. $out .= ' /ExtGState <<';
  12053. foreach ($this->extgstates as $k => $extgstate) {
  12054. if (isset($extgstate['name'])) {
  12055. $out .= ' /'.$extgstate['name'];
  12056. } else {
  12057. $out .= ' /GS'.$k;
  12058. }
  12059. $out .= ' '.$extgstate['n'].' 0 R';
  12060. }
  12061. $out .= ' >>';
  12062. }
  12063. if (isset($this->gradients) AND !empty($this->gradients)) {
  12064. $gp = '';
  12065. $gs = '';
  12066. foreach ($this->gradients as $id => $grad) {
  12067. // gradient patterns
  12068. $gp .= ' /p'.$id.' '.$grad['pattern'].' 0 R';
  12069. // gradient shadings
  12070. $gs .= ' /Sh'.$id.' '.$grad['id'].' 0 R';
  12071. }
  12072. $out .= ' /Pattern <<'.$gp.' >>';
  12073. $out .= ' /Shading <<'.$gs.' >>';
  12074. }
  12075. }
  12076. // spot colors
  12077. if (isset($this->spot_colors) AND !empty($this->spot_colors)) {
  12078. $out .= ' /ColorSpace <<';
  12079. foreach ($this->spot_colors as $color) {
  12080. $out .= ' /CS'.$color['i'].' '.$color['n'].' 0 R';
  12081. }
  12082. $out .= ' >>';
  12083. }
  12084. $out .= ' >>';
  12085. $out .= "\n".'endobj';
  12086. $this->_out($out);
  12087. }
  12088. /**
  12089. * Output Resources.
  12090. * @protected
  12091. */
  12092. protected function _putresources() {
  12093. $this->_putextgstates();
  12094. $this->_putocg();
  12095. $this->_putfonts();
  12096. $this->_putimages();
  12097. $this->_putspotcolors();
  12098. $this->_putshaders();
  12099. $this->_putxobjects();
  12100. $this->_putresourcedict();
  12101. $this->_putdests();
  12102. $this->_putbookmarks();
  12103. $this->_putEmbeddedFiles();
  12104. $this->_putannotsobjs();
  12105. $this->_putjavascript();
  12106. $this->_putencryption();
  12107. }
  12108. /**
  12109. * Adds some Metadata information (Document Information Dictionary)
  12110. * (see Chapter 14.3.3 Document Information Dictionary of PDF32000_2008.pdf Reference)
  12111. * @return int object id
  12112. * @protected
  12113. */
  12114. protected function _putinfo() {
  12115. $oid = $this->_newobj();
  12116. $out = '<<';
  12117. // store current isunicode value
  12118. $prev_isunicode = $this->isunicode;
  12119. if ($this->docinfounicode) {
  12120. $this->isunicode = true;
  12121. }
  12122. if (!$this->empty_string($this->title)) {
  12123. // The document's title.
  12124. $out .= ' /Title '.$this->_textstring($this->title, $oid);
  12125. }
  12126. if (!$this->empty_string($this->author)) {
  12127. // The name of the person who created the document.
  12128. $out .= ' /Author '.$this->_textstring($this->author, $oid);
  12129. }
  12130. if (!$this->empty_string($this->subject)) {
  12131. // The subject of the document.
  12132. $out .= ' /Subject '.$this->_textstring($this->subject, $oid);
  12133. }
  12134. if (!$this->empty_string($this->keywords)) {
  12135. // Keywords associated with the document.
  12136. $out .= ' /Keywords '.$this->_textstring($this->keywords.' TCPDF', $oid);
  12137. }
  12138. if (!$this->empty_string($this->creator)) {
  12139. // 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.
  12140. $out .= ' /Creator '.$this->_textstring($this->creator, $oid);
  12141. }
  12142. // restore previous isunicode value
  12143. $this->isunicode = $prev_isunicode;
  12144. // default producer
  12145. $out .= ' /Producer '.$this->_textstring($this->pdfproducer, $oid);
  12146. // The date and time the document was created, in human-readable form
  12147. $out .= ' /CreationDate '.$this->_datestring(0, $this->doc_creation_timestamp);
  12148. // The date and time the document was most recently modified, in human-readable form
  12149. $out .= ' /ModDate '.$this->_datestring(0, $this->doc_modification_timestamp);
  12150. // A name object indicating whether the document has been modified to include trapping information
  12151. $out .= ' /Trapped /False';
  12152. $out .= ' >>';
  12153. $out .= "\n".'endobj';
  12154. $this->_out($out);
  12155. return $oid;
  12156. }
  12157. /**
  12158. * Set additional XMP data to be added on the default XMP data just before the end of "x:xmpmeta" tag.
  12159. * IMPORTANT: This data is added as-is without controls, so you have to validate your data before using this method!
  12160. * @param $xmp (string) Custom XMP data.
  12161. * @since 5.9.128 (2011-10-06)
  12162. * @public
  12163. */
  12164. public function setExtraXMP($xmp) {
  12165. $this->custom_xmp = $xmp;
  12166. }
  12167. /**
  12168. * Put XMP data object and return ID.
  12169. * @return (int) The object ID.
  12170. * @since 5.9.121 (2011-09-28)
  12171. * @protected
  12172. */
  12173. protected function _putXMP() {
  12174. $oid = $this->_newobj();
  12175. // store current isunicode value
  12176. $prev_isunicode = $this->isunicode;
  12177. $this->isunicode = true;
  12178. $prev_encrypted = $this->encrypted;
  12179. $this->encrypted = false;
  12180. // set XMP data
  12181. $xmp = '<?xpacket begin="'.$this->unichr(0xfeff).'" id="W5M0MpCehiHzreSzNTczkc9d"?>'."\n";
  12182. $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";
  12183. $xmp .= "\t".'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'."\n";
  12184. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
  12185. $xmp .= "\t\t\t".'<dc:format>application/pdf</dc:format>'."\n";
  12186. $xmp .= "\t\t\t".'<dc:title>'."\n";
  12187. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  12188. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->title).'</rdf:li>'."\n";
  12189. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  12190. $xmp .= "\t\t\t".'</dc:title>'."\n";
  12191. $xmp .= "\t\t\t".'<dc:creator>'."\n";
  12192. $xmp .= "\t\t\t\t".'<rdf:Seq>'."\n";
  12193. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->author).'</rdf:li>'."\n";
  12194. $xmp .= "\t\t\t\t".'</rdf:Seq>'."\n";
  12195. $xmp .= "\t\t\t".'</dc:creator>'."\n";
  12196. $xmp .= "\t\t\t".'<dc:description>'."\n";
  12197. $xmp .= "\t\t\t\t".'<rdf:Alt>'."\n";
  12198. $xmp .= "\t\t\t\t\t".'<rdf:li xml:lang="x-default">'.$this->_escapeXML($this->subject).'</rdf:li>'."\n";
  12199. $xmp .= "\t\t\t\t".'</rdf:Alt>'."\n";
  12200. $xmp .= "\t\t\t".'</dc:description>'."\n";
  12201. $xmp .= "\t\t\t".'<dc:subject>'."\n";
  12202. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  12203. $xmp .= "\t\t\t\t\t".'<rdf:li>'.$this->_escapeXML($this->keywords).'</rdf:li>'."\n";
  12204. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  12205. $xmp .= "\t\t\t".'</dc:subject>'."\n";
  12206. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12207. // convert doc creation date format
  12208. $dcdate = $this->getFormattedDate($this->doc_creation_timestamp);
  12209. $doccreationdate = substr($dcdate, 0, 4).'-'.substr($dcdate, 4, 2).'-'.substr($dcdate, 6, 2);
  12210. $doccreationdate .= 'T'.substr($dcdate, 8, 2).':'.substr($dcdate, 10, 2).':'.substr($dcdate, 12, 2);
  12211. $doccreationdate .= '+'.substr($dcdate, 15, 2).':'.substr($dcdate, 18, 2);
  12212. $doccreationdate = $this->_escapeXML($doccreationdate);
  12213. // convert doc modification date format
  12214. $dmdate = $this->getFormattedDate($this->doc_modification_timestamp);
  12215. $docmoddate = substr($dmdate, 0, 4).'-'.substr($dmdate, 4, 2).'-'.substr($dmdate, 6, 2);
  12216. $docmoddate .= 'T'.substr($dmdate, 8, 2).':'.substr($dmdate, 10, 2).':'.substr($dmdate, 12, 2);
  12217. $docmoddate .= '+'.substr($dmdate, 15, 2).':'.substr($dmdate, 18, 2);
  12218. $docmoddate = $this->_escapeXML($docmoddate);
  12219. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">'."\n";
  12220. $xmp .= "\t\t\t".'<xmp:CreateDate>'.$doccreationdate.'</xmp:CreateDate>'."\n";
  12221. $xmp .= "\t\t\t".'<xmp:CreatorTool>'.$this->creator.'</xmp:CreatorTool>'."\n";
  12222. $xmp .= "\t\t\t".'<xmp:ModifyDate>'.$docmoddate.'</xmp:ModifyDate>'."\n";
  12223. $xmp .= "\t\t\t".'<xmp:MetadataDate>'.$doccreationdate.'</xmp:MetadataDate>'."\n";
  12224. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12225. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">'."\n";
  12226. $xmp .= "\t\t\t".'<pdf:Keywords>'.$this->_escapeXML($this->keywords).' TCPDF</pdf:Keywords>'."\n";
  12227. $xmp .= "\t\t\t".'<pdf:Producer>'.$this->_escapeXML($this->pdfproducer).'</pdf:Producer>'."\n";
  12228. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12229. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/">'."\n";
  12230. $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);
  12231. $xmp .= "\t\t\t".'<xmpMM:DocumentID>'.$uuid.'</xmpMM:DocumentID>'."\n";
  12232. $xmp .= "\t\t\t".'<xmpMM:InstanceID>'.$uuid.'</xmpMM:InstanceID>'."\n";
  12233. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12234. if ($this->pdfa_mode) {
  12235. $xmp .= "\t\t".'<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'."\n";
  12236. $xmp .= "\t\t\t".'<pdfaid:part>1</pdfaid:part>'."\n";
  12237. $xmp .= "\t\t\t".'<pdfaid:conformance>B</pdfaid:conformance>'."\n";
  12238. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12239. }
  12240. // XMP extension schemas
  12241. $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";
  12242. $xmp .= "\t\t\t".'<pdfaExtension:schemas>'."\n";
  12243. $xmp .= "\t\t\t\t".'<rdf:Bag>'."\n";
  12244. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12245. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/pdf/1.3/</pdfaSchema:namespaceURI>'."\n";
  12246. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdf</pdfaSchema:prefix>'."\n";
  12247. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>Adobe PDF Schema</pdfaSchema:schema>'."\n";
  12248. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  12249. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12250. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://ns.adobe.com/xap/1.0/mm/</pdfaSchema:namespaceURI>'."\n";
  12251. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>xmpMM</pdfaSchema:prefix>'."\n";
  12252. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>XMP Media Management Schema</pdfaSchema:schema>'."\n";
  12253. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  12254. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  12255. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12256. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12257. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>UUID based identifier for specific incarnation of a document</pdfaProperty:description>'."\n";
  12258. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>InstanceID</pdfaProperty:name>'."\n";
  12259. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>URI</pdfaProperty:valueType>'."\n";
  12260. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12261. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  12262. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  12263. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  12264. $xmp .= "\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12265. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:namespaceURI>http://www.aiim.org/pdfa/ns/id/</pdfaSchema:namespaceURI>'."\n";
  12266. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:prefix>pdfaid</pdfaSchema:prefix>'."\n";
  12267. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:schema>PDF/A ID Schema</pdfaSchema:schema>'."\n";
  12268. $xmp .= "\t\t\t\t\t\t".'<pdfaSchema:property>'."\n";
  12269. $xmp .= "\t\t\t\t\t\t\t".'<rdf:Seq>'."\n";
  12270. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12271. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12272. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Part of PDF/A standard</pdfaProperty:description>'."\n";
  12273. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>part</pdfaProperty:name>'."\n";
  12274. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Integer</pdfaProperty:valueType>'."\n";
  12275. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12276. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12277. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12278. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Amendment of PDF/A standard</pdfaProperty:description>'."\n";
  12279. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>amd</pdfaProperty:name>'."\n";
  12280. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  12281. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12282. $xmp .= "\t\t\t\t\t\t\t\t".'<rdf:li rdf:parseType="Resource">'."\n";
  12283. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:category>internal</pdfaProperty:category>'."\n";
  12284. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:description>Conformance level of PDF/A standard</pdfaProperty:description>'."\n";
  12285. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:name>conformance</pdfaProperty:name>'."\n";
  12286. $xmp .= "\t\t\t\t\t\t\t\t\t".'<pdfaProperty:valueType>Text</pdfaProperty:valueType>'."\n";
  12287. $xmp .= "\t\t\t\t\t\t\t\t".'</rdf:li>'."\n";
  12288. $xmp .= "\t\t\t\t\t\t\t".'</rdf:Seq>'."\n";
  12289. $xmp .= "\t\t\t\t\t\t".'</pdfaSchema:property>'."\n";
  12290. $xmp .= "\t\t\t\t\t".'</rdf:li>'."\n";
  12291. $xmp .= "\t\t\t\t".'</rdf:Bag>'."\n";
  12292. $xmp .= "\t\t\t".'</pdfaExtension:schemas>'."\n";
  12293. $xmp .= "\t\t".'</rdf:Description>'."\n";
  12294. $xmp .= "\t".'</rdf:RDF>'."\n";
  12295. $xmp .= $this->custom_xmp;
  12296. $xmp .= '</x:xmpmeta>'."\n";
  12297. $xmp .= '<?xpacket end="w"?>';
  12298. $out = '<< /Type /Metadata /Subtype /XML /Length '.strlen($xmp).' >> stream'."\n".$xmp."\n".'endstream'."\n".'endobj';
  12299. // restore previous isunicode value
  12300. $this->isunicode = $prev_isunicode;
  12301. $this->encrypted = $prev_encrypted;
  12302. $this->_out($out);
  12303. return $oid;
  12304. }
  12305. /**
  12306. * Output Catalog.
  12307. * @return int object id
  12308. * @protected
  12309. */
  12310. protected function _putcatalog() {
  12311. // put XMP
  12312. $xmpobj = $this->_putXMP();
  12313. // if required, add standard sRGB_IEC61966-2.1 blackscaled ICC colour profile
  12314. if ($this->pdfa_mode OR $this->force_srgb) {
  12315. $iccobj = $this->_newobj();
  12316. $icc = file_get_contents(dirname(__FILE__).'/sRGB.icc');
  12317. $filter = '';
  12318. if ($this->compress) {
  12319. $filter = ' /Filter /FlateDecode';
  12320. $icc = gzcompress($icc);
  12321. }
  12322. $icc = $this->_getrawstream($icc);
  12323. $this->_out('<</N 3 '.$filter.'/Length '.strlen($icc).'>> stream'."\n".$icc."\n".'endstream'."\n".'endobj');
  12324. }
  12325. // start catalog
  12326. $oid = $this->_newobj();
  12327. $out = '<< /Type /Catalog';
  12328. $out .= ' /Version /'.$this->PDFVersion;
  12329. //$out .= ' /Extensions <<>>';
  12330. $out .= ' /Pages 1 0 R';
  12331. //$out .= ' /PageLabels ' //...;
  12332. $out .= ' /Names <<';
  12333. if ((!$this->pdfa_mode) AND ((!empty($this->javascript)) OR (!empty($this->js_objects)))) {
  12334. $out .= ' /JavaScript '.($this->n_js).' 0 R';
  12335. }
  12336. $out .= ' >>';
  12337. if (!empty($this->dests)) {
  12338. $out .= ' /Dests '.$this->n_dests.' 0 R';
  12339. }
  12340. $out .= $this->_putviewerpreferences();
  12341. if (isset($this->LayoutMode) AND (!$this->empty_string($this->LayoutMode))) {
  12342. $out .= ' /PageLayout /'.$this->LayoutMode;
  12343. }
  12344. if (isset($this->PageMode) AND (!$this->empty_string($this->PageMode))) {
  12345. $out .= ' /PageMode /'.$this->PageMode;
  12346. }
  12347. if (count($this->outlines) > 0) {
  12348. $out .= ' /Outlines '.$this->OutlineRoot.' 0 R';
  12349. $out .= ' /PageMode /UseOutlines';
  12350. }
  12351. //$out .= ' /Threads []';
  12352. if ($this->ZoomMode == 'fullpage') {
  12353. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /Fit]';
  12354. } elseif ($this->ZoomMode == 'fullwidth') {
  12355. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /FitH null]';
  12356. } elseif ($this->ZoomMode == 'real') {
  12357. $out .= ' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null 1]';
  12358. } elseif (!is_string($this->ZoomMode)) {
  12359. $out .= sprintf(' /OpenAction ['.$this->page_obj_id[1].' 0 R /XYZ null null %F]', ($this->ZoomMode / 100));
  12360. }
  12361. //$out .= ' /AA <<>>';
  12362. //$out .= ' /URI <<>>';
  12363. $out .= ' /Metadata '.$xmpobj.' 0 R';
  12364. //$out .= ' /StructTreeRoot <<>>';
  12365. //$out .= ' /MarkInfo <<>>';
  12366. if (isset($this->l['a_meta_language'])) {
  12367. $out .= ' /Lang '.$this->_textstring($this->l['a_meta_language'], $oid);
  12368. }
  12369. //$out .= ' /SpiderInfo <<>>';
  12370. // set OutputIntent to sRGB IEC61966-2.1 if required
  12371. if ($this->pdfa_mode OR $this->force_srgb) {
  12372. $out .= ' /OutputIntents [<<';
  12373. $out .= ' /Type /OutputIntent';
  12374. $out .= ' /S /GTS_PDFA1';
  12375. $out .= ' /OutputCondition '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12376. $out .= ' /OutputConditionIdentifier '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12377. $out .= ' /RegistryName '.$this->_textstring('http://www.color.org', $oid);
  12378. $out .= ' /Info '.$this->_textstring('sRGB IEC61966-2.1', $oid);
  12379. $out .= ' /DestOutputProfile '.$iccobj.' 0 R';
  12380. $out .= ' >>]';
  12381. }
  12382. //$out .= ' /PieceInfo <<>>';
  12383. if (!empty($this->pdflayers)) {
  12384. $lyrobjs = '';
  12385. $lyrobjs_print = '';
  12386. $lyrobjs_view = '';
  12387. foreach ($this->pdflayers as $layer) {
  12388. $lyrobjs .= ' '.$layer['objid'].' 0 R';
  12389. if ($layer['print']) {
  12390. $lyrobjs_print .= ' '.$layer['objid'].' 0 R';
  12391. }
  12392. if ($layer['view']) {
  12393. $lyrobjs_view .= ' '.$layer['objid'].' 0 R';
  12394. }
  12395. }
  12396. $out .= ' /OCProperties << /OCGs ['.$lyrobjs.']';
  12397. $out .= ' /D <<';
  12398. $out .= ' /Name '.$this->_textstring('Layers', $oid);
  12399. $out .= ' /Creator '.$this->_textstring('TCPDF', $oid);
  12400. $out .= ' /BaseState /ON';
  12401. $out .= ' /ON ['.$lyrobjs_print.']';
  12402. $out .= ' /OFF ['.$lyrobjs_view.']';
  12403. $out .= ' /Intent /View';
  12404. $out .= ' /AS [';
  12405. $out .= ' << /Event /Print /OCGs ['.$lyrobjs.'] /Category [/Print] >>';
  12406. $out .= ' << /Event /View /OCGs ['.$lyrobjs.'] /Category [/View] >>';
  12407. $out .= ' ]';
  12408. $out .= ' /Order ['.$lyrobjs.']';
  12409. $out .= ' /ListMode /AllPages';
  12410. //$out .= ' /RBGroups ['..']';
  12411. //$out .= ' /Locked ['..']';
  12412. $out .= ' >>';
  12413. $out .= ' >>';
  12414. }
  12415. // AcroForm
  12416. if (!empty($this->form_obj_id) OR ($this->sign AND isset($this->signature_data['cert_type']))) {
  12417. $out .= ' /AcroForm <<';
  12418. $objrefs = '';
  12419. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12420. // set reference for signature object
  12421. $objrefs .= $this->sig_obj_id.' 0 R';
  12422. }
  12423. if (!empty($this->empty_signature_appearance)) {
  12424. foreach ($this->empty_signature_appearance as $esa) {
  12425. // set reference for empty signature objects
  12426. $objrefs .= ' '.$esa['objid'].' 0 R';
  12427. }
  12428. }
  12429. if (!empty($this->form_obj_id)) {
  12430. foreach($this->form_obj_id as $objid) {
  12431. $objrefs .= ' '.$objid.' 0 R';
  12432. }
  12433. }
  12434. $out .= ' /Fields ['.$objrefs.']';
  12435. // It's better to turn off this value and set the appearance stream for each annotation (/AP) to avoid conflicts with signature fields.
  12436. $out .= ' /NeedAppearances false';
  12437. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12438. if ($this->signature_data['cert_type'] > 0) {
  12439. $out .= ' /SigFlags 3';
  12440. } else {
  12441. $out .= ' /SigFlags 1';
  12442. }
  12443. }
  12444. //$out .= ' /CO ';
  12445. if (isset($this->annotation_fonts) AND !empty($this->annotation_fonts)) {
  12446. $out .= ' /DR <<';
  12447. $out .= ' /Font <<';
  12448. foreach ($this->annotation_fonts as $fontkey => $fontid) {
  12449. $out .= ' /F'.$fontid.' '.$this->font_obj_ids[$fontkey].' 0 R';
  12450. }
  12451. $out .= ' >> >>';
  12452. }
  12453. $font = $this->getFontBuffer('helvetica');
  12454. $out .= ' /DA (/F'.$font['i'].' 0 Tf 0 g)';
  12455. $out .= ' /Q '.(($this->rtl)?'2':'0');
  12456. //$out .= ' /XFA ';
  12457. $out .= ' >>';
  12458. // signatures
  12459. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12460. if ($this->signature_data['cert_type'] > 0) {
  12461. $out .= ' /Perms << /DocMDP '.($this->sig_obj_id + 1).' 0 R >>';
  12462. } else {
  12463. $out .= ' /Perms << /UR3 '.($this->sig_obj_id + 1).' 0 R >>';
  12464. }
  12465. }
  12466. }
  12467. //$out .= ' /Legal <<>>';
  12468. //$out .= ' /Requirements []';
  12469. //$out .= ' /Collection <<>>';
  12470. //$out .= ' /NeedsRendering true';
  12471. $out .= ' >>';
  12472. $out .= "\n".'endobj';
  12473. $this->_out($out);
  12474. return $oid;
  12475. }
  12476. /**
  12477. * Output viewer preferences.
  12478. * @return string for viewer preferences
  12479. * @author Nicola asuni
  12480. * @since 3.1.000 (2008-06-09)
  12481. * @protected
  12482. */
  12483. protected function _putviewerpreferences() {
  12484. $out = ' /ViewerPreferences <<';
  12485. if ($this->rtl) {
  12486. $out .= ' /Direction /R2L';
  12487. } else {
  12488. $out .= ' /Direction /L2R';
  12489. }
  12490. if (isset($this->viewer_preferences['HideToolbar']) AND ($this->viewer_preferences['HideToolbar'])) {
  12491. $out .= ' /HideToolbar true';
  12492. }
  12493. if (isset($this->viewer_preferences['HideMenubar']) AND ($this->viewer_preferences['HideMenubar'])) {
  12494. $out .= ' /HideMenubar true';
  12495. }
  12496. if (isset($this->viewer_preferences['HideWindowUI']) AND ($this->viewer_preferences['HideWindowUI'])) {
  12497. $out .= ' /HideWindowUI true';
  12498. }
  12499. if (isset($this->viewer_preferences['FitWindow']) AND ($this->viewer_preferences['FitWindow'])) {
  12500. $out .= ' /FitWindow true';
  12501. }
  12502. if (isset($this->viewer_preferences['CenterWindow']) AND ($this->viewer_preferences['CenterWindow'])) {
  12503. $out .= ' /CenterWindow true';
  12504. }
  12505. if (isset($this->viewer_preferences['DisplayDocTitle']) AND ($this->viewer_preferences['DisplayDocTitle'])) {
  12506. $out .= ' /DisplayDocTitle true';
  12507. }
  12508. if (isset($this->viewer_preferences['NonFullScreenPageMode'])) {
  12509. $out .= ' /NonFullScreenPageMode /'.$this->viewer_preferences['NonFullScreenPageMode'];
  12510. }
  12511. if (isset($this->viewer_preferences['ViewArea'])) {
  12512. $out .= ' /ViewArea /'.$this->viewer_preferences['ViewArea'];
  12513. }
  12514. if (isset($this->viewer_preferences['ViewClip'])) {
  12515. $out .= ' /ViewClip /'.$this->viewer_preferences['ViewClip'];
  12516. }
  12517. if (isset($this->viewer_preferences['PrintArea'])) {
  12518. $out .= ' /PrintArea /'.$this->viewer_preferences['PrintArea'];
  12519. }
  12520. if (isset($this->viewer_preferences['PrintClip'])) {
  12521. $out .= ' /PrintClip /'.$this->viewer_preferences['PrintClip'];
  12522. }
  12523. if (isset($this->viewer_preferences['PrintScaling'])) {
  12524. $out .= ' /PrintScaling /'.$this->viewer_preferences['PrintScaling'];
  12525. }
  12526. if (isset($this->viewer_preferences['Duplex']) AND (!$this->empty_string($this->viewer_preferences['Duplex']))) {
  12527. $out .= ' /Duplex /'.$this->viewer_preferences['Duplex'];
  12528. }
  12529. if (isset($this->viewer_preferences['PickTrayByPDFSize'])) {
  12530. if ($this->viewer_preferences['PickTrayByPDFSize']) {
  12531. $out .= ' /PickTrayByPDFSize true';
  12532. } else {
  12533. $out .= ' /PickTrayByPDFSize false';
  12534. }
  12535. }
  12536. if (isset($this->viewer_preferences['PrintPageRange'])) {
  12537. $PrintPageRangeNum = '';
  12538. foreach ($this->viewer_preferences['PrintPageRange'] as $k => $v) {
  12539. $PrintPageRangeNum .= ' '.($v - 1).'';
  12540. }
  12541. $out .= ' /PrintPageRange ['.substr($PrintPageRangeNum,1).']';
  12542. }
  12543. if (isset($this->viewer_preferences['NumCopies'])) {
  12544. $out .= ' /NumCopies '.intval($this->viewer_preferences['NumCopies']);
  12545. }
  12546. $out .= ' >>';
  12547. return $out;
  12548. }
  12549. /**
  12550. * Output PDF File Header (7.5.2).
  12551. * @protected
  12552. */
  12553. protected function _putheader() {
  12554. $this->_out('%PDF-'.$this->PDFVersion);
  12555. $this->_out('%'.chr(0xe2).chr(0xe3).chr(0xcf).chr(0xd3));
  12556. }
  12557. /**
  12558. * Output end of document (EOF).
  12559. * @protected
  12560. */
  12561. protected function _enddoc() {
  12562. $this->state = 1;
  12563. $this->_putheader();
  12564. $this->_putpages();
  12565. $this->_putresources();
  12566. // empty signature fields
  12567. if (!empty($this->empty_signature_appearance)) {
  12568. foreach ($this->empty_signature_appearance as $key => $esa) {
  12569. // widget annotation for empty signature
  12570. $out = $this->_getobj($esa['objid'])."\n";
  12571. $out .= '<< /Type /Annot';
  12572. $out .= ' /Subtype /Widget';
  12573. $out .= ' /Rect ['.$esa['rect'].']';
  12574. $out .= ' /P '.$this->page_obj_id[($esa['page'])].' 0 R'; // link to signature appearance page
  12575. $out .= ' /F 4';
  12576. $out .= ' /FT /Sig';
  12577. $signame = sprintf('Signature_%03d', ($key + 1));
  12578. $out .= ' /T '.$this->_textstring($signame, $esa['objid']);
  12579. $out .= ' /Ff 0';
  12580. $out .= ' >>';
  12581. $out .= "\n".'endobj';
  12582. $this->_out($out);
  12583. }
  12584. }
  12585. // Signature
  12586. if ($this->sign AND isset($this->signature_data['cert_type'])) {
  12587. // widget annotation for signature
  12588. $out = $this->_getobj($this->sig_obj_id)."\n";
  12589. $out .= '<< /Type /Annot';
  12590. $out .= ' /Subtype /Widget';
  12591. $out .= ' /Rect ['.$this->signature_appearance['rect'].']';
  12592. $out .= ' /P '.$this->page_obj_id[($this->signature_appearance['page'])].' 0 R'; // link to signature appearance page
  12593. $out .= ' /F 4';
  12594. $out .= ' /FT /Sig';
  12595. $out .= ' /T '.$this->_textstring('Signature_000', $this->sig_obj_id);
  12596. $out .= ' /Ff 0';
  12597. $out .= ' /V '.($this->sig_obj_id + 1).' 0 R';
  12598. $out .= ' >>';
  12599. $out .= "\n".'endobj';
  12600. $this->_out($out);
  12601. // signature
  12602. $this->_putsignature();
  12603. }
  12604. // Info
  12605. $objid_info = $this->_putinfo();
  12606. // Catalog
  12607. $objid_catalog = $this->_putcatalog();
  12608. // Cross-ref
  12609. $o = $this->bufferlen;
  12610. // XREF section
  12611. $this->_out('xref');
  12612. $this->_out('0 '.($this->n + 1));
  12613. $this->_out('0000000000 65535 f ');
  12614. $freegen = ($this->n + 2);
  12615. for ($i=1; $i <= $this->n; ++$i) {
  12616. if (!isset($this->offsets[$i]) AND ($i > 1)) {
  12617. $this->_out(sprintf('0000000000 %05d f ', $freegen));
  12618. ++$freegen;
  12619. } else {
  12620. $this->_out(sprintf('%010d 00000 n ', $this->offsets[$i]));
  12621. }
  12622. }
  12623. // TRAILER
  12624. $out = 'trailer'."\n";
  12625. $out .= '<<';
  12626. $out .= ' /Size '.($this->n + 1);
  12627. $out .= ' /Root '.$objid_catalog.' 0 R';
  12628. $out .= ' /Info '.$objid_info.' 0 R';
  12629. if ($this->encrypted) {
  12630. $out .= ' /Encrypt '.$this->encryptdata['objid'].' 0 R';
  12631. }
  12632. $out .= ' /ID [ <'.$this->file_id.'> <'.$this->file_id.'> ]';
  12633. $out .= ' >>';
  12634. $this->_out($out);
  12635. $this->_out('startxref');
  12636. $this->_out($o);
  12637. $this->_out('%%EOF');
  12638. $this->state = 3; // end-of-doc
  12639. if ($this->diskcache) {
  12640. // remove temporary files used for images
  12641. foreach ($this->imagekeys as $key) {
  12642. // remove temporary files
  12643. unlink($this->images[$key]);
  12644. }
  12645. foreach ($this->fontkeys as $key) {
  12646. // remove temporary files
  12647. unlink($this->fonts[$key]);
  12648. }
  12649. }
  12650. }
  12651. /**
  12652. * Initialize a new page.
  12653. * @param $orientation (string) page orientation. Possible values are (case insensitive):<ul><li>P or PORTRAIT (default)</li><li>L or LANDSCAPE</li></ul>
  12654. * @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().
  12655. * @protected
  12656. * @see getPageSizeFromFormat(), setPageFormat()
  12657. */
  12658. protected function _beginpage($orientation='', $format='') {
  12659. ++$this->page;
  12660. $this->pageobjects[$this->page] = array();
  12661. $this->setPageBuffer($this->page, '');
  12662. // initialize array for graphics tranformation positions inside a page buffer
  12663. $this->transfmrk[$this->page] = array();
  12664. $this->state = 2;
  12665. if ($this->empty_string($orientation)) {
  12666. if (isset($this->CurOrientation)) {
  12667. $orientation = $this->CurOrientation;
  12668. } elseif ($this->fwPt > $this->fhPt) {
  12669. // landscape
  12670. $orientation = 'L';
  12671. } else {
  12672. // portrait
  12673. $orientation = 'P';
  12674. }
  12675. }
  12676. if ($this->empty_string($format)) {
  12677. $this->pagedim[$this->page] = $this->pagedim[($this->page - 1)];
  12678. $this->setPageOrientation($orientation);
  12679. } else {
  12680. $this->setPageFormat($format, $orientation);
  12681. }
  12682. if ($this->rtl) {
  12683. $this->x = $this->w - $this->rMargin;
  12684. } else {
  12685. $this->x = $this->lMargin;
  12686. }
  12687. $this->y = $this->tMargin;
  12688. if (isset($this->newpagegroup[$this->page])) {
  12689. // start a new group
  12690. $this->currpagegroup = $this->newpagegroup[$this->page];
  12691. $this->pagegroups[$this->currpagegroup] = 1;
  12692. } elseif (isset($this->currpagegroup) AND ($this->currpagegroup > 0)) {
  12693. ++$this->pagegroups[$this->currpagegroup];
  12694. }
  12695. }
  12696. /**
  12697. * Mark end of page.
  12698. * @protected
  12699. */
  12700. protected function _endpage() {
  12701. $this->setVisibility('all');
  12702. $this->state = 1;
  12703. }
  12704. /**
  12705. * Begin a new object and return the object number.
  12706. * @return int object number
  12707. * @protected
  12708. */
  12709. protected function _newobj() {
  12710. $this->_out($this->_getobj());
  12711. return $this->n;
  12712. }
  12713. /**
  12714. * Return the starting object string for the selected object ID.
  12715. * @param $objid (int) Object ID (leave empty to get a new ID).
  12716. * @return string the starting object string
  12717. * @protected
  12718. * @since 5.8.009 (2010-08-20)
  12719. */
  12720. protected function _getobj($objid='') {
  12721. if ($objid === '') {
  12722. ++$this->n;
  12723. $objid = $this->n;
  12724. }
  12725. $this->offsets[$objid] = $this->bufferlen;
  12726. $this->pageobjects[$this->page][] = $objid;
  12727. return $objid.' 0 obj';
  12728. }
  12729. /**
  12730. * Underline text.
  12731. * @param $x (int) X coordinate
  12732. * @param $y (int) Y coordinate
  12733. * @param $txt (string) text to underline
  12734. * @protected
  12735. */
  12736. protected function _dounderline($x, $y, $txt) {
  12737. $w = $this->GetStringWidth($txt);
  12738. return $this->_dounderlinew($x, $y, $w);
  12739. }
  12740. /**
  12741. * Underline for rectangular text area.
  12742. * @param $x (int) X coordinate
  12743. * @param $y (int) Y coordinate
  12744. * @param $w (int) width to underline
  12745. * @protected
  12746. * @since 4.8.008 (2009-09-29)
  12747. */
  12748. protected function _dounderlinew($x, $y, $w) {
  12749. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12750. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew), $w * $this->k, $linew);
  12751. }
  12752. /**
  12753. * Line through text.
  12754. * @param $x (int) X coordinate
  12755. * @param $y (int) Y coordinate
  12756. * @param $txt (string) text to linethrough
  12757. * @protected
  12758. */
  12759. protected function _dolinethrough($x, $y, $txt) {
  12760. $w = $this->GetStringWidth($txt);
  12761. return $this->_dolinethroughw($x, $y, $w);
  12762. }
  12763. /**
  12764. * Line through for rectangular text area.
  12765. * @param $x (int) X coordinate
  12766. * @param $y (int) Y coordinate
  12767. * @param $w (int) line length (width)
  12768. * @protected
  12769. * @since 4.9.008 (2009-09-29)
  12770. */
  12771. protected function _dolinethroughw($x, $y, $w) {
  12772. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12773. return sprintf('%F %F %F %F re f', $x * $this->k, ((($this->h - $y) * $this->k) + $linew + ($this->FontSizePt / 3)), $w * $this->k, $linew);
  12774. }
  12775. /**
  12776. * Overline text.
  12777. * @param $x (int) X coordinate
  12778. * @param $y (int) Y coordinate
  12779. * @param $txt (string) text to overline
  12780. * @protected
  12781. * @since 4.9.015 (2010-04-19)
  12782. */
  12783. protected function _dooverline($x, $y, $txt) {
  12784. $w = $this->GetStringWidth($txt);
  12785. return $this->_dooverlinew($x, $y, $w);
  12786. }
  12787. /**
  12788. * Overline for rectangular text area.
  12789. * @param $x (int) X coordinate
  12790. * @param $y (int) Y coordinate
  12791. * @param $w (int) width to overline
  12792. * @protected
  12793. * @since 4.9.015 (2010-04-19)
  12794. */
  12795. protected function _dooverlinew($x, $y, $w) {
  12796. $linew = - $this->CurrentFont['ut'] / 1000 * $this->FontSizePt;
  12797. return sprintf('%F %F %F %F re f', $x * $this->k, (($this->h - $y + $this->FontAscent) * $this->k) - $linew, $w * $this->k, $linew);
  12798. }
  12799. /**
  12800. * Read a 4-byte (32 bit) integer from file.
  12801. * @param $f (string) file name.
  12802. * @return 4-byte integer
  12803. * @protected
  12804. */
  12805. protected function _freadint($f) {
  12806. $a = unpack('Ni', fread($f, 4));
  12807. return $a['i'];
  12808. }
  12809. /**
  12810. * Add "\" before "\", "(" and ")"
  12811. * @param $s (string) string to escape.
  12812. * @return string escaped string.
  12813. * @protected
  12814. */
  12815. protected function _escape($s) {
  12816. // the chr(13) substitution fixes the Bugs item #1421290.
  12817. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  12818. }
  12819. /**
  12820. * Format a data string for meta information
  12821. * @param $s (string) data string to escape.
  12822. * @param $n (int) object ID
  12823. * @return string escaped string.
  12824. * @protected
  12825. */
  12826. protected function _datastring($s, $n=0) {
  12827. if ($n == 0) {
  12828. $n = $this->n;
  12829. }
  12830. $s = $this->_encrypt_data($n, $s);
  12831. return '('. $this->_escape($s).')';
  12832. }
  12833. /**
  12834. * Set the document creation timestamp
  12835. * @param $time (mixed) Document creation timestamp in seconds or date-time string.
  12836. * @public
  12837. * @since 5.9.152 (2012-03-23)
  12838. */
  12839. public function setDocCreationTimestamp($time) {
  12840. if (is_string($time)) {
  12841. $time = getTimestamp($time);
  12842. }
  12843. $this->doc_creation_timestamp = intval($time);
  12844. }
  12845. /**
  12846. * Set the document modification timestamp
  12847. * @param $time (mixed) Document modification timestamp in seconds or date-time string.
  12848. * @public
  12849. * @since 5.9.152 (2012-03-23)
  12850. */
  12851. public function setDocModificationTimestamp($time) {
  12852. if (is_string($time)) {
  12853. $time = getTimestamp($time);
  12854. }
  12855. $this->doc_modification_timestamp = intval($time);
  12856. }
  12857. /**
  12858. * Returns document creation timestamp in seconds.
  12859. * @return (int) Creation timestamp in seconds.
  12860. * @public
  12861. * @since 5.9.152 (2012-03-23)
  12862. */
  12863. public function getDocCreationTimestamp() {
  12864. return $this->doc_creation_timestamp;
  12865. }
  12866. /**
  12867. * Returns document modification timestamp in seconds.
  12868. * @return (int) Modfication timestamp in seconds.
  12869. * @public
  12870. * @since 5.9.152 (2012-03-23)
  12871. */
  12872. public function getDocModificationTimestamp() {
  12873. return $this->doc_modification_timestamp;
  12874. }
  12875. /**
  12876. * Returns timestamp in seconds from formatted date-time.
  12877. * @param $date (string) Formatted date-time.
  12878. * @return int seconds.
  12879. * @public
  12880. * @since 5.9.152 (2012-03-23)
  12881. */
  12882. public function getTimestamp($date) {
  12883. if (($date[0] == 'D') AND ($date[1] == ':')) {
  12884. // remove date prefix if present
  12885. $date = substr($date, 2);
  12886. }
  12887. return strtotime($date);
  12888. }
  12889. /**
  12890. * Returns a formatted date-time.
  12891. * @param $time (int) Time in seconds.
  12892. * @return string escaped date string.
  12893. * @public
  12894. * @since 5.9.152 (2012-03-23)
  12895. */
  12896. public function getFormattedDate($time) {
  12897. return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
  12898. }
  12899. /**
  12900. * Returns a formatted date for meta information
  12901. * @param $n (int) Object ID.
  12902. * @param $timestamp (int) Timestamp to convert.
  12903. * @return string escaped date string.
  12904. * @protected
  12905. * @since 4.6.028 (2009-08-25)
  12906. */
  12907. protected function _datestring($n=0, $timestamp=0) {
  12908. if ((empty($timestamp)) OR ($timestamp < 0)) {
  12909. $timestamp = $this->doc_creation_timestamp;
  12910. }
  12911. return $this->_datastring('D:'.$this->getFormattedDate($timestamp), $n);
  12912. }
  12913. /**
  12914. * Format a text string for meta information
  12915. * @param $s (string) string to escape.
  12916. * @param $n (int) object ID
  12917. * @return string escaped string.
  12918. * @protected
  12919. */
  12920. protected function _textstring($s, $n=0) {
  12921. if ($this->isunicode) {
  12922. //Convert string to UTF-16BE
  12923. $s = $this->UTF8ToUTF16BE($s, true);
  12924. }
  12925. return $this->_datastring($s, $n);
  12926. }
  12927. /**
  12928. * THIS METHOD IS DEPRECATED
  12929. * Format a text string
  12930. * @param $s (string) string to escape.
  12931. * @return string escaped string.
  12932. * @protected
  12933. * @deprecated
  12934. */
  12935. protected function _escapetext($s) {
  12936. if ($this->isunicode) {
  12937. if (($this->CurrentFont['type'] == 'core') OR ($this->CurrentFont['type'] == 'TrueType') OR ($this->CurrentFont['type'] == 'Type1')) {
  12938. $s = $this->UTF8ToLatin1($s);
  12939. } else {
  12940. //Convert string to UTF-16BE and reverse RTL language
  12941. $s = $this->utf8StrRev($s, false, $this->tmprtl);
  12942. }
  12943. }
  12944. return $this->_escape($s);
  12945. }
  12946. /**
  12947. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  12948. * @param $str (string) Input string to convert.
  12949. * @return converted string
  12950. * @since 5.9.121 (2011-09-28)
  12951. * @protected
  12952. */
  12953. protected function _escapeXML($str) {
  12954. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  12955. $str = strtr($str, $replaceTable);
  12956. return $str;
  12957. }
  12958. /**
  12959. * get raw output stream.
  12960. * @param $s (string) string to output.
  12961. * @param $n (int) object reference for encryption mode
  12962. * @protected
  12963. * @author Nicola Asuni
  12964. * @since 5.5.000 (2010-06-22)
  12965. */
  12966. protected function _getrawstream($s, $n=0) {
  12967. if ($n <= 0) {
  12968. // default to current object
  12969. $n = $this->n;
  12970. }
  12971. return $this->_encrypt_data($n, $s);
  12972. }
  12973. /**
  12974. * Format output stream (DEPRECATED).
  12975. * @param $s (string) string to output.
  12976. * @param $n (int) object reference for encryption mode
  12977. * @protected
  12978. * @deprecated
  12979. */
  12980. protected function _getstream($s, $n=0) {
  12981. return 'stream'."\n".$this->_getrawstream($s, $n)."\n".'endstream';
  12982. }
  12983. /**
  12984. * Output a stream (DEPRECATED).
  12985. * @param $s (string) string to output.
  12986. * @param $n (int) object reference for encryption mode
  12987. * @protected
  12988. * @deprecated
  12989. */
  12990. protected function _putstream($s, $n=0) {
  12991. $this->_out($this->_getstream($s, $n));
  12992. }
  12993. /**
  12994. * Output a string to the document.
  12995. * @param $s (string) string to output.
  12996. * @protected
  12997. */
  12998. protected function _out($s) {
  12999. if ($this->state == 2) {
  13000. if ($this->inxobj) {
  13001. // we are inside an XObject template
  13002. $this->xobjects[$this->xobjid]['outdata'] .= $s."\n";
  13003. } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
  13004. // puts data before page footer
  13005. $pagebuff = $this->getPageBuffer($this->page);
  13006. $page = substr($pagebuff, 0, -$this->footerlen[$this->page]);
  13007. $footer = substr($pagebuff, -$this->footerlen[$this->page]);
  13008. $this->setPageBuffer($this->page, $page.$s."\n".$footer);
  13009. // update footer position
  13010. $this->footerpos[$this->page] += strlen($s."\n");
  13011. } else {
  13012. $this->setPageBuffer($this->page, $s."\n", true);
  13013. }
  13014. } else {
  13015. $this->setBuffer($s."\n");
  13016. }
  13017. }
  13018. /**
  13019. * Converts UTF-8 strings to codepoints array.<br>
  13020. * Invalid byte sequences will be replaced with 0xFFFD (replacement character)<br>
  13021. * Based on: http://www.faqs.org/rfcs/rfc3629.html
  13022. * <pre>
  13023. * Char. number range | UTF-8 octet sequence
  13024. * (hexadecimal) | (binary)
  13025. * --------------------+-----------------------------------------------
  13026. * 0000 0000-0000 007F | 0xxxxxxx
  13027. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  13028. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  13029. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  13030. * ---------------------------------------------------------------------
  13031. *
  13032. * ABFN notation:
  13033. * ---------------------------------------------------------------------
  13034. * UTF8-octets = *( UTF8-char )
  13035. * UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4
  13036. * UTF8-1 = %x00-7F
  13037. * UTF8-2 = %xC2-DF UTF8-tail
  13038. *
  13039. * UTF8-3 = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
  13040. * %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
  13041. * UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
  13042. * %xF4 %x80-8F 2( UTF8-tail )
  13043. * UTF8-tail = %x80-BF
  13044. * ---------------------------------------------------------------------
  13045. * </pre>
  13046. * @param $str (string) string to process.
  13047. * @return array containing codepoints (UTF-8 characters values)
  13048. * @protected
  13049. * @author Nicola Asuni
  13050. * @since 1.53.0.TC005 (2005-01-05)
  13051. */
  13052. protected function UTF8StringToArray($str) {
  13053. // build a unique string key
  13054. $strkey = md5($str);
  13055. if (isset($this->cache_UTF8StringToArray[$strkey])) {
  13056. // return cached value
  13057. $chrarray = $this->cache_UTF8StringToArray[$strkey]['s'];
  13058. if (!isset($this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']])) {
  13059. if ($this->isunicode) {
  13060. foreach ($chrarray as $chr) {
  13061. // store this char for font subsetting
  13062. $this->CurrentFont['subsetchars'][$chr] = true;
  13063. }
  13064. // update font subsetchars
  13065. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  13066. }
  13067. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  13068. }
  13069. return $chrarray;
  13070. }
  13071. // check cache size
  13072. if ($this->cache_size_UTF8StringToArray >= $this->cache_maxsize_UTF8StringToArray) {
  13073. // remove first element
  13074. array_shift($this->cache_UTF8StringToArray);
  13075. }
  13076. // new cache array for selected string
  13077. $this->cache_UTF8StringToArray[$strkey] = array('s' => array(), 'f' => array());
  13078. ++$this->cache_size_UTF8StringToArray;
  13079. if (!$this->isunicode) {
  13080. // split string into array of equivalent codes
  13081. $strarr = array();
  13082. $strlen = strlen($str);
  13083. for ($i=0; $i < $strlen; ++$i) {
  13084. $strarr[] = ord($str[$i]);
  13085. }
  13086. // insert new value on cache
  13087. $this->cache_UTF8StringToArray[$strkey]['s'] = $strarr;
  13088. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  13089. return $strarr;
  13090. }
  13091. $unichar = -1; // last unicode char
  13092. $unicode = array(); // array containing unicode values
  13093. $bytes = array(); // array containing single character byte sequences
  13094. $numbytes = 1; // number of octetc needed to represent the UTF-8 character
  13095. $str .= ''; // force $str to be a string
  13096. $length = strlen($str);
  13097. for ($i = 0; $i < $length; ++$i) {
  13098. $char = ord($str[$i]); // get one string character at time
  13099. if (count($bytes) == 0) { // get starting octect
  13100. if ($char <= 0x7F) {
  13101. $unichar = $char; // use the character "as is" because is ASCII
  13102. $numbytes = 1;
  13103. } elseif (($char >> 0x05) == 0x06) { // 2 bytes character (0x06 = 110 BIN)
  13104. $bytes[] = ($char - 0xC0) << 0x06;
  13105. $numbytes = 2;
  13106. } elseif (($char >> 0x04) == 0x0E) { // 3 bytes character (0x0E = 1110 BIN)
  13107. $bytes[] = ($char - 0xE0) << 0x0C;
  13108. $numbytes = 3;
  13109. } elseif (($char >> 0x03) == 0x1E) { // 4 bytes character (0x1E = 11110 BIN)
  13110. $bytes[] = ($char - 0xF0) << 0x12;
  13111. $numbytes = 4;
  13112. } else {
  13113. // use replacement character for other invalid sequences
  13114. $unichar = 0xFFFD;
  13115. $bytes = array();
  13116. $numbytes = 1;
  13117. }
  13118. } elseif (($char >> 0x06) == 0x02) { // bytes 2, 3 and 4 must start with 0x02 = 10 BIN
  13119. $bytes[] = $char - 0x80;
  13120. if (count($bytes) == $numbytes) {
  13121. // compose UTF-8 bytes to a single unicode value
  13122. $char = $bytes[0];
  13123. for ($j = 1; $j < $numbytes; ++$j) {
  13124. $char += ($bytes[$j] << (($numbytes - $j - 1) * 0x06));
  13125. }
  13126. if ((($char >= 0xD800) AND ($char <= 0xDFFF)) OR ($char >= 0x10FFFF)) {
  13127. /* The definition of UTF-8 prohibits encoding character numbers between
  13128. U+D800 and U+DFFF, which are reserved for use with the UTF-16
  13129. encoding form (as surrogate pairs) and do not directly represent
  13130. characters. */
  13131. $unichar = 0xFFFD; // use replacement character
  13132. } else {
  13133. $unichar = $char; // add char to array
  13134. }
  13135. // reset data for next char
  13136. $bytes = array();
  13137. $numbytes = 1;
  13138. }
  13139. } else {
  13140. // use replacement character for other invalid sequences
  13141. $unichar = 0xFFFD;
  13142. $bytes = array();
  13143. $numbytes = 1;
  13144. }
  13145. if ($unichar >= 0) {
  13146. // insert unicode value into array
  13147. $unicode[] = $unichar;
  13148. // store this char for font subsetting
  13149. $this->CurrentFont['subsetchars'][$unichar] = true;
  13150. $unichar = -1;
  13151. }
  13152. }
  13153. // update font subsetchars
  13154. $this->setFontSubBuffer($this->CurrentFont['fontkey'], 'subsetchars', $this->CurrentFont['subsetchars']);
  13155. // insert new value on cache
  13156. $this->cache_UTF8StringToArray[$strkey]['s'] = $unicode;
  13157. $this->cache_UTF8StringToArray[$strkey]['f'][$this->CurrentFont['fontkey']] = true;
  13158. return $unicode;
  13159. }
  13160. /**
  13161. * Converts UTF-8 strings to UTF16-BE.<br>
  13162. * @param $str (string) string to process.
  13163. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  13164. * @return string
  13165. * @author Nicola Asuni
  13166. * @since 1.53.0.TC005 (2005-01-05)
  13167. * @see UTF8StringToArray(), arrUTF8ToUTF16BE()
  13168. * @protected
  13169. */
  13170. protected function UTF8ToUTF16BE($str, $setbom=false) {
  13171. if (!$this->isunicode) {
  13172. return $str; // string is not in unicode
  13173. }
  13174. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  13175. return $this->arrUTF8ToUTF16BE($unicode, $setbom);
  13176. }
  13177. /**
  13178. * Converts UTF-8 strings to Latin1 when using the standard 14 core fonts.<br>
  13179. * @param $str (string) string to process.
  13180. * @return string
  13181. * @author Andrew Whitehead, Nicola Asuni
  13182. * @protected
  13183. * @since 3.2.000 (2008-06-23)
  13184. */
  13185. protected function UTF8ToLatin1($str) {
  13186. if (!$this->isunicode) {
  13187. return $str; // string is not in unicode
  13188. }
  13189. $outstr = ''; // string to be returned
  13190. $unicode = $this->UTF8StringToArray($str); // array containing UTF-8 unicode values
  13191. foreach ($unicode as $char) {
  13192. if ($char < 256) {
  13193. $outstr .= chr($char);
  13194. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  13195. // map from UTF-8
  13196. $outstr .= chr($this->unicode->uni_utf8tolatin[$char]);
  13197. } elseif ($char == 0xFFFD) {
  13198. // skip
  13199. } else {
  13200. $outstr .= '?';
  13201. }
  13202. }
  13203. return $outstr;
  13204. }
  13205. /**
  13206. * Converts UTF-8 characters array to array of Latin1 characters<br>
  13207. * @param $unicode (array) array containing UTF-8 unicode values
  13208. * @return array
  13209. * @author Nicola Asuni
  13210. * @protected
  13211. * @since 4.8.023 (2010-01-15)
  13212. */
  13213. protected function UTF8ArrToLatin1($unicode) {
  13214. if ((!$this->isunicode) OR $this->isUnicodeFont()) {
  13215. return $unicode;
  13216. }
  13217. $outarr = array(); // array to be returned
  13218. foreach ($unicode as $char) {
  13219. if ($char < 256) {
  13220. $outarr[] = $char;
  13221. } elseif (array_key_exists($char, $this->unicode->uni_utf8tolatin)) {
  13222. // map from UTF-8
  13223. $outarr[] = $this->unicode->uni_utf8tolatin[$char];
  13224. } elseif ($char == 0xFFFD) {
  13225. // skip
  13226. } else {
  13227. $outarr[] = 63; // '?' character
  13228. }
  13229. }
  13230. return $outarr;
  13231. }
  13232. /**
  13233. * Converts array of UTF-8 characters to UTF16-BE string.<br>
  13234. * Based on: http://www.faqs.org/rfcs/rfc2781.html
  13235. * <pre>
  13236. * Encoding UTF-16:
  13237. *
  13238. * Encoding of a single character from an ISO 10646 character value to
  13239. * UTF-16 proceeds as follows. Let U be the character number, no greater
  13240. * than 0x10FFFF.
  13241. *
  13242. * 1) If U < 0x10000, encode U as a 16-bit unsigned integer and
  13243. * terminate.
  13244. *
  13245. * 2) Let U' = U - 0x10000. Because U is less than or equal to 0x10FFFF,
  13246. * U' must be less than or equal to 0xFFFFF. That is, U' can be
  13247. * represented in 20 bits.
  13248. *
  13249. * 3) Initialize two 16-bit unsigned integers, W1 and W2, to 0xD800 and
  13250. * 0xDC00, respectively. These integers each have 10 bits free to
  13251. * encode the character value, for a total of 20 bits.
  13252. *
  13253. * 4) Assign the 10 high-order bits of the 20-bit U' to the 10 low-order
  13254. * bits of W1 and the 10 low-order bits of U' to the 10 low-order
  13255. * bits of W2. Terminate.
  13256. *
  13257. * Graphically, steps 2 through 4 look like:
  13258. * U' = yyyyyyyyyyxxxxxxxxxx
  13259. * W1 = 110110yyyyyyyyyy
  13260. * W2 = 110111xxxxxxxxxx
  13261. * </pre>
  13262. * @param $unicode (array) array containing UTF-8 unicode values
  13263. * @param $setbom (boolean) if true set the Byte Order Mark (BOM = 0xFEFF)
  13264. * @return string
  13265. * @protected
  13266. * @author Nicola Asuni
  13267. * @since 2.1.000 (2008-01-08)
  13268. * @see UTF8ToUTF16BE()
  13269. */
  13270. protected function arrUTF8ToUTF16BE($unicode, $setbom=false) {
  13271. $outstr = ''; // string to be returned
  13272. if ($setbom) {
  13273. $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
  13274. }
  13275. foreach ($unicode as $char) {
  13276. if ($char == 0x200b) {
  13277. // skip Unicode Character 'ZERO WIDTH SPACE' (DEC:8203, U+200B)
  13278. } elseif ($char == 0xFFFD) {
  13279. $outstr .= "\xFF\xFD"; // replacement character
  13280. } elseif ($char < 0x10000) {
  13281. $outstr .= chr($char >> 0x08);
  13282. $outstr .= chr($char & 0xFF);
  13283. } else {
  13284. $char -= 0x10000;
  13285. $w1 = 0xD800 | ($char >> 0x0a);
  13286. $w2 = 0xDC00 | ($char & 0x3FF);
  13287. $outstr .= chr($w1 >> 0x08);
  13288. $outstr .= chr($w1 & 0xFF);
  13289. $outstr .= chr($w2 >> 0x08);
  13290. $outstr .= chr($w2 & 0xFF);
  13291. }
  13292. }
  13293. return $outstr;
  13294. }
  13295. // ====================================================
  13296. /**
  13297. * Set header font.
  13298. * @param $font (array) font
  13299. * @public
  13300. * @since 1.1
  13301. */
  13302. public function setHeaderFont($font) {
  13303. $this->header_font = $font;
  13304. }
  13305. /**
  13306. * Get header font.
  13307. * @return array()
  13308. * @public
  13309. * @since 4.0.012 (2008-07-24)
  13310. */
  13311. public function getHeaderFont() {
  13312. return $this->header_font;
  13313. }
  13314. /**
  13315. * Set footer font.
  13316. * @param $font (array) font
  13317. * @public
  13318. * @since 1.1
  13319. */
  13320. public function setFooterFont($font) {
  13321. $this->footer_font = $font;
  13322. }
  13323. /**
  13324. * Get Footer font.
  13325. * @return array()
  13326. * @public
  13327. * @since 4.0.012 (2008-07-24)
  13328. */
  13329. public function getFooterFont() {
  13330. return $this->footer_font;
  13331. }
  13332. /**
  13333. * Set language array.
  13334. * @param $language (array)
  13335. * @public
  13336. * @since 1.1
  13337. */
  13338. public function setLanguageArray($language) {
  13339. $this->l = $language;
  13340. if (isset($this->l['a_meta_dir'])) {
  13341. $this->rtl = $this->l['a_meta_dir']=='rtl' ? true : false;
  13342. } else {
  13343. $this->rtl = false;
  13344. }
  13345. }
  13346. /**
  13347. * Returns the PDF data.
  13348. * @public
  13349. */
  13350. public function getPDFData() {
  13351. if ($this->state < 3) {
  13352. $this->Close();
  13353. }
  13354. return $this->buffer;
  13355. }
  13356. /**
  13357. * Output anchor link.
  13358. * @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;)
  13359. * @param $name (string) link name
  13360. * @param $fill (boolean) Indicates if the cell background must be painted (true) or transparent (false).
  13361. * @param $firstline (boolean) if true prints only the first line and return the remaining string.
  13362. * @param $color (array) array of RGB text color
  13363. * @param $style (string) font style (U, D, B, I)
  13364. * @param $firstblock (boolean) if true the string is the starting of a line.
  13365. * @return the number of cells used or the remaining text if $firstline = true;
  13366. * @public
  13367. */
  13368. public function addHtmlLink($url, $name, $fill=false, $firstline=false, $color='', $style=-1, $firstblock=false) {
  13369. if (!$this->empty_string($url) AND ($url{0} == '#')) {
  13370. // convert url to internal link
  13371. $lnkdata = explode(',', $url);
  13372. if (isset($lnkdata[0])) {
  13373. $page = intval(substr($lnkdata[0], 1));
  13374. if (empty($page) OR ($page <= 0)) {
  13375. $page = $this->page;
  13376. }
  13377. if (isset($lnkdata[1]) AND (strlen($lnkdata[1]) > 0)) {
  13378. $lnky = floatval($lnkdata[1]);
  13379. } else {
  13380. $lnky = 0;
  13381. }
  13382. $url = $this->AddLink();
  13383. $this->SetLink($url, $lnky, $page);
  13384. }
  13385. }
  13386. // store current settings
  13387. $prevcolor = $this->fgcolor;
  13388. $prevstyle = $this->FontStyle;
  13389. if (empty($color)) {
  13390. $this->SetTextColorArray($this->htmlLinkColorArray);
  13391. } else {
  13392. $this->SetTextColorArray($color);
  13393. }
  13394. if ($style == -1) {
  13395. $this->SetFont('', $this->FontStyle.$this->htmlLinkFontStyle);
  13396. } else {
  13397. $this->SetFont('', $this->FontStyle.$style);
  13398. }
  13399. $ret = $this->Write($this->lasth, $name, $url, $fill, '', false, 0, $firstline, $firstblock, 0);
  13400. // restore settings
  13401. $this->SetFont('', $prevstyle);
  13402. $this->SetTextColorArray($prevcolor);
  13403. return $ret;
  13404. }
  13405. /**
  13406. * 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.
  13407. * @param $hcolor (string) HTML color.
  13408. * @param $defcol (array) Color to return in case of error.
  13409. * @return array RGB or CMYK color, or false in case of error.
  13410. * @public
  13411. */
  13412. public function convertHTMLColorToDec($hcolor='#FFFFFF', $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  13413. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  13414. $color = strtolower($color);
  13415. // check for javascript color array syntax
  13416. if (strpos($color, '[') !== false) {
  13417. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  13418. $returncolor = array();
  13419. switch ($m[1]) {
  13420. case 'cmyk': {
  13421. // RGB
  13422. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  13423. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  13424. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  13425. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  13426. break;
  13427. }
  13428. case 'rgb': {
  13429. // RGB
  13430. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  13431. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  13432. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  13433. break;
  13434. }
  13435. case 'g': {
  13436. // grayscale
  13437. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  13438. break;
  13439. }
  13440. case 't':
  13441. default: {
  13442. // transparent (empty array)
  13443. break;
  13444. }
  13445. }
  13446. return $returncolor;
  13447. }
  13448. } elseif (($dotpos = strpos($color, '.')) !== false) {
  13449. // remove class parent (i.e.: color.red)
  13450. $color = substr($color, ($dotpos + 1));
  13451. if ($color == 'transparent') {
  13452. // transparent (empty array)
  13453. return array();