createPath method

Map<String, dynamic> createPath(
  1. String char,
  2. num scale,
  3. num offsetX,
  4. num offsetY,
  5. dynamic data,
)

Implementation

Map<String, dynamic> createPath(
    String char, num scale, num offsetX, num offsetY, data) {
  var _font = data["font"];
  List<int> _glyphs = List<int>.from(_font.stringToGlyphs(char));

  var gid = _glyphs[0];
  var charPath = _font.glyphToPath(gid);

  var _preScale = (100000) / ((_font.head["unitsPerEm"] ?? 2048) * 72);
  // var _preScale = 1;
  var ha = Math.round(_font.hmtx["aWidth"][gid] * _preScale);

  var path = ShapePath();

  double x = 0.1;
  double y = 0.1;
  double cpx, cpy, cpx1, cpy1, cpx2, cpy2;

  var cmds = charPath["cmds"];
  List<double> crds = List<double>.from(charPath["crds"].map((e) => e.toDouble()));

  // print(" charPath  before scale ....");
  // print(charPath);

  crds = crds.map((n) => Math.round(n * _preScale).toDouble()).toList();

  // print(" charPath ha: ${ha} _preScale: ${_preScale} ");
  // print(cmds);
  // print(crds);

  int i = 0;
  int l = cmds.length;
  for (int j = 0; j < l; j++) {
    var action = cmds[j];

    switch (action) {
      case 'M': // moveTo
        x = crds[i++] * scale + offsetX;
        y = crds[i++] * scale + offsetY;

        path.moveTo(x, y);
        break;

      case 'L': // lineTo

        x = crds[i++] * scale + offsetX;
        y = crds[i++] * scale + offsetY;

        path.lineTo(x, y);

        break;

      case 'Q': // quadraticCurveTo

        cpx = crds[i++] * scale + offsetX;
        cpy = crds[i++] * scale + offsetY;
        cpx1 = crds[i++] * scale + offsetX;
        cpy1 = crds[i++] * scale + offsetY;

        path.quadraticCurveTo(cpx1, cpy1, cpx, cpy);

        break;

      case 'B':
      case 'C': // bezierCurveTo

        cpx = crds[i++] * scale + offsetX;
        cpy = crds[i++] * scale + offsetY;
        cpx1 = crds[i++] * scale + offsetX;
        cpy1 = crds[i++] * scale + offsetY;
        cpx2 = crds[i++] * scale + offsetX;
        cpy2 = crds[i++] * scale + offsetY;

        path.bezierCurveTo(cpx, cpy, cpx1, cpy1, cpx2, cpy2);

        break;
    }
  }

  return {"offsetX": ha * scale, "path": path};
}