Ejemplo n.º 1
0
 public byte this[ushort index]
 {
     get {
         if (_writer != null)
         {
             var buffer = _writer.GetBuffer();
             return(buffer[index]);
         }
         throw new InvalidOperationException();
     }
     set {
         if (_writer != null)
         {
             _writer.Offset = index;
             _writer.Write(value);
         }
     }
 }
Ejemplo n.º 2
0
        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);
        }