Skip to content

Commit

Permalink
We still should use the font file when loading the font lazy. Otherwise
Browse files Browse the repository at this point in the history
the font file gets fully loaded into memory.
  • Loading branch information
rototor committed Apr 24, 2018
1 parent e55e602 commit 7a4456e
Showing 1 changed file with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
import org.apache.fontbox.ttf.TrueTypeCollection.TrueTypeFontProcessor;
import org.apache.fontbox.ttf.TrueTypeFont;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.*;

import java.io.*;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.logging.Level;

Expand Down Expand Up @@ -187,10 +193,26 @@ private void addFont(TrueTypeFont trueTypeFont, String fontFamilyNameOverride,

PDFont font = PDType0Font.load(_doc, trueTypeFont, subset);

addFont(font, fontFamilyNameOverride, fontWeightOverride, fontStyleOverride, subset);
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;
}
}

private void addFont(PDFont font, String fontFamilyNameOverride, Integer fontWeightOverride, IdentValue fontStyleOverride, boolean subset) {
/**
* Add a font with a lazy loaded PDFont
*/
private void addFontLazy(FSSupplier<PDFont> font, String fontFamilyNameOverride, Integer fontWeightOverride, IdentValue fontStyleOverride, boolean subset) {
FontFamily<FontDescription> fontFamily = getFontFamily(fontFamilyNameOverride);
FontDescription descr = new FontDescription(
_doc,
Expand Down Expand Up @@ -263,21 +285,26 @@ public void addFont(File fontFile, final String fontFamilyNameOverride, final In
/*
* We load the font using the file.
*/
addFont(new FontFileSupplier(fontFile), fontFamilyNameOverride, fontWeightOverride, fontStyleOverride, subset);
addFontLazy(new FilePDFontSupplier(fontFile, _doc), fontFamilyNameOverride, fontWeightOverride, fontStyleOverride, subset);
}

private static class FontFileSupplier implements FSSupplier<InputStream> {
private final File file;
/**
* Loads a Type0 font on demand
*/
private static class FilePDFontSupplier implements FSSupplier<PDFont> {
private final File _fontFile;
private final PDDocument _doc;

FontFileSupplier(File file) {
this.file = file;
FilePDFontSupplier(File fontFile, PDDocument doc) {
this._fontFile = fontFile;
this._doc = doc;
}

@Override
public InputStream supply() {
public PDFont supply() {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
return PDType0Font.load(_doc, _fontFile);
} catch (IOException e) {
return null;
}
}
Expand Down Expand Up @@ -562,6 +589,7 @@ public static class FontDescription implements MinimalFontDescription {
private final PDDocument _doc;

private FSSupplier<InputStream> _supplier;
private FSSupplier<PDFont> _fontSupplier;
private PDFont _font;

private float _underlinePosition;
Expand All @@ -576,10 +604,6 @@ private FontDescription(PDFont font, IdentValue style, int weight) {
this(null, font, style, weight);
}

public FontDescription(PDDocument doc, PDFont font) {
this(doc, font, IdentValue.NORMAL, 400);
}

private FontDescription(PDDocument doc, FSSupplier<InputStream> supplier, int weight, IdentValue style) {
this._supplier = supplier;
this._weight = weight;
Expand All @@ -595,8 +619,21 @@ private FontDescription(PDDocument doc, PDFont font, IdentValue style, int weigh
_doc = doc;
setMetricDefaults();
}


private FontDescription(PDDocument doc, FSSupplier<PDFont> fontSupplier, IdentValue style, int weight) {
_fontSupplier = fontSupplier;
_style = style;
_weight = weight;
_supplier = null;
_doc = doc;
}

private boolean realizeFont(boolean subset) {
if (_font == null && _fontSupplier != null) {
_font = _fontSupplier.supply();
setMetricDefaults();
_fontSupplier = null;
}
if (_font == null && _supplier != null) {
InputStream is = _supplier.supply();
_supplier = null; // We only try once.
Expand Down

0 comments on commit 7a4456e

Please sign in to comment.