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