ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
UnitConverter.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
10  const ENGLISH = 1;
11  const METRIC = 2;
12  const DIGITAL = 3;
13 
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
26  'pt' => 4,
27  'pc' => 48,
28  'in' => 288,
29  self::METRIC => array('pt', '0.352777778', 'mm'),
30  ),
31  self::METRIC => array(
32  'mm' => 1,
33  'cm' => 10,
34  self::ENGLISH => array('mm', '2.83464567', 'pt'),
35  ),
36  );
37 
41  protected $outputPrecision;
42 
46  protected $internalPrecision;
47 
51  private $bcmath;
52 
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');
57  }
58 
77  public function convert($length, $to_unit) {
78 
79  if (!$length->isValid()) return false;
80 
81  $n = $length->getN();
82  $unit = $length->getUnit();
83 
84  if ($n === '0' || $unit === false) {
85  return new HTMLPurifier_Length('0', false);
86  }
87 
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;
92  }
93  if (!$state || !$dest_state) return false;
94 
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;
99 
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
106 
107  for ($i = 0; $i < 2; $i++) {
108 
109  // Determine what unit IN THIS SYSTEM we need to convert to
110  if ($dest_state === $state) {
111  // Simple conversion
112  $dest_unit = $to_unit;
113  } else {
114  // Convert to the smallest unit, pending a system shift
115  $dest_unit = self::$units[$state][$dest_state][0];
116  }
117 
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);
122  $unit = $dest_unit;
123  }
124 
125  // Output was zero, so bail out early. Shouldn't ever happen.
126  if ($n === '') {
127  $n = '0';
128  $unit = $to_unit;
129  break;
130  }
131 
132  // It was a simple conversion, so bail out
133  if ($dest_state === $state) {
134  break;
135  }
136 
137  if ($i !== 0) {
138  // Conversion failed! Apparently, the system we forwarded
139  // to didn't have this unit. This should never happen!
140  return false;
141  }
142 
143  // Pre-condition: $i == 0
144 
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;
149 
150  // One more loop around to convert the unit in the new system.
151 
152  }
153 
154  // Post-condition: $unit == $to_unit
155  if ($unit !== $to_unit) return false;
156 
157  // Useful for debugging:
158  //echo "<pre>n";
159  //echo "$n\nsigfigs = $sigfigs\nnew_log = $new_log\nlog = $log\nrp = $rp\n</pre>\n";
160 
161  $n = $this->round($n, $sigfigs);
162  if (strpos($n, '.') !== false) $n = rtrim($n, '0');
163  $n = rtrim($n, '.');
164 
165  return new HTMLPurifier_Length($n, $unit);
166  }
167 
173  public function getSigFigs($n) {
174  $n = ltrim($n, '0+-');
175  $dp = strpos($n, '.'); // decimal position
176  if ($dp === false) {
177  $sigfigs = strlen(rtrim($n, '0'));
178  } else {
179  $sigfigs = strlen(ltrim($n, '0.')); // eliminate extra decimal character
180  if ($dp !== 0) $sigfigs--;
181  }
182  return $sigfigs;
183  }
184 
188  private function add($s1, $s2, $scale) {
189  if ($this->bcmath) return bcadd($s1, $s2, $scale);
190  else return $this->scale($s1 + $s2, $scale);
191  }
192 
196  private function mul($s1, $s2, $scale) {
197  if ($this->bcmath) return bcmul($s1, $s2, $scale);
198  else return $this->scale($s1 * $s2, $scale);
199  }
200 
204  private function div($s1, $s2, $scale) {
205  if ($this->bcmath) return bcdiv($s1, $s2, $scale);
206  else return $this->scale($s1 / $s2, $scale);
207  }
208 
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
217  if ($this->bcmath) {
218  if ($rp >= 0) {
219  $n = bcadd($n, $neg . '0.' . str_repeat('0', $rp) . '5', $rp + 1);
220  $n = bcdiv($n, '1', $rp);
221  } else {
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);
226  }
227  return $n;
228  } else {
229  return $this->scale(round($n, $sigfigs - $new_log - 1), $rp + 1);
230  }
231  }
232 
236  private function scale($r, $scale) {
237  if ($scale < 0) {
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
244  // appropriately.
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);
248  }
249  return sprintf('%.' . $scale . 'f', (float) $r);
250  }
251 
252 }
253 
254 // vim: et sw=4 sts=4