Skip to content

Commit

Permalink
Added proper font-weight and font-style handling with @font-face rule.
Browse files Browse the repository at this point in the history
This change fixes some issues when specifying a specific font-style or
font-weight through @font-face. For example, when importing a special font
resource specifically for the italic font-style, the PDF output would not
match the correct font if the css rule specifies the style/weight.
  • Loading branch information
Jan Habermann committed Dec 13, 2014
1 parent 476da4d commit 9f12922
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,26 @@ public boolean hasFontFamily() {

return false;
}

public boolean hasFontWeight() {
for (Iterator i = _ruleset.getPropertyDeclarations().iterator(); i.hasNext(); ) {
PropertyDeclaration decl = (PropertyDeclaration)i.next();
if (decl.getPropertyName().equals("font-weight")) {
return true;
}
}

return false;
}

public boolean hasFontStyle() {
for (Iterator i = _ruleset.getPropertyDeclarations().iterator(); i.hasNext(); ) {
PropertyDeclaration decl = (PropertyDeclaration)i.next();
if (decl.getPropertyName().equals("font-style")) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,25 @@ public void importFontFaces(List fontFaces) {
}

boolean embedded = style.isIdent(CSSName.FS_PDF_FONT_EMBED, IdentValue.EMBED);

String encoding = style.getStringProperty(CSSName.FS_PDF_FONT_ENCODING);

String fontFamily = null;
IdentValue fontWeight = null;
IdentValue fontStyle = null;

if (rule.hasFontFamily()) {
fontFamily = style.valueByName(CSSName.FONT_FAMILY).asString();
}

if (rule.hasFontWeight()) {
fontWeight = style.getIdent(CSSName.FONT_WEIGHT);
}

if (rule.hasFontStyle()) {
fontStyle = style.getIdent(CSSName.FONT_STYLE);
}

try {
addFontFaceFont(fontFamily, src.asString(), encoding, embedded, font1, font2);
addFontFaceFont(fontFamily, fontWeight, fontStyle, src.asString(), encoding, embedded, font1, font2);
} catch (DocumentException e) {
XRLog.exception("Could not load font " + src.asString(), e);
continue;
Expand Down Expand Up @@ -243,7 +253,7 @@ public void addFont(String path, String fontFamilyNameOverride,
}

private void addFontFaceFont(
String fontFamilyNameOverride, String uri, String encoding, boolean embedded, byte[] afmttf, byte[] pfb)
String fontFamilyNameOverride, IdentValue fontWeightOverride, IdentValue fontStyleOverride, String uri, String encoding, boolean embedded, byte[] afmttf, byte[] pfb)
throws DocumentException, IOException {
String lower = uri.toLowerCase();
if (lower.endsWith(".otf") || lower.endsWith(".ttf") || lower.indexOf(".ttc,") != -1) {
Expand All @@ -268,6 +278,14 @@ private void addFontFaceFont(

descr.setFromFontFace(true);

if (fontWeightOverride != null) {
descr.setWeight(convertWeightToInt(fontWeightOverride));
}

if (fontStyleOverride != null) {
descr.setStyle(fontStyleOverride);
}

fontFamily.addFontDescription(descr);
}
} else if (lower.endsWith(".afm") || lower.endsWith(".pfm") || lower.endsWith(".pfb") || lower.endsWith(".pfa")) {
Expand Down Expand Up @@ -379,6 +397,7 @@ private FSFont resolveFont(SharedContext ctx, String fontFamily, float size, Ide

String cacheKey = getHashName(normalizedFontFamily, weight, style);
FontDescription result = (FontDescription)_fontCache.get(cacheKey);

if (result != null) {
return new ITextFSFont(result, size);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ public void drawString(String s, float x, float y, JustificationInfo info) {
if (fontSpec != null) {
int need = ITextFontResolver.convertWeightToInt(fontSpec.fontWeight);
int have = desc.getWeight();

if (need > have) {
cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
float lineWidth = fontSize * 0.04f; // 4% of font size
Expand Down

0 comments on commit 9f12922

Please sign in to comment.