View Javadoc

1   // Copyright FreeHEP, 2003-2007
2   package org.freehep.graphics2d.font;
3   
4   import java.awt.Font;
5   import java.awt.GraphicsEnvironment;
6   import java.awt.font.TextAttribute;
7   import java.io.IOException;
8   import java.util.Arrays;
9   import java.util.List;
10  import java.util.Properties;
11  import java.util.Hashtable;
12  
13  /**
14   * 
15   * @author Mark Donszelmann
16   * @version $Id$
17   */
18  public class FontUtilities {
19  
20      private FontUtilities() {
21      }
22  
23      public static List getAllAvailableFonts() {
24          return Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment()
25                  .getAvailableFontFamilyNames());
26      }
27  
28      private static final Properties windowsFonts = new Properties();
29      static {
30          // Looks like Unicode MS makes thinner characters
31          // List fontNames = getAllAvailableFonts();
32          // String arial = fontNames.contains("Arial Unicode MS") ? "Arial
33          // Unicode MS" : "Arial";
34          String arial = "Arial";
35  
36          // logical fonts
37          windowsFonts.setProperty("Dialog", arial);
38          windowsFonts.setProperty("DialogInput", "Courier New");
39          windowsFonts.setProperty("Serif", "Times New Roman");
40          windowsFonts.setProperty("SansSerif", arial);
41          windowsFonts.setProperty("Monospaced", "Courier New");
42  
43          // pdf fonts
44          windowsFonts.setProperty("Courier", "Courier New");
45          windowsFonts.setProperty("Helvetica", arial);
46          windowsFonts.setProperty("Times-Roman", "Times New Roman");
47          windowsFonts.setProperty("TimesRoman", "Times New Roman");
48          windowsFonts.setProperty("Times", "Times New Roman");
49          windowsFonts.setProperty("Symbol", "Arial Unicode MS");
50          windowsFonts.setProperty("ZapfDingbats", "Arial Unicode MS");
51      }
52  
53      public static String getWindowsFontName(String fontName) {
54          return windowsFonts.getProperty(fontName, fontName);
55      }
56  
57      /**
58       * @deprecated use
59       *             org.freehep.graphics2d.font.FontEncoder.getEncodedString()
60       */
61      public static String getEncodedString(String string, String tableName) {
62          return FontEncoder.getEncodedString(string, tableName);
63      }
64  
65      /**
66       * Returns an unicode encoded string from an ascii encoded string, using the
67       * supplied table.
68       * 
69       * @deprecated use
70       *             org.freehep.graphics2d.font.FontEncoder.getEncodedString()
71       */
72      public static String getEncodedString(String string, CharTable charTable) {
73          return FontEncoder.getEncodedString(string, charTable);
74      }
75  
76      public interface ShowString {
77          public void showString(Font font, String string) throws IOException;
78      }
79  
80      private static final CharTable STANDARD_CHAR_TABLES[] = { null,
81              Lookup.getInstance().getTable("Symbol"),
82              Lookup.getInstance().getTable("Zapfdingbats") };
83  
84      private static final Font STANDARD_FONT[] = { null,
85              new Font("Symbol", Font.PLAIN, 10),
86              new Font("ZapfDingbats", Font.PLAIN, 10), };
87  
88      /**
89       * Shows a String and switches the encoding (and font) everytime the unicode
90       * characters leave the range of the curent encoding. Outside the range of
91       * the given latinTable, Symbol and ZapfDingbats are checked. If none of
92       * these three encodings contain the unicode character, an undefined
93       * character is used.
94       */
95      public static void showString(Font font, String string,
96              CharTable latinTable, ShowString device) throws IOException {
97  
98          if (latinTable == null) throw new RuntimeException("FontUtilities.showString(...): latinTable cannot be 'null'");
99  
100         STANDARD_FONT[0] = font;
101         STANDARD_FONT[1] = new Font("Symbol", Font.PLAIN, font.getSize());
102         STANDARD_FONT[2] = new Font("ZapfDingbats", Font.PLAIN, font.getSize());
103         STANDARD_CHAR_TABLES[0] = latinTable;
104 
105         char[] chars = string.toCharArray();
106         String out = "";
107         int lastTable = 0;
108 
109         for (int i = 0; i < chars.length; i++) {
110 
111             // find out suitable table and encoding of this character
112             // try last table first
113             int table = lastTable;
114             char encoding = (char) STANDARD_CHAR_TABLES[table]
115                     .toEncoding(chars[i]);
116             // no success -> try all other tables
117             if (encoding == 0) {
118                 table = -1;
119                 do {
120                     table++;
121                     if (table != lastTable) { // we already checked that
122                         encoding = (char) STANDARD_CHAR_TABLES[table]
123                                 .toEncoding(chars[i]);
124                     }
125                 } while ((encoding == 0)
126                         && (table < STANDARD_CHAR_TABLES.length - 1));
127             }
128             if (encoding == 0)
129                 table = lastTable;
130 
131             if ((table != lastTable) && (!out.equals(""))) {
132                 // if font changes, write the old font and string so far
133                 device.showString(STANDARD_FONT[lastTable], out);
134                 out = "";
135             }
136             // append character to out
137             out += encoding;
138             lastTable = table;
139         }
140 
141         device.showString(STANDARD_FONT[lastTable], out);
142     }
143 
144     /**
145      * there is a bug in the jdk 1.6 which makes
146      * Font.getAttributes() not work correctly. The
147      * method does not return all values. What we dow here
148      * is using the old JDK 1.5 method.
149      *
150      * @param font font
151      * @return Attributes of font
152      */
153     public static Hashtable getAttributes(Font font) {
154         Hashtable result = new Hashtable(7, (float)0.9);
155         result.put(
156             TextAttribute.TRANSFORM,
157             font.getTransform());
158         result.put(
159             TextAttribute.FAMILY,
160             font.getName());
161         result.put(
162             TextAttribute.SIZE,
163             new Float(font.getSize2D()));
164         result.put(
165             TextAttribute.WEIGHT,
166             (font.getStyle() & Font.BOLD) != 0 ?
167                  TextAttribute.WEIGHT_BOLD :
168                  TextAttribute.WEIGHT_REGULAR);
169         result.put(
170             TextAttribute.POSTURE,
171                  (font.getStyle() & Font.ITALIC) != 0 ?
172                  TextAttribute.POSTURE_OBLIQUE :
173                  TextAttribute.POSTURE_REGULAR);
174         result.put(
175             TextAttribute.SUPERSCRIPT,
176             new Integer(0 /* no getter! */));
177         result.put(
178             TextAttribute.WIDTH,
179             new Float(1 /* no getter */));
180         return result;
181     }
182 }