gbpptx 0.5.2 → 0.6.1
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.md +16 -0
- package/package.json +47 -47
- package/src/index.js +29 -29
- package/src/kernel/model.js +651 -651
- package/src/kernel/parts.js +173 -173
- package/src/kernel/pptx.js +72 -72
- package/src/kernel/print.js +76 -76
- package/src/kernel/read.js +1018 -1018
- package/src/kernel/roundtrip.js +174 -174
- package/src/kernel/text.js +278 -278
- package/src/kernel/write.js +385 -385
- package/web/app/slides/file.js +29 -2
- package/web/app/slides-app.js +25 -1
- package/web/app/ui/icons.js +2 -0
- package/web/app/ui/titlebar.js +253 -263
- package/web/index.html +3 -2
- package/web/serve.mjs +3 -2
- package/web/styles/slides.css +1 -1
package/src/kernel/write.js
CHANGED
|
@@ -1,385 +1,385 @@
|
|
|
1
|
-
// [文件] gbpptx·生成 —— Presentation 序列化成 .pptx(页内 spTree 承载全部元素 + 内嵌媒体 + 备注页)
|
|
2
|
-
// [设计] 逻辑画布按 96dpi 写成 EMU,画幅由快照 size 决定(16:9/4:3/自定义均原样保留)。
|
|
3
|
-
// [坑] 图片只认 data URL:前端插图一律 base64 内嵌,远程 URL 会被静默跳过(离线打开必然丢图)。
|
|
4
|
-
|
|
5
|
-
import { OpcPackage, decodeDataUrl, hexColor, pxToEmu, xmlEscape, xmlEscapeAttr } from 'gbooxml';
|
|
6
|
-
import { DEFAULT_THEME, THEMES_HEX, boolAttr, fontSizeToSz, maxF, themeOfHex } from './pptx.js';
|
|
7
|
-
import { DASH_STYLES, sizeOf, shapePreset, tableStyleColors, transitionOf } from './model.js';
|
|
8
|
-
import {
|
|
9
|
-
CT_APP_PROPS, CT_CORE_PROPS, CT_LAYOUT, CT_MASTER, CT_NOTES, CT_PRESENTATION, CT_SLIDE, CT_THEME,
|
|
10
|
-
PART_APP_PROPS, PART_CORE_PROPS, PART_LAYOUT, PART_MASTER, PART_PRESENTATION, PART_THEME,
|
|
11
|
-
REL_APP_PROPS, REL_CORE_PROPS, REL_IMAGE, REL_LAYOUT, REL_MASTER, REL_NOTES, REL_OFFICE_DOC, REL_SLIDE, REL_THEME,
|
|
12
|
-
XML_HEAD, NS_DECL, NS_DRAWING_M, appProps, coreProps, notesFileName, notesPath, notesSlidePart,
|
|
13
|
-
presentationPart, slideFileName, slideID, slideLayoutPart, slideMasterPart, slidePath, spTree, themePart,
|
|
14
|
-
} from './parts.js';
|
|
15
|
-
import { htmlToParas, parasOfPlain } from './text.js';
|
|
16
|
-
|
|
17
|
-
/* ---------- 元素级默认取值(与 model.js 工厂同值) ---------- */
|
|
18
|
-
const SHAPE_STROKE = '#333333';
|
|
19
|
-
const SHAPE_TEXT_COLOR = '#ffffff';
|
|
20
|
-
const TABLE_FONT_SIZE = 16;
|
|
21
|
-
const LINE_STROKE_W = 2;
|
|
22
|
-
const TABLE_BORDER_W = 1;
|
|
23
|
-
|
|
24
|
-
/** 模型里的颜色(CSS 形式)→ 部件属性要的大写裸十六进制 */
|
|
25
|
-
const xmlColor = (css) => hexColor(css, '');
|
|
26
|
-
|
|
27
|
-
/** 实心填充片段;空色一律 noFill(形状可无填充) */
|
|
28
|
-
const fillXML = (color, fallback = '') => {
|
|
29
|
-
const c = xmlColor(color || fallback);
|
|
30
|
-
return c ? `<a:solidFill><a:srgbClr val="${c}"/></a:solidFill>` : '<a:noFill/>';
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/** 线条片段(宽度为 0 或没有颜色 → 无线条) */
|
|
34
|
-
function strokeXML(color, width, dash = 'solid') {
|
|
35
|
-
const c = xmlColor(color);
|
|
36
|
-
if (!c || !(width > 0)) return '<a:ln><a:noFill/></a:ln>';
|
|
37
|
-
let out = `<a:ln w="${pxToEmu(width)}" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:srgbClr val="${c}"/></a:solidFill>`;
|
|
38
|
-
if (dash && dash !== 'solid' && DASH_STYLES[dash]) out += `<a:prstDash val="${dash}"/>`;
|
|
39
|
-
return `${out}</a:ln>`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** 阴影效果(开关式;与渲染层的 drop-shadow 同口径) */
|
|
43
|
-
const shadowXML = (on) => (on
|
|
44
|
-
? '<a:effectLst><a:outerShdw blurRad="40000" dist="20000" dir="5400000" algn="ctr" rotWithShape="0">' +
|
|
45
|
-
'<a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw></a:effectLst>'
|
|
46
|
-
: '<a:effectLst/>');
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 把 Presentation 序列化为 .pptx 字节
|
|
50
|
-
* @param {object} p 快照({version,title,theme,size,slides[]})
|
|
51
|
-
* @returns {Promise<Uint8Array>}
|
|
52
|
-
*/
|
|
53
|
-
export async function buildPptx(p) {
|
|
54
|
-
if (!p) throw new Error('演示文稿为空');
|
|
55
|
-
const slides = (p.slides || []).filter(Boolean);
|
|
56
|
-
if (slides.length === 0) throw new Error('演示文稿没有幻灯片');
|
|
57
|
-
const size = sizeOf(p);
|
|
58
|
-
|
|
59
|
-
const pkg = new OpcPackage();
|
|
60
|
-
pkg.relAs('', 'rId1', REL_OFFICE_DOC, PART_PRESENTATION);
|
|
61
|
-
pkg.relAs('', 'rId2', REL_CORE_PROPS, PART_CORE_PROPS);
|
|
62
|
-
pkg.relAs('', 'rId3', REL_APP_PROPS, PART_APP_PROPS);
|
|
63
|
-
pkg.relAs(PART_PRESENTATION, 'rId1', REL_MASTER, 'slideMasters/slideMaster1.xml');
|
|
64
|
-
|
|
65
|
-
// 页序列:rId 从 2 起(rId1 留给母版),页号与部件名一一对应
|
|
66
|
-
let slideIDs = '';
|
|
67
|
-
slides.forEach((slide, i) => {
|
|
68
|
-
const rid = `rId${i + 2}`;
|
|
69
|
-
pkg.relAs(PART_PRESENTATION, rid, REL_SLIDE, `slides/${slideFileName(i + 1)}`);
|
|
70
|
-
slideIDs += slideID(i + 1, rid, !!slide.hidden);
|
|
71
|
-
});
|
|
72
|
-
pkg.add(PART_PRESENTATION, CT_PRESENTATION, presentationPart(slideIDs, size));
|
|
73
|
-
|
|
74
|
-
pkg.relAs(PART_MASTER, 'rId1', REL_LAYOUT, '../slideLayouts/slideLayout1.xml');
|
|
75
|
-
pkg.relAs(PART_MASTER, 'rId2', REL_THEME, '../theme/theme1.xml');
|
|
76
|
-
pkg.add(PART_MASTER, CT_MASTER, slideMasterPart());
|
|
77
|
-
pkg.relAs(PART_LAYOUT, 'rId1', REL_MASTER, '../slideMasters/slideMaster1.xml');
|
|
78
|
-
pkg.add(PART_LAYOUT, CT_LAYOUT, slideLayoutPart());
|
|
79
|
-
pkg.add(PART_THEME, CT_THEME, themePart(p.theme));
|
|
80
|
-
pkg.add(PART_CORE_PROPS, CT_CORE_PROPS, coreProps(p.title));
|
|
81
|
-
pkg.add(PART_APP_PROPS, CT_APP_PROPS, appProps());
|
|
82
|
-
|
|
83
|
-
const t = { ...themeOfHex(p.theme), key: p.theme };
|
|
84
|
-
const em = new Emitter(pkg);
|
|
85
|
-
slides.forEach((slide, i) => {
|
|
86
|
-
const path = slidePath(i + 1);
|
|
87
|
-
pkg.relAs(path, 'rId1', REL_LAYOUT, '../slideLayouts/slideLayout1.xml');
|
|
88
|
-
em.source = path;
|
|
89
|
-
pkg.add(path, CT_SLIDE, em.slideXML(slide, t));
|
|
90
|
-
|
|
91
|
-
const notes = String(slide.notes || '').trim();
|
|
92
|
-
if (notes !== '') {
|
|
93
|
-
pkg.rel(path, REL_NOTES, `../notesSlides/${notesFileName(i + 1)}`);
|
|
94
|
-
pkg.relAs(notesPath(i + 1), 'rId1', REL_SLIDE, `../slides/${slideFileName(i + 1)}`);
|
|
95
|
-
pkg.add(notesPath(i + 1), CT_NOTES, notesSlidePart(notes));
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return pkg.bytes();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** emitter 把一页写成 slide 部件,同时把该页引用的媒体与关系登记到部件名下 */
|
|
102
|
-
class Emitter {
|
|
103
|
-
constructor(pkg) {
|
|
104
|
-
this.pkg = pkg;
|
|
105
|
-
this.source = '';
|
|
106
|
-
this.media = 0;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
slideXML(slide, t) {
|
|
110
|
-
// [设计] 页底色一律显式写出:不写的话 PowerPoint 会退回母版白底(回读端靠「等于主题底色即视为随主题」还原)
|
|
111
|
-
const bg = slide.bg || `#${t.bg}`;
|
|
112
|
-
let shapes = '';
|
|
113
|
-
let shapeID = 2; // id 1 被图形树的组元素占用
|
|
114
|
-
for (const o of objectsByZ(slide.objects)) {
|
|
115
|
-
const xml = this.objectXML(shapeID, o, t);
|
|
116
|
-
if (xml === '') continue;
|
|
117
|
-
shapes += xml;
|
|
118
|
-
shapeID += 1;
|
|
119
|
-
}
|
|
120
|
-
return XML_HEAD + `<p:sld ${NS_DECL}><p:cSld><p:bg><p:bgPr><a:solidFill><a:srgbClr val="${hexColor(bg, t.bg)}"/>` +
|
|
121
|
-
'</a:solidFill><a:effectLst/></p:bgPr></p:bg>' +
|
|
122
|
-
`${spTree(shapes)}</p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr>${transitionXML(slide)}</p:sld>`;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** 一个页内元素;未知类型返回空串(图片无效会直接抛错,避免静默丢图) */
|
|
126
|
-
objectXML(id, o, t) {
|
|
127
|
-
switch (o.type) {
|
|
128
|
-
case 'text': return textShapeXML(id, o, t);
|
|
129
|
-
case 'shape': return shapeXML(id, o);
|
|
130
|
-
case 'line': return lineXML(id, o);
|
|
131
|
-
case 'table': return tableFrameXML(id, o, t);
|
|
132
|
-
case 'image': {
|
|
133
|
-
// 保留导入时的原始格式(png/jpeg/gif/svg/bmp/tiff/emf/wmf/webp),避免「打开再保存」丢图
|
|
134
|
-
const decoded = decodeDataUrl(o.props.src);
|
|
135
|
-
if (!decoded) throw new Error('图片数据无效或不是内嵌 data URL,无法写入 .pptx');
|
|
136
|
-
this.media += 1;
|
|
137
|
-
const name = `image${this.media}.${decoded.ext}`;
|
|
138
|
-
this.pkg.addMedia(`ppt/media/${name}`, decoded.data);
|
|
139
|
-
const rid = this.pkg.rel(this.source, REL_IMAGE, `../media/${name}`);
|
|
140
|
-
return pictureXML(id, o, rid);
|
|
141
|
-
}
|
|
142
|
-
default: return '';
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** 按层级升序(同级保持原顺序:稳定排序) */
|
|
148
|
-
const objectsByZ = (objects) => [...(objects || [])].filter(Boolean).sort((a, b) => (a.z || 0) - (b.z || 0));
|
|
149
|
-
|
|
150
|
-
/* ---------- 页切换 ---------- */
|
|
151
|
-
|
|
152
|
-
const TRANSITION_DIR = new Set(['l', 'r', 'u', 'd']);
|
|
153
|
-
|
|
154
|
-
function transitionXML(slide) {
|
|
155
|
-
const tr = transitionOf(slide);
|
|
156
|
-
if (!tr || tr.type === 'none') return '';
|
|
157
|
-
const spd = ['slow', 'med', 'fast'].includes(tr.speed) ? tr.speed : 'med';
|
|
158
|
-
const dir = TRANSITION_DIR.has(tr.dir) ? tr.dir : 'l';
|
|
159
|
-
let body = '';
|
|
160
|
-
switch (tr.type) {
|
|
161
|
-
case 'fade': body = '<p:fade/>'; break;
|
|
162
|
-
case 'push': body = `<p:push dir="${dir}"/>`; break;
|
|
163
|
-
case 'wipe': body = `<p:wipe dir="${dir}"/>`; break;
|
|
164
|
-
case 'cover': body = `<p:cover dir="${dir}"/>`; break;
|
|
165
|
-
case 'split': body = '<p:split orient="horz" dir="out"/>'; break;
|
|
166
|
-
case 'zoom': body = '<p:zoom dir="in"/>'; break;
|
|
167
|
-
default: return '';
|
|
168
|
-
}
|
|
169
|
-
return `<p:transition spd="${spd}">${body}</p:transition>`;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/* ---------- 页内元素 ---------- */
|
|
173
|
-
|
|
174
|
-
/** 位置与尺寸(旋转以 1/60000 度计) */
|
|
175
|
-
function xfrm(x, y, w, h, rot, flipH, flipV) {
|
|
176
|
-
let attrs = '';
|
|
177
|
-
if (rot) attrs += ` rot="${Math.trunc(rot * 60000)}"`;
|
|
178
|
-
if (flipH) attrs += ' flipH="1"';
|
|
179
|
-
if (flipV) attrs += ' flipV="1"';
|
|
180
|
-
return `<a:xfrm${attrs}><a:off x="${pxToEmu(x)}" y="${pxToEmu(y)}"/>` +
|
|
181
|
-
`<a:ext cx="${pxToEmu(maxF(1, w))}" cy="${pxToEmu(maxF(1, h))}"/></a:xfrm>`;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/** 段落对齐值 */
|
|
185
|
-
function algn(align) {
|
|
186
|
-
switch (align) {
|
|
187
|
-
case 'center': return 'ctr';
|
|
188
|
-
case 'right': return 'r';
|
|
189
|
-
case 'justify': return 'just';
|
|
190
|
-
default: return 'l';
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const anchorAttr = (anchor) => {
|
|
195
|
-
switch (anchor) {
|
|
196
|
-
case 'middle': return 'ctr';
|
|
197
|
-
case 'bottom': return 'b';
|
|
198
|
-
default: return 't';
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
/** CSS 字体栈 → OOXML 只认的第一个字族 */
|
|
203
|
-
const fontName = (f) => String(f || '').split(',')[0].trim().replace(/^['"]|['"]$/g, '');
|
|
204
|
-
|
|
205
|
-
/** run 级属性;run 没给的字段回落到对象级基准 */
|
|
206
|
-
function runPr(r, b) {
|
|
207
|
-
const font = fontName(r.font || b.font);
|
|
208
|
-
const size = r.size > 0 ? r.size : b.size;
|
|
209
|
-
const color = r.color || b.color;
|
|
210
|
-
let sb = `<a:rPr lang="zh-CN" sz="${fontSizeToSz(size)}"`;
|
|
211
|
-
if (b.bold || r.bold) sb += ' b="1"';
|
|
212
|
-
if (b.italic || r.italic) sb += ' i="1"';
|
|
213
|
-
if (b.underline || r.underline) sb += ' u="sng"';
|
|
214
|
-
if (b.strike || r.strike) sb += ' strike="sngStrike"';
|
|
215
|
-
sb += ' dirty="0">';
|
|
216
|
-
// CT_TextCharacterProperties 的子元素顺序:fill 必须在 latin/ea 之前
|
|
217
|
-
sb += `<a:solidFill><a:srgbClr val="${hexColor(color, THEMES_HEX[DEFAULT_THEME].text)}"/></a:solidFill>`;
|
|
218
|
-
if (font) {
|
|
219
|
-
const e = xmlEscape(font);
|
|
220
|
-
sb += `<a:latin typeface="${e}"/><a:ea typeface="${e}"/>`;
|
|
221
|
-
}
|
|
222
|
-
sb += '</a:rPr>';
|
|
223
|
-
return sb;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/** 段落属性(对齐 + 行距 + 项目符号/编号) */
|
|
227
|
-
function paragraphPr(p, align, lineSpacing) {
|
|
228
|
-
const a = algn(p.align || align);
|
|
229
|
-
const level = Math.max(0, Math.min(8, Number(p.level) || 0));
|
|
230
|
-
const list = p.list === 'bullet' || p.list === 'number';
|
|
231
|
-
// 左对齐也显式写出:形状文字没有版式兜底,省略 algn 会让「左对齐」在回读时退回形状默认值
|
|
232
|
-
let attrs = ` algn="${a}"`;
|
|
233
|
-
if (list) {
|
|
234
|
-
attrs += ` marL="${pxToEmu(24 * (level + 1))}" indent="${pxToEmu(-24)}"`;
|
|
235
|
-
if (level > 0) attrs += ` lvl="${level}"`;
|
|
236
|
-
}
|
|
237
|
-
let body = '';
|
|
238
|
-
if (lineSpacing > 0 && Math.abs(lineSpacing - 1.0) > 0.001) {
|
|
239
|
-
body += `<a:lnSpc><a:spcPct val="${Math.round(lineSpacing * 100000)}"/></a:lnSpc>`;
|
|
240
|
-
}
|
|
241
|
-
if (p.list === 'bullet') {
|
|
242
|
-
body += '<a:buFont typeface="Arial"/><a:buChar char="•"/>';
|
|
243
|
-
} else if (p.list === 'number') {
|
|
244
|
-
body += '<a:buFont typeface="+mj-lt"/><a:buAutoNum type="arabicPeriod"/>';
|
|
245
|
-
}
|
|
246
|
-
return `<a:pPr${attrs}>${body}</a:pPr>`;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/** 段落序列 → <a:p> 串;空段落照出(空 <a:p> 代表一个空行) */
|
|
250
|
-
function parasXML(paras, b, align, lineSpacing = 1.0) {
|
|
251
|
-
const list = paras && paras.length ? paras : [{ align: '', list: '', level: 0, runs: [] }];
|
|
252
|
-
let sb = '';
|
|
253
|
-
for (const p of list) {
|
|
254
|
-
sb += `<a:p>${paragraphPr(p, align, lineSpacing)}`;
|
|
255
|
-
for (const r of p.runs) {
|
|
256
|
-
if (r.text === '') continue;
|
|
257
|
-
sb += `<a:r>${runPr(r, b)}<a:t>${xmlEscape(r.text)}</a:t></a:r>`;
|
|
258
|
-
}
|
|
259
|
-
if (!p.runs.some((r) => r.text !== '')) sb += '<a:endParaRPr lang="zh-CN" dirty="0"/>';
|
|
260
|
-
sb += '</a:p>';
|
|
261
|
-
}
|
|
262
|
-
return sb;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/** 文本框 */
|
|
266
|
-
function textShapeXML(id, o, t) {
|
|
267
|
-
const p = o.props;
|
|
268
|
-
const color = p.color || `#${t.text}`;
|
|
269
|
-
const base = {
|
|
270
|
-
font: p.font, size: p.fontSize, color, bold: p.bold, italic: p.italic,
|
|
271
|
-
underline: p.underline, strike: p.strike,
|
|
272
|
-
};
|
|
273
|
-
const fill = p.fill ? fillXML(p.fill) : '<a:noFill/>';
|
|
274
|
-
const line = p.strokeW > 0 ? strokeXML(p.stroke, p.strokeW) : '<a:ln><a:noFill/></a:ln>';
|
|
275
|
-
return `<p:sp><p:nvSpPr><p:cNvPr id="${id}" name="TextBox ${id}"/><p:cNvSpPr txBox="1"/><p:nvPr/></p:nvSpPr>` +
|
|
276
|
-
`<p:spPr>${xfrm(o.x, o.y, o.w, o.h, o.rot, false, false)}<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>${fill}${line}</p:spPr>` +
|
|
277
|
-
`<p:txBody><a:bodyPr wrap="square" lIns="0" tIns="0" rIns="0" bIns="0" anchor="${anchorAttr(p.anchor)}"/><a:lstStyle/>` +
|
|
278
|
-
`${parasXML(htmlToParas(p.html), base, p.align, p.lineSpacing)}</p:txBody></p:sp>`;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/** 预设几何图形(含内文字) */
|
|
282
|
-
function shapeXML(id, o) {
|
|
283
|
-
const p = o.props;
|
|
284
|
-
const prst = shapePreset(p.kind);
|
|
285
|
-
const fill = fillXML(p.fill);
|
|
286
|
-
const stroke = p.strokeW > 0 ? strokeXML(p.stroke || SHAPE_STROKE, p.strokeW, p.dash) : '<a:ln><a:noFill/></a:ln>';
|
|
287
|
-
let text = `<p:txBody><a:bodyPr anchor="${anchorAttr(p.anchor)}" anchorCtr="0"/><a:lstStyle/><a:p><a:endParaRPr lang="zh-CN" dirty="0"/></a:p></p:txBody>`;
|
|
288
|
-
if (String(p.text || '').trim() !== '') {
|
|
289
|
-
const color = p.textColor || SHAPE_TEXT_COLOR;
|
|
290
|
-
const base = {
|
|
291
|
-
font: p.font, size: p.fontSize, color, bold: p.bold, italic: p.italic,
|
|
292
|
-
underline: p.underline, strike: p.strike,
|
|
293
|
-
};
|
|
294
|
-
text = `<p:txBody><a:bodyPr anchor="${anchorAttr(p.anchor)}" anchorCtr="0"/><a:lstStyle/>` +
|
|
295
|
-
`${parasXML(parasOfPlain(p.text), base, p.align, p.lineSpacing)}</p:txBody>`;
|
|
296
|
-
}
|
|
297
|
-
return `<p:sp><p:nvSpPr><p:cNvPr id="${id}" name="Shape ${id}"/><p:cNvSpPr/><p:nvPr/></p:nvSpPr>` +
|
|
298
|
-
`<p:spPr>${xfrm(o.x, o.y, o.w, o.h, o.rot, false, false)}<a:prstGeom prst="${prst}"><a:avLst/></a:prstGeom>` +
|
|
299
|
-
`${fill}${stroke}${shadowXML(p.shadow)}</p:spPr>${text}</p:sp>`;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/** 直线连接符。编辑器存的是起点 + 向量,负向量靠翻转表达方向 */
|
|
303
|
-
function lineXML(id, o) {
|
|
304
|
-
const p = o.props;
|
|
305
|
-
let x = o.x;
|
|
306
|
-
let y = o.y;
|
|
307
|
-
let w = o.w;
|
|
308
|
-
let h = o.h;
|
|
309
|
-
let flipH = false;
|
|
310
|
-
let flipV = false;
|
|
311
|
-
if (w < 0) { x += w; w = -w; flipH = true; }
|
|
312
|
-
if (h < 0) { y += h; h = -h; flipV = true; }
|
|
313
|
-
const width = p.strokeW > 0 ? p.strokeW : LINE_STROKE_W;
|
|
314
|
-
const color = hexColor(p.stroke, THEMES_HEX[DEFAULT_THEME].text);
|
|
315
|
-
let ln = `<a:ln w="${pxToEmu(width)}"><a:solidFill><a:srgbClr val="${color}"/></a:solidFill>`;
|
|
316
|
-
if (p.dash && p.dash !== 'solid' && DASH_STYLES[p.dash]) ln += `<a:prstDash val="${p.dash}"/>`;
|
|
317
|
-
if (p.arrowStart) ln += '<a:headEnd type="arrow" w="med" len="med"/>';
|
|
318
|
-
if (p.arrowEnd) ln += '<a:tailEnd type="arrow" w="med" len="med"/>';
|
|
319
|
-
ln += '</a:ln>';
|
|
320
|
-
return `<p:cxnSp><p:nvCxnSpPr><p:cNvPr id="${id}" name="Line ${id}"/><p:cNvCxnSpPr/><p:nvPr/></p:nvCxnSpPr>` +
|
|
321
|
-
`<p:spPr>${xfrm(x, y, w, h, 0, flipH, flipV)}<a:prstGeom prst="line"><a:avLst/></a:prstGeom>${ln}</p:spPr></p:cxnSp>`;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/** 图片 */
|
|
325
|
-
function pictureXML(id, o, rid) {
|
|
326
|
-
const p = o.props;
|
|
327
|
-
const crop = p.crop || {};
|
|
328
|
-
const rect = ['l', 't', 'r', 'b'].map((k) => Math.round(Math.max(0, Math.min(99, Number(crop[k]) || 0)) * 1000));
|
|
329
|
-
const srcRect = rect.some((v) => v > 0)
|
|
330
|
-
? `<a:srcRect l="${rect[0]}" t="${rect[1]}" r="${rect[2]}" b="${rect[3]}"/>`
|
|
331
|
-
: '';
|
|
332
|
-
const descr = p.alt ? ` descr="${xmlEscapeAttr(p.alt)}"` : '';
|
|
333
|
-
return `<p:pic><p:nvPicPr><p:cNvPr id="${id}" name="Picture ${id}"${descr}/><p:cNvPicPr/><p:nvPr/></p:nvPicPr>` +
|
|
334
|
-
`<p:blipFill><a:blip r:embed="${rid}"/>${srcRect}<a:stretch><a:fillRect/></a:stretch></p:blipFill>` +
|
|
335
|
-
`<p:spPr>${xfrm(o.x, o.y, o.w, o.h, o.rot, false, false)}<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>${shadowXML(p.shadow)}</p:spPr></p:pic>`;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/** 表格(列宽/行高按百分比分,首行加粗,样式落成显式单元格填充/边框) */
|
|
339
|
-
function tableFrameXML(id, o, t) {
|
|
340
|
-
const p = o.props;
|
|
341
|
-
const rows = p.rows && p.rows.length ? p.rows : [['']];
|
|
342
|
-
const cols = rows[0].length || 1;
|
|
343
|
-
const size = p.fontSize > 0 ? p.fontSize : TABLE_FONT_SIZE;
|
|
344
|
-
const style = tableStyleColors(p.tableStyle, t.key);
|
|
345
|
-
const bodyColor = p.color || style.bodyText || `#${t.text}`;
|
|
346
|
-
const border = xmlColor(style.border);
|
|
347
|
-
|
|
348
|
-
const totalW = pxToEmu(maxF(1, o.w));
|
|
349
|
-
const totalH = pxToEmu(maxF(1, o.h));
|
|
350
|
-
let sb = `<p:graphicFrame><p:nvGraphicFramePr><p:cNvPr id="${id}" name="Table ${id}"/>` +
|
|
351
|
-
'<p:cNvGraphicFramePr/><p:nvPr/></p:nvGraphicFramePr>';
|
|
352
|
-
sb += `<p:xfrm><a:off x="${pxToEmu(o.x)}" y="${pxToEmu(o.y)}"/><a:ext cx="${pxToEmu(maxF(1, o.w))}" cy="${pxToEmu(maxF(1, o.h))}"/></p:xfrm>`;
|
|
353
|
-
sb += `<a:graphic><a:graphicData uri="${NS_DRAWING_M}/table"><a:tbl>` +
|
|
354
|
-
`<a:tblPr firstRow="${boolAttr(p.headerRow)}" bandRow="${boolAttr(p.bandedRows)}"/><a:tblGrid>`;
|
|
355
|
-
for (let c = 0; c < cols; c += 1) sb += `<a:gridCol w="${share(totalW, c, p.colW, cols)}"/>`;
|
|
356
|
-
sb += '</a:tblGrid>';
|
|
357
|
-
rows.forEach((row, ri) => {
|
|
358
|
-
const header = ri === 0 && p.headerRow;
|
|
359
|
-
const band = p.bandedRows && !header && (p.headerRow ? ri % 2 === 1 : ri % 2 === 0);
|
|
360
|
-
const cellFill = header ? style.headerFill : band ? style.bandFill : '';
|
|
361
|
-
const cellColor = header ? (style.headerText || bodyColor) : bodyColor;
|
|
362
|
-
sb += `<a:tr h="${share(totalH, ri, p.rowH, rows.length)}">`;
|
|
363
|
-
const base = { font: p.font, size, color: cellColor, bold: !!p.headerRow && header, italic: false, underline: false, strike: false };
|
|
364
|
-
for (let c = 0; c < cols; c += 1) {
|
|
365
|
-
const cell = c < row.length ? row[c] : '';
|
|
366
|
-
const fill = cellFill ? `<a:solidFill><a:srgbClr val="${xmlColor(cellFill)}"/></a:solidFill>` : '';
|
|
367
|
-
const borderXML = border
|
|
368
|
-
? ['L', 'R', 'T', 'B'].map((side) => `<a:ln${side} w="${pxToEmu(TABLE_BORDER_W)}" cap="flat" cmpd="sng" algn="ctr">` +
|
|
369
|
-
`<a:solidFill><a:srgbClr val="${border}"/></a:solidFill><a:prstDash val="solid"/></a:ln${side}>`).join('')
|
|
370
|
-
: '';
|
|
371
|
-
sb += `<a:tc><a:txBody><a:bodyPr/><a:lstStyle/>${parasXML(parasOfPlain(cell), base, '', 1.2)}</a:txBody>` +
|
|
372
|
-
`<a:tcPr>${borderXML}${fill}</a:tcPr></a:tc>`;
|
|
373
|
-
}
|
|
374
|
-
sb += '</a:tr>';
|
|
375
|
-
});
|
|
376
|
-
sb += '</a:tbl></a:graphicData></a:graphic></p:graphicFrame>';
|
|
377
|
-
return sb;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/** 第 i 格占总长的多少 EMU:百分比表里有有效值就按它分,否则等分 */
|
|
381
|
-
function share(total, i, pcts, count) {
|
|
382
|
-
if (Array.isArray(pcts) && i < pcts.length && pcts[i] > 0) return Math.trunc((total * pcts[i]) / 100);
|
|
383
|
-
return Math.trunc(total / count);
|
|
384
|
-
}
|
|
385
|
-
|
|
1
|
+
// [文件] gbpptx·生成 —— Presentation 序列化成 .pptx(页内 spTree 承载全部元素 + 内嵌媒体 + 备注页)
|
|
2
|
+
// [设计] 逻辑画布按 96dpi 写成 EMU,画幅由快照 size 决定(16:9/4:3/自定义均原样保留)。
|
|
3
|
+
// [坑] 图片只认 data URL:前端插图一律 base64 内嵌,远程 URL 会被静默跳过(离线打开必然丢图)。
|
|
4
|
+
|
|
5
|
+
import { OpcPackage, decodeDataUrl, hexColor, pxToEmu, xmlEscape, xmlEscapeAttr } from 'gbooxml';
|
|
6
|
+
import { DEFAULT_THEME, THEMES_HEX, boolAttr, fontSizeToSz, maxF, themeOfHex } from './pptx.js';
|
|
7
|
+
import { DASH_STYLES, sizeOf, shapePreset, tableStyleColors, transitionOf } from './model.js';
|
|
8
|
+
import {
|
|
9
|
+
CT_APP_PROPS, CT_CORE_PROPS, CT_LAYOUT, CT_MASTER, CT_NOTES, CT_PRESENTATION, CT_SLIDE, CT_THEME,
|
|
10
|
+
PART_APP_PROPS, PART_CORE_PROPS, PART_LAYOUT, PART_MASTER, PART_PRESENTATION, PART_THEME,
|
|
11
|
+
REL_APP_PROPS, REL_CORE_PROPS, REL_IMAGE, REL_LAYOUT, REL_MASTER, REL_NOTES, REL_OFFICE_DOC, REL_SLIDE, REL_THEME,
|
|
12
|
+
XML_HEAD, NS_DECL, NS_DRAWING_M, appProps, coreProps, notesFileName, notesPath, notesSlidePart,
|
|
13
|
+
presentationPart, slideFileName, slideID, slideLayoutPart, slideMasterPart, slidePath, spTree, themePart,
|
|
14
|
+
} from './parts.js';
|
|
15
|
+
import { htmlToParas, parasOfPlain } from './text.js';
|
|
16
|
+
|
|
17
|
+
/* ---------- 元素级默认取值(与 model.js 工厂同值) ---------- */
|
|
18
|
+
const SHAPE_STROKE = '#333333';
|
|
19
|
+
const SHAPE_TEXT_COLOR = '#ffffff';
|
|
20
|
+
const TABLE_FONT_SIZE = 16;
|
|
21
|
+
const LINE_STROKE_W = 2;
|
|
22
|
+
const TABLE_BORDER_W = 1;
|
|
23
|
+
|
|
24
|
+
/** 模型里的颜色(CSS 形式)→ 部件属性要的大写裸十六进制 */
|
|
25
|
+
const xmlColor = (css) => hexColor(css, '');
|
|
26
|
+
|
|
27
|
+
/** 实心填充片段;空色一律 noFill(形状可无填充) */
|
|
28
|
+
const fillXML = (color, fallback = '') => {
|
|
29
|
+
const c = xmlColor(color || fallback);
|
|
30
|
+
return c ? `<a:solidFill><a:srgbClr val="${c}"/></a:solidFill>` : '<a:noFill/>';
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** 线条片段(宽度为 0 或没有颜色 → 无线条) */
|
|
34
|
+
function strokeXML(color, width, dash = 'solid') {
|
|
35
|
+
const c = xmlColor(color);
|
|
36
|
+
if (!c || !(width > 0)) return '<a:ln><a:noFill/></a:ln>';
|
|
37
|
+
let out = `<a:ln w="${pxToEmu(width)}" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:srgbClr val="${c}"/></a:solidFill>`;
|
|
38
|
+
if (dash && dash !== 'solid' && DASH_STYLES[dash]) out += `<a:prstDash val="${dash}"/>`;
|
|
39
|
+
return `${out}</a:ln>`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 阴影效果(开关式;与渲染层的 drop-shadow 同口径) */
|
|
43
|
+
const shadowXML = (on) => (on
|
|
44
|
+
? '<a:effectLst><a:outerShdw blurRad="40000" dist="20000" dir="5400000" algn="ctr" rotWithShape="0">' +
|
|
45
|
+
'<a:srgbClr val="000000"><a:alpha val="35000"/></a:srgbClr></a:outerShdw></a:effectLst>'
|
|
46
|
+
: '<a:effectLst/>');
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 把 Presentation 序列化为 .pptx 字节
|
|
50
|
+
* @param {object} p 快照({version,title,theme,size,slides[]})
|
|
51
|
+
* @returns {Promise<Uint8Array>}
|
|
52
|
+
*/
|
|
53
|
+
export async function buildPptx(p) {
|
|
54
|
+
if (!p) throw new Error('演示文稿为空');
|
|
55
|
+
const slides = (p.slides || []).filter(Boolean);
|
|
56
|
+
if (slides.length === 0) throw new Error('演示文稿没有幻灯片');
|
|
57
|
+
const size = sizeOf(p);
|
|
58
|
+
|
|
59
|
+
const pkg = new OpcPackage();
|
|
60
|
+
pkg.relAs('', 'rId1', REL_OFFICE_DOC, PART_PRESENTATION);
|
|
61
|
+
pkg.relAs('', 'rId2', REL_CORE_PROPS, PART_CORE_PROPS);
|
|
62
|
+
pkg.relAs('', 'rId3', REL_APP_PROPS, PART_APP_PROPS);
|
|
63
|
+
pkg.relAs(PART_PRESENTATION, 'rId1', REL_MASTER, 'slideMasters/slideMaster1.xml');
|
|
64
|
+
|
|
65
|
+
// 页序列:rId 从 2 起(rId1 留给母版),页号与部件名一一对应
|
|
66
|
+
let slideIDs = '';
|
|
67
|
+
slides.forEach((slide, i) => {
|
|
68
|
+
const rid = `rId${i + 2}`;
|
|
69
|
+
pkg.relAs(PART_PRESENTATION, rid, REL_SLIDE, `slides/${slideFileName(i + 1)}`);
|
|
70
|
+
slideIDs += slideID(i + 1, rid, !!slide.hidden);
|
|
71
|
+
});
|
|
72
|
+
pkg.add(PART_PRESENTATION, CT_PRESENTATION, presentationPart(slideIDs, size));
|
|
73
|
+
|
|
74
|
+
pkg.relAs(PART_MASTER, 'rId1', REL_LAYOUT, '../slideLayouts/slideLayout1.xml');
|
|
75
|
+
pkg.relAs(PART_MASTER, 'rId2', REL_THEME, '../theme/theme1.xml');
|
|
76
|
+
pkg.add(PART_MASTER, CT_MASTER, slideMasterPart());
|
|
77
|
+
pkg.relAs(PART_LAYOUT, 'rId1', REL_MASTER, '../slideMasters/slideMaster1.xml');
|
|
78
|
+
pkg.add(PART_LAYOUT, CT_LAYOUT, slideLayoutPart());
|
|
79
|
+
pkg.add(PART_THEME, CT_THEME, themePart(p.theme));
|
|
80
|
+
pkg.add(PART_CORE_PROPS, CT_CORE_PROPS, coreProps(p.title));
|
|
81
|
+
pkg.add(PART_APP_PROPS, CT_APP_PROPS, appProps());
|
|
82
|
+
|
|
83
|
+
const t = { ...themeOfHex(p.theme), key: p.theme };
|
|
84
|
+
const em = new Emitter(pkg);
|
|
85
|
+
slides.forEach((slide, i) => {
|
|
86
|
+
const path = slidePath(i + 1);
|
|
87
|
+
pkg.relAs(path, 'rId1', REL_LAYOUT, '../slideLayouts/slideLayout1.xml');
|
|
88
|
+
em.source = path;
|
|
89
|
+
pkg.add(path, CT_SLIDE, em.slideXML(slide, t));
|
|
90
|
+
|
|
91
|
+
const notes = String(slide.notes || '').trim();
|
|
92
|
+
if (notes !== '') {
|
|
93
|
+
pkg.rel(path, REL_NOTES, `../notesSlides/${notesFileName(i + 1)}`);
|
|
94
|
+
pkg.relAs(notesPath(i + 1), 'rId1', REL_SLIDE, `../slides/${slideFileName(i + 1)}`);
|
|
95
|
+
pkg.add(notesPath(i + 1), CT_NOTES, notesSlidePart(notes));
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return pkg.bytes();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** emitter 把一页写成 slide 部件,同时把该页引用的媒体与关系登记到部件名下 */
|
|
102
|
+
class Emitter {
|
|
103
|
+
constructor(pkg) {
|
|
104
|
+
this.pkg = pkg;
|
|
105
|
+
this.source = '';
|
|
106
|
+
this.media = 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
slideXML(slide, t) {
|
|
110
|
+
// [设计] 页底色一律显式写出:不写的话 PowerPoint 会退回母版白底(回读端靠「等于主题底色即视为随主题」还原)
|
|
111
|
+
const bg = slide.bg || `#${t.bg}`;
|
|
112
|
+
let shapes = '';
|
|
113
|
+
let shapeID = 2; // id 1 被图形树的组元素占用
|
|
114
|
+
for (const o of objectsByZ(slide.objects)) {
|
|
115
|
+
const xml = this.objectXML(shapeID, o, t);
|
|
116
|
+
if (xml === '') continue;
|
|
117
|
+
shapes += xml;
|
|
118
|
+
shapeID += 1;
|
|
119
|
+
}
|
|
120
|
+
return XML_HEAD + `<p:sld ${NS_DECL}><p:cSld><p:bg><p:bgPr><a:solidFill><a:srgbClr val="${hexColor(bg, t.bg)}"/>` +
|
|
121
|
+
'</a:solidFill><a:effectLst/></p:bgPr></p:bg>' +
|
|
122
|
+
`${spTree(shapes)}</p:cSld><p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr>${transitionXML(slide)}</p:sld>`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 一个页内元素;未知类型返回空串(图片无效会直接抛错,避免静默丢图) */
|
|
126
|
+
objectXML(id, o, t) {
|
|
127
|
+
switch (o.type) {
|
|
128
|
+
case 'text': return textShapeXML(id, o, t);
|
|
129
|
+
case 'shape': return shapeXML(id, o);
|
|
130
|
+
case 'line': return lineXML(id, o);
|
|
131
|
+
case 'table': return tableFrameXML(id, o, t);
|
|
132
|
+
case 'image': {
|
|
133
|
+
// 保留导入时的原始格式(png/jpeg/gif/svg/bmp/tiff/emf/wmf/webp),避免「打开再保存」丢图
|
|
134
|
+
const decoded = decodeDataUrl(o.props.src);
|
|
135
|
+
if (!decoded) throw new Error('图片数据无效或不是内嵌 data URL,无法写入 .pptx');
|
|
136
|
+
this.media += 1;
|
|
137
|
+
const name = `image${this.media}.${decoded.ext}`;
|
|
138
|
+
this.pkg.addMedia(`ppt/media/${name}`, decoded.data);
|
|
139
|
+
const rid = this.pkg.rel(this.source, REL_IMAGE, `../media/${name}`);
|
|
140
|
+
return pictureXML(id, o, rid);
|
|
141
|
+
}
|
|
142
|
+
default: return '';
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** 按层级升序(同级保持原顺序:稳定排序) */
|
|
148
|
+
const objectsByZ = (objects) => [...(objects || [])].filter(Boolean).sort((a, b) => (a.z || 0) - (b.z || 0));
|
|
149
|
+
|
|
150
|
+
/* ---------- 页切换 ---------- */
|
|
151
|
+
|
|
152
|
+
const TRANSITION_DIR = new Set(['l', 'r', 'u', 'd']);
|
|
153
|
+
|
|
154
|
+
function transitionXML(slide) {
|
|
155
|
+
const tr = transitionOf(slide);
|
|
156
|
+
if (!tr || tr.type === 'none') return '';
|
|
157
|
+
const spd = ['slow', 'med', 'fast'].includes(tr.speed) ? tr.speed : 'med';
|
|
158
|
+
const dir = TRANSITION_DIR.has(tr.dir) ? tr.dir : 'l';
|
|
159
|
+
let body = '';
|
|
160
|
+
switch (tr.type) {
|
|
161
|
+
case 'fade': body = '<p:fade/>'; break;
|
|
162
|
+
case 'push': body = `<p:push dir="${dir}"/>`; break;
|
|
163
|
+
case 'wipe': body = `<p:wipe dir="${dir}"/>`; break;
|
|
164
|
+
case 'cover': body = `<p:cover dir="${dir}"/>`; break;
|
|
165
|
+
case 'split': body = '<p:split orient="horz" dir="out"/>'; break;
|
|
166
|
+
case 'zoom': body = '<p:zoom dir="in"/>'; break;
|
|
167
|
+
default: return '';
|
|
168
|
+
}
|
|
169
|
+
return `<p:transition spd="${spd}">${body}</p:transition>`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/* ---------- 页内元素 ---------- */
|
|
173
|
+
|
|
174
|
+
/** 位置与尺寸(旋转以 1/60000 度计) */
|
|
175
|
+
function xfrm(x, y, w, h, rot, flipH, flipV) {
|
|
176
|
+
let attrs = '';
|
|
177
|
+
if (rot) attrs += ` rot="${Math.trunc(rot * 60000)}"`;
|
|
178
|
+
if (flipH) attrs += ' flipH="1"';
|
|
179
|
+
if (flipV) attrs += ' flipV="1"';
|
|
180
|
+
return `<a:xfrm${attrs}><a:off x="${pxToEmu(x)}" y="${pxToEmu(y)}"/>` +
|
|
181
|
+
`<a:ext cx="${pxToEmu(maxF(1, w))}" cy="${pxToEmu(maxF(1, h))}"/></a:xfrm>`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** 段落对齐值 */
|
|
185
|
+
function algn(align) {
|
|
186
|
+
switch (align) {
|
|
187
|
+
case 'center': return 'ctr';
|
|
188
|
+
case 'right': return 'r';
|
|
189
|
+
case 'justify': return 'just';
|
|
190
|
+
default: return 'l';
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const anchorAttr = (anchor) => {
|
|
195
|
+
switch (anchor) {
|
|
196
|
+
case 'middle': return 'ctr';
|
|
197
|
+
case 'bottom': return 'b';
|
|
198
|
+
default: return 't';
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/** CSS 字体栈 → OOXML 只认的第一个字族 */
|
|
203
|
+
const fontName = (f) => String(f || '').split(',')[0].trim().replace(/^['"]|['"]$/g, '');
|
|
204
|
+
|
|
205
|
+
/** run 级属性;run 没给的字段回落到对象级基准 */
|
|
206
|
+
function runPr(r, b) {
|
|
207
|
+
const font = fontName(r.font || b.font);
|
|
208
|
+
const size = r.size > 0 ? r.size : b.size;
|
|
209
|
+
const color = r.color || b.color;
|
|
210
|
+
let sb = `<a:rPr lang="zh-CN" sz="${fontSizeToSz(size)}"`;
|
|
211
|
+
if (b.bold || r.bold) sb += ' b="1"';
|
|
212
|
+
if (b.italic || r.italic) sb += ' i="1"';
|
|
213
|
+
if (b.underline || r.underline) sb += ' u="sng"';
|
|
214
|
+
if (b.strike || r.strike) sb += ' strike="sngStrike"';
|
|
215
|
+
sb += ' dirty="0">';
|
|
216
|
+
// CT_TextCharacterProperties 的子元素顺序:fill 必须在 latin/ea 之前
|
|
217
|
+
sb += `<a:solidFill><a:srgbClr val="${hexColor(color, THEMES_HEX[DEFAULT_THEME].text)}"/></a:solidFill>`;
|
|
218
|
+
if (font) {
|
|
219
|
+
const e = xmlEscape(font);
|
|
220
|
+
sb += `<a:latin typeface="${e}"/><a:ea typeface="${e}"/>`;
|
|
221
|
+
}
|
|
222
|
+
sb += '</a:rPr>';
|
|
223
|
+
return sb;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** 段落属性(对齐 + 行距 + 项目符号/编号) */
|
|
227
|
+
function paragraphPr(p, align, lineSpacing) {
|
|
228
|
+
const a = algn(p.align || align);
|
|
229
|
+
const level = Math.max(0, Math.min(8, Number(p.level) || 0));
|
|
230
|
+
const list = p.list === 'bullet' || p.list === 'number';
|
|
231
|
+
// 左对齐也显式写出:形状文字没有版式兜底,省略 algn 会让「左对齐」在回读时退回形状默认值
|
|
232
|
+
let attrs = ` algn="${a}"`;
|
|
233
|
+
if (list) {
|
|
234
|
+
attrs += ` marL="${pxToEmu(24 * (level + 1))}" indent="${pxToEmu(-24)}"`;
|
|
235
|
+
if (level > 0) attrs += ` lvl="${level}"`;
|
|
236
|
+
}
|
|
237
|
+
let body = '';
|
|
238
|
+
if (lineSpacing > 0 && Math.abs(lineSpacing - 1.0) > 0.001) {
|
|
239
|
+
body += `<a:lnSpc><a:spcPct val="${Math.round(lineSpacing * 100000)}"/></a:lnSpc>`;
|
|
240
|
+
}
|
|
241
|
+
if (p.list === 'bullet') {
|
|
242
|
+
body += '<a:buFont typeface="Arial"/><a:buChar char="•"/>';
|
|
243
|
+
} else if (p.list === 'number') {
|
|
244
|
+
body += '<a:buFont typeface="+mj-lt"/><a:buAutoNum type="arabicPeriod"/>';
|
|
245
|
+
}
|
|
246
|
+
return `<a:pPr${attrs}>${body}</a:pPr>`;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** 段落序列 → <a:p> 串;空段落照出(空 <a:p> 代表一个空行) */
|
|
250
|
+
function parasXML(paras, b, align, lineSpacing = 1.0) {
|
|
251
|
+
const list = paras && paras.length ? paras : [{ align: '', list: '', level: 0, runs: [] }];
|
|
252
|
+
let sb = '';
|
|
253
|
+
for (const p of list) {
|
|
254
|
+
sb += `<a:p>${paragraphPr(p, align, lineSpacing)}`;
|
|
255
|
+
for (const r of p.runs) {
|
|
256
|
+
if (r.text === '') continue;
|
|
257
|
+
sb += `<a:r>${runPr(r, b)}<a:t>${xmlEscape(r.text)}</a:t></a:r>`;
|
|
258
|
+
}
|
|
259
|
+
if (!p.runs.some((r) => r.text !== '')) sb += '<a:endParaRPr lang="zh-CN" dirty="0"/>';
|
|
260
|
+
sb += '</a:p>';
|
|
261
|
+
}
|
|
262
|
+
return sb;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** 文本框 */
|
|
266
|
+
function textShapeXML(id, o, t) {
|
|
267
|
+
const p = o.props;
|
|
268
|
+
const color = p.color || `#${t.text}`;
|
|
269
|
+
const base = {
|
|
270
|
+
font: p.font, size: p.fontSize, color, bold: p.bold, italic: p.italic,
|
|
271
|
+
underline: p.underline, strike: p.strike,
|
|
272
|
+
};
|
|
273
|
+
const fill = p.fill ? fillXML(p.fill) : '<a:noFill/>';
|
|
274
|
+
const line = p.strokeW > 0 ? strokeXML(p.stroke, p.strokeW) : '<a:ln><a:noFill/></a:ln>';
|
|
275
|
+
return `<p:sp><p:nvSpPr><p:cNvPr id="${id}" name="TextBox ${id}"/><p:cNvSpPr txBox="1"/><p:nvPr/></p:nvSpPr>` +
|
|
276
|
+
`<p:spPr>${xfrm(o.x, o.y, o.w, o.h, o.rot, false, false)}<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>${fill}${line}</p:spPr>` +
|
|
277
|
+
`<p:txBody><a:bodyPr wrap="square" lIns="0" tIns="0" rIns="0" bIns="0" anchor="${anchorAttr(p.anchor)}"/><a:lstStyle/>` +
|
|
278
|
+
`${parasXML(htmlToParas(p.html), base, p.align, p.lineSpacing)}</p:txBody></p:sp>`;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** 预设几何图形(含内文字) */
|
|
282
|
+
function shapeXML(id, o) {
|
|
283
|
+
const p = o.props;
|
|
284
|
+
const prst = shapePreset(p.kind);
|
|
285
|
+
const fill = fillXML(p.fill);
|
|
286
|
+
const stroke = p.strokeW > 0 ? strokeXML(p.stroke || SHAPE_STROKE, p.strokeW, p.dash) : '<a:ln><a:noFill/></a:ln>';
|
|
287
|
+
let text = `<p:txBody><a:bodyPr anchor="${anchorAttr(p.anchor)}" anchorCtr="0"/><a:lstStyle/><a:p><a:endParaRPr lang="zh-CN" dirty="0"/></a:p></p:txBody>`;
|
|
288
|
+
if (String(p.text || '').trim() !== '') {
|
|
289
|
+
const color = p.textColor || SHAPE_TEXT_COLOR;
|
|
290
|
+
const base = {
|
|
291
|
+
font: p.font, size: p.fontSize, color, bold: p.bold, italic: p.italic,
|
|
292
|
+
underline: p.underline, strike: p.strike,
|
|
293
|
+
};
|
|
294
|
+
text = `<p:txBody><a:bodyPr anchor="${anchorAttr(p.anchor)}" anchorCtr="0"/><a:lstStyle/>` +
|
|
295
|
+
`${parasXML(parasOfPlain(p.text), base, p.align, p.lineSpacing)}</p:txBody>`;
|
|
296
|
+
}
|
|
297
|
+
return `<p:sp><p:nvSpPr><p:cNvPr id="${id}" name="Shape ${id}"/><p:cNvSpPr/><p:nvPr/></p:nvSpPr>` +
|
|
298
|
+
`<p:spPr>${xfrm(o.x, o.y, o.w, o.h, o.rot, false, false)}<a:prstGeom prst="${prst}"><a:avLst/></a:prstGeom>` +
|
|
299
|
+
`${fill}${stroke}${shadowXML(p.shadow)}</p:spPr>${text}</p:sp>`;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** 直线连接符。编辑器存的是起点 + 向量,负向量靠翻转表达方向 */
|
|
303
|
+
function lineXML(id, o) {
|
|
304
|
+
const p = o.props;
|
|
305
|
+
let x = o.x;
|
|
306
|
+
let y = o.y;
|
|
307
|
+
let w = o.w;
|
|
308
|
+
let h = o.h;
|
|
309
|
+
let flipH = false;
|
|
310
|
+
let flipV = false;
|
|
311
|
+
if (w < 0) { x += w; w = -w; flipH = true; }
|
|
312
|
+
if (h < 0) { y += h; h = -h; flipV = true; }
|
|
313
|
+
const width = p.strokeW > 0 ? p.strokeW : LINE_STROKE_W;
|
|
314
|
+
const color = hexColor(p.stroke, THEMES_HEX[DEFAULT_THEME].text);
|
|
315
|
+
let ln = `<a:ln w="${pxToEmu(width)}"><a:solidFill><a:srgbClr val="${color}"/></a:solidFill>`;
|
|
316
|
+
if (p.dash && p.dash !== 'solid' && DASH_STYLES[p.dash]) ln += `<a:prstDash val="${p.dash}"/>`;
|
|
317
|
+
if (p.arrowStart) ln += '<a:headEnd type="arrow" w="med" len="med"/>';
|
|
318
|
+
if (p.arrowEnd) ln += '<a:tailEnd type="arrow" w="med" len="med"/>';
|
|
319
|
+
ln += '</a:ln>';
|
|
320
|
+
return `<p:cxnSp><p:nvCxnSpPr><p:cNvPr id="${id}" name="Line ${id}"/><p:cNvCxnSpPr/><p:nvPr/></p:nvCxnSpPr>` +
|
|
321
|
+
`<p:spPr>${xfrm(x, y, w, h, 0, flipH, flipV)}<a:prstGeom prst="line"><a:avLst/></a:prstGeom>${ln}</p:spPr></p:cxnSp>`;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** 图片 */
|
|
325
|
+
function pictureXML(id, o, rid) {
|
|
326
|
+
const p = o.props;
|
|
327
|
+
const crop = p.crop || {};
|
|
328
|
+
const rect = ['l', 't', 'r', 'b'].map((k) => Math.round(Math.max(0, Math.min(99, Number(crop[k]) || 0)) * 1000));
|
|
329
|
+
const srcRect = rect.some((v) => v > 0)
|
|
330
|
+
? `<a:srcRect l="${rect[0]}" t="${rect[1]}" r="${rect[2]}" b="${rect[3]}"/>`
|
|
331
|
+
: '';
|
|
332
|
+
const descr = p.alt ? ` descr="${xmlEscapeAttr(p.alt)}"` : '';
|
|
333
|
+
return `<p:pic><p:nvPicPr><p:cNvPr id="${id}" name="Picture ${id}"${descr}/><p:cNvPicPr/><p:nvPr/></p:nvPicPr>` +
|
|
334
|
+
`<p:blipFill><a:blip r:embed="${rid}"/>${srcRect}<a:stretch><a:fillRect/></a:stretch></p:blipFill>` +
|
|
335
|
+
`<p:spPr>${xfrm(o.x, o.y, o.w, o.h, o.rot, false, false)}<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>${shadowXML(p.shadow)}</p:spPr></p:pic>`;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** 表格(列宽/行高按百分比分,首行加粗,样式落成显式单元格填充/边框) */
|
|
339
|
+
function tableFrameXML(id, o, t) {
|
|
340
|
+
const p = o.props;
|
|
341
|
+
const rows = p.rows && p.rows.length ? p.rows : [['']];
|
|
342
|
+
const cols = rows[0].length || 1;
|
|
343
|
+
const size = p.fontSize > 0 ? p.fontSize : TABLE_FONT_SIZE;
|
|
344
|
+
const style = tableStyleColors(p.tableStyle, t.key);
|
|
345
|
+
const bodyColor = p.color || style.bodyText || `#${t.text}`;
|
|
346
|
+
const border = xmlColor(style.border);
|
|
347
|
+
|
|
348
|
+
const totalW = pxToEmu(maxF(1, o.w));
|
|
349
|
+
const totalH = pxToEmu(maxF(1, o.h));
|
|
350
|
+
let sb = `<p:graphicFrame><p:nvGraphicFramePr><p:cNvPr id="${id}" name="Table ${id}"/>` +
|
|
351
|
+
'<p:cNvGraphicFramePr/><p:nvPr/></p:nvGraphicFramePr>';
|
|
352
|
+
sb += `<p:xfrm><a:off x="${pxToEmu(o.x)}" y="${pxToEmu(o.y)}"/><a:ext cx="${pxToEmu(maxF(1, o.w))}" cy="${pxToEmu(maxF(1, o.h))}"/></p:xfrm>`;
|
|
353
|
+
sb += `<a:graphic><a:graphicData uri="${NS_DRAWING_M}/table"><a:tbl>` +
|
|
354
|
+
`<a:tblPr firstRow="${boolAttr(p.headerRow)}" bandRow="${boolAttr(p.bandedRows)}"/><a:tblGrid>`;
|
|
355
|
+
for (let c = 0; c < cols; c += 1) sb += `<a:gridCol w="${share(totalW, c, p.colW, cols)}"/>`;
|
|
356
|
+
sb += '</a:tblGrid>';
|
|
357
|
+
rows.forEach((row, ri) => {
|
|
358
|
+
const header = ri === 0 && p.headerRow;
|
|
359
|
+
const band = p.bandedRows && !header && (p.headerRow ? ri % 2 === 1 : ri % 2 === 0);
|
|
360
|
+
const cellFill = header ? style.headerFill : band ? style.bandFill : '';
|
|
361
|
+
const cellColor = header ? (style.headerText || bodyColor) : bodyColor;
|
|
362
|
+
sb += `<a:tr h="${share(totalH, ri, p.rowH, rows.length)}">`;
|
|
363
|
+
const base = { font: p.font, size, color: cellColor, bold: !!p.headerRow && header, italic: false, underline: false, strike: false };
|
|
364
|
+
for (let c = 0; c < cols; c += 1) {
|
|
365
|
+
const cell = c < row.length ? row[c] : '';
|
|
366
|
+
const fill = cellFill ? `<a:solidFill><a:srgbClr val="${xmlColor(cellFill)}"/></a:solidFill>` : '';
|
|
367
|
+
const borderXML = border
|
|
368
|
+
? ['L', 'R', 'T', 'B'].map((side) => `<a:ln${side} w="${pxToEmu(TABLE_BORDER_W)}" cap="flat" cmpd="sng" algn="ctr">` +
|
|
369
|
+
`<a:solidFill><a:srgbClr val="${border}"/></a:solidFill><a:prstDash val="solid"/></a:ln${side}>`).join('')
|
|
370
|
+
: '';
|
|
371
|
+
sb += `<a:tc><a:txBody><a:bodyPr/><a:lstStyle/>${parasXML(parasOfPlain(cell), base, '', 1.2)}</a:txBody>` +
|
|
372
|
+
`<a:tcPr>${borderXML}${fill}</a:tcPr></a:tc>`;
|
|
373
|
+
}
|
|
374
|
+
sb += '</a:tr>';
|
|
375
|
+
});
|
|
376
|
+
sb += '</a:tbl></a:graphicData></a:graphic></p:graphicFrame>';
|
|
377
|
+
return sb;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** 第 i 格占总长的多少 EMU:百分比表里有有效值就按它分,否则等分 */
|
|
381
|
+
function share(total, i, pcts, count) {
|
|
382
|
+
if (Array.isArray(pcts) && i < pcts.length && pcts[i] > 0) return Math.trunc((total * pcts[i]) / 100);
|
|
383
|
+
return Math.trunc(total / count);
|
|
384
|
+
}
|
|
385
|
+
|