예제 #1
0
        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);

                if (data.TryReadString(nameRecord.Length, encoding, out var str))
                {
                    return(nameRecord.ToNameRecord(str));
                }

                // Damaged font
                return(null);
            }
            catch
            {
                return(null);
            }
        }
예제 #2
0
        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.ReadByte();
                    if (data.TryReadString(numberOfCharacters, Encoding.UTF8, out var str))
                    {
                        nameArray[i] = str;
                    }
                }
            }

            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);
        }