draw method

  1. @override
void draw(
  1. ChantContext ctxt
)
override

Paints this the element on Canvas attached to ctxt.

Implementation

@override
void draw(ChantContext ctxt) {
  final canvas = ctxt.canvas;
  double translateWidth = 0.0;
  final alignOffset = switch (textAnchor) {
    .center => bounds.width / 2,
    .right || .end => bounds.width,
    _ => 0,
  };

  canvas.save();

  final properties = <String, dynamic>{
    ...getExtraStyleProperties(ctxt),
    'base-font-family': fontFamily(ctxt),
    'base-font-size': fontSize(ctxt),
    'fill': ctxt.textStyles[cssClass]!['fill'],
  };
  final highlightOverride = {
    if (selected || highlight != null)
      'fill': highlight ?? ctxt.theme.selectionColor,
  };
  for (final span in spans) {
    final xOffset = span.xOffset ?? 0.0;
    if (span.newLine > 0) {
      canvas.translate(
        translateWidth + xOffset,
        fontSize(ctxt) * span.newLine,
      );
      translateWidth = -xOffset;
    } else if (xOffset != 0.0) {
      canvas.translate(translateWidth + xOffset, 0);
      translateWidth = -xOffset;
    }

    final paragraph = span.buildParagraph(
      ctxt,
      {...properties, ...span.properties, ...highlightOverride},
      .start,
      resize,
    );
    // Re-layout only when the constraint width changed; the paragraph
    // itself is cached on the span and reused across frames.
    if (span._lastLayoutWidth != bounds.width) {
      paragraph.layout(ParagraphConstraints(width: bounds.width));
      span._lastLayoutWidth = bounds.width;
    }
    canvas.drawParagraph(paragraph, Offset(bounds.x - alignOffset, bounds.y));

    final metricsWidth = paragraph.maxIntrinsicWidth;
    translateWidth -= metricsWidth;
    canvas.translate(metricsWidth, 0);
  }
  canvas.restore();
}