--- old/src/macosx/classes/sun/font/CCharToGlyphMapper.java 2013-05-30 13:23:19.000000000 -0700 +++ new/src/macosx/classes/sun/font/CCharToGlyphMapper.java 2013-05-30 13:23:18.000000000 -0700 @@ -130,6 +130,15 @@ } public synchronized int charToGlyph(int unicode) { + if (unicode >= 0x10000) { + int[] glyphs = new int[2]; + char[] surrogates = new char[2]; + int base = unicode - 0x10000; + surrogates[0] = (char)((base >>> 10) + HI_SURROGATE_START); + surrogates[1] = (char)((base % 0x400) + LO_SURROGATE_START); + charsToGlyphs(2, surrogates, glyphs); + return glyphs[0]; + } else return charToGlyph((char)unicode); } @@ -138,9 +147,9 @@ } public synchronized void charsToGlyphs(int count, int[] unicodes, int[] glyphs) { - final char[] unicodeChars = new char[count]; - for (int i = 0; i < count; i++) unicodeChars[i] = (char)unicodes[i]; - cache.get(count, unicodeChars, glyphs); + for (int i = 0; i < count; i++) { + glyphs[i] = charToGlyph(unicodes[i]); + }; } // This mapper returns either the glyph code, or if the character can be @@ -166,7 +175,7 @@ firstLayerCache[1] = 1; } - public int get(final char index) { + public synchronized int get(final int index) { if (index < FIRST_LAYER_SIZE) { // catch common glyphcodes return firstLayerCache[index]; @@ -179,12 +188,12 @@ } if (generalCache == null) return 0; - final Integer value = generalCache.get(new Integer(index)); + final Integer value = generalCache.get(index); if (value == null) return 0; return value.intValue(); } - public void put(final char index, final int value) { + public synchronized void put(final int index, final int value) { if (index < FIRST_LAYER_SIZE) { // catch common glyphcodes firstLayerCache[index] = value; @@ -204,7 +213,7 @@ generalCache = new HashMap(); } - generalCache.put(new Integer(index), new Integer(value)); + generalCache.put(index, value); } private class SparseBitShiftingTwoLayerArray { @@ -220,14 +229,14 @@ this.secondLayerLength = size >> shift; } - public int get(final char index) { + public int get(final int index) { final int firstIndex = index >> shift; final int[] firstLayerRow = cache[firstIndex]; if (firstLayerRow == null) return 0; return firstLayerRow[index - (firstIndex * (1 << shift))]; } - public void put(final char index, final int value) { + public void put(final int index, final int value) { final int firstIndex = index >> shift; int[] firstLayerRow = cache[firstIndex]; if (firstLayerRow == null) { @@ -237,77 +246,81 @@ } } - public void get(int count, char[] indicies, int[] values){ + public synchronized void get(int count, char[] indicies, int[] values) + { + // "missed" is the count of 'char' that are not mapped. + // Surrogates count for 2. + // unmappedChars is the unique list of these chars. + // unmappedCharIndices is the location in the original array int missed = 0; - for(int i = 0; i < count; i++){ - char code = indicies[i]; + char[] unmappedChars = null; + int [] unmappedCharIndices = null; + + for (int i = 0; i < count; i++){ + int code = indicies[i]; + if (code >= HI_SURROGATE_START && + code <= HI_SURROGATE_END && i < count - 1) + { + char low = indicies[i + 1]; + if (low >= LO_SURROGATE_START && low <= LO_SURROGATE_END) { + code = (code - HI_SURROGATE_START) * 0x400 + + low - LO_SURROGATE_START + 0x10000; + } + } final int value = get(code); - if(value != 0){ + if (value != 0 && value != -1) { values[i] = value; - }else{ - // zero this element out, because the caller does not - // promise to keep it clean + if (code >= 0x10000) { + values[i+1] = INVISIBLE_GLYPH_ID; + i++; + } + } else { values[i] = 0; + put(code, -1); + if (unmappedChars == null) { + // This is likely to be longer than we need, + // but is the simplest and cheapest option. + unmappedChars = new char[indicies.length]; + unmappedCharIndices = new int[indicies.length]; + } + unmappedChars[missed] = indicies[i]; + unmappedCharIndices[missed] = i; + if (code >= 0x10000) { // was a surrogate pair + unmappedChars[++missed] = indicies[++i]; + } missed++; } } - if (missed == 0) return; // horray! everything is already cached! - - final char[] filteredCodes = new char[missed]; // all index codes requested (partially filled) - final int[] filteredIndicies = new int[missed]; // local indicies into filteredCodes array (totally filled) - - // scan, mark, and store the index codes again to send into native - int j = 0; - int dupes = 0; - for (int i = 0; i < count; i++){ - if (values[i] != 0L) continue; // already filled - - final char code = indicies[i]; - - // we have already promised to fill this code - this is a dupe - if (get(code) == -1){ - filteredIndicies[j] = -1; - dupes++; - j++; - continue; - } - - // this is a code we have not obtained before - // mark this one as "promise to get" in the global cache with a -1 - final int k = j - dupes; - filteredCodes[k] = code; - put(code, -1); - filteredIndicies[j] = k; - j++; + if (missed == 0) { + return; } - final int filteredRunLen = j - dupes; - final int[] filteredValues = new int[filteredRunLen]; - - // bulk call to fill in the distinct values - nativeCharsToGlyphs(fFont.getNativeFontPtr(), filteredRunLen, filteredCodes, filteredValues); + final int[] glyphCodes = new int[missed]; - // scan the requested list, and fill in values from our - // distinct code list which has been filled from "getDistinct" - j = 0; - for (int i = 0; i < count; i++){ - if (values[i] != 0L && values[i] != -1L) continue; // already placed - - final int k = filteredIndicies[j]; // index into filteredImages array - final char code = indicies[i]; - if(k == -1L){ - // we should have already filled the cache with this value - values[i] = get(code); - }else{ - // fill the particular code request, and store in the cache - final int ptr = filteredValues[k]; - values[i] = ptr; - put(code, ptr); + // bulk call to fill in the unmapped code points. + nativeCharsToGlyphs(fFont.getNativeFontPtr(), + missed, unmappedChars, glyphCodes); + + for (int m = 0; m < missed; m++){ + int i = unmappedCharIndices[m]; + int code = unmappedChars[m]; + if (code >= HI_SURROGATE_START && + code <= HI_SURROGATE_END && m < missed - 1) + { + char low = indicies[m + 1]; + if (low >= LO_SURROGATE_START && low <= LO_SURROGATE_END) { + code = (code - HI_SURROGATE_START) * 0x400 + + low - LO_SURROGATE_START + 0x10000; + } + } + values[i] = glyphCodes[m]; + put(code, values[i]); + if (code >= 0x10000) { + m++; + values[i + 1] = INVISIBLE_GLYPH_ID; } - - j++; } } } --- /dev/null 2013-05-30 13:23:20.000000000 -0700 +++ new/test/java/awt/FontClass/SurrogateTest/SuppCharTest.java 2013-05-30 13:23:20.000000000 -0700 @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2013, 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 8015556 + * @summary Surrogate pairs do not render properly on MacOS X. + */ + +import java.util.Locale; +import java.awt.*; +import java.awt.font.*; +import javax.swing.*; + +public class SuppCharTest { + + static String str = "ABC\uD840\uDC01\uD840\uDC00AB"; + static String EXTB_FONT = "MingLiU-ExtB"; + + public static void main(String args[]) throws Exception { + + final Font font = new Font(EXTB_FONT, Font.PLAIN, 36); + if (!EXTB_FONT.equalsIgnoreCase(font.getFamily(Locale.ENGLISH))) { + return; + } + + SwingUtilities.invokeLater(new Runnable(){ + @Override + public void run(){ + JFrame f = new JFrame("Test Supplementary Char Support"); + Component c = new SuppCharComp(font, str); + f.add("Center", c); + JButton b = new JButton(str); + b.setFont(font); + f.add("South", b); + f.pack(); + f.setVisible(true); + } + }); + + /* If a supplementary character was found, 'invisible glyphs' + * with value 65535 will be inserted in the place of the 2nd (low) + * char index. So we are looking here to make sure such substitutions + * took place. + */ + FontRenderContext frc = new FontRenderContext(null, false, false); + GlyphVector gv = font.createGlyphVector(frc, str); + int numGlyphs = gv.getNumGlyphs(); + int[] codes = gv.getGlyphCodes(0, numGlyphs, null); + boolean foundInvisibleGlyph = false; + for (int i=0; i