HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/UnitConverter.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class HTMLPurifier_UnitConverter
00008 {
00009 
00010     const ENGLISH = 1;
00011     const METRIC = 2;
00012     const DIGITAL = 3;
00013 
00023     protected static $units = array(
00024         self::ENGLISH => array(
00025             'px' => 3, // This is as per CSS 2.1 and Firefox. Your mileage may vary
00026             'pt' => 4,
00027             'pc' => 48,
00028             'in' => 288,
00029             self::METRIC => array('pt', '0.352777778', 'mm'),
00030         ),
00031         self::METRIC => array(
00032             'mm' => 1,
00033             'cm' => 10,
00034             self::ENGLISH => array('mm', '2.83464567', 'pt'),
00035         ),
00036     );
00037 
00041     protected $outputPrecision;
00042 
00046     protected $internalPrecision;
00047 
00051     private $bcmath;
00052 
00053     public function __construct($output_precision = 4, $internal_precision = 10, $force_no_bcmath = false) {
00054         $this->outputPrecision = $output_precision;
00055         $this->internalPrecision = $internal_precision;
00056         $this->bcmath = !$force_no_bcmath && function_exists('bcmul');
00057     }
00058 
00077     public function convert($length, $to_unit) {
00078 
00079         if (!$length->isValid()) return false;
00080 
00081         $n    = $length->getN();
00082         $unit = $length->getUnit();
00083 
00084         if ($n === '0' || $unit === false) {
00085             return new HTMLPurifier_Length('0', false);
00086         }
00087 
00088         $state = $dest_state = false;
00089         foreach (self::$units as $k => $x) {
00090             if (isset($x[$unit])) $state = $k;
00091             if (isset($x[$to_unit])) $dest_state = $k;
00092         }
00093         if (!$state || !$dest_state) return false;
00094 
00095         // Some calculations about the initial precision of the number;
00096         // this will be useful when we need to do final rounding.
00097         $sigfigs = $this->getSigFigs($n);
00098         if ($sigfigs < $this->outputPrecision) $sigfigs = $this->outputPrecision;
00099 
00100         // BCMath's internal precision deals only with decimals. Use
00101         // our default if the initial number has no decimals, or increase
00102         // it by how ever many decimals, thus, the number of guard digits
00103         // will always be greater than or equal to internalPrecision.
00104         $log = (int) floor(log(abs($n), 10));
00105         $cp = ($log < 0) ? $this->internalPrecision - $log : $this->internalPrecision; // internal precision
00106 
00107         for ($i = 0; $i < 2; $i++) {
00108 
00109             // Determine what unit IN THIS SYSTEM we need to convert to
00110             if ($dest_state === $state) {
00111                 // Simple conversion
00112                 $dest_unit = $to_unit;
00113             } else {
00114                 // Convert to the smallest unit, pending a system shift
00115                 $dest_unit = self::$units[$state][$dest_state][0];
00116             }
00117 
00118             // Do the conversion if necessary
00119             if ($dest_unit !== $unit) {
00120                 $factor = $this->div(self::$units[$state][$unit], self::$units[$state][$dest_unit], $cp);
00121                 $n = $this->mul($n, $factor, $cp);
00122                 $unit = $dest_unit;
00123             }
00124 
00125             // Output was zero, so bail out early. Shouldn't ever happen.
00126             if ($n === '') {
00127                 $n = '0';
00128                 $unit = $to_unit;
00129                 break;
00130             }
00131 
00132             // It was a simple conversion, so bail out
00133             if ($dest_state === $state) {
00134                 break;
00135             }
00136 
00137             if ($i !== 0) {
00138                 // Conversion failed! Apparently, the system we forwarded
00139                 // to didn't have this unit. This should never happen!
00140                 return false;
00141             }
00142 
00143             // Pre-condition: $i == 0
00144 
00145             // Perform conversion to next system of units
00146             $n = $this->mul($n, self::$units[$state][$dest_state][1], $cp);
00147             $unit = self::$units[$state][$dest_state][2];
00148             $state = $dest_state;
00149 
00150             // One more loop around to convert the unit in the new system.
00151 
00152         }
00153 
00154         // Post-condition: $unit == $to_unit
00155         if ($unit !== $to_unit) return false;
00156 
00157         // Useful for debugging:
00158         //echo "<pre>n";
00159         //echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
00160 
00161         $n = $this->round($n, $sigfigs);
00162         if (strpos($n, '.') !== false) $n = rtrim($n, '0');
00163         $n = rtrim($n, '.');
00164 
00165         return new HTMLPurifier_Length($n, $unit);
00166     }
00167 
00173     public function getSigFigs($n) {
00174         $n = ltrim($n, '0+-');
00175         $dp = strpos($n, '.'); // decimal position
00176         if ($dp === false) {
00177             $sigfigs = strlen(rtrim($n, '0'));
00178         } else {
00179             $sigfigs = strlen(ltrim($n, '0.')); // eliminate extra decimal character
00180             if ($dp !== 0) $sigfigs--;
00181         }
00182         return $sigfigs;
00183     }
00184 
00188     private function add($s1, $s2, $scale) {
00189         if ($this->bcmath) return bcadd($s1, $s2, $scale);
00190         else return $this->scale($s1 + $s2, $scale);
00191     }
00192 
00196     private function mul($s1, $s2, $scale) {
00197         if ($this->bcmath) return bcmul($s1, $s2, $scale);
00198         else return $this->scale($s1 * $s2, $scale);
00199     }
00200 
00204     private function div($s1, $s2, $scale) {
00205         if ($this->bcmath) return bcdiv($s1, $s2, $scale);
00206         else return $this->scale($s1 / $s2, $scale);
00207     }
00208 
00213     private function round($n, $sigfigs) {
00214         $new_log = (int) floor(log(abs($n), 10)); // Number of digits left of decimal - 1
00215         $rp = $sigfigs - $new_log - 1; // Number of decimal places needed
00216         $neg = $n < 0 ? '-' : ''; // Negative sign
00217         if ($this->bcmath) {
00218             if ($rp >= 0) {
00219                 $n = bcadd($n, $neg . '0.' .  str_repeat('0', $rp) . '5', $rp + 1);
00220                 $n = bcdiv($n, '1', $rp);
00221             } else {
00222                 // This algorithm partially depends on the standardized
00223                 // form of numbers that comes out of bcmath.
00224                 $n = bcadd($n, $neg . '5' . str_repeat('0', $new_log - $sigfigs), 0);
00225                 $n = substr($n, 0, $sigfigs + strlen($neg)) . str_repeat('0', $new_log - $sigfigs + 1);
00226             }
00227             return $n;
00228         } else {
00229             return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1);
00230         }
00231     }
00232 
00236     private function scale($r, $scale) {
00237         if ($scale < 0) {
00238             // The f sprintf type doesn't support negative numbers, so we
00239             // need to cludge things manually. First get the string.
00240             $r = sprintf('%.0f', (float) $r);
00241             // Due to floating point precision loss, $r will more than likely
00242             // look something like 4652999999999.9234. We grab one more digit
00243             // than we need to precise from $r and then use that to round
00244             // appropriately.
00245             $precise = (string) round(substr($r, 0, strlen($r) + $scale), -1);
00246             // Now we return it, truncating the zero that was rounded off.
00247             return substr($precise, 0, -1) . str_repeat('0', -$scale + 1);
00248         }
00249         return sprintf('%.' . $scale . 'f', (float) $r);
00250     }
00251 
00252 }
00253 
00254 // vim: et sw=4 sts=4