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 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
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 | // Handle text transform |
---|
57 | $transform = $this->_frame->get_style()->text_transform; |
---|
58 | switch ( strtolower($transform) ) { |
---|
59 | case "capitalize": |
---|
60 | $this->_frame->set_text( ucwords($this->_frame->get_text()) ); |
---|
61 | break; |
---|
62 | |
---|
63 | case "uppercase": |
---|
64 | $this->_frame->set_text( strtoupper($this->_frame->get_text()) ); |
---|
65 | break; |
---|
66 | |
---|
67 | case "lowercase": |
---|
68 | $this->_frame->set_text( strtolower($this->_frame->get_text()) ); |
---|
69 | break; |
---|
70 | |
---|
71 | default: |
---|
72 | // Do nothing |
---|
73 | break; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | //........................................................................ |
---|
78 | |
---|
79 | protected function _collapse_white_space($text) { |
---|
80 | //$text = $this->_frame->get_text(); |
---|
81 | // if ( $this->_block_parent->get_current_line("w") == 0 ) |
---|
82 | // $text = ltrim($text, " \n\r\t"); |
---|
83 | return preg_replace("/[\s\n]+/u", " ", $text); |
---|
84 | } |
---|
85 | |
---|
86 | //........................................................................ |
---|
87 | |
---|
88 | protected function _line_break($text) { |
---|
89 | $style = $this->_frame->get_style(); |
---|
90 | $size = $style->font_size; |
---|
91 | $font = $style->font_family; |
---|
92 | |
---|
93 | // Determine the available width |
---|
94 | $line_width = $this->_frame->get_containing_block("w"); |
---|
95 | $current_line_width = $this->_block_parent->get_current_line("w"); |
---|
96 | |
---|
97 | $available_width = $line_width - $current_line_width; |
---|
98 | |
---|
99 | // split the text into words |
---|
100 | $words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
101 | $wc = count($words); |
---|
102 | |
---|
103 | // Account for word-spacing |
---|
104 | $word_spacing = $style->length_in_pt($style->word_spacing); |
---|
105 | |
---|
106 | // Determine the frame width including margin, padding & border |
---|
107 | $text_width = Font_Metrics::get_text_width($text, $font, $size, $word_spacing); |
---|
108 | $mbp_width = |
---|
109 | $style->length_in_pt( array( $style->margin_left, |
---|
110 | $style->border_left_width, |
---|
111 | $style->padding_left, |
---|
112 | $style->padding_right, |
---|
113 | $style->border_right_width, |
---|
114 | $style->margin_right), $line_width ); |
---|
115 | $frame_width = $text_width + $mbp_width; |
---|
116 | |
---|
117 | // Debugging: |
---|
118 | // pre_r("Text: '" . htmlspecialchars($text). "'"); |
---|
119 | // pre_r("width: " .$frame_width); |
---|
120 | // pre_r("textwidth + delta: $text_width + $mbp_width"); |
---|
121 | // pre_r("font-size: $size"); |
---|
122 | // pre_r("cb[w]: " .$line_width); |
---|
123 | // pre_r("available width: " . $available_width); |
---|
124 | // pre_r("current line width: " . $current_line_width); |
---|
125 | |
---|
126 | // pre_r($words); |
---|
127 | |
---|
128 | if ( $frame_width <= $available_width ) |
---|
129 | return false; |
---|
130 | |
---|
131 | // Determine the split point |
---|
132 | $width = 0; |
---|
133 | $str = ""; |
---|
134 | reset($words); |
---|
135 | |
---|
136 | for ($i = 0; $i < count($words); $i += 2) { |
---|
137 | $word = $words[$i] . (isset($words[$i+1]) ? $words[$i+1] : ""); |
---|
138 | $word_width = Font_Metrics::get_text_width($word, $font, $size, $word_spacing); |
---|
139 | if ( $width + $word_width + $mbp_width > $available_width ) |
---|
140 | break; |
---|
141 | |
---|
142 | $width += $word_width; |
---|
143 | $str .= $word; |
---|
144 | |
---|
145 | } |
---|
146 | |
---|
147 | // The first word has overflowed. Force it onto the line |
---|
148 | if ( $current_line_width == 0 && $width == 0 ) { |
---|
149 | $width += $word_width; |
---|
150 | $str .= $word; |
---|
151 | } |
---|
152 | |
---|
153 | $offset = mb_strlen($str); |
---|
154 | |
---|
155 | // More debugging: |
---|
156 | // pre_var_dump($str); |
---|
157 | // pre_r("Width: ". $width); |
---|
158 | // pre_r("Offset: " . $offset); |
---|
159 | |
---|
160 | return $offset; |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | //........................................................................ |
---|
165 | |
---|
166 | protected function _newline_break($text) { |
---|
167 | |
---|
168 | if ( ($i = mb_strpos($text, "\n")) === false) |
---|
169 | return false; |
---|
170 | |
---|
171 | return $i+1; |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | //........................................................................ |
---|
176 | |
---|
177 | protected function _layout_line() { |
---|
178 | $style = $this->_frame->get_style(); |
---|
179 | $text = $this->_frame->get_text(); |
---|
180 | $size = $style->font_size; |
---|
181 | $font = $style->font_family; |
---|
182 | $word_spacing = $style->length_in_pt($style->word_spacing); |
---|
183 | |
---|
184 | // Determine the text height |
---|
185 | $style->height = Font_Metrics::get_font_height( $font, $size ); |
---|
186 | |
---|
187 | $split = false; |
---|
188 | $add_line = false; |
---|
189 | |
---|
190 | // Handle text transform: |
---|
191 | // http://www.w3.org/TR/CSS21/text.html#propdef-text-transform |
---|
192 | |
---|
193 | switch ($style->text_transform) { |
---|
194 | |
---|
195 | default: |
---|
196 | break; |
---|
197 | |
---|
198 | case "capitalize": |
---|
199 | $text = mb_convert_case($text, MB_CASE_TITLE, 'UTF-8'); |
---|
200 | break; |
---|
201 | |
---|
202 | case "uppercase": |
---|
203 | $text = mb_convert_case($text, MB_CASE_UPPER, 'UTF-8'); |
---|
204 | break; |
---|
205 | |
---|
206 | case "lowercase": |
---|
207 | $text = mb_convert_case($text, MB_CASE_LOWER, 'UTF-8'); |
---|
208 | break; |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | // Handle white-space property: |
---|
213 | // http://www.w3.org/TR/CSS21/text.html#propdef-white-space |
---|
214 | |
---|
215 | switch ($style->white_space) { |
---|
216 | |
---|
217 | default: |
---|
218 | case "normal": |
---|
219 | $this->_frame->set_text( $text = $this->_collapse_white_space($text) ); |
---|
220 | if ( $text == "" ) |
---|
221 | break; |
---|
222 | |
---|
223 | $split = $this->_line_break($text); |
---|
224 | break; |
---|
225 | |
---|
226 | case "pre": |
---|
227 | $split = $this->_newline_break($text); |
---|
228 | $add_line = $split !== false; |
---|
229 | break; |
---|
230 | |
---|
231 | case "nowrap": |
---|
232 | $this->_frame->set_text( $text = $this->_collapse_white_space($text) ); |
---|
233 | break; |
---|
234 | |
---|
235 | case "pre-wrap": |
---|
236 | $split = $this->_newline_break($text); |
---|
237 | |
---|
238 | if ( ($tmp = $this->_line_break($text)) !== false ) { |
---|
239 | $add_line = $split < $tmp; |
---|
240 | $split = min($tmp, $split); |
---|
241 | } else |
---|
242 | $add_line = true; |
---|
243 | |
---|
244 | break; |
---|
245 | |
---|
246 | case "pre-line": |
---|
247 | // Collapse white-space except for \n |
---|
248 | $this->_frame->set_text( $text = preg_replace( "/[ \t]+/u", " ", $text ) ); |
---|
249 | |
---|
250 | if ( $text == "" ) |
---|
251 | break; |
---|
252 | |
---|
253 | $split = $this->_newline_break($text); |
---|
254 | |
---|
255 | if ( ($tmp = $this->_line_break($text)) !== false ) { |
---|
256 | $add_line = $split < $tmp; |
---|
257 | $split = min($tmp, $split); |
---|
258 | } else |
---|
259 | $add_line = true; |
---|
260 | |
---|
261 | break; |
---|
262 | |
---|
263 | } |
---|
264 | |
---|
265 | // Handle degenerate case |
---|
266 | if ( $text === "" ) |
---|
267 | return; |
---|
268 | |
---|
269 | if ( $split !== false) { |
---|
270 | |
---|
271 | // Handle edge cases |
---|
272 | if ( $split == 0 && $text == " " ) { |
---|
273 | $this->_frame->set_text(""); |
---|
274 | return; |
---|
275 | } |
---|
276 | |
---|
277 | if ( $split == 0 ) { |
---|
278 | |
---|
279 | // Trim newlines from the beginning of the line |
---|
280 | //$this->_frame->set_text(ltrim($text, "\n\r")); |
---|
281 | |
---|
282 | $this->_block_parent->add_line(); |
---|
283 | $this->_frame->position(); |
---|
284 | |
---|
285 | // Layout the new line |
---|
286 | $this->_layout_line(); |
---|
287 | |
---|
288 | } else if ( $split < mb_strlen($this->_frame->get_text()) ) { |
---|
289 | |
---|
290 | // split the line if required |
---|
291 | $this->_frame->split_text($split); |
---|
292 | |
---|
293 | // Remove any trailing newlines |
---|
294 | $t = $this->_frame->get_text(); |
---|
295 | |
---|
296 | if ( $split > 1 && $t{$split-1} == "\n" ) |
---|
297 | $this->_frame->set_text( mb_substr($t, 0, -1) ); |
---|
298 | |
---|
299 | } |
---|
300 | |
---|
301 | if ( $add_line ) { |
---|
302 | $this->_block_parent->add_line(); |
---|
303 | $this->_frame->position(); |
---|
304 | } |
---|
305 | |
---|
306 | // Set our new width |
---|
307 | $this->_frame->recalculate_width(); |
---|
308 | |
---|
309 | } else { |
---|
310 | |
---|
311 | $this->_frame->recalculate_width(); |
---|
312 | |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | //........................................................................ |
---|
317 | |
---|
318 | function reflow() { |
---|
319 | |
---|
320 | $this->_block_parent = $this->_frame->find_block_parent(); |
---|
321 | |
---|
322 | // Left trim the text if this is the first text on the line and we're |
---|
323 | // collapsing white space |
---|
324 | // if ( $this->_block_parent->get_current_line("w") == 0 && |
---|
325 | // ($this->_frame->get_style()->white_space != "pre" || |
---|
326 | // $this->_frame->get_style()->white_space != "pre-wrap") ) { |
---|
327 | // $this->_frame->set_text( ltrim( $this->_frame->get_text() ) ); |
---|
328 | // } |
---|
329 | |
---|
330 | $this->_frame->position(); |
---|
331 | |
---|
332 | $this->_layout_line(); |
---|
333 | |
---|
334 | } |
---|
335 | |
---|
336 | //........................................................................ |
---|
337 | |
---|
338 | // Returns an array(0 => min, 1 => max, "min" => min, "max" => max) of the |
---|
339 | // minimum and maximum widths of this frame |
---|
340 | function get_min_max_width() { |
---|
341 | |
---|
342 | $style = $this->_frame->get_style(); |
---|
343 | $this->_block_parent = $this->_frame->find_block_parent(); |
---|
344 | $line_width = $this->_frame->get_containing_block("w"); |
---|
345 | |
---|
346 | $str = $text = $this->_frame->get_text(); |
---|
347 | $size = $style->font_size; |
---|
348 | $font = $style->font_family; |
---|
349 | |
---|
350 | $spacing = $style->length_in_pt($style->word_spacing); |
---|
351 | |
---|
352 | switch($style->white_space) { |
---|
353 | |
---|
354 | default: |
---|
355 | case "normal": |
---|
356 | $str = preg_replace("/[\s\n]+/u"," ", $str); |
---|
357 | case "pre-wrap": |
---|
358 | case "pre-line": |
---|
359 | |
---|
360 | // Find the longest word (i.e. minimum length) |
---|
361 | |
---|
362 | // This technique (using arrays & an anonymous function) is actually |
---|
363 | // faster than doing a single-pass character by character scan. Heh, |
---|
364 | // yes I took the time to bench it ;) |
---|
365 | $words = array_flip(preg_split('/[\s-]+/u',$str, -1, PREG_SPLIT_DELIM_CAPTURE)); |
---|
366 | array_walk($words, create_function('&$val,$str', |
---|
367 | '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$spacing.');')); |
---|
368 | arsort($words); |
---|
369 | $min = reset($words); |
---|
370 | break; |
---|
371 | |
---|
372 | case "pre": |
---|
373 | $lines = array_flip(preg_split('/\n/u', $str)); |
---|
374 | array_walk($lines, create_function('&$val,$str', |
---|
375 | '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$spacing.');')); |
---|
376 | |
---|
377 | arsort($lines); |
---|
378 | $min = reset($lines); |
---|
379 | break; |
---|
380 | |
---|
381 | case "nowrap": |
---|
382 | $min = Font_Metrics::get_text_width($this->_collapse_white_space($str), $font, $size, $spacing); |
---|
383 | break; |
---|
384 | |
---|
385 | } |
---|
386 | |
---|
387 | switch ($style->white_space) { |
---|
388 | |
---|
389 | default: |
---|
390 | case "normal": |
---|
391 | case "nowrap": |
---|
392 | $str = preg_replace("/[\s\n]+/u"," ", $text); |
---|
393 | break; |
---|
394 | |
---|
395 | case "pre-line": |
---|
396 | $str = preg_replace( "/[ \t]+/u", " ", $text); |
---|
397 | |
---|
398 | case "pre-wrap": |
---|
399 | // Find the longest word (i.e. minimum length) |
---|
400 | $lines = array_flip(preg_split('/\n/', $text)); |
---|
401 | array_walk($lines, create_function('&$val,$str', |
---|
402 | '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$spacing.');')); |
---|
403 | arsort($lines); |
---|
404 | reset($lines); |
---|
405 | $str = key($lines); |
---|
406 | break; |
---|
407 | |
---|
408 | } |
---|
409 | |
---|
410 | $max = Font_Metrics::get_text_width($str, $font, $size, $spacing); |
---|
411 | |
---|
412 | $delta = $style->length_in_pt(array($style->margin_left, |
---|
413 | $style->border_left_width, |
---|
414 | $style->padding_left, |
---|
415 | $style->padding_right, |
---|
416 | $style->border_right_width, |
---|
417 | $style->margin_right), $line_width); |
---|
418 | $min += $delta; |
---|
419 | $max += $delta; |
---|
420 | |
---|
421 | return array($min, $max, "min" => $min, "max" => $max); |
---|
422 | |
---|
423 | } |
---|
424 | |
---|
425 | } |
---|
426 | ?> |
---|