Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Measuring string bounds in headless server

3 views
Skip to first unread message

bagbourne

unread,
Dec 19, 2003, 9:05:37 PM12/19/03
to
I have a problem with calculating screen string width for the browser
client.

I'm outputting a stream of content type application/vnd.ms-excel and
sending back an XML spreadsheet. (No complaints about using Mc$oft
products, it's what's needed!)

Excel does not autosize columns which contain string data, so the text
columns are the standard Excel width, and the text is wrapped onto about
7 lines. See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexcl2k2/html/odc_xmlss.asp
for details of the XML format.

Excel does allow you to specify the with of a column in points, so I was
using the awt Font classes to calculate the string bounds in pixels. I
explicitly set the resolution to be used to 72dpi by creating a
FontRenderContext with an AffineTransform with a scaling factor of (1.0,
1.0), so that asking the pixel width, gets the point width.

This worked fine testing it on my windoze machine, but on the Unix
server, it's obviously running in headless mode, and anyway, it doesn't
have the Arial font which I'm specifying in my spreadsheet as the font
to use.

Is there any way of calculating the width of a string in points knowing
its font and the point size of the font? You shouldn't need a graphcs
environment, just the font information. I have all the .TTF files, I
could copy them to the Unix machine if Java could use them and do the
calculation.

I hate leaving the user interface looking so shoddy with wrong sized
columns!

It SHOULD have worked in a headless environment according to
http://java.sun.com/j2se/1.4.2/docs/guide/awt/AWTChanges.html#headless
and we are on version 1.4.2_01. It actually blew up in awt code:


java.lang.NoClassDefFoundError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
at java.awt.Font.initializeFont(Font.java:308)
at java.awt.Font.<init>(Font.java:344)

That's when I'm trying to instantiate a

new Font("Arial", Font.PLAIN, 10);


Any ideas anyone?

0 new messages