KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HelloUnicodeWorld


1 // $Id: HelloUnicodeWorld.java,v 1.2 2003/11/03 10:56:14 mike Exp $
2

3 import java.util.*;
4 import java.io.*;
5 import java.awt.Color JavaDoc;
6 import org.faceless.pdf2.*;
7
8 // A more international Hello World example. Unicode text is
9
// no more difficult than standard Latin text!
10
//
11
// A more complete example of internationalization is in
12
// the "Unicode.java" example
13
//
14
public class HelloUnicodeWorld
15 {
16     public static void main(String JavaDoc[] args)
17         throws IOException
18     {
19     if (args.length==0) {
20         System.err.println("Usage: java HelloUnicodeWorld <truetype-font-file>\n\nThis program needs a TrueType font containing Arabic and Cyrllic\ncharacters, like \"times.ttf\" (the Times-Roman supplied with\nMicrosoft Windows 2000.\n\n");
21         System.exit(1);
22     }
23
24     // Create a new PDF
25
PDF pdf = new PDF();
26
27     // Create a new page
28
//
29
PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
30
31     // Create a new "style" to write in - Black 24pt, with whatever font
32
// was given on the command line.
33
//
34
PDFStyle mystyle = new PDFStyle();
35
36     OpenTypeFont otf = new OpenTypeFont(new FileInputStream(args[0]),2);
37     mystyle.setFont(otf, 24);
38     mystyle.setFillColor(Color.black);
39
40     // Write Hello World in:
41
//
42
// * English
43
// * Russian
44
// * Arabic
45
// * Czech
46
//
47
// Diclaimer: We don't actually speak any of these except English
48
// (and some would argue that isn't too hot either). Speling
49
// mistakes are ours, and ours alone. Thanks to those that emailed
50
// additional languages.
51
//
52
// Feel free to mail corrections, or different languages (greek, thai
53
// or hebrew anyone?) to info@big.faceless.org
54
//
55
// Notice that the arabic letters are all the "nominal" forms, from
56
// the U+0600 table. From version 1.0.1 and above, these will be
57
// automatically substituted with the appropriate ligature.
58
//
59
// We're trying to demonstrate is that you can mix and match
60
// any unicode character in the same *String*, let alone the same
61
// document - which is why this is all on one line and hard to read.
62
//
63
//
64
page.setStyle(mystyle);
65     page.drawText("Hello, World\n\u0417\u0434\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435 \u043C\u0438\u0440\n\u010Cau sv\u011bte\n\u0633\u0644\u0627\u0645 \u0627\u0644\u0639\u0627\u0644\u0645\n", 100, page.getHeight()-100);
66
67     // Add some document info
68
//
69
pdf.setInfo("Author", "Big Faceless Organization");
70     pdf.setInfo("Title", "Unicode Text Demonstration");
71
72     // Write the document to a file
73
//
74
OutputStream fo = new FileOutputStream("HelloUnicodeWorld.pdf");
75     pdf.render(fo);
76     fo.close();
77     }
78 }
79
Popular Tags