dita2odt-img.xsl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- This file is part of the DITA Open Toolkit project.
  3. See the accompanying license.txt file for applicable licenses. -->
  4. <!-- (c) Copyright IBM Corp. 2005, 2006 All Rights Reserved. -->
  5. <!--
  6. <draw:frame text:anchor-type="as-char"
  7. svg:width="6in" svg:height="3.2736in" draw:z-index="0">
  8. <draw:image xlink:href="Pictures/100000000000027A0000015A35A01F85.jpg"
  9. xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/>
  10. </draw:frame>
  11. -->
  12. <xsl:stylesheet
  13. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  14. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  15. xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
  16. xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
  17. xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
  18. xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
  19. xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
  20. xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
  21. xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/"
  22. xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
  23. xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
  24. xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
  25. xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
  26. xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
  27. xmlns:math="http://www.w3.org/1998/Math/MathML"
  28. xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
  29. xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
  30. xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms"
  31. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  32. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  33. xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0"
  34. xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
  35. xmlns:prodtools="http://www.ibm.com/xmlns/prodtools"
  36. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  37. version="2.0"
  38. exclude-result-prefixes="xs dita-ot">
  39. <xsl:output method="xml"/>
  40. <xsl:output indent="yes"/>
  41. <xsl:strip-space elements="*"/>
  42. <xsl:function name="dita-ot:to-inch" as="xs:double">
  43. <xsl:param name="length" as="xs:string?"/>
  44. <xsl:choose>
  45. <xsl:when test="contains($length, 'in')">
  46. <xsl:sequence select="number(substring-before($length, 'in'))"/>
  47. </xsl:when>
  48. <xsl:when test="contains($length, 'cm')">
  49. <xsl:sequence select="number(substring-before($length, 'cm')) div 2.54"/>
  50. </xsl:when>
  51. <xsl:when test="contains($length, 'mm')">
  52. <xsl:sequence select="number(substring-before($length, 'mm')) div 25.4"/>
  53. </xsl:when>
  54. <xsl:when test="contains($length, 'pt')">
  55. <xsl:sequence select="number(substring-before($length, 'pt')) div 72"/>
  56. </xsl:when>
  57. <xsl:when test="contains($length, 'pc')">
  58. <xsl:sequence select="number(substring-before($length, 'pc')) div 6"/>
  59. </xsl:when>
  60. <xsl:when test="contains($length, 'px')">
  61. <xsl:sequence select="number(substring-before($length, 'px')) div 96"/>
  62. </xsl:when>
  63. <xsl:when test="contains($length, 'em')">
  64. <xsl:sequence select="number(substring-before($length, 'em')) div 6"/>
  65. </xsl:when>
  66. <!-- default is px -->
  67. <xsl:otherwise>
  68. <xsl:sequence select="number($length) div 96"/>
  69. </xsl:otherwise>
  70. </xsl:choose>
  71. </xsl:function>
  72. <xsl:template match="*[contains(@class,' topic/image ')]">
  73. <xsl:if test="@href and not(@href='')">
  74. <!-- image meta data -->
  75. <xsl:variable name="scale" as="xs:double">
  76. <xsl:choose>
  77. <xsl:when test="@scale">
  78. <xsl:sequence select="@scale div 100"/>
  79. </xsl:when>
  80. <xsl:otherwise>
  81. <xsl:sequence select="1"/>
  82. </xsl:otherwise>
  83. </xsl:choose>
  84. </xsl:variable>
  85. <xsl:variable name="height">
  86. <xsl:choose>
  87. <xsl:when test="@height">
  88. <xsl:value-of select="dita-ot:to-inch(@height) * $scale"/>
  89. </xsl:when>
  90. <xsl:when test="@dita-ot:image-height">
  91. <xsl:value-of select="dita-ot:to-inch(@dita-ot:image-height) * $scale"/>
  92. </xsl:when>
  93. <xsl:otherwise/>
  94. </xsl:choose>
  95. </xsl:variable>
  96. <xsl:variable name="width">
  97. <xsl:choose>
  98. <xsl:when test="@width">
  99. <xsl:value-of select="dita-ot:to-inch(@width) * $scale"/>
  100. </xsl:when>
  101. <xsl:when test="@dita-ot:image-width">
  102. <xsl:value-of select="dita-ot:to-inch(@dita-ot:image-width) * $scale"/>
  103. </xsl:when>
  104. <xsl:otherwise/>
  105. </xsl:choose>
  106. </xsl:variable>
  107. <xsl:choose>
  108. <!-- nested by body or list -->
  109. <xsl:when test="parent::*[contains(@class, ' topic/body ')] or parent::*[contains(@class, ' topic/li ')] or parent::*[contains(@class, ' topic/sli ')]">
  110. <text:p>
  111. <text:span>
  112. <xsl:choose>
  113. <!-- FIXME: this will never match due to parent xsl:when -->
  114. <xsl:when test="parent::*[contains(@class, ' topic/fig ')][contains(@frame,'top ')]">
  115. <!-- NOP if there is already a break implied by a parent property -->
  116. </xsl:when>
  117. <xsl:otherwise>
  118. <xsl:choose>
  119. <xsl:when test="(@placement='break')">
  120. <!-- start add flagging styles -->
  121. <xsl:apply-templates select="." mode="start-add-odt-flags"/>
  122. </xsl:when>
  123. <xsl:otherwise>
  124. <xsl:call-template name="flagcheck"/>
  125. </xsl:otherwise>
  126. </xsl:choose>
  127. </xsl:otherwise>
  128. </xsl:choose>
  129. <xsl:call-template name="draw_image">
  130. <xsl:with-param name="height" select="$height"/>
  131. <xsl:with-param name="width" select="$width"/>
  132. </xsl:call-template>
  133. <!-- end add flagging styles -->
  134. <xsl:apply-templates select="." mode="end-add-odt-flags"/>
  135. </text:span>
  136. </text:p>
  137. </xsl:when>
  138. <!-- nested by entry -->
  139. <xsl:when test="parent::*[contains(@class, ' topic/entry ')]">
  140. <!-- create p tag -->
  141. <text:p>
  142. <text:span>
  143. <xsl:choose>
  144. <!-- FIXME: this will never match due to parent xsl:when -->
  145. <xsl:when test="parent::*[contains(@class, ' topic/fig ')][contains(@frame,'top ')]">
  146. <!-- NOP if there is already a break implied by a parent property -->
  147. </xsl:when>
  148. <xsl:otherwise>
  149. <xsl:choose>
  150. <xsl:when test="(@placement='break')">
  151. <!-- start add flagging styles -->
  152. <xsl:apply-templates select="." mode="start-add-odt-flags"/>
  153. </xsl:when>
  154. <xsl:otherwise>
  155. <xsl:call-template name="flagcheck"/>
  156. </xsl:otherwise>
  157. </xsl:choose>
  158. </xsl:otherwise>
  159. </xsl:choose>
  160. <xsl:call-template name="draw_image">
  161. <xsl:with-param name="height" select="$height"/>
  162. <xsl:with-param name="width" select="$width"/>
  163. </xsl:call-template>
  164. <!-- end add flagging styles -->
  165. <xsl:apply-templates select="." mode="end-add-odt-flags"/>
  166. </text:span>
  167. </text:p>
  168. </xsl:when>
  169. <!-- nested by stentry -->
  170. <xsl:when test="parent::*[contains(@class, ' topic/stentry ')]">
  171. <text:p>
  172. <text:span>
  173. <xsl:choose>
  174. <!-- FIXME: this will never match due to parent xsl:when -->
  175. <xsl:when test="parent::*[contains(@class, ' topic/fig ')][contains(@frame,'top ')]">
  176. <!-- NOP if there is already a break implied by a parent property -->
  177. </xsl:when>
  178. <xsl:otherwise>
  179. <xsl:choose>
  180. <xsl:when test="(@placement='break')">
  181. <!-- start add flagging styles -->
  182. <xsl:apply-templates select="." mode="start-add-odt-flags"/>
  183. </xsl:when>
  184. <xsl:otherwise>
  185. <xsl:call-template name="flagcheck"/>
  186. </xsl:otherwise>
  187. </xsl:choose>
  188. </xsl:otherwise>
  189. </xsl:choose>
  190. <xsl:call-template name="draw_image">
  191. <xsl:with-param name="height" select="$height"/>
  192. <xsl:with-param name="width" select="$width"/>
  193. </xsl:call-template>
  194. <!-- end add flagging styles -->
  195. <xsl:apply-templates select="." mode="end-add-odt-flags"/>
  196. </text:span>
  197. </text:p>
  198. </xsl:when>
  199. <!-- nested by other tags -->
  200. <xsl:otherwise>
  201. <text:span>
  202. <xsl:choose>
  203. <xsl:when test="parent::*[contains(@class, ' topic/fig ')][contains(@frame,'top ')]">
  204. <!-- NOP if there is already a break implied by a parent property -->
  205. </xsl:when>
  206. <xsl:otherwise>
  207. <xsl:choose>
  208. <xsl:when test="(@placement='break')">
  209. <!-- start add flagging styles -->
  210. <xsl:apply-templates select="." mode="start-add-odt-flags"/>
  211. </xsl:when>
  212. <xsl:otherwise>
  213. <xsl:call-template name="flagcheck"/>
  214. </xsl:otherwise>
  215. </xsl:choose>
  216. </xsl:otherwise>
  217. </xsl:choose>
  218. <xsl:call-template name="draw_image">
  219. <xsl:with-param name="height" select="$height"/>
  220. <xsl:with-param name="width" select="$width"/>
  221. </xsl:call-template>
  222. <!-- end add flagging styles -->
  223. <xsl:apply-templates select="." mode="end-add-odt-flags"/>
  224. </text:span>
  225. </xsl:otherwise>
  226. </xsl:choose>
  227. </xsl:if>
  228. </xsl:template>
  229. <xsl:template name="draw_image">
  230. <xsl:param name="height"/>
  231. <xsl:param name="width"/>
  232. <xsl:choose>
  233. <xsl:when test="not(contains(@href,'://')) and $height and $width">
  234. <draw:frame text:anchor-type="as-char" svg:y="-0.1in">
  235. <xsl:if test="string(number($height)) != 'NaN'">
  236. <xsl:attribute name="svg:width">
  237. <xsl:value-of select="$height"/>
  238. <xsl:text>in</xsl:text>
  239. </xsl:attribute>
  240. </xsl:if>
  241. <xsl:if test="string(number($width)) != 'NaN'">
  242. <xsl:attribute name="svg:width">
  243. <xsl:choose>
  244. <xsl:when test="$width &gt; 6">6</xsl:when>
  245. <xsl:otherwise>
  246. <xsl:value-of select="$width"/>
  247. </xsl:otherwise>
  248. </xsl:choose>
  249. <xsl:text>in</xsl:text>
  250. </xsl:attribute>
  251. </xsl:if>
  252. <draw:image xlink:href="{translate(@href, '\', '/')}">
  253. </draw:image>
  254. </draw:frame>
  255. </xsl:when>
  256. <xsl:otherwise>
  257. <text:a xlink:href="{translate(@href, '\', '/')}" xlink:type="simple">
  258. <xsl:call-template name="gen-img-txt"/>
  259. </text:a>
  260. </xsl:otherwise>
  261. </xsl:choose>
  262. </xsl:template>
  263. <xsl:template name="draw_image_odt">
  264. <xsl:param name="height"/>
  265. <xsl:param name="width"/>
  266. <xsl:param name="imgsrc"/>
  267. <xsl:param name="alttext"/>
  268. <xsl:choose>
  269. <xsl:when test="not(contains($imgsrc,'://')) and $height and $width">
  270. <draw:frame text:anchor-type="as-char" svg:y="-0.1in">
  271. <xsl:if test="string(number($height)) != 'NaN'">
  272. <xsl:attribute name="svg:width">
  273. <xsl:value-of select="$height"/>
  274. <xsl:text>in</xsl:text>
  275. </xsl:attribute>
  276. </xsl:if>
  277. <xsl:if test="string(number($width)) != 'NaN'">
  278. <xsl:attribute name="svg:width">
  279. <xsl:choose>
  280. <xsl:when test="$width &gt; 6">6</xsl:when>
  281. <xsl:otherwise>
  282. <xsl:value-of select="$width"/>
  283. </xsl:otherwise>
  284. </xsl:choose>
  285. <xsl:text>in</xsl:text>
  286. </xsl:attribute>
  287. </xsl:if>
  288. <draw:image xlink:href="{translate($imgsrc, '\', '/')}">
  289. </draw:image>
  290. </draw:frame>
  291. </xsl:when>
  292. <xsl:otherwise>
  293. <text:span>
  294. <!--
  295. <xsl:attribute name="xlink:href">
  296. <xsl:value-of select="translate($imgsrc, '\', '/')"/>
  297. </xsl:attribute>
  298. <xsl:attribute name="xlink:type">simple</xsl:attribute>
  299. -->
  300. <xsl:call-template name="gen-img-txt">
  301. <xsl:with-param name="alttext" select="$alttext"/>
  302. </xsl:call-template>
  303. </text:span>
  304. </xsl:otherwise>
  305. </xsl:choose>
  306. </xsl:template>
  307. <xsl:template name="gen-img-txt">
  308. <xsl:param name="alttext"/>
  309. <xsl:choose>
  310. <xsl:when test="$alttext != ''">
  311. <xsl:call-template name="getVariable">
  312. <xsl:with-param name="id" select="$alttext"/>
  313. </xsl:call-template>
  314. </xsl:when>
  315. <xsl:when test="*[contains(@class,' topic/alt ')]">
  316. <xsl:value-of select="*[contains(@class,' topic/alt ')]"/>
  317. </xsl:when>
  318. <xsl:when test="startflag/alt-text">
  319. <xsl:value-of select="startflag/alt-text"/>
  320. </xsl:when>
  321. <xsl:when test="@alt and not(@alt='')">
  322. <xsl:value-of select="@alt"/>
  323. </xsl:when>
  324. <xsl:when test="text() or *">
  325. <xsl:apply-templates/>
  326. </xsl:when>
  327. <xsl:when test="@href">
  328. <xsl:value-of select="@href"/>
  329. </xsl:when>
  330. <xsl:otherwise/>
  331. </xsl:choose>
  332. </xsl:template>
  333. </xsl:stylesheet>