WYSIWYG problems: sun.java2d.SunGraphics2D verses sun.awt.windows.WPathGraphics

K

Karl Mueller

I am running into some differences in string metrics for certain TrueType
fonts (i.e., currently it appears on my XP box) with a
sun.java2d.SunGraphics2D object verses a sun.awt.windows.WPathGraphics
(i.e., my actual printer) object. All I'm trying to do is to right justify
some text, and when I compute the x coordinate based on the string width for
a string, it always comes out perfect on the sun.java2d.SunGraphics2D object
for all fonts and sometimes (i.e., for standard fonts like Arial, Times
Roman, etc.) it also comes out perfect on a sun.awt.windows.WPathGraphics
object (i.e., that is my printer (the piece of paper I ultimately hold in my
hand) which happens to be a LaserJet 6P for testing).

I use the following lines to compute the string width and that computation
is used to right justify the text:

FontRenderContext fontRenderContext = ((Graphics2D)
pGraphics).getFontRenderContext ();

Rectangle2D rectangle = pFont.getStringBounds(pText, fontRenderContext);

float stringWidth = ((float) (rectangle.getWidth () - rectangle.getX ()));

Afterwards I call the Graphics drawString method like this:

((Graphics2D) pGraphics).drawString(someString, x, y); where x and y are
float values.

I even tried experimenting with setting fractional font rendering hints like
this:

fFractionalMetricsRenderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING,

RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);

fFractionalMetricsRenderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS,

RenderingHints.VALUE_FRACTIONALMETRICS_ON);

and setting it like this:

graphics2D.setRenderingHints(fFractionalMetricsRenderingHints);

But so far nothing has worked with "non-standard" fonts as I call them. With
standard-type fonts, such as Arial, Times Roman, etc., everything works
fine, but other "fancier" TrueType fonts do not! Again with these "problem"
fonts, when I try to right justify the text, it looks perfect on the screen
(i.e., sun.java2d.SunGraphics2D) but the right end of the strings do not
line up at all for the printer (i.e., sun.awt.windows.WPathGraphics).

Does anyone have any ideas on how to solve this seemingly simple problem?

Thanx.

Karl
 
K

Karl Mueller

Being able to display something in a particular way onscreen and then to
also be printed in that very same way is an extremely difficult thing to do
but I think I discovered a way to solve this problem with text at least...
And thus, I decided to respond to my question in the hope that I can help
someone else having the very same problem!

Here is one solution to the problem so that no matter what font you are
using the text will be printed to a printer in the very same way it is
displayed onscreen:

GlyphVector glyphVector = graphicsContext.getFont ().
createGlyphVector(((Graphics2D) graphicsContext).
getFontRenderContext(), someStringObject);

// compute the string width in this way:
float stringWidth = (float) glyphVector.getLogicalBounds().getWidth ();

// then draw the string in this way:
((Graphics2D) pGraphics).drawGlyphVector(glyphVector, x, y);

Based on testing with many different types of fonts on my system, this works
in every case!

Cheerio!

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top