This repository has been archived on 2024-05-19. You can view files and clone it, but cannot push or open issues or pull requests.
l-systems/svg.xsl

95 lines
3.2 KiB
XML
Raw Normal View History

2024-05-04 02:42:50 +02:00
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
2024-05-06 14:40:26 +02:00
xmlns="http://www.w3.org/2000/svg">
<xsl:output indent="yes" />
2024-05-04 02:42:50 +02:00
2024-05-06 14:40:26 +02:00
<xsl:template match="/">
<svg version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:apply-templates select="traceur" />
</svg>
</xsl:template>
2024-05-04 02:42:50 +02:00
2024-05-06 14:40:26 +02:00
<!-- Template pour le traceur -->
<xsl:template match="traceur">
<!-- Tracé -->
<xsl:variable name="path">
<xsl:call-template name="generatePath">
<xsl:with-param name="idx" select="1" />
2024-05-06 16:26:23 +02:00
<xsl:with-param name="path" select="'M0 0 '" />
2024-05-06 14:40:26 +02:00
</xsl:call-template>
</xsl:variable>
2024-05-04 02:42:50 +02:00
2024-05-06 14:40:26 +02:00
<!-- Dimensions arbitraires -->
<xsl:variable
name="width" select="1000" />
<xsl:variable
name="height" select="800" />
2024-05-06 14:40:26 +02:00
<xsl:variable name="scale">
<!-- Valeurs trouvés au fils de plusieurs tests -->
<xsl:variable name="x"
select="$width div max(LINETO/@x) div 1.4" />
<xsl:variable name="y"
select="$height div max(LINETO/@y) div 1.4" />
2024-05-06 14:40:26 +02:00
<!-- La plus petite valeur entre X et Y -->
<xsl:choose>
<xsl:when test="$y > $x">
<xsl:value-of select="$x " />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$y" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
2024-05-06 14:40:26 +02:00
<!-- Translation pour être à peu près centré -->
<xsl:variable
name="transX" select="($width * 1.2) - max(LINETO/@x) * $scale" />
<xsl:variable
name="transY" select="$height - max(LINETO/@y) * $scale" />
2024-05-06 14:40:26 +02:00
<!-- TODO : Faudrais faire en sorte que :
2024-05-06 00:34:58 +02:00
+ le scale est petit
+ l'épaisseur du tracé est épais -->
2024-05-06 14:40:26 +02:00
<g
transform="translate({$transX}, {$transY}) scale({$scale})">
<path d="{$path}" stroke="black" fill="none" />
</g>
</xsl:template>
2024-05-04 02:42:50 +02:00
2024-05-06 14:40:26 +02:00
<!-- Fonction récursive terminale -->
<xsl:template name="generatePath">
<xsl:param name="idx" />
<xsl:param name="path" />
2024-05-04 02:42:50 +02:00
2024-05-06 14:40:26 +02:00
<xsl:choose>
2024-05-06 14:56:05 +02:00
<xsl:when test="$idx > count(*)">
2024-05-06 14:40:26 +02:00
<xsl:value-of select="$path" />
</xsl:when>
<xsl:otherwise>
2024-05-06 14:56:05 +02:00
<xsl:variable
name="currentNode" select="*[position() = $idx]" />
<xsl:variable
name="pos" select="concat($currentNode/@x, ' ', $currentNode/@y)" />
2024-05-04 02:42:50 +02:00
2024-05-06 14:56:05 +02:00
<xsl:choose>
<xsl:when test="name($currentNode) = 'MOVETO'">
<xsl:call-template name="generatePath">
<xsl:with-param name="idx" select="$idx + 1" />
2024-05-06 16:26:23 +02:00
<xsl:with-param name="path" select="concat($path, ' M', $pos)" />
2024-05-06 14:56:05 +02:00
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="generatePath">
<xsl:with-param name="idx" select="$idx + 1" />
2024-05-06 16:26:23 +02:00
<xsl:with-param name="path" select="concat($path, ' L', $pos)" />
2024-05-06 14:56:05 +02:00
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
2024-05-06 14:40:26 +02:00
</xsl:otherwise>
</xsl:choose>
</xsl:template>
2024-05-04 02:42:50 +02:00
</xsl:transform>