date_time_fo_stylesheet.xsl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  2. xmlns:fo="http://www.w3.org/1999/XSL/Format"
  3. xmlns:sverb="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.Verbatim"
  4. xmlns:xverb="com.nwalsh.xalan.Verbatim"
  5. xmlns:lxslt="http://xml.apache.org/xslt"
  6. xmlns:exsl="http://exslt.org/common"
  7. exclude-result-prefixes="sverb xverb lxslt exsl"
  8. version='1.0'>
  9. <!-- Copyright (c) 2005 CrystalClear Software, Inc.
  10. Subject to the Boost Software License, Version 1.0.
  11. (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  12. -->
  13. <xsl:import href="../../../tools/boostbook/xsl/fo.xsl" />
  14. <!-- reset the default margin parameters -->
  15. <xsl:param name="page.margin.bottom" select="'0.25in'"/>
  16. <xsl:param name="page.margin.inner">
  17. <xsl:choose>
  18. <xsl:when test="$double.sided != 0">1.25in</xsl:when>
  19. <xsl:otherwise>0.65in</xsl:otherwise>
  20. </xsl:choose>
  21. </xsl:param>
  22. <xsl:param name="page.margin.outer">
  23. <xsl:choose>
  24. <xsl:when test="$double.sided != 0">0.75in</xsl:when>
  25. <xsl:otherwise>0.65in</xsl:otherwise>
  26. </xsl:choose>
  27. </xsl:param>
  28. <!-- with the above margins, we can fit 38 monospace characters per table cell.
  29. If the margins are changed, set min-reduction to 100 and generate a new pdf. Then
  30. count the number of characters that fit in a cell. Not the slickest method but it works.-->
  31. <xsl:variable name="char-per-cell" select="38"/>
  32. <!-- prevent reducing the font size too much. An 80% reduction
  33. gives us a width of 48 characters per cell -->
  34. <xsl:variable name="min-reduction" select="80"/>
  35. <!-- recursive font-size reduction template -->
  36. <!-- the string may span multiple lines.
  37. break it up and check the length of each line.
  38. adjust font size according to longest line -->
  39. <xsl:template name="font-size-reduction">
  40. <xsl:param name="inp" />
  41. <xsl:param name="max">1</xsl:param>
  42. <xsl:variable name="result">
  43. <xsl:choose>
  44. <!-- &#x000A; is unicode line-feed character -->
  45. <xsl:when test="contains($inp, '&#x000A;')">
  46. <xsl:variable name="str" select="substring-before($inp, '&#x000A;')" />
  47. <xsl:variable name="next" select="substring-after($inp, '&#x000A;')" />
  48. <xsl:variable name="n_max">
  49. <xsl:choose>
  50. <xsl:when test="string-length($str) > $char-per-cell and string-length($str) > $max">
  51. <xsl:value-of select="string-length($str)" />
  52. </xsl:when>
  53. <xsl:otherwise>
  54. <xsl:value-of select="$max" />
  55. </xsl:otherwise>
  56. </xsl:choose>
  57. </xsl:variable>
  58. <xsl:choose>
  59. <xsl:when test="contains($next, '&#x000A;')">
  60. <xsl:call-template name="font-size-reduction">
  61. <xsl:with-param name="inp" select="$next" />
  62. <xsl:with-param name="max" select="$n_max" />
  63. </xsl:call-template>
  64. </xsl:when>
  65. <xsl:otherwise>
  66. <xsl:choose>
  67. <xsl:when test="$n_max > $char-per-cell and $n_max > string-length($next)">
  68. <!-- set size with next.len -->
  69. <xsl:value-of select="round($char-per-cell div $n_max * 100)"/>
  70. </xsl:when>
  71. <xsl:when test="string-length($next) > $char-per-cell and string-length($next) > $n_max">
  72. <!-- set size with n_max -->
  73. <xsl:value-of select="round($char-per-cell div string-length($next) * 100)"/>
  74. </xsl:when>
  75. <xsl:otherwise>100</xsl:otherwise>
  76. </xsl:choose>
  77. </xsl:otherwise>
  78. </xsl:choose>
  79. </xsl:when>
  80. <xsl:otherwise>
  81. <xsl:choose>
  82. <xsl:when test="string-length($inp) > $char-per-cell">
  83. <!-- set size with inp.len -->
  84. <xsl:value-of select="round($char-per-cell div string-length($inp) * 100)"/>
  85. </xsl:when>
  86. <xsl:otherwise>100</xsl:otherwise>
  87. </xsl:choose>
  88. </xsl:otherwise>
  89. </xsl:choose>
  90. </xsl:variable>
  91. <!-- return either "result" or "min-reduction" -->
  92. <xsl:choose>
  93. <xsl:when test="$min-reduction > $result">
  94. <xsl:value-of select="$min-reduction" />
  95. </xsl:when>
  96. <xsl:otherwise>
  97. <xsl:value-of select="$result" />
  98. </xsl:otherwise>
  99. </xsl:choose>
  100. </xsl:template>
  101. <!-- this template was copied directly from docbook/boostbook xsl and modified
  102. to calculate and resize monospace font to fit cells based on length of string -->
  103. <xsl:template match="entry/screen">
  104. <xsl:param name="suppress-numbers" select="'0'"/>
  105. <xsl:variable name="id"><xsl:call-template name="object.id"/></xsl:variable>
  106. <xsl:variable name="content">
  107. <xsl:choose>
  108. <xsl:when test="$suppress-numbers = '0'
  109. and @linenumbering = 'numbered'
  110. and $use.extensions != '0'
  111. and $linenumbering.extension != '0'">
  112. <xsl:call-template name="number.rtf.lines">
  113. <xsl:with-param name="rtf">
  114. <xsl:apply-templates/>
  115. </xsl:with-param>
  116. </xsl:call-template>
  117. </xsl:when>
  118. <xsl:otherwise>
  119. <xsl:apply-templates/>
  120. </xsl:otherwise>
  121. </xsl:choose>
  122. </xsl:variable>
  123. <xsl:variable name="font_size">
  124. <xsl:call-template name="font-size-reduction">
  125. <xsl:with-param name="inp" select="." />
  126. </xsl:call-template>
  127. </xsl:variable>
  128. <!-- write out the tag now that font size has been calculated -->
  129. <xsl:choose>
  130. <xsl:when test="$shade.verbatim != 0">
  131. <fo:block id="{$id}"
  132. white-space-collapse='false'
  133. white-space-treatment='preserve'
  134. linefeed-treatment='preserve'
  135. xsl:use-attribute-sets="monospace.verbatim.properties shade.verbatim.style" font-size="{$font_size}%">
  136. <xsl:choose>
  137. <xsl:when test="$hyphenate.verbatim != 0 and function-available('exsl:node-set')">
  138. <xsl:apply-templates select="exsl:node-set($content)" mode="hyphenate.verbatim"/>
  139. </xsl:when>
  140. <xsl:otherwise>
  141. <xsl:copy-of select="$content"/>
  142. </xsl:otherwise>
  143. </xsl:choose>
  144. </fo:block>
  145. </xsl:when>
  146. <xsl:otherwise>
  147. <fo:block id="{$id}"
  148. white-space-collapse='false'
  149. white-space-treatment='preserve'
  150. linefeed-treatment="preserve"
  151. xsl:use-attribute-sets="monospace.verbatim.properties" font-size="{$font_size}%">
  152. <xsl:choose>
  153. <xsl:when test="$hyphenate.verbatim != 0 and function-available('exsl:node-set')">
  154. <xsl:apply-templates select="exsl:node-set($content)" mode="hyphenate.verbatim"/>
  155. </xsl:when>
  156. <xsl:otherwise>
  157. <xsl:copy-of select="$content"/>
  158. </xsl:otherwise>
  159. </xsl:choose>
  160. </fo:block>
  161. </xsl:otherwise>
  162. </xsl:choose>
  163. </xsl:template>
  164. </xsl:stylesheet>