Skip to content

Commit

Permalink
#251 add debug logs for resolved/unresolved fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Jan 5, 2024
1 parent eb600c4 commit cddb5c5
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 19 deletions.
2 changes: 1 addition & 1 deletion flying-saucer-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-fop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-pdf-itext5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-pdf-osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-pdf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ public boolean isFromFontFace() {
public void setFromFontFace(boolean isFromFontFace) {
_isFromFontFace = isFromFontFace;
}

@Override
public String toString() {
return String.format("Font %s:%s", _font.getPostscriptFontName(), _weight);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public float getSize2D() {
public FontDescription getFontDescription() {
return _font;
}

@Override
public String toString() {
return String.format("%s:%s", _font, _size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.sheet.FontFaceRule;
Expand All @@ -37,9 +39,11 @@
import org.xhtmlrenderer.util.XRLog;
import org.xhtmlrenderer.util.XRRuntimeException;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -52,6 +56,8 @@
import static java.util.Objects.requireNonNull;

public class ITextFontResolver implements FontResolver {
private static final Logger log = LoggerFactory.getLogger(ITextFontResolver.class);

private final Map<String, FontFamily> _fontFamilies = new HashMap<>();
private final Map<String, FontDescription> _fontCache = new HashMap<>();

Expand Down Expand Up @@ -336,24 +342,26 @@ private FontFamily getFontFamily(String fontFamilyName) {
return fontFamily;
}

private FSFont resolveFont(String[] families, float size, IdentValue weight, IdentValue style) {
if (! (style == IdentValue.NORMAL || style == IdentValue.OBLIQUE
private FSFont resolveFont(@Nullable String[] families, float size, IdentValue weight, IdentValue style) {
if (!(style == IdentValue.NORMAL || style == IdentValue.OBLIQUE
|| style == IdentValue.ITALIC)) {
style = IdentValue.NORMAL;
}
if (families != null) {
for (String family : families) {
FSFont font = resolveFont(family, size, weight, style);
if (font != null) {
log.debug("Resolved font {}:{}:{} -> {}", family, weight, style, font);
return font;
}
}
}

log.debug("Could not resolve font {}:{}:{} - fallback to Serif", Arrays.toString(families), weight, style);
return resolveFont("Serif", size, weight, style);
}

private String normalizeFontFamily(String fontFamily) {
String normalizeFontFamily(String fontFamily) {
String result = fontFamily;
// strip off the "s if they are there
if (result.startsWith("\"")) {
Expand All @@ -380,10 +388,11 @@ else if (result.equalsIgnoreCase("monospace")) {
private FSFont resolveFont(String fontFamily, float size, IdentValue weight, IdentValue style) {
String normalizedFontFamily = normalizeFontFamily(fontFamily);

String cacheKey = getHashName(normalizedFontFamily, weight, style);
String cacheKey = String.format("%s-%s-%s", normalizedFontFamily, weight, style);
FontDescription result = _fontCache.get(cacheKey);

if (result != null) {
log.debug("Resolved font {}:{}:{} -> {}", fontFamily, weight, style, result);
return new ITextFSFont(result, size);
}

Expand Down Expand Up @@ -432,11 +441,6 @@ public static int convertWeightToInt(IdentValue weight) {
throw new IllegalArgumentException("Cannot convert weight to integer: " + weight);
}

protected static String getHashName(
String name, IdentValue weight, IdentValue style) {
return name + "-" + weight + "-" + style;
}

protected Map<String, FontFamily> loadFonts() {
Map<String, FontFamily> result = new HashMap<>();
addCourier(result);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.xhtmlrenderer.pdf;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ITextFontResolverTest {
private final ITextFontResolver resolver = new ITextFontResolver();

@Test
void normalizeFontFamily() {
assertThat(resolver.normalizeFontFamily("ArialUnicodeMS")).isEqualTo("ArialUnicodeMS");
assertThat(resolver.normalizeFontFamily("\"ArialUnicodeMS")).isEqualTo("ArialUnicodeMS");
assertThat(resolver.normalizeFontFamily("ArialUnicodeMS\"")).isEqualTo("ArialUnicodeMS");
assertThat(resolver.normalizeFontFamily("\"ArialUnicodeMS\"")).isEqualTo("ArialUnicodeMS");
}

@Test
void normalizeFontFamily_serif() {
assertThat(resolver.normalizeFontFamily("serif")).isEqualTo("Serif");
assertThat(resolver.normalizeFontFamily("SERIF")).isEqualTo("Serif");
assertThat(resolver.normalizeFontFamily("sErIf")).isEqualTo("Serif");
}

@Test
void normalizeFontFamily_sans_serif() {
assertThat(resolver.normalizeFontFamily("sans-serif")).isEqualTo("SansSerif");
assertThat(resolver.normalizeFontFamily("SANS-serif")).isEqualTo("SansSerif");
assertThat(resolver.normalizeFontFamily("sans-SERIF")).isEqualTo("SansSerif");
assertThat(resolver.normalizeFontFamily("\"sans-serif")).isEqualTo("SansSerif");
assertThat(resolver.normalizeFontFamily("sans-serif\"")).isEqualTo("SansSerif");
assertThat(resolver.normalizeFontFamily("\"sans-serif\"")).isEqualTo("SansSerif");
}

@Test
void normalizeFontFamily_monospace() {
assertThat(resolver.normalizeFontFamily("monospace")).isEqualTo("Monospaced");
assertThat(resolver.normalizeFontFamily("MONOSPACE")).isEqualTo("Monospaced");
assertThat(resolver.normalizeFontFamily("\"monospace\"")).isEqualTo("Monospaced");
}
}
2 changes: 1 addition & 1 deletion flying-saucer-swt-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion flying-saucer-swt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.4.0</version>
<version>9.4.1-SNAPSHOT</version>

<packaging>pom</packaging>

Expand Down

0 comments on commit cddb5c5

Please sign in to comment.