performPaint method

  1. @override
void performPaint(
  1. Buffer buffer,
  2. Offset offset
)
override

Hook for subclasses to implement actual painting.

Implementation

@override
void performPaint(Buffer buffer, Offset offset) {
  final canvas = widget as Canvas;
  var currentBuffer = buffer;
  var startX = offset.dx;
  var startY = offset.dy;
  while (currentBuffer is Viewport) {
    startX += currentBuffer.bounds.x;
    startY += currentBuffer.bounds.y;
    currentBuffer = currentBuffer.parent;
  }
  final targetBuffer = currentBuffer;

  final targetWidth = targetBuffer.width;
  final targetHeight = targetBuffer.height;

  final w = size.width;
  final h = size.height;

  final drawWidth = min(w, canvas.width);
  final drawHeight = min(h, canvas.height);

  for (var cy = 0; cy < drawHeight; cy++) {
    final ty = startY + cy;
    if (ty < 0 || ty >= targetHeight) continue;

    for (var cx = 0; cx < drawWidth; cx++) {
      final tx = startX + cx;
      if (tx < 0 || tx >= targetWidth) continue;

      if (canvas.onlyDrawOnSpaces &&
          targetBuffer.getCharacter(tx.toInt(), ty.toInt()) != ' ') {
        continue;
      }

      if (canvas.isOccluded != null && canvas.isOccluded!(cx, cy)) continue;

      final idx = cy * canvas.width + cx;
      final dots = canvas._grid[idx];
      if (dots == 0 &&
          canvas._styles[idx] == null &&
          canvas.style == Style.empty) {
        continue;
      }

      final String char;
      if (canvas.renderMode == CanvasRenderMode.quadrants) {
        char = Canvas._quadrantCache[dots];
      } else if (canvas.renderMode == CanvasRenderMode.density ||
          canvas._antiAliased[idx] == 1) {
        char = Canvas._densityCache[dots];
      } else {
        char = Canvas._brailleCache[dots];
      }

      final targetIdx = ty * targetWidth + tx;
      targetBuffer.characters[targetIdx] = char;
      final s = canvas._styles[idx];
      final targetAttrIdx = targetIdx * 3;
      if (s != null) {
        targetBuffer.attributes[targetAttrIdx + 0] =
            s.foreground?.argb ?? canvas.style.foreground?.argb ?? 0;
        targetBuffer.attributes[targetAttrIdx + 1] =
            s.background?.argb ?? canvas.style.background?.argb ?? 0;
        targetBuffer.attributes[targetAttrIdx + 2] =
            s.modifiers | canvas.style.modifiers;
      } else {
        targetBuffer.attributes[targetAttrIdx + 0] =
            canvas.style.foreground?.argb ?? 0;
        targetBuffer.attributes[targetAttrIdx + 1] =
            canvas.style.background?.argb ?? 0;
        targetBuffer.attributes[targetAttrIdx + 2] = canvas.style.modifiers;
      }
    }
  }
}