@file-viewer/assets-ppt 3.3.0 → 3.3.3
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.
- package/README.en.md +3 -1
- package/README.md +3 -1
- package/README.zh-CN.md +3 -1
- package/package.json +96 -9
- package/viewer/THIRD_PARTY_LICENSES.assets.json +837 -54
- package/viewer/file-viewer-asset-pack.json +20 -31
- package/viewer/vendor/ppt/LICENSE +160 -122
- package/viewer/vendor/ppt/NOTICE.md +30 -0
- package/viewer/vendor/ppt/README.md +86 -0
- package/viewer/vendor/ppt/dist/core/binary.d.ts +31 -0
- package/viewer/vendor/ppt/dist/core/binary.js +102 -0
- package/viewer/vendor/ppt/dist/core/cfb.d.ts +38 -0
- package/viewer/vendor/ppt/dist/core/cfb.js +208 -0
- package/viewer/vendor/ppt/dist/core/crypto.d.ts +12 -0
- package/viewer/vendor/ppt/dist/core/crypto.js +64 -0
- package/viewer/vendor/ppt/dist/core/errors.d.ts +5 -0
- package/viewer/vendor/ppt/dist/core/errors.js +5 -0
- package/viewer/vendor/ppt/dist/core/inflate.d.ts +11 -0
- package/viewer/vendor/ppt/dist/core/inflate.js +337 -0
- package/viewer/vendor/ppt/dist/core/pict.d.ts +14 -0
- package/viewer/vendor/ppt/dist/core/pict.js +472 -0
- package/viewer/vendor/ppt/dist/core/raster.d.ts +7 -0
- package/viewer/vendor/ppt/dist/core/raster.js +101 -0
- package/viewer/vendor/ppt/dist/core/tiff.d.ts +20 -0
- package/viewer/vendor/ppt/dist/core/tiff.js +254 -0
- package/viewer/vendor/ppt/dist/core/utils.d.ts +9 -0
- package/viewer/vendor/ppt/dist/core/utils.js +13 -0
- package/viewer/vendor/ppt/dist/core/vector-font.d.ts +12 -0
- package/viewer/vendor/ppt/dist/core/vector-font.js +29 -0
- package/viewer/vendor/ppt/dist/core/vector.d.ts +19 -0
- package/viewer/vendor/ppt/dist/core/vector.js +1570 -0
- package/viewer/vendor/ppt/dist/core/xml-text.d.ts +1 -0
- package/viewer/vendor/ppt/dist/core/xml-text.js +6 -0
- package/viewer/vendor/ppt/dist/index.d.ts +12 -0
- package/viewer/vendor/ppt/dist/index.js +8 -0
- package/viewer/vendor/ppt/dist/layout/measure.d.ts +15 -0
- package/viewer/vendor/ppt/dist/layout/measure.js +54 -0
- package/viewer/vendor/ppt/dist/layout/text-layout.d.ts +48 -0
- package/viewer/vendor/ppt/dist/layout/text-layout.js +272 -0
- package/viewer/vendor/ppt/dist/manifest.json +13 -0
- package/viewer/vendor/ppt/dist/playback/media.d.ts +11 -0
- package/viewer/vendor/ppt/dist/playback/media.js +37 -0
- package/viewer/vendor/ppt/dist/playback/navigation.d.ts +2 -0
- package/viewer/vendor/ppt/dist/playback/navigation.js +14 -0
- package/viewer/vendor/ppt/dist/playback/timeline.d.ts +47 -0
- package/viewer/vendor/ppt/dist/playback/timeline.js +154 -0
- package/viewer/vendor/ppt/dist/playback/transitions.d.ts +14 -0
- package/viewer/vendor/ppt/dist/playback/transitions.js +41 -0
- package/viewer/vendor/ppt/dist/ppt/animation.d.ts +3 -0
- package/viewer/vendor/ppt/dist/ppt/animation.js +67 -0
- package/viewer/vendor/ppt/dist/ppt/constants.d.ts +72 -0
- package/viewer/vendor/ppt/dist/ppt/constants.js +21 -0
- package/viewer/vendor/ppt/dist/ppt/drawing.d.ts +15 -0
- package/viewer/vendor/ppt/dist/ppt/drawing.js +508 -0
- package/viewer/vendor/ppt/dist/ppt/encryption.d.ts +14 -0
- package/viewer/vendor/ppt/dist/ppt/encryption.js +241 -0
- package/viewer/vendor/ppt/dist/ppt/extras.d.ts +14 -0
- package/viewer/vendor/ppt/dist/ppt/extras.js +225 -0
- package/viewer/vendor/ppt/dist/ppt/geometry.d.ts +29 -0
- package/viewer/vendor/ppt/dist/ppt/geometry.js +448 -0
- package/viewer/vendor/ppt/dist/ppt/headers-footers.d.ts +11 -0
- package/viewer/vendor/ppt/dist/ppt/headers-footers.js +97 -0
- package/viewer/vendor/ppt/dist/ppt/interactions.d.ts +12 -0
- package/viewer/vendor/ppt/dist/ppt/interactions.js +104 -0
- package/viewer/vendor/ppt/dist/ppt/parser.d.ts +12 -0
- package/viewer/vendor/ppt/dist/ppt/parser.js +356 -0
- package/viewer/vendor/ppt/dist/ppt/pictures.d.ts +10 -0
- package/viewer/vendor/ppt/dist/ppt/pictures.js +215 -0
- package/viewer/vendor/ppt/dist/ppt/records.d.ts +13 -0
- package/viewer/vendor/ppt/dist/ppt/records.js +49 -0
- package/viewer/vendor/ppt/dist/ppt/text.d.ts +19 -0
- package/viewer/vendor/ppt/dist/ppt/text.js +789 -0
- package/viewer/vendor/ppt/dist/pptjs.browser.js +9651 -0
- package/viewer/vendor/ppt/dist/pptjs.mjs +9663 -0
- package/viewer/vendor/ppt/dist/pptjs.worker.js +9650 -0
- package/viewer/vendor/ppt/dist/render/effects.d.ts +23 -0
- package/viewer/vendor/ppt/dist/render/effects.js +125 -0
- package/viewer/vendor/ppt/dist/render/fields.d.ts +11 -0
- package/viewer/vendor/ppt/dist/render/fields.js +176 -0
- package/viewer/vendor/ppt/dist/render/geometry.d.ts +11 -0
- package/viewer/vendor/ppt/dist/render/geometry.js +131 -0
- package/viewer/vendor/ppt/dist/render/safe.d.ts +12 -0
- package/viewer/vendor/ppt/dist/render/safe.js +48 -0
- package/viewer/vendor/ppt/dist/render/svg.d.ts +4 -0
- package/viewer/vendor/ppt/dist/render/svg.js +279 -0
- package/viewer/vendor/ppt/dist/render/text.d.ts +9 -0
- package/viewer/vendor/ppt/dist/render/text.js +181 -0
- package/viewer/vendor/ppt/dist/render/three-d.d.ts +32 -0
- package/viewer/vendor/ppt/dist/render/three-d.js +219 -0
- package/viewer/vendor/ppt/dist/render/wordart.d.ts +16 -0
- package/viewer/vendor/ppt/dist/render/wordart.js +214 -0
- package/viewer/vendor/ppt/dist/runtime.d.ts +125 -0
- package/viewer/vendor/ppt/dist/runtime.js +226 -0
- package/viewer/vendor/ppt/dist/types.d.ts +546 -0
- package/viewer/vendor/ppt/dist/types.js +1 -0
- package/viewer/vendor/ppt/dist/viewer.d.ts +3 -0
- package/viewer/vendor/ppt/dist/viewer.js +264 -0
- package/viewer/vendor/ppt/dist/worker-client.d.ts +25 -0
- package/viewer/vendor/ppt/dist/worker-client.js +61 -0
- package/viewer/vendor/ppt/dist/worker.d.ts +1 -0
- package/viewer/vendor/ppt/dist/worker.js +25 -0
- package/viewer/vendor/ppt/package.json +40 -52
- package/viewer/vendor/ppt/NOTICE +0 -229
- package/viewer/vendor/ppt/frame-cache.mjs +0 -731
- package/viewer/vendor/ppt/index.mjs +0 -941
- package/viewer/vendor/ppt/manifest.json +0 -44
- package/viewer/vendor/ppt/ppt-font-cjk.otf +0 -0
- package/viewer/vendor/ppt/ppt-native.wasm +0 -0
- package/viewer/vendor/ppt/worker.mjs +0 -1720
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { PptError } from '../core/errors.js';
|
|
2
|
+
import { base64, color, escapeXml, finite, imageUrl, linkUrl, n, transformNumber } from './safe.js';
|
|
3
|
+
import { mapPathToBounds, presetGeometry } from './geometry.js';
|
|
4
|
+
import { renderText } from './text.js';
|
|
5
|
+
import { headerFooterVisible, resolveTextFields } from './fields.js';
|
|
6
|
+
import { renderGradient, renderShadow } from './effects.js';
|
|
7
|
+
import { renderWordArt } from './wordart.js';
|
|
8
|
+
import { renderExtrusion } from './three-d.js';
|
|
9
|
+
let renderSequence = 0;
|
|
10
|
+
function nextId(ctx) { return `${ctx.prefix}-${++ctx.seq}`; }
|
|
11
|
+
function sceneColor(value, index, ctx, fallback = '#000000') { const rgb = index == null ? undefined : ctx.slide.colorScheme?.[index]; return rgb == null ? color(value, fallback) : `#${[rgb & 255, (rgb >>> 8) & 255, (rgb >>> 16) & 255].map(v => v.toString(16).padStart(2, '0')).join('')}`; }
|
|
12
|
+
function effectContext(ctx) { return { id: nextId(ctx), defs: ctx.defs, resolveColor: (value, index, fallback) => sceneColor(value, index, ctx, fallback), warnings: ctx.warnings, slideId: ctx.slide.id, textMeasurer: ctx.options.textMeasurer, bounds: ctx.slide.size }; }
|
|
13
|
+
function assetUrl(ctx, id) {
|
|
14
|
+
if (ctx.urls.has(id))
|
|
15
|
+
return ctx.urls.get(id);
|
|
16
|
+
const asset = ctx.assets.get(id);
|
|
17
|
+
let url;
|
|
18
|
+
if (asset && !asset.unsupported) {
|
|
19
|
+
if (ctx.options.assetUrl)
|
|
20
|
+
url = imageUrl(ctx.options.assetUrl(asset));
|
|
21
|
+
else if (/^image\/(png|jpeg|gif|bmp|webp|svg\+xml)$/.test(asset.mimeType))
|
|
22
|
+
url = `data:${asset.mimeType};base64,${base64(asset.bytes)}`;
|
|
23
|
+
}
|
|
24
|
+
ctx.urls.set(id, url);
|
|
25
|
+
return url;
|
|
26
|
+
}
|
|
27
|
+
function paint(p, ctx, shape) {
|
|
28
|
+
const opacity = Math.max(0, Math.min(1, finite(p.opacity, 1)));
|
|
29
|
+
if (p.kind === 'none')
|
|
30
|
+
return 'fill="none"';
|
|
31
|
+
if (p.kind === 'solid')
|
|
32
|
+
return `fill="${sceneColor(p.color, p.colorIndex, ctx, '#ffffff')}" fill-opacity="${n(opacity)}"`;
|
|
33
|
+
if (p.kind === 'gradient')
|
|
34
|
+
return renderGradient(p, effectContext(ctx), shape);
|
|
35
|
+
const url = p.assetId ? assetUrl(ctx, p.assetId) : undefined;
|
|
36
|
+
if (url) {
|
|
37
|
+
const id = nextId(ctx), asset = p.assetId ? ctx.assets.get(p.assetId) : undefined, tiled = p.imageStyle === 'pattern' || p.imageStyle === 'texture';
|
|
38
|
+
const width = tiled ? Math.max(1, finite(p.tileWidth, asset?.width ?? 8)) : 1, height = tiled ? Math.max(1, finite(p.tileHeight, asset?.height ?? 8)) : 1;
|
|
39
|
+
let filter = '';
|
|
40
|
+
if (p.imageStyle === 'pattern') {
|
|
41
|
+
// Pattern BLIPs are masks: black selects fillColor, white fillBackColor.
|
|
42
|
+
// Keep their native pixel period instead of stretching an eight-pixel mask
|
|
43
|
+
// across the entire page. Interpolate alpha separately for both paints.
|
|
44
|
+
const foreground = sceneColor(p.color, p.colorIndex, ctx, '#ffffff'), background = sceneColor(p.backColor, p.backColorIndex, ctx, '#ffffff');
|
|
45
|
+
const rgb = (value) => { const hex = value.replace(/^#/, ''); return [0, 1, 2].map(i => { const component = hex.length <= 4 ? hex[i]?.repeat(2) : hex.slice(i * 2, i * 2 + 2); return parseInt(component ?? '00', 16) / 255 || 0; }); }, front = rgb(foreground), back = rgb(background), fid = nextId(ctx), alpha = Math.max(0, Math.min(1, finite(p.backOpacity, 1)));
|
|
46
|
+
const row = (from, to) => `${[.2126, .7152, .0722].map(c => n(c * (to - from))).join(' ')} 0 ${n(from)}`;
|
|
47
|
+
ctx.defs.push(`<filter id="${fid}" color-interpolation-filters="sRGB"><feColorMatrix type="matrix" values="${[...front.map((c, i) => row(c, back[i])), row(opacity, alpha)].join(' ')}"/></filter>`);
|
|
48
|
+
filter = ` filter="url(#${fid})"`;
|
|
49
|
+
}
|
|
50
|
+
ctx.defs.push(`<pattern id="${id}" patternUnits="${tiled ? 'userSpaceOnUse' : 'objectBoundingBox'}" width="${n(width)}" height="${n(height)}"${tiled ? '' : ` viewBox="0 0 1 1" preserveAspectRatio="none"`}><image xlink:href="${escapeXml(url)}" x="0" y="0" width="${n(width)}" height="${n(height)}" preserveAspectRatio="none"${filter}/></pattern>`);
|
|
51
|
+
return `fill="url(#${id})" fill-opacity="${n(p.imageStyle === 'pattern' ? 1 : opacity)}"`;
|
|
52
|
+
}
|
|
53
|
+
return 'fill="none"';
|
|
54
|
+
}
|
|
55
|
+
function marker(ctx, kind, stroke, start) {
|
|
56
|
+
if (kind === 'none' || kind === '0')
|
|
57
|
+
return '';
|
|
58
|
+
const id = nextId(ctx), c = sceneColor(stroke.color, stroke.colorIndex, ctx);
|
|
59
|
+
let geometry = '<path d="M0 0L10 5L0 10Z"/>';
|
|
60
|
+
if (kind === 'stealth' || kind === '2')
|
|
61
|
+
geometry = '<path d="M0 0L10 5L0 10L3 5Z"/>';
|
|
62
|
+
if (kind === 'diamond' || kind === '3')
|
|
63
|
+
geometry = '<path d="M0 5L5 0L10 5L5 10Z"/>';
|
|
64
|
+
if (kind === 'oval' || kind === '4')
|
|
65
|
+
geometry = '<ellipse cx="5" cy="5" rx="5" ry="5"/>';
|
|
66
|
+
if (kind === 'open' || kind === '5')
|
|
67
|
+
geometry = `<path d="M0 0L10 5L0 10" fill="none" stroke="${c}" stroke-width="1.5"/>`;
|
|
68
|
+
ctx.defs.push(`<marker id="${id}" markerWidth="5" markerHeight="5" refX="9" refY="5" viewBox="0 0 10 10" markerUnits="strokeWidth" orient="auto-start-reverse"><g fill="${c}">${geometry}</g></marker>`);
|
|
69
|
+
return ` marker-${start ? 'start' : 'end'}="url(#${id})"`;
|
|
70
|
+
}
|
|
71
|
+
function stroke(line, ctx) {
|
|
72
|
+
if (!line || line.width <= 0)
|
|
73
|
+
return 'stroke="none"';
|
|
74
|
+
let attrs = `stroke="${sceneColor(line.color, line.colorIndex, ctx)}" stroke-width="${n(Math.max(0, finite(line.width)))}" stroke-opacity="${n(Math.max(0, Math.min(1, finite(line.opacity, 1))))}" stroke-linejoin="round"`;
|
|
75
|
+
if (line.cap)
|
|
76
|
+
attrs += ` stroke-linecap="${line.cap}"`;
|
|
77
|
+
if (line.dash?.length)
|
|
78
|
+
attrs += ` stroke-dasharray="${line.dash.map(v => n(Math.max(0, v))).join(' ')}"`;
|
|
79
|
+
if (line.startArrow)
|
|
80
|
+
attrs += marker(ctx, line.startArrow, line, true);
|
|
81
|
+
if (line.endArrow)
|
|
82
|
+
attrs += marker(ctx, line.endArrow, line, false);
|
|
83
|
+
return attrs;
|
|
84
|
+
}
|
|
85
|
+
function renderGeometry(shape, ctx, w, h) {
|
|
86
|
+
const physical = { ...shape, bounds: { ...shape.bounds, width: w, height: h } }, fill = paint(shape.fill, ctx, physical), line = stroke(shape.line, ctx);
|
|
87
|
+
if (shape.path) {
|
|
88
|
+
const bounds = shape.pathBounds ?? { x: 0, y: 0, width: shape.bounds.width, height: shape.bounds.height };
|
|
89
|
+
return (shape.pathParts ?? [{ path: shape.path, noFill: false, noLine: false }]).map(part => `<path d="${escapeXml(mapPathToBounds(part.path, bounds, w, h))}" ${part.noFill ? 'fill="none"' : fill} ${part.noLine ? 'stroke="none"' : line}/>`).join('');
|
|
90
|
+
}
|
|
91
|
+
const geometry = presetGeometry(shape.preset, w, h, shape.source?.properties);
|
|
92
|
+
if (!geometry.supported)
|
|
93
|
+
ctx.warnings.push({ code: 'PRESET_APPROXIMATED', severity: 'warning', message: `Shape preset ${shape.preset} has no exact geometry and is shown with a bounding rectangle`, shapeId: shape.id, slideId: ctx.slide.id });
|
|
94
|
+
else if (shape.preset !== 2 && Object.keys(shape.source?.properties ?? {}).some(key => Number(key) >= 327 && Number(key) <= 336))
|
|
95
|
+
ctx.warnings.push({ code: 'PRESET_ADJUSTMENT_UNSUPPORTED', severity: 'warning', message: `Shape preset ${shape.preset} uses default geometry; source adjustment handles are retained but not evaluated`, shapeId: shape.id, slideId: ctx.slide.id });
|
|
96
|
+
return `<${geometry.tag} ${geometry.attrs} ${geometry.open ? 'fill="none"' : fill} ${line}/>`;
|
|
97
|
+
}
|
|
98
|
+
function shapeTransform(shape) { const b = shape.bounds, cx = b.width / 2, cy = b.height / 2; return `translate(${transformNumber(b.x)} ${transformNumber(b.y)})${shape.rotation ? ` rotate(${transformNumber(shape.rotation)} ${transformNumber(cx)} ${transformNumber(cy)})` : ''}${shape.flipH || shape.flipV ? ` translate(${transformNumber(cx)} ${transformNumber(cy)}) scale(${shape.flipH ? -1 : 1} ${shape.flipV ? -1 : 1}) translate(${transformNumber(-cx)} ${transformNumber(-cy)})` : ''}`; }
|
|
99
|
+
function renderImage(shape, ctx) {
|
|
100
|
+
const b = shape.bounds, image = shape.image, url = image ? assetUrl(ctx, image.assetId) : undefined;
|
|
101
|
+
if (!image || !url) {
|
|
102
|
+
ctx.warnings.push({ code: 'IMAGE_NOT_RENDERABLE', severity: 'warning', message: 'Image resource is unavailable or not supported by this renderer', shapeId: shape.id, slideId: ctx.slide.id });
|
|
103
|
+
return ctx.options.showUnsupported === false ? '' : `<rect width="${n(b.width)}" height="${n(b.height)}" fill="#f1f5f9" stroke="#94a3b8" stroke-dasharray="4 3"/><text x="6" y="18" font-family="sans-serif" font-size="12" fill="#64748b">Image unavailable</text>`;
|
|
104
|
+
}
|
|
105
|
+
const crop = image.crop ?? { left: 0, top: 0, right: 0, bottom: 0 }, vw = 1 - crop.left - crop.right, vh = 1 - crop.top - crop.bottom;
|
|
106
|
+
if (vw <= 0 || vh <= 0)
|
|
107
|
+
return '';
|
|
108
|
+
const id = nextId(ctx);
|
|
109
|
+
ctx.defs.push(`<clipPath id="${id}"><rect width="${n(b.width)}" height="${n(b.height)}"/></clipPath>`);
|
|
110
|
+
let filter = '';
|
|
111
|
+
if (image.brightness != null || image.contrast != null) {
|
|
112
|
+
const fid = nextId(ctx), slope = finite(image.contrast, 1), intercept = finite(image.brightness) + (1 - slope) / 2;
|
|
113
|
+
ctx.defs.push(`<filter id="${fid}" color-interpolation-filters="sRGB"><feComponentTransfer>${['R', 'G', 'B'].map(c => `<feFunc${c} type="linear" slope="${n(slope)}" intercept="${n(intercept)}"/>`).join('')}</feComponentTransfer></filter>`);
|
|
114
|
+
filter = ` filter="url(#${fid})"`;
|
|
115
|
+
}
|
|
116
|
+
return `<g clip-path="url(#${id})"><image xlink:href="${escapeXml(url)}" x="${n(-crop.left * b.width / vw)}" y="${n(-crop.top * b.height / vh)}" width="${n(b.width / vw)}" height="${n(b.height / vh)}" preserveAspectRatio="none"${filter}/></g>`;
|
|
117
|
+
}
|
|
118
|
+
function renderMedia(shape, ctx) {
|
|
119
|
+
const media = shape.media;
|
|
120
|
+
if (!media)
|
|
121
|
+
return '';
|
|
122
|
+
const asset = media.assetId ? ctx.assets.get(media.assetId) : undefined;
|
|
123
|
+
let url;
|
|
124
|
+
if (ctx.options.mediaControls === false)
|
|
125
|
+
return media.posterAssetId ? renderImage({ ...shape, image: { assetId: media.posterAssetId } }, ctx) : '';
|
|
126
|
+
if (asset && !asset.unsupported && /^(?:audio|video)\/[a-z0-9.+-]+$/i.test(asset.mimeType)) {
|
|
127
|
+
const supplied = ctx.options.assetUrl?.(asset);
|
|
128
|
+
if (ctx.options.assetUrl) {
|
|
129
|
+
if (supplied && /^(?:blob:|data:(?:audio|video)\/[a-z0-9.+-]+;base64,)/i.test(supplied))
|
|
130
|
+
url = supplied;
|
|
131
|
+
}
|
|
132
|
+
else
|
|
133
|
+
url = `data:${asset.mimeType};base64,${base64(asset.bytes)}`;
|
|
134
|
+
}
|
|
135
|
+
if (!url) {
|
|
136
|
+
ctx.warnings.push({ code: 'MEDIA_SOURCE_UNAVAILABLE', severity: 'warning', message: 'Media has no supported embedded asset; external paths are not fetched', shapeId: shape.id, slideId: ctx.slide.id });
|
|
137
|
+
return '';
|
|
138
|
+
}
|
|
139
|
+
const tag = media.kind === 'video' ? 'video' : 'audio', poster = media.posterAssetId ? assetUrl(ctx, media.posterAssetId) : undefined, b = shape.bounds;
|
|
140
|
+
return `<foreignObject x="0" y="0" width="${n(b.width)}" height="${n(b.height)}"><div xmlns="http://www.w3.org/1999/xhtml" style="width:100%;height:100%;display:flex;align-items:center;justify-content:center"><${tag} data-ppt-media="${n(media.mediaId)}" controls="controls" preload="none" playsinline="playsinline"${media.loop ? ' loop="loop"' : ''}${poster && tag === 'video' ? ` poster="${escapeXml(poster)}"` : ''} src="${escapeXml(url)}" style="width:100%;max-height:100%;object-fit:contain">Embedded media</${tag}></div></foreignObject>`;
|
|
141
|
+
}
|
|
142
|
+
function multiply(a, b) { return [a[0] * b[0] + a[2] * b[1], a[1] * b[0] + a[3] * b[1], a[0] * b[2] + a[2] * b[3], a[1] * b[2] + a[3] * b[3]]; }
|
|
143
|
+
function shapeLinear(shape) {
|
|
144
|
+
const angle = shape.rotation * Math.PI / 180, cos = Math.cos(angle), sin = Math.sin(angle), hx = shape.flipH ? -1 : 1, vy = shape.flipV ? -1 : 1;
|
|
145
|
+
return [cos * hx, sin * hx, -sin * vy, cos * vy];
|
|
146
|
+
}
|
|
147
|
+
/** Child anchors use FSPGR coordinates; TextCFException font sizes remain points.
|
|
148
|
+
* Lay out all absolute text lengths in the mapped physical frame, then undo only
|
|
149
|
+
* its axis scales. Rotation, reflection and any inherited shear remain in the
|
|
150
|
+
* original shape transform. No text style or source geometry is rewritten. */
|
|
151
|
+
function textAxisScale(shape, parent) {
|
|
152
|
+
const axes = multiply(parent, shapeLinear(shape)), x = Math.hypot(axes[0], axes[1]), y = Math.hypot(axes[2], axes[3]);
|
|
153
|
+
if (!Number.isFinite(x) || x <= 0 || !Number.isFinite(y) || y <= 0 || !Number.isFinite(1 / x) || !Number.isFinite(1 / y))
|
|
154
|
+
return undefined;
|
|
155
|
+
// Test normalized directions, not the tiny unscaled determinant: valid small
|
|
156
|
+
// group coordinates must not underflow, and zero-axis ancestors may produce
|
|
157
|
+
// two nonzero but collinear axes after a child rotation.
|
|
158
|
+
const cross = (axes[0] / x) * (axes[3] / y) - (axes[1] / x) * (axes[2] / y);
|
|
159
|
+
return Number.isFinite(cross) && cross !== 0 ? { x, y } : undefined;
|
|
160
|
+
}
|
|
161
|
+
function shadowWrap(shape, html, ctx, parent) { return shape.shadow ? renderShadow(shape, html, effectContext(ctx), parent) : html; }
|
|
162
|
+
function renderShape(shape, ctx, depth = 0, parent = [1, 0, 0, 1]) {
|
|
163
|
+
if (shape.hidden || depth > 64 || !headerFooterVisible(shape.placeholder?.type, ctx.slide))
|
|
164
|
+
return '';
|
|
165
|
+
const b = shape.bounds, w = Math.max(0, finite(b.width));
|
|
166
|
+
let h = Math.max(0, finite(b.height));
|
|
167
|
+
const textContent = (resizeShape = true) => {
|
|
168
|
+
if (!shape.text)
|
|
169
|
+
return '';
|
|
170
|
+
const scale = textAxisScale(shape, parent);
|
|
171
|
+
if (!scale || !Number.isFinite(w * scale.x) || !Number.isFinite(h * scale.y)) {
|
|
172
|
+
ctx.warnings.push({ code: 'TEXT_TRANSFORM_DEGENERATE', severity: 'warning', message: 'Text has a singular or non-finite ancestor transform and cannot be laid out in a physical frame', slideId: ctx.slide.id, shapeId: shape.id });
|
|
173
|
+
return '';
|
|
174
|
+
}
|
|
175
|
+
const frame = resolveTextFields(shape.text, { slide: ctx.slide, options: ctx.options, date: ctx.date, warnings: ctx.warnings, shapeId: shape.id }, shape.placeholder?.type);
|
|
176
|
+
const html = renderText(frame, w * scale.x, h * scale.y, ctx.slide.colorScheme, { ...ctx.options, warnings: ctx.warnings, slideId: ctx.slide.id, shapeId: shape.id, onLayout: layout => { if (resizeShape && frame.autofit === 'resize')
|
|
177
|
+
h = layout.height / scale.y; } });
|
|
178
|
+
if (resizeShape && h !== shape.bounds.height)
|
|
179
|
+
shape = { ...shape, bounds: { ...shape.bounds, height: h } };
|
|
180
|
+
return scale.x === 1 && scale.y === 1 ? html : `<g transform="scale(${transformNumber(1 / scale.x)} ${transformNumber(1 / scale.y)})">${html}</g>`;
|
|
181
|
+
};
|
|
182
|
+
let text = shape.text && !shape.wordArt ? textContent() : '';
|
|
183
|
+
const animate = (html) => { const href = linkUrl(shape.hyperlink); if (href)
|
|
184
|
+
html = `<a href="${escapeXml(href)}"${href.startsWith('#') ? '' : ' target="_blank" rel="noopener noreferrer"'}>${html}</a>`; return ctx.animationTargets.has(shape.id) ? `<g data-ppt-animation="${n(shape.id)}" data-ppt-parent="${parent.map(transformNumber).join(' ')}">${html}</g>` : html; };
|
|
185
|
+
const label = shape.name ? ` aria-label="${escapeXml(shape.name)}"` : '';
|
|
186
|
+
if (shape.kind === 'group') {
|
|
187
|
+
const gb = shape.groupBounds ?? { x: 0, y: 0, width: w, height: h }, sx = gb.width ? w / gb.width : 1, sy = gb.height ? h / gb.height : 1, linear = multiply(multiply(parent, shapeLinear(shape)), [sx, 0, 0, sy]);
|
|
188
|
+
return animate(shadowWrap(shape, `<g data-ppt-shape="${n(shape.id)}" transform="${shapeTransform(shape)}"${label}><g transform="scale(${transformNumber(sx)} ${transformNumber(sy)}) translate(${transformNumber(-gb.x)} ${transformNumber(-gb.y)})">${(shape.children ?? []).map(s => renderShape(s, ctx, depth + 1, linear)).join('')}</g></g>`, ctx, parent));
|
|
189
|
+
}
|
|
190
|
+
let content = '', wordArtRendered = false;
|
|
191
|
+
if (shape.kind === 'unsupported') {
|
|
192
|
+
if (ctx.options.showUnsupported !== false)
|
|
193
|
+
content = `<rect width="${n(w)}" height="${n(h)}" fill="#f8fafc" stroke="#94a3b8" stroke-dasharray="4 3"/><text x="6" y="18" font-family="sans-serif" font-size="12" fill="#64748b">Unsupported element</text>`;
|
|
194
|
+
}
|
|
195
|
+
else if (shape.kind === 'image' && shape.preset === 75) {
|
|
196
|
+
// A picture frame displays its BLIP, including alpha. Drawing defaults such
|
|
197
|
+
// as fillColor/fFilled describe ordinary geometry, not a second rectangle
|
|
198
|
+
// behind the picture. Painting that rectangle hides earlier scene objects
|
|
199
|
+
// through transparent pixels (including pre-rendered circular crops).
|
|
200
|
+
content = renderImage(shape, ctx);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
const wordArt = shape.wordArt ? renderWordArt(shape, paint(shape.fill, ctx, shape), stroke(shape.line, ctx), effectContext(ctx)) : undefined;
|
|
204
|
+
if (wordArt != null) {
|
|
205
|
+
content = wordArt;
|
|
206
|
+
wordArtRendered = true;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
// Child coordinates can be millions of local units while lineWidth remains
|
|
210
|
+
// an absolute physical length. Map geometry into physical axis lengths and
|
|
211
|
+
// undo those axis scales, as for text. This preserves source paint order,
|
|
212
|
+
// rotations/reflections and SVG viewport zoom without shrinking strokes.
|
|
213
|
+
const scale = (shape.line || shape.fill.imageStyle === 'pattern' || shape.fill.imageStyle === 'texture') && !shape.extrusion ? textAxisScale(shape, parent) : undefined;
|
|
214
|
+
if (scale && (scale.x !== 1 || scale.y !== 1) && Number.isFinite(w * scale.x) && Number.isFinite(h * scale.y))
|
|
215
|
+
content = `<g transform="scale(${transformNumber(1 / scale.x)} ${transformNumber(1 / scale.y)})">${renderGeometry(shape, ctx, w * scale.x, h * scale.y)}</g>`;
|
|
216
|
+
else
|
|
217
|
+
content = renderGeometry(shape, ctx, w, h);
|
|
218
|
+
}
|
|
219
|
+
if (shape.image)
|
|
220
|
+
content += renderImage(shape, ctx);
|
|
221
|
+
}
|
|
222
|
+
// The fallback has historically retained the WordArt geometry bounds. It is
|
|
223
|
+
// reached after geometry creation, so its text fit must not move that frame.
|
|
224
|
+
if (shape.wordArt && !wordArtRendered && shape.text)
|
|
225
|
+
text = textContent(false);
|
|
226
|
+
content += text + renderMedia(shape, ctx);
|
|
227
|
+
if (shape.extrusion)
|
|
228
|
+
content = renderExtrusion(shape, content, effectContext(ctx));
|
|
229
|
+
return animate(shadowWrap(shape, `<g data-ppt-shape="${n(shape.id)}" transform="${shapeTransform(shape)}"${label}>${content}</g>`, ctx, parent));
|
|
230
|
+
}
|
|
231
|
+
function composedShapes(doc, slide, options) {
|
|
232
|
+
if (options.includeMaster === false || slide.showMasterShapes === false)
|
|
233
|
+
return slide.shapes;
|
|
234
|
+
const byId = new Map(doc.masters.map(s => [s.id, s]));
|
|
235
|
+
const covered = new Set();
|
|
236
|
+
const visit = (shapes) => { for (const s of shapes) {
|
|
237
|
+
if (s.placeholder)
|
|
238
|
+
covered.add(s.placeholder.type);
|
|
239
|
+
if (s.children)
|
|
240
|
+
visit(s.children);
|
|
241
|
+
} };
|
|
242
|
+
visit(slide.shapes);
|
|
243
|
+
// Master title/body placeholders supply style/position. Their sample text is never slide content.
|
|
244
|
+
const filterMaster = (shapes) => shapes.flatMap(s => s.placeholder && (covered.has(s.placeholder.type) || ![7, 8, 9, 10].includes(s.placeholder.type)) ? [] : [s.children ? { ...s, children: filterMaster(s.children) } : s]);
|
|
245
|
+
const seen = new Set();
|
|
246
|
+
const collect = (id) => { if (id == null || seen.has(id))
|
|
247
|
+
return []; seen.add(id); const master = byId.get(id); if (!master)
|
|
248
|
+
return []; const own = filterMaster(master.shapes); visit(master.shapes); return [...(master.showMasterShapes === false ? [] : collect(master.masterId)), ...own]; };
|
|
249
|
+
const masterShapes = collect(slide.masterId);
|
|
250
|
+
return [...masterShapes, ...slide.shapes];
|
|
251
|
+
}
|
|
252
|
+
export const defaultPptCss = '.pptjs-slide{display:block;max-width:100%;max-height:100%;user-select:text}.pptjs-slide *{box-sizing:border-box}.pptjs-slide a:focus{outline:2px solid #2563eb;outline-offset:2px}';
|
|
253
|
+
export function renderSlide(doc, index, options = {}) {
|
|
254
|
+
if (!Number.isInteger(index) || index < 0 || index >= doc.slides.length)
|
|
255
|
+
throw new PptError('SLIDE_INDEX', `Slide index ${index} is outside the presentation`);
|
|
256
|
+
const slide = doc.slides[index], w = slide.size.width, h = slide.size.height;
|
|
257
|
+
if (!Number.isFinite(w) || !Number.isFinite(h) || w <= 0 || h <= 0)
|
|
258
|
+
throw new PptError('SLIDE_SIZE', 'Slide size must be finite and positive');
|
|
259
|
+
const ctx = { document: doc, slide, options, date: options.fieldDate ?? new Date(), defs: [], prefix: `ppt-${++renderSequence}-${index}`, seq: 0, assets: new Map(doc.assets.map(a => [a.id, a])), urls: new Map(), warnings: [], animationTargets: new Set(slide.timeline?.effects.map(e => e.targetShapeId) ?? []) };
|
|
260
|
+
const transition = slide.transition;
|
|
261
|
+
if (transition) {
|
|
262
|
+
const direction = transition.direction ?? 0, supported = transition.type === 255 || [0, 6, 23].includes(transition.type) && direction === 0 || [4, 7, 10, 20].includes(transition.type) && Number.isInteger(direction) && direction >= 0 && direction <= 3;
|
|
263
|
+
if (!supported)
|
|
264
|
+
ctx.warnings.push({ code: 'TRANSITION_EFFECT_UNSUPPORTED', severity: 'warning', message: 'This transition variant has no playback implementation and changes slides without the source effect', slideId: slide.id, details: { type: transition.type, direction } });
|
|
265
|
+
if (transition.soundEnabled)
|
|
266
|
+
ctx.warnings.push({ code: 'TRANSITION_SOUND_MANUAL', severity: 'warning', message: 'Transition sound commands are retained but are not played automatically', slideId: slide.id });
|
|
267
|
+
}
|
|
268
|
+
const bg = `<rect width="${n(w)}" height="${n(h)}" ${paint(slide.background, ctx)}/>`;
|
|
269
|
+
const shapes = composedShapes(doc, slide, options).map(s => renderShape(s, ctx)).join('');
|
|
270
|
+
const html = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="pptjs-slide${options.className ? ' ' + escapeXml(options.className) : ''}" role="img" aria-label="${escapeXml(slide.name ?? `Slide ${index + 1}`)}" width="${n(w)}" height="${n(h)}" viewBox="0 0 ${n(w)} ${n(h)}" preserveAspectRatio="xMidYMid meet"><defs>${ctx.defs.join('')}</defs>${bg}${shapes}</svg>`;
|
|
271
|
+
const relatedIds = new Set([slide.id]);
|
|
272
|
+
let id = slide.masterId;
|
|
273
|
+
while (id != null && !relatedIds.has(id)) {
|
|
274
|
+
relatedIds.add(id);
|
|
275
|
+
id = doc.masters.find(m => m.id === id)?.masterId;
|
|
276
|
+
}
|
|
277
|
+
return { html, css: defaultPptCss, warnings: [...doc.diagnostics.filter(d => d.slideId == null || relatedIds.has(d.slideId)), ...ctx.warnings] };
|
|
278
|
+
}
|
|
279
|
+
export function renderSlideSvg(doc, index, options = {}) { return renderSlide(doc, index, options).html; }
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Diagnostic, RenderOptions, TextFrame } from '../types.js';
|
|
2
|
+
import { type TextLayout } from '../layout/text-layout.js';
|
|
3
|
+
export interface TextRenderOptions extends RenderOptions {
|
|
4
|
+
onLayout?: (layout: TextLayout) => void;
|
|
5
|
+
warnings?: Diagnostic[];
|
|
6
|
+
slideId?: number;
|
|
7
|
+
shapeId?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function renderText(frame: TextFrame, width: number, height: number, colors?: number[], options?: TextRenderOptions): string;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { color, cssFont, escapeXml, finite, linkUrl, n } from './safe.js';
|
|
2
|
+
import { createCanvasTextMeasurer } from '../layout/measure.js';
|
|
3
|
+
import { layoutText, paragraphDefaultTabSize, paragraphWrapFlags } from '../layout/text-layout.js';
|
|
4
|
+
function schemeColor(value, index, colors) { const rgb = index == null ? undefined : colors[index]; return rgb == null ? value : `#${[rgb & 255, (rgb >>> 8) & 255, (rgb >>> 16) & 255].map(v => v.toString(16).padStart(2, '0')).join('')}`; }
|
|
5
|
+
/** CFStyle stores effect presence, not a complete blur/light geometry. */
|
|
6
|
+
function textEffects(style) {
|
|
7
|
+
const size = Math.max(.1, finite(style.fontSize, 24)), effects = [];
|
|
8
|
+
if (style.shadow) {
|
|
9
|
+
const offset = Math.max(.5, size / 16);
|
|
10
|
+
effects.push({ dx: offset, dy: offset, color: '#000000', opacity: .45 });
|
|
11
|
+
}
|
|
12
|
+
if (style.emboss) {
|
|
13
|
+
const offset = Math.max(.5, size / 32);
|
|
14
|
+
effects.push({ dx: -offset, dy: -offset, color: '#ffffff', opacity: .8 }, { dx: offset, dy: offset, color: '#000000', opacity: .5 });
|
|
15
|
+
}
|
|
16
|
+
return effects;
|
|
17
|
+
}
|
|
18
|
+
function runHtml(run, colors, fontAlignment) {
|
|
19
|
+
const s = run.style, styles = [];
|
|
20
|
+
if (s.fontFamily)
|
|
21
|
+
styles.push(`font-family:${cssFont(s.fontFamily)}`);
|
|
22
|
+
if (s.fontSize != null)
|
|
23
|
+
styles.push(`font-size:${n(Math.max(.1, finite(s.fontSize, 24)))}px`);
|
|
24
|
+
const resolvedColor = schemeColor(s.color, s.colorIndex, colors);
|
|
25
|
+
if (resolvedColor)
|
|
26
|
+
styles.push(`color:${color(resolvedColor)}`);
|
|
27
|
+
if (s.bold != null)
|
|
28
|
+
styles.push(`font-weight:${s.bold ? '700' : '400'}`);
|
|
29
|
+
if (s.italic != null)
|
|
30
|
+
styles.push(`font-style:${s.italic ? 'italic' : 'normal'}`);
|
|
31
|
+
const decoration = [s.underline ? 'underline' : '', s.strike ? 'line-through' : ''].filter(Boolean).join(' ');
|
|
32
|
+
if (decoration)
|
|
33
|
+
styles.push(`text-decoration:${decoration}`);
|
|
34
|
+
if (s.baseline)
|
|
35
|
+
styles.push(`vertical-align:${n(s.baseline)}%`);
|
|
36
|
+
else if (fontAlignment != null && fontAlignment >= 1 && fontAlignment <= 3)
|
|
37
|
+
styles.push(`vertical-align:${['baseline', 'top', 'middle', 'bottom'][fontAlignment]}`);
|
|
38
|
+
if (s.letterSpacing != null)
|
|
39
|
+
styles.push(`letter-spacing:${n(s.letterSpacing)}px`);
|
|
40
|
+
const effects = textEffects(s);
|
|
41
|
+
if (effects.length)
|
|
42
|
+
styles.push(`text-shadow:${effects.map(effect => `${n(effect.dx)}px ${n(effect.dy)}px 0 ${effect.color}${Math.round(effect.opacity * 255).toString(16).padStart(2, '0')}`).join(',')}`);
|
|
43
|
+
const escaped = (text) => escapeXml(text).replace(/\r\n|\r|\n|\u000b/g, '<br/>');
|
|
44
|
+
const content = s.eastAsianFontFamily ? run.text.split(/([\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef\u{20000}-\u{3134f}]+)/u).map((part, i) => i % 2 ? `<span style="${escapeXml(`font-family:${cssFont(s.eastAsianFontFamily)}`)}">${escaped(part)}</span>` : escaped(part)).join('') : escaped(run.text);
|
|
45
|
+
const html = `<span style="${escapeXml(styles.join(';'))}">${content}</span>`;
|
|
46
|
+
const url = linkUrl(s.hyperlink);
|
|
47
|
+
return url ? `<a href="${escapeXml(url)}"${url.startsWith('#') ? '' : ' target="_blank" rel="noopener noreferrer"'} style="color:inherit;text-decoration:inherit">${html}</a>` : html;
|
|
48
|
+
}
|
|
49
|
+
function paragraphHtml(paragraph, colors) {
|
|
50
|
+
const s = paragraph.style;
|
|
51
|
+
const styles = ['margin:0', 'padding:0', 'min-height:1em', 'position:relative', 'box-sizing:border-box'];
|
|
52
|
+
if (s.alignment === 'distributed')
|
|
53
|
+
styles.push('text-align:justify', 'text-align-last:justify', 'text-justify:inter-character');
|
|
54
|
+
else if (s.alignment)
|
|
55
|
+
styles.push(`text-align:${['left', 'right', 'center', 'justify'].includes(s.alignment) ? s.alignment : 'left'}`);
|
|
56
|
+
const wrapFlags = paragraphWrapFlags(s);
|
|
57
|
+
if (!(wrapFlags & 2))
|
|
58
|
+
styles.push('word-break:break-all');
|
|
59
|
+
if (!(wrapFlags & 1))
|
|
60
|
+
styles.push('line-break:loose');
|
|
61
|
+
if (wrapFlags & 4)
|
|
62
|
+
styles.push('hanging-punctuation:allow-end');
|
|
63
|
+
if (s.direction)
|
|
64
|
+
styles.push(`direction:${s.direction === 'rtl' ? 'rtl' : 'ltr'}`);
|
|
65
|
+
if (s.marginLeft != null)
|
|
66
|
+
styles.push(`margin-left:${n(s.marginLeft)}px`);
|
|
67
|
+
if (s.indent != null)
|
|
68
|
+
styles.push(`text-indent:${n(s.indent)}px`);
|
|
69
|
+
const firstSize = paragraph.runs.find(r => r.style.fontSize)?.style.fontSize ?? 24;
|
|
70
|
+
styles.push(`font-size:${n(Math.max(.1, firstSize))}px`);
|
|
71
|
+
const firstFont = paragraph.runs.find(r => r.style.fontFamily)?.style.fontFamily;
|
|
72
|
+
if (firstFont)
|
|
73
|
+
styles.push(`font-family:${cssFont(firstFont)}`);
|
|
74
|
+
const lineHeight = s.lineHeightPoints ?? firstSize * (s.lineHeight ?? 1.15);
|
|
75
|
+
if (s.spaceBefore != null || s.spaceBeforePercent != null)
|
|
76
|
+
styles.push(`padding-top:${n(Math.max(0, s.spaceBefore ?? finite(s.spaceBeforePercent) * lineHeight))}px`);
|
|
77
|
+
if (s.spaceAfter != null || s.spaceAfterPercent != null)
|
|
78
|
+
styles.push(`padding-bottom:${n(Math.max(0, s.spaceAfter ?? finite(s.spaceAfterPercent) * lineHeight))}px`);
|
|
79
|
+
if (s.defaultTabSize != null)
|
|
80
|
+
styles.push(`tab-size:${n(paragraphDefaultTabSize(s))}px`);
|
|
81
|
+
if (s.lineHeightPoints != null)
|
|
82
|
+
styles.push(`line-height:${n(Math.max(.1, s.lineHeightPoints))}px`);
|
|
83
|
+
else if (s.lineHeight != null)
|
|
84
|
+
styles.push(`line-height:${n(Math.max(.1, s.lineHeight))}`);
|
|
85
|
+
let bullet = '';
|
|
86
|
+
if (s.bullet && s.bulletEnabled !== false) {
|
|
87
|
+
const bs = [`display:inline-block`, `text-indent:0`, `min-width:${n(Math.max(firstSize * .5, -finite(s.indent)))}px`];
|
|
88
|
+
if (s.bullet.fontFamily)
|
|
89
|
+
bs.push(`font-family:${cssFont(s.bullet.fontFamily)}`);
|
|
90
|
+
const firstStyle = paragraph.runs[0]?.style;
|
|
91
|
+
const bc = schemeColor(s.bullet.color, s.bullet.colorIndex, colors) ?? schemeColor(firstStyle?.color, firstStyle?.colorIndex, colors);
|
|
92
|
+
if (bc)
|
|
93
|
+
bs.push(`color:${color(bc)}`);
|
|
94
|
+
if (s.bullet.size != null || s.bullet.sizePercent != null)
|
|
95
|
+
bs.push(`font-size:${n(s.bullet.size ?? finite(s.bullet.sizePercent, 1) * firstSize)}px`);
|
|
96
|
+
bullet = `<span aria-hidden="true" style="${escapeXml(bs.join(';'))}">${escapeXml(s.bullet.text)}</span>`;
|
|
97
|
+
}
|
|
98
|
+
const runs = paragraph.runs.map(r => runHtml(r, colors, s.fontAlignment)).join('');
|
|
99
|
+
return `<div style="${escapeXml(styles.join(';'))}">${bullet}${runs || '<br/>'}</div>`;
|
|
100
|
+
}
|
|
101
|
+
let textClipSequence = 0;
|
|
102
|
+
function textViewportWidth(layout) {
|
|
103
|
+
let width = layout.width;
|
|
104
|
+
// A closing punctuation mark may occupy the right margin or extend beyond it.
|
|
105
|
+
// Extend only the horizontal viewport; overflow:visible would also expose
|
|
106
|
+
// unrelated text below the frame and break vertical clipping.
|
|
107
|
+
for (const line of layout.lines)
|
|
108
|
+
for (const fragment of line.fragments)
|
|
109
|
+
if (fragment.hanging)
|
|
110
|
+
width = Math.max(width, fragment.x + fragment.width);
|
|
111
|
+
return width;
|
|
112
|
+
}
|
|
113
|
+
function svgText(layout, colors, frame) {
|
|
114
|
+
const vertical = frame?.direction === 'vertical' || frame?.direction === 'vertical270';
|
|
115
|
+
const viewportWidth = vertical ? layout.height : textViewportWidth(layout), viewportHeight = vertical ? layout.width : layout.height, clipId = `ppt-text-clip-${++textClipSequence}`;
|
|
116
|
+
const lines = layout.lines.map(line => `<g data-ppt-line="${line.paragraphIndex}">${line.fragments.map(fragment => {
|
|
117
|
+
const s = fragment.style, baseline = (fragment.baseline ?? line.baseline) - (s.fontSize ?? 24) * finite(s.baseline) / 100;
|
|
118
|
+
const decoration = [s.underline ? 'underline' : '', s.strike ? 'line-through' : ''].filter(Boolean).join(' ');
|
|
119
|
+
const paragraph = frame?.paragraphs[line.paragraphIndex], rtl = paragraph?.style.direction === 'rtl' || paragraph?.style.direction == null && /^[\s\d\p{P}]*[\p{Script=Arabic}\p{Script=Hebrew}]/u.test(paragraph?.runs.map(r => r.text).join('') ?? '');
|
|
120
|
+
const x = vertical ? layout.height - baseline : rtl ? layout.width - fragment.x : fragment.x, y = vertical ? fragment.x : baseline;
|
|
121
|
+
const attrs = `font-family="${escapeXml(cssFont(s.fontFamily ?? 'Arial'))}" font-size="${n(Math.max(.1, finite(s.fontSize, 24)))}" font-weight="${s.bold ? '700' : '400'}" font-style="${s.italic ? 'italic' : 'normal'}"${vertical ? ' writing-mode="vertical-rl" text-orientation="mixed"' : ''}${rtl ? ' direction="rtl" unicode-bidi="plaintext" text-anchor="start"' : ''}${decoration ? ` text-decoration="${decoration}"` : ''}${s.letterSpacing != null ? ` letter-spacing="${n(s.letterSpacing)}"` : ''} xml:space="preserve"`;
|
|
122
|
+
const label = escapeXml(fragment.text.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/g, '\ufffd').replace(/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g, '\ufffd'));
|
|
123
|
+
const effects = textEffects(s).map(effect => `<text x="${n(x + effect.dx)}" y="${n(y + effect.dy)}" fill="${effect.color}" fill-opacity="${n(effect.opacity)}" ${attrs} aria-hidden="true">${label}</text>`).join('');
|
|
124
|
+
const glyphs = `${effects}<text x="${n(x)}" y="${n(y)}" fill="${color(schemeColor(s.color, s.colorIndex, colors))}" ${attrs}${fragment.bullet ? ' aria-hidden="true"' : ''}>${label}</text>`;
|
|
125
|
+
const url = linkUrl(s.hyperlink);
|
|
126
|
+
return url ? `<a href="${escapeXml(url)}" xlink:href="${escapeXml(url)}"${url.startsWith('#') ? '' : ' target="_blank" rel="noopener noreferrer"'}>${glyphs}</a>` : glyphs;
|
|
127
|
+
}).join('')}</g>`).join('');
|
|
128
|
+
// SVG 1.1 makes viewport overflow:hidden equivalent to this rectangle.
|
|
129
|
+
// Emit the rectangle explicitly as well: some SVG exporters ignore nested
|
|
130
|
+
// viewport clipping. The horizontal allowance for punctuation stays bounded.
|
|
131
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" width="${n(viewportWidth)}" height="${n(viewportHeight)}" overflow="hidden" data-ppt-layout="measured" data-ppt-text-output="svg" data-ppt-font-scale="${n(layout.scale)}"><defs><clipPath id="${clipId}" clipPathUnits="userSpaceOnUse"><rect x="0" y="0" width="${n(viewportWidth)}" height="${n(viewportHeight)}"/></clipPath></defs><g clip-path="url(#${clipId})"${frame?.direction === 'vertical270' ? ` transform="translate(${n(viewportWidth)} ${n(viewportHeight)}) rotate(180)"` : ''}>${lines}</g></svg>`;
|
|
132
|
+
}
|
|
133
|
+
export function renderText(frame, width, height, colors = [], options = {}) {
|
|
134
|
+
const warn = (code, message) => options.warnings?.push({ code, severity: 'warning', message, slideId: options.slideId, shapeId: options.shapeId });
|
|
135
|
+
if (frame.paragraphs.some(p => p.runs.some(r => r.style.shadow)))
|
|
136
|
+
warn('TEXT_SHADOW_APPROXIMATED', 'Character shadow uses a proportional offset and fixed opacity; exact Office shadow geometry is not reproduced');
|
|
137
|
+
if (frame.paragraphs.some(p => p.runs.some(r => r.style.emboss)))
|
|
138
|
+
warn('TEXT_EMBOSS_APPROXIMATED', 'Character emboss uses opposing highlight/shadow offsets; exact Office lighting is not reproduced');
|
|
139
|
+
if (frame.paragraphs.some(p => p.style.defaultTabSize != null && !(p.style.defaultTabSize > 0) && p.runs.some(r => r.text.includes('\t'))))
|
|
140
|
+
warn('TEXT_TAB_DEFAULT_APPROXIMATED', 'A nonpositive saved default tab step uses a 48px compatibility grid; PowerPoint tab behavior for this value is not fully specified');
|
|
141
|
+
if (options.textLayout === 'measured' || options.textOutput === 'svg') {
|
|
142
|
+
const supported = (!frame.direction || frame.direction === 'horizontal') && !frame.paragraphs.some(p => p.style.direction === 'rtl' || p.runs.some(r => /[\p{Script=Arabic}\p{Script=Hebrew}\u202a-\u202e\u2066-\u2069]/u.test(r.text)));
|
|
143
|
+
const measurer = options.textMeasurer ?? createCanvasTextMeasurer();
|
|
144
|
+
if ((supported || options.textOutput === 'svg') && measurer) {
|
|
145
|
+
const vertical = frame.direction === 'vertical' || frame.direction === 'vertical270';
|
|
146
|
+
const layoutFrame = vertical ? { ...frame, margins: { left: frame.margins.top, right: frame.margins.bottom, top: frame.margins.right, bottom: frame.margins.left } } : frame;
|
|
147
|
+
const layout = layoutText(layoutFrame, { width: vertical ? height : width, height: vertical ? width : height, measurer, scale: frame.fontScale, lineSpacingScale: frame.lineSpacingScale, autofit: vertical ? 'none' : frame.autofit });
|
|
148
|
+
if (!vertical)
|
|
149
|
+
options.onLayout?.(layout);
|
|
150
|
+
if (!supported)
|
|
151
|
+
warn(vertical ? 'TEXT_VERTICAL_SVG_APPROXIMATED' : 'TEXT_BIDI_SVG_APPROXIMATED', vertical ? 'Native SVG vertical glyphs use measured column advances; Office-specific vertical fitting remains approximate' : 'Native SVG direction and shaping retain bidirectional text; mixed-script line layout remains approximate');
|
|
152
|
+
if (layout.lineMetricsApproximate)
|
|
153
|
+
warn('TEXT_LINE_METRICS_APPROXIMATED', 'Normal baseline advance is unavailable from the text measurer; relative line spacing uses nominal font size rather than font bounding-box height');
|
|
154
|
+
if (layout.overflowX || layout.overflowY)
|
|
155
|
+
warn('TEXT_LAYOUT_OVERFLOW', 'Measured text exceeds its frame after the requested fit policy');
|
|
156
|
+
if (options.textOutput === 'svg')
|
|
157
|
+
return svgText(layout, colors, frame);
|
|
158
|
+
const lines = layout.lines.map(line => `<div data-ppt-line="${line.paragraphIndex}" style="position:absolute;inset:0;pointer-events:none">${line.fragments.map(fragment => { const baseline = (fragment.baseline ?? line.baseline) - (fragment.style.fontSize ?? 24) * finite(fragment.style.baseline) / 100; const css = `position:absolute;display:block;left:${n(fragment.x)}px;top:${n(baseline - fragment.ascent)}px;font-size:${n(fragment.style.fontSize ?? 24)}px;font-family:${cssFont(fragment.style.fontFamily ?? 'Arial')};line-height:${n(fragment.ascent + fragment.descent)}px;height:${n(fragment.ascent + fragment.descent)}px;white-space:pre;pointer-events:auto`; return `<span${fragment.bullet ? ' aria-hidden="true"' : ''} style="${escapeXml(css)}">${runHtml({ text: fragment.text, style: { ...fragment.style, baseline: 0 } }, colors)}</span>`; }).join('')}</div>`).join('');
|
|
159
|
+
const viewportWidth = textViewportWidth(layout);
|
|
160
|
+
return `<foreignObject x="0" y="0" width="${n(viewportWidth)}" height="${n(layout.height)}" data-ppt-layout="measured" data-ppt-font-scale="${n(layout.scale)}"><div xmlns="http://www.w3.org/1999/xhtml" style="position:relative;width:${n(viewportWidth)}px;height:${n(layout.height)}px;overflow:hidden;font-family:Arial,sans-serif;color:#000;white-space:pre">${lines}</div></foreignObject>`;
|
|
161
|
+
}
|
|
162
|
+
warn(supported ? (options.textOutput === 'svg' ? 'TEXT_SVG_MEASUREMENT_UNAVAILABLE' : 'TEXT_MEASUREMENT_UNAVAILABLE') : 'TEXT_DIRECTION_NATIVE_LAYOUT', supported ? 'Canvas or a supplied text measurer is unavailable; browser foreign-object text layout is used' : 'Vertical or bidirectional text uses the native browser foreign-object layout path');
|
|
163
|
+
}
|
|
164
|
+
if (frame.paragraphs.some(p => p.style.tabStops?.length))
|
|
165
|
+
warn('TEXT_TABS_NATIVE', 'Explicit tab alignment requires measured layout; native layout uses uniform default tab stops');
|
|
166
|
+
if (frame.paragraphs.some(p => p.style.alignment === 'distributed'))
|
|
167
|
+
warn('TEXT_DISTRIBUTED_NATIVE', 'Distributed alignment uses browser inter-character justification; measured layout supplies explicit character positions');
|
|
168
|
+
if (frame.autofit && frame.autofit !== 'none')
|
|
169
|
+
warn('TEXT_AUTOFIT_NATIVE', 'Dynamic text autofit requires measured horizontal layout; the native path retains the source frame bounds');
|
|
170
|
+
if (frame.fontScale != null && frame.fontScale !== 1) {
|
|
171
|
+
const scale = Math.min(1, Math.max(.05, frame.fontScale));
|
|
172
|
+
frame = { ...frame, paragraphs: frame.paragraphs.map(p => ({ ...p, runs: p.runs.map(r => ({ ...r, style: { ...r.style, fontSize: (r.style.fontSize ?? 24) * scale } })) })) };
|
|
173
|
+
}
|
|
174
|
+
const m = frame.margins, align = frame.verticalAlignment === 'middle' ? 'center' : frame.verticalAlignment === 'bottom' ? 'flex-end' : 'flex-start';
|
|
175
|
+
const style = ['box-sizing:border-box', `width:${n(width)}px`, `height:${n(height)}px`, `padding:${n(m.top)}px ${n(m.right)}px ${n(m.bottom)}px ${n(m.left)}px`, 'display:flex', 'flex-direction:column', `justify-content:${align}`, 'font-family:Arial,sans-serif', 'font-size:24px', 'font-weight:400', 'font-style:normal', 'line-height:1.15', 'color:#000', 'overflow:hidden', `white-space:${frame.wrap ? 'pre-wrap' : 'pre'}`, frame.wrap ? 'overflow-wrap:normal' : '', 'word-break:normal', 'text-rendering:geometricPrecision'];
|
|
176
|
+
if (frame.direction === 'vertical')
|
|
177
|
+
style.push('writing-mode:vertical-rl');
|
|
178
|
+
if (frame.direction === 'vertical270')
|
|
179
|
+
style.push('writing-mode:vertical-rl', 'transform:rotate(180deg)');
|
|
180
|
+
return `<foreignObject x="0" y="0" width="${n(width)}" height="${n(height)}"><div xmlns="http://www.w3.org/1999/xhtml" style="${escapeXml(style.filter(Boolean).join(';'))}"><div style="flex:none;min-width:0;width:100%">${frame.paragraphs.map(p => paragraphHtml(p, colors)).join('')}</div></div></foreignObject>`;
|
|
181
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PptExtrusion, PptShape } from '../types.js';
|
|
2
|
+
import type { EffectContext } from './effects.js';
|
|
3
|
+
export interface MeshPoint {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
z: number;
|
|
7
|
+
}
|
|
8
|
+
export interface PathContour {
|
|
9
|
+
points: {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
}[];
|
|
13
|
+
closed: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface ExtrusionFace {
|
|
16
|
+
points: MeshPoint[];
|
|
17
|
+
depth: number;
|
|
18
|
+
intensity: number;
|
|
19
|
+
visible: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface ExtrusionMesh {
|
|
22
|
+
faces: ExtrusionFace[];
|
|
23
|
+
frontMatrix: [number, number, number, number, number, number];
|
|
24
|
+
contours: PathContour[];
|
|
25
|
+
}
|
|
26
|
+
/** Bounded SVG path flattening, including endpoint-parameterized elliptical arcs. */
|
|
27
|
+
export declare function flattenPath(path: string, maxPoints?: number): PathContour[];
|
|
28
|
+
/** Constrained Y-then-X rotation followed by the selected projection. */
|
|
29
|
+
export declare function projectExtrusionPoint(point: MeshPoint, e: PptExtrusion, width: number, height: number): MeshPoint;
|
|
30
|
+
/** Closed contours produce true side quads; the original vector painting is the front face. */
|
|
31
|
+
export declare function buildExtrusionMesh(shape: PptShape): ExtrusionMesh | undefined;
|
|
32
|
+
export declare function renderExtrusion(shape: PptShape, frontMarkup: string, ctx: EffectContext): string;
|