<?xml version="1.0"?>
<!-- ============================================================ -->
<!--                                                              -->
<!-- 	This file makes a part of RenderX XSL Test Suite          -->
<!--                                                              -->
<!--    Author: Alexander Peshkov                                 -->
<!--                                                              -->
<!--    (c) RenderX, 2003                                         -->
<!--                                                              -->
<!-- ============================================================ -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:fo="http://www.w3.org/1999/XSL/Format"
                              xmlns:svg="http://www.w3.org/2000/svg">
	<xsl:import href="generic.xsl"/>
	
	<xsl:output indent="yes"/>
	
	<xsl:key name="depth" match="//S//*[not(*)]" use="count(ancestor::*[ancestor-or-self::S])"/>
	<!-- Number of leaf nodes -->
	<xsl:variable name="words" select="count(//*[ancestor-or-self::S][not(*)])"/>
	<!-- Maximum tree depth -->
	<xsl:variable name="max-depth">
		<xsl:for-each select="//S//*[generate-id() = generate-id(key('depth', count(ancestor::*[ancestor-or-self::S])))]">
          <xsl:if test="not (//*[count(ancestor::*[ancestor-or-self::S]) &gt; count(current()/ancestor::*[ancestor-or-self::S])])">
            <xsl:value-of select="count(ancestor-or-self::*[ancestor-or-self::S])"/>
          </xsl:if>
		</xsl:for-each>
	</xsl:variable>

	<xsl:template match="S">
		<fo:block font="bold 12pt Helvetica"
		          space-before="16pt"
		          space-after="16pt">
			Sentence<xsl:number/>:</fo:block>
		<fo:block>
			<xsl:value-of select="."/>
		</fo:block>
		<fo:block font="bold 12pt Helvetica"
		          space-before="16pt"
		          space-after="16pt">Bracket structure:</fo:block>
		<fo:block text-align="start">
			<xsl:apply-templates select="." mode="brackets"/>
		</fo:block>
		<fo:block font="bold 12pt Helvetica"
		          space-before="16pt"
		          space-after="16pt">Tree structure:</fo:block>
		<fo:block>
			<fo:instream-foreign-object width="100%" content-width="scale-to-fit" content-height="100%">
				<xsl:apply-templates select="." mode="svg"/>
			</fo:instream-foreign-object>
		</fo:block>
	</xsl:template>
	<xsl:template match="S" mode="svg">
		<xsl:variable name="x" select="$words * 15"/>
		<svg:svg width="{30 * $words}pt" height="{20 * $max-depth + 50}pt" viewBox="0 0 {30 * $words} {20 * $max-depth + 40}">
			<xsl:call-template name="draw-label">
				<xsl:with-param name="x" select="$x"/>
				<xsl:with-param name="depth" select="1"/>
			</xsl:call-template>
			<xsl:apply-templates select="*" mode="svg">
				<xsl:with-param name="parent-x" select="$x"/>
			</xsl:apply-templates>
		</svg:svg>
	</xsl:template>
	<xsl:template match="*" mode="svg">
		<xsl:param name="parent-x"/>
		<xsl:variable name="depth" select="count(ancestor-or-self::*[ancestor-or-self::S])"/>
		<xsl:variable name="words-before" select="count(preceding::*[not(*)][ancestor-or-self::S])"/>
		<xsl:variable name="words-after" select="count(following::*[not(*)][ancestor-or-self::S])"/>
		<xsl:variable name="x" select="($words + $words-before - $words-after) * 15"/>
		<xsl:call-template name="draw-label">
			<xsl:with-param name="x" select="$x"/>
			<xsl:with-param name="depth" select="$depth"/>
		</xsl:call-template>
		<svg:line style="stroke-width:2; stroke: darkred; stroke-linecap: round" x1="{$parent-x}" y1="{$depth * 20 - 17}" x2="{$x}" y2="{$depth * 20 - 10}"/>
		<xsl:if test="not(*)">
			<svg:line style="fill:none; stroke-width:1; stroke: blue; stroke-dasharray: 1 2; stroke-linecap: round" x1="{$x}" y1="{$depth * 20 + 3}" x2="{$x}" y2="{$max-depth * 20 + 10}"/>
			<svg:text style="font-family:Times; font-style:italic; font-size:10" text-anchor="middle" x="{$x}" y="{ $max-depth * 20 + 20 }">
				<xsl:value-of select="."/>
			</svg:text>
		</xsl:if>
		<xsl:apply-templates select="*" mode="svg">
			<xsl:with-param name="parent-x" select="$x"/>
		</xsl:apply-templates>
	</xsl:template>
	<xsl:template name="draw-label">
		<xsl:param name="x"/>
		<xsl:param name="depth"/>
		<svg:text style="font-family:Helvetica; font-size:10" text-anchor="middle" x="{$x}" y="{ $depth * 20 }">
			<xsl:value-of select="name()"/>
		</svg:text>
	</xsl:template>
	<xsl:template match="*[*]" mode="brackets">
		<xsl:text>[</xsl:text>
		<xsl:apply-templates select="*" mode="brackets"/>
		<xsl:text>]</xsl:text>
		<xsl:call-template name="draw-superscript"/>
	</xsl:template>
	<xsl:template match="*[not(*)]" mode="brackets">
		<fo:inline font-style="italic">
			<xsl:value-of select="."/>
		</fo:inline>
		<xsl:call-template name="draw-superscript"/>
	</xsl:template>
	<xsl:template name="draw-superscript">
		<xsl:text/>
		<fo:inline font-size="75%" baseline-shift="super">
			<xsl:value-of select="name()"/>
		</fo:inline>
		<xsl:if test="following-sibling::*">
			<xsl:text/>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>
