07-24-2008 02:40 AM
Hi, guys.
Does anyone know is there a way to rotate font?
Font class has a method derive
public Font derive(int style, int height, int units, int antialiasMode, int effects, int[] transform)
I suspect that the transform parameter should be responsible for rotate action.
But in javadoc there is no information regarding transform array values to rotate font.
Where can I find this information?
Thank you.
Solved! Go to Solution.
07-24-2008 10:35 AM
In order to rotate by an angle b the transform matrix should be as follows:
cos b sin b 0
- sin b cos b 0
0 0 1
So the transform values will be: {cos b, sin b, -sin b, cos b, 0, 0}
Please let us know if this worked with you. I am trying to use the same API to translate/scale fonts, but it is not working with me.
07-25-2008 01:39 AM
I tried way you suggested. No results.
I also tried to play with FontFamily class, tried to get different fonts and rotate (scale) them. No results.
In the javadoc of Font class there is a short piece of information regarding transformation matrix.
But none of ways described in javadoc are working.
I tried to obtain only scalable fonts to work with.I processed all FontFamily instances available, and invoked
isStyleSupported(FontFamily.SCALABLE_FONT) method of FontFamily class instance.
But this method always returns true.
Maybe it is not a suitable method to check the font type (is it scalable or not) - but I did not any mechanism to check the font family except the method isStyleSupported.
What can I say on that. To scale (your question) a font you may try to derive font with bigger size via derive() methodof Font class. If you wish to freely scale font - I currently have no information on that.
And it is not clear how can I rotate a font when javadoc examples do not work in real life.
07-25-2008 12:31 PM
I have been testing the scaling and translation and I got it working with the default font. I did not check for scalable or not. The catch is that I got it working when I draw my own text. If I use a RichTextField, it does not work.
The way I did it is to create my own field and overwrite the paint method as follows:
protected void paint(Graphics graphics) {
try{
String text = "Test of gp";
graphics.setColor(Color.RED);
Font font = Font.getDefault().derive(Font.BOLD,20,Ui.UNITS_px)
graphics.setFont(font);
int x = 10;
int y = 0;
graphics.drawText(text,x,y);
x += font.getAdvance(text);
int interSpace = graphics.getFont().getAdvance("a");
int one = Fixed32.toFP(1);
int two = Fixed32.toFP(2);
int ny = Fixed32.toFP(font.getHeight()/2);
int m = two/3 ;
int[] transform = new int[] { m,0,0,m,0,-one };
int[] transform1 = new int[] { m,0,0,m,0,ny };
int style = graphics.getFont().getStyle();
int size = graphics.getFont().getHeight();
Font sup_font = Font.getDefault().derive(style,size,Ui.UNITS_px,1,
Font sub_font = Font.getDefault().derive(style,size,Ui.UNITS_px,1,
graphics.setFont(sup_font);
graphics.setColor(Color.GREEN);
graphics.drawText("superscript",x,y);
x +=interSpace;
x += sup_font.getAdvance("superscript");
graphics.setFont(font);
graphics.setColor(Color.RED);
graphics.drawText("and",x,y);
x += font.getAdvance("and");
graphics.setFont(sub_font);
graphics.setColor(Color.GREEN);
graphics.drawText("subscript",x,y);
}catch(Exception e)
{
System.out.println("Exception in paint:"+e.getMessage());
}
}
This worked fine. Try to modify it to add the rotation transform.
Meanwhile I am exploring with the RichTextField to see why it is not working.
07-25-2008 01:15 PM
07-28-2008 10:23 AM
Thank you, I'll try to find a solution.
Seems that the forum scripts do not recognize correctly java source code.
Sometimes there are smiley faces.
07-28-2008 11:10 AM
Smileys don't show up in code pasted in a code block (2 buttons to the left of the smiley when creating a message).
07-28-2008 01:31 PM
Here's a very serious example of how to rotate text. ![]()
class StringSpinner extends MainScreen { Font myFont; int xOffset; int yOffset; int theta; public StringSpinner() { xOffset = Display.getWidth() / 2; yOffset = Display.getHeight() / 2; new Thread() { public void run() { while(true) { theta += 5; theta %= 360; invalidate(); try { Thread.sleep(25); } catch (InterruptedException ignore) { } } } }.start(); } public void paint(Graphics graphics) { int thetaFixed = Fixed32.toFP(theta); int cell_11 = Fixed32.cosd(thetaFixed); int cell_12 = -Fixed32.sind(thetaFixed); int cell_21 = Fixed32.sind(thetaFixed); int cell_22 = Fixed32.cosd(thetaFixed); int[] transform = new int[] { cell_11, cell_12, cell_21, cell_22, 0, 0 }; myFont = Font.getDefault().derive(Font.PLAIN, 14, Ui.UNITS_px, Font.ANTIALIAS_STANDARD, 0, transform); graphics.setFont(myFont); graphics.drawText("Weeee!!!", xOffset, yOffset); } }
07-28-2008 07:08 PM
There are also a couple of gotcha's when rotating a font with a transform. The font metrics no longer usable, so you need to do any calculations in a normal font. The fonts are still very ugly even when rotated!
It is very sad that BlackBerry doesn't have a mechanism for installing new fonts so we can try to make BlackBerry look as pretty as Symbian OS.
07-29-2008 01:35 AM
Thank you, Richard.
That works fine ![]()