浏览代码

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
tags/fop-0_95beta
Jeremias Maerki 16 年前
父节点
当前提交
56387de039

+ 21
- 0
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}
*/

+ 5
- 1
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

+ 11
- 1
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;

+ 1
- 0
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();

正在加载...
取消
保存