# Shapes and text boxes

> Rectangles, rounded rectangles, ellipses, lines, arrows and text boxes that float over the grid, with move, resize, rotate, styling, text editing and full keyboard access.

Shapes are drawing objects that float over the cells, like [charts](https://docs.birchui.com/spreadsheet/charts.md) and [pictures](https://docs.birchui.com/spreadsheet/images.md). Use them to annotate a sheet: a callout next to a total, an arrow pointing at an outlier, a text box with instructions.

```ts
const callout = sheet.addShape({
  type: 'roundRect',
  anchor: { cell: 'G3', width: 200, height: 72 },
  fill: '#FFF2CC',
  stroke: '#BF9000',
  text: 'Review with finance before Friday',
});

sheet.addShape({
  type: 'arrow',
  anchor: { cell: 'F4', offsetX: 20, width: 90, height: 40 },
  stroke: '#C00000',
  strokeWidth: 2,
});
```

**`ShapeJSON`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface ShapeJSON
```

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `type` | `ShapeType` |  |
| `anchor` | `DrawingAnchor` |  |
| `rotation?` | `number` |  |
| `fill?` | `string` |  |
| `stroke?` | `string` |  |
| `strokeWidth?` | `number` |  |
| `text?` | `string` |  |
| `textColor?` | `string` |  |
| `fontSize?` | `number` |  |
| `name?` | `string` |  |
| `alt?` | `string` |  |
| `flipH?` | `boolean` |  |
| `flipV?` | `boolean` |  |
| `textAlign?` | `'left' \| 'center' \| 'right'` |  |
| `textVerticalAlign?` | `'top' \| 'middle' \| 'bottom'` |  |
| `textRuns?` | `RichTextRun[]` |  |
| `adjust?` | `number[]` |  |
| `strokeDash?` | `ShapeDash` |  |
| `startArrow?` | `boolean` |  |
| `endArrow?` | `boolean` |  |

Beside the six basic types, `type` accepts the DrawingML preset names that xlsx files use: `triangle`, `rtTriangle`, `diamond`, `parallelogram`, `trapezoid`, `pentagon`, `hexagon`, `octagon`, `rightArrow`, `leftArrow`, `upArrow`, `downArrow`, `leftRightArrow`, `upDownArrow`, `chevron`, `homePlate`, `plus`, `star5`, `leftBrace`, `rightBrace`, `leftBracket`, `rightBracket`, `bracePair`, `bracketPair`, `wedgeRectCallout`, `wedgeRoundRectCallout`, `wedgeEllipseCallout`, `flowChartProcess`, `flowChartDecision` and `flowChartTerminator`, and the connectors `bentConnector3` and `curvedConnector3`. `adjust` holds the preset's adjust values as fractions (a `rightBrace` takes the curl and the height of its tip), `strokeDash` a dash pattern, and `startArrow` and `endArrow` put arrowheads on a line or connector. `flipH` and `flipV` mirror a shape; on a line they choose which diagonal of the box it runs along. A line may be upright, with a width of 0. The Insert menu offers the six basic types; the others come from files or from `addShape()`.

Shapes scale with the sheet zoom and take their place among pictures and charts in the order of the file (see `drawingOrder` under [Pictures](https://docs.birchui.com/spreadsheet/images.md#position-and-size)).

| `type` | Shape |
| --- | --- |
| `rect`, `roundRect`, `ellipse` | A filled outline that can hold centred text. |
| `textBox` | A box with wrapped text. |
| `line`, `arrow` | Drawn from the top-left to the bottom-right corner of the anchor box. Rotate it to point elsewhere. |

| Property | Meaning |
| --- | --- |
| `anchor` | The cell the shape is attached to, optional pixel offsets, and its size in pixels. |
| `rotation` | Degrees, clockwise. |
| `fill`, `stroke`, `strokeWidth` | Colours and line width. Colours accept hex, `rgb()`, `hsl()` and CSS colour names; anything else falls back to the default. |
| `text`, `textColor`, `fontSize` | Text inside the shape. |
| `name`, `alt` | A name for your code and the description read by screen readers. |

In the toolbar, **Insert › Shapes** adds a shape at the active cell and selects it; **Insert › Text box** adds a text box.

## Editing with the mouse

- Click a shape to select it. Eight handles resize it and the handle above it rotates it; hold `Shift` while rotating to snap to 15°.
- Drag the shape to move it. It re-anchors to the cell under its top-left corner, so it follows inserted and deleted rows and columns.
- Double-click to edit its text.

Each move, resize or rotation is one undo step.

## Editing with the keyboard

Shapes are in the tab order. One shape at a time is a tab stop; `Tab` and `Shift` + `Tab` walk through the shapes of the sheet and then leave.

| Keys | Action |
| --- | --- |
| Arrow keys | Move 1 px, 10 px with `Shift` |
| `Alt` + arrow keys | Resize 1 px, 10 px with `Shift` |
| `[`, `]` | Rotate 15° counter-clockwise or clockwise |
| `Enter`, `F2` | Edit the text. `Ctrl` + `Enter` saves, `Escape` cancels. |
| `Delete`, `Backspace` | Delete the shape |
| `Ctrl` + `Z`, `Ctrl` + `Y` | Undo and redo |
| `Escape` | Deselect and return to the grid |

Every change is announced, for example "Rectangle moved to 120, 48". A shape is exposed as an image with its `alt` text, its name, or its type and text.

## API

```ts
sheet.updateShape(callout, { text: 'Approved by finance', fill: '#E2F0D9', rotation: 0 });
sheet.getShapes();
sheet.removeShape(callout);
```

`addShape` throws for an unknown type or an invalid anchor. `updateShape` merges the changes; pass `undefined` for a property to remove it.

Shapes are locked in `readOnly` mode and on a [protected sheet](https://docs.birchui.com/spreadsheet/protection.md) unless **Edit objects** is allowed.

## Saving, printing and Excel files

Shapes are saved in the sheet's `shapes` array in the [workbook JSON](https://docs.birchui.com/spreadsheet/sheets.md#workbookjson) and are included in [print output](https://docs.birchui.com/spreadsheet/printing.md) on the page that holds their anchor cell. They keep their pixel size when the grid is zoomed.

In [xlsx files](https://docs.birchui.com/spreadsheet/xlsx.md#shapes-in-files) shapes are written as DrawingML: rectangles, rounded rectangles, ellipses and text boxes as shapes (`xdr:sp`), lines and arrows as connectors (`xdr:cxnSp`), with their anchor, fill, outline, rotation, flip and text. Importing the file reads them back into `shapes`. Colours are stored as hex, so `steelblue` comes back as `#4682b4`.

A shape in a file that the six types cannot express, such as another preset, a gradient fill, a dashed outline or vertical text, is kept in the file unchanged and is not shown. LibreOffice reads the exported shapes back within 2 pixels of their position and size. Nothing is tested in Microsoft Excel.
