<?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">

<!--
  - Calculate the width over multiple columns
-->
<xsl:template name="table.colwidth">
	<xsl:param name="column"/>
	<xsl:param name="colspan"/>
	<xsl:variable name="width">
		<xsl:for-each select="ancestor::table[1]/colgroup/col[position() = $column]">
			<xsl:call-template name="table.colwidthsum">
				<xsl:with-param name="depth" select="$colspan - 1"/>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:variable>
	<xsl:value-of select="$width"/><xsl:text>%</xsl:text>
</xsl:template>

<!--
  - Recursive helper for adding colunm widths
  - Called by 'table.colwidth' above.
-->
<xsl:template name="table.colwidthsum">
	<xsl:param name="depth"/>
	<xsl:choose>
		<xsl:when test="$depth &lt;= 0">
			<xsl:value-of select="normalize-space(substring-before(@width, '%'))"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="nextwidth">
				<xsl:for-each select="following-sibling::col[1]">
					<xsl:call-template name="table.colwidthsum">
						<xsl:with-param name="depth" select="$depth - 1"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:variable>
			<xsl:variable name="selfwidth">
				<xsl:value-of select="normalize-space(substring-before(@width, '%'))"/>
			</xsl:variable>
			<xsl:value-of select="$selfwidth + $nextwidth"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Return double the current table rule width
-->
<xsl:template name="table.doublerulesep">
	<xsl:param name="rulewidth"/>
	<xsl:variable name="number">
		<xsl:value-of select="number(normalize-space(translate($rulewidth, 'pxptemex', '        ')))"/>
	</xsl:variable>
	<xsl:variable name="unit">
		<xsl:value-of select="normalize-space(translate($rulewidth, '0123456789', '          '))"/>
	</xsl:variable>
	<xsl:value-of select="2 * $number"/>
	<xsl:value-of select="$unit"/>
</xsl:template>

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

	<!-- frame rulesize -->
	<xsl:variable name="rulewidth">
		<xsl:choose>
			<xsl:when test="@screen-rulewidth">
				<xsl:value-of select="normalize-space(@screen-rulewidth)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>1px</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<!-- frame rulecolor -->
	<xsl:variable name="rulecolor">
		<xsl:choose>
			<xsl:when test="@rulecolor">
				<xsl:value-of select="normalize-space(@rulecolor)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>#000000</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- rule -->
	<xsl:variable name="rule">
		<xsl:value-of select="$rulewidth"/>
		<xsl:text> solid </xsl:text>
		<xsl:value-of select="$rulecolor"/>
		<xsl:text>;</xsl:text>
	</xsl:variable>

	<!-- outer frame -->
	<xsl:variable name="style">
		<xsl:text>border-collapse: collapse;</xsl:text>
		<xsl:if test="contains(@frame, 'left')">
			<xsl:text>border-left: </xsl:text>
			<xsl:value-of select="$rule"/>
		</xsl:if>
		<xsl:if test="contains(@frame, 'right')">
			<xsl:text>border-right: </xsl:text>
			<xsl:value-of select="$rule"/>
		</xsl:if>
		<xsl:if test="contains(@frame, 'top')">
			<xsl:text>border-top: </xsl:text>
			<xsl:value-of select="$rule"/>
		</xsl:if>
		<xsl:if test="contains(@frame, 'bottom')">
			<xsl:text>border-bottom: </xsl:text>
			<xsl:value-of select="$rule"/>
		</xsl:if>
	</xsl:variable>

	<!-- width -->
	<xsl:variable name="width">
		<xsl:if test="@screen-width">
			<xsl:text>width:</xsl:text>
			<xsl:value-of select="normalize-space(@screen-width)"/>
			<xsl:text>;</xsl:text>
		</xsl:if>
	</xsl:variable>

	<!-- alignment -->
	<xsl:variable name="alignment">
		<xsl:choose>
			<xsl:when test="not(@align)">
				<xsl:text>center</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="normalize-space(@align)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- combined counter -->
	<xsl:variable name="prefix">
		<xsl:call-template name="table.prefix"/>
	</xsl:variable>

	<!-- container -->
	<table border="0" cellspacing="0" cellpadding="0" width="100%" class="block-element">
		<xsl:if test="caption">
		<tr>
			<td align="{$alignment}">
				<table border="0" cellspacing="0" cellpadding="0" style="{$width}">
					<tr align="center">
						<td>
							<xsl:if test="$prefix != ''">
							<b>
							<xsl:call-template name="i18n.print">
								<xsl:with-param name="key" select="'table'"/>
								<xsl:with-param name="number" select="$prefix"/>
							</xsl:call-template>
							<xsl:text>: </xsl:text>
							</b>
							</xsl:if>
							<xsl:apply-templates select="caption"/>
						</td>
					</tr>
					<tr>
						<td style="font-size: xx-small;">
							<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		</xsl:if>
		<tr>
			<td align="{$alignment}">
			<!-- label -->
			<xsl:apply-templates select="label"/>
			<!-- table -->
			<table border="0" cellspacing="0" cellpadding="2" style="{$style}{$width}">
				<xsl:for-each select="th">
					<xsl:call-template name="table.tr">
						<xsl:with-param name="rulewidth" select="$rulewidth"/>
						<xsl:with-param name="rule" select="$rule"/>
						<xsl:with-param name="rulecolor" select="$rulecolor"/>
					</xsl:call-template>
				</xsl:for-each>
				<xsl:for-each select="tr">
					<xsl:call-template name="table.tr">
						<xsl:with-param name="rulewidth" select="$rulewidth"/>
						<xsl:with-param name="rule" select="$rule"/>
						<xsl:with-param name="rulecolor" select="$rulecolor"/>
					</xsl:call-template>
				</xsl:for-each>
			</table>
			</td>
		</tr>
	</table>
</xsl:template>

<!--
  - Set a table row
-->
<xsl:template name="table.tr">
	<xsl:param name="rule"/>
	<xsl:param name="rulewidth"/>
	<xsl:param name="rulecolor"/>
	<tr>
		<xsl:for-each select="td">
			<xsl:call-template name="table.td">
				<xsl:with-param name="rule" select="$rule"/>
				<xsl:with-param name="rulewidth" select="$rulewidth"/>
				<xsl:with-param name="rulecolor" select="$rulecolor"/>
			</xsl:call-template>
		</xsl:for-each>
	</tr>
</xsl:template>

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

	<xsl:param name="rule"/>
	<xsl:param name="rulewidth"/>
	<xsl:param name="rulecolor"/>

	<!-- 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 span -->
	<xsl:variable name="colspan">
		<xsl:choose>
			<xsl:when test="@colspan &gt; 1">
				<xsl:value-of select="normalize-space(@colspan)"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:number value="1"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- column width -->
	<xsl:variable name="width">
		<xsl:call-template name="table.colwidth">
			<xsl:with-param name="column" select="$column"/>
			<xsl:with-param name="colspan" select="$colspan"/>
		</xsl:call-template>
	</xsl:variable>

	<xsl:variable name="style">
		<xsl:text>width:</xsl:text>
		<xsl:value-of select="$width"/>
		<xsl:text>;</xsl:text>
		<!-- padding -->
		<xsl:text>padding-left: 1ex;</xsl:text>
		<xsl:text>padding-right: 1ex;</xsl:text>
		<!-- vertical alignment -->
		<xsl:choose>
			<xsl:when test="parent::*/@valign">
				<xsl:text>vertical-align:</xsl:text>
				<xsl:value-of select="normalize-space(parent::*/@valign)"/>
				<xsl:text>;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>vertical-align: top;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
		<!-- horizontal alignment -->
		<xsl:choose>
			<xsl:when test="@align">
				<xsl:text>text-align:</xsl:text>
				<xsl:value-of select="normalize-space(@align)"/>
				<xsl:text>;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>text-align: left;</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
		<!-- row separator -->
		<xsl:if test="
			(contains(@frame, 'rowsep') or
			contains(parent::*/@frame, 'rowsep') or
			contains(parent::*/parent::*/@frame, 'rowsep'))
			and	parent::*/following-sibling::tr">
			<xsl:choose>
				<xsl:when test="parent::th">
					<!-- double rule -->
					<xsl:text>border-bottom: </xsl:text>
					<xsl:call-template name="table.doublerulesep">
						<xsl:with-param name="rulewidth" select="$rulewidth"/>
					</xsl:call-template>
					<xsl:text> solid </xsl:text>
					<xsl:value-of select="$rulecolor"/>
					<xsl:text>;</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<!-- standard rule -->
					<xsl:text>border-bottom: </xsl:text>
					<xsl:value-of select="$rule"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		<!-- column separator -->
		<xsl:if test="
			(contains(@frame, 'colsep') or
			contains(parent::*/@frame, 'colsep') or
			contains(parent::*/parent::*/@frame, 'colsep'))
			and	not(position() = last())">
			<xsl:text>border-right: </xsl:text>
			<xsl:value-of select="$rule"/>
		</xsl:if>
		<!-- background color -->
		<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>
		<xsl:if test="$rgb != ''">
			<xsl:text>background-color:</xsl:text>
			<xsl:value-of select="$rgb"/>
			<xsl:text>;</xsl:text>
		</xsl:if>
	</xsl:variable>

	<td style="{$style}">
		<xsl:if test="$colspan &gt; 1">
			<xsl:attribute name="colspan">
				<xsl:value-of select="$colspan"/>
			</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
	</td>
</xsl:template>

<!--
  - Get combined counter for table
-->
<xsl:template name="table.prefix">
	<xsl:choose>
		<xsl:when test="ancestor::chapter">
			<xsl:number count="chapter"/>
			<xsl:text>.</xsl:text>
			<xsl:variable name="id" select="generate-id(ancestor::chapter)"/>
			<xsl:number value="count(preceding::table[
				generate-id(ancestor::chapter) = $id
				and descendant::caption]) + 1"/>
		</xsl:when>
		<xsl:when test="ancestor::appendix">
			<xsl:number count="appendix" format="A"/>
			<xsl:text>.</xsl:text>
			<xsl:variable name="id" select="generate-id(ancestor::appendix)"/>
			<xsl:number value="count(preceding::table[
				generate-id(ancestor::appendix) = $id
				and descendant::caption]) + 1"/>
		</xsl:when>
		<xsl:when test="ancestor::section">
			<xsl:number count="section"/>
			<xsl:text>.</xsl:text>
			<xsl:variable name="id" select="generate-id(ancestor::section)"/>
			<xsl:number value="count(preceding::table[
				generate-id(ancestor::section) = $id
				and descendant::caption]) + 1"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>0.</xsl:text>
			<xsl:number value="count(
				preceding::table/ancestor::*[name() = 'preface']) + 1"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
