Debian Codegraph @

xsl/mediaobject.xsl

raw
1 <?xml version='1.0'?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:exsl="http://exslt.org/common"
4 exclude-result-prefixes="exsl" version='1.0'>
5
6 <!--############################################################################
7 XSLT Stylesheet DocBook -> LaTeX
8 ############################################################################ -->
9
10 <!-- Mediaobject/imagedata parameters -->
11 <xsl:param name="mediaobject.caption.style">\slshape</xsl:param>
12 <xsl:param name="imagedata.default.scale">pagebound</xsl:param>
13 <xsl:param name="imagedata.file.check">1</xsl:param>
14 <xsl:param name="imagedata.boxed">0</xsl:param>
15 <xsl:param name="keep.relative.image.uris" select="0"/>
16
17
18 <!-- Simple mediaobject selection using @role -->
19 <xsl:template name="mediaobject.select.idx">
20 <xsl:param name="olist" select="imageobject|imageobjectco"/>
21 <xsl:param name="role" select="'dblatex'"/>
22 <xsl:choose>
23 <xsl:when test="$olist[@role=$role]">
24 <xsl:value-of select="count($olist[@role=$role][1]/preceding-sibling::*) -
25 count($olist[1]/preceding-sibling::*) + 1"/>
26 </xsl:when>
27 <xsl:otherwise>
28 <xsl:value-of select="1"/>
29 </xsl:otherwise>
30 </xsl:choose>
31 </xsl:template>
32
33
34 <!-- Initial image macro setting, depending on the parameter value -->
35 <xsl:template name="opt.extract">
36 <xsl:param name="optgroup"/>
37 <xsl:param name="opt"/>
38 <xsl:if test="contains($optgroup,$opt)">
39 <xsl:variable name="s" select="substring-after($optgroup,$opt)"/>
40 <xsl:choose>
41 <xsl:when test="contains($s,',')">
42 <xsl:value-of select="normalize-space(substring-before($s,','))"/>
43 </xsl:when>
44 <xsl:otherwise>
45 <xsl:value-of select="normalize-space($s)"/>
46 </xsl:otherwise>
47 </xsl:choose>
48 </xsl:if>
49 </xsl:template>
50
51 <xsl:template name="image.setup">
52 <xsl:variable name="maxwidth">
53 <xsl:call-template name="opt.extract">
54 <xsl:with-param name="optgroup" select="$imagedata.default.scale"/>
55 <xsl:with-param name="opt" select="'maxwidth='"/>
56 </xsl:call-template>
57 </xsl:variable>
58 <xsl:variable name="maxheight">
59 <xsl:call-template name="opt.extract">
60 <xsl:with-param name="optgroup" select="$imagedata.default.scale"/>
61 <xsl:with-param name="opt" select="'maxheight='"/>
62 </xsl:call-template>
63 </xsl:variable>
64 <xsl:if test="$maxwidth!=''">
65 <xsl:text>\def\imgmaxwidth{</xsl:text>
66 <xsl:value-of select="$maxwidth"/>
67 <xsl:text>}&#10;</xsl:text>
68 </xsl:if>
69 <xsl:if test="$maxheight!=''">
70 <xsl:text>\def\imgmaxheight{</xsl:text>
71 <xsl:value-of select="$maxheight"/>
72 <xsl:text>}&#10;</xsl:text>
73 </xsl:if>
74 </xsl:template>
75
76 <xsl:template name="image.autosize">
77 <xsl:choose>
78 <xsl:when test="$imagedata.default.scale='pagebound' or
79 contains($imagedata.default.scale,'maxwidth=') or
80 contains($imagedata.default.scale,'maxheight=')">
81 <xsl:value-of select="1"/>
82 </xsl:when>
83 <xsl:otherwise>
84 <xsl:value-of select="0"/>
85 </xsl:otherwise>
86 </xsl:choose>
87 </xsl:template>
88
89 <xsl:template name="align.environment2">
90 <xsl:param name="align"/>
91 <xsl:param name="align-default" select="'center'"/>
92
93 <xsl:choose>
94 <xsl:when test="$align = 'right'">
95 <xsl:text>flushright</xsl:text>
96 </xsl:when>
97 <xsl:when test="$align = 'left'">
98 <xsl:text>flushleft</xsl:text>
99 </xsl:when>
100 <xsl:when test="$align = 'center'">
101 <xsl:text>center</xsl:text>
102 </xsl:when>
103 <xsl:otherwise>
104 <xsl:value-of select="$align-default"/>
105 </xsl:otherwise>
106 </xsl:choose>
107 </xsl:template>
108
109
110 <xsl:template match="videoobject">
111 <xsl:apply-templates select="videodata"/>
112 </xsl:template>
113
114 <xsl:template match="audioobject">
115 <xsl:apply-templates select="audiodata"/>
116 </xsl:template>
117
118 <xsl:template match="textobject">
119 <xsl:apply-templates/>
120 </xsl:template>
121
122 <!-- ==================================================================== -->
123
124 <xsl:template match="mediaobject/caption" mode="environment">
125 <xsl:param name="parent-align"/>
126
127 <xsl:variable name="align">
128 <xsl:call-template name="align.environment2">
129 <xsl:with-param name="align" select="@align"/>
130 <xsl:with-param name="align-default" select="$parent-align"/>
131 </xsl:call-template>
132 </xsl:variable>
133
134 <xsl:value-of select="concat('\begin{',$align,'}&#10;')"/>
135 <xsl:apply-templates select="."/>
136 <xsl:value-of select="concat('\end{',$align,'}&#10;')"/>
137 </xsl:template>
138
139 <xsl:template match="mediaobject/caption">
140 <xsl:text>{</xsl:text>
141 <xsl:value-of select="$mediaobject.caption.style"/>
142 <xsl:text> </xsl:text>
143 <!-- Apply templates to render captions elements -->
144 <xsl:apply-templates/>
145 <xsl:text>}</xsl:text>
146 </xsl:template>
147
148 <xsl:template match="mediaobject/caption" mode="subfigure">
149 <xsl:text>{</xsl:text>
150 <xsl:value-of select="$mediaobject.caption.style"/>
151 <xsl:text> </xsl:text>
152 <!-- In subfigures, cannot have several paragraphs, so just take
153 the text and normalize it (no \par in it)
154 -->
155 <xsl:call-template name="normalize-scape">
156 <xsl:with-param name="string" select="."/>
157 </xsl:call-template>
158 <xsl:text>}</xsl:text>
159 </xsl:template>
160
161 <!-- ==================================================================== -->
162
163 <xsl:template match="mediaobject|inlinemediaobject">
164 <xsl:variable name="figcount"
165 select="count(ancestor::figure/mediaobject[imageobject])"/>
166
167 <xsl:variable name="mediaobject.alone">
168 <xsl:if test="self::mediaobject and not(parent::figure)">
169 <xsl:value-of select="1"/>
170 </xsl:if>
171 </xsl:variable>
172
173 <xsl:variable name="idx">
174 <xsl:call-template name="mediaobject.select.idx"/>
175 </xsl:variable>
176
177 <xsl:variable name="img"
178 select="(imageobject|imageobjectco)[position()=$idx]"/>
179
180 <!-- Is there an explicit viewport handled by the image data -->
181 <xsl:variable name="viewport">
182 <xsl:if test="$img">
183 <xsl:apply-templates select="$img/descendant::imagedata"
184 mode="viewport"/>
185 </xsl:if>
186 </xsl:variable>
187
188 <!-- The global alignment here applies only if there is no explicit viewport.
189 In case of a viewport, it is processed by imagedata, so keep the default
190 here -->
191 <xsl:variable name="align">
192 <xsl:choose>
193 <xsl:when test="not($img) or
194 not($img/descendant::imagedata/@align) or
195 $viewport=1">
196 <xsl:value-of select="'center'"/>
197 </xsl:when>
198 <xsl:otherwise>
199 <xsl:call-template name="align.environment2">
200 <xsl:with-param name="align"
201 select="$img/descendant::imagedata/@align"/>
202 </xsl:call-template>
203 </xsl:otherwise>
204 </xsl:choose>
205 </xsl:variable>
206
207 <!--
208 within a figure don't put each mediaobject into a separate paragraph,
209 to let the subfigures correctly displayed.
210 -->
211 <xsl:if test="$mediaobject.alone = 1">
212 <xsl:text>&#10;\noindent</xsl:text>
213 <xsl:text>\begin{minipage}[c]{\linewidth}&#10;</xsl:text>
214 <xsl:value-of select="concat('\begin{',$align,'}&#10;')"/>
215 </xsl:if>
216 <xsl:if test="self::inlinemediaobject">
217 <xsl:text>\noindent</xsl:text>
218 </xsl:if>
219 <xsl:choose>
220 <xsl:when test="$img">
221 <xsl:if test="$imagedata.file.check='1'">
222 <xsl:text>\imgexists{</xsl:text>
223 <xsl:apply-templates
224 select="$img/descendant::imagedata"
225 mode="filename.get"/>
226 <xsl:text>}{</xsl:text>
227 </xsl:if>
228 <xsl:apply-templates select="$img"/>
229 <xsl:if test="$imagedata.file.check='1'">
230 <xsl:text>}{</xsl:text>
231 <xsl:apply-templates select="textobject[1]"/>
232 <xsl:text>}</xsl:text>
233 </xsl:if>
234 </xsl:when>
235 <xsl:otherwise>
236 <xsl:apply-templates select="textobject[1]"/>
237 </xsl:otherwise>
238 </xsl:choose>
239 <!-- print the caption if not in a float, or is single -->
240 <xsl:if test="caption and ($figcount &lt;= 1)">
241 <xsl:apply-templates select="caption" mode="environment">
242 <xsl:with-param name="parent-align" select="$align"/>
243 </xsl:apply-templates>
244 </xsl:if>
245 <xsl:if test="$mediaobject.alone = 1">
246 <xsl:value-of select="concat('\end{',$align,'}&#10;')"/>
247 <xsl:text>\end{minipage}&#10;</xsl:text>
248 <xsl:text>&#10;</xsl:text>
249 </xsl:if>
250 </xsl:template>
251
252 <xsl:template match="imageobject">
253 <xsl:variable name="figcount"
254 select="count(ancestor::figure/mediaobject[imageobject])"/>
255 <xsl:if test="$figcount &gt; 1">
256 <!-- space before subfigure to prevent from strange behaviour with other
257 subfigures unless forced by @role -->
258 <xsl:if test="not(ancestor::figure/@role='flow.inline')">
259 <xsl:text> </xsl:text>
260 </xsl:if>
261 <xsl:text>\subfigure</xsl:text>
262 <xsl:if test="../caption">
263 <xsl:text>[</xsl:text>
264 <xsl:apply-templates select="../caption" mode="subfigure"/>
265 <xsl:text>][</xsl:text>
266 <xsl:apply-templates select="../caption"/>
267 <xsl:text>]</xsl:text>
268 </xsl:if>
269 <xsl:text>{</xsl:text>
270 </xsl:if>
271 <xsl:if test="$imagedata.boxed = '1'">
272 <xsl:text>\fbox{</xsl:text>
273 </xsl:if>
274 <xsl:apply-templates select="imagedata"/>
275 <xsl:if test="$imagedata.boxed = '1'">
276 <xsl:text>}</xsl:text>
277 </xsl:if>
278 <xsl:if test="$figcount &gt; 1">
279 <xsl:text>}</xsl:text>
280 </xsl:if>
281 </xsl:template>
282
283 <xsl:template name="unit.eval">
284 <xsl:param name="length"/>
285 <xsl:param name="prop" select="''"/>
286 <xsl:choose>
287 <!-- percentage of something -->
288 <xsl:when test="contains($length, '%') and substring-after($length, '%')=''">
289 <xsl:value-of select="number(substring-before($length, '%')) div 100"/>
290 <xsl:value-of select="$prop"/>
291 </xsl:when>
292 <!-- pixel unit is not handled -->
293 <xsl:when test="contains($length, 'px') and substring-after($length, 'px')=''">
294 <xsl:message>Pixel unit not handled (replaced by pt)</xsl:message>
295 <xsl:value-of select="number(substring-before($length, 'px'))"/>
296 <xsl:text>pt</xsl:text>
297 </xsl:when>
298 <!-- no unit provided means pixel -->
299 <xsl:when test="$length and (number($length)=$length)">
300 <xsl:message>Pixel unit not handled (replaced by pt)</xsl:message>
301 <xsl:value-of select="$length"/>
302 <xsl:text>pt</xsl:text>
303 </xsl:when>
304 <!-- hope the unit is handled -->
305 <xsl:otherwise>
306 <xsl:value-of select="$length"/>
307 </xsl:otherwise>
308 </xsl:choose>
309 </xsl:template>
310
311 <xsl:template name="image.default.set">
312 <xsl:variable name="auto">
313 <xsl:call-template name="image.autosize"/>
314 </xsl:variable>
315 <xsl:choose>
316 <xsl:when test="$auto=1">
317 <!-- use the natural size up to the specified boundaries -->
318 <xsl:text>width=\imgwidth,height=\imgheight,keepaspectratio=true</xsl:text>
319 </xsl:when>
320 <xsl:otherwise>
321 <!-- put the parameter value as is -->
322 <xsl:value-of select="$imagedata.default.scale"/>
323 </xsl:otherwise>
324 </xsl:choose>
325 </xsl:template>
326
327 <!-- the latex macro to use to include a graphic depends on the environment -->
328 <xsl:template name="graphic.begin.get">
329 <xsl:choose>
330 <xsl:when test="ancestor::imageobjectco">
331 <xsl:apply-templates select="ancestor::imageobjectco" mode="graphic.begin"/>
332 </xsl:when>
333 <xsl:otherwise>
334 <xsl:text>\includegraphics</xsl:text>
335 </xsl:otherwise>
336 </xsl:choose>
337 </xsl:template>
338
339 <xsl:template name="graphic.end.get">
340 <xsl:if test="ancestor::imageobjectco">
341 <xsl:apply-templates select="ancestor::imageobjectco" mode="graphic.end"/>
342 </xsl:if>
343 </xsl:template>
344
345 <!-- ==================================================================== -->
346
347 <!-- Image filename to use -->
348 <xsl:template match="imagedata|graphic|inlinegraphic" mode="filename.get">
349 <xsl:choose>
350 <xsl:when test="@entityref">
351 <xsl:value-of select="unparsed-entity-uri(@entityref)"/>
352 </xsl:when>
353 <xsl:when test="@fileref">
354 <xsl:apply-templates select="@fileref"/>
355 </xsl:when>
356 </xsl:choose>
357 </xsl:template>
358
359 <!-- Resolve xml:base attributes (taken from the DocBook Project) -->
360 <xsl:template match="@fileref">
361 <!-- need a check for absolute urls -->
362 <xsl:choose>
363 <xsl:when test="contains(., ':') or starts-with(.,'/')">
364 <!-- it has a uri scheme or starts with '/', so it is an absolute uri -->
365 <xsl:value-of select="."/>
366 </xsl:when>
367 <xsl:when test="$keep.relative.image.uris != 0">
368 <!-- leave it alone -->
369 <xsl:value-of select="."/>
370 </xsl:when>
371 <xsl:otherwise>
372 <!-- its a relative uri -->
373 <xsl:call-template name="relative-uri">
374 </xsl:call-template>
375 </xsl:otherwise>
376 </xsl:choose>
377 </xsl:template>
378
379 <!-- ==================================================================== -->
380
381 <xsl:template match="imagedata" mode="viewport">
382 <xsl:choose>
383 <xsl:when test="(@width or @depth) and
384 (@contentwidth or @contentdepth or @scale or
385 (@scalefit and @scalefit='0'))">
386 <xsl:value-of select="1"/>
387 </xsl:when>
388 <xsl:otherwise>
389 <xsl:value-of select="0"/>
390 </xsl:otherwise>
391 </xsl:choose>
392 </xsl:template>
393
394 <!-- Process an imagedata -->
395
396 <xsl:template match="imagedata" name="imagedata">
397 <xsl:param name="layout.width" select="'\linewidth'"/>
398 <xsl:param name="layout.height" select="'\textheight'"/>
399
400 <xsl:variable name="graphic.begin">
401 <xsl:call-template name="graphic.begin.get"/>
402 </xsl:variable>
403 <xsl:variable name="graphic.end">
404 <xsl:call-template name="graphic.end.get"/>
405 </xsl:variable>
406 <xsl:variable name="piangle">
407 <xsl:call-template name="pi.dblatex_angle">
408 <xsl:with-param name="node" select=".."/>
409 </xsl:call-template>
410 </xsl:variable>
411
412 <xsl:variable name="filename">
413 <xsl:apply-templates select="." mode="filename.get"/>
414 </xsl:variable>
415 <xsl:variable name="width">
416 <xsl:call-template name="unit.eval">
417 <xsl:with-param name="length" select="@width"/>
418 <xsl:with-param name="prop" select="$layout.width"/>
419 </xsl:call-template>
420 </xsl:variable>
421 <xsl:variable name="depth">
422 <xsl:call-template name="unit.eval">
423 <xsl:with-param name="length" select="@depth"/>
424 <xsl:with-param name="prop" select="$layout.height"/>
425 </xsl:call-template>
426 </xsl:variable>
427 <!-- viewport is valid only if there's some viewport spec, and content or
428 scale. TDG says that viewport spec without content/scale and scalefit=0
429 is ignored. -->
430 <xsl:variable name="viewport">
431 <xsl:apply-templates select="." mode="viewport"/>
432 </xsl:variable>
433 <!-- check if some percentage is applied to the content -->
434 <xsl:variable name="widthperct">
435 <xsl:choose>
436 <xsl:when test="@contentwidth and contains(@contentwidth, '%') and
437 substring-after(@contentwidth, '%')=''">
438 <xsl:value-of select="number(substring-before(@contentwidth, '%'))
439 div 100"/>
440 </xsl:when>
441 <xsl:otherwise>
442 <xsl:value-of select="0"/>
443 </xsl:otherwise>
444 </xsl:choose>
445 </xsl:variable>
446 <xsl:variable name="depthperct">
447 <xsl:choose>
448 <xsl:when test="@contentdepth and contains(@contentdepth, '%') and
449 substring-after(@contentdepth, '%')=''">
450 <xsl:value-of select="number(substring-before(@contentdepth, '%'))
451 div 100"/>
452 </xsl:when>
453 <xsl:otherwise>
454 <xsl:value-of select="0"/>
455 </xsl:otherwise>
456 </xsl:choose>
457 </xsl:variable>
458
459 <xsl:text>{</xsl:text>
460 <xsl:if test="$viewport=1">
461 <xsl:text>\begin{minipage}[c]</xsl:text>
462 <xsl:if test="@depth">
463 <!-- depth -->
464 <xsl:text>[</xsl:text>
465 <xsl:value-of select="$depth"/>
466 <xsl:text>]</xsl:text>
467 <xsl:text>[</xsl:text>
468 <!-- vertical alignment (meaningfull only with depth) -->
469 <xsl:choose>
470 <xsl:when test="@valign='bottom'">
471 <xsl:text>b</xsl:text>
472 </xsl:when>
473 <xsl:when test="@valign='middle'">
474 <xsl:text>c</xsl:text>
475 </xsl:when>
476 <xsl:when test="@valign='top'">
477 <xsl:text>t</xsl:text>
478 </xsl:when>
479 <xsl:otherwise>
480 <xsl:text>c</xsl:text>
481 </xsl:otherwise>
482 </xsl:choose>
483 <xsl:text>]</xsl:text>
484 </xsl:if>
485 <!-- width -->
486 <xsl:text>{</xsl:text>
487 <xsl:choose>
488 <xsl:when test="@width">
489 <xsl:value-of select="$width"/>
490 </xsl:when>
491 <xsl:when test="ancestor::mediaobject">
492 <xsl:value-of select="$layout.width"/>
493 </xsl:when>
494 <!-- TODO: inline viewport area should be as wide as the graphic -->
495 <!-- <xsl:text>\figwidth</xsl:text> -->
496 <xsl:otherwise>
497 <xsl:value-of select="$layout.width"/>
498 </xsl:otherwise>
499 </xsl:choose>
500 <xsl:text>}&#10;</xsl:text>
501 </xsl:if>
502 <!-- horizontal alignment -->
503 <xsl:choose>
504 <xsl:when test="@align='center'">
505 <xsl:text>\centering </xsl:text>
506 </xsl:when>
507 <xsl:when test="@align='left'">
508 <xsl:text>\raggedright </xsl:text>
509 </xsl:when>
510 <xsl:when test="@align='right'">
511 <xsl:text>\raggedleft </xsl:text>
512 </xsl:when>
513 </xsl:choose>
514
515 <!-- find out the natural image size -->
516 <xsl:variable name="auto">
517 <xsl:call-template name="image.autosize"/>
518 </xsl:variable>
519 <xsl:if test="$auto=1 or $widthperct!=0 or $depthperct!=0">
520 <xsl:text>\imgevalsize{</xsl:text>
521 <xsl:value-of select="$filename"/>
522 <xsl:text>}</xsl:text>
523 </xsl:if>
524 <xsl:text>{</xsl:text>
525 <xsl:value-of select="$graphic.begin"/>
526 <xsl:text>[</xsl:text>
527 <!-- TDG says that content, scale and scalefit are mutually exclusive -->
528 <xsl:choose>
529 <!-- content area spec -->
530 <xsl:when test="@contentwidth or @contentdepth">
531 <xsl:choose>
532 <!-- special case where both content percentages are the same -->
533 <xsl:when test="$widthperct!=0 and $widthperct=$depthperct">
534 <xsl:text>scale=</xsl:text>
535 <xsl:value-of select="$widthperct"/>
536 </xsl:when>
537 <xsl:otherwise>
538 <xsl:if test="@contentwidth">
539 <xsl:text>width=</xsl:text>
540 <xsl:call-template name="unit.eval">
541 <xsl:with-param name="length" select="@contentwidth"/>
542 <xsl:with-param name="prop" select="'\imgrwidth'"/>
543 </xsl:call-template>
544 <xsl:text>,</xsl:text>
545 </xsl:if>
546 <xsl:if test="@contentdepth">
547 <xsl:text>height=</xsl:text>
548 <xsl:call-template name="unit.eval">
549 <xsl:with-param name="length" select="@contentdepth"/>
550 <xsl:with-param name="prop" select="'\imgrheight'"/>
551 </xsl:call-template>
552 </xsl:if>
553 </xsl:otherwise>
554 </xsl:choose>
555 </xsl:when>
556 <!-- scaling -->
557 <xsl:when test="@scale">
558 <xsl:text>scale=</xsl:text>
559 <xsl:value-of select="number(@scale) div 100"/>
560 </xsl:when>
561 <!-- only viewport area spec with scalefit -->
562 <xsl:when test="(not(@scalefit) or @scalefit='1') and (@width or @depth)">
563 <xsl:if test="@width">
564 <xsl:text>width=</xsl:text>
565 <xsl:value-of select="$width"/>
566 <xsl:text>,</xsl:text>
567 </xsl:if>
568 <xsl:if test="@depth">
569 <xsl:text>height=</xsl:text>
570 <xsl:value-of select="$depth"/>
571 <xsl:text>,</xsl:text>
572 </xsl:if>
573 <!-- TDG says that scale to fit cannot be anamorphic -->
574 <xsl:text>keepaspectratio=true</xsl:text>
575 </xsl:when>
576 <!-- default scaling (if any) -->
577 <xsl:otherwise>
578 <xsl:call-template name="image.default.set"/>
579 </xsl:otherwise>
580 </xsl:choose>
581 <!-- rotate the image? -->
582 <xsl:choose>
583 <xsl:when test="@format = 'PRN'">
584 <xsl:text>,angle=270</xsl:text>
585 </xsl:when>
586 <xsl:when test="$piangle != ''">
587 <xsl:text>,angle=</xsl:text>
588 <xsl:value-of select="$piangle"/>
589 </xsl:when>
590 </xsl:choose>
591 <xsl:text>]{</xsl:text>
592 <xsl:value-of select="$filename"/>
593 <xsl:text>}</xsl:text>
594 <xsl:value-of select="$graphic.end"/>
595 <xsl:text>}</xsl:text>
596 <xsl:if test="$viewport=1">
597 <xsl:text>\end{minipage}&#10;</xsl:text>
598 </xsl:if>
599 <xsl:text>}</xsl:text>
600 </xsl:template>
601
602
603 </xsl:stylesheet>
604