4 * Class for converting between different unit-lengths as specified by
7 class HTMLPurifier_UnitConverter
15 * Units information array. Units are grouped into measuring systems
16 * (English, Metric), and are assigned an integer representing
17 * the conversion factor between that unit and the smallest unit in
18 * the system. Numeric indexes are actually magical constants that
19 * encode conversion data from one system to the next, with a O(n^2)
20 * constraint on memory (this is generally not a problem, since
21 * the number of measuring systems is small.)
23 protected static $units = array(
24 self
::ENGLISH
=> array(
25 'px' => 3, // This is as per CSS 2.1 and Firefox. Your mileage may vary
29 self
::METRIC
=> array('pt', '0.352777778', 'mm'),
31 self
::METRIC
=> array(
34 self
::ENGLISH
=> array('mm', '2.83464567', 'pt'),
39 * Minimum bcmath precision for output.
42 protected $outputPrecision;
45 * Bcmath precision for internal calculations.
48 protected $internalPrecision;
51 * Whether or not BCMath is available.
56 public function __construct($output_precision = 4, $internal_precision = 10, $force_no_bcmath = false)
58 $this->outputPrecision
= $output_precision;
59 $this->internalPrecision
= $internal_precision;
60 $this->bcmath
= !$force_no_bcmath && function_exists('bcmul');
64 * Converts a length object of one unit into another unit.
65 * @param HTMLPurifier_Length $length
66 * Instance of HTMLPurifier_Length to convert. You must validate()
67 * it before passing it here!
68 * @param string $to_unit
70 * @return HTMLPurifier_Length|bool
72 * About precision: This conversion function pays very special
73 * attention to the incoming precision of values and attempts
74 * to maintain a number of significant figure. Results are
75 * fairly accurate up to nine digits. Some caveats:
76 * - If a number is zero-padded as a result of this significant
77 * figure tracking, the zeroes will be eliminated.
78 * - If a number contains less than four sigfigs ($outputPrecision)
79 * and this causes some decimals to be excluded, those
80 * decimals will be added on.
82 public function convert($length, $to_unit)
84 if (!$length->isValid()) {
89 $unit = $length->getUnit();
91 if ($n === '0' ||
$unit === false) {
92 return new HTMLPurifier_Length('0', false);
95 $state = $dest_state = false;
96 foreach (self
::$units as $k => $x) {
97 if (isset($x[$unit])) {
100 if (isset($x[$to_unit])) {
104 if (!$state ||
!$dest_state) {
108 // Some calculations about the initial precision of the number;
109 // this will be useful when we need to do final rounding.
110 $sigfigs = $this->getSigFigs($n);
111 if ($sigfigs < $this->outputPrecision
) {
112 $sigfigs = $this->outputPrecision
;
115 // BCMath's internal precision deals only with decimals. Use
116 // our default if the initial number has no decimals, or increase
117 // it by how ever many decimals, thus, the number of guard digits
118 // will always be greater than or equal to internalPrecision.
119 $log = (int)floor(log(abs($n), 10));
120 $cp = ($log < 0) ?
$this->internalPrecision
- $log : $this->internalPrecision
; // internal precision
122 for ($i = 0; $i < 2; $i++
) {
124 // Determine what unit IN THIS SYSTEM we need to convert to
125 if ($dest_state === $state) {
127 $dest_unit = $to_unit;
129 // Convert to the smallest unit, pending a system shift
130 $dest_unit = self
::$units[$state][$dest_state][0];
133 // Do the conversion if necessary
134 if ($dest_unit !== $unit) {
135 $factor = $this->div(self
::$units[$state][$unit], self
::$units[$state][$dest_unit], $cp);
136 $n = $this->mul($n, $factor, $cp);
140 // Output was zero, so bail out early. Shouldn't ever happen.
147 // It was a simple conversion, so bail out
148 if ($dest_state === $state) {
153 // Conversion failed! Apparently, the system we forwarded
154 // to didn't have this unit. This should never happen!
158 // Pre-condition: $i == 0
160 // Perform conversion to next system of units
161 $n = $this->mul($n, self
::$units[$state][$dest_state][1], $cp);
162 $unit = self
::$units[$state][$dest_state][2];
163 $state = $dest_state;
165 // One more loop around to convert the unit in the new system.
169 // Post-condition: $unit == $to_unit
170 if ($unit !== $to_unit) {
174 // Useful for debugging:
176 //echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
178 $n = $this->round($n, $sigfigs);
179 if (strpos($n, '.') !== false) {
184 return new HTMLPurifier_Length($n, $unit);
188 * Returns the number of significant figures in a string number.
189 * @param string $n Decimal number
190 * @return int number of sigfigs
192 public function getSigFigs($n)
194 $n = ltrim($n, '0+-');
195 $dp = strpos($n, '.'); // decimal position
197 $sigfigs = strlen(rtrim($n, '0'));
199 $sigfigs = strlen(ltrim($n, '0.')); // eliminate extra decimal character
208 * Adds two numbers, using arbitrary precision when available.
214 private function add($s1, $s2, $scale)
217 return bcadd($s1, $s2, $scale);
219 return $this->scale((float)$s1 +
(float)$s2, $scale);
224 * Multiples two numbers, using arbitrary precision when available.
230 private function mul($s1, $s2, $scale)
233 return bcmul($s1, $s2, $scale);
235 return $this->scale((float)$s1 * (float)$s2, $scale);
240 * Divides two numbers, using arbitrary precision when available.
246 private function div($s1, $s2, $scale)
249 return bcdiv($s1, $s2, $scale);
251 return $this->scale((float)$s1 / (float)$s2, $scale);
256 * Rounds a number according to the number of sigfigs it should have,
257 * using arbitrary precision when available.
259 * @param int $sigfigs
262 private function round($n, $sigfigs)
264 $new_log = (int)floor(log(abs($n), 10)); // Number of digits left of decimal - 1
265 $rp = $sigfigs - $new_log - 1; // Number of decimal places needed
266 $neg = $n < 0 ?
'-' : ''; // Negative sign
269 $n = bcadd($n, $neg . '0.' . str_repeat('0', $rp) . '5', $rp +
1);
270 $n = bcdiv($n, '1', $rp);
272 // This algorithm partially depends on the standardized
273 // form of numbers that comes out of bcmath.
274 $n = bcadd($n, $neg . '5' . str_repeat('0', $new_log - $sigfigs), 0);
275 $n = substr($n, 0, $sigfigs +
strlen($neg)) . str_repeat('0', $new_log - $sigfigs +
1);
279 return $this->scale(round($n, $sigfigs - $new_log - 1), $rp +
1);
284 * Scales a float to $scale digits right of decimal point, like BCMath.
289 private function scale($r, $scale)
292 // The f sprintf type doesn't support negative numbers, so we
293 // need to cludge things manually. First get the string.
294 $r = sprintf('%.0f', (float)$r);
295 // Due to floating point precision loss, $r will more than likely
296 // look something like 4652999999999.9234. We grab one more digit
297 // than we need to precise from $r and then use that to round
299 $precise = (string)round(substr($r, 0, strlen($r) +
$scale), -1);
300 // Now we return it, truncating the zero that was rounded off.
301 return substr($precise, 0, -1) . str_repeat('0', -$scale +
1);
303 return sprintf('%.' . $scale . 'f', (float)$r);
307 // vim: et sw=4 sts=4