1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: text_renderer.cls.php,v $ |
---|
6 | * Created on: 2004-06-01 |
---|
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 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
37 | * @package dompdf |
---|
38 | * @version 0.5.1 |
---|
39 | * |
---|
40 | * Changes |
---|
41 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
42 | * @version dompdf_trunk_with_helmut_mods.20090528 |
---|
43 | * - fix text decoration positions according to font metrics |
---|
44 | * @version 20090610 |
---|
45 | * - better accuracy on using different renderer as cpdf, added comments |
---|
46 | */ |
---|
47 | |
---|
48 | /* $Id: text_renderer.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
49 | /** |
---|
50 | * Renders text frames |
---|
51 | * |
---|
52 | * @access private |
---|
53 | * @package dompdf |
---|
54 | */ |
---|
55 | class Text_Renderer extends Abstract_Renderer { |
---|
56 | |
---|
57 | const DECO_THICKNESS = 0.04; // Thickness of underline. Screen: 0.08, print: better less, e.g. 0.04 |
---|
58 | |
---|
59 | //Tweaking if $base and $descent are not accurate. |
---|
60 | //Check method_exists( $this->_canvas, "get_cpdf" ) |
---|
61 | //- For cpdf these can and must stay 0, because font metrics are used directly. |
---|
62 | //- For other renderers, if different values are wanted, separate the parameter sets. |
---|
63 | // But $size and $size-$height seem to be accurate enough |
---|
64 | const UNDERLINE_OFFSET = 0.0; // Relative to bottom of text, as fraction of height. |
---|
65 | const OVERLINE_OFFSET = 0.0; // Relative to top of text |
---|
66 | const LINETHROUGH_OFFSET = 0.0; // Relative to centre of text. |
---|
67 | const DECO_EXTENSION = 0.0; // How far to extend lines past either end, in pt |
---|
68 | |
---|
69 | //........................................................................ |
---|
70 | |
---|
71 | function render(Frame $frame) { |
---|
72 | |
---|
73 | $style = $frame->get_style(); |
---|
74 | list($x, $y) = $frame->get_position(); |
---|
75 | $cb = $frame->get_containing_block(); |
---|
76 | |
---|
77 | if ( ($ml = $style->margin_left) == "auto" || $ml == "none" ) |
---|
78 | $ml = 0; |
---|
79 | |
---|
80 | if ( ($pl = $style->padding_left) == "auto" || $pl == "none" ) |
---|
81 | $pl = 0; |
---|
82 | |
---|
83 | if ( ($bl = $style->border_left_width) == "auto" || $bl == "none" ) |
---|
84 | $bl = 0; |
---|
85 | |
---|
86 | $x += $style->length_in_pt( array($ml, $pl, $bl), $cb["w"] ); |
---|
87 | |
---|
88 | $text = $frame->get_text(); |
---|
89 | |
---|
90 | $font = $style->font_family; |
---|
91 | $size = $style->font_size; |
---|
92 | $height = $style->height; |
---|
93 | $spacing = $frame->get_text_spacing() + $style->word_spacing; |
---|
94 | |
---|
95 | // if ( preg_replace("/[\s]+/", "", $text) == "" ) |
---|
96 | // return; |
---|
97 | |
---|
98 | $this->_canvas->text($x, $y, $text, |
---|
99 | $font, $size, |
---|
100 | $style->color, $spacing); |
---|
101 | |
---|
102 | if ( method_exists( $this->_canvas, "get_cpdf" ) ) { |
---|
103 | $base = ($this->_canvas->get_cpdf()->fonts[$this->_canvas->get_cpdf()->currentFont]['FontBBox'][3]*$size)/1000; |
---|
104 | $descent = ($this->_canvas->get_cpdf()->fonts[$this->_canvas->get_cpdf()->currentFont]['FontBBox'][1]*$size)/1000; |
---|
105 | //print '<pre>Text_Renderer cpdf:'.$base.' '.$descent.' '.$size.'</pre>'; |
---|
106 | } else { |
---|
107 | //Descent is font part below baseline, typically negative. $height is about full height of font box. |
---|
108 | //$descent = -$size/6; is less accurate, depends on font family. |
---|
109 | $base = $size; |
---|
110 | $descent = $size-$height; |
---|
111 | //print '<pre>Text_Renderer other than cpdf:'.$base.' '.$descent.' '.$size.'</pre>'; |
---|
112 | } |
---|
113 | |
---|
114 | // Handle text decoration: |
---|
115 | // http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration |
---|
116 | |
---|
117 | // Draw all applicable text-decorations. Start with the root and work |
---|
118 | // our way down. |
---|
119 | $p = $frame; |
---|
120 | $stack = array(); |
---|
121 | while ( $p = $p->get_parent() ) |
---|
122 | $stack[] = $p; |
---|
123 | |
---|
124 | while ( count($stack) > 0 ) { |
---|
125 | $f = array_pop($stack); |
---|
126 | |
---|
127 | $deco_y = $y; |
---|
128 | if ( ($text_deco = $f->get_style()->text_decoration) === "none" ) |
---|
129 | continue; |
---|
130 | |
---|
131 | $color = $f->get_style()->color; |
---|
132 | |
---|
133 | switch ($text_deco) { |
---|
134 | |
---|
135 | default: |
---|
136 | continue; |
---|
137 | |
---|
138 | case "underline": |
---|
139 | $deco_y += $base - $descent+ $size * (self::UNDERLINE_OFFSET - self::DECO_THICKNESS/2); |
---|
140 | break; |
---|
141 | |
---|
142 | case "overline": |
---|
143 | $deco_y += $size * (self::OVERLINE_OFFSET + self::DECO_THICKNESS/2); |
---|
144 | break; |
---|
145 | |
---|
146 | case "line-through": |
---|
147 | $deco_y += $base * 0.7 + $size * self::LINETHROUGH_OFFSET; |
---|
148 | break; |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | $dx = 0; |
---|
153 | $x1 = $x - self::DECO_EXTENSION; |
---|
154 | $x2 = $x + $style->width + $dx + self::DECO_EXTENSION; |
---|
155 | $this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $size * self::DECO_THICKNESS); |
---|
156 | |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | } |
---|
161 | |
---|
162 | ?> |
---|