Java Code Examples for org.apache.fontbox.ttf.TrueTypeFont#close()

The following examples show how to use org.apache.fontbox.ttf.TrueTypeFont#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDTrueTypeFont.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates a new TrueType font for embedding.
 */
private PDTrueTypeFont(PDDocument document, TrueTypeFont ttf, Encoding encoding,
        boolean closeTTF)
        throws IOException
{
    PDTrueTypeFontEmbedder embedder = new PDTrueTypeFontEmbedder(document, dict, ttf,
                                                                 encoding);
    this.encoding = encoding;
    this.ttf = ttf;
    setFontDescriptor(embedder.getFontDescriptor());
    isEmbedded = true;
    isDamaged = false;
    glyphList = GlyphList.getAdobeGlyphList();
    if (closeTTF)
    {
        // the TTF is fully loaded and it is safe to close the underlying data source
        ttf.close();
    }
}
 
Example 2
Source File: PDType0Font.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Private. Creates a new PDType0Font font for embedding.
 *
 * @param document
 * @param ttf
 * @param embedSubset
 * @param closeTTF whether to close the ttf parameter after embedding. Must be true when the ttf
 * parameter was created in the load() method, false when the ttf parameter was passed to the
 * load() method.
 * @param vertical
 * @throws IOException
 */
private PDType0Font(PDDocument document, TrueTypeFont ttf, boolean embedSubset,
        boolean closeTTF, boolean vertical) throws IOException
{
    if (vertical)
    {
        ttf.enableVerticalSubstitutions();
    }
    embedder = new PDCIDFontType2Embedder(document, dict, ttf, embedSubset, this, vertical);
    descendantFont = embedder.getCIDFont();
    readEncoding();
    fetchCMapUCS2();
    if (closeTTF)
    {
        if (embedSubset)
        {
            this.ttf = ttf;
            document.registerTrueTypeFontForClosing(ttf);
        }
        else
        {
            // the TTF is fully loaded and it is safe to close the underlying data source
            ttf.close();
        }
    }
}
 
Example 3
Source File: PDTrueTypeFont.java    From sambox with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new TrueType font for embedding.
 */
private PDTrueTypeFont(TrueTypeFont ttf, Encoding encoding, boolean closeTTF) throws IOException
{
    PDTrueTypeFontEmbedder embedder = new PDTrueTypeFontEmbedder(dict, ttf, encoding);
    this.encoding = encoding;
    this.ttf = ttf;
    setFontDescriptor(embedder.getFontDescriptor());
    isEmbedded = true;
    isDamaged = false;
    glyphList = GlyphList.getAdobeGlyphList();
    if (closeTTF)
    {
        // the TTF is fully loaded and it is save to close the underlying data source
        ttf.close();
    }
}
 
Example 4
Source File: PDType0Font.java    From sambox with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param document
 * @param ttf
 * @param embedSubset
 * @param closeTTF whether to close the ttf parameter after embedding. Must be true when the ttf parameter was
 * created in the load() method, false when the ttf parameter was passed to the load() method.
 * @param vertical
 * @throws IOException
 */
private PDType0Font(PDDocument document, TrueTypeFont ttf, boolean embedSubset,
        boolean closeTTF, boolean vertical) throws IOException
{
    if (vertical)
    {
        ttf.enableVerticalSubstitutions();
    }
    embedder = new PDCIDFontType2Embedder(document, dict, ttf, embedSubset, this, vertical);
    descendantFont = embedder.getCIDFont();
    readEncoding();
    fetchCMapUCS2();
    if (closeTTF)
    {
        if (embedSubset)
        {
            this.ttf = ttf;
            document.registerTrueTypeFontForClosing(ttf);
        }
        else
        {
            // the TTF is fully loaded and it is safe to close the underlying data source
            ttf.close();
        }
    }
}
 
Example 5
Source File: PDFontTest.java    From sambox with Apache License 2.0 6 votes vote down vote up
/**
 * PDFBOX-3826: Test ability to reuse a TrueTypeFont created from a file or a stream for several PDFs to avoid
 * parsing it over and over again. Also check that full or partial embedding is done, and do render and text
 * extraction.
 *
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testPDFBox3826() throws IOException, URISyntaxException
{
    URL url = PDFont.class
            .getResource("/org/sejda/sambox/resources/ttf/LiberationSans-Regular.ttf");
    File fontFile = new File(url.toURI());

    TrueTypeFont ttf1 = new TTFParser().parse(fontFile);
    testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf1), fontFile);
    ttf1.close();

    TrueTypeFont ttf2 = new TTFParser().parse(new FileInputStream(fontFile));
    testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf2), fontFile);
    ttf2.close();
}
 
Example 6
Source File: FontUtils.java    From sejda with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Tries to load the original full font from the system
 */
public PDFont loadOriginal(PDDocument document) {
    String lookupName = fontName.replace("-", " ");

    LOG.debug("Searching the system for a font matching name '{}'", lookupName);

    FontMapping<TrueTypeFont> fontMapping = FontMappers.instance().getTrueTypeFont(lookupName, null);
    if (fontMapping != null && fontMapping.getFont() != null && !fontMapping.isFallback()) {
        TrueTypeFont mappedFont = fontMapping.getFont();

        try {
            LOG.debug("Original font available on the system: {}", fontName);
            return PDType0Font.load(document, mappedFont.getOriginalData());
        } catch (IOException ioe) {
            LOG.warn("Failed to load font from system", ioe);
            try {
                mappedFont.close();
            } catch (IOException e) {
                LOG.warn("Failed closing font", e);
            }
        }
    }

    return null;
}
 
Example 7
Source File: FontUtils.java    From sejda with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Tries to load a similar full font from the system
 */
public PDFont loadSimilar(PDDocument document) {
    String lookupName = fontName.replace("-", " ");

    // Eg: Arial-BoldMT
    PDFontDescriptor descriptor = new PDFontDescriptor(new COSDictionary());
    descriptor.setFontName(fontName.split("-")[0]);
    descriptor.setForceBold(FontUtils.isBold(subsetFont));
    descriptor.setItalic(FontUtils.isItalic(subsetFont));

    LOG.debug(
            "Searching the system for a font matching name '{}' and description [name:{}, bold:{}, italic:{}]",
            lookupName, descriptor.getFontName(), descriptor.isForceBold(), descriptor.isItalic());

    FontMapping<TrueTypeFont> fontMapping = FontMappers.instance().getTrueTypeFont(lookupName, descriptor);
    if (fontMapping != null && fontMapping.getFont() != null) {
        TrueTypeFont mappedFont = fontMapping.getFont();

        try {
            if (fontMapping.isFallback()) {
                LOG.debug("Fallback font available on the system: {} (for {})", mappedFont.getName(), fontName);
            } else {
                LOG.debug("Original font available on the system: {}", fontName);
            }

            return PDType0Font.load(document, mappedFont.getOriginalData());
        } catch (IOException ioe) {
            LOG.warn("Failed to load font from system", ioe);
            try {
                mappedFont.close();
            } catch (Exception e) {
                LOG.warn("Failed closing font", e);
            }
        }
    }

    return null;
}