Passed
Push — master ( a46cb4...8ff499 )
by Michael
27:12 queued 15:45
created

TCPDF_STATIC::convertHexStringToString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 12
rs 10
1
<?php
2
//============================================================+
3
// File name   : tcpdf_static.php
4
// Version     : 1.1.5
5
// Begin       : 2002-08-03
6
// Last Update : 2024-12-23
7
// Author      : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - [email protected]
8
// License     : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9
// -------------------------------------------------------------------
10
// Copyright (C) 2002-2025 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
/**
37
 * @file
38
 * This is a PHP class that contains static methods for the TCPDF class.<br>
39
 * @package com.tecnick.tcpdf
40
 * @author Nicola Asuni
41
 * @version 1.1.5
42
 */
43
44
/**
45
 * @class TCPDF_STATIC
46
 * Static methods used by the TCPDF class.
47
 * @package com.tecnick.tcpdf
48
 * @brief PHP class for generating PDF documents without requiring external extensions.
49
 * @version 1.1.5
50
 * @author Nicola Asuni - [email protected]
51
 */
52
class TCPDF_STATIC {
53
54
	/**
55
	 * Current TCPDF version.
56
	 * @private static
57
	 */
58
	private static $tcpdf_version = '6.10.0';
59
60
	/**
61
	 * String alias for total number of pages.
62
	 * @public static
63
	 */
64
	public static $alias_tot_pages = '{:ptp:}';
65
66
	/**
67
	 * String alias for page number.
68
	 * @public static
69
	 */
70
	public static $alias_num_page = '{:pnp:}';
71
72
	/**
73
	 * String alias for total number of pages in a single group.
74
	 * @public static
75
	 */
76
	public static $alias_group_tot_pages = '{:ptg:}';
77
78
	/**
79
	 * String alias for group page number.
80
	 * @public static
81
	 */
82
	public static $alias_group_num_page = '{:png:}';
83
84
	/**
85
	 * String alias for right shift compensation used to correctly align page numbers on the right.
86
	 * @public static
87
	 */
88
	public static $alias_right_shift = '{rsc:';
89
90
	/**
91
	 * Encryption padding string.
92
	 * @public static
93
	 */
94
	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";
95
96
	/**
97
	 * ByteRange placemark used during digital signature process.
98
	 * @since 4.6.028 (2009-08-25)
99
	 * @public static
100
	 */
101
	public static $byterange_string = '/ByteRange[0 ********** ********** **********]';
102
103
	/**
104
	 * Array page boxes names
105
	 * @public static
106
	 */
107
	public static $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
108
109
	/**
110
     * Array of default cURL options for curl_setopt_array.
111
     *
112
     * @var array<int, bool|int|string> cURL options.
113
     */
114
    protected const CURLOPT_DEFAULT = [
115
        CURLOPT_CONNECTTIMEOUT => 5,
116
        CURLOPT_MAXREDIRS => 5,
117
        CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP | CURLPROTO_FTP | CURLPROTO_FTPS,
118
        CURLOPT_SSL_VERIFYHOST => 2,
119
        CURLOPT_SSL_VERIFYPEER => true,
120
        CURLOPT_TIMEOUT => 30,
121
        CURLOPT_USERAGENT => 'tcpdf',
122
    ];
123
124
    /**
125
     * Array of fixed cURL options for curl_setopt_array.
126
     *
127
     * @var array<int, bool|int|string> cURL options.
128
     */
129
    protected const CURLOPT_FIXED = [
130
        CURLOPT_FAILONERROR => true,
131
        CURLOPT_RETURNTRANSFER => true,
132
    ];
133
134
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135
136
	/**
137
	 * Return the current TCPDF version.
138
	 * @return string TCPDF version string
139
	 * @since 5.9.012 (2010-11-10)
140
	 * @public static
141
	 */
142
	public static function getTCPDFVersion() {
143
		return self::$tcpdf_version;
144
	}
145
146
	/**
147
	 * Return the current TCPDF producer.
148
	 * @return string TCPDF producer string
149
	 * @since 6.0.000 (2013-03-16)
150
	 * @public static
151
	 */
152
	public static function getTCPDFProducer() {
153
		return "\x54\x43\x50\x44\x46\x20".self::getTCPDFVersion()."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
154
	}
155
156
	/**
157
	 * Check if the URL exist.
158
	 * @param string $url URL to check.
159
	 * @return boolean true if the URl exist, false otherwise.
160
	 * @since 5.9.204 (2013-01-28)
161
	 * @public static
162
	 */
163
	public static function isValidURL($url) {
164
		$headers = @get_headers($url);
165
		if ($headers === false) {
166
			return false;
167
		}
168
    	return (strpos($headers[0], '200') !== false);
169
	}
170
171
	/**
172
	 * Removes SHY characters from text.
173
	 * Unicode Data:<ul>
174
	 * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
175
	 * <li>HTML Entity (decimal): "&amp;#173;"</li>
176
	 * <li>HTML Entity (hex): "&amp;#xad;"</li>
177
	 * <li>HTML Entity (named): "&amp;shy;"</li>
178
	 * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
179
	 * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
180
	 * <li>UTF-8 character: chr(194).chr(173)</li>
181
	 * </ul>
182
	 * @param string $txt input string
183
	 * @param boolean $unicode True if we are in unicode mode, false otherwise.
184
	 * @return string without SHY characters.
185
	 * @since (4.5.019) 2009-02-28
186
	 * @public static
187
	 */
188
	public static function removeSHY($txt='', $unicode=true) {
189
		$txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
190
		if (!$unicode) {
191
			$txt = preg_replace('/([\\xad]{1})/', '', $txt);
192
		}
193
		return $txt;
194
	}
195
196
197
	/**
198
	 * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
199
	 * @param string|array|int $brd 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)))
200
	 * @param string $position multicell position: 'start', 'middle', 'end'
201
	 * @param boolean $opencell True when the cell is left open at the page bottom, false otherwise.
202
	 * @return array border mode array
203
	 * @since 4.4.002 (2008-12-09)
204
	 * @public static
205
	 */
206
	public static function getBorderMode($brd, $position='start', $opencell=true) {
207
		if ((!$opencell) OR empty($brd)) {
208
			return $brd;
209
		}
210
		if ($brd == 1) {
211
			$brd = 'LTRB';
212
		}
213
		if (is_string($brd)) {
214
			// convert string to array
215
			$slen = strlen($brd);
216
			$newbrd = array();
217
			for ($i = 0; $i < $slen; ++$i) {
218
				$newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
219
			}
220
			$brd = $newbrd;
221
		}
222
		foreach ($brd as $border => $style) {
223
			switch ($position) {
224
				case 'start': {
225
					if (strpos($border, 'B') !== false) {
226
						// remove bottom line
227
						$newkey = str_replace('B', '', $border);
228
						if (strlen($newkey) > 0) {
229
							$brd[$newkey] = $style;
230
						}
231
						unset($brd[$border]);
232
					}
233
					break;
234
				}
235
				case 'middle': {
236
					if (strpos($border, 'B') !== false) {
237
						// remove bottom line
238
						$newkey = str_replace('B', '', $border);
239
						if (strlen($newkey) > 0) {
240
							$brd[$newkey] = $style;
241
						}
242
						unset($brd[$border]);
243
						$border = $newkey;
244
					}
245
					if (strpos($border, 'T') !== false) {
246
						// remove bottom line
247
						$newkey = str_replace('T', '', $border);
248
						if (strlen($newkey) > 0) {
249
							$brd[$newkey] = $style;
250
						}
251
						unset($brd[$border]);
252
					}
253
					break;
254
				}
255
				case 'end': {
256
					if (strpos($border, 'T') !== false) {
257
						// remove bottom line
258
						$newkey = str_replace('T', '', $border);
259
						if (strlen($newkey) > 0) {
260
							$brd[$newkey] = $style;
261
						}
262
						unset($brd[$border]);
263
					}
264
					break;
265
				}
266
			}
267
		}
268
		return $brd;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $brd also could return the type integer|string which is incompatible with the documented return type array.
Loading history...
269
	}
270
271
	/**
272
	 * Determine whether a string is empty.
273
	 * @param string $str string to be checked
274
	 * @return bool true if string is empty
275
	 * @since 4.5.044 (2009-04-16)
276
	 * @public static
277
	 */
278
	public static function empty_string($str) {
279
		return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
280
	}
281
282
	/**
283
	 * Returns a temporary filename for caching object on filesystem.
284
	 * @param string $type Type of file (name of the subdir on the tcpdf cache folder).
285
	 * @param string $file_id TCPDF file_id.
286
	 * @return string filename.
287
	 * @since 4.5.000 (2008-12-31)
288
	 * @public static
289
	 */
290
	public static function getObjFilename($type='tmp', $file_id='') {
291
		return tempnam(K_PATH_CACHE, '__tcpdf_'.$file_id.'_'.$type.'_'.md5(TCPDF_STATIC::getRandomSeed()).'_');
292
	}
293
294
	/**
295
	 * Add "\" before "\", "(" and ")"
296
	 * @param string $s string to escape.
297
	 * @return string escaped string.
298
	 * @public static
299
	 */
300
	public static function _escape($s) {
301
		// the chr(13) substitution fixes the Bugs item #1421290.
302
		return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
303
	}
304
305
	/**
306
	* Escape some special characters (&lt; &gt; &amp;) for XML output.
307
	* @param string $str Input string to convert.
308
	* @return string converted string
309
	* @since 5.9.121 (2011-09-28)
310
	 * @public static
311
	 */
312
	public static function _escapeXML($str) {
313
		$replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
314
		$str = strtr($str === null ? '' : $str, $replaceTable);
0 ignored issues
show
introduced by
The condition $str === null is always false.
Loading history...
315
		return $str;
316
	}
317
318
	/**
319
	 * Creates a copy of a class object
320
	 * @param object $object class object to be cloned
321
	 * @return object cloned object
322
	 * @since 4.5.029 (2009-03-19)
323
	 * @public static
324
	 */
325
	public static function objclone($object) {
326
		if (($object instanceof Imagick) AND (version_compare(phpversion('imagick'), '3.0.1') !== 1)) {
327
			// on the versions after 3.0.1 the clone() method was deprecated in favour of clone keyword
328
			return @$object->clone();
329
		}
330
		return @clone($object);
331
	}
332
333
	/**
334
	 * Output input data and compress it if possible.
335
	 * @param string $data Data to output.
336
	 * @param int $length Data length in bytes.
337
	 * @since 5.9.086
338
	 * @public static
339
	 */
340
	public static function sendOutputData($data, $length) {
341
		if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
342
			// the content length may vary if the server is using compression
343
			header('Content-Length: '.$length);
344
		}
345
		echo $data;
346
	}
347
348
	/**
349
	 * Replace page number aliases with number.
350
	 * @param string $page Page content.
351
	 * @param array $replace Array of replacements (array keys are replacement strings, values are alias arrays).
352
	 * @param int $diff If passed, this will be set to the total char number difference between alias and replacements.
353
	 * @return array replaced page content and updated $diff parameter as array.
354
	 * @public static
355
	 */
356
	public static function replacePageNumAliases($page, $replace, $diff=0) {
357
		foreach ($replace as $rep) {
358
			foreach ($rep[3] as $a) {
359
				if (strpos($page, $a) !== false) {
360
					$page = str_replace($a, $rep[0], $page);
361
					$diff += ($rep[2] - $rep[1]);
362
				}
363
			}
364
		}
365
		return array($page, $diff);
366
	}
367
368
	/**
369
	 * Returns timestamp in seconds from formatted date-time.
370
	 * @param string $date Formatted date-time.
371
	 * @return int seconds.
372
	 * @since 5.9.152 (2012-03-23)
373
	 * @public static
374
	 */
375
	public static function getTimestamp($date) {
376
		if (($date[0] == 'D') AND ($date[1] == ':')) {
377
			// remove date prefix if present
378
			$date = substr($date, 2);
379
		}
380
		return strtotime($date);
381
	}
382
383
	/**
384
	 * Returns a formatted date-time.
385
	 * @param int $time Time in seconds.
386
	 * @return string escaped date string.
387
	 * @since 5.9.152 (2012-03-23)
388
	 * @public static
389
	 */
390
	public static function getFormattedDate($time) {
391
		return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
0 ignored issues
show
Bug introduced by
Are you sure substr_replace(date('Ymd...$time)), ''', 0 - 2, 0) of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

391
		return /** @scrutinizer ignore-type */ substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
Loading history...
392
	}
393
394
	/**
395
	 * Returns a string containing random data to be used as a seed for encryption methods.
396
	 * @param string $seed starting seed value
397
	 * @return string containing random data
398
	 * @author Nicola Asuni
399
	 * @since 5.9.006 (2010-10-19)
400
	 * @public static
401
	 */
402
	public static function getRandomSeed($seed='') {
403
		$rnd = uniqid(rand().microtime(true), true);
404
		if (function_exists('posix_getpid')) {
405
			$rnd .= posix_getpid();
406
		}
407
408
		if (function_exists('random_bytes')) {
409
			$rnd .= random_bytes(512);
410
		} elseif (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
411
			// this is not used on windows systems because it is very slow for a know bug
412
			$rnd .= openssl_random_pseudo_bytes(512);
413
		} else {
414
			for ($i = 0; $i < 23; ++$i) {
415
				$rnd .= uniqid('', true);
416
			}
417
		}
418
		return $rnd.$seed.__FILE__.microtime(true);
419
	}
420
421
	/**
422
	 * Encrypts a string using MD5 and returns it's value as a binary string.
423
	 * @param string $str input string
424
	 * @return string MD5 encrypted binary string
425
	 * @since 2.0.000 (2008-01-02)
426
	 * @public static
427
	 */
428
	public static function _md5_16($str) {
429
		return pack('H*', md5($str));
430
	}
431
432
	/**
433
	 * Returns the input text encrypted using AES algorithm and the specified key.
434
	 * This method requires openssl or mcrypt. Text is padded to 16bytes blocks
435
	 * @param string $key encryption key
436
	 * @param string $text input text to be encrypted
437
	 * @return string encrypted text
438
	 * @author Nicola Asuni
439
	 * @since 5.0.005 (2010-05-11)
440
	 * @public static
441
	 */
442
	public static function _AES($key, $text) {
443
		// padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
444
		$padding = 16 - (strlen($text) % 16);
445
		$text .= str_repeat(chr($padding), $padding);
446
		if (extension_loaded('openssl')) {
447
			$algo = 'aes-256-cbc';
448
			if (strlen($key) == 16) {
449
				$algo = 'aes-128-cbc';
450
			}
451
			$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($algo));
452
			$text = openssl_encrypt($text, $algo, $key, OPENSSL_RAW_DATA, $iv);
453
			return $iv.substr($text, 0, -16);
454
		}
455
		$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
456
		$text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
457
		$text = $iv.$text;
458
		return $text;
459
	}
460
461
	/**
462
	 * Returns the input text encrypted using AES algorithm and the specified key.
463
	 * This method requires openssl or mcrypt. Text is not padded
464
	 * @param string $key encryption key
465
	 * @param string $text input text to be encrypted
466
	 * @return string encrypted text
467
	 * @author Nicola Asuni
468
	 * @since TODO
469
	 * @public static
470
	 */
471
	public static function _AESnopad($key, $text) {
472
		if (extension_loaded('openssl')) {
473
			$algo = 'aes-256-cbc';
474
			if (strlen($key) == 16) {
475
				$algo = 'aes-128-cbc';
476
			}
477
			$iv = str_repeat("\x00", openssl_cipher_iv_length($algo));
478
			$text = openssl_encrypt($text, $algo, $key, OPENSSL_RAW_DATA, $iv);
479
			return substr($text, 0, -16);
480
		}
481
		$iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
482
		$text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
483
		return $text;
484
	}
485
486
	/**
487
	 * Returns the input text encrypted using RC4 algorithm and the specified key.
488
	 * RC4 is the standard encryption algorithm used in PDF format
489
	 * @param string $key Encryption key.
490
	 * @param string $text Input text to be encrypted.
491
	 * @param string $last_enc_key Reference to last RC4 key encrypted.
492
	 * @param string $last_enc_key_c Reference to last RC4 computed key.
493
	 * @return string encrypted text
494
	 * @since 2.0.000 (2008-01-02)
495
	 * @author Klemen Vodopivec, Nicola Asuni
496
	 * @public static
497
	 */
498
	public static function _RC4($key, $text, &$last_enc_key, &$last_enc_key_c) {
499
		if (function_exists('mcrypt_encrypt') AND ($out = @mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
500
			// try to use mcrypt function if exist
501
			return $out;
502
		}
503
		if ($last_enc_key != $key) {
504
			$k = str_repeat($key, (int) ((256 / strlen($key)) + 1));
505
			$rc4 = range(0, 255);
506
			$j = 0;
507
			for ($i = 0; $i < 256; ++$i) {
508
				$t = $rc4[$i];
509
				$j = ($j + $t + ord($k[$i])) % 256;
510
				$rc4[$i] = $rc4[$j];
511
				$rc4[$j] = $t;
512
			}
513
			$last_enc_key = $key;
514
			$last_enc_key_c = $rc4;
515
		} else {
516
			$rc4 = $last_enc_key_c;
517
		}
518
		$len = strlen($text);
519
		$a = 0;
520
		$b = 0;
521
		$out = '';
522
		for ($i = 0; $i < $len; ++$i) {
523
			$a = ($a + 1) % 256;
524
			$t = $rc4[$a];
525
			$b = ($b + $t) % 256;
526
			$rc4[$a] = $rc4[$b];
527
			$rc4[$b] = $t;
528
			$k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
529
			$out .= chr(ord($text[$i]) ^ $k);
530
		}
531
		return $out;
532
	}
533
534
	/**
535
	 * Return the permission code used on encryption (P value).
536
	 * @param array $permissions the set of permissions (specify the ones you want to block).
537
	 * @param int $mode encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
538
	 * @since 5.0.005 (2010-05-12)
539
	 * @author Nicola Asuni
540
	 * @public static
541
	 */
542
	public static function getUserPermissionCode($permissions, $mode=0) {
543
		$options = array(
544
			'owner' => 2, // bit 2 -- inverted logic: cleared by default
545
			'print' => 4, // bit 3
546
			'modify' => 8, // bit 4
547
			'copy' => 16, // bit 5
548
			'annot-forms' => 32, // bit 6
549
			'fill-forms' => 256, // bit 9
550
			'extract' => 512, // bit 10
551
			'assemble' => 1024,// bit 11
552
			'print-high' => 2048 // bit 12
553
			);
554
		$protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
555
		foreach ($permissions as $permission) {
556
			if (isset($options[$permission])) {
557
				if (($mode > 0) OR ($options[$permission] <= 32)) {
558
					// set only valid permissions
559
					if ($options[$permission] == 2) {
560
						// the logic for bit 2 is inverted (cleared by default)
561
						$protection += $options[$permission];
562
					} else {
563
						$protection -= $options[$permission];
564
					}
565
				}
566
			}
567
		}
568
		return $protection;
569
	}
570
571
	/**
572
	 * Convert hexadecimal string to string
573
	 * @param string $bs byte-string to convert
574
	 * @return string
575
	 * @since 5.0.005 (2010-05-12)
576
	 * @author Nicola Asuni
577
	 * @public static
578
	 */
579
	public static function convertHexStringToString($bs) {
580
		$string = ''; // string to be returned
581
		$bslength = strlen($bs);
582
		if (($bslength % 2) != 0) {
583
			// padding
584
			$bs .= '0';
585
			++$bslength;
586
		}
587
		for ($i = 0; $i < $bslength; $i += 2) {
588
			$string .= chr(hexdec($bs[$i].$bs[($i + 1)]));
0 ignored issues
show
Bug introduced by
It seems like hexdec($bs[$i] . $bs[$i + 1]) can also be of type double; however, parameter $codepoint of chr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

588
			$string .= chr(/** @scrutinizer ignore-type */ hexdec($bs[$i].$bs[($i + 1)]));
Loading history...
589
		}
590
		return $string;
591
	}
592
593
	/**
594
	 * Convert string to hexadecimal string (byte string)
595
	 * @param string $s string to convert
596
	 * @return string byte string
597
	 * @since 5.0.010 (2010-05-17)
598
	 * @author Nicola Asuni
599
	 * @public static
600
	 */
601
	public static function convertStringToHexString($s) {
602
		$bs = '';
603
		$chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
604
		foreach ($chars as $c) {
605
			$bs .= sprintf('%02s', dechex(ord($c)));
606
		}
607
		return $bs;
608
	}
609
610
	/**
611
	 * Convert encryption P value to a string of bytes, low-order byte first.
612
	 * @param string $protection 32bit encryption permission value (P value)
613
	 * @return string
614
	 * @since 5.0.005 (2010-05-12)
615
	 * @author Nicola Asuni
616
	 * @public static
617
	 */
618
	public static function getEncPermissionsString($protection) {
619
		$binprot = sprintf('%032b', $protection);
620
		$str = chr(bindec(substr($binprot, 24, 8)));
0 ignored issues
show
Bug introduced by
It seems like bindec(substr($binprot, 24, 8)) can also be of type double; however, parameter $codepoint of chr() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

620
		$str = chr(/** @scrutinizer ignore-type */ bindec(substr($binprot, 24, 8)));
Loading history...
621
		$str .= chr(bindec(substr($binprot, 16, 8)));
622
		$str .= chr(bindec(substr($binprot, 8, 8)));
623
		$str .= chr(bindec(substr($binprot, 0, 8)));
624
		return $str;
625
	}
626
627
	/**
628
	 * Encode a name object.
629
	 * @param string $name Name object to encode.
630
	 * @return string Encoded name object.
631
	 * @author Nicola Asuni
632
	 * @since 5.9.097 (2011-06-23)
633
	 * @public static
634
	 */
635
	public static function encodeNameObject($name) {
636
		$escname = '';
637
		$length = strlen($name);
638
		for ($i = 0; $i < $length; ++$i) {
639
			$chr = $name[$i];
640
			if (preg_match('/[0-9a-zA-Z#_=-]/', $chr) == 1) {
641
				$escname .= $chr;
642
			} else {
643
				$escname .= sprintf('#%02X', ord($chr));
644
			}
645
		}
646
		return $escname;
647
	}
648
649
	/**
650
	 * Convert JavaScript form fields properties array to Annotation Properties array.
651
	 * @param array $prop javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
652
	 * @param array $spot_colors Reference to spot colors array.
653
	 * @param boolean $rtl True if in Right-To-Left text direction mode, false otherwise.
654
	 * @return array of annotation properties
655
	 * @author Nicola Asuni
656
	 * @since 4.8.000 (2009-09-06)
657
	 * @public static
658
	 */
659
	public static function getAnnotOptFromJSProp($prop, &$spot_colors, $rtl=false) {
660
		if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
661
			// the annotation options are already defined
662
			return $prop['aopt'];
663
		}
664
		$opt = array(); // value to be returned
665
		// alignment: Controls how the text is laid out within the text field.
666
		if (isset($prop['alignment'])) {
667
			switch ($prop['alignment']) {
668
				case 'left': {
669
					$opt['q'] = 0;
670
					break;
671
				}
672
				case 'center': {
673
					$opt['q'] = 1;
674
					break;
675
				}
676
				case 'right': {
677
					$opt['q'] = 2;
678
					break;
679
				}
680
				default: {
681
					$opt['q'] = ($rtl)?2:0;
682
					break;
683
				}
684
			}
685
		}
686
		// lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
687
		if (isset($prop['lineWidth'])) {
688
			$linewidth = intval($prop['lineWidth']);
689
		} else {
690
			$linewidth = 1;
691
		}
692
		// borderStyle: The border style for a field.
693
		if (isset($prop['borderStyle'])) {
694
			switch ($prop['borderStyle']) {
695
				case 'border.d':
696
				case 'dashed': {
697
					$opt['border'] = array(0, 0, $linewidth, array(3, 2));
698
					$opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
699
					break;
700
				}
701
				case 'border.b':
702
				case 'beveled': {
703
					$opt['border'] = array(0, 0, $linewidth);
704
					$opt['bs'] = array('w'=>$linewidth, 's'=>'B');
705
					break;
706
				}
707
				case 'border.i':
708
				case 'inset': {
709
					$opt['border'] = array(0, 0, $linewidth);
710
					$opt['bs'] = array('w'=>$linewidth, 's'=>'I');
711
					break;
712
				}
713
				case 'border.u':
714
				case 'underline': {
715
					$opt['border'] = array(0, 0, $linewidth);
716
					$opt['bs'] = array('w'=>$linewidth, 's'=>'U');
717
					break;
718
				}
719
				case 'border.s':
720
				case 'solid': {
721
					$opt['border'] = array(0, 0, $linewidth);
722
					$opt['bs'] = array('w'=>$linewidth, 's'=>'S');
723
					break;
724
				}
725
				default: {
726
					break;
727
				}
728
			}
729
		}
730
		if (isset($prop['border']) AND is_array($prop['border'])) {
731
			$opt['border'] = $prop['border'];
732
		}
733
		if (!isset($opt['mk'])) {
734
			$opt['mk'] = array();
735
		}
736
		if (!isset($opt['mk']['if'])) {
737
			$opt['mk']['if'] = array();
738
		}
739
		$opt['mk']['if']['a'] = array(0.5, 0.5);
740
		// buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
741
		if (isset($prop['buttonAlignX'])) {
742
			$opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
743
		}
744
		// buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
745
		if (isset($prop['buttonAlignY'])) {
746
			$opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
747
		}
748
		// buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
749
		if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
750
			$opt['mk']['if']['fb'] = true;
751
		}
752
		// buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
753
		if (isset($prop['buttonScaleHow'])) {
754
			switch ($prop['buttonScaleHow']) {
755
				case 'scaleHow.proportional': {
756
					$opt['mk']['if']['s'] = 'P';
757
					break;
758
				}
759
				case 'scaleHow.anamorphic': {
760
					$opt['mk']['if']['s'] = 'A';
761
					break;
762
				}
763
			}
764
		}
765
		// buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
766
		if (isset($prop['buttonScaleWhen'])) {
767
			switch ($prop['buttonScaleWhen']) {
768
				case 'scaleWhen.always': {
769
					$opt['mk']['if']['sw'] = 'A';
770
					break;
771
				}
772
				case 'scaleWhen.never': {
773
					$opt['mk']['if']['sw'] = 'N';
774
					break;
775
				}
776
				case 'scaleWhen.tooBig': {
777
					$opt['mk']['if']['sw'] = 'B';
778
					break;
779
				}
780
				case 'scaleWhen.tooSmall': {
781
					$opt['mk']['if']['sw'] = 'S';
782
					break;
783
				}
784
			}
785
		}
786
		// buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
787
		if (isset($prop['buttonPosition'])) {
788
			switch ($prop['buttonPosition']) {
789
				case 0:
790
				case 'position.textOnly': {
791
					$opt['mk']['tp'] = 0;
792
					break;
793
				}
794
				case 1:
795
				case 'position.iconOnly': {
796
					$opt['mk']['tp'] = 1;
797
					break;
798
				}
799
				case 2:
800
				case 'position.iconTextV': {
801
					$opt['mk']['tp'] = 2;
802
					break;
803
				}
804
				case 3:
805
				case 'position.textIconV': {
806
					$opt['mk']['tp'] = 3;
807
					break;
808
				}
809
				case 4:
810
				case 'position.iconTextH': {
811
					$opt['mk']['tp'] = 4;
812
					break;
813
				}
814
				case 5:
815
				case 'position.textIconH': {
816
					$opt['mk']['tp'] = 5;
817
					break;
818
				}
819
				case 6:
820
				case 'position.overlay': {
821
					$opt['mk']['tp'] = 6;
822
					break;
823
				}
824
			}
825
		}
826
		// fillColor: Specifies the background color for a field.
827
		if (isset($prop['fillColor'])) {
828
			if (is_array($prop['fillColor'])) {
829
				$opt['mk']['bg'] = $prop['fillColor'];
830
			} else {
831
				$opt['mk']['bg'] = TCPDF_COLORS::convertHTMLColorToDec($prop['fillColor'], $spot_colors);
832
			}
833
		}
834
		// 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.
835
		if (isset($prop['strokeColor'])) {
836
			if (is_array($prop['strokeColor'])) {
837
				$opt['mk']['bc'] = $prop['strokeColor'];
838
			} else {
839
				$opt['mk']['bc'] = TCPDF_COLORS::convertHTMLColorToDec($prop['strokeColor'], $spot_colors);
840
			}
841
		}
842
		// rotation: The rotation of a widget in counterclockwise increments.
843
		if (isset($prop['rotation'])) {
844
			$opt['mk']['r'] = $prop['rotation'];
845
		}
846
		// charLimit: Limits the number of characters that a user can type into a text field.
847
		if (isset($prop['charLimit'])) {
848
			$opt['maxlen'] = intval($prop['charLimit']);
849
		}
850
		$ff = 0;
851
		// readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
852
		if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
853
			$ff += 1 << 0;
854
		}
855
		// required: Specifies whether a field requires a value.
856
		if (isset($prop['required']) AND ($prop['required'] == 'true')) {
857
			$ff += 1 << 1;
858
		}
859
		// multiline: Controls how text is wrapped within the field.
860
		if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
861
			$ff += 1 << 12;
862
		}
863
		// password: Specifies whether the field should display asterisks when data is entered in the field.
864
		if (isset($prop['password']) AND ($prop['password'] == 'true')) {
865
			$ff += 1 << 13;
866
		}
867
		// NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
868
		if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
869
			$ff += 1 << 14;
870
		}
871
		// Radio: If set, the field is a set of radio buttons.
872
		if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
873
			$ff += 1 << 15;
874
		}
875
		// Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
876
		if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
877
			$ff += 1 << 16;
878
		}
879
		// Combo: If set, the field is a combo box; if clear, the field is a list box.
880
		if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
881
			$ff += 1 << 17;
882
		}
883
		// editable: Controls whether a combo box is editable.
884
		if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
885
			$ff += 1 << 18;
886
		}
887
		// Sort: If set, the field's option items shall be sorted alphabetically.
888
		if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
889
			$ff += 1 << 19;
890
		}
891
		// fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
892
		if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
893
			$ff += 1 << 20;
894
		}
895
		// multipleSelection: If true, indicates that a list box allows a multiple selection of items.
896
		if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
897
			$ff += 1 << 21;
898
		}
899
		// doNotSpellCheck: If true, spell checking is not performed on this editable text field.
900
		if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
901
			$ff += 1 << 22;
902
		}
903
		// doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
904
		if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
905
			$ff += 1 << 23;
906
		}
907
		// 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.
908
		if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
909
			$ff += 1 << 24;
910
		}
911
		// 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.
912
		if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
913
			$ff += 1 << 25;
914
		}
915
		// richText: If true, the field allows rich text formatting.
916
		if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
917
			$ff += 1 << 25;
918
		}
919
		// commitOnSelChange: Controls whether a field value is committed after a selection change.
920
		if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
921
			$ff += 1 << 26;
922
		}
923
		$opt['ff'] = $ff;
924
		// defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
925
		if (isset($prop['defaultValue'])) {
926
			$opt['dv'] = $prop['defaultValue'];
927
		}
928
		$f = 4; // default value for annotation flags
929
		// readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
930
		if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
931
			$f += 1 << 6;
932
		}
933
		// display: Controls whether the field is hidden or visible on screen and in print.
934
		if (isset($prop['display'])) {
935
			if ($prop['display'] == 'display.visible') {
936
				//
937
			} elseif ($prop['display'] == 'display.hidden') {
938
				$f += 1 << 1;
939
			} elseif ($prop['display'] == 'display.noPrint') {
940
				$f -= 1 << 2;
941
			} elseif ($prop['display'] == 'display.noView') {
942
				$f += 1 << 5;
943
			}
944
		}
945
		$opt['f'] = $f;
946
		// currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
947
		if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
948
			$opt['i'] = $prop['currentValueIndices'];
949
		}
950
		// value: The value of the field data that the user has entered.
951
		if (isset($prop['value'])) {
952
			if (is_array($prop['value'])) {
953
				$opt['opt'] = array();
954
				foreach ($prop['value'] AS $key => $optval) {
955
					// exportValues: An array of strings representing the export values for the field.
956
					if (isset($prop['exportValues'][$key])) {
957
						$opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
958
					} else {
959
						$opt['opt'][$key] = $prop['value'][$key];
960
					}
961
				}
962
			} else {
963
				$opt['v'] = $prop['value'];
964
			}
965
		}
966
		// richValue: This property specifies the text contents and formatting of a rich text field.
967
		if (isset($prop['richValue'])) {
968
			$opt['rv'] = $prop['richValue'];
969
		}
970
		// submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
971
		if (isset($prop['submitName'])) {
972
			$opt['tm'] = $prop['submitName'];
973
		}
974
		// name: Fully qualified field name.
975
		if (isset($prop['name'])) {
976
			$opt['t'] = $prop['name'];
977
		}
978
		// userName: The user name (short description string) of the field.
979
		if (isset($prop['userName'])) {
980
			$opt['tu'] = $prop['userName'];
981
		}
982
		// highlight: Defines how a button reacts when a user clicks it.
983
		if (isset($prop['highlight'])) {
984
			switch ($prop['highlight']) {
985
				case 'none':
986
				case 'highlight.n': {
987
					$opt['h'] = 'N';
988
					break;
989
				}
990
				case 'invert':
991
				case 'highlight.i': {
992
					$opt['h'] = 'i';
993
					break;
994
				}
995
				case 'push':
996
				case 'highlight.p': {
997
					$opt['h'] = 'P';
998
					break;
999
				}
1000
				case 'outline':
1001
				case 'highlight.o': {
1002
					$opt['h'] = 'O';
1003
					break;
1004
				}
1005
			}
1006
		}
1007
		// Unsupported options:
1008
		// - calcOrderIndex: Changes the calculation order of fields in the document.
1009
		// - delay: Delays the redrawing of a field's appearance.
1010
		// - defaultStyle: This property defines the default style attributes for the form field.
1011
		// - style: Allows the user to set the glyph style of a check box or radio button.
1012
		// - textColor, textFont, textSize
1013
		return $opt;
1014
	}
1015
1016
	/**
1017
	 * Format the page numbers.
1018
	 * This method can be overridden for custom formats.
1019
	 * @param int $num page number
1020
	 * @return string
1021
	 * @since 4.2.005 (2008-11-06)
1022
	 * @public static
1023
	 */
1024
	public static function formatPageNumber($num) {
1025
		return number_format((float)$num, 0, '', '.');
1026
	}
1027
1028
	/**
1029
	 * Format the page numbers on the Table Of Content.
1030
	 * This method can be overridden for custom formats.
1031
	 * @param int $num page number
1032
	 * @return string
1033
	 * @since 4.5.001 (2009-01-04)
1034
	 * @see addTOC(), addHTMLTOC()
1035
	 * @public static
1036
	 */
1037
	public static function formatTOCPageNumber($num) {
1038
		return number_format((float)$num, 0, '', '.');
1039
	}
1040
1041
	/**
1042
	 * Extracts the CSS properties from a CSS string.
1043
	 * @param string $cssdata string containing CSS definitions.
1044
	 * @return array An array where the keys are the CSS selectors and the values are the CSS properties.
1045
	 * @author Nicola Asuni
1046
	 * @since 5.1.000 (2010-05-25)
1047
	 * @public static
1048
	 */
1049
	public static function extractCSSproperties($cssdata) {
1050
		if (empty($cssdata)) {
1051
			return array();
1052
		}
1053
		// remove comments
1054
		$cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
1055
		// remove newlines and multiple spaces
1056
		$cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
1057
		// remove some spaces
1058
		$cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
1059
		// remove empty blocks
1060
		$cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
1061
		// replace media type parenthesis
1062
		$cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\1§', $cssdata);
1063
		$cssdata = preg_replace('/\}\}/si', '}§', $cssdata);
1064
		// trim string
1065
		$cssdata = trim($cssdata);
1066
		// find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
1067
		$cssblocks = array();
1068
		$matches = array();
1069
		if (preg_match_all('/@media[\s]+([^\§]*)§([^§]*)§/i', $cssdata, $matches) > 0) {
1070
			foreach ($matches[1] as $key => $type) {
1071
				$cssblocks[$type] = $matches[2][$key];
1072
			}
1073
			// remove media blocks
1074
			$cssdata = preg_replace('/@media[\s]+([^\§]*)§([^§]*)§/i', '', $cssdata);
1075
		}
1076
		// keep 'all' and 'print' media, other media types are discarded
1077
		if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
1078
			$cssdata .= $cssblocks['all'];
1079
		}
1080
		if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
1081
			$cssdata .= $cssblocks['print'];
1082
		}
1083
		// reset css blocks array
1084
		$cssblocks = array();
1085
		$matches = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $matches is dead and can be removed.
Loading history...
1086
		// explode css data string into array
1087
		if (substr($cssdata, -1) == '}') {
1088
			// remove last parethesis
1089
			$cssdata = substr($cssdata, 0, -1);
1090
		}
1091
		$matches = explode('}', $cssdata);
1092
		foreach ($matches as $key => $block) {
1093
			// index 0 contains the CSS selector, index 1 contains CSS properties
1094
			$cssblocks[$key] = explode('{', $block);
1095
			if (!isset($cssblocks[$key][1])) {
1096
				// remove empty definitions
1097
				unset($cssblocks[$key]);
1098
			}
1099
		}
1100
		// split groups of selectors (comma-separated list of selectors)
1101
		foreach ($cssblocks as $key => $block) {
1102
			if (strpos($block[0], ',') > 0) {
1103
				$selectors = explode(',', $block[0]);
1104
				foreach ($selectors as $sel) {
1105
					$cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
1106
				}
1107
				unset($cssblocks[$key]);
1108
			}
1109
		}
1110
		// covert array to selector => properties
1111
		$cssdata = array();
1112
		foreach ($cssblocks as $block) {
1113
			$selector = $block[0];
1114
			// calculate selector's specificity
1115
			$matches = array();
1116
			$a = 0; // the declaration is not from is a 'style' attribute
1117
			$b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
1118
			$c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
1119
			$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
1120
			$d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' '.$selector, $matches)); // number of element names
1121
			$d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
1122
			$specificity = $a.$b.$c.$d;
1123
			// add specificity to the beginning of the selector
1124
			$cssdata[$specificity.' '.$selector] = $block[1];
1125
		}
1126
		// sort selectors alphabetically to account for specificity
1127
		ksort($cssdata, SORT_STRING);
1128
		// return array
1129
		return $cssdata;
1130
	}
1131
1132
	/**
1133
	 * Cleanup HTML code (requires HTML Tidy library).
1134
	 * @param string $html htmlcode to fix
1135
	 * @param string $default_css CSS commands to add
1136
	 * @param array|null $tagvs parameters for setHtmlVSpace method
1137
	 * @param array|null $tidy_options options for tidy_parse_string function
1138
	 * @param array $tagvspaces Array of vertical spaces for tags.
1139
	 * @return string XHTML code cleaned up
1140
	 * @author Nicola Asuni
1141
	 * @since 5.9.017 (2010-11-16)
1142
	 * @see setHtmlVSpace()
1143
	 * @public static
1144
	 */
1145
	public static function fixHTMLCode($html, $default_css, $tagvs, $tidy_options, &$tagvspaces) {
1146
		// configure parameters for HTML Tidy
1147
		if (TCPDF_STATIC::empty_string($tidy_options)) {
0 ignored issues
show
Bug introduced by
It seems like $tidy_options can also be of type array; however, parameter $str of TCPDF_STATIC::empty_string() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1147
		if (TCPDF_STATIC::empty_string(/** @scrutinizer ignore-type */ $tidy_options)) {
Loading history...
1148
			$tidy_options = array (
1149
				'clean' => 1,
1150
				'drop-empty-paras' => 0,
1151
				'drop-proprietary-attributes' => 1,
1152
				'fix-backslash' => 1,
1153
				'hide-comments' => 1,
1154
				'join-styles' => 1,
1155
				'lower-literals' => 1,
1156
				'merge-divs' => 1,
1157
				'merge-spans' => 1,
1158
				'output-xhtml' => 1,
1159
				'word-2000' => 1,
1160
				'wrap' => 0,
1161
				'output-bom' => 0,
1162
				//'char-encoding' => 'utf8',
1163
				//'input-encoding' => 'utf8',
1164
				//'output-encoding' => 'utf8'
1165
			);
1166
		}
1167
		// clean up the HTML code
1168
		$tidy = tidy_parse_string($html, $tidy_options);
1169
		// fix the HTML
1170
		$tidy->cleanRepair();
1171
		// get the CSS part
1172
		$tidy_head = tidy_get_head($tidy);
1173
		$css = $tidy_head->value;
1174
		$css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
1175
		$css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
1176
		$css = str_replace('/*<![CDATA[*/', '', $css);
1177
		$css = str_replace('/*]]>*/', '', $css);
1178
		preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
1179
		if (isset($matches[1])) {
1180
			$css = strtolower($matches[1]);
1181
		} else {
1182
			$css = '';
1183
		}
1184
		// include default css
1185
		$css = '<style>'.$default_css.$css.'</style>';
1186
		// get the body part
1187
		$tidy_body = tidy_get_body($tidy);
1188
		$html = $tidy_body->value;
1189
		// fix some self-closing tags
1190
		$html = str_replace('<br>', '<br />', $html);
1191
		// remove some empty tag blocks
1192
		$html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
1193
		$html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
1194
		if (!TCPDF_STATIC::empty_string($tagvs)) {
1195
			// set vertical space for some XHTML tags
1196
			$tagvspaces = $tagvs;
1197
		}
1198
		// return the cleaned XHTML code + CSS
1199
		return $css.$html;
1200
	}
1201
1202
	/**
1203
	 * Returns true if the CSS selector is valid for the selected HTML tag
1204
	 * @param array $dom array of HTML tags and properties
1205
	 * @param int $key key of the current HTML tag
1206
	 * @param string $selector CSS selector string
1207
	 * @return true if the selector is valid, false otherwise
1208
	 * @since 5.1.000 (2010-05-25)
1209
	 * @public static
1210
	 */
1211
	public static function isValidCSSSelectorForTag($dom, $key, $selector) {
1212
		$valid = false; // value to be returned
1213
		$tag = $dom[$key]['value'];
1214
		$class = array();
1215
		if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
1216
			$class = explode(' ', strtolower($dom[$key]['attribute']['class']));
1217
		}
1218
		$id = '';
1219
		if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
1220
			$id = strtolower($dom[$key]['attribute']['id']);
1221
		}
1222
		$selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
1223
		$matches = array();
1224
		if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
1225
			$parentop = array_pop($matches[1]);
1226
			$operator = $parentop[0];
1227
			$offset = $parentop[1];
1228
			$lasttag = array_pop($matches[2]);
1229
			$lasttag = strtolower(trim($lasttag[0]));
1230
			if (($lasttag == '*') OR ($lasttag == $tag)) {
1231
				// the last element on selector is our tag or 'any tag'
1232
				$attrib = array_pop($matches[3]);
1233
				$attrib = strtolower(trim($attrib[0]));
1234
				if (!empty($attrib)) {
1235
					// check if matches class, id, attribute, pseudo-class or pseudo-element
1236
					switch ($attrib[0]) {
1237
						case '.': { // class
1238
							if (in_array(substr($attrib, 1), $class)) {
1239
								$valid = true;
1240
							}
1241
							break;
1242
						}
1243
						case '#': { // ID
1244
							if (substr($attrib, 1) == $id) {
1245
								$valid = true;
1246
							}
1247
							break;
1248
						}
1249
						case '[': { // attribute
1250
							$attrmatch = array();
1251
							if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
1252
								$att = strtolower($attrmatch[1]);
1253
								$val = $attrmatch[3];
1254
								if (isset($dom[$key]['attribute'][$att])) {
1255
									switch ($attrmatch[2]) {
1256
										case '=': {
1257
											if ($dom[$key]['attribute'][$att] == $val) {
1258
												$valid = true;
1259
											}
1260
											break;
1261
										}
1262
										case '~=': {
1263
											if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
1264
												$valid = true;
1265
											}
1266
											break;
1267
										}
1268
										case '^=': {
1269
											if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
1270
												$valid = true;
1271
											}
1272
											break;
1273
										}
1274
										case '$=': {
1275
											if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
1276
												$valid = true;
1277
											}
1278
											break;
1279
										}
1280
										case '*=': {
1281
											if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
1282
												$valid = true;
1283
											}
1284
											break;
1285
										}
1286
										case '|=': {
1287
											if ($dom[$key]['attribute'][$att] == $val) {
1288
												$valid = true;
1289
											} elseif (preg_match('/'.$val.'[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
1290
												$valid = true;
1291
											}
1292
											break;
1293
										}
1294
										default: {
1295
											$valid = true;
1296
										}
1297
									}
1298
								}
1299
							}
1300
							break;
1301
						}
1302
						case ':': { // pseudo-class or pseudo-element
1303
							if ($attrib[1] == ':') { // pseudo-element
1304
								// pseudo-elements are not supported!
1305
								// (::first-line, ::first-letter, ::before, ::after)
1306
							} else { // pseudo-class
1307
								// pseudo-classes are not supported!
1308
								// (: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)
1309
							}
1310
							break;
1311
						}
1312
					} // end of switch
1313
				} else {
1314
					$valid = true;
1315
				}
1316
				if ($valid AND ($offset > 0)) {
1317
					$valid = false;
1318
					// check remaining selector part
1319
					$selector = substr($selector, 0, $offset);
1320
					switch ($operator) {
1321
						case ' ': { // descendant of an element
1322
							while ($dom[$key]['parent'] > 0) {
1323
								if (self::isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
1324
									$valid = true;
1325
									break;
1326
								} else {
1327
									$key = $dom[$key]['parent'];
1328
								}
1329
							}
1330
							break;
1331
						}
1332
						case '>': { // child of an element
1333
							$valid = self::isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
1334
							break;
1335
						}
1336
						case '+': { // immediately preceded by an element
1337
							for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
1338
								if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
1339
									$valid = self::isValidCSSSelectorForTag($dom, $i, $selector);
1340
									break;
1341
								}
1342
							}
1343
							break;
1344
						}
1345
						case '~': { // preceded by an element
1346
							for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
1347
								if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
1348
									if (self::isValidCSSSelectorForTag($dom, $i, $selector)) {
1349
										break;
1350
									}
1351
								}
1352
							}
1353
							break;
1354
						}
1355
					}
1356
				}
1357
			}
1358
		}
1359
		return $valid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $valid returns the type boolean which is incompatible with the documented return type true.
Loading history...
1360
	}
1361
1362
	/**
1363
	 * Returns the styles array that apply for the selected HTML tag.
1364
	 * @param array $dom array of HTML tags and properties
1365
	 * @param int $key key of the current HTML tag
1366
	 * @param array $css array of CSS properties
1367
	 * @return array containing CSS properties
1368
	 * @since 5.1.000 (2010-05-25)
1369
	 * @public static
1370
	 */
1371
	public static function getCSSdataArray($dom, $key, $css) {
1372
		$cssarray = array(); // style to be returned
1373
		// get parent CSS selectors
1374
		$selectors = array();
1375
		if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
1376
			$selectors = $dom[($dom[$key]['parent'])]['csssel'];
1377
		}
1378
		// get all styles that apply
1379
		foreach($css as $selector => $style) {
1380
			$pos = strpos($selector, ' ');
1381
			// get specificity
1382
			$specificity = substr($selector, 0, $pos);
1383
			// remove specificity
1384
			$selector = substr($selector, $pos);
1385
			// check if this selector apply to current tag
1386
			if (self::isValidCSSSelectorForTag($dom, $key, $selector)) {
1387
				if (!in_array($selector, $selectors)) {
1388
					// add style if not already added on parent selector
1389
					$cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
1390
					$selectors[] = $selector;
1391
				}
1392
			}
1393
		}
1394
		if (isset($dom[$key]['attribute']['style'])) {
1395
			// attach inline style (latest properties have high priority)
1396
			$cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
1397
		}
1398
		// order the css array to account for specificity
1399
		$cssordered = array();
1400
		foreach ($cssarray as $key => $val) {
0 ignored issues
show
introduced by
$key is overwriting one of the parameters of this function.
Loading history...
1401
			$skey = sprintf('%04d', $key);
1402
			$cssordered[$val['s'].'_'.$skey] = $val;
1403
		}
1404
		// sort selectors alphabetically to account for specificity
1405
		ksort($cssordered, SORT_STRING);
1406
		return array($selectors, $cssordered);
1407
	}
1408
1409
	/**
1410
	 * Compact CSS data array into single string.
1411
	 * @param array $css array of CSS properties
1412
	 * @return string containing merged CSS properties
1413
	 * @since 5.9.070 (2011-04-19)
1414
	 * @public static
1415
	 */
1416
	public static function getTagStyleFromCSSarray($css) {
1417
		$tagstyle = ''; // value to be returned
1418
		foreach ($css as $style) {
1419
			// split single css commands
1420
			$csscmds = explode(';', $style['c']);
1421
			foreach ($csscmds as $cmd) {
1422
				if (!empty($cmd)) {
1423
					$pos = strpos($cmd, ':');
1424
					if ($pos !== false) {
1425
						$cmd = substr($cmd, 0, ($pos + 1));
1426
						if (strpos($tagstyle, $cmd) !== false) {
1427
							// remove duplicate commands (last commands have high priority)
1428
							$tagstyle = preg_replace('/'.$cmd.'[^;]+/i', '', $tagstyle);
1429
						}
1430
					}
1431
				}
1432
			}
1433
			$tagstyle .= ';'.$style['c'];
1434
		}
1435
		// remove multiple semicolons
1436
		$tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
1437
		return $tagstyle;
1438
	}
1439
1440
	/**
1441
	 * Returns the Roman representation of an integer number
1442
	 * @param int $number number to convert
1443
	 * @return string roman representation of the specified number
1444
	 * @since 4.4.004 (2008-12-10)
1445
	 * @public static
1446
	 */
1447
	public static function intToRoman($number) {
1448
		$roman = '';
1449
		if ($number >= 4000) {
1450
			// do not represent numbers above 4000 in Roman numerals
1451
			return strval($number);
1452
		}
1453
		while ($number >= 1000) {
1454
			$roman .= 'M';
1455
			$number -= 1000;
1456
		}
1457
		while ($number >= 900) {
1458
			$roman .= 'CM';
1459
			$number -= 900;
1460
		}
1461
		while ($number >= 500) {
1462
			$roman .= 'D';
1463
			$number -= 500;
1464
		}
1465
		while ($number >= 400) {
1466
			$roman .= 'CD';
1467
			$number -= 400;
1468
		}
1469
		while ($number >= 100) {
1470
			$roman .= 'C';
1471
			$number -= 100;
1472
		}
1473
		while ($number >= 90) {
1474
			$roman .= 'XC';
1475
			$number -= 90;
1476
		}
1477
		while ($number >= 50) {
1478
			$roman .= 'L';
1479
			$number -= 50;
1480
		}
1481
		while ($number >= 40) {
1482
			$roman .= 'XL';
1483
			$number -= 40;
1484
		}
1485
		while ($number >= 10) {
1486
			$roman .= 'X';
1487
			$number -= 10;
1488
		}
1489
		while ($number >= 9) {
1490
			$roman .= 'IX';
1491
			$number -= 9;
1492
		}
1493
		while ($number >= 5) {
1494
			$roman .= 'V';
1495
			$number -= 5;
1496
		}
1497
		while ($number >= 4) {
1498
			$roman .= 'IV';
1499
			$number -= 4;
1500
		}
1501
		while ($number >= 1) {
1502
			$roman .= 'I';
1503
			--$number;
1504
		}
1505
		return $roman;
1506
	}
1507
1508
	/**
1509
	 * Find position of last occurrence of a substring in a string
1510
	 * @param string $haystack The string to search in.
1511
	 * @param string $needle substring to search.
1512
	 * @param int $offset May be specified to begin searching an arbitrary number of characters into the string.
1513
	 * @return int|false Returns the position where the needle exists. Returns FALSE if the needle was not found.
1514
	 * @since 4.8.038 (2010-03-13)
1515
	 * @public static
1516
	 */
1517
	public static function revstrpos($haystack, $needle, $offset = 0) {
1518
		$length = strlen($haystack);
1519
		$offset = ($offset > 0)?($length - $offset):abs($offset);
1520
		$pos = strpos(strrev($haystack), strrev($needle), $offset);
0 ignored issues
show
Bug introduced by
It seems like $offset can also be of type double; however, parameter $offset of strpos() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1520
		$pos = strpos(strrev($haystack), strrev($needle), /** @scrutinizer ignore-type */ $offset);
Loading history...
1521
		return ($pos === false)?false:($length - $pos - strlen($needle));
1522
	}
1523
1524
	/**
1525
	 * Returns an array of hyphenation patterns.
1526
	 * @param string $file TEX file containing hypenation patterns. TEX patterns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
1527
	 * @return array of hyphenation patterns
1528
	 * @author Nicola Asuni
1529
	 * @since 4.9.012 (2010-04-12)
1530
	 * @public static
1531
	 */
1532
	public static function getHyphenPatternsFromTEX($file) {
1533
		// TEX patterns are available at:
1534
		// http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
1535
		$data = file_get_contents($file);
1536
		$patterns = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $patterns is dead and can be removed.
Loading history...
1537
		// remove comments
1538
		$data = preg_replace('/\%[^\n]*/', '', $data);
1539
		// extract the patterns part
1540
		preg_match('/\\\\patterns\{([^\}]*)\}/i', $data, $matches);
1541
		$data = trim(substr($matches[0], 10, -1));
1542
		// extract each pattern
1543
		$patterns_array = preg_split('/[\s]+/', $data);
1544
		// create new language array of patterns
1545
		$patterns = array();
1546
		foreach($patterns_array as $val) {
1547
			if (!TCPDF_STATIC::empty_string($val)) {
1548
				$val = trim($val);
1549
				$val = str_replace('\'', '\\\'', $val);
1550
				$key = preg_replace('/[0-9]+/', '', $val);
1551
				$patterns[$key] = $val;
1552
			}
1553
		}
1554
		return $patterns;
1555
	}
1556
1557
	/**
1558
	 * Get the Path-Painting Operators.
1559
	 * @param string $style Style of rendering. Possible values are:
1560
	 * <ul>
1561
	 *   <li>S or D: Stroke the path.</li>
1562
	 *   <li>s or d: Close and stroke the path.</li>
1563
	 *   <li>f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.</li>
1564
	 *   <li>f* or F*: Fill the path, using the even-odd rule to determine the region to fill.</li>
1565
	 *   <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>
1566
	 *   <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>
1567
	 *   <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>
1568
	 *   <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>
1569
	 *   <li>CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.</li>
1570
	 *   <li>CEO: Clipping mode using the nonzero winding number rule to determine which regions lie inside the clipping path</li>
1571
	 *   <li>n: End the path object without filling or stroking it.</li>
1572
	 * </ul>
1573
	 * @param string $default default style
1574
	 * @return string
1575
	 * @author Nicola Asuni
1576
	 * @since 5.0.000 (2010-04-30)
1577
	 * @public static
1578
	 */
1579
	public static function getPathPaintOperator($style, $default='S') {
1580
		$op = '';
1581
		switch($style) {
1582
			case 'S':
1583
			case 'D': {
1584
				$op = 'S';
1585
				break;
1586
			}
1587
			case 's':
1588
			case 'd': {
1589
				$op = 's';
1590
				break;
1591
			}
1592
			case 'f':
1593
			case 'F': {
1594
				$op = 'f';
1595
				break;
1596
			}
1597
			case 'f*':
1598
			case 'F*': {
1599
				$op = 'f*';
1600
				break;
1601
			}
1602
			case 'B':
1603
			case 'FD':
1604
			case 'DF': {
1605
				$op = 'B';
1606
				break;
1607
			}
1608
			case 'B*':
1609
			case 'F*D':
1610
			case 'DF*': {
1611
				$op = 'B*';
1612
				break;
1613
			}
1614
			case 'b':
1615
			case 'fd':
1616
			case 'df': {
1617
				$op = 'b';
1618
				break;
1619
			}
1620
			case 'b*':
1621
			case 'f*d':
1622
			case 'df*': {
1623
				$op = 'b*';
1624
				break;
1625
			}
1626
			case 'CNZ': {
1627
				$op = 'W n';
1628
				break;
1629
			}
1630
			case 'CEO': {
1631
				$op = 'W* n';
1632
				break;
1633
			}
1634
			case 'n': {
1635
				$op = 'n';
1636
				break;
1637
			}
1638
			default: {
1639
				if (!empty($default)) {
1640
					$op = self::getPathPaintOperator($default, '');
1641
				} else {
1642
					$op = '';
1643
				}
1644
			}
1645
		}
1646
		return $op;
1647
	}
1648
1649
	/**
1650
	 * Get the product of two SVG tranformation matrices
1651
	 * @param array $ta first SVG tranformation matrix
1652
	 * @param array $tb second SVG tranformation matrix
1653
	 * @return array transformation array
1654
	 * @author Nicola Asuni
1655
	 * @since 5.0.000 (2010-05-02)
1656
	 * @public static
1657
	 */
1658
	public static function getTransformationMatrixProduct($ta, $tb) {
1659
		$tm = array();
1660
		$tm[0] = ($ta[0] * $tb[0]) + ($ta[2] * $tb[1]);
1661
		$tm[1] = ($ta[1] * $tb[0]) + ($ta[3] * $tb[1]);
1662
		$tm[2] = ($ta[0] * $tb[2]) + ($ta[2] * $tb[3]);
1663
		$tm[3] = ($ta[1] * $tb[2]) + ($ta[3] * $tb[3]);
1664
		$tm[4] = ($ta[0] * $tb[4]) + ($ta[2] * $tb[5]) + $ta[4];
1665
		$tm[5] = ($ta[1] * $tb[4]) + ($ta[3] * $tb[5]) + $ta[5];
1666
		return $tm;
1667
	}
1668
1669
	/**
1670
	 * Get the tranformation matrix from SVG transform attribute
1671
	 * @param string $attribute transformation
1672
	 * @return array of transformations
1673
	 * @author Nicola Asuni
1674
	 * @since 5.0.000 (2010-05-02)
1675
	 * @public static
1676
	 */
1677
	public static function getSVGTransformMatrix($attribute) {
1678
		// identity matrix
1679
		$tm = array(1, 0, 0, 1, 0, 0);
1680
		$transform = array();
1681
		if (preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)[\s]*\(([^\)]+)\)/si', $attribute, $transform, PREG_SET_ORDER) > 0) {
1682
			foreach ($transform as $key => $data) {
1683
				if (!empty($data[2])) {
1684
					$a = 1;
1685
					$b = 0;
1686
					$c = 0;
1687
					$d = 1;
1688
					$e = 0;
1689
					$f = 0;
1690
					$regs = array();
1691
					switch ($data[1]) {
1692
						case 'matrix': {
1693
							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)) {
1694
								$a = $regs[1];
1695
								$b = $regs[2];
1696
								$c = $regs[3];
1697
								$d = $regs[4];
1698
								$e = $regs[5];
1699
								$f = $regs[6];
1700
							}
1701
							break;
1702
						}
1703
						case 'translate': {
1704
							if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
1705
								$e = $regs[1];
1706
								$f = $regs[2];
1707
							} elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
1708
								$e = $regs[1];
1709
							}
1710
							break;
1711
						}
1712
						case 'scale': {
1713
							if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
1714
								$a = $regs[1];
1715
								$d = $regs[2];
1716
							} elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
1717
								$a = $regs[1];
1718
								$d = $a;
1719
							}
1720
							break;
1721
						}
1722
						case 'rotate': {
1723
							if (preg_match('/([0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
1724
								$ang = deg2rad($regs[1]);
1725
								$x = $regs[2];
1726
								$y = $regs[3];
1727
								$a = cos($ang);
1728
								$b = sin($ang);
1729
								$c = -$b;
1730
								$d = $a;
1731
								$e = ($x * (1 - $a)) - ($y * $c);
1732
								$f = ($y * (1 - $d)) - ($x * $b);
1733
							} elseif (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
1734
								$ang = deg2rad($regs[1]);
1735
								$a = cos($ang);
1736
								$b = sin($ang);
1737
								$c = -$b;
1738
								$d = $a;
1739
								$e = 0;
1740
								$f = 0;
1741
							}
1742
							break;
1743
						}
1744
						case 'skewX': {
1745
							if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
1746
								$c = tan(deg2rad($regs[1]));
1747
							}
1748
							break;
1749
						}
1750
						case 'skewY': {
1751
							if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
1752
								$b = tan(deg2rad($regs[1]));
1753
							}
1754
							break;
1755
						}
1756
					}
1757
					$tm = self::getTransformationMatrixProduct($tm, array($a, $b, $c, $d, $e, $f));
1758
				}
1759
			}
1760
		}
1761
		return $tm;
1762
	}
1763
1764
	/**
1765
	 * Returns the angle in radiants between two vectors
1766
	 * @param int $x1 X coordinate of first vector point
1767
	 * @param int $y1 Y coordinate of first vector point
1768
	 * @param int $x2 X coordinate of second vector point
1769
	 * @param int $y2 Y coordinate of second vector point
1770
	 * @author Nicola Asuni
1771
	 * @since 5.0.000 (2010-05-04)
1772
	 * @public static
1773
	 */
1774
	public static function getVectorsAngle($x1, $y1, $x2, $y2) {
1775
		$dprod = ($x1 * $x2) + ($y1 * $y2);
1776
		$dist1 = sqrt(($x1 * $x1) + ($y1 * $y1));
1777
		$dist2 = sqrt(($x2 * $x2) + ($y2 * $y2));
1778
		$angle = acos($dprod / ($dist1 * $dist2));
1779
		if (is_nan($angle)) {
1780
			$angle = M_PI;
1781
		}
1782
		if ((($x1 * $y2) - ($x2 * $y1)) < 0) {
1783
			$angle *= -1;
1784
		}
1785
		return $angle;
1786
	}
1787
1788
	/**
1789
	 * Split string by a regular expression.
1790
	 * This is a wrapper for the preg_split function to avoid the bug: https://bugs.php.net/bug.php?id=45850
1791
	 * @param string $pattern The regular expression pattern to search for without the modifiers, as a string.
1792
	 * @param string $modifiers The modifiers part of the pattern,
1793
	 * @param string $subject The input string.
1794
	 * @param int $limit 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.
1795
	 * @param int $flags The flags as specified on the preg_split PHP function.
1796
	 * @return array Returns an array containing substrings of subject split along boundaries matched by pattern.modifier
1797
	 * @author Nicola Asuni
1798
	 * @since 6.0.023
1799
	 * @public static
1800
	 */
1801
	public static function pregSplit($pattern, $modifiers, $subject, $limit=NULL, $flags=NULL) {
1802
		// PHP 8.1 deprecates nulls for $limit and $flags
1803
		$limit = $limit === null ? -1 : $limit;
1804
		$flags = $flags === null ? 0 : $flags;
1805
		// the bug only happens on PHP 5.2 when using the u modifier
1806
		if ((strpos($modifiers, 'u') === FALSE) OR (count(preg_split('//u', "\n\t", -1, PREG_SPLIT_NO_EMPTY)) == 2)) {
1807
			$ret = preg_split($pattern.$modifiers, $subject, $limit, $flags);
1808
			if ($ret === false) {
1809
				return array();
1810
			}
1811
			return is_array($ret) ? $ret : array();
0 ignored issues
show
introduced by
The condition is_array($ret) is always true.
Loading history...
1812
		}
1813
		// preg_split is bugged - try alternative solution
1814
		$ret = array();
1815
		while (($nl = strpos($subject, "\n")) !== FALSE) {
1816
			$ret = array_merge($ret, preg_split($pattern.$modifiers, substr($subject, 0, $nl), $limit, $flags));
1817
			$ret[] = "\n";
1818
			$subject = substr($subject, ($nl + 1));
1819
		}
1820
		if (strlen($subject) > 0) {
1821
			$ret = array_merge($ret, preg_split($pattern.$modifiers, $subject, $limit, $flags));
1822
		}
1823
		return $ret;
1824
	}
1825
1826
	/**
1827
	 * Wrapper to use fopen only with local files
1828
	 * @param string $filename Name of the file to open
1829
	 * @param string $mode
1830
	 * @return resource|false Returns a file pointer resource on success, or FALSE on error.
1831
	 * @public static
1832
	 */
1833
	public static function fopenLocal($filename, $mode) {
1834
		if (strpos($filename, '://') === false) {
1835
			$filename = 'file://'.$filename;
1836
		} elseif (stream_is_local($filename) !== true) {
1837
			return false;
1838
		}
1839
		return fopen($filename, $mode);
1840
	}
1841
1842
	/**
1843
	 * Check if the URL exist.
1844
	 * @param string $url URL to check.
1845
	 * @return bool Returns TRUE if the URL exists; FALSE otherwise.
1846
	 * @public static
1847
	 * @since 6.2.25
1848
	 */
1849
	public static function url_exists($url) {
1850
		$crs = curl_init();
1851
        $curlopts = [];
1852
        if (
1853
            (ini_get('open_basedir') == '')
1854
            && (ini_get('safe_mode') === ''
1855
            || ini_get('safe_mode') === false)
1856
        ) {
1857
            $curlopts[CURLOPT_FOLLOWLOCATION] = true;
1858
        }
1859
        $curlopts = array_replace($curlopts, self::CURLOPT_DEFAULT);
1860
        $curlopts = array_replace($curlopts, K_CURLOPTS);
1861
        $curlopts = array_replace($curlopts, self::CURLOPT_FIXED);
1862
        $curlopts[CURLOPT_URL] = $url;
1863
        curl_setopt_array($crs, $curlopts);
1864
		curl_exec($crs);
1865
		$code = curl_getinfo($crs, CURLINFO_HTTP_CODE);
1866
		curl_close($crs);
1867
		return ($code == 200);
1868
	}
1869
1870
	/**
1871
	 * Encode query params in URL
1872
	 *
1873
	 * @param string $url
1874
	 * @return string
1875
	 * @since 6.3.3 (2019-11-01)
1876
	 * @public static
1877
	 */
1878
	public static function encodeUrlQuery($url) {
1879
		$urlData = parse_url($url);
1880
		if (isset($urlData['query']) && $urlData['query']) {
1881
			$urlQueryData = array();
1882
			parse_str(urldecode($urlData['query']), $urlQueryData);
1883
			$port = isset($urlData['port']) ? ':'.$urlData['port'] : '';
1884
			$updatedUrl = $urlData['scheme'].'://'.$urlData['host'].$port.$urlData['path'].'?'.http_build_query($urlQueryData);
1885
		} else {
1886
			$updatedUrl = $url;
1887
		}
1888
		return $updatedUrl;
1889
	}
1890
1891
	/**
1892
	 * Wrapper for file_exists.
1893
	 * Checks whether a file or directory exists.
1894
	 * Only allows some protocols and local files.
1895
	 * @param string $filename Path to the file or directory.
1896
	 * @return bool Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.
1897
	 * @public static
1898
	 */
1899
	public static function file_exists($filename) {
1900
		if (preg_match('|^https?://|', $filename) == 1) {
1901
			return self::url_exists($filename);
1902
		}
1903
		if (strpos($filename, '://')) {
1904
			return false; // only support http and https wrappers for security reasons
1905
		}
1906
		return @file_exists($filename);
1907
	}
1908
1909
	/**
1910
	 * Reads entire file into a string.
1911
	 * The file can be also an URL.
1912
	 * @param string $file Name of the file or URL to read.
1913
	 * @return string|false The function returns the read data or FALSE on failure.
1914
	 * @author Nicola Asuni
1915
	 * @since 6.0.025
1916
	 * @public static
1917
	 */
1918
	public static function fileGetContents($file) {
1919
		$alt = array($file);
1920
		//
1921
		if ((strlen($file) > 1)
1922
		    && ($file[0] === '/')
1923
		    && ($file[1] !== '/')
1924
		    && !empty($_SERVER['DOCUMENT_ROOT'])
1925
		    && ($_SERVER['DOCUMENT_ROOT'] !== '/')
1926
		) {
1927
		    $findroot = strpos($file, $_SERVER['DOCUMENT_ROOT']);
1928
		    if (($findroot === false) || ($findroot > 1)) {
1929
			$alt[] = htmlspecialchars_decode(urldecode($_SERVER['DOCUMENT_ROOT'].$file));
1930
		    }
1931
		}
1932
		//
1933
		$protocol = 'http';
1934
		if (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
1935
		    $protocol .= 's';
1936
		}
1937
		//
1938
		$url = $file;
1939
		if (preg_match('%^//%', $url) && !empty($_SERVER['HTTP_HOST'])) {
1940
			$url = $protocol.':'.str_replace(' ', '%20', $url);
1941
		}
1942
		$url = htmlspecialchars_decode($url);
1943
		$alt[] = $url;
1944
		//
1945
		if (preg_match('%^(https?)://%', $url)
1946
		    && empty($_SERVER['HTTP_HOST'])
1947
		    && empty($_SERVER['DOCUMENT_ROOT'])
1948
		) {
1949
			$urldata = parse_url($url);
1950
			if (empty($urldata['query'])) {
1951
				$host = $protocol.'://'.$_SERVER['HTTP_HOST'];
1952
				if (strpos($url, $host) === 0) {
1953
				    // convert URL to full server path
1954
				    $tmp = str_replace($host, $_SERVER['DOCUMENT_ROOT'], $url);
1955
				    $alt[] = htmlspecialchars_decode(urldecode($tmp));
1956
				}
1957
			}
1958
		}
1959
		//
1960
		if (isset($_SERVER['SCRIPT_URI'])
1961
		    && !preg_match('%^(https?|ftp)://%', $file)
1962
		    && !preg_match('%^//%', $file)
1963
		) {
1964
		    $urldata = @parse_url($_SERVER['SCRIPT_URI']);
1965
		    $alt[] = $urldata['scheme'].'://'.$urldata['host'].(($file[0] == '/') ? '' : '/').$file;
1966
		}
1967
		//
1968
		$alt = array_unique($alt);
1969
		foreach ($alt as $path) {
1970
			if (!self::file_exists($path)) {
1971
				continue;
1972
			}
1973
			$ret = @file_get_contents($path);
1974
			if ( $ret != false ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $ret of type false|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
1975
			    return $ret;
1976
			}
1977
			// try to use CURL for URLs
1978
			if (!ini_get('allow_url_fopen')
1979
				&& function_exists('curl_init')
1980
				&& preg_match('%^(https?|ftp)://%', $path)
1981
			) {
1982
				// try to get remote file data using cURL
1983
				$crs = curl_init();
1984
				$curlopts = [];
1985
				if (
1986
					(ini_get('open_basedir') == '')
1987
					&& (ini_get('safe_mode') === ''
1988
					|| ini_get('safe_mode') === false)
1989
				) {
1990
					$curlopts[CURLOPT_FOLLOWLOCATION] = true;
1991
				}
1992
				$curlopts = array_replace($curlopts, self::CURLOPT_DEFAULT);
1993
				$curlopts = array_replace($curlopts, K_CURLOPTS);
1994
				$curlopts = array_replace($curlopts, self::CURLOPT_FIXED);
1995
				$curlopts[CURLOPT_URL] = $url;
1996
				curl_setopt_array($crs, $curlopts);
1997
				$ret = curl_exec($crs);
1998
				curl_close($crs);
1999
				if ($ret !== false) {
2000
					return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret also could return the type true which is incompatible with the documented return type false|string.
Loading history...
2001
				}
2002
			}
2003
		}
2004
		return false;
2005
	}
2006
2007
	/**
2008
	 * Get ULONG from string (Big Endian 32-bit unsigned integer).
2009
	 * @param string $str string from where to extract value
2010
	 * @param int $offset point from where to read the data
2011
	 * @return int 32 bit value
2012
	 * @author Nicola Asuni
2013
	 * @since 5.2.000 (2010-06-02)
2014
	 * @public static
2015
	 */
2016
	public static function _getULONG($str, $offset) {
2017
		$v = unpack('Ni', substr($str, $offset, 4));
2018
		return $v['i'];
2019
	}
2020
2021
	/**
2022
	 * Get USHORT from string (Big Endian 16-bit unsigned integer).
2023
	 * @param string $str string from where to extract value
2024
	 * @param int $offset point from where to read the data
2025
	 * @return int 16 bit value
2026
	 * @author Nicola Asuni
2027
	 * @since 5.2.000 (2010-06-02)
2028
	 * @public static
2029
	 */
2030
	public static function _getUSHORT($str, $offset) {
2031
		$v = unpack('ni', substr($str, $offset, 2));
2032
		return $v['i'];
2033
	}
2034
2035
	/**
2036
	 * Get SHORT from string (Big Endian 16-bit signed integer).
2037
	 * @param string $str String from where to extract value.
2038
	 * @param int $offset Point from where to read the data.
2039
	 * @return int 16 bit value
2040
	 * @author Nicola Asuni
2041
	 * @since 5.2.000 (2010-06-02)
2042
	 * @public static
2043
	 */
2044
	public static function _getSHORT($str, $offset) {
2045
		$v = unpack('si', substr($str, $offset, 2));
2046
		return $v['i'];
2047
	}
2048
2049
	/**
2050
	 * Get FWORD from string (Big Endian 16-bit signed integer).
2051
	 * @param string $str String from where to extract value.
2052
	 * @param int $offset Point from where to read the data.
2053
	 * @return int 16 bit value
2054
	 * @author Nicola Asuni
2055
	 * @since 5.9.123 (2011-09-30)
2056
	 * @public static
2057
	 */
2058
	public static function _getFWORD($str, $offset) {
2059
		$v = self::_getUSHORT($str, $offset);
2060
		if ($v > 0x7fff) {
2061
			$v -= 0x10000;
2062
		}
2063
		return $v;
2064
	}
2065
2066
	/**
2067
	 * Get UFWORD from string (Big Endian 16-bit unsigned integer).
2068
	 * @param string $str string from where to extract value
2069
	 * @param int $offset point from where to read the data
2070
	 * @return int 16 bit value
2071
	 * @author Nicola Asuni
2072
	 * @since 5.9.123 (2011-09-30)
2073
	 * @public static
2074
	 */
2075
	public static function _getUFWORD($str, $offset) {
2076
		$v = self::_getUSHORT($str, $offset);
2077
		return $v;
2078
	}
2079
2080
	/**
2081
	 * Get FIXED from string (32-bit signed fixed-point number (16.16).
2082
	 * @param string $str string from where to extract value
2083
	 * @param int $offset point from where to read the data
2084
	 * @return int 16 bit value
2085
	 * @author Nicola Asuni
2086
	 * @since 5.9.123 (2011-09-30)
2087
	 * @public static
2088
	 */
2089
	public static function _getFIXED($str, $offset) {
2090
		// mantissa
2091
		$m = self::_getFWORD($str, $offset);
2092
		// fraction
2093
		$f = self::_getUSHORT($str, ($offset + 2));
2094
		$v = floatval(''.$m.'.'.$f.'');
2095
		return $v;
2096
	}
2097
2098
	/**
2099
	 * Get BYTE from string (8-bit unsigned integer).
2100
	 * @param string $str String from where to extract value.
2101
	 * @param int $offset Point from where to read the data.
2102
	 * @return int 8 bit value
2103
	 * @author Nicola Asuni
2104
	 * @since 5.2.000 (2010-06-02)
2105
	 * @public static
2106
	 */
2107
	public static function _getBYTE($str, $offset) {
2108
		$v = unpack('Ci', substr($str, $offset, 1));
2109
		return $v['i'];
2110
	}
2111
	/**
2112
	 * Binary-safe and URL-safe file read.
2113
	 * 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.
2114
	 * @param resource $handle
2115
	 * @param int $length
2116
	 * @return string|false Returns the read string or FALSE in case of error.
2117
	 * @author Nicola Asuni
2118
	 * @since 4.5.027 (2009-03-16)
2119
	 * @public static
2120
	 */
2121
	public static function rfread($handle, $length) {
2122
		$data = fread($handle, $length);
2123
		if ($data === false) {
2124
			return false;
2125
		}
2126
		$rest = ($length - strlen($data));
2127
		if (($rest > 0) && !feof($handle)) {
2128
			$data .= self::rfread($handle, $rest);
2129
		}
2130
		return $data;
2131
	}
2132
2133
	/**
2134
	 * Read a 4-byte (32 bit) integer from file.
2135
	 * @param resource $f file resource.
2136
	 * @return int 4-byte integer
2137
	 * @public static
2138
	 */
2139
	public static function _freadint($f) {
2140
		$a = unpack('Ni', fread($f, 4));
2141
		return $a['i'];
2142
	}
2143
2144
	/**
2145
	 * Array of page formats
2146
	 * measures are calculated in this way: (inches * 72) or (millimeters * 72 / 25.4)
2147
	 * @public static
2148
	 *
2149
     * @var array<string,float[]>
2150
	 */
2151
	public static $page_formats = array(
2152
		// ISO 216 A Series + 2 SIS 014711 extensions
2153
		'A0'                     => array( 2383.937,  3370.394), // = (  841 x 1189 ) mm  = ( 33.11 x 46.81 ) in
2154
		'A1'                     => array( 1683.780,  2383.937), // = (  594 x 841  ) mm  = ( 23.39 x 33.11 ) in
2155
		'A2'                     => array( 1190.551,  1683.780), // = (  420 x 594  ) mm  = ( 16.54 x 23.39 ) in
2156
		'A3'                     => array(  841.890,  1190.551), // = (  297 x 420  ) mm  = ( 11.69 x 16.54 ) in
2157
		'A4'                     => array(  595.276,   841.890), // = (  210 x 297  ) mm  = (  8.27 x 11.69 ) in
2158
		'A5'                     => array(  419.528,   595.276), // = (  148 x 210  ) mm  = (  5.83 x 8.27  ) in
2159
		'A6'                     => array(  297.638,   419.528), // = (  105 x 148  ) mm  = (  4.13 x 5.83  ) in
2160
		'A7'                     => array(  209.764,   297.638), // = (   74 x 105  ) mm  = (  2.91 x 4.13  ) in
2161
		'A8'                     => array(  147.402,   209.764), // = (   52 x 74   ) mm  = (  2.05 x 2.91  ) in
2162
		'A9'                     => array(  104.882,   147.402), // = (   37 x 52   ) mm  = (  1.46 x 2.05  ) in
2163
		'A10'                    => array(   73.701,   104.882), // = (   26 x 37   ) mm  = (  1.02 x 1.46  ) in
2164
		'A11'                    => array(   51.024,    73.701), // = (   18 x 26   ) mm  = (  0.71 x 1.02  ) in
2165
		'A12'                    => array(   36.850,    51.024), // = (   13 x 18   ) mm  = (  0.51 x 0.71  ) in
2166
		// ISO 216 B Series + 2 SIS 014711 extensions
2167
		'B0'                     => array( 2834.646,  4008.189), // = ( 1000 x 1414 ) mm  = ( 39.37 x 55.67 ) in
2168
		'B1'                     => array( 2004.094,  2834.646), // = (  707 x 1000 ) mm  = ( 27.83 x 39.37 ) in
2169
		'B2'                     => array( 1417.323,  2004.094), // = (  500 x 707  ) mm  = ( 19.69 x 27.83 ) in
2170
		'B3'                     => array( 1000.630,  1417.323), // = (  353 x 500  ) mm  = ( 13.90 x 19.69 ) in
2171
		'B4'                     => array(  708.661,  1000.630), // = (  250 x 353  ) mm  = (  9.84 x 13.90 ) in
2172
		'B5'                     => array(  498.898,   708.661), // = (  176 x 250  ) mm  = (  6.93 x 9.84  ) in
2173
		'B6'                     => array(  354.331,   498.898), // = (  125 x 176  ) mm  = (  4.92 x 6.93  ) in
2174
		'B7'                     => array(  249.449,   354.331), // = (   88 x 125  ) mm  = (  3.46 x 4.92  ) in
2175
		'B8'                     => array(  175.748,   249.449), // = (   62 x 88   ) mm  = (  2.44 x 3.46  ) in
2176
		'B9'                     => array(  124.724,   175.748), // = (   44 x 62   ) mm  = (  1.73 x 2.44  ) in
2177
		'B10'                    => array(   87.874,   124.724), // = (   31 x 44   ) mm  = (  1.22 x 1.73  ) in
2178
		'B11'                    => array(   62.362,    87.874), // = (   22 x 31   ) mm  = (  0.87 x 1.22  ) in
2179
		'B12'                    => array(   42.520,    62.362), // = (   15 x 22   ) mm  = (  0.59 x 0.87  ) in
2180
		// ISO 216 C Series + 2 SIS 014711 extensions + 5 EXTENSION
2181
		'C0'                     => array( 2599.370,  3676.535), // = (  917 x 1297 ) mm  = ( 36.10 x 51.06 ) in
2182
		'C1'                     => array( 1836.850,  2599.370), // = (  648 x 917  ) mm  = ( 25.51 x 36.10 ) in
2183
		'C2'                     => array( 1298.268,  1836.850), // = (  458 x 648  ) mm  = ( 18.03 x 25.51 ) in
2184
		'C3'                     => array(  918.425,  1298.268), // = (  324 x 458  ) mm  = ( 12.76 x 18.03 ) in
2185
		'C4'                     => array(  649.134,   918.425), // = (  229 x 324  ) mm  = (  9.02 x 12.76 ) in
2186
		'C5'                     => array(  459.213,   649.134), // = (  162 x 229  ) mm  = (  6.38 x 9.02  ) in
2187
		'C6'                     => array(  323.150,   459.213), // = (  114 x 162  ) mm  = (  4.49 x 6.38  ) in
2188
		'C7'                     => array(  229.606,   323.150), // = (   81 x 114  ) mm  = (  3.19 x 4.49  ) in
2189
		'C8'                     => array(  161.575,   229.606), // = (   57 x 81   ) mm  = (  2.24 x 3.19  ) in
2190
		'C9'                     => array(  113.386,   161.575), // = (   40 x 57   ) mm  = (  1.57 x 2.24  ) in
2191
		'C10'                    => array(   79.370,   113.386), // = (   28 x 40   ) mm  = (  1.10 x 1.57  ) in
2192
		'C11'                    => array(   56.693,    79.370), // = (   20 x 28   ) mm  = (  0.79 x 1.10  ) in
2193
		'C12'                    => array(   39.685,    56.693), // = (   14 x 20   ) mm  = (  0.55 x 0.79  ) in
2194
		'C76'                    => array(  229.606,   459.213), // = (   81 x 162  ) mm  = (  3.19 x 6.38  ) in
2195
		'DL'                     => array(  311.811,   623.622), // = (  110 x 220  ) mm  = (  4.33 x 8.66  ) in
2196
		'DLE'                    => array(  323.150,   637.795), // = (  114 x 225  ) mm  = (  4.49 x 8.86  ) in
2197
		'DLX'                    => array(  340.158,   666.142), // = (  120 x 235  ) mm  = (  4.72 x 9.25  ) in
2198
		'DLP'                    => array(  280.630,   595.276), // = (   99 x 210  ) mm  = (  3.90 x 8.27  ) in (1/3 A4)
2199
		// SIS 014711 E Series
2200
		'E0'                     => array( 2491.654,  3517.795), // = (  879 x 1241 ) mm  = ( 34.61 x 48.86 ) in
2201
		'E1'                     => array( 1757.480,  2491.654), // = (  620 x 879  ) mm  = ( 24.41 x 34.61 ) in
2202
		'E2'                     => array( 1247.244,  1757.480), // = (  440 x 620  ) mm  = ( 17.32 x 24.41 ) in
2203
		'E3'                     => array(  878.740,  1247.244), // = (  310 x 440  ) mm  = ( 12.20 x 17.32 ) in
2204
		'E4'                     => array(  623.622,   878.740), // = (  220 x 310  ) mm  = (  8.66 x 12.20 ) in
2205
		'E5'                     => array(  439.370,   623.622), // = (  155 x 220  ) mm  = (  6.10 x 8.66  ) in
2206
		'E6'                     => array(  311.811,   439.370), // = (  110 x 155  ) mm  = (  4.33 x 6.10  ) in
2207
		'E7'                     => array(  221.102,   311.811), // = (   78 x 110  ) mm  = (  3.07 x 4.33  ) in
2208
		'E8'                     => array(  155.906,   221.102), // = (   55 x 78   ) mm  = (  2.17 x 3.07  ) in
2209
		'E9'                     => array(  110.551,   155.906), // = (   39 x 55   ) mm  = (  1.54 x 2.17  ) in
2210
		'E10'                    => array(   76.535,   110.551), // = (   27 x 39   ) mm  = (  1.06 x 1.54  ) in
2211
		'E11'                    => array(   53.858,    76.535), // = (   19 x 27   ) mm  = (  0.75 x 1.06  ) in
2212
		'E12'                    => array(   36.850,    53.858), // = (   13 x 19   ) mm  = (  0.51 x 0.75  ) in
2213
		// SIS 014711 G Series
2214
		'G0'                     => array( 2715.591,  3838.110), // = (  958 x 1354 ) mm  = ( 37.72 x 53.31 ) in
2215
		'G1'                     => array( 1919.055,  2715.591), // = (  677 x 958  ) mm  = ( 26.65 x 37.72 ) in
2216
		'G2'                     => array( 1357.795,  1919.055), // = (  479 x 677  ) mm  = ( 18.86 x 26.65 ) in
2217
		'G3'                     => array(  958.110,  1357.795), // = (  338 x 479  ) mm  = ( 13.31 x 18.86 ) in
2218
		'G4'                     => array(  677.480,   958.110), // = (  239 x 338  ) mm  = (  9.41 x 13.31 ) in
2219
		'G5'                     => array(  479.055,   677.480), // = (  169 x 239  ) mm  = (  6.65 x 9.41  ) in
2220
		'G6'                     => array(  337.323,   479.055), // = (  119 x 169  ) mm  = (  4.69 x 6.65  ) in
2221
		'G7'                     => array(  238.110,   337.323), // = (   84 x 119  ) mm  = (  3.31 x 4.69  ) in
2222
		'G8'                     => array(  167.244,   238.110), // = (   59 x 84   ) mm  = (  2.32 x 3.31  ) in
2223
		'G9'                     => array(  119.055,   167.244), // = (   42 x 59   ) mm  = (  1.65 x 2.32  ) in
2224
		'G10'                    => array(   82.205,   119.055), // = (   29 x 42   ) mm  = (  1.14 x 1.65  ) in
2225
		'G11'                    => array(   59.528,    82.205), // = (   21 x 29   ) mm  = (  0.83 x 1.14  ) in
2226
		'G12'                    => array(   39.685,    59.528), // = (   14 x 21   ) mm  = (  0.55 x 0.83  ) in
2227
		// ISO Press
2228
		'RA0'                    => array( 2437.795,  3458.268), // = (  860 x 1220 ) mm  = ( 33.86 x 48.03 ) in
2229
		'RA1'                    => array( 1729.134,  2437.795), // = (  610 x 860  ) mm  = ( 24.02 x 33.86 ) in
2230
		'RA2'                    => array( 1218.898,  1729.134), // = (  430 x 610  ) mm  = ( 16.93 x 24.02 ) in
2231
		'RA3'                    => array(  864.567,  1218.898), // = (  305 x 430  ) mm  = ( 12.01 x 16.93 ) in
2232
		'RA4'                    => array(  609.449,   864.567), // = (  215 x 305  ) mm  = (  8.46 x 12.01 ) in
2233
		'SRA0'                   => array( 2551.181,  3628.346), // = (  900 x 1280 ) mm  = ( 35.43 x 50.39 ) in
2234
		'SRA1'                   => array( 1814.173,  2551.181), // = (  640 x 900  ) mm  = ( 25.20 x 35.43 ) in
2235
		'SRA2'                   => array( 1275.591,  1814.173), // = (  450 x 640  ) mm  = ( 17.72 x 25.20 ) in
2236
		'SRA3'                   => array(  907.087,  1275.591), // = (  320 x 450  ) mm  = ( 12.60 x 17.72 ) in
2237
		'SRA4'                   => array(  637.795,   907.087), // = (  225 x 320  ) mm  = (  8.86 x 12.60 ) in
2238
		// German DIN 476
2239
		'4A0'                    => array( 4767.874,  6740.787), // = ( 1682 x 2378 ) mm  = ( 66.22 x 93.62 ) in
2240
		'2A0'                    => array( 3370.394,  4767.874), // = ( 1189 x 1682 ) mm  = ( 46.81 x 66.22 ) in
2241
		// Variations on the ISO Standard
2242
		'A2_EXTRA'               => array( 1261.417,  1754.646), // = (  445 x 619  ) mm  = ( 17.52 x 24.37 ) in
2243
		'A3+'                    => array(  932.598,  1369.134), // = (  329 x 483  ) mm  = ( 12.95 x 19.02 ) in
2244
		'A3_EXTRA'               => array(  912.756,  1261.417), // = (  322 x 445  ) mm  = ( 12.68 x 17.52 ) in
2245
		'A3_SUPER'               => array(  864.567,  1440.000), // = (  305 x 508  ) mm  = ( 12.01 x 20.00 ) in
2246
		'SUPER_A3'               => array(  864.567,  1380.472), // = (  305 x 487  ) mm  = ( 12.01 x 19.17 ) in
2247
		'A4_EXTRA'               => array(  666.142,   912.756), // = (  235 x 322  ) mm  = (  9.25 x 12.68 ) in
2248
		'A4_SUPER'               => array(  649.134,   912.756), // = (  229 x 322  ) mm  = (  9.02 x 12.68 ) in
2249
		'SUPER_A4'               => array(  643.465,  1009.134), // = (  227 x 356  ) mm  = (  8.94 x 14.02 ) in
2250
		'A4_LONG'                => array(  595.276,   986.457), // = (  210 x 348  ) mm  = (  8.27 x 13.70 ) in
2251
		'F4'                     => array(  595.276,   935.433), // = (  210 x 330  ) mm  = (  8.27 x 12.99 ) in
2252
		'SO_B5_EXTRA'            => array(  572.598,   782.362), // = (  202 x 276  ) mm  = (  7.95 x 10.87 ) in
2253
		'A5_EXTRA'               => array(  490.394,   666.142), // = (  173 x 235  ) mm  = (  6.81 x 9.25  ) in
2254
		// ANSI Series
2255
		'ANSI_E'                 => array( 2448.000,  3168.000), // = (  864 x 1118 ) mm  = ( 34.00 x 44.00 ) in
2256
		'ANSI_D'                 => array( 1584.000,  2448.000), // = (  559 x 864  ) mm  = ( 22.00 x 34.00 ) in
2257
		'ANSI_C'                 => array( 1224.000,  1584.000), // = (  432 x 559  ) mm  = ( 17.00 x 22.00 ) in
2258
		'ANSI_B'                 => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
2259
		'ANSI_A'                 => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
2260
		// Traditional 'Loose' North American Paper Sizes
2261
		'USLEDGER'               => array( 1224.000,   792.000), // = (  432 x 279  ) mm  = ( 17.00 x 11.00 ) in
2262
		'LEDGER'                 => array( 1224.000,   792.000), // = (  432 x 279  ) mm  = ( 17.00 x 11.00 ) in
2263
		'ORGANIZERK'             => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
2264
		'BIBLE'                  => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
2265
		'USTABLOID'              => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
2266
		'TABLOID'                => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
2267
		'ORGANIZERM'             => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
2268
		'USLETTER'               => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
2269
		'LETTER'                 => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
2270
		'USLEGAL'                => array(  612.000,  1008.000), // = (  216 x 356  ) mm  = (  8.50 x 14.00 ) in
2271
		'LEGAL'                  => array(  612.000,  1008.000), // = (  216 x 356  ) mm  = (  8.50 x 14.00 ) in
2272
		'GOVERNMENTLETTER'       => array(  576.000,   756.000), // = (  203 x 267  ) mm  = (  8.00 x 10.50 ) in
2273
		'GLETTER'                => array(  576.000,   756.000), // = (  203 x 267  ) mm  = (  8.00 x 10.50 ) in
2274
		'JUNIORLEGAL'            => array(  576.000,   360.000), // = (  203 x 127  ) mm  = (  8.00 x 5.00  ) in
2275
		'JLEGAL'                 => array(  576.000,   360.000), // = (  203 x 127  ) mm  = (  8.00 x 5.00  ) in
2276
		// Other North American Paper Sizes
2277
		'QUADDEMY'               => array( 2520.000,  3240.000), // = (  889 x 1143 ) mm  = ( 35.00 x 45.00 ) in
2278
		'SUPER_B'                => array(  936.000,  1368.000), // = (  330 x 483  ) mm  = ( 13.00 x 19.00 ) in
2279
		'QUARTO'                 => array(  648.000,   792.000), // = (  229 x 279  ) mm  = (  9.00 x 11.00 ) in
2280
		'GOVERNMENTLEGAL'        => array(  612.000,   936.000), // = (  216 x 330  ) mm  = (  8.50 x 13.00 ) in
2281
		'FOLIO'                  => array(  612.000,   936.000), // = (  216 x 330  ) mm  = (  8.50 x 13.00 ) in
2282
		'MONARCH'                => array(  522.000,   756.000), // = (  184 x 267  ) mm  = (  7.25 x 10.50 ) in
2283
		'EXECUTIVE'              => array(  522.000,   756.000), // = (  184 x 267  ) mm  = (  7.25 x 10.50 ) in
2284
		'ORGANIZERL'             => array(  396.000,   612.000), // = (  140 x 216  ) mm  = (  5.50 x 8.50  ) in
2285
		'STATEMENT'              => array(  396.000,   612.000), // = (  140 x 216  ) mm  = (  5.50 x 8.50  ) in
2286
		'MEMO'                   => array(  396.000,   612.000), // = (  140 x 216  ) mm  = (  5.50 x 8.50  ) in
2287
		'FOOLSCAP'               => array(  595.440,   936.000), // = (  210 x 330  ) mm  = (  8.27 x 13.00 ) in
2288
		'COMPACT'                => array(  306.000,   486.000), // = (  108 x 171  ) mm  = (  4.25 x 6.75  ) in
2289
		'ORGANIZERJ'             => array(  198.000,   360.000), // = (   70 x 127  ) mm  = (  2.75 x 5.00  ) in
2290
		// Canadian standard CAN 2-9.60M
2291
		'P1'                     => array( 1587.402,  2437.795), // = (  560 x 860  ) mm  = ( 22.05 x 33.86 ) in
2292
		'P2'                     => array( 1218.898,  1587.402), // = (  430 x 560  ) mm  = ( 16.93 x 22.05 ) in
2293
		'P3'                     => array(  793.701,  1218.898), // = (  280 x 430  ) mm  = ( 11.02 x 16.93 ) in
2294
		'P4'                     => array(  609.449,   793.701), // = (  215 x 280  ) mm  = (  8.46 x 11.02 ) in
2295
		'P5'                     => array(  396.850,   609.449), // = (  140 x 215  ) mm  = (  5.51 x 8.46  ) in
2296
		'P6'                     => array(  303.307,   396.850), // = (  107 x 140  ) mm  = (  4.21 x 5.51  ) in
2297
		// North American Architectural Sizes
2298
		'ARCH_E'                 => array( 2592.000,  3456.000), // = (  914 x 1219 ) mm  = ( 36.00 x 48.00 ) in
2299
		'ARCH_E1'                => array( 2160.000,  3024.000), // = (  762 x 1067 ) mm  = ( 30.00 x 42.00 ) in
2300
		'ARCH_D'                 => array( 1728.000,  2592.000), // = (  610 x 914  ) mm  = ( 24.00 x 36.00 ) in
2301
		'BROADSHEET'             => array( 1296.000,  1728.000), // = (  457 x 610  ) mm  = ( 18.00 x 24.00 ) in
2302
		'ARCH_C'                 => array( 1296.000,  1728.000), // = (  457 x 610  ) mm  = ( 18.00 x 24.00 ) in
2303
		'ARCH_B'                 => array(  864.000,  1296.000), // = (  305 x 457  ) mm  = ( 12.00 x 18.00 ) in
2304
		'ARCH_A'                 => array(  648.000,   864.000), // = (  229 x 305  ) mm  = (  9.00 x 12.00 ) in
2305
		// -- North American Envelope Sizes
2306
		// - Announcement Envelopes
2307
		'ANNENV_A2'              => array(  314.640,   414.000), // = (  111 x 146  ) mm  = (  4.37 x 5.75  ) in
2308
		'ANNENV_A6'              => array(  342.000,   468.000), // = (  121 x 165  ) mm  = (  4.75 x 6.50  ) in
2309
		'ANNENV_A7'              => array(  378.000,   522.000), // = (  133 x 184  ) mm  = (  5.25 x 7.25  ) in
2310
		'ANNENV_A8'              => array(  396.000,   584.640), // = (  140 x 206  ) mm  = (  5.50 x 8.12  ) in
2311
		'ANNENV_A10'             => array(  450.000,   692.640), // = (  159 x 244  ) mm  = (  6.25 x 9.62  ) in
2312
		'ANNENV_SLIM'            => array(  278.640,   638.640), // = (   98 x 225  ) mm  = (  3.87 x 8.87  ) in
2313
		// - Commercial Envelopes
2314
		'COMMENV_N6_1/4'         => array(  252.000,   432.000), // = (   89 x 152  ) mm  = (  3.50 x 6.00  ) in
2315
		'COMMENV_N6_3/4'         => array(  260.640,   468.000), // = (   92 x 165  ) mm  = (  3.62 x 6.50  ) in
2316
		'COMMENV_N8'             => array(  278.640,   540.000), // = (   98 x 191  ) mm  = (  3.87 x 7.50  ) in
2317
		'COMMENV_N9'             => array(  278.640,   638.640), // = (   98 x 225  ) mm  = (  3.87 x 8.87  ) in
2318
		'COMMENV_N10'            => array(  296.640,   684.000), // = (  105 x 241  ) mm  = (  4.12 x 9.50  ) in
2319
		'COMMENV_N11'            => array(  324.000,   746.640), // = (  114 x 263  ) mm  = (  4.50 x 10.37 ) in
2320
		'COMMENV_N12'            => array(  342.000,   792.000), // = (  121 x 279  ) mm  = (  4.75 x 11.00 ) in
2321
		'COMMENV_N14'            => array(  360.000,   828.000), // = (  127 x 292  ) mm  = (  5.00 x 11.50 ) in
2322
		// - Catalogue Envelopes
2323
		'CATENV_N1'              => array(  432.000,   648.000), // = (  152 x 229  ) mm  = (  6.00 x 9.00  ) in
2324
		'CATENV_N1_3/4'          => array(  468.000,   684.000), // = (  165 x 241  ) mm  = (  6.50 x 9.50  ) in
2325
		'CATENV_N2'              => array(  468.000,   720.000), // = (  165 x 254  ) mm  = (  6.50 x 10.00 ) in
2326
		'CATENV_N3'              => array(  504.000,   720.000), // = (  178 x 254  ) mm  = (  7.00 x 10.00 ) in
2327
		'CATENV_N6'              => array(  540.000,   756.000), // = (  191 x 267  ) mm  = (  7.50 x 10.50 ) in
2328
		'CATENV_N7'              => array(  576.000,   792.000), // = (  203 x 279  ) mm  = (  8.00 x 11.00 ) in
2329
		'CATENV_N8'              => array(  594.000,   810.000), // = (  210 x 286  ) mm  = (  8.25 x 11.25 ) in
2330
		'CATENV_N9_1/2'          => array(  612.000,   756.000), // = (  216 x 267  ) mm  = (  8.50 x 10.50 ) in
2331
		'CATENV_N9_3/4'          => array(  630.000,   810.000), // = (  222 x 286  ) mm  = (  8.75 x 11.25 ) in
2332
		'CATENV_N10_1/2'         => array(  648.000,   864.000), // = (  229 x 305  ) mm  = (  9.00 x 12.00 ) in
2333
		'CATENV_N12_1/2'         => array(  684.000,   900.000), // = (  241 x 318  ) mm  = (  9.50 x 12.50 ) in
2334
		'CATENV_N13_1/2'         => array(  720.000,   936.000), // = (  254 x 330  ) mm  = ( 10.00 x 13.00 ) in
2335
		'CATENV_N14_1/4'         => array(  810.000,   882.000), // = (  286 x 311  ) mm  = ( 11.25 x 12.25 ) in
2336
		'CATENV_N14_1/2'         => array(  828.000,  1044.000), // = (  292 x 368  ) mm  = ( 11.50 x 14.50 ) in
2337
		// Japanese (JIS P 0138-61) Standard B-Series
2338
		'JIS_B0'                 => array( 2919.685,  4127.244), // = ( 1030 x 1456 ) mm  = ( 40.55 x 57.32 ) in
2339
		'JIS_B1'                 => array( 2063.622,  2919.685), // = (  728 x 1030 ) mm  = ( 28.66 x 40.55 ) in
2340
		'JIS_B2'                 => array( 1459.843,  2063.622), // = (  515 x 728  ) mm  = ( 20.28 x 28.66 ) in
2341
		'JIS_B3'                 => array( 1031.811,  1459.843), // = (  364 x 515  ) mm  = ( 14.33 x 20.28 ) in
2342
		'JIS_B4'                 => array(  728.504,  1031.811), // = (  257 x 364  ) mm  = ( 10.12 x 14.33 ) in
2343
		'JIS_B5'                 => array(  515.906,   728.504), // = (  182 x 257  ) mm  = (  7.17 x 10.12 ) in
2344
		'JIS_B6'                 => array(  362.835,   515.906), // = (  128 x 182  ) mm  = (  5.04 x 7.17  ) in
2345
		'JIS_B7'                 => array(  257.953,   362.835), // = (   91 x 128  ) mm  = (  3.58 x 5.04  ) in
2346
		'JIS_B8'                 => array(  181.417,   257.953), // = (   64 x 91   ) mm  = (  2.52 x 3.58  ) in
2347
		'JIS_B9'                 => array(  127.559,   181.417), // = (   45 x 64   ) mm  = (  1.77 x 2.52  ) in
2348
		'JIS_B10'                => array(   90.709,   127.559), // = (   32 x 45   ) mm  = (  1.26 x 1.77  ) in
2349
		'JIS_B11'                => array(   62.362,    90.709), // = (   22 x 32   ) mm  = (  0.87 x 1.26  ) in
2350
		'JIS_B12'                => array(   45.354,    62.362), // = (   16 x 22   ) mm  = (  0.63 x 0.87  ) in
2351
		// PA Series
2352
		'PA0'                    => array( 2381.102,  3174.803), // = (  840 x 1120 ) mm  = ( 33.07 x 44.09 ) in
2353
		'PA1'                    => array( 1587.402,  2381.102), // = (  560 x 840  ) mm  = ( 22.05 x 33.07 ) in
2354
		'PA2'                    => array( 1190.551,  1587.402), // = (  420 x 560  ) mm  = ( 16.54 x 22.05 ) in
2355
		'PA3'                    => array(  793.701,  1190.551), // = (  280 x 420  ) mm  = ( 11.02 x 16.54 ) in
2356
		'PA4'                    => array(  595.276,   793.701), // = (  210 x 280  ) mm  = (  8.27 x 11.02 ) in
2357
		'PA5'                    => array(  396.850,   595.276), // = (  140 x 210  ) mm  = (  5.51 x 8.27  ) in
2358
		'PA6'                    => array(  297.638,   396.850), // = (  105 x 140  ) mm  = (  4.13 x 5.51  ) in
2359
		'PA7'                    => array(  198.425,   297.638), // = (   70 x 105  ) mm  = (  2.76 x 4.13  ) in
2360
		'PA8'                    => array(  147.402,   198.425), // = (   52 x 70   ) mm  = (  2.05 x 2.76  ) in
2361
		'PA9'                    => array(   99.213,   147.402), // = (   35 x 52   ) mm  = (  1.38 x 2.05  ) in
2362
		'PA10'                   => array(   73.701,    99.213), // = (   26 x 35   ) mm  = (  1.02 x 1.38  ) in
2363
		// Standard Photographic Print Sizes
2364
		'PASSPORT_PHOTO'         => array(   99.213,   127.559), // = (   35 x 45   ) mm  = (  1.38 x 1.77  ) in
2365
		'E'                      => array(  233.858,   340.157), // = (   82 x 120  ) mm  = (  3.25 x 4.72  ) in
2366
		'L'                      => array(  252.283,   360.000), // = (   89 x 127  ) mm  = (  3.50 x 5.00  ) in
2367
		'3R'                     => array(  252.283,   360.000), // = (   89 x 127  ) mm  = (  3.50 x 5.00  ) in
2368
		'KG'                     => array(  289.134,   430.866), // = (  102 x 152  ) mm  = (  4.02 x 5.98  ) in
2369
		'4R'                     => array(  289.134,   430.866), // = (  102 x 152  ) mm  = (  4.02 x 5.98  ) in
2370
		'4D'                     => array(  340.157,   430.866), // = (  120 x 152  ) mm  = (  4.72 x 5.98  ) in
2371
		'2L'                     => array(  360.000,   504.567), // = (  127 x 178  ) mm  = (  5.00 x 7.01  ) in
2372
		'5R'                     => array(  360.000,   504.567), // = (  127 x 178  ) mm  = (  5.00 x 7.01  ) in
2373
		'8P'                     => array(  430.866,   575.433), // = (  152 x 203  ) mm  = (  5.98 x 7.99  ) in
2374
		'6R'                     => array(  430.866,   575.433), // = (  152 x 203  ) mm  = (  5.98 x 7.99  ) in
2375
		'6P'                     => array(  575.433,   720.000), // = (  203 x 254  ) mm  = (  7.99 x 10.00 ) in
2376
		'8R'                     => array(  575.433,   720.000), // = (  203 x 254  ) mm  = (  7.99 x 10.00 ) in
2377
		'6PW'                    => array(  575.433,   864.567), // = (  203 x 305  ) mm  = (  7.99 x 12.01 ) in
2378
		'S8R'                    => array(  575.433,   864.567), // = (  203 x 305  ) mm  = (  7.99 x 12.01 ) in
2379
		'4P'                     => array(  720.000,   864.567), // = (  254 x 305  ) mm  = ( 10.00 x 12.01 ) in
2380
		'10R'                    => array(  720.000,   864.567), // = (  254 x 305  ) mm  = ( 10.00 x 12.01 ) in
2381
		'4PW'                    => array(  720.000,  1080.000), // = (  254 x 381  ) mm  = ( 10.00 x 15.00 ) in
2382
		'S10R'                   => array(  720.000,  1080.000), // = (  254 x 381  ) mm  = ( 10.00 x 15.00 ) in
2383
		'11R'                    => array(  790.866,  1009.134), // = (  279 x 356  ) mm  = ( 10.98 x 14.02 ) in
2384
		'S11R'                   => array(  790.866,  1224.567), // = (  279 x 432  ) mm  = ( 10.98 x 17.01 ) in
2385
		'12R'                    => array(  864.567,  1080.000), // = (  305 x 381  ) mm  = ( 12.01 x 15.00 ) in
2386
		'S12R'                   => array(  864.567,  1292.598), // = (  305 x 456  ) mm  = ( 12.01 x 17.95 ) in
2387
		// Common Newspaper Sizes
2388
		'NEWSPAPER_BROADSHEET'   => array( 2125.984,  1700.787), // = (  750 x 600  ) mm  = ( 29.53 x 23.62 ) in
2389
		'NEWSPAPER_BERLINER'     => array( 1332.283,   892.913), // = (  470 x 315  ) mm  = ( 18.50 x 12.40 ) in
2390
		'NEWSPAPER_TABLOID'      => array( 1218.898,   793.701), // = (  430 x 280  ) mm  = ( 16.93 x 11.02 ) in
2391
		'NEWSPAPER_COMPACT'      => array( 1218.898,   793.701), // = (  430 x 280  ) mm  = ( 16.93 x 11.02 ) in
2392
		// Business Cards
2393
		'CREDIT_CARD'            => array(  153.014,   242.646), // = (   54 x 86   ) mm  = (  2.13 x 3.37  ) in
2394
		'BUSINESS_CARD'          => array(  153.014,   242.646), // = (   54 x 86   ) mm  = (  2.13 x 3.37  ) in
2395
		'BUSINESS_CARD_ISO7810'  => array(  153.014,   242.646), // = (   54 x 86   ) mm  = (  2.13 x 3.37  ) in
2396
		'BUSINESS_CARD_ISO216'   => array(  147.402,   209.764), // = (   52 x 74   ) mm  = (  2.05 x 2.91  ) in
2397
		'BUSINESS_CARD_IT'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
2398
		'BUSINESS_CARD_UK'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
2399
		'BUSINESS_CARD_FR'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
2400
		'BUSINESS_CARD_DE'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
2401
		'BUSINESS_CARD_ES'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
2402
		'BUSINESS_CARD_CA'       => array(  144.567,   252.283), // = (   51 x 89   ) mm  = (  2.01 x 3.50  ) in
2403
		'BUSINESS_CARD_US'       => array(  144.567,   252.283), // = (   51 x 89   ) mm  = (  2.01 x 3.50  ) in
2404
		'BUSINESS_CARD_JP'       => array(  155.906,   257.953), // = (   55 x 91   ) mm  = (  2.17 x 3.58  ) in
2405
		'BUSINESS_CARD_HK'       => array(  153.071,   255.118), // = (   54 x 90   ) mm  = (  2.13 x 3.54  ) in
2406
		'BUSINESS_CARD_AU'       => array(  155.906,   255.118), // = (   55 x 90   ) mm  = (  2.17 x 3.54  ) in
2407
		'BUSINESS_CARD_DK'       => array(  155.906,   255.118), // = (   55 x 90   ) mm  = (  2.17 x 3.54  ) in
2408
		'BUSINESS_CARD_SE'       => array(  155.906,   255.118), // = (   55 x 90   ) mm  = (  2.17 x 3.54  ) in
2409
		'BUSINESS_CARD_RU'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
2410
		'BUSINESS_CARD_CZ'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
2411
		'BUSINESS_CARD_FI'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
2412
		'BUSINESS_CARD_HU'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
2413
		'BUSINESS_CARD_IL'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
2414
		// Billboards
2415
		'4SHEET'                 => array( 2880.000,  4320.000), // = ( 1016 x 1524 ) mm  = ( 40.00 x 60.00 ) in
2416
		'6SHEET'                 => array( 3401.575,  5102.362), // = ( 1200 x 1800 ) mm  = ( 47.24 x 70.87 ) in
2417
		'12SHEET'                => array( 8640.000,  4320.000), // = ( 3048 x 1524 ) mm  = (120.00 x 60.00 ) in
2418
		'16SHEET'                => array( 5760.000,  8640.000), // = ( 2032 x 3048 ) mm  = ( 80.00 x 120.00) in
2419
		'32SHEET'                => array(11520.000,  8640.000), // = ( 4064 x 3048 ) mm  = (160.00 x 120.00) in
2420
		'48SHEET'                => array(17280.000,  8640.000), // = ( 6096 x 3048 ) mm  = (240.00 x 120.00) in
2421
		'64SHEET'                => array(23040.000,  8640.000), // = ( 8128 x 3048 ) mm  = (320.00 x 120.00) in
2422
		'96SHEET'                => array(34560.000,  8640.000), // = (12192 x 3048 ) mm  = (480.00 x 120.00) in
2423
		// -- Old European Sizes
2424
		// - Old Imperial English Sizes
2425
		'EN_EMPEROR'             => array( 3456.000,  5184.000), // = ( 1219 x 1829 ) mm  = ( 48.00 x 72.00 ) in
2426
		'EN_ANTIQUARIAN'         => array( 2232.000,  3816.000), // = (  787 x 1346 ) mm  = ( 31.00 x 53.00 ) in
2427
		'EN_GRAND_EAGLE'         => array( 2070.000,  3024.000), // = (  730 x 1067 ) mm  = ( 28.75 x 42.00 ) in
2428
		'EN_DOUBLE_ELEPHANT'     => array( 1926.000,  2880.000), // = (  679 x 1016 ) mm  = ( 26.75 x 40.00 ) in
2429
		'EN_ATLAS'               => array( 1872.000,  2448.000), // = (  660 x 864  ) mm  = ( 26.00 x 34.00 ) in
2430
		'EN_COLOMBIER'           => array( 1692.000,  2484.000), // = (  597 x 876  ) mm  = ( 23.50 x 34.50 ) in
2431
		'EN_ELEPHANT'            => array( 1656.000,  2016.000), // = (  584 x 711  ) mm  = ( 23.00 x 28.00 ) in
2432
		'EN_DOUBLE_DEMY'         => array( 1620.000,  2556.000), // = (  572 x 902  ) mm  = ( 22.50 x 35.50 ) in
2433
		'EN_IMPERIAL'            => array( 1584.000,  2160.000), // = (  559 x 762  ) mm  = ( 22.00 x 30.00 ) in
2434
		'EN_PRINCESS'            => array( 1548.000,  2016.000), // = (  546 x 711  ) mm  = ( 21.50 x 28.00 ) in
2435
		'EN_CARTRIDGE'           => array( 1512.000,  1872.000), // = (  533 x 660  ) mm  = ( 21.00 x 26.00 ) in
2436
		'EN_DOUBLE_LARGE_POST'   => array( 1512.000,  2376.000), // = (  533 x 838  ) mm  = ( 21.00 x 33.00 ) in
2437
		'EN_ROYAL'               => array( 1440.000,  1800.000), // = (  508 x 635  ) mm  = ( 20.00 x 25.00 ) in
2438
		'EN_SHEET'               => array( 1404.000,  1692.000), // = (  495 x 597  ) mm  = ( 19.50 x 23.50 ) in
2439
		'EN_HALF_POST'           => array( 1404.000,  1692.000), // = (  495 x 597  ) mm  = ( 19.50 x 23.50 ) in
2440
		'EN_SUPER_ROYAL'         => array( 1368.000,  1944.000), // = (  483 x 686  ) mm  = ( 19.00 x 27.00 ) in
2441
		'EN_DOUBLE_POST'         => array( 1368.000,  2196.000), // = (  483 x 775  ) mm  = ( 19.00 x 30.50 ) in
2442
		'EN_MEDIUM'              => array( 1260.000,  1656.000), // = (  445 x 584  ) mm  = ( 17.50 x 23.00 ) in
2443
		'EN_DEMY'                => array( 1260.000,  1620.000), // = (  445 x 572  ) mm  = ( 17.50 x 22.50 ) in
2444
		'EN_LARGE_POST'          => array( 1188.000,  1512.000), // = (  419 x 533  ) mm  = ( 16.50 x 21.00 ) in
2445
		'EN_COPY_DRAUGHT'        => array( 1152.000,  1440.000), // = (  406 x 508  ) mm  = ( 16.00 x 20.00 ) in
2446
		'EN_POST'                => array( 1116.000,  1386.000), // = (  394 x 489  ) mm  = ( 15.50 x 19.25 ) in
2447
		'EN_CROWN'               => array( 1080.000,  1440.000), // = (  381 x 508  ) mm  = ( 15.00 x 20.00 ) in
2448
		'EN_PINCHED_POST'        => array( 1062.000,  1332.000), // = (  375 x 470  ) mm  = ( 14.75 x 18.50 ) in
2449
		'EN_BRIEF'               => array(  972.000,  1152.000), // = (  343 x 406  ) mm  = ( 13.50 x 16.00 ) in
2450
		'EN_FOOLSCAP'            => array(  972.000,  1224.000), // = (  343 x 432  ) mm  = ( 13.50 x 17.00 ) in
2451
		'EN_SMALL_FOOLSCAP'      => array(  954.000,  1188.000), // = (  337 x 419  ) mm  = ( 13.25 x 16.50 ) in
2452
		'EN_POTT'                => array(  900.000,  1080.000), // = (  318 x 381  ) mm  = ( 12.50 x 15.00 ) in
2453
		// - Old Imperial Belgian Sizes
2454
		'BE_GRAND_AIGLE'         => array( 1984.252,  2948.031), // = (  700 x 1040 ) mm  = ( 27.56 x 40.94 ) in
2455
		'BE_COLOMBIER'           => array( 1757.480,  2409.449), // = (  620 x 850  ) mm  = ( 24.41 x 33.46 ) in
2456
		'BE_DOUBLE_CARRE'        => array( 1757.480,  2607.874), // = (  620 x 920  ) mm  = ( 24.41 x 36.22 ) in
2457
		'BE_ELEPHANT'            => array( 1746.142,  2182.677), // = (  616 x 770  ) mm  = ( 24.25 x 30.31 ) in
2458
		'BE_PETIT_AIGLE'         => array( 1700.787,  2381.102), // = (  600 x 840  ) mm  = ( 23.62 x 33.07 ) in
2459
		'BE_GRAND_JESUS'         => array( 1559.055,  2069.291), // = (  550 x 730  ) mm  = ( 21.65 x 28.74 ) in
2460
		'BE_JESUS'               => array( 1530.709,  2069.291), // = (  540 x 730  ) mm  = ( 21.26 x 28.74 ) in
2461
		'BE_RAISIN'              => array( 1417.323,  1842.520), // = (  500 x 650  ) mm  = ( 19.69 x 25.59 ) in
2462
		'BE_GRAND_MEDIAN'        => array( 1303.937,  1714.961), // = (  460 x 605  ) mm  = ( 18.11 x 23.82 ) in
2463
		'BE_DOUBLE_POSTE'        => array( 1233.071,  1601.575), // = (  435 x 565  ) mm  = ( 17.13 x 22.24 ) in
2464
		'BE_COQUILLE'            => array( 1218.898,  1587.402), // = (  430 x 560  ) mm  = ( 16.93 x 22.05 ) in
2465
		'BE_PETIT_MEDIAN'        => array( 1176.378,  1502.362), // = (  415 x 530  ) mm  = ( 16.34 x 20.87 ) in
2466
		'BE_RUCHE'               => array( 1020.472,  1303.937), // = (  360 x 460  ) mm  = ( 14.17 x 18.11 ) in
2467
		'BE_PROPATRIA'           => array(  977.953,  1218.898), // = (  345 x 430  ) mm  = ( 13.58 x 16.93 ) in
2468
		'BE_LYS'                 => array(  898.583,  1125.354), // = (  317 x 397  ) mm  = ( 12.48 x 15.63 ) in
2469
		'BE_POT'                 => array(  870.236,  1088.504), // = (  307 x 384  ) mm  = ( 12.09 x 15.12 ) in
2470
		'BE_ROSETTE'             => array(  765.354,   983.622), // = (  270 x 347  ) mm  = ( 10.63 x 13.66 ) in
2471
		// - Old Imperial French Sizes
2472
		'FR_UNIVERS'             => array( 2834.646,  3685.039), // = ( 1000 x 1300 ) mm  = ( 39.37 x 51.18 ) in
2473
		'FR_DOUBLE_COLOMBIER'    => array( 2551.181,  3571.654), // = (  900 x 1260 ) mm  = ( 35.43 x 49.61 ) in
2474
		'FR_GRANDE_MONDE'        => array( 2551.181,  3571.654), // = (  900 x 1260 ) mm  = ( 35.43 x 49.61 ) in
2475
		'FR_DOUBLE_SOLEIL'       => array( 2267.717,  3401.575), // = (  800 x 1200 ) mm  = ( 31.50 x 47.24 ) in
2476
		'FR_DOUBLE_JESUS'        => array( 2154.331,  3174.803), // = (  760 x 1120 ) mm  = ( 29.92 x 44.09 ) in
2477
		'FR_GRAND_AIGLE'         => array( 2125.984,  3004.724), // = (  750 x 1060 ) mm  = ( 29.53 x 41.73 ) in
2478
		'FR_PETIT_AIGLE'         => array( 1984.252,  2664.567), // = (  700 x 940  ) mm  = ( 27.56 x 37.01 ) in
2479
		'FR_DOUBLE_RAISIN'       => array( 1842.520,  2834.646), // = (  650 x 1000 ) mm  = ( 25.59 x 39.37 ) in
2480
		'FR_JOURNAL'             => array( 1842.520,  2664.567), // = (  650 x 940  ) mm  = ( 25.59 x 37.01 ) in
2481
		'FR_COLOMBIER_AFFICHE'   => array( 1785.827,  2551.181), // = (  630 x 900  ) mm  = ( 24.80 x 35.43 ) in
2482
		'FR_DOUBLE_CAVALIER'     => array( 1757.480,  2607.874), // = (  620 x 920  ) mm  = ( 24.41 x 36.22 ) in
2483
		'FR_CLOCHE'              => array( 1700.787,  2267.717), // = (  600 x 800  ) mm  = ( 23.62 x 31.50 ) in
2484
		'FR_SOLEIL'              => array( 1700.787,  2267.717), // = (  600 x 800  ) mm  = ( 23.62 x 31.50 ) in
2485
		'FR_DOUBLE_CARRE'        => array( 1587.402,  2551.181), // = (  560 x 900  ) mm  = ( 22.05 x 35.43 ) in
2486
		'FR_DOUBLE_COQUILLE'     => array( 1587.402,  2494.488), // = (  560 x 880  ) mm  = ( 22.05 x 34.65 ) in
2487
		'FR_JESUS'               => array( 1587.402,  2154.331), // = (  560 x 760  ) mm  = ( 22.05 x 29.92 ) in
2488
		'FR_RAISIN'              => array( 1417.323,  1842.520), // = (  500 x 650  ) mm  = ( 19.69 x 25.59 ) in
2489
		'FR_CAVALIER'            => array( 1303.937,  1757.480), // = (  460 x 620  ) mm  = ( 18.11 x 24.41 ) in
2490
		'FR_DOUBLE_COURONNE'     => array( 1303.937,  2040.945), // = (  460 x 720  ) mm  = ( 18.11 x 28.35 ) in
2491
		'FR_CARRE'               => array( 1275.591,  1587.402), // = (  450 x 560  ) mm  = ( 17.72 x 22.05 ) in
2492
		'FR_COQUILLE'            => array( 1247.244,  1587.402), // = (  440 x 560  ) mm  = ( 17.32 x 22.05 ) in
2493
		'FR_DOUBLE_TELLIERE'     => array( 1247.244,  1927.559), // = (  440 x 680  ) mm  = ( 17.32 x 26.77 ) in
2494
		'FR_DOUBLE_CLOCHE'       => array( 1133.858,  1700.787), // = (  400 x 600  ) mm  = ( 15.75 x 23.62 ) in
2495
		'FR_DOUBLE_POT'          => array( 1133.858,  1757.480), // = (  400 x 620  ) mm  = ( 15.75 x 24.41 ) in
2496
		'FR_ECU'                 => array( 1133.858,  1474.016), // = (  400 x 520  ) mm  = ( 15.75 x 20.47 ) in
2497
		'FR_COURONNE'            => array( 1020.472,  1303.937), // = (  360 x 460  ) mm  = ( 14.17 x 18.11 ) in
2498
		'FR_TELLIERE'            => array(  963.780,  1247.244), // = (  340 x 440  ) mm  = ( 13.39 x 17.32 ) in
2499
		'FR_POT'                 => array(  878.740,  1133.858), // = (  310 x 400  ) mm  = ( 12.20 x 15.75 ) in
2500
	);
2501
2502
2503
	/**
2504
	 * Get page dimensions from format name.
2505
	 * @param mixed $format The format name @see self::$page_format<ul>
2506
	 * @return array containing page width and height in points
2507
	 * @since 5.0.010 (2010-05-17)
2508
	 * @public static
2509
	 */
2510
	public static function getPageSizeFromFormat($format) {
2511
		if (isset(self::$page_formats[$format])) {
2512
			return self::$page_formats[$format];
2513
		}
2514
		return self::$page_formats['A4'];
2515
	}
2516
2517
	/**
2518
	 * Set page boundaries.
2519
	 * @param int $page page number
2520
	 * @param string $type 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>
2521
	 * @param float $llx lower-left x coordinate in user units.
2522
	 * @param float $lly lower-left y coordinate in user units.
2523
	 * @param float $urx upper-right x coordinate in user units.
2524
	 * @param float $ury upper-right y coordinate in user units.
2525
	 * @param boolean $points If true uses user units as unit of measure, otherwise uses PDF points.
2526
	 * @param float $k Scale factor (number of points in user unit).
2527
	 * @param array $pagedim Array of page dimensions.
2528
	 * @return array pagedim array of page dimensions.
2529
	 * @since 5.0.010 (2010-05-17)
2530
	 * @public static
2531
	 */
2532
	public static function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points, $k, $pagedim=array()) {
2533
		if (!isset($pagedim[$page])) {
2534
			// initialize array
2535
			$pagedim[$page] = array();
2536
		}
2537
		if (!in_array($type, self::$pageboxes)) {
2538
			return;
2539
		}
2540
		if ($points) {
2541
			$k = 1;
2542
		}
2543
		$pagedim[$page][$type]['llx'] = ($llx * $k);
2544
		$pagedim[$page][$type]['lly'] = ($lly * $k);
2545
		$pagedim[$page][$type]['urx'] = ($urx * $k);
2546
		$pagedim[$page][$type]['ury'] = ($ury * $k);
2547
		return $pagedim;
2548
	}
2549
2550
	/**
2551
	 * Swap X and Y coordinates of page boxes (change page boxes orientation).
2552
	 * @param int $page page number
2553
	 * @param array $pagedim Array of page dimensions.
2554
	 * @return array pagedim array of page dimensions.
2555
	 * @since 5.0.010 (2010-05-17)
2556
	 * @public static
2557
	 */
2558
	public static function swapPageBoxCoordinates($page, $pagedim) {
2559
		foreach (self::$pageboxes as $type) {
2560
			// swap X and Y coordinates
2561
			if (isset($pagedim[$page][$type])) {
2562
				$tmp = $pagedim[$page][$type]['llx'];
2563
				$pagedim[$page][$type]['llx'] = $pagedim[$page][$type]['lly'];
2564
				$pagedim[$page][$type]['lly'] = $tmp;
2565
				$tmp = $pagedim[$page][$type]['urx'];
2566
				$pagedim[$page][$type]['urx'] = $pagedim[$page][$type]['ury'];
2567
				$pagedim[$page][$type]['ury'] = $tmp;
2568
			}
2569
		}
2570
		return $pagedim;
2571
	}
2572
2573
	/**
2574
	 * Get the canonical page layout mode.
2575
	 * @param string $layout The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
2576
	 * @return string Canonical page layout name.
2577
	 * @public static
2578
	 */
2579
	public static function getPageLayoutMode($layout='SinglePage') {
2580
		switch ($layout) {
2581
			case 'default':
2582
			case 'single':
2583
			case 'SinglePage': {
2584
				$layout_mode = 'SinglePage';
2585
				break;
2586
			}
2587
			case 'continuous':
2588
			case 'OneColumn': {
2589
				$layout_mode = 'OneColumn';
2590
				break;
2591
			}
2592
			case 'two':
2593
			case 'TwoColumnLeft': {
2594
				$layout_mode = 'TwoColumnLeft';
2595
				break;
2596
			}
2597
			case 'TwoColumnRight': {
2598
				$layout_mode = 'TwoColumnRight';
2599
				break;
2600
			}
2601
			case 'TwoPageLeft': {
2602
				$layout_mode = 'TwoPageLeft';
2603
				break;
2604
			}
2605
			case 'TwoPageRight': {
2606
				$layout_mode = 'TwoPageRight';
2607
				break;
2608
			}
2609
			default: {
2610
				$layout_mode = 'SinglePage';
2611
			}
2612
		}
2613
		return $layout_mode;
2614
	}
2615
2616
	/**
2617
	 * Get the canonical page layout mode.
2618
	 * @param string $mode A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
2619
	 * @return string Canonical page mode name.
2620
	 * @public static
2621
	 */
2622
	public static function getPageMode($mode='UseNone') {
2623
		switch ($mode) {
2624
			case 'UseNone': {
2625
				$page_mode = 'UseNone';
2626
				break;
2627
			}
2628
			case 'UseOutlines': {
2629
				$page_mode = 'UseOutlines';
2630
				break;
2631
			}
2632
			case 'UseThumbs': {
2633
				$page_mode = 'UseThumbs';
2634
				break;
2635
			}
2636
			case 'FullScreen': {
2637
				$page_mode = 'FullScreen';
2638
				break;
2639
			}
2640
			case 'UseOC': {
2641
				$page_mode = 'UseOC';
2642
				break;
2643
			}
2644
			case '': {
2645
				$page_mode = 'UseAttachments';
2646
				break;
2647
			}
2648
			default: {
2649
				$page_mode = 'UseNone';
2650
			}
2651
		}
2652
		return $page_mode;
2653
	}
2654
2655
} // END OF TCPDF_STATIC CLASS
2656
2657
//============================================================+
2658
// END OF FILE
2659
//============================================================+
2660