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

<!--
	Font and typeface control.
-->

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


	<!-- Typewriter (monospaced) type. -->
	<template name="typewriter" match="typewriter|monospace|tt">
		<common formats="/latex/xelatex/">
			<xsl:text>\texttt{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<tt><xsl:apply-templates /></tt>
		</common>
	</template>
	
	<!-- Sans serif type. -->
	<template name="sans-serif" match="sans-serif|sans|ss|sf">
		<common formats="/latex/xelatex/">
			<xsl:text>\textsf{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<span class="sans"><xsl:apply-templates /></span>
		</common>
	</template>
	
	<!--
		Set a specific typeface. This is kind of tricky, since standard LaTeX and XeLaTeX use different font naming schemes, and in (X)HTML we also need to be able to specify fallbacks. The upshot is that you have to provide three different font specifications for the same typeface!
		
		Note that if the relevant attribute for the current format is omitted, then the font element will be ignored.
		
		@latex: The name of the typeface for standard LaTeX, e.g., "bfu".
		
		@xelatex: The name of the typeface for XeLaTeX, e.g., "Futura Book".
		
		@html: The name of the typeface followed by a list of fallbacks, e.g., "'Futura Book', Helvetica, Arial, sans-serif". Font names containing whitespace, numbers or punctuation should be enclosed in SINGLE quotes.
		
		@fontspec-options: Any options that apply to the typeface when loading with fontspec, e.g., "Scale=MatchLowercase".
	-->
	<template name="font" match="font|typeface">
		<common formats="/latex/">
			<xsl:if test="@latex">
				<xsl:text>\fontfamily{</xsl:text>
				<xsl:value-of select="@latex" />
				<xsl:text>}\selectfont</xsl:text>
			</xsl:if>
			<xsl:apply-templates />
			<xsl:if test="@latex">
				<xsl:text>\normalfont</xsl:text>
			</xsl:if>
		</common>
		<common formats="/xelatex/">
			<xsl:if test="@xelatex">
				<xsl:text>\fontspec</xsl:text>
				<xsl:if test="@fontspec-options">
					<xsl:text>[</xsl:text>
					<xsl:value-of select="@fontspec-options" />
					<xsl:text>]</xsl:text>
				</xsl:if>
				<xsl:text>{</xsl:text>
				<xsl:value-of select="@xelatex" />
				<xsl:text>}</xsl:text>
			</xsl:if>
			<xsl:apply-templates />
			<xsl:if test="@xelatex">
				<xsl:text>\normalfont</xsl:text>
			</xsl:if>
		</common>
		<common formats="/html/xhtml/">
		    <!-- Note that this will insert a redundant SPAN if the @html attribute is omitted. -->
			<span>
				<xsl:if test="@html">
					<xsl:attribute name="style">
						<xsl:text>font-family: </xsl:text>
						<xsl:value-of select="@html" />
						<xsl:text>;</xsl:text>
					</xsl:attribute>
					<xsl:apply-templates />
				</xsl:if>
			</span>
		</common>
	</template>
	
	
	<!--
		Font sizing, using relative font size elements borrowed from LaTeX. Note that we use CSS relative font sizes (smaller and larger), which means that we have to string a bunch of them together to achieve the same effect as a single command in LaTeX. We can't use absolute sizes in CSS as they would probably look different on different machines.
	-->
	<template name="tiny" match="tiny">
		<common formats="/latex/xelatex/">
			<xsl:text>{\tiny </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">4</xsl:with-param>
				<xsl:with-param name="size" >smaller</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="scriptsize" match="scriptsize">
		<common formats="/latex/xelatex/">
			<xsl:text>{\scriptsize </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">3</xsl:with-param>
				<xsl:with-param name="size" >smaller</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="footnotesize" match="footnotesize">
		<common formats="/latex/xelatex/">
			<xsl:text>{\footnotesize </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">2</xsl:with-param>
				<xsl:with-param name="size" >smaller</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="small" match="small">
		<common formats="/latex/xelatex/">
			<xsl:text>{\small </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">1</xsl:with-param>
				<xsl:with-param name="size" >smaller</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="large" match="large">
		<common formats="/latex/xelatex/">
			<xsl:text>{\large </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">1</xsl:with-param>
				<xsl:with-param name="size" >larger</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<!-- Fortunately, XML is case-sensitive. -->
	<template name="Large" match="Large">
		<common formats="/latex/xelatex/">
			<xsl:text>{\Large </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">2</xsl:with-param>
				<xsl:with-param name="size" >larger</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="LARGE" match="LARGE">
		<common formats="/latex/xelatex/">
			<xsl:text>{\LARGE </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">3</xsl:with-param>
				<xsl:with-param name="size" >larger</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="huge" match="huge">
		<common formats="/latex/xelatex/">
			<xsl:text>{\huge </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">4</xsl:with-param>
				<xsl:with-param name="size" >larger</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<template name="Huge" match="Huge">
		<common formats="/latex/xelatex/">
			<xsl:text>{\Huge </xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:call-template name="generate-html-fontsize">
				<xsl:with-param name="repetitions">5</xsl:with-param>
				<xsl:with-param name="size" >larger</xsl:with-param>
			</xsl:call-template>
		</common>
	</template>
	
	<!--
		Utility template to generate the appropriate number of <span> elements to set the relative font size correctly in HTML.
		
		$repetitions: The number of times to repeat the <span>. [default 1]
		
		$size: The "direction" of the relative font size.
			'smaller' [default]
			'larger'
	-->
	<template name="generate-html-fontsize">
		<common formats="/html/xhtml/">
			<xsl:param name="repetitions">1</xsl:param>
			<xsl:param name="size">smaller</xsl:param>
			<xsl:for-each select="1 to $repetitions">
				<!--
					Unfortunately, we have to generate the <span> elements as raw text rather than proper elements because of the separation of the opening and closing tags across different loops.
				-->
				<xsl:text disable-output-escaping="yes">&lt;span style="font-size:</xsl:text>
				<xsl:value-of select="$size" />
				<xsl:text disable-output-escaping="yes">;"&gt;</xsl:text>
			</xsl:for-each>
			<xsl:apply-templates />
			<xsl:for-each select="1 to $repetitions">
				<xsl:text disable-output-escaping="yes">&lt;/span&gt;</xsl:text>
			</xsl:for-each>
		</common>
	</template>
	

	<!-- Non-maths super- and subscript. -->
	<template name="superscript" match="superscript">
		<common formats="/latex/xelatex/">
		    <xsl:if test="ancestor::section">
			    <xsl:text>\texorpdfstring{</xsl:text>
			</xsl:if>
			<xsl:text>\textsuperscript{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		    <xsl:if test="ancestor::section">
			    <xsl:text>}{(</xsl:text>
                <xsl:apply-templates />
                <xsl:text>)}</xsl:text>
			</xsl:if>
		</common>
		<common formats="/html/xhtml/">
			<sup><xsl:apply-templates /></sup>
		</common>
	</template>
	
	<template name="subscript" match="subscript">
		<common formats="/latex/xelatex/">
		    <xsl:if test="ancestor::section">
			    <xsl:text>\texorpdfstring{</xsl:text>
			</xsl:if>
			<xsl:text>\textsubscript{</xsl:text>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		    <xsl:if test="ancestor::section">
			    <xsl:text>}{(</xsl:text>
                <xsl:apply-templates />
                <xsl:text>)}</xsl:text>
			</xsl:if>
		</common>
		<common formats="/html/xhtml/">
			<sub><xsl:apply-templates /></sub>
		</common>
	</template>


</stylesheet>