1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.InputStream ; 55 import java.util.HashMap ; 56 import java.util.StringTokenizer ; 57 58 import com.lowagie.text.pdf.fonts.FontsResourceAnchor; 59 60 public class GlyphList { 61 private static HashMap unicode2names = new HashMap (); 62 private static HashMap names2unicode = new HashMap (); 63 64 static { 65 InputStream is = null; 66 try { 67 is = BaseFont.getResourceStream(BaseFont.RESOURCE_PATH + "glyphlist.txt", new FontsResourceAnchor().getClass().getClassLoader()); 68 if (is == null) { 69 String msg = "glyphlist.txt not found as resource. (It must exist as resource in the package com.lowagie.text.pdf.fonts)"; 70 throw new Exception (msg); 71 } 72 byte buf[] = new byte[1024]; 73 ByteArrayOutputStream out = new ByteArrayOutputStream (); 74 while (true) { 75 int size = is.read(buf); 76 if (size < 0) 77 break; 78 out.write(buf, 0, size); 79 } 80 is.close(); 81 is = null; 82 String s = PdfEncodings.convertToString(out.toByteArray(), null); 83 StringTokenizer tk = new StringTokenizer (s, "\r\n"); 84 while (tk.hasMoreTokens()) { 85 String line = tk.nextToken(); 86 if (line.startsWith("#")) 87 continue; 88 StringTokenizer t2 = new StringTokenizer (line, " ;\r\n\t\f"); 89 String name = null; 90 String hex = null; 91 if (!t2.hasMoreTokens()) 92 continue; 93 name = t2.nextToken(); 94 if (!t2.hasMoreTokens()) 95 continue; 96 hex = t2.nextToken(); 97 Integer num = Integer.valueOf(hex, 16); 98 unicode2names.put(num, name); 99 names2unicode.put(name, new int[]{num.intValue()}); 100 } 101 } 102 catch (Exception e) { 103 System.err.println("glyphlist.txt loading error: " + e.getMessage()); 104 } 105 finally { 106 if (is != null) { 107 try { 108 is.close(); 109 } 110 catch (Exception e) { 111 } 113 } 114 } 115 } 116 117 public static int[] nameToUnicode(String name) { 118 return (int[])names2unicode.get(name); 119 } 120 121 public static String unicodeToName(int num) { 122 return (String )unicode2names.get(new Integer (num)); 123 } 124 } | Popular Tags |