1<?php
2
3require_once(HTML2PS_DIR.'value.generic.php');
4
5class Value extends CSSValue {
6  /**
7   * Type of unit this value is measured with
8   */
9  var $_unit;
10  var $_number;
11  var $_points;
12
13  function scale($scale) {
14    $this->_number *= $scale;
15    $this->_points *= $scale;
16  }
17
18  function &copy() {
19    $value =& new Value;
20    $value->_unit   = $this->_unit;
21    $value->_number = $this->_number;
22    $value->_points = $this->_points;
23    return $value;
24  }
25
26  function getPoints() {
27    return $this->_points;
28  }
29
30  function Value() {
31    $this->_unit   = UNIT_PT;
32    $this->_number = 0;
33    $this->_points = 0;
34  }
35
36  function &fromData($number, $unit) {
37    $value =& new Value;
38    $value->_unit   = $unit;
39    $value->_number = $number;
40    $value->_points = 0;
41    return $value;
42  }
43
44  /**
45   * Create  new  object using  data  contained  in  string CSS  value
46   * representation
47   */
48  function &fromString($string_value) {
49    $value =& new Value;
50    $value->_unit   = $value->unit_from_string($string_value);
51    $value->_number = (double)$string_value;
52    $value->_points = 0;
53    return $value;
54  }
55
56  /**
57   * @static
58   */
59  function unit_from_string($value) {
60    $unit = substr($value, strlen($value)-2, 2);
61    switch ($unit) {
62    case 'pt':
63      return UNIT_PT;
64    case 'px':
65      return UNIT_PX;
66    case 'mm':
67      return UNIT_MM;
68    case 'cm':
69      return UNIT_CM;
70    case 'ex':
71      return UNIT_EX;
72    case 'em':
73      return UNIT_EM;
74    case 'in':
75      return UNIT_IN;
76    case 'pc':
77      return UNIT_PC;
78    default:
79      return UNIT_NONE;
80    }
81  }
82
83  function units2pt($font_size) {
84    $this->_points = $this->toPt($font_size);
85  }
86
87  function toPt($font_size) {
88    switch ($this->_unit) {
89    case UNIT_PT:
90      return pt2pt($this->_number);
91    case UNIT_PX:
92      return px2pt($this->_number);
93    case UNIT_MM:
94      return pt2pt(mm2pt($this->_number));
95    case UNIT_CM:
96      return pt2pt(mm2pt($this->_number*10));
97    case UNIT_EM:
98      return em2pt($this->_number, $font_size);
99    case UNIT_EX:
100      return ex2pt($this->_number, $font_size);
101    case UNIT_IN:
102      return pt2pt($this->_number * 72); // points used by CSS 2.1 are equal to 1/72nd of an inch.
103    case UNIT_PC:
104      return pt2pt($this->_number * 12); // 1 pica equals to 12 points.
105    default:
106      global $g_config;
107
108      if ($g_config['mode'] === 'quirks') {
109        return px2pt($this->_number);
110      } else {
111        return 0;
112      };
113    };
114  }
115}
116
117?>