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.
41 protected $outputPrecision;
44 * Bcmath precision for internal calculations.
46 protected $internalPrecision;
49 * Whether or not BCMath is available
53 public function __construct($output_precision = 4, $internal_precision = 10, $force_no_bcmath = false) {
54 $this->outputPrecision
= $output_precision;
55 $this->internalPrecision
= $internal_precision;
56 $this->bcmath
= !$force_no_bcmath && function_exists('bcmul');
60 * Converts a length object of one unit into another unit.
61 * @param HTMLPurifier_Length $length
62 * Instance of HTMLPurifier_Length to convert. You must validate()
63 * it before passing it here!
64 * @param string $to_unit
67 * About precision: This conversion function pays very special
68 * attention to the incoming precision of values and attempts
69 * to maintain a number of significant figure. Results are
70 * fairly accurate up to nine digits. Some caveats:
71 * - If a number is zero-padded as a result of this significant
72 * figure tracking, the zeroes will be eliminated.
73 * - If a number contains less than four sigfigs ($outputPrecision)
74 * and this causes some decimals to be excluded, those
75 * decimals will be added on.
77 public function convert($length, $to_unit) {
79 if (!$length->isValid()) return false;
82 $unit = $length->getUnit();
84 if ($n === '0' ||
$unit === false) {
85 return new HTMLPurifier_Length('0', false);
88 $state = $dest_state = false;
89 foreach (self
::$units as $k => $x) {
90 if (isset($x[$unit])) $state = $k;
91 if (isset($x[$to_unit])) $dest_state = $k;
93 if (!$state ||
!$dest_state) return false;
95 // Some calculations about the initial precision of the number;
96 // this will be useful when we need to do final rounding.
97 $sigfigs = $this->getSigFigs($n);
98 if ($sigfigs < $this->outputPrecision
) $sigfigs = $this->outputPrecision
;
100 // BCMath's internal precision deals only with decimals. Use
101 // our default if the initial number has no decimals, or increase
102 // it by how ever many decimals, thus, the number of guard digits
103 // will always be greater than or equal to internalPrecision.
104 $log = (int) floor(log(abs($n), 10));
105 $cp = ($log < 0) ?
$this->internalPrecision
- $log : $this->internalPrecision
; // internal precision
107 for ($i = 0; $i < 2; $i++
) {
109 // Determine what unit IN THIS SYSTEM we need to convert to
110 if ($dest_state === $state) {
112 $dest_unit = $to_unit;
114 // Convert to the smallest unit, pending a system shift
115 $dest_unit = self
::$units[$state][$dest_state][0];
118 // Do the conversion if necessary
119 if ($dest_unit !== $unit) {
120 $factor = $this->div(self
::$units[$state][$unit], self
::$units[$state][$dest_unit], $cp);
121 $n = $this->mul($n, $factor, $cp);
125 // Output was zero, so bail out early. Shouldn't ever happen.
132 // It was a simple conversion, so bail out
133 if ($dest_state === $state) {
138 // Conversion failed! Apparently, the system we forwarded
139 // to didn't have this unit. This should never happen!
143 // Pre-condition: $i == 0
145 // Perform conversion to next system of units
146 $n = $this->mul($n, self
::$units[$state][$dest_state][1], $cp);
147 $unit = self
::$units[$state][$dest_state][2];
148 $state = $dest_state;
150 // One more loop around to convert the unit in the new system.
154 // Post-condition: $unit == $to_unit
155 if ($unit !== $to_unit) return false;
157 // Useful for debugging:
159 //echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
161 $n = $this->round($n, $sigfigs);
162 if (strpos($n, '.') !== false) $n = rtrim($n, '0');
165 return new HTMLPurifier_Length($n, $unit);
169 * Returns the number of significant figures in a string number.
170 * @param string $n Decimal number
171 * @return int number of sigfigs
173 public function getSigFigs($n) {
174 $n = ltrim($n, '0+-');
175 $dp = strpos($n, '.'); // decimal position
177 $sigfigs = strlen(rtrim($n, '0'));
179 $sigfigs = strlen(ltrim($n, '0.')); // eliminate extra decimal character
180 if ($dp !== 0) $sigfigs--;
186 * Adds two numbers, using arbitrary precision when available.
188 private function add($s1, $s2, $scale) {
189 if ($this->bcmath
) return bcadd($s1, $s2, $scale);
190 else return $this->scale($s1 +
$s2, $scale);
194 * Multiples two numbers, using arbitrary precision when available.
196 private function mul($s1, $s2, $scale) {
197 if ($this->bcmath
) return bcmul($s1, $s2, $scale);
198 else return $this->scale($s1 * $s2, $scale);
202 * Divides two numbers, using arbitrary precision when available.
204 private function div($s1, $s2, $scale) {
205 if ($this->bcmath
) return bcdiv($s1, $s2, $scale);
206 else return $this->scale($s1 / $s2, $scale);
210 * Rounds a number according to the number of sigfigs it should have,
211 * using arbitrary precision when available.
213 private function round($n, $sigfigs) {
214 $new_log = (int) floor(log(abs($n), 10)); // Number of digits left of decimal - 1
215 $rp = $sigfigs - $new_log - 1; // Number of decimal places needed
216 $neg = $n < 0 ?
'-' : ''; // Negative sign
219 $n = bcadd($n, $neg . '0.' . str_repeat('0', $rp) . '5', $rp +
1);
220 $n = bcdiv($n, '1', $rp);
222 // This algorithm partially depends on the standardized
223 // form of numbers that comes out of bcmath.
224 $n = bcadd($n, $neg . '5' . str_repeat('0', $new_log - $sigfigs), 0);
225 $n = substr($n, 0, $sigfigs +
strlen($neg)) . str_repeat('0', $new_log - $sigfigs +
1);
229 return $this->scale(round($n, $sigfigs - $new_log - 1), $rp +
1);
234 * Scales a float to $scale digits right of decimal point, like BCMath.
236 private function scale($r, $scale) {
238 // The f sprintf type doesn't support negative numbers, so we
239 // need to cludge things manually. First get the string.
240 $r = sprintf('%.0f', (float) $r);
241 // Due to floating point precision loss, $r will more than likely
242 // look something like 4652999999999.9234. We grab one more digit
243 // than we need to precise from $r and then use that to round
245 $precise = (string) round(substr($r, 0, strlen($r) +
$scale), -1);
246 // Now we return it, truncating the zero that was rounded off.
247 return substr($precise, 0, -1) . str_repeat('0', -$scale +
1);
249 return sprintf('%.' . $scale . 'f', (float) $r);
254 // vim: et sw=4 sts=4