getPath method

dynamic getPath(
  1. dynamic x,
  2. dynamic y,
  3. dynamic fontSize,
  4. dynamic options,
  5. dynamic font,
)

Convert the glyph to a Path we can draw on a drawing context. @param {number} x=0 - Horizontal position of the beginning of the text. @param {number} y=0 - Vertical position of the baseline of the text. @param {number} fontSize=72 - Font size in pixels. We scale the glyph units by 1 / unitsPerEm * fontSize. @param {Object=} options - xScale, yScale to stretch the glyph. @param {opentype.Font} if hinting is to be used, the font @return {opentype.Path}

Implementation

getPath(x, y, fontSize, options, font) {

  // print(" Glyph.prototype.getPath ");

  x = x != null ? x : 0;
  y = y != null ? y : 0;
  fontSize = fontSize != null ? fontSize : 72;
  var commands;
  var hPoints;
  if (!options) options = { };
  var xScale = options.xScale;
  var yScale = options.yScale;

  if (options.hinting && font && font.hinting) {
      // in case of hinting, the hinting engine takes care
      // of scaling the points (not the path) before hinting.
      hPoints = this.path && font.hinting.exec(this, fontSize);
      // in case the hinting engine failed hPoints is null
      // and thus reverts to plain rending
  }

  if (hPoints) {
      // Call font.hinting.getCommands instead of `glyf.getPath(hPoints).commands` to avoid a circular dependency
      commands = font.hinting.getCommands(hPoints);
      x = Math.round(x);
      y = Math.round(y);
      // TODO in case of hinting xyScaling is not yet supported
      xScale = yScale = 1;
  } else {
      commands = this.path.commands;
      var scale = 1 / (this.path.unitsPerEm ?? 1000) * fontSize;
      if (xScale == null) xScale = scale;
      if (yScale == null) yScale = scale;
  }

  var p = new Path();
  for (var i = 0; i < commands.length; i += 1) {
      var cmd = commands[i];
      if (cmd.type == 'M') {
          p.moveTo(x + (cmd.x * xScale), y + (-cmd.y * yScale));
      } else if (cmd.type == 'L') {
          p.lineTo(x + (cmd.x * xScale), y + (-cmd.y * yScale));
      } else if (cmd.type == 'Q') {
          p.quadraticCurveTo(x + (cmd.x1 * xScale), y + (-cmd.y1 * yScale),
                            x + (cmd.x * xScale), y + (-cmd.y * yScale));
      } else if (cmd.type == 'C') {
          p.curveTo(x + (cmd.x1 * xScale), y + (-cmd.y1 * yScale),
                    x + (cmd.x2 * xScale), y + (-cmd.y2 * yScale),
                    x + (cmd.x * xScale), y + (-cmd.y * yScale));
      } else if (cmd.type == 'Z') {
          p.closePath();
      }
  }

  return p;
}