Search in sources :

Example 51 with COSNumber

use of org.apache.pdfbox.cos.COSNumber 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)

Example 52 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class PDCIDFont method readWidths.

private void readWidths() {
    widths = new HashMap<>();
    COSBase wBase = dict.getDictionaryObject(COSName.W);
    if (wBase instanceof COSArray) {
        COSArray wArray = (COSArray) wBase;
        int size = wArray.size();
        int counter = 0;
        while (counter < size) {
            COSNumber firstCode = (COSNumber) wArray.getObject(counter++);
            COSBase next = wArray.getObject(counter++);
            if (next instanceof COSArray) {
                COSArray array = (COSArray) next;
                int startRange = firstCode.intValue();
                int arraySize = array.size();
                for (int i = 0; i < arraySize; i++) {
                    COSNumber width = (COSNumber) array.getObject(i);
                    widths.put(startRange + i, width.floatValue());
                }
            } else {
                COSNumber secondCode = (COSNumber) next;
                COSNumber rangeWidth = (COSNumber) wArray.getObject(counter++);
                int startRange = firstCode.intValue();
                int endRange = secondCode.intValue();
                float width = rangeWidth.floatValue();
                for (int i = startRange; i <= endRange; i++) {
                    widths.put(i, width);
                }
            }
        }
    }
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase)

Example 53 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class PDDefaultAppearanceString method processSetFont.

/**
 * Process the set font and font size operator.
 *
 * @param operands the font name and size
 * @throws IOException in case there are missing operators or the font is not within the resources
 */
private void processSetFont(List<COSBase> operands) throws IOException {
    if (operands.size() < 2) {
        throw new IOException("Missing operands for set font operator " + Arrays.toString(operands.toArray()));
    }
    COSBase base0 = operands.get(0);
    COSBase base1 = operands.get(1);
    if (!(base0 instanceof COSName)) {
        return;
    }
    if (!(base1 instanceof COSNumber)) {
        return;
    }
    COSName fontName = (COSName) base0;
    PDFont font = defaultResources.getFont(fontName);
    float fontSize = ((COSNumber) base1).floatValue();
    // todo: handle cases where font == null with special mapping logic (see PDFBOX-2661)
    if (font == null) {
        throw new IOException("Could not find font: /" + fontName.getName());
    }
    setFontName(fontName);
    setFont(font);
    setFontSize(fontSize);
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) COSName(org.apache.pdfbox.cos.COSName) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 54 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class COSArrayList method convertIntegerCOSArrayToList.

/**
 * This will take an array of COSNumbers and return a COSArrayList of
 * java.lang.Integer values.
 *
 * @param intArray The existing integer Array.
 *
 * @return A list that is part of the core Java collections.
 */
public static List<Integer> convertIntegerCOSArrayToList(COSArray intArray) {
    List<Integer> retval = null;
    if (intArray != null) {
        List<Integer> numbers = new ArrayList<>();
        for (int i = 0; i < intArray.size(); i++) {
            COSNumber num;
            if (intArray.get(i) instanceof COSObject) {
                num = (COSNumber) ((COSObject) intArray.get(i)).getObject();
            } else {
                num = (COSNumber) intArray.get(i);
            }
            numbers.add(num.intValue());
        }
        retval = new COSArrayList<>(numbers, intArray);
    }
    return retval;
}
Also used : COSInteger(org.apache.pdfbox.cos.COSInteger) COSObject(org.apache.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSNumber(org.apache.pdfbox.cos.COSNumber)

Example 55 with COSNumber

use of org.apache.pdfbox.cos.COSNumber in project pdfbox by apache.

the class PDStructureElement method getAttributes.

/**
 * Returns the attributes together with their revision numbers (A).
 *
 * @return the attributes as a list, never null.
 */
public Revisions<PDAttributeObject> getAttributes() {
    Revisions<PDAttributeObject> attributes = new Revisions<>();
    COSBase a = this.getCOSObject().getDictionaryObject(COSName.A);
    if (a instanceof COSArray) {
        COSArray aa = (COSArray) a;
        Iterator<COSBase> it = aa.iterator();
        PDAttributeObject ao = null;
        while (it.hasNext()) {
            COSBase item = it.next();
            if (item instanceof COSDictionary) {
                ao = PDAttributeObject.create((COSDictionary) item);
                ao.setStructureElement(this);
                attributes.addObject(ao, 0);
            } else if (item instanceof COSInteger) {
                attributes.setRevisionNumber(ao, ((COSNumber) item).intValue());
            }
        }
    }
    if (a instanceof COSDictionary) {
        PDAttributeObject ao = PDAttributeObject.create((COSDictionary) a);
        ao.setStructureElement(this);
        attributes.addObject(ao, 0);
    }
    return attributes;
}
Also used : COSInteger(org.apache.pdfbox.cos.COSInteger) COSArray(org.apache.pdfbox.cos.COSArray) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase)

Aggregations

COSNumber (org.apache.pdfbox.cos.COSNumber)61 COSBase (org.apache.pdfbox.cos.COSBase)29 MissingOperandException (org.apache.pdfbox.contentstream.operator.MissingOperandException)18 COSArray (org.apache.pdfbox.cos.COSArray)18 COSInteger (org.apache.pdfbox.cos.COSInteger)10 Point2D (java.awt.geom.Point2D)6 IOException (java.io.IOException)6 COSObject (org.apache.pdfbox.cos.COSObject)5 PDBorderStyleDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)5 ArrayList (java.util.ArrayList)4 COSDictionary (org.apache.pdfbox.cos.COSDictionary)4 COSName (org.apache.pdfbox.cos.COSName)4 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)4 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)4 COSFloat (org.apache.pdfbox.cos.COSFloat)3 COSStream (org.apache.pdfbox.cos.COSStream)2 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)2 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 Matrix (org.apache.pdfbox.util.Matrix)2 Paint (java.awt.Paint)1