]> git.stg.codes - stg.git/blob - doc/xslt/html/graphics.xsl
Fixed bfstream test compilation.
[stg.git] / doc / xslt / html / graphics.xsl
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 xmlns:xlink="http://www.w3.org/1999/xlink"
4                 xmlns:stext="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.TextFactory"
5                 xmlns:simg="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.ImageIntrinsics"
6                 xmlns:ximg="xalan://com.nwalsh.xalan.ImageIntrinsics"
7                 xmlns:xtext="xalan://com.nwalsh.xalan.Text"
8                 xmlns:lxslt="http://xml.apache.org/xslt"
9                 exclude-result-prefixes="xlink stext xtext lxslt simg ximg"
10                 extension-element-prefixes="stext xtext"
11                 version='1.0'>
12
13 <!-- ********************************************************************
14      $Id: graphics.xsl 8421 2009-05-04 07:49:49Z bobstayton $
15      ********************************************************************
16
17      This file is part of the XSL DocBook Stylesheet distribution.
18      See ../README or http://docbook.sf.net/release/xsl/current/ for
19      copyright and other information.
20
21      Contributors:
22      Colin Paul Adams, <colin@colina.demon.co.uk>
23
24      ******************************************************************** -->
25
26 <lxslt:component prefix="xtext" elements="insertfile"/>
27 <lxslt:component prefix="ximg" functions="new getWidth getDepth"/>
28
29 <!-- ==================================================================== -->
30 <!-- Graphic format tests for the HTML backend -->
31
32 <xsl:template name="is.graphic.format">
33   <xsl:param name="format"></xsl:param>
34   <xsl:if test="$format = 'SVG'
35                 or $format = 'PNG'
36                 or $format = 'JPG'
37                 or $format = 'JPEG'
38                 or $format = 'linespecific'
39                 or $format = 'GIF'
40                 or $format = 'GIF87a'
41                 or $format = 'GIF89a'
42                 or $format = 'BMP'">1</xsl:if>
43 </xsl:template>
44
45 <xsl:template name="is.graphic.extension">
46   <xsl:param name="ext"></xsl:param>
47   <xsl:variable name="lcext" select="translate($ext,
48                                        'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
49                                        'abcdefghijklmnopqrstuvwxyz')"/>
50   <xsl:if test="$lcext = 'svg'
51              or $lcext = 'png'
52              or $lcext = 'jpeg'
53              or $lcext = 'jpg'
54              or $lcext = 'avi'
55              or $lcext = 'mpg'
56              or $lcext = 'mpeg'
57              or $lcext = 'qt'
58              or $lcext = 'gif'
59              or $lcext = 'bmp'">1</xsl:if>
60 </xsl:template>
61
62 <!-- ==================================================================== -->
63
64 <xsl:template match="screenshot">
65   <div>
66     <xsl:apply-templates select="." mode="common.html.attributes"/>
67     <xsl:apply-templates/>
68   </div>
69 </xsl:template>
70
71 <xsl:template match="screeninfo">
72 </xsl:template>
73
74 <!-- ==================================================================== -->
75
76 <xsl:template name="process.image">
77   <!-- When this template is called, the current node should be  -->
78   <!-- a graphic, inlinegraphic, imagedata, or videodata. All    -->
79   <!-- those elements have the same set of attributes, so we can -->
80   <!-- handle them all in one place.                             -->
81   <xsl:param name="tag" select="'img'"/>
82   <xsl:param name="alt"/>
83   <xsl:param name="longdesc"/>
84
85   <!-- The HTML img element only supports the notion of content-area
86        scaling; it doesn't support the distinction between a
87        content-area and a viewport-area, so we have to make some
88        compromises.
89
90        1. If only the content-area is specified, everything is fine.
91           (If you ask for a three inch image, that's what you'll get.)
92
93        2. If only the viewport-area is provided:
94           - If scalefit=1, treat it as both the content-area and
95             the viewport-area. (If you ask for an image in a five inch
96             area, we'll make the image five inches to fill that area.)
97           - If scalefit=0, ignore the viewport-area specification.
98
99           Note: this is not quite the right semantic and has the additional
100           problem that it can result in anamorphic scaling, which scalefit
101           should never cause.
102
103        3. If both the content-area and the viewport-area is specified
104           on a graphic element, ignore the viewport-area.
105           (If you ask for a three inch image in a five inch area, we'll assume
106            it's better to give you a three inch image in an unspecified area
107            than a five inch image in a five inch area.
108
109        Relative units also cause problems. As a general rule, the stylesheets
110        are operating too early and too loosely coupled with the rendering engine
111        to know things like the current font size or the actual dimensions of
112        an image. Therefore:
113
114        1. We use a fixed size for pixels, $pixels.per.inch
115
116        2. We use a fixed size for "em"s, $points.per.em
117
118        Percentages are problematic. In the following discussion, we speak
119        of width and contentwidth, but the same issues apply to depth and
120        contentdepth
121
122        1. A width of 50% means "half of the available space for the image."
123           That's fine. But note that in HTML, this is a dynamic property and
124           the image size will vary if the browser window is resized.
125
126        2. A contentwidth of 50% means "half of the actual image width". But
127           the stylesheets have no way to assess the image's actual size. Treating
128           this as a width of 50% is one possibility, but it produces behavior
129           (dynamic scaling) that seems entirely out of character with the
130           meaning.
131
132           Instead, the stylesheets define a $nominal.image.width
133           and convert percentages to actual values based on that nominal size.
134
135        Scale can be problematic. Scale applies to the contentwidth, so
136        a scale of 50 when a contentwidth is not specified is analagous to a
137        width of 50%. (If a contentwidth is specified, the scaling factor can
138        be applied to that value and no problem exists.)
139
140        If scale is specified but contentwidth is not supplied, the
141        nominal.image.width is used to calculate a base size
142        for scaling.
143
144        Warning: as a consequence of these decisions, unless the aspect ratio
145        of your image happens to be exactly the same as (nominal width / nominal height),
146        specifying contentwidth="50%" and contentdepth="50%" is NOT going to
147        scale the way you expect (or really, the way it should).
148
149        Don't do that. In fact, a percentage value is not recommended for content
150        size at all. Use scale instead.
151
152        Finally, align and valign are troublesome. Horizontal alignment is now
153        supported by wrapping the image in a <div align="{@align}"> (in block
154        contexts!). I can't think of anything (practical) to do about vertical
155        alignment.
156   -->
157
158   <xsl:variable name="width-units">
159     <xsl:choose>
160       <xsl:when test="$ignore.image.scaling != 0"></xsl:when>
161       <xsl:when test="@width">
162         <xsl:call-template name="length-units">
163           <xsl:with-param name="length" select="@width"/>
164         </xsl:call-template>
165       </xsl:when>
166       <xsl:when test="not(@depth) and $default.image.width != ''">
167         <xsl:call-template name="length-units">
168           <xsl:with-param name="length" select="$default.image.width"/>
169         </xsl:call-template>
170       </xsl:when>
171     </xsl:choose>
172   </xsl:variable>
173
174   <xsl:variable name="width">
175     <xsl:choose>
176       <xsl:when test="$ignore.image.scaling != 0"></xsl:when>
177       <xsl:when test="@width">
178         <xsl:choose>
179           <xsl:when test="$width-units = '%'">
180             <xsl:value-of select="@width"/>
181           </xsl:when>
182           <xsl:otherwise>
183             <xsl:call-template name="length-spec">
184               <xsl:with-param name="length" select="@width"/>
185             </xsl:call-template>
186           </xsl:otherwise>
187         </xsl:choose>
188       </xsl:when>
189       <xsl:when test="not(@depth) and $default.image.width != ''">
190         <xsl:value-of select="$default.image.width"/>
191       </xsl:when>
192     </xsl:choose>
193   </xsl:variable>
194
195   <xsl:variable name="scalefit">
196     <xsl:choose>
197       <xsl:when test="$ignore.image.scaling != 0">0</xsl:when>
198       <xsl:when test="@contentwidth or @contentdepth">0</xsl:when>
199       <xsl:when test="@scale">0</xsl:when>
200       <xsl:when test="@scalefit"><xsl:value-of select="@scalefit"/></xsl:when>
201       <xsl:when test="$width != '' or @depth">1</xsl:when>
202       <xsl:otherwise>0</xsl:otherwise>
203     </xsl:choose>
204   </xsl:variable>
205
206   <xsl:variable name="scale">
207     <xsl:choose>
208       <xsl:when test="$ignore.image.scaling != 0">1.0</xsl:when>
209       <xsl:when test="@contentwidth or @contentdepth">1.0</xsl:when>
210       <xsl:when test="@scale">
211         <xsl:value-of select="@scale div 100.0"/>
212       </xsl:when>
213       <xsl:otherwise>1.0</xsl:otherwise>
214     </xsl:choose>
215   </xsl:variable>
216
217   <xsl:variable name="filename">
218     <xsl:choose>
219       <xsl:when test="local-name(.) = 'graphic'
220                       or local-name(.) = 'inlinegraphic'">
221         <!-- handle legacy graphic and inlinegraphic by new template --> 
222         <xsl:call-template name="mediaobject.filename">
223           <xsl:with-param name="object" select="."/>
224         </xsl:call-template>
225       </xsl:when>
226       <xsl:otherwise>
227         <!-- imagedata, videodata, audiodata -->
228         <xsl:call-template name="mediaobject.filename">
229           <xsl:with-param name="object" select=".."/>
230         </xsl:call-template>
231       </xsl:otherwise>
232     </xsl:choose>
233   </xsl:variable>
234
235   <xsl:variable name="output_filename">
236     <xsl:choose>
237       <xsl:when test="@entityref">
238         <xsl:value-of select="$filename"/>
239       </xsl:when>
240       <!--
241         Moved test for $keep.relative.image.uris to template below:
242             <xsl:template match="@fileref">
243       -->
244       <xsl:otherwise>
245         <xsl:value-of select="$filename"/>
246       </xsl:otherwise>
247     </xsl:choose>
248   </xsl:variable>
249
250   <xsl:variable name="img.src.path.pi">
251     <xsl:call-template name="pi.dbhtml_img.src.path">
252       <xsl:with-param name="node" select=".."/>
253     </xsl:call-template>
254   </xsl:variable>
255
256   <xsl:variable name="filename.for.graphicsize">
257     <xsl:choose>
258       <xsl:when test="$img.src.path.pi != ''">
259         <xsl:value-of select="concat($img.src.path.pi, $filename)"/>
260       </xsl:when>
261       <xsl:when test="$img.src.path != '' and
262                       $graphicsize.use.img.src.path != 0 and
263                       $tag = 'img' and
264                       not(starts-with($filename, '/')) and
265                       not(contains($filename, '://'))">
266         <xsl:value-of select="concat($img.src.path, $filename)"/>
267       </xsl:when>
268       <xsl:otherwise>
269         <xsl:value-of select="$filename"/>
270       </xsl:otherwise>
271     </xsl:choose>
272   </xsl:variable>
273
274   <xsl:variable name="realintrinsicwidth">
275     <!-- This funny compound test works around a bug in XSLTC -->
276     <xsl:choose>
277       <xsl:when test="$use.extensions != 0 and $graphicsize.extension != 0">
278         <xsl:choose>
279           <xsl:when test="function-available('simg:getWidth')">
280             <xsl:value-of select="simg:getWidth(simg:new($filename.for.graphicsize),
281                                                 $nominal.image.width)"/>
282           </xsl:when>
283           <xsl:when test="function-available('ximg:getWidth')">
284             <xsl:value-of select="ximg:getWidth(ximg:new($filename.for.graphicsize),
285                                                 $nominal.image.width)"/>
286           </xsl:when>
287           <xsl:otherwise>
288            <xsl:value-of select="0"/>
289           </xsl:otherwise>
290         </xsl:choose>
291       </xsl:when>
292       <xsl:otherwise>
293         <xsl:value-of select="0"/>
294       </xsl:otherwise>
295     </xsl:choose>
296   </xsl:variable>
297
298   <xsl:variable name="intrinsicwidth">
299     <xsl:choose>
300       <xsl:when test="$realintrinsicwidth = 0">
301        <xsl:value-of select="$nominal.image.width"/>
302       </xsl:when>
303       <xsl:otherwise>
304        <xsl:value-of select="$realintrinsicwidth"/>
305       </xsl:otherwise>
306     </xsl:choose>
307   </xsl:variable>
308
309   <xsl:variable name="intrinsicdepth">
310     <!-- This funny compound test works around a bug in XSLTC -->
311     <xsl:choose>
312       <xsl:when test="$use.extensions != 0 and $graphicsize.extension != 0">
313         <xsl:choose>
314           <xsl:when test="function-available('simg:getDepth')">
315             <xsl:value-of select="simg:getDepth(simg:new($filename.for.graphicsize),
316                                                 $nominal.image.depth)"/>
317           </xsl:when>
318           <xsl:when test="function-available('ximg:getDepth')">
319             <xsl:value-of select="ximg:getDepth(ximg:new($filename.for.graphicsize),
320                                                 $nominal.image.depth)"/>
321           </xsl:when>
322           <xsl:otherwise>
323             <xsl:value-of select="$nominal.image.depth"/>
324           </xsl:otherwise>
325         </xsl:choose>
326       </xsl:when>
327       <xsl:otherwise>
328         <xsl:value-of select="$nominal.image.depth"/>
329       </xsl:otherwise>
330     </xsl:choose>
331   </xsl:variable>
332
333   <xsl:variable name="contentwidth">
334     <xsl:choose>
335       <xsl:when test="$ignore.image.scaling != 0"></xsl:when>
336       <xsl:when test="@contentwidth">
337         <xsl:variable name="units">
338           <xsl:call-template name="length-units">
339             <xsl:with-param name="length" select="@contentwidth"/>
340           </xsl:call-template>
341         </xsl:variable>
342
343         <xsl:choose>
344           <xsl:when test="$units = '%'">
345             <xsl:variable name="cmagnitude">
346               <xsl:call-template name="length-magnitude">
347                 <xsl:with-param name="length" select="@contentwidth"/>
348               </xsl:call-template>
349             </xsl:variable>
350             <xsl:value-of select="$intrinsicwidth * $cmagnitude div 100.0"/>
351             <xsl:text>px</xsl:text>
352           </xsl:when>
353           <xsl:otherwise>
354             <xsl:call-template name="length-spec">
355               <xsl:with-param name="length" select="@contentwidth"/>
356             </xsl:call-template>
357           </xsl:otherwise>
358         </xsl:choose>
359       </xsl:when>
360       <xsl:otherwise>
361         <xsl:value-of select="$intrinsicwidth"/>
362         <xsl:text>px</xsl:text>
363       </xsl:otherwise>
364     </xsl:choose>
365   </xsl:variable>
366
367   <xsl:variable name="scaled.contentwidth">
368     <xsl:if test="$contentwidth != ''">
369       <xsl:variable name="cwidth.in.points">
370         <xsl:call-template name="length-in-points">
371           <xsl:with-param name="length" select="$contentwidth"/>
372           <xsl:with-param name="pixels.per.inch" select="$pixels.per.inch"/>
373           <xsl:with-param name="em.size" select="$points.per.em"/>
374         </xsl:call-template>
375       </xsl:variable>
376       <xsl:value-of select="$cwidth.in.points div 72.0 * $pixels.per.inch * $scale"/>
377     </xsl:if>
378   </xsl:variable>
379
380   <xsl:variable name="html.width">
381     <xsl:choose>
382       <xsl:when test="$ignore.image.scaling != 0"></xsl:when>
383       <xsl:when test="$width-units = '%'">
384         <xsl:value-of select="$width"/>
385       </xsl:when>
386       <xsl:when test="$width != ''">
387         <xsl:variable name="width.in.points">
388           <xsl:call-template name="length-in-points">
389             <xsl:with-param name="length" select="$width"/>
390             <xsl:with-param name="pixels.per.inch" select="$pixels.per.inch"/>
391             <xsl:with-param name="em.size" select="$points.per.em"/>
392           </xsl:call-template>
393         </xsl:variable>
394         <xsl:value-of select="round($width.in.points div 72.0 * $pixels.per.inch)"/>
395       </xsl:when>
396       <xsl:otherwise></xsl:otherwise>
397     </xsl:choose>
398   </xsl:variable>
399
400   <xsl:variable name="contentdepth">
401     <xsl:choose>
402       <xsl:when test="$ignore.image.scaling != 0"></xsl:when>
403       <xsl:when test="@contentdepth">
404         <xsl:variable name="units">
405           <xsl:call-template name="length-units">
406             <xsl:with-param name="length" select="@contentdepth"/>
407           </xsl:call-template>
408         </xsl:variable>
409
410         <xsl:choose>
411           <xsl:when test="$units = '%'">
412             <xsl:variable name="cmagnitude">
413               <xsl:call-template name="length-magnitude">
414                 <xsl:with-param name="length" select="@contentdepth"/>
415               </xsl:call-template>
416             </xsl:variable>
417             <xsl:value-of select="$intrinsicdepth * $cmagnitude div 100.0"/>
418             <xsl:text>px</xsl:text>
419           </xsl:when>
420           <xsl:otherwise>
421             <xsl:call-template name="length-spec">
422               <xsl:with-param name="length" select="@contentdepth"/>
423             </xsl:call-template>
424           </xsl:otherwise>
425         </xsl:choose>
426       </xsl:when>
427       <xsl:otherwise>
428         <xsl:value-of select="$intrinsicdepth"/>
429         <xsl:text>px</xsl:text>
430       </xsl:otherwise>
431     </xsl:choose>
432   </xsl:variable>
433
434   <xsl:variable name="scaled.contentdepth">
435     <xsl:if test="$contentdepth != ''">
436       <xsl:variable name="cdepth.in.points">
437         <xsl:call-template name="length-in-points">
438           <xsl:with-param name="length" select="$contentdepth"/>
439           <xsl:with-param name="pixels.per.inch" select="$pixels.per.inch"/>
440           <xsl:with-param name="em.size" select="$points.per.em"/>
441         </xsl:call-template>
442       </xsl:variable>
443       <xsl:value-of select="$cdepth.in.points div 72.0 * $pixels.per.inch * $scale"/>
444     </xsl:if>
445   </xsl:variable>
446
447   <xsl:variable name="depth-units">
448     <xsl:if test="@depth">
449       <xsl:call-template name="length-units">
450         <xsl:with-param name="length" select="@depth"/>
451       </xsl:call-template>
452     </xsl:if>
453   </xsl:variable>
454
455   <xsl:variable name="depth">
456     <xsl:if test="@depth">
457       <xsl:choose>
458         <xsl:when test="$depth-units = '%'">
459           <xsl:value-of select="@depth"/>
460         </xsl:when>
461         <xsl:otherwise>
462           <xsl:call-template name="length-spec">
463             <xsl:with-param name="length" select="@depth"/>
464           </xsl:call-template>
465         </xsl:otherwise>
466       </xsl:choose>
467     </xsl:if>
468   </xsl:variable>
469
470   <xsl:variable name="html.depth">
471     <xsl:choose>
472       <xsl:when test="$ignore.image.scaling != 0"></xsl:when>
473       <xsl:when test="$depth-units = '%'">
474         <xsl:value-of select="$depth"/>
475       </xsl:when>
476       <xsl:when test="@depth and @depth != ''">
477         <xsl:variable name="depth.in.points">
478           <xsl:call-template name="length-in-points">
479             <xsl:with-param name="length" select="$depth"/>
480             <xsl:with-param name="pixels.per.inch" select="$pixels.per.inch"/>
481             <xsl:with-param name="em.size" select="$points.per.em"/>
482           </xsl:call-template>
483         </xsl:variable>
484         <xsl:value-of select="round($depth.in.points div 72.0 * $pixels.per.inch)"/>
485       </xsl:when>
486       <xsl:otherwise></xsl:otherwise>
487     </xsl:choose>
488   </xsl:variable>
489
490   <xsl:variable name="viewport">
491     <xsl:choose>
492       <xsl:when test="$ignore.image.scaling != 0">0</xsl:when>
493       <xsl:when test="local-name(.) = 'inlinegraphic'
494                       or ancestor::inlinemediaobject
495                       or ancestor::inlineequation">0</xsl:when>
496       <xsl:otherwise>
497         <xsl:value-of select="$make.graphic.viewport"/>
498       </xsl:otherwise>
499     </xsl:choose>
500   </xsl:variable>
501
502 <!--
503   <xsl:message>=====================================
504 scale: <xsl:value-of select="$scale"/>, <xsl:value-of select="$scalefit"/>
505 @contentwidth <xsl:value-of select="@contentwidth"/>
506 $contentwidth <xsl:value-of select="$contentwidth"/>
507 scaled.contentwidth: <xsl:value-of select="$scaled.contentwidth"/>
508 @width: <xsl:value-of select="@width"/>
509 width: <xsl:value-of select="$width"/>
510 html.width: <xsl:value-of select="$html.width"/>
511 @contentdepth <xsl:value-of select="@contentdepth"/>
512 $contentdepth <xsl:value-of select="$contentdepth"/>
513 scaled.contentdepth: <xsl:value-of select="$scaled.contentdepth"/>
514 @depth: <xsl:value-of select="@depth"/>
515 depth: <xsl:value-of select="$depth"/>
516 html.depth: <xsl:value-of select="$html.depth"/>
517 align: <xsl:value-of select="@align"/>
518 valign: <xsl:value-of select="@valign"/></xsl:message>
519 -->
520
521   <xsl:variable name="scaled"
522               select="@width|@depth|@contentwidth|@contentdepth
523                         |@scale|@scalefit"/>
524
525   <xsl:variable name="img">
526     <xsl:choose>
527       <xsl:when test="@format = 'SVG'">
528         <object type="image/svg+xml">
529           <xsl:attribute name="data">
530             <xsl:choose>
531               <xsl:when test="$img.src.path != '' and
532                            $tag = 'img' and
533                            not(starts-with($output_filename, '/')) and
534                            not(contains($output_filename, '://'))">
535                 <xsl:value-of select="$img.src.path"/>
536               </xsl:when>
537            </xsl:choose>
538            <xsl:value-of select="$output_filename"/>
539           </xsl:attribute>
540           <xsl:call-template name="process.image.attributes">
541             <!--xsl:with-param name="alt" select="$alt"/ there's no alt here-->
542             <xsl:with-param name="html.depth" select="$html.depth"/>
543             <xsl:with-param name="html.width" select="$html.width"/>
544             <xsl:with-param name="longdesc" select="$longdesc"/>
545             <xsl:with-param name="scale" select="$scale"/>
546             <xsl:with-param name="scalefit" select="$scalefit"/>
547             <xsl:with-param name="scaled.contentdepth" select="$scaled.contentdepth"/>
548             <xsl:with-param name="scaled.contentwidth" select="$scaled.contentwidth"/>
549             <xsl:with-param name="viewport" select="$viewport"/>
550           </xsl:call-template>
551           <xsl:if test="@align">
552             <xsl:attribute name="align">
553                 <xsl:choose>
554                   <xsl:when test="@align = 'center'">middle</xsl:when>
555                   <xsl:otherwise>
556                     <xsl:value-of select="@align"/>
557                   </xsl:otherwise>
558                 </xsl:choose>
559             </xsl:attribute>
560           </xsl:if>
561           <xsl:if test="$use.embed.for.svg != 0">
562             <embed type="image/svg+xml">
563               <xsl:attribute name="src">
564                 <xsl:choose>
565                   <xsl:when test="$img.src.path != '' and
566                                   $tag = 'img' and
567                                   not(starts-with($output_filename, '/')) and
568                                   not(contains($output_filename, '://'))">
569                     <xsl:value-of select="$img.src.path"/>
570                   </xsl:when>
571                 </xsl:choose>
572                 <xsl:value-of select="$output_filename"/>
573               </xsl:attribute>
574               <xsl:call-template name="process.image.attributes">
575                 <!--xsl:with-param name="alt" select="$alt"/ there's no alt here -->
576                 <xsl:with-param name="html.depth" select="$html.depth"/>
577                 <xsl:with-param name="html.width" select="$html.width"/>
578                 <xsl:with-param name="longdesc" select="$longdesc"/>
579                 <xsl:with-param name="scale" select="$scale"/>
580                 <xsl:with-param name="scalefit" select="$scalefit"/>
581                 <xsl:with-param name="scaled.contentdepth" select="$scaled.contentdepth"/>
582                 <xsl:with-param name="scaled.contentwidth" select="$scaled.contentwidth"/>
583                 <xsl:with-param name="viewport" select="$viewport"/>
584               </xsl:call-template>
585             </embed>
586           </xsl:if>
587         </object>
588       </xsl:when>
589       <xsl:otherwise>
590         <xsl:element name="{$tag}">
591          <xsl:if test="$tag = 'img' and ../../self::imageobjectco">
592            <xsl:variable name="mapname">
593              <xsl:call-template name="object.id">
594                <xsl:with-param name="object" select="../../areaspec"/>
595              </xsl:call-template>
596            </xsl:variable>
597            <xsl:choose>
598              <xsl:when test="$scaled">
599               <!-- It might be possible to handle some scaling; needs -->
600               <!-- more investigation -->
601               <xsl:message>
602                 <xsl:text>Warning: imagemaps not supported </xsl:text>
603                 <xsl:text>on scaled images</xsl:text>
604               </xsl:message>
605              </xsl:when>
606              <xsl:otherwise>
607               <xsl:attribute name="border">0</xsl:attribute>
608               <xsl:attribute name="usemap">
609                 <xsl:value-of select="concat('#', $mapname)"/>
610               </xsl:attribute>
611              </xsl:otherwise>
612            </xsl:choose>
613          </xsl:if>
614
615           <xsl:attribute name="src">
616            <xsl:choose>
617              <xsl:when test="$img.src.path != '' and
618                            $tag = 'img' and
619                              not(starts-with($output_filename, '/')) and
620                            not(contains($output_filename, '://'))">
621                <xsl:value-of select="$img.src.path"/>
622              </xsl:when>
623            </xsl:choose>
624             <xsl:value-of select="$output_filename"/>
625           </xsl:attribute>
626
627           <xsl:if test="@align">
628             <xsl:attribute name="align">
629               <xsl:choose>
630                 <xsl:when test="@align = 'center'">middle</xsl:when>
631                 <xsl:otherwise>
632                   <xsl:value-of select="@align"/>
633                 </xsl:otherwise>
634               </xsl:choose>
635             </xsl:attribute>
636           </xsl:if>
637
638           <xsl:call-template name="process.image.attributes">
639             <xsl:with-param name="alt">
640               <xsl:choose>
641                 <xsl:when test="$alt != ''">
642                   <xsl:copy-of select="$alt"/>
643                 </xsl:when>
644                 <xsl:when test="ancestor::figure">
645                   <xsl:value-of select="normalize-space(ancestor::figure/title)"/>
646                 </xsl:when>
647               </xsl:choose>
648             </xsl:with-param>
649             <xsl:with-param name="html.depth" select="$html.depth"/>
650             <xsl:with-param name="html.width" select="$html.width"/>
651             <xsl:with-param name="longdesc" select="$longdesc"/>
652             <xsl:with-param name="scale" select="$scale"/>
653             <xsl:with-param name="scalefit" select="$scalefit"/>
654             <xsl:with-param name="scaled.contentdepth" select="$scaled.contentdepth"/>
655             <xsl:with-param name="scaled.contentwidth" select="$scaled.contentwidth"/>
656             <xsl:with-param name="viewport" select="$viewport"/>
657           </xsl:call-template>
658         </xsl:element>
659       </xsl:otherwise>
660     </xsl:choose>
661   </xsl:variable>
662
663   <xsl:variable name="bgcolor">
664     <xsl:call-template name="pi.dbhtml_background-color">
665       <xsl:with-param name="node" select=".."/>
666     </xsl:call-template>
667   </xsl:variable>
668
669   <xsl:variable name="use.viewport"
670                 select="$viewport != 0
671                         and ($html.width != ''
672                              or ($html.depth != '' and $depth-units != '%')
673                              or $bgcolor != ''
674                              or @valign)"/>
675
676   <xsl:choose>
677     <xsl:when test="$use.viewport">
678       <table border="0" summary="manufactured viewport for HTML img"
679              cellspacing="0" cellpadding="0">
680         <xsl:if test="$html.width != ''">
681           <xsl:attribute name="width">
682             <xsl:value-of select="$html.width"/>
683           </xsl:attribute>
684         </xsl:if>
685         <tr>
686           <xsl:if test="$html.depth != '' and $depth-units != '%'">
687             <!-- don't do this for percentages because browsers get confused -->
688             <xsl:choose>
689               <xsl:when test="$css.decoration != 0">
690                 <xsl:attribute name="style">
691                   <xsl:text>height: </xsl:text>
692                   <xsl:value-of select="$html.depth"/>
693                   <xsl:text>px</xsl:text>
694                 </xsl:attribute>
695               </xsl:when>
696               <xsl:otherwise>
697                 <xsl:attribute name="height">
698                   <xsl:value-of select="$html.depth"/>
699                 </xsl:attribute>
700               </xsl:otherwise>
701             </xsl:choose>
702           </xsl:if>
703           <td>
704             <xsl:if test="$bgcolor != ''">
705               <xsl:choose>
706                 <xsl:when test="$css.decoration != 0">
707                   <xsl:attribute name="style">
708                     <xsl:text>background-color: </xsl:text>
709                     <xsl:value-of select="$bgcolor"/>
710                   </xsl:attribute>
711                 </xsl:when>
712                 <xsl:otherwise>
713                   <xsl:attribute name="bgcolor">
714                     <xsl:value-of select="$bgcolor"/>
715                   </xsl:attribute>
716                 </xsl:otherwise>
717               </xsl:choose>
718             </xsl:if>
719             <xsl:if test="@align">
720               <xsl:attribute name="align">
721                 <xsl:value-of select="@align"/>
722               </xsl:attribute>
723             </xsl:if>
724             <xsl:if test="@valign">
725               <xsl:attribute name="valign">
726                 <xsl:value-of select="@valign"/>
727               </xsl:attribute>
728             </xsl:if>
729             <xsl:copy-of select="$img"/>
730           </td>
731         </tr>
732       </table>
733     </xsl:when>
734     <xsl:otherwise>
735       <xsl:copy-of select="$img"/>
736     </xsl:otherwise>
737   </xsl:choose>
738
739   <xsl:if test="$tag = 'img' and ../../self::imageobjectco and not($scaled)">
740     <xsl:variable name="mapname">
741       <xsl:call-template name="object.id">
742         <xsl:with-param name="object" select="../../areaspec"/>
743       </xsl:call-template>
744     </xsl:variable>
745
746     <map name="{$mapname}">
747       <xsl:for-each select="../../areaspec//area">
748         <xsl:variable name="units">
749           <xsl:choose>
750             <xsl:when test="@units = 'other' and @otherunits">
751               <xsl:value-of select="@otherunits"/>
752             </xsl:when>
753             <xsl:when test="@units">
754               <xsl:value-of select="@units"/>
755             </xsl:when>
756             <!-- areaspec|areaset/area -->
757             <xsl:when test="../@units = 'other' and ../@otherunits">
758               <xsl:value-of select="../@otherunits"/>
759             </xsl:when>
760             <xsl:when test="../@units">
761               <xsl:value-of select="../@units"/>
762             </xsl:when>
763             <!-- areaspec/areaset/area -->
764             <xsl:when test="../../@units = 'other' and ../../@otherunits">
765               <xsl:value-of select="../@otherunits"/>
766             </xsl:when>
767             <xsl:when test="../../@units">
768               <xsl:value-of select="../../@units"/>
769             </xsl:when>
770             <xsl:otherwise>calspair</xsl:otherwise>
771           </xsl:choose>
772         </xsl:variable>
773  
774         <xsl:choose>
775           <xsl:when test="$units = 'calspair' or
776                           $units = 'imagemap'">
777             <xsl:variable name="coords" select="normalize-space(@coords)"/>
778
779             <area shape="rect">
780               <xsl:variable name="linkends">
781                 <xsl:choose>
782                   <xsl:when test="@linkends">
783                     <xsl:value-of select="normalize-space(@linkends)"/>
784                   </xsl:when>
785                   <xsl:otherwise>
786                     <xsl:value-of select="normalize-space(../@linkends)"/>
787                   </xsl:otherwise>
788                 </xsl:choose>
789               </xsl:variable>
790  
791               <xsl:variable name="href">
792                 <xsl:choose>
793                   <xsl:when test="@xlink:href">
794                     <xsl:value-of select="@xlink:href"/>
795                   </xsl:when>
796                   <xsl:otherwise>
797                     <xsl:value-of select="../@xlink:href"/>
798                   </xsl:otherwise>
799                 </xsl:choose>
800               </xsl:variable>
801  
802               <xsl:choose>
803                 <xsl:when test="$linkends != ''">
804                   <xsl:variable name="linkend">
805                     <xsl:choose>
806                       <xsl:when test="contains($linkends, ' ')">
807                         <xsl:value-of select="substring-before($linkends, ' ')"/>
808                       </xsl:when>
809                       <xsl:otherwise>
810                         <xsl:value-of select="$linkends"/>
811                       </xsl:otherwise>
812                     </xsl:choose>
813                   </xsl:variable>
814                   
815                   <xsl:variable name="target" select="key('id', $linkend)[1]"/>
816                  
817                   <xsl:if test="$target">
818                     <xsl:attribute name="href">
819                       <xsl:call-template name="href.target">
820                         <xsl:with-param name="object" select="$target"/>
821                       </xsl:call-template>
822                     </xsl:attribute>
823                   </xsl:if>
824                 </xsl:when>
825                 <xsl:when test="$href != ''">
826                   <xsl:attribute name="href">
827                     <xsl:value-of select="$href"/>
828                   </xsl:attribute>
829                 </xsl:when>
830               </xsl:choose>
831  
832               <xsl:if test="alt">
833                 <xsl:attribute name="alt">
834                   <xsl:value-of select="alt[1]"/>
835                 </xsl:attribute>
836               </xsl:if>
837  
838               <xsl:attribute name="coords">
839                 <xsl:choose>
840                   <xsl:when test="$units = 'calspair'">
841
842                     <xsl:variable name="p1"
843                                 select="substring-before($coords, ' ')"/>
844                     <xsl:variable name="p2"
845                                 select="substring-after($coords, ' ')"/>
846          
847                     <xsl:variable name="x1" select="substring-before($p1,',')"/>
848                     <xsl:variable name="y1" select="substring-after($p1,',')"/>
849                     <xsl:variable name="x2" select="substring-before($p2,',')"/>
850                     <xsl:variable name="y2" select="substring-after($p2,',')"/>
851          
852                     <xsl:variable name="x1p" select="$x1 div 100.0"/>
853                     <xsl:variable name="y1p" select="$y1 div 100.0"/>
854                     <xsl:variable name="x2p" select="$x2 div 100.0"/>
855                     <xsl:variable name="y2p" select="$y2 div 100.0"/>
856          
857          <!--
858                     <xsl:message>
859                       <xsl:text>units: </xsl:text>
860                       <xsl:value-of select="$units"/>
861                       <xsl:text> </xsl:text>
862                       <xsl:value-of select="$x1p"/><xsl:text>, </xsl:text>
863                       <xsl:value-of select="$y1p"/><xsl:text>, </xsl:text>
864                       <xsl:value-of select="$x2p"/><xsl:text>, </xsl:text>
865                       <xsl:value-of select="$y2p"/><xsl:text>, </xsl:text>
866                     </xsl:message>
867          
868                     <xsl:message>
869                       <xsl:text>      </xsl:text>
870                       <xsl:value-of select="$intrinsicwidth"/>
871                       <xsl:text>, </xsl:text>
872                       <xsl:value-of select="$intrinsicdepth"/>
873                     </xsl:message>
874          
875                     <xsl:message>
876                       <xsl:text>      </xsl:text>
877                       <xsl:value-of select="$units"/>
878                       <xsl:text> </xsl:text>
879                       <xsl:value-of 
880                             select="round($x1p * $intrinsicwidth div 100.0)"/>
881                       <xsl:text>,</xsl:text>
882                       <xsl:value-of select="round($intrinsicdepth
883                                        - ($y2p * $intrinsicdepth div 100.0))"/>
884                       <xsl:text>,</xsl:text>
885                       <xsl:value-of select="round($x2p * 
886                                             $intrinsicwidth div 100.0)"/>
887                       <xsl:text>,</xsl:text>
888                       <xsl:value-of select="round($intrinsicdepth
889                                        - ($y1p * $intrinsicdepth div 100.0))"/>
890                     </xsl:message>
891          -->
892                     <xsl:value-of 
893                              select="round($x1p * $intrinsicwidth div 100.0)"/>
894                     <xsl:text>,</xsl:text>
895                     <xsl:value-of select="round($intrinsicdepth
896                                         - ($y2p * $intrinsicdepth div 100.0))"/>
897                     <xsl:text>,</xsl:text>
898                     <xsl:value-of 
899                              select="round($x2p * $intrinsicwidth div 100.0)"/>
900                     <xsl:text>,</xsl:text>
901                     <xsl:value-of select="round($intrinsicdepth
902                                       - ($y1p * $intrinsicdepth div 100.0))"/>
903                   </xsl:when>
904                   <xsl:otherwise>
905                     <xsl:copy-of select="$coords"/>
906                   </xsl:otherwise>
907                 </xsl:choose>
908               </xsl:attribute>
909             </area>
910           </xsl:when>
911           <xsl:otherwise>
912             <xsl:message>
913               <xsl:text>Warning: only calspair or </xsl:text>
914               <xsl:text>otherunits='imagemap' supported </xsl:text>
915               <xsl:text>in imageobjectco</xsl:text>
916             </xsl:message>
917           </xsl:otherwise>
918         </xsl:choose>
919       </xsl:for-each>
920     </map>
921   </xsl:if>
922 </xsl:template>
923
924 <xsl:template name="process.image.attributes">
925   <xsl:param name="alt"/>
926   <xsl:param name="html.width"/>
927   <xsl:param name="html.depth"/>
928   <xsl:param name="longdesc"/>
929   <xsl:param name="scale"/>
930   <xsl:param name="scalefit"/>
931   <xsl:param name="scaled.contentdepth"/>
932   <xsl:param name="scaled.contentwidth"/>
933   <xsl:param name="viewport"/>
934
935   <xsl:choose>
936     <xsl:when test="@contentwidth or @contentdepth">
937       <!-- ignore @width/@depth, @scale, and @scalefit if specified -->
938       <xsl:if test="@contentwidth and $scaled.contentwidth != ''">
939         <xsl:attribute name="width">
940           <xsl:value-of select="$scaled.contentwidth"/>
941         </xsl:attribute>
942       </xsl:if>
943       <xsl:if test="@contentdepth and $scaled.contentdepth != ''">
944         <xsl:attribute name="height">
945           <xsl:value-of select="$scaled.contentdepth"/>
946         </xsl:attribute>
947       </xsl:if>
948     </xsl:when>
949
950     <xsl:when test="number($scale) != 1.0">
951       <!-- scaling is always uniform, so we only have to specify one dimension -->
952       <!-- ignore @scalefit if specified -->
953       <xsl:attribute name="width">
954         <xsl:value-of select="$scaled.contentwidth"/>
955       </xsl:attribute>
956     </xsl:when>
957
958     <xsl:when test="$scalefit != 0">
959       <xsl:choose>
960         <xsl:when test="contains($html.width, '%')">
961           <xsl:choose>
962             <xsl:when test="$viewport != 0">
963               <!-- The *viewport* will be scaled, so use 100% here! -->
964               <xsl:attribute name="width">
965                 <xsl:value-of select="'100%'"/>
966               </xsl:attribute>
967             </xsl:when>
968             <xsl:otherwise>
969               <xsl:attribute name="width">
970                 <xsl:value-of select="$html.width"/>
971               </xsl:attribute>
972             </xsl:otherwise>
973           </xsl:choose>
974         </xsl:when>
975
976         <xsl:when test="contains($html.depth, '%')">
977           <!-- HTML doesn't deal with this case very well...do nothing -->
978         </xsl:when>
979
980         <xsl:when test="$scaled.contentwidth != '' and $html.width != ''
981                         and $scaled.contentdepth != '' and $html.depth != ''">
982           <!-- scalefit should not be anamorphic; figure out which direction -->
983           <!-- has the limiting scale factor and scale in that direction -->
984           <xsl:choose>
985             <xsl:when test="$html.width div $scaled.contentwidth &gt;
986                             $html.depth div $scaled.contentdepth">
987               <xsl:attribute name="height">
988                 <xsl:value-of select="$html.depth"/>
989               </xsl:attribute>
990             </xsl:when>
991             <xsl:otherwise>
992               <xsl:attribute name="width">
993                 <xsl:value-of select="$html.width"/>
994               </xsl:attribute>
995             </xsl:otherwise>
996           </xsl:choose>
997         </xsl:when>
998
999         <xsl:when test="$scaled.contentwidth != '' and $html.width != ''">
1000           <xsl:attribute name="width">
1001             <xsl:value-of select="$html.width"/>
1002           </xsl:attribute>
1003         </xsl:when>
1004
1005         <xsl:when test="$scaled.contentdepth != '' and $html.depth != ''">
1006           <xsl:attribute name="height">
1007             <xsl:value-of select="$html.depth"/>
1008           </xsl:attribute>
1009         </xsl:when>
1010       </xsl:choose>
1011     </xsl:when>
1012   </xsl:choose>
1013
1014   <xsl:if test="$alt != ''">
1015     <xsl:attribute name="alt">
1016       <xsl:value-of select="normalize-space($alt)"/>
1017     </xsl:attribute>
1018   </xsl:if>
1019
1020   <xsl:if test="$longdesc != ''">
1021     <xsl:attribute name="longdesc">
1022       <xsl:value-of select="$longdesc"/>
1023     </xsl:attribute>
1024   </xsl:if>
1025
1026   <xsl:if test="@align and $viewport = 0">
1027     <xsl:attribute name="align">
1028       <xsl:choose>
1029         <xsl:when test="@align = 'center'">middle</xsl:when>
1030         <xsl:otherwise>
1031           <xsl:value-of select="@align"/>
1032         </xsl:otherwise>
1033       </xsl:choose>
1034     </xsl:attribute>
1035   </xsl:if>
1036 </xsl:template>
1037
1038 <!-- ==================================================================== -->
1039
1040 <xsl:template match="graphic">
1041   <xsl:choose>
1042     <xsl:when test="parent::inlineequation">
1043       <xsl:call-template name="anchor"/>
1044       <xsl:call-template name="process.image"/>
1045     </xsl:when>
1046     <xsl:otherwise>
1047       <div>
1048         <xsl:if test="@align">
1049           <xsl:attribute name="align">
1050             <xsl:value-of select="@align"/>
1051           </xsl:attribute>
1052         </xsl:if>
1053         <xsl:call-template name="anchor"/>
1054         <xsl:call-template name="process.image"/>
1055       </div>
1056     </xsl:otherwise>
1057   </xsl:choose>
1058 </xsl:template>
1059
1060 <xsl:template match="inlinegraphic">
1061   <xsl:variable name="filename">
1062     <xsl:choose>
1063       <xsl:when test="@entityref">
1064         <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
1065       </xsl:when>
1066       <xsl:otherwise>
1067         <xsl:apply-templates select="@fileref"/>
1068       </xsl:otherwise>
1069     </xsl:choose>
1070   </xsl:variable>
1071
1072   <xsl:call-template name="anchor"/>
1073
1074   <xsl:choose>
1075     <xsl:when test="@format='linespecific'">
1076       <xsl:choose>
1077         <xsl:when test="$use.extensions != '0'
1078                         and $textinsert.extension != '0'">
1079           <xsl:choose>
1080             <xsl:when test="element-available('stext:insertfile')">
1081               <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
1082             </xsl:when>
1083             <xsl:when test="element-available('xtext:insertfile')">
1084               <xtext:insertfile href="{$filename}"/>
1085             </xsl:when>
1086             <xsl:otherwise>
1087               <xsl:message terminate="yes">
1088                 <xsl:text>No insertfile extension available.</xsl:text>
1089               </xsl:message>
1090             </xsl:otherwise>
1091           </xsl:choose>
1092         </xsl:when>
1093         <xsl:otherwise>
1094           <xsl:message terminate="yes">
1095             <xsl:text>Cannot insert </xsl:text><xsl:value-of select="$filename"/>
1096             <xsl:text>. Check use.extensions and textinsert.extension parameters.</xsl:text> 
1097           </xsl:message>
1098         </xsl:otherwise>
1099       </xsl:choose>
1100     </xsl:when>
1101     <xsl:otherwise>
1102       <xsl:call-template name="process.image"/>
1103     </xsl:otherwise>
1104   </xsl:choose>
1105 </xsl:template>
1106
1107 <!-- ==================================================================== -->
1108
1109 <xsl:template match="mediaobject|mediaobjectco">
1110
1111   <xsl:variable name="olist" select="imageobject|imageobjectco
1112                      |videoobject|audioobject
1113                      |textobject"/>
1114
1115   <xsl:variable name="object.index">
1116     <xsl:call-template name="select.mediaobject.index">
1117       <xsl:with-param name="olist" select="$olist"/>
1118       <xsl:with-param name="count" select="1"/>
1119     </xsl:call-template>
1120   </xsl:variable>
1121
1122   <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1123
1124   <xsl:variable name="align">
1125     <xsl:value-of select="$object/descendant::imagedata[@align][1]/@align"/>
1126   </xsl:variable>
1127
1128   <div>
1129     <xsl:apply-templates select="." mode="common.html.attributes"/>
1130     <xsl:if test="$align != '' ">
1131       <xsl:attribute name="align">
1132         <xsl:value-of select="$align"/>
1133       </xsl:attribute>
1134     </xsl:if>
1135     <xsl:call-template name="anchor"/>
1136
1137     <xsl:apply-templates select="$object"/>
1138     <xsl:apply-templates select="caption"/>
1139   </div>
1140 </xsl:template>
1141
1142 <xsl:template match="inlinemediaobject">
1143   <span>
1144     <xsl:apply-templates select="." mode="common.html.attributes"/>
1145     <xsl:call-template name="anchor"/>
1146     <xsl:call-template name="select.mediaobject"/>
1147   </span>
1148 </xsl:template>
1149
1150 <xsl:template match="programlisting/inlinemediaobject
1151                      |screen/inlinemediaobject" priority="2">
1152   <!-- the additional span causes problems in some cases -->
1153   <xsl:call-template name="select.mediaobject"/>
1154 </xsl:template>
1155
1156 <!-- ==================================================================== -->
1157
1158 <xsl:template match="imageobjectco">
1159   <xsl:call-template name="anchor"/>
1160   <xsl:apply-templates select="imageobject"/>
1161   <xsl:apply-templates select="calloutlist"/>
1162 </xsl:template>
1163
1164 <xsl:template match="imageobject">
1165   <xsl:apply-templates select="imagedata"/>
1166 </xsl:template>
1167
1168 <xsl:template match="imagedata">
1169   <xsl:variable name="filename">
1170     <xsl:call-template name="mediaobject.filename">
1171       <xsl:with-param name="object" select=".."/>
1172     </xsl:call-template>
1173   </xsl:variable>
1174
1175   <xsl:choose>
1176     <!-- Handle MathML and SVG markup in imagedata -->
1177     <xsl:when test="mml:*" xmlns:mml="http://www.w3.org/1998/Math/MathML">
1178       <xsl:apply-templates/>
1179     </xsl:when>
1180     
1181     <xsl:when test="svg:*" xmlns:svg="http://www.w3.org/2000/svg">
1182       <xsl:apply-templates/>
1183     </xsl:when>
1184
1185     <xsl:when test="@format='linespecific'">
1186       <xsl:choose>
1187         <xsl:when test="$use.extensions != '0'
1188                         and $textinsert.extension != '0'">
1189           <xsl:choose>
1190             <xsl:when test="element-available('stext:insertfile')">
1191               <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
1192             </xsl:when>
1193             <xsl:when test="element-available('xtext:insertfile')">
1194               <xtext:insertfile href="{$filename}"/>
1195             </xsl:when>
1196             <xsl:otherwise>
1197               <xsl:message terminate="yes">
1198                 <xsl:text>No insertfile extension available.</xsl:text>
1199               </xsl:message>
1200             </xsl:otherwise>
1201           </xsl:choose>
1202         </xsl:when>
1203         <xsl:otherwise>
1204           <a xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"
1205              href="{$filename}"/>
1206         </xsl:otherwise>
1207       </xsl:choose>
1208     </xsl:when>
1209     <xsl:otherwise>
1210       <xsl:variable name="longdesc.uri">
1211         <xsl:call-template name="longdesc.uri">
1212           <xsl:with-param name="mediaobject"
1213                           select="ancestor::imageobject/parent::*"/>
1214         </xsl:call-template>
1215       </xsl:variable>
1216
1217       <xsl:variable name="phrases"
1218                     select="ancestor::mediaobject/textobject[phrase]
1219                             |ancestor::inlinemediaobject/textobject[phrase]
1220                             |ancestor::mediaobjectco/textobject[phrase]"/>
1221
1222       <xsl:call-template name="process.image">
1223         <xsl:with-param name="alt">
1224           <xsl:apply-templates select="$phrases[not(@role) or @role!='tex'][1]"/>
1225         </xsl:with-param>
1226         <xsl:with-param name="longdesc">
1227           <xsl:call-template name="write.longdesc">
1228             <xsl:with-param name="mediaobject"
1229                             select="ancestor::imageobject/parent::*"/>
1230           </xsl:call-template>
1231         </xsl:with-param>
1232       </xsl:call-template>
1233
1234       <xsl:if test="$html.longdesc != 0 and $html.longdesc.link != 0
1235                     and ancestor::imageobject/parent::*/textobject[not(phrase)]">
1236         <xsl:call-template name="longdesc.link">
1237           <xsl:with-param name="longdesc.uri" select="$longdesc.uri"/>
1238         </xsl:call-template>
1239       </xsl:if>
1240     </xsl:otherwise>
1241   </xsl:choose>
1242 </xsl:template>
1243
1244 <!-- ==================================================================== -->
1245
1246 <xsl:template name="longdesc.uri">
1247   <xsl:param name="mediaobject" select="."/>
1248   <xsl:if test="$html.longdesc">
1249     <xsl:if test="$mediaobject/textobject[not(phrase)]">
1250       <xsl:variable name="dbhtml.dir">
1251         <xsl:call-template name="dbhtml-dir"/>
1252       </xsl:variable>
1253       <xsl:variable name="filename">
1254         <xsl:call-template name="make-relative-filename">
1255           <xsl:with-param name="base.dir">
1256             <xsl:choose>
1257               <xsl:when test="$dbhtml.dir != ''">
1258                 <xsl:value-of select="$dbhtml.dir"/>
1259               </xsl:when>
1260               <xsl:otherwise>
1261                 <xsl:value-of select="$base.dir"/>
1262               </xsl:otherwise>
1263             </xsl:choose>
1264           </xsl:with-param>
1265           <xsl:with-param name="base.name">
1266             <xsl:choose>
1267               <xsl:when test="
1268                 $mediaobject/@*[local-name() = 'id']
1269                 and not($use.id.as.filename = 0)">
1270                 <!-- * if this mediaobject has an ID, then we use the -->
1271                 <!-- * value of that ID as basename for the "longdesc" -->
1272                 <!-- * file (that is, without prepending an "ld-" too it) -->
1273                 <xsl:value-of select="$mediaobject/@*[local-name() = 'id']"/>
1274                 <xsl:value-of select="$html.ext"/>
1275               </xsl:when>
1276               <xsl:otherwise>
1277                 <!-- * otherwise, if this mediaobject does not have an -->
1278                 <!-- * ID, then we generate an ID... -->
1279                 <xsl:variable name="image-id">
1280                   <xsl:call-template name="object.id">
1281                     <xsl:with-param name="object" select="$mediaobject"/>
1282                   </xsl:call-template>
1283                 </xsl:variable>
1284                 <!-- * ...and then we take that generated ID, prepend an -->
1285                 <!-- * "ld-" to it, and use that as the basename for the file -->
1286                 <xsl:value-of select="concat('ld-',$image-id,$html.ext)"/>
1287               </xsl:otherwise>
1288             </xsl:choose>
1289           </xsl:with-param>
1290         </xsl:call-template>
1291       </xsl:variable>
1292
1293       <xsl:value-of select="$filename"/>
1294     </xsl:if>
1295   </xsl:if>
1296 </xsl:template>
1297
1298 <xsl:template name="write.longdesc">
1299   <xsl:param name="mediaobject" select="."/>
1300   <xsl:if test="$html.longdesc != 0 and $mediaobject/textobject[not(phrase)]">
1301     <xsl:variable name="filename">
1302       <xsl:call-template name="longdesc.uri">
1303         <xsl:with-param name="mediaobject" select="$mediaobject"/>
1304       </xsl:call-template>
1305     </xsl:variable>
1306
1307     <xsl:value-of select="$filename"/>
1308
1309     <xsl:call-template name="write.chunk">
1310       <xsl:with-param name="filename" select="$filename"/>
1311       <xsl:with-param name="quiet" select="$chunk.quietly"/>
1312       <xsl:with-param name="content">
1313       <xsl:call-template name="user.preroot"/>
1314         <html>
1315           <head>
1316             <xsl:call-template name="system.head.content"/>
1317             <xsl:call-template name="head.content">
1318               <xsl:with-param name="title" select="'Long Description'"/>
1319             </xsl:call-template>
1320             <xsl:call-template name="user.head.content"/>
1321           </head>
1322           <body>
1323             <xsl:call-template name="body.attributes"/>
1324             <xsl:for-each select="$mediaobject/textobject[not(phrase)]">
1325               <xsl:apply-templates select="./*"/>
1326             </xsl:for-each>
1327           </body>
1328         </html>
1329         <xsl:value-of select="$chunk.append"/>
1330       </xsl:with-param>
1331     </xsl:call-template>
1332   </xsl:if>
1333 </xsl:template>
1334
1335 <xsl:template name="longdesc.link">
1336   <xsl:param name="longdesc.uri" select="''"/>
1337
1338   <xsl:variable name="this.uri">
1339     <xsl:call-template name="make-relative-filename">
1340       <xsl:with-param name="base.dir" select="$base.dir"/>
1341       <xsl:with-param name="base.name">
1342         <xsl:call-template name="href.target.uri"/>
1343       </xsl:with-param>
1344     </xsl:call-template>
1345   </xsl:variable>
1346
1347   <xsl:variable name="href.to">
1348     <xsl:call-template name="trim.common.uri.paths">
1349       <xsl:with-param name="uriA" select="$longdesc.uri"/>
1350       <xsl:with-param name="uriB" select="$this.uri"/>
1351       <xsl:with-param name="return" select="'A'"/>
1352     </xsl:call-template>
1353   </xsl:variable>
1354
1355   <div class="longdesc-link" align="{$direction.align.end}">
1356     <br clear="all"/>
1357     <span class="longdesc-link">
1358       <xsl:text>[</xsl:text>
1359       <a href="{$href.to}" target="longdesc">D</a>
1360       <xsl:text>]</xsl:text>
1361     </span>
1362   </div>
1363 </xsl:template>
1364
1365 <!-- ==================================================================== -->
1366
1367 <xsl:template match="videoobject">
1368   <xsl:apply-templates select="videodata"/>
1369 </xsl:template>
1370
1371 <xsl:template match="videodata">
1372   <xsl:call-template name="process.image">
1373     <xsl:with-param name="tag" select="'embed'"/>
1374     <xsl:with-param name="alt">
1375       <xsl:apply-templates select="(../../textobject/phrase)[1]"/>
1376     </xsl:with-param>
1377   </xsl:call-template>
1378 </xsl:template>
1379
1380 <!-- ==================================================================== -->
1381
1382 <xsl:template match="audioobject">
1383   <xsl:apply-templates select="audiodata"/>
1384 </xsl:template>
1385
1386 <xsl:template match="audiodata">
1387   <xsl:call-template name="process.image">
1388     <xsl:with-param name="tag" select="'embed'"/>
1389     <xsl:with-param name="alt">
1390       <xsl:apply-templates select="(../../textobject/phrase)[1]"/>
1391     </xsl:with-param>
1392   </xsl:call-template>
1393 </xsl:template>
1394
1395 <!-- ==================================================================== -->
1396
1397 <xsl:template match="textobject">
1398   <xsl:apply-templates/>
1399 </xsl:template>
1400
1401 <xsl:template match="textdata">
1402   <xsl:variable name="filename">
1403     <xsl:choose>
1404       <xsl:when test="@entityref">
1405         <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
1406       </xsl:when>
1407       <xsl:otherwise>
1408         <xsl:apply-templates select="@fileref"/>
1409       </xsl:otherwise>
1410     </xsl:choose>
1411   </xsl:variable>
1412
1413   <xsl:variable name="encoding">
1414     <xsl:choose>
1415       <xsl:when test="@encoding">
1416         <xsl:value-of select="@encoding"/>
1417       </xsl:when>
1418       <xsl:otherwise>
1419         <xsl:value-of select="$textdata.default.encoding"/>
1420       </xsl:otherwise>
1421     </xsl:choose>
1422   </xsl:variable>
1423
1424   <xsl:choose>
1425     <xsl:when test="$use.extensions != '0'
1426                     and $textinsert.extension != '0'">
1427       <xsl:choose>
1428         <xsl:when test="element-available('stext:insertfile')">
1429           <stext:insertfile href="{$filename}" encoding="{$encoding}"/>
1430         </xsl:when>
1431         <xsl:when test="element-available('xtext:insertfile')">
1432           <xtext:insertfile href="{$filename}"/>
1433         </xsl:when>
1434         <xsl:otherwise>
1435           <xsl:message terminate="yes">
1436             <xsl:text>No insertfile extension available.</xsl:text>
1437           </xsl:message>
1438         </xsl:otherwise>
1439       </xsl:choose>
1440     </xsl:when>
1441     <xsl:otherwise>
1442       <xsl:message terminate="yes">
1443         <xsl:text>Cannot insert </xsl:text><xsl:value-of select="$filename"/>
1444         <xsl:text>. Check use.extensions and textinsert.extension parameters.</xsl:text> 
1445       </xsl:message>
1446     </xsl:otherwise>
1447   </xsl:choose>
1448 </xsl:template>
1449
1450 <!-- ==================================================================== -->
1451
1452 <xsl:template match="caption">
1453   <div>
1454     <xsl:apply-templates select="." mode="common.html.attributes"/>
1455     <xsl:if test="@align = 'right' or @align = 'left' or @align='center'">
1456       <xsl:attribute name="align"><xsl:value-of
1457                          select="@align"/></xsl:attribute>
1458     </xsl:if>
1459     <xsl:apply-templates/>
1460   </div>
1461 </xsl:template>
1462
1463 <!-- ==================================================================== -->
1464 <!-- "Support" for SVG -->
1465
1466 <xsl:template match="svg:*" xmlns:svg="http://www.w3.org/2000/svg">
1467   <xsl:copy>
1468     <xsl:copy-of select="@*"/>
1469     <xsl:apply-templates/>
1470   </xsl:copy>
1471 </xsl:template>
1472
1473
1474 <!-- The following works sometimes, but needs to take into account
1475              1. When there is no /*/@xml:base
1476              2. When the chunks are going somewhere else
1477 <xsl:variable name="relpath">
1478   <xsl:call-template name="relative-uri">
1479     <xsl:with-param name="filename" select="@fileref"/>
1480   </xsl:call-template>
1481 </xsl:variable>
1482
1483 <xsl:choose>
1484   <xsl:when test="/*/@xml:base
1485                   and starts-with($relpath,/*/@xml:base)">
1486     <xsl:value-of select="substring-after($relpath,/*/@xml:base)"/>
1487   </xsl:when>
1488   <xsl:otherwise>
1489     <xsl:value-of select="@fileref"/>
1490   </xsl:otherwise>
1491 </xsl:choose>
1492 <xsl:value-of select="@fileref"/>
1493       </xsl:when>
1494 -->
1495 <!-- Resolve xml:base attributes -->
1496 <xsl:template match="@fileref">
1497   <!-- need a check for absolute urls -->
1498   <xsl:choose>
1499     <xsl:when test="contains(., ':')">
1500       <!-- it has a uri scheme so it is an absolute uri -->
1501       <xsl:value-of select="."/>
1502     </xsl:when>
1503     <xsl:when test="$keep.relative.image.uris != 0">
1504       <!-- leave it alone -->
1505       <xsl:value-of select="."/>
1506     </xsl:when>
1507     <xsl:otherwise>
1508       <!-- its a relative uri that needs xml:base processing -->
1509       <xsl:call-template name="relative-uri">
1510       </xsl:call-template>
1511     </xsl:otherwise>
1512   </xsl:choose>
1513 </xsl:template>
1514
1515 </xsl:stylesheet>