blob: bfe5298a7c4e4474c7cd030d1c392f09bfbf1110 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
exclude-result-prefixes="svg"
>
<xsl:param name="height"/>
<!-- prevent users from causing memory overflows: -->
<xsl:variable name="maxHeight" select="1024"/>
<!--
scales an svg to height $height.
this is done by surrounding the image with a new <svg/> element with the desired height and
proportional width, and a viewBox whose dimensions are taken from the original image.
-->
<xsl:template match="/svg:svg">
<xsl:choose>
<xsl:when test="number($height) &gt; 0 and number($height) &lt;= $maxHeight">
<svg:svg>
<xsl:attribute name="height"><xsl:value-of select="$height"/></xsl:attribute>
<xsl:if test="number(@width) &gt; 0 and number(@height) &gt; 0">
<xsl:attribute name="width"><xsl:value-of select="ceiling(@width div @height * $height)"/></xsl:attribute>
</xsl:if>
<xsl:attribute name="viewBox"><xsl:value-of select="concat('0 0 ', @width, ' ', @height)"/></xsl:attribute>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</svg:svg>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
Wow. You requested an image height of <xsl:value-of select="$height"/>. No way José. Go DoS someone else.
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- batik seems to choke on comment elements. workaround: -->
<xsl:template match="svg:comment"/>
</xsl:stylesheet>