length-in-points

length-in-points — Returns the size, in points, of a specified length

Description

The length-in-points template converts a length specification to points and returns that value as an unqualified number.

Caution

There is no way for the template to infer the size of an em. It relies on the default em.size which is initially 10 (for 10pt).

Similarly, converting pixels to points relies on the pixels.per.inch parameter which is initially 90.

<xsl:template name="length-in-points">
  <xsl:param name="length" select="'0pt'"></xsl:param>
  <xsl:param name="em.size" select="10"></xsl:param>
  <xsl:param name="pixels.per.inch" select="90"></xsl:param>

  <xsl:variable name="magnitude">
    <xsl:call-template name="length-magnitude">
      <xsl:with-param name="length" select="$length"></xsl:with-param>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="units">
    <xsl:value-of select="substring($length, string-length($magnitude)+1)"></xsl:value-of>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="$units = 'pt'">
      <xsl:value-of select="$magnitude"></xsl:value-of>
    </xsl:when>
    <xsl:when test="$units = 'cm'">
      <xsl:value-of select="$magnitude div 2.54 * 72.0"></xsl:value-of>
    </xsl:when>
    <xsl:when test="$units = 'mm'">
      <xsl:value-of select="$magnitude div 25.4 * 72.0"></xsl:value-of>
    </xsl:when>
    <xsl:when test="$units = 'in'">
      <xsl:value-of select="$magnitude * 72.0"></xsl:value-of>
    </xsl:when>
    <xsl:when test="$units = 'pc'">
      <xsl:value-of select="$magnitude * 12.0"></xsl:value-of>
    </xsl:when>
    <xsl:when test="$units = 'px'">
      <xsl:value-of select="$magnitude div $pixels.per.inch * 72.0"></xsl:value-of>
    </xsl:when>
    <xsl:when test="$units = 'em'">
      <xsl:value-of select="$magnitude * $em.size"></xsl:value-of>
    </xsl:when>
    <xsl:otherwise>
      <xsl:message>
        <xsl:text>Unrecognized unit of measure: </xsl:text>
        <xsl:value-of select="$units"></xsl:value-of>
        <xsl:text>.</xsl:text>
      </xsl:message>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>