From: Jeremias Maerki Date: Wed, 8 Jan 2003 14:02:48 +0000 (+0000) Subject: Second part of font refactoring: X-Git-Tag: Alt-Design-integration-base~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=11b058d9c7d39e712d8620dc231e4698fdc54f40;p=xmlgraphics-fop.git Second part of font refactoring: Moved most of the non-PDF-specific classes to the fonts package. Makes dependencies clearer First step towards the centralized font registry Lots of Javadocs Fixed Checkstyle errors git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195823 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/render/pdf/CIDFont.java b/src/org/apache/fop/render/pdf/CIDFont.java deleted file mode 100644 index 92ad2a5e8..000000000 --- a/src/org/apache/fop/render/pdf/CIDFont.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 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.pdf; - -import org.apache.fop.pdf.PDFWArray; - -public abstract class CIDFont extends Font { - - // Required - public abstract String getCidBaseFont(); - public abstract byte getCidType(); - public abstract String getCharEncoding(); - public abstract String getRegistry(); - public abstract String getOrdering(); - public abstract int getSupplement(); - // Optional - public int getDefaultWidth() { - return 0; - } - - public PDFWArray getWidths() { - return null; - } - - // public int getWinCharSet() { return 0; } - - // Need For FOP - - /** - * Returns CMap Object . - *

- * If this method does not return null , the mapping from character codes - * to a font number is performed in FOP . When the getCidType() method - * returns CIDFontType2 , this method must not return null . - */ - public CMap getCMap() { - return null; - } - - public boolean isMultiByte() { - return true; - } -} diff --git a/src/org/apache/fop/render/pdf/CMap.java b/src/org/apache/fop/render/pdf/CMap.java deleted file mode 100644 index e7e018a50..000000000 --- a/src/org/apache/fop/render/pdf/CMap.java +++ /dev/null @@ -1,12 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 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.pdf; - -public interface CMap { - public abstract char mapping(char ch); -} diff --git a/src/org/apache/fop/render/pdf/EmbedFontInfo.java b/src/org/apache/fop/render/pdf/EmbedFontInfo.java index 081ea51da..06006f183 100644 --- a/src/org/apache/fop/render/pdf/EmbedFontInfo.java +++ b/src/org/apache/fop/render/pdf/EmbedFontInfo.java @@ -1,43 +1,67 @@ /* * $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.pdf; -import java.util.ArrayList; +import java.util.List; /** * FontInfo contains meta information on fonts (where is the metrics file etc.) */ public class EmbedFontInfo { + private String metricsFile, embedFile; private boolean kerning; - private ArrayList fontTriplets; + private List fontTriplets; + /** + * Main constructor + * @param metricsFile Path to the xml file containing font metrics + * @param kerning True if kerning should be enabled + * @param fontTriplets List of font triplets to associate with this font + * @param embedFile Path to the embeddable font file (may be null) + */ public EmbedFontInfo(String metricsFile, boolean kerning, - ArrayList fontTriplets, String embedFile) { + List fontTriplets, String embedFile) { this.metricsFile = metricsFile; this.embedFile = embedFile; this.kerning = kerning; this.fontTriplets = fontTriplets; } + /** + * Returns the path to the metrics file + * @return the metrics file path + */ public String getMetricsFile() { return metricsFile; } + /** + * Returns the path to the embeddable font file + * @return the font file path + */ public String getEmbedFile() { return embedFile; } + /** + * Determines if kerning is enabled + * @return True if enabled + */ public boolean getKerning() { return kerning; } - public ArrayList getFontTriplets() { + /** + * Returns the list of font triplets associated with this font. + * @return List of font triplets + */ + public List getFontTriplets() { return fontTriplets; } diff --git a/src/org/apache/fop/render/pdf/Font.java b/src/org/apache/fop/render/pdf/Font.java deleted file mode 100644 index a4f00b24a..000000000 --- a/src/org/apache/fop/render/pdf/Font.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 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.pdf; - -// FOP -import org.apache.fop.layout.FontMetric; - -/** - * base class for PDF font classes - */ -public abstract class Font implements FontMetric { - - /** - * get the encoding of the font - */ - public abstract String encoding(); - - /** - * get the base font name - */ - public abstract String fontName(); - - /** - * get the subtype of the font, default is TYPE1 - */ - public byte getSubType() { - return org.apache.fop.pdf.PDFFont.TYPE1; - } - - /** - * map a Unicode character to a code point in the font - */ - public abstract char mapChar(char c); - - public boolean isMultiByte() { - return false; - } - -} - diff --git a/src/org/apache/fop/render/pdf/FontReader.java b/src/org/apache/fop/render/pdf/FontReader.java index 52e6452c4..16ec7fa1d 100644 --- a/src/org/apache/fop/render/pdf/FontReader.java +++ b/src/org/apache/fop/render/pdf/FontReader.java @@ -1,25 +1,33 @@ /* * $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.pdf; -import org.apache.fop.render.pdf.fonts.*; -import org.xml.sax.helpers.DefaultHandler; + +//Java +import java.util.List; +import java.util.Map; +import java.io.IOException; + +//SAX import org.xml.sax.XMLReader; import org.xml.sax.SAXException; -import org.xml.sax.InputSource; import org.xml.sax.Locator; import org.xml.sax.Attributes; -import java.io.IOException; -import java.util.Enumeration; -import java.util.ArrayList; -import java.util.HashMap; -import org.apache.fop.pdf.PDFWArray; -import org.apache.fop.pdf.PDFCIDFont; +import org.xml.sax.helpers.DefaultHandler; + +//FOP import org.apache.fop.apps.FOPException; +import org.apache.fop.fonts.BFEntry; +import org.apache.fop.fonts.CIDFontType; +import org.apache.fop.fonts.CustomFont; +import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontType; +import org.apache.fop.fonts.MultiByteFont; +import org.apache.fop.fonts.SingleByteFont; /** * Class for reading a metric.xml file and creating a font object. @@ -32,19 +40,20 @@ import org.apache.fop.apps.FOPException; * */ public class FontReader extends DefaultHandler { + private Locator locator = null; private boolean isCID = false; + private CustomFont returnFont = null; private MultiByteFont multiFont = null; private SingleByteFont singleFont = null; - private Font returnFont = null; - private String text = null; + private StringBuffer text = new StringBuffer(); - private ArrayList cidWidths = null; + private List cidWidths = null; private int cidWidthIndex = 0; - private HashMap currentKerning = null; + private Map currentKerning = null; - private ArrayList bfranges = null; + private List bfranges = null; private void createFont(String path) throws FOPException { XMLReader parser = null; @@ -54,8 +63,9 @@ public class FontReader extends DefaultHandler { } catch (Exception e) { throw new FOPException(e); } - if (parser == null) + if (parser == null) { throw new FOPException("Unable to create SAX parser"); + } try { parser.setFeature("http://xml.org/sax/features/namespace-prefixes", @@ -78,23 +88,17 @@ public class FontReader extends DefaultHandler { } /** - * Sets the path to embed a font. a null value disables font embedding + * Sets the path to embed a font. A null value disables font embedding. */ public void setFontEmbedPath(String path) { - if (isCID) - multiFont.embedFileName = path; - else - singleFont.embedFileName = path; + returnFont.setEmbedFileName(path); } /** * Enable/disable use of kerning for the font */ - public void useKerning(boolean kern) { - if (isCID) - multiFont.useKerning = true; - else - singleFont.useKerning = true; + public void setKerningEnabled(boolean enabled) { + returnFont.setKerningEnabled(enabled); } @@ -128,52 +132,40 @@ public class FontReader extends DefaultHandler { isCID = true; } else if ("TRUETYPE".equals(attributes.getValue("type"))) { singleFont = new SingleByteFont(); - singleFont.subType = org.apache.fop.pdf.PDFFont.TRUETYPE; + singleFont.setFontType(FontType.TRUETYPE); returnFont = singleFont; isCID = false; } else { singleFont = new SingleByteFont(); - singleFont.subType = org.apache.fop.pdf.PDFFont.TYPE1; + singleFont.setFontType(FontType.TYPE1); returnFont = singleFont; isCID = false; } } else if ("embed".equals(localName)) { - if (isCID) { - // This *is* annoying... should create a common - // interface for sing/multibytefonts... - multiFont.embedFileName = attributes.getValue("file"); - multiFont.embedResourceName = attributes.getValue("class"); - } else { - singleFont.embedFileName = attributes.getValue("file"); - singleFont.embedResourceName = attributes.getValue("class"); - } + returnFont.setEmbedFileName(attributes.getValue("file")); + returnFont.setEmbedResourceName(attributes.getValue("class")); } else if ("cid-widths".equals(localName)) { cidWidthIndex = getInt(attributes.getValue("start-index")); - cidWidths = new ArrayList(); + cidWidths = new java.util.ArrayList(); } else if ("kerning".equals(localName)) { - currentKerning = new HashMap(); - if (isCID) - multiFont.kerning.put(new Integer(attributes.getValue("kpx1")), - currentKerning); - else - singleFont.kerning.put(new Integer(attributes.getValue("kpx1")), - currentKerning); + currentKerning = new java.util.HashMap(); + returnFont.putKerningEntry(new Integer(attributes.getValue("kpx1")), + currentKerning); } else if ("bfranges".equals(localName)) { - bfranges = new ArrayList(); + bfranges = new java.util.ArrayList(); } else if ("bf".equals(localName)) { - BFEntry entry = new BFEntry(); - entry.unicodeStart = getInt(attributes.getValue("us")); - entry.unicodeEnd = getInt(attributes.getValue("ue")); - entry.glyphStartIndex = getInt(attributes.getValue("gi")); + BFEntry entry = new BFEntry(getInt(attributes.getValue("us")), + getInt(attributes.getValue("ue")), + getInt(attributes.getValue("gi"))); bfranges.add(entry); } else if ("wx".equals(localName)) { cidWidths.add(new Integer(attributes.getValue("w"))); } else if ("widths".equals(localName)) { - singleFont.width = new int[256]; + //singleFont.width = new int[256]; } else if ("char".equals(localName)) { try { - singleFont.width[Integer.parseInt(attributes.getValue("idx"))] = - Integer.parseInt(attributes.getValue("wdt")); + singleFont.setWidth( Integer.parseInt(attributes.getValue("idx")), + Integer.parseInt(attributes.getValue("wdt"))); } catch (NumberFormatException ne) { System.out.println("Malformed width in metric file: " + ne.getMessage()); @@ -188,87 +180,57 @@ public class FontReader extends DefaultHandler { int ret = 0; try { ret = Integer.parseInt(str); - } catch (Exception e) {} + } catch (Exception e) { + /**@todo log this exception */ + } return ret; } public void endElement(String uri, String localName, String qName) { - if ("font-name".equals(localName)) - if (isCID) - multiFont.fontName = text; - else - singleFont.fontName = text; - if ("ttc-name".equals(localName) && isCID) - multiFont.ttcName = text; - else if ("cap-height".equals(localName)) - if (isCID) - multiFont.capHeight = getInt(text); - else - singleFont.capHeight = getInt(text); - else if ("x-height".equals(localName)) - if (isCID) - multiFont.xHeight = getInt(text); - else - singleFont.xHeight = getInt(text); - else if ("ascender".equals(localName)) - if (isCID) - multiFont.ascender = getInt(text); - else - singleFont.ascender = getInt(text); - else if ("descender".equals(localName)) - if (isCID) - multiFont.descender = getInt(text); - else - singleFont.descender = getInt(text); - else if ("left".equals(localName)) - if (isCID) - multiFont.fontBBox[0] = getInt(text); - else - singleFont.fontBBox[0] = getInt(text); - else if ("bottom".equals(localName)) - if (isCID) - multiFont.fontBBox[1] = getInt(text); - else - singleFont.fontBBox[1] = getInt(text); - else if ("right".equals(localName)) - if (isCID) - multiFont.fontBBox[2] = getInt(text); - else - singleFont.fontBBox[2] = getInt(text); - else if ("first-char".equals(localName)) - singleFont.firstChar = getInt(text); - else if ("last-char".equals(localName)) - singleFont.lastChar = getInt(text); - else if ("top".equals(localName)) - if (isCID) - multiFont.fontBBox[3] = getInt(text); - else - singleFont.fontBBox[3] = getInt(text); - else if ("flags".equals(localName)) - if (isCID) - multiFont.flags = getInt(text); - else - singleFont.flags = getInt(text); - else if ("stemv".equals(localName)) - if (isCID) - multiFont.stemV = getInt(text); - else - singleFont.stemV = getInt(text); - else if ("italic-angle".equals(localName)) - if (isCID) - multiFont.italicAngle = getInt(text); - else - singleFont.italicAngle = getInt(text); - else if ("missing-width".equals(localName)) - if (isCID) - multiFont.missingWidth = getInt(text); - else - singleFont.missingWidth = getInt(text); - else if ("cid-type".equals(localName)) { - if ("CIDFontType2".equals(text)) - multiFont.cidType = PDFCIDFont.CID_TYPE2; + if ("font-name".equals(localName)) { + returnFont.setFontName(text.toString()); + } else if ("ttc-name".equals(localName) && isCID) { + multiFont.setTTCName(text.toString()); + } else if ("cap-height".equals(localName)) { + returnFont.setCapHeight(getInt(text.toString())); + } else if ("x-height".equals(localName)) { + returnFont.setXHeight(getInt(text.toString())); + } else if ("ascender".equals(localName)) { + returnFont.setAscender(getInt(text.toString())); + } else if ("descender".equals(localName)) { + returnFont.setDescender(getInt(text.toString())); + } else if ("left".equals(localName)) { + int[] bbox = returnFont.getFontBBox(); + bbox[0] = getInt(text.toString()); + returnFont.setFontBBox(bbox); + } else if ("bottom".equals(localName)) { + int[] bbox = returnFont.getFontBBox(); + bbox[1] = getInt(text.toString()); + returnFont.setFontBBox(bbox); + } else if ("right".equals(localName)) { + int[] bbox = returnFont.getFontBBox(); + bbox[2] = getInt(text.toString()); + returnFont.setFontBBox(bbox); + } else if ("top".equals(localName)) { + int[] bbox = returnFont.getFontBBox(); + bbox[3] = getInt(text.toString()); + returnFont.setFontBBox(bbox); + } else if ("first-char".equals(localName)) { + returnFont.setFirstChar(getInt(text.toString())); + } else if ("last-char".equals(localName)) { + returnFont.setLastChar(getInt(text.toString())); + } else if ("flags".equals(localName)) { + returnFont.setFlags(getInt(text.toString())); + } else if ("stemv".equals(localName)) { + returnFont.setStemV(getInt(text.toString())); + } else if ("italic-angle".equals(localName)) { + returnFont.setItalicAngle(getInt(text.toString())); + } else if ("missing-width".equals(localName)) { + returnFont.setMissingWidth(getInt(text.toString())); + } else if ("cid-type".equals(localName)) { + multiFont.setCIDType(CIDFontType.byName(text.toString())); } else if ("default-width".equals(localName)) { - multiFont.defaultWidth = getInt(text); + multiFont.setDefaultWidth(getInt(text.toString())); } else if ("cid-widths".equals(localName)) { int[] wds = new int[cidWidths.size()]; int j = 0; @@ -277,19 +239,17 @@ public class FontReader extends DefaultHandler { wds[j++] = i.intValue(); } - multiFont.warray.addEntry(cidWidthIndex, wds); - multiFont.width = wds; + multiFont.addCIDWidthEntry(cidWidthIndex, wds); + multiFont.setWidthArray(wds); } else if ("bfranges".equals(localName)) { - multiFont.bfentries = (BFEntry[])bfranges.toArray(new BFEntry[0]); + multiFont.setBFEntries((BFEntry[])bfranges.toArray(new BFEntry[0])); } - + text.setLength(0); //Reset text buffer (see characters()) } public void characters(char[] ch, int start, int length) { - char c[] = new char[length]; - System.arraycopy(ch, start, c, 0, length); - text = new String(c); + text.append(ch, start, length); } } diff --git a/src/org/apache/fop/render/pdf/FontSetup.java b/src/org/apache/fop/render/pdf/FontSetup.java index 8d2aa7a06..901763361 100644 --- a/src/org/apache/fop/render/pdf/FontSetup.java +++ b/src/org/apache/fop/render/pdf/FontSetup.java @@ -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. */ @@ -8,16 +8,32 @@ package org.apache.fop.render.pdf; // FOP -import org.apache.fop.render.pdf.fonts.*; +import org.apache.fop.fonts.Font; +import org.apache.fop.fonts.FontDescriptor; +import org.apache.fop.fonts.LazyFont; import org.apache.fop.layout.FontInfo; -import org.apache.fop.layout.FontDescriptor; import org.apache.fop.pdf.PDFDocument; import org.apache.fop.pdf.PDFResources; +// FOP (base 14 fonts) +import org.apache.fop.fonts.base14.Helvetica; +import org.apache.fop.fonts.base14.HelveticaBold; +import org.apache.fop.fonts.base14.HelveticaOblique; +import org.apache.fop.fonts.base14.HelveticaBoldOblique; +import org.apache.fop.fonts.base14.TimesRoman; +import org.apache.fop.fonts.base14.TimesBold; +import org.apache.fop.fonts.base14.TimesItalic; +import org.apache.fop.fonts.base14.TimesBoldItalic; +import org.apache.fop.fonts.base14.Courier; +import org.apache.fop.fonts.base14.CourierBold; +import org.apache.fop.fonts.base14.CourierOblique; +import org.apache.fop.fonts.base14.CourierBoldOblique; +import org.apache.fop.fonts.base14.Symbol; +import org.apache.fop.fonts.base14.ZapfDingbats; // Java -import java.util.HashMap; +import java.util.Map; import java.util.Iterator; -import java.util.ArrayList; +import java.util.List; /** * sets up the PDF fonts. @@ -28,14 +44,15 @@ import java.util.ArrayList; 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 embedList ??? */ - public static void setup(FontInfo fontInfo, ArrayList embedList) { + public static void setup(FontInfo fontInfo, List embedList) { fontInfo.addMetrics("F1", new Helvetica()); fontInfo.addMetrics("F2", new HelveticaOblique()); @@ -133,63 +150,67 @@ public class FontSetup { /** * Add fonts from configuration file starting with * internalnames F + * @param fontInfo the font info object to set up + * @param fontInfos ??? + * @param num starting index for internal font numbering */ - public static void addConfiguredFonts(FontInfo fontInfo, ArrayList fontInfos, int num) { - if (fontInfos == null) - return; + public static void addConfiguredFonts(FontInfo fontInfo, List fontInfos, int num) { + if (fontInfos == null) { + return; //No fonts to process + } String internalName = null; - FontReader reader = null; - - for (int count = 0; count < fontInfos.size(); count++) { - EmbedFontInfo configFontInfo = - (EmbedFontInfo)fontInfos.get(count); - - String metricsFile = configFontInfo.getMetricsFile(); - if (metricsFile != null) { - internalName = "F" + num; - num++; - /* - reader = new FontReader(metricsFile); - reader.useKerning(configFontInfo.getKerning()); - reader.setFontEmbedPath(configFontInfo.getEmbedFile()); - fontInfo.addMetrics(internalName, reader.getFont()); - */ - LazyFont font = new LazyFont(configFontInfo.getEmbedFile(), - metricsFile, - configFontInfo.getKerning()); - fontInfo.addMetrics(internalName, font); - - ArrayList triplets = configFontInfo.getFontTriplets(); - for (int c = 0; c < triplets.size(); c++) { - FontTriplet triplet = (FontTriplet)triplets.get(c); - - int weight = 400; - try { - weight = Integer.parseInt(triplet.getWeight()); - weight = ((int)weight/100) * 100; - if(weight < 100) weight = 100; - if(weight > 900) weight = 900; - } catch(NumberFormatException nfe) { - - } - fontInfo.addFontProperties(internalName, - triplet.getName(), - triplet.getStyle(), - weight); + //FontReader reader = null; + + for (int i = 0; i < fontInfos.size(); i++) { + EmbedFontInfo configFontInfo = (EmbedFontInfo)fontInfos.get(i); + + String metricsFile = configFontInfo.getMetricsFile(); + if (metricsFile != null) { + internalName = "F" + num; + num++; + /* + reader = new FontReader(metricsFile); + reader.useKerning(configFontInfo.getKerning()); + reader.setFontEmbedPath(configFontInfo.getEmbedFile()); + fontInfo.addMetrics(internalName, reader.getFont()); + */ + LazyFont font = new LazyFont(configFontInfo.getEmbedFile(), + metricsFile, + configFontInfo.getKerning()); + fontInfo.addMetrics(internalName, font); + + List triplets = configFontInfo.getFontTriplets(); + for (int c = 0; c < triplets.size(); c++) { + FontTriplet triplet = (FontTriplet)triplets.get(c); + + int weight = 400; + try { + weight = Integer.parseInt(triplet.getWeight()); + weight = ((int)weight / 100) * 100; + weight = Math.min(weight, 100); + weight = Math.max(weight, 900); + } catch (NumberFormatException nfe) { + /**@todo log this exception */ } + fontInfo.addFontProperties(internalName, + triplet.getName(), + triplet.getStyle(), + weight); } + } } } /** - * add the fonts in the font info to the PDF document + * Add the fonts in the font info to the PDF document * * @param doc PDF document to add fonts to + * @param resources PDFResources object to attach the font to * @param fontInfo font info object to get font information from */ public static void addToResources(PDFDocument doc, PDFResources resources, FontInfo fontInfo) { - HashMap fonts = fontInfo.getUsedFonts(); + Map fonts = fontInfo.getUsedFonts(); Iterator e = fonts.keySet().iterator(); while (e.hasNext()) { String f = (String)e.next(); @@ -198,8 +219,8 @@ public class FontSetup { if (font instanceof FontDescriptor) { desc = (FontDescriptor)font; } - resources.addFont(doc.makeFont(f, font.fontName(), - font.encoding(), font, desc)); + resources.addFont(doc.makeFont(f, font.getFontName(), + font.getEncoding(), font, desc)); } } } diff --git a/src/org/apache/fop/render/pdf/PDFRenderer.java b/src/org/apache/fop/render/pdf/PDFRenderer.java index 7bc9af5a5..27dcb8950 100644 --- a/src/org/apache/fop/render/pdf/PDFRenderer.java +++ b/src/org/apache/fop/render/pdf/PDFRenderer.java @@ -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. */ @@ -18,6 +18,8 @@ import org.apache.fop.apps.FOPException; import org.apache.fop.apps.Version; import org.apache.fop.fo.properties.RuleStyle; import org.apache.fop.fo.properties.BackgroundRepeat; +import org.apache.fop.fonts.*; +import org.apache.fop.fonts.FontMetrics; import org.apache.fop.pdf.PDFStream; import org.apache.fop.pdf.PDFDocument; import org.apache.fop.pdf.PDFInfo; @@ -51,11 +53,9 @@ import org.apache.fop.area.inline.Image; import org.apache.fop.area.inline.Leader; import org.apache.fop.area.inline.InlineParent; import org.apache.fop.layout.FontState; -import org.apache.fop.layout.FontMetric; import org.apache.fop.traits.BorderProps; import org.apache.fop.datatypes.ColorType; -import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; @@ -67,9 +67,8 @@ import java.io.OutputStream; import java.awt.Color; import java.awt.geom.Rectangle2D; import java.awt.geom.AffineTransform; -import java.util.HashMap; +import java.util.Map; import java.util.List; -import java.util.ArrayList; /* todo: @@ -103,15 +102,15 @@ public class PDFRenderer extends PrintRenderer { * this is used for prepared pages that cannot be immediately * rendered */ - protected HashMap pages = null; + protected Map pages = null; /** * Page references are stored using the PageViewport as the key * when a reference is made the PageViewport is used * for pdf this means we need the pdf page reference */ - protected HashMap pageReferences = new HashMap(); - protected HashMap pvReferences = new HashMap(); + protected Map pageReferences = new java.util.HashMap(); + protected Map pvReferences = new java.util.HashMap(); private String producer = "FOP"; @@ -149,7 +148,7 @@ public class PDFRenderer extends PrintRenderer { protected int currentFontSize = 0; protected int pageHeight; - protected HashMap filterMap = new HashMap(); + protected Map filterMap = new java.util.HashMap(); /** * true if a TJ command is left to be written @@ -192,7 +191,7 @@ public class PDFRenderer extends PrintRenderer { public void configure(Configuration conf) throws ConfigurationException { Configuration filters = conf.getChild("filterList"); Configuration[] filt = filters.getChildren("value"); - ArrayList filterList = new ArrayList(); + List filterList = new java.util.ArrayList(); for (int i = 0; i < filt.length; i++) { String name = filt[i].getValue(); filterList.add(name); @@ -203,7 +202,7 @@ public class PDFRenderer extends PrintRenderer { Configuration[] font = conf.getChildren("font"); for (int i = 0; i < font.length; i++) { Configuration[] triple = font[i].getChildren("font-triplet"); - ArrayList tripleList = new ArrayList(); + List tripleList = new java.util.ArrayList(); for (int j = 0; j < triple.length; j++) { tripleList.add(new FontTriplet(triple[j].getAttribute("name"), triple[j].getAttribute("style"), @@ -216,7 +215,7 @@ public class PDFRenderer extends PrintRenderer { tripleList, font[i].getAttribute("embed-url")); if(fontList == null) { - fontList = new ArrayList(); + fontList = new java.util.ArrayList(); } fontList.add(efi); } @@ -354,7 +353,7 @@ public class PDFRenderer extends PrintRenderer { currentPage = this.pdfDoc.makePage(this.pdfResources, (int) Math.round(w / 1000), (int) Math.round(h / 1000)); if (pages == null) { - pages = new HashMap(); + pages = new java.util.HashMap(); } pages.put(page, currentPage); pageReferences.put(page.getKey(), currentPage.referencePDF()); @@ -840,7 +839,7 @@ public class PDFRenderer extends PrintRenderer { String s = word.getWord(); - FontMetric metrics = fontInfo.getMetricsFor(name); + FontMetrics metrics = fontInfo.getMetricsFor(name); FontState fs = new FontState(name, metrics, size); escapeText(s, fs, useMultiByte, pdf); pdf.append(endText); @@ -856,8 +855,7 @@ public class PDFRenderer extends PrintRenderer { String endText = useMultiByte ? "> " : ") "; boolean kerningAvailable = false; - HashMap kerning = null; - kerning = fs.getKerning(); + Map kerning = fs.getKerning(); if (kerning != null && !kerning.isEmpty()) { kerningAvailable = true; } @@ -922,8 +920,8 @@ public class PDFRenderer extends PrintRenderer { } private void addKerning(StringBuffer buf, Integer ch1, Integer ch2, - HashMap kerning, String startText, String endText) { - HashMap kernPair = (HashMap) kerning.get(ch1); + Map kerning, String startText, String endText) { + Map kernPair = (Map) kerning.get(ch1); if (kernPair != null) { Integer width = (Integer) kernPair.get(ch2); diff --git a/src/org/apache/fop/render/pdf/fonts/BFEntry.java b/src/org/apache/fop/render/pdf/fonts/BFEntry.java deleted file mode 100644 index 4f3fc2376..000000000 --- a/src/org/apache/fop/render/pdf/fonts/BFEntry.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 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.pdf.fonts; - -/** - * This is just a holder class for bfentries - not get/put methods provided - */ -public class BFEntry { - public int unicodeStart; - public int unicodeEnd; - public int glyphStartIndex; - - public BFEntry() {} - - public BFEntry(int unicodeStart, int unicodeEnd, int glyphStartIndex) { - this.unicodeStart = unicodeStart; - this.unicodeEnd = unicodeEnd; - this.glyphStartIndex = glyphStartIndex; - } - -} diff --git a/src/org/apache/fop/render/pdf/fonts/LazyFont.java b/src/org/apache/fop/render/pdf/fonts/LazyFont.java deleted file mode 100644 index 53ee462cf..000000000 --- a/src/org/apache/fop/render/pdf/fonts/LazyFont.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 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.pdf.fonts; - -import org.apache.fop.render.pdf.Font; -import org.apache.fop.layout.FontDescriptor; -import org.apache.fop.pdf.PDFStream; -import java.util.HashMap; - -import org.apache.fop.render.pdf.FontReader; - -public class LazyFont extends Font implements FontDescriptor { - - private String metricsFileName = null; - private String fontEmbedPath = null; - private boolean useKerning = false; - - private boolean isMetricsLoaded = false; - private Font realFont = null; - private FontDescriptor realFontDescriptor = null; - - public LazyFont(String fontEmbedPath, String metricsFileName, boolean useKerning){ - this.metricsFileName = metricsFileName; - this.fontEmbedPath = fontEmbedPath; - this.useKerning = useKerning; - } - - private void load(){ - if(! isMetricsLoaded){ - isMetricsLoaded = true; - try{ - - // TODO - Possible thread problem here - - FontReader reader = new FontReader(metricsFileName); - reader.useKerning(useKerning); - reader.setFontEmbedPath(fontEmbedPath); - realFont = reader.getFont(); - if(realFont instanceof FontDescriptor){ - realFontDescriptor = (FontDescriptor) realFont; - } - // System.out.println("Metrics " + metricsFileName + " loaded."); - } catch (Exception ex) { - //log.error("Failed to read font metrics file " - // + metricsFileName - // + " : " + ex.getMessage()); - } - } - } - - public Font getRealFont(){ - return realFont; - } - - public boolean isMultiByte() { - return realFont.isMultiByte(); - } - - // Font - public String encoding(){ - load(); - return realFont.encoding(); - } - - public String fontName(){ - load(); - return realFont.fontName(); - } - - public byte getSubType(){ - load(); - return realFont.getSubType(); - } - - public char mapChar(char c){ - load(); - return realFont.mapChar(c); - } - - // FontMetrics - public int getAscender(int size){ - load(); - return realFont.getAscender(size); - } - - public int getCapHeight(int size){ - load(); - return realFont.getCapHeight(size); - } - - public int getDescender(int size){ - load(); - return realFont.getDescender(size); - } - - public int getXHeight(int size){ - load(); - return realFont.getXHeight(size); - } - - public int getFirstChar(){ - load(); - return realFont.getFirstChar(); - } - - public int getLastChar(){ - load(); - return realFont.getLastChar(); - } - - public int width(int i, int size){ - load(); - return realFont.width(i, size); - } - - public int[] getWidths(int size){ - load(); - return realFont.getWidths(size); - } - - // FontDescriptor - public int getCapHeight(){ - load(); - return realFontDescriptor.getCapHeight(); - } - - public int getDescender(){ - load(); - return realFontDescriptor.getDescender(); - } - - public int getAscender(){ - load(); - return realFontDescriptor.getAscender(); - } - - public int getFlags(){ - load(); - return realFontDescriptor.getFlags(); - } - - public int[] getFontBBox(){ - load(); - return realFontDescriptor.getFontBBox(); - } - - public int getItalicAngle(){ - load(); - return realFontDescriptor.getItalicAngle(); - } - - public int getStemV(){ - load(); - return realFontDescriptor.getStemV(); - } - - public boolean hasKerningInfo(){ - load(); - return realFontDescriptor.hasKerningInfo(); - } - - public HashMap getKerningInfo(){ - load(); - return realFontDescriptor.getKerningInfo(); - } - - public boolean isEmbeddable(){ - load(); - return realFontDescriptor.isEmbeddable(); - } - - public PDFStream getFontFile(int objNum){ - load(); - return realFontDescriptor.getFontFile(objNum); - } -} - diff --git a/src/org/apache/fop/render/pdf/fonts/MultiByteFont.java b/src/org/apache/fop/render/pdf/fonts/MultiByteFont.java deleted file mode 100644 index a59239dc9..000000000 --- a/src/org/apache/fop/render/pdf/fonts/MultiByteFont.java +++ /dev/null @@ -1,337 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001 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.pdf.fonts; - -import org.apache.fop.render.pdf.Font; -import org.apache.fop.layout.FontDescriptor; -import org.apache.fop.fonts.Glyphs; -import org.apache.fop.fonts.TTFSubSetFile; -import org.apache.fop.fonts.FontFileReader; -import org.apache.fop.pdf.PDFStream; -import org.apache.fop.pdf.PDFWArray; -import org.apache.fop.pdf.PDFCIDFont; -import org.apache.fop.render.pdf.CIDFont; -import org.apache.fop.render.pdf.CMap; -import org.apache.fop.pdf.PDFTTFStream; -import java.io.InputStream; -import java.io.IOException; -import java.io.FileInputStream; -import java.io.File; -import java.io.BufferedInputStream; -import java.util.HashMap; - -/** - * Generic MultiByte (CID) font - */ -public class MultiByteFont extends CIDFont implements FontDescriptor { - public String fontName = null; - public String ttcName = null; - public String encoding = "Identity-H"; - - public int capHeight = 0; - public int xHeight = 0; - public int ascender = 0; - public int descender = 0; - public int[] fontBBox = { - 0, 0, 0, 0 - }; - - public String embedFileName = null; - public String embedResourceName = null; - public PDFTTFStream embeddedFont = null; - - public int flags = 4; - public int stemV = 0; - public int italicAngle = 0; - public int missingWidth = 0; - public int defaultWidth = 0; - public byte cidType = PDFCIDFont.CID_TYPE2; - - public HashMap kerning = new HashMap(); - public boolean useKerning = true; - private String namePrefix = null; // Quasi unique prefix - private static int uniqueCounter = 1; - public PDFWArray warray = new PDFWArray(); - public int width[] = null; - - public BFEntry[] bfentries = null; - - - /** - * usedGlyphs contains orginal, new glyph index - */ - private HashMap usedGlyphs = new HashMap(); - - /** - * usedGlyphsIndex contains new glyph, original index - */ - private HashMap usedGlyphsIndex = new HashMap(); - int usedGlyphsCount = 0; - - public MultiByteFont() { - // Make sure that the 3 first glyphs are included - usedGlyphs.put(new Integer(0), new Integer(0)); - usedGlyphsIndex.put(new Integer(0), new Integer(0)); - usedGlyphsCount++; - usedGlyphs.put(new Integer(1), new Integer(1)); - usedGlyphsIndex.put(new Integer(1), new Integer(1)); - usedGlyphsCount++; - usedGlyphs.put(new Integer(2), new Integer(2)); - usedGlyphsIndex.put(new Integer(2), new Integer(2)); - usedGlyphsCount++; - - // Create a quasiunique prefix for fontname - int cnt = 0; - synchronized (this.getClass()) { - cnt = uniqueCounter++; - } - int ctm = (int)(System.currentTimeMillis() & 0xffff); - namePrefix = new String(cnt + "E" + Integer.toHexString(ctm)); - } - - public final boolean hasKerningInfo() { - return (useKerning & kerning.isEmpty()); - } - - public final java.util.HashMap getKerningInfo() { - if (useKerning) - return kerning; - else - return new HashMap(); - } - - public byte getSubType() { - return org.apache.fop.pdf.PDFFont.TYPE0; - } - - public String getLang() { - return null; - } - - public String getPanose() { - return null; - } - - public int getAvgWidth() { - return -1; - } - - public int getMinWidth() { - return -1; - } - - public int getMaxWidth() { - return -1; - } - - public int getleading() { - return -1; - } - - public int getStemH() { - return 0; - } - - public int getMissingWidth() { - return missingWidth; - } - - public int getDefaultWidth() { - return defaultWidth; - } - - public String getRegistry() { - return "Adobe"; - } - - public String getOrdering() { - return "UCS"; - } - - public int getSupplement() { - return 0; - } - - public byte getCidType() { - return cidType; - } - - public String getCidBaseFont() { - return isEmbeddable() ? namePrefix + fontName : fontName; - } - - public String getCharEncoding() { - return "Identity-H"; - } - - public PDFWArray getWidths() { - if (isEmbeddable()) { - // Create widths for reencoded chars - warray = new PDFWArray(); - int[] tmpWidth = new int[usedGlyphsCount]; - - for (int i = 0; i < usedGlyphsCount; i++) { - Integer nw = (Integer)usedGlyphsIndex.get(new Integer(i)); - int nwx = (nw == null) ? 0 : nw.intValue(); - tmpWidth[i] = width[nwx]; - } - warray.addEntry(0, tmpWidth); - } - return warray; - } - - public boolean isEmbeddable() { - return (embedFileName == null && embedResourceName == null) ? false - : true; - } - - - public PDFStream getFontFile(int i) { - try { - FontFileReader reader = new FontFileReader(embedFileName); - TTFSubSetFile subset = new TTFSubSetFile(); - - byte[] subsetFont = subset.readFont(reader, ttcName, usedGlyphs); - // Only TrueType CID fonts are supported now - - embeddedFont = new PDFTTFStream(i, subsetFont.length); - embeddedFont.addFilter("flate"); - embeddedFont.addFilter("ascii-85"); - embeddedFont.setData(subsetFont, subsetFont.length); - } catch (IOException ioe) { - //log.error("Failed to embed font [" + i + "] " - // + fontName + ": " + ioe.getMessage()); - return (PDFStream)null; - } - - return (PDFStream)embeddedFont; - } - - public String encoding() { - return encoding; - } - - public String fontName() { - return isEmbeddable() ? namePrefix + fontName : fontName; - } - - public int getAscender() { - return ascender; - } - - public int getDescender() { - return descender; - } - - public int getCapHeight() { - return capHeight; - } - - public int getAscender(int size) { - return size * ascender; - } - - public int getCapHeight(int size) { - return size * capHeight; - } - - public int getDescender(int size) { - return size * descender; - } - - public int getXHeight(int size) { - return size * xHeight; - } - - public int getFlags() { - return flags; - } - - public int[] getFontBBox() { - return fontBBox; - } - - public int getItalicAngle() { - return italicAngle; - } - - public int getStemV() { - return stemV; - } - - public int getFirstChar() { - return 0; - } - - public int getLastChar() { - return 255; - } - - public int width(int i, int size) { - if (isEmbeddable()) { - Integer idx = (Integer)usedGlyphsIndex.get(new Integer(i)); - return size * width[idx.intValue()]; - } else { - return size * width[i]; - } - } - - public int[] getWidths(int size) { - int[] arr = new int[width.length]; - System.arraycopy(width, 0, arr, 0, width.length - 1); - for (int i = 0; i < arr.length; i++) - arr[i] *= size; - return arr; - } - - public Integer reMap(Integer i) { - if (isEmbeddable()) { - Integer ret = (Integer)usedGlyphsIndex.get(i); - if (ret == null) - ret = i; - return ret; - } else { - return i; - } - - } - - public char mapChar(char c) { - int idx = (int)c; - int retIdx = 0; - - for (int i = 0; (i < bfentries.length) && retIdx == 0; i++) { - if (bfentries[i].unicodeStart <= idx - && bfentries[i].unicodeEnd >= idx) { - retIdx = bfentries[i].glyphStartIndex + idx - - bfentries[i].unicodeStart; - } - } - - if (isEmbeddable()) { - // Reencode to a new subset font or get - // the reencoded value - Integer newIdx = (Integer)usedGlyphs.get(new Integer(retIdx)); - if (newIdx == null) { - usedGlyphs.put(new Integer(retIdx), - new Integer(usedGlyphsCount)); - usedGlyphsIndex.put(new Integer(usedGlyphsCount), - new Integer(retIdx)); - retIdx = usedGlyphsCount; - // System.out.println(c+"("+(int)c+") = "+retIdx); - usedGlyphsCount++; - } else { - retIdx = newIdx.intValue(); - } - } - - return (char)retIdx; - } - -} - diff --git a/src/org/apache/fop/render/pdf/fonts/SingleByteFont.java b/src/org/apache/fop/render/pdf/fonts/SingleByteFont.java deleted file mode 100644 index 3dda3490d..000000000 --- a/src/org/apache/fop/render/pdf/fonts/SingleByteFont.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * $Id$ - * Copyright (C) 2001-2002 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.pdf.fonts; - -import org.apache.fop.render.pdf.Font; -import org.apache.fop.render.pdf.CodePointMapping; -import org.apache.fop.layout.FontDescriptor; -import org.apache.fop.fonts.Glyphs; -import org.apache.fop.pdf.PDFStream; -import org.apache.fop.pdf.PDFTTFStream; -import org.apache.fop.pdf.PDFT1Stream; -import java.io.InputStream; -import java.io.FileInputStream; -import java.io.BufferedInputStream; -import java.util.HashMap; - -/** - * Generic SingleByte font - */ -public class SingleByteFont extends Font implements FontDescriptor { - public String fontName = null; - public String encoding = "WinAnsiEncoding"; - private final CodePointMapping mapping - = CodePointMapping.getMapping("WinAnsiEncoding"); - - public int capHeight = 0; - public int xHeight = 0; - public int ascender = 0; - public int descender = 0; - public int[] fontBBox = { - 0, 0, 0, 0 - }; - - public String embedFileName = null; - public String embedResourceName = null; - public PDFStream embeddedFont = null; - - public int firstChar = 0; - public int lastChar = 255; - public int flags = 4; - public int stemV = 0; - public int italicAngle = 0; - public int missingWidth = 0; - - public HashMap kerning = new HashMap(); - public boolean useKerning = true; - - public int width[] = null; - public byte subType = 0; - - public final boolean hasKerningInfo() { - return (useKerning & kerning.isEmpty()); - } - - public final java.util.HashMap getKerningInfo() { - if (useKerning) - return kerning; - else - return new HashMap(); - } - - public byte getSubType() { - return subType; - } - - public int getAvgWidth() { - return -1; - } - - public int getMinWidth() { - return -1; - } - - public int getMaxWidth() { - return -1; - } - - public int getleading() { - return -1; - } - - public int getStemH() { - return 0; - } - - public int getMissingWidth() { - return missingWidth; - } - - public String getCharEncoding() { - return encoding; - } - - public boolean isEmbeddable() { - return (embedFileName == null && embedResourceName == null) ? false - : true; - } - - - public PDFStream getFontFile(int i) { - InputStream instream = null; - - // Get file first - if (embedFileName != null) - try { - instream = new FileInputStream(embedFileName); - } catch (Exception e) { - System.out.println("Failed to embed fontfile: " - + embedFileName); - } - - // Get resource - if (instream == null && embedResourceName != null) - try { - instream = - new BufferedInputStream(this.getClass().getResourceAsStream(embedResourceName)); - } catch (Exception e) { - System.out.println("Failed to embed fontresource: " - + embedResourceName); - } - - if (instream == null) - return (PDFStream)null; - - // Read fontdata - byte[] file = new byte[128000]; - int fsize = 0; - - try { - int l = instream.read(file, 0, 128000); - fsize += l; - - if (l == 128000) { - // More to read - needs to extend - byte[] tmpbuf; - - while (l > 0) { - tmpbuf = new byte[file.length + 64000]; - System.arraycopy(file, 0, tmpbuf, 0, file.length); - l = instream.read(tmpbuf, file.length, 64000); - fsize += l; - file = tmpbuf; - - if (l < 64000) // whole file read. No need to loop again - l = 0; - } - } - - if (subType == org.apache.fop.pdf.PDFFont.TYPE1) { - embeddedFont = new PDFT1Stream(i, fsize); - ((PDFT1Stream)embeddedFont).setData(file, fsize); - } else { - embeddedFont = new PDFTTFStream(i, fsize); - ((PDFTTFStream)embeddedFont).setData(file, fsize); - } - - embeddedFont.addFilter("flate"); - embeddedFont.addFilter("ascii-85"); - instream.close(); - } catch (Exception e) {} - - return (PDFStream)embeddedFont; - } - - public String encoding() { - return encoding; - } - - public String fontName() { - return fontName; - } - - public int getAscender() { - return ascender; - } - - public int getDescender() { - return descender; - } - - public int getCapHeight() { - return capHeight; - } - - public int getAscender(int size) { - return size * ascender; - } - - public int getCapHeight(int size) { - return size * capHeight; - } - - public int getDescender(int size) { - return size * descender; - } - - public int getXHeight(int size) { - return size * xHeight; - } - - public int getFlags() { - return flags; - } - - public int[] getFontBBox() { - return fontBBox; - } - - public int getItalicAngle() { - return italicAngle; - } - - public int getStemV() { - return stemV; - } - - public int getFirstChar() { - return 0; - // return firstChar; - } - - public int getLastChar() { - return lastChar; - } - - public int width(int i, int size) { - return size * width[i]; - } - - public int[] getWidths(int size) { - int[] arr = new int[width.length]; - System.arraycopy(width, 0, arr, 0, width.length - 1); - for (int i = 0; i < arr.length; i++) - arr[i] *= size; - return arr; - } - - public char mapChar(char c) { - char d = mapping.mapChar(c); - if(d != 0) { - return d; - } else { - return '#'; - } - } - -} - diff --git a/src/org/apache/fop/render/pdf/fonts/package.html b/src/org/apache/fop/render/pdf/fonts/package.html deleted file mode 100644 index f3e5c6652..000000000 --- a/src/org/apache/fop/render/pdf/fonts/package.html +++ /dev/null @@ -1,7 +0,0 @@ - -org.apache.fop.render.pdf.fonts Package - -

PDF font information/metrics

-

Generated entirely from XML files.

- - \ No newline at end of file