Search in sources :

Example 1 with PDVectorFont

use of org.apache.pdfbox.pdmodel.font.PDVectorFont in project pdfbox by apache.

the class DebugPageDrawer method showGlyph.

/**
 * Glyph bounding boxes.
 */
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
    // draw glyph
    super.showGlyph(textRenderingMatrix, font, code, unicode, displacement);
    if (showGlyphBounds) {
        Shape bbox;
        // compute visual bounds
        if (font instanceof PDType3Font) {
            // todo: implement me
            return;
        } else {
            AffineTransform at = textRenderingMatrix.createAffineTransform();
            at.concatenate(font.getFontMatrix().createAffineTransform());
            // get the path
            PDVectorFont vectorFont = (PDVectorFont) font;
            GeneralPath path = vectorFont.getNormalizedPath(code);
            if (path == null) {
                return;
            }
            // stretch non-embedded glyph if it does not match the width contained in the PDF
            if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code)) {
                float fontWidth = font.getWidthFromFont(code);
                if (// ignore spaces
                fontWidth > 0 && Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001) {
                    float pdfWidth = displacement.getX() * 1000;
                    at.scale(pdfWidth / fontWidth, 1);
                }
            }
            Shape glyph = at.createTransformedShape(path);
            bbox = glyph.getBounds2D();
        }
        // save
        Graphics2D graphics = getGraphics();
        Color color = graphics.getColor();
        Stroke stroke = graphics.getStroke();
        Shape clip = graphics.getClip();
        // draw
        graphics.setClip(graphics.getDeviceConfiguration().getBounds());
        graphics.setColor(Color.cyan);
        graphics.setStroke(new BasicStroke(.5f));
        graphics.draw(bbox);
        // restore
        graphics.setStroke(stroke);
        graphics.setColor(color);
        graphics.setClip(clip);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) BasicStroke(java.awt.BasicStroke) Shape(java.awt.Shape) PDType3Font(org.apache.pdfbox.pdmodel.font.PDType3Font) GeneralPath(java.awt.geom.GeneralPath) Color(java.awt.Color) AffineTransform(java.awt.geom.AffineTransform) PDVectorFont(org.apache.pdfbox.pdmodel.font.PDVectorFont) Graphics2D(java.awt.Graphics2D)

Example 2 with PDVectorFont

use of org.apache.pdfbox.pdmodel.font.PDVectorFont in project pdfbox by apache.

the class PageDrawer method showFontGlyph.

@Override
protected void showFontGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
    AffineTransform at = textRenderingMatrix.createAffineTransform();
    at.concatenate(font.getFontMatrix().createAffineTransform());
    // create cache if it does not exist
    PDVectorFont vectorFont = ((PDVectorFont) font);
    GlyphCache cache = glyphCaches.get(font);
    if (cache == null) {
        cache = new GlyphCache(vectorFont);
        glyphCaches.put(font, cache);
    }
    GeneralPath path = cache.getPathForCharacterCode(code);
    drawGlyph(path, font, code, displacement, at);
}
Also used : GeneralPath(java.awt.geom.GeneralPath) AffineTransform(java.awt.geom.AffineTransform) PDVectorFont(org.apache.pdfbox.pdmodel.font.PDVectorFont)

Example 3 with PDVectorFont

use of org.apache.pdfbox.pdmodel.font.PDVectorFont in project pdfbox by apache.

the class DrawPrintTextLocations method calculateGlyphBounds.

// this calculates the real (except for type 3 fonts) individual glyph bounds
private Shape calculateGlyphBounds(Matrix textRenderingMatrix, PDFont font, int code) throws IOException {
    GeneralPath path = null;
    AffineTransform at = textRenderingMatrix.createAffineTransform();
    at.concatenate(font.getFontMatrix().createAffineTransform());
    if (font instanceof PDType3Font) {
        // It is difficult to calculate the real individual glyph bounds for type 3 fonts
        // because these are not vector fonts, the content stream could contain almost anything
        // that is found in page content streams.
        PDType3Font t3Font = (PDType3Font) font;
        PDType3CharProc charProc = t3Font.getCharProc(code);
        if (charProc != null) {
            BoundingBox fontBBox = t3Font.getBoundingBox();
            PDRectangle glyphBBox = charProc.getGlyphBBox();
            if (glyphBBox != null) {
                // PDFBOX-3850: glyph bbox could be larger than the font bbox
                glyphBBox.setLowerLeftX(Math.max(fontBBox.getLowerLeftX(), glyphBBox.getLowerLeftX()));
                glyphBBox.setLowerLeftY(Math.max(fontBBox.getLowerLeftY(), glyphBBox.getLowerLeftY()));
                glyphBBox.setUpperRightX(Math.min(fontBBox.getUpperRightX(), glyphBBox.getUpperRightX()));
                glyphBBox.setUpperRightY(Math.min(fontBBox.getUpperRightY(), glyphBBox.getUpperRightY()));
                path = glyphBBox.toGeneralPath();
            }
        }
    } else if (font instanceof PDVectorFont) {
        PDVectorFont vectorFont = (PDVectorFont) font;
        path = vectorFont.getPath(code);
        if (font instanceof PDTrueTypeFont) {
            PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
            int unitsPerEm = ttFont.getTrueTypeFont().getHeader().getUnitsPerEm();
            at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
        }
        if (font instanceof PDType0Font) {
            PDType0Font t0font = (PDType0Font) font;
            if (t0font.getDescendantFont() instanceof PDCIDFontType2) {
                int unitsPerEm = ((PDCIDFontType2) t0font.getDescendantFont()).getTrueTypeFont().getHeader().getUnitsPerEm();
                at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
            }
        }
    } else if (font instanceof PDSimpleFont) {
        PDSimpleFont simpleFont = (PDSimpleFont) font;
        // these two lines do not always work, e.g. for the TT fonts in file 032431.pdf
        // which is why PDVectorFont is tried first.
        String name = simpleFont.getEncoding().getName(code);
        path = simpleFont.getPath(name);
    } else {
        // shouldn't happen, please open issue in JIRA
        System.out.println("Unknown font class: " + font.getClass());
    }
    if (path == null) {
        return null;
    }
    return at.createTransformedShape(path.getBounds2D());
}
Also used : PDType3CharProc(org.apache.pdfbox.pdmodel.font.PDType3CharProc) GeneralPath(java.awt.geom.GeneralPath) PDType3Font(org.apache.pdfbox.pdmodel.font.PDType3Font) PDType0Font(org.apache.pdfbox.pdmodel.font.PDType0Font) BoundingBox(org.apache.fontbox.util.BoundingBox) PDTrueTypeFont(org.apache.pdfbox.pdmodel.font.PDTrueTypeFont) AffineTransform(java.awt.geom.AffineTransform) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDCIDFontType2(org.apache.pdfbox.pdmodel.font.PDCIDFontType2) PDVectorFont(org.apache.pdfbox.pdmodel.font.PDVectorFont) PDSimpleFont(org.apache.pdfbox.pdmodel.font.PDSimpleFont)

Aggregations

AffineTransform (java.awt.geom.AffineTransform)3 GeneralPath (java.awt.geom.GeneralPath)3 PDVectorFont (org.apache.pdfbox.pdmodel.font.PDVectorFont)3 PDType3Font (org.apache.pdfbox.pdmodel.font.PDType3Font)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 Shape (java.awt.Shape)1 Stroke (java.awt.Stroke)1 BoundingBox (org.apache.fontbox.util.BoundingBox)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 PDCIDFontType2 (org.apache.pdfbox.pdmodel.font.PDCIDFontType2)1 PDSimpleFont (org.apache.pdfbox.pdmodel.font.PDSimpleFont)1 PDTrueTypeFont (org.apache.pdfbox.pdmodel.font.PDTrueTypeFont)1 PDType0Font (org.apache.pdfbox.pdmodel.font.PDType0Font)1 PDType3CharProc (org.apache.pdfbox.pdmodel.font.PDType3CharProc)1