protected void writeFontDict() throws IOException {
final FontUse[] fontUses = getFontConsumer().getUsedFontUses();
/* Embed fonts. */
for (int i = 0; i < fontUses.length; i++) {
final FontUse fontUse = fontUses[i];
final FontPs fontPS = (FontPs) fontUse.getFontOutput(
"application/ps");
/* For now, we don't know how to process TrueType fonts. */
if (! (fontUses[i].getEncoding() instanceof EncodingVector)) {
continue;
}
final Font font = fontUse.getFont();
if (! font.isEmbeddable()) {
continue;
}
final String fontContents = new String(
fontPS.getContentsPostScriptHex());
write(fontContents);
}
this.fontProcSet = getApplicationNameShort()
+ "Fonts";
write("%%BeginResource: procset " + this.fontProcSet);
write("%%Title: Font setup (shortcuts) for this file");
write("/" + this.fontProcSet + " 100 dict dup begin");
/* Bind each font to a short resource name. */
for (int i = 0; i < fontUses.length; i++) {
final FontUse fontUse = fontUses[i];
/* For now, we don't know how to process TrueType fonts. */
if (! (fontUses[i].getEncoding() instanceof EncodingVector)) {
continue;
}
final PSFont psFont = getPSFont(fontUse);
write("/" + psFont.getName() + " /" + fontUse.getPostscriptName()
+ " def");
}
write("end def");
write("%%EndResource");
/* Get a List of all EncodingVectors used. */
final List<EncodingVector> encodingsUsed
= new ArrayList<EncodingVector>();
for (int i = 0; i < fontUses.length; i++) {
final Encoding encoding = fontUses[i].getEncoding();
final Font font = fontUses[i].getFont();
/* If the FontUse has the same encoding as the Font, there is no
* need to write the encoding again, because it is already defined
* in the Font dictionary itself. */
if (encoding == font.getInternalEncoding()) {
continue;
}
if (encoding instanceof EncodingVector) {
boolean encodingFound = false;
for (int j = 0; j < encodingsUsed.size() && ! encodingFound;
j++) {
if (encodingsUsed.get(j) == encoding) {
encodingFound = true;
}
}
if (! encodingFound) {
encodingsUsed.add((EncodingVector) encoding);
}
}
}
/* Except PostScript-native encodings, write each encoding vector. */
for (int i = 0; i < encodingsUsed.size(); i++) {
final EncodingVector vector = encodingsUsed.get(i);
if (! vector.isPredefinedPs()) {
writeRaw(vector.asPostScript(null));
}
}
/* Bind the proper encoding to each font. */
for (int i = 0; i < fontUses.length; i++) {
final FontUse fontUse = fontUses[i];
final Font font = fontUse.getFont();
EncodingVector vector = null;
if (! (fontUse.getEncoding() instanceof EncodingVector)) {
/* For now, we don't know how to handle TrueType fonts here. */
continue;
}
vector = (EncodingVector) fontUse.getEncoding();
if (vector == font.getInternalEncoding()) {
/* If using font's internal encoding, don't re-encode. */
continue;
}
write("/" + font.getPostscriptName() + " findfont");
write("dup length dict begin");
write(" {1 index /FID ne {def} {pop pop} ifelse} forall");
write(" /Encoding " + vector.getName() + " def");
write(" currentdict");
write("end");
write("/" + fontUse.getPostscriptName() + " exch definefont pop");
}
}