6232267: TrueType Fonts which have only Apple platform names cannot be loaded
authorprr
Wed, 18 Jan 2017 17:36:24 -0800
changeset 43310 0746906c86f6
parent 43309 5df4efb223bd
child 43311 453e9d97f99a
6232267: TrueType Fonts which have only Apple platform names cannot be loaded Reviewed-by: psadhukhan
jdk/src/java.desktop/share/classes/sun/font/TrueTypeFont.java
jdk/test/java/awt/FontClass/AppleFontNameTest.java
--- a/jdk/src/java.desktop/share/classes/sun/font/TrueTypeFont.java	Wed Jan 18 17:22:03 2017 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/font/TrueTypeFont.java	Wed Jan 18 17:36:24 2017 -0800
@@ -99,6 +99,10 @@
     public static final int ottoTag = 0x4f54544f; // 'otto' - OpenType font
 
     /* -- ID's used in the 'name' table */
+    public static final int MAC_PLATFORM_ID = 1;
+    public static final int MACROMAN_SPECIFIC_ID = 0;
+    public static final int MACROMAN_ENGLISH_LANG = 0;
+
     public static final int MS_PLATFORM_ID = 3;
     /* MS locale id for US English is the "default" */
     public static final short ENGLISH_LOCALE_ID = 0x0409; // 1033 decimal
@@ -1108,7 +1112,12 @@
         metrics[offset+3] = ulSize * pointSize;
     }
 
-    private String makeString(byte[] bytes, int len, short encoding) {
+    private String makeString(byte[] bytes, int len,
+                             short platformID, short encoding) {
+
+        if (platformID == MAC_PLATFORM_ID) {
+            encoding = -1; // hack so we can re-use the code below.
+        }
 
         /* Check for fonts using encodings 2->6 is just for
          * some old DBCS fonts, apparently mostly on Solaris.
@@ -1130,6 +1139,7 @@
 
         String charset;
         switch (encoding) {
+            case -1: charset = "US-ASCII";break;
             case 1:  charset = "UTF-16";  break; // most common case first.
             case 0:  charset = "UTF-16";  break; // symbol uses this
             case 2:  charset = "SJIS";    break;
@@ -1175,7 +1185,8 @@
 
             for (int i=0; i<numRecords; i++) {
                 short platformID = sbuffer.get();
-                if (platformID != MS_PLATFORM_ID) {
+                if (platformID != MS_PLATFORM_ID &&
+                    platformID != MAC_PLATFORM_ID) {
                     sbuffer.position(sbuffer.position()+5);
                     continue; // skip over this record.
                 }
@@ -1185,6 +1196,14 @@
                 int nameLen    = ((int) sbuffer.get()) & 0xffff;
                 int namePtr    = (((int) sbuffer.get()) & 0xffff) + stringPtr;
                 String tmpName = null;
+
+                // only want MacRoman encoding and English name on Mac.
+                if ((platformID == MAC_PLATFORM_ID) &&
+                    (encodingID != MACROMAN_SPECIFIC_ID ||
+                     langID != MACROMAN_ENGLISH_LANG)) {
+                    continue;
+                }
+
                 switch (nameID) {
 
                 case FAMILY_NAME_ID:
@@ -1196,7 +1215,7 @@
                     {
                         buffer.position(namePtr);
                         buffer.get(name, 0, nameLen);
-                        tmpName = makeString(name, nameLen, encodingID);
+                        tmpName = makeString(name, nameLen, platformID, encodingID);
                         if (familyName == null || langID == ENGLISH_LOCALE_ID){
                             familyName = tmpName;
                         }
@@ -1229,7 +1248,7 @@
                     {
                         buffer.position(namePtr);
                         buffer.get(name, 0, nameLen);
-                        tmpName = makeString(name, nameLen, encodingID);
+                        tmpName = makeString(name, nameLen, platformID, encodingID);
 
                         if (fullName == null || langID == ENGLISH_LOCALE_ID) {
                             fullName = tmpName;
@@ -1290,7 +1309,7 @@
                      || langID == findLocaleID)) {
                     buffer.position(namePtr);
                     buffer.get(name, 0, nameLen);
-                    foundName = makeString(name, nameLen, encodingID);
+                    foundName = makeString(name, nameLen, platformID, encodingID);
                     if (langID == findLocaleID) {
                         return foundName;
                     }
@@ -1627,7 +1646,7 @@
                 if (nameID == requestedID) {
                     buffer.position(namePtr);
                     buffer.get(name, 0, nameLen);
-                    names.add(makeString(name, nameLen, encodingID));
+                    names.add(makeString(name, nameLen, platformID, encodingID));
                 }
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/FontClass/AppleFontNameTest.java	Wed Jan 18 17:36:24 2017 -0800
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 6232267
+ * @requires (os.family == "mac")
+ * @summary Test reading a font with only Apple names.
+ * @run main AppleFontNameTest
+ */
+
+import java.awt.Font;
+import java.io.File;
+
+/*
+ * This test picks on a font that is known to have only Apple names.
+ * So it runs only on MacOS and works only if the font is where it
+ * has historically been. Anything else is a silent pass.`
+ */
+
+public class AppleFontNameTest {
+
+    static String file = "/System/Library/Fonts/Menlo.ttc";
+
+    public static void main(String[] args) throws Exception {
+        String os = System.getProperty("os.name");
+        if (!(os.startsWith("Mac"))) {
+            return;
+        }
+        File fontFile = new File(file);
+        if (!fontFile.exists()) {
+            return;
+        }
+        Font[] fonts = Font.createFonts(new File(file));
+        System.out.println("createFont from file returned " + fonts);
+
+        if (fonts == null || fonts.length == 0) {
+            throw new RuntimeException("No fonts");
+        }
+        for (Font f : fonts) {
+            System.out.println(f);
+            if (!f.getFamily().equals("Menlo"))
+               throw new RuntimeException("Expected Menlo, got " + f.getFamily());
+        }
+    }
+}