Debian Codegraph @

freehep-graphicsio-ps/src/main/java/org/freehep/graphicsio/ps/PSFontTable.java

raw
1 // Copyright 2001-2005, FreeHEP.
2 package org.freehep.graphicsio.ps;
3
4 import java.awt.Font;
5 import java.awt.font.FontRenderContext;
6 import java.awt.font.TextAttribute;
7 import java.io.IOException;
8 import java.io.OutputStream;
9 import java.io.PrintStream;
10 import java.text.AttributedCharacterIterator.Attribute;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Properties;
14
15 import org.freehep.graphics2d.font.CharTable;
16 import org.freehep.graphics2d.font.FontUtilities;
17 import org.freehep.graphics2d.font.Lookup;
18 import org.freehep.graphicsio.FontConstants;
19 import org.freehep.graphicsio.font.FontEmbedderType1;
20 import org.freehep.graphicsio.font.FontIncluder;
21 import org.freehep.graphicsio.font.FontTable;
22
23 /**
24 * FontTable for PS files. The fonts name is used as a reference for the font.
25 * When the font is first used, it is embedded to the file if it is not a
26 * standard font. If it is unknown it is not substituted.
27 *
28 * @author Simon Fischer
29 * @version $Id: freehep-graphicsio-ps/src/main/java/org/freehep/graphicsio/ps/PSFontTable.java 59372df5e0d9 2007/02/06 21:11:19 duns $
30 */
31 public class PSFontTable extends FontTable {
32
33 private OutputStream out;
34
35 private FontRenderContext context;
36
37 public PSFontTable(OutputStream out, FontRenderContext context) {
38 super();
39 this.out = out;
40 this.context = context;
41 }
42
43 public CharTable getEncodingTable() {
44 return Lookup.getInstance().getTable("STDLatin");
45 }
46
47 protected void firstRequest(Entry e, boolean embed, String embedAs)
48 throws IOException {
49 FontIncluder fontIncluder = null;
50 e.setWritten(true);
51
52 // There are NO standard fonts in PS.
53 // if (isStandardFont(e.getFont())) return;
54
55 out.flush();
56
57 if (embed) {
58 if (embedAs.equals(FontConstants.EMBED_FONTS_TYPE3)) {
59 fontIncluder = new PSFontEmbedder(context, new PrintStream(out));
60 } else if (embedAs.equals(FontConstants.EMBED_FONTS_TYPE1)) {
61 fontIncluder = new FontEmbedderType1(context, out, true);
62 } else {
63 System.err
64 .println("PSFontTable: not a valid value for embedAs: "
65 + embedAs);
66 }
67 } else {
68 // FIXME: set the best standard font
69 // e.setReference(standardfontName)
70 return;
71 }
72
73 fontIncluder
74 .includeFont(e.getFont(), e.getEncoding(), e.getReference());
75 out.flush();
76 }
77
78 /**
79 * Java font names -> PS Font names, used by {@link #normalize(java.util.Map)}
80 */
81 private static final Properties replaceFonts = new Properties();
82 static {
83 replaceFonts.setProperty("timesroman", "Times");
84 replaceFonts.setProperty("dialog", "Helvetica");
85 replaceFonts.setProperty("dialoginput", "Courier-New");
86 // FIXME: works well on windows, others?
87 replaceFonts.setProperty("serif", "Times");
88 replaceFonts.setProperty("sansserif", "Helvetica");
89 // FIXME: works well on windows, others?
90 replaceFonts.setProperty("monospaced", "Courier-New");
91 replaceFonts.setProperty("typewriter", "Courier-New");
92 }
93
94 /**
95 * fonts that have no TextAttribute.WEIGHT and TextAttribute.POSTURE,
96 * used by {@link #createFontReference(java.awt.Font)}
97 */
98 private static final HashSet<String> ignoreAtributes = new HashSet<String>();
99 static {
100 ignoreAtributes.add("Symbol");
101 ignoreAtributes.add("ZapfDingbats");
102 }
103
104 /**
105 * removes any transformation and superscript, changes the names
106 * to PS font name
107 *
108 * @param font
109 * @return derived font
110 */
111 @SuppressWarnings( "unchecked" )
112 protected Font substituteFont(Font font) {
113 Map<?, ?> attributes = FontUtilities.getAttributes(font);
114 // change names
115 // normalize(attributes);
116 // remove transformations
117 attributes.remove(TextAttribute.TRANSFORM);
118 attributes.remove(TextAttribute.SUPERSCRIPT);
119 return new Font((Map<? extends Attribute, ?>) attributes);
120 }
121
122 /**
123 * Uses the font name as a reference. Whitespace is stripped. The font style
124 * (italic/bold) is added as a suffix delimited by a dash.
125 * Uses {@link #normalize(java.util.Map)}
126 */
127 protected String createFontReference(Font font) {
128 Map /*<TextAttribute, ?>*/<Attribute, Object> attributes = FontUtilities.getAttributes(font);
129 normalize(attributes);
130
131 // replace the name
132 StringBuffer result = new StringBuffer();
133
134 // insert family at the end because oft the "-" between
135 // name and TextAttribute
136 String family = (String) attributes.get(TextAttribute.FAMILY);
137
138 // weight
139 Object weight = ignoreAtributes.contains(family) ?
140 null : attributes.get(TextAttribute.WEIGHT);
141
142 if (TextAttribute.WEIGHT_BOLD.equals(weight)) {
143 result.append("Bold");
144 } else if (TextAttribute.WEIGHT_DEMIBOLD.equals(weight)) {
145 result.append("DemiBold");
146 } else if (TextAttribute.WEIGHT_DEMILIGHT.equals(weight)) {
147 result.append("DemiLight");
148 } else if (TextAttribute.WEIGHT_EXTRA_LIGHT.equals(weight)) {
149 result.append("ExtraLight");
150 } else if (TextAttribute.WEIGHT_EXTRABOLD.equals(weight)) {
151 result.append("ExtraBold");
152 } else if (TextAttribute.WEIGHT_HEAVY.equals(weight)) {
153 result.append("Heavy");
154 } else if (TextAttribute.WEIGHT_LIGHT.equals(weight)) {
155 result.append("Light");
156 } else if (TextAttribute.WEIGHT_MEDIUM.equals(weight)) {
157 result.append("Medium");
158 } else if (TextAttribute.WEIGHT_REGULAR.equals(weight)) {
159 // result.append("WRegular");
160 } else if (TextAttribute.WEIGHT_SEMIBOLD.equals(weight)) {
161 result.append("SemiBold");
162 } else if (TextAttribute.WEIGHT_ULTRABOLD.equals(weight)) {
163 result.append("UltraBold");
164 }
165
166 // italic
167 Object posture = ignoreAtributes.contains(family) ?
168 null : attributes.get(TextAttribute.POSTURE);
169
170 if (TextAttribute.POSTURE_OBLIQUE.equals(posture)) {
171 if (family.equals("Times")) {
172 result.append("Italic");
173 } else {
174 result.append("Oblique");
175 }
176 } else if (TextAttribute.POSTURE_REGULAR.equals(posture)) {
177 // result.append("IRegular");
178 }
179
180 // Times -> Times-Roman
181 if (family.equals("Times") && result.length() == 0) {
182 result.append("Roman");
183 }
184
185 // underline is not a specific font
186 // Object ul = font.getAttributes().get(TextAttribute.UNDERLINE);
187 // if (TextAttribute.UNDERLINE_LOW_DASHED.equals(ul)) {
188 // result.append("UnderlineLowDashed");
189 // } else if (TextAttribute.UNDERLINE_LOW_DOTTED.equals(ul)) {
190 // result.append("UnderlineLowDotted");
191 // } else if (TextAttribute.UNDERLINE_LOW_GRAY.equals(ul)) {
192 // result.append("UnderlineLowGray");
193 // } else if (TextAttribute.UNDERLINE_LOW_ONE_PIXEL.equals(ul)) {
194 // result.append("UnderlineLowOnePixel");
195 // } else if (TextAttribute.UNDERLINE_ON.equals(ul)) {
196 // result.append("Underline");
197 // }
198
199 // strike through is not a specific font
200 // if (font.getAttributes().get(TextAttribute.STRIKETHROUGH) != null) {
201 // result.append("StrikeThrough");
202 // }
203
204 // width is not a specific font
205 // Object width = font.getAttributes().get(TextAttribute.WIDTH);
206 // if (TextAttribute.WIDTH_CONDENSED.equals(width)) {
207 // result.append("Condensed");
208 // } else if (TextAttribute.WIDTH_EXTENDED.equals(width)) {
209 // result.append("Extended");
210 // } else if (TextAttribute.WIDTH_REGULAR.equals(width)) {
211 // // result.append("WRegular");
212 // } else if (TextAttribute.WIDTH_SEMI_CONDENSED.equals(width)) {
213 // result.append("SemiCondensed");
214 // } else if (TextAttribute.WIDTH_SEMI_EXTENDED.equals(width)) {
215 // result.append("SemiExtended");
216 // }
217
218 // insert "name-" at the beginning or return plain "name"
219 if (result.length() > 0) {
220 result.insert(0, "-");
221 result.insert(0, attributes.get(TextAttribute.FAMILY));
222 } else {
223 result.append(attributes.get(TextAttribute.FAMILY));
224 }
225
226 return result.toString();
227 }
228
229 /**
230 * Replaces TextAttribute.FAMILY by values of replaceFonts.
231 * Whitespace is family name stripped. When a
232 * font created using the result of this method the transformation would be:
233 *
234 * <code>java.awt.Font[family=SansSerif,name=SansSerif,style=plain,size=30]</code><BR>
235 * will result to:<BR>
236 * <code>java.awt.Font[family=SansSerif,name=Helvetica,style=plain,size=30]</code><BR><BR>
237 *
238 * Uses {@link FontTable#normalize(java.util.Map)} first.
239 *
240 * @param attributes with font name to change
241 */
242 public static void normalize(Map /*<TextAttribute, ?>*/<Attribute, Object> attributes) {
243 // dialog.bold -> Dialog with TextAttribute.WEIGHT_BOLD
244 FontTable.normalize(attributes);
245
246 // get replaced font family name (Yes it's right, not the name!)
247 String family = replaceFonts.getProperty(
248 ((String) attributes.get(TextAttribute.FAMILY)).toLowerCase());
249 if (family == null) {
250 family = (String) attributes.get(TextAttribute.FAMILY);
251 }
252
253 // remove spaces
254 family = family.replaceAll(" ", "");
255
256 // store family
257 attributes.put(TextAttribute.FAMILY, family);
258 }
259 }