/// <summary> /// Writes a two-byte signed integer to the current stream and advances the stream /// position by two bytes. /// </summary> /// <param name="value">The two-byte signed integer to write.</param> /// <returns>Returns <see langword="true"/> if successful; otherwise, returns <see langword="false"/>.</returns> public bool WriteInt16(short value, ref uint offset) { this.Offset = offset; if (_writer != null && _writer.WriteInt16(value)) { offset = _writer.Offset; return(true); } return(false); }
private bool SerializeHmtx(IList <ushort> advanceWidths, IList <short> lsbs, int numOfHMetrics, int numOfGlyphs) { var hmtxBufferSize = 2 * numOfGlyphs + 2 * numOfHMetrics; var writer = new WoffWriter(hmtxBufferSize); for (int i = 0; i < numOfGlyphs; i++) { if (i < numOfHMetrics) { writer.WriteUInt16(advanceWidths[i]); } writer.WriteInt16(lsbs[i]); } var tableBuffer = writer.GetBuffer(); var hmtxBuffer = new WoffBuffer(tableBuffer); _woffDir.OrigTable = tableBuffer; _woffDir.OrigLength = (uint)tableBuffer.Length; return(true); }
public bool Serialize(WoffWriter writer, WoffIndexer pointsWriter, out uint glyphSize) { glyphSize = 0; if (writer == null) { return(false); } uint startOffset = writer.Offset; // 1. Write the Glyph Header writer.WriteInt16(_numContours); // int16 numberOfContours for number of contours: >= 0 is simple glyph, < 0 composite glyph. writer.WriteInt16(_xMin); // int16 xMin Minimum x for coordinate data. writer.WriteInt16(_yMin); // int16 yMin Minimum y for coordinate data. writer.WriteInt16(_xMax); // int16 xMax Maximum x for coordinate data. writer.WriteInt16(_yMax); // int16 yMax Maximum y for coordinate data. // 2. Write the Simple Glyph Description, if applicable if (_glyphType == WoffGlyphType.Simple) { // uint16 endPtsOfContours[numberOfContours] // Array of point indices for the last point of each contour, in increasing numeric order. Debug.Assert(_endPtsOfContours != null && _endPtsOfContours.Length == _numContours); if (_endPtsOfContours == null || _endPtsOfContours.Length != _numContours) { Debug.Assert(false, "Invalid condition."); return(false); } for (short i = 0; i < _numContours; i++) { writer.WriteUInt16(_endPtsOfContours[i]); } // uint16 instructionLength Total number of bytes for instructions. // If instructionLength is zero, no instructions are present for this glyph, // and this field is followed directly by the flags field.instructionLength writer.WriteUInt16(_instructionLength); // uint8 instructions[instructionLength] Array of instruction byte code for the glyph. if (_instructionLength != 0) { Debug.Assert(_instructions != null && _instructions.Length == _instructionLength); if (_instructions == null || _instructions.Length != _instructionLength) { Debug.Assert(false, "Invalid condition."); return(false); } writer.Write(_instructions); } // Pack the points Debug.Assert(_numPoints == _contours.Count); int pointsCapacity = 5 * _numPoints; // Size without the application of packing. if (pointsWriter == null) { pointsWriter = new WoffIndexer(pointsCapacity); } else { pointsWriter.Offset = 0; } uint pointsLength = 0; if (PackPoints(pointsWriter, (uint)pointsCapacity, ref pointsLength) == false) { Debug.Assert(false, "Invalid condition."); return(false); } // Serialize the points... writer.Write(pointsWriter.GetBuffer(), 0, (int)pointsLength); } // Write the Simple Glyph Description, if applicable else if (_glyphType == WoffGlyphType.Composite) { Debug.Assert(_componentLength != 0); Debug.Assert(_components != null && _componentLength == _components.Length); if (_componentLength == 0 || _components == null || _componentLength != _components.Length) { Debug.Assert(false, "Invalid condition."); return(false); } // Serialize the Composite Glyph data... writer.Write(_components, 0, _componentLength); } // NOTE: Without the padding the serialization of the glyph fails! var padBytesLength = WoffBuffer.CalcPadBytes((int)writer.Offset, 4); if (padBytesLength > 0) { var paddingBuffer = new byte[4]; writer.Write(paddingBuffer, 0, padBytesLength); } uint endOffset = writer.Offset; if (endOffset <= startOffset) { Debug.Assert(false, "Invalid condition."); return(false); } glyphSize = endOffset - startOffset; return(true); }