View Javadoc

1   // Copyright 2001-2003, FreeHEP.
2   package org.freehep.graphicsio.pdf;
3   
4   import java.awt.Font;
5   import java.awt.font.FontRenderContext;
6   import java.io.IOException;
7   import java.util.Collection;
8   import java.util.Iterator;
9   import java.util.Properties;
10  
11  import org.freehep.graphics2d.font.CharTable;
12  import org.freehep.graphics2d.font.Lookup;
13  import org.freehep.graphicsio.FontConstants;
14  import org.freehep.graphicsio.font.FontIncluder;
15  import org.freehep.graphicsio.font.FontTable;
16  
17  /**
18   * A table to remember which fonts were used while writing a pdf document.
19   * Entries to resource dictionaries and embedding of fonts can be done when the
20   * drawing is finished by calling <tt>addAll()</tt>.
21   * 
22   * @author Simon Fischer
23   * @version $Id: PDFFontTable.java 9329 2006-11-14 22:29:00Z duns $
24   */
25  public class PDFFontTable extends FontTable {
26  
27      private int currentFontIndex = 1;
28  
29      private PDFWriter pdf;
30  
31      private PDFRedundanceTracker tracker;
32  
33      public PDFFontTable(PDFWriter pdf) {
34          super();
35          this.pdf = pdf;
36          this.tracker = new PDFRedundanceTracker(pdf);
37      }
38  
39      /** Adds all fonts to a dictionary named "FontList". */
40      public int addFontDictionary() throws IOException {
41          Collection fonts = getEntries();
42          if (fonts.size() > 0) {
43              PDFDictionary fontList = pdf.openDictionary("FontList");
44              for (Iterator i = fonts.iterator(); i.hasNext();) {
45                  Entry e = (Entry) i.next();
46                  fontList.entry(e.getReference(), pdf.ref(e.getReference()));
47              }
48              pdf.close(fontList);
49          }
50          return fonts.size();
51      }
52  
53      /** Embeds all not yet embedded fonts to the file. */
54      public void embedAll(FontRenderContext context, boolean embed,
55              String embedAs) throws IOException {
56          Collection col = getEntries();
57          Iterator i = col.iterator();
58          while (i.hasNext()) {
59              Entry e = (Entry) i.next();
60              if (!e.isWritten()) {
61                  e.setWritten(true);
62  
63                  FontIncluder fontIncluder = null;
64                  if (PDFFontIncluder.isStandardFont(e.getFont())) {
65                      embed = false;
66                  }
67  
68                  if (embed) {
69                      if (embedAs.equals(FontConstants.EMBED_FONTS_TYPE3)) {
70                          fontIncluder = new PDFFontEmbedderType3(context, pdf, e
71                                  .getReference(), tracker);
72                      } else if (embedAs.equals(FontConstants.EMBED_FONTS_TYPE1)) {
73                          fontIncluder = PDFFontEmbedderType1.create(context,
74                                  pdf, e.getReference(), tracker);
75                      } else {
76                          System.out
77                                  .println("PDFFontTable: invalid value for embedAs: "
78                                          + embedAs);
79                      }
80                  } else {
81                      fontIncluder = new PDFFontIncluder(context, pdf, e
82                              .getReference(), tracker);
83                  }
84                  fontIncluder.includeFont(e.getFont(), e.getEncoding(), e
85                          .getReference());
86              }
87          }
88          tracker.writeAll();
89      }
90  
91      public CharTable getEncodingTable() {
92          return Lookup.getInstance().getTable("PDFLatin");
93      }
94  
95      public void firstRequest(Entry e, boolean embed, String embedAs) {
96      }
97  
98      private static final Properties replaceFonts = new Properties();
99      static {
100         replaceFonts.setProperty("Dialog", "Helvetica");
101         replaceFonts.setProperty("DialogInput", "Courier");
102         replaceFonts.setProperty("Serif", "TimesRoman");
103         replaceFonts.setProperty("SansSerif", "Helvetica");
104         replaceFonts.setProperty("Monospaced", "Courier");
105     }
106 
107     protected Font substituteFont(Font font) {
108         String fontName = replaceFonts.getProperty(font.getName(), null);
109         if (fontName != null) {
110             Font newFont = new Font(fontName, font.getSize(), font.getStyle());
111             font = newFont.deriveFont(font.getSize2D());
112         }
113         return font;
114     }
115 
116     /**
117      * Creates the reference by numbering them.
118      * 
119      * @return "F"+currentFontIndex
120      */
121     protected String createFontReference(Font f) {
122         return "F" + (currentFontIndex++);
123     }
124 }