Newer
Older
XML / modules / images.xml
<?xml version="1.0" encoding="utf-8"?>

<!--
	Image handling. Includes support for normal and "zoomed" images in HTML.
-->

<stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


	<!--
		LaTeX images are always handled the same way regardless. Image scaling factor is supported.
		
		TODO: alternatively, allow defining the scaling factor by specifying what proportion of the page width the image should occupy (a la LaTeX [width] parameter).
	-->
	<template name="latex-image" match="image" mode="latex">
		<common formats="/latex/xelatex/">
			<xsl:text>\includegraphics</xsl:text>
			<!--
				General-purpose latex pass-through arguments for \includegraphics. If providing multiple arguments, these should be comma-separated in the source XML, but do not need the enclosing square brackets.
			-->
			<xsl:if test="@latex-options">
				<xsl:text>[</xsl:text>
				<xsl:value-of select="@latex-options" />
				<xsl:text>]</xsl:text>
			</xsl:if>
			<xsl:text>{</xsl:text>
			<xsl:value-of select="infosci:build-image-path( @location, @basename, 'print', @format )" />
			<xsl:text>}</xsl:text>
		</common>
	</template>
	
	
	<!--
		"Standard" scale image. For LaTeX, we simply pass off to the "latex-image" template above.
	-->
	<template name="normal-image" match="image[count(provide-large-version) = 0 and not(@inline)]">
		<common>
			<!--
				Check for obsolete child elements. We need to do this explicitly, as there isn't an apply-templates inside this template to pick them up automatically.
			-->
			<xsl:apply-templates select="basename|format|latex-scaling" />
			
			<!--
				Check for existence of required attributes. For example, it doesn't really make sense for @basename to be missing.
			-->
			<xsl:if test="not( @basename )">
				<xsl:message terminate="yes">
					<xsl:text>ERROR: image element "</xsl:text>
					<!-- Note that the description may be missing, which would make the error message less helpful :). -->
					<xsl:value-of select="description" />
					<xsl:text>" missing required @basename attribute.</xsl:text>
				</xsl:message>
			</xsl:if>
		</common>
		<common formats="/latex/xelatex/"><xsl:apply-templates select="." mode="latex" /></common>
		<common formats="/html/xhtml/">
			<img class="padded" style="border-style: none;">
				<xsl:attribute name="src">
					<xsl:value-of select="infosci:build-image-path( @location, @basename, 'web', @format )" />
				</xsl:attribute>
				
				<xsl:attribute name="alt">
					<xsl:apply-templates select="description" />
					<xsl:if test="not( description )">
						<xsl:value-of select="@basename" />
					</xsl:if>
				</xsl:attribute>
			</img>
		</common>
	</template>
	
	
	<!--
		"Zoomed" image. For LaTeX, we simply pass off to the "latex-image" template above.
	-->
	<template name="zoomable-image" match="image[count(provide-large-version) != 0]">
		<common>
			<!--
				Check for obsolete child elements. We need to do this explicitly, as there isn't an apply-templates inside this template to pick them up automatically.
			-->
			<xsl:apply-templates select="basename|format|latex-scaling" />
			
			<!--
				Check for existence of required attributes. For example, it doesn't really make sense for @basename to be missing.
			-->
			<xsl:if test="not( @basename )">
				<xsl:message terminate="yes">
					<xsl:text>ERROR: image element "</xsl:text>
					<!-- Note that the description may be missing, which would make the error message less helpful :). -->
					<xsl:value-of select="description" />
					<xsl:text>" missing required @basename attribute.</xsl:text>
				</xsl:message>
			</xsl:if>
		</common>
		<common formats="/latex/xelatex/"><xsl:apply-templates select="." mode="latex" /></common>
		<common formats="/html/xhtml/">
			<a>
				<xsl:attribute name="href">
					<xsl:value-of select="infosci:build-image-path( @location, @basename, 'web-zoom', @format )" />
				</xsl:attribute>
				
				<img class="padded" style="border-style: none;">
					<xsl:attribute name="src">
						<xsl:value-of select="infosci:build-image-path( @location, @basename, 'web', @format )" />
					</xsl:attribute>
					
					<xsl:attribute name="alt">
						<xsl:apply-templates select="description" />
						<xsl:if test="not( description )">
							<xsl:value-of select="@basename" />
						</xsl:if>
					</xsl:attribute>
				</img>
				
				<br clear="right" />
				<xsl:text>(Larger version)</xsl:text>
			</a>
		</common>
	</template>
	
	
	<!--
		"Inline" image. These shouldn't be padded and should be about the same height as the text. 0.85em seems about right for (Xe)LaTeX, 1em with vertical-align middle for (X)HTML. Ideally only used for icons and the like.
	-->
	<template name="inline-image" match="image[@inline = 'yes']">
		<common>
			<!--
				Check for obsolete child elements. We need to do this explicitly, as there isn't an apply-templates inside this template to pick them up automatically.
			-->
			<xsl:apply-templates select="basename|format|latex-scaling" />
			
			<!--
				Check for existence of required attributes. For example, it doesn't really make sense for @basename to be missing.
			-->
			<xsl:if test="not( @basename )">
				<xsl:message terminate="yes">
					<xsl:text>ERROR: image element "</xsl:text>
					<!-- Note that the description may be missing, which would make the error message less helpful :). -->
					<xsl:value-of select="description" />
					<xsl:text>" missing required @basename attribute.</xsl:text>
				</xsl:message>
			</xsl:if>
		</common>
		<common formats="/latex/xelatex/">
			<xsl:text>\includegraphics</xsl:text>
			<!--
				General-purpose latex pass-through arguments for \includegraphics. If providing multiple arguments, these should be comma-separated in the source XML, but do not need the enclosing square brackets.
			-->
            <xsl:text>[height=0.85em,keepaspectratio</xsl:text>
			<xsl:if test="@latex-options">
			    <xsl:text>,</xsl:text>
				<xsl:value-of select="@latex-options" />
			</xsl:if>
            <xsl:text>]</xsl:text>
			<xsl:text>{</xsl:text>
			<xsl:value-of select="infosci:build-image-path( @location, @basename, 'print', @format )" />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<img style="padding: 0px; border-style: none; height: 1em; vertical-align: middle;">
				<xsl:attribute name="src">
					<xsl:value-of select="infosci:build-image-path( @location, @basename, 'web', @format )" />
				</xsl:attribute>
				
				<xsl:attribute name="alt">
					<xsl:apply-templates select="description" />
					<xsl:if test="not( description )">
						<xsl:value-of select="@basename" />
					</xsl:if>
				</xsl:attribute>
			</img>
		</common>
	</template>
	
	
	<!--
		Work out the full path specification for an image file to be included = base-path + location (if specified) + basename + suffix + format. With all arguments provided (see example values below), the result will look something like this:
			/base/path/images/an_image-print.pdf
			/base/path/images/an_image-web.png
		
		$base-path is a style sheet global variable; see xml2xslt.xsl.
		
		$location [optional]: The relative path to the image file from the current location (e.g., "images"). If not specified (empty), then this component is omitted.
		
		$basename: The base name of the image file, e.g., "an_image".
		
		$basename-suffix [optional]: Any string to append to the base name. This is usually used to distinguish the web and print versions of an image, e.g., "web" vs. "print". If not specified (empty), then this component is omitted.
		
		$format [optional]: The suffix for the image file, e.g., "png" or "pdf". If not specified (empty), then the global default suffix ($image-format) is used.
	-->
	<function name="infosci:build-image-path" as="xs:string">
		<common>
			<xsl:param name="location" />
			<xsl:param name="basename" />
			<xsl:param name="basename-suffix" />
			<xsl:param name="format" />
			
			<xsl:sequence select="concat($base-path,
										 '/',
										 if (string-length($location) gt 0 )
										 then concat($location, '/')
										 else '',
										 $basename,
										 if (string-length($basename-suffix) gt 0 )
										 then concat('-', $basename-suffix)
										 else '',
										 '.',
										 if (string-length($format ) gt 0 )
										 then $format
										 else $image-format)" />
		</common>
	</function>


</stylesheet>