Skip to content

Commit

Permalink
Merge pull request #384 from DSW-PS/open-dev-v1
Browse files Browse the repository at this point in the history
useFont/addFont with PDFontSupplier (FSSupplier<PDFont>)
  • Loading branch information
danfickle authored Sep 16, 2019
2 parents 69d767a + 6d1f9bc commit 5d4b37e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.openhtmltopdf.pdfboxout;

import org.apache.pdfbox.pdmodel.font.PDFont;

import com.openhtmltopdf.extend.FSSupplier;

/**
* Implementation of {@link FSSupplier} that supplies a {@link PDFont}.<br>
* Subclass this if you need special font loading rules (like using a font-cache, ...).
*/
public class PDFontSupplier implements FSSupplier<PDFont> {

// this used to be a private static class in PdfBoxFontResolver
private PDFont _font;

public PDFontSupplier(PDFont font) {
this._font = font;
}

@Override
public PDFont supply() {
return _font;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,6 @@ private void addFont(TrueTypeFont trueTypeFont, String fontFamilyNameOverride,
addFontLazy(new PDFontSupplier(font), fontFamilyNameOverride, fontWeightOverride, fontStyleOverride, subset);
}

private static class PDFontSupplier implements FSSupplier<PDFont> {
private final PDFont _font;

PDFontSupplier(PDFont font) {
_font = font;
}

@Override
public PDFont supply() {
return _font;
}
}

/**
* Add a font with a lazy loaded PDFont
*/
Expand Down Expand Up @@ -316,7 +303,6 @@ public PDFont supply() {
}
}


/**
* Add a font using a InputStream. The given file must be a TrueType Font
* (.ttf). If you know the underlying stream is a .ttc file you should use
Expand Down Expand Up @@ -345,6 +331,35 @@ public void addFont(FSSupplier<InputStream> supplier, String fontFamilyNameOverr
}
}

/**
* Add a font using a <b>PDFontSupplier</b>. Use this method if you need special rules for font-loading (like using a font-cache)
* and subclass the {@link PDFontSupplier}.
*/
public void addFont(PDFontSupplier supplier, String fontFamilyNameOverride, Integer fontWeightOverride,
IdentValue fontStyleOverride, boolean subset) {
// would have prefered to used FSSupplier<PDFont> but sadly that would give us an error
// because the type-ereasure clashes with addFont(FSSupplier<InputStream> ...)
FontFamily<FontDescription> fontFamily = getFontFamily(fontFamilyNameOverride);

FontDescription descr = new FontDescription(
_doc,
supplier,
normalizeFontStyle(fontStyleOverride),
normalizeFontWeight(fontWeightOverride),
fontFamilyNameOverride,
false, // isFromFontFace
subset,
_fontMetricsCache);

if (!subset) {
if (descr.realizeFont()) {
fontFamily.addFontDescription(descr);
}
} else {
fontFamily.addFontDescription(descr);
}
}

private int normalizeFontWeight(IdentValue fontWeight) {
return fontWeight != null ? FontResolverHelper.convertWeightToInt(fontWeight) : 400;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,16 @@ public PdfBoxRenderer buildPdfRenderer() {
fontStyle = IdentValue.OBLIQUE;
}

// use InputStream supplier
if (font.supplier != null) {
resolver.addFont(font.supplier, font.family, font.weight, fontStyle, font.subset);
} else {
}
// use PDFont supplier
else if (font.pdfontSupplier != null) {
resolver.addFont(font.pdfontSupplier, font.family, font.weight, fontStyle, font.subset);
}
// load via font File
else {
try {
resolver.addFont(font.fontFile, font.family, font.weight, fontStyle, font.subset);
} catch (Exception e) {
Expand Down Expand Up @@ -187,7 +194,7 @@ public PdfRendererBuilder usePDDocument(PDDocument doc) {
*/
public PdfRendererBuilder useFont(FSSupplier<InputStream> supplier, String fontFamily, Integer fontWeight,
FontStyle fontStyle, boolean subset) {
state._fonts.add(new AddedFont(supplier, null, fontWeight, fontFamily, subset, fontStyle));
state._fonts.add(new AddedFont(supplier, null, null, fontWeight, fontFamily, subset, fontStyle));
return this;
}

Expand All @@ -211,7 +218,7 @@ public PdfRendererBuilder useFont(FSSupplier<InputStream> supplier, String fontF
*/
public PdfRendererBuilder useFont(File fontFile, String fontFamily, Integer fontWeight, FontStyle fontStyle,
boolean subset) {
state._fonts.add(new AddedFont(null, fontFile, fontWeight, fontFamily, subset, fontStyle));
state._fonts.add(new AddedFont(null, null, fontFile, fontWeight, fontFamily, subset, fontStyle));
return this;
}

Expand All @@ -227,6 +234,24 @@ public PdfRendererBuilder useFont(File fontFile, String fontFamily) {
return this.useFont(fontFile, fontFamily, 400, FontStyle.NORMAL, true);
}

/**
* Like {@link #useFont(FSSupplier, String, Integer, FontStyle, boolean)} but
* allows to supply a PDFont directly. Subclass {@link PDFontSupplier} if you need
* special font-loading rules (like using a font-cache).
*/
public PdfRendererBuilder useFont(PDFontSupplier supplier, String fontFamily, Integer fontWeight,
FontStyle fontStyle, boolean subset) {
state._fonts.add(new AddedFont(null, supplier, null, fontWeight, fontFamily, subset, fontStyle));
return this;
}

/**
* Simpler overload for
* {@link #useFont(PDFontSupplier, String, Integer, FontStyle, boolean)}
*/
public PdfRendererBuilder useFont(PDFontSupplier supplier, String fontFamily) {
return this.useFont(supplier, fontFamily, 400, FontStyle.NORMAL, true);
}

/**
* Set a producer on the output document
Expand Down Expand Up @@ -278,14 +303,16 @@ public PdfRendererBuilder usePageSupplier(PageSupplier pageSupplier) {

static class AddedFont {
private final FSSupplier<InputStream> supplier;
private final PDFontSupplier pdfontSupplier;
private final File fontFile;
private final Integer weight;
private final String family;
private final boolean subset;
private final FontStyle style;

private AddedFont(FSSupplier<InputStream> supplier, File fontFile, Integer weight, String family,
boolean subset, FontStyle style) {
private AddedFont(FSSupplier<InputStream> supplier, PDFontSupplier pdfontSupplier, File fontFile, Integer weight,
String family, boolean subset, FontStyle style) {
this.pdfontSupplier = pdfontSupplier;
this.supplier = supplier;
this.fontFile = fontFile;
this.weight = weight;
Expand Down

0 comments on commit 5d4b37e

Please sign in to comment.