Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/drawGraphicsToCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,17 @@ export function drawGraphicsToCanvas(
ctx.textAlign = alignMap[anchor]
ctx.textBaseline = baselineMap[anchor]

ctx.fillText(text.text, projected.x, projected.y)
if (text.rotation) {
// Text.rotation is counter-clockwise in graphics (y-up) space; canvas
// rotate() is clockwise in screen (y-down) space, so negate.
ctx.save()
ctx.translate(projected.x, projected.y)
ctx.rotate((-text.rotation * Math.PI) / 180)
ctx.fillText(text.text, 0, 0)
ctx.restore()
} else {
ctx.fillText(text.text, projected.x, projected.y)
}
})
}

Expand Down
5 changes: 5 additions & 0 deletions lib/getPngBufferFromGraphicsObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ export async function getPngBufferFromGraphicsObject(
fontSize: (text.fontSize ?? 12) * strokeScale,
color: text.color || "black",
anchorAlignment: text.anchorSide ?? "center",
// Text.rotation is counter-clockwise in graphics (y-up) space; the
// raster context rotates clockwise in screen (y-down) space, so negate.
rotationRadians: text.rotation
? (-text.rotation * Math.PI) / 180
: undefined,
})
}

Expand Down
25 changes: 24 additions & 1 deletion lib/getSvgFromGraphicsObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,26 @@ function getBounds(graphics: GraphicsObject): Bounds {
const { dx, dy } = offsetMap[anchor]
const x0 = t.x + dx
const y0 = t.y + dy
if (!t.rotation) {
return [
{ x: x0, y: y0 },
{ x: x0 + width, y: y0 + height },
]
}
// Rotate the text box's corners about the anchor so rotated text still
// contributes its true extents to the bounds.
const rad = (t.rotation * Math.PI) / 180
const cos = Math.cos(rad)
const sin = Math.sin(rad)
return [
{ x: x0, y: y0 },
{ x: x0 + width, y: y0 },
{ x: x0, y: y0 + height },
{ x: x0 + width, y: y0 + height },
]
].map((corner) => ({
x: t.x + (corner.x - t.x) * cos - (corner.y - t.y) * sin,
y: t.y + (corner.x - t.x) * sin + (corner.y - t.y) * cos,
}))
}),
]

Expand Down Expand Up @@ -616,6 +632,13 @@ export function getSvgFromGraphicsObject(
"font-family": "sans-serif",
"text-anchor": alignMap[anchor],
"dominant-baseline": baselineMap[anchor],
// Text.rotation is counter-clockwise in graphics (y-up) space;
// SVG rotate() is clockwise in screen (y-down) space, so negate.
...(txt.rotation
? {
transform: `rotate(${-txt.rotation}, ${projected.x}, ${projected.y})`,
}
: {}),
},
children: [{ type: "text", value: txt.text }],
}
Expand Down
5 changes: 5 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export interface Text {
anchorSide?: NinePointAnchor
color?: string
fontSize?: number
/**
* Rotation in degrees, counter-clockwise about the anchor point in graphics
* (y-up) space. 90 makes the text read bottom-to-top.
*/
rotation?: number
layer?: string
step?: number
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions tests/getSvgFromGraphicsObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,33 @@ describe("getSvgFromGraphicsObject", () => {
expect(svg).toMatchSvgSnapshot(import.meta.path, "texts")
})

test("rotates text about its anchor", () => {
const input: GraphicsObject = {
texts: [
{
x: 0,
y: 0,
text: "SPI_SCK",
rotation: 90,
},
{
x: 2,
y: 0,
text: "FLAT",
},
],
}

const svg = getSvgFromGraphicsObject(input)
// rotation is CCW in graphics space -> negated for SVG's screen space
expect(svg).toMatch(/transform="rotate\(-90, [\d.]+, [\d.]+\)"/)
// unrotated text carries no transform
const flatText = svg.split("<text").find((chunk) => chunk.includes("FLAT"))
expect(flatText).toBeDefined()
expect(flatText!).not.toContain("transform=")
expect(svg).toMatchSvgSnapshot(import.meta.path, "rotated-texts")
})

test("respects text anchorSide", () => {
const input: GraphicsObject = {
texts: [
Expand Down
Loading