]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Adjustments for the font refactoring
authorJeremias Maerki <jeremias@apache.org>
Wed, 8 Jan 2003 13:58:25 +0000 (13:58 +0000)
committerJeremias Maerki <jeremias@apache.org>
Wed, 8 Jan 2003 13:58:25 +0000 (13:58 +0000)
Lots of Javadocs
Fixed Checkstyle errors

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195822 13f79535-47bb-0310-9956-ffa450edef68

18 files changed:
src/org/apache/fop/pdf/PDFCIDFont.java
src/org/apache/fop/pdf/PDFDocument.java
src/org/apache/fop/pdf/PDFFont.java
src/org/apache/fop/pdf/PDFFontDescriptor.java
src/org/apache/fop/pdf/PDFFontNonBase14.java
src/org/apache/fop/pdf/PDFFontTrueType.java
src/org/apache/fop/pdf/PDFFontType0.java
src/org/apache/fop/pdf/PDFFontType1.java
src/org/apache/fop/pdf/PDFFontType3.java
src/org/apache/fop/pdf/PDFGState.java
src/org/apache/fop/pdf/PDFICCStream.java
src/org/apache/fop/pdf/PDFStream.java
src/org/apache/fop/pdf/PDFT1Stream.java
src/org/apache/fop/pdf/PDFTTFStream.java
src/org/apache/fop/pdf/PDFWArray.java
src/org/apache/fop/render/awt/AWTFontMetrics.java
src/org/apache/fop/render/awt/FontMetricsMapper.java
src/org/apache/fop/render/awt/FontSetup.java

index 3e912f5e629330e491e34daa466ae92daa5f5313..9a5a27e025f1c750b2a9dd0c20e4475dbd5ed97c 100644 (file)
@@ -1,78 +1,98 @@
 /*
  * $Id$
- * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.CIDFontType;
+
 // based on work by Takayuki Takeuchi
 
 /**
- * class representing a "character identifier" font (p 210 and onwards).
+ * Class representing a "character identifier" font (p 210 and onwards).
  */
 public class PDFCIDFont extends PDFObject {
 
-    public static final byte CID_TYPE0 = 0;
-    public static final byte CID_TYPE2 = 1;
-    protected static final String[] TYPE_NAMES = {
-        "CIDFontType0", "CIDFontType2"
-    };
-
-    protected String basefont;
-
-    protected String cidtype;
-    protected Integer dw;
-    protected PDFWArray w;
-    protected int[] dw2;
-    protected PDFWArray w2;
-    protected PDFCIDSystemInfo systemInfo;
-    protected PDFCIDFontDescriptor descriptor;
-    protected PDFCMap cmap;
+    private String basefont;
+    private CIDFontType cidtype;
+    private Integer dw;
+    private PDFWArray w;
+    private int[] dw2;
+    private PDFWArray w2;
+    private PDFCIDSystemInfo systemInfo;
+    private PDFCIDFontDescriptor descriptor;
+    private PDFCMap cmap;
 
     /**
      * /CIDToGIDMap (only for CIDFontType2, see p 212)
      * can be either "Identity" (default) or a PDFStream
      */
-    protected PDFStream cidMap;
+    private PDFStream cidMap;
 
-    // compatibility with Takayuki Takeuchi
 
     /**
-     * create the /Font object
+     * Create the /Font object
+     * @param number PDF object number
+     * @param basefont Name of the basefont
+     * @param cidtype CID type
+     * @param dw default width
+     * @param w array of character widths
+     * @param registry name of the issuer
+     * @param ordering Unique name of the font
+     * @param supplement Supplement number
+     * @param descriptor CID font descriptor
      */
-    public PDFCIDFont(int number, String basefont, byte cidtype, int dw,
+    public PDFCIDFont(int number, String basefont, CIDFontType cidtype, int dw,
                       int[] w, String registry, String ordering,
                       int supplement, PDFCIDFontDescriptor descriptor) {
 
-        super(number);
+        this(number, basefont, cidtype, dw, 
+                new PDFWArray(w), 
+                new PDFCIDSystemInfo(registry, ordering, supplement),
+                descriptor);
+    }
 
-        this.basefont = basefont;
-        this.cidtype = TYPE_NAMES[(int)cidtype];
-        this.dw = new Integer(dw);
-        this.w = new PDFWArray();
-        this.w.addEntry(0, w);
-        this.dw2 = null;
-        this.w2 = null;
-        this.systemInfo = new PDFCIDSystemInfo(registry, ordering,
-                                               supplement);
-        this.descriptor = descriptor;
-        this.cidMap = null;
-        this.cmap = null;
+    /**
+     * Create the /Font object
+     * @param number PDF object number
+     * @param basefont Name of the basefont
+     * @param cidtype CID type
+     * @param dw default width
+     * @param w array of character widths
+     * @param systemInfo CID system info
+     * @param descriptor CID font descriptor
+     */
+    public PDFCIDFont(int number, String basefont, CIDFontType cidtype, int dw,
+                      int[] w, PDFCIDSystemInfo systemInfo,
+                      PDFCIDFontDescriptor descriptor) {
+
+        this(number, basefont, cidtype, dw, 
+                new PDFWArray(w), 
+                systemInfo,
+                descriptor);
     }
 
     /**
-     * create the /Font object
+     * Create the /Font object
+     * @param number PDF object number
+     * @param basefont Name of the basefont
+     * @param cidtype CID type
+     * @param dw default width
+     * @param w array of character widths
+     * @param systemInfo CID system info
+     * @param descriptor CID font descriptor
      */
-    public PDFCIDFont(int number, String basefont, byte cidtype, int dw,
+    public PDFCIDFont(int number, String basefont, CIDFontType cidtype, int dw,
                       PDFWArray w, PDFCIDSystemInfo systemInfo,
                       PDFCIDFontDescriptor descriptor) {
 
         super(number);
 
         this.basefont = basefont;
-        this.cidtype = TYPE_NAMES[(int)cidtype];
+        this.cidtype = cidtype;
         this.dw = new Integer(dw);
         this.w = w;
         this.dw2 = null;
@@ -84,28 +104,33 @@ public class PDFCIDFont extends PDFObject {
     }
 
     /**
-     * set the /DW attribute
+     * Set the /DW attribute
+     * @param dw the default width
      */
     public void setDW(int dw) {
         this.dw = new Integer(dw);
     }
 
     /**
-     * set the /W array
+     * Set the /W array
+     * @param w the width array
      */
     public void setW(PDFWArray w) {
         this.w = w;
     }
 
     /**
-     * set the (two elements) /DW2 array
+     * Set the (two elements) /DW2 array
+     * @param dw2 the default metrics for vertical writing
      */
     public void setDW2(int[] dw2) {
         this.dw2 = dw2;
     }
 
     /**
-     * set the two elements of the /DW2 array
+     * Set the two elements of the /DW2 array
+     * @param posY position vector
+     * @param displacementY displacement vector
      */
     public void setDW2(int posY, int displacementY) {
         this.dw2 = new int[] {
@@ -115,34 +140,53 @@ public class PDFCIDFont extends PDFObject {
 
     /**
      * Set the CMap used as /ToUnicode cmap
+     * @param cmap character map
      */
     public void setCMAP(PDFCMap cmap) {
         this.cmap = cmap;
     }
 
     /**
-     * set the /W2 array
+     * Set the /W2 array
+     * @param w2 array of metrics for vertical writing
      */
     public void setW2(PDFWArray w2) {
         this.w2 = w2;
     }
 
     /**
-     * set the /CIDToGIDMap (to be used only for CIDFontType2)
+     * Set the /CIDToGIDMap (to be used only for CIDFontType2)
+     * @param map mapping information
      */
     public void setCIDMap(PDFStream map) {
         this.cidMap = map;
     }
 
     /**
-     * set the /CIDToGIDMap (to be used only for CIDFontType2) to "Identity"
+     * Set the /CIDToGIDMap (to be used only for CIDFontType2) to "Identity"
      */
     public void setCIDMapIdentity() {
         this.cidMap = null;    // not an error here, simply use the default
     }
 
     /**
-     * produce the PDF representation for the object
+     * Returns the PDF name for a certain CID font type.
+     * @param cidFontType CID font type
+     * @return corresponding PDF name
+     */
+    protected String getPDFNameForCIDFontType(CIDFontType cidFontType) {
+        if (cidFontType == CIDFontType.CIDTYPE0) {
+            return cidFontType.getName();
+        } else if (cidFontType == CIDFontType.CIDTYPE2) {
+            return cidFontType.getName();
+        } else {
+            throw new IllegalArgumentException("Unsupported CID font type: " 
+                        + cidFontType.getName());
+        }
+    }
+
+    /**
+     * Produce the PDF representation for the object
      *
      * @return the PDF
      */
@@ -150,6 +194,10 @@ public class PDFCIDFont extends PDFObject {
         return toPDFString().getBytes();
     }
 
+    /**
+     * Produce the PDF representation for the object
+     * @return the generated code
+     */
     public String toPDFString() {
         StringBuffer p = new StringBuffer();
         p.append(this.number);
@@ -163,7 +211,7 @@ public class PDFCIDFont extends PDFObject {
             p.append(cidMap.referencePDF());
         }
         p.append(" \n/Subtype /");
-        p.append(this.cidtype);
+        p.append(getPDFNameForCIDFontType(this.cidtype));
         p.append("\n");
         p.append(systemInfo.toPDFString());
         p.append("\n/FontDescriptor ");
index afbe717069ba426b3614d823c60af1723f7b9008..4b0f4fa0002ccf50682e6e38571b5b5968e1a19e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * $Id$
- * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
-import org.apache.fop.render.pdf.CIDFont;
-import org.apache.fop.render.pdf.fonts.LazyFont;
+import org.apache.fop.util.StreamUtilities;
 
-import org.apache.fop.layout.FontMetric;
-import org.apache.fop.layout.FontDescriptor;
+import org.apache.fop.fonts.CIDFont;
+import org.apache.fop.fonts.CustomFont;
+import org.apache.fop.fonts.FontDescriptor;
+import org.apache.fop.fonts.FontMetrics;
+import org.apache.fop.fonts.FontType;
+import org.apache.fop.fonts.LazyFont;
+import org.apache.fop.fonts.MultiByteFont;
+import org.apache.fop.fonts.truetype.FontFileReader;
+import org.apache.fop.fonts.truetype.TTFSubSetFile;
+import org.apache.fop.fonts.type1.PFBData;
+import org.apache.fop.fonts.type1.PFBParser;
 
 // Java
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.List;
 import java.util.Map;
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.awt.geom.Rectangle2D;
 
@@ -54,6 +61,11 @@ public class PDFDocument {
      */
     protected static final String PDF_VERSION = "1.4";
 
+    /**
+     * the encoding to use when converting strings to PDF commandos.
+     */
+    public static final String ENCODING = "ISO-8859-1";
+
     /**
      * the current character position
      */
@@ -62,10 +74,10 @@ public class PDFDocument {
     /**
      * the character position of each object
      */
-    protected List location = new ArrayList();
+    protected List location = new java.util.ArrayList();
 
     /** List of objects to write in the trailer */
-    private List trailerObjects = new ArrayList();
+    private List trailerObjects = new java.util.ArrayList();
 
     /**
      * the counter for object numbering
@@ -75,7 +87,7 @@ public class PDFDocument {
     /**
      * the objects themselves
      */
-    protected List objects = new ArrayList();
+    protected List objects = new java.util.ArrayList();
 
     /**
      * character position of xref table
@@ -127,57 +139,57 @@ public class PDFDocument {
      * the XObjects Map.
      * Should be modified (works only for image subtype)
      */
-    protected Map xObjectsMap = new HashMap();
+    protected Map xObjectsMap = new java.util.HashMap();
 
     /**
      * the Font Map.
      */
-    protected Map fontMap = new HashMap();
+    protected Map fontMap = new java.util.HashMap();
 
     /**
      * The filter map.
      */
-    protected Map filterMap = new HashMap();
+    protected Map filterMap = new java.util.HashMap();
 
     /**
      * List of PDFGState objects.
      */
-    protected List gstates = new ArrayList();
+    protected List gstates = new java.util.ArrayList();
 
     /**
      * List of functions.
      */
-    protected List functions = new ArrayList();
+    protected List functions = new java.util.ArrayList();
 
     /**
      * List of shadings.
      */
-    protected List shadings = new ArrayList();
+    protected List shadings = new java.util.ArrayList();
 
     /**
      * List of patterns.
      */
-    protected List patterns = new ArrayList();
+    protected List patterns = new java.util.ArrayList();
 
     /**
      * List of Links.
      */
-    protected List links = new ArrayList();
+    protected List links = new java.util.ArrayList();
 
     /**
      * List of FileSpecs.
      */
-    protected List filespecs = new ArrayList();
+    protected List filespecs = new java.util.ArrayList();
 
     /**
      * List of GoToRemotes.
      */
-    protected List gotoremotes = new ArrayList();
+    protected List gotoremotes = new java.util.ArrayList();
 
     /**
      * List of GoTos.
      */
-    protected List gotos = new ArrayList();
+    protected List gotos = new java.util.ArrayList();
 
 
     /**
@@ -230,7 +242,7 @@ public class PDFDocument {
      *
      * @param map the map of filter lists for each stream type
      */
-    public void setFilterMap(HashMap map) {
+    public void setFilterMap(Map map) {
         filterMap = map;
     }
 
@@ -891,9 +903,9 @@ public class PDFDocument {
         List theCzero;
         List theCone;
         PDFPattern myPattern;
-        PDFColorSpace theColorSpace;
+        //PDFColorSpace theColorSpace;
         double interpolation = (double)1.000;
-        List theFunctions = new ArrayList();
+        List theFunctions = new java.util.ArrayList();
 
         int currentPosition;
         int lastPosition = theColors.size() - 1;
@@ -940,7 +952,7 @@ public class PDFDocument {
             } else {    // if the center x, center y, and radius specifiy
                 // the gradient, then assume the same center x, center y,
                 // and radius of zero for the other necessary component
-                List newCoords = new ArrayList();
+                List newCoords = new java.util.ArrayList();
                 newCoords.add(theCoords.get(0));
                 newCoords.add(theCoords.get(1));
                 newCoords.add(theCoords.get(2));
@@ -1015,7 +1027,7 @@ public class PDFDocument {
      * @return the created /Font object
      */
     public PDFFont makeFont(String fontname, String basefont,
-                            String encoding, FontMetric metrics,
+                            String encoding, FontMetrics metrics,
                             FontDescriptor descriptor) {
         if (fontMap.containsKey(fontname)) {
             return (PDFFont)fontMap.get(fontname);
@@ -1027,22 +1039,17 @@ public class PDFDocument {
          */
         if (descriptor == null) {
             PDFFont font = new PDFFont(++this.objectcount, fontname,
-                                       PDFFont.TYPE1, basefont, encoding);
+                                       FontType.TYPE1, basefont, encoding);
             this.objects.add(font);
             fontMap.put(fontname, font);
             return font;
         } else {
-            byte subtype = PDFFont.TYPE1;
-            if (metrics instanceof org.apache.fop.render.pdf.Font) {
-                subtype =
-                    ((org.apache.fop.render.pdf.Font)metrics).getSubType();
-            }
+            FontType fonttype = metrics.getFontType();
 
-            PDFFontDescriptor pdfdesc = makeFontDescriptor(descriptor,
-                                        subtype);
+            PDFFontDescriptor pdfdesc = makeFontDescriptor(descriptor);
 
             PDFFontNonBase14 font = null;
-            if (subtype == PDFFont.TYPE0) {
+            if (fonttype == FontType.TYPE0) {
                 /*
                  * Temporary commented out - customized CMaps
                  * isn't needed until /ToUnicode support is added
@@ -1056,24 +1063,24 @@ public class PDFDocument {
                  */
                 font =
                     (PDFFontNonBase14)PDFFont.createFont(++this.objectcount,
-                                                         fontname, subtype,
+                                                         fontname, fonttype,
                                                          basefont,
                                                          "Identity-H");
             } else {
 
                 font =
                     (PDFFontNonBase14)PDFFont.createFont(++this.objectcount,
-                                                         fontname, subtype,
+                                                         fontname, fonttype,
                                                          basefont, encoding);
             }
             this.objects.add(font);
 
             font.setDescriptor(pdfdesc);
 
-            if (subtype == PDFFont.TYPE0) {
+            if (fonttype == FontType.TYPE0) {
                 CIDFont cidMetrics;
                 if (metrics instanceof LazyFont) {
-                    cidMetrics = (CIDFont) ((LazyFont) metrics).getRealFont();
+                    cidMetrics = (CIDFont)((LazyFont) metrics).getRealFont();
                 } else {
                     cidMetrics = (CIDFont)metrics;
                 }
@@ -1083,7 +1090,7 @@ public class PDFDocument {
                                          cidMetrics.getSupplement());
                 PDFCIDFont cidFont =
                     new PDFCIDFont(++this.objectcount, basefont,
-                                   cidMetrics.getCidType(),
+                                   cidMetrics.getCIDType(),
                                    cidMetrics.getDefaultWidth(),
                                    cidMetrics.getWidths(), sysInfo,
                                    (PDFCIDFontDescriptor)pdfdesc);
@@ -1091,9 +1098,16 @@ public class PDFDocument {
 
                 ((PDFFontType0)font).setDescendantFonts(cidFont);
             } else {
-                font.setWidthMetrics(metrics.getFirstChar(),
-                                     metrics.getLastChar(),
-                                     makeArray(metrics.getWidths(1)));
+                int firstChar = 0;
+                int lastChar = 255;
+                if (metrics instanceof CustomFont) {
+                    CustomFont cf = (CustomFont)metrics;
+                    firstChar = cf.getFirstChar();
+                    lastChar = cf.getLastChar();
+                }
+                font.setWidthMetrics(firstChar,
+                                     lastChar,
+                                     makeArray(metrics.getWidths()));
             }
 
             fontMap.put(fontname, font);
@@ -1106,21 +1120,20 @@ public class PDFDocument {
     /**
      * make a /FontDescriptor object
      */
-    public PDFFontDescriptor makeFontDescriptor(FontDescriptor desc,
-            byte subtype) {
+    public PDFFontDescriptor makeFontDescriptor(FontDescriptor desc) {
         PDFFontDescriptor font = null;
 
-        if (subtype == PDFFont.TYPE0) {
+        if (desc.getFontType() == FontType.TYPE0) {
             // CID Font
             font = new PDFCIDFontDescriptor(++this.objectcount,
-                                            desc.fontName(),
+                                            desc.getFontName(),
                                             desc.getFontBBox(),
                                             desc.getCapHeight(), desc.getFlags(),
                                             desc.getItalicAngle(),
                                             desc.getStemV(), null);
         } else {
             // Create normal FontDescriptor
-            font = new PDFFontDescriptor(++this.objectcount, desc.fontName(),
+            font = new PDFFontDescriptor(++this.objectcount, desc.getFontName(),
                                          desc.getAscender(),
                                          desc.getDescender(),
                                          desc.getCapHeight(),
@@ -1133,16 +1146,116 @@ public class PDFDocument {
 
         // Check if the font is embeddable
         if (desc.isEmbeddable()) {
-            PDFStream stream = desc.getFontFile(this.objectcount + 1);
+            PDFStream stream = makeFontFile(this.objectcount + 1, desc);
             if (stream != null) {
                 this.objectcount++;
-                font.setFontFile(desc.getSubType(), stream);
+                font.setFontFile(desc.getFontType(), stream);
                 this.objects.add(stream);
             }
         }
         return font;
     }
 
+    protected InputStream resolveURI(String uri) 
+                throws java.io.FileNotFoundException {
+        try {
+            /**@todo Temporary hack to compile, improve later */
+            return new java.net.URL(uri).openStream();
+        } catch (Exception e) {
+            throw new java.io.FileNotFoundException(
+                "URI could not be resolved (" + e.getMessage() + "): " + uri);
+        }
+    }
+
+    /**
+     * Embeds a font.
+     * @param obj PDF object number to use
+     * @param desc FontDescriptor of the font.
+     * @return PDFStream The embedded font file
+     */
+    public PDFStream makeFontFile(int obj, FontDescriptor desc) {
+        if (desc.getFontType() == FontType.OTHER) {
+            throw new IllegalArgumentException("Trying to embed unsupported font type: " + desc.getFontType());
+        } 
+        if (!(desc instanceof CustomFont)) {
+            throw new IllegalArgumentException("FontDescriptor must be instance of CustomFont, but is a " + desc.getClass().getName());
+        }
+        
+        CustomFont font = (CustomFont)desc;
+        
+        InputStream in = null;
+        try {
+            // Get file first
+            if (font.getEmbedFileName() != null) try {
+                in = resolveURI(font.getEmbedFileName());
+            } catch (Exception e) {
+                System.out.println("Failed to embed fontfile: "
+                                   + font.getEmbedFileName());
+            }
+    
+            // Get resource
+            if (in == null && font.getEmbedResourceName() != null) try {
+                in = new java.io.BufferedInputStream(
+                        this.getClass().getResourceAsStream(font.getEmbedResourceName()));
+            } catch (Exception e) {
+                System.out.println("Failed to embed fontresource: "
+                                   + font.getEmbedResourceName());
+            }
+    
+            if (in == null) {
+                return null;
+            } else try {
+                PDFStream embeddedFont;
+                if (desc.getFontType() == FontType.TYPE0) {
+                    MultiByteFont mbfont = (MultiByteFont)font;
+                    FontFileReader reader = new FontFileReader(in);
+                
+                    TTFSubSetFile subset = new TTFSubSetFile();
+        
+                    byte[] subsetFont = subset.readFont(reader, mbfont.getTTCName(), mbfont.getUsedGlyphs());
+                    // Only TrueType CID fonts are supported now
+        
+                    embeddedFont = new PDFTTFStream(obj, subsetFont.length);
+                    ((PDFTTFStream)embeddedFont).setData(subsetFont, subsetFont.length);
+                } else if (desc.getFontType() == FontType.TYPE1) {
+                    PFBParser parser = new PFBParser();
+                    PFBData pfb = parser.parsePFB(in);
+                    embeddedFont = new PDFT1Stream(obj);
+                    ((PDFT1Stream)embeddedFont).setData(pfb);
+                } else {
+                    byte[] file = StreamUtilities.toByteArray(in, 128000);
+                    embeddedFont = new PDFTTFStream(obj, file.length);
+                    ((PDFTTFStream)embeddedFont).setData(file, file.length);
+                }
+                embeddedFont.addFilter("flate");
+                embeddedFont.addFilter("ascii-85");
+                return embeddedFont;
+            } finally {
+                in.close();
+            }
+        } catch (IOException ioe) {
+            //log.error("Failed to embed font [" + obj + "] "
+            //                       + fontName + ": " + ioe.getMessage());
+            return (PDFStream) null;
+        }
+    }
+    
+
+
+/*
+    public PDFStream getFontFile(int i) {
+        PDFStream embeddedFont = null;
+
+
+        return (PDFStream)embeddedFont;
+    }
+
+
+    public PDFStream getFontFile(int i) {
+    }
+
+*/
+    
 
     /**
      * make an Array object (ex. Widths array for a font)
@@ -1157,7 +1270,7 @@ public class PDFDocument {
     /**
      * make an ExtGState for extra graphics options
      */
-    public PDFGState makeGState(HashMap settings, PDFGState current) {
+    public PDFGState makeGState(Map settings, PDFGState current) {
 
         // try to locate a gstate that has all the settings
         // or will inherit from the current gstate
@@ -1285,7 +1398,7 @@ public class PDFDocument {
     public PDFLink makeLink(Rectangle2D rect, String destination,
                             int linkType, float yoffset) {
 
-        PDFLink linkObject;
+        //PDFLink linkObject;
         int index;
 
         PDFLink link = new PDFLink(++this.objectcount, rect);
@@ -1299,12 +1412,12 @@ public class PDFDocument {
                 PDFGoToRemote remote = getGoToPDFAction(destination, null, -1);
                 link.setAction(remote);
             } else if ((index = destination.indexOf(".pdf#page=")) > 0) {
-                String file = destination.substring(0, index + 4);
+                //String file = destination.substring(0, index + 4);
                 int page = Integer.parseInt(destination.substring(index + 10));
                 PDFGoToRemote remote = getGoToPDFAction(destination, null, page);
                 link.setAction(remote);
             } else if ((index = destination.indexOf(".pdf#dest=")) > 0) {
-                String file = destination.substring(0, index + 4);
+                //String file = destination.substring(0, index + 4);
                 String dest = destination.substring(index + 10);
                 PDFGoToRemote remote = getGoToPDFAction(destination, dest, -1);
                 link.setAction(remote);
index 3d1c2ec059ccde73d69d2d29ae7c96e6f9669866..06e2cc3525f405cf90eead707ea4abf84144a281 100644 (file)
@@ -1,57 +1,25 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.FontType;
+
 /**
- * class representing a /Font object.
- *
+ * Class representing a /Font object.
+ * <p>
  * A more complete object expressing the base font name and encoding of a
  * font along with an internal name for the font used within
  * streams of content.
- *
+ * <p>
  * Fonts are specified on page 198 and onwards of the PDF 1.3 spec.
  */
 public class PDFFont extends PDFObject {
 
-    /**
-     * font subtype to be used as parameter to createFont()
-     */
-    public static final byte TYPE0 = 0;
-
-    /**
-     * font subtype to be used as parameter to createFont()
-     */
-    public static final byte TYPE1 = 1;
-
-    /**
-     * font subtype to be used as parameter to createFont()
-     */
-    public static final byte MMTYPE1 = 2;
-
-    /**
-     * font subtype to be used as parameter to createFont()
-     */
-    public static final byte TYPE3 = 3;
-
-    /**
-     * font subtype to be used as parameter to createFont()
-     */
-    public static final byte TRUETYPE = 4;
-
-    /**
-     * font subtype names as output in the PDF
-     */
-    protected static final String[] TYPE_NAMES =
-        new String[]    // take care of the order
-     {
-        "Type0", "Type1", "MMType1", "Type3", "TrueType"
-    };
-
     /**
      * the internal name for the font (eg "F1")
      */
@@ -59,9 +27,9 @@ public class PDFFont extends PDFObject {
 
     /**
      * the font's subtype
-     * (as defined by the constants TYPE0, TYPE1, MMTYPE1, TYPE3, TRUETYPE)
+     * (as defined by the constants FontType: TYPE0, TYPE1, MMTYPE1, TYPE3, TRUETYPE)
      */
-    protected byte subtype;
+    protected FontType subtype;
 
     /**
      * the base font name (eg "Helvetica")
@@ -90,9 +58,8 @@ public class PDFFont extends PDFObject {
      * @param subtype the font's subtype
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      */
-    public PDFFont(int number, String fontname, byte subtype,
+    public PDFFont(int number, String fontname, FontType subtype,
                    String basefont,
                    Object encoding /* , PDFToUnicode mapping */) {
 
@@ -115,27 +82,27 @@ public class PDFFont extends PDFObject {
      * @param subtype the font's subtype
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
+     * @return the generated PDFFont object
      */
     public static PDFFont createFont(int number, String fontname,
-                                     byte subtype, String basefont,
+                                     FontType subtype, String basefont,
                                      Object encoding) {
-        switch (subtype) {
-        case TYPE0:
-            return new PDFFontType0(number, fontname, subtype, basefont,
+        if (subtype == FontType.TYPE0) {
+            return new PDFFontType0(number, fontname, basefont,
                                     encoding);
-        case TYPE1:
-        case MMTYPE1:
-            return new PDFFontType1(number, fontname, subtype, basefont,
+        } else if ((subtype == FontType.TYPE1)
+                || (subtype == FontType.MMTYPE1)) {
+            return new PDFFontType1(number, fontname, basefont,
                                     encoding);
-        /*
-         * case TYPE3 :
-         * return new PDFFontType3(number, fontname, subtype, basefont, encoding);
-         */
-        case TRUETYPE:
-            return new PDFFontTrueType(number, fontname, subtype, basefont,
+        } else if (subtype == FontType.TYPE3) {
+            //return new PDFFontType3(number, fontname, basefont, encoding);
+            return null; //NYI
+        } else if (subtype == FontType.TRUETYPE) {
+            return new PDFFontTrueType(number, fontname, basefont,
                                        encoding);
+        } else {
+            return null;    // should not happend
         }
-        return null;    // should not happend
     }
 
     /**
@@ -151,44 +118,42 @@ public class PDFFont extends PDFObject {
      * @param lastChar the last character code in the font
      * @param widths an array of size (lastChar - firstChar +1)
      * @param descriptor the descriptor for other font's metrics
+     * @return the generated PDFFont object
      */
     public static PDFFont createFont(int number, String fontname,
-                                     byte subtype, String basefont,
+                                     FontType subtype, String basefont,
                                      Object encoding, int firstChar,
                                      int lastChar, PDFArray widths,
                                      PDFFontDescriptor descriptor) {
 
         PDFFontNonBase14 font;
-        switch (subtype) {
-        case TYPE0:
-            font = new PDFFontType0(number, fontname, subtype, basefont,
+        if (subtype == FontType.TYPE0) {
+            font = new PDFFontType0(number, fontname, basefont,
                                     encoding);
             font.setDescriptor(descriptor);
             return font;
-        case TYPE1:
-        case MMTYPE1:
-            font = new PDFFontType1(number, fontname, subtype, basefont,
+        } else if ((subtype == FontType.TYPE1) 
+                || (subtype == FontType.MMTYPE1)) {
+            font = new PDFFontType1(number, fontname, basefont,
                                     encoding);
             font.setWidthMetrics(firstChar, lastChar, widths);
             font.setDescriptor(descriptor);
             return font;
-        case TYPE3:
-            return null;    // should not happend
-
-        case TRUETYPE:
-            font = new PDFFontTrueType(number, fontname, subtype, basefont,
+        } else if (subtype == FontType.TYPE3) {
+            return null; //NYI, should not happend
+        } else if (subtype == FontType.TRUETYPE) {
+            font = new PDFFontTrueType(number, fontname, basefont,
                                        encoding);
             font.setWidthMetrics(firstChar, lastChar, widths);
             font.setDescriptor(descriptor);
             return font;
-
+        } else {
+            return null;    // should not happend
         }
-        return null;    // should not happend
     }
 
     /**
      * get the internal name used for this font
-     *
      * @return the internal name
      */
     public String getName() {
@@ -196,7 +161,28 @@ public class PDFFont extends PDFObject {
     }
 
     /**
-     * produce the PDF representation for the object
+     * Returns the PDF name for a certain font type.
+     * @param fontType font type
+     * @return String corresponding PDF name
+     */
+    protected String getPDFNameForFontType(FontType fontType) {
+        if (fontType == FontType.TYPE0) {
+            return fontType.getName();
+        } else if (fontType == FontType.TYPE1) {
+            return fontType.getName();
+        } else if (fontType == FontType.MMTYPE1) {
+            return fontType.getName();
+        } else if (fontType == FontType.TYPE3) {
+            return fontType.getName();
+        } else if (fontType == FontType.TRUETYPE) {
+            return fontType.getName();
+        } else {
+            throw new IllegalArgumentException("Unsupported font type: " + fontType.getName());
+        }
+    }
+
+    /**
+     * Produce the PDF representation for the object
      *
      * @return the PDF
      */
@@ -204,7 +190,7 @@ public class PDFFont extends PDFObject {
         StringBuffer p = new StringBuffer();
         p.append(this.number + " " + this.generation
                  + " obj\n<< /Type /Font\n/Subtype /"
-                 + TYPE_NAMES[this.subtype] + "\n/Name /" + this.fontname
+                 + getPDFNameForFontType(this.subtype) + "\n/Name /" + this.fontname
                  + "\n/BaseFont /" + this.basefont);
         if (encoding != null) {
             p.append("\n/Encoding ");
@@ -222,12 +208,14 @@ public class PDFFont extends PDFObject {
     }
 
     /**
-     * fill in the specifics for the font's subtype.
-     *
-     * the given buffer already contains the fields common to all font types.
+     * This method is called to receive the specifics for the font's subtype.
+     * <p>
+     * The given buffer already contains the fields common to all font types.
      *
-     * @param begin the buffer to be completed with the type specific fields
+     * @param target the buffer to be completed with the type specific fields
      */
-    protected void fillInPDF(StringBuffer begin) {}
+    protected void fillInPDF(StringBuffer target) {
+        //nop
+    }
 
 }
index 203879b35ac37831eb0d9c978e0a5b8d3f4cebcf..ba1198462ec513fa1c39c351770213a53fd04032 100644 (file)
@@ -1,42 +1,44 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.FontType;
+
 /**
- * class representing a font descriptor.
- *
+ * Class representing a font descriptor (/FontDescriptor object).
+ * <p>
  * Font descriptors are specified on page 222 and onwards of the PDF 1.3 spec.
  */
 public class PDFFontDescriptor extends PDFObject {
 
     // Required fields
-    protected int ascent;
-    protected int capHeight;
-    protected int descent;
-    protected int flags;
-    protected PDFRectangle fontBBox;
-    protected String basefont;    // PDF-spec: FontName
-    protected int italicAngle;
-    protected int stemV;
+    private int ascent;
+    private int capHeight;
+    private int descent;
+    private int flags;
+    private PDFRectangle fontBBox;
+    private String basefont;    // PDF-spec: FontName
+    private int italicAngle;
+    private int stemV;
     // Optional fields
-    protected int stemH = 0;
-    protected int xHeight = 0;
-    protected int leading = 0;
-    protected int avgWidth = 0;
-    protected int maxWidth = 0;
-    protected int missingWidth = 0;
-    protected PDFStream fontfile;
-    // protected String charSet = null;
+    private int stemH = 0;
+    private int xHeight = 0;
+    private int leading = 0;
+    private int avgWidth = 0;
+    private int maxWidth = 0;
+    private int missingWidth = 0;
+    private PDFStream fontfile;
+    // private String charSet = null;
 
-    protected byte subtype;
+    private FontType subtype;
 
     /**
-     * create the /FontDescriptor object
+     * Create the /FontDescriptor object
      *
      * @param number the object's number
      * @param ascent the maximum height above the baseline
@@ -68,7 +70,18 @@ public class PDFFontDescriptor extends PDFObject {
     }
 
     /**
-     * set the optional metrics
+     * Set the optional metrics.
+     * @param avgWidth The average width of characters in this font. 
+     * The default value is 0.
+     * @param maxWidth The maximum width of characters in this font. 
+     * The default value is 0.
+     * @param missingWidth missing width
+     * @param leading the desired spacing between lines of text. 
+     * The default value is 0.
+     * @param stemH The vertical width of the dominant horizontal stems of 
+     * glyphs in the font. The default value is 0.
+     * @param xHeight The y-coordinate of the top of flat non-ascending 
+     * lowercase letters, measured from the baseline. The default value is 0.
      */
     public void setMetrics(int avgWidth, int maxWidth, int missingWidth,
                            int leading, int stemH, int xHeight) {
@@ -81,12 +94,12 @@ public class PDFFontDescriptor extends PDFObject {
     }
 
     /**
-     * set the optional font file stream
+     * Set the optional font file stream
      *
      * @param subtype the font type defined in the font stream
      * @param fontfile the stream containing an embedded font
      */
-    public void setFontFile(byte subtype, PDFStream fontfile) {
+    public void setFontFile(FontType subtype, PDFStream fontfile) {
         this.subtype = subtype;
         this.fontfile = fontfile;
     }
@@ -94,7 +107,7 @@ public class PDFFontDescriptor extends PDFObject {
     // public void setCharSet(){}//for subset fonts
 
     /**
-     * produce the PDF representation for the object
+     * Produce the PDF representation for the object
      *
      * @return the PDF
      */
@@ -143,17 +156,9 @@ public class PDFFontDescriptor extends PDFObject {
             p.append(leading);
         }
         if (fontfile != null) {
-            switch (subtype) {
-            case PDFFont.TYPE1:
+            if (subtype == FontType.TYPE1) {
                 p.append("\n/FontFile ");
-                break;
-            case PDFFont.TRUETYPE:
-                p.append("\n/FontFile2 ");
-                break;
-            case PDFFont.TYPE0:
-                p.append("\n/FontFile2 ");
-                break;
-            default:
+            } else {
                 p.append("\n/FontFile2 ");
             }
             p.append(fontfile.referencePDF());
@@ -166,12 +171,14 @@ public class PDFFontDescriptor extends PDFObject {
     }
 
     /**
-     * fill in the specifics for the font's descriptor.
-     *
-     * the given buffer already contains the fields common to all descriptors.
+     * Fill in the specifics for the font's descriptor.
+     * <p>
+     * The given buffer already contains the fields common to all descriptors.
      *
      * @param begin the buffer to be completed with the specific fields
      */
-    protected void fillInPDF(StringBuffer begin) {}
+    protected void fillInPDF(StringBuffer begin) {
+        //nop
+    }
 
 }
index ac4abb2613cb6fc0e9ae0b7b36543c657403217b..60ea472e4c91e57dab38776b7005621c8433bcb7 100644 (file)
@@ -1,13 +1,13 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
-// Java
+import org.apache.fop.fonts.FontType;
 
 /**
  * A common ancestor for Type1, TrueType, MMType1 and Type3 fonts
@@ -36,18 +36,17 @@ public abstract class PDFFontNonBase14 extends PDFFont {
     protected PDFFontDescriptor descriptor;
 
     /**
-     * create the /Font object
+     * Create the /Font object
      *
      * @param number the object's number
      * @param fontname the internal name for the font
      * @param subtype the font's subtype
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      */
-    public PDFFontNonBase14(int number, String fontname, byte subtype,
+    public PDFFontNonBase14(int number, String fontname, FontType subtype,
                             String basefont,
-                            Object encoding /* , PDFToUnicode mapping */) {
+                            Object encoding) {
 
         /* generic creation of PDF object */
         super(number, fontname, subtype, basefont, encoding);
@@ -56,7 +55,7 @@ public abstract class PDFFontNonBase14 extends PDFFont {
     }
 
     /**
-     * set the width metrics for the font
+     * Set the width metrics for the font
      *
      * @param firstChar the first character code in the font
      * @param lastChar the last character code in the font
@@ -71,7 +70,7 @@ public abstract class PDFFontNonBase14 extends PDFFont {
     }
 
     /**
-     * set the font descriptor (unused for the Type3 fonts)
+     * Set the font descriptor (unused for the Type3 fonts)
      *
      * @param descriptor the descriptor for other font's metrics
      */
@@ -80,18 +79,18 @@ public abstract class PDFFontNonBase14 extends PDFFont {
     }
 
     /**
-     * fill in the specifics for the font's subtype
+     * @see org.apache.fop.pdf.PDFFont#fillInPDF(StringBuffer)
      */
-    protected void fillInPDF(StringBuffer p) {
-        p.append("\n/FirstChar ");
-        p.append(firstChar);
-        p.append("\n/LastChar ");
-        p.append(lastChar);
-        p.append("\n/Widths ");
-        p.append(this.widths.referencePDF());
+    protected void fillInPDF(StringBuffer target) {
+        target.append("\n/FirstChar ");
+        target.append(firstChar);
+        target.append("\n/LastChar ");
+        target.append(lastChar);
+        target.append("\n/Widths ");
+        target.append(this.widths.referencePDF());
         if (descriptor != null) {
-            p.append("\n/FontDescriptor ");
-            p.append(this.descriptor.referencePDF());
+            target.append("\n/FontDescriptor ");
+            target.append(this.descriptor.referencePDF());
         }
     }
 
index 444670d38ce924fd8259a9dbbd4a08be609d7b4c..c8af61a39015de3ee45e9503f0a4825daf44958f 100644 (file)
@@ -1,15 +1,17 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.FontType;
+
 /**
- * class representing a TrueType font.
- *
+ * Class representing a TrueType font.
+ * <p>
  * In fact everything already done in the superclass.
  * Must only define the not default constructor.
  */
@@ -20,17 +22,15 @@ public class PDFFontTrueType extends PDFFontNonBase14 {
      *
      * @param number the object's number
      * @param fontname the internal name for the font
-     * @param subtype the font's subtype (PDFFont.TRUETYPE)
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      */
-    public PDFFontTrueType(int number, String fontname, byte subtype,
+    public PDFFontTrueType(int number, String fontname, 
                            String basefont,
-                           Object encoding /* , PDFToUnicode mapping */) {
+                           Object encoding) {
 
         /* generic creation of PDF object */
-        super(number, fontname, subtype, basefont, encoding /* , mapping */);
+        super(number, fontname, FontType.TRUETYPE, basefont, encoding /* , mapping */);
     }
 
 }
index 1044ade09b035af0c4b995aed7a5ea1f4c69662a..360e7752d89e04e603e8be731bdff820a7c358d9 100644 (file)
@@ -1,42 +1,45 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.FontType;
+
 /**
- * class representing a Type0 font.
- *
+ * Class representing a Type0 font.
+ * <p>
  * Type0 fonts are specified on page 208 and onwards of the PDF 1.3 spec.
  */
 public class PDFFontType0 extends PDFFontNonBase14 {
 
     /**
-     * this should be an array of CIDFont but only the first one is used
+     * This should be an array of CIDFont but only the first one is used
      */
     protected PDFCIDFont descendantFonts;
 
+    /**
+     * The character map
+     */
     protected PDFCMap cmap;
 
     /**
-     * create the /Font object
+     * Create the /Font object
      *
      * @param number the object's number
      * @param fontname the internal name for the font
-     * @param subtype the font's subtype (PDFFont.TYPE0)
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      */
-    public PDFFontType0(int number, String fontname, byte subtype,
+    public PDFFontType0(int number, String fontname, 
                         String basefont,
-                        Object encoding /* , PDFToUnicode mapping */) {
+                        Object encoding) {
 
         /* generic creation of PDF object */
-        super(number, fontname, subtype, basefont, encoding /* , mapping */);
+        super(number, fontname, FontType.TYPE0, basefont, encoding /* , mapping */);
 
         /* set fields using paramaters */
         this.descendantFonts = null;
@@ -44,51 +47,52 @@ public class PDFFontType0 extends PDFFontNonBase14 {
     }
 
     /**
-     * create the /Font object
+     * Create the /Font object
      *
      * @param number the object's number
      * @param fontname the internal name for the font
-     * @param subtype the font's subtype (PDFFont.TYPE0)
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      * @param descendantFonts the CIDFont upon which this font is based
      */
-    public PDFFontType0(int number, String fontname, byte subtype,
+    public PDFFontType0(int number, String fontname, 
                         String basefont,
-                        Object encoding /* , PDFToUnicode mapping */,
+                        Object encoding
                         PDFCIDFont descendantFonts) {
 
         /* generic creation of PDF object */
-        super(number, fontname, subtype, basefont, encoding /* , mapping */);
+        super(number, fontname, FontType.TYPE0, basefont, encoding /* , mapping */);
 
         /* set fields using paramaters */
         this.descendantFonts = descendantFonts;
     }
 
     /**
-     * set the descendant font
-     *
+     * Set the descendant font
      * @param descendantFonts the CIDFont upon which this font is based
      */
     public void setDescendantFonts(PDFCIDFont descendantFonts) {
         this.descendantFonts = descendantFonts;
     }
 
+    /**
+     * Sets the character map
+     * @param cmap the character map
+     */
     public void setCMAP(PDFCMap cmap) {
         this.cmap = cmap;
     }
 
     /**
-     * fill in the specifics for the font's subtype
+     * @see org.apache.fop.pdf.PDFFont#fillInPDF(StringBuffer)
      */
-    protected void fillInPDF(StringBuffer p) {
+    protected void fillInPDF(StringBuffer target) {
         if (descendantFonts != null) {
-            p.append("\n/DescendantFonts [ "
+            target.append("\n/DescendantFonts [ "
                      + this.descendantFonts.referencePDF() + " ] ");
         }
         if (cmap != null) {
-            p.append("\n/ToUnicode " + cmap.referencePDF());
+            target.append("\n/ToUnicode " + cmap.referencePDF());
         }
     }
 
index 2a98c131d8eaf699f85c9f03566cf625daac33c6..f56f95a5283868375320d919b8c087659862a684 100644 (file)
@@ -1,39 +1,40 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.FontType;
+
 /**
- * class representing a Type1 or MMType1 font (not necessary for the base 14).
- *
+ * Class representing a Type1 or MMType1 font (not necessary for the base 14).
+ * <p>
  * Type1 fonts are specified on page 201 and onwards of the PDF 1.3 spec.
+ * <br>
  * MMType1 fonts are specified on page 205 and onwards of the PDF 1.3 spec.
- *
+ * <p>
  * In fact everything already done in the superclass.
  * Must only define the not default constructor.
  */
 public class PDFFontType1 extends PDFFontNonBase14 {
 
     /**
-     * create the /Font object
+     * Create the /Font object
      *
      * @param number the object's number
      * @param fontname the internal name for the font
-     * @param subtype the font's subtype (PDFFont.TYPE1 or PDFFont.MMTYPE1)
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      */
-    public PDFFontType1(int number, String fontname, byte subtype,
+    public PDFFontType1(int number, String fontname, 
                         String basefont,
-                        Object encoding /* , PDFToUnicode mapping */) {
+                        Object encoding) {
 
         /* generic creation of PDF object */
-        super(number, fontname, subtype, basefont, encoding);
+        super(number, fontname, FontType.TYPE1, basefont, encoding);
     }
 
 }
index dc1173d46e8bd4c8f456d07d0fcc22a2425c9c95..7b8c71bc354a7df4146637bd6c1c2c0d680c2abb 100644 (file)
@@ -1,19 +1,20 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import org.apache.fop.fonts.FontType;
+
 /**
- * class representing a Type3 font.
- *
- * <p><b>CAUTION: this is not yet fully implemented!!!!!!!</b>
+ * Class representing a Type3 font.
+ * <p>
+ * <b>CAUTION: this is not yet fully implemented!!!!!!!</b>
  * the /CharProcs is still missing its <code>toPDF()</code> method.
- * </p>
- *
+ * <p>
  * Type3 fonts are specified on page 206 and onwards of the PDF 1.3 spec.
  */
 public class PDFFontType3 extends PDFFontNonBase14 {
@@ -39,21 +40,19 @@ public class PDFFontType3 extends PDFFontNonBase14 {
     protected PDFResources resources;
 
     /**
-     * create the /Font object
+     * Create the /Font object
      *
      * @param number the object's number
      * @param fontname the internal name for the font
-     * @param subtype the font's subtype (PDFFont.TYPE3)
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      */
-    public PDFFontType3(int number, String fontname, byte subtype,
+    public PDFFontType3(int number, String fontname, 
                         String basefont,
-                        Object encoding /* , PDFToUnicode mapping */) {
+                        Object encoding) {
 
         /* generic creation of PDF object */
-        super(number, fontname, subtype, basefont, encoding /* , mapping */);
+        super(number, fontname, FontType.TYPE3, basefont, encoding /* , mapping */);
 
         this.fontBBox = null;
         this.fontMatrix = null;
@@ -61,26 +60,24 @@ public class PDFFontType3 extends PDFFontNonBase14 {
     }
 
     /**
-     * create the /Font object
+     * Create the /Font object
      *
      * @param number the object's number
      * @param fontname the internal name for the font
-     * @param subtype the font's subtype (PDFFont.TYPE3)
      * @param basefont the base font name
      * @param encoding the character encoding schema used by the font
-     * @param mapping the Unicode mapping mechanism
      * @param fontBBox the font's bounding box
      * @param fontMatrix the font's transformation matrix
      * @param charProcs the glyphs' definitions
      */
-    public PDFFontType3(int number, String fontname, byte subtype,
+    public PDFFontType3(int number, String fontname, 
                         String basefont,
-                        Object encoding /* , PDFToUnicode mapping */,
+                        Object encoding,
                         PDFRectangle fontBBox, PDFArray fontMatrix,
                         PDFCharProcs charProcs) {
 
         /* generic creation of PDF object */
-        super(number, fontname, subtype, basefont, encoding /* , mapping */);
+        super(number, fontname, FontType.TYPE3, basefont, encoding /* , mapping */);
 
         this.fontBBox = fontBBox;
         this.fontMatrix = fontMatrix;
@@ -88,7 +85,7 @@ public class PDFFontType3 extends PDFFontNonBase14 {
     }
 
     /**
-     * set the font's bounding box
+     * Set the font's bounding box
      *
      * @param bbox bounding box for the font
      */
@@ -97,7 +94,7 @@ public class PDFFontType3 extends PDFFontNonBase14 {
     }
 
     /**
-     * set the font's transformation matrix
+     * Set the font's transformation matrix
      *
      * @param matrix the transformation matrix for the font
      */
@@ -106,7 +103,8 @@ public class PDFFontType3 extends PDFFontNonBase14 {
     }
 
     /**
-     * set the glyphs' definitions.
+     * Set the glyphs' definitions.
+     * <p>
      * The /CharProcs object needs to be registered in the document's resources.
      *
      * @param chars the glyphs' dictionary
@@ -116,24 +114,20 @@ public class PDFFontType3 extends PDFFontNonBase14 {
     }
 
     /**
-     * fill in the specifics for the font's subtype.
-     *
-     * the given buffer already contains the fields common to all font types.
-     *
-     * @param p the buffer to be completed with the type specific fields
+     * @see org.apache.fop.pdf.PDFFont#fillInPDF(StringBuffer)
      */
-    protected void fillInPDF(StringBuffer p) {
+    protected void fillInPDF(StringBuffer target) {
         if (fontBBox != null) {
-            p.append("\n/FontBBox ");
-            p.append(fontBBox.toPDF());
+            target.append("\n/FontBBox ");
+            target.append(fontBBox.toPDF());
         }
         if (fontMatrix != null) {
-            p.append("\n/FontMatrix ");
-            p.append(fontMatrix.toPDF());
+            target.append("\n/FontMatrix ");
+            target.append(fontMatrix.toPDF());
         }
         if (charProcs != null) {
-            p.append("\n/CharProcs ");
-            p.append(charProcs.referencePDF());
+            target.append("\n/CharProcs ");
+            target.append(charProcs.referencePDF());
         }
     }
 
index cc5766c719df22a0d0f6e827380e490e58df4fcf..6d021d66f6559b9a3057b872ca49ce3a6a995d0a 100644 (file)
@@ -1,52 +1,82 @@
 /*
  * $Id$
- * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
-import java.util.HashMap;
+import java.util.Map;
 import java.util.Iterator;
 
 /**
- * class representing a /ExtGState object.
- *
+ * Class representing a /ExtGState object.
  */
 public class PDFGState extends PDFObject {
-    public static final String LW = "lw";
-    public static final String LC = "lc";
-    public static final String LJ = "lj";
-    public static final String ML = "ml";
-    public static final String D = "d";
-    public static final String RI = "ri";
-    public static final String OP = "OP";
-    public static final String op = "op";
-    public static final String OPM = "opm";
-    public static final String Font = "font";
-    public static final String BG = "bg";
-    public static final String BG2 = "bg2";
-    public static final String UCR = "ucr";
-    public static final String UCR2 = "ucr2";
-    public static final String TR = "tr";
-    public static final String TR2 = "tr2";
-    public static final String HT = "ht";
-    public static final String FL = "fl";
-    public static final String SM = "sm";
-    public static final String SA = "sa";
-    public static final String BM = "bm";
-    public static final String SMask = "smask";
-    public static final String CA = "CA";
-    public static final String ca = "ca";
-    public static final String AIS = "ais";
-    public static final String TK = "tk";
 
+    /** Line width (LW) */
+    public static final String GSTATE_LINE_WIDTH          = "LW";
+    /** Line cap (LC) */
+    public static final String GSTATE_LINE_CAP            = "LC";
+    /** Line join (LJ) */
+    public static final String GSTATE_LINE_JOIN           = "LJ";
+    /** Miter limit (ML) */
+    public static final String GSTATE_MITER_LIMIT         = "ML";
+    /** Dash pattern (D) */
+    public static final String GSTATE_DASH_PATTERN        = "D";
+    /** Rendering intent (RI) */
+    public static final String GSTATE_RENDERING_INTENT    = "RI";
+    /** Overprint for stroke (OP) */
+    public static final String GSTATE_OVERPRINT_STROKE    = "OP";
+    /** Overprint for fill (op) */
+    public static final String GSTATE_OVERPRINT_FILL      = "op";
+    /** Overprint mode (OPM) */
+    public static final String GSTATE_OVERPRINT_MODE      = "OPM";
+    /** Font (Font) */
+    public static final String GSTATE_FONT                = "Font";
+    /** Black generation (BG) */
+    public static final String GSTATE_BLACK_GENERATION    = "BG";
+    /** Black generation with default (BG2) */
+    public static final String GSTATE_BLACK_GENERATION2   = "BG2";
+    /** Undercolor removal function (UCR) */
+    public static final String GSTATE_UNDERCOLOR_REMOVAL  = "UCR";
+    /** Undercolor removal function with default (UCR2) */
+    public static final String GSTATE_UNDERCOLOR_REMOVAL2 = "UCR2";
+    /** Transfer function (TR) */
+    public static final String GSTATE_TRANSFER_FUNCTION   = "TR";
+    /** Transfer function with default (TR2) */
+    public static final String GSTATE_TRANSFER_FUNCTION2  = "TR2";
+    /** Halftone dictionary or stream (HT) */
+    public static final String GSTATE_HALFTONE_DICT       = "HT";
+    /** Halftone phase (HTP, does not show up anymore in PDF 1.4)*/
+    public static final String GSTATE_HALFTONE_PHASE      = "HTP";
+    /** Flatness (FL) */
+    public static final String GSTATE_FLATNESS            = "FL";
+    /** Smoothness (SM) */
+    public static final String GSTATE_SMOOTHNESS          = "SM";
+    /** Strike adjustment (SA) */
+    public static final String GSTATE_STRIKE_ADJ          = "SA";
+    /** Blend mode (BM, PDF 1.4) */
+    public static final String GSTATE_BLEND_MODE          = "BM";
+    /** Soft mask (SMask, PDF 1.4) */
+    public static final String GSTATE_SOFT_MASK           = "SMask";
+    /** Stroking Alpha (CA, PDF 1.4) */
+    public static final String GSTATE_ALPHA_STROKE        = "CA";
+    /** Nonstroking Alpha (ca, PDF 1.4) */
+    public static final String GSTATE_ALPHA_NONSTROKE     = "ca";
+    /** Alpha Source Flag (AIS, PDF 1.4) */
+    public static final String GSTATE_ALPHA_SOURCE_FLAG   = "AIS";
+    /** Text Knockout Flag (TK, PDF 1.4) */
+    public static final String GSTATE_TEXT_KNOCKOUT       = "TK";
+    
+
+    /** Default GState object */
     public static final PDFGState DEFAULT;
 
     static {
         DEFAULT = new PDFGState(0);
-        HashMap vals = DEFAULT.values;
+        Map vals = DEFAULT.values;
         /*vals.put(LW, new Float(1.0));
         vals.put(LC, new Integer(0));
         vals.put(LJ, new Integer(0));
@@ -57,55 +87,71 @@ public class PDFGState extends PDFObject {
         vals.put(op, Boolean.FALSE);
         vals.put(OPM, new Integer(1));
         vals.put(Font, "");*/
-        vals.put(CA, new Float(1.0));
-        vals.put(ca, new Float(1.0));
+        
+        vals.put(GSTATE_ALPHA_STROKE, new Float(1.0));
+        vals.put(GSTATE_ALPHA_NONSTROKE, new Float(1.0));
     }
 
-    HashMap values = new HashMap();
+    private Map values = new java.util.HashMap();
 
     /**
-     * create a /ExtGState object.
+     * Create a /ExtGState object.
      *
      * @param number the object's number
-     * @param pageReference the pageReference represented by this object
      */
     public PDFGState(int number) {
-
         /* generic creation of object */
         super(number);
-
     }
 
+    /**
+     * Returns the name of this object
+     * @return the name
+     */
     public String getName() {
         return "GS" + this.number;
     }
 
+    /**
+     * Sets the alpha value.
+     * @param val alpha value (0.0 - 1.0)
+     * @param fill True if alpha should be set for non-stroking operations, 
+     * False if for stroking operations
+     */
     public void setAlpha(float val, boolean fill) {
         if (fill) {
-            values.put(ca, new Float(val));
+            values.put(GSTATE_ALPHA_NONSTROKE, new Float(val));
         } else {
-            values.put(CA, new Float(val));
+            values.put(GSTATE_ALPHA_STROKE, new Float(val));
         }
     }
 
+    /**
+     * Adds all values from another GState object to this one.
+     * @param state source object to copy from
+     */
     public void addValues(PDFGState state) {
         values.putAll(state.values);
     }
 
-    public void addValues(HashMap vals) {
+    /**
+     * Adds all values from a Map to this object.
+     * @param vals source object to copy from
+     */
+    public void addValues(Map vals) {
         values.putAll(vals);
     }
 
     /**
-     * represent the object in PDF
+     * Represent the object in PDF.
      *
      * @return the PDF string
      */
     public byte[] toPDF() {
         StringBuffer sb = new StringBuffer(this.number + " " + this.generation
                               + " obj\n<<\n/Type /ExtGState\n");
-        appendVal(sb, ca);
-        appendVal(sb, CA);
+        appendVal(sb, GSTATE_ALPHA_NONSTROKE);
+        appendVal(sb, GSTATE_ALPHA_STROKE);
 
         sb.append(">>\nendobj\n");
         return sb.toString().getBytes();
@@ -128,6 +174,9 @@ public class PDFGState extends PDFObject {
      * endobj
      */
 
+    /**
+     * @see java.lang.Object#equals(Object)
+     */
     public boolean equals(Object obj) {
         if (obj == this) {
             return true;
@@ -135,12 +184,12 @@ public class PDFGState extends PDFObject {
         if (!(obj instanceof PDFGState)) {
             return false;
         }
-        HashMap vals1 = values;
-        HashMap vals2 = ((PDFGState)obj).values;
+        Map vals1 = values;
+        Map vals2 = ((PDFGState)obj).values;
         if (vals1.size() != vals2.size()) {
             return false;
         }
-        for(Iterator iter = vals1.keySet().iterator(); iter.hasNext();) {
+        for (Iterator iter = vals1.keySet().iterator(); iter.hasNext();) {
             Object str = iter.next();
             Object obj1 = vals1.get(str);
             if (!obj1.equals(vals2.get(str))) {
index 9e61487cfa4e541252e2c137db54b7ac844a5c2b..bb49bf98b40e0a3639833eb68150fa92aaef6071 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * $Id$
- * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
@@ -9,25 +9,40 @@ package org.apache.fop.pdf;
 
 import java.awt.color.ICC_Profile;
 
+/**
+ * Special PDFStream for ICC profiles (color profiles).
+ */
 public class PDFICCStream extends PDFStream {
+    
     private int origLength;
     private int len1, len3;
 
     private ICC_Profile cp;
     private PDFColorSpace pdfColorSpace;
 
-    public void setColorSpace(ICC_Profile cp, PDFColorSpace alt) {
-        this.cp = cp;
-        pdfColorSpace = alt;
-    }
-
+    /**
+     * @see org.apache.fop.pdf.PDFObject#PDFObject(int)
+     */
     public PDFICCStream(int num) {
         super(num);
         cp = null;
     }
 
-        // overload the base object method so we don't have to copy
-        // byte arrays around so much
+    /**
+     * Sets the color space to encode in PDF.
+     * @param cp the ICC profile
+     * @param alt the PDF color space
+     */
+    public void setColorSpace(ICC_Profile cp, PDFColorSpace alt) {
+        this.cp = cp;
+        pdfColorSpace = alt;
+    }
+
+    /**
+     * overload the base object method so we don't have to copy
+     * byte arrays around so much
+     * @see org.apache.fop.pdf.PDFObject#output(OutputStream)
+     */
     protected int output(java.io.OutputStream stream)
         throws java.io.IOException {
 
@@ -43,7 +58,7 @@ public class PDFICCStream extends PDFStream {
             pb.append("/Alternate /").append(pdfColorSpace.getColorSpacePDFString()).append(" ");
         }
 
-        pb.append("/Length ").append((_data.getSize() + 1)).append(" ").append(filterEntry);
+        pb.append("/Length ").append((data.getSize() + 1)).append(" ").append(filterEntry);
         pb.append(" >>\n");
         byte[] p = pb.toString().getBytes();
         stream.write(p);
index 2ea9e9f51d4d38cfaf4c02842385075f85dd252c..c469d969d66c741627dc61ea1e0a718f41a9278c 100644 (file)
@@ -1,69 +1,74 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
-import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
-import java.util.ArrayList;
-import java.util.Enumeration;
 
 /**
- * class representing a PDF stream.
- *
+ * Class representing a PDF stream.
+ * <p>
  * A derivative of the PDF Object, a PDF Stream has not only a dictionary
  * but a stream of PDF commands. The stream of commands is where the real
  * work is done, the dictionary just provides information like the stream
  * length.
  */
 public class PDFStream extends PDFObject {
+    
+    /** Key for the default filter */
     public static final String DEFAULT_FILTER = "default";
+    /** Key for the filter used for normal content*/
     public static final String CONTENT_FILTER = "content";
+    /** Key for the filter used for images */
     public static final String IMAGE_FILTER = "image";
+    /** Key for the filter used for JPEG images */
     public static final String JPEG_FILTER = "jpeg";
+    /** Key for the filter used for fonts */
     public static final String FONT_FILTER = "font";
 
     /**
-     * the stream of PDF commands
+     * The stream of PDF commands
      */
-    protected StreamCache _data;
+    protected StreamCache data;
 
     /**
-     * the filters that should be applied
+     * The filters that should be applied
      */
-    private List _filters;
+    private List filters;
 
     /**
-     * create an empty stream object
+     * Create an empty stream object
      *
      * @param number the object's number
      */
     public PDFStream(int number) {
         super(number);
         try {
-            _data = StreamCache.createStreamCache();
+            data = StreamCache.createStreamCache();
         } catch (IOException ex) {
+            /**@todo Log with Logger */
             ex.printStackTrace();
         }
-        _filters = new ArrayList();
+        filters = new java.util.ArrayList();
     }
 
     /**
-     * append data to the stream
+     * Append data to the stream
      *
      * @param s the string of PDF to add
      */
     public void add(String s) {
         try {
-            _data.getOutputStream().write(s.getBytes());
+            data.getOutputStream().write(s.getBytes());
         } catch (IOException ex) {
+            /**@todo Log with Logger */
             ex.printStackTrace();
         }
 
@@ -75,14 +80,19 @@ public class PDFStream extends PDFObject {
      * new instance of the particular filter of choice. The applied
      * flag in the filter is marked true after it has been applied to the
      * data.
+     * @param filter filter to add
      */
     public void addFilter(PDFFilter filter) {
         if (filter != null) {
-            _filters.add(filter);
+            filters.add(filter);
         }
 
     }
 
+    /**
+     * Add a filter for compression of the stream by name.
+     * @param filterType name of the filter to add
+     */
     public void addFilter(String filterType) {
         if (filterType == null) {
             return;
@@ -96,17 +106,22 @@ public class PDFStream extends PDFObject {
         } else if (filterType.equals("")) {
             return;
         } else {
-            //log.error("Unsupported filter type in stream-filter-list: "
-            //                       + filterType);
+            throw new IllegalArgumentException(
+                "Unsupported filter type in stream-filter-list: " + filterType);
         }
     }
 
+    /**
+     * Adds the default filters to this stream.
+     * @param filters Map of filters
+     * @param type which filter list to modify
+     */
     public void addDefaultFilters(Map filters, String type) {
         List filterset = (List)filters.get(type);
-        if(filterset == null) {
+        if (filterset == null) {
             filterset = (List)filters.get(DEFAULT_FILTER);
         }
-        if(filterset == null || filterset.size() == 0) {
+        if (filterset == null || filterset.size() == 0) {
             // built-in default to flate
             addFilter(new FlateFilter());
         } else {
@@ -118,7 +133,7 @@ public class PDFStream extends PDFObject {
     }
 
     /**
-     * append an array of xRGB pixels, ASCII Hex Encoding it first
+     * Append an array of xRGB pixels, ASCII Hex Encoding it first
      *
      * @param pixels the area of pixels
      * @param width the width of the image in pixels
@@ -133,30 +148,35 @@ public class PDFStream extends PDFObject {
                     int g = (p >> 8) & 0xFF;
                     int b = (p) & 0xFF;
                     if (r < 16) {
-                        _data.getOutputStream().write('0');
+                        data.getOutputStream().write('0');
                     }
-                    _data.getOutputStream().write(Integer.toHexString(r).getBytes());
+                    data.getOutputStream().write(Integer.toHexString(r).getBytes());
                     if (g < 16) {
-                        _data.getOutputStream().write('0');
+                        data.getOutputStream().write('0');
                     }
-                    _data.getOutputStream().write(Integer.toHexString(g).getBytes());
+                    data.getOutputStream().write(Integer.toHexString(g).getBytes());
                     if (b < 16) {
-                        _data.getOutputStream().write('0');
+                        data.getOutputStream().write('0');
                     }
-                    _data.getOutputStream().write(Integer.toHexString(b).getBytes());
-                    _data.getOutputStream().write(' ');
+                    data.getOutputStream().write(Integer.toHexString(b).getBytes());
+                    data.getOutputStream().write(' ');
                 }
             }
-            _data.getOutputStream().write(">\n".getBytes());
+            data.getOutputStream().write(">\n".getBytes());
         } catch (IOException ex) {
             ex.printStackTrace();
         }
 
     }
 
+    /**
+     * Used to set the contents of the PDF stream.
+     * @param data the contents as a byte array
+     * @throws IOException in case of an I/O problem
+     */
     public void setData(byte[] data) throws IOException {
-        _data.reset();
-        _data.getOutputStream().write(data);
+        this.data.reset();
+        this.data.getOutputStream().write(data);
     }
 
     /*
@@ -165,9 +185,13 @@ public class PDFStream extends PDFObject {
     }
     */
 
+    /**
+     * Returns the size of the content.
+     * @return size of the content
+     */
     public int getDataLength() {
         try {
-            return _data.getSize();
+            return data.getSize();
         } catch (Exception e) {
             e.printStackTrace();
             return 0;
@@ -175,34 +199,35 @@ public class PDFStream extends PDFObject {
     }
 
     /**
-     * represent as PDF.
+     * Represent as PDF.
      *
      * @return the PDF string.
      */
-    /*
-     * public byte[] toPDF() {
-     * byte[] d = _data.toByteArray();
-     * ByteArrayOutputStream s = new ByteArrayOutputStream();
-     * String p = this.number + " " + this.generation
-     * + " obj\n<< /Length " + (d.length+1)
-     * + " >>\nstream\n";
-     * s.write(p.getBytes());
-     * s.write(d);
-     * s.write("\nendstream\nendobj\n".getBytes());
-     * return s.toByteArray();
-     * }
-     */
     public byte[] toPDF() {
-        throw new RuntimeException();
+        throw new UnsupportedOperationException("Use output(OutputStream) instead");
+        /*
+         * byte[] d = _data.toByteArray();
+         * ByteArrayOutputStream s = new ByteArrayOutputStream();
+         * String p = this.number + " " + this.generation
+         * + " obj\n<< /Length " + (d.length+1)
+         * + " >>\nstream\n";
+         * s.write(p.getBytes());
+         * s.write(d);
+         * s.write("\nendstream\nendobj\n".getBytes());
+         * return s.toByteArray();
+         */
     }
 
-    // overload the base object method so we don't have to copy
-    // byte arrays around so much
+    /**
+     * Overload the base object method so we don't have to copy
+     * byte arrays around so much
+     * @see org.apache.fop.pdf.PDFObject#output(OutputStream)
+     */
     protected int output(OutputStream stream) throws IOException {
         int length = 0;
         String filterEntry = applyFilters();
         byte[] p = (this.number + " " + this.generation + " obj\n<< /Length "
-                    + (_data.getSize() + 1) + " " + filterEntry
+                    + (data.getSize() + 1) + " " + filterEntry
                     + " >>\n").getBytes();
 
         stream.write(p);
@@ -217,15 +242,18 @@ public class PDFStream extends PDFObject {
 
     /**
      * Output just the stream data enclosed by stream/endstream markers
+     * @param stream OutputStream to write to
+     * @return int number of bytes written
+     * @throws IOException in case of an I/O problem
      */
     protected int outputStreamData(OutputStream stream) throws IOException {
         int length = 0;
         byte[] p = "stream\n".getBytes();
         stream.write(p);
         length += p.length;
-        _data.outputStreamData(stream);
-        length += _data.getSize();
-        _data.close();
+        data.outputStreamData(stream);
+        length += data.getSize();
+        data.close();
         p = "\nendstream\n".getBytes();
         stream.write(p);
         length += p.length;
@@ -239,18 +267,20 @@ public class PDFStream extends PDFObject {
      * entries for the stream dictionary. If the filters have already
      * been applied to the data (either externally, or internally)
      * then the dictionary entries are built and returned.
+     * @return a String representing the filter list
+     * @throws IOException in case of an I/O problem
      */
     protected String applyFilters() throws IOException {
-        if (_filters.size() > 0) {
-            List names = new ArrayList();
-            List parms = new ArrayList();
+        if (filters.size() > 0) {
+            List names = new java.util.ArrayList();
+            List parms = new java.util.ArrayList();
 
             // run the filters
-            for (int count = 0; count < _filters.size(); count++) {
-                PDFFilter filter = (PDFFilter)_filters.get(count);
+            for (int count = 0; count < filters.size(); count++) {
+                PDFFilter filter = (PDFFilter)filters.get(count);
                 // apply the filter encoding if neccessary
                 if (!filter.isApplied()) {
-                    _data.applyFilter(filter);
+                    data.applyFilter(filter);
                     filter.setApplied(true);
                 }
                 // place the names in our local vector in reverse order
index 39f9d8b2d823262334ebbbedf5149226446c800a..9b4550fa4f1036e7bee0f597ed2222e98eba1367 100644 (file)
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
-
 package org.apache.fop.pdf;
 
+// Java
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
+// FOP
+import org.apache.fop.fonts.type1.PFBData;
+
+/**
+ * Special PDFStream for embedding Type 1 fonts.
+ */
 public class PDFT1Stream extends PDFStream {
-    private int origLength;
-    private int len1, len3;
-    private byte[] originalData = null;
+    
+    private PFBData pfb;
 
-    public PDFT1Stream(int num, int len) {
+    /**
+     * @see org.apache.fop.pdf.PDFObject#PDFObject(int)
+     */
+    public PDFT1Stream(int num) {
         super(num);
-        origLength = len;
     }
 
-    private static final boolean byteCmp(byte[] src, int offset, byte[] cmp) {
-        boolean ret = true;
-        for (int i = 0; ret == true && i < cmp.length; i++) {
-            // System.out.println("Compare: ");
-            // System.out.println("         "+src[offset+i]+" "+cmp[i]);
-            if (src[offset + i] != cmp[i])
-                ret = false;
-        }
-        return ret;
-    }
 
     /**
-     * calculates the Length1 and Length3 PDFStream attributes for type1
-     * font embedding
+     * Overload the base object method so we don't have to copy
+     * byte arrays around so much
+     * @see org.apache.fop.pdf.PDFObject#output(OutputStream)
      */
-    private void calcLengths(byte[] originalData) {
-        // Calculate length 1 and 3
-        // System.out.println ("Checking font, size = "+originalData.length);
-
-        // Length1 is the size of the initial ascii portion
-        // search for "currentfile eexec"
-        // Get the first binary number and search backwards for "eexec"
-        len1 = 30;
-
-        byte[] eexec = (new String("currentfile eexec")).getBytes();
-        // System.out.println("Length1="+len1);
-        while (!byteCmp(originalData, len1 - eexec.length, eexec))
-            len1++;
-        // Skip newline
-        len1++;
-
-        // Length3 is length of the last portion of the file
-        len3 = 0;
-        byte[] cltom = (new String("cleartomark")).getBytes();
-        len3 -= cltom.length;
-        while (!byteCmp(originalData, origLength + len3, cltom)) {
-            len3--;
-            // System.out.println("Len3="+len3);
-        }
-        len3 = -len3;
-        len3++;
-        // Eat 512 zeroes
-        int numZeroes = 0;
-        byte[] ws1 = "\n".getBytes();
-        byte[] ws2 = "\r".getBytes();
-        byte[] ws3 = "0".getBytes();
-        while ((originalData[origLength - len3] == ws1[0] || originalData[origLength - len3] == ws2[0] || originalData[origLength - len3] == ws3[0])
-               && numZeroes < 512) {
-            len3++;
-            if (originalData[origLength - len3] == ws3[0])
-                numZeroes++;
-        }
-        // System.out.println("Length3="+len3);
-    }
-
-    // overload the base object method so we don't have to copy
-    // byte arrays around so much
     protected int output(java.io.OutputStream stream)
             throws java.io.IOException {
+        if (pfb == null) {
+            throw new IllegalStateException("pfb must not be null at this point");
+        }
         int length = 0;
         String filterEntry = applyFilters();
-        String preData = new String(this.number + " " + this.generation
-                                    + " obj\n<< /Length "
-                                    + (_data.getSize() + 1) + " " + filterEntry
-                                    + " " + "/Length1 " + len1 + " /Length2 "
-                                    + (origLength - len3 - len1)
-                                    + " /Length3 " + len3 + " >>\n");
+        String preData = this.number + " " + this.generation
+                + " obj\n<< /Length " + pfb.getLength() + " " 
+                + filterEntry  
+                + " /Length1 " + pfb.getLength1()
+                + " /Length2 " + pfb.getLength2()
+                + " /Length3 " + pfb.getLength3() + " >>\n";
+
+        byte[] p;
+        try {
+            p = preData.getBytes(PDFDocument.ENCODING);
+        } catch (UnsupportedEncodingException ue) {
+            p = preData.getBytes();
+        }       
 
-        byte[] p = preData.getBytes();
         stream.write(p);
         length += p.length;
 
         length += outputStreamData(stream);
-        p = "endobj\n".getBytes();
+        try {
+            p = "endobj\n".getBytes(PDFDocument.ENCODING);
+        } catch (UnsupportedEncodingException ue) {
+            p = "endobj\n".getBytes();
+        }       
         stream.write(p);
         length += p.length;
-        System.out.println("Embedded Type1 font");
+        //System.out.println("Embedded Type1 font");
         return length;
     }
 
-    public void setData(byte[] data, int size) throws java.io.IOException {
-        calcLengths(data);
-        _data.reset();
+    /**
+     * Used to set the PFBData object that represents the embeddable Type 1 
+     * font.
+     * @param pfb The PFB file
+     * @throws IOException in case of an I/O problem
+     */
+    public void setData(PFBData pfb) throws IOException {
+        data.reset();
         // System.out.println("Writing " + size + " bytes of font data");
-        _data.getOutputStream().write(data, 0, size);
+        this.pfb = pfb;
+        pfb.outputAllParts(data.getOutputStream());
     }
 
 }
index 386f1fec11e3d07f6f322543664cc3cbce3039fa..a1081257d018fe498352271374ba964ef37d7f00 100644 (file)
@@ -1,29 +1,43 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
+import java.io.IOException;
+
+/**
+ * Special PDFStream for embeddable TrueType fonts.
+ */
 public class PDFTTFStream extends PDFStream {
+    
     private int origLength;
 
+    /**
+     * Main constructor
+     * @param num PDF object number
+     * @param len original length
+     */
     public PDFTTFStream(int num, int len) {
         super(num);
         origLength = len;
     }
 
-    // overload the base object method so we don't have to copy
-    // byte arrays around so much
+    /**
+     * Overload the base object method so we don't have to copy
+     * byte arrays around so much
+     * @see org.apache.fop.pdf.PDFObject#output(OutputStream)
+     */
     protected int output(java.io.OutputStream stream)
             throws java.io.IOException {
         int length = 0;
         String filterEntry = applyFilters();
         String preData = new String(this.number + " " + this.generation
                                     + " obj\n<< /Length "
-                                    + (_data.getSize() + 1) + " " + filterEntry
+                                    + (data.getSize() + 1) + " " + filterEntry
                                     + " " + "/Length1 " + origLength
                                     + " >>\n");
 
@@ -38,10 +52,17 @@ public class PDFTTFStream extends PDFStream {
         return length;
     }
 
-    public void setData(byte[] data, int size) throws java.io.IOException {
-        _data.reset();
+    /**
+     * Sets the TrueType font data.
+     * @param data the font payload
+     * @param size size of the payload
+     * @throws IOException in case of an I/O problem
+     */
+    public void setData(byte[] data, int size) throws IOException {
+        this.data.reset();
+        /**@todo Log using Logger */
         System.out.println("Writing " + size + " bytes of font data");
-        _data.getOutputStream().write(data, 0, size);
+        this.data.getOutputStream().write(data, 0, size);
     }
 
 }
index f7fdadba3480b679f6ad20a7c76d5b351267c2cd..d8789f8dcb841564d07cf4174e8ecd88c42e06c0 100644 (file)
@@ -1,30 +1,40 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.pdf;
 
-import java.util.ArrayList;
+import java.util.List;
 
 /**
- * class representing a <b>W</b> array for CID fonts.
+ * Class representing a <b>W</b> array for CID fonts.
  */
 public class PDFWArray {
 
     /**
-     * the metrics
+     * The metrics
      */
-    private ArrayList entries;
+    private List entries = new java.util.ArrayList();
 
+    /** 
+     * Default constructor 
+     */
     public PDFWArray() {
-        entries = new ArrayList();
+    }
+    
+    /**
+     * Convenience constructor
+     * @param metrics the metrics array to initially add
+     */
+    public PDFWArray(int[] metrics) {
+        addEntry(0, metrics);
     }
 
     /**
-     * add an entry for single starting CID.
+     * Add an entry for single starting CID.
      * i.e. in the form "c [w ...]"
      *
      * @param start the starting CID value.
@@ -35,7 +45,7 @@ public class PDFWArray {
     }
 
     /**
-     * add an entry for a range of CIDs (/W element on p 213)
+     * Add an entry for a range of CIDs (/W element on p 213)
      *
      * @param first the first CID in the range
      * @param last the last CID in the range
@@ -48,7 +58,7 @@ public class PDFWArray {
     }
 
     /**
-     * add an entry for a range of CIDs (/W2 element on p 210)
+     * Add an entry for a range of CIDs (/W2 element on p 210)
      *
      * @param first the first CID in the range
      * @param last the last CID in the range
@@ -62,10 +72,18 @@ public class PDFWArray {
         });
     }
 
+    /**
+     * Convert this object to PDF code.
+     * @return byte[] the PDF code
+     */
     public byte[] toPDF() {
         return toPDFString().getBytes();
     }
 
+    /**
+     * Convert this object to PDF code.
+     * @return String the PDF code
+     */
     public String toPDFString() {
         StringBuffer p = new StringBuffer();
         p.append("[ ");
@@ -87,7 +105,7 @@ public class PDFWArray {
     }
 
     /**
-     * inner class for entries in the form "c [w ...]"
+     * Inner class for entries in the form "c [w ...]"
      */
     private static class Entry {
         private int start;
index c88493ca238bea374a8c648c8003b08752eff2d7..1a557840c5813045402193f319857ad6e9753ef2 100644 (file)
@@ -1,32 +1,22 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.render.awt;
 
-// FOP
-import org.apache.fop.layout.FontInfo;
-import org.apache.fop.layout.FontDescriptor;
-import org.apache.fop.layout.FontState;
-
 // Java
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.awt.Component;
 import java.awt.Font;
-import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.geom.Rectangle2D;
 import java.awt.FontMetrics;
-import java.awt.font.FontRenderContext;
 import java.awt.font.TextLayout;
 
 /**
  * This is a FontMetrics to be used  for AWT  rendering.
- * It  instanciates a font, depening on famil and style
+ * It  instanciates a font, depening on family and style
  * values. The java.awt.FontMetrics for this font is then
  * created to be used for the actual measurement.
  * Since layout is word by word and since it is expected that
@@ -89,13 +79,12 @@ public class AWTFontMetrics {
     /**
      * Temp graphics object needed to get the font metrics
      */
-    Graphics2D graphics;
+    private Graphics2D graphics;
 
     /**
      * Constructs a new Font-metrics.
-     * @param parent  an temp graphics object - this is needed  so
-     * that we can get an instance of
-     * java.awt.FontMetrics
+     * @param graphics a temp graphics object - this is needed  so
+     * that we can get an instance of java.awt.FontMetrics
      */
     public AWTFontMetrics(Graphics2D graphics) {
         this.graphics = graphics;
@@ -104,15 +93,16 @@ public class AWTFontMetrics {
     /**
      * Determines the font ascent of the Font described by this
      * FontMetrics object
-     * @param family font family (jave name) to use
-     * @param style font style (jave def.) to use
+     * @param family font family (java name) to use
+     * @param style font style (java def.) to use
+     * @param size font size
      * @return ascent in milliponts
      */
     public int getAscender(String family, int style, int size) {
         setFont(family, style, size);
         // return (int)(FONT_FACTOR * fmt.getAscent());
 
-        // workaround for sun bug on FontMetric.getAscent()
+        // workaround for sun bug on FontMetrics.getAscent()
         // http://developer.java.sun.com/developer/bugParade/bugs/4399887.html
         int realAscent = fmt.getAscent()
                          - (fmt.getDescent() + fmt.getLeading());
@@ -122,6 +112,10 @@ public class AWTFontMetrics {
 
     /**
      * The size of a capital letter measured from the font's baseline
+     * @param family font family
+     * @param style font style
+     * @param size font size
+     * @return capital height in millipoints
      */
     public int getCapHeight(String family, int style, int size) {
         // currently just gets Ascent value but maybe should use
@@ -134,6 +128,7 @@ public class AWTFontMetrics {
      * FontMetrics object
      * @param family font family (jave name) to use
      * @param style font style (jave def.) to use
+     * @param size font size
      * @return descent in milliponts
      */
     public int getDescender(String family, int style, int size) {
@@ -146,6 +141,7 @@ public class AWTFontMetrics {
      * FontMetrics object
      * @param family font family (jave name) to use
      * @param style font style (jave def.) to use
+     * @param size font size
      * @return font height in milliponts
      */
     public int getXHeight(String family, int style, int size) {
@@ -159,7 +155,8 @@ public class AWTFontMetrics {
      * @param  i the character for which to get the width
      * @param family font family (jave name) to use
      * @param style font style (jave def.) to use
-     * @param size the of the font
+     * @param size font size
+     * @return character width in millipoints
      */
     public int width(int i, String family, int style, int size) {
         int w;
@@ -167,10 +164,11 @@ public class AWTFontMetrics {
         // the output seems to look a little better if the
         // space is rendered larger than given by
         // the FontMetrics object
-        if (i <= 32)
+        if (i <= 32) {
             w = (int)(1.4 * fmt.charWidth(i) * FONT_FACTOR);
-        else
+        } else {
             w = (int)(fmt.charWidth(i) * FONT_FACTOR);
+        }
         return w;
     }
 
@@ -179,6 +177,8 @@ public class AWTFontMetrics {
      * characters
      * @param family font family (jave name) to use
      * @param style font style (jave def.) to use
+     * @param size font size
+     * @return array of character widths in millipoints
      */
     public int[] getWidths(String family, int style, int size) {
         int i;
@@ -199,6 +199,7 @@ public class AWTFontMetrics {
      * whether it is a new one
      * @param family font family (jave name) to use
      * @param style font style (jave def.) to use
+     * @param size font size
      * @return true if the font was changed, false otherwise
      */
     private boolean setFont(String family, int style, int size) {
@@ -212,12 +213,13 @@ public class AWTFontMetrics {
             fmt = graphics.getFontMetrics(f1);
             changed = true;
         } else {
-            if ((this.style != style) ||!this.family.equals(family)
+            if ((this.style != style) || !this.family.equals(family)
                     || this.size != s) {
                 if (family.equals(this.family)) {
                     f1 = f1.deriveFont(style, (float)s);
-                } else
+                } else {
                     f1 = new Font(family, style, s);
+                }
                 fmt = graphics.getFontMetrics(f1);
                 changed = true;
             }
@@ -249,8 +251,6 @@ public class AWTFontMetrics {
      * @return font with the desired characeristics.
      */
     public java.awt.Font getFont(String family, int style, int size) {
-        Font f;
-
         setFont(family, style, size);
         return f1;
         /*
index 49b0403d97977b9f8c7c5bf41bb7e4c34fd2f8a5..ecabbc032d2ce2479bc79dbf0b3f4097ef6b147d 100644 (file)
@@ -1,33 +1,30 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
 
 package org.apache.fop.render.awt;
 
-// FOP
-import org.apache.fop.layout.FontInfo;
-import org.apache.fop.layout.FontDescriptor;
-import org.apache.fop.layout.FontState;
-
 // Java
-import java.util.Enumeration;
-import java.util.Hashtable;
 import java.awt.Graphics2D;
-import java.awt.Font;
+import java.util.Map;
+
+// FOP
+import org.apache.fop.fonts.FontMetrics;
+import org.apache.fop.fonts.FontType;
 
 
 /**
- * This class implements org.apache.fop.layout.FontMetric and
+ * This class implements org.apache.fop.layout.FontMetrics and
  * is added to the hash table in FontInfo. It  deferes the
  * actual calculation of the metrics to
  * AWTFontMetrics.  It only keeps the java name and
  * style as member varibles
  */
 
-public class FontMetricsMapper implements org.apache.fop.layout.FontMetric {
+public class FontMetricsMapper implements FontMetrics {
 
     /**
      * The first and last non space-character
@@ -57,86 +54,99 @@ public class FontMetricsMapper implements org.apache.fop.layout.FontMetric {
      * Constructs a new Font-metrics.
      * @param family the family name of the font (java value)
      * @param style the java type style value of the font
-     * @param parent  an AWT component - this is needed  so
-     * that we can get an instance of
-     * java.awt.FontMetrics
+     * @param graphics a Graphics2D object - this is needed  so
+     * that we can get an instance of java.awt.FontMetrics
      */
     public FontMetricsMapper(String family, int style, Graphics2D graphics) {
         this.family = family;
         this.style = style;
-        if (metric == null)
+        if (metric == null) {
             metric = new AWTFontMetrics(graphics);
+        }
     }
 
     /**
-     * Determines the font ascent of the Font described by this
-     * FontMetrics object
-     * @return ascent in milliponts
+     * @see org.apache.fop.layout.FontMetrics#getFontName()
+     */
+    public String getFontName() {
+        return family;
+    }
+
+    /**
+     * @see org.apache.fop.layout.FontMetrics#getFontType()
+     */
+    public FontType getFontType() {
+        return FontType.OTHER;
+    }
+    
+    /**
+     * @see org.apache.fop.layout.FontMetrics#getAscender(int)
      */
     public int getAscender(int size) {
         return metric.getAscender(family, style, size);
     }
 
-
     /**
-     * The size of a capital letter measured from the font's baseline
+     * @see org.apache.fop.layout.FontMetrics#getCapHeight(int)
      */
     public int getCapHeight(int size) {
         return metric.getCapHeight(family, style, size);
     }
 
     /**
-     * Determines the font descent of the Font described by this
-     * FontMetrics object
-     * @return descent in milliponts
+     * @see org.apache.fop.layout.FontMetrics#getDescender(int)
      */
     public int getDescender(int size) {
         return metric.getDescender(family, style, size);
     }
 
     /**
-     * Determines the typical font height of this
-     * FontMetrics object
-     * @return font height in milliponts
+     * @see org.apache.fop.layout.FontMetrics#getXHeight(int)
      */
     public int getXHeight(int size) {
         return metric.getXHeight(family, style, size);
     }
 
-
-    public int getFirstChar() {
-        return FIRST_CHAR;
-    }
-
-    public int getLastChar() {
-        return LAST_CHAR;
-    }
-
     /**
-     * return width (in 1/1000ths of point size) of character at
-     * code point i.
+     * @see org.apache.fop.layout.FontMetrics#getWidth(int, int)
      */
-    public int width(int i, int size) {
+    public int getWidth(int i, int size) {
         return metric.width(i, family, style, size);
     }
 
 
     /**
-     * return width (in 1/1000ths of point size) of all character
+     * @see org.apache.fop.layout.FontMetrics#getWidths()
      */
-    public int[] getWidths(int size) {
-        return metric.getWidths(family, style, size);
+    public int[] getWidths() {
+        return metric.getWidths(family, style, AWTFontMetrics.FONT_SIZE);
     }
 
     /**
      * Gets a Font instance  of the Font that this
      * FontMetrics describes in the desired size.
+     * @param size font size
      * @return font with the desired characeristics.
      */
-    public Font getFont(int size) {
+    public java.awt.Font getFont(int size) {
         return metric.getFont(family, style, size);
     }
 
+    /**
+     * @see org.apache.fop.layout.FontMetrics#getKerningInfo()
+     */
+    public Map getKerningInfo() {
+        return java.util.Collections.EMPTY_MAP;
+    }
+
+    /**
+     * @see org.apache.fop.layout.FontMetrics#hasKerningInfo()
+     */
+    public boolean hasKerningInfo() {
+        return false;
+    }
+
+
 }
 
 
index 3412791785e71f80bbbe53d8e31b560d4e139640..0ed83c968897697da0ba1a759bc49e426c58582c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * $Id$
- * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
+ * Copyright (C) 2001-2003 The Apache Software Foundation. All rights reserved.
  * For details on use and redistribution please refer to the
  * LICENSE file included with these sources.
  */
@@ -9,32 +9,27 @@ package org.apache.fop.render.awt;
 
 // FOP
 import org.apache.fop.layout.FontInfo;
-import org.apache.fop.layout.FontDescriptor;
 
 // Java
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.awt.Font;
 import java.awt.Graphics2D;
 
 /**
- * sets up the AWT fonts. It is similar to
+ * Sets up the AWT fonts. It is similar to
  * org.apache.fop.render.pdf.FontSetup.
  * Assigns the font (with metrics) to internal names like "F1" and
- * assigns family-style-weight triplets to the fonts
+ * assigns family-style-weight triplets to the fonts.
  */
 public class FontSetup {
 
 
     /**
-     * sets up the font info object.
+     * Sets up the font info object.
      *
-     * adds metrics for basic fonts and useful family-style-weight
-     * triplets for lookup
+     * Adds metrics for basic fonts and useful family-style-weight
+     * triplets for lookup.
      *
      * @param fontInfo the font info object to set up
-     * @param parent needed, since a live AWT component is needed
-     * to get a valid java.awt.FontMetrics object
+     * @param graphics needed for acces to font metrics
      */
     public static void setup(FontInfo fontInfo, Graphics2D graphics) {
         FontMetricsMapper metric;