123 124 static final short ShiftJISEncoding = 2; 125 static final short GBKEncoding = 3; 126 static final short Big5Encoding = 4; 127 static final short WansungEncoding = 5; 128 static final short JohabEncoding = 6; 129 static final short MSUnicodeSurrogateEncoding = 10; 130 131 static final char noSuchChar = (char)0xfffd; 132 static final int SHORTMASK = 0x0000ffff; 133 static final int INTMASK = 0xffffffff; 134 135 static final char[][] converterMaps = new char[7][]; 136 137 /* 138 * Unicode->other encoding translation array. A pre-computed look up 139 * which can be shared across all fonts using that encoding. 140 * Using this saves running character coverters repeatedly. 141 */ 142 char[] xlat; 143 144 static CMap initialize(TrueTypeFont font) { 145 146 CMap cmap = null; 147 148 int offset, platformID, encodingID=-1; 149 150 int three0=0, three1=0, three2=0, three3=0, three4=0, three5=0, 151 three6=0, three10=0; 152 boolean threeStar = false; 153 154 ByteBuffer cmapBuffer = font.getTableBuffer(TrueTypeFont.cmapTag); 155 int cmapTableOffset = font.getTableSize(TrueTypeFont.cmapTag); 156 short numberSubTables = cmapBuffer.getShort(2); 157 158 /* locate the offsets of all 3,* (ie Microsoft platform) encodings */ 159 for (int i=0; i<numberSubTables; i++) { 160 cmapBuffer.position(i * 8 + 4); 161 platformID = cmapBuffer.getShort(); 162 if (platformID == 3) { 163 threeStar = true; 164 encodingID = cmapBuffer.getShort(); 165 offset = cmapBuffer.getInt(); 166 switch (encodingID) { 167 case 0: three0 = offset; break; // MS Symbol encoding 168 case 1: three1 = offset; break; // MS Unicode cmap 169 case 2: three2 = offset; break; // ShiftJIS cmap. 170 case 3: three3 = offset; break; // GBK cmap 171 case 4: three4 = offset; break; // Big 5 cmap 172 case 5: three5 = offset; break; // Wansung 173 case 6: three6 = offset; break; // Johab 174 case 10: three10 = offset; break; // MS Unicode surrogates 175 } 176 } 177 } 178 179 /* This defines the preference order for cmap subtables */ 180 if (threeStar) { 181 if (three10 != 0) { 182 cmap = createCMap(cmapBuffer, three10, null); 183 } 184 else if (three0 != 0) { 185 /* The special case treatment of these fonts leads to 186 * anomalies where a user can view "wingdings" and "wingdings2" 187 * and the latter shows all its code points in the unicode 188 * private use area at 0xF000->0XF0FF and the former shows 189 * a scattered subset of its glyphs that are known mappings to 190 * unicode code points. 191 * The primary purpose of these mappings was to facilitate 192 * display of symbol chars etc in composite fonts, however 193 * this is not needed as all these code points are covered 194 * by Lucida Sans Regular. 195 * Commenting this out reduces the role of these two files 245 else { 246 cmap = createCMap(cmapBuffer, three4, 247 getConverterMap(Big5Encoding)); 248 } 249 } 250 else if (three5 != 0) { 251 cmap = createCMap(cmapBuffer, three5, 252 getConverterMap(WansungEncoding)); 253 } 254 else if (three6 != 0) { 255 cmap = createCMap(cmapBuffer, three6, 256 getConverterMap(JohabEncoding)); 257 } 258 } else { 259 /* No 3,* subtable was found. Just use whatever is the first 260 * table listed. Not very useful but maybe better than 261 * rejecting the font entirely? 262 */ 263 cmap = createCMap(cmapBuffer, cmapBuffer.getInt(8), null); 264 } 265 return cmap; 266 } 267 268 /* speed up the converting by setting the range for double 269 * byte characters; 270 */ 271 static char[] getConverter(short encodingID) { 272 int dBegin = 0x8000; 273 int dEnd = 0xffff; 274 String encoding; 275 276 switch (encodingID) { 277 case ShiftJISEncoding: 278 dBegin = 0x8140; 279 dEnd = 0xfcfc; 280 encoding = "SJIS"; 281 break; 282 case GBKEncoding: 283 dBegin = 0x8140; 284 dEnd = 0xfea0; 407 subtableLength = buffer.getInt(offset+4) & INTMASK; 408 } 409 if (offset+subtableLength > buffer.capacity()) { 410 if (FontUtilities.isLogging()) { 411 FontUtilities.getLogger().warning("Cmap subtable overflows buffer."); 412 } 413 } 414 switch (subtableFormat) { 415 case 0: return new CMapFormat0(buffer, offset); 416 case 2: return new CMapFormat2(buffer, offset, xlat); 417 case 4: return new CMapFormat4(buffer, offset, xlat); 418 case 6: return new CMapFormat6(buffer, offset, xlat); 419 case 8: return new CMapFormat8(buffer, offset, xlat); 420 case 10: return new CMapFormat10(buffer, offset, xlat); 421 case 12: return new CMapFormat12(buffer, offset, xlat); 422 default: throw new RuntimeException("Cmap format unimplemented: " + 423 (int)buffer.getChar(offset)); 424 } 425 } 426 427 /* 428 final char charVal(byte[] cmap, int index) { 429 return (char)(((0xff & cmap[index]) << 8)+(0xff & cmap[index+1])); 430 } 431 432 final short shortVal(byte[] cmap, int index) { 433 return (short)(((0xff & cmap[index]) << 8)+(0xff & cmap[index+1])); 434 } 435 */ 436 abstract char getGlyph(int charCode); 437 438 /* Format 4 Header is 439 * ushort format (off=0) 440 * ushort length (off=2) 441 * ushort language (off=4) 442 * ushort segCountX2 (off=6) 443 * ushort searchRange (off=8) 444 * ushort entrySelector (off=10) 445 * ushort rangeShift (off=12) 446 * ushort endCount[segCount] (off=14) 1042 public static final NullCMapClass theNullCmap = new NullCMapClass(); 1043 1044 final int getControlCodeGlyph(int charCode, boolean noSurrogates) { 1045 if (charCode < 0x0010) { 1046 switch (charCode) { 1047 case 0x0009: 1048 case 0x000a: 1049 case 0x000d: return CharToGlyphMapper.INVISIBLE_GLYPH_ID; 1050 } 1051 } else if (charCode >= 0x200c) { 1052 if ((charCode <= 0x200f) || 1053 (charCode >= 0x2028 && charCode <= 0x202e) || 1054 (charCode >= 0x206a && charCode <= 0x206f)) { 1055 return CharToGlyphMapper.INVISIBLE_GLYPH_ID; 1056 } else if (noSurrogates && charCode >= 0xFFFF) { 1057 return 0; 1058 } 1059 } 1060 return -1; 1061 } 1062 } | 123 124 static final short ShiftJISEncoding = 2; 125 static final short GBKEncoding = 3; 126 static final short Big5Encoding = 4; 127 static final short WansungEncoding = 5; 128 static final short JohabEncoding = 6; 129 static final short MSUnicodeSurrogateEncoding = 10; 130 131 static final char noSuchChar = (char)0xfffd; 132 static final int SHORTMASK = 0x0000ffff; 133 static final int INTMASK = 0xffffffff; 134 135 static final char[][] converterMaps = new char[7][]; 136 137 /* 138 * Unicode->other encoding translation array. A pre-computed look up 139 * which can be shared across all fonts using that encoding. 140 * Using this saves running character coverters repeatedly. 141 */ 142 char[] xlat; 143 UVS uvs = null; 144 145 static CMap initialize(TrueTypeFont font) { 146 147 CMap cmap = null; 148 149 int offset, platformID, encodingID=-1; 150 151 int three0=0, three1=0, three2=0, three3=0, three4=0, three5=0, 152 three6=0, three10=0; 153 int zero5=0; // for Unicode Variation Sequences 154 boolean threeStar = false; 155 156 ByteBuffer cmapBuffer = font.getTableBuffer(TrueTypeFont.cmapTag); 157 int cmapTableOffset = font.getTableSize(TrueTypeFont.cmapTag); 158 short numberSubTables = cmapBuffer.getShort(2); 159 160 /* locate the offsets of all 3,* (ie Microsoft platform) encodings */ 161 for (int i=0; i<numberSubTables; i++) { 162 cmapBuffer.position(i * 8 + 4); 163 platformID = cmapBuffer.getShort(); 164 if (platformID == 3) { 165 threeStar = true; 166 encodingID = cmapBuffer.getShort(); 167 offset = cmapBuffer.getInt(); 168 switch (encodingID) { 169 case 0: three0 = offset; break; // MS Symbol encoding 170 case 1: three1 = offset; break; // MS Unicode cmap 171 case 2: three2 = offset; break; // ShiftJIS cmap. 172 case 3: three3 = offset; break; // GBK cmap 173 case 4: three4 = offset; break; // Big 5 cmap 174 case 5: three5 = offset; break; // Wansung 175 case 6: three6 = offset; break; // Johab 176 case 10: three10 = offset; break; // MS Unicode surrogates 177 } 178 } else if (platformID == 0) { 179 encodingID = cmapBuffer.getShort(); 180 offset = cmapBuffer.getInt(); 181 if (encodingID == 5) { 182 zero5 = offset; 183 } 184 } 185 } 186 187 /* This defines the preference order for cmap subtables */ 188 if (threeStar) { 189 if (three10 != 0) { 190 cmap = createCMap(cmapBuffer, three10, null); 191 } 192 else if (three0 != 0) { 193 /* The special case treatment of these fonts leads to 194 * anomalies where a user can view "wingdings" and "wingdings2" 195 * and the latter shows all its code points in the unicode 196 * private use area at 0xF000->0XF0FF and the former shows 197 * a scattered subset of its glyphs that are known mappings to 198 * unicode code points. 199 * The primary purpose of these mappings was to facilitate 200 * display of symbol chars etc in composite fonts, however 201 * this is not needed as all these code points are covered 202 * by Lucida Sans Regular. 203 * Commenting this out reduces the role of these two files 253 else { 254 cmap = createCMap(cmapBuffer, three4, 255 getConverterMap(Big5Encoding)); 256 } 257 } 258 else if (three5 != 0) { 259 cmap = createCMap(cmapBuffer, three5, 260 getConverterMap(WansungEncoding)); 261 } 262 else if (three6 != 0) { 263 cmap = createCMap(cmapBuffer, three6, 264 getConverterMap(JohabEncoding)); 265 } 266 } else { 267 /* No 3,* subtable was found. Just use whatever is the first 268 * table listed. Not very useful but maybe better than 269 * rejecting the font entirely? 270 */ 271 cmap = createCMap(cmapBuffer, cmapBuffer.getInt(8), null); 272 } 273 // For Unicode Variation Sequences 274 if (cmap != null && zero5 != 0){ 275 cmap.createUVS(cmapBuffer, zero5); 276 } 277 return cmap; 278 } 279 280 /* speed up the converting by setting the range for double 281 * byte characters; 282 */ 283 static char[] getConverter(short encodingID) { 284 int dBegin = 0x8000; 285 int dEnd = 0xffff; 286 String encoding; 287 288 switch (encodingID) { 289 case ShiftJISEncoding: 290 dBegin = 0x8140; 291 dEnd = 0xfcfc; 292 encoding = "SJIS"; 293 break; 294 case GBKEncoding: 295 dBegin = 0x8140; 296 dEnd = 0xfea0; 419 subtableLength = buffer.getInt(offset+4) & INTMASK; 420 } 421 if (offset+subtableLength > buffer.capacity()) { 422 if (FontUtilities.isLogging()) { 423 FontUtilities.getLogger().warning("Cmap subtable overflows buffer."); 424 } 425 } 426 switch (subtableFormat) { 427 case 0: return new CMapFormat0(buffer, offset); 428 case 2: return new CMapFormat2(buffer, offset, xlat); 429 case 4: return new CMapFormat4(buffer, offset, xlat); 430 case 6: return new CMapFormat6(buffer, offset, xlat); 431 case 8: return new CMapFormat8(buffer, offset, xlat); 432 case 10: return new CMapFormat10(buffer, offset, xlat); 433 case 12: return new CMapFormat12(buffer, offset, xlat); 434 default: throw new RuntimeException("Cmap format unimplemented: " + 435 (int)buffer.getChar(offset)); 436 } 437 } 438 439 private void createUVS(ByteBuffer buffer, int offset) { 440 int subtableFormat = buffer.getChar(offset); 441 if (subtableFormat == 14) { 442 long subtableLength = buffer.getInt(offset + 2) & INTMASK; 443 if (offset + subtableLength > buffer.capacity()) { 444 if (FontUtilities.isLogging()) { 445 FontUtilities.getLogger().warning("Cmap UVS subtable overflows buffer."); 446 } 447 } 448 this.uvs = new UVS(buffer, offset); 449 } 450 return; 451 } 452 453 /* 454 final char charVal(byte[] cmap, int index) { 455 return (char)(((0xff & cmap[index]) << 8)+(0xff & cmap[index+1])); 456 } 457 458 final short shortVal(byte[] cmap, int index) { 459 return (short)(((0xff & cmap[index]) << 8)+(0xff & cmap[index+1])); 460 } 461 */ 462 abstract char getGlyph(int charCode); 463 464 /* Format 4 Header is 465 * ushort format (off=0) 466 * ushort length (off=2) 467 * ushort language (off=4) 468 * ushort segCountX2 (off=6) 469 * ushort searchRange (off=8) 470 * ushort entrySelector (off=10) 471 * ushort rangeShift (off=12) 472 * ushort endCount[segCount] (off=14) 1068 public static final NullCMapClass theNullCmap = new NullCMapClass(); 1069 1070 final int getControlCodeGlyph(int charCode, boolean noSurrogates) { 1071 if (charCode < 0x0010) { 1072 switch (charCode) { 1073 case 0x0009: 1074 case 0x000a: 1075 case 0x000d: return CharToGlyphMapper.INVISIBLE_GLYPH_ID; 1076 } 1077 } else if (charCode >= 0x200c) { 1078 if ((charCode <= 0x200f) || 1079 (charCode >= 0x2028 && charCode <= 0x202e) || 1080 (charCode >= 0x206a && charCode <= 0x206f)) { 1081 return CharToGlyphMapper.INVISIBLE_GLYPH_ID; 1082 } else if (noSurrogates && charCode >= 0xFFFF) { 1083 return 0; 1084 } 1085 } 1086 return -1; 1087 } 1088 1089 static class UVS { 1090 int numSelectors; 1091 int[] selector; 1092 1093 //for Default UVS Table 1094 int[] numUnicodeValueRanges; 1095 int[][] startUnicodeValue; 1096 byte[][] additionalCount; 1097 //for Non-Default UVS Table 1098 int[] numUVSMapping; 1099 int[][] unicodeValue; 1100 char[][] glyphID; 1101 1102 UVS(ByteBuffer buffer, int offset) { 1103 numSelectors = buffer.getInt(offset+6); 1104 selector = new int[numSelectors]; 1105 numUnicodeValueRanges = new int[numSelectors]; 1106 startUnicodeValue = new int[numSelectors][]; 1107 additionalCount = new byte[numSelectors][]; 1108 numUVSMapping = new int[numSelectors]; 1109 unicodeValue = new int[numSelectors][]; 1110 glyphID = new char[numSelectors][]; 1111 1112 for (int i = 0; i < numSelectors; i++) { 1113 buffer.position(offset + 10 + i * 11); 1114 selector[i] = (buffer.get() & 0xff) << 16; //UINT24 1115 selector[i] += (buffer.get() & 0xff) << 8; 1116 selector[i] += buffer.get() & 0xff; 1117 1118 //for Default UVS Table 1119 int tableOffset = buffer.getInt(offset + 10 + i * 11 + 3); 1120 if (tableOffset == 0){ 1121 numUnicodeValueRanges[i] = 0; 1122 }else{ 1123 buffer.position(offset+tableOffset); 1124 numUnicodeValueRanges[i] = buffer.getInt() & INTMASK; 1125 1126 startUnicodeValue[i] = new int[numUnicodeValueRanges[i]]; 1127 additionalCount[i] = new byte[numUnicodeValueRanges[i]]; 1128 1129 for (int j = 0; j < numUnicodeValueRanges[i]; j++) { 1130 int temp = (buffer.get() & 0xff) << 16; //UINT24 1131 temp += (buffer.get() & 0xff) << 8; 1132 temp += buffer.get() & 0xff; 1133 startUnicodeValue[i][j] = temp; 1134 additionalCount[i][j] = buffer.get(); 1135 } 1136 } 1137 1138 //for Non-Default UVS Table 1139 tableOffset = buffer.getInt(offset + 10 + i * 11 + 7); 1140 if (tableOffset == 0){ 1141 numUVSMapping[i] = 0; 1142 } else { 1143 buffer.position(offset+tableOffset); 1144 numUVSMapping[i] = buffer.getInt() & INTMASK; 1145 unicodeValue[i] = new int[numUVSMapping[i]]; 1146 glyphID[i] = new char[numUVSMapping[i]]; 1147 1148 for (int j = 0; j < numUVSMapping[i]; j++) { 1149 int temp = (buffer.get() & 0xff) << 16; //UINT24 1150 temp += (buffer.get() & 0xff) << 8; 1151 temp += buffer.get() & 0xff; 1152 unicodeValue[i][j]= temp; 1153 glyphID[i][j]= buffer.getChar(); 1154 } 1155 } 1156 } 1157 } 1158 1159 private int cachedCode; 1160 private int targetCachedCode; 1161 private int targetCachedSelector = -1; 1162 1163 /* getGlyph for Variation selector 1164 return value: 1165 0: A special glyph for the variation selector is Not found 1166 -1: Default glyph should be used 1167 0>: A special glyph is found 1168 */ 1169 int getGlyph(int charCode, int variationSelector) { 1170 synchronized(this){ 1171 if (charCode == targetCachedCode && variationSelector == targetCachedSelector) { 1172 return cachedCode; 1173 } 1174 } 1175 1176 int targetSelector = -1; 1177 int result; 1178 for (int i = 0; i < numSelectors; i++) { 1179 if (selector[i] == variationSelector) { 1180 targetSelector = i; 1181 break; 1182 } 1183 } 1184 if (targetSelector == -1){ 1185 result = 0; 1186 storeCache(charCode, variationSelector, result); 1187 return result; 1188 } 1189 if (numUnicodeValueRanges[targetSelector] > 0) { 1190 int index = java.util.Arrays.binarySearch( 1191 startUnicodeValue[targetSelector], charCode); 1192 if (index >= 0){ 1193 result = -1; //pass through default table in actual CMAP 1194 storeCache(charCode, variationSelector, result); 1195 return result; 1196 } else { 1197 index = -index - 2; 1198 if (index >=0 1199 && charCode >= startUnicodeValue[targetSelector][index] 1200 && charCode <= startUnicodeValue[targetSelector][index] 1201 +additionalCount[targetSelector][index]) { 1202 result = -1; //pass through default table in actual CMAP 1203 storeCache(charCode, variationSelector, result); 1204 return result; 1205 } 1206 } 1207 } 1208 if (numUVSMapping[targetSelector] > 0){ 1209 int index = java.util.Arrays.binarySearch( 1210 unicodeValue[targetSelector], charCode); 1211 if (index >= 0){ 1212 result = glyphID[targetSelector][index]; 1213 storeCache(charCode, variationSelector, result); 1214 return result; 1215 } 1216 } 1217 result = 0; 1218 storeCache(charCode, variationSelector, result); 1219 return result; 1220 } 1221 1222 private synchronized void storeCache(int charCode, int variationSelector, int glyph) { 1223 cachedCode = glyph; 1224 targetCachedCode = charCode; 1225 targetCachedSelector = variationSelector; 1226 } 1227 1228 boolean hasVariationSelectorGlyph(int charCode, int variationSelector) { 1229 int result= getGlyph(charCode, variationSelector); 1230 if (result == 0) { 1231 return false; 1232 } else { 1233 return true; 1234 } 1235 } 1236 } 1237 1238 public char getGlyph(int charCode, int variationSelector) { 1239 if (uvs == null) { 1240 return 0; 1241 } 1242 int result = uvs.getGlyph(charCode, variationSelector); 1243 if (result == -1) { 1244 result = this.getGlyph(charCode); 1245 } 1246 return (char)(result & 0xFFFF); 1247 } 1248 1249 public boolean hasVariationSelectorGlyph(int charCode, int variationSelector) { 1250 if (uvs == null) { 1251 return false; 1252 } 1253 return uvs.hasVariationSelectorGlyph(charCode, variationSelector); 1254 } 1255 1256 } |