Imported Upstream version 1.77.1
[platform/upstream/docbook-xsl.git] / xhtml-1_1 / 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 9367 2012-05-12 23:46:35Z 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:apply-templates/>
43   </div>
44 </xsl:template>
45
46 <xsl:template match="screeninfo">
47 </xsl:template>
48
49 <!-- ==================================================================== -->
50
51 <xsl:template name="process.image">
52   <!-- When this template is called, the current node should be  -->
53   <!-- a graphic, inlinegraphic, imagedata, or videodata. All    -->
54   <!-- those elements have the same set of attributes, so we can -->
55   <!-- handle them all in one place.                             -->
56   <xsl:param name="tag" select="'img'"/>
57   <xsl:param name="alt"/>
58   <xsl:param name="longdesc"/>
59
60   <!-- The HTML img element only supports the notion of content-area
61        scaling; it doesn't support the distinction between a
62        content-area and a viewport-area, so we have to make some
63        compromises.
64
65        1. If only the content-area is specified, everything is fine.
66           (If you ask for a three inch image, that's what you'll get.)
67
68        2. If only the viewport-area is provided:
69           - If scalefit=1, treat it as both the content-area and
70             the viewport-area. (If you ask for an image in a five inch
71             area, we'll make the image five inches to fill that area.)
72           - If scalefit=0, ignore the viewport-area specification.
73
74           Note: this is not quite the right semantic and has the additional
75           problem that it can result in anamorphic scaling, which scalefit
76           should never cause.
77
78        3. If both the content-area and the viewport-area is specified
79           on a graphic element, ignore the viewport-area.
80           (If you ask for a three inch image in a five inch area, we'll assume
81            it's better to give you a three inch image in an unspecified area
82            than a five inch image in a five inch area.
83
84        Relative units also cause problems. As a general rule, the stylesheets
85        are operating too early and too loosely coupled with the rendering engine
86        to know things like the current font size or the actual dimensions of
87        an image. Therefore:
88
89        1. We use a fixed size for pixels, $pixels.per.inch
90
91        2. We use a fixed size for "em"s, $points.per.em
92
93        Percentages are problematic. In the following discussion, we speak
94        of width and contentwidth, but the same issues apply to depth and
95        contentdepth
96
97        1. A width of 50% means "half of the available space for the image."
98           That's fine. But note that in HTML, this is a dynamic property and
99           the image size will vary if the browser window is resized.
100
101        2. A contentwidth of 50% means "half of the actual image width". But
102           the stylesheets have no way to assess the image's actual size. Treating
103           this as a width of 50% is one possibility, but it produces behavior
104           (dynamic scaling) that seems entirely out of character with the
105           meaning.
106
107           Instead, the stylesheets define a $nominal.image.width
108           and convert percentages to actual values based on that nominal size.
109
110        Scale can be problematic. Scale applies to the contentwidth, so
111        a scale of 50 when a contentwidth is not specified is analagous to a
112        width of 50%. (If a contentwidth is specified, the scaling factor can
113        be applied to that value and no problem exists.)
114
115        If scale is specified but contentwidth is not supplied, the
116        nominal.image.width is used to calculate a base size
117        for scaling.
118
119        Warning: as a consequence of these decisions, unless the aspect ratio
120        of your image happens to be exactly the same as (nominal width / nominal height),
121        specifying contentwidth="50%" and contentdepth="50%" is NOT going to
122        scale the way you expect (or really, the way it should).
123
124        Don't do that. In fact, a percentage value is not recommended for content
125        size at all. Use scale instead.
126
127        Finally, align and valign are troublesome. Horizontal alignment is now
128        supported by wrapping the image in a <div align="{@align}"> (in block
129        contexts!). I can't think of anything (practical) to do about vertical
130        alignment.
131   -->
132
133   <xsl:variable name="width-units">
134     <xsl:choose>
135       <xsl:when test="$ignore.image.scaling != 0"/>
136       <xsl:when test="@width">
137         <xsl:call-template name="length-units">
138           <xsl:with-param name="length" select="@width"/>
139         </xsl:call-template>
140       </xsl:when>
141       <xsl:when test="not(@depth) and $default.image.width != ''">
142         <xsl:call-template name="length-units">
143           <xsl:with-param name="length" select="$default.image.width"/>
144         </xsl:call-template>
145       </xsl:when>
146     </xsl:choose>
147   </xsl:variable>
148
149   <xsl:variable name="width">
150     <xsl:choose>
151       <xsl:when test="$ignore.image.scaling != 0"/>
152       <xsl:when test="@width">
153         <xsl:choose>
154           <xsl:when test="$width-units = '%'">
155             <xsl:value-of select="@width"/>
156           </xsl:when>
157           <xsl:otherwise>
158             <xsl:call-template name="length-spec">
159               <xsl:with-param name="length" select="@width"/>
160             </xsl:call-template>
161           </xsl:otherwise>
162         </xsl:choose>
163       </xsl:when>
164       <xsl:when test="not(@depth) and $default.image.width != ''">
165         <xsl:value-of select="$default.image.width"/>
166       </xsl:when>
167     </xsl:choose>
168   </xsl:variable>
169
170   <xsl:variable name="scalefit">
171     <xsl:choose>
172       <xsl:when test="$ignore.image.scaling != 0">0</xsl:when>
173       <xsl:when test="@contentwidth or @contentdepth">0</xsl:when>
174       <xsl:when test="@scale">0</xsl:when>
175       <xsl:when test="@scalefit"><xsl:value-of select="@scalefit"/></xsl:when>
176       <xsl:when test="$width != '' or @depth">1</xsl:when>
177       <xsl:otherwise>0</xsl:otherwise>
178     </xsl:choose>
179   </xsl:variable>
180
181   <xsl:variable name="scale">
182     <xsl:choose>
183       <xsl:when test="$ignore.image.scaling != 0">1.0</xsl:when>
184       <xsl:when test="@contentwidth or @contentdepth">1.0</xsl:when>
185       <xsl:when test="@scale">
186         <xsl:value-of select="@scale div 100.0"/>
187       </xsl:when>
188       <xsl:otherwise>1.0</xsl:otherwise>
189     </xsl:choose>
190   </xsl:variable>
191
192   <xsl:variable name="filename">
193     <xsl:choose>
194       <xsl:when test="local-name(.) = 'graphic'                       or local-name(.) = 'inlinegraphic'">
195         <!-- handle legacy graphic and inlinegraphic by new template --> 
196         <xsl:call-template name="mediaobject.filename">
197           <xsl:with-param name="object" select="."/>
198         </xsl:call-template>
199       </xsl:when>
200       <xsl:otherwise>
201         <!-- imagedata, videodata, audiodata -->
202         <xsl:call-template name="mediaobject.filename">
203           <xsl:with-param name="object" select=".."/>
204         </xsl:call-template>
205       </xsl:otherwise>
206     </xsl:choose>
207   </xsl:variable>
208
209   <xsl:variable name="output_filename">
210     <xsl:choose>
211       <xsl:when test="@entityref">
212         <xsl:value-of select="$filename"/>
213       </xsl:when>
214       <!--
215         Moved test for $keep.relative.image.uris to template below:
216             <xsl:template match="@fileref">
217       -->
218       <xsl:otherwise>
219         <xsl:value-of select="$filename"/>
220       </xsl:otherwise>
221     </xsl:choose>
222   </xsl:variable>
223
224   <xsl:variable name="img.src.path.pi">
225     <xsl:call-template name="pi.dbhtml_img.src.path">
226       <xsl:with-param name="node" select=".."/>
227     </xsl:call-template>
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="$img.src.path != '' and                       $graphicsize.use.img.src.path != 0 and                       $tag = 'img' and                       not(starts-with($filename, '/')) and                       not(contains($filename, '://'))">
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="$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="$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="$img.src.path != '' and                            $tag = 'img' and       not(starts-with($output_filename, '/')) and       not(contains($output_filename, '://'))">
494                 <xsl:value-of select="$img.src.path"/>
495               </xsl:when>
496            </xsl:choose>
497            <xsl:value-of select="$output_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="style"><xsl:text>text-align: </xsl:text>
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="$img.src.path != '' and       $tag = 'img' and       not(starts-with($output_filename, '/')) and       not(contains($output_filename, '://'))">
525                     <xsl:value-of select="$img.src.path"/>
526                   </xsl:when>
527                 </xsl:choose>
528                 <xsl:value-of select="$output_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:element name="{$tag}" namespace="http://www.w3.org/1999/xhtml">
547          <xsl:if test="$tag = 'img' and ../../self::imageobjectco">
548            <xsl:variable name="mapname">
549              <xsl:call-template name="object.id">
550                <xsl:with-param name="object" select="../../areaspec"/>
551              </xsl:call-template>
552            </xsl:variable>
553            <xsl:choose>
554              <xsl:when test="$scaled">
555               <!-- It might be possible to handle some scaling; needs -->
556               <!-- more investigation -->
557               <xsl:message>
558                 <xsl:text>Warning: imagemaps not supported </xsl:text>
559                 <xsl:text>on scaled images</xsl:text>
560               </xsl:message>
561              </xsl:when>
562              <xsl:otherwise>
563               <xsl:attribute name="border">0</xsl:attribute>
564               <xsl:attribute name="usemap">
565                 <xsl:value-of select="concat('#', $mapname)"/>
566               </xsl:attribute>
567              </xsl:otherwise>
568            </xsl:choose>
569          </xsl:if>
570
571           <xsl:attribute name="src">
572            <xsl:choose>
573              <xsl:when test="$img.src.path != '' and                            $tag = 'img' and                              not(starts-with($output_filename, '/')) and                            not(contains($output_filename, '://'))">
574                <xsl:value-of select="$img.src.path"/>
575              </xsl:when>
576            </xsl:choose>
577             <xsl:value-of select="$output_filename"/>
578           </xsl:attribute>
579
580           <xsl:if test="@align">
581             <xsl:attribute name="style"><xsl:text>text-align: </xsl:text>
582               <xsl:choose>
583                 <xsl:when test="@align = 'center'">middle</xsl:when>
584                 <xsl:otherwise>
585                   <xsl:value-of select="@align"/>
586                 </xsl:otherwise>
587               </xsl:choose>
588             </xsl:attribute>
589           </xsl:if>
590
591           <xsl:call-template name="process.image.attributes">
592             <xsl:with-param name="alt">
593               <xsl:choose>
594                 <xsl:when test="$alt != ''">
595                   <xsl:copy-of select="$alt"/>
596                 </xsl:when>
597                 <xsl:when test="ancestor::figure">
598                   <xsl:value-of select="normalize-space(ancestor::figure/title)"/>
599                 </xsl:when>
600               </xsl:choose>
601             </xsl:with-param>
602             <xsl:with-param name="html.depth" select="$html.depth"/>
603             <xsl:with-param name="html.width" select="$html.width"/>
604             <xsl:with-param name="longdesc" select="$longdesc"/>
605             <xsl:with-param name="scale" select="$scale"/>
606             <xsl:with-param name="scalefit" select="$scalefit"/>
607             <xsl:with-param name="scaled.contentdepth" select="$scaled.contentdepth"/>
608             <xsl:with-param name="scaled.contentwidth" select="$scaled.contentwidth"/>
609             <xsl:with-param name="viewport" select="$viewport"/>
610           </xsl:call-template>
611         </xsl:element>
612       </xsl:otherwise>
613     </xsl:choose>
614   </xsl:variable>
615
616   <xsl:variable name="bgcolor">
617     <xsl:call-template name="pi.dbhtml_background-color">
618       <xsl:with-param name="node" select=".."/>
619     </xsl:call-template>
620   </xsl:variable>
621
622   <xsl:variable name="use.viewport" select="0"/>
623
624   <xsl:choose>
625     <xsl:when test="$use.viewport">
626       <table border="{$table.border.off}">
627         <xsl:if test="$div.element != 'section'">
628           <xsl:attribute name="summary">manufactured viewport for HTML img</xsl:attribute>
629         </xsl:if>
630         <xsl:if test="$css.decoration != ''">
631           <xsl:attribute name="style">cellpadding: 0; cellspacing: 0;</xsl:attribute>
632         </xsl:if>
633         <xsl:if test="$html.width != ''">
634           <xsl:attribute name="width">
635             <xsl:value-of select="$html.width"/>
636           </xsl:attribute>
637         </xsl:if>
638         <tr>
639           <xsl:if test="$html.depth != '' and $depth-units != '%'">
640             <!-- don't do this for percentages because browsers get confused -->
641             <xsl:choose>
642               <xsl:when test="$css.decoration != 0">
643                 <xsl:attribute name="style">
644                   <xsl:text>height: </xsl:text>
645                   <xsl:value-of select="$html.depth"/>
646                   <xsl:text>px</xsl:text>
647                 </xsl:attribute>
648               </xsl:when>
649               <xsl:otherwise>
650                 <xsl:attribute name="height">
651                   <xsl:value-of select="$html.depth"/>
652                 </xsl:attribute>
653               </xsl:otherwise>
654             </xsl:choose>
655           </xsl:if>
656           <td>
657             <xsl:if test="$bgcolor != ''">
658               <xsl:choose>
659                 <xsl:when test="$css.decoration != 0">
660                   <xsl:attribute name="style">
661                     <xsl:text>background-color: </xsl:text>
662                     <xsl:value-of select="$bgcolor"/>
663                   </xsl:attribute>
664                 </xsl:when>
665                 <xsl:otherwise>
666                   <xsl:attribute name="style"><xsl:text>background-color: </xsl:text>
667                     <xsl:value-of select="$bgcolor"/>
668                   </xsl:attribute>
669                 </xsl:otherwise>
670               </xsl:choose>
671             </xsl:if>
672             <xsl:if test="@align">
673               <xsl:attribute name="style"><xsl:text>text-align: </xsl:text>
674                 <xsl:value-of select="@align"/>
675               </xsl:attribute>
676             </xsl:if>
677             <xsl:if test="@valign">
678               <xsl:attribute name="valign">
679                 <xsl:value-of select="@valign"/>
680               </xsl:attribute>
681             </xsl:if>
682             <xsl:copy-of select="$img"/>
683           </td>
684         </tr>
685       </table>
686     </xsl:when>
687     <xsl:otherwise>
688       <xsl:copy-of select="$img"/>
689     </xsl:otherwise>
690   </xsl:choose>
691
692   <xsl:if test="$tag = 'img' and ../../self::imageobjectco and not($scaled)">
693     <xsl:variable name="mapname">
694       <xsl:call-template name="object.id">
695         <xsl:with-param name="object" select="../../areaspec"/>
696       </xsl:call-template>
697     </xsl:variable>
698
699     <map name="{$mapname}">
700       <xsl:for-each select="../../areaspec//area">
701         <xsl:variable name="units">
702           <xsl:choose>
703             <xsl:when test="@units = 'other' and @otherunits">
704               <xsl:value-of select="@otherunits"/>
705             </xsl:when>
706             <xsl:when test="@units">
707               <xsl:value-of select="@units"/>
708             </xsl:when>
709             <!-- areaspec|areaset/area -->
710             <xsl:when test="../@units = 'other' and ../@otherunits">
711               <xsl:value-of select="../@otherunits"/>
712             </xsl:when>
713             <xsl:when test="../@units">
714               <xsl:value-of select="../@units"/>
715             </xsl:when>
716             <!-- areaspec/areaset/area -->
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             <xsl:otherwise>calspair</xsl:otherwise>
724           </xsl:choose>
725         </xsl:variable>
726  
727         <xsl:choose>
728           <xsl:when test="$units = 'calspair' or                           $units = 'imagemap'">
729             <xsl:variable name="coords" select="normalize-space(@coords)"/>
730
731             <area shape="rect">
732               <xsl:variable name="linkends">
733                 <xsl:choose>
734                   <xsl:when test="@linkends">
735                     <xsl:value-of select="normalize-space(@linkends)"/>
736                   </xsl:when>
737                   <xsl:otherwise>
738                     <xsl:value-of select="normalize-space(../@linkends)"/>
739                   </xsl:otherwise>
740                 </xsl:choose>
741               </xsl:variable>
742  
743               <xsl:variable name="href">
744                 <xsl:choose>
745                   <xsl:when test="@xlink:href">
746                     <xsl:value-of select="@xlink:href"/>
747                   </xsl:when>
748                   <xsl:otherwise>
749                     <xsl:value-of select="../@xlink:href"/>
750                   </xsl:otherwise>
751                 </xsl:choose>
752               </xsl:variable>
753  
754               <xsl:choose>
755                 <xsl:when test="$linkends != ''">
756                   <xsl:variable name="linkend">
757                     <xsl:choose>
758                       <xsl:when test="contains($linkends, ' ')">
759                         <xsl:value-of select="substring-before($linkends, ' ')"/>
760                       </xsl:when>
761                       <xsl:otherwise>
762                         <xsl:value-of select="$linkends"/>
763                       </xsl:otherwise>
764                     </xsl:choose>
765                   </xsl:variable>
766                   
767                   <xsl:variable name="target" select="key('id', $linkend)[1]"/>
768                  
769                   <xsl:if test="$target">
770                     <xsl:attribute name="href">
771                       <xsl:call-template name="href.target">
772                         <xsl:with-param name="object" select="$target"/>
773                       </xsl:call-template>
774                     </xsl:attribute>
775                   </xsl:if>
776                 </xsl:when>
777                 <xsl:when test="$href != ''">
778                   <xsl:attribute name="href">
779                     <xsl:value-of select="$href"/>
780                   </xsl:attribute>
781                 </xsl:when>
782               </xsl:choose>
783  
784               <xsl:if test="alt">
785                 <xsl:attribute name="alt">
786                   <xsl:value-of select="alt[1]"/>
787                 </xsl:attribute>
788               </xsl:if>
789  
790               <xsl:attribute name="coords">
791                 <xsl:choose>
792                   <xsl:when test="$units = 'calspair'">
793
794                     <xsl:variable name="p1" select="substring-before($coords, ' ')"/>
795                     <xsl:variable name="p2" select="substring-after($coords, ' ')"/>
796          
797                     <xsl:variable name="x1" select="substring-before($p1,',')"/>
798                     <xsl:variable name="y1" select="substring-after($p1,',')"/>
799                     <xsl:variable name="x2" select="substring-before($p2,',')"/>
800                     <xsl:variable name="y2" select="substring-after($p2,',')"/>
801          
802                     <xsl:variable name="x1p" select="$x1 div 100.0"/>
803                     <xsl:variable name="y1p" select="$y1 div 100.0"/>
804                     <xsl:variable name="x2p" select="$x2 div 100.0"/>
805                     <xsl:variable name="y2p" select="$y2 div 100.0"/>
806          
807          <!--
808                     <xsl:message>
809                       <xsl:text>units: </xsl:text>
810                       <xsl:value-of select="$units"/>
811                       <xsl:text> </xsl:text>
812                       <xsl:value-of select="$x1p"/><xsl:text>, </xsl:text>
813                       <xsl:value-of select="$y1p"/><xsl:text>, </xsl:text>
814                       <xsl:value-of select="$x2p"/><xsl:text>, </xsl:text>
815                       <xsl:value-of select="$y2p"/><xsl:text>, </xsl:text>
816                     </xsl:message>
817          
818                     <xsl:message>
819                       <xsl:text>      </xsl:text>
820                       <xsl:value-of select="$intrinsicwidth"/>
821                       <xsl:text>, </xsl:text>
822                       <xsl:value-of select="$intrinsicdepth"/>
823                     </xsl:message>
824          
825                     <xsl:message>
826                       <xsl:text>      </xsl:text>
827                       <xsl:value-of select="$units"/>
828                       <xsl:text> </xsl:text>
829                       <xsl:value-of 
830                             select="round($x1p * $intrinsicwidth div 100.0)"/>
831                       <xsl:text>,</xsl:text>
832                       <xsl:value-of select="round($intrinsicdepth
833                                        - ($y2p * $intrinsicdepth div 100.0))"/>
834                       <xsl:text>,</xsl:text>
835                       <xsl:value-of select="round($x2p * 
836                                             $intrinsicwidth div 100.0)"/>
837                       <xsl:text>,</xsl:text>
838                       <xsl:value-of select="round($intrinsicdepth
839                                        - ($y1p * $intrinsicdepth div 100.0))"/>
840                     </xsl:message>
841          -->
842                     <xsl:value-of select="round($x1p * $intrinsicwidth div 100.0)"/>
843                     <xsl:text>,</xsl:text>
844                     <xsl:value-of select="round($intrinsicdepth                                         - ($y2p * $intrinsicdepth div 100.0))"/>
845                     <xsl:text>,</xsl:text>
846                     <xsl:value-of select="round($x2p * $intrinsicwidth div 100.0)"/>
847                     <xsl:text>,</xsl:text>
848                     <xsl:value-of select="round($intrinsicdepth                                       - ($y1p * $intrinsicdepth div 100.0))"/>
849                   </xsl:when>
850                   <xsl:otherwise>
851                     <xsl:copy-of select="$coords"/>
852                   </xsl:otherwise>
853                 </xsl:choose>
854               </xsl:attribute>
855             </area>
856           </xsl:when>
857           <xsl:otherwise>
858             <xsl:message>
859               <xsl:text>Warning: only calspair or </xsl:text>
860               <xsl:text>otherunits='imagemap' supported </xsl:text>
861               <xsl:text>in imageobjectco</xsl:text>
862             </xsl:message>
863           </xsl:otherwise>
864         </xsl:choose>
865       </xsl:for-each>
866     </map>
867   </xsl:if>
868 </xsl:template>
869
870 <xsl:template name="process.image.attributes">
871   <xsl:param name="alt"/>
872   <xsl:param name="html.width"/>
873   <xsl:param name="html.depth"/>
874   <xsl:param name="longdesc"/>
875   <xsl:param name="scale"/>
876   <xsl:param name="scalefit"/>
877   <xsl:param name="scaled.contentdepth"/>
878   <xsl:param name="scaled.contentwidth"/>
879   <xsl:param name="viewport"/>
880
881   <xsl:choose>
882     <xsl:when test="@contentwidth or @contentdepth">
883       <!-- ignore @width/@depth, @scale, and @scalefit if specified -->
884       <xsl:if test="@contentwidth and $scaled.contentwidth != ''">
885         <xsl:attribute name="width">
886           <xsl:value-of select="$scaled.contentwidth"/>
887         </xsl:attribute>
888       </xsl:if>
889       <xsl:if test="@contentdepth and $scaled.contentdepth != ''">
890         <xsl:attribute name="height">
891           <xsl:value-of select="$scaled.contentdepth"/>
892         </xsl:attribute>
893       </xsl:if>
894     </xsl:when>
895
896     <xsl:when test="number($scale) != 1.0">
897       <!-- scaling is always uniform, so we only have to specify one dimension -->
898       <!-- ignore @scalefit if specified -->
899       <xsl:attribute name="width">
900         <xsl:value-of select="$scaled.contentwidth"/>
901       </xsl:attribute>
902     </xsl:when>
903
904     <xsl:when test="$scalefit != 0">
905       <xsl:choose>
906         <xsl:when test="contains($html.width, '%')">
907           <xsl:choose>
908             <xsl:when test="$viewport != 0">
909               <!-- The *viewport* will be scaled, so use 100% here! -->
910               <xsl:attribute name="width">
911                 <xsl:value-of select="'100%'"/>
912               </xsl:attribute>
913             </xsl:when>
914             <xsl:otherwise>
915               <xsl:attribute name="width">
916                 <xsl:value-of select="$html.width"/>
917               </xsl:attribute>
918             </xsl:otherwise>
919           </xsl:choose>
920         </xsl:when>
921
922         <xsl:when test="contains($html.depth, '%')">
923           <!-- HTML doesn't deal with this case very well...do nothing -->
924         </xsl:when>
925
926         <xsl:when test="$scaled.contentwidth != '' and $html.width != ''                         and $scaled.contentdepth != '' and $html.depth != ''">
927           <!-- scalefit should not be anamorphic; figure out which direction -->
928           <!-- has the limiting scale factor and scale in that direction -->
929           <xsl:choose>
930             <xsl:when test="$html.width div $scaled.contentwidth &gt;                             $html.depth div $scaled.contentdepth">
931               <xsl:attribute name="height">
932                 <xsl:value-of select="$html.depth"/>
933               </xsl:attribute>
934             </xsl:when>
935             <xsl:otherwise>
936               <xsl:attribute name="width">
937                 <xsl:value-of select="$html.width"/>
938               </xsl:attribute>
939             </xsl:otherwise>
940           </xsl:choose>
941         </xsl:when>
942
943         <xsl:when test="$scaled.contentwidth != '' and $html.width != ''">
944           <xsl:attribute name="width">
945             <xsl:value-of select="$html.width"/>
946           </xsl:attribute>
947         </xsl:when>
948
949         <xsl:when test="$scaled.contentdepth != '' and $html.depth != ''">
950           <xsl:attribute name="height">
951             <xsl:value-of select="$html.depth"/>
952           </xsl:attribute>
953         </xsl:when>
954       </xsl:choose>
955     </xsl:when>
956   </xsl:choose>
957
958   <xsl:if test="$alt != ''">
959     <xsl:attribute name="alt">
960       <xsl:value-of select="normalize-space($alt)"/>
961     </xsl:attribute>
962   </xsl:if>
963
964   <!-- Turn off longdesc attribute since not supported by browsers
965   <xsl:if test="$longdesc != ''">
966     <xsl:attribute name="longdesc">
967       <xsl:value-of select="$longdesc"/>
968     </xsl:attribute>
969   </xsl:if>
970   -->
971
972   <xsl:if test="@align and $viewport = 0">
973     <xsl:attribute name="style"><xsl:text>text-align: </xsl:text>
974       <xsl:choose>
975         <xsl:when test="@align = 'center'">middle</xsl:when>
976         <xsl:otherwise>
977           <xsl:value-of select="@align"/>
978         </xsl:otherwise>
979       </xsl:choose>
980     </xsl:attribute>
981   </xsl:if>
982 </xsl:template>
983
984 <!-- ==================================================================== -->
985
986 <xsl:template match="graphic">
987   <xsl:choose>
988     <xsl:when test="parent::inlineequation">
989       <span>
990         <xsl:call-template name="id.attribute"/>
991         <xsl:call-template name="anchor"/>
992         <xsl:call-template name="process.image"/>
993       </span>
994     </xsl:when>
995     <xsl:otherwise>
996       <div>
997         <xsl:call-template name="id.attribute"/>
998         <xsl:if test="@align">
999           <xsl:attribute name="style"><xsl:text>text-align: </xsl:text>
1000             <xsl:value-of select="@align"/>
1001           </xsl:attribute>
1002         </xsl:if>
1003         <xsl:call-template name="anchor"/>
1004         <xsl:call-template name="process.image"/>
1005       </div>
1006     </xsl:otherwise>
1007   </xsl:choose>
1008 </xsl:template>
1009
1010 <xsl:template match="inlinegraphic">
1011   <xsl:variable name="filename">
1012     <xsl:choose>
1013       <xsl:when test="@entityref">
1014         <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
1015       </xsl:when>
1016       <xsl:otherwise>
1017         <xsl:apply-templates select="@fileref"/>
1018       </xsl:otherwise>
1019     </xsl:choose>
1020   </xsl:variable>
1021
1022   <xsl:call-template name="anchor"/>
1023
1024   <xsl:choose>
1025     <xsl:when test="@format='linespecific'">
1026       <xsl:choose>
1027         <xsl:when test="$use.extensions != '0'                         and $textinsert.extension != '0'">
1028           <xsl:choose>
1029             <xsl:when test="element-available('stext:insertfile')">
1030               <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
1031             </xsl:when>
1032             <xsl:when test="element-available('xtext:insertfile')">
1033               <xtext:insertfile href="{$filename}"/>
1034             </xsl:when>
1035             <xsl:otherwise>
1036               <xsl:message terminate="yes">
1037                 <xsl:text>No insertfile extension available.</xsl:text>
1038               </xsl:message>
1039             </xsl:otherwise>
1040           </xsl:choose>
1041         </xsl:when>
1042         <xsl:otherwise>
1043           <xsl:message terminate="yes">
1044             <xsl:text>Cannot insert </xsl:text><xsl:value-of select="$filename"/>
1045             <xsl:text>. Check use.extensions and textinsert.extension parameters.</xsl:text> 
1046           </xsl:message>
1047         </xsl:otherwise>
1048       </xsl:choose>
1049     </xsl:when>
1050     <xsl:otherwise>
1051       <xsl:call-template name="process.image"/>
1052     </xsl:otherwise>
1053   </xsl:choose>
1054 </xsl:template>
1055
1056 <!-- ==================================================================== -->
1057
1058 <xsl:template match="mediaobject|mediaobjectco">
1059
1060   <xsl:variable name="olist" select="imageobject|imageobjectco                      |videoobject|audioobject                      |textobject"/>
1061
1062   <xsl:variable name="object.index">
1063     <xsl:call-template name="select.mediaobject.index">
1064       <xsl:with-param name="olist" select="$olist"/>
1065       <xsl:with-param name="count" select="1"/>
1066     </xsl:call-template>
1067   </xsl:variable>
1068
1069   <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1070
1071   <xsl:variable name="align">
1072     <xsl:value-of select="$object/descendant::imagedata[@align][1]/@align"/>
1073   </xsl:variable>
1074
1075   <div>
1076     <xsl:apply-templates select="." mode="common.html.attributes"/>
1077     <xsl:if test="$align != '' ">
1078       <xsl:attribute name="style"><xsl:text>text-align: </xsl:text>
1079         <xsl:value-of select="$align"/>
1080       </xsl:attribute>
1081     </xsl:if>
1082     <xsl:call-template name="id.attribute"/>
1083     <xsl:call-template name="anchor"/>
1084
1085     <xsl:apply-templates select="$object"/>
1086     <xsl:apply-templates select="caption"/>
1087   </div>
1088 </xsl:template>
1089
1090 <xsl:template match="inlinemediaobject">
1091   <span>
1092     <xsl:apply-templates select="." mode="common.html.attributes"/>
1093     <xsl:call-template name="id.attribute"/>
1094     <xsl:call-template name="anchor"/>
1095     <xsl:call-template name="select.mediaobject"/>
1096   </span>
1097 </xsl:template>
1098
1099 <xsl:template match="programlisting/inlinemediaobject                      |screen/inlinemediaobject" priority="2">
1100   <!-- the additional span causes problems in some cases -->
1101   <xsl:call-template name="select.mediaobject"/>
1102 </xsl:template>
1103
1104 <!-- ==================================================================== -->
1105
1106 <xsl:template match="imageobjectco">
1107   <xsl:call-template name="anchor"/>
1108   <xsl:choose>
1109     <!-- select one imageobject? -->
1110     <xsl:when test="$use.role.for.mediaobject != 0 and                     count(imageobject) &gt; 1 and                     imageobject[@role]">
1111       <xsl:variable name="olist" select="imageobject"/>
1112     
1113       <xsl:variable name="object.index">
1114         <xsl:call-template name="select.mediaobject.index">
1115           <xsl:with-param name="olist" select="$olist"/>
1116           <xsl:with-param name="count" select="1"/>
1117         </xsl:call-template>
1118       </xsl:variable>
1119     
1120       <xsl:variable name="object" select="$olist[position() = $object.index]"/>
1121     
1122       <xsl:apply-templates select="$object"/>
1123     </xsl:when>
1124     <xsl:otherwise>
1125       <!-- otherwise process them all -->
1126       <xsl:apply-templates select="imageobject"/>
1127     </xsl:otherwise>
1128   </xsl:choose>
1129
1130   <xsl:apply-templates select="calloutlist"/>
1131
1132 </xsl:template>
1133
1134 <xsl:template match="imageobject">
1135   <xsl:apply-templates select="imagedata"/>
1136 </xsl:template>
1137
1138 <xsl:template match="imagedata">
1139   <xsl:variable name="filename">
1140     <xsl:call-template name="mediaobject.filename">
1141       <xsl:with-param name="object" select=".."/>
1142     </xsl:call-template>
1143   </xsl:variable>
1144
1145   <xsl:choose>
1146     <!-- Handle MathML and SVG markup in imagedata -->
1147     <xsl:when xmlns:mml="http://www.w3.org/1998/Math/MathML" test="mml:*">
1148       <xsl:apply-templates/>
1149     </xsl:when>
1150     
1151     <xsl:when xmlns:svg="http://www.w3.org/2000/svg" test="svg:*">
1152       <xsl:apply-templates/>
1153     </xsl:when>
1154
1155     <xsl:when test="@format='linespecific'">
1156       <xsl:choose>
1157         <xsl:when test="$use.extensions != '0'                         and $textinsert.extension != '0'">
1158           <xsl:choose>
1159             <xsl:when test="element-available('stext:insertfile')">
1160               <stext:insertfile href="{$filename}" encoding="{$textdata.default.encoding}"/>
1161             </xsl:when>
1162             <xsl:when test="element-available('xtext:insertfile')">
1163               <xtext:insertfile href="{$filename}"/>
1164             </xsl:when>
1165             <xsl:otherwise>
1166               <xsl:message terminate="yes">
1167                 <xsl:text>No insertfile extension available.</xsl:text>
1168               </xsl:message>
1169             </xsl:otherwise>
1170           </xsl:choose>
1171         </xsl:when>
1172         <xsl:otherwise>
1173           <a xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" href="{$filename}"/>
1174         </xsl:otherwise>
1175       </xsl:choose>
1176     </xsl:when>
1177     <xsl:otherwise>
1178       <xsl:variable name="longdesc.uri">
1179         <xsl:call-template name="longdesc.uri">
1180           <xsl:with-param name="mediaobject" select="ancestor::imageobject/parent::*"/>
1181         </xsl:call-template>
1182       </xsl:variable>
1183
1184       <xsl:variable name="phrases" select="ancestor::mediaobject/textobject[phrase]                             |ancestor::inlinemediaobject/textobject[phrase]                             |ancestor::mediaobjectco/textobject[phrase]"/>
1185
1186       <xsl:call-template name="process.image">
1187         <xsl:with-param name="alt">
1188           <xsl:apply-templates select="$phrases[not(@role) or @role!='tex'][1]"/>
1189         </xsl:with-param>
1190         <xsl:with-param name="longdesc">
1191           <xsl:call-template name="write.longdesc">
1192             <xsl:with-param name="mediaobject" select="ancestor::imageobject/parent::*"/>
1193           </xsl:call-template>
1194         </xsl:with-param>
1195       </xsl:call-template>
1196
1197       <xsl:if test="$html.longdesc != 0 and $html.longdesc.link != 0                     and ancestor::imageobject/parent::*/textobject[not(phrase)]">
1198         <xsl:call-template name="longdesc.link">
1199           <xsl:with-param name="longdesc.uri" select="$longdesc.uri"/>
1200         </xsl:call-template>
1201       </xsl:if>
1202     </xsl:otherwise>
1203   </xsl:choose>
1204 </xsl:template>
1205
1206 <!-- ==================================================================== -->
1207
1208 <xsl:template name="longdesc.uri">
1209   <xsl:param name="mediaobject" select="."/>
1210   <xsl:if test="$html.longdesc">
1211     <xsl:if test="$mediaobject/textobject[not(phrase)]">
1212       <xsl:variable name="dbhtml.dir">
1213         <xsl:call-template name="dbhtml-dir"/>
1214       </xsl:variable>
1215       <xsl:variable name="filename">
1216         <xsl:call-template name="make-relative-filename">
1217           <xsl:with-param name="base.dir">
1218             <xsl:choose>
1219               <xsl:when test="$dbhtml.dir != ''">
1220                 <xsl:value-of select="$dbhtml.dir"/>
1221               </xsl:when>
1222               <xsl:otherwise>
1223                 <xsl:value-of select="$chunk.base.dir"/>
1224               </xsl:otherwise>
1225             </xsl:choose>
1226           </xsl:with-param>
1227           <xsl:with-param name="base.name">
1228             <xsl:choose>
1229               <xsl:when test="                 $mediaobject/@*[local-name() = 'id']                 and not($use.id.as.filename = 0)">
1230                 <!-- * if this mediaobject has an ID, then we use the -->
1231                 <!-- * value of that ID as basename for the "longdesc" -->
1232                 <!-- * file (that is, without prepending an "ld-" too it) -->
1233                 <xsl:value-of select="$mediaobject/@*[local-name() = 'id']"/>
1234                 <xsl:value-of select="$html.ext"/>
1235               </xsl:when>
1236               <xsl:otherwise>
1237                 <!-- * otherwise, if this mediaobject does not have an -->
1238                 <!-- * ID, then we generate an ID... -->
1239                 <xsl:variable name="image-id">
1240                   <xsl:call-template name="object.id">
1241                     <xsl:with-param name="object" select="$mediaobject"/>
1242                   </xsl:call-template>
1243                 </xsl:variable>
1244                 <!-- * ...and then we take that generated ID, prepend an -->
1245                 <!-- * "ld-" to it, and use that as the basename for the file -->
1246                 <xsl:value-of select="concat('ld-',$image-id,$html.ext)"/>
1247               </xsl:otherwise>
1248             </xsl:choose>
1249           </xsl:with-param>
1250         </xsl:call-template>
1251       </xsl:variable>
1252
1253       <xsl:value-of select="$filename"/>
1254     </xsl:if>
1255   </xsl:if>
1256 </xsl:template>
1257
1258 <xsl:template name="write.longdesc">
1259   <xsl:param name="mediaobject" select="."/>
1260   <xsl:if test="$html.longdesc != 0 and $mediaobject/textobject[not(phrase)]">
1261     <xsl:variable name="filename">
1262       <xsl:call-template name="longdesc.uri">
1263         <xsl:with-param name="mediaobject" select="$mediaobject"/>
1264       </xsl:call-template>
1265     </xsl:variable>
1266
1267     <xsl:value-of select="$filename"/>
1268
1269     <xsl:call-template name="write.chunk">
1270       <xsl:with-param name="filename" select="$filename"/>
1271       <xsl:with-param name="quiet" select="$chunk.quietly"/>
1272       <xsl:with-param name="content">
1273       <xsl:call-template name="user.preroot"/>
1274         <html>
1275           <head>
1276             <xsl:call-template name="system.head.content"/>
1277             <xsl:call-template name="head.content">
1278               <xsl:with-param name="title" select="'Long Description'"/>
1279             </xsl:call-template>
1280             <xsl:call-template name="user.head.content"/>
1281           </head>
1282           <body>
1283             <xsl:call-template name="body.attributes"/>
1284             <xsl:for-each select="$mediaobject/textobject[not(phrase)]">
1285               <xsl:apply-templates select="./*"/>
1286             </xsl:for-each>
1287           </body>
1288         </html>
1289         <xsl:value-of select="$chunk.append"/>
1290       </xsl:with-param>
1291     </xsl:call-template>
1292   </xsl:if>
1293 </xsl:template>
1294
1295 <xsl:template name="longdesc.link">
1296   <xsl:param name="longdesc.uri" select="''"/>
1297
1298   <xsl:variable name="this.uri">
1299     <xsl:call-template name="make-relative-filename">
1300       <xsl:with-param name="base.dir" select="$chunk.base.dir"/>
1301       <xsl:with-param name="base.name">
1302         <xsl:call-template name="href.target.uri"/>
1303       </xsl:with-param>
1304     </xsl:call-template>
1305   </xsl:variable>
1306
1307   <xsl:variable name="href.to">
1308     <xsl:call-template name="trim.common.uri.paths">
1309       <xsl:with-param name="uriA" select="$longdesc.uri"/>
1310       <xsl:with-param name="uriB" select="$this.uri"/>
1311       <xsl:with-param name="return" select="'A'"/>
1312     </xsl:call-template>
1313   </xsl:variable>
1314
1315   <div class="longdesc-link" align="{$direction.align.end}">
1316     <br clear="all"/>
1317     <span class="longdesc-link">
1318       <xsl:text>[</xsl:text>
1319       <a href="{$href.to}" target="longdesc">D</a>
1320       <xsl:text>]</xsl:text>
1321     </span>
1322   </div>
1323 </xsl:template>
1324
1325 <!-- ==================================================================== -->
1326
1327 <xsl:template match="videoobject">
1328   <xsl:apply-templates select="videodata"/>
1329 </xsl:template>
1330
1331 <xsl:template match="videodata">
1332   <xsl:call-template name="process.image">
1333     <xsl:with-param name="tag" select="'embed'"/>
1334     <xsl:with-param name="alt">
1335       <xsl:apply-templates select="(../../textobject/phrase)[1]"/>
1336     </xsl:with-param>
1337   </xsl:call-template>
1338 </xsl:template>
1339
1340 <!-- ==================================================================== -->
1341
1342 <xsl:template match="audioobject">
1343   <xsl:apply-templates select="audiodata"/>
1344 </xsl:template>
1345
1346 <xsl:template match="audiodata">
1347   <xsl:call-template name="process.image">
1348     <xsl:with-param name="tag" select="'embed'"/>
1349     <xsl:with-param name="alt">
1350       <xsl:apply-templates select="(../../textobject/phrase)[1]"/>
1351     </xsl:with-param>
1352   </xsl:call-template>
1353 </xsl:template>
1354
1355 <!-- ==================================================================== -->
1356
1357 <xsl:template match="textobject">
1358   <xsl:apply-templates/>
1359 </xsl:template>
1360
1361 <xsl:template match="textdata">
1362   <xsl:variable name="filename">
1363     <xsl:choose>
1364       <xsl:when test="@entityref">
1365         <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
1366       </xsl:when>
1367       <xsl:otherwise>
1368         <xsl:apply-templates select="@fileref"/>
1369       </xsl:otherwise>
1370     </xsl:choose>
1371   </xsl:variable>
1372
1373   <xsl:variable name="encoding">
1374     <xsl:choose>
1375       <xsl:when test="@encoding">
1376         <xsl:value-of select="@encoding"/>
1377       </xsl:when>
1378       <xsl:otherwise>
1379         <xsl:value-of select="$textdata.default.encoding"/>
1380       </xsl:otherwise>
1381     </xsl:choose>
1382   </xsl:variable>
1383
1384   <xsl:choose>
1385     <xsl:when test="$use.extensions != '0'                     and $textinsert.extension != '0'">
1386       <xsl:choose>
1387         <xsl:when test="element-available('stext:insertfile')">
1388           <stext:insertfile href="{$filename}" encoding="{$encoding}"/>
1389         </xsl:when>
1390         <xsl:when test="element-available('xtext:insertfile')">
1391           <xtext:insertfile href="{$filename}"/>
1392         </xsl:when>
1393         <xsl:otherwise>
1394           <xsl:message terminate="yes">
1395             <xsl:text>No insertfile extension available.</xsl:text>
1396           </xsl:message>
1397         </xsl:otherwise>
1398       </xsl:choose>
1399     </xsl:when>
1400     <xsl:otherwise>
1401       <xsl:message terminate="yes">
1402         <xsl:text>Cannot insert </xsl:text><xsl:value-of select="$filename"/>
1403         <xsl:text>. Check use.extensions and textinsert.extension parameters.</xsl:text> 
1404       </xsl:message>
1405     </xsl:otherwise>
1406   </xsl:choose>
1407 </xsl:template>
1408
1409 <!-- ==================================================================== -->
1410
1411 <xsl:template match="caption">
1412   <div>
1413     <xsl:apply-templates select="." mode="common.html.attributes"/>
1414     <xsl:call-template name="id.attribute"/>
1415     <xsl:if test="@align = 'right' or @align = 'left' or @align='center'">
1416       <xsl:attribute name="style"><xsl:text>text-align: </xsl:text><xsl:value-of select="@align"/></xsl:attribute>
1417     </xsl:if>
1418     <xsl:apply-templates/>
1419   </div>
1420 </xsl:template>
1421
1422 <!-- ==================================================================== -->
1423 <!-- "Support" for SVG -->
1424
1425 <xsl:template xmlns:svg="http://www.w3.org/2000/svg" match="svg:*">
1426   <xsl:copy>
1427     <xsl:copy-of select="@*"/>
1428     <xsl:apply-templates/>
1429   </xsl:copy>
1430 </xsl:template>
1431
1432
1433 <!-- The following works sometimes, but needs to take into account
1434              1. When there is no /*/@xml:base
1435              2. When the chunks are going somewhere else
1436 <xsl:variable name="relpath">
1437   <xsl:call-template name="relative-uri">
1438     <xsl:with-param name="filename" select="@fileref"/>
1439   </xsl:call-template>
1440 </xsl:variable>
1441
1442 <xsl:choose>
1443   <xsl:when test="/*/@xml:base
1444                   and starts-with($relpath,/*/@xml:base)">
1445     <xsl:value-of select="substring-after($relpath,/*/@xml:base)"/>
1446   </xsl:when>
1447   <xsl:otherwise>
1448     <xsl:value-of select="@fileref"/>
1449   </xsl:otherwise>
1450 </xsl:choose>
1451 <xsl:value-of select="@fileref"/>
1452       </xsl:when>
1453 -->
1454 <!-- Resolve xml:base attributes -->
1455 <xsl:template match="@fileref">
1456   <!-- need a check for absolute urls -->
1457   <xsl:choose>
1458     <xsl:when test="contains(., ':')">
1459       <!-- it has a uri scheme so it is an absolute uri -->
1460       <xsl:value-of select="."/>
1461     </xsl:when>
1462     <xsl:when test="$keep.relative.image.uris != 0">
1463       <!-- leave it alone -->
1464       <xsl:value-of select="."/>
1465     </xsl:when>
1466     <xsl:otherwise>
1467       <!-- its a relative uri that needs xml:base processing -->
1468       <xsl:call-template name="relative-uri">
1469       </xsl:call-template>
1470     </xsl:otherwise>
1471   </xsl:choose>
1472 </xsl:template>
1473
1474 </xsl:stylesheet>