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