You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by calathus <ca...@gmail.com> on 2011/01/10 08:32:30 UTC

[pivot]how to display Japanese Fonts using only Java class?

Hi,
I tried to use Japanese font in my Pivot class, but these fonts are
displayed as empty boxes.
How do we support these Japanese fonts localization?

There are actually a few issues to set up Japanese fonts in Linux.
Since I'm using SuSE 11.3, and  Sun's jdk(not the default JVM od SuSE(open
souce JDK)), I needed to install Japanese fonts(sazanami-gothic.ttf,
sazanami-mincho.ttf, and execute the SuSEconfig -module fonts to create new
properties file: fontconfig.SuSE.properties under /usr/lib64/jvm/jre/lib,
and copied it to Sun's JDK directory(/usr/java/jdk1.6.0_22/jre/lib as well
as creating links to these ttf fonts file under jre/lib/fonts).
This setting allowed to use Japanese fonts in Applet in chrome, and swing
from eclipse, also in the window frame of Pivot application. But in the main
window content, the proper Japanese fonts are not used and they are
displayed as empty boxes.

In the Pivot site, there is an explanation to use French character in Pivot
using BXML file, but how can we use Japanese character in a label (for
instance) ?

Following codes are the program I used to test this issue. There I just used
a Japanese text in the string.(konnichiwa means hello)
The similar codes in Swing can display the Japanese character properly
without extra setting.

Is it possible to use Japanese fonts in Pivot ??

--------

public class _Hello implements Application {
    private Window window = null;

    @Override
    public void startup(Display display, Map<String, String> properties)
throws Exception {
        final Object obj = create();
        if (obj instanceof Window) {;
            window = (Window)obj;
        } else if (obj instanceof Component) {
            window = new Window();
            window.setContent((Component)obj);
            window.setTitle("hello.bxml");
        } else {
            System.out.println("getComponent returned object with type:
"+obj.getClass());
        }
        window.open(display);
    }

    @Override
    public boolean shutdown(boolean optional) {
        if (window != null) {
            window.close();
        }

        return false;
    }

    @Override
    public void suspend() {
    }

    @Override
    public void resume() {
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(_Hello.class, args);
    }

    static Window create() throws Exception {
        return new Window() {{
            final Map<String, Object> namespace = new HashMap<String,
Object>();
            setTitle("こんにちは BXML!"); // this is displayed properly
            setMaximized(true);
            setContent(new Label() {{
//                setText("Hello BXML!");
                setText("こんにちは BXML!"); // the Japanese fonts are not used
properly, they are displayed as empty boxes.
                setStyles("{font:'Arial bold 24', color:'#ff0000',
  horizontalAlignment:'center', verticalAlignment:'center'}");
            }}); // INSTANCE, name: <Label>
//            CodeEmitterRuntime.initialize(this, namespace);
        }};
    }
}

-----------------------

This is a swing sample code which display Japanese fonts properly.


public class SwingLabelDemo extends JPanel {
    public SwingLabelDemo() {
        super(new GridLayout(3,1));  //3 rows, 1 column
        JLabel label1, label2, label3;

        ImageIcon icon = createImageIcon("images/middle.gif",
                                         "a pretty but meaningless splat");

        //Create the first label.
        label1 = new JLabel("Image and Text",
                            icon,
                            JLabel.CENTER);
        //Set the position of its text, relative to its icon:
        label1.setVerticalTextPosition(JLabel.BOTTOM);
        label1.setHorizontalTextPosition(JLabel.CENTER);

        //Create the other labels.
        label2 = new JLabel("日本語つかえる");
        label3 = new JLabel(icon);

        //Create tool tips, for the heck of it.
        label1.setToolTipText("A label containing both image and text");
        label2.setToolTipText("A label containing only text");
        label3.setToolTipText("A label containing only an image");

        //Add the labels.
        add(label1);
        add(label2);
        add(label3);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = SwingLabelDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("SwingLabelDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        SwingLabelDemo newContentPane = new SwingLabelDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
-- 
Cheers,
calathus

Re: [pivot]how to display Japanese Fonts using only Java class?

Posted by calathus <ca...@gmail.com>.
apptaro,
Thanks for hints,

I could fix the problem.

in the following codes:
     Font font = new Font("Sazanami Gothic Regular", Font.PLAIN, 1);
     Theme.getTheme().setFont(font);
     ...

       setText("こんにちは BXML!");
       setStyles("{font:'Arial bold 24', color:'#ff0000',
horizontalAlignment:'center', verticalAlignment:'center'}");

I set Arial for the font in the setStyles, but there is no Arial in Sazanami
fonts.
If we replace Arial by Gothic, it works:
 setStyles("{font:'Gothic bold 24', color:'#ff0000',
horizontalAlignment:'center', verticalAlignment:'center'}"); // this works

On Mon, Jan 10, 2011 at 6:27 PM, Taro App <ap...@gmail.com> wrote:

> Hi calathus,
>
> For me, the code below works. This may be a hint for you.
>
> // set font to workaround japanese display issue
> Theme theme = Theme.getTheme();
> theme.setFont(new Font("MS Mincho", Font.PLAIN, 12));
>
> apptaro
>
>
> On Tue, Jan 11, 2011 at 10:58 AM, calathus <ca...@gmail.com> wrote:
> > Thanks for reply.
> > I set fonts at beginning of the startup method, but still it does not
> show
> > Japanese fonts.
> > What is wrong here? Should we use fontFamily as font name???
> >     @Override
> >     public void startup(Display display, Map<String, String> properties)
> > throws Exception {
> >      GraphicsEnvironment ge =
> > GraphicsEnvironment.getLocalGraphicsEnvironment();
> >      String []fontFamilies = ge.getAvailableFontFamilyNames();
> >      for (String fontFamily: fontFamilies) {
> >      System.out.println("fontFamily: "+fontFamily);
> >      } // ==> this shows "Sazanami Gothic", "Sazanami Mincho"
> >      //Font font = new Font("Sazanami Gothic", Font.TRUETYPE_FONT, 12);
> >      for (final String fontFamily: new String[]{"Sazanami Gothic",
> "Sazanami
> > Mincho"}) {
> >                 // do we need to put all these size???
> >      for(int i=12; i<=30; i++) {
> >      final Font font = new Font(fontFamily, Font.PLAIN, i);
> >      //final Font font = new Font(fontFamily, Font.TRUETYPE_FONT, i);
> >      Theme.getTheme().setFont(font);
> >      }
> >      }
> > ....
> > }
> >
> >
> > On Mon, Jan 10, 2011 at 4:39 AM, Greg Brown <gk...@verizon.net>
> wrote:
> >>
> >> > Is it possible to use Japanese fonts in Pivot ??
> >>
> >> Yes, but the default font (Verdana) does not include Japanese
> characters.
> >> When your app starts up, you just need to set a font that does include
> >> Japanese characters. You set the theme font via
> Theme.getTheme().setFont().
> >>
> >> G
> >>
> >>
> >
> >
> >
> > --
> > Cheers,
> > calathus
> >
> >
> >
> >
>



-- 
Cheers,
calathus

Re: [pivot]how to display Japanese Fonts using only Java class?

Posted by Taro App <ap...@gmail.com>.
Hi calathus,

For me, the code below works. This may be a hint for you.

// set font to workaround japanese display issue
Theme theme = Theme.getTheme();
theme.setFont(new Font("MS Mincho", Font.PLAIN, 12));

apptaro


On Tue, Jan 11, 2011 at 10:58 AM, calathus <ca...@gmail.com> wrote:
> Thanks for reply.
> I set fonts at beginning of the startup method, but still it does not show
> Japanese fonts.
> What is wrong here? Should we use fontFamily as font name???
>     @Override
>     public void startup(Display display, Map<String, String> properties)
> throws Exception {
>      GraphicsEnvironment ge =
> GraphicsEnvironment.getLocalGraphicsEnvironment();
>      String []fontFamilies = ge.getAvailableFontFamilyNames();
>      for (String fontFamily: fontFamilies) {
>      System.out.println("fontFamily: "+fontFamily);
>      } // ==> this shows "Sazanami Gothic", "Sazanami Mincho"
>      //Font font = new Font("Sazanami Gothic", Font.TRUETYPE_FONT, 12);
>      for (final String fontFamily: new String[]{"Sazanami Gothic", "Sazanami
> Mincho"}) {
>                 // do we need to put all these size???
>      for(int i=12; i<=30; i++) {
>      final Font font = new Font(fontFamily, Font.PLAIN, i);
>      //final Font font = new Font(fontFamily, Font.TRUETYPE_FONT, i);
>      Theme.getTheme().setFont(font);
>      }
>      }
> ....
> }
>
>
> On Mon, Jan 10, 2011 at 4:39 AM, Greg Brown <gk...@verizon.net> wrote:
>>
>> > Is it possible to use Japanese fonts in Pivot ??
>>
>> Yes, but the default font (Verdana) does not include Japanese characters.
>> When your app starts up, you just need to set a font that does include
>> Japanese characters. You set the theme font via Theme.getTheme().setFont().
>>
>> G
>>
>>
>
>
>
> --
> Cheers,
> calathus
>
>
>
>

Re: [pivot]how to display Japanese Fonts using only Java class?

Posted by calathus <ca...@gmail.com>.
Thanks for reply.

I set fonts at beginning of the startup method, but still it does not show
Japanese fonts.
What is wrong here? Should we use fontFamily as font name???

    @Override
    public void startup(Display display, Map<String, String> properties)
throws Exception {
     GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
     String []fontFamilies = ge.getAvailableFontFamilyNames();
     for (String fontFamily: fontFamilies) {
     System.out.println("fontFamily: "+fontFamily);
     } // ==> this shows "Sazanami Gothic", "Sazanami Mincho"

     //Font font = new Font("Sazanami Gothic", Font.TRUETYPE_FONT, 12);
     for (final String fontFamily: new String[]{"Sazanami Gothic", "Sazanami
Mincho"}) {
                // do we need to put all these size???
     for(int i=12; i<=30; i++) {
     final Font font = new Font(fontFamily, Font.PLAIN, i);
     //final Font font = new Font(fontFamily, Font.TRUETYPE_FONT, i);
     Theme.getTheme().setFont(font);
     }
     }
....
}



On Mon, Jan 10, 2011 at 4:39 AM, Greg Brown <gk...@verizon.net> wrote:

> > Is it possible to use Japanese fonts in Pivot ??
>
> Yes, but the default font (Verdana) does not include Japanese characters.
> When your app starts up, you just need to set a font that does include
> Japanese characters. You set the theme font via Theme.getTheme().setFont().
>
> G
>
>
>


-- 
Cheers,
calathus

Re: [pivot]how to display Japanese Fonts using only Java class?

Posted by Sandro Martini <sa...@gmail.com>.
Hi, 
for the 2.1 release we have already in plan to have even this improvement: 
https://issues.apache.org/jira/browse/PIVOT-588

so it will be possible to have inside the json configuration file more than
1 default font (and not only one as currently) so the load of fonts could go
in fallback on others in the list.

Bye,
Sandro

-- 
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/pivot-how-to-display-Japanese-Fonts-using-only-Java-class-tp2225423p2227090.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: [pivot]how to display Japanese Fonts using only Java class?

Posted by Greg Brown <gk...@verizon.net>.
> Is it possible to use Japanese fonts in Pivot ??

Yes, but the default font (Verdana) does not include Japanese characters. When your app starts up, you just need to set a font that does include Japanese characters. You set the theme font via Theme.getTheme().setFont().

G