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