1<?xml version='1.0'?>
2<!DOCTYPE xsl:stylesheet [
3<!ENTITY lowercase "'abcdefghijklmnopqrstuvwxyz'">
4<!ENTITY uppercase "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'">
5 ]>
6<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
7                xmlns:fo="http://www.w3.org/1999/XSL/Format"
8                xmlns:xlink="http://www.w3.org/1999/xlink"
9                xmlns:stext="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.TextFactory"
10                xmlns:xtext="com.nwalsh.xalan.Text"
11                xmlns:lxslt="http://xml.apache.org/xslt"
12                exclude-result-prefixes="xlink stext xtext lxslt"
13                extension-element-prefixes="stext xtext"
14                version='1.0'>
15
16<!-- ********************************************************************
17     $Id: graphics.xsl,v 1.46 2006/05/07 01:02:44 bobstayton Exp $
18     ********************************************************************
19
20     This file is part of the XSL DocBook Stylesheet distribution.
21     See /README or http://nwalsh.com/docbook/xsl/ for copyright
22     and other information.
23
24     Contributors:
25     Colin Paul Adams, <colin@colina.demon.co.uk>
26     Paul Grosso, <pgrosso@arbortext.com>
27
28     ******************************************************************** -->
29
30<!-- ==================================================================== -->
31<!-- Graphic format tests for the FO backend -->
32
33<xsl:param name="graphic.notations">
34  <!-- n.b. exactly one leading space, one trailing space, and one inter-word space -->
35  <xsl:choose>
36    <xsl:when test="$passivetex.extensions != 0">
37      <xsl:text> PNG PDF JPG JPEG linespecific </xsl:text>
38    </xsl:when>
39    <xsl:when test="$fop1.extensions != 0">
40      <xsl:text> BMP GIF TIFF SVG PNG EPS JPG JPEG linespecific </xsl:text>
41    </xsl:when>
42    <xsl:when test="$fop.extensions != 0">
43      <xsl:text> BMP GIF TIFF SVG PNG EPS JPG JPEG linespecific </xsl:text>
44    </xsl:when>
45    <xsl:when test="$arbortext.extensions != 0">
46      <xsl:text> PNG PDF JPG JPEG linespecific GIF GIF87a GIF89a TIFF BMP </xsl:text>
47    </xsl:when>
48    <xsl:when test="$xep.extensions != 0">
49      <xsl:text> SVG PNG PDF JPG JPEG linespecific GIF GIF87a GIF89a TIFF BMP </xsl:text>
50    </xsl:when>
51    <xsl:otherwise>
52      <xsl:text> PNG PDF JPG JPEG linespecific GIF GIF87a GIF89a TIFF BMP </xsl:text>
53    </xsl:otherwise>
54  </xsl:choose>
55</xsl:param>
56
57<xsl:template name="is.graphic.format">
58  <xsl:param name="format"/>
59  <xsl:if test="contains($graphic.notations, concat(' ',$format,' '))">1</xsl:if>
60</xsl:template>
61
62<xsl:param name="graphic.extensions">
63  <!-- n.b. exactly one leading space, one trailing space, and one inter-word space -->
64  <xsl:choose>
65    <xsl:when test="$passivetex.extensions != 0">
66      <xsl:text> png pdf jpg jpeg </xsl:text>
67    </xsl:when>
68    <xsl:when test="$fop1.extensions != 0">
69      <xsl:text> bmp gif tif tiff svg png pdf jpg jpeg eps </xsl:text>
70    </xsl:when>
71    <xsl:when test="$fop.extensions != 0">
72      <xsl:text> bmp gif tif tiff svg png pdf jpg jpeg eps </xsl:text>
73    </xsl:when>
74    <xsl:when test="$arbortext.extensions != 0">
75      <xsl:text> png pdf jpg jpeg gif tif tiff bmp </xsl:text>
76    </xsl:when>
77    <xsl:when test="$xep.extensions != 0">
78      <xsl:text> svg png pdf jpg jpeg gif tif tiff bmp eps </xsl:text>
79    </xsl:when>
80    <xsl:otherwise>
81      <xsl:text> svg png pdf jpg jpeg gif tif tiff bmp eps </xsl:text>
82    </xsl:otherwise>
83  </xsl:choose>
84</xsl:param>
85
86<xsl:template name="is.graphic.extension">
87  <xsl:param name="ext"/>
88  <xsl:variable name="lcext" select="translate($ext,
89                                       'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
90                                       'abcdefghijklmnopqrstuvwxyz')"/>
91
92  <xsl:if test="contains($graphic.extensions,
93                         concat(' ', $lcext, ' '))">1</xsl:if>
94</xsl:template>
95
96<xsl:template name="graphic.format.content-type">
97  <xsl:param name="format"/>
98  <xsl:variable name="is.graphic.format">
99    <xsl:call-template name="is.graphic.format">
100      <xsl:with-param name="format" select="$format"/>
101    </xsl:call-template>
102  </xsl:variable>
103  <xsl:if test="$is.graphic.format">
104    <xsl:choose>
105      <xsl:when test="$format = ''"></xsl:when>
106      <xsl:when test="$format = 'linespecific'"></xsl:when>
107      <xsl:when test="$format = 'PS'">application/postscript</xsl:when>
108      <xsl:when test="$format = 'PDF'">application/pdf</xsl:when>
109      <xsl:when test="$format = 'PNG'">image/png</xsl:when>
110      <xsl:when test="$format = 'SVG'">image/svg+xml</xsl:when>
111      <xsl:when test="$format = 'JPG'">image/jpeg</xsl:when>
112      <xsl:when test="$format = 'GIF87a'">image/gif</xsl:when>
113      <xsl:when test="$format = 'GIF89a'">image/gif</xsl:when>
114      <xsl:otherwise>
115          <xsl:value-of select="concat('image/', 
116            translate($format, &uppercase;, &lowercase;))"/>
117      </xsl:otherwise>
118    </xsl:choose>
119  </xsl:if>
120</xsl:template>
121
122
123<!-- ==================================================================== -->
124
125<xsl:template match="screenshot">
126  <fo:block>
127    <xsl:apply-templates/>
128  </fo:block>
129</xsl:template>
130
131<xsl:template match="screeninfo">
132</xsl:template>
133
134<!-- ==================================================================== -->
135<!-- Override these templates for FO -->
136<!-- ==================================================================== -->
137
138<xsl:template name="process.image">
139  <!-- When this template is called, the current node should be  -->
140  <!-- a graphic, inlinegraphic, imagedata, or videodata. All    -->
141  <!-- those elements have the same set of attributes, so we can -->
142  <!-- handle them all in one place.                             -->
143
144  <xsl:variable name="scalefit">
145    <xsl:choose>
146      <xsl:when test="$ignore.image.scaling != 0">0</xsl:when>
147      <xsl:when test="@contentwidth">0</xsl:when>
148      <xsl:when test="@contentdepth and 
149                      @contentdepth != '100%'">0</xsl:when>
150      <xsl:when test="@scale">0</xsl:when>
151      <xsl:when test="@scalefit"><xsl:value-of select="@scalefit"/></xsl:when>
152      <xsl:when test="@width or @depth">1</xsl:when>
153      <xsl:otherwise>0</xsl:otherwise>
154    </xsl:choose>
155  </xsl:variable>
156
157  <xsl:variable name="scale">
158    <xsl:choose>
159      <xsl:when test="$ignore.image.scaling != 0">0</xsl:when>
160      <xsl:when test="@contentwidth or @contentdepth">1.0</xsl:when>
161      <xsl:when test="@scale">
162        <xsl:value-of select="@scale div 100.0"/>
163      </xsl:when>
164      <xsl:otherwise>1.0</xsl:otherwise>
165    </xsl:choose>
166  </xsl:variable>
167
168  <xsl:variable name="filename">
169    <xsl:choose>
170      <xsl:when test="local-name(.) = 'graphic'
171                      or local-name(.) = 'inlinegraphic'">
172        <!-- handle legacy graphic and inlinegraphic by new template --> 
173        <xsl:call-template name="mediaobject.filename">
174          <xsl:with-param name="object" select="."/>
175        </xsl:call-template>
176      </xsl:when>
177      <xsl:otherwise>
178        <!-- imagedata, videodata, audiodata -->
179        <xsl:call-template name="mediaobject.filename">
180          <xsl:with-param name="object" select=".."/>
181        </xsl:call-template>
182      </xsl:otherwise>
183    </xsl:choose>
184  </xsl:variable>
185
186  <xsl:variable name="content-type">
187    <xsl:if test="@format">
188      <xsl:call-template name="graphic.format.content-type">
189        <xsl:with-param name="format" select="@format"/>
190      </xsl:call-template>
191    </xsl:if>
192  </xsl:variable>
193
194  <xsl:variable name="bgcolor">
195    <xsl:call-template name="dbfo-attribute">
196      <xsl:with-param name="pis"
197                      select="/processing-instruction('dbfo')"/>
198      <xsl:with-param name="attribute" select="'background-color'"/>
199    </xsl:call-template>
200  </xsl:variable>
201
202  <fo:external-graphic>
203    <xsl:attribute name="src">
204      <xsl:call-template name="fo-external-image">
205        <xsl:with-param name="filename">
206          <xsl:if test="$img.src.path != '' and
207                        not(starts-with($filename, '/')) and
208                        not(contains($filename, '://'))">
209            <xsl:value-of select="$img.src.path"/>
210          </xsl:if>
211          <xsl:value-of select="$filename"/>
212        </xsl:with-param>
213      </xsl:call-template>
214    </xsl:attribute>
215
216    <xsl:attribute name="width">
217      <xsl:choose>
218        <xsl:when test="$ignore.image.scaling != 0">auto</xsl:when>
219        <xsl:when test="contains(@width,'%')">
220          <xsl:value-of select="@width"/>
221        </xsl:when>
222        <xsl:when test="@width and not(@width = '')">
223          <xsl:call-template name="length-spec">
224            <xsl:with-param name="length" select="@width"/>
225            <xsl:with-param name="default.units" select="'px'"/>
226          </xsl:call-template>
227        </xsl:when>
228        <xsl:when test="not(@depth) and $default.image.width != ''">
229          <xsl:call-template name="length-spec">
230            <xsl:with-param name="length" select="$default.image.width"/>
231            <xsl:with-param name="default.units" select="'px'"/>
232          </xsl:call-template>
233        </xsl:when>
234        <xsl:otherwise>auto</xsl:otherwise>
235      </xsl:choose>
236    </xsl:attribute>
237
238    <xsl:attribute name="height">
239      <xsl:choose>
240        <xsl:when test="$ignore.image.scaling != 0">auto</xsl:when>
241        <xsl:when test="contains(@depth,'%')">
242          <xsl:value-of select="@depth"/>
243        </xsl:when>
244        <xsl:when test="@depth">
245          <xsl:call-template name="length-spec">
246            <xsl:with-param name="length" select="@depth"/>
247            <xsl:with-param name="default.units" select="'px'"/>
248          </xsl:call-template>
249        </xsl:when>
250        <xsl:otherwise>auto</xsl:otherwise>
251      </xsl:choose>
252    </xsl:attribute>
253
254    <xsl:attribute name="content-width">
255      <xsl:choose>
256        <xsl:when test="$ignore.image.scaling != 0">auto</xsl:when>
257        <xsl:when test="contains(@contentwidth,'%')">
258          <xsl:value-of select="@contentwidth"/>
259        </xsl:when>
260        <xsl:when test="@contentwidth">
261          <xsl:call-template name="length-spec">
262            <xsl:with-param name="length" select="@contentwidth"/>
263            <xsl:with-param name="default.units" select="'px'"/>
264          </xsl:call-template>
265        </xsl:when>
266        <xsl:when test="number($scale) != 1.0">
267          <xsl:value-of select="$scale * 100"/>
268          <xsl:text>%</xsl:text>
269        </xsl:when>
270        <xsl:when test="$scalefit = 1">scale-to-fit</xsl:when>
271        <xsl:otherwise>auto</xsl:otherwise>
272      </xsl:choose>
273    </xsl:attribute>
274
275    <xsl:attribute name="content-height">
276      <xsl:choose>
277        <xsl:when test="$ignore.image.scaling != 0">auto</xsl:when>
278        <xsl:when test="contains(@contentdepth,'%')">
279          <xsl:value-of select="@contentdepth"/>
280        </xsl:when>
281        <xsl:when test="@contentdepth">
282          <xsl:call-template name="length-spec">
283            <xsl:with-param name="length" select="@contentdepth"/>
284            <xsl:with-param name="default.units" select="'px'"/>
285          </xsl:call-template>
286        </xsl:when>
287        <xsl:when test="number($scale) != 1.0">
288          <xsl:value-of select="$scale * 100"/>
289          <xsl:text>%</xsl:text>
290        </xsl:when>
291        <xsl:when test="$scalefit = 1">scale-to-fit</xsl:when>
292        <xsl:otherwise>auto</xsl:otherwise>
293      </xsl:choose>
294    </xsl:attribute>
295
296    <xsl:if test="$content-type != ''">
297      <xsl:attribute name="content-type">
298        <xsl:value-of select="concat('content-type:',$content-type)"/>
299      </xsl:attribute>
300    </xsl:if>
301
302    <xsl:if test="$bgcolor != ''">
303      <xsl:attribute name="background-color">
304        <xsl:value-of select="$bgcolor"/>
305      </xsl:attribute>
306    </xsl:if>
307
308    <xsl:if test="@align">
309      <xsl:attribute name="text-align">
310        <xsl:value-of select="@align"/>
311      </xsl:attribute>
312    </xsl:if>
313
314    <xsl:if test="@valign">
315      <xsl:attribute name="display-align">
316        <xsl:choose>
317          <xsl:when test="@valign = 'top'">before</xsl:when>
318          <xsl:when test="@valign = 'middle'">center</xsl:when>
319          <xsl:when test="@valign = 'bottom'">after</xsl:when>
320          <xsl:otherwise>auto</xsl:otherwise>
321        </xsl:choose>
322      </xsl:attribute>
323    </xsl:if>
324  </fo:external-graphic>
325</xsl:template>
326
327<!-- ==================================================================== -->
328
329<xsl:template match="graphic">
330  <xsl:choose>
331    <xsl:when test="parent::inlineequation">
332      <xsl:call-template name="process.image"/>
333    </xsl:when>
334    <xsl:otherwise>
335      <fo:block>
336        <xsl:if test="@align">
337          <xsl:attribute name="text-align">
338            <xsl:value-of select="@align"/>
339          </xsl:attribute>
340        </xsl:if>
341        <xsl:call-template name="process.image"/>
342      </fo:block>
343    </xsl:otherwise>
344  </xsl:choose>
345</xsl:template>
346
347<xsl:template match="inlinegraphic">
348  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
349  <xsl:variable name="filename">
350    <xsl:choose>
351      <xsl:when test="@entityref">
352        <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
353      </xsl:when>
354      <xsl:otherwise>
355        <xsl:apply-templates select="@fileref"/>
356      </xsl:otherwise>
357    </xsl:choose>
358  </xsl:variable>
359
360  <xsl:choose>
361    <xsl:when test="@format='linespecific'">
362      <xsl:choose>
363        <xsl:when test="$use.extensions != '0'
364                        and $textinsert.extension != '0'">
365          <xsl:choose>
366            <xsl:when test="contains($vendor, 'SAXON')">
367              <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
368            </xsl:when>
369            <xsl:when test="contains($vendor, 'Apache Software Foundation')">
370              <xtext:insertfile href="{$filename}"/>
371            </xsl:when>
372            <xsl:otherwise>
373              <xsl:message terminate="yes">
374                <xsl:text>Don't know how to insert files with </xsl:text>
375                <xsl:value-of select="$vendor"/>
376              </xsl:message>
377            </xsl:otherwise>
378          </xsl:choose>
379        </xsl:when>
380        <xsl:otherwise>
381          <a xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"
382             href="{$filename}"/>
383        </xsl:otherwise>
384      </xsl:choose>
385    </xsl:when>
386    <xsl:otherwise>
387      <xsl:call-template name="process.image"/>
388    </xsl:otherwise>
389  </xsl:choose>
390</xsl:template>
391
392<!-- ==================================================================== -->
393
394<xsl:template match="mediaobject|mediaobjectco">
395
396  <xsl:variable name="olist" select="imageobject|imageobjectco
397                     |videoobject|audioobject
398                     |textobject"/>
399
400  <xsl:variable name="object.index">
401    <xsl:call-template name="select.mediaobject.index">
402      <xsl:with-param name="olist" select="$olist"/>
403      <xsl:with-param name="count" select="1"/>
404    </xsl:call-template>
405  </xsl:variable>
406
407  <xsl:variable name="object" select="$olist[position() = $object.index]"/>
408
409  <xsl:variable name="align">
410    <xsl:value-of select="$object/descendant::imagedata[@align][1]/@align"/>
411  </xsl:variable>
412
413  <xsl:variable name="id">
414    <xsl:call-template name="object.id"/>
415  </xsl:variable>
416
417  <fo:block id="{$id}">
418    <xsl:if test="$align != '' ">
419      <xsl:attribute name="text-align">
420        <xsl:value-of select="$align"/>
421      </xsl:attribute>
422    </xsl:if>
423
424    <xsl:apply-templates select="$object"/>
425    <xsl:apply-templates select="caption"/>
426  </fo:block>
427</xsl:template>
428
429<xsl:template match="inlinemediaobject">
430  <xsl:call-template name="select.mediaobject"/>
431</xsl:template>
432
433<!-- ==================================================================== -->
434
435<xsl:template match="imageobjectco">
436  <xsl:apply-templates select="imageobject"/>
437  <xsl:apply-templates select="calloutlist"/>
438</xsl:template>
439
440<xsl:template match="imageobject">
441  <xsl:choose>
442    <xsl:when test="imagedata">
443      <xsl:apply-templates select="imagedata"/>
444    </xsl:when>
445    <xsl:otherwise>
446      <fo:instream-foreign-object>
447        <xsl:apply-templates mode="copy-all"/>
448      </fo:instream-foreign-object>
449    </xsl:otherwise>
450  </xsl:choose>
451</xsl:template>
452
453<!-- ==================================================================== -->
454
455<xsl:template match="*" mode="copy-all">
456  <xsl:copy>
457    <xsl:for-each select="@*">
458      <xsl:copy/>
459    </xsl:for-each>
460    <xsl:apply-templates mode="copy-all"/>
461  </xsl:copy>
462</xsl:template>
463
464<xsl:template match="text()|comment()|processing-instruction()" mode="copy-all">
465  <xsl:copy/>
466</xsl:template>
467
468<!-- ==================================================================== -->
469
470<xsl:template match="imagedata">
471  <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
472  <xsl:variable name="filename">
473    <xsl:call-template name="mediaobject.filename">
474      <xsl:with-param name="object" select=".."/>
475    </xsl:call-template>
476  </xsl:variable>
477
478  <xsl:choose>
479    <xsl:when test="@format='linespecific'">
480      <xsl:choose>
481        <xsl:when test="$use.extensions != '0'
482                        and $textinsert.extension != '0'">
483          <xsl:choose>
484            <xsl:when test="contains($vendor, 'SAXON')">
485              <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
486            </xsl:when>
487            <xsl:when test="contains($vendor, 'Apache Software Foundation')">
488              <xtext:insertfile href="{$filename}"/>
489            </xsl:when>
490            <xsl:otherwise>
491              <xsl:message terminate="yes">
492                <xsl:text>Don't know how to insert files with </xsl:text>
493                <xsl:value-of select="$vendor"/>
494              </xsl:message>
495            </xsl:otherwise>
496          </xsl:choose>
497        </xsl:when>
498        <xsl:otherwise>
499          <a xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"
500             href="{$filename}"/>
501        </xsl:otherwise>
502      </xsl:choose>
503    </xsl:when>
504    <xsl:otherwise>
505      <xsl:call-template name="process.image"/>
506    </xsl:otherwise>
507  </xsl:choose>
508</xsl:template>
509
510<!-- ==================================================================== -->
511
512<xsl:template match="videoobject">
513  <xsl:apply-templates select="videodata"/>
514</xsl:template>
515
516<xsl:template match="videodata">
517  <xsl:call-template name="process.image"/>
518</xsl:template>
519
520<!-- ==================================================================== -->
521
522<xsl:template match="audioobject">
523  <xsl:apply-templates select="audiodata"/>
524</xsl:template>
525
526<xsl:template match="audiodata">
527  <xsl:call-template name="process.image"/>
528</xsl:template>
529
530<!-- ==================================================================== -->
531
532<xsl:template match="textobject">
533  <xsl:apply-templates/>
534</xsl:template>
535
536<xsl:template match="textdata">
537  <xsl:variable name="filename">
538    <xsl:choose>
539      <xsl:when test="@entityref">
540        <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
541      </xsl:when>
542      <xsl:otherwise>
543        <xsl:apply-templates select="@fileref"/>
544      </xsl:otherwise>
545    </xsl:choose>
546  </xsl:variable>
547
548  <xsl:variable name="encoding">
549    <xsl:choose>
550      <xsl:when test="@encoding">
551        <xsl:value-of select="@encoding"/>
552      </xsl:when>
553      <xsl:otherwise>
554        <xsl:value-of select="$textdata.default.encoding"/>
555      </xsl:otherwise>
556    </xsl:choose>
557  </xsl:variable>
558
559  <xsl:choose>
560    <xsl:when test="$use.extensions != '0'
561                    and $textinsert.extension != '0'">
562      <xsl:choose>
563        <xsl:when test="element-available('stext:insertfile')">
564          <stext:insertfile href="{$filename}" encoding="{$encoding}"/>
565        </xsl:when>
566        <xsl:when test="element-available('xtext:insertfile')">
567          <xtext:insertfile href="{$filename}"/>
568        </xsl:when>
569        <xsl:otherwise>
570          <xsl:message terminate="yes">
571            <xsl:text>No insertfile extension available.</xsl:text>
572          </xsl:message>
573        </xsl:otherwise>
574      </xsl:choose>
575    </xsl:when>
576    <xsl:otherwise>
577      <a xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"
578         href="{$filename}"/>
579    </xsl:otherwise>
580  </xsl:choose>
581</xsl:template>
582
583<!-- ==================================================================== -->
584
585<xsl:template match="caption">
586  <fo:block>
587    <xsl:if test="@align = 'right' or @align = 'left' or @align='center'">
588      <xsl:attribute name="text-align"><xsl:value-of
589                         select="@align"/></xsl:attribute>
590    </xsl:if>
591    <xsl:apply-templates/>
592  </fo:block>
593</xsl:template>
594
595<!-- ==================================================================== -->
596
597<xsl:template name="fo-external-image">
598  <xsl:param name="filename"/>
599
600  <xsl:choose>
601    <xsl:when test="$passivetex.extensions != 0
602                    or $fop.extensions != 0">
603      <xsl:value-of select="$filename"/>
604    </xsl:when>
605    <xsl:otherwise>
606      <xsl:value-of select="concat('url(', $filename, ')')"/>
607    </xsl:otherwise>
608  </xsl:choose>
609</xsl:template>
610
611<!-- Resolve xml:base attributes -->
612<xsl:template match="@fileref">
613  <!-- need a check for absolute urls -->
614  <xsl:choose>
615    <xsl:when test="contains(., ':')">
616      <!-- it has a uri scheme so it is an absolute uri -->
617      <xsl:value-of select="."/>
618    </xsl:when>
619    <xsl:when test="$keep.relative.image.uris != 0">
620      <!-- leave it alone -->
621      <xsl:value-of select="."/>
622    </xsl:when>
623    <xsl:otherwise>
624      <!-- its a relative uri -->
625      <xsl:call-template name="relative-uri">
626      </xsl:call-template>
627    </xsl:otherwise>
628  </xsl:choose>
629</xsl:template>
630
631</xsl:stylesheet>
632