KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGKernElementBridge


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.bridge;
19
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.apache.batik.gvt.font.Kern;
25 import org.apache.batik.gvt.font.UnicodeRange;
26 import org.w3c.dom.Element JavaDoc;
27
28 /**
29  * A base Bridge class for the kerning elements.
30  *
31  * @author <a HREF="mailto:dean.jackson@cmis.csiro.au">Dean Jackson</a>
32  * @version $Id: SVGKernElementBridge.java,v 1.7 2005/02/27 02:08:51 deweese Exp $
33  */

34 public abstract class SVGKernElementBridge extends AbstractSVGBridge {
35
36     /**
37      * Creates a Kern object that repesents the specified kerning element.
38      *
39      * @param ctx The bridge context.
40      * @param kernElement The kerning element. Should be either a &lt;hkern>
41      * or &lt;vkern> element.
42      * @param font The font the kerning is related to.
43      *
44      * @return kern The new Kern object
45      */

46     public Kern createKern(BridgeContext ctx,
47                            Element JavaDoc kernElement,
48                            SVGGVTFont font) {
49
50         // read all of the kern attributes
51
String JavaDoc u1 = kernElement.getAttributeNS(null, SVG_U1_ATTRIBUTE);
52         String JavaDoc u2 = kernElement.getAttributeNS(null, SVG_U2_ATTRIBUTE);
53         String JavaDoc g1 = kernElement.getAttributeNS(null, SVG_G1_ATTRIBUTE);
54         String JavaDoc g2 = kernElement.getAttributeNS(null, SVG_G2_ATTRIBUTE);
55         String JavaDoc k = kernElement.getAttributeNS(null, SVG_K_ATTRIBUTE);
56         if (k.length() == 0) {
57             k = SVG_KERN_K_DEFAULT_VALUE;
58         }
59         
60         // get the kern float value
61
float kernValue = Float.parseFloat(k);
62         
63         // set up the first and second glyph sets and unicode ranges
64
int firstGlyphLen = 0, secondGlyphLen = 0;
65         int [] firstGlyphSet = null;
66         int [] secondGlyphSet = null;
67         List JavaDoc firstUnicodeRanges = new ArrayList JavaDoc();
68         List JavaDoc secondUnicodeRanges = new ArrayList JavaDoc();
69         
70         // process the u1 attribute
71
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(u1, ",");
72         while (st.hasMoreTokens()) {
73             String JavaDoc token = st.nextToken();
74             if (token.startsWith("U+")) { // its a unicode range
75
firstUnicodeRanges.add(new UnicodeRange(token));
76             } else {
77                 int[] glyphCodes = font.getGlyphCodesForUnicode(token);
78                 if (firstGlyphSet == null) {
79                     firstGlyphSet = glyphCodes;
80                     firstGlyphLen = glyphCodes.length;
81                 }else {
82                     if ((firstGlyphLen + glyphCodes.length) >
83                         firstGlyphSet.length) {
84                         int sz = firstGlyphSet.length*2;
85                         if (sz <firstGlyphLen + glyphCodes.length)
86                             sz = firstGlyphLen + glyphCodes.length;
87                         int [] tmp = new int[sz];
88                         for (int i = 0; i < firstGlyphLen; i++)
89                             tmp[i] = firstGlyphSet[i];
90                         firstGlyphSet = tmp;
91                     }
92                     for (int i = 0; i < glyphCodes.length; i++)
93                         firstGlyphSet[firstGlyphLen++] = glyphCodes[i];
94                 }
95             }
96         }
97         
98         // process the u2 attrbute
99
st = new StringTokenizer JavaDoc(u2, ",");
100         while (st.hasMoreTokens()) {
101             String JavaDoc token = st.nextToken();
102             if (token.startsWith("U+")) { // its a unicode range
103
secondUnicodeRanges.add(new UnicodeRange(token));
104             } else {
105                 int[] glyphCodes = font.getGlyphCodesForUnicode(token);
106                 if (secondGlyphSet == null) {
107                     secondGlyphSet = glyphCodes;
108                     secondGlyphLen = glyphCodes.length;
109                 } else {
110                     if ((secondGlyphLen + glyphCodes.length) >
111                         secondGlyphSet.length) {
112                         int sz = secondGlyphSet.length*2;
113                         if (sz <secondGlyphLen + glyphCodes.length)
114                             sz = secondGlyphLen + glyphCodes.length;
115                         int [] tmp = new int[sz];
116                         for (int i = 0; i < secondGlyphLen; i++)
117                             tmp[i] = secondGlyphSet[i];
118                         secondGlyphSet = tmp;
119                     }
120                     for (int i = 0; i < glyphCodes.length; i++)
121                         secondGlyphSet[secondGlyphLen++] = glyphCodes[i];
122                 }
123             }
124         }
125         
126         // process the g1 attribute
127
st = new StringTokenizer JavaDoc(g1, ",");
128         while (st.hasMoreTokens()) {
129             String JavaDoc token = st.nextToken();
130             int[] glyphCodes = font.getGlyphCodesForName(token);
131             if (firstGlyphSet == null) {
132                 firstGlyphSet = glyphCodes;
133                 firstGlyphLen = glyphCodes.length;
134             }else {
135                 if ((firstGlyphLen + glyphCodes.length) >
136                     firstGlyphSet.length) {
137                     int sz = firstGlyphSet.length*2;
138                     if (sz <firstGlyphLen + glyphCodes.length)
139                         sz = firstGlyphLen + glyphCodes.length;
140                     int [] tmp = new int[sz];
141                     for (int i = 0; i < firstGlyphLen; i++)
142                         tmp[i] = firstGlyphSet[i];
143                     firstGlyphSet = tmp;
144                 }
145                 for (int i = 0; i < glyphCodes.length; i++)
146                     firstGlyphSet[firstGlyphLen++] = glyphCodes[i];
147             }
148         }
149         
150         // process the g2 attribute
151
st = new StringTokenizer JavaDoc(g2, ",");
152         while (st.hasMoreTokens()) {
153             String JavaDoc token = st.nextToken();
154             int[] glyphCodes = font.getGlyphCodesForName(token);
155             if (secondGlyphSet == null) {
156                 secondGlyphSet = glyphCodes;
157                 secondGlyphLen = glyphCodes.length;
158             } else {
159                 if ((secondGlyphLen + glyphCodes.length) >
160                     secondGlyphSet.length) {
161                     int sz = secondGlyphSet.length*2;
162                     if (sz <secondGlyphLen + glyphCodes.length)
163                         sz = secondGlyphLen + glyphCodes.length;
164                     int [] tmp = new int[sz];
165                     for (int i = 0; i < secondGlyphLen; i++)
166                         tmp[i] = secondGlyphSet[i];
167                     secondGlyphSet = tmp;
168                 }
169                 for (int i = 0; i < glyphCodes.length; i++)
170                     secondGlyphSet[secondGlyphLen++] = glyphCodes[i];
171             }
172         }
173
174         // construct the arrays
175
int[] firstGlyphs;
176         if ((firstGlyphLen == 0) ||
177             (firstGlyphLen == firstGlyphSet.length)) {
178             firstGlyphs = firstGlyphSet;
179         } else {
180             firstGlyphs = new int[firstGlyphLen];
181             System.arraycopy(firstGlyphSet, 0, firstGlyphs, 0, firstGlyphLen);
182         }
183         int[] secondGlyphs;
184         if ((secondGlyphLen == 0) ||
185             (secondGlyphLen == secondGlyphSet.length)) {
186             secondGlyphs = secondGlyphSet;
187         } else {
188             secondGlyphs = new int[secondGlyphLen];
189             System.arraycopy(secondGlyphSet, 0, secondGlyphs, 0,
190                              secondGlyphLen);
191         }
192
193         UnicodeRange[] firstRanges;
194         firstRanges = new UnicodeRange[firstUnicodeRanges.size()];
195         firstUnicodeRanges.toArray(firstRanges);
196
197         UnicodeRange[] secondRanges;
198         secondRanges = new UnicodeRange[secondUnicodeRanges.size()];
199         secondUnicodeRanges.toArray(secondRanges);
200
201         // return the new Kern object
202
return new Kern(firstGlyphs, secondGlyphs,
203                         firstRanges, secondRanges, kernValue);
204     }
205 }
206
Popular Tags