author | ddehaven |
Tue, 19 Aug 2014 10:32:16 -0700 | |
changeset 26037 | 508779ce6619 |
parent 26020 | jdk/src/share/classes/sun/font/TrueTypeGlyphMapper.java@68deef2ad1c9 |
parent 25859 | jdk/src/share/classes/sun/font/TrueTypeGlyphMapper.java@3317bb8137f4 |
child 40139 | 76f3dc8c0c28 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2006, 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 |
||
26 |
package sun.font; |
|
27 |
||
28 |
import java.nio.ByteBuffer; |
|
29 |
import java.util.Locale; |
|
30 |
||
31 |
public class TrueTypeGlyphMapper extends CharToGlyphMapper { |
|
32 |
||
33 |
static final char REVERSE_SOLIDUS = 0x005c; // the backslash char. |
|
34 |
static final char JA_YEN = 0x00a5; |
|
35 |
static final char JA_FULLWIDTH_TILDE_CHAR = 0xff5e; |
|
36 |
static final char JA_WAVE_DASH_CHAR = 0x301c; |
|
37 |
||
38 |
/* if running on Solaris and default Locale is ja_JP then |
|
39 |
* we map need to remap reverse solidus (backslash) to Yen as |
|
40 |
* apparently expected there. |
|
41 |
*/ |
|
42 |
static final boolean isJAlocale = Locale.JAPAN.equals(Locale.getDefault()); |
|
43 |
private final boolean needsJAremapping; |
|
44 |
private boolean remapJAWaveDash; |
|
45 |
||
46 |
TrueTypeFont font; |
|
47 |
CMap cmap; |
|
48 |
int numGlyphs; |
|
49 |
||
50 |
public TrueTypeGlyphMapper(TrueTypeFont font) { |
|
51 |
this.font = font; |
|
52 |
try { |
|
53 |
cmap = CMap.initialize(font); |
|
54 |
} catch (Exception e) { |
|
55 |
cmap = null; |
|
56 |
} |
|
57 |
if (cmap == null) { |
|
58 |
handleBadCMAP(); |
|
59 |
} |
|
60 |
missingGlyph = 0; /* standard for TrueType fonts */ |
|
61 |
ByteBuffer buffer = font.getTableBuffer(TrueTypeFont.maxpTag); |
|
26020
68deef2ad1c9
8052396: Catch exceptions resulting from missing font cmap
prr
parents:
24527
diff
changeset
|
62 |
if (buffer != null && buffer.capacity() >= 6) { |
68deef2ad1c9
8052396: Catch exceptions resulting from missing font cmap
prr
parents:
24527
diff
changeset
|
63 |
numGlyphs = buffer.getChar(4); // offset 4 bytes in MAXP table. |
68deef2ad1c9
8052396: Catch exceptions resulting from missing font cmap
prr
parents:
24527
diff
changeset
|
64 |
} else { |
68deef2ad1c9
8052396: Catch exceptions resulting from missing font cmap
prr
parents:
24527
diff
changeset
|
65 |
handleBadCMAP(); |
68deef2ad1c9
8052396: Catch exceptions resulting from missing font cmap
prr
parents:
24527
diff
changeset
|
66 |
} |
3928 | 67 |
if (FontUtilities.isSolaris && isJAlocale && font.supportsJA()) { |
2 | 68 |
needsJAremapping = true; |
3928 | 69 |
if (FontUtilities.isSolaris8 && |
2 | 70 |
getGlyphFromCMAP(JA_WAVE_DASH_CHAR) == missingGlyph) { |
71 |
remapJAWaveDash = true; |
|
72 |
} |
|
73 |
} else { |
|
74 |
needsJAremapping = false; |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
public int getNumGlyphs() { |
|
79 |
return numGlyphs; |
|
80 |
} |
|
81 |
||
82 |
private char getGlyphFromCMAP(int charCode) { |
|
83 |
try { |
|
84 |
char glyphCode = cmap.getGlyph(charCode); |
|
85 |
if (glyphCode < numGlyphs || |
|
86 |
glyphCode >= FileFontStrike.INVISIBLE_GLYPHS) { |
|
87 |
return glyphCode; |
|
88 |
} else { |
|
3928 | 89 |
if (FontUtilities.isLogging()) { |
90 |
FontUtilities.getLogger().warning |
|
2 | 91 |
(font + " out of range glyph id=" + |
92 |
Integer.toHexString((int)glyphCode) + |
|
93 |
" for char " + Integer.toHexString(charCode)); |
|
94 |
} |
|
95 |
return (char)missingGlyph; |
|
96 |
} |
|
97 |
} catch(Exception e) { |
|
98 |
handleBadCMAP(); |
|
99 |
return (char) missingGlyph; |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
private void handleBadCMAP() { |
|
3928 | 104 |
if (FontUtilities.isLogging()) { |
105 |
FontUtilities.getLogger().severe("Null Cmap for " + font + |
|
2 | 106 |
"substituting for this font"); |
107 |
} |
|
3928 | 108 |
SunFontManager.getInstance().deRegisterBadFont(font); |
2 | 109 |
/* The next line is not really a solution, but might |
110 |
* reduce the exceptions until references to this font2D |
|
111 |
* are gone. |
|
112 |
*/ |
|
113 |
cmap = CMap.theNullCmap; |
|
114 |
} |
|
115 |
||
24527 | 116 |
@SuppressWarnings("fallthrough") |
2 | 117 |
private final char remapJAChar(char unicode) { |
118 |
switch (unicode) { |
|
119 |
case REVERSE_SOLIDUS: |
|
120 |
return JA_YEN; |
|
121 |
/* This is a workaround for bug 4533422. |
|
122 |
* Japanese wave dash missing from Solaris JA TrueType fonts. |
|
123 |
*/ |
|
124 |
case JA_WAVE_DASH_CHAR: |
|
125 |
if (remapJAWaveDash) { |
|
126 |
return JA_FULLWIDTH_TILDE_CHAR; |
|
127 |
} |
|
128 |
default: return unicode; |
|
129 |
} |
|
130 |
} |
|
24527 | 131 |
@SuppressWarnings("fallthrough") |
2 | 132 |
private final int remapJAIntChar(int unicode) { |
133 |
switch (unicode) { |
|
134 |
case REVERSE_SOLIDUS: |
|
135 |
return JA_YEN; |
|
136 |
/* This is a workaround for bug 4533422. |
|
137 |
* Japanese wave dash missing from Solaris JA TrueType fonts. |
|
138 |
*/ |
|
139 |
case JA_WAVE_DASH_CHAR: |
|
140 |
if (remapJAWaveDash) { |
|
141 |
return JA_FULLWIDTH_TILDE_CHAR; |
|
142 |
} |
|
143 |
default: return unicode; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
public int charToGlyph(char unicode) { |
|
148 |
if (needsJAremapping) { |
|
149 |
unicode = remapJAChar(unicode); |
|
150 |
} |
|
151 |
int glyph = getGlyphFromCMAP(unicode); |
|
152 |
if (font.checkUseNatives() && glyph < font.glyphToCharMap.length) { |
|
153 |
font.glyphToCharMap[glyph] = unicode; |
|
154 |
} |
|
155 |
return glyph; |
|
156 |
} |
|
157 |
||
158 |
public int charToGlyph(int unicode) { |
|
159 |
if (needsJAremapping) { |
|
160 |
unicode = remapJAIntChar(unicode); |
|
161 |
} |
|
162 |
int glyph = getGlyphFromCMAP(unicode); |
|
163 |
if (font.checkUseNatives() && glyph < font.glyphToCharMap.length) { |
|
164 |
font.glyphToCharMap[glyph] = (char)unicode; |
|
165 |
} |
|
166 |
return glyph; |
|
167 |
} |
|
168 |
||
169 |
public void charsToGlyphs(int count, int[] unicodes, int[] glyphs) { |
|
170 |
for (int i=0;i<count;i++) { |
|
171 |
if (needsJAremapping) { |
|
172 |
glyphs[i] = getGlyphFromCMAP(remapJAIntChar(unicodes[i])); |
|
173 |
} else { |
|
174 |
glyphs[i] = getGlyphFromCMAP(unicodes[i]); |
|
175 |
} |
|
176 |
if (font.checkUseNatives() && |
|
177 |
glyphs[i] < font.glyphToCharMap.length) { |
|
178 |
font.glyphToCharMap[glyphs[i]] = (char)unicodes[i]; |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
public void charsToGlyphs(int count, char[] unicodes, int[] glyphs) { |
|
184 |
||
185 |
for (int i=0; i<count; i++) { |
|
186 |
int code; |
|
187 |
if (needsJAremapping) { |
|
188 |
code = remapJAChar(unicodes[i]); |
|
189 |
} else { |
|
190 |
code = unicodes[i]; // char is unsigned. |
|
191 |
} |
|
192 |
||
193 |
if (code >= HI_SURROGATE_START && |
|
194 |
code <= HI_SURROGATE_END && i < count - 1) { |
|
195 |
char low = unicodes[i + 1]; |
|
196 |
||
197 |
if (low >= LO_SURROGATE_START && |
|
198 |
low <= LO_SURROGATE_END) { |
|
199 |
code = (code - HI_SURROGATE_START) * |
|
200 |
0x400 + low - LO_SURROGATE_START + 0x10000; |
|
201 |
||
202 |
glyphs[i] = getGlyphFromCMAP(code); |
|
203 |
i += 1; // Empty glyph slot after surrogate |
|
204 |
glyphs[i] = INVISIBLE_GLYPH_ID; |
|
205 |
continue; |
|
206 |
} |
|
207 |
} |
|
208 |
glyphs[i] = getGlyphFromCMAP(code); |
|
209 |
||
210 |
if (font.checkUseNatives() && |
|
211 |
glyphs[i] < font.glyphToCharMap.length) { |
|
212 |
font.glyphToCharMap[glyphs[i]] = (char)code; |
|
213 |
} |
|
214 |
||
215 |
} |
|
216 |
} |
|
217 |
||
218 |
/* This variant checks if shaping is needed and immediately |
|
219 |
* returns true if it does. A caller of this method should be expecting |
|
220 |
* to check the return type because it needs to know how to handle |
|
221 |
* the character data for display. |
|
222 |
*/ |
|
223 |
public boolean charsToGlyphsNS(int count, char[] unicodes, int[] glyphs) { |
|
224 |
||
225 |
for (int i=0; i<count; i++) { |
|
226 |
int code; |
|
227 |
if (needsJAremapping) { |
|
228 |
code = remapJAChar(unicodes[i]); |
|
229 |
} else { |
|
230 |
code = unicodes[i]; // char is unsigned. |
|
231 |
} |
|
232 |
||
233 |
if (code >= HI_SURROGATE_START && |
|
234 |
code <= HI_SURROGATE_END && i < count - 1) { |
|
235 |
char low = unicodes[i + 1]; |
|
236 |
||
237 |
if (low >= LO_SURROGATE_START && |
|
238 |
low <= LO_SURROGATE_END) { |
|
239 |
code = (code - HI_SURROGATE_START) * |
|
240 |
0x400 + low - LO_SURROGATE_START + 0x10000; |
|
241 |
glyphs[i + 1] = INVISIBLE_GLYPH_ID; |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
glyphs[i] = getGlyphFromCMAP(code); |
|
246 |
if (font.checkUseNatives() && |
|
247 |
glyphs[i] < font.glyphToCharMap.length) { |
|
248 |
font.glyphToCharMap[glyphs[i]] = (char)code; |
|
249 |
} |
|
250 |
||
3928 | 251 |
if (code < FontUtilities.MIN_LAYOUT_CHARCODE) { |
2 | 252 |
continue; |
253 |
} |
|
3928 | 254 |
else if (FontUtilities.isComplexCharCode(code)) { |
2 | 255 |
return true; |
256 |
} |
|
257 |
else if (code >= 0x10000) { |
|
258 |
i += 1; // Empty glyph slot after surrogate |
|
259 |
continue; |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
return false; |
|
264 |
} |
|
265 |
||
266 |
/* A pretty good heuristic is that the cmap we are using |
|
267 |
* supports 32 bit character codes. |
|
268 |
*/ |
|
269 |
boolean hasSupplementaryChars() { |
|
270 |
return |
|
271 |
cmap instanceof CMap.CMapFormat8 || |
|
272 |
cmap instanceof CMap.CMapFormat10 || |
|
273 |
cmap instanceof CMap.CMapFormat12; |
|
274 |
} |
|
275 |
} |