author | redestad |
Wed, 13 Dec 2017 21:25:49 +0100 | |
changeset 48294 | 2608240fc957 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2001, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
30456
2a753e3fc714
8035302: Eliminate dependency on jdk.charsets from 2D font code.
prr
parents:
29119
diff
changeset
|
26 |
package sun.font; |
2 | 27 |
|
28 |
import java.nio.CharBuffer; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.nio.charset.*; |
|
29119
f680ba340556
8073893: Enable charsets build system to configure euc_tw into java.base module/sun.nio.cs
sherman
parents:
25859
diff
changeset
|
31 |
import sun.nio.cs.*; |
2 | 32 |
|
33 |
public abstract class X11CNS11643 extends Charset { |
|
34 |
private final int plane; |
|
35 |
public X11CNS11643 (int plane, String name) { |
|
36 |
super(name, null); |
|
37 |
switch (plane) { |
|
38 |
case 1: |
|
39 |
this.plane = 0; // CS1 |
|
40 |
break; |
|
41 |
case 2: |
|
42 |
case 3: |
|
43 |
this.plane = plane; |
|
44 |
break; |
|
45 |
default: |
|
46 |
throw new IllegalArgumentException |
|
47 |
("Only planes 1, 2, and 3 supported"); |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
public CharsetEncoder newEncoder() { |
|
52 |
return new Encoder(this, plane); |
|
53 |
} |
|
54 |
||
55 |
public CharsetDecoder newDecoder() { |
|
56 |
return new Decoder(this, plane); |
|
57 |
} |
|
58 |
||
59 |
public boolean contains(Charset cs) { |
|
60 |
return cs instanceof X11CNS11643; |
|
61 |
} |
|
62 |
||
63 |
private class Encoder extends EUC_TW.Encoder { |
|
64 |
private int plane; |
|
65 |
public Encoder(Charset cs, int plane) { |
|
66 |
super(cs); |
|
67 |
this.plane = plane; |
|
68 |
} |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
69 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
70 |
private byte[] bb = new byte[4]; |
2 | 71 |
public boolean canEncode(char c) { |
72 |
if (c <= 0x7F) { |
|
73 |
return false; |
|
74 |
} |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
75 |
int nb = toEUC(c, bb); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
76 |
if (nb == -1) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
77 |
return false; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
78 |
int p = 0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
79 |
if (nb == 4) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
80 |
p = (bb[1] & 0xff) - 0xa0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
81 |
return (p == plane); |
2 | 82 |
} |
83 |
||
84 |
public boolean isLegalReplacement(byte[] repl) { |
|
85 |
return true; |
|
86 |
} |
|
87 |
||
88 |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { |
|
89 |
char[] sa = src.array(); |
|
90 |
int sp = src.arrayOffset() + src.position(); |
|
91 |
int sl = src.arrayOffset() + src.limit(); |
|
92 |
byte[] da = dst.array(); |
|
93 |
int dp = dst.arrayOffset() + dst.position(); |
|
94 |
int dl = dst.arrayOffset() + dst.limit(); |
|
95 |
||
96 |
try { |
|
97 |
while (sp < sl) { |
|
98 |
char c = sa[sp]; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
99 |
if ( c > '\u007f'&& c < '\uFFFE') { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
100 |
int nb = toEUC(c, bb); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
101 |
if (nb != -1) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
102 |
int p = 0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
103 |
if (nb == 4) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
104 |
p = (bb[1] & 0xff) - 0xa0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
105 |
if (p == plane) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
106 |
if (dl - dp < 2) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
107 |
return CoderResult.OVERFLOW; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
108 |
if (nb == 2) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
109 |
da[dp++] = (byte)(bb[0] & 0x7f); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
110 |
da[dp++] = (byte)(bb[1] & 0x7f); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
111 |
} else { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
112 |
da[dp++] = (byte)(bb[2] & 0x7f); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
113 |
da[dp++] = (byte)(bb[3] & 0x7f); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
114 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
115 |
sp++; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
116 |
continue; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
117 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
118 |
} |
2 | 119 |
} |
120 |
return CoderResult.unmappableForLength(1); |
|
121 |
} |
|
122 |
return CoderResult.UNDERFLOW; |
|
123 |
} finally { |
|
124 |
src.position(sp - src.arrayOffset()); |
|
125 |
dst.position(dp - dst.arrayOffset()); |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
private class Decoder extends EUC_TW.Decoder { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
131 |
int plane; |
2 | 132 |
private String table; |
133 |
protected Decoder(Charset cs, int plane) { |
|
134 |
super(cs); |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
135 |
if (plane == 0) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
136 |
this.plane = plane; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
137 |
else if (plane == 2 || plane == 3) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
138 |
this.plane = plane - 1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
139 |
else |
2 | 140 |
throw new IllegalArgumentException |
141 |
("Only planes 1, 2, and 3 supported"); |
|
142 |
} |
|
143 |
||
144 |
//we only work on array backed buffer. |
|
145 |
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { |
|
146 |
byte[] sa = src.array(); |
|
147 |
int sp = src.arrayOffset() + src.position(); |
|
148 |
int sl = src.arrayOffset() + src.limit(); |
|
149 |
||
150 |
char[] da = dst.array(); |
|
151 |
int dp = dst.arrayOffset() + dst.position(); |
|
152 |
int dl = dst.arrayOffset() + dst.limit(); |
|
153 |
||
154 |
try { |
|
155 |
while (sp < sl) { |
|
156 |
if ( sl - sp < 2) { |
|
157 |
return CoderResult.UNDERFLOW; |
|
158 |
} |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
159 |
int b1 = (sa[sp] & 0xff) | 0x80; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
160 |
int b2 = (sa[sp + 1] & 0xff) | 0x80; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
161 |
char[] cc = toUnicode(b1, b2, plane); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
162 |
// plane3 has non-bmp characters(added), x11cnsp3 |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
163 |
// however does not support them |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
164 |
if (cc == null || cc.length == 2) |
2 | 165 |
return CoderResult.unmappableForLength(2); |
166 |
if (dl - dp < 1) |
|
167 |
return CoderResult.OVERFLOW; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
168 |
da[dp++] = cc[0]; |
2 | 169 |
sp +=2; |
170 |
} |
|
171 |
return CoderResult.UNDERFLOW; |
|
172 |
} finally { |
|
173 |
src.position(sp - src.arrayOffset()); |
|
174 |
dst.position(dp - dst.arrayOffset()); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
} |