Select Language
Reply
Specialist I
rafo
Posts: 507
Registered: 07-15-2008
0

Rotate Font

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.

 

----------------
if your issue solved set "Solution" mark at the relevant post
Super Contributor
rab
Posts: 278
Registered: 07-22-2008
0

Re: Rotate Font

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.

 

 

Specialist I
rafo
Posts: 507
Registered: 07-15-2008
0

Re: Rotate Font

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.

----------------
if your issue solved set "Solution" mark at the relevant post
Super Contributor
rab
Posts: 278
Registered: 07-22-2008
0

Re: Rotate Font

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,0x2,transform);
                       Font sub_font = Font.getDefault().derive(style,size,Ui.UNITS_px,1,0x2,transform1);
                                           
                      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.

 

Administrator
MSohm
Posts: 6,008
Registered: 07-09-2008
0

Re: Rotate Font

This is the expected behaviour.  Font translations, transformations and rotations are not supported in TextFields (and Fields derrived from it).  They are supported when drawing directly to a Graphics object.
Mark Sohm
BlackBerry Development Advisor
www.BlackBerryDeveloper.com

Please refrain from posting new questions in solved threads.

Found a bug? Report it using the Issue Tracker
Specialist I
rafo
Posts: 507
Registered: 07-15-2008
0

Re: Rotate Font

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.

----------------
if your issue solved set "Solution" mark at the relevant post
Administrator
MSohm
Posts: 6,008
Registered: 07-09-2008
0

Re: Rotate Font

Smileys don't show up in code pasted in a code block (2 buttons to the left of the smiley when creating a message).

Mark Sohm
BlackBerry Development Advisor
www.BlackBerryDeveloper.com

Please refrain from posting new questions in solved threads.

Found a bug? Report it using the Issue Tracker
Specialist I
richard_puckett
Posts: 190
Registered: 04-03-2008
My Device: Bold 9700

Re: Rotate Font

Here's a very serious example of how to rotate text.  :smileyhappy:

 

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); } }

 

bb9
Contributor
bb9
Posts: 29
Registered: 07-28-2008
0

Re: Rotate Font

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.

 

Specialist I
rafo
Posts: 507
Registered: 07-15-2008
0

Re: Rotate Font

Thank you, Richard.

 

That works fine :smileyhappy:

----------------
if your issue solved set "Solution" mark at the relevant post