File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Creating a image file using TextLayout. Need to find right startYpoint Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating a image file using TextLayout. Need to find right startYpoint" Watch "Creating a image file using TextLayout. Need to find right startYpoint" New topic
Author

Creating a image file using TextLayout. Need to find right startYpoint

Avinash Hegde
Greenhorn

Joined: May 25, 2010
Posts: 5
Hi,

I am trying to create a PNG image out of a text that the user entered. The image height is user configurable.
Steps:
1. Get the text attributes as an AttributedString
2. Create a textLayout instance using the AttributedString and a FontRendererContext.
3. Get the bounds from the textLayout.
4. Create a Buffered Image of height = userdefined and width= textLayout.bounds.width
5. Get the graphics2D object from the buffered image.
6. Add scaling depending on the height.
7. Draw the textLayout on the image and write it into a file.

The problem I am facing is finding the Y position to start drawing from using the textlayout.draw method [Step 7].
I have not been able to get down to a generalized formula that would work with all fonts.
What I see is sometimes the text is clipped [ either @ the top or @ the bottom].
Any help on this regard will be greatly appreciated.
I am adding the code which creates the png.


Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Jack Reacher wrote:


Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Check out the FontMetrics class. It should help you figure out the width/height of a String when rendered using a particular Font
Avinash Hegde
Greenhorn

Joined: May 25, 2010
Posts: 5
Thanks for the tip.
But FontMetrics did not help out much. The whole problem seems to be the scaling. Once scaled I seem to loose control as to where it should start rendering the text.
I modified the image drawing to use fontMetrics


Attached is the png file the above created with Arial Bold Font



[Thumbnail for unicodeY.png]

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Shouldn't you be calling drawString() before scaling?
Avinash Hegde
Greenhorn

Joined: May 25, 2010
Posts: 5
Well no.
According to java docs,

Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling.

So I think the scale has to be before the drawString else the text would not be scaled at all.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Avinash Hegde wrote:Well no.
According to java docs,

Subsequent rendering is resized according to the specified scaling factors relative to the previous scaling.

So I think the scale has to be before the drawString else the text would not be scaled at all.

And I think you should be multiplying y by the heightRatio.


luck, db
There are no new questions, but there may be new answers.
Avinash Hegde
Greenhorn

Joined: May 25, 2010
Posts: 5
Let me try that out. Thanks..
Avinash Hegde
Greenhorn

Joined: May 25, 2010
Posts: 5
Seems to work. thanks everyone..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Creating a image file using TextLayout. Need to find right startYpoint