| 1 | import type { SlideIr } from './ir' |
| 2 | import JSZip from 'jszip' |
| 3 | import PptxGenJS from 'pptxgenjs' |
| 4 | import { describe, expect, it } from 'vitest' |
| 5 | import { buildPptx } from './build' |
| 6 | |
| 7 | /** |
| 8 | * These assert on the generated OOXML, not on the JavaScript that produced it. |
| 9 | * |
| 10 | * That is deliberate: every bug this file exists to catch is a unit conversion, |
| 11 | * and a unit conversion looks correct in the source right up until you read |
| 12 | * what it wrote. `sz="2400"` is checkable against the spec; `pt(fontSize)` is |
| 13 | * not. |
| 14 | */ |
| 15 | |
| 16 | const BLACK = { r: 0, g: 0, b: 0, a: 1 } |
| 17 | |
| 18 | function slide(nodes: SlideIr['nodes']): SlideIr { |
| 19 | return { |
| 20 | no: 1, |
| 21 | clickIndex: 0, |
| 22 | containerId: '001-01', |
| 23 | size: { w: 980, h: 552 }, |
| 24 | nodes, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | async function slideXml(ir: SlideIr[]): Promise<string> { |
| 29 | const buffer = await buildPptx(PptxGenJS, ir, { width: 980, height: 552 }) |
| 30 | const zip = await JSZip.loadAsync(buffer) |
| 31 | return await zip.file('ppt/slides/slide1.xml')!.async('string') |
| 32 | } |
| 33 | |
| 34 | function textNode(runs: SlideIr['nodes'][number] extends never ? never : any): any { |
| 35 | return { |
| 36 | kind: 'text', |
| 37 | sourceId: 1, |
| 38 | rect: { x: 0, y: 0, w: 200, h: 40 }, |
| 39 | elementRect: { x: 0, y: 0, w: 200, h: 40 }, |
| 40 | lineCount: 1, |
| 41 | align: 'left', |
| 42 | lineHeight: 40, |
| 43 | runs, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | describe('unit conversions', () => { |
| 48 | it('writes font size in points, not pixels', async () => { |
| 49 | const xml = await slideXml([ |
| 50 | slide([ |
| 51 | textNode([ |
| 52 | { text: 'Hello', fontSize: 32, fontFamily: 'Arial' }, |
| 53 | ]), |
| 54 | ]), |
| 55 | ]) |
| 56 | // 32 CSS px at 96 px/in is 24 pt, and OOXML stores hundredths of a point. |
| 57 | // Getting this wrong makes every deck a third too large. |
| 58 | expect(xml).toContain('sz="2400"') |
| 59 | }) |
| 60 | |
| 61 | it('writes letter spacing in points and disables kerning', async () => { |
| 62 | const xml = await slideXml([ |
| 63 | slide([ |
| 64 | textNode([ |
| 65 | { |
| 66 | text: 'WIDE', |
| 67 | fontSize: 16, |
| 68 | fontFamily: 'Arial', |
| 69 | letterSpacing: 2, |
| 70 | }, |
| 71 | ]), |
| 72 | ]), |
| 73 | ]) |
| 74 | // 2px -> 1.5pt -> spc is hundredths of a point. |
| 75 | expect(xml).toContain('spc="150"') |
| 76 | expect(xml).toContain('kern="0"') |
| 77 | }) |
| 78 | |
| 79 | it('places geometry at exactly 9525 EMU per pixel', async () => { |
| 80 | const xml = await slideXml([ |
| 81 | slide([ |
| 82 | { |
| 83 | kind: 'box', |
| 84 | sourceId: 1, |
| 85 | rect: { x: 100, y: 50, w: 200, h: 25 }, |
| 86 | fill: BLACK, |
| 87 | }, |
| 88 | ]), |
| 89 | ]) |
| 90 | // Integer EMU throughout, so there is no rounding drift to chase later. |
| 91 | expect(xml).toContain('x="952500"') // 100 * 9525 |
| 92 | expect(xml).toContain('y="476250"') // 50 * 9525 |
| 93 | expect(xml).toContain('cx="1905000"') // 200 * 9525 |
| 94 | expect(xml).toContain('cy="238125"') // 25 * 9525 |
| 95 | }) |
| 96 | |
| 97 | it('writes a corner radius that is not zero', async () => { |
| 98 | const xml = await slideXml([ |
| 99 | slide([ |
| 100 | { |
| 101 | kind: 'box', |
| 102 | sourceId: 1, |
| 103 | rect: { x: 0, y: 0, w: 200, h: 100 }, |
| 104 | fill: BLACK, |
| 105 | radius: 12, |
| 106 | }, |
| 107 | ]), |
| 108 | ]) |
| 109 | expect(xml).toContain('prst="roundRect"') |
| 110 | // OOXML states the corner adjustment as hundred-thousandths of the shorter |
| 111 | // side. 12px of a 100px side is 12%, so exactly 12000. |
| 112 | // |
| 113 | // Asserted exactly, not as "greater than zero": `rectRadius` is documented |
| 114 | // as a 0.0-1.0 fraction but is really inches, and the wrong reading still |
| 115 | // produces a non-zero adjustment (11520 here). A loose assertion passes on |
| 116 | // both and pins nothing, which is what this test caught the first time. |
| 117 | expect(xml).toContain('<a:gd name="adj" fmla="val 12000"/>') |
| 118 | }) |
| 119 | }) |
| 120 | |
| 121 | describe('fills and borders', () => { |
| 122 | it('maps alpha to transparency', async () => { |
| 123 | const xml = await slideXml([ |
| 124 | slide([ |
| 125 | { |
| 126 | kind: 'box', |
| 127 | sourceId: 1, |
| 128 | rect: { x: 0, y: 0, w: 10, h: 10 }, |
| 129 | fill: { r: 255, g: 0, b: 0, a: 0.6 }, |
| 130 | }, |
| 131 | ]), |
| 132 | ]) |
| 133 | // 0.6 opacity is 40% transparent, and OOXML alpha is the inverse again, |
| 134 | // in thousandths of a percent. |
| 135 | expect(xml).toContain('<a:alpha val="60000"/>') |
| 136 | }) |
| 137 | |
| 138 | it('emits no alpha element for an opaque fill', async () => { |
| 139 | const xml = await slideXml([ |
| 140 | slide([ |
| 141 | { |
| 142 | kind: 'box', |
| 143 | sourceId: 1, |
| 144 | rect: { x: 0, y: 0, w: 10, h: 10 }, |
| 145 | fill: BLACK, |
| 146 | }, |
| 147 | ]), |
| 148 | ]) |
| 149 | expect(xml).not.toContain('<a:alpha') |
| 150 | }) |
| 151 | |
| 152 | it('uses one uniform line when all four borders match', async () => { |
| 153 | const border = { width: 2, color: BLACK, style: 'solid' as const } |
| 154 | const xml = await slideXml([ |
| 155 | slide([ |
| 156 | { |
| 157 | kind: 'box', |
| 158 | sourceId: 1, |
| 159 | rect: { x: 0, y: 0, w: 100, h: 100 }, |
| 160 | borders: [border, border, border, border], |
| 161 | }, |
| 162 | ]), |
| 163 | ]) |
| 164 | // One shape, not five. |
| 165 | expect(xml.match(/<p:sp>/g)).toHaveLength(1) |
| 166 | }) |
| 167 | |
| 168 | it('splits differing borders into one rectangle per side', async () => { |
| 169 | const thick = { width: 4, color: BLACK, style: 'solid' as const } |
| 170 | const xml = await slideXml([ |
| 171 | slide([ |
| 172 | { |
| 173 | kind: 'box', |
| 174 | sourceId: 1, |
| 175 | rect: { x: 0, y: 0, w: 100, h: 100 }, |
| 176 | borders: [thick, undefined, undefined, undefined], |
| 177 | }, |
| 178 | ]), |
| 179 | ]) |
| 180 | // Just the edge. pptxgenjs gives a shape a single uniform border, so a |
| 181 | // left-accent bar or a single top rule has to be its own rectangle, and |
| 182 | // the box that would have carried it has neither fill nor border left to |
| 183 | // draw. Emitting it anyway left PowerPoint to resolve a shape with no fill |
| 184 | // and no outline from its default style. |
| 185 | expect(xml.match(/<p:sp>/g)).toHaveLength(1) |
| 186 | expect(xml).toContain('cy="38100"') // the 4px edge: 4 * 9525 |
| 187 | }) |
| 188 | }) |
| 189 | |
| 190 | describe('text', () => { |
| 191 | it('emits a real line break for a soft break, not a vertical tab', async () => { |
| 192 | const xml = await slideXml([ |
| 193 | slide([ |
| 194 | textNode([ |
| 195 | { text: 'first', fontSize: 16, fontFamily: 'Arial' }, |
| 196 | { |
| 197 | text: 'second', |
| 198 | fontSize: 16, |
| 199 | fontFamily: 'Arial', |
| 200 | breakBefore: true, |
| 201 | }, |
| 202 | ]), |
| 203 | ]), |
| 204 | ]) |
| 205 | expect(xml).toContain('<a:br/>') |
| 206 | expect(xml).not.toContain('\v') |
| 207 | }) |
| 208 | |
| 209 | it('keeps the midpoint of a centered line when adding slack', async () => { |
| 210 | const centered = { |
| 211 | ...textNode([ |
| 212 | { text: 'x', fontSize: 16, fontFamily: 'Arial' }, |
| 213 | ]), |
| 214 | align: 'center', |
| 215 | } |
| 216 | const xml = await slideXml([ |
| 217 | slide([centered]), |
| 218 | ]) |
| 219 | // The box is widened by 12px so PowerPoint's wider metrics do not force a |
| 220 | // wrap, so its origin moves left by half of that to hold the center still. |
| 221 | expect(xml).toContain('x="-57150"') // -6 * 9525 |
| 222 | expect(xml).toContain('cx="2019300"') // 212 * 9525 |
| 223 | }) |
| 224 | |
| 225 | it('turns off wrapping for a single line and on for several', async () => { |
| 226 | const one = await slideXml([ |
| 227 | slide([ |
| 228 | textNode([ |
| 229 | { text: 'x', fontSize: 16, fontFamily: 'Arial' }, |
| 230 | ]), |
| 231 | ]), |
| 232 | ]) |
| 233 | expect(one).toContain('wrap="none"') |
| 234 | |
| 235 | const many = await slideXml([ |
| 236 | slide([ |
| 237 | { |
| 238 | ...textNode([ |
| 239 | { text: 'x', fontSize: 16, fontFamily: 'Arial' }, |
| 240 | ]), |
| 241 | lineCount: 3, |
| 242 | }, |
| 243 | ]), |
| 244 | ]) |
| 245 | expect(many).not.toContain('wrap="none"') |
| 246 | }) |
| 247 | }) |
| 248 | |
| 249 | describe('slide assembly', () => { |
| 250 | it('writes notes into the notes slide', async () => { |
| 251 | const buffer = await buildPptx( |
| 252 | PptxGenJS, |
| 253 | [ |
| 254 | { |
| 255 | ...slide([]), |
| 256 | note: 'Say this part out loud.', |
| 257 | }, |
| 258 | ], |
| 259 | { width: 980, height: 552 }, |
| 260 | ) |
| 261 | const zip = await JSZip.loadAsync(buffer) |
| 262 | const notes = await zip.file('ppt/notesSlides/notesSlide1.xml')!.async('string') |
| 263 | expect(notes).toContain('Say this part out loud.') |
| 264 | }) |
| 265 | |
| 266 | it('falls back to a background picture and draws nothing else', async () => { |
| 267 | const png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==' |
| 268 | const xml = await slideXml([ |
| 269 | { |
| 270 | ...slide([ |
| 271 | { |
| 272 | kind: 'box', |
| 273 | sourceId: 1, |
| 274 | rect: { x: 0, y: 0, w: 10, h: 10 }, |
| 275 | fill: BLACK, |
| 276 | }, |
| 277 | ]), |
| 278 | fallbackPng: png, |
| 279 | fallbackReason: 'walker timed out', |
| 280 | }, |
| 281 | ]) |
| 282 | // The safety valve has to be exclusive. Drawing the shapes as well would |
| 283 | // put half-measured content on top of a correct picture of the same thing. |
| 284 | expect(xml).not.toContain('<p:sp>') |
| 285 | }) |
| 286 | |
| 287 | it('produces a non-empty shape tree, which the image exporter never does', async () => { |
| 288 | const xml = await slideXml([ |
| 289 | slide([ |
| 290 | textNode([ |
| 291 | { text: 'Selectable', fontSize: 16, fontFamily: 'Arial' }, |
| 292 | ]), |
| 293 | ]), |
| 294 | ]) |
| 295 | expect(xml).toContain('<a:t>Selectable</a:t>') |
| 296 | }) |
| 297 | }) |
| 298 | |
| 299 | describe('a hyperlink is not underlined unless CSS says so', () => { |
| 300 | it('suppresses the underline pptxgenjs adds to every link', async () => { |
| 301 | // pptxgenjs writes `u="sng"` for any run carrying a hyperlink unless |
| 302 | // `underline` is set. Slidev's own themes rule links with a dashed |
| 303 | // `border-bottom`, which is already drawn as its own shape, so the deck |
| 304 | // came out with two lines under every link. |
| 305 | const xml = await slideXml([ |
| 306 | slide([ |
| 307 | textNode([ |
| 308 | { |
| 309 | text: 'Documentation', |
| 310 | fontSize: 16, |
| 311 | fontFamily: 'Arial', |
| 312 | link: 'https://sli.dev', |
| 313 | }, |
| 314 | ]), |
| 315 | ]), |
| 316 | ]) |
| 317 | expect(xml).toContain('u="none"') |
| 318 | expect(xml).not.toContain('u="sng"') |
| 319 | }) |
| 320 | |
| 321 | it('still underlines a run whose CSS asks for it', async () => { |
| 322 | const xml = await slideXml([ |
| 323 | slide([ |
| 324 | textNode([ |
| 325 | { |
| 326 | text: 'Documentation', |
| 327 | fontSize: 16, |
| 328 | fontFamily: 'Arial', |
| 329 | link: 'https://sli.dev', |
| 330 | underline: true, |
| 331 | }, |
| 332 | ]), |
| 333 | ]), |
| 334 | ]) |
| 335 | expect(xml).toContain('u="sng"') |
| 336 | }) |
| 337 | }) |
| 338 | |
| 339 | describe('a dashed border keeps its dashes', () => { |
| 340 | const dashed = { width: 1, color: BLACK, style: 'dashed' as const } |
| 341 | |
| 342 | it('draws a dashed edge as a line, not a filled bar', async () => { |
| 343 | // A filled rectangle cannot carry a dash pattern. Slidev rules its links |
| 344 | // with `border-bottom: 1px dashed`, so every link in a deck came out with |
| 345 | // a solid bar under it. |
| 346 | const xml = await slideXml([ |
| 347 | slide([ |
| 348 | { |
| 349 | kind: 'box', |
| 350 | sourceId: 1, |
| 351 | rect: { x: 0, y: 0, w: 100, h: 20 }, |
| 352 | borders: [ |
| 353 | undefined, |
| 354 | undefined, |
| 355 | dashed, |
| 356 | undefined, |
| 357 | ], |
| 358 | }, |
| 359 | ]), |
| 360 | ]) |
| 361 | expect(xml).toContain('<a:prstDash val="dash"/>') |
| 362 | expect(xml).toContain('prst="line"') |
| 363 | }) |
| 364 | |
| 365 | it('leaves a solid edge as a filled bar', async () => { |
| 366 | const xml = await slideXml([ |
| 367 | slide([ |
| 368 | { |
| 369 | kind: 'box', |
| 370 | sourceId: 1, |
| 371 | rect: { x: 0, y: 0, w: 100, h: 20 }, |
| 372 | borders: [ |
| 373 | undefined, |
| 374 | undefined, |
| 375 | { width: 1, color: BLACK, style: 'solid' as const }, |
| 376 | undefined, |
| 377 | ], |
| 378 | }, |
| 379 | ]), |
| 380 | ]) |
| 381 | expect(xml).not.toContain('prst="line"') |
| 382 | }) |
| 383 | }) |
| 384 | |
| 385 | describe('a fill is stated, never inherited', () => { |
| 386 | it('writes an explicit noFill for a shape that has none', async () => { |
| 387 | // pptxgenjs writes `<a:noFill/>` for an ABSENT fill and nothing at all for |
| 388 | // `{ type: 'none' }`, so the explicit-looking version was the one that |
| 389 | // inherited the default shape style. |
| 390 | const xml = await slideXml([ |
| 391 | slide([ |
| 392 | { |
| 393 | kind: 'box', |
| 394 | sourceId: 1, |
| 395 | rect: { x: 0, y: 0, w: 100, h: 100 }, |
| 396 | borders: [ |
| 397 | { width: 1, color: BLACK, style: 'solid' as const }, |
| 398 | { width: 1, color: BLACK, style: 'solid' as const }, |
| 399 | { width: 1, color: BLACK, style: 'solid' as const }, |
| 400 | { width: 1, color: BLACK, style: 'solid' as const }, |
| 401 | ], |
| 402 | }, |
| 403 | ]), |
| 404 | ]) |
| 405 | expect(xml).toContain('<a:noFill/>') |
| 406 | }) |
| 407 | }) |
| 408 |