1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: generated_frame_reflower.cls.php,v $ |
---|
6 | * Created on: 2004-06-23 |
---|
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: generated_frame_reflower.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
41 | |
---|
42 | /** |
---|
43 | * Reflows generated content frames (decorates reflower) |
---|
44 | * |
---|
45 | * @access private |
---|
46 | * @package dompdf |
---|
47 | */ |
---|
48 | class Generated_Frame_Reflower extends Frame_Reflower { |
---|
49 | |
---|
50 | protected $_reflower; // Decoration target |
---|
51 | |
---|
52 | function __construct(Frame $frame) { |
---|
53 | parent::__construct($frame); |
---|
54 | } |
---|
55 | |
---|
56 | function set_reflower(Frame_Reflower $reflow) { |
---|
57 | $this->_reflower = $reflow; |
---|
58 | } |
---|
59 | |
---|
60 | //........................................................................ |
---|
61 | |
---|
62 | protected function _parse_string($string) { |
---|
63 | $string = trim($string, "'\""); |
---|
64 | $string = str_replace(array("\\\n",'\\"',"\\'"), |
---|
65 | array("",'"',"'"), $string); |
---|
66 | |
---|
67 | // Convert escaped hex characters into ascii characters (e.g. \A => newline) |
---|
68 | $string = preg_replace_callback("/\\\\([0-9a-fA-F]{0,6})(\s)?(?(2)|(?=[^0-9a-fA-F]))/", |
---|
69 | create_function('$matches', |
---|
70 | 'return chr(hexdec($matches[1]));'), |
---|
71 | $string); |
---|
72 | return $string; |
---|
73 | } |
---|
74 | |
---|
75 | protected function _parse_content() { |
---|
76 | $style = $this->_frame->get_style(); |
---|
77 | |
---|
78 | // Matches generated content |
---|
79 | $re = "/\n". |
---|
80 | "\s(counters?\\([^)]*\\))|\n". |
---|
81 | "\A(counters?\\([^)]*\\))|\n". |
---|
82 | "\s([\"']) ( (?:[^\"']|\\\\[\"'])+ )(?<!\\\\)\\3|\n". |
---|
83 | "\A([\"']) ( (?:[^\"']|\\\\[\"'])+ )(?<!\\\\)\\5|\n" . |
---|
84 | "\s([^\s\"']+)|\n" . |
---|
85 | "\A([^\s\"']+)\n". |
---|
86 | "/xi"; |
---|
87 | |
---|
88 | $content = $style->content; |
---|
89 | |
---|
90 | // split on spaces, except within quotes |
---|
91 | if (!preg_match_all($re, $content, $matches, PREG_SET_ORDER)) |
---|
92 | return; |
---|
93 | |
---|
94 | $text = ""; |
---|
95 | |
---|
96 | foreach ($matches as $match) { |
---|
97 | if ( isset($match[2]) && $match[2] !== "" ) |
---|
98 | $match[1] = $match[1]; |
---|
99 | |
---|
100 | if ( isset($match[6]) && $match[6] !== "" ) |
---|
101 | $match[4] = $match[6]; |
---|
102 | |
---|
103 | if ( isset($match[8]) && $match[8] !== "" ) |
---|
104 | $match[7] = $match[8]; |
---|
105 | |
---|
106 | if ( isset($match[1]) && $match[1] !== "" ) { |
---|
107 | // counters?(...) |
---|
108 | $match[1] = mb_strtolower(trim($match[1])); |
---|
109 | |
---|
110 | // Handle counter() references: |
---|
111 | // http://www.w3.org/TR/CSS21/generate.html#content |
---|
112 | |
---|
113 | $i = mb_strpos($match[1], ")"); |
---|
114 | if ( $i === false ) |
---|
115 | continue; |
---|
116 | |
---|
117 | $args = explode(",", mb_substr($match[1], 7, $i - 7)); |
---|
118 | $counter_id = $args[0]; |
---|
119 | |
---|
120 | if ( $match[1]{7} == "(" ) { |
---|
121 | // counter(name [,style]) |
---|
122 | |
---|
123 | if ( isset($args[1]) ) |
---|
124 | $type = $args[1]; |
---|
125 | else |
---|
126 | $type = null; |
---|
127 | |
---|
128 | |
---|
129 | $p = $this->_frame->find_block_parent(); |
---|
130 | |
---|
131 | $text .= $p->counter_value($counter_id, $type); |
---|
132 | |
---|
133 | } else if ( $match[1]{7} == "s" ) { |
---|
134 | // counters(name, string [,style]) |
---|
135 | if ( isset($args[1]) ) |
---|
136 | $string = $this->_parse_string(trim($args[1])); |
---|
137 | else |
---|
138 | $string = ""; |
---|
139 | |
---|
140 | if ( isset($args[2]) ) |
---|
141 | $type = $args[2]; |
---|
142 | else |
---|
143 | $type = null; |
---|
144 | |
---|
145 | $p = $this->_frame->find_block_parent(); |
---|
146 | $tmp = ""; |
---|
147 | while ($p) { |
---|
148 | $tmp = $p->counter_value($counter_id, $type) . $string . $tmp; |
---|
149 | $p = $p->find_block_parent(); |
---|
150 | } |
---|
151 | $text .= $tmp; |
---|
152 | |
---|
153 | } else |
---|
154 | // countertops? |
---|
155 | continue; |
---|
156 | |
---|
157 | } else if ( isset($match[4]) && $match[4] !== "" ) { |
---|
158 | // String match |
---|
159 | $text .= $this->_parse_string($match[4]); |
---|
160 | |
---|
161 | } else if ( isset($match[7]) && $match[7] !== "" ) { |
---|
162 | // Directive match |
---|
163 | |
---|
164 | if ( $match[7] === "open-quote" ) { |
---|
165 | // FIXME: do something here |
---|
166 | } else if ( $match[7] === "close-quote" ) { |
---|
167 | // FIXME: do something else here |
---|
168 | } else if ( $match[7] === "no-open-quote" ) { |
---|
169 | // FIXME: |
---|
170 | } else if ( $match[7] === "no-close-quote" ) { |
---|
171 | // FIXME: |
---|
172 | } else if ( mb_strpos($match[7],"attr(") === 0 ) { |
---|
173 | |
---|
174 | $i = mb_strpos($match[7],")"); |
---|
175 | if ( $i === false ) |
---|
176 | continue; |
---|
177 | |
---|
178 | $attr = mb_substr($match[7], 6, $i - 6); |
---|
179 | if ( $attr == "" ) |
---|
180 | continue; |
---|
181 | |
---|
182 | $text .= $this->_frame->get_node()->getAttribute($attr); |
---|
183 | } else |
---|
184 | continue; |
---|
185 | |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | return $text; |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | function reflow() { |
---|
194 | $style = $this->_frame->get_style(); |
---|
195 | |
---|
196 | $text = $this->_parse_content(); |
---|
197 | $t_node = $this->_frame->get_node()->ownerDocument->createTextNode($text); |
---|
198 | $t_frame = new Frame($t_node); |
---|
199 | $t_style = $style->get_stylesheet()->create_style(); |
---|
200 | $t_style->inherit($style); |
---|
201 | $t_frame->set_style($t_style); |
---|
202 | |
---|
203 | $this->_frame->prepend_child(Frame_Factory::decorate_frame($t_frame)); |
---|
204 | $this->_reflower->reflow(); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | ?> |
---|