KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Unicode


1 import java.util.*;
2 import java.io.*;
3 import java.awt.Color JavaDoc;
4 import org.faceless.pdf2.*;
5
6 /**
7  * An example showing off the Unicode capabilities of the library. Loads
8  * and displays a number of files in UTF-8 format.
9  *
10  * The program requires a font, which can be passed in on the Command line.
11  * If it's not specified, it defaults to a standard place to find it under
12  * Windows 2K/NT.
13  */

14 public class Unicode
15 {
16     // Which font to use to display the bulk of the text. We've opted for the Times-Roman
17
// that comes with Windows 98 and friends, which has the extended latin, arabic, hebrew
18
// and cyrillic glyphs needed for this demonstration.
19
//
20
private static String JavaDoc NORMALFONT = "C:\\WINNT\\FONTS\\TIMES.TTF";
21
22
23     // The list of languages to use and their names. Note that Georgian (ISO code "ka") and
24
// Thai (ISO code "th") are not included in this list, even though we have the translation,
25
// because we haven't got a font with the required glyphs
26
//
27
private static String JavaDoc[] langs = { "ar", "ca", "da", "de", "el", "en", "eo", "es", "fi", "fr", "ga", "he", "hu", "it", "ja", /*"ka",*/ "ko", "nl", "no-bok", "no-nyn", "oc", "pt-br", "pt-pt", "ro", "ru", "sl", "sv", /*"th",*/ "yi", "zh-cn", "zh-tw" };
28     private static String JavaDoc[] names = { "Arabic", "Catalan", "Danish", "German", "Greek", "English", "Esperanto", "Spanish", "Finnish", "French", "Irish Gaelic", "Hebrew", "Hungarian", "Italian", "Japanese", /*"Georgian",*/ "Korean", "Dutch", "Norwegian (Bokmal)", "Norwegian (Nynorsk)", "Occitan", "Portuguese (Brazil)", "Portuguese (Portugal)", "Romanian", "Russian", "Slovakian", "Swedish", /*"Thai",*/ "Yiddish", "Chinese (Simplified)", "Chinese (Traditional)" };
29
30     private static PDFFont opentypefont, builtinfont;
31
32     public static void main(String JavaDoc[] args)
33         throws IOException
34     {
35     if (args.length>0) NORMALFONT = args[0];
36
37     init();
38
39     PDF pdf = new PDF();
40     addPage(pdf, "Intro", "resources/utf8/intro.txt", getFont("intro"), 16, Locale.ENGLISH);
41
42     // Add the language pages
43
//
44
for (int i=0;i<langs.length;i++)
45     {
46         String JavaDoc file = "resources/utf8/"+langs[i]+".txt";
47         Locale locale = new Locale(langs[i], "UK"); // Country doesn't matter
48
addPage(pdf, names[i], file, getFont(langs[i]), 24, locale);
49     }
50
51     pdf.setLayout("OneColumn", "UseOutlines");
52     pdf.setInfo("Subject", "Demonstration of Unicode functionality\nin the Big Faceless PDF Library");
53
54     // Create the file
55
//
56
OutputStream fo = new FileOutputStream("Unicode.pdf");
57     pdf.render(fo);
58     fo.close();
59     }
60
61     // This routine adds the specified file to the specified PDF, in the specified
62
// font and size.
63
//
64
public static void addPage(PDF pdf, String JavaDoc title, String JavaDoc file, PDFFont font, float size, Locale l)
65     throws IOException
66     {
67     // Create a new page, and add a bookmark to it
68
//
69
PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
70     pdf.getBookmarks().add(new PDFBookmark(title, PDFAction.goTo(page)));
71
72     // Create the header text
73
//
74
pdf.setLocale(Locale.ENGLISH);
75     PDFStyle style = new PDFStyle();
76     style.setFillColor(Color.black);
77     style.setFont(new StandardFont(StandardFont.HELVETICAOBLIQUE), 18);
78     page.setStyle(style);
79     page.drawText(title, 50, page.getHeight()-50);
80
81     // Add gratuitous hyperlink back to our website
82
//
83
style.setFont(new StandardFont(StandardFont.HELVETICAOBLIQUE), 10);
84     page.setStyle(style);
85     page.beginText(50,50,page.getWidth()-50, 70);
86     page.drawText("Created with the ");
87     page.beginTextLink(PDFAction.goToURL(new java.net.URL JavaDoc("http://big.faceless.org/products/pdf")), PDFStyle.LINKSTYLE);
88     page.drawText("Big Faceless PDF Library");
89     page.endTextLink();
90     page.endText(false);
91
92     // Create the body of the text. Be sure to set the working locale of the PDF
93
// to the correct locale for the language. This changes various things, most of
94
// them fairly subtle changes to do with default text-direction, line wrapping
95
// styles etc.
96
//
97
// After we set the locale, we create a brand new style so it will pick up any
98
// text-direction changes implied by the locale. If we reused the same style we
99
// defined above, all the text would be left-aligned (or whatever the default is
100
// for Locale.ENGLISH), even hebrew and arabic!
101
//
102
pdf.setLocale(l);
103
104     // Create a new style, in the specified font
105
//
106
style = new PDFStyle();
107     style.setFillColor(Color.black);
108     style.setFont(font, size);
109     page.setStyle(style);
110
111     // Display the text on the page. First jump through the hoops Java sets us to
112
// load files in UTF-8
113
//
114
page.beginText(50,50,page.getWidth()-50, page.getHeight()-100);
115     BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
116     String JavaDoc s;
117     while ((s=r.readLine())!=null) {
118         page.drawText(page.getStyle().getFont().requote(s,pdf.getLocale()) + "\n");
119     }
120     r.close();
121     page.endText(false);
122     }
123
124
125     //----------------------- UTILITY METHODS FROM HERE ON ----------------------------
126

127
128     // Get the font to be used for the specified language
129
//
130
private static PDFFont getFont(String JavaDoc lang)
131     {
132     if (lang.equals("intro")) return builtinfont;
133
134     // The following languages can use the built in fonts, as all the
135
// characters they require are provided. Pretty much, anyway -
136
// take a look at the Internationalization section of the userguide
137
// for more info.
138
//
139
String JavaDoc[] builtinfontok = { "en", "de", "fr", "es", "no", "sv", "da", "fi", "it", "pt", "ca", "eu", "nl", "sq", "et", "rm", "fo", "is", "ga", "gd", "af", "sw", "fy", "gl", "id", "in", "tl" };
140
141     for (int i=0;i<builtinfontok.length;i++) {
142         if (lang.startsWith(builtinfontok[i])) {
143             return builtinfont;
144         }
145     }
146     if (lang.startsWith("ja")) {
147         return new StandardCJKFont(StandardCJKFont.HEISEIMIN, StandardCJKFont.REGULAR);
148     } else if (lang.startsWith("ko")) {
149         return new StandardCJKFont(StandardCJKFont.HYSMYEONGJO, StandardCJKFont.REGULAR);
150     } else if (lang.equals("zh-tw") || lang.equals("zh-hk")) {
151         return new StandardCJKFont(StandardCJKFont.MSUNG, StandardCJKFont.REGULAR);
152     } else if (lang.equals("zh-cn") || lang.equals("zh-sg")) {
153         return new StandardCJKFont(StandardCJKFont.STSONG, StandardCJKFont.REGULAR);
154     } else {
155         return opentypefont;
156     }
157     }
158
159     // Initialize the fonts to be used
160
//
161
private static void init()
162         throws IOException
163     {
164     // Load the Font to be used for most of the examples. If you get an error
165
// here, you probably haven't changed the NORMALFONT String to point to
166
// the TrueType font file you want to use (we don't supply one with this library).
167
//
168
InputStream in = new FileInputStream(NORMALFONT);
169     opentypefont = new OpenTypeFont(in, 2);
170     in.close();
171     builtinfont = new StandardFont(StandardFont.TIMES);
172     }
173 }
174
Popular Tags