Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / Java 2D / Re: Q for Trembovetski: Font kerning information on: 2004-01-28 04:55:57
Trust Trembovetski and me...  New API never goes into a dot release (well, it might depend on how much influence you wield with Scott...)

Um, I have to admit that the Sun JVM's don't do kerning at all... I expect Apple's does, though I haven't examined their implementation closely.

What are you trying to do with text that the existing text APIs don't do or are too slow at?  Text on a path?  Tightening or loosening the text?  Justification?

Text APIs have to be general enough to support Unicode, then optimized underneath for ASCII or Latin-1.  So for example the API would have to support surrogates.  There's really no API for getting the pure font table information-- not itself so straightforward if you have to support both Type1 and TrueType/OpenType.  You can't even get the font file (if there is one) there are some security issues that would have to be dealt with.  The approach so far has been to just provide higher-level API, and let the real intense folks just pull apart the font data themselves if they have access to it, and have at it.  
2  Game Development / Newbie & Debugging Questions / Re: Not-so-simple font-resizing questino... on: 2004-01-28 04: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.
3  Game Development / Newbie & Debugging Questions / Re: Not-so-simple font-resizing questino... on: 2004-01-28 04: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.)
4  Game Development / Newbie & Debugging Questions / Re: Not-so-simple font-resizing questino... on: 2004-01-28 04: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.
5  Game Development / Newbie & Debugging Questions / Re: Not-so-simple font-resizing questino... on: 2004-01-22 04: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
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

Elsealabs (10 views)
2014-12-28 10:39:27

CopyableCougar4 (17 views)
2014-12-28 02:10:29

BurntPizza (21 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (57 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (94 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!