PageRenderTime 69ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tcpdf/include/tcpdf_static.php

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