From 56387de039f142b731d6ced52db6c79cb422824b Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Tue, 19 Feb 2008 15:47:48 +0000 Subject: [PATCH] Added support for reading the OS/2 table's usWeightClass value which supports the same font weight values as we use in XSL-FO. However, in my tests these values proved to be unreliable (like ExtraBlack fonts returning 400). I just hooked the whole thing in so this work isn't lost if anyone has an idea to make it work. The FontInfoFinder will continue to only use guessed font weights for now. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@629131 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/fonts/CustomFont.java | 21 +++++++++++++++++++ .../fop/fonts/autodetect/FontInfoFinder.java | 6 +++++- .../apache/fop/fonts/truetype/TTFFile.java | 12 ++++++++++- .../fop/fonts/truetype/TTFFontLoader.java | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/fop/fonts/CustomFont.java b/src/java/org/apache/fop/fonts/CustomFont.java index 0ac3b80bc..f6bb52cb3 100644 --- a/src/java/org/apache/fop/fonts/CustomFont.java +++ b/src/java/org/apache/fop/fonts/CustomFont.java @@ -46,6 +46,7 @@ public abstract class CustomFont extends Typeface private int descender = 0; private int[] fontBBox = {0, 0, 0, 0}; private int flags = 4; + private int weight = 0; //0 means unknown weight private int stemV = 0; private int italicAngle = 0; private int missingWidth = 0; @@ -196,6 +197,15 @@ public abstract class CustomFont extends Typeface return flags; } + /** + * Returns the font weight (100, 200...800, 900). This value may be different from the + * one that was actually used to register the font. + * @return the font weight (or 0 if the font weight is unknown) + */ + public int getWeight() { + return this.weight; + } + /** * {@inheritDoc} */ @@ -349,6 +359,17 @@ public abstract class CustomFont extends Typeface this.flags = flags; } + /** + * Sets the font weight. Valid values are 100, 200...800, 900. + * @param weight the font weight + */ + public void setWeight(int weight) { + weight = (weight / 100) * 100; + weight = Math.max(100, weight); + weight = Math.min(900, weight); + this.weight = weight; + } + /** * {@inheritDoc} */ diff --git a/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java b/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java index 1ec8a5528..5fc0525c3 100644 --- a/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java +++ b/src/java/org/apache/fop/fonts/autodetect/FontInfoFinder.java @@ -72,7 +72,11 @@ public class FontInfoFinder { String searchName = fullName.toLowerCase(); String style = guessStyle(customFont, searchName); - int weight = FontUtil.guessWeight(searchName); + int weight; //= customFont.getWeight(); + int guessedWeight = FontUtil.guessWeight(searchName); + //We always take the guessed weight for now since it yield much better results. + //OpenType's OS/2 usWeightClass value proves to be unreliable. + weight = guessedWeight; //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 diff --git a/src/java/org/apache/fop/fonts/truetype/TTFFile.java b/src/java/org/apache/fop/fonts/truetype/TTFFile.java index 86a1d75e2..e60722331 100644 --- a/src/java/org/apache/fop/fonts/truetype/TTFFile.java +++ b/src/java/org/apache/fop/fonts/truetype/TTFFile.java @@ -105,6 +105,7 @@ public class TTFFile { //Ascender/descender from OS/2 table private int os2Ascender = 0; private int os2Descender = 0; + private int usWeightClass = 0; private short lastChar = 0; @@ -620,6 +621,13 @@ public class TTFFile { return flags; } + /** + * Returns the weight class of this font. Valid values are 100, 200....,800, 900. + * @return the weight class value (or 0 if there was no OS/2 table in the font) + */ + public int getWeightClass() { + return this.usWeightClass; + } /** * Returns the StemV attribute of the font. @@ -978,7 +986,9 @@ public class TTFFile { private final void readOS2(FontFileReader in) throws IOException { // Check if font is embeddable if (dirTabs.get("OS/2") != null) { - seekTab(in, "OS/2", 2 * 4); + seekTab(in, "OS/2", 2 * 2); + this.usWeightClass = in.readTTFUShort(); + in.skip(2); int fsType = in.readTTFUShort(); if (fsType == 2) { isEmbeddable = false; diff --git a/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java b/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java index b29aff808..60a6948fc 100644 --- a/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java +++ b/src/java/org/apache/fop/fonts/truetype/TTFFontLoader.java @@ -90,6 +90,7 @@ public class TTFFontLoader extends FontLoader { returnFont.setStemV(Integer.parseInt(ttf.getStemV())); //not used for TTF returnFont.setItalicAngle(Integer.parseInt(ttf.getItalicAngle())); returnFont.setMissingWidth(0); + returnFont.setWeight(ttf.getWeightClass()); multiFont.setCIDType(CIDFontType.CIDTYPE2); int[] wx = ttf.getWidths(); -- 2.39.5