All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.fonts.Dict Maven / Gradle / Ivy

/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2017 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
 @LICENSE@
 *
 * ---------------
 * Dict.java
 * ---------------
 */
package org.jpedal.fonts;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class Dict {

	private int pos;
	private final byte[] data;
	public LinkedHashMap entries = new LinkedHashMap();

	public Dict privateDict;
	public Dict[] fontDicts;
	public byte[][] subrsIndexData;
	public int ascent = 1000;
	public int descent;
	public int[] widths;
	public CIDCharset charset;
	public CIDEncoding encoding;
	public CIDFDSelect fdSelect;

	public LinkedHashMap trackers = new LinkedHashMap();

	Dict(final byte[] data) {
		List operands = new ArrayList();
		this.data = data;
		final int end = data.length;
		while (pos < end) {
			int b = data[pos] & 0xff;
			if (b <= 21) {
				if (b == 12) {
					b = (b << 8) | (data[++pos] & 0xff);
				}
				Number[] operandsArr = new Number[operands.size()];
				operandsArr = operands.toArray(operandsArr);
				entries.put(b, operandsArr);
				operands = new ArrayList();
				++pos;
			} else {
				operands.add(parseOperand());
			}
		}
	}

	private Number parseOperand() {
		int value = data[pos++] & 0xff;
		if (value == 30) {
			return parseFloatOperand();
		} else if (value == 28) {
			value = data[pos++] & 0xff;
			value = ((value << 24) | ((data[pos++] & 0xff) << 16)) >> 16;
			return value;
		} else if (value == 29) {
			value = data[pos++] & 0xff;
			value = (value << 8) | (data[pos++] & 0xff);
			value = (value << 8) | (data[pos++] & 0xff);
			value = (value << 8) | (data[pos++] & 0xff);
			return value;
		} else if (value >= 32 && value <= 246) {
			return value - 139;
		} else if (value >= 247 && value <= 250) {
			return ((value - 247) * 256) + (data[pos++] & 0xff) + 108;
		} else if (value >= 251 && value <= 254) {
			return -((value - 251) * 256) - (data[pos++] & 0xff) - 108;
		}
		return null;
	}

	private float parseFloatOperand() {
		final StringBuilder str = new StringBuilder();
		final int eof = 15;
		final String[] lookup = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "E", "E-", null, "-"};
		final int length = data.length;
		while (pos < length) {
			final int b = data[pos++] & 0xff;
			final int b1 = b >> 4;
			final int b2 = b & 15;

			if (b1 == eof) {
				break;
			}
			str.append(lookup[b1]);

			if (b2 == eof) {
				break;
			}
			str.append(lookup[b2]);
		}
		String res = str.toString();
			
		if (res.isEmpty()) {
			return 0;
		} else if (res.startsWith("E")) {
			res = "1" + res;
		}
		return Float.parseFloat(res);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy