1
Text Shapes
Thibaud edited this page 2026-03-19 22:40:54 +01:00

Plan: Text Shapes

Overview

Add support for creating text shapes in the canvas.

Current State

  • Available shapes: SRectangle, SCircle, STriangle
  • Uses visitor pattern for rendering
  • Menu system in App.java for adding shapes

Implementation Plan

1. Create SText class

  • New class shapes/SText extending AbstractShape
  • Fields: text (String), fontSize (int, default 16), fontName (String, default "SansSerif"), fontStyle (int, default Font.PLAIN)
  • Empty or cancelled input falls back to placeholder "Text"
  • Override resize(ResizeHandle, int, int) as no-op (text not resizable)
  • Implement accept(ShapeVisitor) calling visitor.visitText(this)
  • Bounds are stored as a Rectangle at construction time (position only, zero size); updated and cached by ShapeDraftman on each paint via FontMetrics for accurate hit-test and selection

2. ShapeVisitor interface change

  • Add visitText(SText text) to the ShapeVisitor interface
  • All implementations must implement this method

3. ShapeDraftman changes

  • Implement visitText(SText): render with Graphics2D.drawString()
  • After drawing, measure text via g2d.getFontMetrics() and update the cached bounds on the SText object
  • Selection visuals reuse existing drawHandlerIfSelected() with measured bounds

4. SVGDraftman / HTMLDraftman changes

  • Implement visitText(SText) in both visitors
  • Escape special characters in text content for safe export (& < > " ')

5. Controller changes

  • Add lightweight text-placement mode (boolean addingText)
  • When active, next canvas click prompts via JOptionPane.showInputDialog()
  • If input is null (cancelled): do not create shape, exit text-placement mode
  • If input is empty: use placeholder "Text"
  • Create SText at click position, add to model, exit text-placement mode

6. App.java changes

  • Add "Add Text" menu item that calls controller.enterTextMode()
  • Single-line, no wrapping; bounds expand horizontally with text length

Files to create/modify

  • Create: shapes/SText.java
  • Modify: ShapeVisitor.java — add visitText(SText)
  • Modify: ui/ShapeDraftman.java — implement visitText, update bounds via FontMetrics
  • Modify: ui/visitors/SVGDraftman.java — implement visitText
  • Modify: ui/visitors/HTMLDraftman.java — implement visitText
  • Modify: ui/Controller.java — add text-placement mode
  • Modify: App.java — add "Add Text" menu item

Defaults

  • Font: SansSerif, plain, size 16
  • Placeholder: "Text"
  • Text is single-line, no wrapping

Out of scope (first version)

  • Double-click to edit existing text
  • Font size/style configuration via UI
  • Text resize (no-op for now)