PdfFontMetrics.append constructor

PdfFontMetrics.append(
  1. Iterable<PdfFontMetrics> metrics, {
  2. double letterSpacing = 0,
})

Add another metric

Implementation

factory PdfFontMetrics.append(
  Iterable<PdfFontMetrics> metrics, {
  double letterSpacing = 0,
}) {
  if (metrics.isEmpty) {
    return PdfFontMetrics.zero;
  }

  double? left;
  double? top;
  var right = 0.0;
  double? bottom;
  double? ascent;
  double? descent;
  late double lastBearing;
  double? firstBearing;
  late double spacing;

  for (final metric in metrics) {
    firstBearing ??= metric.leftBearing;
    left ??= metric.left;
    spacing = metric.advanceWidth > 0 ? letterSpacing : 0.0;
    right += metric.advanceWidth + spacing;
    lastBearing = metric.rightBearing;

    top = math.min(top ?? metric.top, metric.top);
    bottom = math.max(bottom ?? metric.bottom, metric.bottom);
    descent = math.min(descent ?? metric.descent, metric.descent);
    ascent = math.max(ascent ?? metric.ascent, metric.ascent);
  }

  return PdfFontMetrics(
    left: left!,
    top: top!,
    right: right - lastBearing - spacing,
    bottom: bottom!,
    ascent: ascent,
    descent: descent,
    advanceWidth: right - spacing,
    leftBearing: firstBearing,
  );
}