Search in sources :

Example 1 with Vector

use of org.apache.pdfbox.util.Vector in project pdfbox by apache.

the class PDCIDFont method getPositionVector.

@Override
public Vector getPositionVector(int code) {
    int cid = codeToCID(code);
    Vector v = positionVectors.get(cid);
    if (v == null) {
        v = getDefaultPositionVector(cid);
    }
    return v;
}
Also used : Vector(org.apache.pdfbox.util.Vector)

Example 2 with Vector

use of org.apache.pdfbox.util.Vector in project pdfbox by apache.

the class PDFStreamEngine method showText.

/**
 * 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(// 0
    fontSize * horizontalScaling, // 0
    0, // 0
    0, // 0
    fontSize, 0, // 1
    textState.getRise());
    // 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);
        // 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, 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));
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) Matrix(org.apache.pdfbox.util.Matrix) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDTextState(org.apache.pdfbox.pdmodel.graphics.state.PDTextState) COSString(org.apache.pdfbox.cos.COSString) Vector(org.apache.pdfbox.util.Vector) PDGraphicsState(org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState)

Example 3 with Vector

use of org.apache.pdfbox.util.Vector in project pdfbox by apache.

the class PDCIDFont method readVerticalDisplacements.

private void readVerticalDisplacements() {
    // default position vector and vertical displacement vector
    COSBase dw2Base = dict.getDictionaryObject(COSName.DW2);
    if (dw2Base instanceof COSArray) {
        COSArray dw2Array = (COSArray) dw2Base;
        COSBase base0 = dw2Array.getObject(0);
        COSBase base1 = dw2Array.getObject(1);
        if (base0 instanceof COSNumber && base1 instanceof COSNumber) {
            dw2[0] = ((COSNumber) base0).floatValue();
            dw2[1] = ((COSNumber) base1).floatValue();
        }
    }
    // vertical metrics for individual CIDs.
    COSBase w2Base = dict.getDictionaryObject(COSName.W2);
    if (w2Base instanceof COSArray) {
        COSArray w2Array = (COSArray) w2Base;
        for (int i = 0; i < w2Array.size(); i++) {
            COSNumber c = (COSNumber) w2Array.getObject(i);
            COSBase next = w2Array.getObject(++i);
            if (next instanceof COSArray) {
                COSArray array = (COSArray) next;
                for (int j = 0; j < array.size(); j++) {
                    int cid = c.intValue() + j / 3;
                    COSNumber w1y = (COSNumber) array.getObject(j);
                    COSNumber v1x = (COSNumber) array.getObject(++j);
                    COSNumber v1y = (COSNumber) array.getObject(++j);
                    verticalDisplacementY.put(cid, w1y.floatValue());
                    positionVectors.put(cid, new Vector(v1x.floatValue(), v1y.floatValue()));
                }
            } else {
                int first = c.intValue();
                int last = ((COSNumber) next).intValue();
                COSNumber w1y = (COSNumber) w2Array.getObject(++i);
                COSNumber v1x = (COSNumber) w2Array.getObject(++i);
                COSNumber v1y = (COSNumber) w2Array.getObject(++i);
                for (int cid = first; cid <= last; cid++) {
                    verticalDisplacementY.put(cid, w1y.floatValue());
                    positionVectors.put(cid, new Vector(v1x.floatValue(), v1y.floatValue()));
                }
            }
        }
    }
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) Vector(org.apache.pdfbox.util.Vector)

Aggregations

Vector (org.apache.pdfbox.util.Vector)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 COSArray (org.apache.pdfbox.cos.COSArray)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSNumber (org.apache.pdfbox.cos.COSNumber)1 COSString (org.apache.pdfbox.cos.COSString)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 PDGraphicsState (org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState)1 PDTextState (org.apache.pdfbox.pdmodel.graphics.state.PDTextState)1 Matrix (org.apache.pdfbox.util.Matrix)1