1<?php
2/**
3 * Simple helper class to work with units (e.g. 'px', 'pt', 'cm'...)
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     LarsDW223
7 */
8
9require_once DOKU_PLUGIN . 'odt/ODT/ODTUnits.php';
10
11// must be run within Dokuwiki
12if (!defined('DOKU_INC')) die();
13
14/**
15 * Class helper_plugin_odt_units
16 *
17 * @package helper\units
18 */
19class helper_plugin_odt_units extends DokuWiki_Plugin {
20    protected $internal = NULL;
21
22    public function __construct() {
23        $this->internal = new ODTUnits();
24    }
25
26    /**
27     * @return array
28     */
29    function getMethods() {
30        $result = array();
31        $result[] = array(
32                'name'   => 'getColorValue',
33                'desc'   => 'returns the color value for a given CSS color name. Returns "#000000" if the name is unknown',
34                'params' => array('name' => 'string'),
35                'return' => array('color value' => 'string'),
36                );
37        return $result;
38    }
39
40    /**
41     * Strips of the leading digits from $value. So left over will be the unit only.
42     *
43     * @param int $value The length value string, e.g. '1cm'.
44     * @return string The unit of $value, e.g. 'cm'
45     */
46    public static function stripDigits ($value) {
47        return ODTUnits::stripDigits ($value);
48    }
49
50    /**
51     * Gets only the digits from $value without the unit.
52     *
53     * @param string|int $value The length value string, e.g. '1cm'.
54     * @return string The digits of $value, e.g. '1'
55     */
56    public static function getDigits ($value) {
57        return ODTUnits::getDigits ($value);
58    }
59
60    /**
61     * Checks if $unit is a valid XSL unit.
62     *
63     * @param string $unit The unit string, e.g. 'cm'.
64     * @return boolean true if valid, false otherwise
65     */
66    public static function isValidXSLUnit($unit) {
67        return ODTUnits::isValidXSLUnit($unit);
68    }
69
70    /**
71     * Checks if length value string $value has a valid XSL unit.
72     *
73     * @param string|int $value The length value string, e.g. '1cm'.
74     * @return boolean true if valid, false otherwise
75     */
76    public static function hasValidXSLUnit($value) {
77        return ODTUnits::hasValidXSLUnit($value);
78    }
79
80    /**
81     * Sets the pixel per em unit used for px to em conversion.
82     *
83     * @param int $value The value to be set.
84     */
85    public function setPixelPerEm ($value) {
86        $this->internal->setPixelPerEm ($value);
87    }
88
89    /**
90     * Query the pixel per em unit.
91     *
92     * @return int The current value.
93     */
94    public function getPixelPerEm () {
95        return $this->internal->getPixelPerEm ();
96    }
97
98    /**
99     * Sets the twips per pixel (X axis) used for px to pt conversion.
100     *
101     * @param int $value The value to be set.
102     */
103    public function setTwipsPerPixelX ($value) {
104        $this->internal->setTwipsPerPixelX ($value);
105    }
106
107    /**
108     * Sets the twips per pixel (Y axis) unit used for px to pt conversion.
109     *
110     * @param int $value The value to be set.
111     */
112    public function setTwipsPerPixelY ($value) {
113        $this->internal->setTwipsPerPixelY ($value);
114    }
115
116    /**
117     * Query the twips per pixel (X axis) setting.
118     *
119     * @return int The current value.
120     */
121    public function getTwipsPerPixelX () {
122        return $this->internal->getTwipsPerPixelX ();
123    }
124
125    /**
126     * Query the twips per pixel (Y axis) setting.
127     *
128     * @return int The current value.
129     */
130    public function getTwipsPerPixelY () {
131        return $this->internal->getTwipsPerPixelY();
132    }
133
134    /**
135     * Convert pixel (X axis) to points according to the current settings.
136     *
137     * @param string|int $pixel String with pixel length value, e.g. '20px'
138     * @return string The current value.
139     */
140    public function pixelToPointsX ($pixel) {
141        return $this->internal->pixelToPointsX ($pixel);
142    }
143
144    /**
145     * Convert pixel (Y axis) to points according to the current settings.
146     *
147     * @param string|int $pixel String with pixel length value, e.g. '20px'
148     * @return string The current value.
149     */
150    public function pixelToPointsY ($pixel) {
151        return $this->internal->pixelToPointsY ($pixel);
152    }
153
154    /**
155     * Convert length value with valid XSL unit to points.
156     *
157     * @param string $value  String with length value, e.g. '20px', '20cm'...
158     * @param string $axis   Is the value to be converted a value on the X or Y axis? Default is 'y'.
159     *        Only relevant for conversion from 'px' or 'em'.
160     * @return string The current value.
161     */
162    public function toPoints ($value, $axis = 'y') {
163        return $this->internal->toPoints ($value, $axis);
164    }
165
166    public function getInternal() {
167        return $this->internal;
168    }
169}
170