KickJava   Java API By Example, From Geeks To Geeks.

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


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.gvt.font;
19
20
21 /**
22  * A class that represents a CSS unicode range. This only handles
23  * a single range of contigous chars, to handle multiple ranges
24  * (comma seperated) use a list of these.
25  *
26  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
27  * @version $Id: UnicodeRange.java,v 1.5 2004/09/01 09:35:23 deweese Exp $
28  */

29 public class UnicodeRange {
30
31     private int firstUnicodeValue;
32     private int lastUnicodeValue;
33
34     /**
35      * Constructs a unicode range from a CSS unicode range string.
36      */

37     public UnicodeRange(String JavaDoc unicodeRange) {
38
39         if (unicodeRange.startsWith("U+") && unicodeRange.length() > 2) {
40             // strip off the U+
41
unicodeRange = unicodeRange.substring(2);
42             int dashIndex = unicodeRange.indexOf('-');
43             String JavaDoc firstValue;
44             String JavaDoc lastValue;
45
46             if (dashIndex != -1) { // it is a simple 2 value range
47
firstValue = unicodeRange.substring(0, dashIndex);
48                 lastValue = unicodeRange.substring(dashIndex+1);
49
50             } else {
51                 firstValue = unicodeRange;
52                 lastValue = unicodeRange;
53                 if (unicodeRange.indexOf('?') != -1) {
54                     firstValue = firstValue.replace('?', '0');
55                     lastValue = lastValue.replace('?', 'F');
56                 }
57             }
58             try {
59                 firstUnicodeValue = Integer.parseInt(firstValue, 16);
60                 lastUnicodeValue = Integer.parseInt(lastValue, 16);
61             } catch (NumberFormatException JavaDoc e) {
62                 firstUnicodeValue = -1;
63                 lastUnicodeValue = -1;
64             }
65         } else {
66             // not a valid unicode range
67
firstUnicodeValue = -1;
68             lastUnicodeValue = -1;
69         }
70     }
71
72     /**
73      * Returns true if the specified unicode value is within this range.
74      */

75     public boolean contains(String JavaDoc unicode) {
76         if (unicode.length() == 1) {
77             int unicodeVal = unicode.charAt(0);
78             return contains(unicodeVal);
79         }
80         return false;
81     }
82
83     public boolean contains(int unicodeVal) {
84         return ((unicodeVal >= firstUnicodeValue) &&
85                 (unicodeVal <= lastUnicodeValue));
86     }
87
88 }
89
Popular Tags