1 import java.util.*; 2 import java.io.*; 3 import java.awt.Color ; 4 import org.faceless.pdf2.*; 5 6 14 public class Unicode 15 { 16 private static String NORMALFONT = "C:\\WINNT\\FONTS\\TIMES.TTF"; 21 22 23 private static String [] langs = { "ar", "ca", "da", "de", "el", "en", "eo", "es", "fi", "fr", "ga", "he", "hu", "it", "ja", "ko", "nl", "no-bok", "no-nyn", "oc", "pt-br", "pt-pt", "ro", "ru", "sl", "sv", "yi", "zh-cn", "zh-tw" }; 28 private static String [] names = { "Arabic", "Catalan", "Danish", "German", "Greek", "English", "Esperanto", "Spanish", "Finnish", "French", "Irish Gaelic", "Hebrew", "Hungarian", "Italian", "Japanese", "Korean", "Dutch", "Norwegian (Bokmal)", "Norwegian (Nynorsk)", "Occitan", "Portuguese (Brazil)", "Portuguese (Portugal)", "Romanian", "Russian", "Slovakian", "Swedish", "Yiddish", "Chinese (Simplified)", "Chinese (Traditional)" }; 29 30 private static PDFFont opentypefont, builtinfont; 31 32 public static void main(String [] 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 for (int i=0;i<langs.length;i++) 45 { 46 String file = "resources/utf8/"+langs[i]+".txt"; 47 Locale locale = new Locale(langs[i], "UK"); 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 OutputStream fo = new FileOutputStream("Unicode.pdf"); 57 pdf.render(fo); 58 fo.close(); 59 } 60 61 public static void addPage(PDF pdf, String title, String file, PDFFont font, float size, Locale l) 65 throws IOException 66 { 67 PDFPage page = pdf.newPage(PDF.PAGESIZE_A4); 70 pdf.getBookmarks().add(new PDFBookmark(title, PDFAction.goTo(page))); 71 72 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 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 ("http://big.faceless.org/products/pdf")), PDFStyle.LINKSTYLE); 88 page.drawText("Big Faceless PDF Library"); 89 page.endTextLink(); 90 page.endText(false); 91 92 pdf.setLocale(l); 103 104 style = new PDFStyle(); 107 style.setFillColor(Color.black); 108 style.setFont(font, size); 109 page.setStyle(style); 110 111 page.beginText(50,50,page.getWidth()-50, page.getHeight()-100); 115 BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); 116 String 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 127 128 private static PDFFont getFont(String lang) 131 { 132 if (lang.equals("intro")) return builtinfont; 133 134 String [] 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 private static void init() 162 throws IOException 163 { 164 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 |