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 child = this.child;
  if (child == null) return;
  final p = _paragraph;
  if (p != null && p.hasSize && p.firstChild == null) {
    final key = (
      p.text,
      p.constraints,
      p.textAlign,
      p.textDirection,
      p.textScaler,
      p.maxLines,
      p.overflow,
      p.softWrap,
      p.locale,
      p.strutStyle,
      p.textWidthBasis,
      p.textHeightBehavior,
    );
    if (key != _cutFor) {
      _drop();
      _pieces = _cut(p);
      _cutFor = key;
    }
    final shift = MatrixUtils.getAsTranslation(p.getTransformTo(this));
    if (_pieces.isNotEmpty && shift != null) {
      final canvas = context.canvas;
      final at = offset + shift;
      // Where the paragraph is on the page, which the edges are snapped on.
      final page = localToGlobal(shift);
      double snap(double v) => (v + 0.5).floorToDouble();
      Rect snapped(Rect r) => Rect.fromLTRB(snap(r.left), snap(r.top), snap(r.right), snap(r.bottom));
      for (final piece in _pieces) {
        final cut = snapped(piece.area.shift(page)).shift(at - page);
        if (cut.isEmpty) continue;
        canvas
          ..save()
          ..clipRect(cut)
          ..saveLayer(cut, Paint());
        // THE TILE IS SIZED AND PLACED ON THE SNAPPED STRIP: Blink snaps
        // the background positioning area to whole pixels, each edge half
        // up on the page, before it sizes the tile on it and puts it
        // (BackgroundImageGeometry's snapped positioning area), and does
        // not snap the tile. Chrome, a 50% tile repeated over a box from
        // x 111.27 to 229.5: the area is 111 to 230, the tile 59.5, and
        // its copies meet at 170.5; a tile moved `10px` on a box from
        // 98.47 starts at 108 exactly. On a box broken over lines each
        // line's strip is snapped as it lies on that line.
        final span = piece.span;
        final tile = span.tileOf(snapped(piece.strip.shift(page))).shift(at - page);
        const e = 1e-6;
        if (tile.left <= cut.left + e &&
            tile.top <= cut.top + e &&
            tile.right >= cut.right - e &&
            tile.bottom >= cut.bottom - e) {
          // A tile that covers the piece is one gradient over all of it,
          // as the browser draws a single tile.
          canvas.drawRect(cut, Paint()..shader = span.background.createShader(tile));
        } else if (!span._laidX && !span._laidY) {
          // A tile laid once short of the piece is the gradient over the
          // tile, and nothing past it (3.4, `no-repeat`).
          final r = tile.intersect(cut);
          if (!r.isEmpty) canvas.drawRect(r, Paint()..shader = span.background.createShader(tile));
        } else if (!tile.isEmpty) {
          // A TILE THAT REPEATS is Chrome's pattern of it: the tile drawn
          // once on whole device pixels and laid again and again from
          // where it is put, read pixel for pixel where the tile is a
          // whole number of them and BILINEARLY where it is not -- the
          // raster scaled back down to the tile. Chrome, a 10-stripe
          // gradient over a tile 59.5 across from 111: the pixel from 116
          // to 117 is 0.953 of the first stripe, where the stripes meet
          // at 116.95 -- the raster's texels read, not the gradient at
          // the pixel's centre -- and the pixel at a seam at 170.5 half
          // of each copy. Along an axis it is not repeated on, it stops
          // at the tile (3.4).
          final image = _tileOf(piece, tile.size);
          final whole =
              (image.width - tile.width * _dpr).abs() < 1e-6 && (image.height - tile.height * _dpr).abs() < 1e-6;
          final matrix = Matrix4.translationValues(tile.left, tile.top, 0)
            ..scaleByDouble(tile.width / image.width, tile.height / image.height, 1, 1);
          final r = Rect.fromLTRB(
            span._laidX ? cut.left : math.max(cut.left, tile.left),
            span._laidY ? cut.top : math.max(cut.top, tile.top),
            span._laidX ? cut.right : math.min(cut.right, tile.right),
            span._laidY ? cut.bottom : math.min(cut.bottom, tile.bottom),
          );
          if (!r.isEmpty) {
            canvas.drawRect(
              r,
              Paint()
                ..shader = ImageShader(
                  image,
                  TileMode.repeated,
                  TileMode.repeated,
                  matrix.storage,
                  filterQuality: whole ? FilterQuality.none : FilterQuality.low,
                ),
            );
          }
        }
        canvas.saveLayer(cut, Paint()..blendMode = BlendMode.dstIn);
        piece.mask.paint(canvas, at);
        canvas
          ..restore()
          ..restore()
          ..restore();
        // THE BORDER round the padding box, snapped as the area is: the
        // ring between the two rects, one fill, as Blink fills a border
        // of one colour -- no seam where two sides meet.
        final b = span.border;
        if (b != EdgeInsets.zero && span.borderColor.a != 0) {
          final l = piece.first ? b.left : 0.0, r = piece.last ? b.right : 0.0;
          final ring = snapped(
            Rect.fromLTRB(
              piece.area.left - l,
              piece.area.top - b.top,
              piece.area.right + r,
              piece.area.bottom + b.bottom,
            ).shift(page),
          ).shift(at - page);
          final hole = Rect.fromLTRB(ring.left + l, ring.top + b.top, ring.right - r, ring.bottom - b.bottom);
          final ink = Paint()..color = span.borderColor;
          if (hole.width > 0 && hole.height > 0) {
            canvas.drawDRRect(
              RRect.fromRectAndRadius(ring, Radius.zero),
              RRect.fromRectAndRadius(hole, Radius.zero),
              ink,
            );
          } else {
            canvas.drawRect(ring, ink);
          }
        }
      }
    }
  }
  context.paintChild(child, offset);
}