MDL-49515 libraries: Update to editpdf/classes/pdf.php
[moodle.git] / mod / assign / feedback / editpdf / fpdi / fpdi_bridge.php
1 <?php
2 //
3 //  FPDI - Version 1.5.4
4 //
5 //    Copyright 2004-2015 Setasign - Jan Slabon
6 //
7 //  Licensed under the Apache License, Version 2.0 (the "License");
8 //  you may not use this file except in compliance with the License.
9 //  You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //  Unless required by applicable law or agreed to in writing, software
14 //  distributed under the License is distributed on an "AS IS" BASIS,
15 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //  See the License for the specific language governing permissions and
17 //  limitations under the License.
18 //
20 /**
21  * This file is used as a bridge between TCPDF or FPDF
22  * It will dynamically create the class extending the available
23  * class FPDF or TCPDF.
24  *
25  * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
26  */
28 if (!class_exists('TCPDF', false)) {
29     /**
30      * Class fpdi_bridge
31      */
32     class fpdi_bridge extends FPDF
33     {
34         // empty body
35     }
37 } else {
39     /**
40      * Class fpdi_bridge
41      *
42      * This has been modified to use the Moodle pdflib wrapper which in turn extends TCPDF
43      */
44     class fpdi_bridge extends pdf
45     {
46         /**
47          * Array of Tpl-Data
48          *
49          * @var array
50          */
51         protected $_tpls = array();
53         /**
54          * Name-prefix of Templates used in Resources-Dictionary
55          *
56          * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
57          */
58         public $tplPrefix = "/TPL";
60         /**
61          * Current Object Id.
62          *
63          * @var integer
64          */
65         protected $_currentObjId;
67         /**
68          * Return XObjects Dictionary.
69          *
70          * Overwritten to add additional XObjects to the resources dictionary of TCPDF
71          *
72          * @return string
73          */
74         protected function _getxobjectdict()
75         {
76             $out = parent::_getxobjectdict();
77             foreach ($this->_tpls as $tplIdx => $tpl) {
78                 $out .= sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n']);
79             }
81             return $out;
82         }
84         /**
85          * Writes a PDF value to the resulting document.
86          *
87          * Prepares the value for encryption of imported data by FPDI
88          *
89          * @param array $value
90          */
91         protected function _prepareValue(&$value)
92         {
93             switch ($value[0]) {
94                 case pdf_parser::TYPE_STRING:
95                     if ($this->encrypted) {
96                         $value[1] = $this->_unescape($value[1]);
97                         $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
98                         $value[1] = TCPDF_STATIC::_escape($value[1]);
99                     }
100                     break;
102                 case pdf_parser::TYPE_STREAM:
103                     if ($this->encrypted) {
104                         $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]);
105                         $value[1][1]['/Length'] = array(
106                             pdf_parser::TYPE_NUMERIC,
107                             strlen($value[2][1])
108                         );
109                     }
110                     break;
112                 case pdf_parser::TYPE_HEX:
113                     if ($this->encrypted) {
114                         $value[1] = $this->hex2str($value[1]);
115                         $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
117                         // remake hexstring of encrypted string
118                         $value[1] = $this->str2hex($value[1]);
119                     }
120                     break;
121             }
122         }
124         /**
125          * Un-escapes a PDF string
126          *
127          * @param string $s
128          * @return string
129          */
130         protected function _unescape($s)
131         {
132             $out = '';
133             for ($count = 0, $n = strlen($s); $count < $n; $count++) {
134                 if ($s[$count] != '\\' || $count == $n-1) {
135                     $out .= $s[$count];
136                 } else {
137                     switch ($s[++$count]) {
138                         case ')':
139                         case '(':
140                         case '\\':
141                             $out .= $s[$count];
142                             break;
143                         case 'f':
144                             $out .= chr(0x0C);
145                             break;
146                         case 'b':
147                             $out .= chr(0x08);
148                             break;
149                         case 't':
150                             $out .= chr(0x09);
151                             break;
152                         case 'r':
153                             $out .= chr(0x0D);
154                             break;
155                         case 'n':
156                             $out .= chr(0x0A);
157                             break;
158                         case "\r":
159                             if ($count != $n-1 && $s[$count+1] == "\n")
160                                 $count++;
161                             break;
162                         case "\n":
163                             break;
164                         default:
165                             // Octal-Values
166                             if (ord($s[$count]) >= ord('0') &&
167                                 ord($s[$count]) <= ord('9')) {
168                                 $oct = ''. $s[$count];
170                                 if (ord($s[$count+1]) >= ord('0') &&
171                                     ord($s[$count+1]) <= ord('9')) {
172                                     $oct .= $s[++$count];
174                                     if (ord($s[$count+1]) >= ord('0') &&
175                                         ord($s[$count+1]) <= ord('9')) {
176                                         $oct .= $s[++$count];
177                                     }
178                                 }
180                                 $out .= chr(octdec($oct));
181                             } else {
182                                 $out .= $s[$count];
183                             }
184                     }
185                 }
186             }
187             return $out;
188         }
190         /**
191          * Hexadecimal to string
192          *
193          * @param string $data
194          * @return string
195          */
196         public function hex2str($data)
197         {
198             $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
199             if ((strlen($data) % 2) == 1) {
200                 $data .= '0';
201             }
203             return pack('H*', $data);
204         }
206         /**
207          * String to hexadecimal
208          *
209          * @param string $str
210          * @return string
211          */
212         public function str2hex($str)
213         {
214             return current(unpack('H*', $str));
215         }
216     }