RE: [xsl] Using Java in XSLT to determine font metrics

Subject: RE: [xsl] Using Java in XSLT to determine font metrics
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 22 Dec 2008 16:00:37 -0000
As far as I can see the Java class java.awt.FontMetrics does not have a
method getFontMetrics().

Generally, you may find the command line option -TJ useful for debugging
this kind of problem.

Also, please get out of the habit of writing this kind of code:

    <xsl:variable name="metrics">
        <xsl:value-of select="FontMetrics:getFontMetrics()"/>

when you mean

    <xsl:variable name="metrics" select="FontMetrics:getFontMetrics()"/>

This is always highly inefficient, and when you are manipulating external
Java objects it will almost certainly make your code incorrect, because you
really don't want to convert the object to a string.

Michael Kay 

> -----Original Message-----
> From: Ylvisaker, Steve [mailto:steve.j.ylvisaker@xxxxxxxxxxxxx] 
> Sent: 22 December 2008 15:37
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Using Java in XSLT to determine font metrics
> This is my first attempt at calling an external Java method 
> to do work on behalf of  a stylesheet. I'm not a Java 
> developer so I think I need a fairly complete "cookbook"
>  description of how to do this. My objective is to determine 
> the exact length, in pixels,  of a given string based on the 
> font and font size in play.
>  I'm sure I have two problems here. (1) to properly bind my 
> stylesheet to a Java name  space and (2) proper use of the 
> Java methods that parse my font and return the  metrics I'm 
> looking for.
>  Following is my failed stylesheet, annotated with comments 
> explaining what I am  "trying" to do at each step. I am very 
> certain that I am not properly telling whomever  which font I 
> am using.
>  I am working within the <oXygen/> IDE (Saxon8) and have 
> successfully gotten the example on  page 134 of Michael's 
> XSLT 2.0 3rd Edition to work. This tells me that I am 
> somewhat  successful in using Java extensions.
>  When I process this stylesheet I get the following error:
>  Cannot find a matching 0-argument function named 
> {ext://java.awt.FontMetrics}getFontMetrics()
>  Unfortunately this error message doesn't tell me if the 
> problem is that I'm not finding  the Java method or if I'm 
> finding it but not using it properly.
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet 
> xmlns:xsl=""; version="2.0"
>     xmlns:FontMetrics="ext://java.awt.FontMetrics">
>     <xsl:output indent="yes"/>
> <xsl:template match="/">
>     <xsl:variable name="tstString" select="'Hello World'"/>
> <!-- declair a font and fontsize - not shure who is listening 
> to this -->
>     FontMetrics:Font("helveticaWorld", Font.PLAIN, 10)
> <!-- obtain the metrics for the specified font -->
>     <xsl:variable name="metrics">
>         <xsl:value-of select="FontMetrics:getFontMetrics()"/>
>     </xsl:variable>
> <!-- obtain the length in pixels of the test string -->
>     <xsl:variable name="resultStringLength">
>          <xsl:value-of select="metrics.stringWidth($tstString)"/>
>     </xsl:variable>
> <!-- display the result -->
>     <xsl:message>result:<xsl:value-of 
> select="$resultStringLength"/></xsl:message>
> </xsl:template>
> </xsl:stylesheet>
> Thanks in advance for you help. Please keep in mind that I am 
> not a Java developer at all so nothing is obvious to me.
> Steve Ylvisaker
> Information transmitted by this email is proprietary to 
> Medtronic and is intended for use only by the individual or 
> entity to which it is addressed, and may contain information 
> that is private, privileged, confidential or exempt from 
> disclosure under applicable law. If you are not the intended 
> recipient or it appears that this mail has been forwarded to 
> you without proper authority, you are notified that any use 
> or dissemination of this information in any manner is 
> strictly prohibited. In such cases, please delete this mail 
> from your records.
> To view this notice in other languages you can either select 
> the following link or manually copy and paste the link into 
> the address bar of a web browser:

Current Thread