author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 550 | e85f91b9bb95 |
child 1721 | 7a86d7e13fdf |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.font; |
|
27 |
||
28 |
import java.awt.Font; |
|
29 |
import java.awt.FontFormatException; |
|
30 |
import java.awt.GraphicsEnvironment; |
|
31 |
import java.awt.geom.Point2D; |
|
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.RandomAccessFile; |
|
35 |
import java.io.UnsupportedEncodingException; |
|
36 |
import java.nio.ByteBuffer; |
|
37 |
import java.nio.CharBuffer; |
|
38 |
import java.nio.IntBuffer; |
|
39 |
import java.nio.ShortBuffer; |
|
40 |
import java.nio.channels.ClosedChannelException; |
|
41 |
import java.nio.channels.FileChannel; |
|
42 |
import java.util.HashSet; |
|
43 |
import java.util.Locale; |
|
44 |
import java.util.logging.Level; |
|
45 |
import sun.java2d.Disposer; |
|
46 |
import sun.java2d.DisposerRecord; |
|
47 |
||
48 |
/** |
|
49 |
* TrueTypeFont is not called SFntFont because it is not expected |
|
50 |
* to handle all types that may be housed in a such a font file. |
|
51 |
* If additional types are supported later, it may make sense to |
|
52 |
* create an SFnt superclass. Eg to handle sfnt-housed postscript fonts. |
|
53 |
* OpenType fonts are handled by this class, and possibly should be |
|
54 |
* represented by a subclass. |
|
55 |
* An instance stores some information from the font file to faciliate |
|
56 |
* faster access. File size, the table directory and the names of the font |
|
57 |
* are the most important of these. It amounts to approx 400 bytes |
|
58 |
* for a typical font. Systems with mutiple locales sometimes have up to 400 |
|
59 |
* font files, and an app which loads all font files would need around |
|
60 |
* 160Kbytes. So storing any more info than this would be expensive. |
|
61 |
*/ |
|
62 |
public class TrueTypeFont extends FileFont { |
|
63 |
||
64 |
/* -- Tags for required TrueType tables */ |
|
65 |
public static final int cmapTag = 0x636D6170; // 'cmap' |
|
66 |
public static final int glyfTag = 0x676C7966; // 'glyf' |
|
67 |
public static final int headTag = 0x68656164; // 'head' |
|
68 |
public static final int hheaTag = 0x68686561; // 'hhea' |
|
69 |
public static final int hmtxTag = 0x686D7478; // 'hmtx' |
|
70 |
public static final int locaTag = 0x6C6F6361; // 'loca' |
|
71 |
public static final int maxpTag = 0x6D617870; // 'maxp' |
|
72 |
public static final int nameTag = 0x6E616D65; // 'name' |
|
73 |
public static final int postTag = 0x706F7374; // 'post' |
|
74 |
public static final int os_2Tag = 0x4F532F32; // 'OS/2' |
|
75 |
||
76 |
/* -- Tags for opentype related tables */ |
|
77 |
public static final int GDEFTag = 0x47444546; // 'GDEF' |
|
78 |
public static final int GPOSTag = 0x47504F53; // 'GPOS' |
|
79 |
public static final int GSUBTag = 0x47535542; // 'GSUB' |
|
80 |
public static final int mortTag = 0x6D6F7274; // 'mort' |
|
81 |
||
82 |
/* -- Tags for non-standard tables */ |
|
83 |
public static final int fdscTag = 0x66647363; // 'fdsc' - gxFont descriptor |
|
84 |
public static final int fvarTag = 0x66766172; // 'fvar' - gxFont variations |
|
85 |
public static final int featTag = 0x66656174; // 'feat' - layout features |
|
86 |
public static final int EBLCTag = 0x45424C43; // 'EBLC' - embedded bitmaps |
|
87 |
public static final int gaspTag = 0x67617370; // 'gasp' - hint/smooth sizes |
|
88 |
||
89 |
/* -- Other tags */ |
|
90 |
public static final int ttcfTag = 0x74746366; // 'ttcf' - TTC file |
|
91 |
public static final int v1ttTag = 0x00010000; // 'v1tt' - Version 1 TT font |
|
92 |
public static final int trueTag = 0x74727565; // 'true' - Version 2 TT font |
|
93 |
||
94 |
/* -- ID's used in the 'name' table */ |
|
95 |
public static final int MS_PLATFORM_ID = 3; |
|
96 |
/* MS locale id for US English is the "default" */ |
|
97 |
public static final short ENGLISH_LOCALE_ID = 0x0409; // 1033 decimal |
|
98 |
public static final int FAMILY_NAME_ID = 1; |
|
99 |
// public static final int STYLE_WEIGHT_ID = 2; // currently unused. |
|
100 |
public static final int FULL_NAME_ID = 4; |
|
101 |
public static final int POSTSCRIPT_NAME_ID = 6; |
|
102 |
||
103 |
||
104 |
class DirectoryEntry { |
|
105 |
int tag; |
|
106 |
int offset; |
|
107 |
int length; |
|
108 |
} |
|
109 |
||
110 |
/* There is a pool which limits the number of fd's that are in |
|
111 |
* use. Normally fd's are closed as they are replaced in the pool. |
|
112 |
* But if an instance of this class becomes unreferenced, then there |
|
113 |
* needs to be a way to close the fd. A finalize() method could do this, |
|
114 |
* but using the Disposer class will ensure its called in a more timely |
|
115 |
* manner. This is not something which should be relied upon to free |
|
116 |
* fd's - its a safeguard. |
|
117 |
*/ |
|
118 |
private static class TTDisposerRecord implements DisposerRecord { |
|
119 |
||
120 |
FileChannel channel = null; |
|
121 |
||
122 |
public synchronized void dispose() { |
|
123 |
try { |
|
124 |
if (channel != null) { |
|
125 |
channel.close(); |
|
126 |
} |
|
127 |
} catch (IOException e) { |
|
128 |
} finally { |
|
129 |
channel = null; |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
TTDisposerRecord disposerRecord = new TTDisposerRecord(); |
|
135 |
||
136 |
/* > 0 only if this font is a part of a collection */ |
|
137 |
int fontIndex = 0; |
|
138 |
||
139 |
/* Number of fonts in this collection. ==1 if not a collection */ |
|
140 |
int directoryCount = 1; |
|
141 |
||
142 |
/* offset in file of table directory for this font */ |
|
143 |
int directoryOffset; // 12 if its not a collection. |
|
144 |
||
145 |
/* number of table entries in the directory/offsets table */ |
|
146 |
int numTables; |
|
147 |
||
148 |
/* The contents of the the directory/offsets table */ |
|
149 |
DirectoryEntry []tableDirectory; |
|
150 |
||
151 |
// protected byte []gposTable = null; |
|
152 |
// protected byte []gdefTable = null; |
|
153 |
// protected byte []gsubTable = null; |
|
154 |
// protected byte []mortTable = null; |
|
155 |
// protected boolean hintsTabledChecked = false; |
|
156 |
// protected boolean containsHintsTable = false; |
|
157 |
||
158 |
/* These fields are set from os/2 table info. */ |
|
159 |
private boolean supportsJA; |
|
160 |
private boolean supportsCJK; |
|
161 |
||
162 |
/** |
|
163 |
* - does basic verification of the file |
|
164 |
* - reads the header table for this font (within a collection) |
|
165 |
* - reads the names (full, family). |
|
166 |
* - determines the style of the font. |
|
167 |
* - initializes the CMAP |
|
168 |
* @throws FontFormatException - if the font can't be opened |
|
169 |
* or fails verification, or there's no usable cmap |
|
170 |
*/ |
|
171 |
TrueTypeFont(String platname, Object nativeNames, int fIndex, |
|
172 |
boolean javaRasterizer) |
|
173 |
throws FontFormatException { |
|
174 |
super(platname, nativeNames); |
|
175 |
useJavaRasterizer = javaRasterizer; |
|
176 |
fontRank = Font2D.TTF_RANK; |
|
177 |
verify(); |
|
178 |
init(fIndex); |
|
179 |
Disposer.addObjectRecord(this, disposerRecord); |
|
180 |
} |
|
181 |
||
182 |
/* Enable natives just for fonts picked up from the platform that |
|
183 |
* may have external bitmaps on Solaris. Could do this just for |
|
184 |
* the fonts that are specified in font configuration files which |
|
185 |
* would lighten the burden (think about that). |
|
186 |
* The EBLCTag is used to skip natives for fonts that contain embedded |
|
187 |
* bitmaps as there's no need to use X11 for those fonts. |
|
188 |
* Skip all the latin fonts as they don't need this treatment. |
|
189 |
* Further refine this to fonts that are natively accessible (ie |
|
190 |
* as PCF bitmap fonts on the X11 font path). |
|
191 |
* This method is called when creating the first strike for this font. |
|
192 |
*/ |
|
193 |
protected boolean checkUseNatives() { |
|
194 |
if (checkedNatives) { |
|
195 |
return useNatives; |
|
196 |
} |
|
197 |
if (!FontManager.isSolaris || useJavaRasterizer || |
|
198 |
FontManager.useT2K || nativeNames == null || |
|
199 |
getDirectoryEntry(EBLCTag) != null || |
|
200 |
GraphicsEnvironment.isHeadless()) { |
|
201 |
checkedNatives = true; |
|
202 |
return false; /* useNatives is false */ |
|
203 |
} else if (nativeNames instanceof String) { |
|
204 |
String name = (String)nativeNames; |
|
205 |
/* Don't do do this for Latin fonts */ |
|
206 |
if (name.indexOf("8859") > 0) { |
|
207 |
checkedNatives = true; |
|
208 |
return false; |
|
209 |
} else if (NativeFont.hasExternalBitmaps(name)) { |
|
210 |
nativeFonts = new NativeFont[1]; |
|
211 |
try { |
|
212 |
nativeFonts[0] = new NativeFont(name, true); |
|
213 |
/* If reach here we have an non-latin font that has |
|
214 |
* external bitmaps and we successfully created it. |
|
215 |
*/ |
|
216 |
useNatives = true; |
|
217 |
} catch (FontFormatException e) { |
|
218 |
nativeFonts = null; |
|
219 |
} |
|
220 |
} |
|
221 |
} else if (nativeNames instanceof String[]) { |
|
222 |
String[] natNames = (String[])nativeNames; |
|
223 |
int numNames = natNames.length; |
|
224 |
boolean externalBitmaps = false; |
|
225 |
for (int nn = 0; nn < numNames; nn++) { |
|
226 |
if (natNames[nn].indexOf("8859") > 0) { |
|
227 |
checkedNatives = true; |
|
228 |
return false; |
|
229 |
} else if (NativeFont.hasExternalBitmaps(natNames[nn])) { |
|
230 |
externalBitmaps = true; |
|
231 |
} |
|
232 |
} |
|
233 |
if (!externalBitmaps) { |
|
234 |
checkedNatives = true; |
|
235 |
return false; |
|
236 |
} |
|
237 |
useNatives = true; |
|
238 |
nativeFonts = new NativeFont[numNames]; |
|
239 |
for (int nn = 0; nn < numNames; nn++) { |
|
240 |
try { |
|
241 |
nativeFonts[nn] = new NativeFont(natNames[nn], true); |
|
242 |
} catch (FontFormatException e) { |
|
243 |
useNatives = false; |
|
244 |
nativeFonts = null; |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
if (useNatives) { |
|
249 |
glyphToCharMap = new char[getMapper().getNumGlyphs()]; |
|
250 |
} |
|
251 |
checkedNatives = true; |
|
252 |
return useNatives; |
|
253 |
} |
|
254 |
||
255 |
||
256 |
/* This is intended to be called, and the returned value used, |
|
257 |
* from within a block synchronized on this font object. |
|
258 |
* ie the channel returned may be nulled out at any time by "close()" |
|
259 |
* unless the caller holds a lock. |
|
260 |
* Deadlock warning: FontManager.addToPool(..) acquires a global lock, |
|
261 |
* which means nested locks may be in effect. |
|
262 |
*/ |
|
263 |
private synchronized FileChannel open() throws FontFormatException { |
|
264 |
if (disposerRecord.channel == null) { |
|
265 |
if (FontManager.logging) { |
|
266 |
FontManager.logger.info("open TTF: " + platName); |
|
267 |
} |
|
268 |
try { |
|
269 |
RandomAccessFile raf = (RandomAccessFile) |
|
270 |
java.security.AccessController.doPrivileged( |
|
271 |
new java.security.PrivilegedAction() { |
|
272 |
public Object run() { |
|
273 |
try { |
|
274 |
return new RandomAccessFile(platName, "r"); |
|
275 |
} catch (FileNotFoundException ffne) { |
|
276 |
} |
|
277 |
return null; |
|
278 |
} |
|
279 |
}); |
|
280 |
disposerRecord.channel = raf.getChannel(); |
|
281 |
fileSize = (int)disposerRecord.channel.size(); |
|
282 |
FontManager.addToPool(this); |
|
283 |
} catch (NullPointerException e) { |
|
284 |
close(); |
|
285 |
throw new FontFormatException(e.toString()); |
|
286 |
} catch (ClosedChannelException e) { |
|
287 |
/* NIO I/O is interruptible, recurse to retry operation. |
|
288 |
* The call to channel.size() above can throw this exception. |
|
289 |
* Clear interrupts before recursing in case NIO didn't. |
|
290 |
* Note that close() sets disposerRecord.channel to null. |
|
291 |
*/ |
|
292 |
Thread.interrupted(); |
|
293 |
close(); |
|
294 |
open(); |
|
295 |
} catch (IOException e) { |
|
296 |
close(); |
|
297 |
throw new FontFormatException(e.toString()); |
|
298 |
} |
|
299 |
} |
|
300 |
return disposerRecord.channel; |
|
301 |
} |
|
302 |
||
303 |
protected synchronized void close() { |
|
304 |
disposerRecord.dispose(); |
|
305 |
} |
|
306 |
||
307 |
||
308 |
int readBlock(ByteBuffer buffer, int offset, int length) { |
|
309 |
int bread = 0; |
|
310 |
try { |
|
311 |
synchronized (this) { |
|
312 |
if (disposerRecord.channel == null) { |
|
313 |
open(); |
|
314 |
} |
|
315 |
if (offset + length > fileSize) { |
|
316 |
if (offset >= fileSize) { |
|
317 |
/* Since the caller ensures that offset is < fileSize |
|
318 |
* this condition suggests that fileSize is now |
|
319 |
* different than the value we originally provided |
|
320 |
* to native when the scaler was created. |
|
321 |
* Also fileSize is updated every time we |
|
322 |
* open() the file here, but in native the value |
|
323 |
* isn't updated. If the file has changed whilst we |
|
324 |
* are executing we want to bail, not spin. |
|
325 |
*/ |
|
326 |
if (FontManager.logging) { |
|
327 |
String msg = "Read offset is " + offset + |
|
328 |
" file size is " + fileSize+ |
|
329 |
" file is " + platName; |
|
330 |
FontManager.logger.severe(msg); |
|
331 |
} |
|
332 |
return -1; |
|
333 |
} else { |
|
334 |
length = fileSize - offset; |
|
335 |
} |
|
336 |
} |
|
337 |
buffer.clear(); |
|
338 |
disposerRecord.channel.position(offset); |
|
339 |
while (bread < length) { |
|
340 |
int cnt = disposerRecord.channel.read(buffer); |
|
341 |
if (cnt == -1) { |
|
342 |
String msg = "Unexpected EOF " + this; |
|
343 |
int currSize = (int)disposerRecord.channel.size(); |
|
344 |
if (currSize != fileSize) { |
|
345 |
msg += " File size was " + fileSize + |
|
346 |
" and now is " + currSize; |
|
347 |
} |
|
348 |
if (FontManager.logging) { |
|
349 |
FontManager.logger.severe(msg); |
|
350 |
} |
|
351 |
// We could still flip() the buffer here because |
|
352 |
// it's possible that we did read some data in |
|
353 |
// an earlier loop, and we probably should |
|
354 |
// return that to the caller. Although if |
|
355 |
// the caller expected 8K of data and we return |
|
356 |
// only a few bytes then maybe it's better instead to |
|
357 |
// set bread = -1 to indicate failure. |
|
358 |
// The following is therefore using arbitrary values |
|
359 |
// but is meant to allow cases where enough |
|
360 |
// data was read to probably continue. |
|
361 |
if (bread > length/2 || bread > 16384) { |
|
362 |
buffer.flip(); |
|
363 |
if (FontManager.logging) { |
|
364 |
msg = "Returning " + bread + |
|
365 |
" bytes instead of " + length; |
|
366 |
FontManager.logger.severe(msg); |
|
367 |
} |
|
368 |
} else { |
|
369 |
bread = -1; |
|
370 |
} |
|
371 |
throw new IOException(msg); |
|
372 |
} |
|
373 |
bread += cnt; |
|
374 |
} |
|
375 |
buffer.flip(); |
|
376 |
if (bread > length) { // possible if buffer.size() > length |
|
377 |
bread = length; |
|
378 |
} |
|
379 |
} |
|
380 |
} catch (FontFormatException e) { |
|
381 |
if (FontManager.logging) { |
|
382 |
FontManager.logger.log(Level.SEVERE, |
|
383 |
"While reading " + platName, e); |
|
384 |
} |
|
385 |
bread = -1; // signal EOF |
|
386 |
deregisterFontAndClearStrikeCache(); |
|
387 |
} catch (ClosedChannelException e) { |
|
388 |
/* NIO I/O is interruptible, recurse to retry operation. |
|
389 |
* Clear interrupts before recursing in case NIO didn't. |
|
390 |
*/ |
|
391 |
Thread.interrupted(); |
|
392 |
close(); |
|
393 |
return readBlock(buffer, offset, length); |
|
394 |
} catch (IOException e) { |
|
395 |
/* If we did not read any bytes at all and the exception is |
|
396 |
* not a recoverable one (ie is not ClosedChannelException) then |
|
397 |
* we should indicate that there is no point in re-trying. |
|
398 |
* Other than an attempt to read past the end of the file it |
|
399 |
* seems unlikely this would occur as problems opening the |
|
400 |
* file are handled as a FontFormatException. |
|
401 |
*/ |
|
402 |
if (FontManager.logging) { |
|
403 |
FontManager.logger.log(Level.SEVERE, |
|
404 |
"While reading " + platName, e); |
|
405 |
} |
|
406 |
if (bread == 0) { |
|
407 |
bread = -1; // signal EOF |
|
408 |
deregisterFontAndClearStrikeCache(); |
|
409 |
} |
|
410 |
} |
|
411 |
return bread; |
|
412 |
} |
|
413 |
||
414 |
ByteBuffer readBlock(int offset, int length) { |
|
415 |
||
416 |
ByteBuffer buffer = ByteBuffer.allocate(length); |
|
417 |
try { |
|
418 |
synchronized (this) { |
|
419 |
if (disposerRecord.channel == null) { |
|
420 |
open(); |
|
421 |
} |
|
422 |
if (offset + length > fileSize) { |
|
423 |
if (offset > fileSize) { |
|
424 |
return null; // assert? |
|
425 |
} else { |
|
426 |
buffer = ByteBuffer.allocate(fileSize-offset); |
|
427 |
} |
|
428 |
} |
|
429 |
disposerRecord.channel.position(offset); |
|
430 |
disposerRecord.channel.read(buffer); |
|
431 |
buffer.flip(); |
|
432 |
} |
|
433 |
} catch (FontFormatException e) { |
|
434 |
return null; |
|
435 |
} catch (ClosedChannelException e) { |
|
436 |
/* NIO I/O is interruptible, recurse to retry operation. |
|
437 |
* Clear interrupts before recursing in case NIO didn't. |
|
438 |
*/ |
|
439 |
Thread.interrupted(); |
|
440 |
close(); |
|
441 |
readBlock(buffer, offset, length); |
|
442 |
} catch (IOException e) { |
|
443 |
return null; |
|
444 |
} |
|
445 |
return buffer; |
|
446 |
} |
|
447 |
||
448 |
/* This is used by native code which can't allocate a direct byte |
|
449 |
* buffer because of bug 4845371. It, and references to it in native |
|
450 |
* code in scalerMethods.c can be removed once that bug is fixed. |
|
451 |
* 4845371 is now fixed but we'll keep this around as it doesn't cost |
|
452 |
* us anything if its never used/called. |
|
453 |
*/ |
|
454 |
byte[] readBytes(int offset, int length) { |
|
455 |
ByteBuffer buffer = readBlock(offset, length); |
|
456 |
if (buffer.hasArray()) { |
|
457 |
return buffer.array(); |
|
458 |
} else { |
|
459 |
byte[] bufferBytes = new byte[buffer.limit()]; |
|
460 |
buffer.get(bufferBytes); |
|
461 |
return bufferBytes; |
|
462 |
} |
|
463 |
} |
|
464 |
||
465 |
private void verify() throws FontFormatException { |
|
466 |
open(); |
|
467 |
} |
|
468 |
||
469 |
/* sizes, in bytes, of TT/TTC header records */ |
|
470 |
private static final int TTCHEADERSIZE = 12; |
|
471 |
private static final int DIRECTORYHEADERSIZE = 12; |
|
472 |
private static final int DIRECTORYENTRYSIZE = 16; |
|
473 |
||
474 |
protected void init(int fIndex) throws FontFormatException { |
|
475 |
int headerOffset = 0; |
|
476 |
ByteBuffer buffer = readBlock(0, TTCHEADERSIZE); |
|
477 |
try { |
|
478 |
switch (buffer.getInt()) { |
|
479 |
||
480 |
case ttcfTag: |
|
481 |
buffer.getInt(); // skip TTC version ID |
|
482 |
directoryCount = buffer.getInt(); |
|
483 |
if (fIndex >= directoryCount) { |
|
484 |
throw new FontFormatException("Bad collection index"); |
|
485 |
} |
|
486 |
fontIndex = fIndex; |
|
487 |
buffer = readBlock(TTCHEADERSIZE+4*fIndex, 4); |
|
488 |
headerOffset = buffer.getInt(); |
|
489 |
break; |
|
490 |
||
491 |
case v1ttTag: |
|
492 |
case trueTag: |
|
493 |
break; |
|
494 |
||
495 |
default: |
|
496 |
throw new FontFormatException("Unsupported sfnt " + platName); |
|
497 |
} |
|
498 |
||
499 |
/* Now have the offset of this TT font (possibly within a TTC) |
|
500 |
* After the TT version/scaler type field, is the short |
|
501 |
* representing the number of tables in the table directory. |
|
502 |
* The table directory begins at 12 bytes after the header. |
|
503 |
* Each table entry is 16 bytes long (4 32-bit ints) |
|
504 |
*/ |
|
505 |
buffer = readBlock(headerOffset+4, 2); |
|
506 |
numTables = buffer.getShort(); |
|
507 |
directoryOffset = headerOffset+DIRECTORYHEADERSIZE; |
|
508 |
ByteBuffer bbuffer = readBlock(directoryOffset, |
|
509 |
numTables*DIRECTORYENTRYSIZE); |
|
510 |
IntBuffer ibuffer = bbuffer.asIntBuffer(); |
|
511 |
DirectoryEntry table; |
|
512 |
tableDirectory = new DirectoryEntry[numTables]; |
|
513 |
for (int i=0; i<numTables;i++) { |
|
514 |
tableDirectory[i] = table = new DirectoryEntry(); |
|
515 |
table.tag = ibuffer.get(); |
|
516 |
/* checksum */ ibuffer.get(); |
|
517 |
table.offset = ibuffer.get(); |
|
518 |
table.length = ibuffer.get(); |
|
519 |
if (table.offset + table.length > fileSize) { |
|
520 |
throw new FontFormatException("bad table, tag="+table.tag); |
|
521 |
} |
|
522 |
} |
|
523 |
initNames(); |
|
524 |
} catch (Exception e) { |
|
525 |
if (FontManager.logging) { |
|
526 |
FontManager.logger.severe(e.toString()); |
|
527 |
} |
|
528 |
if (e instanceof FontFormatException) { |
|
529 |
throw (FontFormatException)e; |
|
530 |
} else { |
|
531 |
throw new FontFormatException(e.toString()); |
|
532 |
} |
|
533 |
} |
|
534 |
if (familyName == null || fullName == null) { |
|
535 |
throw new FontFormatException("Font name not found"); |
|
536 |
} |
|
537 |
/* The os2_Table is needed to gather some info, but we don't |
|
538 |
* want to keep it around (as a field) so obtain it once and |
|
539 |
* pass it to the code that needs it. |
|
540 |
*/ |
|
541 |
ByteBuffer os2_Table = getTableBuffer(os_2Tag); |
|
542 |
setStyle(os2_Table); |
|
543 |
setCJKSupport(os2_Table); |
|
544 |
||
545 |
ByteBuffer head_Table = getTableBuffer(headTag); |
|
546 |
int upem = -1; |
|
547 |
if (head_Table != null && head_Table.capacity() >= 18) { |
|
548 |
ShortBuffer sb = head_Table.asShortBuffer(); |
|
549 |
upem = sb.get(9) & 0xffff; |
|
550 |
} |
|
551 |
setStrikethroughMetrics(os2_Table, upem); |
|
552 |
||
553 |
ByteBuffer post_Table = getTableBuffer(postTag); |
|
554 |
setUnderlineMetrics(post_Table, upem); |
|
555 |
} |
|
556 |
||
557 |
/* The array index corresponds to a bit offset in the TrueType |
|
558 |
* font's OS/2 compatibility table's code page ranges fields. |
|
559 |
* These are two 32 bit unsigned int fields at offsets 78 and 82. |
|
560 |
* We are only interested in determining if the font supports |
|
561 |
* the windows encodings we expect as the default encoding in |
|
562 |
* supported locales, so we only map the first of these fields. |
|
563 |
*/ |
|
564 |
static final String encoding_mapping[] = { |
|
565 |
"cp1252", /* 0:Latin 1 */ |
|
566 |
"cp1250", /* 1:Latin 2 */ |
|
567 |
"cp1251", /* 2:Cyrillic */ |
|
568 |
"cp1253", /* 3:Greek */ |
|
569 |
"cp1254", /* 4:Turkish/Latin 5 */ |
|
570 |
"cp1255", /* 5:Hebrew */ |
|
571 |
"cp1256", /* 6:Arabic */ |
|
572 |
"cp1257", /* 7:Windows Baltic */ |
|
573 |
"", /* 8:reserved for alternate ANSI */ |
|
574 |
"", /* 9:reserved for alternate ANSI */ |
|
575 |
"", /* 10:reserved for alternate ANSI */ |
|
576 |
"", /* 11:reserved for alternate ANSI */ |
|
577 |
"", /* 12:reserved for alternate ANSI */ |
|
578 |
"", /* 13:reserved for alternate ANSI */ |
|
579 |
"", /* 14:reserved for alternate ANSI */ |
|
580 |
"", /* 15:reserved for alternate ANSI */ |
|
581 |
"ms874", /* 16:Thai */ |
|
582 |
"ms932", /* 17:JIS/Japanese */ |
|
583 |
"gbk", /* 18:PRC GBK Cp950 */ |
|
584 |
"ms949", /* 19:Korean Extended Wansung */ |
|
585 |
"ms950", /* 20:Chinese (Taiwan, Hongkong, Macau) */ |
|
586 |
"ms1361", /* 21:Korean Johab */ |
|
587 |
"", /* 22 */ |
|
588 |
"", /* 23 */ |
|
589 |
"", /* 24 */ |
|
590 |
"", /* 25 */ |
|
591 |
"", /* 26 */ |
|
592 |
"", /* 27 */ |
|
593 |
"", /* 28 */ |
|
594 |
"", /* 29 */ |
|
595 |
"", /* 30 */ |
|
596 |
"", /* 31 */ |
|
597 |
}; |
|
598 |
||
599 |
/* This maps two letter language codes to a Windows code page. |
|
600 |
* Note that eg Cp1252 (the first subarray) is not exactly the same as |
|
601 |
* Latin-1 since Windows code pages are do not necessarily correspond. |
|
602 |
* There are two codepages for zh and ko so if a font supports |
|
603 |
* only one of these ranges then we need to distinguish based on |
|
604 |
* country. So far this only seems to matter for zh. |
|
605 |
* REMIND: Unicode locales such as Hindi do not have a code page so |
|
606 |
* this whole mechansim needs to be revised to map languages to |
|
607 |
* the Unicode ranges either when this fails, or as an additional |
|
608 |
* validating test. Basing it on Unicode ranges should get us away |
|
609 |
* from needing to map to this small and incomplete set of Windows |
|
610 |
* code pages which looks odd on non-Windows platforms. |
|
611 |
*/ |
|
612 |
private static final String languages[][] = { |
|
613 |
||
614 |
/* cp1252/Latin 1 */ |
|
615 |
{ "en", "ca", "da", "de", "es", "fi", "fr", "is", "it", |
|
616 |
"nl", "no", "pt", "sq", "sv", }, |
|
617 |
||
618 |
/* cp1250/Latin2 */ |
|
619 |
{ "cs", "cz", "et", "hr", "hu", "nr", "pl", "ro", "sk", |
|
620 |
"sl", "sq", "sr", }, |
|
621 |
||
622 |
/* cp1251/Cyrillic */ |
|
623 |
{ "bg", "mk", "ru", "sh", "uk" }, |
|
624 |
||
625 |
/* cp1253/Greek*/ |
|
626 |
{ "el" }, |
|
627 |
||
628 |
/* cp1254/Turkish,Latin 5 */ |
|
629 |
{ "tr" }, |
|
630 |
||
631 |
/* cp1255/Hebrew */ |
|
632 |
{ "he" }, |
|
633 |
||
634 |
/* cp1256/Arabic */ |
|
635 |
{ "ar" }, |
|
636 |
||
637 |
/* cp1257/Windows Baltic */ |
|
638 |
{ "et", "lt", "lv" }, |
|
639 |
||
640 |
/* ms874/Thai */ |
|
641 |
{ "th" }, |
|
642 |
||
643 |
/* ms932/Japanese */ |
|
644 |
{ "ja" }, |
|
645 |
||
646 |
/* gbk/Chinese (PRC GBK Cp950) */ |
|
647 |
{ "zh", "zh_CN", }, |
|
648 |
||
649 |
/* ms949/Korean Extended Wansung */ |
|
650 |
{ "ko" }, |
|
651 |
||
652 |
/* ms950/Chinese (Taiwan, Hongkong, Macau) */ |
|
653 |
{ "zh_HK", "zh_TW", }, |
|
654 |
||
655 |
/* ms1361/Korean Johab */ |
|
656 |
{ "ko" }, |
|
657 |
}; |
|
658 |
||
659 |
private static final String codePages[] = { |
|
660 |
"cp1252", |
|
661 |
"cp1250", |
|
662 |
"cp1251", |
|
663 |
"cp1253", |
|
664 |
"cp1254", |
|
665 |
"cp1255", |
|
666 |
"cp1256", |
|
667 |
"cp1257", |
|
668 |
"ms874", |
|
669 |
"ms932", |
|
670 |
"gbk", |
|
671 |
"ms949", |
|
672 |
"ms950", |
|
673 |
"ms1361", |
|
674 |
}; |
|
675 |
||
676 |
private static String defaultCodePage = null; |
|
677 |
static String getCodePage() { |
|
678 |
||
679 |
if (defaultCodePage != null) { |
|
680 |
return defaultCodePage; |
|
681 |
} |
|
682 |
||
683 |
if (FontManager.isWindows) { |
|
684 |
defaultCodePage = |
|
685 |
(String)java.security.AccessController.doPrivileged( |
|
686 |
new sun.security.action.GetPropertyAction("file.encoding")); |
|
687 |
} else { |
|
688 |
if (languages.length != codePages.length) { |
|
689 |
throw new InternalError("wrong code pages array length"); |
|
690 |
} |
|
691 |
Locale locale = sun.awt.SunToolkit.getStartupLocale(); |
|
692 |
||
693 |
String language = locale.getLanguage(); |
|
694 |
if (language != null) { |
|
695 |
if (language.equals("zh")) { |
|
696 |
String country = locale.getCountry(); |
|
697 |
if (country != null) { |
|
698 |
language = language + "_" + country; |
|
699 |
} |
|
700 |
} |
|
701 |
for (int i=0; i<languages.length;i++) { |
|
702 |
for (int l=0;l<languages[i].length; l++) { |
|
703 |
if (language.equals(languages[i][l])) { |
|
704 |
defaultCodePage = codePages[i]; |
|
705 |
return defaultCodePage; |
|
706 |
} |
|
707 |
} |
|
708 |
} |
|
709 |
} |
|
710 |
} |
|
711 |
if (defaultCodePage == null) { |
|
712 |
defaultCodePage = ""; |
|
713 |
} |
|
714 |
return defaultCodePage; |
|
715 |
} |
|
716 |
||
717 |
/* Theoretically, reserved bits must not be set, include symbol bits */ |
|
718 |
public static final int reserved_bits1 = 0x80000000; |
|
719 |
public static final int reserved_bits2 = 0x0000ffff; |
|
720 |
boolean supportsEncoding(String encoding) { |
|
721 |
if (encoding == null) { |
|
722 |
encoding = getCodePage(); |
|
723 |
} |
|
724 |
if ("".equals(encoding)) { |
|
725 |
return false; |
|
726 |
} |
|
727 |
||
728 |
encoding = encoding.toLowerCase(); |
|
729 |
||
730 |
/* java_props_md.c has a couple of special cases |
|
731 |
* if language packs are installed. In these encodings the |
|
732 |
* fontconfig files pick up different fonts : |
|
733 |
* SimSun-18030 and MingLiU_HKSCS. Since these fonts will |
|
734 |
* indicate they support the base encoding, we need to rewrite |
|
735 |
* these encodings here before checking the map/array. |
|
736 |
*/ |
|
737 |
if (encoding.equals("gb18030")) { |
|
738 |
encoding = "gbk"; |
|
739 |
} else if (encoding.equals("ms950_hkscs")) { |
|
740 |
encoding = "ms950"; |
|
741 |
} |
|
742 |
||
743 |
ByteBuffer buffer = getTableBuffer(os_2Tag); |
|
744 |
/* required info is at offsets 78 and 82 */ |
|
745 |
if (buffer == null || buffer.capacity() < 86) { |
|
746 |
return false; |
|
747 |
} |
|
748 |
||
749 |
int range1 = buffer.getInt(78); /* ulCodePageRange1 */ |
|
750 |
int range2 = buffer.getInt(82); /* ulCodePageRange2 */ |
|
751 |
||
752 |
/* This test is too stringent for Arial on Solaris (and perhaps |
|
753 |
* other fonts). Arial has at least one reserved bit set for an |
|
754 |
* unknown reason. |
|
755 |
*/ |
|
756 |
// if (((range1 & reserved_bits1) | (range2 & reserved_bits2)) != 0) { |
|
757 |
// return false; |
|
758 |
// } |
|
759 |
||
760 |
for (int em=0; em<encoding_mapping.length; em++) { |
|
761 |
if (encoding_mapping[em].equals(encoding)) { |
|
762 |
if (((1 << em) & range1) != 0) { |
|
763 |
return true; |
|
764 |
} |
|
765 |
} |
|
766 |
} |
|
767 |
return false; |
|
768 |
} |
|
769 |
||
770 |
||
771 |
/* Use info in the os_2Table to test CJK support */ |
|
772 |
private void setCJKSupport(ByteBuffer os2Table) { |
|
773 |
/* required info is in ulong at offset 46 */ |
|
774 |
if (os2Table == null || os2Table.capacity() < 50) { |
|
775 |
return; |
|
776 |
} |
|
777 |
int range2 = os2Table.getInt(46); /* ulUnicodeRange2 */ |
|
778 |
||
779 |
/* Any of these bits set in the 32-63 range indicate a font with |
|
780 |
* support for a CJK range. We aren't looking at some other bits |
|
781 |
* in the 64-69 range such as half width forms as its unlikely a font |
|
782 |
* would include those and none of these. |
|
783 |
*/ |
|
784 |
supportsCJK = ((range2 & 0x29bf0000) != 0); |
|
785 |
||
786 |
/* This should be generalised, but for now just need to know if |
|
787 |
* Hiragana or Katakana ranges are supported by the font. |
|
788 |
* In the 4 longs representing unicode ranges supported |
|
789 |
* bits 49 & 50 indicate hiragana and katakana |
|
790 |
* This is bits 17 & 18 in the 2nd ulong. If either is supported |
|
791 |
* we presume this is a JA font. |
|
792 |
*/ |
|
793 |
supportsJA = ((range2 & 0x60000) != 0); |
|
794 |
} |
|
795 |
||
796 |
boolean supportsJA() { |
|
797 |
return supportsJA; |
|
798 |
} |
|
799 |
||
800 |
ByteBuffer getTableBuffer(int tag) { |
|
801 |
DirectoryEntry entry = null; |
|
802 |
||
803 |
for (int i=0;i<numTables;i++) { |
|
804 |
if (tableDirectory[i].tag == tag) { |
|
805 |
entry = tableDirectory[i]; |
|
806 |
break; |
|
807 |
} |
|
808 |
} |
|
809 |
if (entry == null || entry.length == 0 || |
|
810 |
entry.offset+entry.length > fileSize) { |
|
811 |
return null; |
|
812 |
} |
|
813 |
||
814 |
int bread = 0; |
|
815 |
ByteBuffer buffer = ByteBuffer.allocate(entry.length); |
|
816 |
synchronized (this) { |
|
817 |
try { |
|
818 |
if (disposerRecord.channel == null) { |
|
819 |
open(); |
|
820 |
} |
|
821 |
disposerRecord.channel.position(entry.offset); |
|
822 |
bread = disposerRecord.channel.read(buffer); |
|
823 |
buffer.flip(); |
|
824 |
} catch (ClosedChannelException e) { |
|
825 |
/* NIO I/O is interruptible, recurse to retry operation. |
|
826 |
* Clear interrupts before recursing in case NIO didn't. |
|
827 |
*/ |
|
828 |
Thread.interrupted(); |
|
829 |
close(); |
|
830 |
return getTableBuffer(tag); |
|
831 |
} catch (IOException e) { |
|
832 |
return null; |
|
833 |
} catch (FontFormatException e) { |
|
834 |
return null; |
|
835 |
} |
|
836 |
||
837 |
if (bread < entry.length) { |
|
838 |
return null; |
|
839 |
} else { |
|
840 |
return buffer; |
|
841 |
} |
|
842 |
} |
|
843 |
} |
|
844 |
||
845 |
/* NB: is it better to move declaration to Font2D? */ |
|
846 |
long getLayoutTableCache() { |
|
847 |
try { |
|
848 |
return getScaler().getLayoutTableCache(); |
|
849 |
} catch(FontScalerException fe) { |
|
850 |
return 0L; |
|
851 |
} |
|
852 |
} |
|
853 |
||
854 |
byte[] getTableBytes(int tag) { |
|
855 |
ByteBuffer buffer = getTableBuffer(tag); |
|
856 |
if (buffer == null) { |
|
857 |
return null; |
|
858 |
} else if (buffer.hasArray()) { |
|
859 |
try { |
|
860 |
return buffer.array(); |
|
861 |
} catch (Exception re) { |
|
862 |
} |
|
863 |
} |
|
864 |
byte []data = new byte[getTableSize(tag)]; |
|
865 |
buffer.get(data); |
|
866 |
return data; |
|
867 |
} |
|
868 |
||
869 |
int getTableSize(int tag) { |
|
870 |
for (int i=0;i<numTables;i++) { |
|
871 |
if (tableDirectory[i].tag == tag) { |
|
872 |
return tableDirectory[i].length; |
|
873 |
} |
|
874 |
} |
|
875 |
return 0; |
|
876 |
} |
|
877 |
||
878 |
int getTableOffset(int tag) { |
|
879 |
for (int i=0;i<numTables;i++) { |
|
880 |
if (tableDirectory[i].tag == tag) { |
|
881 |
return tableDirectory[i].offset; |
|
882 |
} |
|
883 |
} |
|
884 |
return 0; |
|
885 |
} |
|
886 |
||
887 |
DirectoryEntry getDirectoryEntry(int tag) { |
|
888 |
for (int i=0;i<numTables;i++) { |
|
889 |
if (tableDirectory[i].tag == tag) { |
|
890 |
return tableDirectory[i]; |
|
891 |
} |
|
892 |
} |
|
893 |
return null; |
|
894 |
} |
|
895 |
||
550
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
896 |
/* Used to determine if this size has embedded bitmaps, which |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
897 |
* for CJK fonts should be used in preference to LCD glyphs. |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
898 |
*/ |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
899 |
boolean useEmbeddedBitmapsForSize(int ptSize) { |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
900 |
if (!supportsCJK) { |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
901 |
return false; |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
902 |
} |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
903 |
if (getDirectoryEntry(EBLCTag) == null) { |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
904 |
return false; |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
905 |
} |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
906 |
ByteBuffer eblcTable = getTableBuffer(EBLCTag); |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
907 |
int numSizes = eblcTable.getInt(4); |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
908 |
/* The bitmapSizeTable's start at offset of 8. |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
909 |
* Each bitmapSizeTable entry is 48 bytes. |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
910 |
* The offset of ppemY in the entry is 45. |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
911 |
*/ |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
912 |
for (int i=0;i<numSizes;i++) { |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
913 |
int ppemY = eblcTable.get(8+(i*48)+45) &0xff; |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
914 |
if (ppemY == ptSize) { |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
915 |
return true; |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
916 |
} |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
917 |
} |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
918 |
return false; |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
919 |
} |
e85f91b9bb95
6656651: Windows Look and Feel LCD glyph images have some differences from native applications.
prr
parents:
2
diff
changeset
|
920 |
|
2 | 921 |
public String getFullName() { |
922 |
return fullName; |
|
923 |
} |
|
924 |
||
925 |
/* This probably won't get called but is there to support the |
|
926 |
* contract() of setStyle() defined in the superclass. |
|
927 |
*/ |
|
928 |
protected void setStyle() { |
|
929 |
setStyle(getTableBuffer(os_2Tag)); |
|
930 |
} |
|
931 |
||
932 |
/* TrueTypeFont can use the fsSelection fields of OS/2 table |
|
933 |
* to determine the style. In the unlikely case that doesn't exist, |
|
934 |
* can use macStyle in the 'head' table but simpler to |
|
935 |
* fall back to super class algorithm of looking for well known string. |
|
936 |
* A very few fonts don't specify this information, but I only |
|
937 |
* came across one: Lucida Sans Thai Typewriter Oblique in |
|
938 |
* /usr/openwin/lib/locale/th_TH/X11/fonts/TrueType/lucidai.ttf |
|
939 |
* that explicitly specified the wrong value. It says its regular. |
|
940 |
* I didn't find any fonts that were inconsistent (ie regular plus some |
|
941 |
* other value). |
|
942 |
*/ |
|
943 |
private static final int fsSelectionItalicBit = 0x00001; |
|
944 |
private static final int fsSelectionBoldBit = 0x00020; |
|
945 |
private static final int fsSelectionRegularBit = 0x00040; |
|
946 |
private void setStyle(ByteBuffer os_2Table) { |
|
947 |
/* fsSelection is unsigned short at buffer offset 62 */ |
|
948 |
if (os_2Table == null || os_2Table.capacity() < 64) { |
|
949 |
super.setStyle(); |
|
950 |
return; |
|
951 |
} |
|
952 |
int fsSelection = os_2Table.getChar(62) & 0xffff; |
|
953 |
int italic = fsSelection & fsSelectionItalicBit; |
|
954 |
int bold = fsSelection & fsSelectionBoldBit; |
|
955 |
int regular = fsSelection & fsSelectionRegularBit; |
|
956 |
// System.out.println("platname="+platName+" font="+fullName+ |
|
957 |
// " family="+familyName+ |
|
958 |
// " R="+regular+" I="+italic+" B="+bold); |
|
959 |
if (regular!=0 && ((italic|bold)!=0)) { |
|
960 |
/* This is inconsistent. Try using the font name algorithm */ |
|
961 |
super.setStyle(); |
|
962 |
return; |
|
963 |
} else if ((regular|italic|bold) == 0) { |
|
964 |
/* No style specified. Try using the font name algorithm */ |
|
965 |
super.setStyle(); |
|
966 |
return; |
|
967 |
} |
|
968 |
switch (bold|italic) { |
|
969 |
case fsSelectionItalicBit: |
|
970 |
style = Font.ITALIC; |
|
971 |
break; |
|
972 |
case fsSelectionBoldBit: |
|
973 |
if (FontManager.isSolaris && platName.endsWith("HG-GothicB.ttf")) { |
|
974 |
/* Workaround for Solaris's use of a JA font that's marked as |
|
975 |
* being designed bold, but is used as a PLAIN font. |
|
976 |
*/ |
|
977 |
style = Font.PLAIN; |
|
978 |
} else { |
|
979 |
style = Font.BOLD; |
|
980 |
} |
|
981 |
break; |
|
982 |
case fsSelectionBoldBit|fsSelectionItalicBit: |
|
983 |
style = Font.BOLD|Font.ITALIC; |
|
984 |
} |
|
985 |
} |
|
986 |
||
987 |
private float stSize, stPos, ulSize, ulPos; |
|
988 |
||
989 |
private void setStrikethroughMetrics(ByteBuffer os_2Table, int upem) { |
|
990 |
if (os_2Table == null || os_2Table.capacity() < 30 || upem < 0) { |
|
991 |
stSize = .05f; |
|
992 |
stPos = -.4f; |
|
993 |
return; |
|
994 |
} |
|
995 |
ShortBuffer sb = os_2Table.asShortBuffer(); |
|
996 |
stSize = sb.get(13) / (float)upem; |
|
997 |
stPos = -sb.get(14) / (float)upem; |
|
998 |
} |
|
999 |
||
1000 |
private void setUnderlineMetrics(ByteBuffer postTable, int upem) { |
|
1001 |
if (postTable == null || postTable.capacity() < 12 || upem < 0) { |
|
1002 |
ulSize = .05f; |
|
1003 |
ulPos = .1f; |
|
1004 |
return; |
|
1005 |
} |
|
1006 |
ShortBuffer sb = postTable.asShortBuffer(); |
|
1007 |
ulSize = sb.get(5) / (float)upem; |
|
1008 |
ulPos = -sb.get(4) / (float)upem; |
|
1009 |
} |
|
1010 |
||
1011 |
public void getStyleMetrics(float pointSize, float[] metrics, int offset) { |
|
1012 |
metrics[offset] = stPos * pointSize; |
|
1013 |
metrics[offset+1] = stSize * pointSize; |
|
1014 |
metrics[offset+2] = ulPos * pointSize; |
|
1015 |
metrics[offset+3] = ulSize * pointSize; |
|
1016 |
} |
|
1017 |
||
1018 |
private String makeString(byte[] bytes, int len, short encoding) { |
|
1019 |
||
1020 |
/* Check for fonts using encodings 2->6 is just for |
|
1021 |
* some old DBCS fonts, apparently mostly on Solaris. |
|
1022 |
* Some of these fonts encode ascii names as double-byte characters. |
|
1023 |
* ie with a leading zero byte for what properly should be a |
|
1024 |
* single byte-char. |
|
1025 |
*/ |
|
1026 |
if (encoding >=2 && encoding <= 6) { |
|
1027 |
byte[] oldbytes = bytes; |
|
1028 |
int oldlen = len; |
|
1029 |
bytes = new byte[oldlen]; |
|
1030 |
len = 0; |
|
1031 |
for (int i=0; i<oldlen; i++) { |
|
1032 |
if (oldbytes[i] != 0) { |
|
1033 |
bytes[len++] = oldbytes[i]; |
|
1034 |
} |
|
1035 |
} |
|
1036 |
} |
|
1037 |
||
1038 |
String charset; |
|
1039 |
switch (encoding) { |
|
1040 |
case 1: charset = "UTF-16"; break; // most common case first. |
|
1041 |
case 0: charset = "UTF-16"; break; // symbol uses this |
|
1042 |
case 2: charset = "SJIS"; break; |
|
1043 |
case 3: charset = "GBK"; break; |
|
1044 |
case 4: charset = "MS950"; break; |
|
1045 |
case 5: charset = "EUC_KR"; break; |
|
1046 |
case 6: charset = "Johab"; break; |
|
1047 |
default: charset = "UTF-16"; break; |
|
1048 |
} |
|
1049 |
||
1050 |
try { |
|
1051 |
return new String(bytes, 0, len, charset); |
|
1052 |
} catch (UnsupportedEncodingException e) { |
|
1053 |
if (FontManager.logging) { |
|
1054 |
FontManager.logger.warning(e + " EncodingID=" + encoding); |
|
1055 |
} |
|
1056 |
return new String(bytes, 0, len); |
|
1057 |
} catch (Throwable t) { |
|
1058 |
return null; |
|
1059 |
} |
|
1060 |
} |
|
1061 |
||
1062 |
protected void initNames() { |
|
1063 |
||
1064 |
byte[] name = new byte[256]; |
|
1065 |
ByteBuffer buffer = getTableBuffer(nameTag); |
|
1066 |
||
1067 |
if (buffer != null) { |
|
1068 |
ShortBuffer sbuffer = buffer.asShortBuffer(); |
|
1069 |
sbuffer.get(); // format - not needed. |
|
1070 |
short numRecords = sbuffer.get(); |
|
1071 |
/* The name table uses unsigned shorts. Many of these |
|
1072 |
* are known small values that fit in a short. |
|
1073 |
* The values that are sizes or offsets into the table could be |
|
1074 |
* greater than 32767, so read and store those as ints |
|
1075 |
*/ |
|
1076 |
int stringPtr = sbuffer.get() & 0xffff; |
|
1077 |
for (int i=0; i<numRecords; i++) { |
|
1078 |
short platformID = sbuffer.get(); |
|
1079 |
if (platformID != MS_PLATFORM_ID) { |
|
1080 |
sbuffer.position(sbuffer.position()+5); |
|
1081 |
continue; // skip over this record. |
|
1082 |
} |
|
1083 |
short encodingID = sbuffer.get(); |
|
1084 |
short langID = sbuffer.get(); |
|
1085 |
short nameID = sbuffer.get(); |
|
1086 |
int nameLen = ((int) sbuffer.get()) & 0xffff; |
|
1087 |
int namePtr = (((int) sbuffer.get()) & 0xffff) + stringPtr; |
|
1088 |
||
1089 |
switch (nameID) { |
|
1090 |
||
1091 |
case FAMILY_NAME_ID: |
|
1092 |
||
1093 |
if (familyName == null || langID == ENGLISH_LOCALE_ID) { |
|
1094 |
buffer.position(namePtr); |
|
1095 |
buffer.get(name, 0, nameLen); |
|
1096 |
familyName = makeString(name, nameLen, encodingID); |
|
1097 |
} |
|
1098 |
/* |
|
1099 |
for (int ii=0;ii<nameLen;ii++) { |
|
1100 |
int val = (int)name[ii]&0xff; |
|
1101 |
System.err.print(Integer.toHexString(val)+ " "); |
|
1102 |
} |
|
1103 |
System.err.println(); |
|
1104 |
System.err.println("familyName="+familyName + |
|
1105 |
" nameLen="+nameLen+ |
|
1106 |
" langID="+langID+ " eid="+encodingID + |
|
1107 |
" str len="+familyName.length()); |
|
1108 |
||
1109 |
*/ |
|
1110 |
break; |
|
1111 |
||
1112 |
case FULL_NAME_ID: |
|
1113 |
||
1114 |
if (fullName == null || langID == ENGLISH_LOCALE_ID) { |
|
1115 |
buffer.position(namePtr); |
|
1116 |
buffer.get(name, 0, nameLen); |
|
1117 |
fullName = makeString(name, nameLen, encodingID); |
|
1118 |
} |
|
1119 |
break; |
|
1120 |
||
1121 |
} |
|
1122 |
} |
|
1123 |
} |
|
1124 |
} |
|
1125 |
||
1126 |
/* Return the requested name in the requested locale, for the |
|
1127 |
* MS platform ID. If the requested locale isn't found, return US |
|
1128 |
* English, if that isn't found, return null and let the caller |
|
1129 |
* figure out how to handle that. |
|
1130 |
*/ |
|
1131 |
protected String lookupName(short findLocaleID, int findNameID) { |
|
1132 |
String foundName = null; |
|
1133 |
byte[] name = new byte[1024]; |
|
1134 |
||
1135 |
ByteBuffer buffer = getTableBuffer(nameTag); |
|
1136 |
if (buffer != null) { |
|
1137 |
ShortBuffer sbuffer = buffer.asShortBuffer(); |
|
1138 |
sbuffer.get(); // format - not needed. |
|
1139 |
short numRecords = sbuffer.get(); |
|
1140 |
||
1141 |
/* The name table uses unsigned shorts. Many of these |
|
1142 |
* are known small values that fit in a short. |
|
1143 |
* The values that are sizes or offsets into the table could be |
|
1144 |
* greater than 32767, so read and store those as ints |
|
1145 |
*/ |
|
1146 |
int stringPtr = ((int) sbuffer.get()) & 0xffff; |
|
1147 |
||
1148 |
for (int i=0; i<numRecords; i++) { |
|
1149 |
short platformID = sbuffer.get(); |
|
1150 |
if (platformID != MS_PLATFORM_ID) { |
|
1151 |
sbuffer.position(sbuffer.position()+5); |
|
1152 |
continue; // skip over this record. |
|
1153 |
} |
|
1154 |
short encodingID = sbuffer.get(); |
|
1155 |
short langID = sbuffer.get(); |
|
1156 |
short nameID = sbuffer.get(); |
|
1157 |
int nameLen = ((int) sbuffer.get()) & 0xffff; |
|
1158 |
int namePtr = (((int) sbuffer.get()) & 0xffff) + stringPtr; |
|
1159 |
if (nameID == findNameID && |
|
1160 |
((foundName == null && langID == ENGLISH_LOCALE_ID) |
|
1161 |
|| langID == findLocaleID)) { |
|
1162 |
buffer.position(namePtr); |
|
1163 |
buffer.get(name, 0, nameLen); |
|
1164 |
foundName = makeString(name, nameLen, encodingID); |
|
1165 |
if (langID == findLocaleID) { |
|
1166 |
return foundName; |
|
1167 |
} |
|
1168 |
} |
|
1169 |
} |
|
1170 |
} |
|
1171 |
return foundName; |
|
1172 |
} |
|
1173 |
||
1174 |
/** |
|
1175 |
* @return number of logical fonts. Is "1" for all but TTC files |
|
1176 |
*/ |
|
1177 |
public int getFontCount() { |
|
1178 |
return directoryCount; |
|
1179 |
} |
|
1180 |
||
1181 |
protected synchronized FontScaler getScaler() { |
|
1182 |
if (scaler == null) { |
|
1183 |
scaler = FontManager.getScaler(this, fontIndex, |
|
1184 |
supportsCJK, fileSize); |
|
1185 |
} |
|
1186 |
return scaler; |
|
1187 |
} |
|
1188 |
||
1189 |
||
1190 |
/* Postscript name is rarely requested. Don't waste cycles locating it |
|
1191 |
* as part of font creation, nor storage to hold it. Get it only on demand. |
|
1192 |
*/ |
|
1193 |
public String getPostscriptName() { |
|
1194 |
String name = lookupName(ENGLISH_LOCALE_ID, POSTSCRIPT_NAME_ID); |
|
1195 |
if (name == null) { |
|
1196 |
return fullName; |
|
1197 |
} else { |
|
1198 |
return name; |
|
1199 |
} |
|
1200 |
} |
|
1201 |
||
1202 |
public String getFontName(Locale locale) { |
|
1203 |
if (locale == null) { |
|
1204 |
return fullName; |
|
1205 |
} else { |
|
1206 |
short localeID = FontManager.getLCIDFromLocale(locale); |
|
1207 |
String name = lookupName(localeID, FULL_NAME_ID); |
|
1208 |
if (name == null) { |
|
1209 |
return fullName; |
|
1210 |
} else { |
|
1211 |
return name; |
|
1212 |
} |
|
1213 |
} |
|
1214 |
} |
|
1215 |
||
1216 |
public String getFamilyName(Locale locale) { |
|
1217 |
if (locale == null) { |
|
1218 |
return familyName; |
|
1219 |
} else { |
|
1220 |
short localeID = FontManager.getLCIDFromLocale(locale); |
|
1221 |
String name = lookupName(localeID, FAMILY_NAME_ID); |
|
1222 |
if (name == null) { |
|
1223 |
return familyName; |
|
1224 |
} else { |
|
1225 |
return name; |
|
1226 |
} |
|
1227 |
} |
|
1228 |
} |
|
1229 |
||
1230 |
public CharToGlyphMapper getMapper() { |
|
1231 |
if (mapper == null) { |
|
1232 |
mapper = new TrueTypeGlyphMapper(this); |
|
1233 |
} |
|
1234 |
return mapper; |
|
1235 |
} |
|
1236 |
||
1237 |
/* This duplicates initNames() but that has to run fast as its used |
|
1238 |
* during typical start-up and the information here is likely never |
|
1239 |
* needed. |
|
1240 |
*/ |
|
1241 |
protected void initAllNames(int requestedID, HashSet names) { |
|
1242 |
||
1243 |
byte[] name = new byte[256]; |
|
1244 |
ByteBuffer buffer = getTableBuffer(nameTag); |
|
1245 |
||
1246 |
if (buffer != null) { |
|
1247 |
ShortBuffer sbuffer = buffer.asShortBuffer(); |
|
1248 |
sbuffer.get(); // format - not needed. |
|
1249 |
short numRecords = sbuffer.get(); |
|
1250 |
||
1251 |
/* The name table uses unsigned shorts. Many of these |
|
1252 |
* are known small values that fit in a short. |
|
1253 |
* The values that are sizes or offsets into the table could be |
|
1254 |
* greater than 32767, so read and store those as ints |
|
1255 |
*/ |
|
1256 |
int stringPtr = ((int) sbuffer.get()) & 0xffff; |
|
1257 |
for (int i=0; i<numRecords; i++) { |
|
1258 |
short platformID = sbuffer.get(); |
|
1259 |
if (platformID != MS_PLATFORM_ID) { |
|
1260 |
sbuffer.position(sbuffer.position()+5); |
|
1261 |
continue; // skip over this record. |
|
1262 |
} |
|
1263 |
short encodingID = sbuffer.get(); |
|
1264 |
short langID = sbuffer.get(); |
|
1265 |
short nameID = sbuffer.get(); |
|
1266 |
int nameLen = ((int) sbuffer.get()) & 0xffff; |
|
1267 |
int namePtr = (((int) sbuffer.get()) & 0xffff) + stringPtr; |
|
1268 |
||
1269 |
if (nameID == requestedID) { |
|
1270 |
buffer.position(namePtr); |
|
1271 |
buffer.get(name, 0, nameLen); |
|
1272 |
names.add(makeString(name, nameLen, encodingID)); |
|
1273 |
} |
|
1274 |
} |
|
1275 |
} |
|
1276 |
} |
|
1277 |
||
1278 |
String[] getAllFamilyNames() { |
|
1279 |
HashSet aSet = new HashSet(); |
|
1280 |
try { |
|
1281 |
initAllNames(FAMILY_NAME_ID, aSet); |
|
1282 |
} catch (Exception e) { |
|
1283 |
/* In case of malformed font */ |
|
1284 |
} |
|
1285 |
return (String[])aSet.toArray(new String[0]); |
|
1286 |
} |
|
1287 |
||
1288 |
String[] getAllFullNames() { |
|
1289 |
HashSet aSet = new HashSet(); |
|
1290 |
try { |
|
1291 |
initAllNames(FULL_NAME_ID, aSet); |
|
1292 |
} catch (Exception e) { |
|
1293 |
/* In case of malformed font */ |
|
1294 |
} |
|
1295 |
return (String[])aSet.toArray(new String[0]); |
|
1296 |
} |
|
1297 |
||
1298 |
/* Used by the OpenType engine for mark positioning. |
|
1299 |
*/ |
|
1300 |
Point2D.Float getGlyphPoint(long pScalerContext, |
|
1301 |
int glyphCode, int ptNumber) { |
|
1302 |
try { |
|
1303 |
return getScaler().getGlyphPoint(pScalerContext, |
|
1304 |
glyphCode, ptNumber); |
|
1305 |
} catch(FontScalerException fe) { |
|
1306 |
return null; |
|
1307 |
} |
|
1308 |
} |
|
1309 |
||
1310 |
private char[] gaspTable; |
|
1311 |
||
1312 |
private char[] getGaspTable() { |
|
1313 |
||
1314 |
if (gaspTable != null) { |
|
1315 |
return gaspTable; |
|
1316 |
} |
|
1317 |
||
1318 |
ByteBuffer buffer = getTableBuffer(gaspTag); |
|
1319 |
if (buffer == null) { |
|
1320 |
return gaspTable = new char[0]; |
|
1321 |
} |
|
1322 |
||
1323 |
CharBuffer cbuffer = buffer.asCharBuffer(); |
|
1324 |
char format = cbuffer.get(); |
|
1325 |
/* format "1" has appeared for some Windows Vista fonts. |
|
1326 |
* Its presently undocumented but the existing values |
|
1327 |
* seem to be still valid so we can use it. |
|
1328 |
*/ |
|
1329 |
if (format > 1) { // unrecognised format |
|
1330 |
return gaspTable = new char[0]; |
|
1331 |
} |
|
1332 |
||
1333 |
char numRanges = cbuffer.get(); |
|
1334 |
if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check |
|
1335 |
return gaspTable = new char[0]; |
|
1336 |
} |
|
1337 |
gaspTable = new char[2*numRanges]; |
|
1338 |
cbuffer.get(gaspTable); |
|
1339 |
return gaspTable; |
|
1340 |
} |
|
1341 |
||
1342 |
/* This is to obtain info from the TT 'gasp' (grid-fitting and |
|
1343 |
* scan-conversion procedure) table which specifies three combinations: |
|
1344 |
* Hint, Smooth (greyscale), Hint and Smooth. |
|
1345 |
* In this simplified scheme we don't distinguish the latter two. We |
|
1346 |
* hint even at small sizes, so as to preserve metrics consistency. |
|
1347 |
* If the information isn't available default values are substituted. |
|
1348 |
* The more precise defaults we'd do if we distinguished the cases are: |
|
1349 |
* Bold (no other style) fonts : |
|
1350 |
* 0-8 : Smooth ( do grey) |
|
1351 |
* 9+ : Hint + smooth (gridfit + grey) |
|
1352 |
* Plain, Italic and Bold-Italic fonts : |
|
1353 |
* 0-8 : Smooth ( do grey) |
|
1354 |
* 9-17 : Hint (gridfit) |
|
1355 |
* 18+ : Hint + smooth (gridfit + grey) |
|
1356 |
* The defaults should rarely come into play as most TT fonts provide |
|
1357 |
* better defaults. |
|
1358 |
* REMIND: consider unpacking the table into an array of booleans |
|
1359 |
* for faster use. |
|
1360 |
*/ |
|
1361 |
public boolean useAAForPtSize(int ptsize) { |
|
1362 |
||
1363 |
char[] gasp = getGaspTable(); |
|
1364 |
if (gasp.length > 0) { |
|
1365 |
for (int i=0;i<gasp.length;i+=2) { |
|
1366 |
if (ptsize <= gasp[i]) { |
|
1367 |
return ((gasp[i+1] & 0x2) != 0); // bit 2 means DO_GRAY; |
|
1368 |
} |
|
1369 |
} |
|
1370 |
return true; |
|
1371 |
} |
|
1372 |
||
1373 |
if (style == Font.BOLD) { |
|
1374 |
return true; |
|
1375 |
} else { |
|
1376 |
return ptsize <= 8 || ptsize >= 18; |
|
1377 |
} |
|
1378 |
} |
|
1379 |
||
1380 |
public boolean hasSupplementaryChars() { |
|
1381 |
return ((TrueTypeGlyphMapper)getMapper()).hasSupplementaryChars(); |
|
1382 |
} |
|
1383 |
||
1384 |
public String toString() { |
|
1385 |
return "** TrueType Font: Family="+familyName+ " Name="+fullName+ |
|
1386 |
" style="+style+" fileName="+platName; |
|
1387 |
} |
|
1388 |
} |