gbpptx 0.1.1 → 0.2.0

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.
@@ -3,7 +3,8 @@
3
3
  // 因此导入导出都不需要解析 sldLayout 的占位符几何——代价是导出后的页不套用模板版式(所见即所得)。
4
4
 
5
5
  import { hexColor, LINK_COLOR, pxToEmu, xmlEscape } from 'gbooxml';
6
- import { SLIDE_H, SLIDE_W, THEME_ORDER, THEMES_HEX, DEFAULT_THEME, isThemeKey, themeOfHex } from './pptx.js';
6
+ import { THEME_ORDER, THEMES_HEX, DEFAULT_THEME, isThemeKey, themeOfHex } from './pptx.js';
7
+ import { normalizeSize } from './model.js';
7
8
 
8
9
  /* ---------- 命名空间 ---------- */
9
10
 
@@ -91,16 +92,19 @@ const FMT_SCHEME = '<a:fmtScheme name="Guabi"><a:fillStyleLst>' +
91
92
  '</a:bgFillStyleLst></a:fmtScheme>';
92
93
 
93
94
  /** 演示文稿主部件(页 id 从 256 起、母版 id 固定 2^31——PowerPoint 的隐含要求) */
94
- export const presentationPart = (slideIDs) =>
95
- XML_HEAD + `<p:presentation ${NS_DECL}>` +
96
- '<p:sldMasterIdLst><p:sldMasterId id="2147483648" r:id="rId1"/></p:sldMasterIdLst>' +
97
- `<p:sldIdLst>${slideIDs}</p:sldIdLst>` +
98
- `<p:sldSz cx="${pxToEmu(SLIDE_W)}" cy="${pxToEmu(SLIDE_H)}"/>` +
99
- '<p:notesSz cx="6858000" cy="9144000"/>' +
100
- '</p:presentation>';
101
-
102
- /** 页条目(id 从 256 起,rId 指向该页的关系) */
103
- export const slideID = (n, rid) => `<p:sldId id="${255 + n}" r:id="${rid}"/>`;
95
+ export const presentationPart = (slideIDs, size) => {
96
+ const s = normalizeSize(size);
97
+ return XML_HEAD + `<p:presentation ${NS_DECL}>` +
98
+ '<p:sldMasterIdLst><p:sldMasterId id="2147483648" r:id="rId1"/></p:sldMasterIdLst>' +
99
+ `<p:sldIdLst>${slideIDs}</p:sldIdLst>` +
100
+ `<p:sldSz cx="${pxToEmu(s.w)}" cy="${pxToEmu(s.h)}"/>` +
101
+ '<p:notesSz cx="6858000" cy="9144000"/>' +
102
+ '</p:presentation>';
103
+ };
104
+
105
+ /** 页条目(id 从 256 起,rId 指向该页的关系;hidden 写成 show="0") */
106
+ export const slideID = (n, rid, hidden = false) =>
107
+ `<p:sldId id="${255 + n}" r:id="${rid}"${hidden ? ' show="0"' : ''}/>`;
104
108
 
105
109
  /** 母版:空图形树 + 一个空白版式 */
106
110
  export const slideMasterPart = () =>
@@ -1,10 +1,9 @@
1
1
  // [文件] gbpptx·转换引擎常量 —— 模型⇄PresentationML 的主题表、字号/几何换算与图形预设映射(移植 Go pptx.go)
2
2
 
3
- import { THEMES, THEME_KEYS, themeOf } from './model.js';
3
+ import { SHAPE_KINDS, SLIDE_H, SLIDE_W, THEMES, THEME_KEYS, themeOf } from './model.js';
4
4
 
5
5
  /** 幻灯片逻辑坐标(16:9),与前端画布、放映、打印同一坐标系 */
6
- export const SLIDE_W = 960;
7
- export const SLIDE_H = 540;
6
+ export { SLIDE_W, SLIDE_H };
8
7
 
9
8
  /** 主题表(大写裸十六进制形式):由 model.js 的 THEMES 派生,禁另立第二份色值(改一处必漏一处) */
10
9
  const toHex = (css) => String(css).replace('#', '').toUpperCase();
@@ -47,16 +46,22 @@ export const themeLabel = (key) => themeOf(key).label;
47
46
 
48
47
  /* ---------- 图形预设映射 ---------- */
49
48
 
50
- /** PresentationML prst → 编辑器 kind */
51
- export const PRST_TO_KIND = {
52
- rect: 'rect', roundRect: 'round', ellipse: 'ellipse', triangle: 'triangle',
53
- diamond: 'diamond', rightArrow: 'arrow', star5: 'star', chevron: 'chevron',
54
- };
49
+ /** 编辑器 kind → PresentationML prst(唯一来源:model.js 的形状库) */
50
+ export const KIND_TO_PRST = Object.fromEntries(SHAPE_KINDS.map((k) => [k.kind, k.prst]));
55
51
 
56
- /** 编辑器 kind → PresentationML prst */
57
- export const KIND_TO_PRST = {
58
- rect: 'rect', round: 'roundRect', ellipse: 'ellipse', triangle: 'triangle',
59
- diamond: 'diamond', arrow: 'rightArrow', star: 'star5', chevron: 'chevron',
52
+ /** PresentationML prst → 编辑器 kind;不认识的预设按最接近的可编辑形状收口 */
53
+ export const PRST_TO_KIND = {
54
+ ...Object.fromEntries(SHAPE_KINDS.map((k) => [k.prst, k.kind])),
55
+ flowChartProcess: 'rect',
56
+ flowChartAlternateProcess: 'round',
57
+ flowChartDecision: 'diamond',
58
+ flowChartTerminator: 'round',
59
+ flowChartDocument: 'rect',
60
+ flowChartPredefinedProcess: 'rect',
61
+ flowChartConnector: 'ellipse',
62
+ wedgeRectCallout: 'rect',
63
+ wedgeRoundRectCallout: 'round',
64
+ wedgeEllipseCallout: 'ellipse',
60
65
  };
61
66
 
62
67
  /** 是否像 pptx(OPC 包一律是 zip;旧版 .ppt 是 OLE 复合文档,魔数 D0CF) */
@@ -1,15 +1,14 @@
1
- // [文件] gbpptx·打印 / PDF 通道 —— 逐页堆进隐藏 iframe 调起浏览器打印(用户在对话框选「另存为 PDF」即得 PDF;移植 slidesPrint.js)
2
- // 说明:页渲染从 UI 侧注入(renderPage(slide, theme) → HTMLElement),内核不耦合渲染实现。
1
+ // [文件] gbpptx·打印 / PDF 通道 —— 逐页堆进隐藏 iframe 调起浏览器打印(用户在对话框选「另存为 PDF」即得 PDF)
2
+ // 说明:页渲染从 UI 侧注入(renderPage(slide, theme, {size}) → HTMLElement),内核不耦合渲染实现。
3
3
 
4
- import { SLIDE_W } from './model.js';
4
+ import { SLIDE_H, SLIDE_W, normalizeSize } from './model.js';
5
5
 
6
- /** A4 横向纸张尺寸按 96dpi CSS px 换算:幻灯片占满纸宽,剩余高度留作上下白边 */
6
+ /** A4 横向纸张尺寸按 96dpi CSS px 换算 */
7
7
  const SHEET_W = Math.round((297 / 25.4) * 96);
8
8
  /** [坑] 纸高留 2px 余量:mm→px 四舍五入后正好等于纸高时,浏览器会多切一张空白页 */
9
9
  const SHEET_H = Math.round((210 / 25.4) * 96) - 2;
10
- const PRINT_SCALE = SHEET_W / SLIDE_W;
11
10
 
12
- const PRINT_CSS = `
11
+ const printCss = (scale) => `
13
12
  @page { size: A4 landscape; margin: 0 }
14
13
  html, body { margin: 0; padding: 0 }
15
14
  .pptx-print-page {
@@ -22,7 +21,7 @@ html, body { margin: 0; padding: 0 }
22
21
  break-after: page;
23
22
  }
24
23
  .pptx-print-page:last-child { break-after: auto }
25
- .pptx-print-scale { transform: scale(${PRINT_SCALE}); transform-origin: center center }
24
+ .pptx-print-scale { transform: scale(${scale}); transform-origin: center center }
26
25
  `;
27
26
 
28
27
  /** 主文档样式表整套搬进 iframe(样式与正文同源,不在 iframe 里重抄一份) */
@@ -34,21 +33,24 @@ const styleTags = () =>
34
33
  * 把页栈逐页堆进隐藏 iframe 并调起浏览器打印。
35
34
  * @param {Array} slides 归一后的页面列表
36
35
  * @param {string} theme 主题键
37
- * @param {(slide:object, theme:string) => HTMLElement} renderPage 单页渲染器(UI 注入)
36
+ * @param {(slide:object, theme:string, opts:object) => HTMLElement} renderPage 单页渲染器(UI 注入)
37
+ * @param {{w:number,h:number}} [size] 画幅
38
38
  * @returns {Promise<void>} 打印对话框关闭后兑现
39
39
  */
40
- export const printSlides = (slides, theme, renderPage) => new Promise((resolve) => {
40
+ export const printSlides = (slides, theme, renderPage, size = { w: SLIDE_W, h: SLIDE_H }) => new Promise((resolve) => {
41
41
  if (!slides || !slides.length || typeof renderPage !== 'function') return resolve();
42
+ const paper = normalizeSize(size);
43
+ const scale = Math.min(SHEET_W / paper.w, SHEET_H / paper.h);
42
44
  const host = document.createElement('div');
43
45
  host.style.cssText = 'position:fixed;left:-99999px;top:0';
44
46
  document.body.appendChild(host);
45
47
  for (const slide of slides) {
46
48
  const page = document.createElement('div');
47
49
  page.className = 'pptx-print-page';
48
- const scale = document.createElement('div');
49
- scale.className = 'pptx-print-scale';
50
- scale.appendChild(renderPage(slide, theme));
51
- page.appendChild(scale);
50
+ const box = document.createElement('div');
51
+ box.className = 'pptx-print-scale';
52
+ box.appendChild(renderPage(slide, theme, { size: paper }));
53
+ page.appendChild(box);
52
54
  host.appendChild(page);
53
55
  }
54
56
  const html = host.innerHTML;
@@ -62,7 +64,7 @@ export const printSlides = (slides, theme, renderPage) => new Promise((resolve)
62
64
  document.body.appendChild(frame);
63
65
 
64
66
  const doc = frame.contentDocument;
65
- doc.head.innerHTML = `<meta charset="utf-8">${styleTags()}<style>${PRINT_CSS}</style>`;
67
+ doc.head.innerHTML = `<meta charset="utf-8">${styleTags()}<style>${printCss(scale)}</style>`;
66
68
  doc.body.innerHTML = html;
67
69
 
68
70
  const cleanup = () => {