Sif2svg.xsl

From Synfig Studio :: Documentation
Jump to: navigation, search
Languages Language: 

English


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" xmlns:str="http://exslt.org/strings" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
	<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

	<xsl:template match="canvas">
		<svg version="1.1" width="{@width}" height="{@height}" filter="url(#gamma)">
			<xsl:attribute name="viewBox">
				<xsl:variable name="view-box" select="str:split(@view-box)"/>
				<xsl:value-of select="0+$view-box[1]"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="-$view-box[2]"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="-$view-box[1]+$view-box[3]"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="$view-box[2]-$view-box[4]"/>
			</xsl:attribute>
			<xsl:if test="@antialias=0">
				<xsl:attribute name="shape-rendering">optimizeSpeed</xsl:attribute>
			</xsl:if>
			<defs>
				<filter id="gamma">
					<feComponentTransfer>
						<feFuncR type="gamma" exponent="0.45"/>
						<feFuncG type="gamma" exponent="0.45"/>
						<feFuncB type="gamma" exponent="0.45"/>
					</feComponentTransfer>
				</filter>
			</defs>
			<xsl:apply-templates select="layer|name|desc"/>
		</svg>
	</xsl:template>

	<xsl:template match="name">
		<title><xsl:value-of select="."/></title>
	</xsl:template>

	<xsl:template match="desc">
		<desc><xsl:value-of select="."/></desc>
	</xsl:template>

	<xsl:template match="layer">
		<error type="{@type}"/>
	</xsl:template>

	<xsl:template match="layer[@type='PasteCanvas']">
		<g transform="translate({param/vector/x}, {-param/vector/y})">
			<xsl:apply-templates select="param[@name='canvas']/canvas/layer"/>
		</g>
	</xsl:template>

	<xsl:template match="layer[@type='rectangle']">
		<rect id="{@desc}" x="{math:min(param/vector/x)}" y="{-math:max(param/vector/y)}" width="{math:abs(param[@name='point2']/vector/x - param[@name='point1']/vector/x)}" height="{math:abs(param[@name='point2']/vector/y - param[@name='point1']/vector/y)}" fill-opacity="{param[@name='amount']/real/@value * param/color/a}">
			<xsl:attribute name="fill">
				<xsl:apply-templates select="param/color"/>
			</xsl:attribute>
		</rect>
	</xsl:template>

	<xsl:template match="layer[@type='import']">
		<image id="{@desc}" xlink:href="{param[@name='filename']/string}" x="{math:min(param/vector/x)}" y="{-math:max(param/vector/y)}" width="{math:abs(param[@name='br']/vector/x - param[@name='tl']/vector/x)}" height="{math:abs(param[@name='br']/vector/y - param[@name='tl']/vector/y)}">
			<xsl:if test="param[@name='br']/vector/x < param[@name='tl']/vector/x">
				<xsl:attribute name="transform">
					<xsl:text>scale(</xsl:text>
					<xsl:choose>
					<xsl:when test="param[@name='br']/vector/x < param[@name='tl']/vector/x">
						<xsl:text>-1</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>1</xsl:text>
					</xsl:otherwise>
					</xsl:choose>
					<xsl:text>,</xsl:text>
					<xsl:text>1</xsl:text>
					<xsl:text>)</xsl:text>
				</xsl:attribute>
			</xsl:if>
		</image>
	</xsl:template>

	<xsl:template match="layer[@type='star']">
		<circle id="{@desc}" cx="{param[@name='origin']/vector/x}" cy="{-param[@name='origin']/vector/y}" r="{param[@name='radius1']/real/@value}" r2="{param[@name='radius1']/real/@value}" points="5">
			<xsl:attribute name="fill">
				<xsl:apply-templates select="param/color"/>
			</xsl:attribute>
		</circle>
	</xsl:template>

	<xsl:template match="layer[@type='circle']">
		<circle id="{@desc}" r="{param[@name='radius']/real/@value}" fill-opacity="{param[@name='amount']/real/@value * param/color/a}">
			<xsl:attribute name="cx">
				<xsl:choose>
					<xsl:when test="param[@name='origin']/vector">
						<xsl:value-of select="param[@name='origin']/vector/x"/>
					</xsl:when>
					<xsl:when test="param[@name='pos']/vector">
						<xsl:value-of select="param[@name='pos']/vector/x"/>
					</xsl:when>
					<xsl:when test="param[@name='origin']/animated[@type='vector']">
						<xsl:value-of select="param[@name='origin']/animated[@type='vector']/waypoint/vector/x"/>
					</xsl:when>
				</xsl:choose>
			</xsl:attribute>
			<xsl:attribute name="cy">
				<xsl:choose>
					<xsl:when test="param[@name='origin']/vector">
						<xsl:value-of select="-param[@name='origin']/vector/y"/>
					</xsl:when>
					<xsl:when test="param[@name='pos']/vector">
						<xsl:value-of select="-param[@name='pos']/vector/y"/>
					</xsl:when>
					<xsl:when test="param[@name='origin']/animated[@type='vector']">
						<xsl:value-of select="-param[@name='origin']/animated[@type='vector']/waypoint/vector/y"/>
					</xsl:when>
				</xsl:choose>
			</xsl:attribute>
			<xsl:attribute name="fill">
				<xsl:apply-templates select="param/color"/>
			</xsl:attribute>
			<xsl:if test="param[@name='origin']/animated[@type='vector']">
				<xsl:call-template name="smilTransform">
					<xsl:with-param name="type" select="'translate'"/>
					<xsl:with-param name="time" select="param[@name='origin']/animated[@type='vector']/waypoint"/>
					<xsl:with-param name="animate" select="param[@name='origin']/animated[@type='vector']/waypoint/vector"/>
				</xsl:call-template>
				<!--<xsl:call-template name="smilXML">
					<xsl:with-param name="attribute" select="'cx'"/>
					<xsl:with-param name="time" select="param[@name='origin']/animated[@type='vector']/waypoint/@time"/>
					<xsl:with-param name="animate" select="param[@name='origin']/animated[@type='vector']/waypoint/vector/x"/>
				</xsl:call-template>
				<xsl:call-template name="smilXML">
					<xsl:with-param name="attribute" select="'cy'"/>
					<xsl:with-param name="time" select="param[@name='origin']/animated[@type='vector']/waypoint/@time"/>
					<xsl:with-param name="animate" select="param[@name='origin']/animated[@type='vector']/waypoint/vector/y"/>
				</xsl:call-template>-->
			</xsl:if>
		</circle>
	</xsl:template>

	<xsl:template match="layer[@type='text']">
		<xsl:variable name="text" select="str:split(param[@name='text']/string, '
')"/>
		<xsl:variable name="x">
			<xsl:if test="param[@name='origin']/vector/x">
				<xsl:value-of select="param[@name='origin']/vector/x"/>
			</xsl:if>
			<xsl:if test="param[@name='pos']/vector/x">
				<xsl:value-of select="param[@name='pos']/vector/x"/>
			</xsl:if>
		</xsl:variable>
		<text id="{@desc}" x="{$x}" font-weight="{param[@name='weight']/integer/@value}" font-family="{param[@name='family']/string}" text-anchor="middle" fill-opacity="{param[@name='amount']/real/@value * param/color/a}">
			<xsl:attribute name="y">
				<xsl:if test="param[@name='origin']/vector/y">
					<xsl:value-of select="-param[@name='origin']/vector/y - count($text) div 8.75"/>
				</xsl:if>
				<xsl:if test="param[@name='pos']/vector/y">
					<xsl:value-of select="-param[@name='pos']/vector/y - count($text) div 8.75"/>
				</xsl:if>
			</xsl:attribute>
			<xsl:attribute name="font-size">
				<xsl:value-of select="param[@name='size']/vector/x * 1"/>
			</xsl:attribute>
			<xsl:attribute name="fill">
				<xsl:apply-templates select="param/color"/>
			</xsl:attribute>
			<xsl:for-each select="$text">
				<xsl:choose>
					<xsl:when test="1 = position()">
						<xsl:value-of select="."/>
					</xsl:when>
					<xsl:otherwise>
						<tspan x="{$x}" dy="1em"><xsl:value-of select="."/></tspan>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</text>
	</xsl:template>

	<xsl:template match="layer[@type='polygon']">
		<xsl:variable name="origin" select="param[@name='origin']/vector"/>
		<polygon id="{@desc}" fill-opacity="{param[@name='amount']/real/@value * param/color/a}">
			<xsl:attribute name="points">
				<xsl:for-each select="param[@name='vector_list']/dynamic_list[@type='vector']/entry/vector">
					<xsl:value-of select="$origin/x + x"/><xsl:text>,</xsl:text>
					<xsl:value-of select="-$origin/y - y"/><xsl:text> </xsl:text>
				</xsl:for-each>
			</xsl:attribute>
			<xsl:attribute name="fill">
				<xsl:apply-templates select="param/color"/>
			</xsl:attribute>
		</polygon>
	</xsl:template>

	<xsl:template match="layer[@type='region'] | layer[@type='outline']">
		<xsl:variable name="x" select="param[@name='origin']/vector/x"/>
		<xsl:variable name="y" select="-param[@name='origin']/vector/y"/>
		<xsl:variable name="points">
			<xsl:for-each select="param[@name='bline']/bline[@type='bline_point']/entry/composite[@type='bline_point']">
				<xsl:choose>
					<xsl:when test="position() = 1">
						<xsl:text>M </xsl:text><xsl:value-of select="$x + point/vector/x"/>,<xsl:value-of select="$y - point/vector/y"/>
						<xsl:text> C </xsl:text><xsl:value-of select="$x + point/vector/x + t2/radial_composite[@type='vector']/radius/real/@value * -math:cos(t2/radial_composite[@type='vector']/theta/angle/@value) div 3"/>,<xsl:value-of select="$y - point/vector/y + t2/radial_composite[@type='vector']/radius/real/@value * math:sin(t2/radial_composite[@type='vector']/theta/angle/@value) div 3"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text> </xsl:text><xsl:value-of select="$x + point/vector/x + t1/radial_composite[@type='vector']/radius/real/@value * -math:cos(3.14159 * t1/radial_composite[@type='vector']/theta/angle/@value div 180) div 3"/>,<xsl:value-of select="$y - point/vector/y + t1/radial_composite[@type='vector']/radius/real/@value * math:sin(3.14159 * t1/radial_composite[@type='vector']/theta/angle/@value div 180) div 3"/>
						<xsl:text> </xsl:text><xsl:value-of select="$x + point/vector/x"/>,<xsl:value-of select="$y - point/vector/y"/>
						<xsl:text> C </xsl:text><xsl:value-of select="$x + point/vector/x + t2/radial_composite[@type='vector']/radius/real/@value * math:cos(3.14159 * t2/radial_composite[@type='vector']/theta/angle/@value div 180) div 3"/>,<xsl:value-of select="$y - point/vector/y + t2/radial_composite[@type='vector']/radius/real/@value * -math:sin(3.14159 * t2/radial_composite[@type='vector']/theta/angle/@value div 180) div 3"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
			<xsl:if test="param[@name='bline']/bline[@loop] = 'true'"> <!-- FIXME: reprendre ici demain. -->
				<xsl:text> </xsl:text><xsl:value-of select="$x + param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/point/vector/x + param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/t1/radial_composite[@type='vector']/radius/real/@value * -math:cos(3.14159 * param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/t1/radial_composite[@type='vector']/theta/angle/@value div 180) div 3"/>,<xsl:value-of select="$y - param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/point/vector/y + param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/t1/radial_composite[@type='vector']/radius/real/@value * math:sin(3.14159 * param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/t1/radial_composite[@type='vector']/theta/angle/@value div 180) div 3"/>
				<xsl:text> </xsl:text><xsl:value-of select="$x + param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/point/vector/x"/>,<xsl:value-of select="$y - param[@name='bline']/bline[@type='bline_point']/entry[1]/composite[@type='bline_point']/point/vector/y"/>
			</xsl:if>
		</xsl:variable>
		<path id="{@desc}" d="{$points}">
			<xsl:choose>
				<xsl:when test="@type='region'">
					<xsl:attribute name="fill">
						<xsl:apply-templates select="param/color"/>
					</xsl:attribute>
					<xsl:attribute name="stroke">none</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="stroke">
						<xsl:apply-templates select="param/color"/>
					</xsl:attribute>
					<xsl:attribute name="stroke-width"><xsl:value-of select="param[@name='width']/real/@value"/></xsl:attribute>
					<xsl:attribute name="fill">none</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="param[@name='origin']/animated[@type='vector']">
				<xsl:call-template name="smilTransform">
					<xsl:with-param name="attribute" select="'translate'"/>
					<xsl:with-param name="time" select="param[@name='origin']/animated[@type='vector']/waypoint"/>
					<xsl:with-param name="animate" select="param[@name='origin']/animated[@type='vector']/waypoint/vector"/>
				</xsl:call-template>
			</xsl:if>
		</path>
	</xsl:template>

	<!--<xsl:template match="layer[@type='linear_gradient']">
		<defs>
			<linearGradient id="{@desc}"
				x1="0"
				y1="0"
				x2="{math:min(param[@name='p2']/vector/x)}"
				y2="{-math:max(param[@name='p2']/vector/y)}">
				<xsl:for-each select="param[@name='gradient']/gradient/color">
					<stop 	offset="{100*@pos}%"
						stop-color="rgb({str:split(255*r, '.')[1]},{str:split(255*g, '.')[1]},{str:split(255*b, '.')[1]})"/>
				</xsl:for-each>
			</linearGradient>
		</defs>
		<rect fill="url(#{@desc})">
			<xsl:variable name="view-box" select="str:split(/canvas/@view-box)"/>
			<xsl:attribute name="x"><xsl:value-of select="0+$view-box[1]"/></xsl:attribute>
			<xsl:attribute name="y"><xsl:value-of select="-$view-box[2]"/></xsl:attribute>
			<xsl:attribute name="width"><xsl:value-of select="-$view-box[1]+$view-box[3]"/></xsl:attribute>
			<xsl:attribute name="height"><xsl:value-of select="$view-box[2]-$view-box[4]"/></xsl:attribute>
		</rect>
	</xsl:template>-->

	<xsl:template match="color">
		<xsl:text>rgb(</xsl:text>
		<xsl:value-of select="str:split(r * 255, '.')[1]"/><xsl:text>,</xsl:text>
		<xsl:value-of select="str:split(g * 255, '.')[1]"/><xsl:text>,</xsl:text>
		<xsl:value-of select="str:split(b * 255, '.')[1]"/><xsl:text>)</xsl:text>
	</xsl:template>

	<xsl:template match="d">
		<xsl:param name="vector"/>
	</xsl:template>

	<xsl:template name="keyTimes">
		<xsl:param name="time"/>
		<xsl:value-of select="$time/@time"/>
		<!--<xsl:for-each select="$time">
			<xsl:value-of select="str:replace(@time, 's', '') div str:replace(/canvas/@end-time, 's', '')"/>
			<xsl:if test="not(position()=last())">
				<xsl:text>; </xsl:text>
			</xsl:if>
		</xsl:for-each>-->
	</xsl:template>

	<xsl:template name="smilXML">
		<xsl:param name="attribute"/>
		<xsl:param name="time"/>
		<xsl:param name="animate"/>
		<animate attributeName="{$attribute}" dur="{/canvas/@end-time}" fill="freeze" repeatCount="indefinite">
			<!--<xsl:attribute name="keyTimes">
				<xsl:call-template name="keyTimes">
					<xsl:with-param name="time" select="$time" />
				</xsl:call-template>
			</xsl:attribute>-->
			<xsl:attribute name="values">
				<xsl:for-each select="$animate">
					<xsl:value-of select="."/>
					<xsl:if test="not(position()=last())">
						<xsl:text>; </xsl:text>
					</xsl:if>
				</xsl:for-each>
			</xsl:attribute>
		</animate>
	</xsl:template>

	<xsl:template name="smilTransform">
		<xsl:param name="type"/>
		<xsl:param name="time"/>
		<xsl:param name="animate"/>
		<animateTransform attributeName="transform" type="{$type}" dur="{/canvas/@end-time}" fill="freeze" repeatCount="indefinite">
			<!--<xsl:attribute name="keyTimes">
				<xsl:call-template name="keyTimes">
					<xsl:with-param name="vector" select="$time" />
				</xsl:call-template>
			</xsl:attribute>-->
			<xsl:attribute name="values">
				<xsl:for-each select="$animate">
					<xsl:value-of select="-str:split(/canvas/@view-box)[1] + x"/>,<xsl:value-of select="y"/>
					<xsl:if test="not(position()=last())">
						<xsl:text>; </xsl:text>
					</xsl:if>
				</xsl:for-each>
			</xsl:attribute>
		</animateTransform>
	</xsl:template>
</xsl:stylesheet>


Languages Language: 

English