xsl-list
[Top] [All Lists]

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

2008-12-22 11:57:58
Thanks Michael

I think I found the method getFontMetrics(font) in Java class java.awt.Graphics
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html

I added -TJ as a parameter but I'm not convinced <oXygen/> is paying attention 
to it.

My error message now seems to be the result of my incorrect use of the method:

Error in call to extension function {public java.awt.FontMetrics 
java.awt.Graphics.getFontMetrics()}: Cannot convert supplied XPath value to the 
required type for the extension function.

I have tried several values for $font as well as placing the font name directly 
in the parameter list with the same result.

My revised stylesheet is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"
    xmlns:FontMetrics="ext://java.awt.Graphics">
    <xsl:output indent="yes"/>
<xsl:template match="/">

<!-- establish a font name -->
<xsl:variable name="font" select="'HelveticaWorld-Regular'"/>

<!-- obtain the metrics for a specified font -->
    <xsl:variable name="metrics" select="FontMetrics:getFontMetrics($font)"/>


<!-- display the result -->
    <xsl:message>result:<xsl:value-of select="$metrics"/></xsl:message>

</xsl:template>

</xsl:stylesheet>

-----Original Message-----
From: Michael Kay [mailto:mike(_at_)saxonica(_dot_)com]
Sent: Monday, December 22, 2008 10:01 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Using Java in XSLT to determine font metrics

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()"/>
    </xsl:variable>

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
http://www.saxonica.com/

-----Original Message-----
From: Ylvisaker, Steve 
[mailto:steve(_dot_)j(_dot_)ylvisaker(_at_)medtronic(_dot_)com]
Sent: 22 December 2008 15:37
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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="http://www.w3.org/1999/XSL/Transform"; 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

[CONFIDENTIALITY AND PRIVACY NOTICE]

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: http://emaildisclaimer.medtronic.com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--