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: GlyfCompositeComp.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 GlyfCompositeComp {
028:
029: public static final short ARG_1_AND_2_ARE_WORDS = 0x0001;
030: public static final short ARGS_ARE_XY_VALUES = 0x0002;
031: public static final short ROUND_XY_TO_GRID = 0x0004;
032: public static final short WE_HAVE_A_SCALE = 0x0008;
033: public static final short MORE_COMPONENTS = 0x0020;
034: public static final short WE_HAVE_AN_X_AND_Y_SCALE = 0x0040;
035: public static final short WE_HAVE_A_TWO_BY_TWO = 0x0080;
036: public static final short WE_HAVE_INSTRUCTIONS = 0x0100;
037: public static final short USE_MY_METRICS = 0x0200;
038:
039: private int firstIndex;
040: private int firstContour;
041: private short argument1;
042: private short argument2;
043: private short flags;
044: private int glyphIndex;
045: private double xscale = 1.0;
046: private double yscale = 1.0;
047: private double scale01 = 0.0;
048: private double scale10 = 0.0;
049: private int xtranslate = 0;
050: private int ytranslate = 0;
051: private int point1 = 0;
052: private int point2 = 0;
053:
054: protected GlyfCompositeComp(ByteArrayInputStream bais) {
055: flags = (short) ((bais.read() << 8) | bais.read());
056: glyphIndex = ((bais.read() << 8) | bais.read());
057:
058: // Get the arguments as just their raw values
059: if ((flags & ARG_1_AND_2_ARE_WORDS) != 0) {
060: argument1 = (short) (bais.read() << 8 | bais.read());
061: argument2 = (short) (bais.read() << 8 | bais.read());
062: } else {
063: argument1 = (short) bais.read();
064: argument2 = (short) bais.read();
065: }
066:
067: // Assign the arguments according to the flags
068: if ((flags & ARGS_ARE_XY_VALUES) != 0) {
069: xtranslate = argument1;
070: ytranslate = argument2;
071: } else {
072: point1 = argument1;
073: point2 = argument2;
074: }
075:
076: // Get the scale values (if any)
077: if ((flags & WE_HAVE_A_SCALE) != 0) {
078: int i = (short) (bais.read() << 8 | bais.read());
079: xscale = yscale = (double) i / (double) 0x4000;
080: } else if ((flags & WE_HAVE_AN_X_AND_Y_SCALE) != 0) {
081: short i = (short) (bais.read() << 8 | bais.read());
082: xscale = (double) i / (double) 0x4000;
083: i = (short) (bais.read() << 8 | bais.read());
084: yscale = (double) i / (double) 0x4000;
085: } else if ((flags & WE_HAVE_A_TWO_BY_TWO) != 0) {
086: int i = (short) (bais.read() << 8 | bais.read());
087: xscale = (double) i / (double) 0x4000;
088: i = (short) (bais.read() << 8 | bais.read());
089: scale01 = (double) i / (double) 0x4000;
090: i = (short) (bais.read() << 8 | bais.read());
091: scale10 = (double) i / (double) 0x4000;
092: i = (short) (bais.read() << 8 | bais.read());
093: yscale = (double) i / (double) 0x4000;
094: }
095: }
096:
097: public void setFirstIndex(int idx) {
098: firstIndex = idx;
099: }
100:
101: public int getFirstIndex() {
102: return firstIndex;
103: }
104:
105: public void setFirstContour(int idx) {
106: firstContour = idx;
107: }
108:
109: public int getFirstContour() {
110: return firstContour;
111: }
112:
113: public short getArgument1() {
114: return argument1;
115: }
116:
117: public short getArgument2() {
118: return argument2;
119: }
120:
121: public short getFlags() {
122: return flags;
123: }
124:
125: public int getGlyphIndex() {
126: return glyphIndex;
127: }
128:
129: public double getScale01() {
130: return scale01;
131: }
132:
133: public double getScale10() {
134: return scale10;
135: }
136:
137: public double getXScale() {
138: return xscale;
139: }
140:
141: public double getYScale() {
142: return yscale;
143: }
144:
145: public int getXTranslate() {
146: return xtranslate;
147: }
148:
149: public int getYTranslate() {
150: return ytranslate;
151: }
152:
153: /**
154: * Transforms an x-coordinate of a point for this component.
155: * @param x The x-coordinate of the point to transform
156: * @param y The y-coordinate of the point to transform
157: * @return The transformed x-coordinate
158: */
159: public int scaleX(int x, int y) {
160: return Math.round((float) (x * xscale + y * scale10));
161: }
162:
163: /**
164: * Transforms a y-coordinate of a point for this component.
165: * @param x The x-coordinate of the point to transform
166: * @param y The y-coordinate of the point to transform
167: * @return The transformed y-coordinate
168: */
169: public int scaleY(int x, int y) {
170: return Math.round((float) (x * scale01 + y * yscale));
171: }
172: }
|