renderWay method

  1. @override
void renderWay(
  1. RenderInfo<Renderinstruction> renderInfo,
  2. RenderContext renderContext,
  3. WayProperties wayProperties
)

Renders a caption for a way (e.g., a street name).

The caption is drawn at the center of the way's bounding box, adjusted for rotation to remain horizontal.

Implementation

@override
void renderWay(RenderInfo renderInfo, RenderContext renderContext, WayProperties wayProperties) {
  if (renderContext is! UiRenderContext) throw Exception("renderContext is not UiRenderContext ${renderContext.runtimeType}");
  MappointRelative relative = wayProperties.getCenterAbsolute(renderContext.projection).offset(renderContext.reference).offset(0, renderinstruction.dy);
  //print("paint caption boundar: $boundary $relative ${shape}");

  ui.Canvas? uiCanvas = renderContext.canvas.expose();
  if (renderContext.rotationRadian != 0) {
    uiCanvas.save();
    uiCanvas.translate(relative.dx, relative.dy);
    // if the map is rotated 30° clockwise we have to paint the caption -30° (counter-clockwise) so that it is horizontal
    uiCanvas.rotate(2 * pi - renderContext.rotationRadian);
    uiCanvas.translate(-relative.dx, -relative.dy);
  }

  ParagraphEntry? front;
  ParagraphEntry? back;
  if (paintFront != null) front = ParagraphCacheMgr().getEntry(renderInfo.caption!, textPaint, paintFront!, renderinstruction.getMaxTextWidth());
  if (paintBack != null) back = ParagraphCacheMgr().getEntry(renderInfo.caption!, textPaint, paintBack!, renderinstruction.getMaxTextWidth());

  MapRectangle boundary = renderinstruction.calculateBoundaryWithSymbol(
    renderinstruction.position,
    back?.getWidth() ?? front?.getWidth() ?? 0,
    back?.getHeight() ?? front?.getHeight() ?? 0,
  );
  // uiCanvas.drawRect(
  //     ui.Rect.fromLTWH(relative.x + boundary.left, relative.y + boundary.top,
  //         boundary.getWidth(), boundary.getHeight()),
  //     ui.Paint()..color = Colors.red.withOpacity(0.5));

  if (back != null) uiCanvas.drawParagraph(back.paragraph, ui.Offset(relative.dx + boundary.left, relative.dy + boundary.top));
  if (front != null) uiCanvas.drawParagraph(front.paragraph, ui.Offset(relative.dx + boundary.left, relative.dy + boundary.top));
  // uiCanvas.drawCircle(ui.Offset(this.xy.x - origin.x, this.xy.y - origin.y),
  //     5, ui.Paint()..color = Colors.blue);
  if (renderContext.rotationRadian != 0) {
    uiCanvas.restore();
  }
}