/** Parse the Widths array and DW object */
private void parseWidths(PDFObject fontObj)
throws IOException {
// read the default width (otpional)
PDFObject defaultWidthObj = fontObj.getDictRef("DW");
if (defaultWidthObj != null && defaultWidthObj.getIntValue() != 0) {
// XOND: commented out the setting of new default width, as several
// PDFs are displayed in a wrong format due to this:
// this.defaultWidth = defaultWidthObj.getIntValue();
}
int entryIdx = 0;
int first = 0;
int last = 0;
PDFObject[] widthArray;
// read the widths table
PDFObject widthObj = fontObj.getDictRef("W");
if (widthObj != null) {
// initialize the widths array
this.widths = new HashMap<Character, Float>();
// parse the width array
widthArray = widthObj.getArray();
/* an entry can be in one of two forms:
* <startIndex> <endIndex> <value> or
* <startIndex> [ array of values ]
* we use the entryIdx to differentitate between them
*/
for (int i = 0; i < widthArray.length; i++) {
if (entryIdx == 0) {
// first value in an entry. Just store it
first = widthArray[i].getIntValue();
} else if (entryIdx == 1) {
// second value -- is it an int or array?
if (widthArray[i].getType() == PDFObject.ARRAY) {
// add all the entries in the array to the width array
PDFObject[] entries = widthArray[i].getArray();
for (int c = 0; c < entries.length; c++) {
Character key = Character.valueOf((char) (c + first));
// value is width / default width
float value = entries[c].getIntValue();
this.widths.put(key, new Float(value));
}
// all done
entryIdx = -1;
} else {
last = widthArray[i].getIntValue();
}
} else {
// third value. Set a range
int value = widthArray[i].getIntValue();
// set the range
for (int c = first; c <= last; c++) {
this.widths.put(Character.valueOf((char) c), new Float(value));
}
// all done
entryIdx = -1;
}
entryIdx++;
}
}
// read the optional vertical default width
defaultWidthObj = fontObj.getDictRef("DW2");
if (defaultWidthObj != null) {
this.defaultWidthVertical = defaultWidthObj.getIntValue();
}
// read the vertical widths table
widthObj = fontObj.getDictRef("W2");
if (widthObj != null) {
// initialize the widths array
this.widthsVertical = new HashMap<Character, Float>();
// parse the width2 array
widthArray = widthObj.getArray();
/* an entry can be in one of two forms:
* <startIndex> <endIndex> <value> or
* <startIndex> [ array of values ]
* we use the entryIdx to differentitate between them