001: /*
002: * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.motif;
027:
028: import java.nio.ByteBuffer;
029: import java.nio.CharBuffer;
030: import java.nio.charset.*;
031:
032: public class X11Dingbats extends Charset {
033: public X11Dingbats() {
034: super ("X11Dingbats", null);
035: }
036:
037: public CharsetEncoder newEncoder() {
038: return new Encoder(this );
039: }
040:
041: /* Seems like supporting a decoder is required, but we aren't going
042: * to be publically exposing this class, so no need to waste work
043: */
044: public CharsetDecoder newDecoder() {
045: throw new Error(
046: "Decoder is not supported by X11Dingbats Charset");
047: }
048:
049: public boolean contains(Charset cs) {
050: return cs instanceof X11Dingbats;
051: }
052:
053: private static class Encoder extends CharsetEncoder {
054: public Encoder(Charset cs) {
055: super (cs, 1.0f, 1.0f);
056: }
057:
058: public boolean canEncode(char ch) {
059: if (ch >= 0x2701 && ch <= 0x275e) { // direct map
060: return true;
061: }
062: if (ch >= 0x2761 && ch <= 0x27be) {
063: return (table[ch - 0x2761] != 0x00);
064: }
065: return false;
066: }
067:
068: protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
069: char[] sa = src.array();
070: int sp = src.arrayOffset() + src.position();
071: int sl = src.arrayOffset() + src.limit();
072: assert (sp <= sl);
073: sp = (sp <= sl ? sp : sl);
074: byte[] da = dst.array();
075: int dp = dst.arrayOffset() + dst.position();
076: int dl = dst.arrayOffset() + dst.limit();
077: assert (dp <= dl);
078: dp = (dp <= dl ? dp : dl);
079:
080: try {
081: while (sp < sl) {
082: char c = sa[sp];
083: if (dl - dp < 1)
084: return CoderResult.OVERFLOW;
085:
086: if (!canEncode(c))
087: return CoderResult.unmappableForLength(1);
088: sp++;
089: if (c >= 0x2761) {
090: da[dp++] = table[c - 0x2761]; // table lookup
091: } else {
092: da[dp++] = (byte) (c + 0x20 - 0x2700); // direct map
093: }
094: }
095: return CoderResult.UNDERFLOW;
096: } finally {
097: src.position(sp - src.arrayOffset());
098: dst.position(dp - dst.arrayOffset());
099: }
100: }
101:
102: private static byte[] table = { (byte) 0xa1, (byte) 0xa2,
103: (byte) 0xa3, (byte) 0xa4, (byte) 0xa5, (byte) 0xa6,
104: (byte) 0xa7, (byte) 0x00, (byte) 0x00, (byte) 0x00,
105: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
106: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
107: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xb6,
108: (byte) 0xb7, (byte) 0xb8, (byte) 0xb9, (byte) 0xba,
109: (byte) 0xbb, (byte) 0xbc, (byte) 0xbd, (byte) 0xbe,
110: (byte) 0xbf, (byte) 0xc0, (byte) 0xc1, (byte) 0xc2,
111: (byte) 0xc3, (byte) 0xc4, (byte) 0xc5, (byte) 0xc6,
112: (byte) 0xc7, (byte) 0xc8, (byte) 0xc9, (byte) 0xca,
113: (byte) 0xcb, (byte) 0xcc, (byte) 0xcd, (byte) 0xce,
114: (byte) 0xcf, (byte) 0xd0, (byte) 0xd1, (byte) 0xd2,
115: (byte) 0xd3, (byte) 0xd4, (byte) 0x00, (byte) 0x00,
116: (byte) 0x00, (byte) 0xd8, (byte) 0xd9, (byte) 0xda,
117: (byte) 0xdb, (byte) 0xdc, (byte) 0xdd, (byte) 0xde,
118: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
119: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
120: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
121: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
122: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
123: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
124: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
125: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
126:
127: /* The default implementation creates a decoder and we don't have one */
128: public boolean isLegalReplacement(byte[] repl) {
129: return true;
130: }
131: }
132: }
|