1 /*
2  * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.font;
27 
28 import java.nio.ByteBuffer;
29 import java.nio.CharBuffer;
30 import java.nio.charset.*;
31 
32 public class X11Dingbats extends Charset {
X11Dingbats()33     public X11Dingbats () {
34         super("X11Dingbats", null);
35     }
36 
newEncoder()37     public CharsetEncoder newEncoder() {
38         return new Encoder(this);
39     }
40 
41     /* Seems like supporting a decoder is required, but we aren't going
42      * to be publically exposing this class, so no need to waste work
43      */
newDecoder()44     public CharsetDecoder newDecoder() {
45         throw new Error("Decoder is not supported by X11Dingbats Charset");
46     }
47 
contains(Charset cs)48     public boolean contains(Charset cs) {
49         return cs instanceof X11Dingbats;
50     }
51 
52     private static class Encoder extends CharsetEncoder {
Encoder(Charset cs)53         public Encoder(Charset cs) {
54             super(cs, 1.0f, 1.0f);
55         }
56 
canEncode(char ch)57         public boolean canEncode(char ch) {
58             if (ch >= 0x2701 && ch <= 0x275e) { // direct map
59                 return true;
60             }
61             if (ch >= 0x2761 && ch <= 0x27be) {
62                 return (table[ch - 0x2761] != 0x00);
63             }
64             return false;
65         }
66 
encodeLoop(CharBuffer src, ByteBuffer dst)67         protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
68             char[] sa = src.array();
69             int sp = src.arrayOffset() + src.position();
70             int sl = src.arrayOffset() + src.limit();
71             assert (sp <= sl);
72             sp = (sp <= sl ? sp : sl);
73             byte[] da = dst.array();
74             int dp = dst.arrayOffset() + dst.position();
75             int dl = dst.arrayOffset() + dst.limit();
76             assert (dp <= dl);
77             dp = (dp <= dl ? dp : dl);
78 
79             try {
80                 while (sp < sl) {
81                     char c = sa[sp];
82                     if (dl - dp < 1)
83                         return CoderResult.OVERFLOW;
84 
85                     if (!canEncode(c))
86                         return CoderResult.unmappableForLength(1);
87                     sp++;
88                     if (c >= 0x2761){
89                         da[dp++] = table[c - 0x2761]; // table lookup
90                     } else {
91                         da[dp++] = (byte)(c + 0x20 - 0x2700); // direct map
92                     }
93                 }
94                 return CoderResult.UNDERFLOW;
95             } finally {
96                 src.position(sp - src.arrayOffset());
97                 dst.position(dp - dst.arrayOffset());
98             }
99         }
100 
101         private static byte[] table = {
102             (byte)0xa1, (byte)0xa2, (byte)0xa3, (byte)0xa4,
103             (byte)0xa5, (byte)0xa6, (byte)0xa7,
104             (byte)0x00, (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)0xb6, (byte)0xb7,
108             (byte)0xb8, (byte)0xb9, (byte)0xba, (byte)0xbb,
109             (byte)0xbc, (byte)0xbd, (byte)0xbe, (byte)0xbf,
110             (byte)0xc0, (byte)0xc1, (byte)0xc2, (byte)0xc3,
111             (byte)0xc4, (byte)0xc5, (byte)0xc6, (byte)0xc7,
112             (byte)0xc8, (byte)0xc9, (byte)0xca, (byte)0xcb,
113             (byte)0xcc, (byte)0xcd, (byte)0xce, (byte)0xcf,
114             (byte)0xd0, (byte)0xd1, (byte)0xd2, (byte)0xd3,
115             (byte)0xd4, (byte)0x00, (byte)0x00, (byte)0x00,
116             (byte)0xd8, (byte)0xd9, (byte)0xda, (byte)0xdb,
117             (byte)0xdc, (byte)0xdd, (byte)0xde, (byte)0x00,
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};
126 
127         /* The default implementation creates a decoder and we don't have one */
isLegalReplacement(byte[] repl)128         public boolean isLegalReplacement(byte[] repl) {
129             return true;
130         }
131     }
132 }
133