setStrokeOrder method

void setStrokeOrder(
  1. String strokeOrder
)

Implementation

void setStrokeOrder(String strokeOrder) {
  dynamic parsedJson;
  List<Path> tmpStrokes;
  List<List<Offset>> tmpMedians;
  List<int> tmpRadicalStrokeIndices = [];

  try {
    parsedJson = json.decode(strokeOrder.replaceAll("'", '"'));
  } catch (e) {
    throw FormatException("Invalid JSON string for stroke order.");
  }

  try {
    tmpStrokes = List.generate(
        parsedJson['strokes'].length,
        (index) => parseSvgPath(parsedJson['strokes'][index]).transform(
            // Transformation according to the makemeahanzi documentation
            Matrix4(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 900, 0, 1)
                .storage));
  } catch (e) {
    throw FormatException("Invalid strokes in stroke order JSON.");
  }

  try {
    tmpMedians = List.generate(parsedJson['medians'].length, (iStroke) {
      return List.generate(parsedJson['medians'][iStroke].length, (iPoint) {
        return Offset(
            (parsedJson['medians'][iStroke][iPoint][0]).toDouble(),
            (parsedJson['medians'][iStroke][iPoint][1] * -1 + 900)
                .toDouble());
      });
    });
  } catch (e) {
    throw FormatException("Invalid medians in stroke order JSON.");
  }

  if (tmpMedians.length != tmpStrokes.length) {
    throw FormatException("Number of strokes and medians not equal.");
  }

  try {
    tmpRadicalStrokeIndices = List<int>.generate(
        parsedJson['radStrokes'].length,
        (index) => parsedJson['radStrokes'][index]);
  } catch (e) {
    print("Could not read radical stroke indices from JSON.");
    tmpRadicalStrokeIndices = [];
  }

  if (tmpStrokes.isNotEmpty) {
    _strokeOrder = strokeOrder;
    _strokes = tmpStrokes;
    _medians = tmpMedians;
    _radicalStrokeIndices = tmpRadicalStrokeIndices;
    _nStrokes = _strokes.length;
  }
}