paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset offset
)
override

Paint this render object into the given context at the given offset.

Subclasses should override this method to provide a visual appearance for themselves. The render object's local coordinate system is axis-aligned with the coordinate system of the context's canvas and the render object's local origin (i.e, x=0 and y=0) is placed at the given offset in the context's canvas.

Do not call this function directly. If you wish to paint yourself, call markNeedsPaint instead to schedule a call to this function. If you wish to paint one of your children, call PaintingContext.paintChild on the given context.

When painting one of your children (via a paint child function on the given context), the current canvas held by the context might change because draw operations before and after painting children might need to be recorded on separate compositing layers.

Implementation

@override
void paint(PaintingContext context, Offset offset) {
  final laid = _laid;
  if (laid == null) return;
  final painters = _paintersFor(laid);
  final kids = _children;
  // Each fragment snapped to the page's pixels, half up, as Blink snaps a
  // paint rect; and the strip each is cut from -- the box's fragments set
  // end to end.
  final page = localToGlobal(Offset.zero);
  double snap(double v) => (v + 0.5).floorToDouble();
  final boxes = <Rect>[];
  for (final f in laid.fragments) {
    final g = f.rect.shift(page + f.box.shift);
    boxes.add(Rect.fromLTRB(snap(g.left), snap(g.top), snap(g.right), snap(g.bottom)).shift(-page));
  }
  final strips = List<Rect>.filled(boxes.length, Rect.zero);
  final byBox = <_Box, List<int>>{};
  for (var i = 0; i < laid.fragments.length; i++) {
    (byBox[laid.fragments[i].box] ??= []).add(i);
  }
  final rtl = _textDirection == TextDirection.rtl;
  for (final idx in byBox.values) {
    final length = idx.fold(0.0, (t, i) => t + boxes[i].width);
    var before = 0.0;
    for (final i in idx) {
      final b = boxes[i];
      strips[i] = rtl
          ? Rect.fromLTWH(b.right + before - length, b.top, length, b.height)
          : Rect.fromLTWH(b.left - before, b.top, length, b.height);
      before += b.width;
    }
  }
  final atomNodes = _atomsOf(_content);
  void paintOp(PaintingContext context, Offset offset, _Op op, {bool flat = false}) {
    switch (op) {
      case _FrameOp(:final fragment):
        final f = laid.fragments[fragment];
        final d = f.box.node?.decoration;
        final box = boxes[fragment];
        if (d == null || box.isEmpty) return;
        final canvas = context.canvas;
        final painter = d.createBoxPainter(markNeedsPaint);
        final strip = strips[fragment].shift(offset);
        // A BOX OF ONE FRAGMENT is its own strip, and is painted as a
        // block's decoration is, uncut: an outer shadow falls outside
        // the border box (css-backgrounds-3 7.1.1). Only a fragment of
        // several is its slice of the strip (css-break-3 5.4).
        final whole = byBox[f.box]!.length == 1;
        if (!whole) {
          canvas
            ..save()
            ..clipRect(box.shift(offset));
        }
        painter.paint(canvas, strip.topLeft, ImageConfiguration(size: strip.size, textDirection: _textDirection));
        if (!whole) canvas.restore();
        painter.dispose();
      case _TextOp(:final piece):
        final p = laid.pieces[piece];
        if (p.text.trim().isEmpty && (p.style.decoration ?? TextDecoration.none) == TextDecoration.none) return;
        final tp = painters[piece];
        // SKIA ROUNDS THE PARAGRAPH'S BASELINE, then the glyphs' device
        // row: a painter with its baseline 15.5 below its top (Inter at
        // 16px) draws its glyphs 16 below, and put 4.5 down to stand on a
        // baseline at 20 they came out on 21 -- every flow's text a pixel
        // under Chrome's, which stands the glyphs on the rounded
        // baseline. Measured in the harness: Arial at 13px (11.98) drawn
        // from 0.5 down stands on 13, not 12.
        final b = tp.computeDistanceToActualBaseline(TextBaseline.alphabetic).roundToDouble();
        tp.paint(context.canvas, offset + op.box.shift + Offset(p.x, p.baseline - b));
      case _AtomOp(:final atom):
        if (atom < kids.length) {
          final kid = kids[atom];
          final data = kid.parentData! as InlineFlowParentData;
          if (!data.shown) return;
          final opacity = atom < atomNodes.length ? atomNodes[atom].opacity : 1.0;
          final at = offset + data.offset + data.shift;
          if (opacity >= 1) {
            context.paintChild(kid, at);
            return;
          }
          final alpha = Color.getAlphaFromOpacity(opacity);
          if (alpha == 0) return;
          if (flat) {
            context.canvas.saveLayer(null, Paint()..color = Color.fromARGB(alpha, 0, 0, 0));
            context.paintChild(kid, at);
            context.canvas.restore();
          } else {
            context.pushOpacity(offset, alpha, (c, o) => c.paintChild(kid, o + data.offset + data.shift));
          }
        }
    }
  }

  // A FADED OR MOVED BOX IS A GROUP painted after the rest (appendix E,
  // step 8): the ops of no such box first, in order; then each group, in
  // tree order -- the ops of its own, then the groups inside it, the same
  // way. A faded box is one picture at its alpha; a moved one is no
  // layer, only later. A BLENDED box is one `saveLayer`, restored in its
  // mode at its alpha, and every faded box inside it is one too ([flat]):
  // a layer pushed inside a `saveLayer` is no part of it.
  void group(PaintingContext context, Offset offset, _PaintGroup g, {bool flat = false}) {
    for (final i in g.ops) {
      paintOp(context, offset, laid.ops[i], flat: flat);
    }
    for (final inner in g.inner) {
      final b = inner.box!;
      final opacity = b.node!.opacity, mode = b.node!.blendMode;
      if (opacity >= 1 && mode == BlendMode.srcOver) {
        group(context, offset, inner, flat: flat);
        continue;
      }
      final alpha = Color.getAlphaFromOpacity(opacity);
      if (alpha == 0) continue;
      if (flat || mode != BlendMode.srcOver) {
        final canvas = context.canvas;
        canvas.saveLayer(
          null,
          Paint()
            ..color = Color.fromARGB(alpha, 0, 0, 0)
            ..blendMode = mode,
        );
        group(context, offset, inner, flat: true);
        canvas.restore();
        continue;
      }
      context.pushOpacity(offset, alpha, (c, o) => group(c, o, inner));
    }
    // What is placed against the box, after all it holds.
    final kid = g.box == null ? null : _placedKids?[g.box];
    if (kid != null) {
      final data = kid.parentData! as InlineFlowParentData;
      if (data.shown) context.paintChild(kid, offset + data.offset + data.shift);
    }
  }

  group(context, offset, _groupsOf(laid));
  for (final (kid, opacity) in _orphans ?? const <(RenderBox, double)>[]) {
    final data = kid.parentData! as InlineFlowParentData;
    final at = offset + data.offset + data.shift;
    if (opacity >= 1) {
      context.paintChild(kid, at);
    } else {
      context.pushOpacity(
        offset,
        Color.getAlphaFromOpacity(opacity),
        (c, o) => c.paintChild(kid, o + data.offset + data.shift),
      );
    }
  }
}