public byte[] Rebuild(bool checksumAdjustment)
        {
            // create a Motorola Byte Order buffer for the new table
            WoffBuffer headBuffer = new WoffBuffer(SizeOfTable);

            // populate the buffer
            headBuffer.SetFixed(this.TableVersionNumber, (uint)FieldOffsets.TableVersionNumber);
            headBuffer.SetFixed(this.FontRevision, (uint)FieldOffsets.FontRevision);
            headBuffer.SetUInt(this.CheckSumAdjustment, (uint)FieldOffsets.CheckSumAdjustment);
            headBuffer.SetUInt(this.MagicNumber, (uint)FieldOffsets.MagicNumber);
            headBuffer.SetUShort(this.Flags, (uint)FieldOffsets.Flags);
            headBuffer.SetUShort(this.UnitsPerEm, (uint)FieldOffsets.UnitsPerEm);
            headBuffer.SetLong(this.Created, (uint)FieldOffsets.Created);
            headBuffer.SetLong(this.Modified, (uint)FieldOffsets.Modified);
            headBuffer.SetShort(this.XMin, (uint)FieldOffsets.XMin);
            headBuffer.SetShort(this.YMin, (uint)FieldOffsets.YMin);
            headBuffer.SetShort(this.XMax, (uint)FieldOffsets.XMax);
            headBuffer.SetShort(this.YMax, (uint)FieldOffsets.YMax);
            headBuffer.SetUShort(this.MacStyle, (uint)FieldOffsets.MacStyle);
            headBuffer.SetUShort(this.LowestRecPPEM, (uint)FieldOffsets.LowestRecPPEM);
            headBuffer.SetShort(this.FontDirectionHint, (uint)FieldOffsets.FontDirectionHint);
            headBuffer.SetShort(this.IndexToLocFormat, (uint)FieldOffsets.IndexToLocFormat);
            headBuffer.SetShort(this.GlyphDataFormat, (uint)FieldOffsets.GlyphDataFormat);

            if (checksumAdjustment)
            {
                // For checksum adjustment, we set this value to 0
                headBuffer.SetUInt(0, (uint)FieldOffsets.CheckSumAdjustment);
            }

            return(headBuffer.GetBuffer());
        }
Beispiel #2
0
        public static bool BinaryEqual(WoffBuffer buf1, WoffBuffer buf2)
        {
            bool bEqual = true;

            if (buf1.GetLength() != buf2.GetLength())
            {
                bEqual = false;
            }
            else
            {
                byte[] b1 = buf1.GetBuffer();
                byte[] b2 = buf2.GetBuffer();
                for (int i = 0; i < b1.Length; i++)
                {
                    if (b1[i] != b2[i])
                    {
                        bEqual = false;
                        break;
                    }
                }
            }

            return(bEqual);
        }
Beispiel #3
0
        protected override bool ReconstructTable()
        {
            var headBuffer = _woffDir.OrigTable;

            if (headBuffer == null || headBuffer.Length < 6)
            {
                return(false);
            }
            var length = headBuffer.Length;

            _tableBuffer = new WoffBuffer((uint)length);
            _tableBuffer.Copy(headBuffer);

            var fullFontName   = this.GetNameString();
            var fontFamilyName = this.GetFamilyString();
            var postScriptName = this.GetPostScriptString();

            // Check if the FullFontName and FontFamilyName are stripped from the compressed table data
            // Some tools (such as the dvisvgm – A fast DVI to SVG converter) strip both leaving only the PostScriptName
            if (!string.IsNullOrWhiteSpace(fullFontName) && !string.IsNullOrWhiteSpace(fontFamilyName))
            {
                return(true);
            }

            var fixFullFontName   = string.IsNullOrWhiteSpace(fullFontName);
            var fixFontFamilyName = string.IsNullOrWhiteSpace(fontFamilyName);

            // For the fix, we will just duplicate an existing name record, find out which one.
            ushort searchRecord = 0;

            if (!fixFullFontName)
            {
                searchRecord = (ushort)NameIdentifiers.FullFontName;
            }
            else if (!fixFontFamilyName)
            {
                searchRecord = (ushort)NameIdentifiers.FontFamilyName;
            }
            else if (!string.IsNullOrWhiteSpace(postScriptName))
            {
                searchRecord = (ushort)NameIdentifiers.PostScriptName;
            }
            else
            {
                // We cannot fix it...
                return(true);
            }

            int numRecords = this.NumberNameRecords;
            List <NameRecord> nameRecords = new List <NameRecord>(numRecords);

            for (uint i = 0; i < numRecords; i++)
            {
                NameRecord nr = GetNameRecord(i);
                if (nr != null)
                {
                    byte[] nameBytes = GetEncodedString(nr);
                    if (nameBytes != null && nameBytes.Length != 0)
                    {
                        nr.NameBytes  = nameBytes;
                        nr.NameString = DecodeString(nr.PlatformID, nr.EncodingID, nameBytes);

                        nameRecords.Add(nr);

                        if (searchRecord == nr.NameID)
                        {
                            if (fixFontFamilyName)
                            {
                                var nrFontFamily = nr.Clone();
                                nrFontFamily.NameID = (ushort)NameIdentifiers.FontFamilyName;

                                nameRecords.Add(nrFontFamily);
                            }
                            if (fixFullFontName)
                            {
                                var nrFullFont = nr.Clone();
                                nrFullFont.NameID = (ushort)NameIdentifiers.FullFontName;

                                nameRecords.Add(nrFullFont);
                            }
                        }
                    }
                }
            }

            numRecords = nameRecords.Count;

            List <byte[]> bytesNameString = new List <byte[]>();
            uint          lengthOfStrings = 0;
            ushort        offsetToStrings = (ushort)(6 + (numRecords * 12));

            for (int i = 0; i < numRecords; i++)
            {
                var    nrc        = nameRecords[i];
                byte[] byteString = nrc.NameBytes;
                bytesNameString.Add(byteString);
                lengthOfStrings += (ushort)byteString.Length;
            }

            // create a Motorola Byte Order buffer for the new table
            var tableBuffer = new WoffBuffer((uint)((ushort)FieldOffsets.NameRecords + numRecords * 12 + lengthOfStrings));

            // populate the buffer
            tableBuffer.SetUShort(this.FormatSelector, (uint)FieldOffsets.FormatSelector);
            tableBuffer.SetUShort((ushort)numRecords, (uint)FieldOffsets.NumberNameRecords);
            tableBuffer.SetUShort(offsetToStrings, (uint)FieldOffsets.OffsetToStrings);

            ushort nOffset = 0;

            // Write the NameRecords and Strings
            for (int i = 0; i < numRecords; i++)
            {
                byte[] namBytes = bytesNameString[i];

                uint startOffset = (uint)((ushort)(FieldOffsets.NameRecords) + i * NameRecord.SizeOf);

                tableBuffer.SetUShort((nameRecords[i]).PlatformID, startOffset + (ushort)NameRecord.FieldOffsets.PlatformID);
                tableBuffer.SetUShort((nameRecords[i]).EncodingID, startOffset + (ushort)NameRecord.FieldOffsets.EncodingID);
                tableBuffer.SetUShort((nameRecords[i]).LanguageID, startOffset + (ushort)NameRecord.FieldOffsets.LanguageID);
                tableBuffer.SetUShort((nameRecords[i]).NameID, startOffset + (ushort)NameRecord.FieldOffsets.NameID);
                tableBuffer.SetUShort((ushort)namBytes.Length, startOffset + (ushort)NameRecord.FieldOffsets.StringLength);
                tableBuffer.SetUShort(nOffset, startOffset + (ushort)NameRecord.FieldOffsets.StringOffset);

                //Write the string to the buffer
                for (int j = 0; j < namBytes.Length; j++)
                {
                    tableBuffer.SetByte(namBytes[j], (uint)(offsetToStrings + nOffset + j));
                }

                nOffset += (ushort)namBytes.Length;
            }


            _tableBuffer = tableBuffer;

            var nameBuffer = _tableBuffer.GetBuffer();

            _woffDir.OrigTable  = nameBuffer;
            _woffDir.OrigLength = (uint)nameBuffer.Length;
            _woffDir.RecalculateChecksum();

            return(true);
        }