<!--=================================================================
	Stylesheet to convert a Go game in XML format into SVG format
	    Written by daniel.gilder@ntlworld.com

  Version 1.04, 24th Feb 2009 

	v.0.07  Slight changes for Jago 4.24 compatability
	v.0.08  Comments displayed, off board stones slightly smaller
	v.0.081 fixed bug in matchStone if variations present
	v.0.082 fixed bug in bugfix matchStone and bug in findDups
	        changed initial variables into params and added a few
	v.0.083 fixed bug in bugfix findDups. Options for showMarks
	v.0.084 SVG max height determined by counting comments
	v.1.0   Marks displayed in black and white. Added plain
	        format, board clipping and numbering options.
	        SVG max width estimated from longest comment. Solved
	        problem of displaying moves on un-numbered stones.
	        'Label' attribute supported. Javscript extensions for
		      diagram splitting parameters, thanks to Dmitry Mayorov
		      Labels on empty points hide grid lines underneath.
		      Moves with @at='' flagged as passes. All Info shown
  v.1.02  Added px to font-size to fix browser display issue.
  v.1.03  Don't insert a new viewbox unless needed.
  		    Added xmlns address.
  v.1.04  Added font-family Arial and some font parameters
          Hopefully fixed bug in matchstone    

	1. Javascript extensions only work with Xalan
   	   (? - maybe they don't work at all anymore)
	2. Variations not displayed. How to navigate?
	3. Delete not suported.
	4. Shadows not very good. Could be improved by checking if
   	   right hand and lower points occupied. Then drawing shadow
   	   appropriately.
	5. Maximum boardsize is 29
==================================================================-->

<xsl:stylesheet xmlns="http://www.w3.org/2000/svg"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:lxslt="http://xml.apache.org/xslt"
    xmlns:goTool="anywho"
    extension-element-prefixes="goTool"
    version="1.0">
<xsl:output method="xml" indent="yes" media-type="image/svg"/>

<!--====================Start of Parameters=======================-->

<!-- displays Clipx and ClipY parameter values if set to 'on'     -->

<xsl:param name="showParams">off</xsl:param>
<xsl:param name="showParamsX">200</xsl:param>
<xsl:param name="showParamsY">25</xsl:param>

<!--Various numbering and board titling parameters:
		stopAt 'n' displays moves of number less than or equal to 'n'
		numberFrom 'n' numbers moves greater than or equal to 'n'
		numberOffset 'n' changes move labelling to (movenumber - n)   -->

<xsl:param name="boardName"/>
<xsl:param name="showRange">on</xsl:param>
<xsl:param name="showRangeY">15</xsl:param>
<xsl:param name="stopAt">999</xsl:param>
<xsl:param name="numberFrom">0</xsl:param>
<xsl:param name="numberOffset">0</xsl:param>

<!--Setting format to plain produces plain black and white stones
		with no board background                                      -->

<xsl:param name="format">pretty</xsl:param>

<!-- These 2 figures control where the board is drawn             -->

<xsl:param name="WholeBOffsetX">5</xsl:param>
<xsl:param name="WholeBOffsetY">110</xsl:param>

<!--These parameters control how (and if) the board is clipped.
		Sensible values are integers, abs(value) less than BoardSize
		If both are 0 no clipping takes place                         -->

<xsl:param name="clipX">0</xsl:param>
<xsl:param name="clipY">0</xsl:param>

<!-- These parameters control where (and if) game info is drawn   -->

<xsl:param name="showName">on</xsl:param>
<xsl:param name="nameY">15</xsl:param>
<xsl:param name="showInfo">on</xsl:param>
<xsl:param name="hideInfo">Application,BoardSize,User</xsl:param>
<xsl:param name="infoOffsetX">5</xsl:param>
<xsl:param name="infoOffsetY">35</xsl:param>

<!--These 2 figures control where the ko (ie duplicate) stones
		are drawn relative to the lower LHS of the board background   -->

<xsl:param name="KoOffsetX">10</xsl:param>
<xsl:param name="KoOffsetY">35</xsl:param>

<!--These params control where (and if) the comments are drawn
		(relative to the upper RHS of the board background)           -->

<xsl:param name="showComments">on</xsl:param>
<xsl:param name="CommentOffsetX">25</xsl:param>
<xsl:param name="CommentOffsetY">0</xsl:param>

<!--This figure controls the size of stones drawn off the board
		relative to those drawn on it                                 -->

<xsl:param name="OffStoneScale">0.9</xsl:param>

<!--This figure controls the distance between the outermost lines
		of the board and the edge of the wood image. It is a 
		proportion of StoneWidth. ie any number less than 0.5 and the
		stones will overhang the wooden edge. 0.62 is c.traditional
		GridSize less than 280 results in misshapen stones (why?)     -->

<xsl:param name="StoneUnderHang">0.62</xsl:param>
<xsl:param name="GridSize">360</xsl:param>

<!--What board marks do you want displayed?
		options are All, Last, None                                   -->

<xsl:param name="showMarks">None</xsl:param>

<!-- Words that mean 'pass' and 'at', maximum length 6 and 3      -->

<xsl:param name="passText">pass</xsl:param>
<xsl:param name="atText">at</xsl:param>

<!-- Do you want to output reams of debugging info or not?        -->

<xsl:param name="debug" select="false()"/>

<xsl:param name="svgWidth">450</xsl:param>
<xsl:param name="svgHeight">750</xsl:param>
<xsl:param name="fontFamily">Arial</xsl:param>
<xsl:param name="stoneFontSize">60</xsl:param>
<xsl:param name="fontUnit">px</xsl:param>

<!--====================End of Parameters=========================-->

<xsl:variable name="yGap" select="1.1"/>
<xsl:variable name="textGap" select="0.8"/>
<xsl:variable name="ShapeOffset" select="150"/>
<xsl:variable name="StoneRadius" select="150"/>
<xsl:variable name="BoardSizePath" select="/Go/GoGame/Information/BoardSize/."/>
<xsl:variable name="MyStoneWidth" select="$GridSize div (($BoardSizePath)-1)"/>
<xsl:variable name="MyWoodSize" select="$GridSize+($StoneUnderHang*2*$MyStoneWidth)"/>
<xsl:variable name="ScaleFactor" select="$OffStoneScale*($MyStoneWidth div ($StoneRadius*2))"/>

<xsl:variable name="lastMove">
  <xsl:for-each select="/Go/GoGame/Nodes/Node/Black|/Go/GoGame/Nodes/Black
	|/Go/GoGame/Nodes/Node/White|/Go/GoGame/Nodes/White">
    <xsl:sort select="@number" data-type="number" order="descending"/>
    <xsl:if test="position()=1">
      <xsl:value-of select="@number"/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

<xsl:variable name="myStopAt">
	<xsl:choose>
	<xsl:when test="$stopAt=999">
		<xsl:if test="$lastMove!=''"><xsl:value-of select="$lastMove"/></xsl:if>
		<xsl:if test="$lastMove=''"><xsl:value-of select="0"/></xsl:if>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$stopAt"/>
	</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<xsl:variable name="useGoTool">
	<xsl:choose>
	<xsl:when test="($numberFrom &gt;0) and function-available('goTool:init')">
		<xsl:value-of select="true()"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="false()"/>
	</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<!--=================================================================
		This key is used to find duplicate moves
==================================================================-->

<xsl:key name="ids" match="/Go/GoGame/Nodes/Node/Black|/Go/GoGame/Nodes/Black|
	/Go/GoGame/Nodes/Node/White|/Go/GoGame/Nodes/White|/Go/GoGame/Nodes/Node/AddBlack
	|/Go/GoGame/Nodes/Node/AddWhite" use="@at"/>

<!--==============================================================-->

<xsl:template match="/">
	<xsl:variable name="error">
		<xsl:choose>
		<xsl:when test="($numberFrom &gt; 0) and not(function-available('goTool:init'))">
			<xsl:value-of select="1"/>
		</xsl:when>
		<xsl:when test="$useGoTool='true'">
			<xsl:value-of select="goTool:init(string($BoardSizePath))"/>
			<xsl:call-template name="loadMoves"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="0"/>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:choose>
		<xsl:when test="$error=0">
			<xsl:apply-templates select="Go"/>
		</xsl:when>
		<xsl:when test="$error=1">
			<xsl:message terminate="yes">Javascript not available</xsl:message>
		</xsl:when>
		<xsl:when test="$error=2">
			<xsl:message terminate="yes">BoardSize error</xsl:message>
		</xsl:when>
		<xsl:otherwise>
			<xsl:message terminate="yes">Unknown error</xsl:message>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--=================================================================
	code determines whether the SVG heigth needs to be bigger due
	to the number of comment lines to display and/or wider due to
	the longest comment
==================================================================-->

<xsl:template match="/Go">

	<xsl:variable name="longComment">
	  <xsl:for-each select="/Go/GoGame/Nodes/Node/Comment/P|/Go/GoGame/Nodes/Comment/P">
			<xsl:sort select="string-length(.)" data-type="number" order="descending" />
 	  	<xsl:if test="position() = 1">
  	  	<xsl:value-of select="string-length(.)" />
  	  </xsl:if>
	  </xsl:for-each>
	</xsl:variable>

	<xsl:variable name="MaxOffsetX" select="$CommentOffsetX+$WholeBOffsetX+$MyWoodSize+
		($OffStoneScale*$yGap*$MyStoneWidth)+($longComment*25*4*$ScaleFactor)"/>
	
	<xsl:variable name="myWidth">
	<xsl:choose>
	<xsl:when test="$MaxOffsetX &gt; $svgWidth">
		<xsl:value-of select="$MaxOffsetX"/>	
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$svgWidth"/>
	</xsl:otherwise>
	</xsl:choose>
  </xsl:variable>

  <xsl:variable name="countComments" select="
		count(/Go/GoGame/Nodes/Node/Comment/P|/Go/GoGame/Nodes/Comment/P)"/>
	
	<xsl:variable name="MaxOffsetY" select="$CommentOffsetY+$WholeBOffsetY+
		($OffStoneScale*$yGap*$MyStoneWidth*($countComments - 1))"/>
  
	<xsl:variable name="myHeight">
	<xsl:choose>
	<xsl:when test="$MaxOffsetY &gt; $svgHeight">
		<xsl:value-of select="$MaxOffsetY"/>	
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$svgHeight"/>
	</xsl:otherwise>
	</xsl:choose>
  </xsl:variable>

	<svg xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="{$myWidth}" height="{$myHeight}">
		<xsl:call-template name="setupSVG"/>
		<xsl:apply-templates select="GoGame"/>
	</svg>

</xsl:template>

<!--=================================================================
cases to determine whether and where to clip board
==================================================================-->

<xsl:template match="GoGame">
	<title>Go Game <xsl:value-of select="@name"/></title>

	<xsl:variable name="nudge" select="$StoneUnderHang - ($ShapeOffset div ($StoneRadius*2))"/>

	<xsl:variable name="thisClipX"> 
	<xsl:choose>
	<xsl:when test="$clipX&gt;0">
		<xsl:value-of select="$MyStoneWidth*($clipX+$nudge)"/>
	</xsl:when>
	<xsl:when test="$clipX&lt;0">
		<xsl:value-of select="$MyStoneWidth*($clipX - $nudge)"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="0"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<xsl:variable name="thisClipY"> 
	<xsl:choose>
	<xsl:when test="$clipY&gt;0">
		<xsl:value-of select="$MyStoneWidth*($clipY+$nudge)"/>
	</xsl:when>
	<xsl:when test="$clipY&lt;0">
		<xsl:value-of select="$MyStoneWidth*($clipY - $nudge)"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="0"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<xsl:variable name="shiftX"> 
	<xsl:choose>
	<xsl:when test="$clipX&gt;0">
		<xsl:value-of select="$MyStoneWidth*($clipX+$nudge)"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="0"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<xsl:variable name="shiftY"> 
	<xsl:choose>
	<xsl:when test="$clipY&gt;0">
		<xsl:value-of select="$MyStoneWidth*($clipY+$nudge)"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="0"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<g transform="matrix(1 0 0 1 {$WholeBOffsetX - $shiftX} {$WholeBOffsetY - $shiftY})">
	<xsl:if test="($thisClipX!='0') and ($thisClipY!='0')">
		<svg preserveAspectRatio="xMinYMin slice" width="{$MyWoodSize}" 
		height="{$MyWoodSize}" viewBox="0 0 {$MyWoodSize} {$MyWoodSize}">
			<g transform="matrix(1 0 0 1 {$thisClipX} {$thisClipY})">
			<xsl:apply-templates select="Information/BoardSize"/>
			<xsl:apply-templates select="Nodes"/>
			</g>
		</svg>
	</xsl:if>
	<xsl:if test="($thisClipX='0') and ($thisClipY='0')">
		<xsl:apply-templates select="Information/BoardSize"/>
		<xsl:apply-templates select="Nodes"/>
	</xsl:if>
	</g>

	<xsl:variable name="shiftRangeX"> 
	<xsl:choose>
	<xsl:when test="$clipX&gt;0">
		<xsl:value-of select="$MyWoodSize - $shiftX"/>
	</xsl:when>
	<xsl:when test="$clipX&lt;0">
		<xsl:value-of select="$MyWoodSize+$thisClipX"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$MyWoodSize"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<xsl:variable name="shiftRangeY"> 
	<xsl:choose>
	<xsl:when test="$clipY&gt;0">
		<xsl:value-of select="- $shiftY"/>
	</xsl:when>
	<xsl:when test="$clipY&lt;0">
		<xsl:value-of select="$thisClipY"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="0"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<xsl:if test="@name and $showName='on'">
		<xsl:variable name="nameX" select="$shiftRangeX div 2"/>
		<xsl:variable name="fontScale" select="$stoneFontSize div 4"/>
		<text x="0" y="0" text-anchor="middle" transform="translate({$nameX},{$nameY})" 
		style="fill:rgb(0,0,0);font-family:{$fontFamily};font-size:{$fontScale}{$fontUnit}">
			<xsl:value-of select="@name"/>
		</text>
	</xsl:if>
	<g transform="matrix(1 0 0 1 {$shiftRangeX div 2} {$shiftRangeY})">
		<xsl:if test="($showRange='on') or ($boardName!='')">
			<xsl:call-template name="findRange"/>
		</xsl:if>
	</g>
	<xsl:variable name="trackX" select="$WholeBOffsetX - $shiftX+$thisClipX"/>
	<xsl:variable name="trackY" select="$WholeBOffsetY - $shiftY+$thisClipY - $shiftRangeY"/>
	<g transform="matrix(1 0 0 1 0 {$shiftRangeY})">
		<xsl:call-template name="findDups">
			<xsl:with-param name="xLimit" select="$shiftRangeX"/>
			<xsl:with-param name="trackX" select="$trackX"/>
			<xsl:with-param name="trackY" select="$trackY"/>
		</xsl:call-template>
	</g>
	<g transform="matrix(1 0 0 1 {$shiftRangeX} 0)">
		<xsl:if test="$showComments='on'">
			<xsl:call-template name="findComments"/>
		</xsl:if>
	</g>
	<xsl:if test="$showParams='on'">
		<xsl:call-template name="findParams"/>
	</xsl:if>
	<xsl:if test="$showInfo='on'">
		<xsl:apply-templates select="Information"/>
	</xsl:if>
</xsl:template>

<!--==============================================================-->

<xsl:template match="Nodes">
	<xsl:apply-templates select="Black"/>
	<xsl:apply-templates select="White"/>
	<xsl:apply-templates select="Node"/>
</xsl:template>

<!--==============================================================-->

<xsl:template match="Information">
	<xsl:for-each select="child::*[not(contains($hideInfo,name(.)))]">
		<xsl:variable name="infoX" select="$infoOffsetX+$WholeBOffsetX+250*((position() - 1) mod 2)"/>
		<xsl:variable name="infoY" select="$infoOffsetY+20*(floor((position() - 1) div 2))"/>
		<xsl:variable name="fontScale" select="$stoneFontSize div 5"/>
		<text x="0" y="0" transform="translate({$infoX},{$infoY})" style="fill:rgb(0,0,0);font-family:{$fontFamily}; font-size:{$fontScale}{$fontUnit}">
			<xsl:value-of select="name(.)"/>:
		</text>
		<text x="0" y="0" transform="translate({75+$infoX},{$infoY})" style="fill:rgb(0,0,0);font-family:{$fontFamily};font-size:{$fontScale}{$fontUnit}">
			<xsl:value-of select="."/>
		</text>
	</xsl:for-each>
</xsl:template>

<!--==============================================================-->

<xsl:template match="Node">
	<xsl:apply-templates select="Black"/>
	<xsl:apply-templates select="White"/>
	<xsl:apply-templates select="AddBlack"/>
	<xsl:apply-templates select="AddWhite"/>
	<xsl:apply-templates select="Mark"/>
</xsl:template>

<!--==============================================================-->

<xsl:template match="AddWhite|AddBlack">
	<xsl:variable name="precedingMoveNum">
		<xsl:call-template name="getPrecedingMoveNum"/>
	</xsl:variable>

	<xsl:if test="$precedingMoveNum &lt;=$myStopAt">
		<xsl:if test="$useGoTool='true'">
			<xsl:variable name="error" select="goTool:loadMove(string(@at), string(name(.)))"/>
			<xsl:if test="$error='true'">
				<xsl:message> Addstone error (<xsl:value-of select="$error"/>)</xsl:message>
			</xsl:if>
		</xsl:if>
		<xsl:call-template name="findxy">
			<xsl:with-param name="type" select="substring-after(name(.),'Add')"/>
			<xsl:with-param name="axis" select="@at"/>
			<xsl:with-param name="label"/>
 		</xsl:call-template>
	</xsl:if>
</xsl:template>

<!--==============================================================-->

<xsl:template match="White|Black">
	<xsl:if test="(@number &lt;= $myStopAt) and @at!=''">
		<xsl:call-template name="matchStone">
			<xsl:with-param name="type" select="name(.)"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<!--=================================================================
	If a move is passed here, a check is done to see whether it is
	the first occurrence of a move on that point. If so, it goes
	to findxy
==================================================================-->

<xsl:template name="matchStone">
	<xsl:param name="type">Black</xsl:param>
	<xsl:if test="$debug">
		<xsl:message>matchStone (<xsl:value-of select="$type"/>, <xsl:value-of select="@number"/>, <xsl:value-of select="@at"/>)</xsl:message>
	</xsl:if>

	<xsl:variable name="xy" select="@at"/>

	<xsl:variable name="match">
		<xsl:choose>
			<xsl:when test="@number &lt; $numberFrom">
				<xsl:call-template name="getColourA">
					<xsl:with-param name="xy" select="$xy"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="getColourB">
					<xsl:with-param name="xy" select="$xy"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="isCaptured">
		<xsl:choose>
			<xsl:when test="$useGoTool='true' and @number &lt; $numberFrom and goTool:peekCell(string(@at))!=$type">
				<xsl:value-of select="true()"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="false()"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="isDuplicate">
		<xsl:choose>
			<xsl:when test="$useGoTool='true' and @number &gt;= $numberFrom and goTool:peekCell(string(@at))!='empty'">
				<xsl:value-of select="true()"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="false()"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="showStone">
		<xsl:choose>
			<xsl:when test="@number &lt; $numberFrom">
				<xsl:if test="$match='' and $isCaptured='false'">
					<xsl:value-of select="true()"/>
				</xsl:if>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="$match='' and $isDuplicate='false'">
					<xsl:value-of select="true()"/>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="$showStone='true'">
		<xsl:call-template name="findxy">
			<xsl:with-param name="type" select="$type"/>
		  <xsl:with-param name="axis" select="@at"/>
			<xsl:with-param name="label" select="@number"/>
	  </xsl:call-template>
	</xsl:if>
</xsl:template>

<!--=================================================================
	findxy converts co-ordinate strings like 'B14' into x=1,y=14
	before passing them onto drawxy and labelStone 
==================================================================-->

<xsl:template name="findxy">
	<xsl:param name="type">Black</xsl:param>
	<xsl:param name="axis">A1</xsl:param>
	<xsl:param name="label">1</xsl:param>
	<xsl:if test="$debug">
		<xsl:message>findxy (<xsl:value-of select="$type"/>)</xsl:message>
		<xsl:message>findxy (<xsl:value-of select="$axis"/>)</xsl:message>
		<xsl:message>findxy (<xsl:value-of select="$label"/>)</xsl:message>
	</xsl:if>

  <xsl:variable name="MYxaxis" select="substring($axis, 1, 1)"/>
  <xsl:variable name="MYyaxis" select="substring($axis, 2, string-length($axis)-1)"/>
	<xsl:variable name="tens">
	<xsl:choose>
	<xsl:when test="contains('VWXYZ[\]^',$MYxaxis)">
		<xsl:value-of select="20"/>
	</xsl:when>
	<xsl:when test="contains('LMNOPQRSTU',$MYxaxis)">
		<xsl:value-of select="10"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="0"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>
  <xsl:variable name="x" select="($tens)+translate($MYxaxis,
		'ABCDEFGHJKLMNOPQRSTUVWXYZ[\]^','01234567890123456789012345678')"/>
	<xsl:variable name="finalx" select="$MyStoneWidth*($StoneUnderHang+$x)"/>
	<xsl:variable name="finaly" select="$MyStoneWidth*($StoneUnderHang+$BoardSizePath - $MYyaxis)"/>

	<xsl:variable name="isNum">
		<xsl:choose>
		<xsl:when test="string(number($label))='NaN'">
			<xsl:value-of select="0"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="1"/>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="($type!='#white') and ($type!='#black')">
	<xsl:call-template name="drawxy">
			<xsl:with-param name="type" select="$type"/>
		  <xsl:with-param name="finalx" select="$finalx"/>
		  <xsl:with-param name="finaly" select="$finaly"/>
		  <xsl:with-param name="scale" select="1"/>
	</xsl:call-template>
	</xsl:if>
	<xsl:variable name="myLabel">
		<xsl:choose>
		<xsl:when test="($isNum='1') and ($label&gt;=$numberFrom)">
			<xsl:value-of select="$label - $numberOffset"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$label"/>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="$label!='' and ((($isNum='1') and ($label&gt;=$numberFrom)) or ($isNum='0'))">
		<xsl:call-template name="labelStone">
			<xsl:with-param name="type" select="$type"/>
		  <xsl:with-param name="finalx" select="$finalx"/>
		  <xsl:with-param name="finaly" select="$finaly"/>
		  <xsl:with-param name="label" select="$myLabel"/>
		  <xsl:with-param name="scale" select="1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<!--=================================================================
	drawxy draws stones, numbered in an appropriate colour and size
==================================================================-->

<xsl:template name="drawxy">
	<xsl:param name="type">Black</xsl:param>
	<xsl:param name="finalx">0</xsl:param>
	<xsl:param name="finaly">0</xsl:param>
	<xsl:param name="scale">1</xsl:param>
	<xsl:if test="$debug">
		<xsl:message>drawxy (<xsl:value-of select="$type"/>)</xsl:message>
		<xsl:message>drawxy (<xsl:value-of select="$finalx"/>)</xsl:message>
		<xsl:message>drawxy (<xsl:value-of select="$finaly"/>)</xsl:message>
		<xsl:message>drawxy (<xsl:value-of select="$scale"/>)</xsl:message>
	</xsl:if>

  <xsl:variable name="stoneType">
	<xsl:choose>
		<xsl:when test="$type='Black'">#blackStone</xsl:when>
		<xsl:when test="$type='White'">#whiteStone</xsl:when>
		<xsl:otherwise><xsl:value-of select="$type"/></xsl:otherwise>
	</xsl:choose>
  </xsl:variable>

	<xsl:variable name="ScaleFactor" select="format-number($scale*($MyStoneWidth div ($StoneRadius*2)),'#.#####')"/>
	<xsl:variable name="myX" select="format-number(($finalx)-($ScaleFactor*$ShapeOffset),'#.#####')"/>
	<xsl:variable name="myY" select="format-number(($finaly)-($ScaleFactor*$ShapeOffset),'#.#####')"/>
	
	<g transform="matrix({$ScaleFactor} 0 0 {$ScaleFactor} {$myX} {$myY})">
		<use xlink:href="{$stoneType}"/>
	</g>

</xsl:template>

<!--=================================================================
	Only the first 3 characters of a label are displayed 
==================================================================-->

<xsl:template name="labelStone">
	<xsl:param name="type">Black</xsl:param>
	<xsl:param name="finalx">0</xsl:param>
	<xsl:param name="finaly">0</xsl:param>
	<xsl:param name="label">0</xsl:param>
	<xsl:param name="scale">1</xsl:param>
	<xsl:if test="$debug">
		<xsl:message>labelStone (<xsl:value-of select="$type"/>)</xsl:message>
		<xsl:message>labelStone (<xsl:value-of select="$finalx"/>)</xsl:message>
		<xsl:message>labelStone (<xsl:value-of select="$finaly"/>)</xsl:message>
		<xsl:message>labelStone (<xsl:value-of select="$label"/>)</xsl:message>
		<xsl:message>labelStone (<xsl:value-of select="$scale"/>)</xsl:message>
	</xsl:if>

  <xsl:variable name="textColour">
	<xsl:choose>
		<xsl:when test="($type='Black') or ($type='#black')">white</xsl:when>
		<xsl:when test="($type='White') or ($type='#white')">black</xsl:when>
		<xsl:otherwise>none</xsl:otherwise>
	</xsl:choose>
  </xsl:variable>

	<xsl:variable name="ScaleFactor" select="format-number($scale*($MyStoneWidth div ($StoneRadius*2)),'#.#####')"/>
	<xsl:variable name="myX" select="format-number(($finalx)-($ScaleFactor*$ShapeOffset),'#.#####')"/>
	<xsl:variable name="myY" select="format-number(($finaly)-($ScaleFactor*$ShapeOffset),'#.#####')"/>
	
	<g transform="matrix({$ScaleFactor} 0 0 {$ScaleFactor} {$myX} {$myY})">

  <xsl:variable name="MYlength" select="string-length($label)"/>
	<xsl:choose>
		<xsl:when test="$MYlength='3'">
			<g transform='matrix(2 0 0 4 {-300+$ShapeOffset} {-320+$ShapeOffset})'>
			<text x='100' y='100' style='fill:{$textColour};font-family:{$fontFamily};font-size:{$stoneFontSize}{$fontUnit}'>
			<xsl:value-of select="$label"/></text></g>
		</xsl:when>
		<xsl:when test="$MYlength='2'">
			<g transform='matrix(3 0 0 4 {-405+$ShapeOffset} {-320+$ShapeOffset})'>
			<text x='100' y='100' style='fill:{$textColour};font-family:{$fontFamily};font-size:{$stoneFontSize}{$fontUnit}'>
			<xsl:value-of select="$label"/></text></g>
		</xsl:when>
		<xsl:when test="$MYlength='1'">
		<g transform='matrix(5 0 0 4 {-590+$ShapeOffset} {-320+$ShapeOffset})'>
			<text x='100' y='100' style='fill:{$textColour};font-family:{$fontFamily};font-size:{$stoneFontSize}{$fontUnit}'>
			<xsl:value-of select="$label"/></text></g>
		</xsl:when>
		<xsl:when test="$MYlength &gt; '3'">
			<g transform='matrix(2 0 0 4 {-300+$ShapeOffset} {-320+$ShapeOffset})'>
			<text x='100' y='100' style='fill:{$textColour};font-family:{$fontFamily};font-size:{$stoneFontSize}{$fontUnit}'>
			<xsl:value-of select="substring($label, 1, 3)"/></text></g>
		</xsl:when>
		<xsl:otherwise></xsl:otherwise>
	</xsl:choose>

	</g>
</xsl:template>

<!--==============================================================-->

<xsl:template match="Mark">
	<xsl:variable name="precedingMoveNum">
		<xsl:call-template name="getPrecedingMoveNum"/>
	</xsl:variable>
	<xsl:choose>
	<xsl:when test="$showMarks='All'">
		<xsl:if test="$precedingMoveNum&lt;=$myStopAt">
			<xsl:call-template name="doMark">
				<xsl:with-param name="moveNum" select="$precedingMoveNum"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:when>
	<xsl:when test="$showMarks='Last'">
		<xsl:if test="$precedingMoveNum=$myStopAt">
			<xsl:call-template name="doMark">
				<xsl:with-param name="moveNum" select="$precedingMoveNum"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:when>
	<xsl:otherwise></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--=================================================================
	doMark draws marks, if there's a black stone on the point it
	draws the white version of the mark
==================================================================-->

<xsl:template name="doMark">
	<xsl:param name="moveNum">1</xsl:param>

	<xsl:variable name="xy" select="@at"/>

	<xsl:variable name="match">
		<xsl:choose>
			<xsl:when test="$useGoTool='false'">
				<xsl:call-template name="getColourA">
					<xsl:with-param name="xy" select="$xy"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="getColourB">
					<xsl:with-param name="xy" select="$xy"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="pointStatus">
		<xsl:if test="$useGoTool='false'">
			<xsl:choose>
				<xsl:when test="$match=''">empty</xsl:when>
				<xsl:otherwise><xsl:value-of select="substring-before($match,'-')"/></xsl:otherwise>
			</xsl:choose>
		</xsl:if>
		<xsl:if test="$useGoTool='true'">
			<xsl:choose>
				<xsl:when test="$match=''"><xsl:value-of select="goTool:peekCell(string(@at))"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="substring-before($match,'-')"/></xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:variable>

	<xsl:variable name="colour">
		<xsl:choose>
			<xsl:when test="$pointStatus='empty' or $pointStatus='White'">#black</xsl:when>
			<xsl:otherwise>#white</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="$debug">
		<xsl:message>Mark (<xsl:value-of select="@at"/>)</xsl:message>
		<xsl:message>moveNum is(<xsl:value-of select="$moveNum"/>)</xsl:message>
		<xsl:message>match is(<xsl:value-of select="$match"/>)</xsl:message>
		<xsl:message>pointStatus is(<xsl:value-of select="$pointStatus"/>)</xsl:message>
		<xsl:message>colour is(<xsl:value-of select="$colour"/>)</xsl:message>
	</xsl:if>

	<xsl:if test="$moveNum&gt;=$numberFrom">
		<xsl:choose>
		<xsl:when test="not(@type)">
			<xsl:choose>
			<xsl:when test="@label and ($pointStatus='black')">	
				<xsl:call-template name="findxy">
					<xsl:with-param name="type">#black</xsl:with-param>
				  <xsl:with-param name="axis" select="@at"/>
					<xsl:with-param name="label" select="@label"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="@label and ($pointStatus='white')">	
				<xsl:call-template name="findxy">
					<xsl:with-param name="type">#white</xsl:with-param>
				  <xsl:with-param name="axis" select="@at"/>
					<xsl:with-param name="label" select="@label"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="@label and ($pointStatus='empty')">	
				<xsl:if test="$format='plain'">
					<xsl:call-template name="findxy">
						<xsl:with-param name="type">#whitefillsquare</xsl:with-param>
					  <xsl:with-param name="axis" select="@at"/>
						<xsl:with-param name="label" select="@label"/>
					</xsl:call-template>
				</xsl:if>
				<xsl:call-template name="findxy">
					<xsl:with-param name="type">#white</xsl:with-param>
				  <xsl:with-param name="axis" select="@at"/>
					<xsl:with-param name="label" select="@label"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="findxy">
					<xsl:with-param name="type" select="concat($colour,'cross')"/>
				  <xsl:with-param name="axis" select="@at"/>
					<xsl:with-param name="label"/>
				</xsl:call-template>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="findxy">
				<xsl:with-param name="type" select="concat($colour,@type)"/>
			  <xsl:with-param name="axis" select="@at"/>
				<xsl:with-param name="label"/>
			</xsl:call-template>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:if>
</xsl:template>

<!--=================================================================
	findDups finds all moves on a point that already has (or had)
	a move there, and finds the colour and move number of the 
	first move there, then sends them off to be drawn. Also passes
==================================================================-->

<xsl:template name = "findDups" >
	<xsl:param name="xLimit">0</xsl:param>
	<xsl:param name="trackX">0</xsl:param>
	<xsl:param name="trackY">0</xsl:param>
	
	<xsl:if test="$useGoTool='true'"><goTool:counterInit value="0"/></xsl:if>
  <xsl:for-each select=
	"/Go/GoGame/Nodes/Node/Black[(@at='') or (generate-id(.)!=generate-id(key('ids',@at)) and @number&gt;=$numberFrom and @number&lt;=$myStopAt)]
	|/Go/GoGame/Nodes/Node/White[(@at='') or (generate-id(.)!=generate-id(key('ids',@at)) and @number&gt;=$numberFrom and @number&lt;=$myStopAt)]
	|/Go/GoGame/Nodes/Black[(@at='') or (generate-id(.)!=generate-id(key('ids',@at)) and @number&gt;=$numberFrom and @number&lt;=$myStopAt)]
  |/Go/GoGame/Nodes/White[(@at='') or (generate-id(.)!=generate-id(key('ids',@at)) and @number&gt;=$numberFrom and @number&lt;=$myStopAt)]
	|/Go/GoGame/Nodes/Node/AddBlack[(@at='') or (generate-id(.)!=generate-id(key('ids',@at)))]
	|/Go/GoGame/Nodes/Node/AddWhite[(@at='') or (generate-id(.)!=generate-id(key('ids',@at)))]">

		<xsl:variable name="colour" select = "name(.)" />
		<xsl:variable name="number" select = "@number" />
		<xsl:variable name="pos" select = "position()"/>
		<xsl:variable name="xy" select="@at"/>	

		<xsl:variable name="tryNumber">
			<xsl:for-each select="preceding::White|preceding::Black|preceding::AddWhite|preceding::AddBlack">
				<xsl:if test="$xy=@at">
					<xsl:if test="name(parent::*)!='Variation' and name(../parent::*)!='Variation'">
							<xsl:if test="@number &gt;= $numberFrom">
								<xsl:value-of select="concat(@number,'-')"/>
							</xsl:if>
					</xsl:if>
				</xsl:if>
			</xsl:for-each>
		</xsl:variable>

		<xsl:variable name="tryPos">
			<xsl:if test="$tryNumber=''">
				<xsl:for-each select="preceding::White|preceding::Black|preceding::AddWhite|preceding::AddBlack">
					<xsl:if test="$xy=@at">
						<xsl:if test="name(parent::*)!='Variation' and name(../parent::*)!='Variation'">
							<xsl:value-of select="concat($xy,'-')"/>
						</xsl:if>
					</xsl:if>
				</xsl:for-each>
			</xsl:if>
		</xsl:variable>

		<xsl:variable name="matchNumber">
			<xsl:choose>
			<xsl:when test="$tryPos!=''">
				<xsl:value-of select="$tryPos"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$tryNumber"/>
			</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>

	<xsl:variable name="matchColourB">
		<xsl:call-template name="getColourB">
			<xsl:with-param name="xy" select="$xy"/>
		</xsl:call-template>
	</xsl:variable>

	<xsl:if test="$debug">
		<xsl:message>testing number(<xsl:value-of select="@number"/>)</xsl:message>
		<xsl:message>found tryNumber(<xsl:value-of select="$tryNumber"/>)</xsl:message>
		<xsl:message>found tryPos(<xsl:value-of select="$tryPos"/>)</xsl:message>
		<xsl:message>using (<xsl:value-of select="$matchNumber"/>)</xsl:message>
		<xsl:message>found matchB(<xsl:value-of select="$matchColourB"/>)</xsl:message>
	</xsl:if>

		<xsl:if test="$useGoTool='false' or ($useGoTool='true' and not(goTool:peekCell(string(@at))='empty' and $matchColourB=''))">
			<xsl:variable name="myPos">
				<xsl:choose>
				<xsl:when test="$useGoTool='false'">
					<xsl:value-of select="$pos"/>
				</xsl:when>
				<xsl:otherwise>
					<goTool:counterIncr/>
					<xsl:value-of select="goTool:counterRead()"/>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

			<xsl:variable name="matchColour">
				<xsl:choose>
					<xsl:when test="$useGoTool='false'">
						<xsl:call-template name="getColourA">
							<xsl:with-param name="xy" select="$xy"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:choose>
							<xsl:when test="goTool:peekCell(string(@at))='empty'">
								<xsl:value-of select="$matchColourB"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="concat(goTool:peekCell(string(@at)),'-')"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

			<xsl:call-template name="drawKoStone">
				<xsl:with-param name="offset" select="$myPos"/>
				<xsl:with-param name="type1" select="$colour"/>
				<xsl:with-param name="num1" select="$number"/>
				<xsl:with-param name="type2" select="substring-before($matchColour,'-')"/>
				<xsl:with-param name="num2" select="substring-before($matchNumber,'-')"/>
				<xsl:with-param name="xLimit" select="$xLimit"/>
				<xsl:with-param name="spot" select="$xy"/>
				<xsl:with-param name="trackX" select="$trackX"/>
				<xsl:with-param name="trackY" select="$trackY"/>
			</xsl:call-template>
		</xsl:if>

	</xsl:for-each>
</xsl:template>

<!--=================================================================
	drawKostone draws sets up params so ko (ie duplicated) stones
	get drawn underneath the board in the right place (and passes)
==================================================================-->

<xsl:template name="drawKoStone">
	<xsl:param name="offset">0</xsl:param>
	<xsl:param name="type1">black</xsl:param>
	<xsl:param name="num1">0</xsl:param>
	<xsl:param name="type2">black</xsl:param>
	<xsl:param name="num2">0</xsl:param>
	<xsl:param name="xLimit">0</xsl:param>
	<xsl:param name="spot">0</xsl:param>
	<xsl:param name="trackX">0</xsl:param>
	<xsl:param name="trackY">0</xsl:param>

	<xsl:variable name="koWidth" select="floor(($xLimit - $KoOffsetX) div 
	(($OffStoneScale*3.7*$MyStoneWidth)- $ScaleFactor))"/>

	<xsl:variable name="MyKoOffsetX" select="$KoOffsetX+$WholeBOffsetX
			+($OffStoneScale*3.7*$MyStoneWidth*((($offset)-1) mod $koWidth))"/>
	<xsl:variable name="MyKoOffsetY" select="$KoOffsetY+$WholeBOffsetY
			+$MyWoodSize+($OffStoneScale*$yGap*$MyStoneWidth*floor((($offset)-1) div $koWidth))"/>

	<xsl:call-template name="drawxy">
			<xsl:with-param name="type" select="$type1"/>
			<xsl:with-param name="finalx" select="$MyKoOffsetX"/>
			<xsl:with-param name="finaly" select="$MyKoOffsetY"/>
		  <xsl:with-param name="scale" select="$OffStoneScale"/>
	</xsl:call-template>
	<xsl:call-template name="labelStone">
			<xsl:with-param name="type" select="$type1"/>
			<xsl:with-param name="finalx" select="$MyKoOffsetX"/>
			<xsl:with-param name="finaly" select="$MyKoOffsetY"/>
		  <xsl:with-param name="label" select="$num1 - $numberOffset"/>
		  <xsl:with-param name="scale" select="$OffStoneScale"/>
	</xsl:call-template>

	<xsl:variable name="myScale" select="format-number(3*$ScaleFactor,'#.#####')"/>
	<xsl:variable name="myX" select="format-number(($MyKoOffsetX)+(0.8*$OffStoneScale*$MyStoneWidth),'#.#####')"/>
	<xsl:variable name="myY" select="format-number($MyKoOffsetY,'#.#####')"/>

	<xsl:variable name="myText">
		<xsl:choose>
		<xsl:when test="$spot=''">
			<xsl:value-of select="$passText"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$atText"/>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<g transform='matrix({$myScale} 0 0 {$myScale} {$myX} {$myY})'>
		<text x='0' y='20' style='fill:black;font-family:{$fontFamily};font-size:{$stoneFontSize}{$fontUnit}'><xsl:value-of select="$myText"/></text>
	</g>

 <xsl:if test="$spot!=''">
	<xsl:call-template name="drawxy">
			<xsl:with-param name="type" select="$type2"/>
			<xsl:with-param name="finalx" select="$MyKoOffsetX+($OffStoneScale*2.2*$MyStoneWidth)"/>
			<xsl:with-param name="finaly" select="$MyKoOffsetY"/>
		  <xsl:with-param name="scale" select="$OffStoneScale"/>
	</xsl:call-template>

	<xsl:variable name="isNum">
		<xsl:choose>
		<xsl:when test="string(number($num2))='NaN'">
			<xsl:value-of select="0"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="1"/>
		</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:variable name="newLabel">
	<xsl:choose>
	<xsl:when test="($num2&lt;$numberFrom) or ($isNum='0')">
		<xsl:value-of select="$spot"/>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$num2 - $numberOffset"/>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:variable>

	<xsl:call-template name="labelStone">
			<xsl:with-param name="type" select="$type2"/>
			<xsl:with-param name="finalx" select="$MyKoOffsetX+($OffStoneScale*2.2*$MyStoneWidth)"/>
			<xsl:with-param name="finaly" select="$MyKoOffsetY"/>
		  <xsl:with-param name="label" select="$newLabel"/>
		  <xsl:with-param name="scale" select="$OffStoneScale"/>
	</xsl:call-template>
	
	<xsl:variable name="colour">
		<xsl:choose>
			<xsl:when test="$type2='Black'">#black</xsl:when>
			<xsl:otherwise>#white</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:if test="($num2&lt;$numberFrom) or ($isNum='0')">
		<xsl:variable name="myX1" select="format-number($trackX,'#.#####')"/>
		<xsl:variable name="myY1" select="format-number($trackY,'#.#####')"/>
	
		<g transform="matrix(1 0 0 1 {$myX1} {$myY1})">
			<xsl:call-template name="findxy">
				<xsl:with-param name="type" select="$colour"/>
				<xsl:with-param name="axis" select="$spot"/>
			  <xsl:with-param name="label" select="$newLabel"/>
			</xsl:call-template>
		</g> 
	</xsl:if>
 </xsl:if>
</xsl:template>

<!--=================================================================
	findComments finds comments and context stone
==================================================================-->

<xsl:template name="findComments" >
  <xsl:for-each select="/Go/GoGame/Nodes/Node/Comment/P[string(.) and ../preceding-sibling::*[@number&gt;=$numberFrom]]
	|/Go/GoGame/Nodes/Comment/P[string(.) and ../preceding-sibling::*[@number&gt;=$numberFrom]]">
  	<xsl:variable name="pos" select="position()"/>
		<xsl:variable name="matchNumber">
			<xsl:call-template name="getPrecedingMoveNum"/>
		</xsl:variable> 
		<xsl:if test="$matchNumber&lt;=$myStopAt">
		  <xsl:variable name="matchColour">
				<xsl:for-each select="../preceding-sibling::Black|../preceding-sibling::White">
					<xsl:value-of select = "name(.)"/>
				</xsl:for-each>
		  </xsl:variable>
			<xsl:call-template name="drawComments">
				<xsl:with-param name="offset" select="$pos"/>
				<xsl:with-param name="type1" select="$matchColour"/>
				<xsl:with-param name="num1" select="$matchNumber - $numberOffset"/>
				<xsl:with-param name="comment" select="."/>
			</xsl:call-template>
		</xsl:if>
 	</xsl:for-each>
</xsl:template>

<!--=================================================================
	drawComments writes comments and sends context stone to drawxy
==================================================================-->

<xsl:template name="drawComments">
	<xsl:param name="offset">0</xsl:param>
	<xsl:param name="type1">black</xsl:param>
	<xsl:param name="num1">0</xsl:param>
	<xsl:param name="comment">no comment</xsl:param>

	<xsl:variable name="MyCoOffsetX" select="$CommentOffsetX+$WholeBOffsetX"/>
	<xsl:variable name="MyCoOffsetY" select="$CommentOffsetY+$WholeBOffsetY+
		($OffStoneScale*$yGap*$MyStoneWidth*(($offset)-1))"/>

	<xsl:call-template name="drawxy">
			<xsl:with-param name="type" select="$type1"/>
			<xsl:with-param name="finalx" select="$MyCoOffsetX"/>
			<xsl:with-param name="finaly" select="$MyCoOffsetY"/>
		  <xsl:with-param name="scale" select="$OffStoneScale"/>
	</xsl:call-template>
	<xsl:call-template name="labelStone">
			<xsl:with-param name="type" select="$type1"/>
			<xsl:with-param name="finalx" select="$MyCoOffsetX"/>
			<xsl:with-param name="finaly" select="$MyCoOffsetY"/>
		  <xsl:with-param name="label" select="$num1"/>
		  <xsl:with-param name="scale" select="$OffStoneScale"/>
	</xsl:call-template>

	<xsl:variable name="myScale" select="format-number(3*$ScaleFactor,'#.#####')"/>
	<xsl:variable name="myX" select="format-number($MyCoOffsetX+($textGap*$MyStoneWidth),'#.#####')"/>
	<xsl:variable name="myY" select="format-number($MyCoOffsetY,'#.#####')"/>
	<g transform='matrix({$myScale} 0 0 {$myScale} {$myX} {$myY})'>
		<text x='0' y='20' style='fill:black;font-family:{$fontFamily};font-size:{$stoneFontSize}{$fontUnit}'><xsl:value-of select = "."/></text>
	</g>

</xsl:template>

<!--==============================================================-->

<xsl:template match="BoardSize">

	<xsl:variable name="MyGridOffset" select="$StoneUnderHang*$MyStoneWidth"/>

	<xsl:if test="$format='pretty'">
		<xsl:variable name="lineFudge" select="4.5 div $MyStoneWidth"/>
		<rect x="0" y="0" width="{$MyWoodSize}" height="{$MyWoodSize}" style="fill:url(#wood)"/>
		<rect x="0" y="0" width="{$GridSize}" height="{$GridSize}" 
		style="stroke-width:0.6;stroke:rgb(0,0,0);fill:url(#grid)"
		transform="matrix(1 0 0 1 {$MyGridOffset - $lineFudge} {$MyGridOffset - $lineFudge})"/>
	</xsl:if>

	<xsl:if test="$format='plain'">
		<xsl:variable name="lineFudge" select="10 div $MyStoneWidth"/>
		<rect x="0" y="0" width="{$GridSize}" height="{$GridSize}"
		style="stroke-width:2;stroke:rgb(0,0,0);fill:url(#grid)"
		transform="matrix(1 0 0 1 {$MyGridOffset - $lineFudge} {$MyGridOffset - $lineFudge})"/>
	</xsl:if>

	<xsl:choose>
	<xsl:when test=".='19'">
		<xsl:call-template name="displayDots">
		  <xsl:with-param name="dotType">#Jdot</xsl:with-param>
			<xsl:with-param name="dotList">K10,Q10,D10,K16,Q16,D16,K4,Q4,D4</xsl:with-param>
		</xsl:call-template>
	</xsl:when>
	<xsl:when test=".='17'">
		<xsl:call-template name="displayDots">
		  <xsl:with-param name="dotType">#Jdot</xsl:with-param>
			<xsl:with-param name="dotList">J9,O9,D9,J14,O14,D14,J4,O4,D4</xsl:with-param>
		</xsl:call-template>
	</xsl:when>
	<xsl:when test=".='15'">
		<xsl:call-template name="displayDots">
		  <xsl:with-param name="dotType">#Jdot</xsl:with-param>
			<xsl:with-param name="dotList">H8,M8,D8,H12,M12,D12,H4,M4,D4</xsl:with-param>
		</xsl:call-template>
	</xsl:when>
	<xsl:when test=".='13'">
		<xsl:call-template name="displayDots">
		  <xsl:with-param name="dotType">#Jdot</xsl:with-param>
			<xsl:with-param name="dotList">K10,D10,K4,Q4</xsl:with-param>
		</xsl:call-template>
	</xsl:when>
	<xsl:otherwise></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--==============================================================-->
 
<xsl:template name="setupSVG">

	<xsl:if test="$format='pretty'">
	<defs>
		<pattern id="wood" patternUnits="userSpaceOnUse" width="96" height="96">
			<image xlink:href="wood057.jpg" x="0" y="0" width="96" height="96"/>
		</pattern>
		<pattern id="grid" width="{$MyStoneWidth}" height="{$MyStoneWidth}"
			patternUnits="userSpaceOnUse" patternContentUnits="userSpaceOnUse">
			<line style="stroke:rgb(0,0,0);stroke-width:1;fill:none;fill-opacity:1" x1="0"
			y1="0" x2="0" y2="{$MyStoneWidth}"/>
			<line style="stroke:rgb(0,0,0);stroke-width:1;fill:none;fill-opacity:1" x1="0"
			y1="0" x2="{$MyStoneWidth}" y2="0"/>
		</pattern>
		<filter id="Gaussian_Blur" filterUnits="objectBoundingBox" x="-10%" y="-10%"
			width="150%" height="150%">
			<feGaussianBlur in="SourceGraphic" stdDeviation="10 10"/>
		</filter>
		<radialGradient id="black_spot" cx="30%" cy="30%" fx="30%" fy="30%" r="40%"
			gradientUnits="objectBoundingBox">
			<stop offset="0" style="stop-color:rgb(200,200,200);stop-opacity:1"/>
			<stop offset="1" style="stop-color:rgb(10,10,10);stop-opacity:1"/>
		</radialGradient>
		<radialGradient id="white_spot" cx="30%" cy="30%" fx="40%" fy="40%" r="60%"
			gradientUnits="objectBoundingBox">
			<stop offset="0" style="stop-color:rgb(255,255,255);stop-opacity:1"/>
			<stop offset="0.5" style="stop-color:rgb(250,250,250);stop-opacity:1"/>
			<stop offset="1" style="stop-color:rgb(200,200,200);stop-opacity:1"/>
		</radialGradient>
		<path id="shadow" d="M134.071 295.34 C183.395 295.34 254.213 301.444 284.027 
		250.493 C293.17 234.867 301.211 125.883 281.527 128.766 C276.385 129.519
		179.026 259.739 137.821 285.09" style="filter:url(#Gaussian_Blur);
		opacity:0.65;fill:rgb(20,20,20);stroke:none;stroke-width:1;"/>
	</defs>
	<symbol id="blackStone">
		<use xlink:href="#shadow"/>
		<circle style="stroke:none;stroke-width:1;stroke-opacity:1;fill:url(#black_spot);
		fill-opacity:1;" cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="{$StoneRadius}"/>
	</symbol>
	<symbol id="whiteStone">
		<use xlink:href="#shadow"/>
		<circle style="stroke:none;stroke-width:1;stroke-opacity:1;fill:url(#white_spot);
		fill-opacity:1;" cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="{$StoneRadius}"/>
	</symbol>
	<symbol id="Jdot">
		<circle cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="25" style="fill:rgb(0,0,0);
		stroke:none;stroke-width:1"/>
	</symbol>
	</xsl:if>

	<xsl:if test="$format='plain'">
	<defs>	
		<pattern id="grid" width="{$MyStoneWidth}" height="{$MyStoneWidth}"
		patternUnits="userSpaceOnUse" patternContentUnits="userSpaceOnUse">
			<line style="stroke:rgb(0,0,0);stroke-width:2;fill:none;fill-opacity:1" x1="0"
			y1="0" x2="0" y2="{$MyStoneWidth}"/>
			<line style="stroke:rgb(0,0,0);stroke-width:2;fill:none;fill-opacity:1" x1="0"
			y1="0" x2="{$MyStoneWidth}" y2="0"/>
		</pattern>
	</defs>
	<symbol id="blackStone">
		<circle style="stroke:none;stroke-width:1;stroke-opacity:1;fill:rgb(0,0,0);
		fill-opacity:1;" cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="{$StoneRadius}"/>
	</symbol>
	<symbol id="whiteStone">
		<circle style="stroke:rgb(0,0,0);stroke-width:10;stroke-opacity:1;fill:rgb(255,255,255);
		fill-opacity:1;" cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="{$StoneRadius - 5}"/>
	</symbol>
	<symbol id="Jdot">
		<circle cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="35" style="fill:rgb(0,0,0);
		stroke:none;stroke-width:1"/>
	</symbol>
	<symbol id="whitefillsquare">
		<rect x="{-150+$ShapeOffset}" y="{-150+$ShapeOffset}" width="300" height="300" 
		style="stroke:none;fill:rgb(255,255,255)"/>
	</symbol>
	</xsl:if>

	<symbol id="blacksquare">
		<rect x="{-90+$ShapeOffset}" y="{-90+$ShapeOffset}" width="180" height="180" 
		style="stroke:rgb(0,0,0);stroke-width:20;fill:none"/>
	</symbol>
	<symbol id="blackcircle">
		<circle cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="100" 
		style="fill:none;stroke:rgb(0,0,0);stroke-width:20"/>
	</symbol>
	<symbol id="blacktriangle">
		<polygon points="{$ShapeOffset},{-115+$ShapeOffset} {-100+$ShapeOffset},
		{85+$ShapeOffset} {100+$ShapeOffset},{85+$ShapeOffset}" style="fill:none;
		stroke:rgb(0,0,0);stroke-width:20"/>
	</symbol>
	<symbol id="blackcross">
		<line x1="{-100+$ShapeOffset}" y1="{-100+$ShapeOffset}" x2="{100+$ShapeOffset}" 
		y2="{100+$ShapeOffset}" style="fill:none;stroke:rgb(0,0,0);stroke-width:20"/>
		<line x1="{100+$ShapeOffset}" y1="{-100+$ShapeOffset}" x2="{-100+$ShapeOffset}" 
		y2="{100+$ShapeOffset}" style="fill:none;stroke:rgb(0,0,0);stroke-width:20"/>
	</symbol>
	<symbol id="whitesquare">
		<rect x="{-90+$ShapeOffset}" y="{-90+$ShapeOffset}" width="180" height="180" 
		style="stroke:rgb(255,255,255);stroke-width:20;fill:none"/>
	</symbol>
	<symbol id="whitecircle">
		<circle cx="{$ShapeOffset}" cy="{$ShapeOffset}" r="100" 
		style="fill:none;stroke:rgb(255,255,255);stroke-width:20"/>
	</symbol>
	<symbol id="whitetriangle">
		<polygon points="{$ShapeOffset},{-115+$ShapeOffset} {-100+$ShapeOffset},
		{85+$ShapeOffset} {100+$ShapeOffset},{85+$ShapeOffset}" style="fill:none;
		stroke:rgb(255,255,255);stroke-width:20"/>
	</symbol>
	<symbol id="whitecross">
		<line x1="{-100+$ShapeOffset}" y1="{-100+$ShapeOffset}" x2="{100+$ShapeOffset}" 
		y2="{100+$ShapeOffset}" style="fill:none;stroke:rgb(255,255,255);stroke-width:20"/>
		<line x1="{100+$ShapeOffset}" y1="{-100+$ShapeOffset}" x2="{-100+$ShapeOffset}" 
		y2="{100+$ShapeOffset}" style="fill:none;stroke:rgb(255,255,255);stroke-width:20"/>
	</symbol>

</xsl:template>

<!--=================================================================
	information on the name and range of numbers in the diagram
==================================================================-->

<xsl:template name="findRange">

	<xsl:variable name="myNumberFrom">
 		<xsl:choose>
		<xsl:when test="$numberFrom=0">
			<xsl:value-of select="1"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$numberFrom"/>
		</xsl:otherwise>
		</xsl:choose>
  </xsl:variable>

  <xsl:variable name="boardString">
 		<xsl:choose>
		<xsl:when test="($boardName!='') and ($showRange='on')">
			<xsl:value-of select="concat($boardName,' (',$myNumberFrom,' - ',$myStopAt,')')"/>
		</xsl:when>
		<xsl:when test="($boardName='') and ($showRange='on')">
			<xsl:value-of select="concat($myNumberFrom,' - ',$myStopAt)"/>
		</xsl:when>
		<xsl:when test="($boardName!='') and ($showRange!='on')">
			<xsl:value-of select="$boardName"/>
		</xsl:when>
		<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
  </xsl:variable>

	<xsl:variable name="fontScale" select="$stoneFontSize div 5"/>
	<text x='0' y='0' text-anchor="middle" 
	transform='translate({$WholeBOffsetX} {$WholeBOffsetY+$MyWoodSize+$showRangeY})' 
	style='fill:black;font-family:{$fontFamily};font-size:{$fontScale}{$fontUnit}'><xsl:value-of select="$boardString"/></text>
</xsl:template>

<!--=================================================================
	basic display of some parameter values
==================================================================-->

<xsl:template name="findParams">
	<xsl:variable name="fontScale" select="$stoneFontSize div 5"/>
	<text x='0' y='0' transform='translate({$showParamsX} {$showParamsY})' 
	style='fill:black;font-family:{$fontFamily};font-size:{$fontScale}{$fontUnit}'>ClipX is <xsl:value-of select="$clipX"/></text>
	<text x='0' y='0' transform='translate({$showParamsX} {$showParamsY+20})'
	style='fill:black;font-family:{$fontFamily};font-size:{$fontScale}{$fontUnit}'>ClipY is <xsl:value-of select="$clipY"/></text>
</xsl:template>

<!--==============================================================-->

<xsl:template name="getPrecedingMoveNum">
	<xsl:variable name="firstCount" select="count(
		preceding::White[name(parent::*)!='Variation' and name(../parent::*)!='Variation']
		|preceding::Black[name(parent::*)!='Variation' and name(../parent::*)!='Variation'])"/>
	<xsl:choose>
		<xsl:when test="$firstCount=''">
			<xsl:value-of select="0"/>
		</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$firstCount"/>
	</xsl:otherwise>
	</xsl:choose>	
</xsl:template>

<!--=================================================================
	Searches to return colour of move (if any) currently on a point
==================================================================-->

<xsl:template name="getColourA">
	<xsl:param name="xy">A1</xsl:param>
		<xsl:for-each select="preceding::White|preceding::Black|preceding::AddWhite|preceding::AddBlack">
			<xsl:if test="$xy=@at">
				<xsl:if test="name(parent::*)!='Variation' and name(../parent::*)!='Variation'">
					<xsl:choose>
					<xsl:when test="name(.)='Black' or name(.)='White'">
						<xsl:value-of select="concat(name(.),'-')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat(substring-after(name(.),'Add'),'-')"/>
					</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
			</xsl:if>
		</xsl:for-each>
</xsl:template>

<xsl:template name="getColourB">
	<xsl:param name="xy">A1</xsl:param>
		<xsl:for-each select="preceding::White[@number &gt;= $numberFrom]
		|preceding::Black[@number &gt;= $numberFrom]
		|preceding::AddWhite[preceding-sibling::*[@number &gt;= $numberFrom]]
		|preceding::AddBlack[preceding-sibling::*[@number &gt;= $numberFrom]]">
			<xsl:if test="$xy=@at">
				<xsl:if test="name(parent::*)!='Variation' and name(../parent::*)!='Variation'">
					<xsl:choose>
					<xsl:when test="name(.)='Black' or name(.)='White'">
						<xsl:value-of select="concat(name(.),'-')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="concat(substring-after(name(.),'Add'),'-')"/>
					</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
			</xsl:if>
		</xsl:for-each>
</xsl:template>

<!--=================================================================
	display handicap points
==================================================================-->

<xsl:template name="displayDots">
	<xsl:param name="dotType">#Jdot</xsl:param>
	<xsl:param name="dotList"/>

	<xsl:variable name="point" select="substring-before($dotList,',')"/>
  <xsl:choose>
    <xsl:when test="string-length($point) &gt; 0">
			<xsl:call-template name="findxy">
      	<xsl:with-param name="type"><xsl:value-of select="$dotType"/></xsl:with-param>
		  	<xsl:with-param name="axis"><xsl:value-of select="$point"/></xsl:with-param>
				<xsl:with-param name="label"/>
			</xsl:call-template>
      <xsl:call-template name="displayDots">
        <xsl:with-param name="dotList"><xsl:value-of select="substring-after($dotList,',')"/></xsl:with-param>
    	</xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
			<xsl:call-template name="findxy">
      	<xsl:with-param name="type"><xsl:value-of select="$dotType"/></xsl:with-param>
		  	<xsl:with-param name="axis"><xsl:value-of select="$dotList"/></xsl:with-param>
				<xsl:with-param name="label"/>
			</xsl:call-template>
    </xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--=================================================================
	loads moves (and setup stones) to script in document order
==================================================================-->

<xsl:template name="loadMoves">
	<xsl:for-each select="(//Black|//White|//AddBlack|//AddWhite)">
		<xsl:if test="name(parent::*)!='Variation' and name(../parent::*)!='Variation'">
			<xsl:variable name="findNum"> 
				<xsl:choose>
				<xsl:when test="name(.)='Black' or name(.)='White'">
					<xsl:value-of select="@number"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="getPrecedingMoveNum"/>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

			<xsl:if test="$findNum &lt; $numberFrom">
	 			<xsl:variable name="error" select="goTool:loadMove(string(@at), string(name(.)))"/>
				<xsl:if test="$error='true'">
					<xsl:message> move error (<xsl:value-of select="$error"/>)</xsl:message>
				</xsl:if>
			</xsl:if>		
		</xsl:if>
	</xsl:for-each>

	<xsl:if test="$debug">
		<xsl:message>peekAll (<xsl:value-of select="goTool:peekAll()"/>)</xsl:message>
	</xsl:if>
</xsl:template>

<!--=================================================================
	Javascript tools to detect captured stones in split diagrams
==================================================================-->

<lxslt:component prefix="goTool" 
		 elements="counterInit counterIncr"
		 functions="counterRead init loadMove peekCell peekAll">
<lxslt:script lang="javascript">
<![CDATA[
// Copyleft 2000 by Dmitry Mayorov.  Use it anyway you choose.
// Modified 2002 by D Gilder.  Use it anyway you choose.

var s = "ABCDEFGHJKLMNOPQRSTUVWXYZ[\]^";
var boardsize      = 19;
var blackMove      = true;
var cellfarm       = new Array(); // cellfarm[gname(x, y)] = {"x"|"o"|"."}
var counter;
var group_x                = new Array(); // used by *group*() functions
var group_y                = new Array(); // used by *group*() functions
var group_has_liberties    = false;       // used by *group*() functions

//
// Returns the hash name of the specified location.
// This hash name is used as an index for cellfarm array.
//
function gname(x, y)
{
  return "grid" + x + ", " + y;
}

//
// Change a value of the cell
//
function put_cell(x, y, isempty)
{
  var g;

  g = gname(x, y);
  if (isempty)
    cellfarm[g] = ".";
  else 
		if (blackMove) 
	    cellfarm[g] = "x";
	  else 
	    cellfarm[g] = "o";
}

function delete_group(isblack)
{
  for (var i = 0; i < group_x.length; i++) {
    put_cell(group_x[i], group_y[i], true);
  }
}

function group_iterator(x, y, c)
{
  if ((x < 0) || (x >= boardsize) || (y < 0) || (y >= boardsize)) {
    return;
  }
  var n = gname(x, y);
  if (cellfarm[n] == c) {
    group_x[group_x.length] = x;
    group_y[group_y.length] = y;
    var xa = new Array(x - 1, x + 1, x,     x);
    var ya = new Array(y,     y,     y - 1, y + 1);
    for (var i = 0; i < 4; i++) {
      var xx = xa[i];
      var yy = ya[i];
      var already = false;
      for (var j = 0; j < group_x.length; j++) {
        if ((xx == group_x[j]) && (yy == group_y[j])) {
	          already = true;
	          break;
        }
      }
      if (!already) {
        group_iterator(xx, yy, c);
      }
    }
  } else if (cellfarm[n] == '.') {
    group_has_liberties = true;
  } 
}

function get_group(x, y, isblack)
{
  var c;
  if (isblack)
    c = "x";
  else
    c = "o";
  group_x.length = 0;
  group_y.length = 0;
  group_has_liberties = false;
  group_iterator(x, y, c);
}

function loadMove (xy, colour)
{
 	var x;
	var y;
	error = true;
	x = s.indexOf(xy.substring(0,1));
	y = boardsize - Number(xy.substring(1,xy.length));
	if (!isNaN(x) && !isNaN(y)) {
		error = false;
		if ((colour == "White") || (colour == "AddWhite"))
			blackMove = false;
		else
			blackMove = true;	
		applyMove (x, y);
	}
  return error;
}

function applyMove (x, y)
{
  put_cell(x, y, false);

  var xarray = new Array(x - 1, x + 1, x,     x    );
  var yarray = new Array(y,     y,     y - 1, y + 1);
  for (var i = 0; i < 4; i++) {
    get_group(xarray[i], yarray[i], !blackMove);
    if (group_x.length > 0) {
      if (!group_has_liberties) {
        delete_group(!blackMove);
      }
    }
  }
	return null;
}

//
// examine cell contents
//
function peekCell(xy) 
{
	var answer;
 	var x;
	var y;

	x = s.indexOf(xy.substring(0,1));
	y = boardsize - Number(xy.substring(1,xy.length));
	answer = cellfarm[gname(x, y)];
	if (answer == '.')
		return 'empty';
	if (answer == 'x')
		return 'Black';
	if (answer == 'o')
		return 'White'; 
}

//
// examine everything (for debugging)
//
function peekAll()
{
  var h;

  h = "";
  for(var y = 0; y < boardsize; y++)
  {
    for(var x = 0; x < boardsize; x++)
    {
      h = h + cellfarm[gname(x, y)];
    }
  }
  return h;
}

//
// initialise
//
function init (size)
{
	error = 2
  boardsize = parseInt(size);
	if (!isNaN(boardsize))
		error = 0
	  for(var y = 0; y < boardsize; y++)
	  {
	    for(var x = 0; x < boardsize; x++)
	    {
 	     put_cell(x, y, true);
 	   }
 	 }
  return error;
}

function counterInit (xslproc, elem) {
	counter = parseInt(elem.getAttribute ("value"));
	return null;
}

function counterRead () { // Return a string.
	return "" + counter;
}

function counterIncr () {
	counter++;
	return null;
}

]]>
</lxslt:script>
</lxslt:component>

<!--==============================================================-->

<xsl:template match="Delete">
</xsl:template>

<xsl:template match="Variation">
</xsl:template>

</xsl:stylesheet>


