danfickle / openhtmltopdf

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!
https://danfickle.github.io/pdf-templates/index.html
Other
1.93k stars 359 forks source link

Font weight and style resolving inconsistencies #868

Open HerbertsVaadin opened 2 years ago

HerbertsVaadin commented 2 years ago

In com.openhtmltopdf.svgsupport.PDFTranscoder.OpenHtmlFontResolver two getWeight methods resolve the font-weight:400 to different weight values.

private Float getWeight(Integer weight) {
    if (weight == null) {
        return null;
    }

    switch (weight.intValue()) {
    ...
    case 400:
        return TextAttribute.WEIGHT_REGULAR;
    ...
    }
}
Float getWeight(IdentValue weight) {
    if (weight == IdentValue.NORMAL) {
        return TextAttribute.WEIGHT_REGULAR;
    ...
    } else if (weight == IdentValue.FONT_WEIGHT_400) {
        return TextAttribute.WEIGHT_MEDIUM;
    } ...

}

WEIGHT_REGULAR=Float.valueOf(1.0f); WEIGHT_MEDIUM=Float.valueOf(1.5f);

Similar problem for resolving styles. Normal resolves to null here:

private Float getStyle(IdentValue fontStyle) {
    if (fontStyle == IdentValue.ITALIC ||
        fontStyle == IdentValue.OBLIQUE)
        return TextAttribute.POSTURE_OBLIQUE;

    return null;
}

Normal resolves to 0f here (which is the preffered value).

private Float getStyle(FontStyle style) {
    switch (style) {
    case ITALIC:
    case OBLIQUE:
        return TextAttribute.POSTURE_OBLIQUE;
    case NORMAL:
    default:
        return 0f;
    }
}

This causes the font not being interpreted correctly for drawing SVGs.