Example usage for org.apache.pdfbox.cos COSArray getInt

List of usage examples for org.apache.pdfbox.cos COSArray getInt

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSArray getInt.

Prototype

public int getInt(int index) 

Source Link

Document

Get the value of the array as an integer.

Usage

From source file:net.padaf.preflight.font.CompositeFontValidator.java

License:Apache License

/**
 * For a CIDFont the width array, there are two formats of width array :
 * <UL>//  w  w  w .j a  v a 2  s  .  c  om
 * <li>C [W1...Wn] : C is an integer specifying a starting CID value and the
 * array of n numbers specify widths for n consecutive CIDs.
 * <li>Cf Cl W : Defines the same width W for the range Cf to Cl
 * </UL>
 * This method gets a linked hash map of width where the key is a CID and the
 * value is the Width.
 * 
 * @return
 * @throws ValidationException
 */
protected LinkedHashMap<Integer, Integer> getWidthsArray() throws ValidationException {
    LinkedHashMap<Integer, Integer> widthsMap = new LinkedHashMap<Integer, Integer>();
    COSDocument cDoc = handler.getDocument().getDocument();
    COSBase cBase = this.cidFont.getItem(COSName.getPDFName("W"));
    COSArray wArr = COSUtils.getAsArray(cBase, cDoc);

    for (int i = 0; i < wArr.size();) {

        int firstCid = wArr.getInt(i);

        if (i + 1 >= wArr.size()) {
            throw new ValidationException("Invalid format of the W entry");
        }

        COSBase cb = wArr.getObject(i + 1);
        if (COSUtils.isArray(cb, cDoc)) {

            // ---- First Format
            COSArray seqWidths = COSUtils.getAsArray(cb, cDoc);
            widthsMap.put(firstCid, seqWidths.getInt(0));
            for (int jw = 1; jw < seqWidths.size(); jw++) {
                widthsMap.put((firstCid + jw), seqWidths.getInt(jw));
            }

            i = i + 2;

        } else {

            // ---- Second Format
            if (i + 2 >= wArr.size()) {
                throw new ValidationException("Invalid format of the W entry");
            }

            int lastCid = wArr.getInt(i + 1);
            int commonWidth = wArr.getInt(i + 2);
            for (int jw = firstCid; jw <= lastCid; ++jw) {
                widthsMap.put((firstCid + jw), commonWidth);
            }

            i = i + 3;

        }

    }

    return widthsMap;
}