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