Author: jeremias
Date: Tue Feb 19 05:08:39 2008
New Revision: 629093
URL: http://svn.apache.org/viewvc?rev=629093&view=rev
Log:
Bugzilla #44451:
The patch fixes two problems:
- If distinct fonts declare the same font family name, the resulting font
mapping is currently arbitrary; now, a name-similarity heuristic is used to
prioritize the font mappings.
- "Medium" and "demi" fonts are now recognized as "bold", solving several
real-world problems (although this solution may be an oversimplification).
Submitted by: Justus Piater <justus-bulk.at.piater.name>
Patch modified by jeremias:
- Style fixes (tab chars)
- Refined font weight identification: medium -> 500, semi/demi -> 600
Modified:
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontInfo.java
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontResolver.java
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontTriplet.java
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontUtil.java
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
xmlgraphics/fop/trunk/src/java/org/apache/fop/render/PrintRenderer.java
xmlgraphics/fop/trunk/status.xml
Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontInfo.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontInfo.java?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontInfo.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontInfo.java Tue Feb
19 05:08:39 2008
@@ -52,6 +52,10 @@
/** look up a font-triplet to find a font-name */
private Map triplets; //Map<FontTriplet,String> (String = font key)
+ /** look up a font-triplet to find its priority
+ * (only used inside addFontProperties()) */
+ private Map tripletPriorities; //Map<FontTriplet,Integer>
+
/** look up a font-name to get a font (that implements FontMetrics at
least) */
private Map fonts; //Map<String,FontMetrics> (String = font key)
@@ -68,6 +72,7 @@
*/
public FontInfo() {
this.triplets = new java.util.HashMap();
+ this.tripletPriorities = new java.util.HashMap();
this.fonts = new java.util.HashMap();
this.usedFonts = new java.util.HashMap();
}
@@ -78,6 +83,8 @@
* @return True if valid
*/
public boolean isSetupValid() {
+ //We're only called when font setup is done:
+ tripletPriorities = null; // candidate for garbage collection
return triplets.containsKey(Font.DEFAULT_FONT);
}
@@ -105,7 +112,42 @@
if (log.isDebugEnabled()) {
log.debug("Registering: " + triplet + " under " + name);
}
+ String oldName = (String)triplets.get(triplet);
+ int newPriority = triplet.getPriority();
+ if (oldName != null) {
+ int oldPriority =
((Integer)tripletPriorities.get(triplet)).intValue();
+ if (oldPriority < newPriority) {
+ logDuplicateFont(triplet, false, oldName, oldPriority,
+ name, newPriority);
+ return;
+ } else {
+ logDuplicateFont(triplet, true, oldName, oldPriority,
+ name, newPriority);
+ }
+ }
this.triplets.put(triplet, name);
+ this.tripletPriorities.put(triplet, new Integer(newPriority));
+ }
+
+ /** Log warning about duplicate font triplets.
+ * @param triplet the duplicate font triplet
+ * @param replacing true iff the new font will replace the old one
+ * @param oldKey the old internal font name
+ * @param oldPriority the priority of the existing font mapping
+ * @param newKey the new internal font name
+ * @param newPriority the priority of the duplicate font mapping
+ */
+ private void logDuplicateFont(FontTriplet triplet, boolean replacing,
+ String oldKey, int oldPriority,
+ String newKey, int newPriority) {
+ if (log.isDebugEnabled()) {
+ log.debug(triplet
+ + (replacing ? ": Replacing " : ": Not replacing ")
+ +
((FontMetrics)fonts.get(triplets.get(triplet))).getFullName()
+ + " (" + oldPriority + ") by "
+ + ((FontMetrics)fonts.get(newKey)).getFullName()
+ + " (" + newPriority + ")");
+ }
}
/**
Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontResolver.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontResolver.java?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontResolver.java
(original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontResolver.java Tue
Feb 19 05:08:39 2008
@@ -28,7 +28,7 @@
/**
* 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
+ * resolve() method is defined to be implicitly 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.
Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontTriplet.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontTriplet.java?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontTriplet.java
(original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontTriplet.java Tue
Feb 19 05:08:39 2008
@@ -32,10 +32,11 @@
private String name;
private String style;
private int weight;
+ private int priority; // priority of this triplet/font mapping
//This is only a cache
private transient String key;
-
+
/**
* Creates a new font triplet.
* @param name font name
@@ -43,9 +44,21 @@
* @param weight font weight (100, 200, 300...800, 900)
*/
public FontTriplet(String name, String style, int weight) {
+ this(name, style, weight, 0);
+ }
+
+ /**
+ * Creates a new font triplet.
+ * @param name font name
+ * @param style font style (normal, italic etc.)
+ * @param weight font weight (100, 200, 300...800, 900)
+ * @param priority priority of this triplet/font mapping
+ */
+ public FontTriplet(String name, String style, int weight, int priority) {
this.name = name;
this.style = style;
this.weight = weight;
+ this.priority = priority;
}
/** @return the font name */
@@ -61,6 +74,11 @@
/** @return the font weight */
public int getWeight() {
return weight;
+ }
+
+ /** @return the priority of this triplet/font mapping */
+ public int getPriority() {
+ return priority;
}
private String getKey() {
Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontUtil.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontUtil.java?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontUtil.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/FontUtil.java Tue Feb
19 05:08:39 2008
@@ -77,14 +77,18 @@
}
/** font constituent names which identify a font as being of "italic"
style */
- private static final String[] ITALIC_WORDS = {"italic", "oblique"};
+ private static final String[] ITALIC_WORDS = {"italic", "oblique",
"inclined"};
/** font constituent names which identify a font as being of "light"
weight */
private static final String[] LIGHT_WORDS = {"light"};
+ /** font constituent names which identify a font as being of "medium"
weight */
+ private static final String[] MEDIUM_WORDS = {"medium"};
+ /** font constituent names which identify a font as being of "demi/semi"
weight */
+ private static final String[] DEMI_WORDS = {"demi", "semi"};
/** font constituent names which identify a font as being of "bold" weight
*/
private static final String[] BOLD_WORDS = {"bold"};
- /** font constituent names which identify a font as being of "bold" weight
*/
- private static final String[] EXTRA_BOLD_WORDS = {"extrabold", "black",
+ /** font constituent names which identify a font as being of "extra bold"
weight */
+ private static final String[] EXTRA_BOLD_WORDS = {"extrabold", "extra
bold", "black",
"heavy", "ultra", "super"};
/**
@@ -109,9 +113,24 @@
public static int guessWeight(String fontName) {
// weight
int weight = Font.WEIGHT_NORMAL;
+
for (int i = 0; i < BOLD_WORDS.length; i++) {
if (fontName.indexOf(BOLD_WORDS[i]) != -1) {
weight = Font.WEIGHT_BOLD;
+ break;
+ }
+ }
+ for (int i = 0; i < MEDIUM_WORDS.length; i++) {
+ if (fontName.indexOf(MEDIUM_WORDS[i]) != -1) {
+ weight = Font.WEIGHT_NORMAL + 100; //500
+ break;
+ }
+ }
+ //Search for "semi/demi" before "light", but after "bold"
+ //(normally semi/demi-bold is meant, but it can also be
semi/demi-light)
+ for (int i = 0; i < DEMI_WORDS.length; i++) {
+ if (fontName.indexOf(DEMI_WORDS[i]) != -1) {
+ weight = Font.WEIGHT_BOLD - 100; //600
break;
}
}
Modified:
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
---
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
(original)
+++
xmlgraphics/fop/trunk/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
Tue Feb 19 05:08:39 2008
@@ -31,6 +31,7 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+
import org.apache.fop.fonts.CachedFontInfo;
import org.apache.fop.fonts.CustomFont;
import org.apache.fop.fonts.EmbedFontInfo;
@@ -66,28 +67,32 @@
// default style and weight triplet vales (fallback)
String strippedName = stripQuotes(customFont.getStrippedFontName());
- String subName = customFont.getFontSubName();
- String searchName = strippedName.toLowerCase();
- if (subName != null) {
- searchName += subName.toLowerCase();
- }
-
+ //String subName = customFont.getFontSubName();
+ String fullName = stripQuotes(customFont.getFullName());
+ String searchName = fullName.toLowerCase();
+
String style = guessStyle(customFont, searchName);
int weight = FontUtil.guessWeight(searchName);
//Full Name usually includes style/weight info so don't use these
traits
//If we still want to use these traits, we have to make
FontInfo.fontLookup() smarter
- String fullName = stripQuotes(customFont.getFullName());
- triplets.add(new FontTriplet(fullName, Font.STYLE_NORMAL,
Font.WEIGHT_NORMAL));
+ triplets.add(new FontTriplet(fullName, Font.STYLE_NORMAL,
Font.WEIGHT_NORMAL, 0));
if (!fullName.equals(strippedName)) {
- triplets.add(new FontTriplet(strippedName, Font.STYLE_NORMAL,
Font.WEIGHT_NORMAL));
+ triplets.add(new FontTriplet(strippedName, Font.STYLE_NORMAL,
Font.WEIGHT_NORMAL, 0));
}
Set familyNames = customFont.getFamilyNames();
Iterator iter = familyNames.iterator();
while (iter.hasNext()) {
String familyName = stripQuotes((String)iter.next());
if (!fullName.equals(familyName)) {
- triplets.add(new FontTriplet(familyName, style, weight));
+ /* Heuristic:
+ * The more similar the family name to the full font name,
+ * the higher the priority of its triplet.
+ * (Lower values indicate higher priorities.) */
+ int priority = fullName.startsWith(familyName)
+ ? fullName.length() - familyName.length()
+ : fullName.length();
+ triplets.add(new FontTriplet(familyName, style, weight,
priority));
}
}
}
Modified:
xmlgraphics/fop/trunk/src/java/org/apache/fop/render/PrintRenderer.java
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/render/PrintRenderer.java?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/render/PrintRenderer.java
(original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/render/PrintRenderer.java Tue
Feb 19 05:08:39 2008
@@ -78,7 +78,7 @@
}
/**
- * Returns the internal font key fot a font triplet coming from the area
tree
+ * Returns the internal font key for a font triplet coming from the area
tree
* @param area the area from which to retrieve the font triplet information
* @return the internal font key (F1, F2 etc.) or null if not found
*/
Modified: xmlgraphics/fop/trunk/status.xml
URL:
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/status.xml?rev=629093&r1=629092&r2=629093&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/status.xml (original)
+++ xmlgraphics/fop/trunk/status.xml Tue Feb 19 05:08:39 2008
@@ -28,6 +28,10 @@
<changes>
<release version="FOP Trunk">
+ <action context="Fonts" dev="JM" type="fix" fixed-bug="44451"
due-to="Justus Piater">
+ Improved the font auto-detection so fonts accessed using the
font-family name are
+ selected with higher accuracy.
+ </action>
<action context="API" dev="JM" type="remove">
Removed deprecated methods in the "apps" package that were left-overs
from the API
discussions.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]