3909N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * TrueTypeFont is not called SFntFont because it is not expected 0N/A * to handle all types that may be housed in a such a font file. 0N/A * If additional types are supported later, it may make sense to 0N/A * create an SFnt superclass. Eg to handle sfnt-housed postscript fonts. 0N/A * OpenType fonts are handled by this class, and possibly should be 0N/A * represented by a subclass. 0N/A * An instance stores some information from the font file to faciliate 0N/A * faster access. File size, the table directory and the names of the font 0N/A * are the most important of these. It amounts to approx 400 bytes 0N/A * for a typical font. Systems with mutiple locales sometimes have up to 400 0N/A * font files, and an app which loads all font files would need around 0N/A * 160Kbytes. So storing any more info than this would be expensive. 0N/A /* -- Tags for required TrueType tables */ 0N/A public static final int cmapTag =
0x636D6170;
// 'cmap' 0N/A public static final int glyfTag =
0x676C7966;
// 'glyf' 0N/A public static final int headTag =
0x68656164;
// 'head' 0N/A public static final int hheaTag =
0x68686561;
// 'hhea' 0N/A public static final int hmtxTag =
0x686D7478;
// 'hmtx' 0N/A public static final int locaTag =
0x6C6F6361;
// 'loca' 0N/A public static final int maxpTag =
0x6D617870;
// 'maxp' 0N/A public static final int nameTag =
0x6E616D65;
// 'name' 0N/A public static final int postTag =
0x706F7374;
// 'post' 0N/A public static final int os_2Tag =
0x4F532F32;
// 'OS/2' 0N/A /* -- Tags for opentype related tables */ 0N/A public static final int GDEFTag =
0x47444546;
// 'GDEF' 0N/A public static final int GPOSTag =
0x47504F53;
// 'GPOS' 0N/A public static final int GSUBTag =
0x47535542;
// 'GSUB' 0N/A public static final int mortTag =
0x6D6F7274;
// 'mort' 0N/A /* -- Tags for non-standard tables */ 0N/A public static final int fdscTag =
0x66647363;
// 'fdsc' - gxFont descriptor 0N/A public static final int fvarTag =
0x66766172;
// 'fvar' - gxFont variations 0N/A public static final int featTag =
0x66656174;
// 'feat' - layout features 0N/A public static final int EBLCTag =
0x45424C43;
// 'EBLC' - embedded bitmaps 0N/A public static final int ttcfTag =
0x74746366;
// 'ttcf' - TTC file 0N/A public static final int v1ttTag =
0x00010000;
// 'v1tt' - Version 1 TT font 0N/A public static final int trueTag =
0x74727565;
// 'true' - Version 2 TT font 752N/A public static final int ottoTag =
0x4f54544f;
// 'otto' - OpenType font 0N/A /* -- ID's used in the 'name' table */ 0N/A /* MS locale id for US English is the "default" */ 0N/A // public static final int STYLE_WEIGHT_ID = 2; // currently unused. 1686N/A private static final short US_LCID =
0x0409;
// US English - default 0N/A /* There is a pool which limits the number of fd's that are in 0N/A * use. Normally fd's are closed as they are replaced in the pool. 0N/A * But if an instance of this class becomes unreferenced, then there 0N/A * needs to be a way to close the fd. A finalize() method could do this, 0N/A * but using the Disposer class will ensure its called in a more timely 0N/A * manner. This is not something which should be relied upon to free 0N/A * fd's - its a safeguard. 0N/A /* > 0 only if this font is a part of a collection */ 0N/A /* Number of fonts in this collection. ==1 if not a collection */ 0N/A /* offset in file of table directory for this font */ 0N/A// protected byte []gposTable = null; 0N/A// protected byte []gdefTable = null; 0N/A// protected byte []gsubTable = null; 0N/A// protected byte []mortTable = null; 0N/A// protected boolean hintsTabledChecked = false; 0N/A// protected boolean containsHintsTable = false; 0N/A /* These fields are set from os/2 table info. */ 1284N/A /* These are for faster access to the name of the font as 1284N/A * typically exposed via API to applications. 0N/A * - does basic verification of the file 0N/A * - reads the header table for this font (within a collection) 0N/A * - reads the names (full, family). 0N/A * - determines the style of the font. 0N/A * - initializes the CMAP 0N/A * @throws FontFormatException - if the font can't be opened 0N/A * or fails verification, or there's no usable cmap 0N/A /* Enable natives just for fonts picked up from the platform that 0N/A * may have external bitmaps on Solaris. Could do this just for 0N/A * the fonts that are specified in font configuration files which 0N/A * would lighten the burden (think about that). 0N/A * The EBLCTag is used to skip natives for fonts that contain embedded 0N/A * bitmaps as there's no need to use X11 for those fonts. 0N/A * Skip all the latin fonts as they don't need this treatment. 0N/A * Further refine this to fonts that are natively accessible (ie 0N/A * as PCF bitmap fonts on the X11 font path). 0N/A * This method is called when creating the first strike for this font. 0N/A return false;
/* useNatives is false */ 0N/A /* Don't do do this for Latin fonts */ 0N/A /* If reach here we have an non-latin font that has 0N/A * external bitmaps and we successfully created it. 0N/A /* This is intended to be called, and the returned value used, 0N/A * from within a block synchronized on this font object. 0N/A * ie the channel returned may be nulled out at any time by "close()" 0N/A * unless the caller holds a lock. 0N/A * Deadlock warning: FontManager.addToPool(..) acquires a global lock, 0N/A * which means nested locks may be in effect. 0N/A /* NIO I/O is interruptible, recurse to retry operation. 0N/A * The call to channel.size() above can throw this exception. 0N/A * Clear interrupts before recursing in case NIO didn't. 0N/A * Note that close() sets disposerRecord.channel to null. 0N/A synchronized (
this) {
0N/A /* Since the caller ensures that offset is < fileSize 0N/A * this condition suggests that fileSize is now 0N/A * different than the value we originally provided 0N/A * to native when the scaler was created. 0N/A * Also fileSize is updated every time we 0N/A * open() the file here, but in native the value 0N/A * isn't updated. If the file has changed whilst we 0N/A * are executing we want to bail, not spin. 0N/A // We could still flip() the buffer here because 0N/A // it's possible that we did read some data in 0N/A // an earlier loop, and we probably should 0N/A // return that to the caller. Although if 0N/A // the caller expected 8K of data and we return 0N/A // only a few bytes then maybe it's better instead to 0N/A // set bread = -1 to indicate failure. 0N/A // The following is therefore using arbitrary values 0N/A // but is meant to allow cases where enough 0N/A // data was read to probably continue. 0N/A /* NIO I/O is interruptible, recurse to retry operation. 0N/A * Clear interrupts before recursing in case NIO didn't. 0N/A /* If we did not read any bytes at all and the exception is 0N/A * not a recoverable one (ie is not ClosedChannelException) then 0N/A * we should indicate that there is no point in re-trying. 0N/A * Other than an attempt to read past the end of the file it 0N/A * seems unlikely this would occur as problems opening the 0N/A * file are handled as a FontFormatException. 0N/A synchronized (
this) {
0N/A /* NIO I/O is interruptible, recurse to retry operation. 0N/A * Clear interrupts before recursing in case NIO didn't. 0N/A /* This is used by native code which can't allocate a direct byte 0N/A * buffer because of bug 4845371. It, and references to it in native 0N/A * code in scalerMethods.c can be removed once that bug is fixed. 0N/A * 4845371 is now fixed but we'll keep this around as it doesn't cost 0N/A /* sizes, in bytes, of TT/TTC header records */ 0N/A /* Now have the offset of this TT font (possibly within a TTC) 0N/A * representing the number of tables in the table directory. 0N/A * The table directory begins at 12 bytes after the header. 0N/A * Each table entry is 16 bytes long (4 32-bit ints) 0N/A /* The os2_Table is needed to gather some info, but we don't 0N/A * want to keep it around (as a field) so obtain it once and 0N/A * pass it to the code that needs it. 0N/A /* The array index corresponds to a bit offset in the TrueType 0N/A * font's OS/2 compatibility table's code page ranges fields. 0N/A * These are two 32 bit unsigned int fields at offsets 78 and 82. 0N/A * We are only interested in determining if the font supports 0N/A * the windows encodings we expect as the default encoding in 0N/A * supported locales, so we only map the first of these fields. 0N/A "cp1252",
/* 0:Latin 1 */ 0N/A "cp1250",
/* 1:Latin 2 */ 0N/A "cp1251",
/* 2:Cyrillic */ 0N/A "cp1253",
/* 3:Greek */ 0N/A "cp1255",
/* 5:Hebrew */ 0N/A "cp1256",
/* 6:Arabic */ 0N/A "cp1257",
/* 7:Windows Baltic */ 0N/A "",
/* 8:reserved for alternate ANSI */ 0N/A "",
/* 9:reserved for alternate ANSI */ 0N/A "",
/* 10:reserved for alternate ANSI */ 0N/A "",
/* 11:reserved for alternate ANSI */ 0N/A "",
/* 12:reserved for alternate ANSI */ 0N/A "",
/* 13:reserved for alternate ANSI */ 0N/A "",
/* 14:reserved for alternate ANSI */ 0N/A "",
/* 15:reserved for alternate ANSI */ 0N/A "ms874",
/* 16:Thai */ 0N/A "gbk",
/* 18:PRC GBK Cp950 */ 0N/A "ms949",
/* 19:Korean Extended Wansung */ 0N/A "ms950",
/* 20:Chinese (Taiwan, Hongkong, Macau) */ 0N/A "ms1361",
/* 21:Korean Johab */ 0N/A /* This maps two letter language codes to a Windows code page. 0N/A * Note that eg Cp1252 (the first subarray) is not exactly the same as 0N/A * Latin-1 since Windows code pages are do not necessarily correspond. 0N/A * There are two codepages for zh and ko so if a font supports 0N/A * only one of these ranges then we need to distinguish based on 0N/A * country. So far this only seems to matter for zh. 0N/A * REMIND: Unicode locales such as Hindi do not have a code page so 0N/A * this whole mechansim needs to be revised to map languages to 0N/A * the Unicode ranges either when this fails, or as an additional 0N/A * validating test. Basing it on Unicode ranges should get us away 0N/A * from needing to map to this small and incomplete set of Windows 0N/A * code pages which looks odd on non-Windows platforms. 0N/A {
"en",
"ca",
"da",
"de",
"es",
"fi",
"fr",
"is",
"it",
0N/A "nl",
"no",
"pt",
"sq",
"sv", },
0N/A {
"cs",
"cz",
"et",
"hr",
"hu",
"nr",
"pl",
"ro",
"sk",
0N/A "sl",
"sq",
"sr", },
0N/A {
"bg",
"mk",
"ru",
"sh",
"uk" },
0N/A {
"et",
"lt",
"lv" },
0N/A {
"zh_HK",
"zh_TW", },
0N/A /* Theoretically, reserved bits must not be set, include symbol bits */ 0N/A /* java_props_md.c has a couple of special cases 0N/A * if language packs are installed. In these encodings the 0N/A * fontconfig files pick up different fonts : 0N/A * SimSun-18030 and MingLiU_HKSCS. Since these fonts will 0N/A * indicate they support the base encoding, we need to rewrite 0N/A /* required info is at offsets 78 and 82 */ 0N/A /* This test is too stringent for Arial on Solaris (and perhaps 0N/A * other fonts). Arial has at least one reserved bit set for an 0N/A// if (((range1 & reserved_bits1) | (range2 & reserved_bits2)) != 0) { 0N/A /* Use info in the os_2Table to test CJK support */ 0N/A /* required info is in ulong at offset 46 */ 0N/A /* Any of these bits set in the 32-63 range indicate a font with 0N/A * support for a CJK range. We aren't looking at some other bits 0N/A * in the 64-69 range such as half width forms as its unlikely a font 0N/A * would include those and none of these. 0N/A /* This should be generalised, but for now just need to know if 0N/A * Hiragana or Katakana ranges are supported by the font. 0N/A * In the 4 longs representing unicode ranges supported 0N/A * bits 49 & 50 indicate hiragana and katakana 0N/A * This is bits 17 & 18 in the 2nd ulong. If either is supported 0N/A * we presume this is a JA font. 0N/A synchronized (
this) {
0N/A /* NIO I/O is interruptible, recurse to retry operation. 0N/A * Clear interrupts before recursing in case NIO didn't. 0N/A /* NB: is it better to move declaration to Font2D? */ 301N/A /* Used to determine if this size has embedded bitmaps, which 301N/A * for CJK fonts should be used in preference to LCD glyphs. 301N/A /* The bitmapSizeTable's start at offset of 8. 301N/A * Each bitmapSizeTable entry is 48 bytes. 301N/A * The offset of ppemY in the entry is 45. 0N/A /* This probably won't get called but is there to support the 0N/A * contract() of setStyle() defined in the superclass. 0N/A /* TrueTypeFont can use the fsSelection fields of OS/2 table 0N/A * to determine the style. In the unlikely case that doesn't exist, 0N/A * can use macStyle in the 'head' table but simpler to 0N/A * fall back to super class algorithm of looking for well known string. 0N/A * A very few fonts don't specify this information, but I only 0N/A * came across one: Lucida Sans Thai Typewriter Oblique in 0N/A * that explicitly specified the wrong value. It says its regular. 0N/A * I didn't find any fonts that were inconsistent (ie regular plus some 0N/A /* fsSelection is unsigned short at buffer offset 62 */ 0N/A// System.out.println("platname="+platName+" font="+fullName+ 0N/A// " family="+familyName+ 0N/A// " R="+regular+" I="+italic+" B="+bold); 0N/A /* This is inconsistent. Try using the font name algorithm */ 0N/A /* No style specified. Try using the font name algorithm */ 0N/A /* Workaround for Solaris's use of a JA font that's marked as 0N/A * being designed bold, but is used as a PLAIN font. 0N/A /* Check for fonts using encodings 2->6 is just for 0N/A * some old DBCS fonts, apparently mostly on Solaris. 0N/A * Some of these fonts encode ascii names as double-byte characters. 0N/A * ie with a leading zero byte for what properly should be a 0N/A case 1:
charset =
"UTF-16";
break;
// most common case first. 0N/A case 0:
charset =
"UTF-16";
break;
// symbol uses this 0N/A /* The name table uses unsigned shorts. Many of these 0N/A * are known small values that fit in a short. 0N/A * The values that are sizes or offsets into the table could be 0N/A * greater than 32767, so read and store those as ints 0N/A continue;
// skip over this record. 0N/A for (int ii=0;ii<nameLen;ii++) { 0N/A int val = (int)name[ii]&0xff; 0N/A System.err.print(Integer.toHexString(val)+ " "); 0N/A System.err.println(); 0N/A System.err.println("familyName="+familyName + 0N/A " nameLen="+nameLen+ 0N/A " langID="+langID+ " eid="+encodingID + 0N/A " str len="+familyName.length()); 0N/A /* Return the requested name in the requested locale, for the 0N/A * MS platform ID. If the requested locale isn't found, return US 0N/A * English, if that isn't found, return null and let the caller 0N/A * figure out how to handle that. 0N/A /* The name table uses unsigned shorts. Many of these 0N/A * are known small values that fit in a short. 0N/A * The values that are sizes or offsets into the table could be 0N/A * greater than 32767, so read and store those as ints 0N/A continue;
// skip over this record. 0N/A * @return number of logical fonts. Is "1" for all but TTC files 0N/A /* Postscript name is rarely requested. Don't waste cycles locating it 0N/A * as part of font creation, nor storage to hold it. Get it only on demand. 1686N/A // Return a Microsoft LCID from the given Locale. 1686N/A // Used when getting localized font data. 1686N/A // the following statements are derived from the langIDMap 1686N/A // $1~/0x04/ { print " addLCIDMapEntry(map, " substr($3, 0, 3) "\", (short) " substr($1, 0, 6) ");" ; next } 1686N/A // $3~/,/ { print " addLCIDMapEntry(map, " $3 " (short) " substr($1, 0, 6) ");" ; next } 1686N/A // { print " addLCIDMapEntry(map, " $3 ", (short) " substr($1, 0, 6) ");" ; next } 1686N/A // The lines of this script: 1686N/A // - eliminate questionable locales 1686N/A // - eliminate language-only locales 1686N/A // - eliminate the default LCID value 1686N/A // - eliminate a few other unneeded LCID values 1686N/A // - print language-only locale entries for x04* LCID values 1686N/A // (apparently Microsoft doesn't use language-only LCID values - 1686N/A // - print complete entries for all other LCID values 1686N/A // awk -f awk-script langIDMap > statements 1686N/A // optimize for common case 0N/A /* This duplicates initNames() but that has to run fast as its used 0N/A * during typical start-up and the information here is likely never 0N/A /* The name table uses unsigned shorts. Many of these 0N/A * are known small values that fit in a short. 0N/A * The values that are sizes or offsets into the table could be 0N/A * greater than 32767, so read and store those as ints 0N/A continue;
// skip over this record. 0N/A /* In case of malformed font */ 0N/A /* In case of malformed font */ 0N/A /* Used by the OpenType engine for mark positioning. 0N/A /* format "1" has appeared for some Windows Vista fonts. 0N/A * Its presently undocumented but the existing values 0N/A * seem to be still valid so we can use it. 0N/A /* This is to obtain info from the TT 'gasp' (grid-fitting and 0N/A * scan-conversion procedure) table which specifies three combinations: 0N/A * Hint, Smooth (greyscale), Hint and Smooth. 0N/A * In this simplified scheme we don't distinguish the latter two. We 0N/A * hint even at small sizes, so as to preserve metrics consistency. 0N/A * If the information isn't available default values are substituted. 0N/A * The more precise defaults we'd do if we distinguished the cases are: 0N/A * Bold (no other style) fonts : 0N/A * 0-8 : Smooth ( do grey) 0N/A * 9+ : Hint + smooth (gridfit + grey) 0N/A * Plain, Italic and Bold-Italic fonts : 0N/A * 0-8 : Smooth ( do grey) 0N/A * 9-17 : Hint (gridfit) 0N/A * 18+ : Hint + smooth (gridfit + grey) 0N/A * The defaults should rarely come into play as most TT fonts provide 0N/A * REMIND: consider unpacking the table into an array of booleans 0N/A return ((
gasp[i+
1] &
0x2) !=
0);
// bit 2 means DO_GRAY;