FW: Font Graphics? solved

2011-08-25 Thread Eric Douglas
I figured out what I was missing.
You can use GraphicsConfiguration.createCompatibleImage(w,h) to create a
dummy image.
You can use that image's createGraphics() to create a dummy graphics.
You can use that graphics' getFontRenderContext() to get a dummy context
to pass in to java.awt.Font.getStringBounds() to measure a font.
The FontRenderContext by default only measures in integers.
You can set hints on the graphics for antialias and fractional metrics
before getting the context.
My confusion was in thinking the graphics object was linked to the image
object.
If you do the createGraphics, then set the hints, then call
createGraphics (or getGraphics) again it gets a new graphics.  It
doesn't keep the hints.


-Original Message-
From: Eric Douglas 
Sent: Wednesday, August 24, 2011 11:24 AM
To: 'fop-dev@xmlgraphics.apache.org'
Subject: Font Graphics?

I'm sure this is getting a bit off topic but I was wondering if someone
here could help me understand this.
I'm guessing someone here has a better understanding of font graphics
since I started trying to understand it by digging through the FOP
classes used for loading custom fonts and displaying them in the AWT
viewer.

I'm trying to use this code:
  fontGraphics.setFont(baseFont);
  frc = fontGraphics.getFontRenderContext();
  Rectangle2D r2d = baseFont.getStringBounds(A, frc);
  baseCharWidth = r2d.getWidth();

I need this value in 2 places.
The second place works fine.  baseCharWidth gets a value of 0.60253906
which is what I want.
The first place returns a value of 1.0.
The baseFont object is the same java.awt.Font object in both places.
The fontGraphics in both places is created from a
bufferedImage.getGraphics().
So the only difference is the BufferedImage.
The second one creates the image from a
javax.swing.JPanel(java.awt.Component).createImage(w,h).
The first one doesn't have any GUI yet so I created a dummy image from a
GraphicsConfiguration.createCompatibleImage(w,h).

What am I missing?


Font Graphics?

2011-08-24 Thread Eric Douglas
I'm sure this is getting a bit off topic but I was wondering if someone
here could help me understand this.
I'm guessing someone here has a better understanding of font graphics
since I started trying to understand it by digging through the FOP
classes used for loading custom fonts and displaying them in the AWT
viewer.

I'm trying to use this code:
  fontGraphics.setFont(baseFont);
  frc = fontGraphics.getFontRenderContext();
  Rectangle2D r2d = baseFont.getStringBounds(A, frc);
  baseCharWidth = r2d.getWidth();

I need this value in 2 places.
The second place works fine.  baseCharWidth gets a value of 0.60253906
which is what I want.
The first place returns a value of 1.0.
The baseFont object is the same java.awt.Font object in both places.
The fontGraphics in both places is created from a
bufferedImage.getGraphics().
So the only difference is the BufferedImage.
The second one creates the image from a
javax.swing.JPanel(java.awt.Component).createImage(w,h).
The first one doesn't have any GUI yet so I created a dummy image from a
GraphicsConfiguration.createCompatibleImage(w,h).

What am I missing?