View Javadoc

1   // Copyright 2001-2005 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   
8   import org.freehep.graphics2d.font.CharTable;
9   import org.freehep.graphicsio.font.FontIncluder;
10  
11  /**
12   * Includes one of the 14 Type1 fonts in PDF documents
13   * 
14   * @author Simon Fischer
15   * @version $id$
16   */
17  public class PDFFontIncluder extends FontIncluder {
18  
19      private static final int PLAIN = 0;
20  
21      private static final int BOLD = 1;
22  
23      private static final int ITALIC = 2;
24  
25      private static final int BOLDITALIC = 3;
26  
27      private static final int COURIER = 0;
28  
29      private static final int HELVETICA = 1;
30  
31      private static final int TIMES = 2;
32  
33      private static final int SYMBOL = 3;
34  
35      private static final int DINGBATS = 4;
36  
37      private static final String[][] STANDARD_FONT = {
38              { "Courier", "Courier-Bold", "Courier-Oblique",
39                      "Courier-BoldOblique" },
40              { "Helvetica", "Helvetica-Bold", "Helvetica-Oblique",
41                      "Helvetica-BoldOblique" },
42              { "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic" },
43              { "Symbol" }, { "ZapfDingbats" } };
44  
45      private PDFWriter pdf;
46  
47      private String reference;
48  
49      private PDFRedundanceTracker redundanceTracker;
50  
51      public PDFFontIncluder(FontRenderContext context, PDFWriter pdf,
52              String reference, PDFRedundanceTracker redundanceTracker) {
53          super(context);
54          this.pdf = pdf;
55          this.reference = reference;
56          this.redundanceTracker = redundanceTracker;
57      }
58  
59      protected void openIncludeFont() throws IOException {
60  
61          int style = getFontStyle(getFont());
62          int fontBaseIndex = getFontBaseIndex(getFont());
63  
64          PDFDictionary font = pdf.openDictionary(reference);
65          font.entry("Type", pdf.name("Font"));
66          font.entry("Subtype", pdf.name("Type1"));
67          font.entry("Name", pdf.name(reference));
68          font.entry("BaseFont", pdf.name(STANDARD_FONT[fontBaseIndex][style]));
69          // font.entry("Encoding", pdf.ref(reference+"Encoding"));
70          font.entry("Encoding", redundanceTracker.getReference(
71                  getEncodingTable(), PDFCharTableWriter.getInstance()));
72          pdf.close(font);
73      }
74  
75      protected void writeEncoding(CharTable charTable) throws IOException {
76      }
77  
78      public static boolean isStandardFont(Font font) {
79          String fontName = font.getName().toLowerCase();
80          return (fontName.indexOf("helvetica") >= 0)
81                  || (fontName.indexOf("times") >= 0)
82                  || (fontName.indexOf("courier") >= 0)
83                  || (fontName.indexOf("symbol") >= 0)
84                  || (fontName.indexOf("dingbats") >= 0);
85      }
86  
87      /** Returns the index of the standard font according to STANDARD_FONT. */
88      private static int getFontBaseIndex(Font font) {
89          String fontName = font.getName().toLowerCase();
90          if (fontName.indexOf("helvetica") >= 0) {
91              return HELVETICA;
92          } else if (fontName.indexOf("times") >= 0) {
93              return TIMES;
94          } else if (fontName.indexOf("courier") >= 0) {
95              return COURIER;
96          } else if (fontName.indexOf("symbol") >= 0) {
97              return SYMBOL;
98          } else if (fontName.indexOf("dingbats") >= 0) {
99              return DINGBATS;
100         } else {
101             // use HELVETICA as default
102             return HELVETICA;
103         }
104     }
105 
106     /** Returns the index of the respective entry in STANDARD_FONT[fontIndex]. */
107     private static int getFontStyle(Font font) {
108         int fontBase = getFontBaseIndex(font);
109         if ((fontBase >= 0) && (STANDARD_FONT[fontBase].length == 1))
110             return PLAIN;
111         if (fontBase < 0)
112             return -1;
113         if (font.isBold()) {
114             if (font.isItalic()) {
115                 return BOLDITALIC;
116             } else {
117                 return BOLD;
118             }
119         } else {
120             if (font.isItalic())
121                 return ITALIC;
122         }
123         return PLAIN;
124     }
125 
126 }