Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Avinash Hegde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Reacher wrote:


Please check your private messages for an important administrative matter
 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
unicodeY.png
[Thumbnail for unicodeY.png]
Custom Renderer
 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't you be calling drawString() before scaling?
 
Avinash Hegde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Avinash Hegde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try that out. Thanks..
 
Avinash Hegde
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to work. thanks everyone..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic