1
0

spaces.xsl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?xml version='1.0' encoding="utf-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:fo="http://www.w3.org/1999/XSL/Format"
  4. version='1.0'>
  5. <!-- ********************************************************************
  6. $Id: spaces.xsl 9647 2012-10-26 17:42:03Z bobstayton $
  7. ********************************************************************
  8. XSL-FO specification treats all space characters like ordinary spaces.
  9. We need to map them to fo:leader with different widths in order to
  10. simulate desired behaviour.
  11. ******************************************************************** -->
  12. <xsl:template match="text()[namespace-uri(..) = '' or
  13. namespace-uri(..) = 'http://docbook.org/ns/docbook']">
  14. <xsl:call-template name="space.2000.subst">
  15. <xsl:with-param name="string" select="."/>
  16. </xsl:call-template>
  17. </xsl:template>
  18. <xsl:param name="space.enquad.width">0.5em</xsl:param> <!-- U+2000 -->
  19. <xsl:param name="space.emquad.width">1em</xsl:param> <!-- U+2001 -->
  20. <xsl:param name="space.enspace.width">0.5em</xsl:param> <!-- U+2002 -->
  21. <xsl:param name="space.emspace.width">1em</xsl:param> <!-- U+2003 -->
  22. <xsl:param name="space.3emspace.width">0.33em</xsl:param><!-- U+2004 -->
  23. <xsl:param name="space.4emspace.width">0.25em</xsl:param><!-- U+2005 -->
  24. <xsl:param name="space.6emspace.width">0.16em</xsl:param><!-- U+2006 -->
  25. <xsl:param name="space.figspace.width"></xsl:param> <!-- U+2007 -->
  26. <xsl:param name="space.punctspace.width"></xsl:param> <!-- U+2008 -->
  27. <xsl:param name="space.thinspace.width">0.2em</xsl:param><!-- U+2009 -->
  28. <xsl:param name="space.hairspace.width">0.1em</xsl:param><!-- U+200A -->
  29. <xsl:template name="space.2000.subst">
  30. <xsl:param name="string"/>
  31. <xsl:choose>
  32. <xsl:when test="contains($string, '&#x2000;') and $space.enquad.width != ''">
  33. <xsl:call-template name="space.2001.subst">
  34. <xsl:with-param name="string" select="substring-before($string, '&#x2000;')"/>
  35. </xsl:call-template>
  36. <fo:leader leader-length="{$space.enquad.width}"/>
  37. <xsl:call-template name="space.2000.subst">
  38. <xsl:with-param name="string" select="substring-after($string, '&#x2000;')"/>
  39. </xsl:call-template>
  40. </xsl:when>
  41. <xsl:otherwise>
  42. <xsl:call-template name="space.2001.subst">
  43. <xsl:with-param name="string" select="$string"/>
  44. </xsl:call-template>
  45. </xsl:otherwise>
  46. </xsl:choose>
  47. </xsl:template>
  48. <xsl:template name="space.2001.subst">
  49. <xsl:param name="string"/>
  50. <xsl:choose>
  51. <xsl:when test="contains($string, '&#x2001;') and $space.emquad.width != ''">
  52. <xsl:call-template name="space.2002.subst">
  53. <xsl:with-param name="string" select="substring-before($string, '&#x2001;')"/>
  54. </xsl:call-template>
  55. <fo:leader leader-length="{$space.emquad.width}"/>
  56. <xsl:call-template name="space.2001.subst">
  57. <xsl:with-param name="string" select="substring-after($string, '&#x2001;')"/>
  58. </xsl:call-template>
  59. </xsl:when>
  60. <xsl:otherwise>
  61. <xsl:call-template name="space.2002.subst">
  62. <xsl:with-param name="string" select="$string"/>
  63. </xsl:call-template>
  64. </xsl:otherwise>
  65. </xsl:choose>
  66. </xsl:template>
  67. <xsl:template name="space.2002.subst">
  68. <xsl:param name="string"/>
  69. <xsl:choose>
  70. <xsl:when test="contains($string, '&#x2002;') and $space.enspace.width != ''">
  71. <xsl:call-template name="space.2003.subst">
  72. <xsl:with-param name="string" select="substring-before($string, '&#x2002;')"/>
  73. </xsl:call-template>
  74. <fo:leader leader-length="{$space.enspace.width}"/>
  75. <xsl:call-template name="space.2002.subst">
  76. <xsl:with-param name="string" select="substring-after($string, '&#x2002;')"/>
  77. </xsl:call-template>
  78. </xsl:when>
  79. <xsl:otherwise>
  80. <xsl:call-template name="space.2003.subst">
  81. <xsl:with-param name="string" select="$string"/>
  82. </xsl:call-template>
  83. </xsl:otherwise>
  84. </xsl:choose>
  85. </xsl:template>
  86. <xsl:template name="space.2003.subst">
  87. <xsl:param name="string"/>
  88. <xsl:choose>
  89. <xsl:when test="contains($string, '&#x2003;') and $space.emspace.width != ''">
  90. <xsl:call-template name="space.2004.subst">
  91. <xsl:with-param name="string" select="substring-before($string, '&#x2003;')"/>
  92. </xsl:call-template>
  93. <fo:leader leader-length="{$space.emspace.width}"/>
  94. <xsl:call-template name="space.2003.subst">
  95. <xsl:with-param name="string" select="substring-after($string, '&#x2003;')"/>
  96. </xsl:call-template>
  97. </xsl:when>
  98. <xsl:otherwise>
  99. <xsl:call-template name="space.2004.subst">
  100. <xsl:with-param name="string" select="$string"/>
  101. </xsl:call-template>
  102. </xsl:otherwise>
  103. </xsl:choose>
  104. </xsl:template>
  105. <xsl:template name="space.2004.subst">
  106. <xsl:param name="string"/>
  107. <xsl:choose>
  108. <xsl:when test="contains($string, '&#x2004;') and $space.3emspace.width != ''">
  109. <xsl:call-template name="space.2005.subst">
  110. <xsl:with-param name="string" select="substring-before($string, '&#x2004;')"/>
  111. </xsl:call-template>
  112. <fo:leader leader-length="{$space.3emspace.width}"/>
  113. <xsl:call-template name="space.2004.subst">
  114. <xsl:with-param name="string" select="substring-after($string, '&#x2004;')"/>
  115. </xsl:call-template>
  116. </xsl:when>
  117. <xsl:otherwise>
  118. <xsl:call-template name="space.2005.subst">
  119. <xsl:with-param name="string" select="$string"/>
  120. </xsl:call-template>
  121. </xsl:otherwise>
  122. </xsl:choose>
  123. </xsl:template>
  124. <xsl:template name="space.2005.subst">
  125. <xsl:param name="string"/>
  126. <xsl:choose>
  127. <xsl:when test="contains($string, '&#x2005;') and $space.4emspace.width != ''">
  128. <xsl:call-template name="space.2006.subst">
  129. <xsl:with-param name="string" select="substring-before($string, '&#x2005;')"/>
  130. </xsl:call-template>
  131. <fo:leader leader-length="{$space.4emspace.width}"/>
  132. <xsl:call-template name="space.2005.subst">
  133. <xsl:with-param name="string" select="substring-after($string, '&#x2005;')"/>
  134. </xsl:call-template>
  135. </xsl:when>
  136. <xsl:otherwise>
  137. <xsl:call-template name="space.2006.subst">
  138. <xsl:with-param name="string" select="$string"/>
  139. </xsl:call-template>
  140. </xsl:otherwise>
  141. </xsl:choose>
  142. </xsl:template>
  143. <xsl:template name="space.2006.subst">
  144. <xsl:param name="string"/>
  145. <xsl:choose>
  146. <xsl:when test="contains($string, '&#x2006;') and $space.6emspace.width != ''">
  147. <xsl:call-template name="space.2007.subst">
  148. <xsl:with-param name="string" select="substring-before($string, '&#x2006;')"/>
  149. </xsl:call-template>
  150. <fo:leader leader-length="{$space.6emspace.width}"/>
  151. <xsl:call-template name="space.2006.subst">
  152. <xsl:with-param name="string" select="substring-after($string, '&#x2006;')"/>
  153. </xsl:call-template>
  154. </xsl:when>
  155. <xsl:otherwise>
  156. <xsl:call-template name="space.2007.subst">
  157. <xsl:with-param name="string" select="$string"/>
  158. </xsl:call-template>
  159. </xsl:otherwise>
  160. </xsl:choose>
  161. </xsl:template>
  162. <xsl:template name="space.2007.subst">
  163. <xsl:param name="string"/>
  164. <xsl:choose>
  165. <xsl:when test="contains($string, '&#x2007;') and $space.figspace.width != ''">
  166. <xsl:call-template name="space.2008.subst">
  167. <xsl:with-param name="string" select="substring-before($string, '&#x2007;')"/>
  168. </xsl:call-template>
  169. <fo:leader leader-length="{$space.figspace.width}"/>
  170. <xsl:call-template name="space.2007.subst">
  171. <xsl:with-param name="string" select="substring-after($string, '&#x2007;')"/>
  172. </xsl:call-template>
  173. </xsl:when>
  174. <xsl:otherwise>
  175. <xsl:call-template name="space.2008.subst">
  176. <xsl:with-param name="string" select="$string"/>
  177. </xsl:call-template>
  178. </xsl:otherwise>
  179. </xsl:choose>
  180. </xsl:template>
  181. <xsl:template name="space.2008.subst">
  182. <xsl:param name="string"/>
  183. <xsl:choose>
  184. <xsl:when test="contains($string, '&#x2008;') and $space.punctspace.width != ''">
  185. <xsl:call-template name="space.2009.subst">
  186. <xsl:with-param name="string" select="substring-before($string, '&#x2008;')"/>
  187. </xsl:call-template>
  188. <fo:leader leader-length="{$space.punctspace.width}"/>
  189. <xsl:call-template name="space.2008.subst">
  190. <xsl:with-param name="string" select="substring-after($string, '&#x2008;')"/>
  191. </xsl:call-template>
  192. </xsl:when>
  193. <xsl:otherwise>
  194. <xsl:call-template name="space.2009.subst">
  195. <xsl:with-param name="string" select="$string"/>
  196. </xsl:call-template>
  197. </xsl:otherwise>
  198. </xsl:choose>
  199. </xsl:template>
  200. <xsl:template name="space.2009.subst">
  201. <xsl:param name="string"/>
  202. <xsl:choose>
  203. <xsl:when test="contains($string, '&#x2009;') and $space.thinspace.width != ''">
  204. <xsl:call-template name="space.200A.subst">
  205. <xsl:with-param name="string" select="substring-before($string, '&#x2009;')"/>
  206. </xsl:call-template>
  207. <fo:leader leader-length="{$space.thinspace.width}"/>
  208. <xsl:call-template name="space.2009.subst">
  209. <xsl:with-param name="string" select="substring-after($string, '&#x2009;')"/>
  210. </xsl:call-template>
  211. </xsl:when>
  212. <xsl:otherwise>
  213. <xsl:call-template name="space.200A.subst">
  214. <xsl:with-param name="string" select="$string"/>
  215. </xsl:call-template>
  216. </xsl:otherwise>
  217. </xsl:choose>
  218. </xsl:template>
  219. <xsl:template name="space.200A.subst">
  220. <xsl:param name="string"/>
  221. <xsl:choose>
  222. <xsl:when test="contains($string, '&#x200A;') and $space.hairspace.width != ''">
  223. <xsl:value-of select="substring-before($string, '&#x200A;')"/>
  224. <fo:leader leader-length="{$space.hairspace.width}"/>
  225. <xsl:call-template name="space.200A.subst">
  226. <xsl:with-param name="string" select="substring-after($string, '&#x200A;')"/>
  227. </xsl:call-template>
  228. </xsl:when>
  229. <xsl:otherwise>
  230. <xsl:value-of select="$string"/>
  231. </xsl:otherwise>
  232. </xsl:choose>
  233. </xsl:template>
  234. </xsl:stylesheet>