source: trunk/filemanager/tp/dompdf/include/text_frame_reflower.cls.php @ 7673

Revision 7673, 12.1 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

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