Exemplo n.º 1
0
        private byte[] BuildNameTable()
        {
            using (var bos = new MemoryStream())
                using (var output = new BinaryWriter(bos))
                {
                    NamingTable name = ttf.Naming;
                    if (name == null || keepTables != null && !keepTables.Contains("name", StringComparer.Ordinal))
                    {
                        return(null);
                    }

                    List <NameRecord> nameRecords = name.NameRecords;
                    int numRecords = (int)nameRecords.Count(p => ShouldCopyNameRecord(p));
                    WriteUint16(output, 0);
                    WriteUint16(output, numRecords);
                    WriteUint16(output, 2 * 3 + 2 * 6 * numRecords);

                    if (numRecords == 0)
                    {
                        return(null);
                    }

                    byte[][] names = new byte[numRecords][];
                    int      j     = 0;
                    foreach (NameRecord record in nameRecords)
                    {
                        if (ShouldCopyNameRecord(record))
                        {
                            int platform = record.PlatformId;
                            int encoding = record.PlatformEncodingId;
                            var charset  = Charset.ISO88591;

                            if (platform == CmapTable.PLATFORM_WINDOWS &&
                                encoding == CmapTable.ENCODING_WIN_UNICODE_BMP)
                            {
                                charset = Charset.UTF16BE;
                            }
                            else if (platform == 2) // ISO [deprecated]=
                            {
                                if (encoding == 0)  // 7-bit ASCII
                                {
                                    charset = Charset.ASCII;
                                }
                                else if (encoding == 1) // ISO 10646=
                                {
                                    //not sure input this input correct??
                                    charset = Charset.UTF16BE;
                                }
                            }
                            string value = record.Text;
                            if (record.NameId == 6 && prefix != null)
                            {
                                value = prefix + value;
                            }
                            names[j] = charset.GetBytes(value);
                            j++;
                        }
                    }

                    int offset = 0;
                    j = 0;
                    foreach (NameRecord nr in nameRecords)
                    {
                        if (ShouldCopyNameRecord(nr))
                        {
                            WriteUint16(output, nr.PlatformId);
                            WriteUint16(output, nr.PlatformEncodingId);
                            WriteUint16(output, nr.LanguageId);
                            WriteUint16(output, nr.NameId);
                            WriteUint16(output, names[j].Length);
                            WriteUint16(output, offset);
                            offset += names[j].Length;
                            j++;
                        }
                    }

                    for (int i = 0; i < numRecords; i++)
                    {
                        output.Write(names[i]);
                    }

                    output.Flush();
                    return(bos.ToArray());
                }
        }
Exemplo n.º 2
0
        private TTFTable ReadTableDirectory(TrueTypeFont font, TTFDataStream raf)
        {
            TTFTable table;
            string   tag = raf.ReadString(4);

            switch (tag)
            {
            case CmapTable.TAG:
                table = new CmapTable(font);
                break;

            case GlyphTable.TAG:
                table = new GlyphTable(font);
                break;

            case HeaderTable.TAG:
                table = new HeaderTable(font);
                break;

            case HorizontalHeaderTable.TAG:
                table = new HorizontalHeaderTable(font);
                break;

            case HorizontalMetricsTable.TAG:
                table = new HorizontalMetricsTable(font);
                break;

            case IndexToLocationTable.TAG:
                table = new IndexToLocationTable(font);
                break;

            case MaximumProfileTable.TAG:
                table = new MaximumProfileTable(font);
                break;

            case NamingTable.TAG:
                table = new NamingTable(font);
                break;

            case OS2WindowsMetricsTable.TAG:
                table = new OS2WindowsMetricsTable(font);
                break;

            case PostScriptTable.TAG:
                table = new PostScriptTable(font);
                break;

            case DigitalSignatureTable.TAG:
                table = new DigitalSignatureTable(font);
                break;

            case KerningTable.TAG:
                table = new KerningTable(font);
                break;

            case VerticalHeaderTable.TAG:
                table = new VerticalHeaderTable(font);
                break;

            case VerticalMetricsTable.TAG:
                table = new VerticalMetricsTable(font);
                break;

            case VerticalOriginTable.TAG:
                table = new VerticalOriginTable(font);
                break;

            case GlyphSubstitutionTable.TAG:
                table = new GlyphSubstitutionTable(font);
                break;

            default:
                table = ReadTable(font, tag);
                break;
            }
            table.Tag      = tag;
            table.CheckSum = raf.ReadUnsignedInt();
            table.Offset   = raf.ReadUnsignedInt();
            table.Length   = raf.ReadUnsignedInt();

            // skip tables with zero length (except glyf)
            if (table.Length == 0 && !tag.Equals(GlyphTable.TAG, StringComparison.Ordinal))
            {
                return(null);
            }

            return(table);
        }