draw static method

void draw(
  1. Canvas canvas,
  2. LatexRenderResult renderResult, {
  3. Color backgroundColor = Colors.transparent,
})

Draws the LatexRenderResult to a Canvas.

Draw order: Background -> Highlight Rectangles -> Content. Perfectly aligns with the standard Canvas drawing logic.

Implementation

static void draw(
  Canvas canvas,
  LatexRenderResult renderResult, {
  Color backgroundColor = Colors.transparent,
}) {
  final hPad = renderResult.horizontalPadding;
  final vPad = renderResult.verticalPadding;

  // 1. Draw background
  if (backgroundColor != Colors.transparent) {
    final bgPaint = Paint()..color = backgroundColor;
    canvas.drawRect(
      Rect.fromLTWH(
        0,
        0,
        renderResult.canvasWidth,
        renderResult.canvasHeight,
      ),
      bgPaint,
    );
  }

  // 2. Draw highlight backgrounds (drawn before content, as the bottom layer)
  for (final pair in renderResult.highlightRects) {
    final rect = pair.key;
    final range = pair.value;

    final highlightRect = Rect.fromLTWH(
      rect.x + hPad,
      rect.y + vPad,
      rect.width,
      rect.height,
    );

    final fillPaint = Paint()
      ..color = range.color
      ..style = PaintingStyle.fill;

    canvas.drawRect(highlightRect, fillPaint);

    if (range.borderColor != null) {
      final borderPaint = Paint()
        ..color = range.borderColor!
        ..style = PaintingStyle.stroke
        ..strokeWidth = 1.0;

      canvas.drawRect(highlightRect, borderPaint);
    }
  }

  // 3. Draw content
  renderResult.layout.draw(canvas, hPad, vPad);
}