001: /*
002: * Copyright 1997-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;
027:
028: import java.nio.ByteBuffer;
029: import java.nio.CharBuffer;
030: import java.nio.charset.*;
031:
032: public class Symbol extends Charset {
033: public Symbol() {
034: super ("Symbol", 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("Decoder is not implemented for Symbol Charset");
046: }
047:
048: public boolean contains(Charset cs) {
049: return cs instanceof Symbol;
050: }
051:
052: private static class Encoder extends CharsetEncoder {
053: public Encoder(Charset cs) {
054: super (cs, 1.0f, 1.0f);
055: }
056:
057: public boolean canEncode(char c) {
058: if (c >= 0x2200 && c <= 0x22ef) {
059: if (table_math[c - 0x2200] != 0x00) {
060: return true;
061: }
062: } else if (c >= 0x0391 && c <= 0x03d6) {
063: if (table_greek[c - 0x0391] != 0x00) {
064: return true;
065: }
066: }
067: return false;
068: }
069:
070: protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
071: char[] sa = src.array();
072: int sp = src.arrayOffset() + src.position();
073: int sl = src.arrayOffset() + src.limit();
074: assert (sp <= sl);
075: sp = (sp <= sl ? sp : sl);
076: byte[] da = dst.array();
077: int dp = dst.arrayOffset() + dst.position();
078: int dl = dst.arrayOffset() + dst.limit();
079: assert (dp <= dl);
080: dp = (dp <= dl ? dp : dl);
081:
082: try {
083: while (sp < sl) {
084: char c = sa[sp];
085: if (dl - dp < 1)
086: return CoderResult.OVERFLOW;
087: if (!canEncode(c))
088: return CoderResult.unmappableForLength(1);
089: sp++;
090: if (c >= 0x2200 && c <= 0x22ef) {
091: da[dp++] = table_math[c - 0x2200];
092: } else if (c >= 0x0391 && c <= 0x03d6) {
093: da[dp++] = table_greek[c - 0x0391];
094: }
095: }
096: return CoderResult.UNDERFLOW;
097: } finally {
098: src.position(sp - src.arrayOffset());
099: dst.position(dp - dst.arrayOffset());
100: }
101: }
102:
103: private static byte[] table_math = {
104: (byte) 0042,
105: (byte) 0000,
106: (byte) 0144,
107: (byte) 0044,
108: (byte) 0000,
109: (byte) 0306,
110: (byte) 0104,
111: (byte) 0321, // 00
112: (byte) 0316,
113: (byte) 0317,
114: (byte) 0000,
115: (byte) 0000,
116: (byte) 0000,
117: (byte) 0047,
118: (byte) 0000,
119: (byte) 0120,
120: (byte) 0000,
121: (byte) 0345,
122: (byte) 0055,
123: (byte) 0000,
124: (byte) 0000,
125: (byte) 0244,
126: (byte) 0000,
127: (byte) 0052, // 10
128: (byte) 0260,
129: (byte) 0267,
130: (byte) 0326,
131: (byte) 0000,
132: (byte) 0000,
133: (byte) 0265,
134: (byte) 0245,
135: (byte) 0000,
136: (byte) 0000,
137: (byte) 0000,
138: (byte) 0000,
139: (byte) 0275,
140: (byte) 0000,
141: (byte) 0000,
142: (byte) 0000,
143: (byte) 0331, // 20
144: (byte) 0332,
145: (byte) 0307,
146: (byte) 0310,
147: (byte) 0362,
148: (byte) 0000,
149: (byte) 0000,
150: (byte) 0000,
151: (byte) 0000,
152: (byte) 0000,
153: (byte) 0000,
154: (byte) 0000,
155: (byte) 0000,
156: (byte) 0134,
157: (byte) 0000,
158: (byte) 0000,
159: (byte) 0000, // 30
160: (byte) 0000,
161: (byte) 0000,
162: (byte) 0000,
163: (byte) 0000,
164: (byte) 0176,
165: (byte) 0000,
166: (byte) 0000,
167: (byte) 0000,
168: (byte) 0000,
169: (byte) 0000,
170: (byte) 0000,
171: (byte) 0000,
172: (byte) 0000,
173: (byte) 0100,
174: (byte) 0000,
175: (byte) 0000, // 40
176: (byte) 0273,
177: (byte) 0000,
178: (byte) 0000,
179: (byte) 0000,
180: (byte) 0000,
181: (byte) 0000,
182: (byte) 0000,
183: (byte) 0000,
184: (byte) 0000,
185: (byte) 0000,
186: (byte) 0000,
187: (byte) 0000,
188: (byte) 0000,
189: (byte) 0000,
190: (byte) 0000,
191: (byte) 0000, // 50
192: (byte) 0000,
193: (byte) 0000,
194: (byte) 0000,
195: (byte) 0000,
196: (byte) 0000,
197: (byte) 0000,
198: (byte) 0000,
199: (byte) 0000,
200: (byte) 0271,
201: (byte) 0272,
202: (byte) 0000,
203: (byte) 0000,
204: (byte) 0243,
205: (byte) 0263,
206: (byte) 0000,
207: (byte) 0000, // 60
208: (byte) 0000,
209: (byte) 0000,
210: (byte) 0000,
211: (byte) 0000,
212: (byte) 0000,
213: (byte) 0000,
214: (byte) 0000,
215: (byte) 0000,
216: (byte) 0000,
217: (byte) 0000,
218: (byte) 0000,
219: (byte) 0000,
220: (byte) 0000,
221: (byte) 0000,
222: (byte) 0000,
223: (byte) 0000, // 70
224: (byte) 0000,
225: (byte) 0000,
226: (byte) 0000,
227: (byte) 0000,
228: (byte) 0000,
229: (byte) 0000,
230: (byte) 0000,
231: (byte) 0000,
232: (byte) 0000,
233: (byte) 0000,
234: (byte) 0314,
235: (byte) 0311,
236: (byte) 0313,
237: (byte) 0000,
238: (byte) 0315,
239: (byte) 0312, // 80
240: (byte) 0000,
241: (byte) 0000,
242: (byte) 0000,
243: (byte) 0000,
244: (byte) 0000,
245: (byte) 0000,
246: (byte) 0000,
247: (byte) 0000,
248: (byte) 0000,
249: (byte) 0000,
250: (byte) 0000,
251: (byte) 0000,
252: (byte) 0000,
253: (byte) 0305,
254: (byte) 0000,
255: (byte) 0304, // 90
256: (byte) 0000,
257: (byte) 0000,
258: (byte) 0000,
259: (byte) 0000,
260: (byte) 0000,
261: (byte) 0000,
262: (byte) 0000,
263: (byte) 0000,
264: (byte) 0000,
265: (byte) 0000,
266: (byte) 0000,
267: (byte) 0000,
268: (byte) 0000,
269: (byte) 0136,
270: (byte) 0000,
271: (byte) 0000, // a0
272: (byte) 0000, (byte) 0000, (byte) 0000,
273: (byte) 0000,
274: (byte) 0000,
275: (byte) 0000,
276: (byte) 0000,
277: (byte) 0000,
278: (byte) 0000,
279: (byte) 0000,
280: (byte) 0000,
281: (byte) 0000,
282: (byte) 0000,
283: (byte) 0000,
284: (byte) 0000,
285: (byte) 0000, // b0
286: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
287: (byte) 0000, (byte) 0000, (byte) 0000,
288: (byte) 0000,
289: (byte) 0000,
290: (byte) 0000,
291: (byte) 0000,
292: (byte) 0000,
293: (byte) 0340,
294: (byte) 0327,
295: (byte) 0000,
296: (byte) 0000, // c0
297: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
298: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
299: (byte) 0000, (byte) 0000, (byte) 0000,
300: (byte) 0000,
301: (byte) 0000,
302: (byte) 0000,
303: (byte) 0000,
304: (byte) 0000, // d0
305: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
306: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
307: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
308: (byte) 0000, (byte) 0000, (byte) 0000,
309: (byte) 0000, // e0
310: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
311: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0274, };
312:
313: private static byte[] table_greek = {
314: (byte) 0101,
315: (byte) 0102,
316: (byte) 0107,
317: (byte) 0104,
318: (byte) 0105,
319: (byte) 0132,
320: (byte) 0110, // 90
321: (byte) 0121, (byte) 0111, (byte) 0113, (byte) 0114,
322: (byte) 0115,
323: (byte) 0116,
324: (byte) 0130,
325: (byte) 0117,
326: (byte) 0120,
327: (byte) 0122,
328: (byte) 0000,
329: (byte) 0123,
330: (byte) 0124,
331: (byte) 0125,
332: (byte) 0106,
333: (byte) 0103, // a0
334: (byte) 0131, (byte) 0127, (byte) 0000, (byte) 0000,
335: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
336: (byte) 0000,
337: (byte) 0141,
338: (byte) 0142,
339: (byte) 0147,
340: (byte) 0144,
341: (byte) 0145,
342: (byte) 0172,
343: (byte) 0150, // b0
344: (byte) 0161, (byte) 0151, (byte) 0153, (byte) 0154,
345: (byte) 0155, (byte) 0156, (byte) 0170, (byte) 0157,
346: (byte) 0160, (byte) 0162, (byte) 0126, (byte) 0163,
347: (byte) 0164,
348: (byte) 0165,
349: (byte) 0146,
350: (byte) 0143, // c0
351: (byte) 0171, (byte) 0167, (byte) 0000, (byte) 0000,
352: (byte) 0000, (byte) 0000, (byte) 0000, (byte) 0000,
353: (byte) 0000, (byte) 0112, (byte) 0241, (byte) 0000,
354: (byte) 0000, (byte) 0152, (byte) 0166, // d0
355: };
356:
357: /* The default implementation creates a decoder and we don't have one */
358: public boolean isLegalReplacement(byte[] repl) {
359: return true;
360: }
361: }
362: }
|