1

I am correcting Font display in one of our old java Swing application. Which changed after using "-Dfile.encoding=UTF-8" option at run time.

FontClass.jar is my sample jar file.

option 1:
javaw -Xmx256M -jar -Dfile.encoding=UTF-8 "FontClass.jar"

option 2:
javaw -Xmx256M -jar "FontClass.jar"

With option 1: I get Monospaced display

With option 2: I get non Monospaced display.

Limitation:

i) -Dfile.encoding=UTF-8 This option we have added to have all our streams to support UTF-8 support. Insteading of modifing each streams manually.

ii) We are using java swing frame work and in many placeses. We are uisng default Font. Changing all this is lot of work.

So now my question is there is any command line option by using which I can get Monospace display with option 1:

Below is the sample code:

class Show extends Frame {
    FontMetrics fontM;
    String outString;

    Show(String target, String title, Font font) {

        setTitle(title);
        outString = target;

        fontM = getFontMetrics(font);
        setFont(font);

        int size = 0;
        for (int i = 0; i < outString.length(); i++) {
            size += fontM.charWidth(outString.charAt(i));
        }
        size += 24;

        setSize(size, fontM.getHeight() + 60);
        setLocation(getSize().width / 2, getSize().height / 2);
        setVisible(true);
    }

    public void paint(Graphics g) {
        Insets insets = getInsets();
        int x = insets.left;
        int y = insets.top;
        g.drawString(outString, x + 6, y + fontM.getAscent() + 14);
    }
}

public class Fontclass {

    public static void main(String[] args) { 



        String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217");  

        String inputString = "\niiii\naaaaiiii";

        String displayString = jaString + " " + inputString;

        Font font;
        font = new Font("Courier",3, 24);

        new Show(displayString, "Demo",font);

    }
}
vrbilgi
  • 5,703
  • 12
  • 44
  • 60
  • 1
    *"Below is the sample code:"* Where is the question? Why does the code use AWT components rather than Swing? Please fix the code formatting, with almost 700 rep. you should have figured it out by now.. – Andrew Thompson May 03 '13 at 06:15
  • It looks exactly the same for me in both cases... – Jon Skeet May 03 '13 at 06:18
  • For me and many other in my team both looks different. With option 1 Font "Courier" is "Courier". But with option 2 Courier" looks like "Courier new" – vrbilgi May 03 '13 at 09:14
  • Would it not be reasonable that some text file, say in ISO-8859-1 is involved, maybe listing fonts? Look at fontconfig and psfont*.properties in jre/lib. Maybe a non-ASCII char got inserted. – Joop Eggen May 03 '13 at 10:08

1 Answers1

1

From the FontMetrics API, which defines advance in terms of charWidth(), "Note that the advance of a String is not necessarily the sum of the advances of its characters." Moreover, a font named "Courier" may be monospaced, but it may not be the same font specified by Font.MONOSPACED.

Instead, use TextLayout to derive the required geometry, as shown here, here and here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045