private static string[] GetFormat2GlyphNames(TrueTypeDataBytes data) { const int reservedIndexStart = 32768; var numberOfGlyphs = data.ReadUnsignedShort(); var glyphNameIndex = new int[numberOfGlyphs]; var glyphNames = new string[numberOfGlyphs]; var maxIndex = int.MinValue; for (var i = 0; i < numberOfGlyphs; i++) { var index = data.ReadUnsignedShort(); glyphNameIndex[i] = index; if (index < reservedIndexStart) { maxIndex = Math.Max(maxIndex, index); } } var nameArray = default(string[]); if (maxIndex >= WindowsGlyphList4.NumberOfMacGlyphs) { var namesLength = maxIndex - WindowsGlyphList4.NumberOfMacGlyphs + 1; nameArray = new string[namesLength]; for (var i = 0; i < namesLength; i++) { var numberOfCharacters = data.ReadUnsignedByte(); nameArray[i] = data.ReadString(numberOfCharacters, Encoding.UTF8); } } for (int i = 0; i < numberOfGlyphs; i++) { var index = glyphNameIndex[i]; if (index < WindowsGlyphList4.NumberOfMacGlyphs) { glyphNames[i] = WindowsGlyphList4.MacGlyphNames[index]; } else if (index >= WindowsGlyphList4.NumberOfMacGlyphs && index < reservedIndexStart) { if (nameArray == null) { throw new InvalidOperationException("The name array was null despite the number of glyphs exceeding the maximum Mac Glyphs."); } glyphNames[i] = nameArray[index - WindowsGlyphList4.NumberOfMacGlyphs]; } else { glyphNames[i] = ".undefined"; } } return(glyphNames); }
public NameTable Parse(TrueTypeHeaderTable header, TrueTypeDataBytes data, TableRegister.Builder register) { data.Seek(header.Offset); // ReSharper disable once UnusedVariable var format = data.ReadUnsignedShort(); var count = data.ReadUnsignedShort(); var stringOffset = data.ReadUnsignedShort(); var names = new NameRecordBuilder[count]; for (var i = 0; i < count; i++) { names[i] = NameRecordBuilder.Read(data); } var strings = new TrueTypeNameRecord[count]; for (var i = 0; i < count; i++) { var nameRecord = names[i]; var encoding = OtherEncodings.Iso88591; switch (nameRecord.PlatformId) { case TrueTypePlatformIdentifier.Windows: { var platformEncoding = (TrueTypeWindowsEncodingIdentifier)nameRecord.PlatformEncodingId; if (platformEncoding == TrueTypeWindowsEncodingIdentifier.Symbol || platformEncoding == TrueTypeWindowsEncodingIdentifier.UnicodeBmp) { encoding = Encoding.BigEndianUnicode; } break; } case TrueTypePlatformIdentifier.Unicode: { encoding = Encoding.BigEndianUnicode; break; } case TrueTypePlatformIdentifier.Iso: { switch (nameRecord.PlatformEncodingId) { case 0: encoding = Encoding.GetEncoding("US-ASCII"); break; case 1: encoding = Encoding.GetEncoding("ISO-10646-UCS-2"); break; } break; } } var position = header.Offset + stringOffset + nameRecord.Offset; data.Seek(position); var str = data.ReadString(nameRecord.Length, encoding); strings[i] = nameRecord.ToNameRecord(str); } return(new NameTable(header, GetName(4, strings), GetName(1, strings), GetName(2, strings), strings)); }
private static TrueTypeNameRecord GetTrueTypeNameRecord(NameRecordBuilder nameRecord, TrueTypeDataBytes data, uint offset) { try { var encoding = OtherEncodings.Iso88591; switch (nameRecord.PlatformId) { case TrueTypePlatformIdentifier.Windows: { var platformEncoding = (TrueTypeWindowsEncodingIdentifier)nameRecord.PlatformEncodingId; if (platformEncoding == TrueTypeWindowsEncodingIdentifier.Symbol || platformEncoding == TrueTypeWindowsEncodingIdentifier.UnicodeBmp) { encoding = Encoding.BigEndianUnicode; } break; } case TrueTypePlatformIdentifier.Unicode: { encoding = Encoding.BigEndianUnicode; break; } case TrueTypePlatformIdentifier.Iso: { switch (nameRecord.PlatformEncodingId) { case 0: encoding = Encoding.GetEncoding("US-ASCII"); break; case 1: encoding = Encoding.GetEncoding("ISO-10646-UCS-2"); break; } break; } } var position = offset + nameRecord.Offset; if (position >= data.Length) { return(null); } data.Seek(position); var str = data.ReadString(nameRecord.Length, encoding); return(nameRecord.ToNameRecord(str)); } catch { return(null); } }