Projet

Général

Profil

Paste
Télécharger (104 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / tcpdf-6.2.9 / include / tcpdf_static.php @ 7fe061e8

1
<?php
2
//============================================================+
3
// File name   : tcpdf_static.php
4
// Version     : 1.1.3
5
// Begin       : 2002-08-03
6
// Last Update : 2015-04-28
7
// Author      : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
8
// License     : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9
// -------------------------------------------------------------------
10
// Copyright (C) 2002-2015 Nicola Asuni - Tecnick.com LTD
11
//
12
// This file is part of TCPDF software library.
13
//
14
// TCPDF is free software: you can redistribute it and/or modify it
15
// under the terms of the GNU Lesser General Public License as
16
// published by the Free Software Foundation, either version 3 of the
17
// License, or (at your option) any later version.
18
//
19
// TCPDF is distributed in the hope that it will be useful, but
20
// WITHOUT ANY WARRANTY; without even the implied warranty of
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
// See the GNU Lesser General Public License for more details.
23
//
24
// You should have received a copy of the License
25
// along with TCPDF. If not, see
26
// <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
27
//
28
// See LICENSE.TXT file for more information.
29
// -------------------------------------------------------------------
30
//
31
// Description :
32
//   Static methods used by the TCPDF class.
33
//
34
//============================================================+
35

    
36
/**
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.2
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.1
50
 * @author Nicola Asuni - info@tecnick.com
51
 */
52
class TCPDF_STATIC {
53

    
54
        /**
55
         * Current TCPDF version.
56
         * @private static
57
         */
58
        private static $tcpdf_version = '6.2.9';
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 page formats
111
         * measures are calculated in this way: (inches * 72) or (millimeters * 72 / 25.4)
112
         * @public static
113
         */
114
        public static $page_formats = array(
115
                // ISO 216 A Series + 2 SIS 014711 extensions
116
                'A0'                     => array( 2383.937,  3370.394), // = (  841 x 1189 ) mm  = ( 33.11 x 46.81 ) in
117
                'A1'                     => array( 1683.780,  2383.937), // = (  594 x 841  ) mm  = ( 23.39 x 33.11 ) in
118
                'A2'                     => array( 1190.551,  1683.780), // = (  420 x 594  ) mm  = ( 16.54 x 23.39 ) in
119
                'A3'                     => array(  841.890,  1190.551), // = (  297 x 420  ) mm  = ( 11.69 x 16.54 ) in
120
                'A4'                     => array(  595.276,   841.890), // = (  210 x 297  ) mm  = (  8.27 x 11.69 ) in
121
                'A5'                     => array(  419.528,   595.276), // = (  148 x 210  ) mm  = (  5.83 x 8.27  ) in
122
                'A6'                     => array(  297.638,   419.528), // = (  105 x 148  ) mm  = (  4.13 x 5.83  ) in
123
                'A7'                     => array(  209.764,   297.638), // = (   74 x 105  ) mm  = (  2.91 x 4.13  ) in
124
                'A8'                     => array(  147.402,   209.764), // = (   52 x 74   ) mm  = (  2.05 x 2.91  ) in
125
                'A9'                     => array(  104.882,   147.402), // = (   37 x 52   ) mm  = (  1.46 x 2.05  ) in
126
                'A10'                    => array(   73.701,   104.882), // = (   26 x 37   ) mm  = (  1.02 x 1.46  ) in
127
                'A11'                    => array(   51.024,    73.701), // = (   18 x 26   ) mm  = (  0.71 x 1.02  ) in
128
                'A12'                    => array(   36.850,    51.024), // = (   13 x 18   ) mm  = (  0.51 x 0.71  ) in
129
                // ISO 216 B Series + 2 SIS 014711 extensions
130
                'B0'                     => array( 2834.646,  4008.189), // = ( 1000 x 1414 ) mm  = ( 39.37 x 55.67 ) in
131
                'B1'                     => array( 2004.094,  2834.646), // = (  707 x 1000 ) mm  = ( 27.83 x 39.37 ) in
132
                'B2'                     => array( 1417.323,  2004.094), // = (  500 x 707  ) mm  = ( 19.69 x 27.83 ) in
133
                'B3'                     => array( 1000.630,  1417.323), // = (  353 x 500  ) mm  = ( 13.90 x 19.69 ) in
134
                'B4'                     => array(  708.661,  1000.630), // = (  250 x 353  ) mm  = (  9.84 x 13.90 ) in
135
                'B5'                     => array(  498.898,   708.661), // = (  176 x 250  ) mm  = (  6.93 x 9.84  ) in
136
                'B6'                     => array(  354.331,   498.898), // = (  125 x 176  ) mm  = (  4.92 x 6.93  ) in
137
                'B7'                     => array(  249.449,   354.331), // = (   88 x 125  ) mm  = (  3.46 x 4.92  ) in
138
                'B8'                     => array(  175.748,   249.449), // = (   62 x 88   ) mm  = (  2.44 x 3.46  ) in
139
                'B9'                     => array(  124.724,   175.748), // = (   44 x 62   ) mm  = (  1.73 x 2.44  ) in
140
                'B10'                    => array(   87.874,   124.724), // = (   31 x 44   ) mm  = (  1.22 x 1.73  ) in
141
                'B11'                    => array(   62.362,    87.874), // = (   22 x 31   ) mm  = (  0.87 x 1.22  ) in
142
                'B12'                    => array(   42.520,    62.362), // = (   15 x 22   ) mm  = (  0.59 x 0.87  ) in
143
                // ISO 216 C Series + 2 SIS 014711 extensions + 5 EXTENSION
144
                'C0'                     => array( 2599.370,  3676.535), // = (  917 x 1297 ) mm  = ( 36.10 x 51.06 ) in
145
                'C1'                     => array( 1836.850,  2599.370), // = (  648 x 917  ) mm  = ( 25.51 x 36.10 ) in
146
                'C2'                     => array( 1298.268,  1836.850), // = (  458 x 648  ) mm  = ( 18.03 x 25.51 ) in
147
                'C3'                     => array(  918.425,  1298.268), // = (  324 x 458  ) mm  = ( 12.76 x 18.03 ) in
148
                'C4'                     => array(  649.134,   918.425), // = (  229 x 324  ) mm  = (  9.02 x 12.76 ) in
149
                'C5'                     => array(  459.213,   649.134), // = (  162 x 229  ) mm  = (  6.38 x 9.02  ) in
150
                'C6'                     => array(  323.150,   459.213), // = (  114 x 162  ) mm  = (  4.49 x 6.38  ) in
151
                'C7'                     => array(  229.606,   323.150), // = (   81 x 114  ) mm  = (  3.19 x 4.49  ) in
152
                'C8'                     => array(  161.575,   229.606), // = (   57 x 81   ) mm  = (  2.24 x 3.19  ) in
153
                'C9'                     => array(  113.386,   161.575), // = (   40 x 57   ) mm  = (  1.57 x 2.24  ) in
154
                'C10'                    => array(   79.370,   113.386), // = (   28 x 40   ) mm  = (  1.10 x 1.57  ) in
155
                'C11'                    => array(   56.693,    79.370), // = (   20 x 28   ) mm  = (  0.79 x 1.10  ) in
156
                'C12'                    => array(   39.685,    56.693), // = (   14 x 20   ) mm  = (  0.55 x 0.79  ) in
157
                'C76'                    => array(  229.606,   459.213), // = (   81 x 162  ) mm  = (  3.19 x 6.38  ) in
158
                'DL'                     => array(  311.811,   623.622), // = (  110 x 220  ) mm  = (  4.33 x 8.66  ) in
159
                'DLE'                    => array(  323.150,   637.795), // = (  114 x 225  ) mm  = (  4.49 x 8.86  ) in
160
                'DLX'                    => array(  340.158,   666.142), // = (  120 x 235  ) mm  = (  4.72 x 9.25  ) in
161
                'DLP'                    => array(  280.630,   595.276), // = (   99 x 210  ) mm  = (  3.90 x 8.27  ) in (1/3 A4)
162
                // SIS 014711 E Series
163
                'E0'                     => array( 2491.654,  3517.795), // = (  879 x 1241 ) mm  = ( 34.61 x 48.86 ) in
164
                'E1'                     => array( 1757.480,  2491.654), // = (  620 x 879  ) mm  = ( 24.41 x 34.61 ) in
165
                'E2'                     => array( 1247.244,  1757.480), // = (  440 x 620  ) mm  = ( 17.32 x 24.41 ) in
166
                'E3'                     => array(  878.740,  1247.244), // = (  310 x 440  ) mm  = ( 12.20 x 17.32 ) in
167
                'E4'                     => array(  623.622,   878.740), // = (  220 x 310  ) mm  = (  8.66 x 12.20 ) in
168
                'E5'                     => array(  439.370,   623.622), // = (  155 x 220  ) mm  = (  6.10 x 8.66  ) in
169
                'E6'                     => array(  311.811,   439.370), // = (  110 x 155  ) mm  = (  4.33 x 6.10  ) in
170
                'E7'                     => array(  221.102,   311.811), // = (   78 x 110  ) mm  = (  3.07 x 4.33  ) in
171
                'E8'                     => array(  155.906,   221.102), // = (   55 x 78   ) mm  = (  2.17 x 3.07  ) in
172
                'E9'                     => array(  110.551,   155.906), // = (   39 x 55   ) mm  = (  1.54 x 2.17  ) in
173
                'E10'                    => array(   76.535,   110.551), // = (   27 x 39   ) mm  = (  1.06 x 1.54  ) in
174
                'E11'                    => array(   53.858,    76.535), // = (   19 x 27   ) mm  = (  0.75 x 1.06  ) in
175
                'E12'                    => array(   36.850,    53.858), // = (   13 x 19   ) mm  = (  0.51 x 0.75  ) in
176
                // SIS 014711 G Series
177
                'G0'                     => array( 2715.591,  3838.110), // = (  958 x 1354 ) mm  = ( 37.72 x 53.31 ) in
178
                'G1'                     => array( 1919.055,  2715.591), // = (  677 x 958  ) mm  = ( 26.65 x 37.72 ) in
179
                'G2'                     => array( 1357.795,  1919.055), // = (  479 x 677  ) mm  = ( 18.86 x 26.65 ) in
180
                'G3'                     => array(  958.110,  1357.795), // = (  338 x 479  ) mm  = ( 13.31 x 18.86 ) in
181
                'G4'                     => array(  677.480,   958.110), // = (  239 x 338  ) mm  = (  9.41 x 13.31 ) in
182
                'G5'                     => array(  479.055,   677.480), // = (  169 x 239  ) mm  = (  6.65 x 9.41  ) in
183
                'G6'                     => array(  337.323,   479.055), // = (  119 x 169  ) mm  = (  4.69 x 6.65  ) in
184
                'G7'                     => array(  238.110,   337.323), // = (   84 x 119  ) mm  = (  3.31 x 4.69  ) in
185
                'G8'                     => array(  167.244,   238.110), // = (   59 x 84   ) mm  = (  2.32 x 3.31  ) in
186
                'G9'                     => array(  119.055,   167.244), // = (   42 x 59   ) mm  = (  1.65 x 2.32  ) in
187
                'G10'                    => array(   82.205,   119.055), // = (   29 x 42   ) mm  = (  1.14 x 1.65  ) in
188
                'G11'                    => array(   59.528,    82.205), // = (   21 x 29   ) mm  = (  0.83 x 1.14  ) in
189
                'G12'                    => array(   39.685,    59.528), // = (   14 x 21   ) mm  = (  0.55 x 0.83  ) in
190
                // ISO Press
191
                'RA0'                    => array( 2437.795,  3458.268), // = (  860 x 1220 ) mm  = ( 33.86 x 48.03 ) in
192
                'RA1'                    => array( 1729.134,  2437.795), // = (  610 x 860  ) mm  = ( 24.02 x 33.86 ) in
193
                'RA2'                    => array( 1218.898,  1729.134), // = (  430 x 610  ) mm  = ( 16.93 x 24.02 ) in
194
                'RA3'                    => array(  864.567,  1218.898), // = (  305 x 430  ) mm  = ( 12.01 x 16.93 ) in
195
                'RA4'                    => array(  609.449,   864.567), // = (  215 x 305  ) mm  = (  8.46 x 12.01 ) in
196
                'SRA0'                   => array( 2551.181,  3628.346), // = (  900 x 1280 ) mm  = ( 35.43 x 50.39 ) in
197
                'SRA1'                   => array( 1814.173,  2551.181), // = (  640 x 900  ) mm  = ( 25.20 x 35.43 ) in
198
                'SRA2'                   => array( 1275.591,  1814.173), // = (  450 x 640  ) mm  = ( 17.72 x 25.20 ) in
199
                'SRA3'                   => array(  907.087,  1275.591), // = (  320 x 450  ) mm  = ( 12.60 x 17.72 ) in
200
                'SRA4'                   => array(  637.795,   907.087), // = (  225 x 320  ) mm  = (  8.86 x 12.60 ) in
201
                // German DIN 476
202
                '4A0'                    => array( 4767.874,  6740.787), // = ( 1682 x 2378 ) mm  = ( 66.22 x 93.62 ) in
203
                '2A0'                    => array( 3370.394,  4767.874), // = ( 1189 x 1682 ) mm  = ( 46.81 x 66.22 ) in
204
                // Variations on the ISO Standard
205
                'A2_EXTRA'               => array( 1261.417,  1754.646), // = (  445 x 619  ) mm  = ( 17.52 x 24.37 ) in
206
                'A3+'                    => array(  932.598,  1369.134), // = (  329 x 483  ) mm  = ( 12.95 x 19.02 ) in
207
                'A3_EXTRA'               => array(  912.756,  1261.417), // = (  322 x 445  ) mm  = ( 12.68 x 17.52 ) in
208
                'A3_SUPER'               => array(  864.567,  1440.000), // = (  305 x 508  ) mm  = ( 12.01 x 20.00 ) in
209
                'SUPER_A3'               => array(  864.567,  1380.472), // = (  305 x 487  ) mm  = ( 12.01 x 19.17 ) in
210
                'A4_EXTRA'               => array(  666.142,   912.756), // = (  235 x 322  ) mm  = (  9.25 x 12.68 ) in
211
                'A4_SUPER'               => array(  649.134,   912.756), // = (  229 x 322  ) mm  = (  9.02 x 12.68 ) in
212
                'SUPER_A4'               => array(  643.465,  1009.134), // = (  227 x 356  ) mm  = (  8.94 x 14.02 ) in
213
                'A4_LONG'                => array(  595.276,   986.457), // = (  210 x 348  ) mm  = (  8.27 x 13.70 ) in
214
                'F4'                     => array(  595.276,   935.433), // = (  210 x 330  ) mm  = (  8.27 x 12.99 ) in
215
                'SO_B5_EXTRA'            => array(  572.598,   782.362), // = (  202 x 276  ) mm  = (  7.95 x 10.87 ) in
216
                'A5_EXTRA'               => array(  490.394,   666.142), // = (  173 x 235  ) mm  = (  6.81 x 9.25  ) in
217
                // ANSI Series
218
                'ANSI_E'                 => array( 2448.000,  3168.000), // = (  864 x 1118 ) mm  = ( 34.00 x 44.00 ) in
219
                'ANSI_D'                 => array( 1584.000,  2448.000), // = (  559 x 864  ) mm  = ( 22.00 x 34.00 ) in
220
                'ANSI_C'                 => array( 1224.000,  1584.000), // = (  432 x 559  ) mm  = ( 17.00 x 22.00 ) in
221
                'ANSI_B'                 => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
222
                'ANSI_A'                 => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
223
                // Traditional 'Loose' North American Paper Sizes
224
                'USLEDGER'               => array( 1224.000,   792.000), // = (  432 x 279  ) mm  = ( 17.00 x 11.00 ) in
225
                'LEDGER'                 => array( 1224.000,   792.000), // = (  432 x 279  ) mm  = ( 17.00 x 11.00 ) in
226
                'ORGANIZERK'             => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
227
                'BIBLE'                  => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
228
                'USTABLOID'              => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
229
                'TABLOID'                => array(  792.000,  1224.000), // = (  279 x 432  ) mm  = ( 11.00 x 17.00 ) in
230
                'ORGANIZERM'             => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
231
                'USLETTER'               => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
232
                'LETTER'                 => array(  612.000,   792.000), // = (  216 x 279  ) mm  = (  8.50 x 11.00 ) in
233
                'USLEGAL'                => array(  612.000,  1008.000), // = (  216 x 356  ) mm  = (  8.50 x 14.00 ) in
234
                'LEGAL'                  => array(  612.000,  1008.000), // = (  216 x 356  ) mm  = (  8.50 x 14.00 ) in
235
                'GOVERNMENTLETTER'       => array(  576.000,   756.000), // = (  203 x 267  ) mm  = (  8.00 x 10.50 ) in
236
                'GLETTER'                => array(  576.000,   756.000), // = (  203 x 267  ) mm  = (  8.00 x 10.50 ) in
237
                'JUNIORLEGAL'            => array(  576.000,   360.000), // = (  203 x 127  ) mm  = (  8.00 x 5.00  ) in
238
                'JLEGAL'                 => array(  576.000,   360.000), // = (  203 x 127  ) mm  = (  8.00 x 5.00  ) in
239
                // Other North American Paper Sizes
240
                'QUADDEMY'               => array( 2520.000,  3240.000), // = (  889 x 1143 ) mm  = ( 35.00 x 45.00 ) in
241
                'SUPER_B'                => array(  936.000,  1368.000), // = (  330 x 483  ) mm  = ( 13.00 x 19.00 ) in
242
                'QUARTO'                 => array(  648.000,   792.000), // = (  229 x 279  ) mm  = (  9.00 x 11.00 ) in
243
                'GOVERNMENTLEGAL'        => array(  612.000,   936.000), // = (  216 x 330  ) mm  = (  8.50 x 13.00 ) in
244
                'FOLIO'                  => array(  612.000,   936.000), // = (  216 x 330  ) mm  = (  8.50 x 13.00 ) in
245
                'MONARCH'                => array(  522.000,   756.000), // = (  184 x 267  ) mm  = (  7.25 x 10.50 ) in
246
                'EXECUTIVE'              => array(  522.000,   756.000), // = (  184 x 267  ) mm  = (  7.25 x 10.50 ) in
247
                'ORGANIZERL'             => array(  396.000,   612.000), // = (  140 x 216  ) mm  = (  5.50 x 8.50  ) in
248
                'STATEMENT'              => array(  396.000,   612.000), // = (  140 x 216  ) mm  = (  5.50 x 8.50  ) in
249
                'MEMO'                   => array(  396.000,   612.000), // = (  140 x 216  ) mm  = (  5.50 x 8.50  ) in
250
                'FOOLSCAP'               => array(  595.440,   936.000), // = (  210 x 330  ) mm  = (  8.27 x 13.00 ) in
251
                'COMPACT'                => array(  306.000,   486.000), // = (  108 x 171  ) mm  = (  4.25 x 6.75  ) in
252
                'ORGANIZERJ'             => array(  198.000,   360.000), // = (   70 x 127  ) mm  = (  2.75 x 5.00  ) in
253
                // Canadian standard CAN 2-9.60M
254
                'P1'                     => array( 1587.402,  2437.795), // = (  560 x 860  ) mm  = ( 22.05 x 33.86 ) in
255
                'P2'                     => array( 1218.898,  1587.402), // = (  430 x 560  ) mm  = ( 16.93 x 22.05 ) in
256
                'P3'                     => array(  793.701,  1218.898), // = (  280 x 430  ) mm  = ( 11.02 x 16.93 ) in
257
                'P4'                     => array(  609.449,   793.701), // = (  215 x 280  ) mm  = (  8.46 x 11.02 ) in
258
                'P5'                     => array(  396.850,   609.449), // = (  140 x 215  ) mm  = (  5.51 x 8.46  ) in
259
                'P6'                     => array(  303.307,   396.850), // = (  107 x 140  ) mm  = (  4.21 x 5.51  ) in
260
                // North American Architectural Sizes
261
                'ARCH_E'                 => array( 2592.000,  3456.000), // = (  914 x 1219 ) mm  = ( 36.00 x 48.00 ) in
262
                'ARCH_E1'                => array( 2160.000,  3024.000), // = (  762 x 1067 ) mm  = ( 30.00 x 42.00 ) in
263
                'ARCH_D'                 => array( 1728.000,  2592.000), // = (  610 x 914  ) mm  = ( 24.00 x 36.00 ) in
264
                'BROADSHEET'             => array( 1296.000,  1728.000), // = (  457 x 610  ) mm  = ( 18.00 x 24.00 ) in
265
                'ARCH_C'                 => array( 1296.000,  1728.000), // = (  457 x 610  ) mm  = ( 18.00 x 24.00 ) in
266
                'ARCH_B'                 => array(  864.000,  1296.000), // = (  305 x 457  ) mm  = ( 12.00 x 18.00 ) in
267
                'ARCH_A'                 => array(  648.000,   864.000), // = (  229 x 305  ) mm  = (  9.00 x 12.00 ) in
268
                // -- North American Envelope Sizes
269
                // - Announcement Envelopes
270
                'ANNENV_A2'              => array(  314.640,   414.000), // = (  111 x 146  ) mm  = (  4.37 x 5.75  ) in
271
                'ANNENV_A6'              => array(  342.000,   468.000), // = (  121 x 165  ) mm  = (  4.75 x 6.50  ) in
272
                'ANNENV_A7'              => array(  378.000,   522.000), // = (  133 x 184  ) mm  = (  5.25 x 7.25  ) in
273
                'ANNENV_A8'              => array(  396.000,   584.640), // = (  140 x 206  ) mm  = (  5.50 x 8.12  ) in
274
                'ANNENV_A10'             => array(  450.000,   692.640), // = (  159 x 244  ) mm  = (  6.25 x 9.62  ) in
275
                'ANNENV_SLIM'            => array(  278.640,   638.640), // = (   98 x 225  ) mm  = (  3.87 x 8.87  ) in
276
                // - Commercial Envelopes
277
                'COMMENV_N6_1/4'         => array(  252.000,   432.000), // = (   89 x 152  ) mm  = (  3.50 x 6.00  ) in
278
                'COMMENV_N6_3/4'         => array(  260.640,   468.000), // = (   92 x 165  ) mm  = (  3.62 x 6.50  ) in
279
                'COMMENV_N8'             => array(  278.640,   540.000), // = (   98 x 191  ) mm  = (  3.87 x 7.50  ) in
280
                'COMMENV_N9'             => array(  278.640,   638.640), // = (   98 x 225  ) mm  = (  3.87 x 8.87  ) in
281
                'COMMENV_N10'            => array(  296.640,   684.000), // = (  105 x 241  ) mm  = (  4.12 x 9.50  ) in
282
                'COMMENV_N11'            => array(  324.000,   746.640), // = (  114 x 263  ) mm  = (  4.50 x 10.37 ) in
283
                'COMMENV_N12'            => array(  342.000,   792.000), // = (  121 x 279  ) mm  = (  4.75 x 11.00 ) in
284
                'COMMENV_N14'            => array(  360.000,   828.000), // = (  127 x 292  ) mm  = (  5.00 x 11.50 ) in
285
                // - Catalogue Envelopes
286
                'CATENV_N1'              => array(  432.000,   648.000), // = (  152 x 229  ) mm  = (  6.00 x 9.00  ) in
287
                'CATENV_N1_3/4'          => array(  468.000,   684.000), // = (  165 x 241  ) mm  = (  6.50 x 9.50  ) in
288
                'CATENV_N2'              => array(  468.000,   720.000), // = (  165 x 254  ) mm  = (  6.50 x 10.00 ) in
289
                'CATENV_N3'              => array(  504.000,   720.000), // = (  178 x 254  ) mm  = (  7.00 x 10.00 ) in
290
                'CATENV_N6'              => array(  540.000,   756.000), // = (  191 x 267  ) mm  = (  7.50 x 10.50 ) in
291
                'CATENV_N7'              => array(  576.000,   792.000), // = (  203 x 279  ) mm  = (  8.00 x 11.00 ) in
292
                'CATENV_N8'              => array(  594.000,   810.000), // = (  210 x 286  ) mm  = (  8.25 x 11.25 ) in
293
                'CATENV_N9_1/2'          => array(  612.000,   756.000), // = (  216 x 267  ) mm  = (  8.50 x 10.50 ) in
294
                'CATENV_N9_3/4'          => array(  630.000,   810.000), // = (  222 x 286  ) mm  = (  8.75 x 11.25 ) in
295
                'CATENV_N10_1/2'         => array(  648.000,   864.000), // = (  229 x 305  ) mm  = (  9.00 x 12.00 ) in
296
                'CATENV_N12_1/2'         => array(  684.000,   900.000), // = (  241 x 318  ) mm  = (  9.50 x 12.50 ) in
297
                'CATENV_N13_1/2'         => array(  720.000,   936.000), // = (  254 x 330  ) mm  = ( 10.00 x 13.00 ) in
298
                'CATENV_N14_1/4'         => array(  810.000,   882.000), // = (  286 x 311  ) mm  = ( 11.25 x 12.25 ) in
299
                'CATENV_N14_1/2'         => array(  828.000,  1044.000), // = (  292 x 368  ) mm  = ( 11.50 x 14.50 ) in
300
                // Japanese (JIS P 0138-61) Standard B-Series
301
                'JIS_B0'                 => array( 2919.685,  4127.244), // = ( 1030 x 1456 ) mm  = ( 40.55 x 57.32 ) in
302
                'JIS_B1'                 => array( 2063.622,  2919.685), // = (  728 x 1030 ) mm  = ( 28.66 x 40.55 ) in
303
                'JIS_B2'                 => array( 1459.843,  2063.622), // = (  515 x 728  ) mm  = ( 20.28 x 28.66 ) in
304
                'JIS_B3'                 => array( 1031.811,  1459.843), // = (  364 x 515  ) mm  = ( 14.33 x 20.28 ) in
305
                'JIS_B4'                 => array(  728.504,  1031.811), // = (  257 x 364  ) mm  = ( 10.12 x 14.33 ) in
306
                'JIS_B5'                 => array(  515.906,   728.504), // = (  182 x 257  ) mm  = (  7.17 x 10.12 ) in
307
                'JIS_B6'                 => array(  362.835,   515.906), // = (  128 x 182  ) mm  = (  5.04 x 7.17  ) in
308
                'JIS_B7'                 => array(  257.953,   362.835), // = (   91 x 128  ) mm  = (  3.58 x 5.04  ) in
309
                'JIS_B8'                 => array(  181.417,   257.953), // = (   64 x 91   ) mm  = (  2.52 x 3.58  ) in
310
                'JIS_B9'                 => array(  127.559,   181.417), // = (   45 x 64   ) mm  = (  1.77 x 2.52  ) in
311
                'JIS_B10'                => array(   90.709,   127.559), // = (   32 x 45   ) mm  = (  1.26 x 1.77  ) in
312
                'JIS_B11'                => array(   62.362,    90.709), // = (   22 x 32   ) mm  = (  0.87 x 1.26  ) in
313
                'JIS_B12'                => array(   45.354,    62.362), // = (   16 x 22   ) mm  = (  0.63 x 0.87  ) in
314
                // PA Series
315
                'PA0'                    => array( 2381.102,  3174.803), // = (  840 x 1120 ) mm  = ( 33.07 x 44.09 ) in
316
                'PA1'                    => array( 1587.402,  2381.102), // = (  560 x 840  ) mm  = ( 22.05 x 33.07 ) in
317
                'PA2'                    => array( 1190.551,  1587.402), // = (  420 x 560  ) mm  = ( 16.54 x 22.05 ) in
318
                'PA3'                    => array(  793.701,  1190.551), // = (  280 x 420  ) mm  = ( 11.02 x 16.54 ) in
319
                'PA4'                    => array(  595.276,   793.701), // = (  210 x 280  ) mm  = (  8.27 x 11.02 ) in
320
                'PA5'                    => array(  396.850,   595.276), // = (  140 x 210  ) mm  = (  5.51 x 8.27  ) in
321
                'PA6'                    => array(  297.638,   396.850), // = (  105 x 140  ) mm  = (  4.13 x 5.51  ) in
322
                'PA7'                    => array(  198.425,   297.638), // = (   70 x 105  ) mm  = (  2.76 x 4.13  ) in
323
                'PA8'                    => array(  147.402,   198.425), // = (   52 x 70   ) mm  = (  2.05 x 2.76  ) in
324
                'PA9'                    => array(   99.213,   147.402), // = (   35 x 52   ) mm  = (  1.38 x 2.05  ) in
325
                'PA10'                   => array(   73.701,    99.213), // = (   26 x 35   ) mm  = (  1.02 x 1.38  ) in
326
                // Standard Photographic Print Sizes
327
                'PASSPORT_PHOTO'         => array(   99.213,   127.559), // = (   35 x 45   ) mm  = (  1.38 x 1.77  ) in
328
                'E'                      => array(  233.858,   340.157), // = (   82 x 120  ) mm  = (  3.25 x 4.72  ) in
329
                'L'                      => array(  252.283,   360.000), // = (   89 x 127  ) mm  = (  3.50 x 5.00  ) in
330
                '3R'                     => array(  252.283,   360.000), // = (   89 x 127  ) mm  = (  3.50 x 5.00  ) in
331
                'KG'                     => array(  289.134,   430.866), // = (  102 x 152  ) mm  = (  4.02 x 5.98  ) in
332
                '4R'                     => array(  289.134,   430.866), // = (  102 x 152  ) mm  = (  4.02 x 5.98  ) in
333
                '4D'                     => array(  340.157,   430.866), // = (  120 x 152  ) mm  = (  4.72 x 5.98  ) in
334
                '2L'                     => array(  360.000,   504.567), // = (  127 x 178  ) mm  = (  5.00 x 7.01  ) in
335
                '5R'                     => array(  360.000,   504.567), // = (  127 x 178  ) mm  = (  5.00 x 7.01  ) in
336
                '8P'                     => array(  430.866,   575.433), // = (  152 x 203  ) mm  = (  5.98 x 7.99  ) in
337
                '6R'                     => array(  430.866,   575.433), // = (  152 x 203  ) mm  = (  5.98 x 7.99  ) in
338
                '6P'                     => array(  575.433,   720.000), // = (  203 x 254  ) mm  = (  7.99 x 10.00 ) in
339
                '8R'                     => array(  575.433,   720.000), // = (  203 x 254  ) mm  = (  7.99 x 10.00 ) in
340
                '6PW'                    => array(  575.433,   864.567), // = (  203 x 305  ) mm  = (  7.99 x 12.01 ) in
341
                'S8R'                    => array(  575.433,   864.567), // = (  203 x 305  ) mm  = (  7.99 x 12.01 ) in
342
                '4P'                     => array(  720.000,   864.567), // = (  254 x 305  ) mm  = ( 10.00 x 12.01 ) in
343
                '10R'                    => array(  720.000,   864.567), // = (  254 x 305  ) mm  = ( 10.00 x 12.01 ) in
344
                '4PW'                    => array(  720.000,  1080.000), // = (  254 x 381  ) mm  = ( 10.00 x 15.00 ) in
345
                'S10R'                   => array(  720.000,  1080.000), // = (  254 x 381  ) mm  = ( 10.00 x 15.00 ) in
346
                '11R'                    => array(  790.866,  1009.134), // = (  279 x 356  ) mm  = ( 10.98 x 14.02 ) in
347
                'S11R'                   => array(  790.866,  1224.567), // = (  279 x 432  ) mm  = ( 10.98 x 17.01 ) in
348
                '12R'                    => array(  864.567,  1080.000), // = (  305 x 381  ) mm  = ( 12.01 x 15.00 ) in
349
                'S12R'                   => array(  864.567,  1292.598), // = (  305 x 456  ) mm  = ( 12.01 x 17.95 ) in
350
                // Common Newspaper Sizes
351
                'NEWSPAPER_BROADSHEET'   => array( 2125.984,  1700.787), // = (  750 x 600  ) mm  = ( 29.53 x 23.62 ) in
352
                'NEWSPAPER_BERLINER'     => array( 1332.283,   892.913), // = (  470 x 315  ) mm  = ( 18.50 x 12.40 ) in
353
                'NEWSPAPER_TABLOID'      => array( 1218.898,   793.701), // = (  430 x 280  ) mm  = ( 16.93 x 11.02 ) in
354
                'NEWSPAPER_COMPACT'      => array( 1218.898,   793.701), // = (  430 x 280  ) mm  = ( 16.93 x 11.02 ) in
355
                // Business Cards
356
                'CREDIT_CARD'            => array(  153.014,   242.646), // = (   54 x 86   ) mm  = (  2.13 x 3.37  ) in
357
                'BUSINESS_CARD'          => array(  153.014,   242.646), // = (   54 x 86   ) mm  = (  2.13 x 3.37  ) in
358
                'BUSINESS_CARD_ISO7810'  => array(  153.014,   242.646), // = (   54 x 86   ) mm  = (  2.13 x 3.37  ) in
359
                'BUSINESS_CARD_ISO216'   => array(  147.402,   209.764), // = (   52 x 74   ) mm  = (  2.05 x 2.91  ) in
360
                'BUSINESS_CARD_IT'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
361
                'BUSINESS_CARD_UK'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
362
                'BUSINESS_CARD_FR'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
363
                'BUSINESS_CARD_DE'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
364
                'BUSINESS_CARD_ES'       => array(  155.906,   240.945), // = (   55 x 85   ) mm  = (  2.17 x 3.35  ) in
365
                'BUSINESS_CARD_CA'       => array(  144.567,   252.283), // = (   51 x 89   ) mm  = (  2.01 x 3.50  ) in
366
                'BUSINESS_CARD_US'       => array(  144.567,   252.283), // = (   51 x 89   ) mm  = (  2.01 x 3.50  ) in
367
                'BUSINESS_CARD_JP'       => array(  155.906,   257.953), // = (   55 x 91   ) mm  = (  2.17 x 3.58  ) in
368
                'BUSINESS_CARD_HK'       => array(  153.071,   255.118), // = (   54 x 90   ) mm  = (  2.13 x 3.54  ) in
369
                'BUSINESS_CARD_AU'       => array(  155.906,   255.118), // = (   55 x 90   ) mm  = (  2.17 x 3.54  ) in
370
                'BUSINESS_CARD_DK'       => array(  155.906,   255.118), // = (   55 x 90   ) mm  = (  2.17 x 3.54  ) in
371
                'BUSINESS_CARD_SE'       => array(  155.906,   255.118), // = (   55 x 90   ) mm  = (  2.17 x 3.54  ) in
372
                'BUSINESS_CARD_RU'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
373
                'BUSINESS_CARD_CZ'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
374
                'BUSINESS_CARD_FI'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
375
                'BUSINESS_CARD_HU'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
376
                'BUSINESS_CARD_IL'       => array(  141.732,   255.118), // = (   50 x 90   ) mm  = (  1.97 x 3.54  ) in
377
                // Billboards
378
                '4SHEET'                 => array( 2880.000,  4320.000), // = ( 1016 x 1524 ) mm  = ( 40.00 x 60.00 ) in
379
                '6SHEET'                 => array( 3401.575,  5102.362), // = ( 1200 x 1800 ) mm  = ( 47.24 x 70.87 ) in
380
                '12SHEET'                => array( 8640.000,  4320.000), // = ( 3048 x 1524 ) mm  = (120.00 x 60.00 ) in
381
                '16SHEET'                => array( 5760.000,  8640.000), // = ( 2032 x 3048 ) mm  = ( 80.00 x 120.00) in
382
                '32SHEET'                => array(11520.000,  8640.000), // = ( 4064 x 3048 ) mm  = (160.00 x 120.00) in
383
                '48SHEET'                => array(17280.000,  8640.000), // = ( 6096 x 3048 ) mm  = (240.00 x 120.00) in
384
                '64SHEET'                => array(23040.000,  8640.000), // = ( 8128 x 3048 ) mm  = (320.00 x 120.00) in
385
                '96SHEET'                => array(34560.000,  8640.000), // = (12192 x 3048 ) mm  = (480.00 x 120.00) in
386
                // -- Old European Sizes
387
                // - Old Imperial English Sizes
388
                'EN_EMPEROR'             => array( 3456.000,  5184.000), // = ( 1219 x 1829 ) mm  = ( 48.00 x 72.00 ) in
389
                'EN_ANTIQUARIAN'         => array( 2232.000,  3816.000), // = (  787 x 1346 ) mm  = ( 31.00 x 53.00 ) in
390
                'EN_GRAND_EAGLE'         => array( 2070.000,  3024.000), // = (  730 x 1067 ) mm  = ( 28.75 x 42.00 ) in
391
                'EN_DOUBLE_ELEPHANT'     => array( 1926.000,  2880.000), // = (  679 x 1016 ) mm  = ( 26.75 x 40.00 ) in
392
                'EN_ATLAS'               => array( 1872.000,  2448.000), // = (  660 x 864  ) mm  = ( 26.00 x 34.00 ) in
393
                'EN_COLOMBIER'           => array( 1692.000,  2484.000), // = (  597 x 876  ) mm  = ( 23.50 x 34.50 ) in
394
                'EN_ELEPHANT'            => array( 1656.000,  2016.000), // = (  584 x 711  ) mm  = ( 23.00 x 28.00 ) in
395
                'EN_DOUBLE_DEMY'         => array( 1620.000,  2556.000), // = (  572 x 902  ) mm  = ( 22.50 x 35.50 ) in
396
                'EN_IMPERIAL'            => array( 1584.000,  2160.000), // = (  559 x 762  ) mm  = ( 22.00 x 30.00 ) in
397
                'EN_PRINCESS'            => array( 1548.000,  2016.000), // = (  546 x 711  ) mm  = ( 21.50 x 28.00 ) in
398
                'EN_CARTRIDGE'           => array( 1512.000,  1872.000), // = (  533 x 660  ) mm  = ( 21.00 x 26.00 ) in
399
                'EN_DOUBLE_LARGE_POST'   => array( 1512.000,  2376.000), // = (  533 x 838  ) mm  = ( 21.00 x 33.00 ) in
400
                'EN_ROYAL'               => array( 1440.000,  1800.000), // = (  508 x 635  ) mm  = ( 20.00 x 25.00 ) in
401
                'EN_SHEET'               => array( 1404.000,  1692.000), // = (  495 x 597  ) mm  = ( 19.50 x 23.50 ) in
402
                'EN_HALF_POST'           => array( 1404.000,  1692.000), // = (  495 x 597  ) mm  = ( 19.50 x 23.50 ) in
403
                'EN_SUPER_ROYAL'         => array( 1368.000,  1944.000), // = (  483 x 686  ) mm  = ( 19.00 x 27.00 ) in
404
                'EN_DOUBLE_POST'         => array( 1368.000,  2196.000), // = (  483 x 775  ) mm  = ( 19.00 x 30.50 ) in
405
                'EN_MEDIUM'              => array( 1260.000,  1656.000), // = (  445 x 584  ) mm  = ( 17.50 x 23.00 ) in
406
                'EN_DEMY'                => array( 1260.000,  1620.000), // = (  445 x 572  ) mm  = ( 17.50 x 22.50 ) in
407
                'EN_LARGE_POST'          => array( 1188.000,  1512.000), // = (  419 x 533  ) mm  = ( 16.50 x 21.00 ) in
408
                'EN_COPY_DRAUGHT'        => array( 1152.000,  1440.000), // = (  406 x 508  ) mm  = ( 16.00 x 20.00 ) in
409
                'EN_POST'                => array( 1116.000,  1386.000), // = (  394 x 489  ) mm  = ( 15.50 x 19.25 ) in
410
                'EN_CROWN'               => array( 1080.000,  1440.000), // = (  381 x 508  ) mm  = ( 15.00 x 20.00 ) in
411
                'EN_PINCHED_POST'        => array( 1062.000,  1332.000), // = (  375 x 470  ) mm  = ( 14.75 x 18.50 ) in
412
                'EN_BRIEF'               => array(  972.000,  1152.000), // = (  343 x 406  ) mm  = ( 13.50 x 16.00 ) in
413
                'EN_FOOLSCAP'            => array(  972.000,  1224.000), // = (  343 x 432  ) mm  = ( 13.50 x 17.00 ) in
414
                'EN_SMALL_FOOLSCAP'      => array(  954.000,  1188.000), // = (  337 x 419  ) mm  = ( 13.25 x 16.50 ) in
415
                'EN_POTT'                => array(  900.000,  1080.000), // = (  318 x 381  ) mm  = ( 12.50 x 15.00 ) in
416
                // - Old Imperial Belgian Sizes
417
                'BE_GRAND_AIGLE'         => array( 1984.252,  2948.031), // = (  700 x 1040 ) mm  = ( 27.56 x 40.94 ) in
418
                'BE_COLOMBIER'           => array( 1757.480,  2409.449), // = (  620 x 850  ) mm  = ( 24.41 x 33.46 ) in
419
                'BE_DOUBLE_CARRE'        => array( 1757.480,  2607.874), // = (  620 x 920  ) mm  = ( 24.41 x 36.22 ) in
420
                'BE_ELEPHANT'            => array( 1746.142,  2182.677), // = (  616 x 770  ) mm  = ( 24.25 x 30.31 ) in
421
                'BE_PETIT_AIGLE'         => array( 1700.787,  2381.102), // = (  600 x 840  ) mm  = ( 23.62 x 33.07 ) in
422
                'BE_GRAND_JESUS'         => array( 1559.055,  2069.291), // = (  550 x 730  ) mm  = ( 21.65 x 28.74 ) in
423
                'BE_JESUS'               => array( 1530.709,  2069.291), // = (  540 x 730  ) mm  = ( 21.26 x 28.74 ) in
424
                'BE_RAISIN'              => array( 1417.323,  1842.520), // = (  500 x 650  ) mm  = ( 19.69 x 25.59 ) in
425
                'BE_GRAND_MEDIAN'        => array( 1303.937,  1714.961), // = (  460 x 605  ) mm  = ( 18.11 x 23.82 ) in
426
                'BE_DOUBLE_POSTE'        => array( 1233.071,  1601.575), // = (  435 x 565  ) mm  = ( 17.13 x 22.24 ) in
427
                'BE_COQUILLE'            => array( 1218.898,  1587.402), // = (  430 x 560  ) mm  = ( 16.93 x 22.05 ) in
428
                'BE_PETIT_MEDIAN'        => array( 1176.378,  1502.362), // = (  415 x 530  ) mm  = ( 16.34 x 20.87 ) in
429
                'BE_RUCHE'               => array( 1020.472,  1303.937), // = (  360 x 460  ) mm  = ( 14.17 x 18.11 ) in
430
                'BE_PROPATRIA'           => array(  977.953,  1218.898), // = (  345 x 430  ) mm  = ( 13.58 x 16.93 ) in
431
                'BE_LYS'                 => array(  898.583,  1125.354), // = (  317 x 397  ) mm  = ( 12.48 x 15.63 ) in
432
                'BE_POT'                 => array(  870.236,  1088.504), // = (  307 x 384  ) mm  = ( 12.09 x 15.12 ) in
433
                'BE_ROSETTE'             => array(  765.354,   983.622), // = (  270 x 347  ) mm  = ( 10.63 x 13.66 ) in
434
                // - Old Imperial French Sizes
435
                'FR_UNIVERS'             => array( 2834.646,  3685.039), // = ( 1000 x 1300 ) mm  = ( 39.37 x 51.18 ) in
436
                'FR_DOUBLE_COLOMBIER'    => array( 2551.181,  3571.654), // = (  900 x 1260 ) mm  = ( 35.43 x 49.61 ) in
437
                'FR_GRANDE_MONDE'        => array( 2551.181,  3571.654), // = (  900 x 1260 ) mm  = ( 35.43 x 49.61 ) in
438
                'FR_DOUBLE_SOLEIL'       => array( 2267.717,  3401.575), // = (  800 x 1200 ) mm  = ( 31.50 x 47.24 ) in
439
                'FR_DOUBLE_JESUS'        => array( 2154.331,  3174.803), // = (  760 x 1120 ) mm  = ( 29.92 x 44.09 ) in
440
                'FR_GRAND_AIGLE'         => array( 2125.984,  3004.724), // = (  750 x 1060 ) mm  = ( 29.53 x 41.73 ) in
441
                'FR_PETIT_AIGLE'         => array( 1984.252,  2664.567), // = (  700 x 940  ) mm  = ( 27.56 x 37.01 ) in
442
                'FR_DOUBLE_RAISIN'       => array( 1842.520,  2834.646), // = (  650 x 1000 ) mm  = ( 25.59 x 39.37 ) in
443
                'FR_JOURNAL'             => array( 1842.520,  2664.567), // = (  650 x 940  ) mm  = ( 25.59 x 37.01 ) in
444
                'FR_COLOMBIER_AFFICHE'   => array( 1785.827,  2551.181), // = (  630 x 900  ) mm  = ( 24.80 x 35.43 ) in
445
                'FR_DOUBLE_CAVALIER'     => array( 1757.480,  2607.874), // = (  620 x 920  ) mm  = ( 24.41 x 36.22 ) in
446
                'FR_CLOCHE'              => array( 1700.787,  2267.717), // = (  600 x 800  ) mm  = ( 23.62 x 31.50 ) in
447
                'FR_SOLEIL'              => array( 1700.787,  2267.717), // = (  600 x 800  ) mm  = ( 23.62 x 31.50 ) in
448
                'FR_DOUBLE_CARRE'        => array( 1587.402,  2551.181), // = (  560 x 900  ) mm  = ( 22.05 x 35.43 ) in
449
                'FR_DOUBLE_COQUILLE'     => array( 1587.402,  2494.488), // = (  560 x 880  ) mm  = ( 22.05 x 34.65 ) in
450
                'FR_JESUS'               => array( 1587.402,  2154.331), // = (  560 x 760  ) mm  = ( 22.05 x 29.92 ) in
451
                'FR_RAISIN'              => array( 1417.323,  1842.520), // = (  500 x 650  ) mm  = ( 19.69 x 25.59 ) in
452
                'FR_CAVALIER'            => array( 1303.937,  1757.480), // = (  460 x 620  ) mm  = ( 18.11 x 24.41 ) in
453
                'FR_DOUBLE_COURONNE'     => array( 1303.937,  2040.945), // = (  460 x 720  ) mm  = ( 18.11 x 28.35 ) in
454
                'FR_CARRE'               => array( 1275.591,  1587.402), // = (  450 x 560  ) mm  = ( 17.72 x 22.05 ) in
455
                'FR_COQUILLE'            => array( 1247.244,  1587.402), // = (  440 x 560  ) mm  = ( 17.32 x 22.05 ) in
456
                'FR_DOUBLE_TELLIERE'     => array( 1247.244,  1927.559), // = (  440 x 680  ) mm  = ( 17.32 x 26.77 ) in
457
                'FR_DOUBLE_CLOCHE'       => array( 1133.858,  1700.787), // = (  400 x 600  ) mm  = ( 15.75 x 23.62 ) in
458
                'FR_DOUBLE_POT'          => array( 1133.858,  1757.480), // = (  400 x 620  ) mm  = ( 15.75 x 24.41 ) in
459
                'FR_ECU'                 => array( 1133.858,  1474.016), // = (  400 x 520  ) mm  = ( 15.75 x 20.47 ) in
460
                'FR_COURONNE'            => array( 1020.472,  1303.937), // = (  360 x 460  ) mm  = ( 14.17 x 18.11 ) in
461
                'FR_TELLIERE'            => array(  963.780,  1247.244), // = (  340 x 440  ) mm  = ( 13.39 x 17.32 ) in
462
                'FR_POT'                 => array(  878.740,  1133.858), // = (  310 x 400  ) mm  = ( 12.20 x 15.75 ) in
463
        );
464

    
465
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
466

    
467
        /**
468
         * Return the current TCPDF version.
469
         * @return TCPDF version string
470
         * @since 5.9.012 (2010-11-10)
471
         * @public static
472
         */
473
        public static function getTCPDFVersion() {
474
                return self::$tcpdf_version;
475
        }
476

    
477
        /**
478
         * Return the current TCPDF producer.
479
         * @return TCPDF producer string
480
         * @since 6.0.000 (2013-03-16)
481
         * @public static
482
         */
483
        public static function getTCPDFProducer() {
484
                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";
485
        }
486

    
487
        /**
488
         * Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
489
         * @param $mqr (boolean) FALSE for off, TRUE for on.
490
         * @since 4.6.025 (2009-08-17)
491
         * @public static
492
         */
493
        public static function set_mqr($mqr) {
494
                if (!defined('PHP_VERSION_ID')) {
495
                        $version = PHP_VERSION;
496
                        define('PHP_VERSION_ID', (($version[0] * 10000) + ($version[2] * 100) + $version[4]));
497
                }
498
                if (PHP_VERSION_ID < 50300) {
499
                        @set_magic_quotes_runtime($mqr);
500
                }
501
        }
502

    
503
        /**
504
         * Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
505
         * @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
506
         * @since 4.6.025 (2009-08-17)
507
         * @public static
508
         */
509
        public static function get_mqr() {
510
                if (!defined('PHP_VERSION_ID')) {
511
                        $version = PHP_VERSION;
512
                        define('PHP_VERSION_ID', (($version[0] * 10000) + ($version[2] * 100) + $version[4]));
513
                }
514
                if (PHP_VERSION_ID < 50300) {
515
                        return @get_magic_quotes_runtime();
516
                }
517
                return 0;
518
        }
519

    
520
        /**
521
         * Get page dimensions from format name.
522
         * @param $format (mixed) The format name @see self::$page_format<ul>
523
         * @return array containing page width and height in points
524
         * @since 5.0.010 (2010-05-17)
525
         * @public static
526
         */
527
        public static function getPageSizeFromFormat($format) {
528
                if (isset(self::$page_formats[$format])) {
529
                        return self::$page_formats[$format];
530
                }
531
                return self::$page_formats['A4'];
532
        }
533

    
534
        /**
535
         * Set page boundaries.
536
         * @param $page (int) page number
537
         * @param $type (string) valid values are: <ul><li>'MediaBox' : the boundaries of the physical medium on which the page shall be displayed or printed;</li><li>'CropBox' : the visible region of default user space;</li><li>'BleedBox' : the region to which the contents of the page shall be clipped when output in a production environment;</li><li>'TrimBox' : the intended dimensions of the finished page after trimming;</li><li>'ArtBox' : the page's meaningful content (including potential white space).</li></ul>
538
         * @param $llx (float) lower-left x coordinate in user units.
539
         * @param $lly (float) lower-left y coordinate in user units.
540
         * @param $urx (float) upper-right x coordinate in user units.
541
         * @param $ury (float) upper-right y coordinate in user units.
542
         * @param $points (boolean) If true uses user units as unit of measure, otherwise uses PDF points.
543
         * @param $k (float) Scale factor (number of points in user unit).
544
         * @param $pagedim (array) Array of page dimensions.
545
         * @return pagedim array of page dimensions.
546
         * @since 5.0.010 (2010-05-17)
547
         * @public static
548
         */
549
        public static function setPageBoxes($page, $type, $llx, $lly, $urx, $ury, $points=false, $k, $pagedim=array()) {
550
                if (!isset($pagedim[$page])) {
551
                        // initialize array
552
                        $pagedim[$page] = array();
553
                }
554
                if (!in_array($type, self::$pageboxes)) {
555
                        return;
556
                }
557
                if ($points) {
558
                        $k = 1;
559
                }
560
                $pagedim[$page][$type]['llx'] = ($llx * $k);
561
                $pagedim[$page][$type]['lly'] = ($lly * $k);
562
                $pagedim[$page][$type]['urx'] = ($urx * $k);
563
                $pagedim[$page][$type]['ury'] = ($ury * $k);
564
                return $pagedim;
565
        }
566

    
567
        /**
568
         * Swap X and Y coordinates of page boxes (change page boxes orientation).
569
         * @param $page (int) page number
570
         * @param $pagedim (array) Array of page dimensions.
571
         * @return pagedim array of page dimensions.
572
         * @since 5.0.010 (2010-05-17)
573
         * @public static
574
         */
575
        public static function swapPageBoxCoordinates($page, $pagedim) {
576
                foreach (self::$pageboxes as $type) {
577
                        // swap X and Y coordinates
578
                        if (isset($pagedim[$page][$type])) {
579
                                $tmp = $pagedim[$page][$type]['llx'];
580
                                $pagedim[$page][$type]['llx'] = $pagedim[$page][$type]['lly'];
581
                                $pagedim[$page][$type]['lly'] = $tmp;
582
                                $tmp = $pagedim[$page][$type]['urx'];
583
                                $pagedim[$page][$type]['urx'] = $pagedim[$page][$type]['ury'];
584
                                $pagedim[$page][$type]['ury'] = $tmp;
585
                        }
586
                }
587
                return $pagedim;
588
        }
589

    
590
        /**
591
         * Get the canonical page layout mode.
592
         * @param $layout (string) The page layout. Possible values are:<ul><li>SinglePage Display one page at a time</li><li>OneColumn Display the pages in one column</li><li>TwoColumnLeft Display the pages in two columns, with odd-numbered pages on the left</li><li>TwoColumnRight Display the pages in two columns, with odd-numbered pages on the right</li><li>TwoPageLeft (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the left</li><li>TwoPageRight (PDF 1.5) Display the pages two at a time, with odd-numbered pages on the right</li></ul>
593
         * @return (string) Canonical page layout name.
594
         * @public static
595
         */
596
        public static function getPageLayoutMode($layout='SinglePage') {
597
                switch ($layout) {
598
                        case 'default':
599
                        case 'single':
600
                        case 'SinglePage': {
601
                                $layout_mode = 'SinglePage';
602
                                break;
603
                        }
604
                        case 'continuous':
605
                        case 'OneColumn': {
606
                                $layout_mode = 'OneColumn';
607
                                break;
608
                        }
609
                        case 'two':
610
                        case 'TwoColumnLeft': {
611
                                $layout_mode = 'TwoColumnLeft';
612
                                break;
613
                        }
614
                        case 'TwoColumnRight': {
615
                                $layout_mode = 'TwoColumnRight';
616
                                break;
617
                        }
618
                        case 'TwoPageLeft': {
619
                                $layout_mode = 'TwoPageLeft';
620
                                break;
621
                        }
622
                        case 'TwoPageRight': {
623
                                $layout_mode = 'TwoPageRight';
624
                                break;
625
                        }
626
                        default: {
627
                                $layout_mode = 'SinglePage';
628
                        }
629
                }
630
                return $layout_mode;
631
        }
632

    
633
        /**
634
         * Get the canonical page layout mode.
635
         * @param $mode (string) A name object specifying how the document should be displayed when opened:<ul><li>UseNone Neither document outline nor thumbnail images visible</li><li>UseOutlines Document outline visible</li><li>UseThumbs Thumbnail images visible</li><li>FullScreen Full-screen mode, with no menu bar, window controls, or any other window visible</li><li>UseOC (PDF 1.5) Optional content group panel visible</li><li>UseAttachments (PDF 1.6) Attachments panel visible</li></ul>
636
         * @return (string) Canonical page mode name.
637
         * @public static
638
         */
639
        public static function getPageMode($mode='UseNone') {
640
                switch ($mode) {
641
                        case 'UseNone': {
642
                                $page_mode = 'UseNone';
643
                                break;
644
                        }
645
                        case 'UseOutlines': {
646
                                $page_mode = 'UseOutlines';
647
                                break;
648
                        }
649
                        case 'UseThumbs': {
650
                                $page_mode = 'UseThumbs';
651
                                break;
652
                        }
653
                        case 'FullScreen': {
654
                                $page_mode = 'FullScreen';
655
                                break;
656
                        }
657
                        case 'UseOC': {
658
                                $page_mode = 'UseOC';
659
                                break;
660
                        }
661
                        case '': {
662
                                $page_mode = 'UseAttachments';
663
                                break;
664
                        }
665
                        default: {
666
                                $page_mode = 'UseNone';
667
                        }
668
                }
669
                return $page_mode;
670
        }
671

    
672
        /**
673
         * Check if the URL exist.
674
         * @param $url (string) URL to check.
675
         * @return Boolean true if the URl exist, false otherwise.
676
         * @since 5.9.204 (2013-01-28)
677
         * @public static
678
         */
679
        public static function isValidURL($url) {
680
                $headers = @get_headers($url);
681
            return (strpos($headers[0], '200') !== false);
682
        }
683

    
684
        /**
685
         * Removes SHY characters from text.
686
         * Unicode Data:<ul>
687
         * <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
688
         * <li>HTML Entity (decimal): "&amp;#173;"</li>
689
         * <li>HTML Entity (hex): "&amp;#xad;"</li>
690
         * <li>HTML Entity (named): "&amp;shy;"</li>
691
         * <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
692
         * <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
693
         * <li>UTF-8 character: chr(194).chr(173)</li>
694
         * </ul>
695
         * @param $txt (string) input string
696
         * @param $unicode (boolean) True if we are in unicode mode, false otherwise.
697
         * @return string without SHY characters.
698
         * @since (4.5.019) 2009-02-28
699
         * @public static
700
         */
701
        public static function removeSHY($txt='', $unicode=true) {
702
                $txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
703
                if (!$unicode) {
704
                        $txt = preg_replace('/([\\xad]{1})/', '', $txt);
705
                }
706
                return $txt;
707
        }
708

    
709

    
710
        /**
711
         * Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
712
         * @param $brd (mixed) Indicates if borders must be drawn around the cell block. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
713
         * @param $position (string) multicell position: 'start', 'middle', 'end'
714
         * @param $opencell (boolean) True when the cell is left open at the page bottom, false otherwise.
715
         * @return border mode array
716
         * @since 4.4.002 (2008-12-09)
717
         * @public static
718
         */
719
        public static function getBorderMode($brd, $position='start', $opencell=true) {
720
                if ((!$opencell) OR empty($brd)) {
721
                        return $brd;
722
                }
723
                if ($brd == 1) {
724
                        $brd = 'LTRB';
725
                }
726
                if (is_string($brd)) {
727
                        // convert string to array
728
                        $slen = strlen($brd);
729
                        $newbrd = array();
730
                        for ($i = 0; $i < $slen; ++$i) {
731
                                $newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
732
                        }
733
                        $brd = $newbrd;
734
                }
735
                foreach ($brd as $border => $style) {
736
                        switch ($position) {
737
                                case 'start': {
738
                                        if (strpos($border, 'B') !== false) {
739
                                                // remove bottom line
740
                                                $newkey = str_replace('B', '', $border);
741
                                                if (strlen($newkey) > 0) {
742
                                                        $brd[$newkey] = $style;
743
                                                }
744
                                                unset($brd[$border]);
745
                                        }
746
                                        break;
747
                                }
748
                                case 'middle': {
749
                                        if (strpos($border, 'B') !== false) {
750
                                                // remove bottom line
751
                                                $newkey = str_replace('B', '', $border);
752
                                                if (strlen($newkey) > 0) {
753
                                                        $brd[$newkey] = $style;
754
                                                }
755
                                                unset($brd[$border]);
756
                                                $border = $newkey;
757
                                        }
758
                                        if (strpos($border, 'T') !== false) {
759
                                                // remove bottom line
760
                                                $newkey = str_replace('T', '', $border);
761
                                                if (strlen($newkey) > 0) {
762
                                                        $brd[$newkey] = $style;
763
                                                }
764
                                                unset($brd[$border]);
765
                                        }
766
                                        break;
767
                                }
768
                                case 'end': {
769
                                        if (strpos($border, 'T') !== false) {
770
                                                // remove bottom line
771
                                                $newkey = str_replace('T', '', $border);
772
                                                if (strlen($newkey) > 0) {
773
                                                        $brd[$newkey] = $style;
774
                                                }
775
                                                unset($brd[$border]);
776
                                        }
777
                                        break;
778
                                }
779
                        }
780
                }
781
                return $brd;
782
        }
783

    
784
        /**
785
         * Determine whether a string is empty.
786
         * @param $str (string) string to be checked
787
         * @return boolean true if string is empty
788
         * @since 4.5.044 (2009-04-16)
789
         * @public static
790
         */
791
        public static function empty_string($str) {
792
                return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
793
        }
794

    
795
        /**
796
         * Returns a temporary filename for caching object on filesystem.
797
         * @param $type (string) Type of file (name of the subdir on the tcpdf cache folder).
798
         * @param $file_id (string) TCPDF file_id.
799
         * @return string filename.
800
         * @since 4.5.000 (2008-12-31)
801
         * @public static
802
         */
803
        public static function getObjFilename($type='tmp', $file_id='') {
804
                return tempnam(K_PATH_CACHE, '__tcpdf_'.$file_id.'_'.$type.'_'.md5(TCPDF_STATIC::getRandomSeed()).'_');
805
        }
806

    
807
        /**
808
         * Add "\" before "\", "(" and ")"
809
         * @param $s (string) string to escape.
810
         * @return string escaped string.
811
         * @public static
812
         */
813
        public static function _escape($s) {
814
                // the chr(13) substitution fixes the Bugs item #1421290.
815
                return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
816
        }
817

    
818
        /**
819
        * Escape some special characters (&lt; &gt; &amp;) for XML output.
820
        * @param $str (string) Input string to convert.
821
        * @return converted string
822
        * @since 5.9.121 (2011-09-28)
823
         * @public static
824
         */
825
        public static function _escapeXML($str) {
826
                $replaceTable = array("\0" => '', '&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
827
                $str = strtr($str, $replaceTable);
828
                return $str;
829
        }
830

    
831
        /**
832
         * Creates a copy of a class object
833
         * @param $object (object) class object to be cloned
834
         * @return cloned object
835
         * @since 4.5.029 (2009-03-19)
836
         * @public static
837
         */
838
        public static function objclone($object) {
839
                if (($object instanceof Imagick) AND (version_compare(phpversion('imagick'), '3.0.1') !== 1)) {
840
                        // on the versions after 3.0.1 the clone() method was deprecated in favour of clone keyword
841
                        return @$object->clone();
842
                }
843
                return @clone($object);
844
        }
845

    
846
        /**
847
         * Output input data and compress it if possible.
848
         * @param $data (string) Data to output.
849
         * @param $length (int) Data length in bytes.
850
         * @since 5.9.086
851
         * @public static
852
         */
853
        public static function sendOutputData($data, $length) {
854
                if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
855
                        // the content length may vary if the server is using compression
856
                        header('Content-Length: '.$length);
857
                }
858
                echo $data;
859
        }
860

    
861
        /**
862
         * Replace page number aliases with number.
863
         * @param $page (string) Page content.
864
         * @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
865
         * @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
866
         * @return replaced page content and updated $diff parameter as array.
867
         * @public static
868
         */
869
        public static function replacePageNumAliases($page, $replace, $diff=0) {
870
                foreach ($replace as $rep) {
871
                        foreach ($rep[3] as $a) {
872
                                if (strpos($page, $a) !== false) {
873
                                        $page = str_replace($a, $rep[0], $page);
874
                                        $diff += ($rep[2] - $rep[1]);
875
                                }
876
                        }
877
                }
878
                return array($page, $diff);
879
        }
880

    
881
        /**
882
         * Returns timestamp in seconds from formatted date-time.
883
         * @param $date (string) Formatted date-time.
884
         * @return int seconds.
885
         * @since 5.9.152 (2012-03-23)
886
         * @public static
887
         */
888
        public static function getTimestamp($date) {
889
                if (($date[0] == 'D') AND ($date[1] == ':')) {
890
                        // remove date prefix if present
891
                        $date = substr($date, 2);
892
                }
893
                return strtotime($date);
894
        }
895

    
896
        /**
897
         * Returns a formatted date-time.
898
         * @param $time (int) Time in seconds.
899
         * @return string escaped date string.
900
         * @since 5.9.152 (2012-03-23)
901
         * @public static
902
         */
903
        public static function getFormattedDate($time) {
904
                return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
905
        }
906

    
907
        /**
908
         * Get ULONG from string (Big Endian 32-bit unsigned integer).
909
         * @param $str (string) string from where to extract value
910
         * @param $offset (int) point from where to read the data
911
         * @return int 32 bit value
912
         * @author Nicola Asuni
913
         * @since 5.2.000 (2010-06-02)
914
         * @public static
915
         */
916
        public static function _getULONG($str, $offset) {
917
                $v = unpack('Ni', substr($str, $offset, 4));
918
                return $v['i'];
919
        }
920

    
921
        /**
922
         * Get USHORT from string (Big Endian 16-bit unsigned integer).
923
         * @param $str (string) string from where to extract value
924
         * @param $offset (int) point from where to read the data
925
         * @return int 16 bit value
926
         * @author Nicola Asuni
927
         * @since 5.2.000 (2010-06-02)
928
         * @public static
929
         */
930
        public static function _getUSHORT($str, $offset) {
931
                $v = unpack('ni', substr($str, $offset, 2));
932
                return $v['i'];
933
        }
934

    
935
        /**
936
         * Get SHORT from string (Big Endian 16-bit signed integer).
937
         * @param $str (string) String from where to extract value.
938
         * @param $offset (int) Point from where to read the data.
939
         * @return int 16 bit value
940
         * @author Nicola Asuni
941
         * @since 5.2.000 (2010-06-02)
942
         * @public static
943
         */
944
        public static function _getSHORT($str, $offset) {
945
                $v = unpack('si', substr($str, $offset, 2));
946
                return $v['i'];
947
        }
948

    
949
        /**
950
         * Get FWORD from string (Big Endian 16-bit signed integer).
951
         * @param $str (string) String from where to extract value.
952
         * @param $offset (int) Point from where to read the data.
953
         * @return int 16 bit value
954
         * @author Nicola Asuni
955
         * @since 5.9.123 (2011-09-30)
956
         * @public static
957
         */
958
        public static function _getFWORD($str, $offset) {
959
                $v = self::_getUSHORT($str, $offset);
960
                if ($v > 0x7fff) {
961
                        $v -= 0x10000;
962
                }
963
                return $v;
964
        }
965

    
966
        /**
967
         * Get UFWORD from string (Big Endian 16-bit unsigned integer).
968
         * @param $str (string) string from where to extract value
969
         * @param $offset (int) point from where to read the data
970
         * @return int 16 bit value
971
         * @author Nicola Asuni
972
         * @since 5.9.123 (2011-09-30)
973
         * @public static
974
         */
975
        public static function _getUFWORD($str, $offset) {
976
                $v = self::_getUSHORT($str, $offset);
977
                return $v;
978
        }
979

    
980
        /**
981
         * Get FIXED from string (32-bit signed fixed-point number (16.16).
982
         * @param $str (string) string from where to extract value
983
         * @param $offset (int) point from where to read the data
984
         * @return int 16 bit value
985
         * @author Nicola Asuni
986
         * @since 5.9.123 (2011-09-30)
987
         * @public static
988
         */
989
        public static function _getFIXED($str, $offset) {
990
                // mantissa
991
                $m = self::_getFWORD($str, $offset);
992
                // fraction
993
                $f = self::_getUSHORT($str, ($offset + 2));
994
                $v = floatval(''.$m.'.'.$f.'');
995
                return $v;
996
        }
997

    
998
        /**
999
         * Get BYTE from string (8-bit unsigned integer).
1000
         * @param $str (string) String from where to extract value.
1001
         * @param $offset (int) Point from where to read the data.
1002
         * @return int 8 bit value
1003
         * @author Nicola Asuni
1004
         * @since 5.2.000 (2010-06-02)
1005
         * @public static
1006
         */
1007
        public static function _getBYTE($str, $offset) {
1008
                $v = unpack('Ci', substr($str, $offset, 1));
1009
                return $v['i'];
1010
        }
1011
        /**
1012
         * Binary-safe and URL-safe file read.
1013
         * 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.
1014
         * @param $handle (resource)
1015
         * @param $length (int)
1016
         * @return Returns the read string or FALSE in case of error.
1017
         * @author Nicola Asuni
1018
         * @since 4.5.027 (2009-03-16)
1019
         * @public static
1020
         */
1021
        public static function rfread($handle, $length) {
1022
                $data = fread($handle, $length);
1023
                if ($data === false) {
1024
                        return false;
1025
                }
1026
                $rest = ($length - strlen($data));
1027
                if ($rest > 0) {
1028
                        $data .= self::rfread($handle, $rest);
1029
                }
1030
                return $data;
1031
        }
1032

    
1033
        /**
1034
         * Read a 4-byte (32 bit) integer from file.
1035
         * @param $f (string) file name.
1036
         * @return 4-byte integer
1037
         * @public static
1038
         */
1039
        public static function _freadint($f) {
1040
                $a = unpack('Ni', fread($f, 4));
1041
                return $a['i'];
1042
        }
1043

    
1044
        /**
1045
         * Returns a string containing random data to be used as a seed for encryption methods.
1046
         * @param $seed (string) starting seed value
1047
         * @return string containing random data
1048
         * @author Nicola Asuni
1049
         * @since 5.9.006 (2010-10-19)
1050
         * @public static
1051
         */
1052
        public static function getRandomSeed($seed='') {
1053
                $rnd = uniqid(rand().microtime(true), true);
1054
                if (function_exists('posix_getpid')) {
1055
                        $rnd .= posix_getpid();
1056
                }
1057
                if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
1058
                        // this is not used on windows systems because it is very slow for a know bug
1059
                        $rnd .= openssl_random_pseudo_bytes(512);
1060
                } else {
1061
                        for ($i = 0; $i < 23; ++$i) {
1062
                                $rnd .= uniqid('', true);
1063
                        }
1064
                }
1065
                return $rnd.$seed.__FILE__.serialize($_SERVER).microtime(true);
1066
        }
1067

    
1068
        /**
1069
         * Encrypts a string using MD5 and returns it's value as a binary string.
1070
         * @param $str (string) input string
1071
         * @return String MD5 encrypted binary string
1072
         * @since 2.0.000 (2008-01-02)
1073
         * @public static
1074
         */
1075
        public static function _md5_16($str) {
1076
                return pack('H*', md5($str));
1077
        }
1078

    
1079
        /**
1080
         * Returns the input text exrypted using AES algorithm and the specified key.
1081
         * This method requires mcrypt.
1082
         * @param $key (string) encryption key
1083
         * @param $text (String) input text to be encrypted
1084
         * @return String encrypted text
1085
         * @author Nicola Asuni
1086
         * @since 5.0.005 (2010-05-11)
1087
         * @public static
1088
         */
1089
        public static function _AES($key, $text) {
1090
                // padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
1091
                $padding = 16 - (strlen($text) % 16);
1092
                $text .= str_repeat(chr($padding), $padding);
1093
                $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
1094
                $text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
1095
                $text = $iv.$text;
1096
                return $text;
1097
        }
1098

    
1099
        /**
1100
         * Returns the input text encrypted using RC4 algorithm and the specified key.
1101
         * RC4 is the standard encryption algorithm used in PDF format
1102
         * @param $key (string) Encryption key.
1103
         * @param $text (String) Input text to be encrypted.
1104
         * @param $last_enc_key (String) Reference to last RC4 key encrypted.
1105
         * @param $last_enc_key_c (String) Reference to last RC4 computed key.
1106
         * @return String encrypted text
1107
         * @since 2.0.000 (2008-01-02)
1108
         * @author Klemen Vodopivec, Nicola Asuni
1109
         * @public static
1110
         */
1111
        public static function _RC4($key, $text, &$last_enc_key, &$last_enc_key_c) {
1112
                if (function_exists('mcrypt_encrypt') AND ($out = @mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
1113
                        // try to use mcrypt function if exist
1114
                        return $out;
1115
                }
1116
                if ($last_enc_key != $key) {
1117
                        $k = str_repeat($key, ((256 / strlen($key)) + 1));
1118
                        $rc4 = range(0, 255);
1119
                        $j = 0;
1120
                        for ($i = 0; $i < 256; ++$i) {
1121
                                $t = $rc4[$i];
1122
                                $j = ($j + $t + ord($k[$i])) % 256;
1123
                                $rc4[$i] = $rc4[$j];
1124
                                $rc4[$j] = $t;
1125
                        }
1126
                        $last_enc_key = $key;
1127
                        $last_enc_key_c = $rc4;
1128
                } else {
1129
                        $rc4 = $last_enc_key_c;
1130
                }
1131
                $len = strlen($text);
1132
                $a = 0;
1133
                $b = 0;
1134
                $out = '';
1135
                for ($i = 0; $i < $len; ++$i) {
1136
                        $a = ($a + 1) % 256;
1137
                        $t = $rc4[$a];
1138
                        $b = ($b + $t) % 256;
1139
                        $rc4[$a] = $rc4[$b];
1140
                        $rc4[$b] = $t;
1141
                        $k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
1142
                        $out .= chr(ord($text[$i]) ^ $k);
1143
                }
1144
                return $out;
1145
        }
1146

    
1147
        /**
1148
         * Return the permission code used on encryption (P value).
1149
         * @param $permissions (Array) the set of permissions (specify the ones you want to block).
1150
         * @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
1151
         * @since 5.0.005 (2010-05-12)
1152
         * @author Nicola Asuni
1153
         * @public static
1154
         */
1155
        public static function getUserPermissionCode($permissions, $mode=0) {
1156
                $options = array(
1157
                        'owner' => 2, // bit 2 -- inverted logic: cleared by default
1158
                        'print' => 4, // bit 3
1159
                        'modify' => 8, // bit 4
1160
                        'copy' => 16, // bit 5
1161
                        'annot-forms' => 32, // bit 6
1162
                        'fill-forms' => 256, // bit 9
1163
                        'extract' => 512, // bit 10
1164
                        'assemble' => 1024,// bit 11
1165
                        'print-high' => 2048 // bit 12
1166
                        );
1167
                $protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
1168
                foreach ($permissions as $permission) {
1169
                        if (isset($options[$permission])) {
1170
                                if (($mode > 0) OR ($options[$permission] <= 32)) {
1171
                                        // set only valid permissions
1172
                                        if ($options[$permission] == 2) {
1173
                                                // the logic for bit 2 is inverted (cleared by default)
1174
                                                $protection += $options[$permission];
1175
                                        } else {
1176
                                                $protection -= $options[$permission];
1177
                                        }
1178
                                }
1179
                        }
1180
                }
1181
                return $protection;
1182
        }
1183

    
1184
        /**
1185
         * Convert hexadecimal string to string
1186
         * @param $bs (string) byte-string to convert
1187
         * @return String
1188
         * @since 5.0.005 (2010-05-12)
1189
         * @author Nicola Asuni
1190
         * @public static
1191
         */
1192
        public static function convertHexStringToString($bs) {
1193
                $string = ''; // string to be returned
1194
                $bslength = strlen($bs);
1195
                if (($bslength % 2) != 0) {
1196
                        // padding
1197
                        $bs .= '0';
1198
                        ++$bslength;
1199
                }
1200
                for ($i = 0; $i < $bslength; $i += 2) {
1201
                        $string .= chr(hexdec($bs[$i].$bs[($i + 1)]));
1202
                }
1203
                return $string;
1204
        }
1205

    
1206
        /**
1207
         * Convert string to hexadecimal string (byte string)
1208
         * @param $s (string) string to convert
1209
         * @return byte string
1210
         * @since 5.0.010 (2010-05-17)
1211
         * @author Nicola Asuni
1212
         * @public static
1213
         */
1214
        public static function convertStringToHexString($s) {
1215
                $bs = '';
1216
                $chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
1217
                foreach ($chars as $c) {
1218
                        $bs .= sprintf('%02s', dechex(ord($c)));
1219
                }
1220
                return $bs;
1221
        }
1222

    
1223
        /**
1224
         * Convert encryption P value to a string of bytes, low-order byte first.
1225
         * @param $protection (string) 32bit encryption permission value (P value)
1226
         * @return String
1227
         * @since 5.0.005 (2010-05-12)
1228
         * @author Nicola Asuni
1229
         * @public static
1230
         */
1231
        public static function getEncPermissionsString($protection) {
1232
                $binprot = sprintf('%032b', $protection);
1233
                $str = chr(bindec(substr($binprot, 24, 8)));
1234
                $str .= chr(bindec(substr($binprot, 16, 8)));
1235
                $str .= chr(bindec(substr($binprot, 8, 8)));
1236
                $str .= chr(bindec(substr($binprot, 0, 8)));
1237
                return $str;
1238
        }
1239

    
1240
        /**
1241
         * Encode a name object.
1242
         * @param $name (string) Name object to encode.
1243
         * @return (string) Encoded name object.
1244
         * @author Nicola Asuni
1245
         * @since 5.9.097 (2011-06-23)
1246
         * @public static
1247
         */
1248
        public static function encodeNameObject($name) {
1249
                $escname = '';
1250
                $length = strlen($name);
1251
                for ($i = 0; $i < $length; ++$i) {
1252
                        $chr = $name[$i];
1253
                        if (preg_match('/[0-9a-zA-Z#_=-]/', $chr) == 1) {
1254
                                $escname .= $chr;
1255
                        } else {
1256
                                $escname .= sprintf('#%02X', ord($chr));
1257
                        }
1258
                }
1259
                return $escname;
1260
        }
1261

    
1262
        /**
1263
         * Convert JavaScript form fields properties array to Annotation Properties array.
1264
         * @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
1265
         * @param $spot_colors (array) Reference to spot colors array.
1266
         * @param $rtl (boolean) True if in Right-To-Left text direction mode, false otherwise.
1267
         * @return array of annotation properties
1268
         * @author Nicola Asuni
1269
         * @since 4.8.000 (2009-09-06)
1270
         * @public static
1271
         */
1272
        public static function getAnnotOptFromJSProp($prop, &$spot_colors, $rtl=false) {
1273
                if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
1274
                        // the annotation options area lready defined
1275
                        return $prop['aopt'];
1276
                }
1277
                $opt = array(); // value to be returned
1278
                // alignment: Controls how the text is laid out within the text field.
1279
                if (isset($prop['alignment'])) {
1280
                        switch ($prop['alignment']) {
1281
                                case 'left': {
1282
                                        $opt['q'] = 0;
1283
                                        break;
1284
                                }
1285
                                case 'center': {
1286
                                        $opt['q'] = 1;
1287
                                        break;
1288
                                }
1289
                                case 'right': {
1290
                                        $opt['q'] = 2;
1291
                                        break;
1292
                                }
1293
                                default: {
1294
                                        $opt['q'] = ($rtl)?2:0;
1295
                                        break;
1296
                                }
1297
                        }
1298
                }
1299
                // lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
1300
                if (isset($prop['lineWidth'])) {
1301
                        $linewidth = intval($prop['lineWidth']);
1302
                } else {
1303
                        $linewidth = 1;
1304
                }
1305
                // borderStyle: The border style for a field.
1306
                if (isset($prop['borderStyle'])) {
1307
                        switch ($prop['borderStyle']) {
1308
                                case 'border.d':
1309
                                case 'dashed': {
1310
                                        $opt['border'] = array(0, 0, $linewidth, array(3, 2));
1311
                                        $opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
1312
                                        break;
1313
                                }
1314
                                case 'border.b':
1315
                                case 'beveled': {
1316
                                        $opt['border'] = array(0, 0, $linewidth);
1317
                                        $opt['bs'] = array('w'=>$linewidth, 's'=>'B');
1318
                                        break;
1319
                                }
1320
                                case 'border.i':
1321
                                case 'inset': {
1322
                                        $opt['border'] = array(0, 0, $linewidth);
1323
                                        $opt['bs'] = array('w'=>$linewidth, 's'=>'I');
1324
                                        break;
1325
                                }
1326
                                case 'border.u':
1327
                                case 'underline': {
1328
                                        $opt['border'] = array(0, 0, $linewidth);
1329
                                        $opt['bs'] = array('w'=>$linewidth, 's'=>'U');
1330
                                        break;
1331
                                }
1332
                                case 'border.s':
1333
                                case 'solid': {
1334
                                        $opt['border'] = array(0, 0, $linewidth);
1335
                                        $opt['bs'] = array('w'=>$linewidth, 's'=>'S');
1336
                                        break;
1337
                                }
1338
                                default: {
1339
                                        break;
1340
                                }
1341
                        }
1342
                }
1343
                if (isset($prop['border']) AND is_array($prop['border'])) {
1344
                        $opt['border'] = $prop['border'];
1345
                }
1346
                if (!isset($opt['mk'])) {
1347
                        $opt['mk'] = array();
1348
                }
1349
                if (!isset($opt['mk']['if'])) {
1350
                        $opt['mk']['if'] = array();
1351
                }
1352
                $opt['mk']['if']['a'] = array(0.5, 0.5);
1353
                // buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
1354
                if (isset($prop['buttonAlignX'])) {
1355
                        $opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
1356
                }
1357
                // buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
1358
                if (isset($prop['buttonAlignY'])) {
1359
                        $opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
1360
                }
1361
                // buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
1362
                if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
1363
                        $opt['mk']['if']['fb'] = true;
1364
                }
1365
                // buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
1366
                if (isset($prop['buttonScaleHow'])) {
1367
                        switch ($prop['buttonScaleHow']) {
1368
                                case 'scaleHow.proportional': {
1369
                                        $opt['mk']['if']['s'] = 'P';
1370
                                        break;
1371
                                }
1372
                                case 'scaleHow.anamorphic': {
1373
                                        $opt['mk']['if']['s'] = 'A';
1374
                                        break;
1375
                                }
1376
                        }
1377
                }
1378
                // buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
1379
                if (isset($prop['buttonScaleWhen'])) {
1380
                        switch ($prop['buttonScaleWhen']) {
1381
                                case 'scaleWhen.always': {
1382
                                        $opt['mk']['if']['sw'] = 'A';
1383
                                        break;
1384
                                }
1385
                                case 'scaleWhen.never': {
1386
                                        $opt['mk']['if']['sw'] = 'N';
1387
                                        break;
1388
                                }
1389
                                case 'scaleWhen.tooBig': {
1390
                                        $opt['mk']['if']['sw'] = 'B';
1391
                                        break;
1392
                                }
1393
                                case 'scaleWhen.tooSmall': {
1394
                                        $opt['mk']['if']['sw'] = 'S';
1395
                                        break;
1396
                                }
1397
                        }
1398
                }
1399
                // buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
1400
                if (isset($prop['buttonPosition'])) {
1401
                        switch ($prop['buttonPosition']) {
1402
                                case 0:
1403
                                case 'position.textOnly': {
1404
                                        $opt['mk']['tp'] = 0;
1405
                                        break;
1406
                                }
1407
                                case 1:
1408
                                case 'position.iconOnly': {
1409
                                        $opt['mk']['tp'] = 1;
1410
                                        break;
1411
                                }
1412
                                case 2:
1413
                                case 'position.iconTextV': {
1414
                                        $opt['mk']['tp'] = 2;
1415
                                        break;
1416
                                }
1417
                                case 3:
1418
                                case 'position.textIconV': {
1419
                                        $opt['mk']['tp'] = 3;
1420
                                        break;
1421
                                }
1422
                                case 4:
1423
                                case 'position.iconTextH': {
1424
                                        $opt['mk']['tp'] = 4;
1425
                                        break;
1426
                                }
1427
                                case 5:
1428
                                case 'position.textIconH': {
1429
                                        $opt['mk']['tp'] = 5;
1430
                                        break;
1431
                                }
1432
                                case 6:
1433
                                case 'position.overlay': {
1434
                                        $opt['mk']['tp'] = 6;
1435
                                        break;
1436
                                }
1437
                        }
1438
                }
1439
                // fillColor: Specifies the background color for a field.
1440
                if (isset($prop['fillColor'])) {
1441
                        if (is_array($prop['fillColor'])) {
1442
                                $opt['mk']['bg'] = $prop['fillColor'];
1443
                        } else {
1444
                                $opt['mk']['bg'] = TCPDF_COLORS::convertHTMLColorToDec($prop['fillColor'], $spot_colors);
1445
                        }
1446
                }
1447
                // 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.
1448
                if (isset($prop['strokeColor'])) {
1449
                        if (is_array($prop['strokeColor'])) {
1450
                                $opt['mk']['bc'] = $prop['strokeColor'];
1451
                        } else {
1452
                                $opt['mk']['bc'] = TCPDF_COLORS::convertHTMLColorToDec($prop['strokeColor'], $spot_colors);
1453
                        }
1454
                }
1455
                // rotation: The rotation of a widget in counterclockwise increments.
1456
                if (isset($prop['rotation'])) {
1457
                        $opt['mk']['r'] = $prop['rotation'];
1458
                }
1459
                // charLimit: Limits the number of characters that a user can type into a text field.
1460
                if (isset($prop['charLimit'])) {
1461
                        $opt['maxlen'] = intval($prop['charLimit']);
1462
                }
1463
                if (!isset($ff)) {
1464
                        $ff = 0; // default value
1465
                }
1466
                // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
1467
                if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
1468
                        $ff += 1 << 0;
1469
                }
1470
                // required: Specifies whether a field requires a value.
1471
                if (isset($prop['required']) AND ($prop['required'] == 'true')) {
1472
                        $ff += 1 << 1;
1473
                }
1474
                // multiline: Controls how text is wrapped within the field.
1475
                if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
1476
                        $ff += 1 << 12;
1477
                }
1478
                // password: Specifies whether the field should display asterisks when data is entered in the field.
1479
                if (isset($prop['password']) AND ($prop['password'] == 'true')) {
1480
                        $ff += 1 << 13;
1481
                }
1482
                // NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
1483
                if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
1484
                        $ff += 1 << 14;
1485
                }
1486
                // Radio: If set, the field is a set of radio buttons.
1487
                if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
1488
                        $ff += 1 << 15;
1489
                }
1490
                // Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
1491
                if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
1492
                        $ff += 1 << 16;
1493
                }
1494
                // Combo: If set, the field is a combo box; if clear, the field is a list box.
1495
                if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
1496
                        $ff += 1 << 17;
1497
                }
1498
                // editable: Controls whether a combo box is editable.
1499
                if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
1500
                        $ff += 1 << 18;
1501
                }
1502
                // Sort: If set, the field's option items shall be sorted alphabetically.
1503
                if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
1504
                        $ff += 1 << 19;
1505
                }
1506
                // fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
1507
                if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
1508
                        $ff += 1 << 20;
1509
                }
1510
                // multipleSelection: If true, indicates that a list box allows a multiple selection of items.
1511
                if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
1512
                        $ff += 1 << 21;
1513
                }
1514
                // doNotSpellCheck: If true, spell checking is not performed on this editable text field.
1515
                if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
1516
                        $ff += 1 << 22;
1517
                }
1518
                // doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
1519
                if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
1520
                        $ff += 1 << 23;
1521
                }
1522
                // 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.
1523
                if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
1524
                        $ff += 1 << 24;
1525
                }
1526
                // 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.
1527
                if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
1528
                        $ff += 1 << 25;
1529
                }
1530
                // richText: If true, the field allows rich text formatting.
1531
                if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
1532
                        $ff += 1 << 25;
1533
                }
1534
                // commitOnSelChange: Controls whether a field value is committed after a selection change.
1535
                if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
1536
                        $ff += 1 << 26;
1537
                }
1538
                $opt['ff'] = $ff;
1539
                // defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
1540
                if (isset($prop['defaultValue'])) {
1541
                        $opt['dv'] = $prop['defaultValue'];
1542
                }
1543
                $f = 4; // default value for annotation flags
1544
                // readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
1545
                if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
1546
                        $f += 1 << 6;
1547
                }
1548
                // display: Controls whether the field is hidden or visible on screen and in print.
1549
                if (isset($prop['display'])) {
1550
                        if ($prop['display'] == 'display.visible') {
1551
                                //
1552
                        } elseif ($prop['display'] == 'display.hidden') {
1553
                                $f += 1 << 1;
1554
                        } elseif ($prop['display'] == 'display.noPrint') {
1555
                                $f -= 1 << 2;
1556
                        } elseif ($prop['display'] == 'display.noView') {
1557
                                $f += 1 << 5;
1558
                        }
1559
                }
1560
                $opt['f'] = $f;
1561
                // currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
1562
                if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
1563
                        $opt['i'] = $prop['currentValueIndices'];
1564
                }
1565
                // value: The value of the field data that the user has entered.
1566
                if (isset($prop['value'])) {
1567
                        if (is_array($prop['value'])) {
1568
                                $opt['opt'] = array();
1569
                                foreach ($prop['value'] AS $key => $optval) {
1570
                                        // exportValues: An array of strings representing the export values for the field.
1571
                                        if (isset($prop['exportValues'][$key])) {
1572
                                                $opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
1573
                                        } else {
1574
                                                $opt['opt'][$key] = $prop['value'][$key];
1575
                                        }
1576
                                }
1577
                        } else {
1578
                                $opt['v'] = $prop['value'];
1579
                        }
1580
                }
1581
                // richValue: This property specifies the text contents and formatting of a rich text field.
1582
                if (isset($prop['richValue'])) {
1583
                        $opt['rv'] = $prop['richValue'];
1584
                }
1585
                // submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
1586
                if (isset($prop['submitName'])) {
1587
                        $opt['tm'] = $prop['submitName'];
1588
                }
1589
                // name: Fully qualified field name.
1590
                if (isset($prop['name'])) {
1591
                        $opt['t'] = $prop['name'];
1592
                }
1593
                // userName: The user name (short description string) of the field.
1594
                if (isset($prop['userName'])) {
1595
                        $opt['tu'] = $prop['userName'];
1596
                }
1597
                // highlight: Defines how a button reacts when a user clicks it.
1598
                if (isset($prop['highlight'])) {
1599
                        switch ($prop['highlight']) {
1600
                                case 'none':
1601
                                case 'highlight.n': {
1602
                                        $opt['h'] = 'N';
1603
                                        break;
1604
                                }
1605
                                case 'invert':
1606
                                case 'highlight.i': {
1607
                                        $opt['h'] = 'i';
1608
                                        break;
1609
                                }
1610
                                case 'push':
1611
                                case 'highlight.p': {
1612
                                        $opt['h'] = 'P';
1613
                                        break;
1614
                                }
1615
                                case 'outline':
1616
                                case 'highlight.o': {
1617
                                        $opt['h'] = 'O';
1618
                                        break;
1619
                                }
1620
                        }
1621
                }
1622
                // Unsupported options:
1623
                // - calcOrderIndex: Changes the calculation order of fields in the document.
1624
                // - delay: Delays the redrawing of a field's appearance.
1625
                // - defaultStyle: This property defines the default style attributes for the form field.
1626
                // - style: Allows the user to set the glyph style of a check box or radio button.
1627
                // - textColor, textFont, textSize
1628
                return $opt;
1629
        }
1630

    
1631
        /**
1632
         * Format the page numbers.
1633
         * This method can be overriden for custom formats.
1634
         * @param $num (int) page number
1635
         * @since 4.2.005 (2008-11-06)
1636
         * @public static
1637
         */
1638
        public static function formatPageNumber($num) {
1639
                return number_format((float)$num, 0, '', '.');
1640
        }
1641

    
1642
        /**
1643
         * Format the page numbers on the Table Of Content.
1644
         * This method can be overriden for custom formats.
1645
         * @param $num (int) page number
1646
         * @since 4.5.001 (2009-01-04)
1647
         * @see addTOC(), addHTMLTOC()
1648
         * @public static
1649
         */
1650
        public static function formatTOCPageNumber($num) {
1651
                return number_format((float)$num, 0, '', '.');
1652
        }
1653

    
1654
        /**
1655
         * Extracts the CSS properties from a CSS string.
1656
         * @param $cssdata (string) string containing CSS definitions.
1657
         * @return An array where the keys are the CSS selectors and the values are the CSS properties.
1658
         * @author Nicola Asuni
1659
         * @since 5.1.000 (2010-05-25)
1660
         * @public static
1661
         */
1662
        public static function extractCSSproperties($cssdata) {
1663
                if (empty($cssdata)) {
1664
                        return array();
1665
                }
1666
                // remove comments
1667
                $cssdata = preg_replace('/\/\*[^\*]*\*\//', '', $cssdata);
1668
                // remove newlines and multiple spaces
1669
                $cssdata = preg_replace('/[\s]+/', ' ', $cssdata);
1670
                // remove some spaces
1671
                $cssdata = preg_replace('/[\s]*([;:\{\}]{1})[\s]*/', '\\1', $cssdata);
1672
                // remove empty blocks
1673
                $cssdata = preg_replace('/([^\}\{]+)\{\}/', '', $cssdata);
1674
                // replace media type parenthesis
1675
                $cssdata = preg_replace('/@media[\s]+([^\{]*)\{/i', '@media \\', $cssdata);
1676
                $cssdata = preg_replace('/\}\}/si', '', $cssdata);
1677
                // trim string
1678
                $cssdata = trim($cssdata);
1679
                // find media blocks (all, braille, embossed, handheld, print, projection, screen, speech, tty, tv)
1680
                $cssblocks = array();
1681
                $matches = array();
1682
                if (preg_match_all('/@media[\s]+([^]*)§([^§]*)§/i', $cssdata, $matches) > 0) {
1683
                        foreach ($matches[1] as $key => $type) {
1684
                                $cssblocks[$type] = $matches[2][$key];
1685
                        }
1686
                        // remove media blocks
1687
                        $cssdata = preg_replace('/@media[\s]+([^]*)§([^§]*)§/i', '', $cssdata);
1688
                }
1689
                // keep 'all' and 'print' media, other media types are discarded
1690
                if (isset($cssblocks['all']) AND !empty($cssblocks['all'])) {
1691
                        $cssdata .= $cssblocks['all'];
1692
                }
1693
                if (isset($cssblocks['print']) AND !empty($cssblocks['print'])) {
1694
                        $cssdata .= $cssblocks['print'];
1695
                }
1696
                // reset css blocks array
1697
                $cssblocks = array();
1698
                $matches = array();
1699
                // explode css data string into array
1700
                if (substr($cssdata, -1) == '}') {
1701
                        // remove last parethesis
1702
                        $cssdata = substr($cssdata, 0, -1);
1703
                }
1704
                $matches = explode('}', $cssdata);
1705
                foreach ($matches as $key => $block) {
1706
                        // index 0 contains the CSS selector, index 1 contains CSS properties
1707
                        $cssblocks[$key] = explode('{', $block);
1708
                        if (!isset($cssblocks[$key][1])) {
1709
                                // remove empty definitions
1710
                                unset($cssblocks[$key]);
1711
                        }
1712
                }
1713
                // split groups of selectors (comma-separated list of selectors)
1714
                foreach ($cssblocks as $key => $block) {
1715
                        if (strpos($block[0], ',') > 0) {
1716
                                $selectors = explode(',', $block[0]);
1717
                                foreach ($selectors as $sel) {
1718
                                        $cssblocks[] = array(0 => trim($sel), 1 => $block[1]);
1719
                                }
1720
                                unset($cssblocks[$key]);
1721
                        }
1722
                }
1723
                // covert array to selector => properties
1724
                $cssdata = array();
1725
                foreach ($cssblocks as $block) {
1726
                        $selector = $block[0];
1727
                        // calculate selector's specificity
1728
                        $matches = array();
1729
                        $a = 0; // the declaration is not from is a 'style' attribute
1730
                        $b = intval(preg_match_all('/[\#]/', $selector, $matches)); // number of ID attributes
1731
                        $c = intval(preg_match_all('/[\[\.]/', $selector, $matches)); // number of other attributes
1732
                        $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
1733
                        $d = intval(preg_match_all('/[\>\+\~\s]{1}[a-zA-Z0-9]+/', ' '.$selector, $matches)); // number of element names
1734
                        $d += intval(preg_match_all('/[\:][\:]/', $selector, $matches)); // number of pseudo-elements
1735
                        $specificity = $a.$b.$c.$d;
1736
                        // add specificity to the beginning of the selector
1737
                        $cssdata[$specificity.' '.$selector] = $block[1];
1738
                }
1739
                // sort selectors alphabetically to account for specificity
1740
                ksort($cssdata, SORT_STRING);
1741
                // return array
1742
                return $cssdata;
1743
        }
1744

    
1745
        /**
1746
         * Cleanup HTML code (requires HTML Tidy library).
1747
         * @param $html (string) htmlcode to fix
1748
         * @param $default_css (string) CSS commands to add
1749
         * @param $tagvs (array) parameters for setHtmlVSpace method
1750
         * @param $tidy_options (array) options for tidy_parse_string function
1751
         * @param $tagvspaces (array) Array of vertical spaces for tags.
1752
         * @return string XHTML code cleaned up
1753
         * @author Nicola Asuni
1754
         * @since 5.9.017 (2010-11-16)
1755
         * @see setHtmlVSpace()
1756
         * @public static
1757
         */
1758
        public static function fixHTMLCode($html, $default_css='', $tagvs='', $tidy_options='', &$tagvspaces) {
1759
                // configure parameters for HTML Tidy
1760
                if ($tidy_options === '') {
1761
                        $tidy_options = array (
1762
                                'clean' => 1,
1763
                                'drop-empty-paras' => 0,
1764
                                'drop-proprietary-attributes' => 1,
1765
                                'fix-backslash' => 1,
1766
                                'hide-comments' => 1,
1767
                                'join-styles' => 1,
1768
                                'lower-literals' => 1,
1769
                                'merge-divs' => 1,
1770
                                'merge-spans' => 1,
1771
                                'output-xhtml' => 1,
1772
                                'word-2000' => 1,
1773
                                'wrap' => 0,
1774
                                'output-bom' => 0,
1775
                                //'char-encoding' => 'utf8',
1776
                                //'input-encoding' => 'utf8',
1777
                                //'output-encoding' => 'utf8'
1778
                        );
1779
                }
1780
                // clean up the HTML code
1781
                $tidy = tidy_parse_string($html, $tidy_options);
1782
                // fix the HTML
1783
                $tidy->cleanRepair();
1784
                // get the CSS part
1785
                $tidy_head = tidy_get_head($tidy);
1786
                $css = $tidy_head->value;
1787
                $css = preg_replace('/<style([^>]+)>/ims', '<style>', $css);
1788
                $css = preg_replace('/<\/style>(.*)<style>/ims', "\n", $css);
1789
                $css = str_replace('/*<![CDATA[*/', '', $css);
1790
                $css = str_replace('/*]]>*/', '', $css);
1791
                preg_match('/<style>(.*)<\/style>/ims', $css, $matches);
1792
                if (isset($matches[1])) {
1793
                        $css = strtolower($matches[1]);
1794
                } else {
1795
                        $css = '';
1796
                }
1797
                // include default css
1798
                $css = '<style>'.$default_css.$css.'</style>';
1799
                // get the body part
1800
                $tidy_body = tidy_get_body($tidy);
1801
                $html = $tidy_body->value;
1802
                // fix some self-closing tags
1803
                $html = str_replace('<br>', '<br />', $html);
1804
                // remove some empty tag blocks
1805
                $html = preg_replace('/<div([^\>]*)><\/div>/', '', $html);
1806
                $html = preg_replace('/<p([^\>]*)><\/p>/', '', $html);
1807
                if ($tagvs !== '') {
1808
                        // set vertical space for some XHTML tags
1809
                        $tagvspaces = $tagvs;
1810
                }
1811
                // return the cleaned XHTML code + CSS
1812
                return $css.$html;
1813
        }
1814

    
1815
        /**
1816
         * Returns true if the CSS selector is valid for the selected HTML tag
1817
         * @param $dom (array) array of HTML tags and properties
1818
         * @param $key (int) key of the current HTML tag
1819
         * @param $selector (string) CSS selector string
1820
         * @return true if the selector is valid, false otherwise
1821
         * @since 5.1.000 (2010-05-25)
1822
         * @public static
1823
         */
1824
        public static function isValidCSSSelectorForTag($dom, $key, $selector) {
1825
                $valid = false; // value to be returned
1826
                $tag = $dom[$key]['value'];
1827
                $class = array();
1828
                if (isset($dom[$key]['attribute']['class']) AND !empty($dom[$key]['attribute']['class'])) {
1829
                        $class = explode(' ', strtolower($dom[$key]['attribute']['class']));
1830
                }
1831
                $id = '';
1832
                if (isset($dom[$key]['attribute']['id']) AND !empty($dom[$key]['attribute']['id'])) {
1833
                        $id = strtolower($dom[$key]['attribute']['id']);
1834
                }
1835
                $selector = preg_replace('/([\>\+\~\s]{1})([\.]{1})([^\>\+\~\s]*)/si', '\\1*.\\3', $selector);
1836
                $matches = array();
1837
                if (preg_match_all('/([\>\+\~\s]{1})([a-zA-Z0-9\*]+)([^\>\+\~\s]*)/si', $selector, $matches, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE) > 0) {
1838
                        $parentop = array_pop($matches[1]);
1839
                        $operator = $parentop[0];
1840
                        $offset = $parentop[1];
1841
                        $lasttag = array_pop($matches[2]);
1842
                        $lasttag = strtolower(trim($lasttag[0]));
1843
                        if (($lasttag == '*') OR ($lasttag == $tag)) {
1844
                                // the last element on selector is our tag or 'any tag'
1845
                                $attrib = array_pop($matches[3]);
1846
                                $attrib = strtolower(trim($attrib[0]));
1847
                                if (!empty($attrib)) {
1848
                                        // check if matches class, id, attribute, pseudo-class or pseudo-element
1849
                                        switch ($attrib[0]) {
1850
                                                case '.': { // class
1851
                                                        if (in_array(substr($attrib, 1), $class)) {
1852
                                                                $valid = true;
1853
                                                        }
1854
                                                        break;
1855
                                                }
1856
                                                case '#': { // ID
1857
                                                        if (substr($attrib, 1) == $id) {
1858
                                                                $valid = true;
1859
                                                        }
1860
                                                        break;
1861
                                                }
1862
                                                case '[': { // attribute
1863
                                                        $attrmatch = array();
1864
                                                        if (preg_match('/\[([a-zA-Z0-9]*)[\s]*([\~\^\$\*\|\=]*)[\s]*["]?([^"\]]*)["]?\]/i', $attrib, $attrmatch) > 0) {
1865
                                                                $att = strtolower($attrmatch[1]);
1866
                                                                $val = $attrmatch[3];
1867
                                                                if (isset($dom[$key]['attribute'][$att])) {
1868
                                                                        switch ($attrmatch[2]) {
1869
                                                                                case '=': {
1870
                                                                                        if ($dom[$key]['attribute'][$att] == $val) {
1871
                                                                                                $valid = true;
1872
                                                                                        }
1873
                                                                                        break;
1874
                                                                                }
1875
                                                                                case '~=': {
1876
                                                                                        if (in_array($val, explode(' ', $dom[$key]['attribute'][$att]))) {
1877
                                                                                                $valid = true;
1878
                                                                                        }
1879
                                                                                        break;
1880
                                                                                }
1881
                                                                                case '^=': {
1882
                                                                                        if ($val == substr($dom[$key]['attribute'][$att], 0, strlen($val))) {
1883
                                                                                                $valid = true;
1884
                                                                                        }
1885
                                                                                        break;
1886
                                                                                }
1887
                                                                                case '$=': {
1888
                                                                                        if ($val == substr($dom[$key]['attribute'][$att], -strlen($val))) {
1889
                                                                                                $valid = true;
1890
                                                                                        }
1891
                                                                                        break;
1892
                                                                                }
1893
                                                                                case '*=': {
1894
                                                                                        if (strpos($dom[$key]['attribute'][$att], $val) !== false) {
1895
                                                                                                $valid = true;
1896
                                                                                        }
1897
                                                                                        break;
1898
                                                                                }
1899
                                                                                case '|=': {
1900
                                                                                        if ($dom[$key]['attribute'][$att] == $val) {
1901
                                                                                                $valid = true;
1902
                                                                                        } elseif (preg_match('/'.$val.'[\-]{1}/i', $dom[$key]['attribute'][$att]) > 0) {
1903
                                                                                                $valid = true;
1904
                                                                                        }
1905
                                                                                        break;
1906
                                                                                }
1907
                                                                                default: {
1908
                                                                                        $valid = true;
1909
                                                                                }
1910
                                                                        }
1911
                                                                }
1912
                                                        }
1913
                                                        break;
1914
                                                }
1915
                                                case ':': { // pseudo-class or pseudo-element
1916
                                                        if ($attrib[1] == ':') { // pseudo-element
1917
                                                                // pseudo-elements are not supported!
1918
                                                                // (::first-line, ::first-letter, ::before, ::after)
1919
                                                        } else { // pseudo-class
1920
                                                                // pseudo-classes are not supported!
1921
                                                                // (: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)
1922
                                                        }
1923
                                                        break;
1924
                                                }
1925
                                        } // end of switch
1926
                                } else {
1927
                                        $valid = true;
1928
                                }
1929
                                if ($valid AND ($offset > 0)) {
1930
                                        $valid = false;
1931
                                        // check remaining selector part
1932
                                        $selector = substr($selector, 0, $offset);
1933
                                        switch ($operator) {
1934
                                                case ' ': { // descendant of an element
1935
                                                        while ($dom[$key]['parent'] > 0) {
1936
                                                                if (self::isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector)) {
1937
                                                                        $valid = true;
1938
                                                                        break;
1939
                                                                } else {
1940
                                                                        $key = $dom[$key]['parent'];
1941
                                                                }
1942
                                                        }
1943
                                                        break;
1944
                                                }
1945
                                                case '>': { // child of an element
1946
                                                        $valid = self::isValidCSSSelectorForTag($dom, $dom[$key]['parent'], $selector);
1947
                                                        break;
1948
                                                }
1949
                                                case '+': { // immediately preceded by an element
1950
                                                        for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
1951
                                                                if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
1952
                                                                        $valid = self::isValidCSSSelectorForTag($dom, $i, $selector);
1953
                                                                        break;
1954
                                                                }
1955
                                                        }
1956
                                                        break;
1957
                                                }
1958
                                                case '~': { // preceded by an element
1959
                                                        for ($i = ($key - 1); $i > $dom[$key]['parent']; --$i) {
1960
                                                                if ($dom[$i]['tag'] AND $dom[$i]['opening']) {
1961
                                                                        if (self::isValidCSSSelectorForTag($dom, $i, $selector)) {
1962
                                                                                break;
1963
                                                                        }
1964
                                                                }
1965
                                                        }
1966
                                                        break;
1967
                                                }
1968
                                        }
1969
                                }
1970
                        }
1971
                }
1972
                return $valid;
1973
        }
1974

    
1975
        /**
1976
         * Returns the styles array that apply for the selected HTML tag.
1977
         * @param $dom (array) array of HTML tags and properties
1978
         * @param $key (int) key of the current HTML tag
1979
         * @param $css (array) array of CSS properties
1980
         * @return array containing CSS properties
1981
         * @since 5.1.000 (2010-05-25)
1982
         * @public static
1983
         */
1984
        public static function getCSSdataArray($dom, $key, $css) {
1985
                $cssarray = array(); // style to be returned
1986
                // get parent CSS selectors
1987
                $selectors = array();
1988
                if (isset($dom[($dom[$key]['parent'])]['csssel'])) {
1989
                        $selectors = $dom[($dom[$key]['parent'])]['csssel'];
1990
                }
1991
                // get all styles that apply
1992
                foreach($css as $selector => $style) {
1993
                        $pos = strpos($selector, ' ');
1994
                        // get specificity
1995
                        $specificity = substr($selector, 0, $pos);
1996
                        // remove specificity
1997
                        $selector = substr($selector, $pos);
1998
                        // check if this selector apply to current tag
1999
                        if (self::isValidCSSSelectorForTag($dom, $key, $selector)) {
2000
                                if (!in_array($selector, $selectors)) {
2001
                                        // add style if not already added on parent selector
2002
                                        $cssarray[] = array('k' => $selector, 's' => $specificity, 'c' => $style);
2003
                                        $selectors[] = $selector;
2004
                                }
2005
                        }
2006
                }
2007
                if (isset($dom[$key]['attribute']['style'])) {
2008
                        // attach inline style (latest properties have high priority)
2009
                        $cssarray[] = array('k' => '', 's' => '1000', 'c' => $dom[$key]['attribute']['style']);
2010
                }
2011
                // order the css array to account for specificity
2012
                $cssordered = array();
2013
                foreach ($cssarray as $key => $val) {
2014
                        $skey = sprintf('%04d', $key);
2015
                        $cssordered[$val['s'].'_'.$skey] = $val;
2016
                }
2017
                // sort selectors alphabetically to account for specificity
2018
                ksort($cssordered, SORT_STRING);
2019
                return array($selectors, $cssordered);
2020
        }
2021

    
2022
        /**
2023
         * Compact CSS data array into single string.
2024
         * @param $css (array) array of CSS properties
2025
         * @return string containing merged CSS properties
2026
         * @since 5.9.070 (2011-04-19)
2027
         * @public static
2028
         */
2029
        public static function getTagStyleFromCSSarray($css) {
2030
                $tagstyle = ''; // value to be returned
2031
                foreach ($css as $style) {
2032
                        // split single css commands
2033
                        $csscmds = explode(';', $style['c']);
2034
                        foreach ($csscmds as $cmd) {
2035
                                if (!empty($cmd)) {
2036
                                        $pos = strpos($cmd, ':');
2037
                                        if ($pos !== false) {
2038
                                                $cmd = substr($cmd, 0, ($pos + 1));
2039
                                                if (strpos($tagstyle, $cmd) !== false) {
2040
                                                        // remove duplicate commands (last commands have high priority)
2041
                                                        $tagstyle = preg_replace('/'.$cmd.'[^;]+/i', '', $tagstyle);
2042
                                                }
2043
                                        }
2044
                                }
2045
                        }
2046
                        $tagstyle .= ';'.$style['c'];
2047
                }
2048
                // remove multiple semicolons
2049
                $tagstyle = preg_replace('/[;]+/', ';', $tagstyle);
2050
                return $tagstyle;
2051
        }
2052

    
2053
        /**
2054
         * Returns the Roman representation of an integer number
2055
         * @param $number (int) number to convert
2056
         * @return string roman representation of the specified number
2057
         * @since 4.4.004 (2008-12-10)
2058
         * @public static
2059
         */
2060
        public static function intToRoman($number) {
2061
                $roman = '';
2062
                while ($number >= 1000) {
2063
                        $roman .= 'M';
2064
                        $number -= 1000;
2065
                }
2066
                while ($number >= 900) {
2067
                        $roman .= 'CM';
2068
                        $number -= 900;
2069
                }
2070
                while ($number >= 500) {
2071
                        $roman .= 'D';
2072
                        $number -= 500;
2073
                }
2074
                while ($number >= 400) {
2075
                        $roman .= 'CD';
2076
                        $number -= 400;
2077
                }
2078
                while ($number >= 100) {
2079
                        $roman .= 'C';
2080
                        $number -= 100;
2081
                }
2082
                while ($number >= 90) {
2083
                        $roman .= 'XC';
2084
                        $number -= 90;
2085
                }
2086
                while ($number >= 50) {
2087
                        $roman .= 'L';
2088
                        $number -= 50;
2089
                }
2090
                while ($number >= 40) {
2091
                        $roman .= 'XL';
2092
                        $number -= 40;
2093
                }
2094
                while ($number >= 10) {
2095
                        $roman .= 'X';
2096
                        $number -= 10;
2097
                }
2098
                while ($number >= 9) {
2099
                        $roman .= 'IX';
2100
                        $number -= 9;
2101
                }
2102
                while ($number >= 5) {
2103
                        $roman .= 'V';
2104
                        $number -= 5;
2105
                }
2106
                while ($number >= 4) {
2107
                        $roman .= 'IV';
2108
                        $number -= 4;
2109
                }
2110
                while ($number >= 1) {
2111
                        $roman .= 'I';
2112
                        --$number;
2113
                }
2114
                return $roman;
2115
        }
2116

    
2117
        /**
2118
         * Find position of last occurrence of a substring in a string
2119
         * @param $haystack (string) The string to search in.
2120
         * @param $needle (string) substring to search.
2121
         * @param $offset (int) May be specified to begin searching an arbitrary number of characters into the string.
2122
         * @return Returns the position where the needle exists. Returns FALSE if the needle was not found.
2123
         * @since 4.8.038 (2010-03-13)
2124
         * @public static
2125
         */
2126
        public static function revstrpos($haystack, $needle, $offset = 0) {
2127
                $length = strlen($haystack);
2128
                $offset = ($offset > 0)?($length - $offset):abs($offset);
2129
                $pos = strpos(strrev($haystack), strrev($needle), $offset);
2130
                return ($pos === false)?false:($length - $pos - strlen($needle));
2131
        }
2132

    
2133
        /**
2134
         * Returns an array of hyphenation patterns.
2135
         * @param $file (string) TEX file containing hypenation patterns. TEX pattrns can be downloaded from http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
2136
         * @return array of hyphenation patterns
2137
         * @author Nicola Asuni
2138
         * @since 4.9.012 (2010-04-12)
2139
         * @public static
2140
         */
2141
        public static function getHyphenPatternsFromTEX($file) {
2142
                // TEX patterns are available at:
2143
                // http://www.ctan.org/tex-archive/language/hyph-utf8/tex/generic/hyph-utf8/patterns/
2144
                $data = file_get_contents($file);
2145
                $patterns = array();
2146
                // remove comments
2147
                $data = preg_replace('/\%[^\n]*/', '', $data);
2148
                // extract the patterns part
2149
                preg_match('/\\\\patterns\{([^\}]*)\}/i', $data, $matches);
2150
                $data = trim(substr($matches[0], 10, -1));
2151
                // extract each pattern
2152
                $patterns_array = preg_split('/[\s]+/', $data);
2153
                // create new language array of patterns
2154
                $patterns = array();
2155
                foreach($patterns_array as $val) {
2156
                        if (!TCPDF_STATIC::empty_string($val)) {
2157
                                $val = trim($val);
2158
                                $val = str_replace('\'', '\\\'', $val);
2159
                                $key = preg_replace('/[0-9]+/', '', $val);
2160
                                $patterns[$key] = $val;
2161
                        }
2162
                }
2163
                return $patterns;
2164
        }
2165

    
2166
        /**
2167
         * Get the Path-Painting Operators.
2168
         * @param $style (string) Style of rendering. Possible values are:
2169
         * <ul>
2170
         *   <li>S or D: Stroke the path.</li>
2171
         *   <li>s or d: Close and stroke the path.</li>
2172
         *   <li>f or F: Fill the path, using the nonzero winding number rule to determine the region to fill.</li>
2173
         *   <li>f* or F*: Fill the path, using the even-odd rule to determine the region to fill.</li>
2174
         *   <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>
2175
         *   <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>
2176
         *   <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>
2177
         *   <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>
2178
         *   <li>CNZ: Clipping mode using the even-odd rule to determine which regions lie inside the clipping path.</li>
2179
         *   <li>CEO: Clipping mode using the nonzero winding number rule to determine which regions lie inside the clipping path</li>
2180
         *   <li>n: End the path object without filling or stroking it.</li>
2181
         * </ul>
2182
         * @param $default (string) default style
2183
         * @author Nicola Asuni
2184
         * @since 5.0.000 (2010-04-30)
2185
         * @public static
2186
         */
2187
        public static function getPathPaintOperator($style, $default='S') {
2188
                $op = '';
2189
                switch($style) {
2190
                        case 'S':
2191
                        case 'D': {
2192
                                $op = 'S';
2193
                                break;
2194
                        }
2195
                        case 's':
2196
                        case 'd': {
2197
                                $op = 's';
2198
                                break;
2199
                        }
2200
                        case 'f':
2201
                        case 'F': {
2202
                                $op = 'f';
2203
                                break;
2204
                        }
2205
                        case 'f*':
2206
                        case 'F*': {
2207
                                $op = 'f*';
2208
                                break;
2209
                        }
2210
                        case 'B':
2211
                        case 'FD':
2212
                        case 'DF': {
2213
                                $op = 'B';
2214
                                break;
2215
                        }
2216
                        case 'B*':
2217
                        case 'F*D':
2218
                        case 'DF*': {
2219
                                $op = 'B*';
2220
                                break;
2221
                        }
2222
                        case 'b':
2223
                        case 'fd':
2224
                        case 'df': {
2225
                                $op = 'b';
2226
                                break;
2227
                        }
2228
                        case 'b*':
2229
                        case 'f*d':
2230
                        case 'df*': {
2231
                                $op = 'b*';
2232
                                break;
2233
                        }
2234
                        case 'CNZ': {
2235
                                $op = 'W n';
2236
                                break;
2237
                        }
2238
                        case 'CEO': {
2239
                                $op = 'W* n';
2240
                                break;
2241
                        }
2242
                        case 'n': {
2243
                                $op = 'n';
2244
                                break;
2245
                        }
2246
                        default: {
2247
                                if (!empty($default)) {
2248
                                        $op = self::getPathPaintOperator($default, '');
2249
                                } else {
2250
                                        $op = '';
2251
                                }
2252
                        }
2253
                }
2254
                return $op;
2255
        }
2256

    
2257
        /**
2258
         * Get the product of two SVG tranformation matrices
2259
         * @param $ta (array) first SVG tranformation matrix
2260
         * @param $tb (array) second SVG tranformation matrix
2261
         * @return transformation array
2262
         * @author Nicola Asuni
2263
         * @since 5.0.000 (2010-05-02)
2264
         * @public static
2265
         */
2266
        public static function getTransformationMatrixProduct($ta, $tb) {
2267
                $tm = array();
2268
                $tm[0] = ($ta[0] * $tb[0]) + ($ta[2] * $tb[1]);
2269
                $tm[1] = ($ta[1] * $tb[0]) + ($ta[3] * $tb[1]);
2270
                $tm[2] = ($ta[0] * $tb[2]) + ($ta[2] * $tb[3]);
2271
                $tm[3] = ($ta[1] * $tb[2]) + ($ta[3] * $tb[3]);
2272
                $tm[4] = ($ta[0] * $tb[4]) + ($ta[2] * $tb[5]) + $ta[4];
2273
                $tm[5] = ($ta[1] * $tb[4]) + ($ta[3] * $tb[5]) + $ta[5];
2274
                return $tm;
2275
        }
2276

    
2277
        /**
2278
         * Get the tranformation matrix from SVG transform attribute
2279
         * @param $attribute (string) transformation
2280
         * @return array of transformations
2281
         * @author Nicola Asuni
2282
         * @since 5.0.000 (2010-05-02)
2283
         * @public static
2284
         */
2285
        public static function getSVGTransformMatrix($attribute) {
2286
                // identity matrix
2287
                $tm = array(1, 0, 0, 1, 0, 0);
2288
                $transform = array();
2289
                if (preg_match_all('/(matrix|translate|scale|rotate|skewX|skewY)[\s]*\(([^\)]+)\)/si', $attribute, $transform, PREG_SET_ORDER) > 0) {
2290
                        foreach ($transform as $key => $data) {
2291
                                if (!empty($data[2])) {
2292
                                        $a = 1;
2293
                                        $b = 0;
2294
                                        $c = 0;
2295
                                        $d = 1;
2296
                                        $e = 0;
2297
                                        $f = 0;
2298
                                        $regs = array();
2299
                                        switch ($data[1]) {
2300
                                                case 'matrix': {
2301
                                                        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)) {
2302
                                                                $a = $regs[1];
2303
                                                                $b = $regs[2];
2304
                                                                $c = $regs[3];
2305
                                                                $d = $regs[4];
2306
                                                                $e = $regs[5];
2307
                                                                $f = $regs[6];
2308
                                                        }
2309
                                                        break;
2310
                                                }
2311
                                                case 'translate': {
2312
                                                        if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
2313
                                                                $e = $regs[1];
2314
                                                                $f = $regs[2];
2315
                                                        } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
2316
                                                                $e = $regs[1];
2317
                                                        }
2318
                                                        break;
2319
                                                }
2320
                                                case 'scale': {
2321
                                                        if (preg_match('/([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
2322
                                                                $a = $regs[1];
2323
                                                                $d = $regs[2];
2324
                                                        } elseif (preg_match('/([a-z0-9\-\.]+)/si', $data[2], $regs)) {
2325
                                                                $a = $regs[1];
2326
                                                                $d = $a;
2327
                                                        }
2328
                                                        break;
2329
                                                }
2330
                                                case 'rotate': {
2331
                                                        if (preg_match('/([0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)[\,\s]+([a-z0-9\-\.]+)/si', $data[2], $regs)) {
2332
                                                                $ang = deg2rad($regs[1]);
2333
                                                                $x = $regs[2];
2334
                                                                $y = $regs[3];
2335
                                                                $a = cos($ang);
2336
                                                                $b = sin($ang);
2337
                                                                $c = -$b;
2338
                                                                $d = $a;
2339
                                                                $e = ($x * (1 - $a)) - ($y * $c);
2340
                                                                $f = ($y * (1 - $d)) - ($x * $b);
2341
                                                        } elseif (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
2342
                                                                $ang = deg2rad($regs[1]);
2343
                                                                $a = cos($ang);
2344
                                                                $b = sin($ang);
2345
                                                                $c = -$b;
2346
                                                                $d = $a;
2347
                                                                $e = 0;
2348
                                                                $f = 0;
2349
                                                        }
2350
                                                        break;
2351
                                                }
2352
                                                case 'skewX': {
2353
                                                        if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
2354
                                                                $c = tan(deg2rad($regs[1]));
2355
                                                        }
2356
                                                        break;
2357
                                                }
2358
                                                case 'skewY': {
2359
                                                        if (preg_match('/([0-9\-\.]+)/si', $data[2], $regs)) {
2360
                                                                $b = tan(deg2rad($regs[1]));
2361
                                                        }
2362
                                                        break;
2363
                                                }
2364
                                        }
2365
                                        $tm = self::getTransformationMatrixProduct($tm, array($a, $b, $c, $d, $e, $f));
2366
                                }
2367
                        }
2368
                }
2369
                return $tm;
2370
        }
2371

    
2372
        /**
2373
         * Returns the angle in radiants between two vectors
2374
         * @param $x1 (int) X coordinate of first vector point
2375
         * @param $y1 (int) Y coordinate of first vector point
2376
         * @param $x2 (int) X coordinate of second vector point
2377
         * @param $y2 (int) Y coordinate of second vector point
2378
         * @author Nicola Asuni
2379
         * @since 5.0.000 (2010-05-04)
2380
         * @public static
2381
         */
2382
        public static function getVectorsAngle($x1, $y1, $x2, $y2) {
2383
                $dprod = ($x1 * $x2) + ($y1 * $y2);
2384
                $dist1 = sqrt(($x1 * $x1) + ($y1 * $y1));
2385
                $dist2 = sqrt(($x2 * $x2) + ($y2 * $y2));
2386
                $angle = acos($dprod / ($dist1 * $dist2));
2387
                if (is_nan($angle)) {
2388
                        $angle = M_PI;
2389
                }
2390
                if ((($x1 * $y2) - ($x2 * $y1)) < 0) {
2391
                        $angle *= -1;
2392
                }
2393
                return $angle;
2394
        }
2395

    
2396
        /**
2397
         * Split string by a regular expression.
2398
         * This is a wrapper for the preg_split function to avoid the bug: https://bugs.php.net/bug.php?id=45850
2399
         * @param $pattern (string) The regular expression pattern to search for without the modifiers, as a string.
2400
         * @param $modifiers (string) The modifiers part of the pattern,
2401
         * @param $subject (string) The input string.
2402
         * @param $limit (int) If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or NULL means "no limit" and, as is standard across PHP, you can use NULL to skip to the flags parameter.
2403
         * @param $flags (int) The flags as specified on the preg_split PHP function.
2404
         * @return Returns an array containing substrings of subject split along boundaries matched by pattern.modifier
2405
         * @author Nicola Asuni
2406
         * @since 6.0.023
2407
         * @public static
2408
         */
2409
        public static function pregSplit($pattern, $modifiers, $subject, $limit=NULL, $flags=NULL) {
2410
                // the bug only happens on PHP 5.2 when using the u modifier
2411
                if ((strpos($modifiers, 'u') === FALSE) OR (count(preg_split('//u', "\n\t", -1, PREG_SPLIT_NO_EMPTY)) == 2)) {
2412
                        return preg_split($pattern.$modifiers, $subject, $limit, $flags);
2413
                }
2414
                // preg_split is bugged - try alternative solution
2415
                $ret = array();
2416
                while (($nl = strpos($subject, "\n")) !== FALSE) {
2417
                        $ret = array_merge($ret, preg_split($pattern.$modifiers, substr($subject, 0, $nl), $limit, $flags));
2418
                        $ret[] = "\n";
2419
                        $subject = substr($subject, ($nl + 1));
2420
                }
2421
                if (strlen($subject) > 0) {
2422
                        $ret = array_merge($ret, preg_split($pattern.$modifiers, $subject, $limit, $flags));
2423
                }
2424
                return $ret;
2425
        }
2426

    
2427
        /**
2428
         * Wrapper to use fopen only with local files
2429
         * @param filename (string) Name of the file to open
2430
         * @param $mode (string) 
2431
         * @return Returns a file pointer resource on success, or FALSE on error.  
2432
         * @public static
2433
         */
2434
        public static function fopenLocal($filename, $mode) {
2435
                if (strpos($filename, '://') === false) {
2436
                        $filename = 'file://'.$filename;
2437
                } elseif (stream_is_local($filename) !== true) {
2438
                        return false;
2439
                }
2440
                return fopen($filename, $mode);
2441
        }
2442

    
2443
        /**
2444
         * Reads entire file into a string.
2445
         * The file can be also an URL.
2446
         * @param $file (string) Name of the file or URL to read.
2447
         * @return The function returns the read data or FALSE on failure. 
2448
         * @author Nicola Asuni
2449
         * @since 6.0.025
2450
         * @public static
2451
         */
2452
        public static function fileGetContents($file) {
2453
                //$file = html_entity_decode($file);
2454
                // array of possible alternative paths/URLs
2455
                $alt = array($file);
2456
                // replace URL relative path with full real server path
2457
                if ((strlen($file) > 1)
2458
                        AND ($file[0] == '/')
2459
                        AND ($file[1] != '/')
2460
                        AND !empty($_SERVER['DOCUMENT_ROOT'])
2461
                        AND ($_SERVER['DOCUMENT_ROOT'] != '/')) {
2462
                        $findroot = strpos($file, $_SERVER['DOCUMENT_ROOT']);
2463
                        if (($findroot === false) OR ($findroot > 1)) {
2464
                                if (substr($_SERVER['DOCUMENT_ROOT'], -1) == '/') {
2465
                                        $tmp = substr($_SERVER['DOCUMENT_ROOT'], 0, -1).$file;
2466
                                } else {
2467
                                        $tmp = $_SERVER['DOCUMENT_ROOT'].$file;
2468
                                }
2469
                                $alt[] = htmlspecialchars_decode(urldecode($tmp));
2470
                        }
2471
                }
2472
                // URL mode
2473
                $url = $file;
2474
                // check for missing protocol
2475
                if (preg_match('%^/{2}%', $url)) {
2476
                        if (preg_match('%^([^:]+:)//%i', K_PATH_URL, $match)) {
2477
                                $url = $match[1].str_replace(' ', '%20', $url);
2478
                                $alt[] = $url;
2479
                        }
2480
                }
2481
                $urldata = @parse_url($url);
2482
                if (!isset($urldata['query']) OR (strlen($urldata['query']) <= 0)) {
2483
                        if (K_PATH_URL AND (strpos($url, K_PATH_URL) === 0)) {
2484
                                // convert URL to full server path
2485
                                $tmp = str_replace(K_PATH_URL, K_PATH_MAIN, $url);
2486
                                $tmp = htmlspecialchars_decode(urldecode($tmp));
2487
                                $alt[] = $tmp;
2488
                        }
2489
                }
2490
                if (isset($_SERVER['SCRIPT_URI'])) {
2491
                        $urldata = @parse_url($_SERVER['SCRIPT_URI']);
2492
                        $alt[] = $urldata['scheme'].'://'.$urldata['host'].(($file[0] == '/') ? '' : '/').$file;
2493
                }
2494
                foreach ($alt as $f) {
2495
                        $ret = @file_get_contents($f);
2496
                        if (($ret === FALSE)
2497
                                AND !ini_get('allow_url_fopen')
2498
                                AND function_exists('curl_init')
2499
                                AND preg_match('%^(https?|ftp)://%', $f)) {
2500
                                // try to get remote file data using cURL
2501
                                $cs = curl_init(); // curl session
2502
                                curl_setopt($cs, CURLOPT_URL, $f);
2503
                                curl_setopt($cs, CURLOPT_BINARYTRANSFER, true);
2504
                                curl_setopt($cs, CURLOPT_FAILONERROR, true);
2505
                                curl_setopt($cs, CURLOPT_RETURNTRANSFER, true);
2506
                                if ((ini_get('open_basedir') == '') AND (!ini_get('safe_mode'))) {
2507
                                        curl_setopt($cs, CURLOPT_FOLLOWLOCATION, true);
2508
                                }
2509
                                curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 5);
2510
                                curl_setopt($cs, CURLOPT_TIMEOUT, 30);
2511
                                curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, false);
2512
                                curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, false);
2513
                                curl_setopt($cs, CURLOPT_USERAGENT, 'TCPDF');
2514
                                $ret = curl_exec($cs);
2515
                                curl_close($cs);
2516
                        }
2517
                        if ($ret !== FALSE) {
2518
                                break;
2519
                        }
2520
                }
2521
                return $ret;
2522
        }
2523

    
2524
} // END OF TCPDF_STATIC CLASS
2525

    
2526
//============================================================+
2527
// END OF FILE
2528
//============================================================+