decode method

DecodeResponse decode(
  1. ByteData data,
  2. VectorGraphicsCodecListener? listener, {
  3. DecodeResponse? response,
})

Decode the vector_graphics binary.

Without a provided VectorGraphicsCodecListener, this method will only validate the basic structure of an object. decoders that wish to construct a dart:ui Picture object should implement VectorGraphicsCodecListener.

Throws a StateError If the message is invalid.

Implementation

DecodeResponse decode(ByteData data, VectorGraphicsCodecListener? listener,
    {DecodeResponse? response}) {
  final _ReadBuffer buffer;
  if (response == null) {
    buffer = _ReadBuffer(data);
    if (data.lengthInBytes < 5) {
      throw StateError(
          'The provided data was not a vector_graphics binary asset.');
    }
    final int magicNumber = buffer.getUint32();
    if (magicNumber != _magicNumber) {
      throw StateError(
          'The provided data was not a vector_graphics binary asset.');
    }
    final int version = buffer.getUint8();
    if (version != _version) {
      throw StateError(
          'The provided data does not match the currently supported version.');
    }
  } else {
    buffer = response._buffer!;
  }

  bool readImage = false;
  while (buffer.hasRemaining) {
    final int type = buffer.getUint8();
    switch (type) {
      case _beginCommandsTag:
        if (readImage) {
          return DecodeResponse(false, buffer);
        }
        continue;
      case _linearGradientTag:
        _readLinearGradient(buffer, listener);
        continue;
      case _radialGradientTag:
        _readRadialGradient(buffer, listener);
        continue;
      case _fillPaintTag:
        _readFillPaint(buffer, listener);
        continue;
      case _strokePaintTag:
        _readStrokePaint(buffer, listener);
        continue;
      case _pathTag:
        _readPath(buffer, listener, half: false);
        continue;
      case _pathTagHalfPrecision:
        _readPath(buffer, listener, half: true);
        continue;
      case _drawPathTag:
        _readDrawPath(buffer, listener);
        continue;
      case _drawVerticesTag:
        _readDrawVertices(buffer, listener);
        continue;
      case _restoreTag:
        listener?.onRestoreLayer();
        continue;
      case _saveLayerTag:
        _readSaveLayer(buffer, listener);
        continue;
      case _sizeTag:
        _readSize(buffer, listener);
        continue;
      case _clipPathTag:
        _readClipPath(buffer, listener);
        continue;
      case _maskTag:
        listener?.onMask();
        continue;
      case _textConfigTag:
        _readTextConfig(buffer, listener);
        continue;
      case _drawTextTag:
        _readDrawText(buffer, listener);
        continue;
      case _imageConfigTag:
        readImage = true;
        _readImageConfig(buffer, listener);
        continue;
      case _drawImageTag:
        _readDrawImage(buffer, listener);
        continue;
      case _patternTag:
        _readPattern(buffer, listener);
        continue;
      case _textPositionTag:
        _readTextPosition(buffer, listener);
        continue;
      case _updateTextPositionTag:
        _readUpdateTextPosition(buffer, listener);
        continue;
      default:
        throw StateError('Unknown type tag $type');
    }
  }
  return const DecodeResponse(true, null);
}