4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Brian Sweeney <eclecticgeek@gmail.com>
7 * @author Fabien Ménager <fabien.menager@gmail.com>
8 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
10 namespace Dompdf\FrameDecorator
;
16 use Dompdf\FontMetrics
;
19 * Decorates Frame objects for text layout
24 class Text
extends AbstractFrameDecorator
28 protected $_text_spacing;
30 function __construct(Frame
$frame, Dompdf
$dompdf)
32 if (!$frame->is_text_node()) {
33 throw new Exception("Text_Decorator can only be applied to #text nodes.");
36 parent
::__construct($frame, $dompdf);
37 $this->_text_spacing
= null;
40 //........................................................................
45 $this->_text_spacing
= null;
48 //........................................................................
51 function get_text_spacing()
53 return $this->_text_spacing
;
58 // FIXME: this should be in a child class (and is incorrect)
59 // if ( $this->_frame->get_style()->content !== "normal" ) {
60 // $this->_frame->get_node()->data = $this->_frame->get_style()->content;
61 // $this->_frame->get_style()->content = "normal";
64 // Helpers::pre_r("---");
65 // $style = $this->_frame->get_style();
66 // var_dump($text = $this->_frame->get_node()->data);
67 // var_dump($asc = utf8_decode($text));
68 // for ($i = 0; $i < strlen($asc); $i++)
69 // Helpers::pre_r("$i: " . $asc[$i] . " - " . ord($asc[$i]));
70 // Helpers::pre_r("width: " . $this->_dompdf->getFontMetrics()->getTextWidth($text, $style->font_family, $style->font_size));
72 return $this->_frame
->get_node()->data
;
75 //........................................................................
77 // Vertical margins & padding do not apply to text frames
79 // http://www.w3.org/TR/CSS21/visudet.html#inline-non-replaced:
81 // The vertical padding, border and margin of an inline, non-replaced box
82 // start at the top and bottom of the content area, not the
83 // 'line-height'. But only the 'line-height' is used to calculate the
84 // height of the line box.
85 function get_margin_height()
87 // This function is called in add_frame_to_line() and is used to
88 // determine the line height, so we actually want to return the
89 // 'line-height' property, not the actual margin box
90 $style = $this->get_parent()->get_style();
91 $font = $style->font_family
;
92 $size = $style->font_size
;
95 Helpers::pre_r('-----');
96 Helpers::pre_r($style->line_height);
97 Helpers::pre_r($style->font_size);
98 Helpers::pre_r($this->_dompdf->getFontMetrics()->getFontHeight($font, $size));
99 Helpers::pre_r(($style->line_height / $size) * $this->_dompdf->getFontMetrics()->getFontHeight($font, $size));
102 return ($style->line_height
/ ($size > 0 ?
$size : 1)) * $this->_dompdf
->getFontMetrics()->getFontHeight($font, $size);
106 function get_padding_box()
108 $pb = $this->_frame
->get_padding_box();
109 $pb[3] = $pb["h"] = $this->_frame
->get_style()->height
;
113 //........................................................................
116 function set_text_spacing($spacing)
118 $style = $this->_frame
->get_style();
120 $this->_text_spacing
= $spacing;
121 $char_spacing = $style->length_in_pt($style->letter_spacing
);
123 // Re-adjust our width to account for the change in spacing
124 $style->width
= $this->_dompdf
->getFontMetrics()->getTextWidth($this->get_text(), $style->font_family
, $style->font_size
, $spacing, $char_spacing);
127 //........................................................................
129 // Recalculate the text width
130 function recalculate_width()
132 $style = $this->get_style();
133 $text = $this->get_text();
134 $size = $style->font_size
;
135 $font = $style->font_family
;
136 $word_spacing = $style->length_in_pt($style->word_spacing
);
137 $char_spacing = $style->length_in_pt($style->letter_spacing
);
139 return $style->width
= $this->_dompdf
->getFontMetrics()->getTextWidth($text, $font, $size, $word_spacing, $char_spacing);
142 //........................................................................
144 // Text manipulation methods
146 // split the text in this frame at the offset specified. The remaining
147 // text is added a sibling frame following this one and is returned.
148 function split_text($offset)
154 $split = $this->_frame
->get_node()->splitText($offset);
156 $deco = $this->copy($split);
158 $p = $this->get_parent();
159 $p->insert_child_after($deco, $this, false);
161 if ($p instanceof Inline
) {
168 //........................................................................
170 function delete_text($offset, $count)
172 $this->_frame
->get_node()->deleteData($offset, $count);
175 //........................................................................
177 function set_text($text)
179 $this->_frame
->get_node()->data
= $text;