1
<?xml version="1.0" encoding="UTF-8"?>
4
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
Copyright 2008 by Sun Microsystems, Inc.
8
OpenOffice.org - a multi-platform office productivity suite
10
$RCSfile: measure_conversion.xsl,v $
14
This file is part of OpenOffice.org.
16
OpenOffice.org is free software: you can redistribute it and/or modify
17
it under the terms of the GNU Lesser General Public License version 3
18
only, as published by the Free Software Foundation.
20
OpenOffice.org is distributed in the hope that it will be useful,
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
GNU Lesser General Public License version 3 for more details
24
(a copy is included in the LICENSE file that accompanied this code).
26
You should have received a copy of the GNU Lesser General Public License
27
version 3 along with OpenOffice.org. If not, see
28
<http://www.openoffice.org/license.html>
29
for a copy of the LGPLv3 License.
33
For further documentation and updates visit http://xml.openoffice.org/odf2xhtml
35
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
36
<!-- DPI (dots per inch) the standard resolution of given pictures (necessary for the conversion of 'cm' into 'pixel')
37
Although many pictures have a 96 dpi resolution, a higher resoltion give better results for common browsers -->
38
<xsl:param name="dpi" select="111"/>
39
<xsl:param name="centimeter-in-mm" select="10"/>
40
<xsl:param name="inch-in-mm" select="25.4"/>
41
<xsl:param name="didot-point-in-mm" select="0.376065"/>
42
<xsl:param name="pica-in-mm" select="4.2333333"/>
43
<xsl:param name="point-in-mm" select="0.3527778"/>
44
<xsl:param name="twip-in-mm" select="0.017636684"/>
45
<xsl:param name="pixel-in-mm" select="$inch-in-mm div $dpi"/>
46
<!-- ***** MEASUREMENT CONVERSIONS *****
48
The measure to be converted.
49
The current measure is judged by a substring (e.g. 'mm', 'cm', 'in', 'pica'...)
50
directly added to the number.
52
PARAM 'rounding-factor'
53
Is used for the rounding of decimal places.
54
The parameter number is the product of 1 and some '10', where
55
every zero represents a decimal place.
57
For example, providing as parameter:
58
<xsl:param name="rounding-factor" select="10000" />
59
Gives by default four decimal places.
61
To round two decimal places, basically the following is done:
62
<xsl:value-of select="round(100 * value) div 100"/>
64
RETURN The converted number, by default rounded to four decimal places.
65
In case the input measure could not be matched the same value is
66
returned and a warning message is written out.
71
* 1 milimeter (mm), the basic measure
73
* 1 centimeter (cm) = 10 mm
75
* 1 inch (in) = 25.4 mm
76
While the English have already seen the light (read: the metric system), the US
77
remains loyal to this medieval system.
79
* 1 point (pt) = 0.35277777.. mm
80
Sometimes called PostScript point (ppt), as when Adobe created PostScript, they added their own system of points.
81
There are exactly 72 PostScript points in 1 inch.
83
* 1 twip = twentieth of a (PostScript) point
84
A twip (twentieth of a point) is a 1/20th of a PostScript point, a traditional measure in printing.
86
* 1 didot point (dpt) = 0.376065 mm
87
Didot point after the French typographer Firmin Didot (1764-1836).
90
http://www.unc.edu/~rowlett/units/dictP.html:
91
"A unit of length used by typographers and printers. When printing was done
92
from hand-set metal type, one point represented the smallest element of type
93
that could be handled, roughly 1/64 inch. Eventually, the point was standardized
94
in Britain and America as exactly 1/72.27 = 0.013 837 inch, which is
95
about 0.35 mm (351.46 micrometers). In continental Europe, typographers
96
traditionally used a slightly larger point of 0.014 83 inch (about
97
1/72 pouce, 0.377 mm, or roughly 1/67 English inch), called a Didot point
98
after the French typographer Firmin Didot (1764-1836). In the U.S.,
99
Adobe software defines the point to be exactly 1/72 inch (0.013 888 9 inch
100
or 0.352 777 8 millimeters) and TeX software uses a slightly smaller point
101
of 0.351 459 8035 mm. The German standards agency DIN has proposed that
102
all these units be replaced by multiples of 0.25 millimeters (1/101.6 inch).
104
* 1 pica = 4.233333 mm
105
1/6 inch or 12 points
107
* 1 pixel (px) = 0.26458333.. mm (relative to 'DPI', here: 96 dpi)
108
Most pictures have the 96 dpi resolution, but the dpi variable may vary by stylesheet parameter
112
<!-- changing measure to mm -->
113
<xsl:template name="convert2mm">
114
<xsl:param name="value"/>
115
<xsl:param name="rounding-factor" select="10000"/>
117
<xsl:when test="contains($value, 'mm')">
118
<xsl:value-of select="substring-before($value, 'mm')"/>
120
<xsl:when test="contains($value, 'cm')">
121
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm' ) * $centimeter-in-mm)) div $rounding-factor"/>
123
<xsl:when test="contains($value, 'in')">
124
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in' ) * $inch-in-mm)) div $rounding-factor"/>
126
<xsl:when test="contains($value, 'pt')">
127
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') * $point-in-mm)) div $rounding-factor"/>
129
<xsl:when test="contains($value, 'twip')">
130
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') * $twip-in-mm)) div $rounding-factor"/>
132
<xsl:when test="contains($value, 'dpt')">
133
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') * $didot-point-in-mm)) div $rounding-factor"/>
135
<xsl:when test="contains($value, 'pica')">
136
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') * $pica-in-mm)) div $rounding-factor"/>
138
<xsl:when test="contains($value, 'px')">
139
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') * $pixel-in-mm)) div $rounding-factor"/>
142
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'mm'!</xsl:message>
143
<xsl:value-of select="$value"/>
147
<!-- changing measure to cm -->
148
<xsl:template name="convert2cm">
149
<xsl:param name="value"/>
150
<xsl:param name="rounding-factor" select="10000"/>
152
<xsl:when test="contains($value, 'mm')">
153
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $centimeter-in-mm)) div $rounding-factor"/>
155
<xsl:when test="contains($value, 'cm')">
156
<xsl:value-of select="substring-before($value, 'cm')"/>
158
<xsl:when test="contains($value, 'in')">
159
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $centimeter-in-mm * $inch-in-mm)) div $rounding-factor"/>
161
<xsl:when test="contains($value, 'pt')">
162
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $centimeter-in-mm * $point-in-mm)) div $rounding-factor"/>
164
<xsl:when test="contains($value, 'dpt')">
165
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $centimeter-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
167
<xsl:when test="contains($value, 'pica')">
168
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $centimeter-in-mm * $pica-in-mm)) div $rounding-factor"/>
170
<xsl:when test="contains($value, 'twip')">
171
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $centimeter-in-mm * $twip-in-mm)) div $rounding-factor"/>
173
<xsl:when test="contains($value, 'px')">
174
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $centimeter-in-mm * $pixel-in-mm)) div $rounding-factor"/>
177
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'cm'!</xsl:message>
178
<xsl:value-of select="$value"/>
182
<!-- changing measure to inch (cp. section comment) -->
183
<xsl:template name="convert2in">
184
<xsl:param name="value"/>
185
<xsl:param name="rounding-factor" select="10000"/>
187
<xsl:when test="contains($value, 'mm')">
188
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $inch-in-mm)) div $rounding-factor"/>
190
<xsl:when test="contains($value, 'cm')">
191
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $inch-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
193
<xsl:when test="contains($value, 'in')">
194
<xsl:value-of select="substring-before($value, 'in')"/>
196
<xsl:when test="contains($value, 'pt')">
197
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $inch-in-mm * $point-in-mm)) div $rounding-factor"/>
199
<xsl:when test="contains($value, 'dpt')">
200
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $inch-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
202
<xsl:when test="contains($value, 'pica')">
203
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $inch-in-mm * $pica-in-mm)) div $rounding-factor"/>
205
<xsl:when test="contains($value, 'twip')">
206
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $inch-in-mm * $twip-in-mm)) div $rounding-factor"/>
208
<xsl:when test="contains($value, 'px')">
209
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $inch-in-mm * $pixel-in-mm)) div $rounding-factor"/>
212
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'in'!</xsl:message>
213
<xsl:value-of select="$value"/>
217
<!-- changing measure to dpt (cp. section comment) -->
218
<xsl:template name="convert2dpt">
219
<xsl:param name="value"/>
220
<xsl:param name="rounding-factor" select="10000"/>
222
<xsl:when test="contains($value, 'mm')">
223
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $didot-point-in-mm)) div $rounding-factor"/>
225
<xsl:when test="contains($value, 'cm')">
226
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $didot-point-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
228
<xsl:when test="contains($value, 'in')">
229
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $didot-point-in-mm * $inch-in-mm)) div $rounding-factor"/>
231
<xsl:when test="contains($value, 'pt')">
232
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $didot-point-in-mm * $point-in-mm)) div $rounding-factor"/>
234
<xsl:when test="contains($value, 'dpt')">
235
<xsl:value-of select="substring-before($value, 'dpt')"/>
237
<xsl:when test="contains($value, 'pica')">
238
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $didot-point-in-mm * $pica-in-mm)) div $rounding-factor"/>
240
<xsl:when test="contains($value, 'twip')">
241
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $didot-point-in-mm * $twip-in-mm)) div $rounding-factor"/>
243
<xsl:when test="contains($value, 'px')">
244
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $didot-point-in-mm * $pixel-in-mm)) div $rounding-factor"/>
247
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'dpt'!</xsl:message>
248
<xsl:value-of select="$value"/>
252
<!-- changing measure to pica (cp. section comment) -->
253
<xsl:template name="convert2pica">
254
<xsl:param name="value"/>
255
<xsl:param name="rounding-factor" select="10000"/>
257
<xsl:when test="contains($value, 'mm')">
258
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $pica-in-mm)) div $rounding-factor"/>
260
<xsl:when test="contains($value, 'cm')">
261
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $pica-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
263
<xsl:when test="contains($value, 'in')">
264
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $pica-in-mm * $inch-in-mm)) div $rounding-factor"/>
266
<xsl:when test="contains($value, 'pt')">
267
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $pica-in-mm * $point-in-mm)) div $rounding-factor"/>
269
<xsl:when test="contains($value, 'dpt')">
270
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $pica-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
272
<xsl:when test="contains($value, 'pica')">
273
<xsl:value-of select="substring-before($value, 'pica')"/>
275
<xsl:when test="contains($value, 'twip')">
276
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $pica-in-mm * $twip-in-mm)) div $rounding-factor"/>
278
<xsl:when test="contains($value, 'px')">
279
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $pica-in-mm * $pixel-in-mm)) div $rounding-factor"/>
282
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'pica'!</xsl:message>
283
<xsl:value-of select="$value"/>
287
<!-- changing measure to pt (cp. section comment) -->
288
<xsl:template name="convert2pt">
289
<xsl:param name="value"/>
290
<xsl:param name="rounding-factor" select="10000"/>
292
<xsl:when test="contains($value, 'mm')">
293
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $point-in-mm)) div $rounding-factor"/>
295
<xsl:when test="contains($value, 'cm')">
296
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $point-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
298
<xsl:when test="contains($value, 'in')">
299
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $point-in-mm * $inch-in-mm)) div $rounding-factor"/>
301
<xsl:when test="contains($value, 'pt')">
302
<xsl:value-of select="substring-before($value, 'pt')"/>
304
<xsl:when test="contains($value, 'dpt')">
305
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $point-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
307
<xsl:when test="contains($value, 'pica')">
308
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $point-in-mm * $pica-in-mm)) div $rounding-factor"/>
310
<xsl:when test="contains($value, 'twip')">
311
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $point-in-mm * $twip-in-mm)) div $rounding-factor"/>
313
<xsl:when test="contains($value, 'px')">
314
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $point-in-mm * $pixel-in-mm)) div $rounding-factor"/>
317
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'pt'!</xsl:message>
318
<xsl:value-of select="$value"/>
322
<!-- changing measure to twip (cp. section comment) -->
323
<xsl:template name="convert2twip">
324
<xsl:param name="value"/>
325
<xsl:param name="rounding-factor" select="10000"/>
327
<xsl:when test="contains($value, 'mm')">
328
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $twip-in-mm)) div $rounding-factor"/>
330
<xsl:when test="contains($value, 'cm')">
331
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $twip-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
333
<xsl:when test="contains($value, 'in')">
334
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $twip-in-mm * $inch-in-mm)) div $rounding-factor"/>
336
<xsl:when test="contains($value, 'pt')">
337
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $twip-in-mm * $point-in-mm)) div $rounding-factor"/>
339
<xsl:when test="contains($value, 'dpt')">
340
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $twip-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
342
<xsl:when test="contains($value, 'pica')">
343
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $twip-in-mm * $pica-in-mm)) div $rounding-factor"/>
345
<xsl:when test="contains($value, 'twip')">
346
<xsl:value-of select="substring-before($value, 'twip')"/>
348
<xsl:when test="contains($value, 'px')">
349
<xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $twip-in-mm * $pixel-in-mm)) div $rounding-factor"/>
352
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'twip'!</xsl:message>
353
<xsl:value-of select="$value"/>
357
<!-- changing measure to pixel by via parameter provided dpi (dots per inch) standard factor (cp. section comment) -->
358
<xsl:template name="convert2px">
359
<xsl:param name="value"/>
361
<xsl:when test="contains($value, 'mm')">
362
<xsl:value-of select="round(number(substring-before($value, 'mm')) div $pixel-in-mm)"/>
364
<xsl:when test="contains($value, 'cm')">
365
<xsl:value-of select="round(number(substring-before($value, 'cm')) div $pixel-in-mm * $centimeter-in-mm)"/>
367
<xsl:when test="contains($value, 'in')">
368
<xsl:value-of select="round(number(substring-before($value, 'in')) div $pixel-in-mm * $inch-in-mm)"/>
370
<xsl:when test="contains($value, 'pt')">
371
<xsl:value-of select="round(number(substring-before($value, 'pt')) div $pixel-in-mm * $point-in-mm)"/>
373
<xsl:when test="contains($value, 'dpt')">
374
<xsl:value-of select="round(number(substring-before($value, 'dpt')) div $pixel-in-mm * $didot-point-in-mm)"/>
376
<xsl:when test="contains($value, 'pica')">
377
<xsl:value-of select="round(number(substring-before($value, 'pica')) div $pixel-in-mm * $pica-in-mm)"/>
379
<xsl:when test="contains($value, 'twip')">
380
<xsl:value-of select="round(number(substring-before($value, 'twip')) div $pixel-in-mm * $twip-in-mm)"/>
382
<xsl:when test="contains($value, 'px')">
383
<xsl:value-of select="$value"/>
386
<xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'px'!</xsl:message>
387
<xsl:value-of select="$value"/>