source: branches/2.2/filemanager/tp/dompdf/include/attribute_translator.cls.php @ 3019

Revision 3019, 15.4 KB checked in by amuller, 14 years ago (diff)

Ticket #1135 - Corrigindo CSS e adicionando filemanager

Line 
1<?php
2/**
3 * DOMPDF - PHP5 HTML to PDF renderer
4 *
5 * File: $RCSfile: attribute_translator.cls.php,v $
6 * Created on: 2004-09-13
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: attribute_translator.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */
41
42/**
43 * Translates HTML 4.0 attributes into CSS rules
44 *
45 * @access private
46 * @package dompdf
47 */
48class Attribute_Translator {
49 
50  // Munged data originally from
51  // http://www.w3.org/TR/REC-html40/index/attributes.html
52  //
53  // thank you var_export() :D
54  static private $__ATTRIBUTE_LOOKUP =
55    array (//'caption' => array ( 'align' => '', ),
56           'img_inner' =>  // img tags actually end up wrapping img_inner elements
57           array ('align' => array('bottom' => 'vertical-align: baseline;',
58                                   'middle' => 'vertical-align: middle;',
59                                   'top' => 'vertical-align: top;',
60                                   'left' => 'float: left;',
61                                   'right' => 'float: right;'),
62                  'border' => 'border-width: %0.2f px;',
63                  'height' => 'height: %s;',
64                  'hspace' => 'padding-left: %1$0.2f px; padding-right: %1$0.2f px;',
65                  'vspace' => 'padding-top: %1$0.2f px; padding-bottom: %1$0.2f px',
66                  'width' => 'width: %s;',
67                  ),
68           'table' =>
69           array ("align" => array(//'left' => '',
70                        'center' => 'margin-left: auto; margin-right: auto;',
71                        //'right' => ''
72                        ),
73                  'bgcolor' => 'background-color: %s;',
74                  'border' => '!set_table_border',
75                  'cellpadding' => '!set_table_cellpadding',
76                  'cellspacing' => 'border-spacing: %0.2f; border-collapse: separate;',
77                  'frame' => array('void' => 'border-style: none;',
78                                   'above' => 'border-top-style: solid;',
79                                   'below' => 'border-bottom-style: solid;',
80                                   'hsides' => 'border-left-style: solid; border-right-style: solid;',
81                                   'vsides' => 'border-top-style: solid; border-bottom-style: solid;',
82                                   'lhs' => 'border-left-style: solid;',
83                                   'rhs' => 'border-right-style: solid;',
84                                   'box' => 'border-style: solid;',
85                                   'border' => 'border-style: solid;'),
86                  'rules' => '!set_table_rules',
87                  'width' => 'width: %s;',
88                  ),
89           'hr' =>
90           array (
91                  'align' => '!set_hr_align', // Need to grab width to set 'left' & 'right' correctly
92                  'noshade' => 'border-style: solid;',
93                  'size' => 'border-width: %0.2f px;',
94                  'width' => 'width: %s;',
95                  ),
96           'div' =>
97           array (
98                  'align' => 'text-align: %s;',
99                  ),
100           'h1' =>
101           array (
102                  'align' => 'text-align: %s;',
103                  ),
104           'h2' =>
105           array (
106                  'align' => 'text-align: %s;',
107                  ),
108           'h3' =>
109           array (
110                  'align' => 'text-align: %s;',
111                  ),
112           'h4' =>
113           array (
114                  'align' => 'text-align: %s;',
115                  ),
116           'h5' =>
117           array (
118                  'align' => 'text-align: %s;',
119                  ),
120           'h6' =>
121           array (
122                  'align' => 'text-align: %s;',
123                  ),
124           'p' =>
125           array (
126                  'align' => 'text-align: %s;',
127                  ),
128//            'col' =>
129//            array (
130//                   'align' => '',
131//                   'valign' => '',
132//                   ),
133//            'colgroup' =>
134//            array (
135//                   'align' => '',
136//                   'valign' => '',
137//                   ),
138           'tbody' =>
139           array (
140                  'align' => '!set_table_row_align',
141                  'valign' => '!set_table_row_valign',
142                  ),
143           'td' =>
144           array (
145                  'align' => 'text-align: %s;',
146                  'bgcolor' => 'background-color: %s;',
147                  'height' => 'height: %s;',
148                  'nowrap' => 'white-space: nowrap;',
149                  'valign' => 'vertical-align: %s;',
150                  'width' => 'width: %s;',
151                  ),
152           'tfoot' =>
153           array (
154                  'align' => '!set_table_row_align',
155                  'valign' => '!set_table_row_valign',
156                  ),
157           'th' =>
158           array (
159                  'align' => 'text-align: %s;',
160                  'bgcolor' => 'background-color: %s;',
161                  'height' => 'height: %s;',
162                  'nowrap' => 'white-space: nowrap;',
163                  'valign' => 'vertical-align: %s;',
164                  'width' => 'width: %s;',
165                  ),
166           'thead' =>
167           array (
168                  'align' => '!set_table_row_align',
169                  'valign' => '!set_table_row_valign',
170                  ),
171           'tr' =>
172           array (
173                  'align' => '!set_table_row_align',
174                  'bgcolor' => '!set_table_row_bgcolor',
175                  'valign' => '!set_table_row_valign',
176                  ),
177           'body' =>
178           array (
179                  'background' => 'background-image: url(%s);',
180                  'bgcolor' => 'background-color: %s;',
181                  'link' => '!set_body_link',
182                  'text' => 'color: %s;',
183                  ),
184           'br' =>
185           array (
186                  'clear' => 'clear: %s;',
187                  ),
188           'basefont' =>
189           array (
190                  'color' => 'color: %s;',
191                  'face' => 'font-family: %s;',
192                  'size' => '!set_basefont_size',
193                  ),
194           'font' =>
195           array (
196                  'color' => 'color: %s;',
197                  'face' => 'font-family: %s;',
198                  'size' => '!set_font_size',
199                  ),
200           'dir' =>
201           array (
202                  'compact' => 'margin: 0.5em 0;',
203                  ),
204           'dl' =>
205           array (
206                  'compact' => 'margin: 0.5em 0;',
207                  ),
208           'menu' =>
209           array (
210                  'compact' => 'margin: 0.5em 0;',
211                  ),
212           'ol' =>
213           array (
214                  'compact' => 'margin: 0.5em 0;',
215                  'start' => 'counter-reset: -dompdf-default-counter %d;',
216                  'type' => 'list-style-type: %s;',
217                  ),
218           'ul' =>
219           array (
220                  'compact' => 'margin: 0.5em 0;',
221                  'type' => 'list-style-type: %s;',
222                  ),
223           'li' =>
224           array (
225                  'type' => 'list-style-type: %s;',
226                  'value' => 'counter-reset: -dompdf-default-counter %d;',
227                  ),
228           'pre' =>
229           array (
230                  'width' => 'width: %s;',
231                  ),
232           );
233
234 
235  static protected $_last_basefont_size = 3;
236  static protected $_font_size_lookup = array(1=>"xx-small",
237                                              2=>"x-small",
238                                              3=>"medium",
239                                              4=>"large",
240                                              5=>"x-large",
241                                              6=>"xx-large",
242                                              7=>"300%");
243 
244 
245  static function translate_attributes($frame) {
246    $node = $frame->get_node();
247    $tag = $node->tagName;
248
249    if ( !isset(self::$__ATTRIBUTE_LOOKUP[$tag]) )
250      return;
251
252    $valid_attrs = self::$__ATTRIBUTE_LOOKUP[$tag];
253    $attrs = $node->attributes;
254    $style = rtrim($node->getAttribute("style"), "; ");
255    if ( $style != "" )
256      $style .= ";";
257
258    foreach ($attrs as $attr => $attr_node ) {
259      if ( !isset($valid_attrs[$attr]) )
260        continue;
261
262      $value = $attr_node->value;
263
264      $target = $valid_attrs[$attr];
265     
266      // Look up $value in $target, if $target is an array:
267      if ( is_array($target) ) {
268
269        if ( isset($target[$value]) )
270          $style .= " " . self::_resolve_target($node, $target[$value], $value);
271
272      } else {
273        // otherwise use target directly
274        $style .= " " . self::_resolve_target($node, $target, $value);
275      }
276    }
277    if ( !is_null($style) ) {
278      $style = ltrim($style);
279      $node->setAttribute("style", $style);
280    }
281   
282  }
283
284  static protected function _resolve_target($node, $target, $value) {
285    if ( $target{0} == "!" ) {
286      // Function call
287      $func = "_" . mb_substr($target, 1);
288      return self::$func($node, $value);
289    }
290   
291    return $value ? sprintf($target, $value) : "";
292  }
293
294  //.....................................................................
295
296  static protected function _set_table_cellpadding($node, $value) {
297
298    $td_list = $node->getElementsByTagName("td");
299    foreach ($td_list as $td) {
300      $style = rtrim($td->getAttribute("style"), ";");
301      $style .= "; padding: $value" . "px;";
302      $style = ltrim($style, ";");
303      $td->setAttribute("style", $style);
304    }
305    return null;
306  }
307
308  static protected function _set_table_border($node, $value) {
309    $td_list = $node->getElementsByTagName("td");
310    foreach ($td_list as $td) {
311      $style = $td->getAttribute("style");
312      if ( strpos($style, "border") !== false )
313        continue;
314      $style = rtrim($style, ";");
315      $style .= "; border-width: $value" . "px; border-style: ridge;";
316      $style = ltrim($style, ";");
317      $td->setAttribute("style", $style);
318    }
319    $th_list = $node->getElementsByTagName("th");
320    foreach ($th_list as $th) {
321      $style = $th->getAttribute("style");
322      if ( strpos($style, "border") !== false )
323        continue;
324      $style = rtrim($style, ";");
325      $style .= "; border-width: $value" . "px; border-style: ridge;";
326      $style = ltrim($style, ";");
327      $th->setAttribute("style", $style);
328    }
329   
330    return null;
331  }
332
333  static protected function _set_table_cellspacing($node, $value) {
334    $style = rtrim($td->getAttribute($style), ";");
335
336    if ( $value == 0 )
337      $style .= "; border-collapse: collapse;";
338     
339    else
340      $style = "; border-collapse: separate;";
341     
342    return ltrim($style, ";");
343  }
344 
345  static protected function _set_table_rules($node, $value) {
346    $new_style = "; border-collapse: collapse;";
347    switch ($value) {
348    case "none":
349      $new_style .= "border-style: none;";
350      break;
351
352    case "groups":
353      // FIXME: unsupported
354      return;
355
356    case "rows":
357      $new_style .= "border-style: solid none solid none; border-width: 1px; ";
358      break;
359
360    case "cols":
361      $new_style .= "border-style: none solid none solid; border-width: 1px; ";
362      break;
363
364    case "all":
365      $new_style .= "border-style: solid; border-width: 1px; ";
366      break;
367     
368    default:
369      // Invalid value
370      return null;
371    }
372
373    $td_list = $node->getElementsByTagName("td");
374   
375    foreach ($td_list as $td) {
376      $style = $td->getAttribute("style");
377      $style .= $new_style;
378      $td->setAttribute("style", $style);
379    }
380    return null;
381  }
382
383  static protected function _set_hr_align($node, $value) {
384
385    $style = rtrim($node->getAttribute("style"),";");
386    $width = $node->getAttribute("width");
387    if ( $width == "" )
388      $width = "100%";
389
390    $remainder = 100 - (double)rtrim($width, "% ");
391   
392    switch ($value) {
393    case "left":
394      $style .= "; margin-right: $remainder %;";
395      break;
396
397    case "right":
398      $style .= "; margin-left: $remainder %;";
399      break;
400
401    case "center":
402      $style .= "; margin-left: auto; margin-right: auto;";
403      break;
404
405    default:
406      return null;
407    }
408    return ltrim($style, "; ");
409   
410  }
411
412  static protected function _set_table_row_align($node, $value) {
413
414    $td_list = $node->getElementsByTagName("td");
415
416    foreach ($td_list as $td) {
417      $style = rtrim($td->getAttribute("style"), ";");
418      $style .= "; text-align: $value;";
419      $style = ltrim($style, "; ");
420      $td->setAttribute("style", $style);
421    }
422
423    return null;
424  }
425
426  static protected function _set_table_row_valign($node, $value) {
427
428    $td_list = $node->getElementsByTagName("td");
429
430    foreach ($td_list as $td) {
431      $style = rtrim($td->getAttribute("style"), ";");
432      $style .= "; vertical-align: $value;";
433      $style = ltrim($style, "; ");
434      $td->setAttribute("style", $style);
435    }
436
437    return null;
438  }
439
440  static protected function _set_table_row_bgcolor($node, $value) {
441
442    $td_list = $node->getElementsByTagName("td");
443
444    foreach ($td_list as $td) {
445      $style = rtrim($td->getAttribute("style"), ";");
446      $style .= "; background-color: $value;";
447      $style = ltrim($style, "; ");
448      $td->setAttribute("style", $style);
449    }
450
451    return null;
452  }
453
454  static protected function _set_body_link($node, $value) {
455
456    $a_list = $node->getElementsByTagName("a");
457
458    foreach ($a_list as $a) {
459      $style = rtrim($a->getAttribute("style"), ";");
460      $style .= "; color: $value;";
461      $style = ltrim($style, "; ");
462      $a->setAttribute("style", $style);
463    }
464
465    return null;
466  }
467
468  static protected function _set_basefont_size($node, $value) {
469    // FIXME: ? we don't actually set the font size of anything here, just
470    // the base size for later modification by <font> tags.
471    self::$_last_basefont_size = $value;
472    return null;
473  }
474 
475  static protected function _set_font_size($node, $value) {
476    $style = $node->getAttribute("style");
477
478    if ( $value{0} == "-" || $value{0} == "+" )
479      $value = self::$_last_basefont_size + (int)$value;
480
481    if ( isset(self::$_font_size_lookup[$value]) )
482      $style .= "; font-size: " . self::$_font_size_lookup[$value] . ";";
483    else
484      $style .= "; font-size: $value;";
485
486    return ltrim($style, "; ");
487   
488  }
489
490}
491
492?>
Note: See TracBrowser for help on using the repository browser.