paint static method

void paint(
  1. Canvas canvas,
  2. UDocShape shape,
  3. UDocShapeGeometry geometry, {
  4. bool selected = false,
  5. bool hideText = false,
})

Implementation

static void paint(Canvas canvas, UDocShape shape, UDocShapeGeometry geometry, {bool selected = false, bool hideText = false}) {
  if (shape.points.isEmpty) return;
  final List<Offset> points = shape.points.map(geometry.toPixels).toList();
  final double width = max(0.8, geometry.lengthToPixels(shape.strokeWidth));
  final bool layered = shape.opacity < 1;
  if (layered) canvas.saveLayer(null, Paint()..color = Color.fromRGBO(0, 0, 0, shape.opacity));
  final Paint stroke = Paint()
    ..color = shape.strokeColor
    ..style = PaintingStyle.stroke
    ..strokeWidth = width
    ..strokeCap = StrokeCap.round
    ..strokeJoin = StrokeJoin.round
    ..isAntiAlias = true;
  final Rect box = points.length >= 2 ? Rect.fromPoints(points.first, points.last) : Rect.fromCenter(center: points.first, width: 1, height: 1);
  switch (shape.kind) {
    case UDocShapeKind.pen:
      _stroke(canvas, _smooth(points), stroke, dashed: shape.dashed);
      break;
    case UDocShapeKind.highlighter:
      stroke
        ..color = shape.strokeColor.withValues(alpha: shape.strokeColor.a * 0.38)
        ..strokeCap = StrokeCap.square
        ..blendMode = BlendMode.multiply;
      _stroke(canvas, _smooth(points), stroke);
      break;
    case UDocShapeKind.line:
    case UDocShapeKind.arrow:
    case UDocShapeKind.doubleArrow:
      if (points.length < 2) break;
      _stroke(
        canvas,
        Path()
          ..moveTo(points.first.dx, points.first.dy)
          ..lineTo(points.last.dx, points.last.dy),
        stroke,
        dashed: shape.dashed,
      );
      if (shape.kind != UDocShapeKind.line) _arrowHead(canvas, points.first, points.last, width, shape.strokeColor);
      if (shape.kind == UDocShapeKind.doubleArrow) _arrowHead(canvas, points.last, points.first, width, shape.strokeColor);
      break;
    case UDocShapeKind.rectangle:
    case UDocShapeKind.roundedRectangle:
    case UDocShapeKind.ellipse:
      final Path path = shape.kind == UDocShapeKind.ellipse
          ? (Path()..addOval(box))
          : (Path()..addRRect(RRect.fromRectAndRadius(box, Radius.circular(shape.kind == UDocShapeKind.roundedRectangle ? min(box.shortestSide * 0.25, 24) : 0))));
      final Color? fill = shape.fillColor;
      if (fill != null) canvas.drawPath(path, Paint()..color = fill);
      if (shape.strokeWidth > 0 && shape.strokeColor.a > 0) _stroke(canvas, path, stroke, dashed: shape.dashed);
      break;
    case UDocShapeKind.areaHighlight:
      canvas.drawRRect(
        RRect.fromRectAndRadius(box, const Radius.circular(3)),
        Paint()
          ..color = (shape.fillColor ?? shape.strokeColor).withValues(alpha: 0.32)
          ..blendMode = BlendMode.multiply,
      );
      break;
    case UDocShapeKind.textBox:
      final Color? fill = shape.fillColor;
      if (fill != null) canvas.drawRRect(RRect.fromRectAndRadius(box.inflate(4), const Radius.circular(6)), Paint()..color = fill);
      if (shape.dashed) {
        final Paint border = Paint()
          ..color = shape.strokeColor
          ..style = PaintingStyle.stroke
          ..strokeWidth = 1.2;
        _stroke(canvas, Path()..addRRect(RRect.fromRectAndRadius(box.inflate(4), const Radius.circular(6))), border, dashed: true);
      }
      if (!hideText) {
        final TextPainter text = textPainter(shape, geometry, box.width);
        text.paint(canvas, box.topLeft);
        text.dispose();
      }
      break;
    case UDocShapeKind.stickyNote:
      final double size = stickySize(geometry);
      UDocMarkupPainter.badge(canvas, points.first + Offset(size / 2, size / 2), shape.strokeColor, size);
      break;
  }
  if (layered) canvas.restore();
  if (selected) {
    final Rect outline = pixelBounds(shape, geometry).inflate(6);
    canvas.drawRect(
      outline,
      Paint()
        ..color = const Color(0xFF1E88E5)
        ..style = PaintingStyle.stroke
        ..strokeWidth = 1.2,
    );
  }
}