org.apache.pdfbox.util.Vector Java Examples

The following examples show how to use org.apache.pdfbox.util.Vector. 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: PDFVisibleTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
void deleteCharsInPath() {
    for (List<TextPosition> list : charactersByArticle) {
        List<TextPosition> toRemove = new ArrayList<>();
        for (TextPosition text : list) {
            Matrix textMatrix = text.getTextMatrix();
            Vector start = textMatrix.transform(new Vector(0, 0));
            Vector end = new Vector(start.getX() + text.getWidth(), start.getY());
            if (linePath.contains(lowerLeftX + start.getX(), lowerLeftY + start.getY()) ||
                    (checkEndPointToo && linePath.contains(lowerLeftX + end.getX(), lowerLeftY + end.getY()))) {
                toRemove.add(text);
            }
        }
        if (toRemove.size() != 0) {
            System.out.println(toRemove.size());
            list.removeAll(toRemove);
        }
    }
}
 
Example #2
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected void showType3Glyph(Matrix textRenderingMatrix, PDType3Font font, int code,
        String unicode, Vector displacement) throws IOException
{
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();
    if (!RenderingMode.NEITHER.equals(renderingMode))
    {
        super.showType3Glyph(textRenderingMatrix, font, code, unicode, displacement);
    }
}
 
Example #3
Source File: PDFVisibleTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void processTextPosition(TextPosition text) {
    Matrix textMatrix = text.getTextMatrix();
    Vector start = textMatrix.transform(new Vector(0, 0));
    Vector end = new Vector(start.getX() + text.getWidth(), start.getY());

    PDGraphicsState gs = getGraphicsState();
    Area area = gs.getCurrentClippingPath();
    if (area == null ||
            (contains(area, lowerLeftX + start.getX(), lowerLeftY + start.getY()) &&
                    ((!checkEndPointToo) || contains(area, lowerLeftX + end.getX(), lowerLeftY + end.getY()))))
        super.processTextPosition(text);
    else if (dropStream != null)
        dropStream.printf("Clipped '%s' at %s,%s\n", text.getUnicode(), lowerLeftX + start.getX(), lowerLeftY + start.getY());
}
 
Example #4
Source File: PdfToTextInfoConverter.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void processTextPosition(TextPosition text) {
    PDGraphicsState gs = getGraphicsState();
    // check opacity for stroke and fill text 
    if (gs.getAlphaConstant() < Constants.EPSILON && gs.getNonStrokeAlphaConstant() < Constants.EPSILON) {
        return;
    }                       

    Vector center = getTextPositionCenterPoint(text);
    Area area = gs.getCurrentClippingPath();
    if (area == null || area.contains(lowerLeftX + center.getX(), lowerLeftY + center.getY())) {            
        nonStrokingColors.put(text, gs.getNonStrokingColor());
        super.processTextPosition(text);
    }
}
 
Example #5
Source File: PdfToTextInfoConverter.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
private Integer getCharacterBackgroundColor(TextPosition text) {
    Integer fillColorRgb = null;
    try {           
        for (Map.Entry<GeneralPath, PDColor> filledPath : filledPaths.entrySet()) {
            Vector center = getTextPositionCenterPoint(text);
            if (filledPath.getKey().contains(lowerLeftX + center.getX(), lowerLeftY + center.getY())) {
                fillColorRgb = filledPath.getValue().toRGB();                   
            }
        }
    } catch (IOException e) {
        logger.error("Could not convert color to RGB", e);
    }
    return fillColorRgb;
}
 
Example #6
Source File: PDCIDFont.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Vector getPositionVector(int code)
{
    int cid = codeToCID(code);
    Vector v = positionVectors.get(cid);
    if (v == null)
    {
        v = getDefaultPositionVector(cid);
    }
    return v;
}
 
Example #7
Source File: BoundingBoxFinder.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, Vector displacement)
        throws IOException {
    super.showGlyph(textRenderingMatrix, font, code, displacement);
    Shape shape = calculateGlyphBounds(textRenderingMatrix, font, code);
    if (shape != null) {
        Rectangle2D rect = shape.getBounds2D();
        add(rect);
    }
}
 
Example #8
Source File: PDType0Font.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public Vector getDisplacement(int code) throws IOException
{
    if (isVertical())
    {
        return new Vector(0, descendantFont.getVerticalDisplacementVectorY(code) / 1000f);
    }
    else
    {
        return super.getDisplacement(code);
    }
}
 
Example #9
Source File: PageVerticalAnalyzer.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, Vector displacement)
        throws IOException {
    super.showGlyph(textRenderingMatrix, font, code, displacement);
    Shape shape = calculateGlyphBounds(textRenderingMatrix, font, code);
    if (shape != null) {
        Rectangle2D rect = shape.getBounds2D();
        addVerticalUseSection(rect.getMinY(), rect.getMaxY());
    }
}
 
Example #10
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@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());

    Glyph2D glyph2D = createGlyph2D(font);
    drawGlyph2D(glyph2D, font, code, displacement, at);
}
 
Example #11
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Called when a glyph is to be processed.This method is intended for overriding in subclasses,
 * the default implementation does nothing.
 *
 * @param textRenderingMatrix the current text rendering matrix, T<sub>rm</sub>
 * @param font the current font
 * @param code internal PDF character code for the glyph
 * @param unicode the Unicode text for this glyph, or null if the PDF does provide it
 * @param displacement the displacement (i.e. advance) of the glyph in text space
 * @throws IOException if the glyph cannot be processed
 */
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode,
                         Vector displacement) throws IOException
{
    if (font instanceof PDType3Font)
    {
        showType3Glyph(textRenderingMatrix, (PDType3Font)font, code, unicode, displacement);
    }
    else
    {
        showFontGlyph(textRenderingMatrix, font, code, unicode, displacement);
    }
}
 
Example #12
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Process text from the PDF Stream. You should override this method if you want to
 * perform an action when encoded text is being processed.
 *
 * @param string the encoded text
 * @throws IOException if there is an error processing the string
 */
protected void showText(byte[] string) throws IOException
{
    PDGraphicsState state = getGraphicsState();
    PDTextState textState = state.getTextState();

    // get the current font
    PDFont font = textState.getFont();
    if (font == null)
    {
        LOG.warn("No current font, will use default");
        font = PDFontFactory.createDefaultFont();
    }

    float fontSize = textState.getFontSize();
    float horizontalScaling = textState.getHorizontalScaling() / 100f;
    float charSpacing = textState.getCharacterSpacing();

    // put the text state parameters into matrix form
    Matrix parameters = new Matrix(
            fontSize * horizontalScaling, 0, // 0
            0, fontSize,                     // 0
            0, textState.getRise());         // 1

    // read the stream until it is empty
    InputStream in = new ByteArrayInputStream(string);
    while (in.available() > 0)
    {
        // decode a character
        int before = in.available();
        int code = font.readCode(in);
        int codeLength = before - in.available();
        String unicode = font.toUnicode(code);

        // Word spacing shall be applied to every occurrence of the single-byte character code
        // 32 in a string when using a simple font or a composite font that defines code 32 as
        // a single-byte code.
        float wordSpacing = 0;
        if (codeLength == 1 && code == 32)
        {
            wordSpacing += textState.getWordSpacing();
        }

        // text rendering matrix (text space -> device space)
        Matrix ctm = state.getCurrentTransformationMatrix();
        Matrix textRenderingMatrix = parameters.multiply(textMatrix).multiply(ctm);

        // get glyph's position vector if this is vertical text
        // changes to vertical text should be tested with PDFBOX-2294 and PDFBOX-1422
        if (font.isVertical())
        {
            // position vector, in text space
            Vector v = font.getPositionVector(code);

            // apply the position vector to the horizontal origin to get the vertical origin
            textRenderingMatrix.translate(v);
        }

        // get glyph's horizontal and vertical displacements, in text space
        Vector w = font.getDisplacement(code);

        // process the decoded glyph
        saveGraphicsState();
        Matrix textMatrixOld = textMatrix;
        Matrix textLineMatrixOld = textLineMatrix;
        showGlyph(textRenderingMatrix, font, code, unicode, w);
        textMatrix = textMatrixOld;
        textLineMatrix = textLineMatrixOld;
        restoreGraphicsState();

        // calculate the combined displacements
        float tx;
        float ty;
        if (font.isVertical())
        {
            tx = 0;
            ty = w.getY() * fontSize + charSpacing + wordSpacing;
        }
        else
        {
            tx = (w.getX() * fontSize + charSpacing + wordSpacing) * horizontalScaling;
            ty = 0;
        }

        // update the text matrix
        textMatrix.concatenate(Matrix.getTranslateInstance(tx, ty));
    }
}
 
Example #13
Source File: SimpleXObjectTextStripper.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, Vector displacement)
        throws IOException {
    stringBuilder.append(font.toUnicode(code));
}
 
Example #14
Source File: PDType3Font.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Vector getDisplacement(int code) throws IOException
{
    return getFontMatrix().transform(new Vector(getWidth(code), 0));
}
 
Example #15
Source File: PDType0Font.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Vector getPositionVector(int code)
{
    // units are always 1/1000 text space, font matrix is not used, see FOP-2252
    return descendantFont.getPositionVector(code).scale(-1 / 1000f);
}
 
Example #16
Source File: PDFont.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Vector getPositionVector(int code)
{
    throw new UnsupportedOperationException("Horizontal fonts have no position vector");
}
 
Example #17
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Render the font using the Glyph2D interface.
 * 
 * @param glyph2D the Glyph2D implementation provided a GeneralPath for each glyph
 * @param font the font
 * @param code character code
 * @param displacement the glyph's displacement (advance)
 * @param at the transformation
 * @throws IOException if something went wrong
 */
private void drawGlyph2D(Glyph2D glyph2D, PDFont font, int code, Vector displacement,
                         AffineTransform at) throws IOException
{
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();

    GeneralPath path = glyph2D.getPathForCharacterCode(code);
    if (path != null)
    {
        // Stretch non-embedded glyph if it does not match the height/width contained in the PDF.
        // Vertical fonts have zero X displacement, so the following code scales to 0 if we don't skip it.
        // TODO: How should vertical fonts be handled?
        if (!font.isEmbedded() && !font.isVertical() && !font.isStandard14() && font.hasExplicitWidth(code))
        {
            float fontWidth = font.getWidthFromFont(code);
            if (fontWidth > 0 && // ignore spaces
                    Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001)
            {
                float pdfWidth = displacement.getX() * 1000;
                at.scale(pdfWidth / fontWidth, 1);
            }
        }

        // render glyph
        Shape glyph = at.createTransformedShape(path);

        if (renderingMode.isFill())
        {
            graphics.setComposite(state.getNonStrokingJavaComposite());
            graphics.setPaint(getNonStrokingPaint());
            setClip();
            if (isContentRendered())
            {
                graphics.fill(glyph);
            }
        }

        if (renderingMode.isStroke())
        {
            graphics.setComposite(state.getStrokingJavaComposite());
            graphics.setPaint(getStrokingPaint());
            graphics.setStroke(getStroke());
            setClip();
            if (isContentRendered())
            {
                graphics.draw(glyph);
            }
        }

        if (renderingMode.isClip())
        {
            textClippings.add(glyph);
        }
    }
}
 
Example #18
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Called when a glyph is to be processed.This method is intended for overriding in subclasses,
 * the default implementation does nothing.
 *
 * @param textRenderingMatrix the current text rendering matrix, T<sub>rm</sub>
 * @param font the current font
 * @param code internal PDF character code for the glyph
 * @param unicode the Unicode text for this glyph, or null if the PDF does provide it
 * @param displacement the displacement (i.e. advance) of the glyph in text space
 * @throws IOException if the glyph cannot be processed
 */
protected void showType3Glyph(Matrix textRenderingMatrix, PDType3Font font, int code,
                              String unicode, Vector displacement) throws IOException
{
    PDType3CharProc charProc = font.getCharProc(code);
    if (charProc != null)
    {
        processType3Stream(charProc, textRenderingMatrix);
    }
}
 
Example #19
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Called when a glyph is to be processed.This method is intended for overriding in subclasses,
 * the default implementation does nothing.
 *
 * @param textRenderingMatrix the current text rendering matrix, T<sub>rm</sub>
 * @param font the current font
 * @param code internal PDF character code for the glyph
 * @param unicode the Unicode text for this glyph, or null if the PDF does provide it
 * @param displacement the displacement (i.e. advance) of the glyph in text space
 * @throws IOException if the glyph cannot be processed
 */
protected void showFontGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode,
                             Vector displacement) throws IOException
{
    // overridden in subclasses
}
 
Example #20
Source File: PDCIDFont.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the default position vector (v).
 *
 * @param cid CID
 */
private Vector getDefaultPositionVector(int cid)
{
    return new Vector(getWidthForCID(cid) / 2, dw2[0]);
}
 
Example #21
Source File: PDFont.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the displacement vector (w0, w1) in text space, for the given character.
 * For horizontal text only the x component is used, for vertical text only the y component.
 *
 * @param code character code
 * @return displacement vector
 */
public Vector getDisplacement(int code) throws IOException
{
    return new Vector(getWidth(code) / 1000, 0);
}
 
Example #22
Source File: PDFontLike.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the position vector (v), in text space, for the given character.
 * This represents the position of vertical origin relative to horizontal origin, for
 * horizontal writing it will always be (0, 0). For vertical writing both x and y are set.
 *
 * @param code character code
 * @return position vector
 */
Vector getPositionVector(int code);