root / drupal7 / sites / all / libraries / fpdi-1.5.4 / pdf_context.php @ 4b706e38
1 | 60370e04 | Assos Assos | <?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 | //
|
||
19 | |||
20 | /**
|
||
21 | * Class pdf_context
|
||
22 | */
|
||
23 | class pdf_context |
||
24 | { |
||
25 | /**
|
||
26 | * Mode
|
||
27 | *
|
||
28 | * @var integer 0 = file | 1 = string
|
||
29 | */
|
||
30 | protected $_mode = 0; |
||
31 | |||
32 | /**
|
||
33 | * @var resource|string
|
||
34 | */
|
||
35 | public $file; |
||
36 | |||
37 | /**
|
||
38 | * @var string
|
||
39 | */
|
||
40 | public $buffer; |
||
41 | |||
42 | /**
|
||
43 | * @var integer
|
||
44 | */
|
||
45 | public $offset; |
||
46 | |||
47 | /**
|
||
48 | * @var integer
|
||
49 | */
|
||
50 | public $length; |
||
51 | |||
52 | /**
|
||
53 | * @var array
|
||
54 | */
|
||
55 | public $stack; |
||
56 | |||
57 | /**
|
||
58 | * The constructor
|
||
59 | *
|
||
60 | * @param resource $f
|
||
61 | */
|
||
62 | public function __construct(&$f) |
||
63 | { |
||
64 | $this->file =& $f; |
||
65 | if (is_string($this->file)) |
||
66 | $this->_mode = 1; |
||
67 | |||
68 | $this->reset(); |
||
69 | } |
||
70 | |||
71 | /**
|
||
72 | * Get the position in the file stream
|
||
73 | *
|
||
74 | * @return int
|
||
75 | */
|
||
76 | public function getPos() |
||
77 | { |
||
78 | if ($this->_mode == 0) { |
||
79 | return ftell($this->file); |
||
80 | } else {
|
||
81 | return 0; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /**
|
||
86 | * Reset the position in the file stream.
|
||
87 | *
|
||
88 | * Optionally move the file pointer to a new location and reset the buffered data.
|
||
89 | *
|
||
90 | * @param null $pos
|
||
91 | * @param int $l
|
||
92 | */
|
||
93 | public function reset($pos = null, $l = 100) |
||
94 | { |
||
95 | if ($this->_mode == 0) { |
||
96 | if (!is_null($pos)) { |
||
97 | fseek ($this->file, $pos); |
||
98 | } |
||
99 | |||
100 | $this->buffer = $l > 0 ? fread($this->file, $l) : ''; |
||
101 | $this->length = strlen($this->buffer); |
||
102 | if ($this->length < $l) |
||
103 | $this->increaseLength($l - $this->length); |
||
104 | } else {
|
||
105 | $this->buffer = $this->file; |
||
106 | $this->length = strlen($this->buffer); |
||
107 | } |
||
108 | $this->offset = 0; |
||
109 | $this->stack = array(); |
||
110 | } |
||
111 | |||
112 | /**
|
||
113 | * Make sure that there is at least one character beyond the current offset in the buffer.
|
||
114 | *
|
||
115 | * To prevent the tokenizer from attempting to access data that does not exist.
|
||
116 | *
|
||
117 | * @return bool
|
||
118 | */
|
||
119 | public function ensureContent() |
||
120 | { |
||
121 | if ($this->offset >= $this->length - 1) { |
||
122 | return $this->increaseLength(); |
||
123 | } else {
|
||
124 | return true; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | /**
|
||
129 | * Forcefully read more data into the buffer
|
||
130 | *
|
||
131 | * @param int $l
|
||
132 | * @return bool
|
||
133 | */
|
||
134 | public function increaseLength($l = 100) |
||
135 | { |
||
136 | if ($this->_mode == 0 && feof($this->file)) { |
||
137 | return false; |
||
138 | } else if ($this->_mode == 0) { |
||
139 | $totalLength = $this->length + $l; |
||
140 | do {
|
||
141 | $toRead = $totalLength - $this->length; |
||
142 | if ($toRead < 1) |
||
143 | break;
|
||
144 | |||
145 | $this->buffer .= fread($this->file, $toRead); |
||
146 | } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file)); |
||
147 | |||
148 | return true; |
||
149 | } else {
|
||
150 | return false; |
||
151 | } |
||
152 | } |
||
153 | } |