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

131 lines
4.1 KiB
XML
Raw Permalink 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="/">
2024-05-18 22:58:01 +02:00
<xsl:variable name="maxX"
select="max(*/*/@x)" />
<xsl:variable name="maxY"
select="max(*/*/@y)" />
<xsl:variable name="minX"
select="min(*/*/@x)" />
<xsl:variable name="minY"
select="min(*/*/@y)" />
<!-- Largeur et hauteur avec décalage -->
<xsl:variable name="width"
select="$maxX - $minX" />
<xsl:variable name="height"
select="$maxY - $minY" />
<svg
version="1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 {$width} {$height}">
<xsl:apply-templates select="traceur">
<xsl:with-param name="minX" select="$minX" />
<xsl:with-param name="minY" select="$minY" />
<xsl:with-param name="strokeWidth">
2024-05-18 23:02:56 +02:00
<!-- On prend le plus grand côté qu'on divise
avec `d` une valeur arbitraire qui rend bien -->
2024-05-18 22:58:01 +02:00
<xsl:variable name="d" select="1000" />
2024-05-18 23:02:56 +02:00
2024-05-18 22:58:01 +02:00
<xsl:choose>
<xsl:when test="$width > $height">
<xsl:value-of select="$width div $d" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$height div $d" />
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:apply-templates>
2024-05-06 14:40:26 +02:00
</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">
2024-05-18 23:02:56 +02:00
<xsl:param
name="minX" />
<xsl:param
name="minY" />
<xsl:param
name="strokeWidth" />
2024-05-18 22:58:01 +02:00
<!-- Décalage -->
2024-05-18 23:02:56 +02:00
<xsl:variable
2024-05-18 22:58:01 +02:00
name="transX">
2024-05-06 14:40:26 +02:00
<xsl:choose>
2024-05-18 22:58:01 +02:00
<xsl:when test="0 > $minX">
<xsl:value-of select="abs($minX)" />
2024-05-06 14:40:26 +02:00
</xsl:when>
<xsl:otherwise>
2024-05-18 22:58:01 +02:00
<xsl:value-of select="0" />
2024-05-06 14:40:26 +02:00
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable
2024-05-18 22:58:01 +02:00
name="transY">
<xsl:choose>
<xsl:when test="0 > $minY">
<xsl:value-of select="abs($minY)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
2024-05-18 22:58:01 +02:00
<g
stroke="black" stroke-width="{$strokeWidth}"
transform="translate({$transX}, {$transY})">
2024-05-06 20:16:34 +02:00
<xsl:call-template name="generatePath" />
2024-05-06 14:40:26 +02:00
</g>
</xsl:template>
2024-05-04 02:42:50 +02:00
2024-05-06 20:16:34 +02:00
<!-- Tracés -->
2024-05-06 14:40:26 +02:00
<xsl:template name="generatePath">
2024-05-06 20:16:34 +02:00
<xsl:param name="idx"
select="1" />
2024-05-18 23:02:56 +02:00
<xsl:param name="x"
select="0" />
<xsl:param name="y"
select="0" />
2024-05-06 20:16:34 +02:00
2024-05-18 23:02:56 +02:00
<xsl:if test="count(*) >= $idx">
<xsl:variable name="action"
select="*[position() = $idx]" />
2024-05-04 02:42:50 +02:00
2024-05-06 20:16:34 +02:00
<xsl:choose>
<!-- Déplacement -->
<xsl:when test="name($action) = 'MOVETO'">
<xsl:call-template name="generatePath">
<xsl:with-param name="idx" select="$idx + 1" />
<xsl:with-param name="x" select="$action/@x" />
<xsl:with-param name="y" select="$action/@y" />
</xsl:call-template>
</xsl:when>
2024-05-04 02:42:50 +02:00
2024-05-06 20:16:34 +02:00
<!-- Ecriture -->
<xsl:when test="name($action) = 'LINETO'">
<xsl:variable name="pos"
select="concat($x, ' ', $y)" />
<xsl:variable name="nouvellePos"
select="concat($action/@x, ' ', $action/@y)" />
<path
d="{concat('M', $pos, ' L', $nouvellePos)}" />
<xsl:call-template
name="generatePath">
<xsl:with-param name="idx" select="$idx + 1" />
<xsl:with-param name="x" select="$action/@x" />
<xsl:with-param name="y" select="$action/@y" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:if>
2024-05-06 14:40:26 +02:00
</xsl:template>
2024-05-04 02:42:50 +02:00
</xsl:transform>