001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.svggen.font.table;
020:
021: import java.io.ByteArrayInputStream;
022:
023: /**
024: * @version $Id: GlyfSimpleDescript.java 475477 2006-11-15 22:44:28Z cam $
025: * @author <a href="mailto:david@steadystate.co.uk">David Schweinsberg</a>
026: */
027: public class GlyfSimpleDescript extends GlyfDescript {
028:
029: private int[] endPtsOfContours;
030: private byte[] flags;
031: private short[] xCoordinates;
032: private short[] yCoordinates;
033: private int count;
034:
035: public GlyfSimpleDescript(GlyfTable parentTable,
036: short numberOfContours, ByteArrayInputStream bais) {
037:
038: super (parentTable, numberOfContours, bais);
039:
040: // Simple glyph description
041: endPtsOfContours = new int[numberOfContours];
042: for (int i = 0; i < numberOfContours; i++) {
043: endPtsOfContours[i] = (bais.read() << 8 | bais.read());
044: }
045:
046: // The last end point index reveals the total number of points
047: count = endPtsOfContours[numberOfContours - 1] + 1;
048: flags = new byte[count];
049: xCoordinates = new short[count];
050: yCoordinates = new short[count];
051:
052: int instructionCount = (bais.read() << 8 | bais.read());
053: readInstructions(bais, instructionCount);
054: readFlags(count, bais);
055: readCoords(count, bais);
056: }
057:
058: public int getEndPtOfContours(int i) {
059: return endPtsOfContours[i];
060: }
061:
062: public byte getFlags(int i) {
063: return flags[i];
064: }
065:
066: public short getXCoordinate(int i) {
067: return xCoordinates[i];
068: }
069:
070: public short getYCoordinate(int i) {
071: return yCoordinates[i];
072: }
073:
074: public boolean isComposite() {
075: return false;
076: }
077:
078: public int getPointCount() {
079: return count;
080: }
081:
082: public int getContourCount() {
083: return getNumberOfContours();
084: }
085:
086: /*
087: public int getComponentIndex(int c) {
088: return 0;
089: }
090:
091: public int getComponentCount() {
092: return 1;
093: }
094: */
095: /**
096: * The table is stored as relative values, but we'll store them as absolutes
097: */
098: private void readCoords(int count, ByteArrayInputStream bais) {
099: short x = 0;
100: short y = 0;
101: for (int i = 0; i < count; i++) {
102: if ((flags[i] & xDual) != 0) {
103: if ((flags[i] & xShortVector) != 0) {
104: x += (short) bais.read();
105: }
106: } else {
107: if ((flags[i] & xShortVector) != 0) {
108: x += (short) -((short) bais.read());
109: } else {
110: x += (short) (bais.read() << 8 | bais.read());
111: }
112: }
113: xCoordinates[i] = x;
114: }
115:
116: for (int i = 0; i < count; i++) {
117: if ((flags[i] & yDual) != 0) {
118: if ((flags[i] & yShortVector) != 0) {
119: y += (short) bais.read();
120: }
121: } else {
122: if ((flags[i] & yShortVector) != 0) {
123: y += (short) -((short) bais.read());
124: } else {
125: y += (short) (bais.read() << 8 | bais.read());
126: }
127: }
128: yCoordinates[i] = y;
129: }
130: }
131:
132: /**
133: * The flags are run-length encoded
134: */
135: private void readFlags(int flagCount, ByteArrayInputStream bais) {
136: try {
137: for (int index = 0; index < flagCount; index++) {
138: flags[index] = (byte) bais.read();
139: if ((flags[index] & repeat) != 0) {
140: int repeats = bais.read();
141: for (int i = 1; i <= repeats; i++) {
142: flags[index + i] = flags[index];
143: }
144: index += repeats;
145: }
146: }
147: } catch (ArrayIndexOutOfBoundsException e) {
148: System.out.println("error: array index out of bounds");
149: }
150: }
151: }
|