PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/tcpdf/include/tcpdf_static.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 2837 lines | 1715 code | 71 blank | 1051 comment | 253 complexity | 15f1595cbcbf1d6f0c7ceea6435fdfc9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf_static.php
  4. // Version : 1.0.002
  5. // Begin : 2002-08-03
  6. // Last Update : 2013-09-14
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2013 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the License
  25. // along with TCPDF. If not, see
  26. // <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
  27. //
  28. // See LICENSE.TXT file for more information.
  29. // -------------------------------------------------------------------
  30. //
  31. // Description :
  32. // Static methods used by the TCPDF class.
  33. //
  34. //============================================================+
  35. /**
  36. * @file
  37. * This is a PHP class that contains static methods for the TCPDF class.<br>
  38. * @package com.tecnick.tcpdf
  39. * @author Nicola Asuni
  40. * @version 1.0.002
  41. */
  42. /**
  43. * @class TCPDF_STATIC
  44. * Static methods used by the TCPDF class.
  45. * @package com.tecnick.tcpdf
  46. * @brief PHP class for generating PDF documents without requiring external extensions.
  47. * @version 1.0.002
  48. * @author Nicola Asuni - info@tecnick.com
  49. */
  50. class TCPDF_STATIC {
  51. /**
  52. * Current TCPDF version.
  53. * @private static
  54. */
  55. private static $tcpdf_version = '6.0.031';
  56. /**
  57. * String alias for total number of pages.
  58. * @public static
  59. */
  60. public static $alias_tot_pages = '{:ptp:}';
  61. /**
  62. * String alias for page number.
  63. * @public static
  64. */
  65. public static $alias_num_page = '{:pnp:}';
  66. /**
  67. * String alias for total number of pages in a single group.
  68. * @public static
  69. */
  70. public static $alias_group_tot_pages = '{:ptg:}';
  71. /**
  72. * String alias for group page number.
  73. * @public static
  74. */
  75. public static $alias_group_num_page = '{:png:}';
  76. /**
  77. * String alias for right shift compensation used to correctly align page numbers on the right.
  78. * @public static
  79. */
  80. public static $alias_right_shift = '{rsc:';
  81. /**
  82. * Encryption padding string.
  83. * @public static
  84. */
  85. public static $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";
  86. /**
  87. * ByteRange placemark used during digital signature process.
  88. * @since 4.6.028 (2009-08-25)
  89. * @public static
  90. */
  91. public static $byterange_string = '/ByteRange[0 ********** ********** **********]';
  92. /**
  93. * Array page boxes names
  94. * @public static
  95. */
  96. public static $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
  97. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  98. /**
  99. * Return the current TCPDF version.
  100. * @return TCPDF version string
  101. * @since 5.9.012 (2010-11-10)
  102. * @public static
  103. */
  104. public static function getTCPDFVersion() {
  105. return self::$tcpdf_version;
  106. }
  107. /**
  108. * Return the current TCPDF producer.
  109. * @return TCPDF producer string
  110. * @since 6.0.000 (2013-03-16)
  111. * @public static
  112. */
  113. public static function getTCPDFProducer() {
  114. return "\x54\x43\x50\x44\x46\x20".self::getTCPDFVersion()."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
  115. }
  116. /**
  117. * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
  118. * @param $mqr (boolean) FALSE for off, TRUE for on.
  119. * @since 4.6.025 (2009-08-17)
  120. * @public static
  121. */
  122. public static function set_mqr($mqr) {
  123. if (!defined('PHP_VERSION_ID')) {
  124. $version = PHP_VERSION;
  125. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  126. }
  127. if (PHP_VERSION_ID < 50300) {
  128. @set_magic_quotes_runtime($mqr);
  129. }
  130. }
  131. /**
  132. * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
  133. * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
  134. * @since 4.6.025 (2009-08-17)
  135. * @public static
  136. */
  137. public static function get_mqr() {
  138. if (!defined('PHP_VERSION_ID')) {
  139. $version = PHP_VERSION;
  140. define('PHP_VERSION_ID', (($version{0} * 10000) + ($version{2} * 100) + $version{4}));
  141. }
  142. if (PHP_VERSION_ID < 50300) {
  143. return @get_magic_quotes_runtime();
  144. }
  145. return 0;
  146. }
  147. /**
  148. * Get page dimensions from format name.
  149. * @param $format (mixed) The format name. It can be: <ul>
  150. * <li><b>ISO 216 A Series + 2 SIS 014711 extensions</b></li>
  151. * <li>A0 (841x1189 mm ; 33.11x46.81 in)</li>
  152. * <li>A1 (594x841 mm ; 23.39x33.11 in)</li>
  153. * <li>A2 (420x594 mm ; 16.54x23.39 in)</li>
  154. * <li>A3 (297x420 mm ; 11.69x16.54 in)</li>
  155. * <li>A4 (210x297 mm ; 8.27x11.69 in)</li>
  156. * <li>A5 (148x210 mm ; 5.83x8.27 in)</li>
  157. * <li>A6 (105x148 mm ; 4.13x5.83 in)</li>
  158. * <li>A7 (74x105 mm ; 2.91x4.13 in)</li>
  159. * <li>A8 (52x74 mm ; 2.05x2.91 in)</li>
  160. * <li>A9 (37x52 mm ; 1.46x2.05 in)</li>
  161. * <li>A10 (26x37 mm ; 1.02x1.46 in)</li>
  162. * <li>A11 (18x26 mm ; 0.71x1.02 in)</li>
  163. * <li>A12 (13x18 mm ; 0.51x0.71 in)</li>
  164. * <li><b>ISO 216 B Series + 2 SIS 014711 extensions</b></li>
  165. * <li>B0 (1000x1414 mm ; 39.37x55.67 in)</li>
  166. * <li>B1 (707x1000 mm ; 27.83x39.37 in)</li>
  167. * <li>B2 (500x707 mm ; 19.69x27.83 in)</li>
  168. * <li>B3 (353x500 mm ; 13.90x19.69 in)</li>
  169. * <li>B4 (250x353 mm ; 9.84x13.90 in)</li>
  170. * <li>B5 (176x250 mm ; 6.93x9.84 in)</li>
  171. * <li>B6 (125x176 mm ; 4.92x6.93 in)</li>
  172. * <li>B7 (88x125 mm ; 3.46x4.92 in)</li>
  173. * <li>B8 (62x88 mm ; 2.44x3.46 in)</li>
  174. * <li>B9 (44x62 mm ; 1.73x2.44 in)</li>
  175. * <li>B10 (31x44 mm ; 1.22x1.73 in)</li>
  176. * <li>B11 (22x31 mm ; 0.87x1.22 in)</li>
  177. * <li>B12 (15x22 mm ; 0.59x0.87 in)</li>
  178. * <li><b>ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION</b></li>
  179. * <li>C0 (917x1297 mm ; 36.10x51.06 in)</li>
  180. * <li>C1 (648x917 mm ; 25.51x36.10 in)</li>
  181. * <li>C2 (458x648 mm ; 18.03x25.51 in)</li>
  182. * <li>C3 (324x458 mm ; 12.76x18.03 in)</li>
  183. * <li>C4 (229x324 mm ; 9.02x12.76 in)</li>
  184. * <li>C5 (162x229 mm ; 6.38x9.02 in)</li>
  185. * <li>C6 (114x162 mm ; 4.49x6.38 in)</li>
  186. * <li>C7 (81x114 mm ; 3.19x4.49 in)</li>
  187. * <li>C8 (57x81 mm ; 2.24x3.19 in)</li>
  188. * <li>C9 (40x57 mm ; 1.57x2.24 in)</li>
  189. * <li>C10 (28x40 mm ; 1.10x1.57 in)</li>
  190. * <li>C11 (20x28 mm ; 0.79x1.10 in)</li>
  191. * <li>C12 (14x20 mm ; 0.55x0.79 in)</li>
  192. * <li>C76 (81x162 mm ; 3.19x6.38 in)</li>
  193. * <li>DL (110x220 mm ; 4.33x8.66 in)</li>
  194. * <li><b>SIS 014711 E Series</b></li>
  195. * <li>E0 (879x1241 mm ; 34.61x48.86 in)</li>
  196. * <li>E1 (620x879 mm ; 24.41x34.61 in)</li>
  197. * <li>E2 (440x620 mm ; 17.32x24.41 in)</li>
  198. * <li>E3 (310x440 mm ; 12.20x17.32 in)</li>
  199. * <li>E4 (220x310 mm ; 8.66x12.20 in)</li>
  200. * <li>E5 (155x220 mm ; 6.10x8.66 in)</li>
  201. * <li>E6 (110x155 mm ; 4.33x6.10 in)</li>
  202. * <li>E7 (78x110 mm ; 3.07x4.33 in)</li>
  203. * <li>E8 (55x78 mm ; 2.17x3.07 in)</li>
  204. * <li>E9 (39x55 mm ; 1.54x2.17 in)</li>
  205. * <li>E10 (27x39 mm ; 1.06x1.54 in)</li>
  206. * <li>E11 (19x27 mm ; 0.75x1.06 in)</li>
  207. * <li>E12 (13x19 mm ; 0.51x0.75 in)</li>
  208. * <li><b>SIS 014711 G Series</b></li>
  209. * <li>G0 (958x1354 mm ; 37.72x53.31 in)</li>
  210. * <li>G1 (677x958 mm ; 26.65x37.72 in)</li>
  211. * <li>G2 (479x677 mm ; 18.86x26.65 in)</li>
  212. * <li>G3 (338x479 mm ; 13.31x18.86 in)</li>
  213. * <li>G4 (239x338 mm ; 9.41x13.31 in)</li>
  214. * <li>G5 (169x239 mm ; 6.65x9.41 in)</li>
  215. * <li>G6 (119x169 mm ; 4.69x6.65 in)</li>
  216. * <li>G7 (84x119 mm ; 3.31x4.69 in)</li>
  217. * <li>G8 (59x84 mm ; 2.32x3.31 in)</li>
  218. * <li>G9 (42x59 mm ; 1.65x2.32 in)</li>
  219. * <li>G10 (29x42 mm ; 1.14x1.65 in)</li>
  220. * <li>G11 (21x29 mm ; 0.83x1.14 in)</li>
  221. * <li>G12 (14x21 mm ; 0.55x0.83 in)</li>
  222. * <li><b>ISO Press</b></li>
  223. * <li>RA0 (860x1220 mm ; 33.86x48.03 in)</li>
  224. * <li>RA1 (610x860 mm ; 24.02x33.86 in)</li>
  225. * <li>RA2 (430x610 mm ; 16.93x24.02 in)</li>
  226. * <li>RA3 (305x430 mm ; 12.01x16.93 in)</li>
  227. * <li>RA4 (215x305 mm ; 8.46x12.01 in)</li>
  228. * <li>SRA0 (900x1280 mm ; 35.43x50.39 in)</li>
  229. * <li>SRA1 (640x900 mm ; 25.20x35.43 in)</li>
  230. * <li>SRA2 (450x640 mm ; 17.72x25.20 in)</li>
  231. * <li>SRA3 (320x450 mm ; 12.60x17.72 in)</li>
  232. * <li>SRA4 (225x320 mm ; 8.86x12.60 in)</li>
  233. * <li><b>German DIN 476</b></li>
  234. * <li>4A0 (1682x2378 mm ; 66.22x93.62 in)</li>
  235. * <li>2A0 (1189x1682 mm ; 46.81x66.22 in)</li>
  236. * <li><b>Variations on the ISO Standard</b></li>
  237. * <li>A2_EXTRA (445x619 mm ; 17.52x24.37 in)</li>
  238. * <li>A3+ (329x483 mm ; 12.95x19.02 in)</li>
  239. * <li>A3_EXTRA (322x445 mm ; 12.68x17.52 in)</li>
  240. * <li>A3_SUPER (305x508 mm ; 12.01x20.00 in)</li>
  241. * <li>SUPER_A3 (305x487 mm ; 12.01x19.17 in)</li>
  242. * <li>A4_EXTRA (235x322 mm ; 9.25x12.68 in)</li>
  243. * <li>A4_SUPER (229x322 mm ; 9.02x12.68 in)</li>
  244. * <li>SUPER_A4 (227x356 mm ; 8.94x14.02 in)</li>
  245. * <li>A4_LONG (210x348 mm ; 8.27x13.70 in)</li>
  246. * <li>F4 (210x330 mm ; 8.27x12.99 in)</li>
  247. * <li>SO_B5_EXTRA (202x276 mm ; 7.95x10.87 in)</li>
  248. * <li>A5_EXTRA (173x235 mm ; 6.81x9.25 in)</li>
  249. * <li><b>ANSI Series</b></li>
  250. * <li>ANSI_E (864x1118 mm ; 34.00x44.00 in)</li>
  251. * <li>ANSI_D (559x864 mm ; 22.00x34.00 in)</li>
  252. * <li>ANSI_C (432x559 mm ; 17.00x22.00 in)</li>
  253. * <li>ANSI_B (279x432 mm ; 11.00x17.00 in)</li>
  254. * <li>ANSI_A (216x279 mm ; 8.50x11.00 in)</li>
  255. * <li><b>Traditional 'Loose' North American Paper Sizes</b></li>
  256. * <li>LEDGER, USLEDGER (432x279 mm ; 17.00x11.00 in)</li>
  257. * <li>TABLOID, USTABLOID, BIBLE, ORGANIZERK (279x432 mm ; 11.00x17.00 in)</li>
  258. * <li>LETTER, USLETTER, ORGANIZERM (216x279 mm ; 8.50x11.00 in)</li>
  259. * <li>LEGAL, USLEGAL (216x356 mm ; 8.50x14.00 in)</li>
  260. * <li>GLETTER, GOVERNMENTLETTER (203x267 mm ; 8.00x10.50 in)</li>
  261. * <li>JLEGAL, JUNIORLEGAL (203x127 mm ; 8.00x5.00 in)</li>
  262. * <li><b>Other North American Paper Sizes</b></li>
  263. * <li>QUADDEMY (889x1143 mm ; 35.00x45.00 in)</li>
  264. * <li>SUPER_B (330x483 mm ; 13.00x19.00 in)</li>
  265. * <li>QUARTO (229x279 mm ; 9.00x11.00 in)</li>
  266. * <li>FOLIO, GOVERNMENTLEGAL (216x330 mm ; 8.50x13.00 in)</li>
  267. * <li>EXECUTIVE, MONARCH (184x267 mm ; 7.25x10.50 in)</li>
  268. * <li>MEMO, STATEMENT, ORGANIZERL (140x216 mm ; 5.50x8.50 in)</li>
  269. * <li>FOOLSCAP (210x330 mm ; 8.27x13.00 in)</li>
  270. * <li>COMPACT (108x171 mm ; 4.25x6.75 in)</li>
  271. * <li>ORGANIZERJ (70x127 mm ; 2.75x5.00 in)</li>
  272. * <li><b>Canadian standard CAN 2-9.60M</b></li>
  273. * <li>P1 (560x860 mm ; 22.05x33.86 in)</li>
  274. * <li>P2 (430x560 mm ; 16.93x22.05 in)</li>
  275. * <li>P3 (280x430 mm ; 11.02x16.93 in)</li>
  276. * <li>P4 (215x280 mm ; 8.46x11.02 in)</li>
  277. * <li>P5 (140x215 mm ; 5.51x8.46 in)</li>
  278. * <li>P6 (107x140 mm ; 4.21x5.51 in)</li>
  279. * <li><b>North American Architectural Sizes</b></li>
  280. * <li>ARCH_E (914x1219 mm ; 36.00x48.00 in)</li>
  281. * <li>ARCH_E1 (762x1067 mm ; 30.00x42.00 in)</li>
  282. * <li>ARCH_D (610x914 mm ; 24.00x36.00 in)</li>
  283. * <li>ARCH_C, BROADSHEET (457x610 mm ; 18.00x24.00 in)</li>
  284. * <li>ARCH_B (305x457 mm ; 12.00x18.00 in)</li>
  285. * <li>ARCH_A (229x305 mm ; 9.00x12.00 in)</li>
  286. * <li><b>Announcement Envelopes</b></li>
  287. * <li>ANNENV_A2 (111x146 mm ; 4.37x5.75 in)</li>
  288. * <li>ANNENV_A6 (121x165 mm ; 4.75x6.50 in)</li>
  289. * <li>ANNENV_A7 (133x184 mm ; 5.25x7.25 in)</li>
  290. * <li>ANNENV_A8 (140x206 mm ; 5.50x8.12 in)</li>
  291. * <li>ANNENV_A10 (159x244 mm ; 6.25x9.62 in)</li>
  292. * <li>ANNENV_SLIM (98x225 mm ; 3.87x8.87 in)</li>
  293. * <li><b>Commercial Envelopes</b></li>
  294. * <li>COMMENV_N6_1/4 (89x152 mm ; 3.50x6.00 in)</li>
  295. * <li>COMMENV_N6_3/4 (92x165 mm ; 3.62x6.50 in)</li>
  296. * <li>COMMENV_N8 (98x191 mm ; 3.87x7.50 in)</li>
  297. * <li>COMMENV_N9 (98x225 mm ; 3.87x8.87 in)</li>
  298. * <li>COMMENV_N10 (105x241 mm ; 4.12x9.50 in)</li>
  299. * <li>COMMENV_N11 (114x263 mm ; 4.50x10.37 in)</li>
  300. * <li>COMMENV_N12 (121x279 mm ; 4.75x11.00 in)</li>
  301. * <li>COMMENV_N14 (127x292 mm ; 5.00x11.50 in)</li>
  302. * <li><b>Catalogue Envelopes</b></li>
  303. * <li>CATENV_N1 (152x229 mm ; 6.00x9.00 in)</li>
  304. * <li>CATENV_N1_3/4 (165x241 mm ; 6.50x9.50 in)</li>
  305. * <li>CATENV_N2 (165x254 mm ; 6.50x10.00 in)</li>
  306. * <li>CATENV_N3 (178x254 mm ; 7.00x10.00 in)</li>
  307. * <li>CATENV_N6 (191x267 mm ; 7.50x10.50 in)</li>
  308. * <li>CATENV_N7 (203x279 mm ; 8.00x11.00 in)</li>
  309. * <li>CATENV_N8 (210x286 mm ; 8.25x11.25 in)</li>
  310. * <li>CATENV_N9_1/2 (216x267 mm ; 8.50x10.50 in)</li>
  311. * <li>CATENV_N9_3/4 (222x286 mm ; 8.75x11.25 in)</li>
  312. * <li>CATENV_N10_1/2 (229x305 mm ; 9.00x12.00 in)</li>
  313. * <li>CATENV_N12_1/2 (241x318 mm ; 9.50x12.50 in)</li>
  314. * <li>CATENV_N13_1/2 (254x330 mm ; 10.00x13.00 in)</li>
  315. * <li>CATENV_N14_1/4 (286x311 mm ; 11.25x12.25 in)</li>
  316. * <li>CATENV_N14_1/2 (292x368 mm ; 11.50x14.50 in)</li>
  317. * <li><b>Japanese (JIS P 0138-61) Standard B-Series</b></li>
  318. * <li>JIS_B0 (1030x1456 mm ; 40.55x57.32 in)</li>
  319. * <li>JIS_B1 (728x1030 mm ; 28.66x40.55 in)</li>
  320. * <li>JIS_B2 (515x728 mm ; 20.28x28.66 in)</li>
  321. * <li>JIS_B3 (364x515 mm ; 14.33x20.28 in)</li>
  322. * <li>JIS_B4 (257x364 mm ; 10.12x14.33 in)</li>
  323. * <li>JIS_B5 (182x257 mm ; 7.17x10.12 in)</li>
  324. * <li>JIS_B6 (128x182 mm ; 5.04x7.17 in)</li>
  325. * <li>JIS_B7 (91x128 mm ; 3.58x5.04 in)</li>
  326. * <li>JIS_B8 (64x91 mm ; 2.52x3.58 in)</li>
  327. * <li>JIS_B9 (45x64 mm ; 1.77x2.52 in)</li>
  328. * <li>JIS_B10 (32x45 mm ; 1.26x1.77 in)</li>
  329. * <li>JIS_B11 (22x32 mm ; 0.87x1.26 in)</li>
  330. * <li>JIS_B12 (16x22 mm ; 0.63x0.87 in)</li>
  331. * <li><b>PA Series</b></li>
  332. * <li>PA0 (840x1120 mm ; 33.07x44.09 in)</li>
  333. * <li>PA1 (560x840 mm ; 22.05x33.07 in)</li>
  334. * <li>PA2 (420x560 mm ; 16.54x22.05 in)</li>
  335. * <li>PA3 (280x420 mm ; 11.02x16.54 in)</li>
  336. * <li>PA4 (210x280 mm ; 8.27x11.02 in)</li>
  337. * <li>PA5 (140x210 mm ; 5.51x8.27 in)</li>
  338. * <li>PA6 (105x140 mm ; 4.13x5.51 in)</li>
  339. * <li>PA7 (70x105 mm ; 2.76x4.13 in)</li>
  340. * <li>PA8 (52x70 mm ; 2.05x2.76 in)</li>
  341. * <li>PA9 (35x52 mm ; 1.38x2.05 in)</li>
  342. * <li>PA10 (26x35 mm ; 1.02x1.38 in)</li>
  343. * <li><b>Standard Photographic Print Sizes</b></li>
  344. * <li>PASSPORT_PHOTO (35x45 mm ; 1.38x1.77 in)</li>
  345. * <li>E (82x120 mm ; 3.25x4.72 in)</li>
  346. * <li>3R, L (89x127 mm ; 3.50x5.00 in)</li>
  347. * <li>4R, KG (102x152 mm ; 4.02x5.98 in)</li>
  348. * <li>4D (120x152 mm ; 4.72x5.98 in)</li>
  349. * <li>5R, 2L (127x178 mm ; 5.00x7.01 in)</li>
  350. * <li>6R, 8P (152x203 mm ; 5.98x7.99 in)</li>
  351. * <li>8R, 6P (203x254 mm ; 7.99x10.00 in)</li>
  352. * <li>S8R, 6PW (203x305 mm ; 7.99x12.01 in)</li>
  353. * <li>10R, 4P (254x305 mm ; 10.00x12.01 in)</li>
  354. * <li>S10R, 4PW (254x381 mm ; 10.00x15.00 in)</li>
  355. * <li>11R (279x356 mm ; 10.98x14.02 in)</li>
  356. * <li>S11R (279x432 mm ; 10.98x17.01 in)</li>
  357. * <li>12R (305x381 mm ; 12.01x15.00 in)</li>
  358. * <li>S12R (305x456 mm ; 12.01x17.95 in)</li>
  359. * <li><b>Common Newspaper Sizes</b></li>
  360. * <li>NEWSPAPER_BROADSHEET (750x600 mm ; 29.53x23.62 in)</li>
  361. * <li>NEWSPAPER_BERLINER (470x315 mm ; 18.50x12.40 in)</li>
  362. * <li>NEWSPAPER_COMPACT, NEWSPAPER_TABLOID (430x280 mm ; 16.93x11.02 in)</li>
  363. * <li><b>Business Cards</b></li>
  364. * <li>CREDIT_CARD, BUSINESS_CARD, BUSINESS_CARD_ISO7810 (54x86 mm ; 2.13x3.37 in)</li>
  365. * <li>BUSINESS_CARD_ISO216 (52x74 mm ; 2.05x2.91 in)</li>
  366. * <li>BUSINESS_CARD_IT, BUSINESS_CARD_UK, BUSINESS_CARD_FR, BUSINESS_CARD_DE, BUSINESS_CARD_ES (55x85 mm ; 2.17x3.35 in)</li>
  367. * <li>BUSINESS_CARD_US, BUSINESS_CARD_CA (51x89 mm ; 2.01x3.50 in)</li>
  368. * <li>BUSINESS_CARD_JP (55x91 mm ; 2.17x3.58 in)</li>
  369. * <li>BUSINESS_CARD_HK (54x90 mm ; 2.13x3.54 in)</li>
  370. * <li>BUSINESS_CARD_AU, BUSINESS_CARD_DK, BUSINESS_CARD_SE (55x90 mm ; 2.17x3.54 in)</li>
  371. * <li>BUSINESS_CARD_RU, BUSINESS_CARD_CZ, BUSINESS_CARD_FI, BUSINESS_CARD_HU, BUSINESS_CARD_IL (50x90 mm ; 1.97x3.54 in)</li>
  372. * <li><b>Billboards</b></li>
  373. * <li>4SHEET (1016x1524 mm ; 40.00x60.00 in)</li>
  374. * <li>6SHEET (1200x1800 mm ; 47.24x70.87 in)</li>
  375. * <li>12SHEET (3048x1524 mm ; 120.00x60.00 in)</li>
  376. * <li>16SHEET (2032x3048 mm ; 80.00x120.00 in)</li>
  377. * <li>32SHEET (4064x3048 mm ; 160.00x120.00 in)</li>
  378. * <li>48SHEET (6096x3048 mm ; 240.00x120.00 in)</li>
  379. * <li>64SHEET (8128x3048 mm ; 320.00x120.00 in)</li>
  380. * <li>96SHEET (12192x3048 mm ; 480.00x120.00 in)</li>
  381. * <li><b>Old Imperial English (some are still used in USA)</b></li>
  382. * <li>EN_EMPEROR (1219x1829 mm ; 48.00x72.00 in)</li>
  383. * <li>EN_ANTIQUARIAN (787x1346 mm ; 31.00x53.00 in)</li>
  384. * <li>EN_GRAND_EAGLE (730x1067 mm ; 28.75x42.00 in)</li>
  385. * <li>EN_DOUBLE_ELEPHANT (679x1016 mm ; 26.75x40.00 in)</li>
  386. * <li>EN_ATLAS (660x864 mm ; 26.00x34.00 in)</li>
  387. * <li>EN_COLOMBIER (597x876 mm ; 23.50x34.50 in)</li>
  388. * <li>EN_ELEPHANT (584x711 mm ; 23.00x28.00 in)</li>
  389. * <li>EN_DOUBLE_DEMY (572x902 mm ; 22.50x35.50 in)</li>
  390. * <li>EN_IMPERIAL (559x762 mm ; 22.00x30.00 in)</li>
  391. * <li>EN_PRINCESS (546x711 mm ; 21.50x28.00 in)</li>
  392. * <li>EN_CARTRIDGE (533x660 mm ; 21.00x26.00 in)</li>
  393. * <li>EN_DOUBLE_LARGE_POST (533x838 mm ; 21.00x33.00 in)</li>
  394. * <li>EN_ROYAL (508x635 mm ; 20.00x25.00 in)</li>
  395. * <li>EN_SHEET, EN_HALF_POST (495x597 mm ; 19.50x23.50 in)</li>
  396. * <li>EN_SUPER_ROYAL (483x686 mm ; 19.00x27.00 in)</li>
  397. * <li>EN_DOUBLE_POST (483x775 mm ; 19.00x30.50 in)</li>
  398. * <li>EN_MEDIUM (445x584 mm ; 17.50x23.00 in)</li>
  399. * <li>EN_DEMY (445x572 mm ; 17.50x22.50 in)</li>
  400. * <li>EN_LARGE_POST (419x533 mm ; 16.50x21.00 in)</li>
  401. * <li>EN_COPY_DRAUGHT (406x508 mm ; 16.00x20.00 in)</li>
  402. * <li>EN_POST (394x489 mm ; 15.50x19.25 in)</li>
  403. * <li>EN_CROWN (381x508 mm ; 15.00x20.00 in)</li>
  404. * <li>EN_PINCHED_POST (375x470 mm ; 14.75x18.50 in)</li>
  405. * <li>EN_BRIEF (343x406 mm ; 13.50x16.00 in)</li>
  406. * <li>EN_FOOLSCAP (343x432 mm ; 13.50x17.00 in)</li>
  407. * <li>EN_SMALL_FOOLSCAP (337x419 mm ; 13.25x16.50 in)</li>
  408. * <li>EN_POTT (318x381 mm ; 12.50x15.00 in)</li>
  409. * <li><b>Old Imperial Belgian</b></li>
  410. * <li>BE_GRAND_AIGLE (700x1040 mm ; 27.56x40.94 in)</li>
  411. * <li>BE_COLOMBIER (620x850 mm ; 24.41x33.46 in)</li>
  412. * <li>BE_DOUBLE_CARRE (620x920 mm ; 24.41x36.22 in)</li>
  413. * <li>BE_ELEPHANT (616x770 mm ; 24.25x30.31 in)</li>
  414. * <li>BE_PETIT_AIGLE (600x840 mm ; 23.62x33.07 in)</li>
  415. * <li>BE_GRAND_JESUS (550x730 mm ; 21.65x28.74 in)</li>
  416. * <li>BE_JESUS (540x730 mm ; 21.26x28.74 in)</li>
  417. * <li>BE_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  418. * <li>BE_GRAND_MEDIAN (460x605 mm ; 18.11x23.82 in)</li>
  419. * <li>BE_DOUBLE_POSTE (435x565 mm ; 17.13x22.24 in)</li>
  420. * <li>BE_COQUILLE (430x560 mm ; 16.93x22.05 in)</li>
  421. * <li>BE_PETIT_MEDIAN (415x530 mm ; 16.34x20.87 in)</li>
  422. * <li>BE_RUCHE (360x460 mm ; 14.17x18.11 in)</li>
  423. * <li>BE_PROPATRIA (345x430 mm ; 13.58x16.93 in)</li>
  424. * <li>BE_LYS (317x397 mm ; 12.48x15.63 in)</li>
  425. * <li>BE_POT (307x384 mm ; 12.09x15.12 in)</li>
  426. * <li>BE_ROSETTE (270x347 mm ; 10.63x13.66 in)</li>
  427. * <li><b>Old Imperial French</b></li>
  428. * <li>FR_UNIVERS (1000x1300 mm ; 39.37x51.18 in)</li>
  429. * <li>FR_DOUBLE_COLOMBIER (900x1260 mm ; 35.43x49.61 in)</li>
  430. * <li>FR_GRANDE_MONDE (900x1260 mm ; 35.43x49.61 in)</li>
  431. * <li>FR_DOUBLE_SOLEIL (800x1200 mm ; 31.50x47.24 in)</li>
  432. * <li>FR_DOUBLE_JESUS (760x1120 mm ; 29.92x44.09 in)</li>
  433. * <li>FR_GRAND_AIGLE (750x1060 mm ; 29.53x41.73 in)</li>
  434. * <li>FR_PETIT_AIGLE (700x940 mm ; 27.56x37.01 in)</li>
  435. * <li>FR_DOUBLE_RAISIN (650x1000 mm ; 25.59x39.37 in)</li>
  436. * <li>FR_JOURNAL (650x940 mm ; 25.59x37.01 in)</li>
  437. * <li>FR_COLOMBIER_AFFICHE (630x900 mm ; 24.80x35.43 in)</li>
  438. * <li>FR_DOUBLE_CAVALIER (620x920 mm ; 24.41x36.22 in)</li>
  439. * <li>FR_CLOCHE (600x800 mm ; 23.62x31.50 in)</li>
  440. * <li>FR_SOLEIL (600x800 mm ; 23.62x31.50 in)</li>
  441. * <li>FR_DOUBLE_CARRE (560x900 mm ; 22.05x35.43 in)</li>
  442. * <li>FR_DOUBLE_COQUILLE (560x880 mm ; 22.05x34.65 in)</li>
  443. * <li>FR_JESUS (560x760 mm ; 22.05x29.92 in)</li>
  444. * <li>FR_RAISIN (500x650 mm ; 19.69x25.59 in)</li>
  445. * <li>FR_CAVALIER (460x620 mm ; 18.11x24.41 in)</li>
  446. * <li>FR_DOUBLE_COURONNE (460x720 mm ; 18.11x28.35 in)</li>
  447. * <li>FR_CARRE (450x560 mm ; 17.72x22.05 in)</li>
  448. * <li>FR_COQUILLE (440x560 mm ; 17.32x22.05 in)</li>
  449. * <li>FR_DOUBLE_TELLIERE (440x680 mm ; 17.32x26.77 in)</li>
  450. * <li>FR_DOUBLE_CLOCHE (400x600 mm ; 15.75x23.62 in)</li>
  451. * <li>FR_DOUBLE_POT (400x620 mm ; 15.75x24.41 in)</li>
  452. * <li>FR_ECU (400x520 mm ; 15.75x20.47 in)</li>
  453. * <li>FR_COURONNE (360x460 mm ; 14.17x18.11 in)</li>
  454. * <li>FR_TELLIERE (340x440 mm ; 13.39x17.32 in)</li>
  455. * <li>FR_POT (310x400 mm ; 12.20x15.75 in)</li>
  456. * </ul>
  457. * @return array containing page width and height in points
  458. * @since 5.0.010 (2010-05-17)
  459. * @public static
  460. */
  461. public static function getPageSizeFromFormat($format) {
  462. // Paper cordinates are calculated in this way: (inches * 72) where (1 inch = 25.4 mm)
  463. switch (strtoupper($format)) {
  464. // ISO 216 A Series + 2 SIS 014711 extensions
  465. case 'A0' : {$pf = array( 2383.937, 3370.394); break;}
  466. case 'A1' : {$pf = array( 1683.780, 2383.937); break;}
  467. case 'A2' : {$pf = array( 1190.551, 1683.780); break;}
  468. case 'A3' : {$pf = array( 841.890, 1190.551); break;}
  469. case 'A4' : {$pf = array( 595.276, 841.890); break;}
  470. case 'A5' : {$pf = array( 419.528, 595.276); break;}
  471. case 'A6' : {$pf = array( 297.638, 419.528); break;}
  472. case 'A7' : {$pf = array( 209.764, 297.638); break;}
  473. case 'A8' : {$pf = array( 147.402, 209.764); break;}
  474. case 'A9' : {$pf = array( 104.882, 147.402); break;}
  475. case 'A10': {$pf = array( 73.701, 104.882); break;}
  476. case 'A11': {$pf = array( 51.024, 73.701); break;}
  477. case 'A12': {$pf = array( 36.850, 51.024); break;}
  478. // ISO 216 B Series + 2 SIS 014711 extensions
  479. case 'B0' : {$pf = array( 2834.646, 4008.189); break;}
  480. case 'B1' : {$pf = array( 2004.094, 2834.646); break;}
  481. case 'B2' : {$pf = array( 1417.323, 2004.094); break;}
  482. case 'B3' : {$pf = array( 1000.630, 1417.323); break;}
  483. case 'B4' : {$pf = array( 708.661, 1000.630); break;}
  484. case 'B5' : {$pf = array( 498.898, 708.661); break;}
  485. case 'B6' : {$pf = array( 354.331, 498.898); break;}
  486. case 'B7' : {$pf = array( 249.449, 354.331); break;}
  487. case 'B8' : {$pf = array( 175.748, 249.449); break;}
  488. case 'B9' : {$pf = array( 124.724, 175.748); break;}
  489. case 'B10': {$pf = array( 87.874, 124.724); break;}
  490. case 'B11': {$pf = array( 62.362, 87.874); break;}
  491. case 'B12': {$pf = array( 42.520, 62.362); break;}
  492. // ISO 216 C Series + 2 SIS 014711 extensions + 2 EXTENSION
  493. case 'C0' : {$pf = array( 2599.370, 3676.535); break;}
  494. case 'C1' : {$pf = array( 1836.850, 2599.370); break;}
  495. case 'C2' : {$pf = array( 1298.268, 1836.850); break;}
  496. case 'C3' : {$pf = array( 918.425, 1298.268); break;}
  497. case 'C4' : {$pf = array( 649.134, 918.425); break;}
  498. case 'C5' : {$pf = array( 459.213, 649.134); break;}
  499. case 'C6' : {$pf = array( 323.150, 459.213); break;}
  500. case 'C7' : {$pf = array( 229.606, 323.150); break;}
  501. case 'C8' : {$pf = array( 161.575, 229.606); break;}
  502. case 'C9' : {$pf = array( 113.386, 161.575); break;}
  503. case 'C10': {$pf = array( 79.370, 113.386); break;}
  504. case 'C11': {$pf = array( 56.693, 79.370); break;}
  505. case 'C12': {$pf = array( 39.685, 56.693); break;}
  506. case 'C76': {$pf = array( 229.606, 459.213); break;}
  507. case 'DL' : {$pf = array( 311.811, 623.622); break;}
  508. // SIS 014711 E Series
  509. case 'E0' : {$pf = array( 2491.654, 3517.795); break;}
  510. case 'E1' : {$pf = array( 1757.480, 2491.654); break;}
  511. case 'E2' : {$pf = array( 1247.244, 1757.480); break;}
  512. case 'E3' : {$pf = array( 878.740, 1247.244); break;}
  513. case 'E4' : {$pf = array( 623.622, 878.740); break;}
  514. case 'E5' : {$pf = array( 439.370, 623.622); break;}
  515. case 'E6' : {$pf = array( 311.811, 439.370); break;}
  516. case 'E7' : {$pf = array( 221.102, 311.811); break;}
  517. case 'E8' : {$pf = array( 155.906, 221.102); break;}
  518. case 'E9' : {$pf = array( 110.551, 155.906); break;}
  519. case 'E10': {$pf = array( 76.535, 110.551); break;}
  520. case 'E11': {$pf = array( 53.858, 76.535); break;}
  521. case 'E12': {$pf = array( 36.850, 53.858); break;}
  522. // SIS 014711 G Series
  523. case 'G0' : {$pf = array( 2715.591, 3838.110); break;}
  524. case 'G1' : {$pf = array( 1919.055, 2715.591); break;}
  525. case 'G2' : {$pf = array( 1357.795, 1919.055); break;}
  526. case 'G3' : {$pf = array( 958.110, 1357.795); break;}
  527. case 'G4' : {$pf = array( 677.480, 958.110); break;}
  528. case 'G5' : {$pf = array( 479.055, 677.480); break;}
  529. case 'G6' : {$pf = array( 337.323, 479.055); break;}
  530. case 'G7' : {$pf = array( 238.110, 337.323); break;}
  531. case 'G8' : {$pf = array( 167.244, 238.110); break;}
  532. case 'G9' : {$pf = array( 119.055, 167.244); break;}
  533. case 'G10': {$pf = array( 82.205, 119.055); break;}
  534. case 'G11': {$pf = array( 59.528, 82.205); break;}
  535. case 'G12': {$pf = array( 39.685, 59.528); break;}
  536. // ISO Press
  537. case 'RA0': {$pf = array( 2437.795, 3458.268); break;}
  538. case 'RA1': {$pf = array( 1729.134, 2437.795); break;}
  539. case 'RA2': {$pf = array( 1218.898, 1729.134); break;}
  540. case 'RA3': {$pf = array( 864.567, 1218.898); break;}
  541. case 'RA4': {$pf = array( 609.449, 864.567); break;}
  542. case 'SRA0': {$pf = array( 2551.181, 3628.346); break;}
  543. case 'SRA1': {$pf = array( 1814.173, 2551.181); break;}
  544. case 'SRA2': {$pf = array( 1275.591, 1814.173); break;}
  545. case 'SRA3': {$pf = array( 907.087, 1275.591); break;}
  546. case 'SRA4': {$pf = array( 637.795, 907.087); break;}
  547. // German DIN 476
  548. case '4A0': {$pf = array( 4767.874, 6740.787); break;}
  549. case '2A0': {$pf = array( 3370.394, 4767.874); break;}
  550. // Variations on the ISO Standard
  551. case 'A2_EXTRA' : {$pf = array( 1261.417, 1754.646); break;}
  552. case 'A3+' : {$pf = array( 932.598, 1369.134); break;}
  553. case 'A3_EXTRA' : {$pf = array( 912.756, 1261.417); break;}
  554. case 'A3_SUPER' : {$pf = array( 864.567, 1440.000); break;}
  555. case 'SUPER_A3' : {$pf = array( 864.567, 1380.472); break;}
  556. case 'A4_EXTRA' : {$pf = array( 666.142, 912.756); break;}
  557. case 'A4_SUPER' : {$pf = array( 649.134, 912.756); break;}
  558. case 'SUPER_A4' : {$pf = array( 643.465, 1009.134); break;}
  559. case 'A4_LONG' : {$pf = array( 595.276, 986.457); break;}
  560. case 'F4' : {$pf = array( 595.276, 935.433); break;}
  561. case 'SO_B5_EXTRA': {$pf = array( 572.598, 782.362); break;}
  562. case 'A5_EXTRA' : {$pf = array( 490.394, 666.142); break;}
  563. // ANSI Series
  564. case 'ANSI_E': {$pf = array( 2448.000, 3168.000); break;}
  565. case 'ANSI_D': {$pf = array( 1584.000, 2448.000); break;}
  566. case 'ANSI_C': {$pf = array( 1224.000, 1584.000); break;}
  567. case 'ANSI_B': {$pf = array( 792.000, 1224.000); break;}
  568. case 'ANSI_A': {$pf = array( 612.000, 792.000); break;}
  569. // Traditional 'Loose' North American Paper Sizes
  570. case 'USLEDGER':
  571. case 'LEDGER' : {$pf = array( 1224.000, 792.000); break;}
  572. case 'ORGANIZERK':
  573. case 'BIBLE':
  574. case 'USTABLOID':
  575. case 'TABLOID': {$pf = array( 792.000, 1224.000); break;}
  576. case 'ORGANIZERM':
  577. case 'USLETTER':
  578. case 'LETTER' : {$pf = array( 612.000, 792.000); break;}
  579. case 'USLEGAL':
  580. case 'LEGAL' : {$pf = array( 612.000, 1008.000); break;}
  581. case 'GOVERNMENTLETTER':
  582. case 'GLETTER': {$pf = array( 576.000, 756.000); break;}
  583. case 'JUNIORLEGAL':
  584. case 'JLEGAL' : {$pf = array( 576.000, 360.000); break;}
  585. // Other North American Paper Sizes
  586. case 'QUADDEMY': {$pf = array( 2520.000, 3240.000); break;}
  587. case 'SUPER_B': {$pf = array( 936.000, 1368.000); break;}
  588. case 'QUARTO': {$pf = array( 648.000, 792.000); break;}
  589. case 'GOVERNMENTLEGAL':
  590. case 'FOLIO': {$pf = array( 612.000, 936.000); break;}
  591. case 'MONARCH':
  592. case 'EXECUTIVE': {$pf = array( 522.000, 756.000); break;}
  593. case 'ORGANIZERL':
  594. case 'STATEMENT':
  595. case 'MEMO': {$pf = array( 396.000, 612.000); break;}
  596. case 'FOOLSCAP': {$pf = array( 595.440, 936.000); break;}
  597. case 'COMPACT': {$pf = array( 306.000, 486.000); break;}
  598. case 'ORGANIZERJ': {$pf = array( 198.000, 360.000); break;}
  599. // Canadian standard CAN 2-9.60M
  600. case 'P1': {$pf = array( 1587.402, 2437.795); break;}
  601. case 'P2': {$pf = array( 1218.898, 1587.402); break;}
  602. case 'P3': {$pf = array( 793.701, 1218.898); break;}
  603. case 'P4': {$pf = array( 609.449, 793.701); break;}
  604. case 'P5': {$pf = array( 396.850, 609.449); break;}
  605. case 'P6': {$pf = array( 303.307, 396.850); break;}
  606. // North American Architectural Sizes
  607. case 'ARCH_E' : {$pf = array( 2592.000, 3456.000); break;}
  608. case 'ARCH_E1': {$pf = array( 2160.000, 3024.000); break;}
  609. case 'ARCH_D' : {$pf = array( 1728.000, 2592.000); break;}
  610. case 'BROADSHEET':
  611. case 'ARCH_C' : {$pf = array( 1296.000, 1728.000); break;}
  612. case 'ARCH_B' : {$pf = array( 864.000, 1296.000); break;}
  613. case 'ARCH_A' : {$pf = array( 648.000, 864.000); break;}
  614. // --- North American Envelope Sizes ---
  615. // - Announcement Envelopes
  616. case 'ANNENV_A2' : {$pf = array( 314.640, 414.000); break;}
  617. case 'ANNENV_A6' : {$pf = array( 342.000, 468.000); break;}
  618. case 'ANNENV_A7' : {$pf = array( 378.000, 522.000); break;}
  619. case 'ANNENV_A8' : {$pf = array( 396.000, 584.640); break;}
  620. case 'ANNENV_A10' : {$pf = array( 450.000, 692.640); break;}
  621. case 'ANNENV_SLIM': {$pf = array( 278.640, 638.640); break;}
  622. // - Commercial Envelopes
  623. case 'COMMENV_N6_1/4': {$pf = array( 252.000, 432.000); break;}
  624. case 'COMMENV_N6_3/4': {$pf = array( 260.640, 468.000); break;}
  625. case 'COMMENV_N8' : {$pf = array( 278.640, 540.000); break;}
  626. case 'COMMENV_N9' : {$pf = array( 278.640, 638.640); break;}
  627. case 'COMMENV_N10' : {$pf = array( 296.640, 684.000); break;}
  628. case 'COMMENV_N11' : {$pf = array( 324.000, 746.640); break;}
  629. case 'COMMENV_N12' : {$pf = array( 342.000, 792.000); break;}
  630. case 'COMMENV_N14' : {$pf = array( 360.000, 828.000); break;}
  631. // - Catalogue Envelopes
  632. case 'CATENV_N1' : {$pf = array( 432.000, 648.000); break;}
  633. case 'CATENV_N1_3/4' : {$pf = array( 468.000, 684.000); break;}
  634. case 'CATENV_N2' : {$pf = array( 468.000, 720.000); break;}
  635. case 'CATENV_N3' : {$pf = array( 504.000, 720.000); break;}
  636. case 'CATENV_N6' : {$pf = array( 540.000, 756.000); break;}
  637. case 'CATENV_N7' : {$pf = array( 576.000, 792.000); break;}
  638. case 'CATENV_N8' : {$pf = array( 594.000, 810.000); break;}
  639. case 'CATENV_N9_1/2' : {$pf = array( 612.000, 756.000); break;}
  640. case 'CATENV_N9_3/4' : {$pf = array( 630.000, 810.000); break;}
  641. case 'CATENV_N10_1/2': {$pf = array( 648.000, 864.000); break;}
  642. case 'CATENV_N12_1/2': {$pf = array( 684.000, 900.000); break;}
  643. case 'CATENV_N13_1/2': {$pf = array( 720.000, 936.000); break;}
  644. case 'CATENV_N14_1/4': {$pf = array( 810.000, 882.000); break;}
  645. case 'CATENV_N14_1/2': {$pf = array( 828.000, 1044.000); break;}
  646. // Japanese (JIS P 0138-61) Standard B-Series
  647. case 'JIS_B0' : {$pf = array( 2919.685, 4127.244); break;}
  648. case 'JIS_B1' : {$pf = array( 2063.622, 2919.685); break;}
  649. case 'JIS_B2' : {$pf = array( 1459.843, 2063.622); break;}
  650. case 'JIS_B3' : {$pf = array( 1031.811, 1459.843); break;}
  651. case 'JIS_B4' : {$pf = array( 728.504, 1031.811); break;}
  652. case 'JIS_B5' : {$pf = array( 515.906, 728.504); break;}
  653. case 'JIS_B6' : {$pf = array( 362.835, 515.906); break;}
  654. case 'JIS_B7' : {$pf = array( 257.953, 362.835); break;}
  655. case 'JIS_B8' : {$pf = array( 181.417, 257.953); break;}
  656. case 'JIS_B9' : {$pf = array( 127.559, 181.417); break;}
  657. case 'JIS_B10': {$pf = array( 90.709, 127.559); break;}
  658. case 'JIS_B11': {$pf = array( 62.362, 90.709); break;}
  659. case 'JIS_B12': {$pf = array( 45.354, 62.362); break;}
  660. // PA Series
  661. case 'PA0' : {$pf = array( 2381.102, 3174.803,); break;}
  662. case 'PA1' : {$pf = array( 1587.402, 2381.102); break;}
  663. case 'PA2' : {$pf = array( 1190.551, 1587.402); break;}
  664. case 'PA3' : {$pf = array( 793.701, 1190.551); break;}
  665. case 'PA4' : {$pf = array( 595.276, 793.701); break;}
  666. case 'PA5' : {$pf = array( 396.850, 595.276); break;}
  667. case 'PA6' : {$pf = array( 297.638, 396.850); break;}
  668. case 'PA7' : {$pf = array( 198.425, 297.638); break;}
  669. case 'PA8' : {$pf = array( 147.402, 198.425); break;}
  670. case 'PA9' : {$pf = array( 99.213, 147.402); break;}
  671. case 'PA10': {$pf = array( 73.701, 99.213); break;}
  672. // Standard Photographic Print Sizes
  673. case 'PASSPORT_PHOTO': {$pf = array( 99.213, 127.559); break;}
  674. case 'E' : {$pf = array( 233.858, 340.157); break;}
  675. case 'L':
  676. case '3R' : {$pf = array( 252.283, 360.000); break;}
  677. case 'KG':
  678. case '4R' : {$pf = array( 289.134, 430.866); break;}
  679. case '4D' : {$pf = array( 340.157, 430.866); break;}
  680. case '2L':
  681. case '5R' : {$pf = array( 360.000, 504.567); break;}
  682. case '8P':
  683. case '6R' : {$pf = array( 430.866, 575.433); break;}
  684. case '6P':
  685. case '8R' : {$pf = array( 575.433, 720.000); break;}
  686. case '6PW':
  687. case 'S8R' : {$pf = array( 575.433, 864.567); break;}
  688. case '4P':
  689. case '10R' : {$pf = array( 720.000, 864.567); break;}
  690. case '4PW':
  691. case 'S10R': {$pf = array( 720.000, 1080.000); break;}
  692. case '11R' : {$pf = array( 790.866, 1009.134); break;}
  693. case 'S11R': {$pf = array( 790.866, 1224.567); break;}
  694. case '12R' : {$pf = array( 864.567, 1080.000); break;}
  695. case 'S12R': {$pf = array( 864.567, 1292.598); break;}
  696. // Common Newspaper Sizes
  697. case 'NEWSPAPER_BROADSHEET': {$pf = array( 2125.984, 1700.787); break;}
  698. case 'NEWSPAPER_BERLINER' : {$pf = array( 1332.283, 892.913); break;}
  699. case 'NEWSPAPER_TABLOID':
  700. case 'NEWSPAPER_COMPACT' : {$pf = array( 1218.898, 793.701); break;}
  701. // Business Cards
  702. case 'CREDIT_CARD':
  703. case 'BUSINESS_CARD':
  704. case 'BUSINESS_CARD_ISO7810': {$pf = array( 153.014, 242.646); break;}
  705. case 'BUSINESS_CARD_ISO216' : {$pf = array( 147.402, 209.764); break;}
  706. case 'BUSINESS_CARD_IT':
  707. case 'BUSINESS_CARD_UK':
  708. case 'BUSINESS_CARD_FR':
  709. case 'BUSINESS_CARD_DE':
  710. case 'BUSINESS_CARD_ES' : {$pf = array( 155.906, 240.945); break;}
  711. case 'BUSINESS_CARD_CA':
  712. case 'BUSINESS_CARD_US' : {$pf = array( 144.567, 252.283); break;}
  713. case 'BUSINESS_CARD_JP' : {$pf = array( 155.906, 257.953); break;}
  714. case 'BUSINESS_CARD_HK' : {$pf = array( 153.071, 255.118); break;}
  715. case 'BUSINESS_CARD_AU':
  716. case 'BUSINESS_CARD_DK':
  717. case 'BUSINESS_CARD_SE' : {$pf = array( 155.906, 255.118); break;}
  718. case 'BUSINESS_CARD_RU':
  719. case 'BUSINESS_CARD_CZ':
  720. case 'BUSINESS_CARD_FI':
  721. case 'BUSINESS_CARD_HU':
  722. case 'BUSINESS_CARD_IL' : {$pf = array( 141.732, 255.118); break;}
  723. // Billboards
  724. case '4SHEET' : {$pf = array( 2880.000, 4320.000); break;}
  725. case '6SHEET' : {$pf = array( 3401.575, 5102.362); break;}
  726. case '12SHEET': {$pf = array( 8640.000, 4320.000); break;}
  727. case '16SHEET': {$pf = array( 5760.000, 8640.000); break;}
  728. case '32SHEET': {$pf = array(11520.000, 8640.000); break;}
  729. case '48SHEET': {$pf = array(17280.000, 8640.000); break;}
  730. case '64SHEET': {$pf = array(23040.000, 8640.000); break;}
  731. case '96SHEET': {$pf = array(34560.000, 8640.000); break;}
  732. // Old European Sizes
  733. // - Old Imperial English Sizes
  734. case 'EN_EMPEROR' : {$pf = array( 3456.000, 5184.000); break;}
  735. case 'EN_ANTIQUARIAN' : {$pf = array( 2232.000, 3816.000); break;}
  736. case 'EN_GRAND_EAGLE' : {$pf = array( 2070.000, 3024.000); break;}
  737. case 'EN_DOUBLE_ELEPHANT' : {$pf = array( 1926.000, 2880.000); break;}
  738. case 'EN_ATLAS' : {$pf = array( 1872.000, 2448.000); break;}
  739. case 'EN_COLOMBIER' : {$pf = array( 1692.000, 2484.000); break;}
  740. case 'EN_ELEPHANT' : {$pf = array( 1656.000, 2016.000); break;}
  741. case 'EN_DOUBLE_DEMY' : {$pf = array( 1620.000, 2556.000); break;}
  742. case 'EN_IMPERIAL' : {$pf = array( 1584.000, 2160.000); break;}
  743. case 'EN_PRINCESS' : {$pf = array( 1548.000, 2016.000); break;}
  744. case 'EN_CARTRIDGE' : {$pf = array( 1512.000, 1872.000); break;}
  745. case 'EN_DOUBLE_LARGE_POST': {$pf = array( 1512.000, 2376.000); break;}
  746. case 'EN_ROYAL' : {$pf = array( 1440.000, 1800.000); break;}
  747. case 'EN_SHEET':
  748. case 'EN_HALF_POST' : {$pf = array( 1404.000, 1692.000); break;}
  749. case 'EN_SUPER_ROYAL' : {$pf = array( 1368.000, 1944.000); break;}
  750. case 'EN_DOUBLE_POST' : {$pf = array( 1368.000, 2196.000); break;}
  751. case 'EN_MEDIUM' : {$pf = array( 1260.000, 1656.000); break;}
  752. case 'EN_DEMY' : {$pf = array( 1260.000, 1620.000); break;}
  753. case 'EN_LARGE_POST' : {$pf = array( 1188.000, 1512.000); break;}
  754. case 'EN_COPY_DRAUGHT' : {$pf = array( 1152.000, 1440.000); break;}
  755. case 'EN_POST' : {$pf = array( 1116.000, 1386.000); break;}
  756. case 'EN_CROWN' : {$pf = array( 1080.000, 1440.000); break;}
  757. case 'EN_PINCHED_POST' : {$pf = array( 1062.000, 1332.000); break;}
  758. case 'EN_BRIEF' : {$pf = array( 972.000, 1152.000); break;}
  759. case 'EN_FOOLSCAP' : {$pf = array( 972.000, 1224.000); break;}
  760. case 'EN_SMALL_FOOLSCAP' : {$pf = array( 954.000, 1188.000); break;}
  761. case 'EN_POTT' : {$pf = array( 900.000, 1080.000); break;}
  762. // - Old Imperial Belgian Sizes
  763. case 'BE_GRAND_AIGLE' : {$pf = array( 1984.252, 2948.031); break;}
  764. case 'BE_COLOMBIER' : {$pf = array( 1757.480, 2409.449); break;}
  765. case 'BE_DOUBLE_CARRE': {$pf = array( 1757.480, 2607.874); break;}
  766. case 'BE_ELEPHANT' : {$pf = array( 1746.142, 2182.677); break;}
  767. case 'BE_PETIT_AIGLE' : {$pf = array( 1700.787, 2381.102); break;}
  768. case 'BE_GRAND_JESUS' : {$pf = array( 1559.055, 2069.291); break;}
  769. case 'BE_JESUS' : {$pf = array( 1530.709, 2069.291); break;}
  770. case 'BE_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  771. case 'BE_GRAND_MEDIAN': {$pf = array( 1303.937, 1714.961); break;}
  772. case 'BE_DOUBLE_POSTE': {$pf = array( 1233.071, 1601.575); break;}
  773. case 'BE_COQUILLE' : {$pf = array( 1218.898, 1587.402); break;}
  774. case 'BE_PETIT_MEDIAN': {$pf = array( 1176.378, 1502.362); break;}
  775. case 'BE_RUCHE' : {$pf = array( 1020.472, 1303.937); break;}
  776. case 'BE_PROPATRIA' : {$pf = array( 977.953, 1218.898); break;}
  777. case 'BE_LYS' : {$pf = array( 898.583, 1125.354); break;}
  778. case 'BE_POT' : {$pf = array( 870.236, 1088.504); break;}
  779. case 'BE_ROSETTE' : {$pf = array( 765.354, 983.622); break;}
  780. // - Old Imperial French Sizes
  781. case 'FR_UNIVERS' : {$pf = array( 2834.646, 3685.039); break;}
  782. case 'FR_DOUBLE_COLOMBIER' : {$pf = array( 2551.181, 3571.654); break;}
  783. case 'FR_GRANDE_MONDE' : {$pf = array( 2551.181, 3571.654); break;}
  784. case 'FR_DOUBLE_SOLEIL' : {$pf = array( 2267.717, 3401.575); break;}
  785. case 'FR_DOUBLE_JESUS' : {$pf = array( 2154.331, 3174.803); break;}
  786. case 'FR_GRAND_AIGLE' : {$pf = array( 2125.984, 3004.724); break;}
  787. case 'FR_PETIT_AIGLE' : {$pf = array( 1984.252, 2664.567); break;}
  788. case 'FR_DOUBLE_RAISIN' : {$pf = array( 1842.520, 2834.646); break;}
  789. case 'FR_JOURNAL' : {$pf = array( 1842.520, 2664.567); break;}
  790. case 'FR_COLOMBIER_AFFICHE': {$pf = array( 1785.827, 2551.181); break;}
  791. case 'FR_DOUBLE_CAVALIER' : {$pf = array( 1757.480, 2607.874); break;}
  792. case 'FR_CLOCHE' : {$pf = array( 1700.787, 2267.717); break;}
  793. case 'FR_SOLEIL' : {$pf = array( 1700.787, 2267.717); break;}
  794. case 'FR_DOUBLE_CARRE' : {$pf = array( 1587.402, 2551.181); break;}
  795. case 'FR_DOUBLE_COQUILLE' : {$pf = array( 1587.402, 2494.488); break;}
  796. case 'FR_JESUS' : {$pf = array( 1587.402, 2154.331); break;}
  797. case 'FR_RAISIN' : {$pf = array( 1417.323, 1842.520); break;}
  798. case 'FR_CAVALIER' : {$pf = array( 1303.937, 1757.480); break;}
  799. case 'FR_DOUBLE_COURONNE' : {$pf = array( 1303.937, 2040.945); break;}
  800. case 'FR_CARRE' : {$pf = array( 1275.591, 1587.402); break;}
  801. case 'FR_COQUILLE' : {$pf = array( 1247.244, 1587.402); break;}
  802. case 'FR_DOUBLE_TELLIERE' : {$pf = array( 1247.244, 1927.559); break;}
  803. case 'FR_DOUBLE_CLOCHE' : {$pf = array( 1133.858, 1700.787); break;}
  804. case 'FR_DOUBLE_POT' : {$pf = array( 1133.858, 1757.480); break;}
  805. case 'FR_ECU' : {$pf = array( 1133.858, 1474.016); break;}
  806. case 'FR_COURONNE' : {$pf = array( 1020.472, 1303.937); break;}
  807. case 'FR_TELLIERE' : {$pf = array( 963.780, 1247.244); break;}
  808. case 'FR_POT' : {$pf = array( 878.740, 1133.858); break;}
  809. // DEFAULT ISO A4
  810. default: {$pf = array( 595.276, 841.890); break;}
  811. }
  812. return $pf;
  813. }
  814. /**
  815. * Set page boundaries.
  816. * @param $page (int) page number
  817. * @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>
  818. * @param $llx (float) lower-left x coordinate in user units.
  819. * @param $lly (float) lower-left y coordinate in user units.
  820. * @param $urx (float) upper-right x coordinate in user units.
  821. * @param $ury (float) upper-right y coordinate in user units.
  822. * @param $points (boolean) If true uses user units as unit of measure, otherwise uses PDF points.
  823. * @param $k (float) Scale factor (number of points in user unit).
  824. * @param $pagedim (array) Array of page dimensions.
  825. * @return pagedim array of page dimensions.
  826. * @since 5.0.010 (2010-05-17)
  827. * @public static
  828. */
  829. public static function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false, $k, $pagedim=array()) {
  830. if (!isset($pagedim[$page])) {
  831. // initialize array
  832. $pagedim[$page] = array();
  833. }
  834. if (!in_array($type, self::$pageboxes)) {
  835. return;
  836. }
  837. if ($points) {
  838. $k = 1;
  839. }
  840. $pagedim[$page][$type]['llx'] = ($llx * $k);
  841. $pagedim[$page][$type]['lly'] = ($lly * $k);
  842. $pagedim[$page][$type]['urx'] = ($urx * $k);
  843. $pagedim[$page][$type]['ury'] = ($ury * $k);
  844. return $pagedim;
  845. }
  846. /**
  847. * Swap X and Y coordinates of page boxes (change page boxes orientation).
  848. * @param $page (int) page number
  849. * @param $pagedim (array) Array of page dimensions.
  850. * @return pagedim array of page dimensions.
  851. * @since 5.0.010 (2010-05-17)
  852. * @public static
  853. */
  854. public static function swapPageBoxCoordinates($page, $pagedim) {
  855. foreach (self::$pageboxes as $type) {
  856. // swap X and Y coordinates
  857. if (isset($pagedim[$page][$type])) {
  858. $tmp = $pagedim[$page][$type]['llx'];
  859. $pagedim[$page][$type]['llx'] = $pagedim[$page][$type]['lly'];
  860. $pagedim[$page][$type]['lly'] = $tmp;
  861. $tmp = $pagedim[$page][$type]['urx'];
  862. $pagedim[$page][$type]['urx'] = $pagedim[$page][$type]['ury'];
  863. $pagedim[$page][$type]['ury'] = $tmp;
  864. }
  865. }
  866. return $pagedim;
  867. }
  868. /**
  869. * Get the canonical page layout mode.
  870. * @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>
  871. * @return (string) Canonical page layout name.
  872. * @public static
  873. */
  874. public static function getPageLayoutMode($layout='SinglePage') {
  875. switch ($layout) {
  876. case 'default':
  877. case 'single':
  878. case 'SinglePage': {
  879. $layout_mode = 'SinglePage';
  880. break;
  881. }
  882. case 'continuous':
  883. case 'OneColumn': {
  884. $layout_mode = 'OneColumn';
  885. break;
  886. }
  887. case 'two':
  888. case 'TwoColumnLeft': {
  889. $layout_mode = 'TwoColumnLeft';
  890. break;
  891. }
  892. case 'TwoColumnRight': {
  893. $layout_mode = 'TwoColumnRight';
  894. break;
  895. }
  896. case 'TwoPageLeft': {
  897. $layout_mode = 'TwoPageLeft';
  898. break;
  899. }
  900. case 'TwoPageRight': {
  901. $layout_mode = 'TwoPageRight';
  902. break;
  903. }
  904. default: {
  905. $layout_mode = 'SinglePage';
  906. }
  907. }
  908. return $layout_mode;
  909. }
  910. /**
  911. * Get the canonical page layout mode.
  912. * @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>
  913. * @return (string) Canonical page mode name.
  914. * @public static
  915. */
  916. public static function getPageMode($mode='UseNone') {
  917. switch ($mode) {
  918. case 'UseNone': {
  919. $page_mode = 'UseNone';
  920. break;
  921. }
  922. case 'UseOutlines': {
  923. $page_mode = 'UseOutlines';
  924. break;
  925. }
  926. case 'UseThumbs': {
  927. $page_mode = 'UseThumbs';
  928. break;
  929. }
  930. case 'FullScreen': {
  931. $page_mode = 'FullScreen';
  932. break;
  933. }
  934. case 'UseOC': {
  935. $page_mode = 'UseOC';
  936. break;
  937. }
  938. case '': {
  939. $page_mode = 'UseAttachments';
  940. break;
  941. }
  942. default: {
  943. $page_mode = 'UseNone';
  944. }
  945. }
  946. return $page_mode;
  947. }
  948. /**
  949. * Check if the URL exist.
  950. * @param $url (string) URL to check.
  951. * @return Boolean true if the URl exist, false otherwise.
  952. * @since 5.9.204 (2013-01-28)
  953. * @public static
  954. */
  955. public static function isValidURL($url) {
  956. $headers = @get_headers($url);
  957. return (strpos($headers[0], '200') !== false);
  958. }
  959. /**
  960. * Removes SHY characters from text.
  961. * Unicode Data:<ul>
  962. * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
  963. * <li>HTML Entity (decimal): "&amp;#173;"</li>
  964. * <li>HTML Entity (hex): "&amp;#xad;"</li>
  965. * <li>HTML Entity (named): "&amp;shy;"</li>
  966. * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
  967. * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
  968. * <li>UTF-8 character: chr(194).chr(173)</li>
  969. * </ul>
  970. * @param $txt (string) input string
  971. * @param $unicode (boolean) True if we are in unicode mode, false otherwise.
  972. * @return string without SHY characters.
  973. * @since (4.5.019) 2009-02-28
  974. * @public static
  975. */
  976. public static function removeSHY($txt='', $unicode=true) {
  977. $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
  978. if (!$unicode) {
  979. $txt = preg_replace('/([\\xad]{1})/', '', $txt);
  980. }
  981. return $txt;
  982. }
  983. /**
  984. * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
  985. * @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)))
  986. * @param $position (string) multicell position: 'start', 'middle', 'end'
  987. * @param $opencell (boolean) True when the cell is left open at the page bottom, false otherwise.
  988. * @return border mode array
  989. * @since 4.4.002 (2008-12-09)
  990. * @public static
  991. */
  992. public static function getBorderMode($brd, $position='start', $opencell=true) {
  993. if ((!$opencell) OR empty($brd)) {
  994. return $brd;
  995. }
  996. if ($brd == 1) {
  997. $brd = 'LTRB';
  998. }
  999. if (is_string($brd)) {
  1000. // convert string to array
  1001. $slen = strlen($brd);
  1002. $newbrd = array();
  1003. for ($i = 0; $i < $slen; ++$i) {
  1004. $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
  1005. }
  1006. $brd = $newbrd;
  1007. }
  1008. foreach ($brd as $border => $style) {
  1009. switch ($position) {
  1010. case 'start': {
  1011. if (strpos($border, 'B') !== false) {
  1012. // remove bottom line
  1013. $newkey = str_replace('B', '', $border);
  1014. if (strlen($newkey) > 0) {
  1015. $brd[$newkey] = $style;
  1016. }
  1017. unset($brd[$border]);
  1018. }
  1019. break;
  1020. }
  1021. case 'middle': {
  1022. if (strpos($border, 'B') !== false) {
  1023. // remove bottom line
  1024. $newkey = str_replace('B', '', $border);
  1025. if (strlen($newkey) > 0) {
  1026. $brd[$newkey] = $style;
  1027. }
  1028. unset($brd[$border]);
  1029. $border = $newkey;
  1030. }
  1031. if (strpos($border, 'T') !== false) {
  1032. // remove bottom line
  1033. $newkey = str_replace('T', '', $border);
  1034. if (strlen($newkey) > 0) {
  1035. $brd[$newkey] = $style;
  1036. }
  1037. unset($brd[$border]);
  1038. }
  1039. break;
  1040. }
  1041. case 'end': {
  1042. if (strpos($border, 'T') !== false) {
  1043. // remove bottom line
  1044. $newkey = str_replace('T', '', $border);
  1045. if (strlen($newkey) > 0) {
  1046. $brd[$newkey] = $style;
  1047. }
  1048. unset($brd[$border]);
  1049. }
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. return $brd;
  1055. }
  1056. /**
  1057. * Determine whether a string is empty.
  1058. * @param $str (string) string to be checked
  1059. * @return boolean true if string is empty
  1060. * @since 4.5.044 (2009-04-16)
  1061. * @public static
  1062. */
  1063. public static function empty_string($str) {
  1064. return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
  1065. }
  1066. /**
  1067. * Returns a temporary filename for caching object on filesystem.
  1068. * @param $name (string) Prefix to add to the file name.
  1069. * @return string filename.
  1070. * @since 4.5.000 (2008-12-31)
  1071. * @public static
  1072. */
  1073. public static function getObjFilename($name) {
  1074. return tempnam(K_PATH_CACHE, $name.'_');
  1075. }
  1076. /**
  1077. * Add "\" before "\", "(" and ")"
  1078. * @param $s (string) string to escape.
  1079. * @return string escaped string.
  1080. * @public static
  1081. */
  1082. public static function _escape($s) {
  1083. // the chr(13) substitution fixes the Bugs item #1421290.
  1084. return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
  1085. }
  1086. /**
  1087. * Escape some special characters (&lt; &gt; &amp;) for XML output.
  1088. * @param $str (string) Input string to convert.
  1089. * @return converted string
  1090. * @since 5.9.121 (2011-09-28)
  1091. * @public static
  1092. */
  1093. public static function _escapeXML($str) {
  1094. $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
  1095. $str = strtr($str, $replaceTable);
  1096. return $str;
  1097. }
  1098. /**
  1099. * Creates a copy of a class object
  1100. * @param $object (object) class object to be cloned
  1101. * @return cloned object
  1102. * @since 4.5.029 (2009-03-19)
  1103. * @public static
  1104. */
  1105. public static function objclone($object) {
  1106. if (($object instanceof Imagick) AND (version_compare(phpversion('imagick'), '3.0.1') !== 1)) {
  1107. // on the versions after 3.0.1 the clone() method was deprecated in favour of clone keyword
  1108. return @$object->clone();
  1109. }
  1110. return @clone($object);
  1111. }
  1112. /**
  1113. * Ouput input data and compress it if possible.
  1114. * @param $data (string) Data to output.
  1115. * @param $length (int) Data length in bytes.
  1116. * @since 5.9.086
  1117. * @public static
  1118. */
  1119. public static function sendOutputData($data, $length) {
  1120. if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  1121. // the content length may vary if the server is using compression
  1122. header('Content-Length: '.$length);
  1123. }
  1124. echo $data;
  1125. }
  1126. /**
  1127. * Replace page number aliases with number.
  1128. * @param $page (string) Page content.
  1129. * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
  1130. * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
  1131. * @return replaced page content and updated $diff parameter as array.
  1132. * @public static
  1133. */
  1134. public static function replacePageNumAliases($page, $replace, $diff=0) {
  1135. foreach ($replace as $rep) {
  1136. foreach ($rep[3] as $a) {
  1137. if (strpos($page, $a) !== false) {
  1138. $page = str_replace($a, $rep[0], $page);
  1139. $diff += ($rep[2] - $rep[1]);
  1140. }
  1141. }
  1142. }
  1143. return array($page, $diff);
  1144. }
  1145. /**
  1146. * Returns timestamp in seconds from formatted date-time.
  1147. * @param $date (string) Formatted date-time.
  1148. * @return int seconds.
  1149. * @since 5.9.152 (2012-03-23)
  1150. * @public static
  1151. */
  1152. public static function getTimestamp($date) {
  1153. if (($date[0] == 'D') AND ($date[1] == ':')) {
  1154. // remove date prefix if present
  1155. $date = substr($date, 2);
  1156. }
  1157. return strtotime($date);
  1158. }
  1159. /**
  1160. * Returns a formatted date-time.
  1161. * @param $time (int) Time in seconds.
  1162. * @return string escaped date string.
  1163. * @since 5.9.152 (2012-03-23)
  1164. * @public static
  1165. */
  1166. public static function getFormattedDate($time) {
  1167. return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
  1168. }
  1169. /**
  1170. * Get ULONG from string (Big Endian 32-bit unsigned integer).
  1171. * @param $str (string) string from where to extract value
  1172. * @param $offset (int) point from where to read the data
  1173. * @return int 32 bit value
  1174. * @author Nicola Asuni
  1175. * @since 5.2.000 (2010-06-02)
  1176. * @public static
  1177. */
  1178. public static function _getULONG($str, $offset) {
  1179. $v = unpack('Ni', substr($str, $offset, 4));
  1180. return $v['i'];
  1181. }
  1182. /**
  1183. * Get USHORT from string (Big Endian 16-bit unsigned integer).
  1184. * @param $str (string) string from where to extract value
  1185. * @param $offset (int) point from where to read the data
  1186. * @return int 16 bit value
  1187. * @author Nicola Asuni
  1188. * @since 5.2.000 (2010-06-02)
  1189. * @public static
  1190. */
  1191. public static function _getUSHORT($str, $offset) {
  1192. $v = unpack('ni', substr($str, $offset, 2));
  1193. return $v['i'];
  1194. }
  1195. /**
  1196. * Get SHORT from string (Big Endian 16-bit signed integer).
  1197. * @param $str (string) String from where to extract value.
  1198. * @param $offset (int) Point from where to read the data.
  1199. * @return int 16 bit value
  1200. * @author Nicola Asuni
  1201. * @since 5.2.000 (2010-06-02)
  1202. * @public static
  1203. */
  1204. public static function _getSHORT($str, $offset) {
  1205. $v = unpack('si', substr($str, $offset, 2));
  1206. return $v['i'];
  1207. }
  1208. /**
  1209. * Get FWORD from string (Big Endian 16-bit signed integer).
  1210. * @param $str (string) String from where to extract value.
  1211. * @param $offset (int) Point from where to read the data.
  1212. * @return int 16 bit value
  1213. * @author Nicola Asuni
  1214. * @since 5.9.123 (2011-09-30)
  1215. * @public static
  1216. */
  1217. public static function _getFWORD($str, $offset) {
  1218. $v = self::_getUSHORT($str, $offset);
  1219. if ($v > 0x7fff) {
  1220. $v -= 0x10000;
  1221. }
  1222. return $v;
  1223. }
  1224. /**
  1225. * Get UFWORD from string (Big Endian 16-bit unsigned integer).
  1226. * @param $str (string) string from where to extract value
  1227. * @param $offset (int) point from where to read the data
  1228. * @return int 16 bit value
  1229. * @author Nicola Asuni
  1230. * @since 5.9.123 (2011-09-30)
  1231. * @public static
  1232. */
  1233. public static function _getUFWORD($str, $offset) {
  1234. $v = self::_getUSHORT($str, $offset);
  1235. return $v;
  1236. }
  1237. /**
  1238. * Get FIXED from string (32-bit signed fixed-point number (16.16).
  1239. * @param $str (string) string from where to extract value
  1240. * @param $offset (int) point from where to read the data
  1241. * @return int 16 bit value
  1242. * @author Nicola Asuni
  1243. * @since 5.9.123 (2011-09-30)
  1244. * @public static
  1245. */
  1246. public static function _getFIXED($str, $offset) {
  1247. // mantissa
  1248. $m = self::_getFWORD($str, $offset);
  1249. // fraction
  1250. $f = self::_getUSHORT($str, ($offset + 2));
  1251. $v = floatval(''.$m.'.'.$f.'');
  1252. return $v;
  1253. }
  1254. /**
  1255. * Get BYTE from string (8-bit unsigned integer).
  1256. * @param $str (string) String from where to extract value.
  1257. * @param $offset (int) Point from where to read the data.
  1258. * @return int 8 bit value
  1259. * @author Nicola Asuni
  1260. * @since 5.2.000 (2010-06-02)
  1261. * @public static
  1262. */
  1263. public static function _getBYTE($str, $offset) {
  1264. $v = unpack('Ci', substr($str, $offset, 1));
  1265. return $v['i'];
  1266. }
  1267. /**
  1268. * Binary-safe and URL-safe file read.
  1269. * 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.
  1270. * @param $handle (resource)
  1271. * @param $length (int)
  1272. * @return Returns the read string or FALSE in case of error.
  1273. * @author Nicola Asuni
  1274. * @since 4.5.027 (2009-03-16)
  1275. * @public static
  1276. */
  1277. public static function rfread($handle, $length) {
  1278. $data = fread($handle, $length);
  1279. if ($data === false) {
  1280. return false;
  1281. }
  1282. $rest = ($length - strlen($data));
  1283. if ($rest > 0) {
  1284. $data .= self::rfread($handle, $rest);
  1285. }
  1286. return $data;
  1287. }
  1288. /**
  1289. * Read a 4-byte (32 bit) integer from file.
  1290. * @param $f (string) file name.
  1291. * @return 4-byte integer
  1292. * @public static
  1293. */
  1294. public static function _freadint($f) {
  1295. $a = unpack('Ni', fread($f, 4));
  1296. return $a['i'];
  1297. }
  1298. /**
  1299. * Returns a string containing random data to be used as a seed for encryption methods.
  1300. * @param $seed (string) starting seed value
  1301. * @return string containing random data
  1302. * @author Nicola Asuni
  1303. * @since 5.9.006 (2010-10-19)
  1304. * @public static
  1305. */
  1306. public static function getRandomSeed($seed='') {
  1307. $seed .= microtime();
  1308. if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
  1309. // this is not used on windows systems because it is very slow for a know bug
  1310. $seed .= openssl_random_pseudo_bytes(512);
  1311. } else {
  1312. for ($i = 0; $i < 23; ++$i) {
  1313. $seed .= uniqid('', true);
  1314. }
  1315. }
  1316. $seed .= uniqid('', true);
  1317. $seed .= rand();
  1318. $seed .= getmypid();
  1319. $seed .= __FILE__;
  1320. if (isset($_SERVER['REMOTE_ADDR'])) {
  1321. $seed .= $_SERVER['REMOTE_ADDR'];
  1322. }
  1323. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  1324. $seed .= $_SERVER['HTTP_USER_AGENT'];
  1325. }
  1326. if (isset($_SERVER['HTTP_ACCEPT'])) {
  1327. $seed .= $_SERVER['HTTP_ACCEPT'];
  1328. }
  1329. if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  1330. $seed .= $_SERVER['HTTP_ACCEPT_ENCODING'];
  1331. }
  1332. if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  1333. $seed .= $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  1334. }
  1335. if (isset($_SERVER['HTTP_ACCEPT_CHARSET'])) {
  1336. $seed .= $_SERVER['HTTP_ACCEPT_CHARSET'];
  1337. }
  1338. $seed .= rand();
  1339. $seed .= uniqid('', true);
  1340. $seed .= microtime();
  1341. return $seed;
  1342. }
  1343. /**
  1344. * Encrypts a string using MD5 and returns it's value as a binary string.
  1345. * @param $str (string) input string
  1346. * @return String MD5 encrypted binary string
  1347. * @since 2.0.000 (2008-01-02)
  1348. * @public static
  1349. */
  1350. public static function _md5_16($str) {
  1351. return pack('H*', md5($str));
  1352. }
  1353. /**
  1354. * Returns the input text exrypted using AES algorithm and the specified key.
  1355. * This method requires mcrypt.
  1356. * @param $key (string) encryption key
  1357. * @param $text (String) input text to be encrypted
  1358. * @return String encrypted text
  1359. * @author Nicola Asuni
  1360. * @since 5.0.005 (2010-05-11)
  1361. * @public static
  1362. */
  1363. public static function _AES($key, $text) {
  1364. // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
  1365. $padding = 16 - (strlen($text) % 16);
  1366. $text .= str_repeat(chr($padding), $padding);
  1367. $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  1368. $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  1369. $text = $iv.$text;
  1370. return $text;
  1371. }
  1372. /**
  1373. * Returns the input text encrypted using RC4 algorithm and the specified key.
  1374. * RC4 is the standard encryption algorithm used in PDF format
  1375. * @param $key (string) Encryption key.
  1376. * @param $text (String) Input text to be encrypted.
  1377. * @param $last_enc_key (String) Reference to last RC4 key encrypted.
  1378. * @param $last_enc_key_c (String) Reference to last RC4 computed key.
  1379. * @return String encrypted text
  1380. * @since 2.0.000 (2008-01-02)
  1381. * @author Klemen Vodopivec, Nicola Asuni
  1382. * @public static
  1383. */
  1384. public static function _RC4($key, $text, &$last_enc_key, &$last_enc_key_c) {
  1385. if (function_exists('mcrypt_encrypt') AND ($out = @mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
  1386. // try to use mcrypt function if exist
  1387. return $out;
  1388. }
  1389. if ($last_enc_key != $key) {
  1390. $k = str_repeat($key, ((256 / strlen($key)) + 1));
  1391. $rc4 = range(0, 255);
  1392. $j = 0;
  1393. for ($i = 0; $i < 256; ++$i) {
  1394. $t = $rc4[$i];
  1395. $j = ($j + $t + ord($k[$i])) % 256;
  1396. $rc4[$i] = $rc4[$j];
  1397. $rc4[$j] = $t;
  1398. }
  1399. $last_enc_key = $key;
  1400. $last_enc_key_c = $rc4;
  1401. } else {
  1402. $rc4 = $last_enc_key_c;
  1403. }
  1404. $len = strlen($text);
  1405. $a = 0;
  1406. $b = 0;
  1407. $out = '';
  1408. for ($i = 0; $i < $len; ++$i) {
  1409. $a = ($a + 1) % 256;
  1410. $t = $rc4[$a];
  1411. $b = ($b + $t) % 256;
  1412. $rc4[$a] = $rc4[$b];
  1413. $rc4[$b] = $t;
  1414. $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
  1415. $out .= chr(ord($text[$i]) ^ $k);
  1416. }
  1417. return $out;
  1418. }
  1419. /**
  1420. * Return the premission code used on encryption (P value).
  1421. * @param $permissions (Array) the set of permissions (specify the ones you want to block).
  1422. * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
  1423. * @since 5.0.005 (2010-05-12)
  1424. * @author Nicola Asuni
  1425. * @public static
  1426. */
  1427. public static function getUserPermissionCode($permissions, $mode=0) {
  1428. $options = array(
  1429. 'owner' => 2, // bit 2 -- inverted logic: cleared by default
  1430. 'print' => 4, // bit 3
  1431. 'modify' => 8, // bit 4
  1432. 'copy' => 16, // bit 5
  1433. 'annot-forms' => 32, // bit 6
  1434. 'fill-forms' => 256, // bit 9
  1435. 'extract' => 512, // bit 10
  1436. 'assemble' => 1024,// bit 11
  1437. 'print-high' => 2048 // bit 12
  1438. );
  1439. $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
  1440. foreach ($permissions as $permission) {
  1441. if (isset($options[$permission])) {
  1442. if (($mode > 0) OR ($options[$permission] <= 32)) {
  1443. // set only valid permissions
  1444. if ($options[$permission] == 2) {
  1445. // the logic for bit 2 is inverted (cleared by default)
  1446. $protection += $options[$permission];
  1447. } else {
  1448. $protection -= $options[$permission];
  1449. }
  1450. }
  1451. }
  1452. }
  1453. return $protection;
  1454. }
  1455. /**
  1456. * Convert hexadecimal string to string
  1457. * @param $bs (string) byte-string to convert
  1458. * @return String
  1459. * @since 5.0.005 (2010-05-12)
  1460. * @author Nicola Asuni
  1461. * @public static
  1462. */
  1463. public static function convertHexStringToString($bs) {
  1464. $string = ''; // string to be returned
  1465. $bslength = strlen($bs);
  1466. if (($bslength % 2) != 0) {
  1467. // padding
  1468. $bs .= '0';
  1469. ++$bslength;
  1470. }
  1471. for ($i = 0; $i < $bslength; $i += 2) {
  1472. $string .= chr(hexdec($bs[$i].$bs[($i + 1)]));
  1473. }
  1474. return $string;
  1475. }
  1476. /**
  1477. * Convert string to hexadecimal string (byte string)
  1478. * @param $s (string) string to convert
  1479. * @return byte string
  1480. * @since 5.0.010 (2010-05-17)
  1481. * @author Nicola Asuni
  1482. * @public static
  1483. */
  1484. public static function convertStringToHexString($s) {
  1485. $bs = '';
  1486. $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  1487. foreach ($chars as $c) {
  1488. $bs .= sprintf('%02s', dechex(ord($c)));
  1489. }
  1490. return $bs;
  1491. }
  1492. /**
  1493. * Convert encryption P value to a string of bytes, low-order byte first.
  1494. * @param $protection (string) 32bit encryption permission value (P value)
  1495. * @return String
  1496. * @since 5.0.005 (2010-05-12)
  1497. * @author Nicola Asuni
  1498. * @public static
  1499. */
  1500. public static function getEncPermissionsString($protection) {
  1501. $binprot = sprintf('%032b', $protection);
  1502. $str = chr(bindec(substr($binprot, 24, 8)));
  1503. $str .= chr(bindec(substr($binprot, 16, 8)));
  1504. $str .= chr(bindec(substr($binprot, 8, 8)));
  1505. $str .= chr(bindec(substr($binprot, 0, 8)));
  1506. return $str;
  1507. }
  1508. /**
  1509. * Encode a name object.
  1510. * @param $name (string) Name object to encode.
  1511. * @return (string) Encoded name object.
  1512. * @author Nicola Asuni
  1513. * @since 5.9.097 (2011-06-23)
  1514. * @public static
  1515. */
  1516. public static function encodeNameObject($name) {
  1517. $escname = '';
  1518. $length = strlen($name);
  1519. for ($i = 0; $i < $length; ++$i) {
  1520. $chr = $name[$i];
  1521. if (preg_match('/[0-9a-zA-Z]/', $chr) == 1) {
  1522. $escname .= $chr;
  1523. } else {
  1524. $escname .= sprintf('#%02X', ord($chr));
  1525. }
  1526. }
  1527. return $escname;
  1528. }
  1529. /**
  1530. * Convert JavaScript form fields properties array to Annotation Properties array.
  1531. * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
  1532. * @param $spot_colors (array) Reference to spot colors array.
  1533. * @param $rtl (boolean) True if in Right-To-Left text direction mode, false otherwise.
  1534. * @return array of annotation properties
  1535. * @author Nicola Asuni
  1536. * @since 4.8.000 (2009-09-06)
  1537. * @public static
  1538. */
  1539. public static function getAnnotOptFromJSProp($prop, &$spot_colors, $rtl=false) {
  1540. if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
  1541. // the annotation options area lready defined
  1542. return $prop['aopt'];
  1543. }
  1544. $opt = array(); // value to be returned
  1545. // alignment: Controls how the text is laid out within the text field.
  1546. if (isset($prop['alignment'])) {
  1547. switch ($prop['alignment']) {
  1548. case 'left': {
  1549. $opt['q'] = 0;
  1550. break;
  1551. }
  1552. case 'center': {
  1553. $opt['q'] = 1;
  1554. break;
  1555. }
  1556. case 'right': {
  1557. $opt['q'] = 2;
  1558. break;
  1559. }
  1560. default: {
  1561. $opt['q'] = ($rtl)?2:0;
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
  1567. if (isset($prop['lineWidth'])) {
  1568. $linewidth = intval($prop['lineWidth']);
  1569. } else {
  1570. $linewidth = 1;
  1571. }
  1572. // borderStyle: The border style for a field.
  1573. if (isset($prop['borderStyle'])) {
  1574. switch ($prop['borderStyle']) {
  1575. case 'border.d':
  1576. case 'dashed': {
  1577. $opt['border'] = array(0, 0, $linewidth, array(3, 2));
  1578. $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
  1579. break;
  1580. }
  1581. case 'border.b':
  1582. case 'beveled': {
  1583. $opt['border'] = array(0, 0, $linewidth);
  1584. $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
  1585. break;
  1586. }
  1587. case 'border.i':
  1588. case 'inset': {
  1589. $opt['border'] = array(0, 0, $linewidth);
  1590. $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
  1591. break;
  1592. }
  1593. case 'border.u':
  1594. case 'underline': {
  1595. $opt['border'] = array(0, 0, $linewidth);
  1596. $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
  1597. break;
  1598. }
  1599. case 'border.s':
  1600. case 'solid': {
  1601. $opt['border'] = array(0, 0, $linewidth);
  1602. $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
  1603. break;
  1604. }
  1605. default: {
  1606. break;
  1607. }
  1608. }
  1609. }
  1610. if (isset($prop['border']) AND is_array($prop['border'])) {
  1611. $opt['border'] = $prop['border'];
  1612. }
  1613. if (!isset($opt['mk'])) {
  1614. $opt['mk'] = array();
  1615. }
  1616. if (!isset($opt['mk']['if'])) {
  1617. $opt['mk']['if'] = array();
  1618. }
  1619. $opt['mk']['if']['a'] = array(0.5, 0.5);
  1620. // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
  1621. if (isset($prop['buttonAlignX'])) {
  1622. $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
  1623. }
  1624. // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
  1625. if (isset($prop['buttonAlignY'])) {
  1626. $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
  1627. }
  1628. // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
  1629. if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
  1630. $opt['mk']['if']['fb'] = true;
  1631. }
  1632. // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
  1633. if (isset($prop['buttonScaleHow'])) {
  1634. switch ($prop['buttonScaleHow']) {
  1635. case 'scaleHow.proportional': {
  1636. $opt['mk']['if']['s'] = 'P';
  1637. break;
  1638. }
  1639. case 'scaleHow.anamorphic': {
  1640. $opt['mk']['if']['s'] = 'A';
  1641. break;
  1642. }
  1643. }
  1644. }
  1645. // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
  1646. if (isset($prop['buttonScaleWhen'])) {
  1647. switch ($prop['buttonScaleWhen']) {
  1648. case 'scaleWhen.always': {
  1649. $opt['mk']['if']['sw'] = 'A';
  1650. break;
  1651. }
  1652. case 'scaleWhen.never': {
  1653. $opt['mk']['if']['sw'] = 'N';
  1654. break;
  1655. }
  1656. case 'scaleWhen.tooBig': {
  1657. $opt['mk']['if']['sw'] = 'B';
  1658. break;
  1659. }
  1660. case 'scaleWhen.tooSmall': {
  1661. $opt['mk']['if']['sw'] = 'S';
  1662. break;
  1663. }
  1664. }
  1665. }
  1666. // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
  1667. if (isset($prop['buttonPosition'])) {
  1668. switch ($prop['buttonPosition']) {
  1669. case 0:
  1670. case 'position.textOnly': {
  1671. $opt['mk']['tp'] = 0;
  1672. break;
  1673. }
  1674. case 1:
  1675. case 'position.iconOnly': {
  1676. $opt['mk']['tp'] = 1;
  1677. break;
  1678. }
  1679. case 2:
  1680. case 'position.iconTextV': {
  1681. $opt['mk']['tp'] = 2;
  1682. break;
  1683. }
  1684. case 3:
  1685. case 'position.textIconV': {
  1686. $opt['mk']['tp'] = 3;
  1687. break;
  1688. }
  1689. case 4:
  1690. case 'position.iconTextH': {
  1691. $opt['mk']['tp'] = 4;
  1692. break;
  1693. }
  1694. case 5:
  1695. case 'position.textIconH': {
  1696. $opt['mk']['tp'] = 5;
  1697. break;
  1698. }
  1699. case 6:
  1700. case 'position.overlay': {
  1701. $opt['mk']['tp'] = 6;
  1702. break;
  1703. }
  1704. }
  1705. }
  1706. // fillColor: Specifies the background color for a field.
  1707. if (isset($prop['fillColor'])) {
  1708. if (is_array($prop['fillColor'])) {
  1709. $opt['mk']['bg'] = $prop['fillColor'];
  1710. } else {
  1711. $opt['mk']['bg'] = TCPDF_COLORS::convertHTMLColorToDec($prop['fillColor'], $spot_colors);
  1712. }
  1713. }
  1714. // strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
  1715. if (isset($prop['strokeColor'])) {
  1716. if (is_array($prop['strokeColor'])) {
  1717. $opt['mk']['bc'] = $prop['strokeColor'];
  1718. } else {
  1719. $opt['mk']['bc'] = TCPDF_COLORS::convertHTMLColorToDec($prop['strokeColor'], $spot_colors);
  1720. }
  1721. }
  1722. // rotation: The rotation of a widget in counterclockwise increments.
  1723. if (isset($prop['rotation'])) {
  1724. $opt['mk']['r'] = $prop['rotation'];
  1725. }
  1726. // charLimit: Limits the number of characters that a user can type into a text field.
  1727. if (isset($prop['charLimit'])) {
  1728. $opt['maxlen'] = intval($prop['charLimit']);
  1729. }
  1730. if (!isset($ff)) {
  1731. $ff = 0; // default value
  1732. }
  1733. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  1734. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  1735. $ff += 1 << 0;
  1736. }
  1737. // required: Specifies whether a field requires a value.
  1738. if (isset($prop['required']) AND ($prop['required'] == 'true')) {
  1739. $ff += 1 << 1;
  1740. }
  1741. // multiline: Controls how text is wrapped within the field.
  1742. if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
  1743. $ff += 1 << 12;
  1744. }
  1745. // password: Specifies whether the field should display asterisks when data is entered in the field.
  1746. if (isset($prop['password']) AND ($prop['password'] == 'true')) {
  1747. $ff += 1 << 13;
  1748. }
  1749. // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
  1750. if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
  1751. $ff += 1 << 14;
  1752. }
  1753. // Radio: If set, the field is a set of radio buttons.
  1754. if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
  1755. $ff += 1 << 15;
  1756. }
  1757. // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
  1758. if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
  1759. $ff += 1 << 16;
  1760. }
  1761. // Combo: If set, the field is a combo box; if clear, the field is a list box.
  1762. if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
  1763. $ff += 1 << 17;
  1764. }
  1765. // editable: Controls whether a combo box is editable.
  1766. if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
  1767. $ff += 1 << 18;
  1768. }
  1769. // Sort: If set, the field's option items shall be sorted alphabetically.
  1770. if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
  1771. $ff += 1 << 19;
  1772. }
  1773. // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
  1774. if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
  1775. $ff += 1 << 20;
  1776. }
  1777. // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
  1778. if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
  1779. $ff += 1 << 21;
  1780. }
  1781. // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
  1782. if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
  1783. $ff += 1 << 22;
  1784. }
  1785. // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
  1786. if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
  1787. $ff += 1 << 23;
  1788. }
  1789. // comb: If set to true, the field background is drawn as series of boxes (one for each character in the value of the field) and each character of the content is drawn within those boxes. The number of boxes drawn is determined from the charLimit property. It applies only to text fields. The setter will also raise if any of the following field properties are also set multiline, password, and fileSelect. A side-effect of setting this property is that the doNotScroll property is also set.
  1790. if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
  1791. $ff += 1 << 24;
  1792. }
  1793. // radiosInUnison: If false, even if a group of radio buttons have the same name and export value, they behave in a mutually exclusive fashion, like HTML radio buttons.
  1794. if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
  1795. $ff += 1 << 25;
  1796. }
  1797. // richText: If true, the field allows rich text formatting.
  1798. if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
  1799. $ff += 1 << 25;
  1800. }
  1801. // commitOnSelChange: Controls whether a field value is committed after a selection change.
  1802. if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
  1803. $ff += 1 << 26;
  1804. }
  1805. $opt['ff'] = $ff;
  1806. // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
  1807. if (isset($prop['defaultValue'])) {
  1808. $opt['dv'] = $prop['defaultValue'];
  1809. }
  1810. $f = 4; // default value for annotation flags
  1811. // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
  1812. if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
  1813. $f += 1 << 6;
  1814. }
  1815. // display: Controls whether the field is hidden or visible on screen and in print.
  1816. if (isset($prop['display'])) {
  1817. if ($prop['display'] == 'display.visible') {
  1818. //
  1819. } elseif ($prop['display'] == 'display.hidden') {
  1820. $f += 1 << 1;
  1821. } elseif ($prop['display'] == 'display.noPrint') {
  1822. $f -= 1 << 2;
  1823. } elseif ($prop['display'] == 'display.noView') {
  1824. $f += 1 << 5;
  1825. }
  1826. }
  1827. $opt['f'] = $f;
  1828. // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
  1829. if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
  1830. $opt['i'] = $prop['currentValueIndices'];
  1831. }
  1832. // value: The value of the field data that the user has entered.
  1833. if (isset($prop['value'])) {
  1834. if (is_array($prop['value'])) {
  1835. $opt['opt'] = array();
  1836. foreach ($prop['value'] AS $key => $optval) {
  1837. // exportValues: An array of strings representing the export values for the field.
  1838. if (isset($prop['exportValues'][$key])) {
  1839. $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
  1840. } else {
  1841. $opt['opt'][$key] = $prop['value'][$key];
  1842. }
  1843. }
  1844. } else {
  1845. $opt['v'] = $prop['value'];
  1846. }
  1847. }
  1848. // richValue: This property specifies the text contents and formatting of a rich text field.
  1849. if (isset($prop['richValue'])) {
  1850. $opt['rv'] = $prop['richValue'];
  1851. }
  1852. // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
  1853. if (isset($prop['submitName'])) {
  1854. $opt['tm'] = $prop['submitName'];
  1855. }
  1856. // name: Fully qualified field name.
  1857. if (isset($prop['name'])) {
  1858. $opt['t'] = $prop['name'];
  1859. }
  1860. // userName: The user name (short description string) of the field.
  1861. if (isset($prop['userName'])) {
  1862. $opt['tu'] = $prop['userName'];
  1863. }
  1864. // highlight: Defines how a button reacts when a user clicks it.
  1865. if (isset($prop['highlight'])) {
  1866. switch ($prop['highlight']) {
  1867. case 'none':
  1868. case 'highlight.n': {
  1869. $opt['h'] = 'N';
  1870. break;
  1871. }
  1872. case 'invert':
  1873. case 'highlight.i': {
  1874. $opt['h'] = 'i';
  1875. break;
  1876. }
  1877. case 'push':
  1878. case 'highlight.p': {
  1879. $opt['h'] = 'P';
  1880. break;
  1881. }
  1882. case 'outline':
  1883. case 'highlight.o': {
  1884. $opt['h'] = 'O';
  1885. break;
  1886. }
  1887. }
  1888. }
  1889. // Unsupported options:
  1890. // - calcOrderIndex: Changes the calculation order of fields in the document.
  1891. // - delay: Delays the redrawing of a field's appearance.
  1892. // - defaultStyle: This property defines the default style attributes for the form field.
  1893. // - style: Allows the user to set the glyph style of a check box or radio button.
  1894. // - textColor, textFont, textSize
  1895. return $opt;
  1896. }
  1897. /**
  1898. * Format the page numbers.
  1899. * This method can be overriden for custom formats.
  1900. * @param $num (int) page number
  1901. * @since 4.2.005 (2008-11-06)
  1902. * @public static
  1903. */
  1904. public static function formatPageNumber($num) {
  1905. return number_format((float)$num, 0, '', '.');
  1906. }
  1907. /**
  1908. * Format the page numbers on the Table Of Content.
  1909. * This method can be overriden for custom formats.
  1910. * @param $num (int) page number
  1911. * @since 4.5.001 (2009-01-04)
  1912. * @see addTOC(), addHTMLTOC()
  1913. * @public static
  1914. */
  1915. public static function formatTOCPageNumber($num) {
  1916. return number_format((float)$num, 0, '', '.');
  1917. }
  1918. /**
  1919. * Extracts the CSS properties from a CSS string.
  1920. * @param $cssdata (string) string containing CSS definitions.
  1921. * @return An array where the keys are the CSS selectors and the values are the CSS properties.
  1922. * @author Nicola Asuni
  1923. * @since 5.1.000 (2010-05-25)
  1924. * @public static
  1925. */
  1926. public static function extractCSSproperties($cssdata) {
  1927. if (empty($cssdata)) {
  1928. return array();
  1929. }
  1930. // remove comments
  1931. $cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
  1932. // remove newlines and multiple spaces
  1933. $cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
  1934. // remove some spaces
  1935. $cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
  1936. // remove empty blocks
  1937. $cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
  1938. // replace media type parenthesis
  1939. $cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\1§', $cssdata);
  1940. $cssdata = preg_replace('/\}\}/si', '}§', $cssdata);
  1941. // trim string
  1942. $cssdata = trim($cssdata);
  1943. // find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
  1944. $cssblocks = array();
  1945. $matches = array();
  1946. if (preg_match_all('/@media[\s]+([^\§]*)§([^§]*)§/i', $cssdata, $matches) > 0) {
  1947. foreach ($matches[1] as $key => $type) {
  1948. $cssblocks[$type] = $matches[2][$key];
  1949. }
  1950. // remove media blocks
  1951. $cssdata = preg_replace('/@media[\s]+([^\§]*)§([^§]*)§/i', '', $cssdata);
  1952. }
  1953. // keep 'all' and 'print' media, other media types are discarded
  1954. if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
  1955. $cssdata .= $cssblocks['all'];
  1956. }
  1957. if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
  1958. $cssdata .= $cssblocks['print'];
  1959. }
  1960. // reset css blocks array
  1961. $cssblocks = array();
  1962. $matches = array();
  1963. // explode css data string into array
  1964. if (substr($cssdata, -1) == '}') {
  1965. // remove last parethesis
  1966. $cssdata = substr($cssdata, 0, -1);
  1967. }
  1968. $matches = explode('}', $cssdata);
  1969. foreach ($matches as $key => $block) {
  1970. // index 0 contains the CSS selector, index 1 contains CSS properties
  1971. $cssblocks[$key] = explode('{', $block);
  1972. if (!isset($cssblocks[$key][1])) {
  1973. // remove empty definitions
  1974. unset($cssblocks[$key]);
  1975. }
  1976. }
  1977. // split groups of selectors (comma-separated list of selectors)
  1978. foreach ($cssblocks as $key => $block) {
  1979. if (strpos($block[0], ',') > 0) {
  1980. $selectors = explode(',', $block[0]);
  1981. foreach ($selectors as $sel) {
  1982. $cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
  1983. }
  1984. unset($cssblocks[$key]);
  1985. }
  1986. }
  1987. // covert array to selector => properties
  1988. $cssdata = array();
  1989. foreach ($cssblocks as $block) {
  1990. $selector = $block[0];
  1991. // calculate selector's specificity
  1992. $matches = array();
  1993. $a = 0; // the declaration is not from is a 'style' attribute
  1994. $b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
  1995. $c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
  1996. $c += intval(preg_match_all('/[\:]link|visited|hover|active|focus|target|lang|enabled|disabled|checked|indeterminate|root|nth|first|last|only|empty|contains|not/i', $selector, $matches)); // number of pseudo-classes
  1997. $d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' '.$selector, $matches)); // number of element names
  1998. $d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
  1999. $specificity = $a.$b.$c.$d;
  2000. // add specificity to the beginning of the selector
  2001. $cssdata[$specificity.' '.$selector] = $block[1];
  2002. }
  2003. // sort selectors alphabetically to account for specificity
  2004. ksort($cssdata, SORT_STRING);
  2005. // return array
  2006. return $cssdata;
  2007. }
  2008. /**
  2009. * Cleanup HTML code (requires HTML Tidy library).
  2010. * @param $html (string) htmlcode to fix
  2011. * @param $default_css (string) CSS commands to add
  2012. * @param $tagvs (array) parameters for setHtmlVSpace method
  2013. * @param $tidy_options (array) options for tidy_parse_string function
  2014. * @param $tagvspaces (array) Array of vertical spaces for tags.
  2015. * @return string XHTML code cleaned up
  2016. * @author Nicola Asuni
  2017. * @since 5.9.017 (2010-11-16)
  2018. * @see setHtmlVSpace()
  2019. * @public static
  2020. */
  2021. public static function fixHTMLCode($html, $default_css='', $tagvs='', $tidy_options='', &$tagvspaces) {
  2022. // configure parameters for HTML Tidy
  2023. if ($tidy_options === '') {
  2024. $tidy_options = array (
  2025. 'clean' => 1,
  2026. 'drop-empty-paras' => 0,
  2027. 'drop-proprietary-attributes' => 1,
  2028. 'fix-backslash' => 1,
  2029. 'hide-comments' => 1,
  2030. 'join-styles' => 1,
  2031. 'lower-literals' => 1,
  2032. 'merge-divs' => 1,
  2033. 'merge-spans' => 1,
  2034. 'output-xhtml' => 1,
  2035. 'word-2000' => 1,
  2036. 'wrap' => 0,
  2037. 'output-bom' => 0,
  2038. //'char-encoding' => 'utf8',
  2039. //'input-encoding' => 'utf8',
  2040. //'output-encoding' => 'utf8'
  2041. );
  2042. }
  2043. // clean up the HTML code
  2044. $tidy = tidy_parse_string($html, $tidy_options);
  2045. // fix the HTML
  2046. $tidy->cleanRepair();
  2047. // get the CSS part
  2048. $tidy_head = tidy_get_head($tidy);
  2049. $css = $tidy_head->value;
  2050. $css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
  2051. $css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
  2052. $css = str_replace('/*<![CDATA[*/', '', $css);
  2053. $css = str_replace('/*]]>*/', '', $css);
  2054. preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
  2055. if (isset($matches[1])) {
  2056. $css = strtolower($matches[1]);
  2057. } else {
  2058. $css = '';
  2059. }
  2060. // include default css
  2061. $css = '<style>'.$default_css.$css.'</style>';
  2062. // get the body part
  2063. $tidy_body = tidy_get_body($tidy);
  2064. $html = $tidy_body->value;
  2065. // fix some self-closing tags
  2066. $html = str_replace('<br>', '<br />', $html);
  2067. // remove some empty tag blocks
  2068. $html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
  2069. $html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
  2070. if ($tagvs !== '') {
  2071. // set vertical space for some XHTML tags
  2072. $tagvspaces = $tagvs;
  2073. }
  2074. // return the cleaned XHTML code + CSS
  2075. return $css.$html;
  2076. }
  2077. /**
  2078. * Returns true if the CSS selector is valid for the selected HTML tag
  2079. * @param $dom (array) array of HTML tags and properties
  2080. * @param $key (int) key of the current HTML tag
  2081. * @param $selector (string) CSS selector string
  2082. * @return true if the selector is valid, false otherwise
  2083. * @since 5.1.000 (2010-05-25)
  2084. * @public static
  2085. */
  2086. public static function isValidCSSSelectorForTag($dom, $key, $selector) {
  2087. $valid = false; // value to be returned
  2088. $tag = $dom[$key]['value'];
  2089. $class = array();
  2090. if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
  2091. $class = explode(' ', strtolower($dom[$key]['attribute']['class']));
  2092. }
  2093. $id = '';
  2094. if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
  2095. $id = strtolower($dom[$key]['attribute']['id']);
  2096. }
  2097. $selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
  2098. $matches = array();
  2099. if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
  2100. $parentop = array_pop($matches[1]);
  2101. $operator = $parentop[0];
  2102. $offset = $parentop[1];
  2103. $lasttag = array_pop($matches[2]);
  2104. $lasttag = strtolower(trim($lasttag[0]));
  2105. if (($lasttag == '*') OR ($lasttag == $tag)) {
  2106. // the last element on selector is our tag or 'any tag'
  2107. $attrib = array_pop($matches[3]);
  2108. $attrib = strtolower(trim($attrib[0]));
  2109. if (!empty($attrib)) {
  2110. // check if matches class, id, attribute, pseudo-class or pseudo-element
  2111. switch ($attrib{0}) {
  2112. case '.': { // class
  2113. if (in_array(substr($attrib, 1), $class)) {
  2114. $valid = true;
  2115. }
  2116. break;
  2117. }
  2118. case '#': { // ID
  2119. if (substr($attrib, 1) == $id) {
  2120. $valid = true;
  2121. }
  2122. break;
  2123. }
  2124. case '[': { // attribute
  2125. $attrmatch = array();
  2126. if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
  2127. $att = strtolower($attrmatch[1]);
  2128. $val = $attrmatch[3];
  2129. if (isset($dom[$key]['attribute'][$att])) {
  2130. switch ($attrmatch[2]) {
  2131. case '=': {
  2132. if ($dom[$key]['attribute'][$att] == $val) {
  2133. $valid = true;
  2134. }
  2135. break;
  2136. }
  2137. case '~=': {
  2138. if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
  2139. $valid = true;
  2140. }
  2141. break;
  2142. }
  2143. case '^=': {
  2144. if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
  2145. $valid = true;
  2146. }
  2147. break;
  2148. }
  2149. case '$=': {
  2150. if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
  2151. $valid = true;
  2152. }
  2153. break;
  2154. }
  2155. case '*=': {
  2156. if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
  2157. $valid = true;
  2158. }
  2159. break;
  2160. }
  2161. case '|=': {
  2162. if ($dom[$key]['attribute'][$att] == $val) {
  2163. $valid = true;
  2164. } elseif (preg_match('/'.$val.'[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
  2165. $valid = true;
  2166. }
  2167. break;
  2168. }
  2169. default: {
  2170. $valid = true;
  2171. }
  2172. }
  2173. }
  2174. }
  2175. break;
  2176. }
  2177. case ':': { // pseudo-class or pseudo-element
  2178. if ($attrib{1} == ':') { // pseudo-element
  2179. // pseudo-elements are not supported!
  2180. // (::first-line, ::first-letter, ::before, ::after)
  2181. } else { // pseudo-class
  2182. // pseudo-classes are not supported!
  2183. // (:root, :nth-child(n), :nth-last-child(n), :nth-of-type(n), :nth-last-of-type(n), :first-child, :last-child, :first-of-type, :last-of-type, :only-child, :only-of-type, :empty, :link, :visited, :active, :hover, :focus, :target, :lang(fr), :enabled, :disabled, :checked)
  2184. }
  2185. break;
  2186. }
  2187. } // end of switch
  2188. } else {
  2189. $valid = true;
  2190. }
  2191. if ($valid AND ($offset > 0)) {
  2192. $valid = false;
  2193. // check remaining selector part
  2194. $selector = substr($selector, 0, $offset);
  2195. switch ($operator) {
  2196. case ' ': { // descendant of an element
  2197. while ($dom[$key]['parent'] > 0) {
  2198. if (self::isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
  2199. $valid = true;
  2200. break;
  2201. } else {
  2202. $key = $dom[$key]['parent'];
  2203. }
  2204. }
  2205. break;
  2206. }
  2207. case '>': { // child of an element
  2208. $valid = self::isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
  2209. break;
  2210. }
  2211. case '+': { // immediately preceded by an element
  2212. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  2213. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  2214. $valid = self::isValidCSSSelectorForTag($dom, $i, $selector);
  2215. break;
  2216. }
  2217. }
  2218. break;
  2219. }
  2220. case '~': { // preceded by an element
  2221. for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
  2222. if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
  2223. if (self::isValidCSSSelectorForTag($dom, $i, $selector)) {
  2224. break;
  2225. }
  2226. }
  2227. }
  2228. break;
  2229. }
  2230. }
  2231. }
  2232. }
  2233. }
  2234. return $valid;
  2235. }
  2236. /**
  2237. * Returns the styles array that apply for the selected HTML tag.
  2238. * @param $dom (array) array of HTML tags and properties
  2239. * @param $key (int) key of the current HTML tag
  2240. * @param $css (array) array of CSS properties
  2241. * @return array containing CSS properties
  2242. * @since 5.1.000 (2010-05-25)
  2243. * @public static
  2244. */
  2245. public static function getCSSdataArray($dom, $key, $css) {
  2246. $cssarray = array(); // style to be returned
  2247. // get parent CSS selectors
  2248. $selectors = array();
  2249. if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
  2250. $selectors = $dom[($dom[$key]['parent'])]['csssel'];
  2251. }
  2252. // get all styles that apply
  2253. foreach($css as $selector => $style) {
  2254. $pos = strpos($selector, ' ');
  2255. // get specificity
  2256. $specificity = substr($selector, 0, $pos);
  2257. // remove specificity
  2258. $selector = substr($selector, $pos);
  2259. // check if this selector apply to current tag
  2260. if (self::isValidCSSSelectorForTag($dom, $key, $selector)) {
  2261. if (!in_array($selector, $selectors)) {
  2262. // add style if not already added on parent selector
  2263. $cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
  2264. $selectors[] = $selector;
  2265. }
  2266. }
  2267. }
  2268. if (isset($dom[$key]['attribute']['style'])) {
  2269. // attach inline style (latest properties have high priority)
  2270. $cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
  2271. }
  2272. // order the css array to account for specificity
  2273. $cssordered = array();
  2274. foreach ($cssarray as $key => $val) {
  2275. $skey = sprintf('%04d', $key);
  2276. $cssordered[$val['s'].'_'.$skey] = $val;
  2277. }
  2278. // sort selectors alphabetically to account for specificity
  2279. ksort($cssordered, SORT_STRING);
  2280. return array($selectors, $cssordered);
  2281. }
  2282. /**
  2283. * Compact CSS data array into single string.
  2284. * @param $css (array) array of CSS properties
  2285. * @return string containing merged CSS properties
  2286. * @since 5.9.070 (2011-04-19)
  2287. * @public static
  2288. */
  2289. public static function getTagStyleFromCSSarray($css) {
  2290. $tagstyle = ''; // value to be returned
  2291. foreach ($css as $style) {
  2292. // split single css commands
  2293. $csscmds = explode(';', $style['c']);
  2294. foreach ($csscmds as $cmd) {
  2295. if (!empty($cmd)) {
  2296. $pos = strpos($cmd, ':');
  2297. if ($pos !== false) {
  2298. $cmd = substr($cmd, 0, ($pos + 1));
  2299. if (strpos($tagstyle, $cmd) !== false) {
  2300. // remove duplicate commands (last commands have high priority)
  2301. $tagstyle = preg_replace('/'.$cmd.'[^;]+/i', '', $tagstyle);
  2302. }
  2303. }
  2304. }
  2305. }
  2306. $tagstyle .= ';'.$style['c'];
  2307. }
  2308. // remove multiple semicolons
  2309. $tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
  2310. return $tagstyle;
  2311. }
  2312. /**
  2313. * Returns the Roman representation of an integer number
  2314. * @param $number (int) number to convert
  2315. * @return string roman representation of the specified number
  2316. * @since 4.4.004 (2008-12-10)
  2317. * @public static
  2318. */
  2319. public static function intToRoman($number) {
  2320. $roman = '';
  2321. while ($number >= 1000) {
  2322. $roman .= 'M';
  2323. $number -= 1000;
  2324. }
  2325. while ($number >= 900) {
  2326. $roman .= 'CM';
  2327. $number -= 900;
  2328. }
  2329. while ($number >= 500) {
  2330. $roman .= 'D';
  2331. $number -= 500;
  2332. }
  2333. while ($number >= 400) {
  2334. $roman .= 'CD';
  2335. $number -= 400;
  2336. }
  2337. while ($number >= 100) {
  2338. $roman .= 'C';
  2339. $number -= 100;
  2340. }
  2341. while ($number >= 90) {
  2342. $roman .= 'XC';
  2343. $number -= 90;
  2344. }
  2345. while ($number >= 50) {
  2346. $roman .= 'L';
  2347. $number -= 50;
  2348. }
  2349. while ($number >= 40) {
  2350. $roman .= 'XL';
  2351. $number -= 40;
  2352. }
  2353. while ($number >= 10) {
  2354. $roman .= 'X';
  2355. $number -= 10;
  2356. }
  2357. while ($number >= 9) {
  2358. $roman .= 'IX';
  2359. $number -= 9;
  2360. }
  2361. while ($number >= 5) {
  2362. $roman .= 'V';
  2363. $number -= 5;
  2364. }
  2365. while ($number >= 4) {
  2366. $roman .= 'IV';
  2367. $number -= 4;
  2368. }
  2369. while ($number >= 1) {
  2370. $roman .= 'I';
  2371. --$number;
  2372. }
  2373. return $roman;
  2374. }
  2375. /**
  2376. * Find position of last occurrence of a substring in a string
  2377. * @param $haystack (string) The string to search in.
  2378. * @param $needle (string) substring to search.
  2379. * @param $offset (int) May be specified to begin searching an arbitrary number of characters into the string.
  2380. * @return Returns the position where the needle exists. Returns FALSE if the needle was not found.
  2381. * @since 4.8.038 (2010-03-13)
  2382. * @public static
  2383. */
  2384. public static function revstrpos($haystack, $needle, $offset = 0) {
  2385. $length = strlen($haystack);
  2386. $offset = ($offset > 0)?($length - $offset):abs($offset);
  2387. $pos = strpos(strrev($haystack), strrev($needle), $offset);
  2388. return ($pos === false)?false:($length - $pos - strlen($needle));
  2389. }
  2390. /**
  2391. * Serialize an array of parameters to be used with TCPDF tag in HTML code.
  2392. * @param $pararray (array) parameters array
  2393. * @return sting containing serialized data
  2394. * @since 4.9.006 (2010-04-02)
  2395. * @public static
  2396. */
  2397. public static function serializeTCPDFtagParameters($pararray) {
  2398. return urlencode(serialize($pararray));
  2399. }
  2400. /**
  2401. * Returns an array of hyphenation patterns.
  2402. * @param $file (string) TEX file containing hypenation patterns. TEX pattrns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
  2403. * @return array of hyphenation patterns
  2404. * @author Nicola Asuni
  2405. * @since 4.9.012 (2010-04-12)
  2406. * @public static
  2407. */
  2408. public static function getHyphenPatternsFromTEX($file) {
  2409. // TEX patterns are available at:
  2410. // http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
  2411. $data = file_get_contents($file);
  2412. $patterns = array();
  2413. // remove comments
  2414. $data = preg_replace('/\%[^\n]*/', '', $data);
  2415. // extract the patterns part
  2416. preg_match('/\\\\patterns\{([^\}]*)\}/i', $data, $matches);
  2417. $data = trim(substr($matches[0], 10, -1));
  2418. // extract each pattern
  2419. $patterns_array = preg_split('/[\s]+/', $data);
  2420. // create new language array of patterns
  2421. $patterns = array();
  2422. foreach($patterns_array as $val) {
  2423. if (!TCPDF_STATIC::empty_string($val)) {
  2424. $val = trim($val);
  2425. $val = str_replace('\'', '\\\'', $val);
  2426. $key = preg_replace('/[0-9]+/', '', $val);
  2427. $patterns[$key] = $val;
  2428. }
  2429. }
  2430. return $patterns;
  2431. }
  2432. /**
  2433. * Get the Path-Painting Operators.
  2434. * @param $style (string) Style of rendering. Possible values are:
  2435. * <ul>
  2436. * <li>S or D: Stroke the path.</li>
  2437. * <li>s or d: Close and stroke the path.</li>
  2438. * <li>f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.</li>
  2439. * <li>f* or F*: Fill the path, using the even-odd rule to determine the region to fill.</li>
  2440. * <li>B or FD or DF: Fill and then stroke the path, using the nonzero winding number rule to determine the region to fill.</li>
  2441. * <li>B* or F*D or DF*: Fill and then stroke the path, using the even-odd rule to determine the region to fill.</li>
  2442. * <li>b or fd or df: Close, fill, and then stroke the path, using the nonzero winding number rule to determine the region to fill.</li>
  2443. * <li>b or f*d or df*: Close, fill, and then stroke the path, using the even-odd rule to determine the region to fill.</li>
  2444. * <li>CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.</li>
  2445. * <li>CEO: Clipping mode using the nonzero winding number rule to determine which regions lie inside the clipping path</li>
  2446. * <li>n: End the path object without filling or stroking it.</li>
  2447. * </ul>
  2448. * @param $default (string) default style
  2449. * @author Nicola Asuni
  2450. * @since 5.0.000 (2010-04-30)
  2451. * @public static
  2452. */
  2453. public static function getPathPaintOperator($style, $default='S') {
  2454. $op = '';
  2455. switch($style) {
  2456. case 'S':
  2457. case 'D': {
  2458. $op = 'S';
  2459. break;
  2460. }
  2461. case 's':
  2462. case 'd': {
  2463. $op = 's';
  2464. break;
  2465. }
  2466. case 'f':
  2467. case 'F': {
  2468. $op = 'f';
  2469. break;
  2470. }
  2471. case 'f*':
  2472. case 'F*': {
  2473. $op = 'f*';
  2474. break;
  2475. }
  2476. case 'B':
  2477. case 'FD':
  2478. case 'DF': {
  2479. $op = 'B';
  2480. break;
  2481. }
  2482. case 'B*':
  2483. case 'F*D':
  2484. case 'DF*': {
  2485. $op = 'B*';
  2486. break;
  2487. }
  2488. case 'b':
  2489. case 'fd':
  2490. case 'df': {
  2491. $op = 'b';
  2492. break;
  2493. }
  2494. case 'b*':
  2495. case 'f*d':
  2496. case 'df*': {
  2497. $op = 'b*';
  2498. break;
  2499. }
  2500. case 'CNZ': {
  2501. $op = 'W n';
  2502. break;
  2503. }
  2504. case 'CEO': {
  2505. $op = 'W* n';
  2506. break;
  2507. }
  2508. case 'n': {
  2509. $op = 'n';
  2510. break;
  2511. }
  2512. default: {
  2513. if (!empty($default)) {
  2514. $op = self::getPathPaintOperator($default, '');
  2515. } else {
  2516. $op = '';
  2517. }
  2518. }
  2519. }
  2520. return $op;
  2521. }
  2522. /**
  2523. * Get the product of two SVG tranformation matrices
  2524. * @param $ta (array) first SVG tranformation matrix
  2525. * @param $tb (array) second SVG tranformation matrix
  2526. * @return transformation array
  2527. * @author Nicola Asuni
  2528. * @since 5.0.000 (2010-05-02)
  2529. * @public static
  2530. */
  2531. public static function getTransformationMatrixProduct($ta, $tb) {
  2532. $tm = array();
  2533. $tm[0] = ($ta[0] * $tb[0]) + ($ta[2] * $tb[1]);
  2534. $tm[1] = ($ta[1] * $tb[0]) + ($ta[3] * $tb[1]);
  2535. $tm[2] = ($ta[0] * $tb[2]) + ($ta[2] * $tb[3]);
  2536. $tm[3] = ($ta[1] * $tb[2]) + ($ta[3] * $tb[3]);
  2537. $tm[4] = ($ta[0] * $tb[4]) + ($ta[2] * $tb[5]) + $ta[4];
  2538. $tm[5] = ($ta[1] * $tb[4]) + ($ta[3] * $tb[5]) + $ta[5];
  2539. return $tm;
  2540. }
  2541. /**
  2542. * Get the tranformation matrix from SVG transform attribute
  2543. * @param $attribute (string) transformation
  2544. * @return array of transformations
  2545. * @author Nicola Asuni
  2546. * @since 5.0.000 (2010-05-02)
  2547. * @public static
  2548. */
  2549. public static function getSVGTransformMatrix($attribute) {
  2550. // identity matrix
  2551. $tm = array(1, 0, 0, 1, 0, 0);
  2552. $transform = array();
  2553. if (preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)[\s]*\(([^\)]+)\)/si', $attribute, $transform, PREG_SET_ORDER) > 0) {
  2554. foreach ($transform as $key => $data) {
  2555. if (!empty($data[2])) {
  2556. $a = 1;
  2557. $b = 0;
  2558. $c = 0;
  2559. $d = 1;
  2560. $e = 0;
  2561. $f = 0;
  2562. $regs = array();
  2563. switch ($data[1]) {
  2564. case 'matrix': {
  2565. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  2566. $a = $regs[1];
  2567. $b = $regs[2];
  2568. $c = $regs[3];
  2569. $d = $regs[4];
  2570. $e = $regs[5];
  2571. $f = $regs[6];
  2572. }
  2573. break;
  2574. }
  2575. case 'translate': {
  2576. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  2577. $e = $regs[1];
  2578. $f = $regs[2];
  2579. } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  2580. $e = $regs[1];
  2581. }
  2582. break;
  2583. }
  2584. case 'scale': {
  2585. if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  2586. $a = $regs[1];
  2587. $d = $regs[2];
  2588. } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  2589. $a = $regs[1];
  2590. $d = $a;
  2591. }
  2592. break;
  2593. }
  2594. case 'rotate': {
  2595. if (preg_match('/([0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
  2596. $ang = deg2rad($regs[1]);
  2597. $x = $regs[2];
  2598. $y = $regs[3];
  2599. $a = cos($ang);
  2600. $b = sin($ang);
  2601. $c = -$b;
  2602. $d = $a;
  2603. $e = ($x * (1 - $a)) - ($y * $c);
  2604. $f = ($y * (1 - $d)) - ($x * $b);
  2605. } elseif (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  2606. $ang = deg2rad($regs[1]);
  2607. $a = cos($ang);
  2608. $b = sin($ang);
  2609. $c = -$b;
  2610. $d = $a;
  2611. $e = 0;
  2612. $f = 0;
  2613. }
  2614. break;
  2615. }
  2616. case 'skewX': {
  2617. if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  2618. $c = tan(deg2rad($regs[1]));
  2619. }
  2620. break;
  2621. }
  2622. case 'skewY': {
  2623. if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
  2624. $b = tan(deg2rad($regs[1]));
  2625. }
  2626. break;
  2627. }
  2628. }
  2629. $tm = self::getTransformationMatrixProduct($tm, array($a, $b, $c, $d, $e, $f));
  2630. }
  2631. }
  2632. }
  2633. return $tm;
  2634. }
  2635. /**
  2636. * Returns the angle in radiants between two vectors
  2637. * @param $x1 (int) X coordinate of first vector point
  2638. * @param $y1 (int) Y coordinate of first vector point
  2639. * @param $x2 (int) X coordinate of second vector point
  2640. * @param $y2 (int) Y coordinate of second vector point
  2641. * @author Nicola Asuni
  2642. * @since 5.0.000 (2010-05-04)
  2643. * @public static
  2644. */
  2645. public static function getVectorsAngle($x1, $y1, $x2, $y2) {
  2646. $dprod = ($x1 * $x2) + ($y1 * $y2);
  2647. $dist1 = sqrt(($x1 * $x1) + ($y1 * $y1));
  2648. $dist2 = sqrt(($x2 * $x2) + ($y2 * $y2));
  2649. $angle = acos($dprod / ($dist1 * $dist2));
  2650. if (is_nan($angle)) {
  2651. $angle = M_PI;
  2652. }
  2653. if ((($x1 * $y2) - ($x2 * $y1)) < 0) {
  2654. $angle *= -1;
  2655. }
  2656. return $angle;
  2657. }
  2658. /**
  2659. * Split string by a regular expression.
  2660. * This is a wrapper for the preg_split function to avoid the bug: https://bugs.php.net/bug.php?id=45850
  2661. * @param $pattern (string) The regular expression pattern to search for without the modifiers, as a string.
  2662. * @param $modifiers (string) The modifiers part of the pattern,
  2663. * @param $subject (string) The input string.
  2664. * @param $limit (int) If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or NULL means "no limit" and, as is standard across PHP, you can use NULL to skip to the flags parameter.
  2665. * @param $flags (int) The flags as specified on the preg_split PHP function.
  2666. * @return Returns an array containing substrings of subject split along boundaries matched by pattern.modifier
  2667. * @author Nicola Asuni
  2668. * @since 6.0.023
  2669. * @public static
  2670. */
  2671. public static function pregSplit($pattern, $modifiers, $subject, $limit=NULL, $flags=NULL) {
  2672. // the bug only happens on PHP 5.2 when using the u modifier
  2673. if ((strpos($modifiers, 'u') === FALSE) OR (count(preg_split('//u', "\n\t", -1, PREG_SPLIT_NO_EMPTY)) == 2)) {
  2674. return preg_split($pattern.$modifiers, $subject, $limit, $flags);
  2675. }
  2676. // preg_split is bugged - try alternative solution
  2677. $ret = array();
  2678. while (($nl = strpos($subject, "\n")) !== FALSE) {
  2679. $ret = array_merge($ret, preg_split($pattern.$modifiers, substr($subject, 0, $nl), $limit, $flags));
  2680. $ret[] = "\n";
  2681. $subject = substr($subject, ($nl + 1));
  2682. }
  2683. if (strlen($subject) > 0) {
  2684. $ret = array_merge($ret, preg_split($pattern.$modifiers, $subject, $limit, $flags));
  2685. }
  2686. return $ret;
  2687. }
  2688. /**
  2689. * Reads entire file into a string.
  2690. * The file can be also an URL.
  2691. * @param $file (string) Name of the file or URL to read.
  2692. * @return The function returns the read data or FALSE on failure.
  2693. * @author Nicola Asuni
  2694. * @since 6.0.025
  2695. * @public static
  2696. */
  2697. public static function fileGetContents($file) {
  2698. // array of possible alternative paths/URLs
  2699. $alt = array($file);
  2700. // replace URL relative path with full real server path
  2701. if ((strlen($file) > 1)
  2702. AND ($file[0] == '/')
  2703. AND ($file[1] != '/')
  2704. AND !empty($_SERVER['DOCUMENT_ROOT'])
  2705. AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
  2706. $findroot = strpos($file, $_SERVER['DOCUMENT_ROOT']);
  2707. if (($findroot === false) OR ($findroot > 1)) {
  2708. if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
  2709. $tmp = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$file;
  2710. } else {
  2711. $tmp = $_SERVER['DOCUMENT_ROOT'].$file;
  2712. }
  2713. $alt[] = htmlspecialchars_decode(urldecode($tmp));
  2714. }
  2715. }
  2716. // URL mode
  2717. $url = $file;
  2718. // check for missing protocol
  2719. if (preg_match('%^/{2}%', $url)) {
  2720. if (preg_match('%^([^:]+:)//%i', K_PATH_URL, $match)) {
  2721. $url = $match[1].str_replace(' ', '%20', $url);
  2722. $alt[] = $url;
  2723. }
  2724. }
  2725. $urldata = @parse_url($url);
  2726. if (!isset($urldata['query']) OR (strlen($urldata['query']) <= 0)) {
  2727. if (strpos($url, K_PATH_URL) === 0) {
  2728. // convert URL to full server path
  2729. $tmp = str_replace(K_PATH_URL, K_PATH_MAIN, $url);
  2730. $tmp = htmlspecialchars_decode(urldecode($tmp));
  2731. $alt[] = $tmp;
  2732. }
  2733. }
  2734. foreach ($alt as $f) {
  2735. $ret = @file_get_contents($f);
  2736. if (($ret === FALSE)
  2737. AND !ini_get('allow_url_fopen')
  2738. AND function_exists('curl_init')
  2739. AND preg_match('%^(https?|ftp)://%', $f)) {
  2740. // try to get remote file data using cURL
  2741. $cs = curl_init(); // curl session
  2742. curl_setopt($cs, CURLOPT_URL, $file);
  2743. curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
  2744. curl_setopt($cs, CURLOPT_FAILONERROR, true);
  2745. curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
  2746. if ((ini_get('open_basedir') == '') AND (!ini_get('safe_mode'))) {
  2747. curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
  2748. }
  2749. curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
  2750. curl_setopt($cs, CURLOPT_TIMEOUT, 30);
  2751. curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
  2752. curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
  2753. curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
  2754. $ret = curl_exec($cs);
  2755. curl_close($cs);
  2756. }
  2757. if ($ret !== FALSE) {
  2758. break;
  2759. }
  2760. }
  2761. return $ret;
  2762. }
  2763. } // END OF TCPDF_STATIC CLASS
  2764. //============================================================+
  2765. // END OF FILE
  2766. //============================================================+