f404039eeed172a7ff76a850e58acae7bb3154e4
[moodle.git] / mod / assign / feedback / editpdf / fpdi / pdf_context.php
1 <?php\r
2 //\r
3 //  FPDI - Version 1.4.4\r
4 //\r
5 //    Copyright 2004-2013 Setasign - Jan Slabon\r
6 //\r
7 //  Licensed under the Apache License, Version 2.0 (the "License");\r
8 //  you may not use this file except in compliance with the License.\r
9 //  You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 //  Unless required by applicable law or agreed to in writing, software\r
14 //  distributed under the License is distributed on an "AS IS" BASIS,\r
15 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 //  See the License for the specific language governing permissions and\r
17 //  limitations under the License.\r
18 //\r
19 \r
20 if (!class_exists('pdf_context', false)) {\r
21     \r
22     class pdf_context {\r
23     \r
24         /**\r
25          * Modi\r
26          *\r
27          * @var integer 0 = file | 1 = string\r
28          */\r
29         var $_mode = 0;\r
30         \r
31         var $file;\r
32         var $buffer;\r
33         var $offset;\r
34         var $length;\r
35     \r
36         var $stack;\r
37     \r
38         // Constructor\r
39     \r
40         function pdf_context(&$f) {\r
41                 $this->file =& $f;\r
42                 if (is_string($this->file))\r
43                     $this->_mode = 1;\r
44                 $this->reset();\r
45         }\r
46     \r
47         // Optionally move the file\r
48         // pointer to a new location\r
49         // and reset the buffered data\r
50     \r
51         function reset($pos = null, $l = 100) {\r
52             if ($this->_mode == 0) {\r
53                 if (!is_null ($pos)) {\r
54                                 fseek ($this->file, $pos);\r
55                         }\r
56         \r
57                         $this->buffer = $l > 0 ? fread($this->file, $l) : '';\r
58                         $this->length = strlen($this->buffer);\r
59                         if ($this->length < $l)\r
60                     $this->increase_length($l - $this->length);\r
61             } else {\r
62                 $this->buffer = $this->file;\r
63                 $this->length = strlen($this->buffer);\r
64             }\r
65                 $this->offset = 0;\r
66                 $this->stack = array();\r
67         }\r
68     \r
69         // Make sure that there is at least one\r
70         // character beyond the current offset in\r
71         // the buffer to prevent the tokenizer\r
72         // from attempting to access data that does\r
73         // not exist\r
74     \r
75         function ensure_content() {\r
76                 if ($this->offset >= $this->length - 1) {\r
77                         return $this->increase_length();\r
78                 } else {\r
79                         return true;\r
80                 }\r
81         }\r
82     \r
83         // Forcefully read more data into the buffer\r
84     \r
85         function increase_length($l = 100) {\r
86                         if ($this->_mode == 0 && feof($this->file)) {\r
87                                 return false;\r
88                         } elseif ($this->_mode == 0) {\r
89                             $totalLength = $this->length + $l;\r
90                             do {\r
91                                 $toRead = $totalLength - $this->length;\r
92                                 if ($toRead < 1)\r
93                                         break;\r
94                             \r
95                                 $this->buffer .= fread($this->file, $toRead);\r
96                     } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));\r
97                                 \r
98                                 return true;\r
99                         } else {\r
100                         return false;\r
101                         }\r
102                 }\r
103     }\r