Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Not-so-simple font-resizing questino...  (Read 3716 times)
0 Members and 1 Guest are viewing this topic.
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Posted 2004-01-19 00:09:11 »

As many of you may well be aware (since I've mentioned it a lot), I'm trying to build a JPanel that resizes its text whenever the panel itself is resized, so that the text always takes up the maximum space it can.  I've struggled with this a lot, and I think I'm the closest I've come to date to having a solution.  

But I'm having one big problem.  First, let me give you my code.  The comments should explain most of it, but there's two variables that are unexplained.  "allTexts" is an array of Strings that holds all the lines of text that will be used for the panel (my approach only tests for line width, not height), and "allFonts" is an array of Fonts that holds all the fonts used by all the texts.

Here's my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
// Setting the fontSize.  First we find the longest line.  We create an integer to specify the longest line:
                 
int longestLine = 0 ;
                  
// We'll need a FontRenderContext to get widths with:
                 
FontRenderContext fontRC = g2D.getFontRenderContext () ;
                  
// Now we count through the lines:
                 
for ( int i = 1 ; i < allTexts.length  ; i ++ ) {
                        
      // Is the current line longer than the longest line?
                       
      if ( allFonts [ i ].getStringBounds ( allTexts [ i ] , fontRC )
            .getWidth ()
            > allFonts [ longestLine ]
            .getStringBounds ( allTexts [ longestLine ] , fontRC )
            .getWidth () )
                                    
            // If so, set a new longestLine:
                             
            longestLine = i ;       
                        
}
                  
                  
// Setting maxFont and maxWidth, using longestLine:
                 
Font maxFont = allFonts [ longestLine ] ;
                  
String maxWidth = allTexts [ longestLine ] ;
                  
// the ratio of the longest line's point size to its width, so that we can
// make a ballpark guess as to the right font size:
                 
float widthRatio = maxFont.getSize2D ()
      / ( float ) maxFont.getStringBounds ( maxWidth, fontRC ).getWidth () ;                   
                        
// Now using that ratio to make a ballpark guess as to the font size:
                 
fontSize = getWidth () * widthRatio ;
                  
// Setting maxFont to that size:
                 
maxFont = maxFont.deriveFont ( fontSize ) ;
                  
// Is the maxWidth string smaller than the width of the panel by more than 3
// pixels?

while ( getWidth () - maxFont.getStringBounds ( maxWidth, fontRC ).getWidth ()
> 3 ) {
                                    
            // If so, increase the size of the font by half a point:
                                                     
            fontSize = fontSize + 0.5f ;
                              
            maxFont = maxFont.deriveFont ( fontSize ) ;
                              
            // Is the maxWidth String now longer than the width of the panel?
                 
            if ( getWidth () < maxFont.getStringBounds ( maxWidth, fontRC ).getWidth () )
                                          
                  // If so, break out of the loop:
                                         
                  break ;
                                                
}            
                  
// Is the maxWidth String now longer than the width of the panel?
                 
while ( getWidth ()
      < maxFont.getStringBounds ( maxWidth, fontRC ).getWidth () ) {
                              
            // If so, decrease the size of the font by half a point:
                             
            fontSize = fontSize - 0.5f ;
                              
            maxFont = maxFont.deriveFont ( fontSize ) ;
                              
}

// Now we should have maxFont at the largest size possible, within half a point.


So, this all seems to work, with one problem: the lines come out too short.  I realize my code allows the lines to be up to 3 pixels shorter than the width of the panel, but the lines come out way shorter than that.  The longest line of text seems to be 8-12 pixels short of the side of the panel.   I tried setting my code to be more sensitive, by changing the "0.5f"s to "0.05f"s, but it didn't seem to change the size the lines were coming out.  Can anyone see what's going on ?
Offline Orden

Senior Newbie





« Reply #1 - Posted 2004-01-19 02:35:47 »

Without looking at your code you cannot assume each character has the same font width, proportional pitch fonts to fixed pitched fonts. Here a link that explains the difference...
http://www.webopedia.com/TERM/F/fixed_pitch.html

OK had a look at your code....

What you are trying to do is only possible with a fixed pitched font if your getWidth()%maxwidth.length()=0

or with a proportional font if you are lucky...... Smiley

You will always be out by a number of pixels unless you change the spacing on a few words/characters to make up the difference....

The only way to get it accurate without doing the spacing thing...is to draw your text into an image the same size as the text width/height and scale the image to the screen.
Hope that helps

All the Best

Paul
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #2 - Posted 2004-01-19 17:13:50 »

Paul, thanks for replying.  But I beg to differ with you about fixed-width fonts.  The method Font.getStringBounds ( String string, FontRenderingContext frc ) returns a Rectangle2D that bounds the given string for the given graphics context, whether or not it's a fixed-width font.  Using those rectangles, I can find the widest string among any number of strings, regardless of character spacing.  Then I should be able to test the width of the widest line at different point sizes.

My code tests first whether the width of the widest string is under the width of the panel, and if it is, it increases the width of the string until it is over the width of the panel.  Then it takes the string that is wider than the panel and decreases its point size by half a point until the string is smaller than the panel width.  It seems to me that this approach should guarantee that I have the widest possible font size, within half a point (or in practice a full point, since java's fonts don't really pay attention to fractional point sizes).

The problem is that my fonts *sometimes* come out at least a full point to small.  To test this, I added

fontSize = fontSize + 1F ;

to the end of the code that determines font size.  The result of this was that at some panel sizes, all the text still fit in the window, which means that normally the code was making it at least one point too small.  But at the same time, at other window sizes, the +1 font size proved too big, and the text didn't fit in the window.  I don't understand this.  Again, I tried changing the font-resizing increment from 0.5f to 0.05f, but my results didn't seem to be any more precise.

I'll greatly appreciate any help that can come my way.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mark Thornton

Senior Member





« Reply #3 - Posted 2004-01-19 17:35:28 »

The string bounds you are obtaining represent the logical size of the text and not the visible physical size. The width for example indicates where the next character would be placed relative to the start position.
You will find methods on GlyphVector for obtaining a rectangle bounding the true physical (visible) area of the displayed text.
Offline Orden

Senior Newbie





« Reply #4 - Posted 2004-01-19 19:32:24 »

Quote
The problem is that my fonts *sometimes* come out at least a full point to small.  To test this, I added

fontSize = fontSize + 1F ;


The reason the fontsize is coming out a full point smaller, is if you are out by 10 pixels and you increase the point size by 1F you are then increasing the space used by the string line by number of characters*point change which could be greater than your screen size-3 pix. So your code is just finding the best fit font for the screen.

All The Best

Paul
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #5 - Posted 2004-01-19 23:38:12 »

Mark and Paul, thanks for responding.

Mark:

Thanks for the tip. I don't really understand what a 'logical size' is, but as you say it's different from the actual visible size, I'll look into these GlyphVectors you speak of.


Paul:

I'm sorry, I forgot to mention that I've modified my code a bit.  The first 'while' loop no longer test for the line being being within 3 pixels of the panel width, it just tests for the line being smaller than the panel width. So the process goes: if the line is smaller than the panel width, then increase it until it's larger, then take the larger line, and increase it until it's smaller.  This should result in a font size that is the maximum size possible within the panel boundaries.  It doesn't.  At least, not always.  I know for certain, from testing, that it sometimes chooses a font that's a full point smaller than the biggest possible font.  And I can't understand why.  But maybe I'll tinker with GlyphVectors and that will solve everything.
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #6 - Posted 2004-01-20 04:02:06 »

Okay, this is driving me bizzonkers.  I tried fixing everything up with GlyphVectors, and now the fonts are coming out WAY too small!  Like, HALF the size they should be.

Here's the modified code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
// Setting the fontSize.  First we find the longest line.  We create an
// integer to hold the place in the allTexts array of the longestLine:
                 
int longestLine = 0 ;
                 
// We'll need a FontRenderContext to get widths with:
                 
FontRenderContext fontRC = g2D.getFontRenderContext () ;
                       
// And a double to hold the longest width:
                       
double longestWidth = allFonts [ longestLine ]
      .createGlyphVector ( fontRC , allTexts [ longestLine ] )
      .getPixelBounds ( null, 0 , 0 ).getWidth () ;
                 
// Now we count through the lines:
                 
for ( int i = 1 ; i < allTexts.length  ; i ++ ) {
                       
      // Is the current line longer than the longest line?
                       
      if ( allFonts [ i ].createGlyphVector ( fontRC , allTexts [ i ] )
            .getPixelBounds ( null, 0 , 0 ).getWidth ()
            > longestWidth ) {
                                   
            // If so, set a new longestLine:
                             
            longestLine = i ;
                                   
            // And set longestWidth:
                                   
            longestWidth =       allFonts [ longestLine ]
            .createGlyphVector ( fontRC , allTexts [ longestLine ] )
                  .getPixelBounds ( null, 0 , 0 ).getWidth () ;
     
                       
      }
                       
}
                 
//**TEMP:
                       
System.out.println ( allTexts [ longestLine ] ) ;
                 
// Setting maxFont and maxWidth, using longestLine:
                 
Font maxFont = allFonts [ longestLine ] ;
                 
String maxWidth = allTexts [ longestLine ] ;
                 
// the ratio of the longest line's point size to its width, so that we
// can make a ballpark guess as to the right font size:
                 
float widthRatio = maxFont.getSize2D () / ( float ) longestWidth ;                  
                       
// Now using that ratio to make a ballpark guess as to the font size:
                 
fontSize = getWidth () * widthRatio ;
                 
// Setting maxFont to that size:
                 
maxFont = maxFont.deriveFont ( fontSize ) ;
                       
//***TEMP: the width of the panel:

double thisWidth = getWidth () ;

                 
// Is the maxWidth string smaller than the width of the panel?
                 
while ( getWidth () > longestWidth ) {
                                                                       
      // If so, increase the size of the font by half a point:
                                                     
      fontSize = fontSize + 0.5f ;
                             
      maxFont = maxFont.deriveFont ( fontSize ) ;
                                                                       
      // And set longestWidth:
                                   
      longestWidth = maxFont
            .createGlyphVector ( fontRC , maxWidth )
            .getPixelBounds ( null , 0 , 0 ).getWidth () ;
                                                                                                                 
}            
                 
// Is the maxWidth String now longer than the width of the panel?
                 
while ( getWidth () < longestWidth ) {
                                   
      // If so, decrease the size of the font by half a point:
                             
      fontSize = fontSize - 0.5f ;
                             
      maxFont = maxFont.deriveFont ( fontSize ) ;
                                                                                                           
      // And set longestWidth:
                                   
      longestWidth = maxFont
            .createGlyphVector ( fontRC , maxWidth )
            .getPixelBounds ( null , 0 , 0 ).getWidth () ;                                                                                                            
                             
}
                 
// Now we should have the the largest possible font size, within half a
// point.
                       
// ***TEMP:
                       
System.out.println ( "Panel Width: " + getWidth () ) ;
                       
System.out.println ( "MaxWidth: " + longestWidth ) ;


All the stuff commented with "//***TEMP:" is in there for debugging purposes.  Using that stuff, I can tell that, for example (these numbers are not exact), when getWidth returns 120, and longestWidth evaluates to 118, the on-screen font is only a little over half the size of the panel!  That is, the GlyphVector.getPixelBounds.getWidth method is correctly evaluating to very near the panel size, when the font it represents is way too small!

Hellllllllp!
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #7 - Posted 2004-01-22 04:35:01 »

Well, I don't know what to make of all this.  I tinkered with my code so that my program actually displayed the bounding boxes produced by GlyphVector.getPixelBounds, and all I can say is, I don't know what the hell those boundaries represent, but they certainly don't represent what they're supposed to.  

They're supposed to represent the bounding box around all the pixels actually affected by the given GlyphVector, and I know for a fact they don't, because I had my program draw the GlyphVector and then draw the pixelBounds, and the freakin' pixelBounds came out almost twice as wide as the GlyphVector text.  Then I tried checking out the GlyphVector.getVisualBounds method, and it was just as wrong as getPixelBounds.

Finally, I tried using the GlyphVector.getLogicalBounds method, even though Mark said the logical bounds would be inaccurate, and darn if they don't look pretty accurate to me.  I have no idea why Font.getStringBounds.getWidth should return a different value than Font.createGlyphVector.getLogicalBounds.getWidth, but my code wasn't working with getStringBounds, and it IS working with getLogicalBounds.  Go figure.  This Java, she crazy, no?
Offline dougfelt

Junior Newbie




Java Text = Unicode + Layout


« Reply #8 - Posted 2004-01-22 05:57:04 »

This tweaked, standalone version of your code works for me in Sun's J2SDK 1.4.2.  I suspect my default FontRenderTransform is different from the one you're getting from your graphics.  Try making sure that fractionalMetrics is true.

BTW, in general you should avoid using Font.createGlyphVector unless you know all your text is ASCII.  Also, createGlyphVector has some overhead (you are creating several objects, after all) and you might as well avoid it when you can.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

public class TestScale {
    public static void main(String[] args) {
        int pw = 200;
        for (int i = 0; i < args.length; ++i) {
            if (args[i].startsWith("-w")) {
                pw = Integer.parseInt(args[++i]);
            }
        }
        System.out.println("Starting panel width: " + pw);

        String[] allTexts = {
            "A short line.",
            "A longer line.",
            "Too short.",
            "Like, way longer than the others.",
            "Long, but not so long."
        };
        Font f = new Font("Dialog", Font.PLAIN, 12);
        Font[] allFonts = {
            f, f, f, f, f
        };
           
        // Setting the fontSize.  First we find the longest line.  We create an
       // integer to hold the place in the allTexts array of the longestLine:
       int longestLine = 0 ;
   
        // We'll need a FontRenderContext to get widths with:
       FontRenderContext fontRC = new FontRenderContext(null, true, true); // fractional metrics on
   
        // And a double to hold the longest width:
       double longestWidth = allFonts [ longestLine ]
            .createGlyphVector ( fontRC , allTexts [ longestLine ] )
            .getPixelBounds ( null, 0 , 0 ).getWidth () ;
   
        // Now we count through the lines:
       for ( int i = 1 ; i < allTexts.length  ; i ++ ) {
            // Is the current line longer than the longest line?
           double temp;
            if ( (temp = allFonts [ i ].createGlyphVector ( fontRC , allTexts [ i ] )
                  .getPixelBounds ( null, 0 , 0 ).getWidth ())
                 > longestWidth ) {
       
                // If so, set a new longestLine:
               longestLine = i ;  
       
                // And set longestWidth:
               longestWidth =  temp;
            }
        }
   
        //**TEMP:
       System.out.println ( allTexts [ longestLine ] ) ;

        for (int panelWidth = pw, i = 0; i < 10; panelWidth += 5, ++i) { // should adjust panelWidth here!!!
           // Setting maxFont and maxWidth, using longestLine:
           Font maxFont = allFonts [ longestLine ] ;
            String maxWidth = allTexts [ longestLine ] ;
   
            // the ratio of the longest line's point size to its width, so that we
           // can make a ballpark guess as to the right font size:
           float widthRatio = maxFont.getSize2D () / ( float ) longestWidth ;    
     
            // Now using that ratio to make a ballpark guess as to the font size:
           float fontSize = panelWidth * widthRatio ;
   
            // Setting maxFont to that size:
           maxFont = maxFont.deriveFont ( fontSize ) ;
     
            // iterate until we converge to within half a pixel
           int iterations = 0;
            while ( Math.abs(panelWidth - longestWidth) > .5 ) {
             
                // If so, scale the font size by the relative widths
               fontSize = (float)(fontSize * panelWidth / longestWidth) ;
     
                maxFont = maxFont.deriveFont ( fontSize ) ;
             
                // And set longestWidth:
               longestWidth = maxFont
                    .createGlyphVector ( fontRC , maxWidth )
                    .getPixelBounds ( null , 0 , 0 ).getWidth () ;

                ++iterations;
            }  
   
            System.out.println ( "Panel Width: " + panelWidth +
                                 " MaxWidth: " + longestWidth +
                                 " iterations: " + iterations +
                                 " font size: + maxFont.getSize2D());
        }
    }
}


Here's my results on a win2k system:

Starting panel width: 200
Like, way longer than the others.
Panel Width: 200 MaxWidth: 200.0 iterations: 2 font size: 13.927027
Panel Width: 205 MaxWidth: 205.0 iterations: 2 font size: 14.279213
Panel Width: 210 MaxWidth: 210.0 iterations: 3 font size: 14.621629
Panel Width: 215 MaxWidth: 215.0 iterations: 3 font size: 14.954442
Panel Width: 220 MaxWidth: 220.0 iterations: 3 font size: 15.287282
Panel Width: 225 MaxWidth: 225.0 iterations: 2 font size: 15.689565
Panel Width: 230 MaxWidth: 230.0 iterations: 2 font size: 16.022388
Panel Width: 235 MaxWidth: 235.0 iterations: 3 font size: 16.37691
Panel Width: 240 MaxWidth: 240.0 iterations: 3 font size: 16.71169
Panel Width: 245 MaxWidth: 245.0 iterations: 2 font size: 17.116066
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #9 - Posted 2004-01-22 22:21:12 »

Doug, thanks so much for your detailed, thoughtful reply.  I'll have to take that code home and play with it a little.  But at first pass I have a couple questions/comments.

Re: your BTW: when won't my characters be ASCII?  I'm sure they'll be ASCII for all the purposes I'm using the class for.  Is there something else you'd recommend doing that would be applicable to both ASCII and non-ASCII characters?

In the part of the code where the longest line is found, I notice that you made it more efficient by removing at least a couple of extra calls to createGlyphVector.  Thanks for the instruction; your approach is superior to mine.  Just one thing: shouldn't it start with i=0 now, instead of i=1?

And again, the part where you specify "iterate until we converge within half a pixel" is a much better way to do things than the way I was doing them.  I'm really happy to get such excellent feedback.

But, and I perhaps shouldn't mention this until I've tried out your code, but still... all your streamlining doesn't get at the main problem I was having: getPixelBounds returns a rectangle that, to my eye, has nearly nothing to do with the actual size of the displayed text.  I was actually not having a problem scaling using getPixelBounds, I was scaling entirely accurately, it's just that I was scaling to the wrong thing: a really inaccurate rectangle.  This discrepancy won't show up until you actually try to display the text, which is why your test code looks like it's working perfectly.  My debugging outputs always reported that my clumsy scaling was working perfectly, too, when I could plainly see that it wasn't, which is part of what was driving me batty.

getLogicalBounds, however, returns a rectangle that appears to be exactly the same size as the displayed text.  Using getLogicalBounds, my code works just fine.  Not as efficiently as yours, but it gets the job done.

One last thing:  I'm confused about fractional fonts.  When I use a float to specify a Font (using Font.deriveFont ( float )), the font stores that fraction inside itself put sets it point size by rounding that fraction off to the nearest integer.  Is there a way to get my fonts to display at fractions of their point sizes?  If so, I'd love to know about it.

Again, thanks a lot for your response.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #10 - Posted 2004-01-23 15:09:12 »

Doug:

I took your code home and ran it and it didn't quite work.  It fell into an infinite loop at the first panel size, because of the font-size problem I mentioned before: it doesn't matter if you set the font size with a float, its actual size gets set by rounding that float to an integer.  So, on panel width 200, the longestWidth keeps flipping back and forth between 198.0 and 231.0; each time the longestWidth is lower the code increases the fontSize, and each time it's higher it decreases the fontSize, and it never resolves.  Note that I didn't change your code at all, I cut and pasted it right into Eclipse.

Also, I should note that the font sizes the code hovers around are 10.whatever and 11.whatever, not 13.whatever as shown in your output.  This suggests to me that the weird thing about PixelBounds returning a wildly inaccurate rectangle is a Mac-platform bug.  Perhaps not being able to use fractional font sizes is also Mac-specific, though that would be odd.
Offline trembovetski

Senior Member




If only I knew what I'm talking about!


« Reply #11 - Posted 2004-01-24 05:30:41 »

jessewclark, what JDK version were you running it on?
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #12 - Posted 2004-01-24 21:14:34 »

The latest Mac version, I think it's 1.4.something.  In OS X 1.2.6.
Offline dougfelt

Junior Newbie




Java Text = Unicode + Layout


« Reply #13 - Posted 2004-01-28 05:07:38 »

Oh, missed that you're using the Mac.  Apple's java text implementation is entirely different from Sun's-- it's based on their own native text routines, while Sun's is in Java with some help from code in Sun's JRE for opentype support.  The specs for text rendering are not tight, so I can't completely fault Apple, but certainly the expectation is that if you create a font with a fractional point size, that's what you get.
Offline dougfelt

Junior Newbie




Java Text = Unicode + Layout


« Reply #14 - Posted 2004-01-28 05:24:16 »

I owe you a reply to your other questions as well... I don't quite seem to get the message, do I :-)

The problem with getPixelBounds sounds like an Apple JVM bug, it works on Sun's JVM on Win2K and Solaris, and (I should hope!) on IBM's JVMs as well. Apple's got a beta release of a new 1.4.something release, you might try that and see if it fixes this.

The logicalBounds is a bounds constructed from the ascent, descent+leading, origin, and position of the last base glyph + advance.  With unstyled ASCII text it's probably fine as a substitute for pixel bounds, if that's truly what you want.  Italic text, or text with accents above or below the line, might extend past these bounds.  That may not matter to you.  The visual bounds will get this information, but (owing to pixel alignment) still might not match the pixel bounds.

If you were rendering, say, Arabic or Indic, constructing a GlyphVector using Font.createGlyphVector will not get you the proper layout, because proper rendering of text in these languages reorders the characters and does fancy glyph substitution.  TextLayout handles all this, so creating a TextLayout would be better if you really cared about these cases.  I don't see many games with Arabic or Indic text in them, so I expect you'll get along fine with GlyphVector  Wink.

(Yeah, my code should have had a limit on the max iterations to be more robust.  I was kinda sloppy there.)
Offline dougfelt

Junior Newbie




Java Text = Unicode + Layout


« Reply #15 - Posted 2004-01-28 05:33:21 »

Obviously I have nothing better to do tonight...

The fractional point size is converted to an int internally because of the legacy Font APIs that used ints.  When using the old API you get an int, when you use the new API you (ought to!) get a float.  And yes, the float should affect the size of the text that the font renders.  Essentially all the point sizes are like a scale of an 'ideal' 1 point font.  Now, some fonts will use bitmaps and not scale smoothly, and others will do custom hinting at different point sizes (removing small details that can't be rendered at small sizes, for instance) so it's not always a simple scale, but most of the time it is-- well, on my JVM at least.

Java by default should render at 1 pixel = 1 point = 1/72 of an inch.  The idea is if you want a true scale to real world dimensions (if your monitor is say 90dpi) then you scale the graphics first.  There's some api somewhere that gets you that transform.  The Mac might be applying that transform, or its fonts may be designed to a different default pont size (if you're using Mac fonts).  the difference in point size cbetween what you see and what I see could have many causes.
Offline jessewclark

Senior Newbie




Heaven means muzak.


« Reply #16 - Posted 2004-01-29 21:56:10 »

Doug, thanks for your responses.  I've given up on trying to get fractional fonts, and I'm using, um, Font.getBounds ( string , fontRenderContext ) , I think that's it, and it's working all right.  I really wish I could get exact point sizes, but I guess the mac just doesn't do that.  Maybe I'll start a new thread with that question.  Thanks for your help.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!