aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/fonts
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2006-03-16 13:51:14 +0000
committerJeremias Maerki <jeremias@apache.org>2006-03-16 13:51:14 +0000
commite6d53d8a9c84b6447f3db4b2b36576d418df1a4a (patch)
tree7c9c4dcaa414499c66c262383108879e5007c815 /src/java/org/apache/fop/fonts
parentde1255b13f15201dbba5d6c282cded7719e3dc8f (diff)
downloadxmlgraphics-fop-e6d53d8a9c84b6447f3db4b2b36576d418df1a4a.tar.gz
xmlgraphics-fop-e6d53d8a9c84b6447f3db4b2b36576d418df1a4a.zip
Added support for fixed-width spaces and kerning.
Note: letter-spacing is still implemented as before. Fixing this cannot be done without looking at space resolution as a whole on inline level. The Character area tree object is now deprecated. Normal Text objects should be used to keep renderers simpler. Introduced FontResolver interface to remove the dependency of FOUserAgent into the font package. Added kerning information to the base14 fonts, Helvetica and Times. By default it is not used to keep the status-quo. Enable base14 kerning through the FOUserAgent (FopFactory after the API finalization). Restored the production of EPS images in PDF output as PostScript XObjects. Restored functionality for a more compact format for the area tree renderer to save space. LayoutEngineTester slightly changed to allow for configuration values from the XML file. For now, only enabling base14 kerning is supported. Removed duplicate offset variable in SpaceArea. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@386327 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/fonts')
-rw-r--r--src/java/org/apache/fop/fonts/CustomFont.java29
-rw-r--r--src/java/org/apache/fop/fonts/Font.java94
-rw-r--r--src/java/org/apache/fop/fonts/FontReader.java9
-rw-r--r--src/java/org/apache/fop/fonts/FontResolver.java38
-rw-r--r--src/java/org/apache/fop/fonts/FontSetup.java56
-rw-r--r--src/java/org/apache/fop/fonts/LazyFont.java18
6 files changed, 151 insertions, 93 deletions
diff --git a/src/java/org/apache/fop/fonts/CustomFont.java b/src/java/org/apache/fop/fonts/CustomFont.java
index f39bb108d..85c7da1ee 100644
--- a/src/java/org/apache/fop/fonts/CustomFont.java
+++ b/src/java/org/apache/fop/fonts/CustomFont.java
@@ -21,9 +21,6 @@ package org.apache.fop.fonts;
import java.util.Map;
import javax.xml.transform.Source;
-import org.apache.fop.apps.FOUserAgent;
-
-
/**
* Abstract base class for custom fonts loaded from files, for example.
*/
@@ -33,7 +30,7 @@ public abstract class CustomFont extends Typeface
private String fontName = null;
private String embedFileName = null;
private String embedResourceName = null;
- private FOUserAgent userAgent = null;
+ private FontResolver resolver = null;
private int capHeight = 0;
private int xHeight = 0;
@@ -48,7 +45,7 @@ public abstract class CustomFont extends Typeface
private int firstChar = 0;
private int lastChar = 255;
- private Map kerning = new java.util.HashMap();
+ private Map kerning;
private boolean useKerning = true;
@@ -74,10 +71,11 @@ public abstract class CustomFont extends Typeface
* @return Source for an embeddable font file or null if not available.
*/
public Source getEmbedFileSource() {
- if (userAgent != null && embedFileName != null) {
- return userAgent.resolveURI(embedFileName, userAgent.getFontBaseURL());
+ if (resolver != null && embedFileName != null) {
+ return resolver.resolve(embedFileName);
+ } else {
+ return null;
}
- return null;
}
/**
@@ -212,14 +210,14 @@ public abstract class CustomFont extends Typeface
* @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
*/
public final boolean hasKerningInfo() {
- return (isKerningEnabled() & kerning.isEmpty());
+ return (isKerningEnabled() && (kerning != null) && !kerning.isEmpty());
}
/**
* @see org.apache.fop.fonts.FontMetrics#getKerningInfo()
*/
public final Map getKerningInfo() {
- if (isKerningEnabled()) {
+ if (hasKerningInfo()) {
return kerning;
} else {
return java.util.Collections.EMPTY_MAP;
@@ -343,17 +341,20 @@ public abstract class CustomFont extends Typeface
}
/**
- * Sets the user agent environment. Needed for URI resolution
- * @param userAgent the user agent
+ * Sets the font resolver. Needed for URI resolution.
+ * @param resolver the font resolver
*/
- public void setUserAgent(FOUserAgent userAgent) {
- this.userAgent = userAgent;
+ public void setResolver(FontResolver resolver) {
+ this.resolver = resolver;
}
/**
* @see org.apache.fop.fonts.MutableFont#putKerningEntry(Integer, Map)
*/
public void putKerningEntry(Integer key, Map value) {
+ if (kerning == null) {
+ kerning = new java.util.HashMap();
+ }
this.kerning.put(key, value);
}
diff --git a/src/java/org/apache/fop/fonts/Font.java b/src/java/org/apache/fop/fonts/Font.java
index cbbc81b21..1a2d39651 100644
--- a/src/java/org/apache/fop/fonts/Font.java
+++ b/src/java/org/apache/fop/fonts/Font.java
@@ -111,18 +111,39 @@ public class Font {
return metric.getXHeight(fontSize) / 1000;
}
+ /** @return true if the font has kerning info */
+ public boolean hasKerning() {
+ return metric.hasKerningInfo();
+ }
+
/**
* Returns the font's kerning table
* @return the kerning table
*/
public Map getKerning() {
- Map ret = metric.getKerningInfo();
- if (ret != null) {
- return ret;
+ if (metric.hasKerningInfo()) {
+ return metric.getKerningInfo();
} else {
return java.util.Collections.EMPTY_MAP;
}
}
+
+ /**
+ * Returns the amount of kerning between two characters.
+ * @param ch1 first character
+ * @param ch2 second character
+ * @return the distance to adjust for kerning, 0 if there's no kerning
+ */
+ public int getKernValue(char ch1, char ch2) {
+ Map kernPair = (Map)getKerning().get(new Integer(ch1));
+ if (kernPair != null) {
+ Integer width = (Integer)kernPair.get(new Integer(ch2));
+ if (width != null) {
+ return width.intValue();
+ }
+ }
+ return 0;
+ }
/**
* Returns the width of a character
@@ -206,63 +227,50 @@ public class Font {
if ((c == '\n') || (c == '\r') || (c == '\t') || (c == '\u00A0')) {
width = getCharWidth(' ');
} else {
- width = getWidth(mapChar(c));
+ if (hasChar(c)) {
+ width = getWidth(mapChar(c));
+ } else {
+ width = -1;
+ }
if (width <= 0) {
// Estimate the width of spaces not represented in
// the font
- int em = getWidth(mapChar('m'));
- int en = getWidth(mapChar('n'));
- if (em <= 0) {
- em = 500 * getFontSize();
- }
- if (en <= 0) {
- en = em - 10;
- }
+ int em = getFontSize(); //http://en.wikipedia.org/wiki/Em_(typography)
+ int en = em / 2; //http://en.wikipedia.org/wiki/En_(typography)
if (c == ' ') {
width = em;
- }
- if (c == '\u2000') {
+ } else if (c == '\u2000') {
width = en;
- }
- if (c == '\u2001') {
+ } else if (c == '\u2001') {
width = em;
- }
- if (c == '\u2002') {
+ } else if (c == '\u2002') {
width = em / 2;
- }
- if (c == '\u2003') {
+ } else if (c == '\u2003') {
width = getFontSize();
- }
- if (c == '\u2004') {
+ } else if (c == '\u2004') {
width = em / 3;
- }
- if (c == '\u2005') {
+ } else if (c == '\u2005') {
width = em / 4;
- }
- if (c == '\u2006') {
+ } else if (c == '\u2006') {
width = em / 6;
- }
- if (c == '\u2007') {
- width = getCharWidth(' ');
- }
- if (c == '\u2008') {
+ } else if (c == '\u2007') {
+ width = getCharWidth('0');
+ } else if (c == '\u2008') {
width = getCharWidth('.');
- }
- if (c == '\u2009') {
+ } else if (c == '\u2009') {
width = em / 5;
- }
- if (c == '\u200A') {
- width = 5;
- }
- if (c == '\u200B') {
- width = 100;
- }
- if (c == '\u202F') {
+ } else if (c == '\u200A') {
+ width = em / 10;
+ } else if (c == '\u200B') {
+ width = 0;
+ } else if (c == '\u202F') {
width = getCharWidth(' ') / 2;
- }
- if (c == '\u3000') {
+ } else if (c == '\u3000') {
width = getCharWidth(' ') * 2;
+ } else {
+ //Will be internally replaced by "#" if not found
+ width = getWidth(mapChar(c));
}
}
}
diff --git a/src/java/org/apache/fop/fonts/FontReader.java b/src/java/org/apache/fop/fonts/FontReader.java
index 74ed06579..cc9888962 100644
--- a/src/java/org/apache/fop/fonts/FontReader.java
+++ b/src/java/org/apache/fop/fonts/FontReader.java
@@ -34,7 +34,6 @@ import org.xml.sax.helpers.DefaultHandler;
//FOP
import org.apache.fop.apps.FOPException;
-import org.apache.fop.apps.FOUserAgent;
import org.xml.sax.InputSource;
/**
@@ -114,11 +113,11 @@ public class FontReader extends DefaultHandler {
}
/**
- * Sets the user agent environment. Needed for URI resolution
- * @param userAgent the user agent
+ * Sets the font resolver. Needed for URI resolution.
+ * @param resolver the font resolver
*/
- public void setUserAgent(FOUserAgent userAgent) {
- returnFont.setUserAgent(userAgent);
+ public void setResolver(FontResolver resolver) {
+ returnFont.setResolver(resolver);
}
diff --git a/src/java/org/apache/fop/fonts/FontResolver.java b/src/java/org/apache/fop/fonts/FontResolver.java
new file mode 100644
index 000000000..7999de646
--- /dev/null
+++ b/src/java/org/apache/fop/fonts/FontResolver.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.fonts;
+
+import javax.xml.transform.Source;
+
+/**
+ * This interface is used to resolve absolute and relative font URIs.
+ */
+public interface FontResolver {
+
+ /**
+ * Called to resolve an URI to a Source instance. The base URI needed by the URIResolver's
+ * resolve() method is defined to be implicitely available in this case. If the URI cannot
+ * be resolved, null is returned and it is assumed that the FontResolver implementation
+ * already warned the user about the problem.
+ * @param href An href attribute, which may be relative or absolute.
+ * @return A Source object, or null if the href could not resolved.
+ */
+ Source resolve(String href);
+
+}
diff --git a/src/java/org/apache/fop/fonts/FontSetup.java b/src/java/org/apache/fop/fonts/FontSetup.java
index 140fcb8b8..4cab92d9d 100644
--- a/src/java/org/apache/fop/fonts/FontSetup.java
+++ b/src/java/org/apache/fop/fonts/FontSetup.java
@@ -18,8 +18,6 @@
package org.apache.fop.fonts;
-import org.apache.fop.apps.FOUserAgent;
-
// FOP (base 14 fonts)
import org.apache.fop.fonts.base14.Helvetica;
import org.apache.fop.fonts.base14.HelveticaBold;
@@ -68,22 +66,39 @@ public class FontSetup {
* triplets for lookup.
*
* @param fontInfo the font info object to set up
- * @param embedList ???
+ * @param embedList a list of EmbedFontInfo objects
+ * @param resolver the font resolver
+ */
+ public static void setup(FontInfo fontInfo, List embedList, FontResolver resolver) {
+ setup(fontInfo, embedList, resolver, false);
+ }
+
+ /**
+ * Sets up the font info object.
+ *
+ * Adds metrics for basic fonts and useful family-style-weight
+ * triplets for lookup.
+ *
+ * @param fontInfo the font info object to set up
+ * @param embedList a list of EmbedFontInfo objects
+ * @param resolver the font resolver
+ * @param enableBase14Kerning true if kerning should be enabled for base 14 fonts
*/
- public static void setup(FontInfo fontInfo, List embedList, FOUserAgent ua) {
+ public static void setup(FontInfo fontInfo, List embedList, FontResolver resolver,
+ boolean enableBase14Kerning) {
- fontInfo.addMetrics("F1", new Helvetica());
- fontInfo.addMetrics("F2", new HelveticaOblique());
- fontInfo.addMetrics("F3", new HelveticaBold());
- fontInfo.addMetrics("F4", new HelveticaBoldOblique());
- fontInfo.addMetrics("F5", new TimesRoman());
- fontInfo.addMetrics("F6", new TimesItalic());
- fontInfo.addMetrics("F7", new TimesBold());
- fontInfo.addMetrics("F8", new TimesBoldItalic());
- fontInfo.addMetrics("F9", new Courier());
- fontInfo.addMetrics("F10", new CourierOblique());
- fontInfo.addMetrics("F11", new CourierBold());
- fontInfo.addMetrics("F12", new CourierBoldOblique());
+ fontInfo.addMetrics("F1", new Helvetica(enableBase14Kerning));
+ fontInfo.addMetrics("F2", new HelveticaOblique(enableBase14Kerning));
+ fontInfo.addMetrics("F3", new HelveticaBold(enableBase14Kerning));
+ fontInfo.addMetrics("F4", new HelveticaBoldOblique(enableBase14Kerning));
+ fontInfo.addMetrics("F5", new TimesRoman(enableBase14Kerning));
+ fontInfo.addMetrics("F6", new TimesItalic(enableBase14Kerning));
+ fontInfo.addMetrics("F7", new TimesBold(enableBase14Kerning));
+ fontInfo.addMetrics("F8", new TimesBoldItalic(enableBase14Kerning));
+ fontInfo.addMetrics("F9", new Courier(enableBase14Kerning));
+ fontInfo.addMetrics("F10", new CourierOblique(enableBase14Kerning));
+ fontInfo.addMetrics("F11", new CourierBold(enableBase14Kerning));
+ fontInfo.addMetrics("F12", new CourierBoldOblique(enableBase14Kerning));
fontInfo.addMetrics("F13", new Symbol());
fontInfo.addMetrics("F14", new ZapfDingbats());
@@ -162,17 +177,18 @@ public class FontSetup {
"normal", Font.NORMAL);
/* Add configured fonts */
- addConfiguredFonts(fontInfo, embedList, 15, ua);
+ addConfiguredFonts(fontInfo, embedList, 15, resolver);
}
/**
* Add fonts from configuration file starting with internal name F<num>.
* @param fontInfo the font info object to set up
- * @param fontInfoList
+ * @param fontInfoList a list of EmbedFontInfo objects
* @param num starting index for internal font numbering
+ * @param resolver the font resolver
*/
public static void addConfiguredFonts(FontInfo fontInfo, List fontInfoList
- , int num, FOUserAgent userAgent) {
+ , int num, FontResolver resolver) {
if (fontInfoList == null) {
return; //No fonts to process
}
@@ -196,7 +212,7 @@ public class FontSetup {
LazyFont font = new LazyFont(configFontInfo.getEmbedFile(),
metricsFile,
configFontInfo.getKerning(),
- userAgent);
+ resolver);
fontInfo.addMetrics(internalName, font);
List triplets = configFontInfo.getFontTriplets();
diff --git a/src/java/org/apache/fop/fonts/LazyFont.java b/src/java/org/apache/fop/fonts/LazyFont.java
index f8d22f3bb..ee0b47ec3 100644
--- a/src/java/org/apache/fop/fonts/LazyFont.java
+++ b/src/java/org/apache/fop/fonts/LazyFont.java
@@ -17,10 +17,8 @@
/* $Id$ */
package org.apache.fop.fonts;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.xml.transform.Source;
@@ -29,7 +27,6 @@ import javax.xml.transform.stream.StreamSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.apps.FOPException;
-import org.apache.fop.apps.FOUserAgent;
import org.xml.sax.InputSource;
/**
@@ -47,21 +44,21 @@ public class LazyFont extends Typeface implements FontDescriptor {
private Typeface realFont = null;
private FontDescriptor realFontDescriptor = null;
- private FOUserAgent userAgent = null;
+ private FontResolver resolver = null;
/**
* Main constructor
* @param fontEmbedPath path to embeddable file (may be null)
* @param metricsFileName path to the metrics XML file
* @param useKerning True, if kerning should be enabled
- * @param userAgent the environment for uri resoltuion
+ * @param resolver the font resolver to handle font URIs
*/
public LazyFont(String fontEmbedPath, String metricsFileName
- , boolean useKerning, FOUserAgent userAgent) {
+ , boolean useKerning, FontResolver resolver) {
this.metricsFileName = metricsFileName;
this.fontEmbedPath = fontEmbedPath;
this.useKerning = useKerning;
- this.userAgent = userAgent;
+ this.resolver = resolver;
}
private void load(boolean fail) {
@@ -69,9 +66,8 @@ public class LazyFont extends Typeface implements FontDescriptor {
try {
/**@todo Possible thread problem here */
FontReader reader = null;
- if (userAgent != null) {
- Source source = userAgent.resolveURI(metricsFileName
- , userAgent.getFontBaseURL());
+ if (resolver != null) {
+ Source source = resolver.resolve(metricsFileName);
if (source == null) {
String err = "Cannot load font: failed to create Source from metrics file "
+ metricsFileName;
@@ -106,7 +102,7 @@ public class LazyFont extends Typeface implements FontDescriptor {
}
reader.setKerningEnabled(useKerning);
reader.setFontEmbedPath(fontEmbedPath);
- reader.setUserAgent(userAgent);
+ reader.setResolver(resolver);
realFont = reader.getFont();
if (realFont instanceof FontDescriptor) {
realFontDescriptor = (FontDescriptor) realFont;