1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: text_frame_decorator.cls.php,v $ |
---|
6 | * Created on: 2004-06-04 |
---|
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_decorator.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
41 | |
---|
42 | /** |
---|
43 | * Decorates Frame objects for text layout |
---|
44 | * |
---|
45 | * @access private |
---|
46 | * @package dompdf |
---|
47 | */ |
---|
48 | class Text_Frame_Decorator extends Frame_Decorator { |
---|
49 | |
---|
50 | // protected members |
---|
51 | protected $_text_spacing; |
---|
52 | |
---|
53 | function __construct(Frame $frame, DOMPDF $dompdf) { |
---|
54 | if ( $frame->get_node()->nodeName != "#text" ) |
---|
55 | throw new DOMPDF_Exception("Text_Decorator can only be applied to #text nodes."); |
---|
56 | |
---|
57 | parent::__construct($frame, $dompdf); |
---|
58 | $this->_text_spacing = null; |
---|
59 | } |
---|
60 | |
---|
61 | //........................................................................ |
---|
62 | |
---|
63 | function reset() { |
---|
64 | parent::reset(); |
---|
65 | $this->_text_spacing = null; |
---|
66 | } |
---|
67 | |
---|
68 | //........................................................................ |
---|
69 | |
---|
70 | // Accessor methods |
---|
71 | function get_text_spacing() { return $this->_text_spacing; } |
---|
72 | |
---|
73 | function get_text() { |
---|
74 | // FIXME: this should be in a child class (and is incorrect) |
---|
75 | if ( $this->_frame->get_style()->content !== "normal" ) { |
---|
76 | $this->_frame->get_node()->data = $this->_frame->get_style()->content; |
---|
77 | $this->_frame->get_style()->content = "normal"; |
---|
78 | } |
---|
79 | |
---|
80 | // pre_r("---"); |
---|
81 | // $style = $this->_frame->get_style(); |
---|
82 | // var_dump($text = $this->_frame->get_node()->data); |
---|
83 | // var_dump($asc = utf8_decode($text)); |
---|
84 | // for ($i = 0; $i < strlen($asc); $i++) |
---|
85 | // pre_r("$i: " . $asc{$i} . " - " . ord($asc{$i})); |
---|
86 | // pre_r("width: " . Font_Metrics::get_text_width($text, $style->font_family, $style->font_size)); |
---|
87 | |
---|
88 | return $this->_frame->get_node()->data; |
---|
89 | } |
---|
90 | |
---|
91 | //........................................................................ |
---|
92 | |
---|
93 | // Vertical margins & padding do not apply to text frames |
---|
94 | |
---|
95 | // http://www.w3.org/TR/CSS21/visudet.html#inline-non-replaced: |
---|
96 | // |
---|
97 | // The vertical padding, border and margin of an inline, non-replaced box |
---|
98 | // start at the top and bottom of the content area, not the |
---|
99 | // 'line-height'. But only the 'line-height' is used to calculate the |
---|
100 | // height of the line box. |
---|
101 | function get_margin_height() { |
---|
102 | // This function is called in add_frame_to_line() and is used to |
---|
103 | // determine the line height, so we actually want to return the |
---|
104 | // 'line-height' property, not the actual margin box |
---|
105 | $style = $this->get_style(); |
---|
106 | $font = $style->font_family; |
---|
107 | $size = $style->font_size; |
---|
108 | |
---|
109 | /* |
---|
110 | pre_r('-----'); |
---|
111 | pre_r($style->line_height); |
---|
112 | pre_r($style->font_size); |
---|
113 | pre_r(Font_Metrics::get_font_height($font, $size)); |
---|
114 | pre_r(($style->line_height / $size) * Font_Metrics::get_font_height($font, $size)); |
---|
115 | */ |
---|
116 | |
---|
117 | return ($style->line_height / $size) * Font_Metrics::get_font_height($font, $size); |
---|
118 | |
---|
119 | } |
---|
120 | |
---|
121 | function get_padding_box() { |
---|
122 | $pb = $this->_frame->get_padding_box(); |
---|
123 | $pb[3] = $pb["h"] = $this->_frame->get_style()->height; |
---|
124 | return $pb; |
---|
125 | } |
---|
126 | //........................................................................ |
---|
127 | |
---|
128 | // Set method |
---|
129 | function set_text_spacing($spacing) { |
---|
130 | $this->_text_spacing = $spacing; |
---|
131 | |
---|
132 | $style = $this->_frame->get_style(); |
---|
133 | // Re-adjust our width to account for the change in spacing |
---|
134 | $style->width = Font_Metrics::get_text_width($this->get_text(), $style->font_family, $style->font_size, $spacing); |
---|
135 | } |
---|
136 | |
---|
137 | //........................................................................ |
---|
138 | |
---|
139 | // Recalculate the text width |
---|
140 | function recalculate_width() { |
---|
141 | $style = $this->get_style(); |
---|
142 | $text = $this->get_text(); |
---|
143 | $size = $style->font_size; |
---|
144 | $font = $style->font_family; |
---|
145 | $word_spacing = $style->length_in_pt($style->word_spacing); |
---|
146 | |
---|
147 | $style->width = Font_Metrics::get_text_width($text, $font, $size, $word_spacing); |
---|
148 | } |
---|
149 | |
---|
150 | //........................................................................ |
---|
151 | |
---|
152 | // Text manipulation methods |
---|
153 | |
---|
154 | // split the text in this frame at the offset specified. The remaining |
---|
155 | // text is added a sibling frame following this one and is returned. |
---|
156 | function split_text($offset) { |
---|
157 | if ( $offset == 0 ) |
---|
158 | return; |
---|
159 | |
---|
160 | $split = $this->_frame->get_node()->splitText($offset); |
---|
161 | $deco = $this->copy($split); |
---|
162 | |
---|
163 | $p = $this->get_parent(); |
---|
164 | $p->insert_child_after($deco, $this, false); |
---|
165 | |
---|
166 | if ( $p instanceof Inline_Frame_Decorator ) |
---|
167 | $p->split($deco); |
---|
168 | |
---|
169 | } |
---|
170 | |
---|
171 | //........................................................................ |
---|
172 | |
---|
173 | function delete_text($offset, $count) { |
---|
174 | $this->_frame->get_node()->deleteData($offset, $count); |
---|
175 | } |
---|
176 | |
---|
177 | //........................................................................ |
---|
178 | |
---|
179 | function set_text($text) { |
---|
180 | $this->_frame->get_node()->data = $text; |
---|
181 | } |
---|
182 | |
---|
183 | } |
---|
184 | ?> |
---|