Saxon Extensions
Download Saxon Extensions (74k, ZIP)
This package provides a set of Saxon-specific XSLT extension functions for various purposes.
Font-related functions:
renderedTextLength()- Calculates the approximate rendered length of a text string given a font specification.
renderedTextFontName()- Returns the name of the actual font used by Java for a given input font family name. Useful for debugging problems resolving font family names to the proper font.
listJavaSystemFonts()- Returns a newline-delimited list of all the fonts known to Java for the current system. Useful for debugging font name resolution problems.
renderedTextLength()
This function is primarily intended to enable simple and approximate XSLT-time copy fitting for use with XSL-FO or CSS-based HTML output (that is, environments where composition is done in terms of real measurements, as opposed to device-specific measurements such as pixels).
The renderedTextLength() function takes a string and font specification (family, weight, size) and returns the nomimal rendered length of that string as determined by the Java AWT FontMetrics support. This value is "nominal" because it cannot take into account any variables that might be applied in the FO or HTML rendition step, such as additional tightening or loosing of inter-character or inter-word space, differences in font metrics calculation, and so on. Also, if the output is justified, then renderedTextLength() cannot predict the real rendered length. Thus, the value is approximate. But it is still better than nothing and should usually be within one em width of the final value, making it suitable for simple copy fitting, such as calculating the width of columns in definition-list type presentations.
To use the function, specify a text string and font specification, consisting of the font family name, the font weight (bold or normal), and the font size, in points:
<xsl:variable name="text">
<xsl:apply-templates select="term" mode="text-only"/>
</xsl:variable>
<xsl:variable name="term-width"
select="inno_iso:renderedTextWidth($text, 'Times New Roman',
'normal', 12pt)"/>
<fo:list-block provisional-distance-between-starts="{$term-width}">
...
Testing The renderedTextLength() Function
The source
package includes the directory xslt/which contains a sample document
and XSLT style sheet to run against that document. The output demonstrates
the use of the function to calculate string widths and generate definition
lists that are automatically tuned to their content.
To run the XSLT script, you must have the saxon.jar JAR file available. The following instructions assume that the inno_iso_saxon_extensions.jar file is at the top level of the project . The instructions must also include the java executable in your path.
Navigate to the xslts/ directory and issue the following
command to generate an FO instance from the rendered_text_width_test.xml
file:
The rendered result should look something like this:
The XSLT style sheet rendered_text_width_test_fo.xsl
includes a template that demonstrates how to calculate the length
of the longest member of a node list, which is used in this case to determine
the length of the longest term in each definition list.
License
This package is offered under the GNU Lesser General Public
License (LGPL), which means that the compiled library may be distributed with
non-open-source applications. If the source code is modified, it must carry
the Innodata Isogen copyright statement and the system it is incorporated
in must itself be licensed with the GNU GPL or LGPL license. See the the file
LICENSE.inno-iso_saxon_extensions in the distribution package.
Other licensing terms are available by request to
Contact Eliot Kimber, , for more information or to report bugs with this software.


