measure_conversion.xsl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * This file incorporates work covered by the following license notice:
  10. *
  11. * Licensed to the Apache Software Foundation (ASF) under one or more
  12. * contributor license agreements. See the NOTICE file distributed
  13. * with this work for additional information regarding copyright
  14. * ownership. The ASF licenses this file to you under the Apache
  15. * License, Version 2.0 (the "License"); you may not use this file
  16. * except in compliance with the License. You may obtain a copy of
  17. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  18. -->
  19. <!--
  20. For further documentation and updates visit http://xml.openoffice.org/odf2xhtml
  21. -->
  22. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  23. <!-- DPI (dots per inch) the standard resolution of given pictures (necessary for the conversion of 'cm' into 'pixel')
  24. Although many pictures have a 96 dpi resolution, a higher resolution give better results for common browsers -->
  25. <xsl:param name="dpi" select="111"/>
  26. <xsl:param name="centimeter-in-mm" select="10"/>
  27. <xsl:param name="inch-in-mm" select="25.4"/>
  28. <xsl:param name="didot-point-in-mm" select="0.376065"/>
  29. <xsl:param name="pica-in-mm" select="4.2333333"/>
  30. <xsl:param name="point-in-mm" select="0.3527778"/>
  31. <xsl:param name="twip-in-mm" select="0.017636684"/>
  32. <xsl:param name="pixel-in-mm" select="$inch-in-mm div $dpi"/>
  33. <!-- ***** MEASUREMENT CONVERSIONS *****
  34. PARAM 'value'
  35. The measure to be converted.
  36. The current measure is judged by a substring (e.g. 'mm', 'cm', 'in', 'pica'...)
  37. directly added to the number.
  38. PARAM 'rounding-factor'
  39. Is used for the rounding of decimal places.
  40. The parameter number is the product of 1 and some '10', where
  41. every zero represents a decimal place.
  42. For example, providing as parameter:
  43. <xsl:param name="rounding-factor" select="10000" />
  44. Gives by default four decimal places.
  45. To round two decimal places, basically the following is done:
  46. <xsl:value-of select="round(100 * value) div 100"/>
  47. RETURN The converted number, by default rounded to four decimal places.
  48. In case the input measure could not be matched the same value is
  49. returned and a warning message is written out.
  50. MEASURE LIST:
  51. * 1 millimeter (mm), the basic measure
  52. * 1 centimeter (cm) = 10 mm
  53. * 1 inch (in) = 25.4 mm
  54. While the English have already seen the light (read: the metric system), the US
  55. remains loyal to this medieval system.
  56. * 1 point (pt) = 0.35277777.. mm
  57. Sometimes called PostScript point (ppt), as when Adobe created PostScript, they added their own system of points.
  58. There are exactly 72 PostScript points in 1 inch.
  59. * 1 twip = twentieth of a (PostScript) point
  60. A twip (twentieth of a point) is a 1/20th of a PostScript point, a traditional measure in printing.
  61. * 1 didot point (dpt) = 0.376065 mm
  62. Didot point after the French typographer Firmin Didot (1764-1836).
  63. More details under
  64. http://www.unc.edu/~rowlett/units/dictP.html:
  65. "A unit of length used by typographers and printers. When printing was done
  66. from hand-set metal type, one point represented the smallest element of type
  67. that could be handled, roughly 1/64 inch. Eventually, the point was standardized
  68. in Britain and America as exactly 1/72.27 = 0.013 837 inch, which is
  69. about 0.35 mm (351.46 micrometers). In continental Europe, typographers
  70. traditionally used a slightly larger point of 0.014 83 inch (about
  71. 1/72 pouce, 0.377 mm, or roughly 1/67 English inch), called a Didot point
  72. after the French typographer Firmin Didot (1764-1836). In the U.S.,
  73. Adobe software defines the point to be exactly 1/72 inch (0.013 888 9 inch
  74. or 0.352 777 8 millimeters) and TeX software uses a slightly smaller point
  75. of 0.351 459 8035 mm. The German standards agency DIN has proposed that
  76. all these units be replaced by multiples of 0.25 millimeters (1/101.6 inch).
  77. * 1 pica = 4.233333 mm
  78. 1/6 inch or 12 points
  79. * 1 pixel (px) = 0.26458333.. mm (relative to 'DPI', here: 96 dpi)
  80. Most pictures have the 96 dpi resolution, but the dpi variable may vary by stylesheet parameter
  81. -->
  82. <!-- changing measure to mm -->
  83. <xsl:template name="convert2mm">
  84. <xsl:param name="value"/>
  85. <xsl:param name="rounding-factor" select="10000"/>
  86. <xsl:choose>
  87. <xsl:when test="contains($value, 'mm')">
  88. <xsl:value-of select="substring-before($value, 'mm')"/>
  89. </xsl:when>
  90. <xsl:when test="contains($value, 'cm')">
  91. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm' ) * $centimeter-in-mm)) div $rounding-factor"/>
  92. </xsl:when>
  93. <xsl:when test="contains($value, 'in')">
  94. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in' ) * $inch-in-mm)) div $rounding-factor"/>
  95. </xsl:when>
  96. <xsl:when test="contains($value, 'pt')">
  97. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') * $point-in-mm)) div $rounding-factor"/>
  98. </xsl:when>
  99. <xsl:when test="contains($value, 'twip')">
  100. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') * $twip-in-mm)) div $rounding-factor"/>
  101. </xsl:when>
  102. <xsl:when test="contains($value, 'dpt')">
  103. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') * $didot-point-in-mm)) div $rounding-factor"/>
  104. </xsl:when>
  105. <xsl:when test="contains($value, 'pica')">
  106. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') * $pica-in-mm)) div $rounding-factor"/>
  107. </xsl:when>
  108. <xsl:when test="contains($value, 'px')">
  109. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') * $pixel-in-mm)) div $rounding-factor"/>
  110. </xsl:when>
  111. <xsl:otherwise>
  112. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'mm'!</xsl:message>
  113. <xsl:value-of select="$value"/>
  114. </xsl:otherwise>
  115. </xsl:choose>
  116. </xsl:template>
  117. <!-- changing measure to cm -->
  118. <xsl:template name="convert2cm">
  119. <xsl:param name="value"/>
  120. <xsl:param name="rounding-factor" select="10000"/>
  121. <xsl:choose>
  122. <xsl:when test="contains($value, 'mm')">
  123. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $centimeter-in-mm)) div $rounding-factor"/>
  124. </xsl:when>
  125. <xsl:when test="contains($value, 'cm')">
  126. <xsl:value-of select="substring-before($value, 'cm')"/>
  127. </xsl:when>
  128. <xsl:when test="contains($value, 'in')">
  129. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $centimeter-in-mm * $inch-in-mm)) div $rounding-factor"/>
  130. </xsl:when>
  131. <xsl:when test="contains($value, 'pt')">
  132. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $centimeter-in-mm * $point-in-mm)) div $rounding-factor"/>
  133. </xsl:when>
  134. <xsl:when test="contains($value, 'dpt')">
  135. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $centimeter-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  136. </xsl:when>
  137. <xsl:when test="contains($value, 'pica')">
  138. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $centimeter-in-mm * $pica-in-mm)) div $rounding-factor"/>
  139. </xsl:when>
  140. <xsl:when test="contains($value, 'twip')">
  141. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $centimeter-in-mm * $twip-in-mm)) div $rounding-factor"/>
  142. </xsl:when>
  143. <xsl:when test="contains($value, 'px')">
  144. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $centimeter-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  145. </xsl:when>
  146. <xsl:otherwise>
  147. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'cm'!</xsl:message>
  148. <xsl:value-of select="$value"/>
  149. </xsl:otherwise>
  150. </xsl:choose>
  151. </xsl:template>
  152. <!-- changing measure to inch (cp. section comment) -->
  153. <xsl:template name="convert2in">
  154. <xsl:param name="value"/>
  155. <xsl:param name="rounding-factor" select="10000"/>
  156. <xsl:choose>
  157. <xsl:when test="contains($value, 'mm')">
  158. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $inch-in-mm)) div $rounding-factor"/>
  159. </xsl:when>
  160. <xsl:when test="contains($value, 'cm')">
  161. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $inch-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  162. </xsl:when>
  163. <xsl:when test="contains($value, 'in')">
  164. <xsl:value-of select="substring-before($value, 'in')"/>
  165. </xsl:when>
  166. <xsl:when test="contains($value, 'pt')">
  167. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $inch-in-mm * $point-in-mm)) div $rounding-factor"/>
  168. </xsl:when>
  169. <xsl:when test="contains($value, 'dpt')">
  170. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $inch-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  171. </xsl:when>
  172. <xsl:when test="contains($value, 'pica')">
  173. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $inch-in-mm * $pica-in-mm)) div $rounding-factor"/>
  174. </xsl:when>
  175. <xsl:when test="contains($value, 'twip')">
  176. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $inch-in-mm * $twip-in-mm)) div $rounding-factor"/>
  177. </xsl:when>
  178. <xsl:when test="contains($value, 'px')">
  179. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $inch-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  180. </xsl:when>
  181. <xsl:otherwise>
  182. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'in'!</xsl:message>
  183. <xsl:value-of select="$value"/>
  184. </xsl:otherwise>
  185. </xsl:choose>
  186. </xsl:template>
  187. <!-- changing measure to dpt (cp. section comment) -->
  188. <xsl:template name="convert2dpt">
  189. <xsl:param name="value"/>
  190. <xsl:param name="rounding-factor" select="10000"/>
  191. <xsl:choose>
  192. <xsl:when test="contains($value, 'mm')">
  193. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $didot-point-in-mm)) div $rounding-factor"/>
  194. </xsl:when>
  195. <xsl:when test="contains($value, 'cm')">
  196. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $didot-point-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  197. </xsl:when>
  198. <xsl:when test="contains($value, 'in')">
  199. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $didot-point-in-mm * $inch-in-mm)) div $rounding-factor"/>
  200. </xsl:when>
  201. <xsl:when test="contains($value, 'pt')">
  202. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $didot-point-in-mm * $point-in-mm)) div $rounding-factor"/>
  203. </xsl:when>
  204. <xsl:when test="contains($value, 'dpt')">
  205. <xsl:value-of select="substring-before($value, 'dpt')"/>
  206. </xsl:when>
  207. <xsl:when test="contains($value, 'pica')">
  208. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $didot-point-in-mm * $pica-in-mm)) div $rounding-factor"/>
  209. </xsl:when>
  210. <xsl:when test="contains($value, 'twip')">
  211. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $didot-point-in-mm * $twip-in-mm)) div $rounding-factor"/>
  212. </xsl:when>
  213. <xsl:when test="contains($value, 'px')">
  214. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $didot-point-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  215. </xsl:when>
  216. <xsl:otherwise>
  217. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'dpt'!</xsl:message>
  218. <xsl:value-of select="$value"/>
  219. </xsl:otherwise>
  220. </xsl:choose>
  221. </xsl:template>
  222. <!-- changing measure to pica (cp. section comment) -->
  223. <xsl:template name="convert2pica">
  224. <xsl:param name="value"/>
  225. <xsl:param name="rounding-factor" select="10000"/>
  226. <xsl:choose>
  227. <xsl:when test="contains($value, 'mm')">
  228. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $pica-in-mm)) div $rounding-factor"/>
  229. </xsl:when>
  230. <xsl:when test="contains($value, 'cm')">
  231. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $pica-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  232. </xsl:when>
  233. <xsl:when test="contains($value, 'in')">
  234. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $pica-in-mm * $inch-in-mm)) div $rounding-factor"/>
  235. </xsl:when>
  236. <xsl:when test="contains($value, 'pt')">
  237. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $pica-in-mm * $point-in-mm)) div $rounding-factor"/>
  238. </xsl:when>
  239. <xsl:when test="contains($value, 'dpt')">
  240. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $pica-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  241. </xsl:when>
  242. <xsl:when test="contains($value, 'pica')">
  243. <xsl:value-of select="substring-before($value, 'pica')"/>
  244. </xsl:when>
  245. <xsl:when test="contains($value, 'twip')">
  246. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $pica-in-mm * $twip-in-mm)) div $rounding-factor"/>
  247. </xsl:when>
  248. <xsl:when test="contains($value, 'px')">
  249. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $pica-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  250. </xsl:when>
  251. <xsl:otherwise>
  252. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'pica'!</xsl:message>
  253. <xsl:value-of select="$value"/>
  254. </xsl:otherwise>
  255. </xsl:choose>
  256. </xsl:template>
  257. <!-- changing measure to pt (cp. section comment) -->
  258. <xsl:template name="convert2pt">
  259. <xsl:param name="value"/>
  260. <xsl:param name="rounding-factor" select="10000"/>
  261. <xsl:choose>
  262. <xsl:when test="contains($value, 'mm')">
  263. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $point-in-mm)) div $rounding-factor"/>
  264. </xsl:when>
  265. <xsl:when test="contains($value, 'cm')">
  266. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $point-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  267. </xsl:when>
  268. <xsl:when test="contains($value, 'in')">
  269. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $point-in-mm * $inch-in-mm)) div $rounding-factor"/>
  270. </xsl:when>
  271. <xsl:when test="contains($value, 'pt')">
  272. <xsl:value-of select="substring-before($value, 'pt')"/>
  273. </xsl:when>
  274. <xsl:when test="contains($value, 'dpt')">
  275. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $point-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  276. </xsl:when>
  277. <xsl:when test="contains($value, 'pica')">
  278. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $point-in-mm * $pica-in-mm)) div $rounding-factor"/>
  279. </xsl:when>
  280. <xsl:when test="contains($value, 'twip')">
  281. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'twip') div $point-in-mm * $twip-in-mm)) div $rounding-factor"/>
  282. </xsl:when>
  283. <xsl:when test="contains($value, 'px')">
  284. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $point-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  285. </xsl:when>
  286. <xsl:otherwise>
  287. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'pt'!</xsl:message>
  288. <xsl:value-of select="$value"/>
  289. </xsl:otherwise>
  290. </xsl:choose>
  291. </xsl:template>
  292. <!-- changing measure to twip (cp. section comment) -->
  293. <xsl:template name="convert2twip">
  294. <xsl:param name="value"/>
  295. <xsl:param name="rounding-factor" select="10000"/>
  296. <xsl:choose>
  297. <xsl:when test="contains($value, 'mm')">
  298. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'mm') div $twip-in-mm)) div $rounding-factor"/>
  299. </xsl:when>
  300. <xsl:when test="contains($value, 'cm')">
  301. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'cm') div $twip-in-mm * $centimeter-in-mm)) div $rounding-factor"/>
  302. </xsl:when>
  303. <xsl:when test="contains($value, 'in')">
  304. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'in') div $twip-in-mm * $inch-in-mm)) div $rounding-factor"/>
  305. </xsl:when>
  306. <xsl:when test="contains($value, 'pt')">
  307. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pt') div $twip-in-mm * $point-in-mm)) div $rounding-factor"/>
  308. </xsl:when>
  309. <xsl:when test="contains($value, 'dpt')">
  310. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'dpt') div $twip-in-mm * $didot-point-in-mm)) div $rounding-factor"/>
  311. </xsl:when>
  312. <xsl:when test="contains($value, 'pica')">
  313. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'pica') div $twip-in-mm * $pica-in-mm)) div $rounding-factor"/>
  314. </xsl:when>
  315. <xsl:when test="contains($value, 'twip')">
  316. <xsl:value-of select="substring-before($value, 'twip')"/>
  317. </xsl:when>
  318. <xsl:when test="contains($value, 'px')">
  319. <xsl:value-of select="round($rounding-factor * number(substring-before($value, 'px') div $twip-in-mm * $pixel-in-mm)) div $rounding-factor"/>
  320. </xsl:when>
  321. <xsl:otherwise>
  322. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'twip'!</xsl:message>
  323. <xsl:value-of select="$value"/>
  324. </xsl:otherwise>
  325. </xsl:choose>
  326. </xsl:template>
  327. <!-- changing measure to pixel by via parameter provided dpi (dots per inch) standard factor (cp. section comment) -->
  328. <xsl:template name="convert2px">
  329. <xsl:param name="value"/>
  330. <xsl:choose>
  331. <xsl:when test="contains($value, 'mm')">
  332. <xsl:value-of select="round(number(substring-before($value, 'mm')) div $pixel-in-mm)"/>
  333. </xsl:when>
  334. <xsl:when test="contains($value, 'cm')">
  335. <xsl:value-of select="round(number(substring-before($value, 'cm')) div $pixel-in-mm * $centimeter-in-mm)"/>
  336. </xsl:when>
  337. <xsl:when test="contains($value, 'in')">
  338. <xsl:value-of select="round(number(substring-before($value, 'in')) div $pixel-in-mm * $inch-in-mm)"/>
  339. </xsl:when>
  340. <xsl:when test="contains($value, 'pt')">
  341. <xsl:value-of select="round(number(substring-before($value, 'pt')) div $pixel-in-mm * $point-in-mm)"/>
  342. </xsl:when>
  343. <xsl:when test="contains($value, 'dpt')">
  344. <xsl:value-of select="round(number(substring-before($value, 'dpt')) div $pixel-in-mm * $didot-point-in-mm)"/>
  345. </xsl:when>
  346. <xsl:when test="contains($value, 'pica')">
  347. <xsl:value-of select="round(number(substring-before($value, 'pica')) div $pixel-in-mm * $pica-in-mm)"/>
  348. </xsl:when>
  349. <xsl:when test="contains($value, 'twip')">
  350. <xsl:value-of select="round(number(substring-before($value, 'twip')) div $pixel-in-mm * $twip-in-mm)"/>
  351. </xsl:when>
  352. <xsl:when test="contains($value, 'px')">
  353. <xsl:value-of select="$value"/>
  354. </xsl:when>
  355. <xsl:otherwise>
  356. <xsl:message>measure_conversion.xsl: Find no conversion for <xsl:value-of select="$value"/> to 'px'!</xsl:message>
  357. <xsl:value-of select="$value"/>
  358. </xsl:otherwise>
  359. </xsl:choose>
  360. </xsl:template>
  361. <xsl:template name="ConvertMeasure">
  362. <xsl:param name="TargetMeasure" select="'cm'"/>
  363. <xsl:param name="TargetTruncate" select=" 'all' "/>
  364. <xsl:param name="value"/>
  365. <!-- When TargetTruncate ='all' it returns the number whichsoever the return value is negative or positive
  366. When TargetTruncate ='nonNegative' it only returns nonNegative number, all negative number to be returned as 0
  367. When TargetTruncate ='positive" it only returns positive number, all nonPositive number to be returned as 1 -->
  368. <xsl:variable name="return_value">
  369. <xsl:choose>
  370. <!-- remove the measure mark, if the value is null, the result should be 0. Must be the first case -->
  371. <xsl:when test="string-length(translate(string($value),'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ','')) = 0">0</xsl:when>
  372. <xsl:when test="string-length(translate(string($value),'- .0123456789','')) = 0">
  373. <xsl:value-of select="$value"/>
  374. </xsl:when>
  375. <xsl:when test="$TargetMeasure = 'cm'">
  376. <xsl:call-template name="convert2cm">
  377. <xsl:with-param name="value" select="$value"/>
  378. </xsl:call-template>
  379. </xsl:when>
  380. <xsl:when test="$TargetMeasure = 'pt'">
  381. <xsl:call-template name="convert2pt">
  382. <xsl:with-param name="value" select="$value"/>
  383. </xsl:call-template>
  384. </xsl:when>
  385. <xsl:when test="$TargetMeasure = 'twip'">
  386. <xsl:call-template name="convert2twip">
  387. <xsl:with-param name="value" select="$value"/>
  388. </xsl:call-template>
  389. </xsl:when>
  390. <xsl:when test="$TargetMeasure = 'in'">
  391. <xsl:call-template name="convert2in">
  392. <xsl:with-param name="value" select="$value"/>
  393. </xsl:call-template>
  394. </xsl:when>
  395. </xsl:choose>
  396. </xsl:variable>
  397. <xsl:choose>
  398. <xsl:when test="$TargetTruncate = 'all' ">
  399. <xsl:choose>
  400. <xsl:when test="string(number($TargetMeasure)) = 'NaN' ">
  401. <xsl:value-of select=" '0' "/>
  402. </xsl:when>
  403. <xsl:otherwise>
  404. <xsl:value-of select="$return_value"/>
  405. </xsl:otherwise>
  406. </xsl:choose>
  407. </xsl:when>
  408. <xsl:when test="$TargetTruncate = 'nonNegative' ">
  409. <xsl:choose>
  410. <xsl:when test="string(number($TargetMeasure)) = 'NaN' ">
  411. <xsl:value-of select=" '0' "/>
  412. </xsl:when>
  413. <xsl:otherwise>
  414. <xsl:choose>
  415. <xsl:when test=" $return_value &lt; 0 ">
  416. <xsl:value-of select=" '0' "/>
  417. </xsl:when>
  418. <xsl:otherwise>
  419. <xsl:value-of select="$return_value"/>
  420. </xsl:otherwise>
  421. </xsl:choose>
  422. </xsl:otherwise>
  423. </xsl:choose>
  424. </xsl:when>
  425. <xsl:when test="$TargetTruncate = 'positive' ">
  426. <xsl:choose>
  427. <xsl:when test="string(number($TargetMeasure)) = 'NaN' ">
  428. <xsl:value-of select=" '1' "/>
  429. </xsl:when>
  430. <xsl:otherwise>
  431. <xsl:choose>
  432. <xsl:when test=" $return_value &lt;= 0 ">
  433. <xsl:value-of select=" '1' "/>
  434. </xsl:when>
  435. <xsl:otherwise>
  436. <xsl:value-of select="$return_value"/>
  437. </xsl:otherwise>
  438. </xsl:choose>
  439. </xsl:otherwise>
  440. </xsl:choose>
  441. </xsl:when>
  442. </xsl:choose>
  443. </xsl:template>
  444. <xsl:template name="Add-With-Measure">
  445. <xsl:param name="value1"/>
  446. <xsl:param name="value2"/>
  447. <xsl:param name="TargetMeasure" select="'in'"/>
  448. <xsl:variable name="number-value1">
  449. <xsl:call-template name="ConvertMeasure">
  450. <xsl:with-param name="value" select="$value1"/>
  451. <xsl:with-param name="TargetMeasure" select="$TargetMeasure"/>
  452. </xsl:call-template>
  453. </xsl:variable>
  454. <xsl:variable name="number-value2">
  455. <xsl:call-template name="ConvertMeasure">
  456. <xsl:with-param name="value" select="$value2"/>
  457. <xsl:with-param name="TargetMeasure" select="$TargetMeasure"/>
  458. </xsl:call-template>
  459. </xsl:variable>
  460. <xsl:value-of select="$number-value1 + $number-value2"/>
  461. </xsl:template>
  462. </xsl:stylesheet>