measure_conversion.xsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--***********************************************************
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. ***********************************************************-->
  22. <!--
  23. For further documentation and updates visit http://xml.openoffice.org/odf2xhtml
  24. -->
  25. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  26. <!-- DPI (dots per inch) the standard resolution of given pictures (necessary for the conversion of 'cm' into 'pixel')
  27. Although many pictures have a 96 dpi resolution, a higher resoltion give better results for common browsers -->
  28. <xsl:param name="dpi" select="111"/>
  29. <xsl:param name="centimeter-in-mm" select="10"/>
  30. <xsl:param name="inch-in-mm" select="25.4"/>
  31. <xsl:param name="didot-point-in-mm" select="0.376065"/>
  32. <xsl:param name="pica-in-mm" select="4.2333333"/>
  33. <xsl:param name="point-in-mm" select="0.3527778"/>
  34. <xsl:param name="twip-in-mm" select="0.017636684"/>
  35. <xsl:param name="pixel-in-mm" select="$inch-in-mm div $dpi"/>
  36. <!-- ***** MEASUREMENT CONVERSIONS *****
  37. PARAM 'value'
  38. The measure to be converted.
  39. The current measure is judged by a substring (e.g. 'mm', 'cm', 'in', 'pica'...)
  40. directly added to the number.
  41. PARAM 'rounding-factor'
  42. Is used for the rounding of decimal places.
  43. The parameter number is the product of 1 and some '10', where
  44. every zero represents a decimal place.
  45. For example, providing as parameter:
  46. <xsl:param name="rounding-factor" select="10000" />
  47. Gives by default four decimal places.
  48. To round two decimal places, basically the following is done:
  49. <xsl:value-of select="round(100 * value) div 100"/>
  50. RETURN The converted number, by default rounded to four decimal places.
  51. In case the input measure could not be matched the same value is
  52. returned and a warning message is written out.
  53. MEASURE LIST:
  54. * 1 milimeter (mm), the basic measure
  55. * 1 centimeter (cm) = 10 mm
  56. * 1 inch (in) = 25.4 mm
  57. While the English have already seen the light (read: the metric system), the US
  58. remains loyal to this medieval system.
  59. * 1 point (pt) = 0.35277777.. mm
  60. Sometimes called PostScript point (ppt), as when Adobe created PostScript, they added their own system of points.
  61. There are exactly 72 PostScript points in 1 inch.
  62. * 1 twip = twentieth of a (PostScript) point
  63. A twip (twentieth of a point) is a 1/20th of a PostScript point, a traditional measure in printing.
  64. * 1 didot point (dpt) = 0.376065 mm
  65. Didot point after the French typographer Firmin Didot (1764-1836).
  66. More details under
  67. http://www.unc.edu/~rowlett/units/dictP.html:
  68. "A unit of length used by typographers and printers. When printing was done
  69. from hand-set metal type, one point represented the smallest element of type
  70. that could be handled, roughly 1/64 inch. Eventually, the point was standardized
  71. in Britain and America as exactly 1/72.27 = 0.013 837 inch, which is
  72. about 0.35 mm (351.46 micrometers). In continental Europe, typographers
  73. traditionally used a slightly larger point of 0.014 83 inch (about
  74. 1/72 pouce, 0.377 mm, or roughly 1/67 English inch), called a Didot point
  75. after the French typographer Firmin Didot (1764-1836). In the U.S.,
  76. Adobe software defines the point to be exactly 1/72 inch (0.013 888 9 inch
  77. or 0.352 777 8 millimeters) and TeX software uses a slightly smaller point
  78. of 0.351 459 8035 mm. The German standards agency DIN has proposed that
  79. all these units be replaced by multiples of 0.25 millimeters (1/101.6 inch).
  80. * 1 pica = 4.233333 mm
  81. 1/6 inch or 12 points
  82. * 1 pixel (px) = 0.26458333.. mm (relative to 'DPI', here: 96 dpi)
  83. Most pictures have the 96 dpi resolution, but the dpi variable may vary by stylesheet parameter
  84. -->
  85. <!-- changing measure to mm -->
  86. <xsl:template name="convert2mm">
  87. <xsl:param name="value"/>
  88. <xsl:param name="rounding-factor" select="10000"/>
  89. <xsl:choose>
  90. <xsl:when test="contains($value, 'mm')">
  91. <xsl:value-of select="substring-before($value, 'mm')"/>
  92. </xsl:when>
  93. <xsl:when test="contains($value, 'cm')">
  94. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm' ) * $centimeter-in-mm)) div $rounding-factor"/>
  95. </xsl:when>
  96. <xsl:when test="contains($value, 'in')">
  97. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in' ) * $inch-in-mm)) div $rounding-factor"/>
  98. </xsl:when>
  99. <xsl:when test="contains($value, 'pt')">
  100. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') * $point-in-mm)) div $rounding-factor"/>
  101. </xsl:when>
  102. <xsl:when test="contains($value, 'twip')">
  103. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') * $twip-in-mm)) div $rounding-factor"/>
  104. </xsl:when>
  105. <xsl:when test="contains($value, 'dpt')">
  106. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') * $didot-point-in-mm)) div $rounding-factor"/>
  107. </xsl:when>
  108. <xsl:when test="contains($value, 'pica')">
  109. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') * $pica-in-mm)) div $rounding-factor"/>
  110. </xsl:when>
  111. <xsl:when test="contains($value, 'px')">
  112. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') * $pixel-in-mm)) div $rounding-factor"/>
  113. </xsl:when>
  114. <xsl:otherwise>
  115. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'mm'!</xsl:message>
  116. <xsl:value-of select="$value"/>
  117. </xsl:otherwise>
  118. </xsl:choose>
  119. </xsl:template>
  120. <!-- changing measure to cm -->
  121. <xsl:template name="convert2cm">
  122. <xsl:param name="value"/>
  123. <xsl:param name="rounding-factor" select="10000"/>
  124. <xsl:choose>
  125. <xsl:when test="contains($value, 'mm')">
  126. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $centimeter-in-mm)) div $rounding-factor"/>
  127. </xsl:when>
  128. <xsl:when test="contains($value, 'cm')">
  129. <xsl:value-of select="substring-before($value, 'cm')"/>
  130. </xsl:when>
  131. <xsl:when test="contains($value, 'in')">
  132. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $centimeter-in-mm * $inch-in-mm)) div $rounding-factor"/>
  133. </xsl:when>
  134. <xsl:when test="contains($value, 'pt')">
  135. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $centimeter-in-mm * $point-in-mm)) div $rounding-factor"/>
  136. </xsl:when>
  137. <xsl:when test="contains($value, 'dpt')">
  138. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $centimeter-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  139. </xsl:when>
  140. <xsl:when test="contains($value, 'pica')">
  141. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $centimeter-in-mm * $pica-in-mm)) div $rounding-factor"/>
  142. </xsl:when>
  143. <xsl:when test="contains($value, 'twip')">
  144. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $centimeter-in-mm * $twip-in-mm)) div $rounding-factor"/>
  145. </xsl:when>
  146. <xsl:when test="contains($value, 'px')">
  147. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $centimeter-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  148. </xsl:when>
  149. <xsl:otherwise>
  150. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'cm'!</xsl:message>
  151. <xsl:value-of select="$value"/>
  152. </xsl:otherwise>
  153. </xsl:choose>
  154. </xsl:template>
  155. <!-- changing measure to inch (cp. section comment) -->
  156. <xsl:template name="convert2in">
  157. <xsl:param name="value"/>
  158. <xsl:param name="rounding-factor" select="10000"/>
  159. <xsl:choose>
  160. <xsl:when test="contains($value, 'mm')">
  161. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $inch-in-mm)) div $rounding-factor"/>
  162. </xsl:when>
  163. <xsl:when test="contains($value, 'cm')">
  164. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $inch-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  165. </xsl:when>
  166. <xsl:when test="contains($value, 'in')">
  167. <xsl:value-of select="substring-before($value, 'in')"/>
  168. </xsl:when>
  169. <xsl:when test="contains($value, 'pt')">
  170. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $inch-in-mm * $point-in-mm)) div $rounding-factor"/>
  171. </xsl:when>
  172. <xsl:when test="contains($value, 'dpt')">
  173. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $inch-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  174. </xsl:when>
  175. <xsl:when test="contains($value, 'pica')">
  176. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $inch-in-mm * $pica-in-mm)) div $rounding-factor"/>
  177. </xsl:when>
  178. <xsl:when test="contains($value, 'twip')">
  179. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $inch-in-mm * $twip-in-mm)) div $rounding-factor"/>
  180. </xsl:when>
  181. <xsl:when test="contains($value, 'px')">
  182. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $inch-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  183. </xsl:when>
  184. <xsl:otherwise>
  185. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'in'!</xsl:message>
  186. <xsl:value-of select="$value"/>
  187. </xsl:otherwise>
  188. </xsl:choose>
  189. </xsl:template>
  190. <!-- changing measure to dpt (cp. section comment) -->
  191. <xsl:template name="convert2dpt">
  192. <xsl:param name="value"/>
  193. <xsl:param name="rounding-factor" select="10000"/>
  194. <xsl:choose>
  195. <xsl:when test="contains($value, 'mm')">
  196. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $didot-point-in-mm)) div $rounding-factor"/>
  197. </xsl:when>
  198. <xsl:when test="contains($value, 'cm')">
  199. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $didot-point-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  200. </xsl:when>
  201. <xsl:when test="contains($value, 'in')">
  202. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $didot-point-in-mm * $inch-in-mm)) div $rounding-factor"/>
  203. </xsl:when>
  204. <xsl:when test="contains($value, 'pt')">
  205. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $didot-point-in-mm * $point-in-mm)) div $rounding-factor"/>
  206. </xsl:when>
  207. <xsl:when test="contains($value, 'dpt')">
  208. <xsl:value-of select="substring-before($value, 'dpt')"/>
  209. </xsl:when>
  210. <xsl:when test="contains($value, 'pica')">
  211. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $didot-point-in-mm * $pica-in-mm)) div $rounding-factor"/>
  212. </xsl:when>
  213. <xsl:when test="contains($value, 'twip')">
  214. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $didot-point-in-mm * $twip-in-mm)) div $rounding-factor"/>
  215. </xsl:when>
  216. <xsl:when test="contains($value, 'px')">
  217. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $didot-point-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  218. </xsl:when>
  219. <xsl:otherwise>
  220. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'dpt'!</xsl:message>
  221. <xsl:value-of select="$value"/>
  222. </xsl:otherwise>
  223. </xsl:choose>
  224. </xsl:template>
  225. <!-- changing measure to pica (cp. section comment) -->
  226. <xsl:template name="convert2pica">
  227. <xsl:param name="value"/>
  228. <xsl:param name="rounding-factor" select="10000"/>
  229. <xsl:choose>
  230. <xsl:when test="contains($value, 'mm')">
  231. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $pica-in-mm)) div $rounding-factor"/>
  232. </xsl:when>
  233. <xsl:when test="contains($value, 'cm')">
  234. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $pica-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  235. </xsl:when>
  236. <xsl:when test="contains($value, 'in')">
  237. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $pica-in-mm * $inch-in-mm)) div $rounding-factor"/>
  238. </xsl:when>
  239. <xsl:when test="contains($value, 'pt')">
  240. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $pica-in-mm * $point-in-mm)) div $rounding-factor"/>
  241. </xsl:when>
  242. <xsl:when test="contains($value, 'dpt')">
  243. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $pica-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  244. </xsl:when>
  245. <xsl:when test="contains($value, 'pica')">
  246. <xsl:value-of select="substring-before($value, 'pica')"/>
  247. </xsl:when>
  248. <xsl:when test="contains($value, 'twip')">
  249. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $pica-in-mm * $twip-in-mm)) div $rounding-factor"/>
  250. </xsl:when>
  251. <xsl:when test="contains($value, 'px')">
  252. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $pica-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  253. </xsl:when>
  254. <xsl:otherwise>
  255. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'pica'!</xsl:message>
  256. <xsl:value-of select="$value"/>
  257. </xsl:otherwise>
  258. </xsl:choose>
  259. </xsl:template>
  260. <!-- changing measure to pt (cp. section comment) -->
  261. <xsl:template name="convert2pt">
  262. <xsl:param name="value"/>
  263. <xsl:param name="rounding-factor" select="10000"/>
  264. <xsl:choose>
  265. <xsl:when test="contains($value, 'mm')">
  266. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $point-in-mm)) div $rounding-factor"/>
  267. </xsl:when>
  268. <xsl:when test="contains($value, 'cm')">
  269. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $point-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  270. </xsl:when>
  271. <xsl:when test="contains($value, 'in')">
  272. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $point-in-mm * $inch-in-mm)) div $rounding-factor"/>
  273. </xsl:when>
  274. <xsl:when test="contains($value, 'pt')">
  275. <xsl:value-of select="substring-before($value, 'pt')"/>
  276. </xsl:when>
  277. <xsl:when test="contains($value, 'dpt')">
  278. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $point-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  279. </xsl:when>
  280. <xsl:when test="contains($value, 'pica')">
  281. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $point-in-mm * $pica-in-mm)) div $rounding-factor"/>
  282. </xsl:when>
  283. <xsl:when test="contains($value, 'twip')">
  284. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $point-in-mm * $twip-in-mm)) div $rounding-factor"/>
  285. </xsl:when>
  286. <xsl:when test="contains($value, 'px')">
  287. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $point-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  288. </xsl:when>
  289. <xsl:otherwise>
  290. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'pt'!</xsl:message>
  291. <xsl:value-of select="$value"/>
  292. </xsl:otherwise>
  293. </xsl:choose>
  294. </xsl:template>
  295. <!-- changing measure to twip (cp. section comment) -->
  296. <xsl:template name="convert2twip">
  297. <xsl:param name="value"/>
  298. <xsl:param name="rounding-factor" select="10000"/>
  299. <xsl:choose>
  300. <xsl:when test="contains($value, 'mm')">
  301. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $twip-in-mm)) div $rounding-factor"/>
  302. </xsl:when>
  303. <xsl:when test="contains($value, 'cm')">
  304. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $twip-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  305. </xsl:when>
  306. <xsl:when test="contains($value, 'in')">
  307. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $twip-in-mm * $inch-in-mm)) div $rounding-factor"/>
  308. </xsl:when>
  309. <xsl:when test="contains($value, 'pt')">
  310. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $twip-in-mm * $point-in-mm)) div $rounding-factor"/>
  311. </xsl:when>
  312. <xsl:when test="contains($value, 'dpt')">
  313. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $twip-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  314. </xsl:when>
  315. <xsl:when test="contains($value, 'pica')">
  316. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $twip-in-mm * $pica-in-mm)) div $rounding-factor"/>
  317. </xsl:when>
  318. <xsl:when test="contains($value, 'twip')">
  319. <xsl:value-of select="substring-before($value, 'twip')"/>
  320. </xsl:when>
  321. <xsl:when test="contains($value, 'px')">
  322. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $twip-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  323. </xsl:when>
  324. <xsl:otherwise>
  325. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'twip'!</xsl:message>
  326. <xsl:value-of select="$value"/>
  327. </xsl:otherwise>
  328. </xsl:choose>
  329. </xsl:template>
  330. <!-- changing measure to pixel by via parameter provided dpi (dots per inch) standard factor (cp. section comment) -->
  331. <xsl:template name="convert2px">
  332. <xsl:param name="value"/>
  333. <xsl:choose>
  334. <xsl:when test="contains($value, 'mm')">
  335. <xsl:value-of select="round(number(substring-before($value, 'mm')) div $pixel-in-mm)"/>
  336. </xsl:when>
  337. <xsl:when test="contains($value, 'cm')">
  338. <xsl:value-of select="round(number(substring-before($value, 'cm')) div $pixel-in-mm * $centimeter-in-mm)"/>
  339. </xsl:when>
  340. <xsl:when test="contains($value, 'in')">
  341. <xsl:value-of select="round(number(substring-before($value, 'in')) div $pixel-in-mm * $inch-in-mm)"/>
  342. </xsl:when>
  343. <xsl:when test="contains($value, 'pt')">
  344. <xsl:value-of select="round(number(substring-before($value, 'pt')) div $pixel-in-mm * $point-in-mm)"/>
  345. </xsl:when>
  346. <xsl:when test="contains($value, 'dpt')">
  347. <xsl:value-of select="round(number(substring-before($value, 'dpt')) div $pixel-in-mm * $didot-point-in-mm)"/>
  348. </xsl:when>
  349. <xsl:when test="contains($value, 'pica')">
  350. <xsl:value-of select="round(number(substring-before($value, 'pica')) div $pixel-in-mm * $pica-in-mm)"/>
  351. </xsl:when>
  352. <xsl:when test="contains($value, 'twip')">
  353. <xsl:value-of select="round(number(substring-before($value, 'twip')) div $pixel-in-mm * $twip-in-mm)"/>
  354. </xsl:when>
  355. <xsl:when test="contains($value, 'px')">
  356. <xsl:value-of select="$value"/>
  357. </xsl:when>
  358. <xsl:otherwise>
  359. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'px'!</xsl:message>
  360. <xsl:value-of select="$value"/>
  361. </xsl:otherwise>
  362. </xsl:choose>
  363. </xsl:template>
  364. </xsl:stylesheet>