/// <summary> /// Writes an unsigned byte to the current stream and advances the stream position by one byte. /// </summary> /// <param name="value">The unsigned byte to write.</param> /// <returns>Returns <see langword="true"/> if successful; otherwise, returns <see langword="false"/>.</returns> public bool Write(byte value) { if (_buffer != null && _buffer.CanWrite) { _converter.SetByte(value, 0); _buffer.Write(_converter.Buffer, 0, WoffBuffer.SizeOfByte); return(true); } return(false); }
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); }