[EMAIL PROTECTED] wrote on Mon, 18 Feb 2008 08:16:12 +0100:
> Having pondered this for a while, I now think this would actually be
> the cleaner solution. I'll soon send another patch.
This was easy... I do like this one better. Jeremias?
Justus.
Index: src/java/org/apache/fop/render/PrintRenderer.java
===================================================================
--- src/java/org/apache/fop/render/PrintRenderer.java (revision 628254)
+++ src/java/org/apache/fop/render/PrintRenderer.java (working copy)
@@ -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
*/
Index: src/java/org/apache/fop/fonts/FontUtil.java
===================================================================
--- src/java/org/apache/fop/fonts/FontUtil.java (revision 628254)
+++ src/java/org/apache/fop/fonts/FontUtil.java (working copy)
@@ -82,7 +82,7 @@
/** 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 "bold" weight */
- private static final String[] BOLD_WORDS = {"bold"};
+ private static final String[] BOLD_WORDS = {"bold", "medium", "demi"};
/** font constituent names which identify a font as being of "bold" weight */
private static final String[] EXTRA_BOLD_WORDS = {"extrabold", "black",
"heavy", "ultra", "super"};
Index: src/java/org/apache/fop/fonts/FontTriplet.java
===================================================================
--- src/java/org/apache/fop/fonts/FontTriplet.java (revision 628254)
+++ src/java/org/apache/fop/fonts/FontTriplet.java (working copy)
@@ -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 */
@@ -63,6 +76,11 @@
return weight;
}
+ /** @return the priority of this triplet/font mapping */
+ public int getPriority() {
+ return priority;
+ }
+
private String getKey() {
if (this.key == null) {
//This caches the combined key
Index: src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java
===================================================================
--- src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java (revision 628254)
+++ src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java (working copy)
@@ -67,27 +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 fullName = stripQuotes(customFont.getFullName());
+ String searchName = fullName.toLowerCase();
+
String style = guessStyle(customFont, searchName);
+ // Isn't weight information contained in the font file??:
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));
}
}
}
Index: src/java/org/apache/fop/fonts/FontInfo.java
===================================================================
--- src/java/org/apache/fop/fonts/FontInfo.java (revision 628254)
+++ src/java/org/apache/fop/fonts/FontInfo.java (working copy)
@@ -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,9 +112,45 @@
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 + ")");
+ }
+ }
+
/**
* Adds font metrics for a specific font.
* @param name internal key
Index: src/java/org/apache/fop/fonts/FontResolver.java
===================================================================
--- src/java/org/apache/fop/fonts/FontResolver.java (revision 628254)
+++ src/java/org/apache/fop/fonts/FontResolver.java (working copy)
@@ -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.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]