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