1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: text_frame_reflower.cls.php,v $ |
---|
6 | * Created on: 2004-06-17 |
---|
7 | * |
---|
8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
9 | * |
---|
10 | * This library is free software; you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU Lesser General Public |
---|
12 | * License as published by the Free Software Foundation; either |
---|
13 | * version 2.1 of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * This library is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * Lesser General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU Lesser General Public License |
---|
21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
23 | * 02111-1307 USA |
---|
24 | * |
---|
25 | * Alternatively, you may distribute this software under the terms of the |
---|
26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
29 | * |
---|
30 | * The latest version of DOMPDF might be available at: |
---|
31 | * http://www.digitaljunkies.ca/dompdf |
---|
32 | * |
---|
33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
34 | * @copyright 2004 Benj Carson |
---|
35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
36 | * @package dompdf |
---|
37 | * @version 0.5.1 |
---|
38 | */ |
---|
39 | |
---|
40 | /* $Id: text_frame_reflower.cls.php,v 1.8 2006/07/07 21:31:05 benjcarson Exp $ */ |
---|
41 | |
---|
42 | /** |
---|
43 | * Reflows text frames. |
---|
44 | * |
---|
45 | * @access private |
---|
46 | * @package dompdf |
---|
47 | */ |
---|
48 | class Text_Frame_Reflower extends Frame_Reflower { |
---|
49 | |
---|
50 | protected $_block_parent; // Nearest block-level ancestor |
---|
51 | |
---|
52 | function __construct(Text_Frame_Decorator $frame) { |
---|
53 | parent::__construct($frame); |
---|
54 | $this->_block_parent = null; |
---|
55 | } |
---|
56 | |
---|
57 | //........................................................................ |
---|
58 | |
---|
59 | protected function _collapse_white_space($text) { |
---|
60 | //$text = $this->_frame->get_text(); |
---|
61 | if ( $this->_block_parent->get_current_line("w") == 0 ) |
---|
62 | $text = ltrim($text, " \n\r\t"); |
---|
63 | return preg_replace("/[\s\n]+/u", " ", $text); |
---|
64 | } |
---|
65 | |
---|
66 | //........................................................................ |
---|
67 | |
---|
68 | protected function _line_break($text) { |
---|
69 | $style = $this->_frame->get_style(); |
---|
70 | $size = $style->font_size; |
---|
71 | $font = $style->font_family; |
---|
72 | |
---|
73 | // Determine the available width |
---|
74 | $line_width = $this->_frame->get_containing_block("w"); |
---|
75 | $current_line_width = $this->_block_parent->get_current_line("w"); |
---|
76 | |
---|
77 | $available_width = $line_width - $current_line_width; |
---|
78 | |
---|
79 | // split the text into words |
---|
80 | $words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
81 | $wc = count($words); |
---|
82 | |
---|
83 | // Account for word-spacing |
---|
84 | $word_spacing = $style->length_in_pt($style->word_spacing); |
---|
85 | |
---|
86 | // Determine the frame width including margin, padding & border |
---|
87 | $text_width = Font_Metrics::get_text_width($text, $font, $size, $word_spacing); |
---|
88 | $mbp_width = |
---|
89 | $style->length_in_pt( array( $style->margin_left, |
---|
90 | $style->border_left_width, |
---|
91 | $style->padding_left, |
---|
92 | $style->padding_right, |
---|
93 | $style->border_right_width, |
---|
94 | $style->margin_right), $line_width ); |
---|
95 | $frame_width = $text_width + $mbp_width; |
---|
96 | |
---|
97 | // Debugging: |
---|
98 | // pre_r("Text: '" . htmlspecialchars($text). "'"); |
---|
99 | // pre_r("width: " .$frame_width); |
---|
100 | // pre_r("textwidth + delta: $text_width + $mbp_width"); |
---|
101 | // pre_r("font-size: $size"); |
---|
102 | // pre_r("cb[w]: " .$line_width); |
---|
103 | // pre_r("available width: " . $available_width); |
---|
104 | // pre_r("current line width: " . $current_line_width); |
---|
105 | |
---|
106 | // pre_r($words); |
---|
107 | |
---|
108 | if ( $frame_width <= $available_width ) |
---|
109 | return false; |
---|
110 | |
---|
111 | // Determine the split point |
---|
112 | $width = 0; |
---|
113 | $str = ""; |
---|
114 | reset($words); |
---|
115 | |
---|
116 | for ($i = 0; $i < count($words); $i += 2) { |
---|
117 | $word = $words[$i] . (isset($words[$i+1]) ? $words[$i+1] : ""); |
---|
118 | $word_width = Font_Metrics::get_text_width($word, $font, $size, $word_spacing); |
---|
119 | if ( $width + $word_width + $mbp_width > $available_width ) |
---|
120 | break; |
---|
121 | |
---|
122 | $width += $word_width; |
---|
123 | $str .= $word; |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | // The first word has overflowed. Force it onto the line |
---|
128 | if ( $current_line_width == 0 && $width == 0 ) { |
---|
129 | $width += $word_width; |
---|
130 | $str .= $word; |
---|
131 | } |
---|
132 | |
---|
133 | $offset = mb_strlen($str); |
---|
134 | |
---|
135 | // More debugging: |
---|
136 | // pre_var_dump($str); |
---|
137 | // pre_r("Width: ". $width); |
---|
138 | // pre_r("Offset: " . $offset); |
---|
139 | |
---|
140 | return $offset; |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | //........................................................................ |
---|
145 | |
---|
146 | protected function _newline_break($text) { |
---|
147 | |
---|
148 | if ( ($i = mb_strpos($text, "\n")) === false) |
---|
149 | return false; |
---|
150 | |
---|
151 | return $i+1; |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | //........................................................................ |
---|
156 | |
---|
157 | protected function _layout_line() { |
---|
158 | $style = $this->_frame->get_style(); |
---|
159 | $text = $this->_frame->get_text(); |
---|
160 | $size = $style->font_size; |
---|
161 | $font = $style->font_family; |
---|
162 | $word_spacing = $style->length_in_pt($style->word_spacing); |
---|
163 | |
---|
164 | // Determine the text height |
---|
165 | $style->height = Font_Metrics::get_font_height( $font, $size ); |
---|
166 | |
---|
167 | $split = false; |
---|
168 | $add_line = false; |
---|
169 | |
---|
170 | // Handle white-space property: |
---|
171 | // http://www.w3.org/TR/CSS21/text.html#propdef-white-space |
---|
172 | |
---|
173 | switch ($style->white_space) { |
---|
174 | |
---|
175 | default: |
---|
176 | case "normal": |
---|
177 | $this->_frame->set_text( $text = $this->_collapse_white_space($text) ); |
---|
178 | if ( $text == "" ) |
---|
179 | break; |
---|
180 | |
---|
181 | $split = $this->_line_break($text); |
---|
182 | break; |
---|
183 | |
---|
184 | case "pre": |
---|
185 | $split = $this->_newline_break($text); |
---|
186 | $add_line = $split !== false; |
---|
187 | break; |
---|
188 | |
---|
189 | case "nowrap": |
---|
190 | $this->_frame->set_text( $text = $this->_collapse_white_space($text) ); |
---|
191 | break; |
---|
192 | |
---|
193 | case "pre-wrap": |
---|
194 | $split = $this->_newline_break($text); |
---|
195 | |
---|
196 | if ( ($tmp = $this->_line_break($text)) !== false ) { |
---|
197 | $add_line = $split < $tmp; |
---|
198 | $split = min($tmp, $split); |
---|
199 | } else |
---|
200 | $add_line = true; |
---|
201 | |
---|
202 | break; |
---|
203 | |
---|
204 | case "pre-line": |
---|
205 | // Collapse white-space except for \n |
---|
206 | $this->_frame->set_text( $text = preg_replace( "/[ \t]+/u", " ", $text ) ); |
---|
207 | |
---|
208 | if ( $text == "" ) |
---|
209 | break; |
---|
210 | |
---|
211 | $split = $this->_newline_break($text); |
---|
212 | |
---|
213 | if ( ($tmp = $this->_line_break($text)) !== false ) { |
---|
214 | $add_line = $split < $tmp; |
---|
215 | $split = min($tmp, $split); |
---|
216 | } else |
---|
217 | $add_line = true; |
---|
218 | |
---|
219 | break; |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | // Handle degenerate case |
---|
224 | if ( $text === "" ) |
---|
225 | return; |
---|
226 | |
---|
227 | if ( $split !== false) { |
---|
228 | |
---|
229 | // Handle edge cases |
---|
230 | if ( $split == 0 && $text == " " ) { |
---|
231 | $this->_frame->set_text(""); |
---|
232 | return; |
---|
233 | } |
---|
234 | |
---|
235 | if ( $split == 0 ) { |
---|
236 | |
---|
237 | // Trim newlines from the beginning of the line |
---|
238 | //$this->_frame->set_text(ltrim($text, "\n\r")); |
---|
239 | |
---|
240 | $this->_block_parent->add_line(); |
---|
241 | $this->_frame->position(); |
---|
242 | |
---|
243 | // Layout the new line |
---|
244 | $this->_layout_line(); |
---|
245 | |
---|
246 | } else if ( $split < mb_strlen($this->_frame->get_text()) ) { |
---|
247 | |
---|
248 | // split the line if required |
---|
249 | $this->_frame->split_text($split); |
---|
250 | |
---|
251 | // Remove any trailing newlines |
---|
252 | $t = $this->_frame->get_text(); |
---|
253 | |
---|
254 | if ( $split > 1 && $t{$split-1} == "\n" ) |
---|
255 | $this->_frame->set_text( mb_substr($t, 0, -1) ); |
---|
256 | |
---|
257 | } |
---|
258 | |
---|
259 | if ( $add_line ) { |
---|
260 | $this->_block_parent->add_line(); |
---|
261 | $this->_frame->position(); |
---|
262 | } |
---|
263 | |
---|
264 | // Set our new width |
---|
265 | $width = Font_Metrics::get_text_width($this->_frame->get_text(), $font, $size, $word_spacing); |
---|
266 | $style->width = $width; |
---|
267 | |
---|
268 | } else { |
---|
269 | // Add the current frame to the line |
---|
270 | $width = Font_Metrics::get_text_width($text, $font, $size, $word_spacing); |
---|
271 | $style->width = $width; |
---|
272 | |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | //........................................................................ |
---|
277 | |
---|
278 | function reflow() { |
---|
279 | |
---|
280 | $this->_block_parent = $this->_frame->find_block_parent(); |
---|
281 | |
---|
282 | $this->_frame->position(); |
---|
283 | |
---|
284 | $this->_layout_line(); |
---|
285 | |
---|
286 | } |
---|
287 | |
---|
288 | //........................................................................ |
---|
289 | |
---|
290 | // Returns an array(0 => min, 1 => max, "min" => min, "max" => max) of the |
---|
291 | // minimum and maximum widths of this frame |
---|
292 | function get_min_max_width() { |
---|
293 | |
---|
294 | $style = $this->_frame->get_style(); |
---|
295 | $this->_block_parent = $this->_frame->find_block_parent(); |
---|
296 | $line_width = $this->_frame->get_containing_block("w"); |
---|
297 | |
---|
298 | $str = $text = $this->_frame->get_text(); |
---|
299 | $size = $style->font_size; |
---|
300 | $font = $style->font_family; |
---|
301 | |
---|
302 | $spacing = $style->length_in_pt($style->word_spacing); |
---|
303 | |
---|
304 | switch($style->white_space) { |
---|
305 | |
---|
306 | default: |
---|
307 | case "normal": |
---|
308 | $str = preg_replace("/[\s\n]+/u"," ", $str); |
---|
309 | case "pre-wrap": |
---|
310 | case "pre-line": |
---|
311 | |
---|
312 | // Find the longest word (i.e. minimum length) |
---|
313 | |
---|
314 | // This technique (using arrays & an anonymous function) is actually |
---|
315 | // faster than doing a single-pass character by character scan. Heh, |
---|
316 | // yes I took the time to bench it ;) |
---|
317 | $words = array_flip(preg_split("/[\s-]+/u",$str, -1, PREG_SPLIT_DELIM_CAPTURE)); |
---|
318 | array_walk($words, create_function('&$val,$str', |
---|
319 | '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$spacing.');')); |
---|
320 | arsort($words); |
---|
321 | $min = reset($words); |
---|
322 | break; |
---|
323 | |
---|
324 | case "pre": |
---|
325 | $lines = array_flip(preg_split("/\n/u", $str)); |
---|
326 | array_walk($lines, create_function('&$val,$str', |
---|
327 | '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$spacing.');')); |
---|
328 | |
---|
329 | arsort($lines); |
---|
330 | $min = reset($lines); |
---|
331 | break; |
---|
332 | |
---|
333 | case "nowrap": |
---|
334 | $min = Font_Metrics::get_text_width($this->_collapse_white_space($str), $font, $size, $spacing); |
---|
335 | break; |
---|
336 | |
---|
337 | } |
---|
338 | |
---|
339 | switch ($style->white_space) { |
---|
340 | |
---|
341 | default: |
---|
342 | case "normal": |
---|
343 | case "nowrap": |
---|
344 | $str = preg_replace("/[\s\n]+/u"," ", $text); |
---|
345 | break; |
---|
346 | |
---|
347 | case "pre-line": |
---|
348 | $str = preg_replace( "/[ \t]+/u", " ", $text); |
---|
349 | |
---|
350 | case "pre-wrap": |
---|
351 | // Find the longest word (i.e. minimum length) |
---|
352 | $lines = array_flip(preg_split("/\n/", $text)); |
---|
353 | array_walk($lines, create_function('&$val,$str', |
---|
354 | '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$spacing.');')); |
---|
355 | arsort($lines); |
---|
356 | reset($lines); |
---|
357 | $str = key($lines); |
---|
358 | break; |
---|
359 | |
---|
360 | } |
---|
361 | |
---|
362 | $max = Font_Metrics::get_text_width($str, $font, $size, $spacing); |
---|
363 | |
---|
364 | $delta = $style->length_in_pt(array($style->margin_left, |
---|
365 | $style->border_left_width, |
---|
366 | $style->padding_left, |
---|
367 | $style->padding_right, |
---|
368 | $style->border_right_width, |
---|
369 | $style->margin_right), $line_width); |
---|
370 | $min += $delta; |
---|
371 | $max += $delta; |
---|
372 | |
---|
373 | return array($min, $max, "min" => $min, "max" => $max); |
---|
374 | |
---|
375 | } |
---|
376 | |
---|
377 | } |
---|
378 | ?> |
---|