<?xml version="1.0" encoding="UTF-8"?>
<!--
 - Desc:    This file is part of the eCromedos document preparation system
 - Date:    2006/03/09
 - Author:  Tobias Koch (tkoch@ecromedos.net)
 - License: GNU General Public License, version 2
 - URL:     http://www.ecromedos.net
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--
  - Extrac column width attribute
-->
<xsl:template name="table.colwidth">
	<xsl:param name="width"/>
	<xsl:choose>
		<xsl:when test="substring($width, string-length($width), string-length($width)) = '%'">
			<xsl:variable name="percent">
				<xsl:value-of select="number(substring($width, 1, string-length($width) - 1))"/>
			</xsl:variable>
			<xsl:value-of select="$percent div 100.0"/>
			<xsl:text>\ecmdstablewidth</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$width"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Extract table width attribute
-->
<xsl:template name="table.totalwidth">
	<xsl:param name="width"/>
	<xsl:choose>
		<xsl:when test="substring($width, string-length($width), string-length($width)) = '%'">
			<xsl:variable name="percent">
				<xsl:value-of select="number(substring($width, 1, string-length($width) - 1))"/>
			</xsl:variable>
			<xsl:value-of select="$percent div 100.0"/>
			<xsl:text>\linewidth</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$width"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Set a table
-->
<xsl:template match="table">

	<!-- number of columns -->
	<xsl:variable name="columns" select="count(colgroup/col)"/>

	<!-- set rulewidth -->
	<xsl:text>&#x0a;\setlength{\arrayrulewidth}{</xsl:text>
	<xsl:choose>
		<xsl:when test="@print-rulewidth">
			<xsl:value-of select="normalize-space(@print-rulewidth)"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>0.76pt</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>}&#x0a;</xsl:text>
	
	<!-- set rulecolor -->
	<xsl:text>&#x0a;\arrayrulecolor[rgb]{</xsl:text>
		<xsl:choose>
			<xsl:when test="@rulecolor">
				<xsl:call-template name="color.triple">
					<xsl:with-param name="rgb" select="normalize-space(@rulecolor)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>0.0,0.0,0.0</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	<xsl:text>}&#x0a;</xsl:text>

	<!-- adjust padding -->
	<xsl:text>\setlength{\ecmdscolsep}{\tabcolsep - \arrayrulewidth}&#x0a;</xsl:text>

	<!-- calculate table width without padding -->
	<xsl:text>&#x0a;\setlength{\ecmdstablewidth}{</xsl:text>
	<xsl:choose>
		<xsl:when test="@print-width">
			<xsl:call-template name="table.totalwidth">
				<xsl:with-param name="width" select="normalize-space(@print-width)"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>\linewidth</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text> - \tabcolsep * 2 * </xsl:text>
	<xsl:value-of select="$columns"/>
	<xsl:text> - \arrayrulewidth * </xsl:text>
	<xsl:value-of select="
		count(colgroup/col[(position() != last()) 
		and contains(@frame, 'colsep')])"/>
	<xsl:if test="contains(@frame, 'left')">
		<xsl:text> - \arrayrulewidth </xsl:text>
	</xsl:if>
	<xsl:if test="contains(@frame, 'right')">
		<xsl:text> - \arrayrulewidth </xsl:text>
	</xsl:if>
	<xsl:text>}&#x0a;</xsl:text>

	<!-- decide whether to use longtable or tabular -->
	<xsl:choose>
		<xsl:when test="@float = 'yes' or $global.columns > 1">
			<xsl:call-template name="table.tabular" select="."/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="table.longtable" select="."/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:if test="name(following-sibling::*[1]) = 'table'">
		<xsl:text>\vspace{-\topsep}</xsl:text>
	</xsl:if>
</xsl:template>

<!--
  - Set floating tables or while in multicolumn mode with 'tabular' environment
-->
<xsl:template name="table.tabular">

	<!-- open table environment -->
	<xsl:text>\begin{table}[</xsl:text>
	<xsl:choose>
		<xsl:when test="@float='yes'">
			<xsl:text>h!</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>H</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>]&#x0a;</xsl:text>
	<xsl:choose>
		<xsl:when test="@align = 'left'">
			<xsl:text></xsl:text>
		</xsl:when>
		<xsl:when test="@align = 'right'">
			<xsl:text>\hfill{}&#x0a;</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>\centering{}&#x0a;</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>\begin{minipage}{</xsl:text>
	<xsl:choose>
		<xsl:when test="@print-width">
			<xsl:call-template name="table.totalwidth">
				<xsl:with-param name="width" select="normalize-space(@print-width)"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>1.0\linewidth</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>}&#x0a;</xsl:text>
	<!-- caption -->
	<xsl:if test="caption">
		<xsl:text>\caption{</xsl:text>
		<xsl:value-of select="normalize-space(caption)"/>
		<xsl:text>}&#x0a;</xsl:text>
	</xsl:if>
	<xsl:apply-templates select="label"/>
	<!-- start table -->
	<xsl:text>\begin{tabular}{</xsl:text>
		<xsl:for-each select="colgroup/col">
			<xsl:text> c </xsl:text>
		</xsl:for-each>
		<xsl:text>}&#x0a;</xsl:text>
		<!-- table top rule if no table head -->
		<xsl:if test="contains(@frame, 'top') and not(th)">
			<xsl:text>\hhline{</xsl:text>
			<xsl:for-each select="colgroup/col">
				<xsl:text>-</xsl:text>
			</xsl:for-each>
			<xsl:text>}&#x0a;</xsl:text>
		</xsl:if>
		<!-- table head -->
		<xsl:apply-templates select="th"/>
		<!-- table rows -->
		<xsl:apply-templates select="tr"/>
	<xsl:text>\end{tabular}</xsl:text>
	<xsl:text>\end{minipage}&#x0a;</xsl:text>
	<xsl:text>\end{table}&#x0a;</xsl:text>

</xsl:template>

<!--
  - Set table with 'longtable' environment
-->
<xsl:template name="table.longtable">

	<!-- open longtable environment -->
	<xsl:text>\begin{longtable}[</xsl:text>
	<xsl:choose>
		<xsl:when test="@align='left'">
			<xsl:text>l</xsl:text>
		</xsl:when>
		<xsl:when test="@align='right'">
			<xsl:text>r</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>c</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:text>]{</xsl:text>
		<xsl:for-each select="colgroup/col">
			<xsl:text> c </xsl:text>
		</xsl:for-each>
		<xsl:text>}&#x0a;</xsl:text>
		<!-- caption -->
		<xsl:if test="caption">
			<xsl:text>\caption{</xsl:text>
			<xsl:value-of select="normalize-space(caption)"/>
			<xsl:apply-templates select="label"/>
			<xsl:text>}\tabularnewline&#x0a;</xsl:text>
		</xsl:if>
		<!-- table head -->
		<xsl:apply-templates select="th"/>
		<xsl:text>\endfirsthead&#x0a;</xsl:text>
		<xsl:if test="caption">
			<xsl:text>\caption{</xsl:text>
			<xsl:value-of select="normalize-space(caption)"/>
			<xsl:text>}\tabularnewline&#x0a;</xsl:text>
		</xsl:if>
		<!-- table head -->
		<xsl:apply-templates select="th"/>
		<xsl:text>\endhead&#x0a;</xsl:text>

		<!-- table top rule if no table head -->
		<xsl:if test="contains(@frame, 'top') and not(th)">
			<xsl:text>\hhline{</xsl:text>
			<xsl:for-each select="colgroup/col">
				<xsl:text>-</xsl:text>
			</xsl:for-each>
			<xsl:text>}&#x0a;</xsl:text>
		</xsl:if>

		<!-- table rows -->
		<xsl:apply-templates select="tr"/>
	<xsl:text>\end{longtable}</xsl:text>
	
	<!-- setup text for footnotes in table -->
	<xsl:call-template name="footnotetext"/>
	<xsl:text>&#x0a;&#x0a;</xsl:text>
</xsl:template>

<!--
  - Catch table row
-->
<xsl:template match="tr">
	<xsl:call-template name="table.tr"/>
</xsl:template>

<!--
  - Catch table header row
-->
<xsl:template match="th">
	<!-- table top -->
	<xsl:if test="contains(parent::table/@frame, 'top')">
		<xsl:text>\hhline{</xsl:text>
		<xsl:for-each select="parent::table/colgroup/col">
			<xsl:text>-</xsl:text>
		</xsl:for-each>
		<xsl:text>}&#x0a;</xsl:text>
	</xsl:if>
	<xsl:call-template name="table.tr"/>
</xsl:template>

<!--
  - Set a table row
-->
<xsl:template name="table.tr">

	<!-- process row cells -->
	<xsl:for-each select="td">
		<xsl:if test="not(position() = 1)">
			<xsl:text disable-output-escaping="yes"> &amp; </xsl:text>
		</xsl:if>
		<xsl:call-template name="table.td"/>
	</xsl:for-each>
	<xsl:text>\tabularnewline&#x0a;</xsl:text>

	<!-- print border -->
	<xsl:variable name="border">
		<xsl:for-each select="td">
			<xsl:if test="(position() = 1) and
				contains(parent::*/parent::*/@frame, 'left')">
				<xsl:text>|</xsl:text>
			</xsl:if>
			<!-- row separator -->
			<xsl:choose>
				<xsl:when test="
					((contains(@frame, 'rowsep') or
					contains(parent::*/@frame, 'rowsep') or
					contains(parent::*/parent::*/@frame, 'rowsep'))
					and	parent::*/following-sibling::tr)
					or
					(not(parent::*/following-sibling::tr) and
					contains(parent::*/parent::*/@frame, 'bottom'))">
					<xsl:if test="@colspan">
						<xsl:text>*{</xsl:text>
						<xsl:value-of select="number(normalize-space(@colspan))"/>
						<xsl:text>}{</xsl:text>
					</xsl:if>
					<xsl:choose>
						<xsl:when test="parent::th">
							<xsl:text>=</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>-</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:if test="@colspan">
						<xsl:text>}</xsl:text>
					</xsl:if>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>~</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="
				((contains(@frame, 'colsep') or
				contains(parent::*/@frame, 'colsep') or
				contains(parent::*/parent::*/@frame, 'colsep'))
				and	not(position() = last()))
				or
				((position() = last()) and
				contains(parent::*/parent::*/@frame, 'right'))">
				<xsl:text>|</xsl:text>
			</xsl:if>
		</xsl:for-each>
	</xsl:variable>
	<xsl:if test="contains($border, '-') or contains($border, '=')">
		<xsl:text>\hhline{</xsl:text>
		<xsl:value-of select="$border"/>
		<xsl:text>}&#x0a;</xsl:text>
	</xsl:if>
</xsl:template>

<!--
  - Set a table cell
-->
<xsl:template name="table.td">

		<!-- colspan -->
		<xsl:variable name="colspan">
			<xsl:choose>
				<xsl:when test="@colspan &gt; 1">
					<xsl:value-of select="number(@colspan)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:number value="1"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- column position -->
		<xsl:variable name="column">
			<xsl:value-of select="
				count(preceding-sibling::td[not(@colspan &gt; 1)]) + 
				sum(preceding-sibling::td[@colspan &gt; 1]/@colspan) + 1"/>
		</xsl:variable>

		<!-- column width -->
		<xsl:variable name="colwidth">
			<xsl:for-each select="
				ancestor::table[1]/colgroup/col[
					position() &gt;= $column and
					position() &lt; $column + number($colspan)]">
				<xsl:call-template name="table.colwidth">
					<xsl:with-param name="width" select="@width"/>
				</xsl:call-template>
				<xsl:if test="position() != last()">
					<xsl:text> + </xsl:text>
				</xsl:if>
			</xsl:for-each>
			<!-- if this is a multicolumn, we have to add -->
			<xsl:if test="$colspan &gt; 1">
				<xsl:text> + \tabcolsep * </xsl:text>
				<xsl:value-of select="(number($colspan) - 1) * 2"/>
			</xsl:if>
		</xsl:variable>

		<!-- color your life -->
		<xsl:variable name="rgb">
			<xsl:choose>
				<xsl:when test="@color">
					<xsl:value-of select="normalize-space(@color)"/>
				</xsl:when>
				<xsl:when test="parent::*/@color">
					<xsl:value-of select="normalize-space(parent::*/@color)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text></xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- start multicolumn -->
		<xsl:text>\multicolumn{</xsl:text>
		<xsl:value-of select="$colspan"/>
		<xsl:text>}{</xsl:text>

		<!-- left table border -->
		<xsl:if test="
			position() = 1 and 
			contains(parent::*/parent::*/@frame, 'left')">
			<xsl:text>|</xsl:text>
		</xsl:if>

		<!-- clear left cell border -->
		<xsl:if test="position() != 1">
			<xsl:text>@{\hspace{\tabcolsep}} </xsl:text>
		</xsl:if>

		<!-- color cell -->
		<xsl:if test="$rgb != ''">
			<xsl:text>&gt;{\columncolor[rgb]{</xsl:text>
			<xsl:call-template name="color.triple">
				<xsl:with-param name="rgb" select="$rgb"/>
			</xsl:call-template>
			<xsl:text>}}</xsl:text>
		</xsl:if>

		<!-- vertical row alignment -->
		<xsl:choose>
			<xsl:when test="parent::*/@valign = 'middle'">
				<xsl:text>m{</xsl:text>
			</xsl:when>
			<xsl:when test="parent::*/@valign = 'bottom'">
				<xsl:text>b{</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>p{</xsl:text>
			</xsl:otherwise>
		</xsl:choose>

		<!-- set cell width -->
		<xsl:value-of select="$colwidth"/>
		<xsl:text>} </xsl:text>
		
		<!-- clear right cell border -->
		<xsl:if test="position() != last()">
			<xsl:text>@{\hspace{\ecmdscolsep}} </xsl:text>
		</xsl:if>

		<!-- column separator, right border -->
		<xsl:if test="
			((contains(@frame, 'colsep') or
			contains(parent::*/@frame, 'colsep') or
			contains(parent::*/parent::*/@frame, 'colsep')) and
			not(position() = last()))
			or
			((position() = last()) and
			contains(parent::*/parent::*/@frame, 'right'))">
			<xsl:text>|</xsl:text>
		</xsl:if>
		<xsl:text>}{</xsl:text>

		<!-- text alignment -->
		<xsl:choose>
			<xsl:when test="@align = 'right'">
				<xsl:text>\raggedleft&#x0a;</xsl:text>
			</xsl:when>
			<xsl:when test="@align = 'center'">
				<xsl:text>\centering&#x0a;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>\raggedright&#x0a;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>

		<!-- process cell content -->
		<xsl:apply-templates/>

		<!-- end multicolumn -->
		<xsl:text>}</xsl:text>
</xsl:template>

</xsl:stylesheet>
