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.gvt.font;
020:
021: import java.util.Arrays;
022:
023: /**
024: * The Kern class describes an entry in the "kerning table". It provides
025: * a kerning value to be used when laying out characters side
026: * by side. It may be used for either horizontal or vertical kerning.
027: *
028: * @author <a href="mailto:dean.jackson@cmis.csiro.au">Dean Jackson</a>
029: * @version $Id: Kern.java 475685 2006-11-16 11:16:05Z cam $
030: */
031: public class Kern {
032:
033: private int[] firstGlyphCodes;
034: private int[] secondGlyphCodes;
035: private UnicodeRange[] firstUnicodeRanges;
036: private UnicodeRange[] secondUnicodeRanges;
037: private float kerningAdjust;
038:
039: /**
040: * Creates a Kern object with the given glyph arrays
041: * and kerning value. The first and second sets of glyphs for this kerning
042: * entry consist of the union of glyphs in the glyph code arrays and the
043: * unicode ranges.
044: *
045: * @param firstGlyphCodes An array of glyph codes that are part of the first
046: * set of glyphs in this kerning entry.
047: * @param secondGlyphCodes An array of glyph codes that are part of the
048: * second set of glyphs in this kerning entry.
049: * @param firstUnicodeRanges An array of unicode ranges that are part of the
050: * first set of glyphs in this kerning entry.
051: * @param secondUnicodeRanges An array of unicode ranges that are part of
052: * the second set of glyphs in this kerning entry.
053: * @param adjustValue The kerning adjustment (positive value means the space
054: * between glyphs should decrease).
055: */
056: public Kern(int[] firstGlyphCodes, int[] secondGlyphCodes,
057: UnicodeRange[] firstUnicodeRanges,
058: UnicodeRange[] secondUnicodeRanges, float adjustValue) {
059: this .firstGlyphCodes = firstGlyphCodes;
060: this .secondGlyphCodes = secondGlyphCodes;
061: this .firstUnicodeRanges = firstUnicodeRanges;
062: this .secondUnicodeRanges = secondUnicodeRanges;
063: this .kerningAdjust = adjustValue;
064:
065: if (firstGlyphCodes != null)
066: Arrays.sort(this .firstGlyphCodes);
067: if (secondGlyphCodes != null)
068: Arrays.sort(this .secondGlyphCodes);
069: }
070:
071: /**
072: * Returns true if the specified glyph is one of the glyphs considered
073: * as first by this kerning entry. Returns false otherwise.
074: *
075: * @param glyphCode The id of the glyph to test.
076: * @param glyphUnicode The unicode value of the glyph to test.
077: * @return True if this glyph is in the list of first glyphs for
078: * the kerning entry
079: */
080: public boolean matchesFirstGlyph(int glyphCode, String glyphUnicode) {
081: if (firstGlyphCodes != null) {
082: int pt = Arrays.binarySearch(firstGlyphCodes, glyphCode);
083: if (pt >= 0)
084: return true;
085: }
086: if (glyphUnicode.length() < 1)
087: return false;
088: char glyphChar = glyphUnicode.charAt(0);
089: for (int i = 0; i < firstUnicodeRanges.length; i++) {
090: if (firstUnicodeRanges[i].contains(glyphChar))
091: return true;
092: }
093: return false;
094: }
095:
096: /**
097: * Returns true if the specified glyph is one of the glyphs considered
098: * as first by this kerning entry. Returns false otherwise.
099: *
100: * @param glyphCode The id of the glyph to test.
101: * @param glyphUnicode The unicode value of the glyph to test.
102: * @return True if this glyph is in the list of first glyphs for
103: * the kerning entry
104: */
105: public boolean matchesFirstGlyph(int glyphCode, char glyphUnicode) {
106: if (firstGlyphCodes != null) {
107: int pt = Arrays.binarySearch(firstGlyphCodes, glyphCode);
108: if (pt >= 0)
109: return true;
110: }
111: for (int i = 0; i < firstUnicodeRanges.length; i++) {
112: if (firstUnicodeRanges[i].contains(glyphUnicode))
113: return true;
114: }
115: return false;
116: }
117:
118: /**
119: * Returns true if the specified glyph is one of the glyphs considered
120: * as second by this kerning entry. Returns false otherwise.
121: *
122: * @param glyphCode The id of the glyph to test.
123: * @param glyphUnicode The unicode value of the glyph to test.
124:
125: * @return True if this glyph is in the list of second glyphs for the
126: * kerning entry
127: */
128: public boolean matchesSecondGlyph(int glyphCode, String glyphUnicode) {
129: if (secondGlyphCodes != null) {
130: int pt = Arrays.binarySearch(secondGlyphCodes, glyphCode);
131: if (pt >= 0)
132: return true;
133: }
134: if (glyphUnicode.length() < 1)
135: return false;
136: char glyphChar = glyphUnicode.charAt(0);
137: for (int i = 0; i < secondUnicodeRanges.length; i++) {
138: if (secondUnicodeRanges[i].contains(glyphChar))
139: return true;
140: }
141: return false;
142: }
143:
144: /**
145: * Returns true if the specified glyph is one of the glyphs considered
146: * as second by this kerning entry. Returns false otherwise.
147: *
148: * @param glyphCode The id of the glyph to test.
149: * @param glyphUnicode The unicode value of the glyph to test.
150:
151: * @return True if this glyph is in the list of second glyphs for the
152: * kerning entry
153: */
154: public boolean matchesSecondGlyph(int glyphCode, char glyphUnicode) {
155: if (secondGlyphCodes != null) {
156: int pt = Arrays.binarySearch(secondGlyphCodes, glyphCode);
157: if (pt >= 0)
158: return true;
159: }
160: for (int i = 0; i < secondUnicodeRanges.length; i++) {
161: if (secondUnicodeRanges[i].contains(glyphUnicode))
162: return true;
163: }
164: return false;
165: }
166:
167: /**
168: * Returns the kerning adjustment value for this kerning entry (a positive
169: * value means the space between characters should decrease).
170: *
171: * @return The kerning adjustment for this kerning entry.
172: */
173: public float getAdjustValue() {
174: return kerningAdjust;
175: }
176:
177: }
|