Sync snapshot (2026-08-16) (#99)
- Docs: print dialog with live preview, EMF/WMF rendering, nested tables, margin comments, Word-style table UX - Slides: chart and SmartArt fidelity overhaul, Format Background pane, Shape Tools tab, PowerPoint line-spacing model - Sheets: ribbon batches (themes, page breaks, protection, goal seek), AI formula audit and workbook search, import fidelity fixes - PDF: comments margin with in-place note editing - UI: unified color pickers and save icon across apps (icon by @ulangmeier in #96) Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
co-authored by
GenOffice
parent
e4d9d407c1
commit
04a994b9e9
File diff suppressed because it is too large
Load Diff
@@ -105,10 +105,25 @@ export function buildRenderSlide(
|
||||
): RenderSlide {
|
||||
const vp = makeViewport(size, opts.fitWidthPx)
|
||||
const metrics = opts.metrics ?? new HeuristicMetrics()
|
||||
const sub =
|
||||
const subNum =
|
||||
opts.slideNo != null
|
||||
? (el: SlideElement) => withSlideNum(el, opts.slideNo!)
|
||||
: (el: SlideElement) => el
|
||||
// <p:sp useBgFill="1">: painted with the slide's effective background (gradient geometry is
|
||||
// approximated in shape-local coords; such shapes are near-page-sized in practice)
|
||||
const subBg = (e: SlideElement): SlideElement => {
|
||||
if (e.type === 'group') {
|
||||
const g = e as GroupElement
|
||||
const children = g.children.map(subBg)
|
||||
return children.some((c, i) => c !== g.children[i]) ? { ...g, children } : e
|
||||
}
|
||||
return (e.type === 'shape' || e.type === 'text') &&
|
||||
(e as TextElement).useBgFill &&
|
||||
slide.background
|
||||
? ({ ...e, fill: slide.background } as SlideElement)
|
||||
: e
|
||||
}
|
||||
const sub = (el: SlideElement): SlideElement => subBg(subNum(el))
|
||||
const nodes: RenderNode[] = []
|
||||
// master/layout decoration layer: drawn at the bottom (above the background, below content), read-only
|
||||
for (const el of slide.decorations ?? []) {
|
||||
@@ -144,6 +159,8 @@ export function buildRenderSlide(
|
||||
heightPx: vp.heightPx,
|
||||
scale: vp.scale,
|
||||
background: resolveFill(slide.background, vp, opts.media),
|
||||
...(slide.bgOwn ? { bgOwn: true } : {}),
|
||||
...(slide.masterSpHidden ? { bgGraphicsHidden: true } : {}),
|
||||
nodes,
|
||||
...(hidden ? { hidden: true } : {}),
|
||||
}
|
||||
@@ -183,6 +200,28 @@ function buildNode(
|
||||
const bg = resolveFill(chartEl.chart.bgFill, vp, media)
|
||||
if (bg.kind !== 'none') (node as import('./render-tree').ChartRenderNode).bgFill = bg
|
||||
}
|
||||
if (chartEl.chart.border) {
|
||||
;(node as import('./render-tree').ChartRenderNode).border = {
|
||||
color: chartEl.chart.border.color,
|
||||
widthPx: emuToPx(chartEl.chart.border.widthEmu, vp.scale),
|
||||
}
|
||||
}
|
||||
const plotRect = (node as import('./render-tree').ChartRenderNode).plotRect
|
||||
if (plotRect && chartEl.chart.plotFill) {
|
||||
const f = resolveFill(chartEl.chart.plotFill, vp, media)
|
||||
if (f.kind !== 'none') plotRect.fill = f
|
||||
}
|
||||
// chartUserShapes line overlays: fractions of the chart frame, drawn over the plot
|
||||
for (const ul of chartEl.chart.userLines ?? []) {
|
||||
;(node as import('./render-tree').ChartRenderNode).axisLines.push({
|
||||
x1: ul.x1 * box.w,
|
||||
y1: ul.y1 * box.h,
|
||||
x2: ul.x2 * box.w,
|
||||
y2: ul.y2 * box.h,
|
||||
color: ul.color,
|
||||
widthPx: Math.max(emuToPx(ul.widthEmu, vp.scale), 0.75),
|
||||
})
|
||||
}
|
||||
}
|
||||
return node
|
||||
}
|
||||
@@ -191,20 +230,51 @@ function buildNode(
|
||||
// SmartArt read-only preview: pre-rendered drawing shapes rendered with group semantics
|
||||
// (diagram canvas coordinate size = frame ext, equivalent to chOff 0,0 / chExt ext)
|
||||
if (pt.previewShapes?.length) {
|
||||
// The drawing part is a generation-time cache: when the frame was resized
|
||||
// afterwards, its shapes keep the old coordinates. PowerPoint re-lays the diagram
|
||||
// out into the frame; approximate that by mapping the child space to the drawing
|
||||
// bounds so the group transform compresses the shapes into the frame.
|
||||
const fx = pt.transform.offset.cx
|
||||
const fy = pt.transform.offset.cy
|
||||
let bx = 0
|
||||
let by = 0
|
||||
for (const sh of pt.previewShapes) {
|
||||
const o = sh.transform?.offset
|
||||
if (!o) continue
|
||||
// rotation-aware visual bounds: a rotated box's raw offset can lie far outside
|
||||
// the frame while the shape itself fits (90deg-rotated boxes swap their extents)
|
||||
const th = (((sh.transform?.rot ?? 0) / 60000) * Math.PI) / 180
|
||||
const hw = Math.abs((o.cx / 2) * Math.cos(th)) + Math.abs((o.cy / 2) * Math.sin(th))
|
||||
const hh = Math.abs((o.cx / 2) * Math.sin(th)) + Math.abs((o.cy / 2) * Math.cos(th))
|
||||
bx = Math.max(bx, o.x + o.cx / 2 + hw)
|
||||
by = Math.max(by, o.y + o.cy / 2 + hh)
|
||||
}
|
||||
const pseudo: GroupElement = {
|
||||
id: pt.id,
|
||||
type: 'group',
|
||||
anchor: pt.anchor,
|
||||
transform: pt.transform,
|
||||
children: pt.previewShapes,
|
||||
childOffset: { x: 0, y: 0, cx: pt.transform.offset.cx, cy: pt.transform.offset.cy },
|
||||
// Small overshoots are intentional bleed (rotated accent lines cross the frame
|
||||
// edge; PowerPoint keeps them) - only a gross overflow marks a stale cache
|
||||
// (tdf-style resized frames overflow 2x+)
|
||||
childOffset: {
|
||||
x: 0,
|
||||
y: 0,
|
||||
cx: bx > fx * 1.2 ? bx : fx,
|
||||
cy: by > fy * 1.2 ? by : fy,
|
||||
},
|
||||
}
|
||||
return buildGroup(pseudo, box, vp, metrics, media)
|
||||
}
|
||||
// OLE read-only preview: the embedded preview image stretches to fill the frame (ignoring the pic's own xfrm)
|
||||
// OLE read-only preview: the embedded preview image stretches to fill the frame (ignoring the
|
||||
// pic's own xfrm) over an opaque white canvas (metafile previews are often transparent)
|
||||
if (pt.previewPicture) {
|
||||
return buildPicture({ ...pt.previewPicture, id: pt.id }, box, vp, media)
|
||||
const pic = buildPicture({ ...pt.previewPicture, id: pt.id }, box, vp, media)
|
||||
if (pic?.type === 'picture') pic.bgColor = '#FFFFFF'
|
||||
return pic
|
||||
}
|
||||
if (pt.noChip) return null
|
||||
return buildChip(pt, box)
|
||||
}
|
||||
default:
|
||||
@@ -260,6 +330,7 @@ function buildShape(
|
||||
box,
|
||||
sourceId: el.id,
|
||||
fill: resolveFill(el.fill, vp, media),
|
||||
...(el.fillOverlay ? { fillOverlay: resolveFill(el.fillOverlay, vp, media) } : {}),
|
||||
...(el.placeholder ? { placeholder: el.placeholder } : {}),
|
||||
...(el.presetGeometry ? { presetGeometry: el.presetGeometry } : {}),
|
||||
}
|
||||
@@ -357,6 +428,10 @@ function buildPicture(
|
||||
): PictureRenderNode {
|
||||
const dataUrl = el.dataUrl ?? (el.mediaRef ? media?.(el.mediaRef) : undefined)
|
||||
const clip = pictureClip(el.presetGeometry, box, el.adjust)
|
||||
// GDI metafiles play back on an opaque white DC: PowerPoint shows a white panel for
|
||||
// an EMF/WMF that never paints its background (PlanS academy banner, measured)
|
||||
const isMetafile =
|
||||
typeof dataUrl === 'string' && /^data:image\/(x-)?(emf|wmf|emz|wmz)[;,]/.test(dataUrl)
|
||||
const node: PictureRenderNode = {
|
||||
id: `r_${el.id}`,
|
||||
type: 'picture',
|
||||
@@ -370,6 +445,10 @@ function buildPicture(
|
||||
...(el.media ? { media: el.media.kind } : {}),
|
||||
...(el.name ? { name: el.name } : {}),
|
||||
...(el.descr ? { descr: el.descr } : {}),
|
||||
...(el.fill && el.fill.type !== 'none' ? { fill: resolveFill(el.fill, vp, media) } : {}),
|
||||
...(isMetafile ? { bgColor: '#FFFFFF' } : {}),
|
||||
...(el.duotone ? { duotone: el.duotone } : {}),
|
||||
...(el.clrChange ? { clrChange: el.clrChange } : {}),
|
||||
}
|
||||
const stroke = resolveStroke(el.stroke, vp)
|
||||
if (stroke) node.stroke = stroke
|
||||
@@ -443,9 +522,10 @@ function buildTable(
|
||||
media: MediaResolver | undefined,
|
||||
): TableRenderNode {
|
||||
const sumW = el.colWidths.reduce((a, b) => a + b, 0) || 1
|
||||
const sumH = el.rowHeights.reduce((a, b) => a + b, 0) || 1
|
||||
const colPx = el.colWidths.map((w) => (w / sumW) * box.w)
|
||||
const rowPx = el.rowHeights.map((h) => (h / sumH) * box.h)
|
||||
// a:tr h is an absolute minimum height (h=0 → size to content); PowerPoint ignores a
|
||||
// stale frame cy and recomputes the table height from the rows
|
||||
const rowPx = el.rowHeights.map((h) => emuToPx(h, vp.scale))
|
||||
// Prefix sums → start of each column
|
||||
const colX: number[] = [0]
|
||||
for (const w of colPx) colX.push(colX[colX.length - 1]! + w)
|
||||
@@ -468,7 +548,9 @@ function buildTable(
|
||||
vp,
|
||||
trimEdgeSpacing: true,
|
||||
})
|
||||
const needed = probe.contentHeight + probe.insets.t + probe.insets.b
|
||||
// PowerPoint sizes auto rows by the glyph extent (last baseline + descent), not
|
||||
// the line-box sum — with explicit lnSpc the box extends below the ink (18pt probe)
|
||||
const needed = (probe.inkBottom ?? probe.contentHeight) + probe.insets.t + probe.insets.b
|
||||
if (needed > (rowPx[r] ?? 0)) rowPx[r] = needed
|
||||
})
|
||||
})
|
||||
@@ -527,6 +609,7 @@ function buildTable(
|
||||
box,
|
||||
sourceId: el.id,
|
||||
cells,
|
||||
...(el.bgFill ? { bgFill: resolveFill(el.bgFill, vp, media) } : {}),
|
||||
gridX: colX,
|
||||
gridY: rowY,
|
||||
...(el.styleFlags ? { styleFlags: el.styleFlags } : {}),
|
||||
@@ -550,7 +633,9 @@ function chartStyleInfo(m: ChartElement['chart']): import('./render-tree').Chart
|
||||
: m.pseudo3D
|
||||
? 'pie3D'
|
||||
: 'pie'
|
||||
: m.kind
|
||||
: m.kind === 'funnel' || m.kind === 'sunburst'
|
||||
? 'unknown'
|
||||
: m.kind
|
||||
return {
|
||||
kind,
|
||||
legendPos: m.legendPos == null ? 'none' : m.legendPos === 'tr' ? 'r' : m.legendPos,
|
||||
|
||||
@@ -36,12 +36,30 @@ export function resolveFill(
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
case 'image':
|
||||
case 'image': {
|
||||
// Tile natural size: image pixels are 96dpi units; vp.scale is the 96dpi-px multiplier
|
||||
const pxPerImagePx = vp.scale
|
||||
return {
|
||||
kind: 'image',
|
||||
dataUrl: media?.(fill.mediaRef),
|
||||
mode: fill.mode ?? 'stretch',
|
||||
...(fill.alpha != null ? { alpha: fill.alpha } : {}),
|
||||
...(fill.fillRect ? { fillRect: fill.fillRect } : {}),
|
||||
...(fill.duotone ? { duotone: fill.duotone } : {}),
|
||||
...(fill.clrChange ? { clrChange: fill.clrChange } : {}),
|
||||
...(fill.tile
|
||||
? {
|
||||
tile: {
|
||||
scaleX: pxPerImagePx * fill.tile.sx,
|
||||
scaleY: pxPerImagePx * fill.tile.sy,
|
||||
txPx: emuToPx(fill.tile.tx, vp.scale),
|
||||
tyPx: emuToPx(fill.tile.ty, vp.scale),
|
||||
algn: fill.tile.algn,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
}
|
||||
case 'pattern':
|
||||
// Pattern fills are approximated with the foreground color for now (Phase 2 skips 8x8 tile replication)
|
||||
return { kind: 'solid', color: fill.fg }
|
||||
|
||||
@@ -27,8 +27,11 @@ export interface FontMetrics {
|
||||
ascent: number
|
||||
/** Descent at the font size (baseline to bottom, px, positive) */
|
||||
descent: number
|
||||
/** Suggested line height (px) */
|
||||
/** Suggested line height (px) — the line box, without external leading */
|
||||
lineHeight: number
|
||||
/** External leading (hhea lineGap, px): advances the next line but sits outside the
|
||||
* line box (CoreText/PowerPoint single-spacing = lineHeight + externalLeading) */
|
||||
externalLeading?: number
|
||||
}
|
||||
|
||||
export interface FontMetricsProvider {
|
||||
@@ -166,6 +169,8 @@ export interface OpentypeFontLike {
|
||||
unitsPerEm: number
|
||||
ascender: number
|
||||
descender: number
|
||||
/** hhea lineGap (external leading, font units); part of the single-spacing line height */
|
||||
lineGap?: number
|
||||
getAdvanceWidth(text: string, fontSize: number): number
|
||||
/** Optional: char → glyph index (0 = missing glyph). Used for the missing-glyph heuristic fallback. */
|
||||
charToGlyphIndex?(char: string): number
|
||||
@@ -187,7 +192,17 @@ export class OpentypeMetrics implements FontMetricsProvider {
|
||||
const scale = style.fontSizePx / font.unitsPerEm
|
||||
const ascent = font.ascender * scale
|
||||
const descent = Math.abs(font.descender) * scale
|
||||
return { ascent, descent, lineHeight: ascent + descent }
|
||||
// Single spacing includes the hhea lineGap as EXTERNAL leading: CoreText — and with
|
||||
// it PowerPoint for Mac — paces Times New Roman/Arial at (asc+desc+gap) = 1.15em,
|
||||
// not the bare asc+desc 1.107/1.117em (dropping the gap made multi-line text creep
|
||||
// upward). The gap advances lines but stays outside the line box.
|
||||
const lineGap = (font.lineGap ?? 0) * scale
|
||||
return {
|
||||
ascent,
|
||||
descent,
|
||||
lineHeight: ascent + descent,
|
||||
...(lineGap > 0 ? { externalLeading: lineGap } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
measure(text: string, style: RunStyle): number {
|
||||
|
||||
@@ -51,7 +51,12 @@ export function connectorPoints(
|
||||
* - bentConnector4: 3 elbows, adj1 = first-segment x fraction, adj2 = second-segment y fraction (default 50% each)
|
||||
* - bentConnector5: 4 elbows, adj1/adj2/adj3 by analogy
|
||||
*/
|
||||
function bentConnectorPts(preset: string, w: number, h: number, adjust?: Record<string, number>): number[] {
|
||||
function bentConnectorPts(
|
||||
preset: string,
|
||||
w: number,
|
||||
h: number,
|
||||
adjust?: Record<string, number>,
|
||||
): number[] {
|
||||
const n = parseInt(preset.slice(-1), 10) || 2
|
||||
const clamp = (v: number, lo = -2, hi = 3) => Math.min(Math.max(v, lo), hi)
|
||||
const adj = (name: string, dflt: number) =>
|
||||
@@ -180,6 +185,51 @@ export function presetPolygon(
|
||||
// prettier-ignore
|
||||
return [x1, y2, x2, y2, x2, y1, x3, y1, x3, y2, x4, y2, x4, y3, x3, y3, x3, y4, x2, y4, x2, y3, x1, y3]
|
||||
}
|
||||
case 'mathNotEqual': {
|
||||
// ECMA-376 formulas; the slash may overflow the box on extreme aspect ratios (spec behavior)
|
||||
const a1 = Math.min(frac('adj1', 23520), 0.5)
|
||||
const crAng = Math.min(Math.max(adjust?.adj2 ?? 6600000, 4200000), 6600000)
|
||||
const a3 = Math.min(frac('adj3', 11760), 1 - 2 * a1)
|
||||
const dy1 = h * a1
|
||||
const dy2 = (h * a3) / 2
|
||||
const dx1 = (w * 73490) / 200000
|
||||
const hc = w / 2
|
||||
const vc = h / 2
|
||||
const hd2 = h / 2
|
||||
const x1 = hc - dx1
|
||||
const x8 = hc + dx1
|
||||
const y2 = vc - dy2
|
||||
const y3 = vc + dy2
|
||||
const y1 = y2 - dy1
|
||||
const y4 = y3 + dy1
|
||||
const cadj2 = (crAng - 5400000) / 60000
|
||||
const xadj2 = hd2 * Math.tan(cadj2 * D2R)
|
||||
const len = Math.hypot(xadj2, hd2)
|
||||
const bhw = (len * dy1) / hd2
|
||||
const x7 = hc + xadj2 - bhw / 2
|
||||
const x6 = x7 - (xadj2 * y1) / hd2
|
||||
const x5 = x7 - (xadj2 * y2) / hd2
|
||||
const x4 = x7 - (xadj2 * y3) / hd2
|
||||
const x3 = x7 - (xadj2 * y4) / hd2
|
||||
const rx6 = x6 + bhw
|
||||
const rx5 = x5 + bhw
|
||||
const rx4 = x4 + bhw
|
||||
const rx3 = x3 + bhw
|
||||
const rx7 = x7 + bhw
|
||||
const dx7 = (dy1 * hd2) / len
|
||||
const dy3 = (dy1 * xadj2) / len
|
||||
const rx = cadj2 > 0 ? x7 + dx7 : rx7
|
||||
const lx = cadj2 > 0 ? x7 : rx7 - dx7
|
||||
const ry = cadj2 > 0 ? dy3 : 0
|
||||
const ly = cadj2 > 0 ? 0 : -dy3
|
||||
const dlx = w - rx
|
||||
const drx = w - lx
|
||||
const dly = h - ry
|
||||
const dry = h - ly
|
||||
// prettier-ignore
|
||||
return [x1, y1, x6, y1, lx, ly, rx, ry, rx6, y1, x8, y1, x8, y2, rx5, y2, rx4, y3, x8, y3,
|
||||
x8, y4, rx3, y4, drx, dry, dlx, dly, x3, y4, x1, y4, x1, y3, x4, y3, x5, y2, x1, y2]
|
||||
}
|
||||
case 'plus': {
|
||||
const a = ss * frac('adj', 25000)
|
||||
const x1 = a
|
||||
@@ -187,7 +237,32 @@ export function presetPolygon(
|
||||
const y1 = a
|
||||
const y2 = h - a
|
||||
// Cross: vertical arm positioned by height, horizontal arm by width
|
||||
return [x1, 0, x2, 0, x2, y1, w, y1, w, y2, x2, y2, x2, h, x1, h, x1, y2, 0, y2, 0, y1, x1, y1]
|
||||
return [
|
||||
x1,
|
||||
0,
|
||||
x2,
|
||||
0,
|
||||
x2,
|
||||
y1,
|
||||
w,
|
||||
y1,
|
||||
w,
|
||||
y2,
|
||||
x2,
|
||||
y2,
|
||||
x2,
|
||||
h,
|
||||
x1,
|
||||
h,
|
||||
x1,
|
||||
y2,
|
||||
0,
|
||||
y2,
|
||||
0,
|
||||
y1,
|
||||
x1,
|
||||
y1,
|
||||
]
|
||||
}
|
||||
case 'rightArrow': {
|
||||
const thick = h * frac('adj1', 50000)
|
||||
@@ -235,8 +310,26 @@ export function presetPolygon(
|
||||
const y1 = (h - thick) / 2
|
||||
const y2 = (h + thick) / 2
|
||||
return [
|
||||
0, h / 2, head, 0, head, y1, w - head, y1, w - head, 0, w, h / 2,
|
||||
w - head, h, w - head, y2, head, y2, head, h,
|
||||
0,
|
||||
h / 2,
|
||||
head,
|
||||
0,
|
||||
head,
|
||||
y1,
|
||||
w - head,
|
||||
y1,
|
||||
w - head,
|
||||
0,
|
||||
w,
|
||||
h / 2,
|
||||
w - head,
|
||||
h,
|
||||
w - head,
|
||||
y2,
|
||||
head,
|
||||
y2,
|
||||
head,
|
||||
h,
|
||||
]
|
||||
}
|
||||
case 'upDownArrow': {
|
||||
@@ -245,8 +338,26 @@ export function presetPolygon(
|
||||
const x1 = (w - thick) / 2
|
||||
const x2 = (w + thick) / 2
|
||||
return [
|
||||
w / 2, 0, w, head, x2, head, x2, h - head, w, h - head, w / 2, h,
|
||||
0, h - head, x1, h - head, x1, head, 0, head,
|
||||
w / 2,
|
||||
0,
|
||||
w,
|
||||
head,
|
||||
x2,
|
||||
head,
|
||||
x2,
|
||||
h - head,
|
||||
w,
|
||||
h - head,
|
||||
w / 2,
|
||||
h,
|
||||
0,
|
||||
h - head,
|
||||
x1,
|
||||
h - head,
|
||||
x1,
|
||||
head,
|
||||
0,
|
||||
head,
|
||||
]
|
||||
}
|
||||
case 'chevron': {
|
||||
@@ -289,8 +400,10 @@ export function presetPolygon(
|
||||
}
|
||||
case 'lightningBolt': {
|
||||
// Fixed points from ECMA presetShapeDefinitions (21600 coordinate system)
|
||||
const u = [8472, 0, 12860, 6672, 11050, 6672, 16577, 12007, 14767, 12007,
|
||||
21600, 21600, 10800, 14387, 12377, 14387, 5333, 6667, 7778, 6667]
|
||||
const u = [
|
||||
8472, 0, 12860, 6672, 11050, 6672, 16577, 12007, 14767, 12007, 21600, 21600, 10800, 14387,
|
||||
12377, 14387, 5333, 6667, 7778, 6667,
|
||||
]
|
||||
return u.map((v, i) => (v / 21600) * (i % 2 === 0 ? w : h))
|
||||
}
|
||||
case 'flowChartPreparation':
|
||||
@@ -312,8 +425,6 @@ export function presetPolygon(
|
||||
const depth = Math.min(frac('adj1', 15000) * 2, 0.6)
|
||||
return gearPoints(6, w, h, 1 - depth)
|
||||
}
|
||||
case 'funnel':
|
||||
return [0, 0, w, 0, w * 0.62, h * 0.62, w * 0.62, h, w * 0.38, h, w * 0.38, h * 0.62]
|
||||
case 'quadArrow': {
|
||||
const sw2 = (ss * frac('adj1', 22500)) / 2
|
||||
const hw = ss * frac('adj2', 22500)
|
||||
@@ -321,10 +432,54 @@ export function presetPolygon(
|
||||
const cx = w / 2
|
||||
const cy = h / 2
|
||||
return [
|
||||
cx, 0, cx + hw, hl, cx + sw2, hl, cx + sw2, cy - sw2, w - hl, cy - sw2, w - hl, cy - hw,
|
||||
w, cy, w - hl, cy + hw, w - hl, cy + sw2, cx + sw2, cy + sw2, cx + sw2, h - hl, cx + hw, h - hl,
|
||||
cx, h, cx - hw, h - hl, cx - sw2, h - hl, cx - sw2, cy + sw2, hl, cy + sw2, hl, cy + hw,
|
||||
0, cy, hl, cy - hw, hl, cy - sw2, cx - sw2, cy - sw2, cx - sw2, hl, cx - hw, hl,
|
||||
cx,
|
||||
0,
|
||||
cx + hw,
|
||||
hl,
|
||||
cx + sw2,
|
||||
hl,
|
||||
cx + sw2,
|
||||
cy - sw2,
|
||||
w - hl,
|
||||
cy - sw2,
|
||||
w - hl,
|
||||
cy - hw,
|
||||
w,
|
||||
cy,
|
||||
w - hl,
|
||||
cy + hw,
|
||||
w - hl,
|
||||
cy + sw2,
|
||||
cx + sw2,
|
||||
cy + sw2,
|
||||
cx + sw2,
|
||||
h - hl,
|
||||
cx + hw,
|
||||
h - hl,
|
||||
cx,
|
||||
h,
|
||||
cx - hw,
|
||||
h - hl,
|
||||
cx - sw2,
|
||||
h - hl,
|
||||
cx - sw2,
|
||||
cy + sw2,
|
||||
hl,
|
||||
cy + sw2,
|
||||
hl,
|
||||
cy + hw,
|
||||
0,
|
||||
cy,
|
||||
hl,
|
||||
cy - hw,
|
||||
hl,
|
||||
cy - sw2,
|
||||
cx - sw2,
|
||||
cy - sw2,
|
||||
cx - sw2,
|
||||
hl,
|
||||
cx - hw,
|
||||
hl,
|
||||
]
|
||||
}
|
||||
case 'bentArrow': {
|
||||
@@ -333,8 +488,24 @@ export function presetPolygon(
|
||||
const hl = ss * frac('adj3', 25000)
|
||||
const yc = Math.max(hw, t / 2)
|
||||
return [
|
||||
0, h, 0, yc - t / 2, w - hl, yc - t / 2, w - hl, yc - hw, w, yc,
|
||||
w - hl, yc + hw, w - hl, yc + t / 2, t, yc + t / 2, t, h,
|
||||
0,
|
||||
h,
|
||||
0,
|
||||
yc - t / 2,
|
||||
w - hl,
|
||||
yc - t / 2,
|
||||
w - hl,
|
||||
yc - hw,
|
||||
w,
|
||||
yc,
|
||||
w - hl,
|
||||
yc + hw,
|
||||
w - hl,
|
||||
yc + t / 2,
|
||||
t,
|
||||
yc + t / 2,
|
||||
t,
|
||||
h,
|
||||
]
|
||||
}
|
||||
case 'wedgeRectCallout': {
|
||||
@@ -403,7 +574,12 @@ function gearPoints(teeth: number, w: number, h: number, innerR: number): number
|
||||
const pts: number[] = []
|
||||
for (let i = 0; i < teeth; i++) {
|
||||
const c = -90 + i * pitch
|
||||
for (const [off, r] of [[-rootHalf, innerR], [-tipHalf, 1], [tipHalf, 1], [rootHalf, innerR]] as const) {
|
||||
for (const [off, r] of [
|
||||
[-rootHalf, innerR],
|
||||
[-tipHalf, 1],
|
||||
[tipHalf, 1],
|
||||
[rootHalf, innerR],
|
||||
] as const) {
|
||||
const a = ((c + off) * Math.PI) / 180
|
||||
pts.push(cx + Math.cos(a) * cx * r, cy + Math.sin(a) * cy * r)
|
||||
}
|
||||
@@ -447,17 +623,36 @@ const D2R = Math.PI / 180
|
||||
/** SVG path builder (local px); elliptical arcs are split into ≤90° parametric segments as cubics. */
|
||||
class PathB {
|
||||
private parts: string[] = []
|
||||
M(x: number, y: number) { this.parts.push(`M ${R2(x)} ${R2(y)}`); return this }
|
||||
L(x: number, y: number) { this.parts.push(`L ${R2(x)} ${R2(y)}`); return this }
|
||||
M(x: number, y: number) {
|
||||
this.parts.push(`M ${R2(x)} ${R2(y)}`)
|
||||
return this
|
||||
}
|
||||
L(x: number, y: number) {
|
||||
this.parts.push(`L ${R2(x)} ${R2(y)}`)
|
||||
return this
|
||||
}
|
||||
Q(x1: number, y1: number, x: number, y: number) {
|
||||
this.parts.push(`Q ${R2(x1)} ${R2(y1)} ${R2(x)} ${R2(y)}`); return this
|
||||
this.parts.push(`Q ${R2(x1)} ${R2(y1)} ${R2(x)} ${R2(y)}`)
|
||||
return this
|
||||
}
|
||||
C(x1: number, y1: number, x2: number, y2: number, x: number, y: number) {
|
||||
this.parts.push(`C ${R2(x1)} ${R2(y1)} ${R2(x2)} ${R2(y2)} ${R2(x)} ${R2(y)}`); return this
|
||||
this.parts.push(`C ${R2(x1)} ${R2(y1)} ${R2(x2)} ${R2(y2)} ${R2(x)} ${R2(y)}`)
|
||||
return this
|
||||
}
|
||||
Z() {
|
||||
this.parts.push('Z')
|
||||
return this
|
||||
}
|
||||
Z() { this.parts.push('Z'); return this }
|
||||
/** Parametric angles (degrees, y-down clockwise positive); move says whether to M/L to the arc start first */
|
||||
arc(cx: number, cy: number, rx: number, ry: number, startDeg: number, sweepDeg: number, move?: 'M' | 'L') {
|
||||
arc(
|
||||
cx: number,
|
||||
cy: number,
|
||||
rx: number,
|
||||
ry: number,
|
||||
startDeg: number,
|
||||
sweepDeg: number,
|
||||
move?: 'M' | 'L',
|
||||
) {
|
||||
const st = startDeg * D2R
|
||||
const sw = sweepDeg * D2R
|
||||
const sx = cx + rx * Math.cos(st)
|
||||
@@ -475,12 +670,20 @@ class PathB {
|
||||
const y1 = cy + ry * Math.sin(a1)
|
||||
const x2 = cx + rx * Math.cos(a2)
|
||||
const y2 = cy + ry * Math.sin(a2)
|
||||
this.C(x1 - k * rx * Math.sin(a1), y1 + k * ry * Math.cos(a1),
|
||||
x2 + k * rx * Math.sin(a2), y2 - k * ry * Math.cos(a2), x2, y2)
|
||||
this.C(
|
||||
x1 - k * rx * Math.sin(a1),
|
||||
y1 + k * ry * Math.cos(a1),
|
||||
x2 + k * rx * Math.sin(a2),
|
||||
y2 - k * ry * Math.cos(a2),
|
||||
x2,
|
||||
y2,
|
||||
)
|
||||
}
|
||||
return this
|
||||
}
|
||||
d() { return this.parts.join(' ') }
|
||||
d() {
|
||||
return this.parts.join(' ')
|
||||
}
|
||||
}
|
||||
|
||||
/** Single-anchor ellipse subpath (full circle). */
|
||||
@@ -489,7 +692,12 @@ function ellipseSub(b: PathB, cx: number, cy: number, rx: number, ry: number, cc
|
||||
}
|
||||
|
||||
/** Rectangle with mixed round/snip corners (corner order TL/TR/BR/BL). */
|
||||
function mixedCornerRect(w: number, h: number, sizes: number[], kinds: Array<'none' | 'round' | 'snip'>): string {
|
||||
function mixedCornerRect(
|
||||
w: number,
|
||||
h: number,
|
||||
sizes: number[],
|
||||
kinds: Array<'none' | 'round' | 'snip'>,
|
||||
): string {
|
||||
const [tl, tr, br, bl] = sizes as [number, number, number, number]
|
||||
const b = new PathB()
|
||||
b.M(tl, 0).L(w - tr, 0)
|
||||
@@ -511,15 +719,48 @@ function mixedCornerRect(w: number, h: number, sizes: number[], kinds: Array<'no
|
||||
function cloudBlob(w: number, h: number): PathB {
|
||||
const b = new PathB()
|
||||
const u: Array<[number, number]> = [
|
||||
[0.2, 0.85], [0.06, 0.86], [0, 0.72], [0.02, 0.59], [0.03, 0.47], [0.11, 0.39], [0.2, 0.42],
|
||||
[0.19, 0.26], [0.29, 0.14], [0.4, 0.2], [0.45, 0.07], [0.6, 0.04], [0.67, 0.14],
|
||||
[0.76, 0.04], [0.91, 0.1], [0.92, 0.26], [0.99, 0.31], [1, 0.46], [0.97, 0.56],
|
||||
[1, 0.69], [0.94, 0.81], [0.85, 0.82], [0.83, 0.94], [0.72, 0.98], [0.65, 0.91],
|
||||
[0.58, 1], [0.45, 1], [0.39, 0.91], [0.33, 0.98], [0.23, 0.95], [0.2, 0.85],
|
||||
[0.2, 0.85],
|
||||
[0.06, 0.86],
|
||||
[0, 0.72],
|
||||
[0.02, 0.59],
|
||||
[0.03, 0.47],
|
||||
[0.11, 0.39],
|
||||
[0.2, 0.42],
|
||||
[0.19, 0.26],
|
||||
[0.29, 0.14],
|
||||
[0.4, 0.2],
|
||||
[0.45, 0.07],
|
||||
[0.6, 0.04],
|
||||
[0.67, 0.14],
|
||||
[0.76, 0.04],
|
||||
[0.91, 0.1],
|
||||
[0.92, 0.26],
|
||||
[0.99, 0.31],
|
||||
[1, 0.46],
|
||||
[0.97, 0.56],
|
||||
[1, 0.69],
|
||||
[0.94, 0.81],
|
||||
[0.85, 0.82],
|
||||
[0.83, 0.94],
|
||||
[0.72, 0.98],
|
||||
[0.65, 0.91],
|
||||
[0.58, 1],
|
||||
[0.45, 1],
|
||||
[0.39, 0.91],
|
||||
[0.33, 0.98],
|
||||
[0.23, 0.95],
|
||||
[0.2, 0.85],
|
||||
]
|
||||
b.M(u[0]![0] * w, u[0]![1] * h)
|
||||
for (let i = 1; i + 2 < u.length + 1; i += 3) {
|
||||
b.C(u[i]![0] * w, u[i]![1] * h, u[i + 1]![0] * w, u[i + 1]![1] * h, u[i + 2]![0] * w, u[i + 2]![1] * h)
|
||||
b.C(
|
||||
u[i]![0] * w,
|
||||
u[i]![1] * h,
|
||||
u[i + 1]![0] * w,
|
||||
u[i + 1]![1] * h,
|
||||
u[i + 2]![0] * w,
|
||||
u[i + 2]![1] * h,
|
||||
)
|
||||
}
|
||||
return b.Z()
|
||||
}
|
||||
@@ -543,7 +784,7 @@ export function presetPath(
|
||||
Math.min(Math.max((adjust?.[name] ?? dflt) / 100000, 0), 1)
|
||||
/** Angular adjusts: 1/60000 degree → degrees */
|
||||
const ang = (name: string, dflt: number) => (adjust?.[name] ?? dflt) / 60000
|
||||
const sweepCW = (a1: number, a2: number) => ((a2 - a1) % 360 + 360) % 360
|
||||
const sweepCW = (a1: number, a2: number) => (((a2 - a1) % 360) + 360) % 360
|
||||
|
||||
switch (preset) {
|
||||
case 'arc': {
|
||||
@@ -558,12 +799,23 @@ export function presetPath(
|
||||
case 'chord': {
|
||||
const a1 = ang('adj1', 2700000)
|
||||
const a2 = ang('adj2', 16200000)
|
||||
return { path: new PathB().arc(cx, cy, cx, cy, a1, sweepCW(a1, a2) || 180, 'M').Z().d() }
|
||||
return {
|
||||
path: new PathB()
|
||||
.arc(cx, cy, cx, cy, a1, sweepCW(a1, a2) || 180, 'M')
|
||||
.Z()
|
||||
.d(),
|
||||
}
|
||||
}
|
||||
case 'pie': {
|
||||
const a1 = ang('adj1', 0)
|
||||
const a2 = ang('adj2', 16200000)
|
||||
return { path: new PathB().M(cx, cy).arc(cx, cy, cx, cy, a1, sweepCW(a1, a2) || 270, 'L').Z().d() }
|
||||
return {
|
||||
path: new PathB()
|
||||
.M(cx, cy)
|
||||
.arc(cx, cy, cx, cy, a1, sweepCW(a1, a2) || 270, 'L')
|
||||
.Z()
|
||||
.d(),
|
||||
}
|
||||
}
|
||||
case 'blockArc': {
|
||||
const a1 = ang('adj1', 10800000)
|
||||
@@ -576,6 +828,33 @@ export function presetPath(
|
||||
b.arc(cx, cy, rxI, ryI, a1 + sw, -sw, 'L').Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'circularArrow':
|
||||
case 'leftCircularArrow': {
|
||||
// Approximation of the ECMA geometry: an annular band plus a triangular arrowhead
|
||||
// at the sweep end; the left variant mirrors the winding direction
|
||||
const left = preset === 'leftCircularArrow'
|
||||
const t = ss * frac('adj1', 12500)
|
||||
const headR = t * 0.9
|
||||
const rxO = Math.max(cx - headR, 0)
|
||||
const ryO = Math.max(cy - headR, 0)
|
||||
const rxI = Math.max(rxO - t, 0)
|
||||
const ryI = Math.max(ryO - t, 0)
|
||||
const rxM = (rxO + rxI) / 2
|
||||
const ryM = (ryO + ryI) / 2
|
||||
const start = left ? 115 : 65
|
||||
const sweep = left ? -250 : 250
|
||||
const end = start + sweep
|
||||
const headAng = left ? -32 : 32
|
||||
const pt = (deg: number, rx: number, ry: number) =>
|
||||
[cx + rx * Math.cos(deg * D2R), cy + ry * Math.sin(deg * D2R)] as const
|
||||
const b = new PathB().arc(cx, cy, rxO, ryO, start, sweep, 'M')
|
||||
const [hx1, hy1] = pt(end, rxO + headR, ryO + headR)
|
||||
const [tx, ty] = pt(end + headAng, rxM, ryM)
|
||||
const [hx2, hy2] = pt(end, Math.max(rxI - headR, 0), Math.max(ryI - headR, 0))
|
||||
b.L(hx1, hy1).L(tx, ty).L(hx2, hy2)
|
||||
b.arc(cx, cy, rxI, ryI, end, -sweep, 'L').Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'donut': {
|
||||
const t = ss * frac('adj', 25000)
|
||||
const b = new PathB()
|
||||
@@ -587,7 +866,11 @@ export function presetPath(
|
||||
case 'frame': {
|
||||
const t = ss * frac('adj1', 12500)
|
||||
const b = new PathB().M(0, 0).L(w, 0).L(w, h).L(0, h).Z()
|
||||
b.M(t, t).L(t, h - t).L(w - t, h - t).L(w - t, t).Z()
|
||||
b.M(t, t)
|
||||
.L(t, h - t)
|
||||
.L(w - t, h - t)
|
||||
.L(w - t, t)
|
||||
.Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'round1Rect': {
|
||||
@@ -609,6 +892,40 @@ export function presetPath(
|
||||
const r2 = ss * frac('adj2', 16667)
|
||||
return { path: mixedCornerRect(w, h, [r1, r2, 0, 0], ['round', 'snip', 'none', 'none']) }
|
||||
}
|
||||
case 'mathEqual': {
|
||||
const a1 = Math.min(frac('adj1', 23520), 0.36745)
|
||||
const a2 = Math.min(frac('adj2', 11760), 1 - 2 * a1)
|
||||
const dy1 = h * a1
|
||||
const dy2 = (h * a2) / 2
|
||||
const dx1 = (w * 73490) / 200000
|
||||
const x1 = cx - dx1
|
||||
const x2 = cx + dx1
|
||||
const y2 = cy - dy2
|
||||
const y3 = cy + dy2
|
||||
const y1 = y2 - dy1
|
||||
const y4 = y3 + dy1
|
||||
const b = new PathB().M(x1, y1).L(x2, y1).L(x2, y2).L(x1, y2).Z()
|
||||
b.M(x1, y3).L(x2, y3).L(x2, y4).L(x1, y4).Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'funnel': {
|
||||
const d = ss / 20
|
||||
const hd4 = h / 4
|
||||
const rw3 = cx / 4
|
||||
const rh3 = hd4 / 4
|
||||
// ECMA arcTo angles are ray angles; convert to PathB's parametric (both ellipses share the cx:hd4 aspect)
|
||||
const par = (deg: number) =>
|
||||
Math.atan2(cx * Math.sin(deg * D2R), hd4 * Math.cos(deg * D2R)) / D2R
|
||||
const da = Math.atan2(hd4 * Math.sin(8 * D2R), cx * Math.cos(8 * D2R)) / D2R
|
||||
const t0 = par(180 - da)
|
||||
const t3 = par(da)
|
||||
const b = new PathB()
|
||||
b.arc(cx, hd4, cx, hd4, t0, t3 + 360 - t0, 'M')
|
||||
b.arc(cx, h - rh3, rw3, rh3, t3, par(180 - da) - t3, 'L').Z()
|
||||
// Mouth interior: inner ellipse reverse-wound punches the hole
|
||||
b.arc(cx, hd4, Math.max(cx - d, 0), Math.max(hd4 - d, 0), 180, -360, 'M').Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'heart': {
|
||||
const b = new PathB().M(0.5 * w, 0.3 * h)
|
||||
b.C(0.5 * w, 0.12 * h, 0.36 * w, 0.01 * h, 0.22 * w, 0.01 * h)
|
||||
@@ -653,7 +970,10 @@ export function presetPath(
|
||||
const tipY = cy + h * adjRaw(adjust, 'adj2', 62500)
|
||||
const b = cloudBlob(w, h)
|
||||
// Tail: two small bubbles toward the tip
|
||||
for (const [t, r] of [[0.72, 0.075], [0.92, 0.045]] as const) {
|
||||
for (const [t, r] of [
|
||||
[0.72, 0.075],
|
||||
[0.92, 0.045],
|
||||
] as const) {
|
||||
ellipseSub(b, cx + (tipX - cx) * t, cy + (tipY - cy) * t, ss * r, ss * r)
|
||||
}
|
||||
return { path: b.d() }
|
||||
@@ -677,8 +997,22 @@ export function presetPath(
|
||||
}
|
||||
case 'cube': {
|
||||
const d = ss * frac('adj', 25000)
|
||||
const path = new PathB().M(0, d).L(d, 0).L(w, 0).L(w, h - d).L(w - d, h).L(0, h).Z().d()
|
||||
const inner = new PathB().M(0, d).L(w - d, d).L(w, 0).M(w - d, d).L(w - d, h).d()
|
||||
const path = new PathB()
|
||||
.M(0, d)
|
||||
.L(d, 0)
|
||||
.L(w, 0)
|
||||
.L(w, h - d)
|
||||
.L(w - d, h)
|
||||
.L(0, h)
|
||||
.Z()
|
||||
.d()
|
||||
const inner = new PathB()
|
||||
.M(0, d)
|
||||
.L(w - d, d)
|
||||
.L(w, 0)
|
||||
.M(w - d, d)
|
||||
.L(w - d, h)
|
||||
.d()
|
||||
return { path, strokePath: inner }
|
||||
}
|
||||
case 'can': {
|
||||
@@ -708,14 +1042,36 @@ export function presetPath(
|
||||
const t = ss * frac('adj', 12500)
|
||||
const path = new PathB().M(0, 0).L(w, 0).L(w, h).L(0, h).Z().d()
|
||||
const inner = new PathB()
|
||||
.M(t, t).L(w - t, t).L(w - t, h - t).L(t, h - t).Z()
|
||||
.M(0, 0).L(t, t).M(w, 0).L(w - t, t).M(w, h).L(w - t, h - t).M(0, h).L(t, h - t)
|
||||
.M(t, t)
|
||||
.L(w - t, t)
|
||||
.L(w - t, h - t)
|
||||
.L(t, h - t)
|
||||
.Z()
|
||||
.M(0, 0)
|
||||
.L(t, t)
|
||||
.M(w, 0)
|
||||
.L(w - t, t)
|
||||
.M(w, h)
|
||||
.L(w - t, h - t)
|
||||
.M(0, h)
|
||||
.L(t, h - t)
|
||||
return { path, strokePath: inner.d() }
|
||||
}
|
||||
case 'foldedCorner': {
|
||||
const f = ss * frac('adj', 16667)
|
||||
const path = new PathB().M(0, 0).L(w, 0).L(w, h - f).L(w - f, h).L(0, h).Z().d()
|
||||
const fold = new PathB().M(w - f, h).L(w - 0.8 * f, h - 0.8 * f).L(w, h - f).d()
|
||||
const path = new PathB()
|
||||
.M(0, 0)
|
||||
.L(w, 0)
|
||||
.L(w, h - f)
|
||||
.L(w - f, h)
|
||||
.L(0, h)
|
||||
.Z()
|
||||
.d()
|
||||
const fold = new PathB()
|
||||
.M(w - f, h)
|
||||
.L(w - 0.8 * f, h - 0.8 * f)
|
||||
.L(w, h - f)
|
||||
.d()
|
||||
return { path, strokePath: fold }
|
||||
}
|
||||
case 'smileyFace': {
|
||||
@@ -725,7 +1081,9 @@ export function presetPath(
|
||||
const face = new PathB()
|
||||
ellipseSub(face, 0.35 * w, 0.37 * h, 0.05 * w, 0.05 * h)
|
||||
ellipseSub(face, 0.65 * w, 0.37 * h, 0.05 * w, 0.05 * h)
|
||||
face.M(0.3 * w, 0.67 * h).Q(cx, h * Math.min(Math.max(0.67 + 4 * g, 0.4), 0.95), 0.7 * w, 0.67 * h)
|
||||
face
|
||||
.M(0.3 * w, 0.67 * h)
|
||||
.Q(cx, h * Math.min(Math.max(0.67 + 4 * g, 0.4), 0.95), 0.7 * w, 0.67 * h)
|
||||
return { path: b.d(), strokePath: face.d() }
|
||||
}
|
||||
case 'noSmoking': {
|
||||
@@ -743,21 +1101,53 @@ export function presetPath(
|
||||
const len = Math.hypot(p2x - p1x, p2y - p1y) || 1
|
||||
const nx = (-(p2y - p1y) / len) * (t / 2)
|
||||
const ny = ((p2x - p1x) / len) * (t / 2)
|
||||
b.M(p1x + nx, p1y + ny).L(p2x + nx, p2y + ny).L(p2x - nx, p2y - ny).L(p1x - nx, p1y - ny).Z()
|
||||
b.M(p1x + nx, p1y + ny)
|
||||
.L(p2x + nx, p2y + ny)
|
||||
.L(p2x - nx, p2y - ny)
|
||||
.L(p1x - nx, p1y - ny)
|
||||
.Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'ribbon': {
|
||||
const b = new PathB()
|
||||
b.M(0, 0.25 * h).L(0.25 * w, 0.25 * h).L(0.25 * w, h).L(0, h).L(0.0833 * w, 0.625 * h).Z()
|
||||
b.M(w, 0.25 * h).L(0.75 * w, 0.25 * h).L(0.75 * w, h).L(w, h).L(0.9167 * w, 0.625 * h).Z()
|
||||
b.M(0.125 * w, 0).L(0.875 * w, 0).L(0.875 * w, 0.75 * h).L(0.125 * w, 0.75 * h).Z()
|
||||
b.M(0, 0.25 * h)
|
||||
.L(0.25 * w, 0.25 * h)
|
||||
.L(0.25 * w, h)
|
||||
.L(0, h)
|
||||
.L(0.0833 * w, 0.625 * h)
|
||||
.Z()
|
||||
b.M(w, 0.25 * h)
|
||||
.L(0.75 * w, 0.25 * h)
|
||||
.L(0.75 * w, h)
|
||||
.L(w, h)
|
||||
.L(0.9167 * w, 0.625 * h)
|
||||
.Z()
|
||||
b.M(0.125 * w, 0)
|
||||
.L(0.875 * w, 0)
|
||||
.L(0.875 * w, 0.75 * h)
|
||||
.L(0.125 * w, 0.75 * h)
|
||||
.Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'ribbon2': {
|
||||
const b = new PathB()
|
||||
b.M(0, 0.75 * h).L(0.25 * w, 0.75 * h).L(0.25 * w, 0).L(0, 0).L(0.0833 * w, 0.375 * h).Z()
|
||||
b.M(w, 0.75 * h).L(0.75 * w, 0.75 * h).L(0.75 * w, 0).L(w, 0).L(0.9167 * w, 0.375 * h).Z()
|
||||
b.M(0.125 * w, h).L(0.875 * w, h).L(0.875 * w, 0.25 * h).L(0.125 * w, 0.25 * h).Z()
|
||||
b.M(0, 0.75 * h)
|
||||
.L(0.25 * w, 0.75 * h)
|
||||
.L(0.25 * w, 0)
|
||||
.L(0, 0)
|
||||
.L(0.0833 * w, 0.375 * h)
|
||||
.Z()
|
||||
b.M(w, 0.75 * h)
|
||||
.L(0.75 * w, 0.75 * h)
|
||||
.L(0.75 * w, 0)
|
||||
.L(w, 0)
|
||||
.L(0.9167 * w, 0.375 * h)
|
||||
.Z()
|
||||
b.M(0.125 * w, h)
|
||||
.L(0.875 * w, h)
|
||||
.L(0.875 * w, 0.25 * h)
|
||||
.L(0.125 * w, 0.25 * h)
|
||||
.Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'wave': {
|
||||
@@ -765,16 +1155,37 @@ export function presetPath(
|
||||
const b = new PathB().M(0, a)
|
||||
b.C(w / 6, 0, w / 3, 0, w / 2, a).C((2 * w) / 3, 2 * a, (5 * w) / 6, 2 * a, w, a)
|
||||
b.L(w, h - a)
|
||||
b.C((5 * w) / 6, h, (2 * w) / 3, h, w / 2, h - a).C(w / 3, h - 2 * a, w / 6, h - 2 * a, 0, h - a)
|
||||
b.C((5 * w) / 6, h, (2 * w) / 3, h, w / 2, h - a).C(
|
||||
w / 3,
|
||||
h - 2 * a,
|
||||
w / 6,
|
||||
h - 2 * a,
|
||||
0,
|
||||
h - a,
|
||||
)
|
||||
return { path: b.Z().d() }
|
||||
}
|
||||
case 'doubleWave': {
|
||||
const a = h * Math.min(frac('adj1', 6250), 0.2)
|
||||
const b = new PathB().M(0, a)
|
||||
b.C(w / 12, 0, w / 6, 0, w / 4, a).C(w / 3, 2 * a, (5 * w) / 12, 2 * a, w / 2, a)
|
||||
b.C((7 * w) / 12, 0, (2 * w) / 3, 0, (3 * w) / 4, a).C((5 * w) / 6, 2 * a, (11 * w) / 12, 2 * a, w, a)
|
||||
b.C((7 * w) / 12, 0, (2 * w) / 3, 0, (3 * w) / 4, a).C(
|
||||
(5 * w) / 6,
|
||||
2 * a,
|
||||
(11 * w) / 12,
|
||||
2 * a,
|
||||
w,
|
||||
a,
|
||||
)
|
||||
b.L(w, h - a)
|
||||
b.C((11 * w) / 12, h, (5 * w) / 6, h, (3 * w) / 4, h - a).C((2 * w) / 3, h - 2 * a, (7 * w) / 12, h - 2 * a, w / 2, h - a)
|
||||
b.C((11 * w) / 12, h, (5 * w) / 6, h, (3 * w) / 4, h - a).C(
|
||||
(2 * w) / 3,
|
||||
h - 2 * a,
|
||||
(7 * w) / 12,
|
||||
h - 2 * a,
|
||||
w / 2,
|
||||
h - a,
|
||||
)
|
||||
b.C((5 * w) / 12, h, w / 3, h, w / 4, h - a).C(w / 6, h - 2 * a, w / 12, h - 2 * a, 0, h - a)
|
||||
return { path: b.Z().d() }
|
||||
}
|
||||
@@ -788,7 +1199,12 @@ export function presetPath(
|
||||
const b = new PathB().M(0, h).L(0, ryO)
|
||||
b.arc(rxO, ryO, rxO, ryO, 180, 180)
|
||||
const yh = h - hl
|
||||
b.L(xrc + t / 2, yh).L(xrc + hw, yh).L(xrc, h).L(xrc - hw, yh).L(xrc - t / 2, yh).L(xrc - t / 2, ryO)
|
||||
b.L(xrc + t / 2, yh)
|
||||
.L(xrc + hw, yh)
|
||||
.L(xrc, h)
|
||||
.L(xrc - hw, yh)
|
||||
.L(xrc - t / 2, yh)
|
||||
.L(xrc - t / 2, ryO)
|
||||
b.arc(rxO, ryO, Math.max(rxO - t, 0), Math.max(ryO - t, ryO * 0.2), 0, -180)
|
||||
b.L(t, h).Z()
|
||||
return { path: b.d() }
|
||||
@@ -798,7 +1214,9 @@ export function presetPath(
|
||||
const b = new PathB().M(0, 0)
|
||||
b.arc(0, cy, w, cy, 270, 90)
|
||||
const bi = Math.max(w - 1.5 * t, 0)
|
||||
b.L((w + bi) / 2, Math.min(h, cy + 1.2 * t)).L(bi, cy).L(w - t, cy)
|
||||
b.L((w + bi) / 2, Math.min(h, cy + 1.2 * t))
|
||||
.L(bi, cy)
|
||||
.L(w - t, cy)
|
||||
b.arc(0, cy, Math.max(w - t, 0), Math.max(cy - t, 0), 0, -90)
|
||||
b.L(0, 0).Z()
|
||||
return { path: b.d() }
|
||||
@@ -812,8 +1230,16 @@ export function presetPath(
|
||||
const bs = (ss * 5) / 32
|
||||
const b = new PathB()
|
||||
b.M(bs, y1).L(xh, y1).L(xh, 0).L(w, cy).L(xh, h).L(xh, y2).L(bs, y2).Z()
|
||||
b.M(0, y1).L(ss / 32, y1).L(ss / 32, y2).L(0, y2).Z()
|
||||
b.M(ss / 16, y1).L(ss / 8, y1).L(ss / 8, y2).L(ss / 16, y2).Z()
|
||||
b.M(0, y1)
|
||||
.L(ss / 32, y1)
|
||||
.L(ss / 32, y2)
|
||||
.L(0, y2)
|
||||
.Z()
|
||||
b.M(ss / 16, y1)
|
||||
.L(ss / 8, y1)
|
||||
.L(ss / 8, y2)
|
||||
.L(ss / 16, y2)
|
||||
.Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'wedgeRoundRectCallout': {
|
||||
@@ -821,8 +1247,14 @@ export function presetPath(
|
||||
const tipX = cx + w * adjRaw(adjust, 'adj1', -20833)
|
||||
const tipY = cy + h * adjRaw(adjust, 'adj2', 62500)
|
||||
const b = new PathB()
|
||||
b.M(r, 0).L(w - r, 0).arc(w - r, r, r, r, 270, 90).L(w, h - r)
|
||||
b.arc(w - r, h - r, r, r, 0, 90).L(r, h).arc(r, h - r, r, r, 90, 90).L(0, r)
|
||||
b.M(r, 0)
|
||||
.L(w - r, 0)
|
||||
.arc(w - r, r, r, r, 270, 90)
|
||||
.L(w, h - r)
|
||||
b.arc(w - r, h - r, r, r, 0, 90)
|
||||
.L(r, h)
|
||||
.arc(r, h - r, r, r, 90, 90)
|
||||
.L(0, r)
|
||||
b.arc(r, r, r, r, 180, 90).Z()
|
||||
appendWedgeTail(b, w, h, tipX, tipY)
|
||||
return { path: b.d() }
|
||||
@@ -841,28 +1273,50 @@ export function presetPath(
|
||||
}
|
||||
case 'flowChartPredefinedProcess': {
|
||||
const path = new PathB().M(0, 0).L(w, 0).L(w, h).L(0, h).Z().d()
|
||||
const lines = new PathB().M(w / 8, 0).L(w / 8, h).M((7 * w) / 8, 0).L((7 * w) / 8, h).d()
|
||||
const lines = new PathB()
|
||||
.M(w / 8, 0)
|
||||
.L(w / 8, h)
|
||||
.M((7 * w) / 8, 0)
|
||||
.L((7 * w) / 8, h)
|
||||
.d()
|
||||
return { path, strokePath: lines }
|
||||
}
|
||||
case 'flowChartInternalStorage': {
|
||||
const path = new PathB().M(0, 0).L(w, 0).L(w, h).L(0, h).Z().d()
|
||||
const lines = new PathB().M(w / 8, 0).L(w / 8, h).M(0, h / 8).L(w, h / 8).d()
|
||||
const lines = new PathB()
|
||||
.M(w / 8, 0)
|
||||
.L(w / 8, h)
|
||||
.M(0, h / 8)
|
||||
.L(w, h / 8)
|
||||
.d()
|
||||
return { path, strokePath: lines }
|
||||
}
|
||||
case 'flowChartDocument': {
|
||||
const b = new PathB().M(0, 0).L(w, 0).L(w, 0.83 * h)
|
||||
const b = new PathB()
|
||||
.M(0, 0)
|
||||
.L(w, 0)
|
||||
.L(w, 0.83 * h)
|
||||
b.C(0.75 * w, 0.72 * h, 0.58 * w, 0.72 * h, 0.5 * w, 0.83 * h)
|
||||
b.C(0.42 * w, 0.94 * h, 0.25 * w, 0.94 * h, 0, 0.83 * h)
|
||||
return { path: b.Z().d() }
|
||||
}
|
||||
case 'flowChartMultidocument': {
|
||||
const b = new PathB().M(0, 0.12 * h).L(0.88 * w, 0.12 * h).L(0.88 * w, 0.85 * h)
|
||||
const b = new PathB()
|
||||
.M(0, 0.12 * h)
|
||||
.L(0.88 * w, 0.12 * h)
|
||||
.L(0.88 * w, 0.85 * h)
|
||||
b.C(0.66 * w, 0.74 * h, 0.51 * w, 0.74 * h, 0.44 * w, 0.85 * h)
|
||||
b.C(0.37 * w, 0.96 * h, 0.22 * w, 0.96 * h, 0, 0.85 * h)
|
||||
b.Z()
|
||||
const backs = new PathB()
|
||||
.M(0.06 * w, 0.12 * h).L(0.06 * w, 0.06 * h).L(0.94 * w, 0.06 * h).L(0.94 * w, 0.6 * h)
|
||||
.M(0.12 * w, 0.06 * h).L(0.12 * w, 0).L(w, 0).L(w, 0.53 * h)
|
||||
.M(0.06 * w, 0.12 * h)
|
||||
.L(0.06 * w, 0.06 * h)
|
||||
.L(0.94 * w, 0.06 * h)
|
||||
.L(0.94 * w, 0.6 * h)
|
||||
.M(0.12 * w, 0.06 * h)
|
||||
.L(0.12 * w, 0)
|
||||
.L(w, 0)
|
||||
.L(w, 0.53 * h)
|
||||
return { path: b.d(), strokePath: backs.d() }
|
||||
}
|
||||
case 'flowChartConnector': {
|
||||
@@ -882,7 +1336,11 @@ export function presetPath(
|
||||
const dx = cx * Math.SQRT1_2
|
||||
const dy = cy * Math.SQRT1_2
|
||||
const lines = new PathB()
|
||||
.M(cx - dx, cy - dy).L(cx + dx, cy + dy).M(cx + dx, cy - dy).L(cx - dx, cy + dy).d()
|
||||
.M(cx - dx, cy - dy)
|
||||
.L(cx + dx, cy + dy)
|
||||
.M(cx + dx, cy - dy)
|
||||
.L(cx - dx, cy + dy)
|
||||
.d()
|
||||
return { path: b.d(), strokePath: lines }
|
||||
}
|
||||
case 'flowChartSort': {
|
||||
@@ -895,8 +1353,13 @@ export function presetPath(
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'flowChartDisplay': {
|
||||
const b = new PathB().M(0, cy).L(w / 6, 0).L((5 * w) / 6, 0)
|
||||
b.arc((5 * w) / 6, cy, w / 6, cy, 270, 180).L(w / 6, h).Z()
|
||||
const b = new PathB()
|
||||
.M(0, cy)
|
||||
.L(w / 6, 0)
|
||||
.L((5 * w) / 6, 0)
|
||||
b.arc((5 * w) / 6, cy, w / 6, cy, 270, 180)
|
||||
.L(w / 6, h)
|
||||
.Z()
|
||||
return { path: b.d() }
|
||||
}
|
||||
case 'flowChartPunchedTape': {
|
||||
@@ -910,12 +1373,18 @@ export function presetPath(
|
||||
}
|
||||
case 'leftBracket': {
|
||||
const r = Math.min(h / 2, ss * frac('adj', 8333))
|
||||
const b = new PathB().arc(w, r, w, r, 270, -90, 'M').L(0, h - r).arc(w, h - r, w, r, 180, -90)
|
||||
const b = new PathB()
|
||||
.arc(w, r, w, r, 270, -90, 'M')
|
||||
.L(0, h - r)
|
||||
.arc(w, h - r, w, r, 180, -90)
|
||||
return { strokePath: b.d() }
|
||||
}
|
||||
case 'rightBracket': {
|
||||
const r = Math.min(h / 2, ss * frac('adj', 8333))
|
||||
const b = new PathB().arc(0, r, w, r, 270, 90, 'M').L(w, h - r).arc(0, h - r, w, r, 0, 90)
|
||||
const b = new PathB()
|
||||
.arc(0, r, w, r, 270, 90, 'M')
|
||||
.L(w, h - r)
|
||||
.arc(0, h - r, w, r, 0, 90)
|
||||
return { strokePath: b.d() }
|
||||
}
|
||||
case 'leftBrace': {
|
||||
@@ -923,7 +1392,9 @@ export function presetPath(
|
||||
const mid = h * frac('adj2', 50000)
|
||||
const xm = w / 2
|
||||
const b = new PathB().arc(w, r, xm, r, 270, -90, 'M').L(xm, mid - r)
|
||||
b.arc(0, mid - r, xm, r, 0, 90).arc(0, mid + r, xm, r, 270, 90).L(xm, h - r)
|
||||
b.arc(0, mid - r, xm, r, 0, 90)
|
||||
.arc(0, mid + r, xm, r, 270, 90)
|
||||
.L(xm, h - r)
|
||||
b.arc(w, h - r, xm, r, 180, -90)
|
||||
return { strokePath: b.d() }
|
||||
}
|
||||
@@ -932,7 +1403,9 @@ export function presetPath(
|
||||
const mid = h * frac('adj2', 50000)
|
||||
const xm = w / 2
|
||||
const b = new PathB().arc(0, r, xm, r, 270, 90, 'M').L(xm, mid - r)
|
||||
b.arc(w, mid - r, xm, r, 180, -90).arc(w, mid + r, xm, r, 270, -90).L(xm, h - r)
|
||||
b.arc(w, mid - r, xm, r, 180, -90)
|
||||
.arc(w, mid + r, xm, r, 270, -90)
|
||||
.L(xm, h - r)
|
||||
b.arc(0, h - r, xm, r, 0, 90)
|
||||
return { strokePath: b.d() }
|
||||
}
|
||||
@@ -950,11 +1423,16 @@ function appendWedgeTail(b: PathB, w: number, h: number, tipX: number, tipY: num
|
||||
if (Math.abs(ny) >= Math.abs(nx)) {
|
||||
const bx = clamp(tipX, 2 * g, w - 2 * g)
|
||||
const ey = ny >= 0 ? h : 0
|
||||
b.M(bx - g, ey).L(tipX, tipY).L(bx + g, ey).Z()
|
||||
b.M(bx - g, ey)
|
||||
.L(tipX, tipY)
|
||||
.L(bx + g, ey)
|
||||
.Z()
|
||||
} else {
|
||||
const by = clamp(tipY, 2 * g, h - 2 * g)
|
||||
const ex = nx >= 0 ? w : 0
|
||||
b.M(ex, by - g).L(tipX, tipY).L(ex, by + g).Z()
|
||||
b.M(ex, by - g)
|
||||
.L(tipX, tipY)
|
||||
.L(ex, by + g)
|
||||
.Z()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,21 @@ export type RenderFill =
|
||||
/** Radial focus center as width/height fractions (from <a:fillToRect>; default 0.5/0.5) */
|
||||
center?: { x: number; y: number }
|
||||
}
|
||||
| { kind: 'image'; dataUrl?: string; mode: 'stretch' | 'tile' }
|
||||
| {
|
||||
kind: 'image'
|
||||
dataUrl?: string
|
||||
mode: 'stretch' | 'tile'
|
||||
/** Translucent picture fill (alphaModFix, 0-1) */
|
||||
alpha?: number
|
||||
/** Image maps into this inset subrect of the shape (stretch fillRect, fractions) */
|
||||
fillRect?: { l: number; t: number; r: number; b: number }
|
||||
/** [dark, light] duotone colors mapped over image luminance */
|
||||
duotone?: [string, string]
|
||||
/** clrChange: pixels matching `from` become `to` (#RRGGBB or #RRGGBBAA) */
|
||||
clrChange?: { from: string; to: string }
|
||||
/** Tile grid: scale in px-per-image-px, anchor offsets in px, and the algn anchor */
|
||||
tile?: { scaleX: number; scaleY: number; txPx: number; tyPx: number; algn: string }
|
||||
}
|
||||
|
||||
export interface RenderStroke {
|
||||
color: string
|
||||
@@ -107,6 +121,8 @@ export interface GlyphRun {
|
||||
letterSpacingPx?: number
|
||||
/** Text outline (<a:rPr><a:ln>, commonly used by WordArt) */
|
||||
outline?: { color: string; widthPx: number }
|
||||
/** Run outer shadow (px), drawn via canvas shadow props */
|
||||
shadow?: { color: string; blurPx: number; offsetX: number; offsetY: number }
|
||||
/** Extra per-char spacing spread in by justify alignment (px); draw-only, the editor ignores it and doesn't store it */
|
||||
justifyExtraPx?: number
|
||||
/** Super/subscript baseline shift (px, positive = up; <a:rPr baseline>), already baked into baselineY */
|
||||
@@ -131,6 +147,9 @@ export interface TextLine {
|
||||
/** Line top y (px relative to the text box top-left) */
|
||||
top: number
|
||||
height: number
|
||||
/** Legacy: line advance when it exceeded height (external leading). The 1.2em
|
||||
* PowerPoint line model folds everything into height; kept for stored decks. */
|
||||
advance?: number
|
||||
/** This line starts a model paragraph (false/absent = auto-wrap continuation of the previous one; the editor splits paragraphs on it) */
|
||||
paraStart?: boolean
|
||||
/** Trailing whitespace swallowed when wrapping (the editor re-adds a space when joining lines; hard breaks/CJK wrapping don't set it) */
|
||||
@@ -145,6 +164,9 @@ export interface TextLine {
|
||||
marLPx?: number
|
||||
/** First-line indent in px (editor display; only applies when the paragraph has no bullet) */
|
||||
indentPx?: number
|
||||
/** Baseline offset minus the line's ascent, signed (canvas baseline = top + leadAbove + ascent;
|
||||
* positive when the line box is taller than the glyphs, negative when shorter — the editor overlay compensates) */
|
||||
leadAbove?: number
|
||||
}
|
||||
|
||||
export interface RenderTextLayout {
|
||||
@@ -159,6 +181,8 @@ export interface RenderTextLayout {
|
||||
lnSpcReduction?: number
|
||||
/** Total content height after layout (px), used for vertical alignment positioning */
|
||||
contentHeight: number
|
||||
/** Ink bottom (last baseline + descent, px): PowerPoint's basis for auto table row heights */
|
||||
inkBottom?: number
|
||||
/** bodyPr wrap (false = no wrapping, overflows the box; the editor also doesn't wrap) */
|
||||
wrap: boolean
|
||||
/** bodyPr vert: vertical column layout (lines = columns, right→left); vert/vert270/wordArtVert degrade to eaVert */
|
||||
@@ -199,6 +223,8 @@ export interface ShapeRenderNode extends RenderNodeBase {
|
||||
tailEnd?: ArrowEndRender
|
||||
}
|
||||
fill: RenderFill
|
||||
/** <a:fillOverlay>: second fill drawn over the base with multiply blending */
|
||||
fillOverlay?: RenderFill
|
||||
stroke?: RenderStroke
|
||||
shadow?: RenderShadow
|
||||
glow?: RenderGlow
|
||||
@@ -208,6 +234,14 @@ export interface ShapeRenderNode extends RenderNodeBase {
|
||||
export interface PictureRenderNode extends RenderNodeBase {
|
||||
type: 'picture'
|
||||
dataUrl?: string
|
||||
/** Opaque backdrop behind the image (OLE previews render on a white canvas; metafiles are often transparent) */
|
||||
bgColor?: string
|
||||
/** Shape fill from the pic's own spPr, always drawn behind the (possibly translucent) image */
|
||||
fill?: RenderFill
|
||||
/** [dark, light] duotone colors applied to the picture pixels */
|
||||
duotone?: [string, string]
|
||||
/** clrChange applied to the picture pixels before duotone */
|
||||
clrChange?: { from: string; to: string }
|
||||
/** Picture shape-geometry clip (picture styles): three channels matching shape geometry; clip when any is set */
|
||||
clip?: { cornerRadiusPx?: number; polygonPoints?: number[]; pathData?: string }
|
||||
/** Source image crop ratios (0..1, how much each side is cropped) */
|
||||
@@ -267,6 +301,8 @@ export interface TableCellRender {
|
||||
export interface TableRenderNode extends RenderNodeBase {
|
||||
type: 'table'
|
||||
cells: TableCellRender[]
|
||||
/** Table-style <a:tblBg>: drawn under the cells (alpha band fills composite over it) */
|
||||
bgFill?: TableCellRender['fill']
|
||||
/** Grid line offsets relative to the box (px): gridX has nCols+1 entries, gridY nRows+1 */
|
||||
gridX: number[]
|
||||
gridY: number[]
|
||||
@@ -319,6 +355,18 @@ export interface ChartRenderNode extends RenderNodeBase {
|
||||
styleInfo?: ChartStyleInfo
|
||||
/** Whole-chart background (chartSpace spPr, e.g. picture fill) drawn under all primitives */
|
||||
bgFill?: RenderFill
|
||||
/** Whole-chart frame border (chartSpace spPr ln) drawn over all primitives */
|
||||
border?: { color: string; widthPx: number }
|
||||
/** Plot-area fill/border rectangle, drawn under the gridlines */
|
||||
plotRect?: {
|
||||
x: number
|
||||
y: number
|
||||
w: number
|
||||
h: number
|
||||
fill?: RenderFill
|
||||
borderColor?: string
|
||||
borderWidthPx?: number
|
||||
}
|
||||
/** Gridlines / axis lines */
|
||||
gridLines: Array<{
|
||||
x1: number
|
||||
@@ -327,6 +375,7 @@ export interface ChartRenderNode extends RenderNodeBase {
|
||||
y2: number
|
||||
color: string
|
||||
dash?: number[]
|
||||
widthPx?: number
|
||||
}>
|
||||
axisLines: Array<{
|
||||
x1: number
|
||||
@@ -348,6 +397,7 @@ export interface ChartRenderNode extends RenderNodeBase {
|
||||
smooth?: boolean
|
||||
closed?: boolean
|
||||
fill?: string
|
||||
dash?: number[]
|
||||
}>
|
||||
/** Data-point markers (circles) */
|
||||
markers: Array<{ x: number; y: number; r: number; color: string }>
|
||||
@@ -381,6 +431,10 @@ export interface RenderSlide {
|
||||
/** Viewport scale (fitWidthPx / slide baseline px width) — geometry coordinates already include it */
|
||||
scale: number
|
||||
background: RenderFill
|
||||
/** The slide carries its own <p:bg> override (enables "reset background") */
|
||||
bgOwn?: boolean
|
||||
/** <p:sld showMasterSp="0">: master/layout background graphics hidden on this slide */
|
||||
bgGraphicsHidden?: boolean
|
||||
nodes: RenderNode[]
|
||||
/** Hidden slide (<p:sld show="0">): thumbnails get a badge, skipped during presentation */
|
||||
hidden?: boolean
|
||||
|
||||
@@ -58,10 +58,24 @@ interface Token {
|
||||
blShift?: number
|
||||
/** Text outline (<a:rPr><a:ln>, commonly used by WordArt) */
|
||||
outline?: { color: string; widthPx: number }
|
||||
/** Run outer shadow (px, offset resolved from dist/dir) */
|
||||
shadow?: { color: string; blurPx: number; offsetX: number; offsetY: number }
|
||||
/** Run hyperlink (TextRun.hyperlink encoding) */
|
||||
link?: string
|
||||
}
|
||||
|
||||
/** Alpha of a #RRGGBB(AA) color as 0..1 (1 when opaque or unset). */
|
||||
function hexAlpha(hex: string | undefined): number {
|
||||
return hex && hex.length === 9 ? parseInt(hex.slice(7), 16) / 255 : 1
|
||||
}
|
||||
|
||||
/** Multiply a #RRGGBB(AA) color's alpha by factor → #RRGGBBAA. */
|
||||
function scaleHexAlpha(hex: string, factor: number): string {
|
||||
if (factor >= 1) return hex
|
||||
const a = Math.round(hexAlpha(hex) * factor * 255)
|
||||
return hex.slice(0, 7) + Math.max(0, Math.min(255, a)).toString(16).padStart(2, '0').toUpperCase()
|
||||
}
|
||||
|
||||
function runStyle(run: TextRun, scale: number, fontScale: number): RunStyle {
|
||||
const sizePt = run.fontSize ?? DEFAULT_SIZE_PT
|
||||
return {
|
||||
@@ -92,6 +106,158 @@ function applyCap(text: string, cap: string | undefined): string {
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* Adobe Symbol encoding → Unicode (the differing positions; digits/punctuation are identity).
|
||||
* Symbol-font runs store legacy byte codes ('m' = μ): PowerPoint draws them through the
|
||||
* font's symbol cmap, but macOS's Symbol.ttf (and every fallback) is Unicode-encoded, so both
|
||||
* measuring and drawing need the real code points. Exotic bracket-piece glyphs stay unmapped.
|
||||
*/
|
||||
const SYMBOL_TO_UNICODE: Record<number, number> = {
|
||||
0x22: 0x2200,
|
||||
0x24: 0x2203,
|
||||
0x27: 0x220b,
|
||||
0x2a: 0x2217,
|
||||
0x2d: 0x2212,
|
||||
0x40: 0x2245,
|
||||
0x41: 0x0391,
|
||||
0x42: 0x0392,
|
||||
0x43: 0x03a7,
|
||||
0x44: 0x0394,
|
||||
0x45: 0x0395,
|
||||
0x46: 0x03a6,
|
||||
0x47: 0x0393,
|
||||
0x48: 0x0397,
|
||||
0x49: 0x0399,
|
||||
0x4a: 0x03d1,
|
||||
0x4b: 0x039a,
|
||||
0x4c: 0x039b,
|
||||
0x4d: 0x039c,
|
||||
0x4e: 0x039d,
|
||||
0x4f: 0x039f,
|
||||
0x50: 0x03a0,
|
||||
0x51: 0x0398,
|
||||
0x52: 0x03a1,
|
||||
0x53: 0x03a3,
|
||||
0x54: 0x03a4,
|
||||
0x55: 0x03a5,
|
||||
0x56: 0x03c2,
|
||||
0x57: 0x03a9,
|
||||
0x58: 0x039e,
|
||||
0x59: 0x03a8,
|
||||
0x5a: 0x0396,
|
||||
0x5c: 0x2234,
|
||||
0x5e: 0x22a5,
|
||||
0x60: 0x203e,
|
||||
0x61: 0x03b1,
|
||||
0x62: 0x03b2,
|
||||
0x63: 0x03c7,
|
||||
0x64: 0x03b4,
|
||||
0x65: 0x03b5,
|
||||
0x66: 0x03c6,
|
||||
0x67: 0x03b3,
|
||||
0x68: 0x03b7,
|
||||
0x69: 0x03b9,
|
||||
0x6a: 0x03d5,
|
||||
0x6b: 0x03ba,
|
||||
0x6c: 0x03bb,
|
||||
0x6d: 0x03bc,
|
||||
0x6e: 0x03bd,
|
||||
0x6f: 0x03bf,
|
||||
0x70: 0x03c0,
|
||||
0x71: 0x03b8,
|
||||
0x72: 0x03c1,
|
||||
0x73: 0x03c3,
|
||||
0x74: 0x03c4,
|
||||
0x75: 0x03c5,
|
||||
0x76: 0x03d6,
|
||||
0x77: 0x03c9,
|
||||
0x78: 0x03be,
|
||||
0x79: 0x03c8,
|
||||
0x7a: 0x03b6,
|
||||
0x7e: 0x223c,
|
||||
0xa1: 0x03d2,
|
||||
0xa2: 0x2032,
|
||||
0xa3: 0x2264,
|
||||
0xa4: 0x2044,
|
||||
0xa5: 0x221e,
|
||||
0xa6: 0x0192,
|
||||
0xa7: 0x2663,
|
||||
0xa8: 0x2666,
|
||||
0xa9: 0x2665,
|
||||
0xaa: 0x2660,
|
||||
0xab: 0x2194,
|
||||
0xac: 0x2190,
|
||||
0xad: 0x2191,
|
||||
0xae: 0x2192,
|
||||
0xaf: 0x2193,
|
||||
0xb0: 0x00b0,
|
||||
0xb1: 0x00b1,
|
||||
0xb2: 0x2033,
|
||||
0xb3: 0x2265,
|
||||
0xb4: 0x00d7,
|
||||
0xb5: 0x221d,
|
||||
0xb6: 0x2202,
|
||||
0xb7: 0x2022,
|
||||
0xb8: 0x00f7,
|
||||
0xb9: 0x2260,
|
||||
0xba: 0x2261,
|
||||
0xbb: 0x2248,
|
||||
0xbc: 0x2026,
|
||||
0xbf: 0x21b5,
|
||||
0xc0: 0x2135,
|
||||
0xc1: 0x2111,
|
||||
0xc2: 0x211c,
|
||||
0xc3: 0x2118,
|
||||
0xc4: 0x2297,
|
||||
0xc5: 0x2295,
|
||||
0xc6: 0x2205,
|
||||
0xc7: 0x2229,
|
||||
0xc8: 0x222a,
|
||||
0xc9: 0x2283,
|
||||
0xca: 0x2287,
|
||||
0xcb: 0x2284,
|
||||
0xcc: 0x2282,
|
||||
0xcd: 0x2286,
|
||||
0xce: 0x2208,
|
||||
0xcf: 0x2209,
|
||||
0xd0: 0x2220,
|
||||
0xd1: 0x2207,
|
||||
0xd2: 0x00ae,
|
||||
0xd3: 0x00a9,
|
||||
0xd4: 0x2122,
|
||||
0xd5: 0x220f,
|
||||
0xd6: 0x221a,
|
||||
0xd7: 0x22c5,
|
||||
0xd8: 0x00ac,
|
||||
0xd9: 0x2227,
|
||||
0xda: 0x2228,
|
||||
0xdb: 0x21d4,
|
||||
0xdc: 0x21d0,
|
||||
0xdd: 0x21d1,
|
||||
0xde: 0x21d2,
|
||||
0xdf: 0x21d3,
|
||||
0xe0: 0x25ca,
|
||||
0xe1: 0x2329,
|
||||
0xe5: 0x2211,
|
||||
0xf1: 0x232a,
|
||||
0xf2: 0x222b,
|
||||
}
|
||||
const SYMBOL_FONT_RE = /^symbol$/i
|
||||
/** Translate a Symbol-encoded run to Unicode (accepts both raw bytes and F0xx PUA storage). */
|
||||
function symbolRunText(text: string): string {
|
||||
let out = ''
|
||||
for (const ch of text) {
|
||||
let cp = ch.codePointAt(0) ?? 0
|
||||
if (cp >= 0xf000 && cp <= 0xf0ff) cp -= 0xf000
|
||||
// Unmapped positions: ASCII identity slots (digits/punctuation) use the stripped code
|
||||
// even when stored as PUA; high unmapped slots (bracket pieces) keep the original char
|
||||
// (stripping would fabricate Latin-1 letters like U+00E6).
|
||||
const fallback = cp >= 0x20 && cp <= 0x7f ? cp : (ch.codePointAt(0) ?? 0)
|
||||
out += String.fromCodePoint(SYMBOL_TO_UNICODE[cp] ?? fallback)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** Splits a paragraph's runs into a breakable token stream (whitespace / single CJK chars / Latin words). */
|
||||
function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Token[] {
|
||||
const tokens: Token[] = []
|
||||
@@ -122,6 +288,24 @@ function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Toke
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(run.shadow
|
||||
? {
|
||||
shadow: {
|
||||
// A translucent fill dims its own shadow (PowerPoint-observed): scale the
|
||||
// shadow alpha by the text fill alpha
|
||||
color: scaleHexAlpha(run.shadow.color, hexAlpha(run.color)),
|
||||
blurPx: emuToPx(run.shadow.blurRad, scale) * fontScale,
|
||||
offsetX:
|
||||
emuToPx(run.shadow.dist, scale) *
|
||||
fontScale *
|
||||
Math.cos((run.shadow.dirDeg * Math.PI) / 180),
|
||||
offsetY:
|
||||
emuToPx(run.shadow.dist, scale) *
|
||||
fontScale *
|
||||
Math.sin((run.shadow.dirDeg * Math.PI) / 180),
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
let buf = ''
|
||||
const flushWord = () => {
|
||||
@@ -136,8 +320,12 @@ function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Toke
|
||||
}
|
||||
buf = ''
|
||||
}
|
||||
// Symbol-encoded runs become Unicode before cap/segmentation (cap would uppercase μ→Μ)
|
||||
const runText = SYMBOL_FONT_RE.test(style.fontFamily)
|
||||
? symbolRunText(run.text)
|
||||
: applyCap(run.text, run.cap)
|
||||
// Per grapheme cluster: combining marks / ZWJ emoji always share a token with their base char, never split by wrapping
|
||||
for (const ch of graphemes(applyCap(run.text, run.cap))) {
|
||||
for (const ch of graphemes(runText)) {
|
||||
const cp = ch.codePointAt(0) ?? 0
|
||||
if (ch === '\n' || ch === '\v') {
|
||||
flushWord()
|
||||
@@ -145,6 +333,11 @@ function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Toke
|
||||
} else if (ch === ' ' || ch === '\t') {
|
||||
flushWord()
|
||||
tokens.push({ ...base, text: ch, breakable: true, isSpace: true })
|
||||
} else if (ch === ' ') {
|
||||
// NBSP: glues neighbors into one token but draws/measures at plain-space
|
||||
// width (fonts like Carlito have no U+00A0 glyph → the missing-glyph
|
||||
// fallback would badly over-measure it)
|
||||
buf += ' '
|
||||
} else if (isWideChar(cp)) {
|
||||
flushWord()
|
||||
tokens.push({ ...base, text: ch, breakable: true, isSpace: false })
|
||||
@@ -157,6 +350,19 @@ function tokenizeParagraph(p: Paragraph, scale: number, fontScale: number): Toke
|
||||
return tokens
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy symbol-font bullets (<a:buFont> Wingdings/Webdings): the glyph must be drawn
|
||||
* with that font, and ASCII bullet codes normalized into the font's F0xx PUA range
|
||||
* (files carry either encoding; the shipped fonts map the PUA).
|
||||
*/
|
||||
const SYMBOL_BULLET_RE = /^(wingdings|webdings)/i
|
||||
function symbolBulletText(font: string | undefined, char: string): string | undefined {
|
||||
if (!font || !SYMBOL_BULLET_RE.test(font)) return undefined
|
||||
const cp = char.codePointAt(0) ?? 0
|
||||
if (cp >= 0xf000 && cp <= 0xf0ff) return char
|
||||
return cp >= 0x20 && cp <= 0xff ? String.fromCodePoint(0xf000 + cp) : char
|
||||
}
|
||||
|
||||
/** Southeast Asian scripts without spaces (Thai/Burmese/Khmer/Lao); word boundaries need ICU dictionary segmentation. */
|
||||
const SEA_RE = /[-က-႟ក-]/
|
||||
const WORD_SEG: Intl.Segmenter | null =
|
||||
@@ -246,6 +452,10 @@ interface LaidLine {
|
||||
descent: number
|
||||
/** Single line height (without lnSpc multiplier/reduction; the basis for spcPct paragraph spacing) */
|
||||
singleH: number
|
||||
/** Baseline offset minus the line's max ascent (signed): baseline = top + leadAbove + ascent.
|
||||
* Positive when the box is taller than the glyphs (lnSpc>100%), negative when shorter (lnSpc<100%
|
||||
* or fonts whose ascent+descent exceed the 1.2em box). */
|
||||
leadAbove?: number
|
||||
/** Trailing whitespace was swallowed when wrapping (tells the editor to re-add a space when joining lines) */
|
||||
trailingSpace?: boolean
|
||||
/** The line ends at a soft-break sentinel; value = the sentinel run's model index */
|
||||
@@ -294,6 +504,8 @@ function layoutParagraph(
|
||||
italic: false,
|
||||
}
|
||||
const m = metrics.metrics(st)
|
||||
const box = lineH(p, st.fontSizePx, scale, lnSpcRed)
|
||||
const leadAbove = baselineOff(p, box, m.descent) - m.ascent
|
||||
lines.push({
|
||||
runs: src
|
||||
? [
|
||||
@@ -314,10 +526,11 @@ function layoutParagraph(
|
||||
},
|
||||
]
|
||||
: [],
|
||||
height: lineH(p, m, scale, lnSpcRed),
|
||||
height: box,
|
||||
...(leadAbove ? { leadAbove } : {}),
|
||||
ascent: m.ascent,
|
||||
descent: m.descent,
|
||||
singleH: Math.max(m.lineHeight, m.ascent + m.descent),
|
||||
singleH: PPT_SINGLE * st.fontSizePx,
|
||||
...(trailingSpace ? { trailingSpace } : {}),
|
||||
})
|
||||
return
|
||||
@@ -327,6 +540,7 @@ function layoutParagraph(
|
||||
lines.push(line)
|
||||
}
|
||||
|
||||
let endedWithBreak = false
|
||||
for (const tok of tokens) {
|
||||
// <a:br/> forced break: breaks regardless of wrap; record the sentinel run index for editor round-trips
|
||||
if (tok.isBreak) {
|
||||
@@ -334,8 +548,10 @@ function layoutParagraph(
|
||||
lines[lines.length - 1]!.softBreakAfter = tok.srcRun
|
||||
cur = []
|
||||
curW = 0
|
||||
endedWithBreak = true
|
||||
continue
|
||||
}
|
||||
endedWithBreak = false
|
||||
const w = tokenWidth(tok, metrics)
|
||||
// The first line loses firstLineShrinkPx to the overflowing bullet glyph; evaluated
|
||||
// lazily because the soft wrap right below can end line 0 for this same token
|
||||
@@ -356,6 +572,8 @@ function layoutParagraph(
|
||||
curW += w
|
||||
}
|
||||
if (cur.length) pushLine(cur)
|
||||
// A trailing <a:br/> shows an empty last line (PowerPoint counts it when anchoring)
|
||||
else if (endedWithBreak) pushLine([])
|
||||
if (!lines.length) pushLine([])
|
||||
return lines
|
||||
}
|
||||
@@ -387,14 +605,14 @@ function buildLine(
|
||||
let x = 0
|
||||
let ascent = 0
|
||||
let descent = 0
|
||||
let lineHM = 0
|
||||
let sizeM = 0
|
||||
const runs: GlyphRun[] = []
|
||||
// Lines with RTL levels are reordered into visual order before laying out x (break in logical order → draw in visual order)
|
||||
for (const tok of visualOrder(toks)) {
|
||||
const m = metrics.metrics(tok.style)
|
||||
ascent = Math.max(ascent, m.ascent)
|
||||
descent = Math.max(descent, m.descent)
|
||||
lineHM = Math.max(lineHM, m.lineHeight)
|
||||
sizeM = Math.max(sizeM, tok.style.fontSizePx)
|
||||
const w = tokenWidth(tok, metrics)
|
||||
runs.push({
|
||||
text: tok.text,
|
||||
@@ -413,6 +631,7 @@ function buildLine(
|
||||
widthPx: w,
|
||||
...(tok.ls ? { letterSpacingPx: tok.ls } : {}),
|
||||
...(tok.outline ? { outline: tok.outline } : {}),
|
||||
...(tok.shadow ? { shadow: tok.shadow } : {}),
|
||||
...(tok.blShift ? { baselineShiftPx: tok.blShift } : {}),
|
||||
...(tok.blPct ? { baselinePct: tok.blPct } : {}),
|
||||
...(tok.level != null && tok.level % 2 === 1 ? { rtl: true } : {}),
|
||||
@@ -423,39 +642,48 @@ function buildLine(
|
||||
})
|
||||
x += w
|
||||
}
|
||||
const height = lineH(
|
||||
p,
|
||||
{ ascent, descent, lineHeight: lineHM || ascent + descent },
|
||||
scale,
|
||||
lnSpcRed,
|
||||
)
|
||||
const box = lineH(p, sizeM, scale, lnSpcRed)
|
||||
const leadAbove = baselineOff(p, box, descent) - ascent
|
||||
return {
|
||||
runs,
|
||||
height,
|
||||
height: box,
|
||||
...(leadAbove ? { leadAbove } : {}),
|
||||
ascent,
|
||||
descent,
|
||||
singleH: Math.max(lineHM || ascent + descent, ascent + descent),
|
||||
singleH: PPT_SINGLE * sizeM,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes line height with the paragraph's line-spacing settings. Single spacing =
|
||||
* the font's suggested line height (opentype: hhea ascent+descent; heuristic: 1.2em)
|
||||
* — with no extra ×1.2 on top (the earlier double scaling made large-size
|
||||
* title spacing ~20% too tall).
|
||||
* PowerPoint line model (mac PPT, 48pt probe deck, 2026-08-16 — font-independent):
|
||||
* - default / explicit 100%: line box = 1.2 × fontSize (regardless of font metrics);
|
||||
* the baseline is BOTTOM-anchored: box − the font's real descent.
|
||||
* - explicit spcPct ≠ 100 (incl. <100) and spcPts: box = pct × 1.2em (or the absolute
|
||||
* points), and the baseline sits at 0.7333 × box (a generic 0.88/0.32 em split scaled
|
||||
* with the box) — real ascent/descent are NOT consulted, so at 70% the ink pokes
|
||||
* above the box top exactly like PowerPoint.
|
||||
* All ten probed fonts (incl. MS PGothic 1.0em and Malgun 1.32em hhea) pace at 1.2em.
|
||||
*/
|
||||
function lineH(
|
||||
p: Paragraph,
|
||||
m: { ascent: number; descent: number; lineHeight: number },
|
||||
scale: number,
|
||||
lnSpcRed = 0,
|
||||
): number {
|
||||
const single = Math.max(m.lineHeight, m.ascent + m.descent)
|
||||
const PPT_SINGLE = 1.2
|
||||
const PPT_BASELINE_FRAC = 0.88 / 1.2
|
||||
|
||||
function lineH(p: Paragraph, sizePx: number, scale: number, lnSpcRed = 0): number {
|
||||
if (p.lineExact != null) return ptToPx(p.lineExact, scale) // exact line spacing is unaffected by autofit spacing reduction
|
||||
const single = PPT_SINGLE * sizePx
|
||||
const base = p.lineHeight != null ? single * (p.lineHeight / 100) : single
|
||||
return base * (1 - lnSpcRed)
|
||||
}
|
||||
|
||||
/** Baseline offset from the line-box top (see the model above). */
|
||||
function baselineOff(p: Paragraph, box: number, descent: number): number {
|
||||
// Explicit spacing (spcPct != 100 and spcPts at every size — 36..120pt probed): the
|
||||
// generic 0.7333 split; only default/100% consults the font's real descent.
|
||||
if (p.lineExact != null || (p.lineHeight != null && p.lineHeight !== 100)) {
|
||||
return PPT_BASELINE_FRAC * box
|
||||
}
|
||||
return box - descent
|
||||
}
|
||||
|
||||
export interface TextLayoutInput {
|
||||
body: TextBody
|
||||
boxWidthPx: number
|
||||
@@ -467,6 +695,44 @@ export interface TextLayoutInput {
|
||||
trimEdgeSpacing?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* bodyPr numCol: distribute a single laid-out line stream into columns. PowerPoint fills
|
||||
* a column to the box height, then continues at the top of the next (no balancing); a line
|
||||
* never splits across the boundary. Lines are rebased to the column top and shifted right
|
||||
* by whole column strides; the last column keeps any overflow.
|
||||
*/
|
||||
function flowIntoColumns(
|
||||
result: { lines: TextLine[]; contentHeight: number },
|
||||
availHeight: number,
|
||||
numCol: number,
|
||||
stride: number,
|
||||
): { lines: TextLine[]; contentHeight: number } {
|
||||
const out: TextLine[] = []
|
||||
let col = 0
|
||||
let colTop = 0 // original-stream top of the current column's first line
|
||||
let maxBottom = 0
|
||||
for (const ln of result.lines) {
|
||||
let top = ln.top - colTop
|
||||
if (col < numCol - 1 && top > 0 && top + ln.height > availHeight) {
|
||||
col += 1
|
||||
colTop = ln.top
|
||||
top = 0
|
||||
}
|
||||
const dx = col * stride
|
||||
const dy = top - ln.top
|
||||
out.push({
|
||||
...ln,
|
||||
top,
|
||||
runs:
|
||||
dx || dy
|
||||
? ln.runs.map((r) => ({ ...r, x: r.x + dx, baselineY: r.baselineY + dy }))
|
||||
: ln.runs,
|
||||
})
|
||||
maxBottom = Math.max(maxBottom, top + ln.height)
|
||||
}
|
||||
return { lines: out, contentHeight: maxBottom }
|
||||
}
|
||||
|
||||
/** Main entry: TextBody → RenderTextLayout (including autofit shrink stepping). */
|
||||
export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
const { body, boxWidthPx, boxHeightPx, metrics, vp } = input
|
||||
@@ -492,8 +758,14 @@ export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
vp.scale,
|
||||
)
|
||||
|
||||
// bodyPr numCol: paragraphs wrap at the column width and fill column after column
|
||||
const numCol = body.numCol && body.numCol > 1 ? Math.floor(body.numCol) : 1
|
||||
const colGapPx = numCol > 1 ? emuToPx(body.spcCol ?? 0, vp.scale) : 0
|
||||
const colWidth =
|
||||
numCol > 1 ? Math.max((availWidth - (numCol - 1) * colGapPx) / numCol, 1) : availWidth
|
||||
|
||||
const build = (fontScale: number, lnSpcRed: number) =>
|
||||
layoutAll(body, availWidth, wrap, metrics, vp.scale, fontScale, lnSpcRed, input.trimEdgeSpacing)
|
||||
layoutAll(body, colWidth, wrap, metrics, vp.scale, fontScale, lnSpcRed, input.trimEdgeSpacing)
|
||||
|
||||
// PowerPoint's stored shrink ratio takes priority (files with shrunk text render
|
||||
// as-is; we don't scale back up per our own metrics). Only if content still
|
||||
@@ -504,18 +776,23 @@ export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
let fontScale = storedScale
|
||||
let lnSpcReduction = storedRed
|
||||
let result = build(fontScale, lnSpcReduction)
|
||||
// PowerPoint's autofit ignores trailing blank paragraphs (files where the stored
|
||||
// ratio only fits when they are excluded), so overflow is judged by the bottom of
|
||||
// the last non-blank line — plus 3% slack for residual metric drift, since
|
||||
// PowerPoint renders the authored scale as-is.
|
||||
const fitBottom = (r: { lines: TextLine[]; contentHeight: number }): number => {
|
||||
for (let i = r.lines.length - 1; i >= 0; i--) {
|
||||
const ln = r.lines[i]!
|
||||
if (ln.runs.some((run) => run.text.trim())) return ln.top + ln.height
|
||||
// PowerPoint's autofit ignores blank lines on both ends (files where the stored
|
||||
// ratio only fits when they are excluded), so overflow is judged by the span from
|
||||
// the first to the last non-blank line — plus 3% slack for residual metric drift,
|
||||
// since PowerPoint renders the authored scale as-is.
|
||||
const fitSpan = (r: { lines: TextLine[]; contentHeight: number }): number => {
|
||||
let top: number | undefined
|
||||
let bottom: number | undefined
|
||||
for (const ln of r.lines) {
|
||||
if (!ln.runs.some((run) => run.text.trim())) continue
|
||||
top ??= ln.top
|
||||
bottom = ln.top + ln.height
|
||||
}
|
||||
return r.contentHeight
|
||||
return bottom === undefined ? r.contentHeight : bottom - (top ?? 0)
|
||||
}
|
||||
if (body.autofit === 'shrink' && fitBottom(result) > availHeight * 1.03) {
|
||||
// With columns, the single-stream span may spread over numCol columns of availHeight
|
||||
const fitTarget = availHeight * numCol
|
||||
if (body.autofit === 'shrink' && fitSpan(result) > fitTarget * 1.03) {
|
||||
for (const [fs, red] of SHRINK_STEPS) {
|
||||
if (fs >= storedScale - 1e-6) continue
|
||||
const effRed = Math.max(red, storedRed)
|
||||
@@ -523,16 +800,20 @@ export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
fontScale = fs
|
||||
lnSpcReduction = effRed
|
||||
result = r
|
||||
if (fitBottom(r) <= availHeight) break
|
||||
if (fitSpan(r) <= fitTarget) break
|
||||
// Still doesn't fit after all steps → stop at 25% (overflow allowed)
|
||||
}
|
||||
}
|
||||
if (numCol > 1) result = flowIntoColumns(result, availHeight, numCol, colWidth + colGapPx)
|
||||
|
||||
// Vertical anchor: middle/bottom shift everything down (negative offsets allowed
|
||||
// when content is too tall → overflow upward/on both sides),
|
||||
// baked straight into line coordinates so the renderer draws as-is.
|
||||
const anchor = body.anchor ?? 'top'
|
||||
const extraH = availHeight - result.contentHeight
|
||||
// Anchor against the glyph extent: with explicit lnSpc the line box extends below the
|
||||
// ink, and rows/boxes sized to the ink (auto table rows) would otherwise get a negative
|
||||
// offset that clips the top. Equal to contentHeight under single spacing.
|
||||
const extraH = availHeight - (result.inkBottom ?? result.contentHeight)
|
||||
const dy = anchor === 'middle' ? extraH / 2 : anchor === 'bottom' ? extraH : 0
|
||||
const lines = dy
|
||||
? result.lines.map((ln) => ({
|
||||
@@ -549,6 +830,7 @@ export function layoutText(input: TextLayoutInput): RenderTextLayout {
|
||||
fontScale,
|
||||
...(lnSpcReduction ? { lnSpcReduction } : {}),
|
||||
contentHeight: result.contentHeight,
|
||||
...(result.inkBottom ? { inkBottom: result.inkBottom } : {}),
|
||||
wrap,
|
||||
}
|
||||
}
|
||||
@@ -620,24 +902,16 @@ function layoutTextVertical(
|
||||
const paraCols: Col[] = []
|
||||
let cur: GlyphRun[] = []
|
||||
let curH = 0
|
||||
let agg = { ascent: 0, descent: 0, lineHeight: 0 }
|
||||
let agg = { ascent: 0, descent: 0, size: 0 }
|
||||
|
||||
const finishCol = (soft?: number) => {
|
||||
let m = agg
|
||||
if (!cur.length) {
|
||||
const st: RunStyle = {
|
||||
fontFamily: DEFAULT_FONT,
|
||||
fontSizePx: ptToPx(DEFAULT_SIZE_PT, scale) * fontScale,
|
||||
bold: false,
|
||||
italic: false,
|
||||
}
|
||||
m = metrics.metrics(st)
|
||||
}
|
||||
let size = agg.size
|
||||
if (!cur.length) size = ptToPx(DEFAULT_SIZE_PT, scale) * fontScale
|
||||
paraCols.push({
|
||||
runs: cur,
|
||||
usedH: curH,
|
||||
// Line height maps to column width: the column's max metrics × the paragraph line-spacing setting
|
||||
colW: lineH(p, { ...m, lineHeight: m.lineHeight || m.ascent + m.descent }, scale, 0),
|
||||
// Line height maps to column width: 1.2em of the column's max font size × the paragraph line-spacing setting
|
||||
colW: lineH(p, size, scale, 0),
|
||||
paraStart: false,
|
||||
...(soft != null ? { softBreakAfter: soft } : {}),
|
||||
gapBefore: 0,
|
||||
@@ -645,7 +919,7 @@ function layoutTextVertical(
|
||||
})
|
||||
cur = []
|
||||
curH = 0
|
||||
agg = { ascent: 0, descent: 0, lineHeight: 0 }
|
||||
agg = { ascent: 0, descent: 0, size: 0 }
|
||||
}
|
||||
|
||||
const pushCell = (tok: Token, g: string, isBullet = false) => {
|
||||
@@ -656,7 +930,7 @@ function layoutTextVertical(
|
||||
agg = {
|
||||
ascent: Math.max(agg.ascent, m.ascent),
|
||||
descent: Math.max(agg.descent, m.descent),
|
||||
lineHeight: Math.max(agg.lineHeight, m.lineHeight),
|
||||
size: Math.max(agg.size, tok.style.fontSizePx),
|
||||
}
|
||||
cur.push({
|
||||
text: g,
|
||||
@@ -672,6 +946,7 @@ function layoutTextVertical(
|
||||
...(tok.strike ? { strike: true } : {}),
|
||||
...(tok.highlight ? { highlight: tok.highlight } : {}),
|
||||
...(tok.outline ? { outline: tok.outline } : {}),
|
||||
...(tok.shadow ? { shadow: tok.shadow } : {}),
|
||||
widthPx: metrics.measure(g, tok.style),
|
||||
...(tok.blPct ? { baselinePct: tok.blPct } : {}),
|
||||
...(isBullet ? { isBullet: true } : { srcRunIdx: tok.srcRun }),
|
||||
@@ -689,7 +964,7 @@ function layoutTextVertical(
|
||||
agg = {
|
||||
ascent: Math.max(agg.ascent, m.ascent),
|
||||
descent: Math.max(agg.descent, m.descent),
|
||||
lineHeight: Math.max(agg.lineHeight, m.lineHeight),
|
||||
size: Math.max(agg.size, tok.style.fontSizePx),
|
||||
}
|
||||
cur.push({
|
||||
text: tok.text,
|
||||
@@ -705,6 +980,7 @@ function layoutTextVertical(
|
||||
...(tok.strike ? { strike: true } : {}),
|
||||
...(tok.highlight ? { highlight: tok.highlight } : {}),
|
||||
...(tok.outline ? { outline: tok.outline } : {}),
|
||||
...(tok.shadow ? { shadow: tok.shadow } : {}),
|
||||
widthPx: adv,
|
||||
rotate90: true,
|
||||
srcRunIdx: tok.srcRun,
|
||||
@@ -717,15 +993,22 @@ function layoutTextVertical(
|
||||
const hasText = p.runs.some((r) => r.text.trim())
|
||||
const bulletType = p.bullet?.type
|
||||
const hasBullet = hasText && (bulletType === 'char' || bulletType === 'number')
|
||||
if (bulletType === 'number' && hasText) autoNum += 1
|
||||
if (bulletType === 'number' && hasText)
|
||||
autoNum = autoNum === 0 ? (p.bullet?.startAt ?? 1) : autoNum + 1
|
||||
else if (bulletType !== 'number') autoNum = 0
|
||||
if (hasBullet && p.runs[0]) {
|
||||
const base = runStyle(p.runs[0], scale, fontScale)
|
||||
// <a:buSzPct>: bullet glyph size as a percentage of the first run's size
|
||||
const st =
|
||||
let st =
|
||||
p.bullet?.sizePct != null
|
||||
? { ...base, fontSizePx: base.fontSizePx * (p.bullet.sizePct / 100) }
|
||||
: base
|
||||
let glyph = bulletType === 'char' ? (p.bullet?.char ?? '•') : `${autoNum}.`
|
||||
const sym = bulletType === 'char' ? symbolBulletText(p.bullet?.font, glyph) : undefined
|
||||
if (sym) {
|
||||
glyph = sym
|
||||
st = { ...st, fontFamily: p.bullet!.font! }
|
||||
}
|
||||
pushCell(
|
||||
{
|
||||
text: '',
|
||||
@@ -737,7 +1020,7 @@ function layoutTextVertical(
|
||||
isSpace: false,
|
||||
srcRun: 0,
|
||||
},
|
||||
bulletType === 'char' ? (p.bullet?.char ?? '•') : `${autoNum}.`,
|
||||
glyph,
|
||||
true,
|
||||
)
|
||||
}
|
||||
@@ -825,22 +1108,29 @@ function layoutAll(
|
||||
fontScale: number,
|
||||
lnSpcRed: number,
|
||||
trimEdgeSpacing?: boolean,
|
||||
): { lines: TextLine[]; contentHeight: number } {
|
||||
): { lines: TextLine[]; contentHeight: number; inkBottom?: number } {
|
||||
const outLines: TextLine[] = []
|
||||
// Glyph-extent bottom (last baseline + descent): PowerPoint sizes auto table rows by ink,
|
||||
// not by line-box sum (18pt probe: lnSpc 115% single-line row = 0.7333x box + descent)
|
||||
let inkBottom = 0
|
||||
let y = 0
|
||||
let autoNum = 0 // buAutoNum sequential numbering (reset on a non-numbered paragraph)
|
||||
for (const [pIdx, p] of body.paragraphs.entries()) {
|
||||
// Indent and bullets (body lines start at marL; the bullet
|
||||
// draws at marL+indent, negative indent = hanging indent; without a bullet the
|
||||
// first line starts at marL+indent)
|
||||
const marLPx = emuToPx(p.marL ?? 0, scale)
|
||||
// first line starts at marL+indent). When no layer of the style chain defines
|
||||
// marL, PowerPoint's built-in default indents each level by 0.5" (457200 EMU).
|
||||
const marLPx = emuToPx(p.marL ?? (p.level ? p.level * 457200 : 0), scale)
|
||||
const indentPx = emuToPx(p.indent ?? 0, scale)
|
||||
const hasText = p.runs.some((r) => r.text.trim())
|
||||
const bulletType = p.bullet?.type
|
||||
const hasBullet = hasText && (bulletType === 'char' || bulletType === 'number')
|
||||
if (bulletType === 'number' && hasText) autoNum += 1
|
||||
if (bulletType === 'number' && hasText)
|
||||
autoNum = autoNum === 0 ? (p.bullet?.startAt ?? 1) : autoNum + 1
|
||||
else if (bulletType !== 'number') autoNum = 0
|
||||
const bulletText = bulletType === 'char' ? (p.bullet?.char ?? '•') : `${autoNum}.`
|
||||
let bulletText = bulletType === 'char' ? (p.bullet?.char ?? '•') : `${autoNum}.`
|
||||
const symText = bulletType === 'char' ? symbolBulletText(p.bullet?.font, bulletText) : undefined
|
||||
if (symText) bulletText = symText
|
||||
|
||||
const textX = marLPx
|
||||
const avail = Math.max(availWidth - textX, 1)
|
||||
@@ -860,6 +1150,7 @@ function layoutAll(
|
||||
p.bullet?.sizePct != null
|
||||
? { ...base, fontSizePx: base.fontSizePx * (p.bullet.sizePct / 100) }
|
||||
: base
|
||||
if (symText) bulletSt = { ...bulletSt, fontFamily: p.bullet!.font! }
|
||||
bulletW = metrics.measure(bulletText, bulletSt)
|
||||
}
|
||||
const bulletX = Math.max(marLPx + indentPx, 0)
|
||||
@@ -876,14 +1167,17 @@ function layoutAll(
|
||||
lnSpcRed,
|
||||
bulletOverflowPx,
|
||||
)
|
||||
// Space before/after: spcPts is absolute pt; spcPct is a percentage of the paragraph's single line height (100 = one line)
|
||||
// Space before/after: spcPts is absolute pt; spcPct is a percentage of the paragraph's single line height (100 = one line).
|
||||
// PowerPoint ignores space-before on a text frame's FIRST paragraph in every body
|
||||
// (0047 measured: defaultTextStyle spcBef 50% shifted no first line), not just table cells.
|
||||
const singleH = laid[0]?.singleH ?? 0
|
||||
if (!(trimEdgeSpacing && pIdx === 0))
|
||||
if (pIdx !== 0)
|
||||
y +=
|
||||
ptToPx(p.spaceBefore ?? 0, scale) +
|
||||
(p.spaceBeforePct ? singleH * (p.spaceBeforePct / 100) : 0)
|
||||
laid.forEach((ln, li) => {
|
||||
const baseline = y + ln.ascent
|
||||
const baseline = y + (ln.leadAbove ?? 0) + ln.ascent
|
||||
inkBottom = Math.max(inkBottom, baseline + ln.descent)
|
||||
const lineWidth = ln.runs.reduce((acc, r) => acc + r.widthPx, 0)
|
||||
// Without a bullet the first line adds indent (positive or negative); with a bullet
|
||||
// the body starts at marL, pushed right when the glyph overflows the hanging indent
|
||||
@@ -947,6 +1241,7 @@ function layoutAll(
|
||||
runs,
|
||||
top: y,
|
||||
height: ln.height,
|
||||
...(ln.leadAbove ? { leadAbove: ln.leadAbove } : {}),
|
||||
paraStart: li === 0,
|
||||
...(ln.trailingSpace ? { trailingSpace: true } : {}),
|
||||
...(ln.softBreakAfter != null ? { softBreakAfter: ln.softBreakAfter } : {}),
|
||||
@@ -961,5 +1256,5 @@ function layoutAll(
|
||||
y +=
|
||||
ptToPx(p.spaceAfter ?? 0, scale) + (p.spaceAfterPct ? singleH * (p.spaceAfterPct / 100) : 0)
|
||||
}
|
||||
return { lines: outLines, contentHeight: y }
|
||||
return { lines: outLines, contentHeight: y, inkBottom }
|
||||
}
|
||||
|
||||
@@ -233,6 +233,54 @@ describe('buildChartNode', () => {
|
||||
expect(nums[1]).not.toBeCloseTo(nums[8]!, 3)
|
||||
})
|
||||
|
||||
it('exploded 2D pie: wedge centers offset along mid-angles, radius shrunk to keep the footprint', () => {
|
||||
const base: ChartModel = {
|
||||
kind: 'pie',
|
||||
categories: ['x', 'y'],
|
||||
series: [{ values: [1, 1] }],
|
||||
}
|
||||
const plain = buildChartNode('r_e0', 'ele0', base, box, vp, metrics)!
|
||||
const exploded = buildChartNode(
|
||||
'r_e1',
|
||||
'ele1',
|
||||
{ ...base, series: [{ values: [1, 1], explosionPct: 25 }] },
|
||||
box,
|
||||
vp,
|
||||
metrics,
|
||||
)!
|
||||
const r0 = plain.wedges![0]!.outerR
|
||||
const [w1, w2] = exploded.wedges!
|
||||
// offset = 25% of diameter, radius / (1 + 2·25%)
|
||||
expect(w1!.outerR).toBeCloseTo(r0 / 1.5, 5)
|
||||
const off = 0.5 * w1!.outerR
|
||||
// slice 1 sweeps [-90°, 90°] → mid-angle 0° (right); slice 2 mirrors left
|
||||
expect(w1!.cx - plain.wedges![0]!.cx).toBeCloseTo(off, 5)
|
||||
expect(w1!.cy).toBeCloseTo(plain.wedges![0]!.cy, 5)
|
||||
expect(w2!.cx - plain.wedges![1]!.cx).toBeCloseTo(-off, 5)
|
||||
})
|
||||
|
||||
it('exploded pseudo-3D pie: per-point explosion separates only that slice', () => {
|
||||
const pie: ChartModel = {
|
||||
kind: 'pie',
|
||||
pseudo3D: true,
|
||||
rotXDeg: 30,
|
||||
categories: ['x', 'y'],
|
||||
series: [{ values: [1, 1], pointExplosionPct: [25, undefined] }],
|
||||
}
|
||||
const node = buildChartNode('r_e2', 'ele2', pie, box, vp, metrics)!
|
||||
const faces = node.paths!.filter((p) => p.stroke === '#ffffff')
|
||||
expect(faces).toHaveLength(2)
|
||||
// face path apex = wedge center: "M cx cy L …"
|
||||
const apex = (d: string) =>
|
||||
d
|
||||
.match(/-?\d+(\.\d+)?/g)!
|
||||
.slice(0, 2)
|
||||
.map(Number)
|
||||
const [a1, a2] = [apex(faces[0]!.d), apex(faces[1]!.d)]
|
||||
expect(a1[0]).toBeGreaterThan(a2[0]!)
|
||||
expect(a1[1]).toBeCloseTo(a2[1]!, 5)
|
||||
})
|
||||
|
||||
it('doughnut hole uses holePct of outer radius', () => {
|
||||
const dough: ChartModel = {
|
||||
kind: 'pie',
|
||||
@@ -380,3 +428,174 @@ describe('chartSpace default text size reaches every label group', () => {
|
||||
expect(sizes.has(Math.round(ptToPxAt(10) * scale))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('sunburst / funnel (chartEx)', () => {
|
||||
it('merges a row ending at an ancestor with deeper rows sharing the path', () => {
|
||||
// Row 0 ends at "Stem" (its own value); rows 1-2 are leaves under the same "Stem"
|
||||
const model: ChartModel = {
|
||||
kind: 'sunburst',
|
||||
categories: ['', 'Leaf1', 'Leaf2'],
|
||||
series: [{ values: [10, 20, 30] }],
|
||||
sunburst: {
|
||||
levels: [
|
||||
['', 'Leaf1', 'Leaf2'],
|
||||
['Stem', 'Stem', 'Stem'],
|
||||
],
|
||||
sizes: [10, 20, 30],
|
||||
},
|
||||
}
|
||||
const node = buildChartNode('r_1', 'el1', model, box, vp, metrics)!
|
||||
const stemWedges = node.wedges!.filter(
|
||||
(w) => w.innerR === Math.min(...node.wedges!.map((x) => x.innerR)),
|
||||
)
|
||||
// One merged "Stem" ring segment spanning the full circle, not two siblings
|
||||
expect(stemWedges).toHaveLength(1)
|
||||
expect(stemWedges[0]!.sweepDeg).toBeCloseTo(360)
|
||||
// Two leaf segments on the outer ring (the ancestor's own 10 leaves a gap)
|
||||
expect(node.wedges!.length).toBe(3)
|
||||
})
|
||||
|
||||
it('funnel draws centered bars with category labels', () => {
|
||||
const model: ChartModel = {
|
||||
kind: 'funnel',
|
||||
categories: ['A', 'B'],
|
||||
series: [{ values: [50, 100] }],
|
||||
gapWidthPct: 6,
|
||||
}
|
||||
const node = buildChartNode('r_1', 'el1', model, box, vp, metrics)!
|
||||
expect(node.bars).toHaveLength(2)
|
||||
// Widest bar is centered: symmetric margins
|
||||
const b = node.bars[1]!
|
||||
expect(Math.abs(b.x - (node.box.w - (b.x + b.w)))).toBeLessThan(30)
|
||||
expect(node.bars[1]!.w).toBeCloseTo(node.bars[0]!.w * 2, 0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('horizontal bar series order and labels (python-pptx barH sheets)', () => {
|
||||
const barHModel: ChartModel = {
|
||||
kind: 'bar',
|
||||
barDir: 'bar',
|
||||
categories: ['Foo', 'Bar'],
|
||||
series: [
|
||||
{ name: 'Series 1', color: '#4472C4', values: [1.2, 2.3], dataLabels: true },
|
||||
{ name: 'Series 2', color: '#ED7D31', values: [4.5, 5.6], dataLabels: true },
|
||||
],
|
||||
legendPos: 'r',
|
||||
dataLabels: true,
|
||||
} as any
|
||||
|
||||
it('series 1 sits nearest the category axis (bottom of each group)', () => {
|
||||
const node = buildChartNode('r_1', 'el1', barHModel, box, vp, metrics)!
|
||||
// two bars per category; within a group the series-1 bar has the LARGER y
|
||||
const s1 = node.bars.filter((b) => b.color === '#4472C4')
|
||||
const s2 = node.bars.filter((b) => b.color === '#ED7D31')
|
||||
expect(s1[0]!.y).toBeGreaterThan(s2[0]!.y)
|
||||
})
|
||||
|
||||
it('legend lists series in reverse (Series 2 first)', () => {
|
||||
const node = buildChartNode('r_1', 'el1', barHModel, box, vp, metrics)!
|
||||
const legendTexts = node.labels.filter((l) => l.text.startsWith('Series')).map((l) => l.text)
|
||||
expect(legendTexts[0]).toBe('Series 2')
|
||||
})
|
||||
})
|
||||
|
||||
describe('composite data labels (c:showSerName/showCatName)', () => {
|
||||
it('prepends series and category names to the value', () => {
|
||||
const m: ChartModel = {
|
||||
kind: 'bar',
|
||||
barDir: 'bar',
|
||||
categories: ['Category 1'],
|
||||
series: [{ name: 'Series 2', color: '#ED7D31', values: [2.4], dataLabels: true }],
|
||||
dataLabels: true,
|
||||
dataLabelSerName: true,
|
||||
dataLabelCatName: true,
|
||||
} as any
|
||||
const node = buildChartNode('r_1', 'el1', m, box, vp, metrics)!
|
||||
expect(node.labels.some((l) => l.text === 'Series 2, Category 1, 2.4')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('size-aware tick cap', () => {
|
||||
const mk = (): ChartModel =>
|
||||
({
|
||||
kind: 'bar',
|
||||
categories: ['A', 'B'],
|
||||
series: [{ name: 'S', color: '#4472C4', values: [12.3, 8] }],
|
||||
}) as any
|
||||
|
||||
it('a short chart steps the unit up the 1/2/5 ladder (range 13 -> unit 5)', () => {
|
||||
const small = { ...box, h: 110 }
|
||||
const node = buildChartNode('r_1', 'el1', mk(), small, vp, metrics)!
|
||||
const ticks = node.labels.filter((l) => /^\d+$/.test(l.text)).map((l) => Number(l.text))
|
||||
expect(Math.max(...ticks)).toBe(15)
|
||||
expect(ticks).toContain(5)
|
||||
expect(ticks).not.toContain(2)
|
||||
})
|
||||
|
||||
it('a tall chart keeps the dense ratio-rule unit', () => {
|
||||
const tall = { ...box, h: 900 }
|
||||
const node = buildChartNode('r_1', 'el1', mk(), tall, vp, metrics)!
|
||||
const ticks = node.labels.filter((l) => /^\d+$/.test(l.text)).map((l) => Number(l.text))
|
||||
expect(ticks).toContain(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('chart wave2: palette idx colors, multi-level cats, barH hidden labels', () => {
|
||||
it('automatic series colors key off paletteIdx, not document order', () => {
|
||||
const m: ChartModel = {
|
||||
kind: 'bar',
|
||||
categories: ['A'],
|
||||
series: [
|
||||
{ values: [1], paletteIdx: 2 },
|
||||
{ values: [2], paletteIdx: 0 },
|
||||
],
|
||||
themePalette: ['#111111', '#222222', '#333333'],
|
||||
} as any
|
||||
const node = buildChartNode('r_1', 'el1', m, box, vp, metrics)!
|
||||
const colors = node.bars.map((b) => b.color)
|
||||
expect(colors[0]).toBe('#333333')
|
||||
expect(colors[1]).toBe('#111111')
|
||||
})
|
||||
|
||||
it('renders outer-level group labels centered under their spans', () => {
|
||||
const m: ChartModel = {
|
||||
kind: 'bar',
|
||||
categories: ['SF', 'LA', 'NY', 'Albany'],
|
||||
series: [{ values: [1, 2, 3, 4] }],
|
||||
categoryGroups: [
|
||||
{ label: 'CA', start: 0 },
|
||||
{ label: 'NY2', start: 2 },
|
||||
],
|
||||
} as any
|
||||
const node = buildChartNode('r_1', 'el1', m, box, vp, metrics)!
|
||||
const ca = node.labels.find((l) => l.text === 'CA')!
|
||||
const ny = node.labels.find((l) => l.text === 'NY2')!
|
||||
expect(ca).toBeTruthy()
|
||||
expect(ny.x).toBeGreaterThan(ca.x)
|
||||
// group row sits below the leaf category labels
|
||||
const leaf = node.labels.find((l) => l.text === 'SF')!
|
||||
expect(ca.y).toBeGreaterThan(leaf.y)
|
||||
})
|
||||
|
||||
it('barH with tickLblPos none draws no category labels', () => {
|
||||
const m: ChartModel = {
|
||||
kind: 'bar',
|
||||
barDir: 'bar',
|
||||
categories: ['Category 1', 'Category 2'],
|
||||
series: [{ values: [1, 2] }],
|
||||
catAxis: { tickLblHidden: true },
|
||||
} as any
|
||||
const node = buildChartNode('r_1', 'el1', m, box, vp, metrics)!
|
||||
expect(node.labels.some((l) => l.text.startsWith('Category'))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('legend swatch metrics (PowerPoint ~0.5em squares)', () => {
|
||||
it('side legend swatches are half-em and reserve a tight width', () => {
|
||||
const node = buildChartNode('r_1', 'el1', lineModel, box, vp, metrics)!
|
||||
for (const sw of node.swatches) {
|
||||
expect(sw.w).toBeLessThan(20)
|
||||
expect(Math.abs(sw.w - sw.h)).toBeLessThan(1)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -616,4 +616,168 @@ describe('buildRenderSlide (end-to-end on real fixture)', () => {
|
||||
expect(n.pathData).toMatch(/^M .* C /)
|
||||
expect(n.pathData.endsWith('Z')).toBe(true)
|
||||
})
|
||||
|
||||
it('useBgFill shapes take the slide background fill (tdf93868)', async () => {
|
||||
const { deck } = await openPptx(enginePptx('01_standard_business.pptx'))
|
||||
const slide = deck.slides[0]!
|
||||
const bg = {
|
||||
type: 'gradient',
|
||||
stops: [
|
||||
{ pos: 0, color: '#000000' },
|
||||
{ pos: 1, color: '#3F3F3F' },
|
||||
],
|
||||
angle: 16200000,
|
||||
}
|
||||
const el: any = {
|
||||
id: 'bgsp1',
|
||||
type: 'shape',
|
||||
anchor: { spIndex: -1, originalXml: '', range: [0, 0] },
|
||||
transform: {
|
||||
offset: { x: 0, y: 0, cx: 914400, cy: 914400 },
|
||||
rot: 0,
|
||||
flipH: false,
|
||||
flipV: false,
|
||||
},
|
||||
presetGeometry: 'roundRect',
|
||||
fill: { type: 'solid', color: '#FFFFFF' },
|
||||
useBgFill: true,
|
||||
}
|
||||
const rs = buildRenderSlide(
|
||||
{ ...slide, elements: [el], decorations: [], background: bg as any },
|
||||
deck.size,
|
||||
{ fitWidthPx: 1280 },
|
||||
)
|
||||
const n = rs.nodes[0] as any
|
||||
expect(n.fill.kind).toBe('gradient')
|
||||
expect(n.fill.stops).toEqual([
|
||||
{ pos: 0, color: '#000000' },
|
||||
{ pos: 1, color: '#3F3F3F' },
|
||||
])
|
||||
// without a resolvable background the parsed fill stays as fallback
|
||||
const rs2 = buildRenderSlide(
|
||||
{ ...slide, elements: [el], decorations: [], background: undefined },
|
||||
deck.size,
|
||||
{ fitWidthPx: 1280 },
|
||||
)
|
||||
expect((rs2.nodes[0] as any).fill).toEqual({ kind: 'solid', color: '#FFFFFF' })
|
||||
|
||||
// useBgFill also applies inside groups
|
||||
const group: any = {
|
||||
id: 'g1',
|
||||
type: 'group',
|
||||
anchor: { spIndex: -1, originalXml: '', range: [0, 0] },
|
||||
transform: el.transform,
|
||||
children: [{ ...el, id: 'bgsp2' }],
|
||||
childOffset: { x: 0, y: 0, cx: 914400, cy: 914400 },
|
||||
}
|
||||
const rs3 = buildRenderSlide(
|
||||
{ ...slide, elements: [group], decorations: [], background: bg as any },
|
||||
deck.size,
|
||||
{ fitWidthPx: 1280 },
|
||||
)
|
||||
const child = (rs3.nodes[0] as any).children[0]
|
||||
expect(child.fill.kind).toBe('gradient')
|
||||
})
|
||||
})
|
||||
|
||||
describe('table row heights are absolute (napierone 0032: h="0" rows size to content)', () => {
|
||||
it('does not stretch a lone non-zero header row across the whole frame', async () => {
|
||||
const { deck } = await openPptx(enginePptx('01_standard_business.pptx'))
|
||||
const slide = deck.slides[0]!
|
||||
const el: any = {
|
||||
id: 'tbl_1',
|
||||
type: 'table',
|
||||
anchor: { spIndex: -1, originalXml: '', range: [0, 0] },
|
||||
transform: {
|
||||
offset: { x: 0, y: 0, cx: 8208912, cy: 4566280 },
|
||||
rot: 0,
|
||||
flipH: false,
|
||||
flipV: false,
|
||||
},
|
||||
colWidths: [4104456, 4104456],
|
||||
// header has an explicit height; the rest are h="0" (auto)
|
||||
rowHeights: [360040, 360040, 0],
|
||||
rows: [
|
||||
[{}, {}],
|
||||
[{}, {}],
|
||||
[{}, {}],
|
||||
],
|
||||
}
|
||||
const rs = buildRenderSlide({ ...slide, elements: [el], decorations: [] }, deck.size, {
|
||||
fitWidthPx: 1280,
|
||||
})
|
||||
const node = rs.nodes[0] as any
|
||||
expect(node.type).toBe('table')
|
||||
// EMU heights convert absolutely: 360040 EMU of a 4566280 EMU frame ≈ 7.9% of box.h,
|
||||
// not box.h/2 (the old proportional split over the two non-zero rows)
|
||||
const expected = (360040 / 4566280) * node.box.h
|
||||
expect(node.gridY[1]).toBeCloseTo(expected, 1)
|
||||
expect(node.gridY[2] - node.gridY[1]).toBeCloseTo(expected, 1)
|
||||
// empty h="0" row stays at content height (zero here)
|
||||
expect(node.gridY[3]).toBeCloseTo(node.gridY[2], 1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('picture spPr fill backdrop (napierone 0027)', () => {
|
||||
it('threads a solid pic fill through to the render node', async () => {
|
||||
const { deck } = await openPptx(enginePptx('01_standard_business.pptx'))
|
||||
const slide = deck.slides[0]!
|
||||
const el: any = {
|
||||
id: 'pic_1',
|
||||
type: 'picture',
|
||||
anchor: { spIndex: -1, originalXml: '', range: [0, 0] },
|
||||
transform: {
|
||||
offset: { x: 0, y: 0, cx: 1000000, cy: 1000000 },
|
||||
rot: 0,
|
||||
flipH: false,
|
||||
flipV: false,
|
||||
},
|
||||
mediaRef: 'ppt/media/image1.png',
|
||||
opacity: 0.26,
|
||||
fill: { type: 'solid', color: '#000000' },
|
||||
duotone: ['#111111', '#EEEEEE'],
|
||||
clrChange: { from: '#000000', to: '#00000000' },
|
||||
}
|
||||
const rs = buildRenderSlide({ ...slide, elements: [el], decorations: [] }, deck.size, {
|
||||
fitWidthPx: 1280,
|
||||
})
|
||||
const node = rs.nodes[0] as any
|
||||
expect(node.type).toBe('picture')
|
||||
expect(node.fill).toEqual({ kind: 'solid', color: '#000000' })
|
||||
expect(node.opacity).toBeCloseTo(0.26, 5)
|
||||
expect(node.duotone).toEqual(['#111111', '#EEEEEE'])
|
||||
expect(node.clrChange).toEqual({ from: '#000000', to: '#00000000' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('metafile pictures get an opaque white backdrop (PlanS academy banner)', () => {
|
||||
it('sets bgColor for EMF data URLs and leaves raster images alone', async () => {
|
||||
const { deck } = await openPptx(enginePptx('01_standard_business.pptx'))
|
||||
const slide = deck.slides[0]!
|
||||
const mk = (dataUrl: string): any => ({
|
||||
id: 'pic_m',
|
||||
type: 'picture',
|
||||
anchor: { spIndex: -1, originalXml: '', range: [0, 0] },
|
||||
transform: {
|
||||
offset: { x: 0, y: 0, cx: 1000000, cy: 1000000 },
|
||||
rot: 0,
|
||||
flipH: false,
|
||||
flipV: false,
|
||||
},
|
||||
mediaRef: '',
|
||||
dataUrl,
|
||||
})
|
||||
const rs = buildRenderSlide(
|
||||
{
|
||||
...slide,
|
||||
elements: [mk('data:image/x-emf;base64,AAAA'), mk('data:image/png;base64,AAAA')],
|
||||
decorations: [],
|
||||
},
|
||||
deck.size,
|
||||
{ fitWidthPx: 1280 },
|
||||
)
|
||||
const [emf, png] = rs.nodes as any[]
|
||||
expect(emf.bgColor).toBe('#FFFFFF')
|
||||
expect(png.bgColor).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -110,3 +110,36 @@ describe('passthrough preview rendering', () => {
|
||||
expect(n.media).toBe('video')
|
||||
})
|
||||
})
|
||||
|
||||
describe('stale drawing scale-to-frame threshold', () => {
|
||||
const mkPreview = (childCy: number): PassthroughElement =>
|
||||
({
|
||||
id: 'pt1',
|
||||
type: 'passthrough',
|
||||
kind: 'smartart',
|
||||
anchor,
|
||||
transform: transform(0, 0, 1000000, 1000000),
|
||||
previewShapes: [
|
||||
{
|
||||
id: 'sp1',
|
||||
type: 'shape',
|
||||
anchor,
|
||||
transform: transform(0, 0, 1000000, childCy),
|
||||
fill: { type: 'solid', color: '#4F81BD' },
|
||||
} as any,
|
||||
],
|
||||
}) as any
|
||||
const groupScaleY = (el: PassthroughElement): number => {
|
||||
const rs = buildRenderSlide(slideOf([el]), size, { fitWidthPx: 1280 })
|
||||
const g = rs.nodes[0] as GroupRenderNode
|
||||
return g.childScaleY ?? 1
|
||||
}
|
||||
|
||||
it('a small overshoot (intentional bleed) is left alone', () => {
|
||||
expect(groupScaleY(mkPreview(1100000))).toBeCloseTo(1, 3)
|
||||
})
|
||||
|
||||
it('a gross overflow (stale cache) compresses into the frame', () => {
|
||||
expect(groupScaleY(mkPreview(2500000))).toBeLessThan(0.5)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { isConnectorPreset, connectorPoints, presetPolygon, presetPath, isPillPreset } from '../src/preset-geometry'
|
||||
import {
|
||||
isConnectorPreset,
|
||||
connectorPoints,
|
||||
presetPolygon,
|
||||
presetPath,
|
||||
isPillPreset,
|
||||
} from '../src/preset-geometry'
|
||||
|
||||
describe('connectorPoints', () => {
|
||||
it('straight connector runs corner to corner; flip mirrors direction', () => {
|
||||
@@ -14,7 +20,9 @@ describe('connectorPoints', () => {
|
||||
})
|
||||
|
||||
it('bentConnector2 has one elbow', () => {
|
||||
expect(connectorPoints('bentConnector2', 200, 100, false, false)).toEqual([0, 0, 200, 0, 200, 100])
|
||||
expect(connectorPoints('bentConnector2', 200, 100, false, false)).toEqual([
|
||||
0, 0, 200, 0, 200, 100,
|
||||
])
|
||||
})
|
||||
|
||||
it('isConnectorPreset recognizes connector presets only', () => {
|
||||
@@ -59,7 +67,16 @@ describe('presetPolygon', () => {
|
||||
})
|
||||
|
||||
it('chevron / homePlate / hexagon / star5 stay within bounds', () => {
|
||||
for (const p of ['chevron', 'homePlate', 'hexagon', 'star5', 'parallelogram', 'trapezoid', 'octagon', 'plus']) {
|
||||
for (const p of [
|
||||
'chevron',
|
||||
'homePlate',
|
||||
'hexagon',
|
||||
'star5',
|
||||
'parallelogram',
|
||||
'trapezoid',
|
||||
'octagon',
|
||||
'plus',
|
||||
]) {
|
||||
const pts = presetPolygon(p, 120, 90)
|
||||
expect(pts, p).toBeTruthy()
|
||||
inBounds(pts!, 120, 90)
|
||||
@@ -79,7 +96,9 @@ describe('presetPolygon', () => {
|
||||
})
|
||||
|
||||
it('snip1Rect cuts top-right corner by adj fraction of short side', () => {
|
||||
expect(presetPolygon('snip1Rect', 200, 100, { adj: 20000 })).toEqual([0, 0, 180, 0, 200, 20, 200, 100, 0, 100])
|
||||
expect(presetPolygon('snip1Rect', 200, 100, { adj: 20000 })).toEqual([
|
||||
0, 0, 180, 0, 200, 20, 200, 100, 0, 100,
|
||||
])
|
||||
})
|
||||
|
||||
it('notchedRightArrow has tail notch at mid height', () => {
|
||||
@@ -92,11 +111,30 @@ describe('presetPolygon', () => {
|
||||
|
||||
it('new polygon presets stay in bounds and are non-trivial', () => {
|
||||
for (const p of [
|
||||
'snip2SameRect', 'snip2DiagRect', 'halfFrame', 'corner', 'diagStripe', 'lightningBolt',
|
||||
'flowChartPreparation', 'flowChartManualInput', 'flowChartManualOperation',
|
||||
'flowChartOffpageConnector', 'flowChartExtract', 'flowChartMerge', 'flowChartCollate',
|
||||
'gear6', 'funnel', 'quadArrow', 'bentArrow', 'irregularSeal1', 'irregularSeal2',
|
||||
'star7', 'star10', 'star12', 'star16', 'star24', 'star32',
|
||||
'snip2SameRect',
|
||||
'snip2DiagRect',
|
||||
'halfFrame',
|
||||
'corner',
|
||||
'diagStripe',
|
||||
'lightningBolt',
|
||||
'flowChartPreparation',
|
||||
'flowChartManualInput',
|
||||
'flowChartManualOperation',
|
||||
'flowChartOffpageConnector',
|
||||
'flowChartExtract',
|
||||
'flowChartMerge',
|
||||
'flowChartCollate',
|
||||
'gear6',
|
||||
'quadArrow',
|
||||
'bentArrow',
|
||||
'irregularSeal1',
|
||||
'irregularSeal2',
|
||||
'star7',
|
||||
'star10',
|
||||
'star12',
|
||||
'star16',
|
||||
'star24',
|
||||
'star32',
|
||||
]) {
|
||||
const pts = presetPolygon(p, 120, 90)
|
||||
expect(pts, p).toBeTruthy()
|
||||
@@ -109,6 +147,21 @@ describe('presetPolygon', () => {
|
||||
expect(presetPolygon('star7', 100, 100)!.length).toBe(28)
|
||||
})
|
||||
|
||||
it('mathNotEqual is a 20-vertex polygon: two bars crossed by a slash', () => {
|
||||
const pts = presetPolygon('mathNotEqual', 200, 100)!
|
||||
expect(pts.length).toBe(40)
|
||||
const xs = pts.filter((_, i) => i % 2 === 0)
|
||||
const ys = pts.filter((_, i) => i % 2 === 1)
|
||||
// bars span hc±36.745%w and y 20.6..79.4; slash tips reach y=0 and y=h
|
||||
expect(Math.min(...xs)).toBeCloseTo(200 / 2 - 200 * 0.36745, 1)
|
||||
expect(Math.min(...ys)).toBe(0)
|
||||
expect(Math.max(...ys)).toBe(100)
|
||||
// default 110° slash leans right at the top
|
||||
const topXs = pts.filter((_, i) => i % 2 === 0 && pts[i + 1] === 0)
|
||||
const botXs = pts.filter((_, i) => i % 2 === 0 && pts[i + 1] === 100)
|
||||
expect(Math.min(...topXs)).toBeGreaterThan(Math.max(...botXs))
|
||||
})
|
||||
|
||||
it('wedgeRectCallout default puts tail on bottom edge toward tip', () => {
|
||||
const pts = presetPolygon('wedgeRectCallout', 100, 100)!
|
||||
// tip = (50-20.833, 50+62.5) = (29.17, 112.5): may exceed the box (tail tip)
|
||||
@@ -118,7 +171,11 @@ describe('presetPolygon', () => {
|
||||
})
|
||||
|
||||
describe('presetPath', () => {
|
||||
const nums = (d: string) => d.split(' ').filter((t) => Number.isFinite(Number(t))).map(Number)
|
||||
const nums = (d: string) =>
|
||||
d
|
||||
.split(' ')
|
||||
.filter((t) => Number.isFinite(Number(t)))
|
||||
.map(Number)
|
||||
const inBox = (d: string, w: number, h: number, slack = 0.01) => {
|
||||
const v = nums(d)
|
||||
for (let i = 0; i < v.length; i += 2) {
|
||||
@@ -143,6 +200,26 @@ describe('presetPath', () => {
|
||||
expect(r.fillPath).toMatch(/Z$/)
|
||||
})
|
||||
|
||||
it('mathEqual is two horizontal bars centered on the box', () => {
|
||||
const r = presetPath('mathEqual', 200, 100)!
|
||||
expect(r.path!.match(/M /g)!.length).toBe(2)
|
||||
expect(r.path!.match(/Z/g)!.length).toBe(2)
|
||||
// bars span hc±36.745%w, thickness 23.52%h, half-gap 5.88%h
|
||||
expect(r.path).toMatch(/^M 26.51 20.6 L 173.49 20.6 L 173.49 44.12 L 26.51 44.12 Z/)
|
||||
expect(r.path).toMatch(/M 26.51 55.88 L 173.49 55.88 L 173.49 79.4 L 26.51 79.4 Z$/)
|
||||
})
|
||||
|
||||
it('funnel has mouth ring hole + body reaching the stem bottom', () => {
|
||||
const r = presetPath('funnel', 100, 100)!
|
||||
expect(r.path!.match(/M /g)!.length).toBe(2)
|
||||
const v = nums(r.path!)
|
||||
const ys = v.filter((_, i) => i % 2 === 1)
|
||||
expect(Math.max(...ys)).toBeGreaterThan(95) // stem bottom arc reaches ~h
|
||||
expect(Math.min(...ys)).toBeLessThanOrEqual(0.01) // mouth top arc reaches y=0
|
||||
inBox(r.path!, 100, 100)
|
||||
expect(presetPolygon('funnel', 100, 100)).toBeNull()
|
||||
})
|
||||
|
||||
it('donut has two subpaths (outer + reversed hole)', () => {
|
||||
const r = presetPath('donut', 100, 100)!
|
||||
expect(r.path!.match(/M /g)!.length).toBe(2)
|
||||
@@ -159,7 +236,14 @@ describe('presetPath', () => {
|
||||
})
|
||||
|
||||
it('cube/can/bevel/foldedCorner carry decorative strokePath', () => {
|
||||
for (const p of ['cube', 'can', 'bevel', 'foldedCorner', 'smileyFace', 'flowChartPredefinedProcess']) {
|
||||
for (const p of [
|
||||
'cube',
|
||||
'can',
|
||||
'bevel',
|
||||
'foldedCorner',
|
||||
'smileyFace',
|
||||
'flowChartPredefinedProcess',
|
||||
]) {
|
||||
const r = presetPath(p, 120, 90)!
|
||||
expect(r.path, p).toMatch(/^M /)
|
||||
expect(r.strokePath, p).toMatch(/^M /)
|
||||
@@ -169,12 +253,39 @@ describe('presetPath', () => {
|
||||
|
||||
it('curved/blob presets stay near box', () => {
|
||||
for (const p of [
|
||||
'heart', 'moon', 'sun', 'cloud', 'teardrop', 'plaque', 'noSmoking', 'ribbon', 'ribbon2',
|
||||
'wave', 'doubleWave', 'uturnArrow', 'curvedRightArrow', 'stripedRightArrow', 'chord', 'blockArc',
|
||||
'frame', 'round1Rect', 'round2SameRect', 'round2DiagRect', 'snipRoundRect',
|
||||
'flowChartDocument', 'flowChartMultidocument', 'flowChartConnector', 'flowChartOr',
|
||||
'flowChartSummingJunction', 'flowChartSort', 'flowChartDelay', 'flowChartDisplay',
|
||||
'flowChartPunchedTape', 'flowChartMagneticDisk', 'flowChartMagneticDrum', 'flowChartInternalStorage',
|
||||
'heart',
|
||||
'moon',
|
||||
'sun',
|
||||
'cloud',
|
||||
'teardrop',
|
||||
'plaque',
|
||||
'noSmoking',
|
||||
'ribbon',
|
||||
'ribbon2',
|
||||
'wave',
|
||||
'doubleWave',
|
||||
'uturnArrow',
|
||||
'curvedRightArrow',
|
||||
'stripedRightArrow',
|
||||
'chord',
|
||||
'blockArc',
|
||||
'frame',
|
||||
'round1Rect',
|
||||
'round2SameRect',
|
||||
'round2DiagRect',
|
||||
'snipRoundRect',
|
||||
'flowChartDocument',
|
||||
'flowChartMultidocument',
|
||||
'flowChartConnector',
|
||||
'flowChartOr',
|
||||
'flowChartSummingJunction',
|
||||
'flowChartSort',
|
||||
'flowChartDelay',
|
||||
'flowChartDisplay',
|
||||
'flowChartPunchedTape',
|
||||
'flowChartMagneticDisk',
|
||||
'flowChartMagneticDrum',
|
||||
'flowChartInternalStorage',
|
||||
]) {
|
||||
const r = presetPath(p, 120, 90)
|
||||
expect(r, p).toBeTruthy()
|
||||
@@ -206,3 +317,14 @@ describe('presetPath', () => {
|
||||
expect(presetPath(undefined, 100, 100)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('circularArrow presets', () => {
|
||||
it('returns a ring-with-arrowhead path instead of falling back to a filled rect', () => {
|
||||
for (const preset of ['circularArrow', 'leftCircularArrow']) {
|
||||
const r = presetPath(preset, 100, 100)
|
||||
expect(r?.path).toBeTruthy()
|
||||
// annular: the path contains two arc sweeps (many cubic segments), not a 4-corner box
|
||||
expect((r!.path!.match(/C /g) ?? []).length).toBeGreaterThan(4)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -54,6 +54,12 @@ describe('2.3 metrics', () => {
|
||||
const met = om.metrics(real)
|
||||
expect(met.ascent).toBeCloseTo(80, 4)
|
||||
expect(met.descent).toBeCloseTo(20, 4)
|
||||
expect(met.lineHeight).toBeCloseTo(100, 4)
|
||||
// hhea lineGap becomes external leading (advances lines, outside the line box)
|
||||
const om2 = new OpentypeMetrics(() => ({ ...fakeFont, lineGap: 100 }))
|
||||
const met2 = om2.metrics(real)
|
||||
expect(met2.lineHeight).toBeCloseTo(100, 4)
|
||||
expect(met2.externalLeading).toBeCloseTo(10, 4)
|
||||
// Unknown fonts fall back to the heuristic (no throw)
|
||||
const unknown = { fontFamily: 'Nope', fontSizePx: 100, bold: false, italic: false }
|
||||
expect(om.measure('ab', unknown)).toBeGreaterThan(0)
|
||||
@@ -188,8 +194,29 @@ describe('2.3 text layout', () => {
|
||||
vp,
|
||||
})
|
||||
expect(spaced.contentHeight).toBeGreaterThan(single.contentHeight)
|
||||
// Double line spacing + 12pt before + 6pt after
|
||||
expect(spaced.lines[0]!.top).toBeGreaterThan(0) // spaceBefore pushed the line start down
|
||||
// PowerPoint ignores space-before on the frame's first paragraph (0047 measured);
|
||||
// the taller line comes from lineHeight/spaceAfter only
|
||||
expect(spaced.lines[0]!.top).toBe(0)
|
||||
})
|
||||
|
||||
it('space-before applies from the second paragraph on, never the first', () => {
|
||||
const layout = layoutText({
|
||||
body: body({
|
||||
paragraphs: [
|
||||
{ runs: [{ text: 'a', fontSize: 20 }], spaceBefore: 12 },
|
||||
{ runs: [{ text: 'b', fontSize: 20 }], spaceBefore: 12 },
|
||||
],
|
||||
}),
|
||||
boxWidthPx: 400,
|
||||
boxHeightPx: 400,
|
||||
metrics: new HeuristicMetrics(),
|
||||
vp,
|
||||
})
|
||||
expect(layout.lines[0]!.top).toBe(0)
|
||||
expect(layout.lines[1]!.top - (layout.lines[0]!.top + layout.lines[0]!.height)).toBeCloseTo(
|
||||
(12 * 96) / 72,
|
||||
1,
|
||||
)
|
||||
})
|
||||
|
||||
it('align=center centers each line horizontally (baked into run x)', () => {
|
||||
@@ -774,8 +801,9 @@ describe('table cell edge spacing (trimEdgeSpacing)', () => {
|
||||
vp,
|
||||
trimEdgeSpacing: true,
|
||||
})
|
||||
// 10pt before-first + 10pt after-last dropped (pt → px at scale 1: ×96/72)
|
||||
expect(plain.contentHeight - trimmed.contentHeight).toBeCloseTo((20 * 96) / 72, 1)
|
||||
// First-paragraph space-before is dropped in every body (PowerPoint semantics);
|
||||
// trimEdgeSpacing additionally drops the last space-after (pt → px at scale 1: ×96/72)
|
||||
expect(plain.contentHeight - trimmed.contentHeight).toBeCloseTo((10 * 96) / 72, 1)
|
||||
// inner spacing (after-first + before-last) is kept
|
||||
expect(trimmed.lines[1]!.top - (trimmed.lines[0]!.top + trimmed.lines[0]!.height)).toBeCloseTo(
|
||||
(20 * 96) / 72,
|
||||
@@ -821,3 +849,130 @@ describe('autofit ignores trailing blank paragraphs', () => {
|
||||
expect(layout.fontScale).toBeLessThan(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('buAutoNum startAt', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const style = { fontFamily: 'Arial', fontSizePx: 20, bold: false, italic: false }
|
||||
const para = (text: string, startAt?: number) => ({
|
||||
runs: [{ text, ...style, fontSize: 20 }],
|
||||
bullet: { type: 'number' as const, ...(startAt != null ? { startAt } : {}) },
|
||||
marL: 457200,
|
||||
indent: -457200,
|
||||
})
|
||||
|
||||
it('starts the sequence at startAt and continues from there', () => {
|
||||
const layout = layoutText({
|
||||
body: body({ paragraphs: [para('first', 3), para('second')] as any }),
|
||||
boxWidthPx: 800,
|
||||
boxHeightPx: 200,
|
||||
metrics: m,
|
||||
vp,
|
||||
})
|
||||
const bullets = layout.lines
|
||||
.map((l) => l.runs.find((r: any) => r.isBullet))
|
||||
.filter(Boolean)
|
||||
.map((r: any) => r.text)
|
||||
expect(bullets).toEqual(['3.', '4.'])
|
||||
})
|
||||
|
||||
it('defaults to 1 without startAt', () => {
|
||||
const layout = layoutText({
|
||||
body: body({ paragraphs: [para('only')] as any }),
|
||||
boxWidthPx: 800,
|
||||
boxHeightPx: 200,
|
||||
metrics: m,
|
||||
vp,
|
||||
})
|
||||
const b = layout.lines[0]!.runs.find((r: any) => r.isBullet) as any
|
||||
expect(b.text).toBe('1.')
|
||||
})
|
||||
})
|
||||
|
||||
describe('lnSpc > 100%: excess spacing sits above the glyphs', () => {
|
||||
const m = new HeuristicMetrics()
|
||||
const style = { fontFamily: 'Arial', fontSizePx: 20, bold: false, italic: false }
|
||||
const para = (text: string, lineHeight?: number) => ({
|
||||
runs: [{ text, ...style, fontSize: 20 }],
|
||||
...(lineHeight != null ? { lineHeight } : {}),
|
||||
})
|
||||
|
||||
it('150% spacing pushes the first baseline down by the half-line excess', () => {
|
||||
const single = layoutText({
|
||||
body: body({ paragraphs: [para('watermark')] as any }),
|
||||
boxWidthPx: 800,
|
||||
boxHeightPx: 400,
|
||||
metrics: m,
|
||||
vp,
|
||||
})
|
||||
const spaced = layoutText({
|
||||
body: body({ paragraphs: [para('watermark', 150)] as any }),
|
||||
boxWidthPx: 800,
|
||||
boxHeightPx: 400,
|
||||
metrics: m,
|
||||
vp,
|
||||
})
|
||||
// PowerPoint model (48pt probe): explicit pct spacing places the baseline at
|
||||
// 0.7333 x the line box, ignoring font metrics; single spacing bottom-anchors
|
||||
// (box - descent). Heuristic metrics: descent 0.2em.
|
||||
const b1 = single.lines[0]!.runs[0]!.baselineY
|
||||
const b2 = spaced.lines[0]!.runs[0]!.baselineY
|
||||
expect(single.lines[0]!.height).toBeCloseTo(spaced.lines[0]!.height / 1.5, 1)
|
||||
expect(b1).toBeCloseTo(single.lines[0]!.height - 0.2 * (single.lines[0]!.height / 1.2), 1)
|
||||
expect(b2).toBeCloseTo(spaced.lines[0]!.height * (0.88 / 1.2), 1)
|
||||
})
|
||||
|
||||
it('sub-100% spacing shrinks the box and lifts the baseline with it (ink may poke above)', () => {
|
||||
const tight = layoutText({
|
||||
body: body({ paragraphs: [para('text', 70)] as any }),
|
||||
boxWidthPx: 800,
|
||||
boxHeightPx: 400,
|
||||
metrics: m,
|
||||
vp,
|
||||
})
|
||||
const single = layoutText({
|
||||
body: body({ paragraphs: [para('text')] as any }),
|
||||
boxWidthPx: 800,
|
||||
boxHeightPx: 400,
|
||||
metrics: m,
|
||||
vp,
|
||||
})
|
||||
expect(tight.lines[0]!.height).toBeCloseTo(single.lines[0]!.height * 0.7, 1)
|
||||
// generic law: baseline = 0.7333 x box — above the single-spacing baseline
|
||||
expect(tight.lines[0]!.runs[0]!.baselineY).toBeCloseTo(tight.lines[0]!.height * (0.88 / 1.2), 1)
|
||||
expect(tight.lines[0]!.runs[0]!.baselineY).toBeLessThan(single.lines[0]!.runs[0]!.baselineY)
|
||||
})
|
||||
})
|
||||
|
||||
describe('bodyPr numCol columns', () => {
|
||||
it('flows lines into the next column when the box height is exceeded', () => {
|
||||
const paras = Array.from({ length: 8 }, (_, i) => ({
|
||||
runs: [{ text: `p${i}`, fontSize: 20 }],
|
||||
}))
|
||||
const layout = layoutText({
|
||||
body: { ...body({ paragraphs: paras }), numCol: 2, spcCol: 0 },
|
||||
boxWidthPx: 400,
|
||||
boxHeightPx: 4 * 20 * (96 / 72) * 1.2 + 2, // fits 4 lines per column
|
||||
metrics: new HeuristicMetrics(),
|
||||
vp,
|
||||
})
|
||||
const xs = layout.lines.map((l) => l.runs[0]!.x)
|
||||
// First 4 lines in column 1 (x < 200), rest shifted a full column stride right
|
||||
expect(xs.slice(0, 4).every((x) => x < 200)).toBe(true)
|
||||
expect(xs.slice(4).every((x) => x >= 200)).toBe(true)
|
||||
// Column 2 restarts at the top, baselines rebased with the line tops
|
||||
expect(layout.lines[4]!.top).toBe(0)
|
||||
expect(layout.lines[4]!.runs[0]!.baselineY).toBeLessThan(layout.lines[3]!.runs[0]!.baselineY)
|
||||
expect(layout.contentHeight).toBeCloseTo(layout.lines[3]!.top + layout.lines[3]!.height, 1)
|
||||
})
|
||||
|
||||
it('single column behavior unchanged when numCol absent', () => {
|
||||
const layout = layoutText({
|
||||
body: body({ paragraphs: [{ runs: [{ text: 'x', fontSize: 20 }] }] }),
|
||||
boxWidthPx: 400,
|
||||
boxHeightPx: 100,
|
||||
metrics: new HeuristicMetrics(),
|
||||
vp,
|
||||
})
|
||||
expect(layout.lines.length).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user