@open-slide/core 2.0.0-beta.0 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/build-Bzy-qlGc.js +19 -0
  2. package/dist/cli/bin.js +201 -21
  3. package/dist/{config-AtA2Bqtn.d.ts → config-DjWifhaA.d.ts} +1 -1
  4. package/dist/{dev-BChELmc-.js → dev-MXa5Tod4.js} +36 -10
  5. package/dist/index.d.ts +3 -2
  6. package/dist/index.js +1 -1
  7. package/dist/locale/index.d.ts +1 -1
  8. package/dist/locale/index.js +1 -1
  9. package/dist/preview-DEpQcKz3.js +24 -0
  10. package/dist/{types-BzPSOxe-.d.ts → types-BWg-Dtbg.d.ts} +1 -2
  11. package/dist/vite/index.d.ts +1 -1
  12. package/dist/vite/index.js +1 -1
  13. package/dist/{zh-tw-BxZ_pYqe.js → zh-tw-DlKyeNX7.js} +4 -8
  14. package/package.json +1 -1
  15. package/skills/slide-authoring/SKILL.md +3 -3
  16. package/skills/slide-authoring/references/morph.md +5 -5
  17. package/skills/slide-authoring/references/transitions.md +38 -37
  18. package/skills/slide-authoring/references/webfonts.md +25 -6
  19. package/src/app/components/command/slide-command-menu.tsx +10 -0
  20. package/src/app/components/slide-transition-layer.tsx +8 -1
  21. package/src/app/lib/export-pptx.ts +122 -193
  22. package/src/app/lib/pptx/color.ts +49 -0
  23. package/src/app/lib/pptx/css.test.ts +172 -0
  24. package/src/app/lib/pptx/css.ts +450 -0
  25. package/src/app/lib/pptx/fonts.ts +145 -0
  26. package/src/app/lib/pptx/images.ts +116 -0
  27. package/src/app/lib/pptx/measure.ts +1299 -0
  28. package/src/app/lib/pptx/ooxml.test.ts +199 -0
  29. package/src/app/lib/pptx/ooxml.ts +470 -0
  30. package/src/app/lib/pptx/raster.ts +189 -0
  31. package/src/app/lib/pptx/render.ts +102 -0
  32. package/src/app/lib/pptx/scene.ts +116 -0
  33. package/src/app/lib/pptx/text-lines.test.ts +82 -0
  34. package/src/app/lib/pptx/text-lines.ts +109 -0
  35. package/src/app/lib/pptx/units.ts +22 -0
  36. package/src/app/lib/transition.test.ts +31 -0
  37. package/src/app/lib/transition.ts +13 -0
  38. package/src/app/routes/slide.tsx +21 -27
  39. package/src/locale/en.ts +1 -3
  40. package/src/locale/ja.ts +1 -3
  41. package/src/locale/types.ts +1 -2
  42. package/src/locale/zh-cn.ts +1 -2
  43. package/src/locale/zh-tw.ts +1 -2
  44. package/dist/build-CfAxF6Dm.js +0 -14
  45. package/dist/preview-CcCQa268.js +0 -14
  46. package/dist/{config-C_VI2EpT.js → config-nbSvY7z_.js} +2 -2
@@ -0,0 +1,145 @@
1
+ import { parseFontFamilies } from './css';
2
+
3
+ type Platform = 'mac' | 'win' | 'other';
4
+
5
+ const GENERIC = new Set([
6
+ 'system-ui',
7
+ '-apple-system',
8
+ 'blinkmacsystemfont',
9
+ 'ui-sans-serif',
10
+ 'ui-serif',
11
+ 'ui-monospace',
12
+ 'ui-rounded',
13
+ 'sans-serif',
14
+ 'serif',
15
+ 'monospace',
16
+ 'cursive',
17
+ 'fantasy',
18
+ 'math',
19
+ 'emoji',
20
+ 'fangsong',
21
+ ]);
22
+
23
+ const GENERIC_MAP: Record<Platform, Record<string, string>> = {
24
+ mac: {
25
+ system: 'Helvetica Neue',
26
+ sans: 'Helvetica',
27
+ serif: 'Times New Roman',
28
+ mono: 'Menlo',
29
+ cursive: 'Apple Chancery',
30
+ fantasy: 'Papyrus',
31
+ },
32
+ win: {
33
+ system: 'Segoe UI',
34
+ sans: 'Arial',
35
+ serif: 'Times New Roman',
36
+ mono: 'Consolas',
37
+ cursive: 'Comic Sans MS',
38
+ fantasy: 'Impact',
39
+ },
40
+ other: {
41
+ system: 'Arial',
42
+ sans: 'Arial',
43
+ serif: 'Times New Roman',
44
+ mono: 'Courier New',
45
+ cursive: 'Comic Sans MS',
46
+ fantasy: 'Impact',
47
+ },
48
+ };
49
+
50
+ function detectPlatform(): Platform {
51
+ if (typeof navigator === 'undefined') return 'other';
52
+ const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
53
+ const p = (nav.userAgentData?.platform ?? navigator.platform ?? '').toLowerCase();
54
+ if (p.includes('mac') || p.includes('iphone') || p.includes('ipad')) return 'mac';
55
+ if (p.includes('win')) return 'win';
56
+ return 'other';
57
+ }
58
+
59
+ function mapGeneric(family: string, platform: Platform): string {
60
+ const map = GENERIC_MAP[platform];
61
+ switch (family.toLowerCase()) {
62
+ case 'system-ui':
63
+ case '-apple-system':
64
+ case 'blinkmacsystemfont':
65
+ case 'ui-sans-serif':
66
+ case 'ui-rounded':
67
+ return map.system;
68
+ case 'serif':
69
+ case 'ui-serif':
70
+ case 'fangsong':
71
+ return map.serif;
72
+ case 'monospace':
73
+ case 'ui-monospace':
74
+ return map.mono;
75
+ case 'cursive':
76
+ return map.cursive;
77
+ case 'fantasy':
78
+ return map.fantasy;
79
+ case 'math':
80
+ return 'Cambria Math';
81
+ default:
82
+ return map.sans;
83
+ }
84
+ }
85
+
86
+ const PROBE_TEXT = 'mmmmmmmmmmlliWWwwIIi@#0123';
87
+ const availability = new Map<string, boolean>();
88
+ const resolved = new Map<string, string>();
89
+ let probeCtx: CanvasRenderingContext2D | null | undefined;
90
+
91
+ function probe(): CanvasRenderingContext2D | null {
92
+ if (probeCtx === undefined) {
93
+ probeCtx = document.createElement('canvas').getContext('2d');
94
+ }
95
+ return probeCtx;
96
+ }
97
+
98
+ function widthWith(ctx: CanvasRenderingContext2D, font: string): number {
99
+ ctx.font = `72px ${font}`;
100
+ return ctx.measureText(PROBE_TEXT).width;
101
+ }
102
+
103
+ export function isFontAvailable(family: string): boolean {
104
+ const cached = availability.get(family);
105
+ if (cached !== undefined) return cached;
106
+ const ctx = probe();
107
+ let available = false;
108
+ if (ctx) {
109
+ const quoted = `"${family.replace(/"/g, '')}"`;
110
+ for (const base of ['monospace', 'serif', 'sans-serif']) {
111
+ if (Math.abs(widthWith(ctx, `${quoted}, ${base}`) - widthWith(ctx, base)) > 0.01) {
112
+ available = true;
113
+ break;
114
+ }
115
+ }
116
+ }
117
+ availability.set(family, available);
118
+ return available;
119
+ }
120
+
121
+ /**
122
+ * PowerPoint needs one concrete family name, so pick the first entry of the CSS
123
+ * stack that the browser can actually render, mapping generic keywords to the
124
+ * platform's default face.
125
+ */
126
+ export function resolveFontFamily(stack: string): string {
127
+ const cached = resolved.get(stack);
128
+ if (cached) return cached;
129
+ const platform = detectPlatform();
130
+ let result: string | null = null;
131
+ for (const family of parseFontFamilies(stack)) {
132
+ if (!family) continue;
133
+ if (GENERIC.has(family.toLowerCase())) {
134
+ result = mapGeneric(family, platform);
135
+ break;
136
+ }
137
+ if (isFontAvailable(family)) {
138
+ result = family;
139
+ break;
140
+ }
141
+ }
142
+ const out = result ?? mapGeneric('sans-serif', platform);
143
+ resolved.set(stack, out);
144
+ return out;
145
+ }
@@ -0,0 +1,116 @@
1
+ import type { ImageFormat, SceneImage } from './scene';
2
+
3
+ export type LoadedImage = { id: number; width: number; height: number };
4
+
5
+ function sniffFormat(bytes: Uint8Array): ImageFormat | 'other' {
6
+ if (bytes.length > 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e) return 'png';
7
+ if (bytes.length > 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)
8
+ return 'jpeg';
9
+ if (bytes.length > 6 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46) return 'gif';
10
+ const head = new TextDecoder('utf-8', { fatal: false })
11
+ .decode(bytes.subarray(0, Math.min(bytes.length, 512)))
12
+ .trimStart();
13
+ if (head.startsWith('<') && /<svg[\s>]/i.test(head)) return 'svg';
14
+ return 'other';
15
+ }
16
+
17
+ async function decodeSize(blob: Blob): Promise<{ width: number; height: number } | null> {
18
+ try {
19
+ const bitmap = await createImageBitmap(blob);
20
+ const size = { width: bitmap.width, height: bitmap.height };
21
+ bitmap.close();
22
+ return size;
23
+ } catch {
24
+ return null;
25
+ }
26
+ }
27
+
28
+ async function encodePng(
29
+ source: CanvasImageSource,
30
+ width: number,
31
+ height: number,
32
+ ): Promise<Uint8Array | null> {
33
+ const canvas = document.createElement('canvas');
34
+ canvas.width = Math.max(1, Math.round(width));
35
+ canvas.height = Math.max(1, Math.round(height));
36
+ const ctx = canvas.getContext('2d');
37
+ if (!ctx) return null;
38
+ try {
39
+ ctx.drawImage(source, 0, 0, canvas.width, canvas.height);
40
+ const blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, 'image/png'));
41
+ return blob ? new Uint8Array(await blob.arrayBuffer()) : null;
42
+ } catch {
43
+ return null;
44
+ }
45
+ }
46
+
47
+ function loadImageElement(url: string): Promise<HTMLImageElement | null> {
48
+ return new Promise((resolve) => {
49
+ const img = new Image();
50
+ img.crossOrigin = 'anonymous';
51
+ img.onload = () => resolve(img);
52
+ img.onerror = () => resolve(null);
53
+ img.src = url;
54
+ });
55
+ }
56
+
57
+ const SVG_RASTER_SCALE = 2;
58
+
59
+ export class ImageStore {
60
+ readonly images: SceneImage[] = [];
61
+ private readonly byKey = new Map<string, Promise<LoadedImage | null>>();
62
+ private nextId = 1;
63
+
64
+ add(bytes: Uint8Array, format: ImageFormat, svg?: Uint8Array): number {
65
+ const id = this.nextId++;
66
+ this.images.push({ id, bytes, format, svg });
67
+ return id;
68
+ }
69
+
70
+ fromUrl(url: string): Promise<LoadedImage | null> {
71
+ let pending = this.byKey.get(url);
72
+ if (!pending) {
73
+ pending = this.load(url);
74
+ this.byKey.set(url, pending);
75
+ }
76
+ return pending;
77
+ }
78
+
79
+ private async load(url: string): Promise<LoadedImage | null> {
80
+ let bytes: Uint8Array | null = null;
81
+ try {
82
+ const res = await fetch(url);
83
+ if (res.ok) bytes = new Uint8Array(await res.arrayBuffer());
84
+ } catch {}
85
+
86
+ if (bytes) {
87
+ const format = sniffFormat(bytes);
88
+ if (format === 'png' || format === 'jpeg' || format === 'gif') {
89
+ const size = await decodeSize(new Blob([bytes as BlobPart]));
90
+ if (!size) return null;
91
+ return { id: this.add(bytes, format), ...size };
92
+ }
93
+ if (format === 'svg') {
94
+ const img = await loadImageElement(url);
95
+ if (!img || img.naturalWidth === 0) return null;
96
+ const png = await encodePng(
97
+ img,
98
+ img.naturalWidth * SVG_RASTER_SCALE,
99
+ img.naturalHeight * SVG_RASTER_SCALE,
100
+ );
101
+ if (!png) return null;
102
+ return {
103
+ id: this.add(png, 'png', bytes),
104
+ width: img.naturalWidth,
105
+ height: img.naturalHeight,
106
+ };
107
+ }
108
+ }
109
+
110
+ const img = await loadImageElement(url);
111
+ if (!img || img.naturalWidth === 0) return null;
112
+ const png = await encodePng(img, img.naturalWidth, img.naturalHeight);
113
+ if (!png) return null;
114
+ return { id: this.add(png, 'png'), width: img.naturalWidth, height: img.naturalHeight };
115
+ }
116
+ }