resize method

GenericGlyph resize({
  1. int? ascender,
  2. int? descender,
  3. int? fontHeight,
  4. double? ratioX,
  5. double? ratioY,
})

Resizes according to ascender/descender or a font height.

Implementation

GenericGlyph resize({
  int? ascender,
  int? descender,
  int? fontHeight,
  double? ratioX,
  double? ratioY,
}) {
  final metrics = this.metrics;

  late final int longestSide;
  late final double sideRatioX;
  late final double sideRatioY;

  if (ascender != null && descender != null) {
    longestSide = math.max(metrics.height, metrics.width);
    sideRatioX = (ascender + descender) / longestSide * (ratioX ?? 1);
    sideRatioY = (ascender + descender) / longestSide * (ratioY ?? 1);
  } else if (fontHeight != null) {
    longestSide = bounds.height.toInt();
    sideRatioX = fontHeight / longestSide * (ratioX ?? 1);
    sideRatioY = fontHeight / longestSide * (ratioY ?? 1);
  } else {
    throw ArgumentError('Wrong parameters for resizing');
  }

  // No need to resize
  if ((sideRatioX - 1).abs() < .02 && (sideRatioY - 1).abs() < .02) {
    return this;
  }

  final newOutlines = outlines.map((o) {
    final newOutline = o.copy();
    final newPointList = newOutline.pointList
        .map((e) => math.Point<num>(e.x * sideRatioX, e.y * sideRatioY))
        .toList();
    newOutline.pointList
      ..clear()
      ..addAll(newPointList);
    return newOutline;
  }).toList();

  final newBounds = math.Rectangle.fromPoints(
    math.Point<num>(bounds.bottomLeft.toDoublePoint().x * sideRatioX,
        bounds.bottomLeft.toDoublePoint().y * sideRatioY),
    math.Point<num>(bounds.topRight.toDoublePoint().x * sideRatioX,
        bounds.topRight.toDoublePoint().y * sideRatioY),
    // bounds.topRight.toDoublePoint() * sideRatio,
  );

  return GenericGlyph(newOutlines, newBounds, metadata);
}