KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > font > Kern


1 /*
2
3    Copyright 2001 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.gvt.font;
19
20 import java.util.Arrays JavaDoc;
21
22 /**
23  * The Kern class describes an entry in the "kerning table". It provides
24  * a kerning value to be used when laying out characters side
25  * by side. It may be used for either horizontal or vertical kerning.
26  *
27  * @author <a HREF="mailto:dean.jackson@cmis.csiro.au">Dean Jackson</a>
28  * @version $Id: Kern.java,v 1.6 2004/09/01 09:35:23 deweese Exp $
29  */

30 public class Kern {
31
32     private int[] firstGlyphCodes;
33     private int[] secondGlyphCodes;
34     private UnicodeRange[] firstUnicodeRanges;
35     private UnicodeRange[] secondUnicodeRanges;
36     private float kerningAdjust;
37
38     /**
39      * Creates a Kern object with the given glyph arrays
40      * and kerning value. The first and second sets of glyphs for this kerning
41      * entry consist of the union of glyphs in the glyph code arrays and the
42      * unicode ranges.
43      *
44      * @param firstGlyphCodes An array of glyph codes that are part of the first
45      * set of glyphs in this kerning entry.
46      * @param secondGlyphCodes An array of glyph codes that are part of the
47      * second set of glyphs in this kerning entry.
48      * @param firstUnicodeRanges An array of unicode ranges that are part of the
49      * first set of glyphs in this kerning entry.
50      * @param secondUnicodeRanges An array of unicode ranges that are part of
51      * the second set of glyphs in this kerning entry.
52      * @param adjustValue The kerning adjustment (positive value means the space
53      * between glyphs should decrease).
54      */

55     public Kern(int[] firstGlyphCodes,
56         int[] secondGlyphCodes,
57                 UnicodeRange[] firstUnicodeRanges,
58                 UnicodeRange[] secondUnicodeRanges,
59                 float adjustValue) {
60         this.firstGlyphCodes = firstGlyphCodes;
61         this.secondGlyphCodes = secondGlyphCodes;
62         this.firstUnicodeRanges = firstUnicodeRanges;
63         this.secondUnicodeRanges = secondUnicodeRanges;
64         this.kerningAdjust = adjustValue;
65
66         if (firstGlyphCodes != null)
67             Arrays.sort(this.firstGlyphCodes);
68         if (secondGlyphCodes != null)
69             Arrays.sort(this.secondGlyphCodes);
70     }
71
72     /**
73      * Returns true if the specified glyph is one of the glyphs considered
74      * as first by this kerning entry. Returns false otherwise.
75      *
76      * @param glyphCode The id of the glyph to test.
77      * @param glyphUnicode The unicode value of the glyph to test.
78      * @return True if this glyph is in the list of first glyphs for
79      * the kerning entry
80      */

81     public boolean matchesFirstGlyph(int glyphCode, String JavaDoc glyphUnicode) {
82         if (firstGlyphCodes != null) {
83             int pt = Arrays.binarySearch(firstGlyphCodes, glyphCode);
84             if (pt >= 0) return true;
85         }
86         if (glyphUnicode.length() < 1) return false;
87         char glyphChar = glyphUnicode.charAt(0);
88         for (int i = 0; i < firstUnicodeRanges.length; i++) {
89             if (firstUnicodeRanges[i].contains(glyphChar))
90                 return true;
91         }
92         return false;
93     }
94
95     /**
96      * Returns true if the specified glyph is one of the glyphs considered
97      * as first by this kerning entry. Returns false otherwise.
98      *
99      * @param glyphCode The id of the glyph to test.
100      * @param glyphUnicode The unicode value of the glyph to test.
101      * @return True if this glyph is in the list of first glyphs for
102      * the kerning entry
103      */

104     public boolean matchesFirstGlyph(int glyphCode, char glyphUnicode) {
105         if (firstGlyphCodes != null) {
106             int pt = Arrays.binarySearch(firstGlyphCodes, glyphCode);
107             if (pt >= 0) return true;
108         }
109         for (int i = 0; i < firstUnicodeRanges.length; i++) {
110             if (firstUnicodeRanges[i].contains(glyphUnicode))
111                 return true;
112         }
113         return false;
114     }
115
116     /**
117      * Returns true if the specified glyph is one of the glyphs considered
118      * as second by this kerning entry. Returns false otherwise.
119      *
120      * @param glyphCode The id of the glyph to test.
121      * @param glyphUnicode The unicode value of the glyph to test.
122
123      * @return True if this glyph is in the list of second glyphs for the
124      * kerning entry
125      */

126     public boolean matchesSecondGlyph(int glyphCode, String JavaDoc glyphUnicode) {
127         if (secondGlyphCodes != null) {
128             int pt = Arrays.binarySearch(secondGlyphCodes, glyphCode);
129             if (pt >= 0) return true;
130         }
131         if (glyphUnicode.length() < 1) return false;
132         char glyphChar = glyphUnicode.charAt(0);
133         for (int i = 0; i < secondUnicodeRanges.length; i++) {
134             if (secondUnicodeRanges[i].contains(glyphChar))
135                 return true;
136         }
137         return false;
138     }
139
140     /**
141      * Returns true if the specified glyph is one of the glyphs considered
142      * as second by this kerning entry. Returns false otherwise.
143      *
144      * @param glyphCode The id of the glyph to test.
145      * @param glyphUnicode The unicode value of the glyph to test.
146
147      * @return True if this glyph is in the list of second glyphs for the
148      * kerning entry
149      */

150     public boolean matchesSecondGlyph(int glyphCode, char glyphUnicode) {
151         if (secondGlyphCodes != null) {
152             int pt = Arrays.binarySearch(secondGlyphCodes, glyphCode);
153             if (pt >= 0) return true;
154         }
155         for (int i = 0; i < secondUnicodeRanges.length; i++) {
156             if (secondUnicodeRanges[i].contains(glyphUnicode))
157                 return true;
158         }
159         return false;
160     }
161
162     /**
163      * Returns the kerning adjustment value for this kerning entry (a positive
164      * value means the space between characters should decrease).
165      *
166      * @return The kerning adjustment for this kerning entry.
167      */

168     public float getAdjustValue() {
169         return kerningAdjust;
170     }
171
172 }
173
Popular Tags