1<?php
2// $Header: /cvsroot/html2ps/utils_units.php,v 1.22 2007/01/24 18:56:10 Konstantin Exp $
3
4function round_units($value) {
5  return round($value,2);
6}
7
8function pt2pt($pt) {
9  return $pt * $GLOBALS['g_pt_scale'];
10}
11
12function px2pt($px) {
13  global $g_px_scale;
14  return $px * $g_px_scale;
15}
16
17function mm2pt($mm) {
18  return $mm * 2.834645669;
19}
20
21function units_mul($value, $koeff) {
22  if (preg_match("/(pt|pc|px|mm|cm|em|ex)$/",$value)) {
23    $units = substr($value, strlen($value)-2,2);
24  } else {
25    $units = "";
26  };
27
28  return sprintf("%.2f%s",
29                 round((double)$value * $koeff,2),
30                 $units);
31}
32
33function punits2pt($value, $font_size) {
34  $value = trim($value);
35
36  // Check if current value is percentage
37  if (substr($value, strlen($value)-1, 1) === "%") {
38    return array((float)$value, true);
39  } else {
40    return array(units2pt($value, $font_size), false);
41  }
42}
43
44function em2pt($value, $font_size) {
45  return $font_size * (double)$value * EM_KOEFF;
46}
47
48function ex2pt($value, $font_size) {
49  return $font_size * (double)$value * EX_KOEFF;
50}
51
52function units2pt($value, $font_size = null) {
53  $unit = Value::unit_from_string($value);
54
55  switch ($unit) {
56  case UNIT_PT:
57    return pt2pt((double)$value);
58  case UNIT_PX:
59    return px2pt((double)$value);
60  case UNIT_MM:
61    return pt2pt(mm2pt((double)$value));
62  case UNIT_CM:
63    return pt2pt(mm2pt((double)$value*10));
64  case UNIT_EM:
65    return em2pt((double)$value, $font_size);
66  case UNIT_EX:
67    return ex2pt((double)$value, $font_size);
68  case UNIT_IN:
69    return pt2pt((double)$value*72); // points used by CSS 2.1 are equal to 1/72nd of an inch.
70  case UNIT_PC:
71    return pt2pt((double)$value*12); // 1 pica equals to 12 points.
72  default:
73    global $g_config;
74    if ($g_config['mode'] === 'quirks') {
75      return px2pt((double)$value);
76    } else {
77      return 0;
78    };
79  };
80}
81
82?>