KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > font > table > GlyfSimpleDescript


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen.font.table;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21
22 /**
23  * @version $Id: GlyfSimpleDescript.java,v 1.4 2004/08/18 07:15:21 vhardy Exp $
24  * @author <a HREF="mailto:david@steadystate.co.uk">David Schweinsberg</a>
25  */

26 public class GlyfSimpleDescript extends GlyfDescript {
27
28     private int[] endPtsOfContours;
29     private byte[] flags;
30     private short[] xCoordinates;
31     private short[] yCoordinates;
32     private int count;
33
34     public GlyfSimpleDescript(GlyfTable parentTable, short numberOfContours, ByteArrayInputStream JavaDoc bais) {
35
36         super(parentTable, numberOfContours, bais);
37         
38         // Simple glyph description
39
endPtsOfContours = new int[numberOfContours];
40         for (int i = 0; i < numberOfContours; i++) {
41             endPtsOfContours[i] = (bais.read()<<8 | bais.read());
42         }
43
44         // The last end point index reveals the total number of points
45
count = endPtsOfContours[numberOfContours-1] + 1;
46         flags = new byte[count];
47         xCoordinates = new short[count];
48         yCoordinates = new short[count];
49
50         int instructionCount = (bais.read()<<8 | bais.read());
51         readInstructions(bais, instructionCount);
52         readFlags(count, bais);
53         readCoords(count, bais);
54     }
55
56     public int getEndPtOfContours(int i) {
57         return endPtsOfContours[i];
58     }
59
60     public byte getFlags(int i) {
61         return flags[i];
62     }
63
64     public short getXCoordinate(int i) {
65         return xCoordinates[i];
66     }
67
68     public short getYCoordinate(int i) {
69         return yCoordinates[i];
70     }
71
72     public boolean isComposite() {
73         return false;
74     }
75
76     public int getPointCount() {
77         return count;
78     }
79
80     public int getContourCount() {
81         return getNumberOfContours();
82     }
83     /*
84     public int getComponentIndex(int c) {
85     return 0;
86     }
87
88     public int getComponentCount() {
89     return 1;
90     }
91      */

92     /**
93      * The table is stored as relative values, but we'll store them as absolutes
94      */

95     private void readCoords(int count, ByteArrayInputStream JavaDoc bais) {
96         short x = 0;
97         short y = 0;
98         for (int i = 0; i < count; i++) {
99             if ((flags[i] & xDual) != 0) {
100                 if ((flags[i] & xShortVector) != 0) {
101                     x += (short) bais.read();
102                 }
103             } else {
104                 if ((flags[i] & xShortVector) != 0) {
105                     x += (short) -((short) bais.read());
106                 } else {
107                     x += (short)(bais.read()<<8 | bais.read());
108                 }
109             }
110             xCoordinates[i] = x;
111         }
112
113         for (int i = 0; i < count; i++) {
114             if ((flags[i] & yDual) != 0) {
115                 if ((flags[i] & yShortVector) != 0) {
116                     y += (short) bais.read();
117                 }
118             } else {
119                 if ((flags[i] & yShortVector) != 0) {
120                     y += (short) -((short) bais.read());
121                 } else {
122                     y += (short)(bais.read()<<8 | bais.read());
123                 }
124             }
125             yCoordinates[i] = y;
126         }
127     }
128
129     /**
130      * The flags are run-length encoded
131      */

132     private void readFlags(int flagCount, ByteArrayInputStream JavaDoc bais) {
133         try {
134             for (int index = 0; index < flagCount; index++) {
135                 flags[index] = (byte) bais.read();
136                 if ((flags[index] & repeat) != 0) {
137                     int repeats = bais.read();
138                     for (int i = 1; i <= repeats; i++) {
139                         flags[index + i] = flags[index];
140                     }
141                     index += repeats;
142                 }
143             }
144         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
145             System.out.println("error: array index out of bounds");
146         }
147     }
148 }
149
Popular Tags