@sw7d/paper-renderer 0.1.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.
Files changed (38) hide show
  1. package/README.MD +10 -0
  2. package/dist/paper-renderer.js +6862 -0
  3. package/dist/src/components/PaperEditor.vue.d.ts +172 -0
  4. package/dist/src/components/PaperPreview.vue.d.ts +26 -0
  5. package/dist/src/composables/paperHelpers.d.ts +35 -0
  6. package/dist/src/composables/usePageSvgRenderer.d.ts +26 -0
  7. package/dist/src/composables/usePaperBlockOps.d.ts +74 -0
  8. package/dist/src/composables/usePaperCore.d.ts +181 -0
  9. package/dist/src/composables/usePaperCursor.d.ts +45 -0
  10. package/dist/src/composables/usePaperSelection.d.ts +45 -0
  11. package/dist/src/composables/useTableRenderer.d.ts +33 -0
  12. package/dist/src/engine/PaperLayout.d.ts +59 -0
  13. package/dist/src/engine/SvgRenderer.d.ts +75 -0
  14. package/dist/src/engine/svg/headerRenderer.d.ts +36 -0
  15. package/dist/src/engine/svg/optionsRenderer.d.ts +23 -0
  16. package/dist/src/engine/svg/questionRenderer.d.ts +35 -0
  17. package/dist/src/index.d.ts +35 -0
  18. package/dist/src/types/document.d.ts +498 -0
  19. package/dist/src/types/paper.d.ts +278 -0
  20. package/dist/src/utils/charWidth.d.ts +46 -0
  21. package/dist/src/utils/chineseNumber.d.ts +50 -0
  22. package/dist/src/utils/deserialization/parseDocument.d.ts +19 -0
  23. package/dist/src/utils/deserialization/parseInlines.d.ts +13 -0
  24. package/dist/src/utils/deserialization/parseQuestions.d.ts +21 -0
  25. package/dist/src/utils/documentSerializer.d.ts +33 -0
  26. package/dist/src/utils/mathjax.d.ts +57 -0
  27. package/dist/src/utils/paperHtmlBuilder.d.ts +21 -0
  28. package/dist/src/utils/qrcode.d.ts +7 -0
  29. package/dist/src/utils/questionHtmlParser.d.ts +40 -0
  30. package/dist/src/utils/questionJsonParser.d.ts +61 -0
  31. package/dist/src/utils/serialization/buildBody.d.ts +11 -0
  32. package/dist/src/utils/serialization/buildPagesExport.d.ts +45 -0
  33. package/dist/src/utils/serialization/buildQuestions.d.ts +23 -0
  34. package/dist/src/utils/serialization/shared.d.ts +61 -0
  35. package/dist/src/utils/textWrap.d.ts +36 -0
  36. package/dist/style.css +2 -0
  37. package/dist/vite.config.d.ts +2 -0
  38. package/package.json +53 -0
@@ -0,0 +1,278 @@
1
+ /**
2
+ * 试卷排版引擎 - 核心类型定义
3
+ */
4
+ export type PaperSize = 'A4' | 'A3' | '8K' | '16K';
5
+ export type ColumnMode = 1 | 2 | 3;
6
+ export interface PaperDimensions {
7
+ width: number;
8
+ height: number;
9
+ label: string;
10
+ }
11
+ export declare const PAPER_DIMENSIONS: Record<PaperSize, PaperDimensions>;
12
+ /** 纸张物理尺寸(mm),用于序列化/预览中的 px↔mm 转换
13
+ * 宽高比必须与 PAPER_DIMENSIONS 一致(A3/16K 为横向试卷) */
14
+ export declare const PAPER_MM: Record<PaperSize, {
15
+ w: number;
16
+ h: number;
17
+ }>;
18
+ export interface PageSettings {
19
+ paperSize: PaperSize;
20
+ columns: ColumnMode;
21
+ fontSize: number;
22
+ lineSpacing: number;
23
+ margins: [number, number, number, number];
24
+ showCornerMarks: boolean;
25
+ showBBoxes: boolean;
26
+ columnGap: number;
27
+ /** 从页面顶部到 header 容器的 Y 偏移,统一为 18 */
28
+ headerTop: number;
29
+ /** 首页(偶数页)body 的 Y 偏移 */
30
+ bodyTopEven: number;
31
+ /** 其他页(奇数页)body 的 Y 偏移 */
32
+ bodyTopOdd: number;
33
+ /** 是否显示分数 */
34
+ showScore: boolean;
35
+ }
36
+ /**
37
+ * 每款纸张的默认配置预设
38
+ * 切换纸张时自动应用,但用户可手动覆盖
39
+ */
40
+ export interface PaperPreset {
41
+ columns: ColumnMode;
42
+ margins: [number, number, number, number];
43
+ columnGap: number;
44
+ headerTop: number;
45
+ bodyTopEven: number;
46
+ bodyTopOdd: number;
47
+ }
48
+ export declare const PAPER_PRESETS: Record<PaperSize, PaperPreset>;
49
+ /** 根据纸张尺寸获取预设配置 */
50
+ export declare function getPaperPreset(size: PaperSize): PaperPreset;
51
+ /**
52
+ * 从 paper.type 字符串解析纸张尺寸和列数
53
+ * 格式示例: "A4", "A3_2", "8K_2", "16K_2"
54
+ */
55
+ export declare function parsePaperType(paperType: string | undefined): {
56
+ size: PaperSize;
57
+ columns: ColumnMode;
58
+ };
59
+ export declare const DEFAULT_PAGE_SETTINGS: PageSettings;
60
+ export type InlineType = 'text' | 'formula' | 'underline' | 'image' | 'enter' | 'object-fill';
61
+ export interface BaseInline {
62
+ type: InlineType;
63
+ width: number;
64
+ height: number;
65
+ }
66
+ export interface TextInline extends BaseInline {
67
+ type: 'text';
68
+ value: string;
69
+ fontFamily: string;
70
+ fontSize: number;
71
+ bold: boolean;
72
+ italic: boolean;
73
+ color: string;
74
+ xPositions: number[];
75
+ /** 加点字:文字上方渲染小圆点 */
76
+ dotAbove?: boolean;
77
+ /** 波浪线:文字下方渲染波浪线 */
78
+ wavyLine?: boolean;
79
+ /** 下划线(普通文本下划线,区别于 UnderlineInline 填空) */
80
+ underline?: boolean;
81
+ /** 删除线 */
82
+ strikethrough?: boolean;
83
+ /** 背景色 / 高亮色 */
84
+ backgroundColor?: string;
85
+ /** 垂直偏移量(正=下移,负=上移),用于上下标 */
86
+ verticalAlign?: number;
87
+ }
88
+ export interface FormulaInline extends BaseInline {
89
+ type: 'formula';
90
+ svgContent: string;
91
+ /** 原始 LaTeX 字符串,如 "50^{\circ }" */
92
+ rawLatex?: string;
93
+ /** 原始 MathML 字符串(当 data-latex 为空时),用于序列化 */
94
+ rawMathml?: string;
95
+ }
96
+ export interface UnderlineInline extends BaseInline {
97
+ type: 'underline';
98
+ placeholder: string;
99
+ lineWidth: number;
100
+ isEmpty: boolean;
101
+ id?: string;
102
+ topicId?: string;
103
+ realIndex?: number;
104
+ /** 填空序号 */
105
+ index?: number;
106
+ /** 该题总空数 */
107
+ blankCount?: number;
108
+ /** 是否显示空序号 */
109
+ showBlankIndex?: boolean;
110
+ /** 是否允许跨行拆分 */
111
+ split?: boolean;
112
+ /** 最小拆分宽度 */
113
+ minSplitWidth?: number;
114
+ /** 标准空序号 */
115
+ standardBlankIndex?: number;
116
+ /** 是否非题干模式 */
117
+ isNoStemMode?: boolean;
118
+ /** 空白类型:tiankong=填空题, xuanze=选择题, judge=判断题 */
119
+ blankType?: 'tiankong' | 'xuanze' | 'judge';
120
+ /** 标准答案(从题库HTML标签内文本提取) */
121
+ correctAnswer?: string;
122
+ /** 是否为折行拆分后的后续段(非首段),由 wrapElements 设置 */
123
+ isSplitPart?: boolean;
124
+ }
125
+ export interface ImageInline extends BaseInline {
126
+ type: 'image';
127
+ src: string;
128
+ /** 行内图片唯一标识,用于点击检测和转换为锚点 */
129
+ id?: string;
130
+ /** 原始 HTML(如 <table>),由前端 html2canvas 渲染为 dataURL 后填入 src */
131
+ rawHtml?: string;
132
+ }
133
+ export interface EnterInline extends BaseInline {
134
+ type: 'enter';
135
+ /** 是否为 <br> 强制换行标记(不由 wrapElements 自动折行) */
136
+ isLineBreak?: boolean;
137
+ }
138
+ /** 涂卡区(选择题答题区) */
139
+ export interface ObjectFillInline extends BaseInline {
140
+ type: 'object-fill';
141
+ answerType?: string;
142
+ index?: number;
143
+ blankCount?: number;
144
+ }
145
+ export type InlineElement = TextInline | FormulaInline | UnderlineInline | ImageInline | EnterInline | ObjectFillInline;
146
+ export interface Line {
147
+ elements: InlineElement[];
148
+ baseline: number;
149
+ lineHeight: number;
150
+ y: number;
151
+ /** 行内元素 X 偏移(用于文字环绕等场景) */
152
+ offsetX?: number;
153
+ /** 此行的有效宽度(折行时考虑环绕后的实际可用宽度),用于对齐计算 */
154
+ availableWidth?: number;
155
+ }
156
+ export interface Paragraph {
157
+ lines: Line[];
158
+ y: number;
159
+ height: number;
160
+ /** 段落对齐方式:left/center/right */
161
+ justification?: 'left' | 'center' | 'right';
162
+ /** 是否允许跨栏/跨页拆分,默认 true */
163
+ split?: boolean;
164
+ }
165
+ export interface OptionItem {
166
+ /** 选项标签,如 "A", "B" */
167
+ label: string;
168
+ /** 选项内容 HTML(原始数据,用于序列化保留) */
169
+ contentHtml: string;
170
+ /** 解析后的段落(复用 Paragraph 结构) */
171
+ paragraphs: Paragraph[];
172
+ }
173
+ /** 题目类型 */
174
+ export type QuestionType = 'single_chose' | 'multi_chose' | 'fill_blank' | 'answer_question';
175
+ export type BlockType = 'question' | 'sub-question' | 'title' | 'info-line' | 'separator';
176
+ export interface ContentBlock {
177
+ id: string;
178
+ type: BlockType;
179
+ paragraphs: Paragraph[];
180
+ height: number;
181
+ metadata: Record<string, any>;
182
+ /** 选择题选项(仅选择题有值) */
183
+ options?: OptionItem[];
184
+ /** 题目类型 */
185
+ questionType?: QuestionType;
186
+ /** 与下一块保持同行/同栏(题组标题等场景) */
187
+ keepNext?: boolean;
188
+ /** 内容保护开关:true 时禁止删除/修改文字,仅允许回车折行 */
189
+ protectContent?: boolean;
190
+ }
191
+ export interface PageBlock {
192
+ blockId: string;
193
+ blockType: BlockType;
194
+ paragraphStart: number;
195
+ paragraphCount: number;
196
+ x: number;
197
+ y: number;
198
+ width: number;
199
+ height: number;
200
+ }
201
+ export interface CornerMark {
202
+ position: 'tl' | 'tr' | 'bl' | 'br';
203
+ bbox: BBox;
204
+ }
205
+ /**
206
+ * BBox — 紧凑矩形坐标 [x, y, w, h]
207
+ *
208
+ * 所有坐标值保留最多 2 位小数,单位:模板像素
209
+ * 坐标系:页面左上角 (0, 0)
210
+ */
211
+ export type BBox = [number, number, number, number];
212
+ /** 构造 BBox,自动 round 到 2 位小数 */
213
+ export declare function toBBox(x: number, y: number, w: number, h: number): BBox;
214
+ /** round to 2 decimal places */
215
+ export declare function r2(n: number): number;
216
+ /**
217
+ * 渲染区域 — 页面上所有有意义元素的绝对像素坐标
218
+ *
219
+ * 坐标系:页面左上角 (0, 0),单位:模板像素
220
+ * 与 CornerMark 同一坐标系,可直接用于批改透视变换
221
+ *
222
+ * 批改流程:
223
+ * 1. 检测扫描图中 4 个角标 → 计算透视变换矩阵 H
224
+ * 2. 对每个 RenderedRegion 的 bbox 应用 H → 扫描图上的实际位置
225
+ * 3. 裁剪区域 → 识别 / 打分
226
+ */
227
+ export interface RenderedRegion {
228
+ /** 区域类型 */
229
+ type: 'header-desc' | 'paper-name' | 'qr-code' | 'student-name' | 'student-class' | 'student-code' | 'score' | 'page-number' | 'correction-box' | 'title-embed' | 'column-separator' | 'question-border' | 'underline';
230
+ /** 页面绝对坐标 [x, y, w, h] (px) */
231
+ bbox: BBox;
232
+ /** 附加信息(如 blockId、题号等) */
233
+ meta?: Record<string, any>;
234
+ }
235
+ export interface Page {
236
+ index: number;
237
+ width: number;
238
+ height: number;
239
+ blocks: PageBlock[];
240
+ cornerMarks: CornerMark[];
241
+ /** 渲染后所有有意义区域的绝对坐标(由 SvgRenderer.computePageRegions 填充) */
242
+ renderedRegions: RenderedRegion[];
243
+ }
244
+ export interface PaperDocument {
245
+ settings: PageSettings;
246
+ blocks: ContentBlock[];
247
+ pages: Page[];
248
+ }
249
+ export interface SelectionState {
250
+ blockId: string | null;
251
+ paragraphIndex: number | null;
252
+ lineIndex: number | null;
253
+ elementIndex: number | null;
254
+ }
255
+ export interface EditorState {
256
+ doc: PaperDocument;
257
+ selection: SelectionState;
258
+ zoom: number;
259
+ currentPage: number;
260
+ isPreviewMode: boolean;
261
+ }
262
+ /** 渲染模式:edit=编辑模式(含虚线边框、点击区等),view=浏览模式(纯净 SVG) */
263
+ export type RenderMode = 'edit' | 'view';
264
+ /** Header 布局模式:由纸张方向决定 */
265
+ export type HeaderLayoutMode = 'portrait' | 'landscape';
266
+ /**
267
+ * Header 布局配置 — 由 PaperLayout/SvgRenderer 根据纸张方向动态计算
268
+ *
269
+ * - portrait(竖向纸张 A4/8K):contentWidth = 全页内容宽度,元素居中/居左
270
+ * - landscape(横向纸张 A3/16K):contentWidth = 首栏宽度,元素在半宽内居中/居左
271
+ */
272
+ export interface HeaderLayoutConfig {
273
+ mode: HeaderLayoutMode;
274
+ /** header 内容区宽度(px) */
275
+ contentWidth: number;
276
+ /** header 内容区 X 偏移(相对于 marginLeft,px) */
277
+ contentX: number;
278
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * charWidth.ts — Unicode 字符像素宽度计算
3
+ *
4
+ * 使用 Canvas 2D 精确测量文本宽度(匹配浏览器实际渲染),
5
+ * 回退到估算值(当 Canvas 不可用时,如 SSR 环境)。
6
+ *
7
+ * 参考:学科网渲染实现
8
+ * 汉字 "若关于" x 偏移:35 → 38.5(+3.5=空格) → 52.5(+14=汉字) → 66.5(+14=汉字)
9
+ * 其中 fontSize=14: 空格=3.5=14*0.25, 汉字=14=14*1.0
10
+ */
11
+ /**
12
+ * 获取单个字符的宽度(px)
13
+ * 优先使用 Canvas 精确测量,回退到估算值
14
+ * @param char 字符
15
+ * @param fontSize 字号(px)
16
+ * @param fontFamily 字体族(可选,默认 Times New Roman + 宋体)
17
+ * @param bold 是否粗体
18
+ * @param italic 是否斜体
19
+ */
20
+ export declare function getCharWidth(char: string, fontSize: number, fontFamily?: string, bold?: boolean, italic?: boolean): number;
21
+ /**
22
+ * 计算文本段的总宽度
23
+ * @param text 文本
24
+ * @param fontSize 字号(px)
25
+ * @param fontFamily 字体族(可选)
26
+ * @param bold 是否粗体
27
+ * @param italic 是否斜体
28
+ */
29
+ export declare function calcTextWidth(text: string, fontSize: number, fontFamily?: string, bold?: boolean, italic?: boolean): number;
30
+ /**
31
+ * 计算文本段每个字符的 X 偏移位置(相对起点,不含字符本身宽度)
32
+ * 返回数组长度 = 字符数,每个值为该字符起始 X 偏移
33
+ */
34
+ export declare function calcCharPositions(text: string, fontSize: number, fontFamily?: string, bold?: boolean, italic?: boolean): number[];
35
+ /**
36
+ * 计算文本段的精确高度(ascent + descent)
37
+ * 使用 Canvas actualBoundingBoxAscent / actualBoundingBoxDescent
38
+ * 回退到估算值(当 Canvas 不可用时)
39
+ */
40
+ export declare function calcTextHeight(text: string, fontSize: number, fontFamily?: string, bold?: boolean, italic?: boolean): {
41
+ ascent: number;
42
+ descent: number;
43
+ height: number;
44
+ };
45
+ /** 清除宽度缓存(字体变更时调用) */
46
+ export declare function clearWidthCache(): void;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * 中文数字工具 — 将整数转换为中文序数
3
+ *
4
+ * 用于题组标题的中文序号自动生成:
5
+ * 1 → "一"
6
+ * 2 → "二"
7
+ * ...
8
+ * 10 → "十"
9
+ * 11 → "十一"
10
+ * 21 → "二十一"
11
+ * ...
12
+ * 999 → "九百九十九"
13
+ *
14
+ * 用法:
15
+ * import { toChineseOrdinal, formatSectionTitle, extractBaseTitle } from './chineseNumber'
16
+ */
17
+ /**
18
+ * 将整数转换为中文序数(支持 1-999)
19
+ *
20
+ * 示例:
21
+ * 1 → "一"
22
+ * 10 → "十"
23
+ * 11 → "十一"
24
+ * 20 → "二十"
25
+ * 21 → "二十一"
26
+ * 100 → "一百"
27
+ * 101 → "一百零一"
28
+ * 110 → "一百一十"
29
+ * 111 → "一百一十一"
30
+ * 999 → "九百九十九"
31
+ */
32
+ export declare function toChineseOrdinal(n: number): string;
33
+ /**
34
+ * 将基础标题与中文序号格式化显示
35
+ *
36
+ * @param title 基础标题,如 "选择题"
37
+ * @param ordinal 序号(从 1 开始),如 1
38
+ * @returns 格式化后的完整标题,如 "一、选择题"
39
+ */
40
+ export declare function formatSectionTitle(title: string, ordinal: number): string;
41
+ /**
42
+ * 从已有的完整标签中提取基础标题(去除中文序号前缀)
43
+ *
44
+ * 示例:
45
+ * "一、选择题" → "选择题"
46
+ * "十、填空题" → "填空题"
47
+ * "二十一、计算题" → "计算题"
48
+ * "选择题" → "选择题"(无序号时返回原值)
49
+ */
50
+ export declare function extractBaseTitle(label: string): string;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * deserialization/parseDocument.ts
3
+ *
4
+ * 文档反序列化主流程:SwDocument → 内部模型
5
+ */
6
+ import type { ContentBlock, PageSettings } from '../../types/paper';
7
+ export interface DeserializeResult {
8
+ blocks: ContentBlock[];
9
+ settings: PageSettings;
10
+ paperName: string;
11
+ schoolName: string;
12
+ qrCodeContent: string;
13
+ qrCodeImage: string;
14
+ totalScore: number;
15
+ }
16
+ /**
17
+ * 从 JSON 字符串反序列化为编辑器内部状态
18
+ */
19
+ export declare function deserializeDocument(json: string): DeserializeResult;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * deserialization/parseInlines.ts
3
+ *
4
+ * 内联元素反序列化:InlineNode → InlineElement
5
+ */
6
+ import type { InlineElement } from '../../types/paper';
7
+ import type { InlineNode } from '../../types/document';
8
+ /**
9
+ * 反序列化单个内联节点
10
+ */
11
+ export declare function parseInlineNode(inline: InlineNode): InlineElement | null;
12
+ /** 重置反序列化 ID 计数器(测试用) */
13
+ export declare function resetDeserializeIdCounter(): void;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * deserialization/parseQuestions.ts
3
+ *
4
+ * 题目反序列化:QuestionNode → ContentBlock[]
5
+ */
6
+ import type { ContentBlock, Paragraph } from '../../types/paper';
7
+ import type { ParagraphNode, QuestionNode } from '../../types/document';
8
+ /**
9
+ * 将 QuestionNode 反序列化为 ContentBlock[]
10
+ */
11
+ export declare function questionNodeToBlocks(qNode: QuestionNode, colWidth?: number, fontSize?: number, lineHeight?: number): ContentBlock[];
12
+ /**
13
+ * 从 QuestionNode 中提取分数
14
+ */
15
+ export declare function extractScore(qNode: QuestionNode): string;
16
+ /**
17
+ * 反序列化段落节点
18
+ */
19
+ export declare function parseParagraphNode(pn: ParagraphNode, contentWidth?: number, _fontSize?: number, _lineHeight?: number): Paragraph;
20
+ /** 重置块 ID 计数器(测试用) */
21
+ export declare function resetBlockIdCounter(): void;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * documentSerializer.ts
3
+ *
4
+ * 在编辑器内部模型 (ContentBlock[] + PageSettings) 与
5
+ * 文档格式 (SwDocument) 之间进行双向转换。
6
+ *
7
+ * 三层架构:
8
+ * canvas-editor IElement[] ←→ ContentBlock[] ←→ SwDocument (JSON)
9
+ * (编辑层) (内部排版层) (持久化层)
10
+ *
11
+ * 本文件为入口编排器,具体逻辑拆分至:
12
+ * serialization/ — 序列化(内部模型 → SwDocument)
13
+ * deserialization/ — 反序列化(SwDocument → 内部模型)
14
+ */
15
+ import type { ContentBlock, PageSettings, Page } from '../types/paper';
16
+ export interface SerializeOptions {
17
+ paperName?: string;
18
+ schoolName?: string;
19
+ qrCodeContent?: string;
20
+ qrCodeImage?: string;
21
+ totalScore?: number;
22
+ pages?: Page[];
23
+ blocks?: ContentBlock[];
24
+ }
25
+ export type { DeserializeResult } from './deserialization/parseDocument';
26
+ /**
27
+ * 将编辑器内部状态序列化为 JSON 字符串
28
+ */
29
+ export declare function serializeDocument(blocks: ContentBlock[], settings: PageSettings, options?: SerializeOptions): string;
30
+ /**
31
+ * 从 JSON 字符串反序列化为编辑器内部状态
32
+ */
33
+ export declare function deserializeDocument(json: string): import("./documentSerializer").DeserializeResult;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * mathjax.ts — 公式渲染服务
3
+ *
4
+ * MathJax v3 通过 index.html 中的 <script> 标签加载(public/mathjax/tex-mml-svg.js),
5
+ * 使用 window.MathJax.tex2svg() 同步渲染 LaTeX 为 SVG,
6
+ * 使用 window.MathJax.mathml2svg() 同步渲染 MathML 为 SVG。
7
+ *
8
+ * 优化:
9
+ * - 内置 LRU 缓存,相同公式只渲染一次
10
+ * - 提供批量渲染 API,通过 requestAnimationFrame 分片避免阻塞主线程
11
+ *
12
+ * 用法:
13
+ * import { latexToSvg, latexToSvgBatch, mathmlToSvg } from '../utils/mathjax'
14
+ * const result = latexToSvg('\\frac{a}{b}')
15
+ * // result = { svg: '<svg ...>...</svg>', width: 55, height: 22 }
16
+ * const mmlResult = mathmlToSvg('<math>...</math>')
17
+ */
18
+ export interface LatexResult {
19
+ /** 完整的 <svg> 标签字符串 */
20
+ svg: string;
21
+ /** SVG 宽度(px) */
22
+ width: number;
23
+ /** SVG 高度(px) */
24
+ height: number;
25
+ }
26
+ /** 清空公式缓存(纸张设置变更时调用) */
27
+ export declare function clearFormulaCache(): void;
28
+ /**
29
+ * 将 LaTeX 字符串渲染为 SVG
30
+ *
31
+ * @param latex LaTeX 源码(不含 \( \) 包裹符)
32
+ * @param fontSize 基础字号,默认 14
33
+ * @param display 是否为行间公式,默认 false(行内公式)
34
+ */
35
+ export declare function latexToSvg(latex: string, fontSize?: number, display?: boolean): LatexResult;
36
+ /**
37
+ * 将 MathML 字符串渲染为 SVG
38
+ *
39
+ * @param mathml MathML 源码(可以是 URL 编码的)
40
+ * @param fontSize 基础字号,默认 14
41
+ * @param display 是否为行间公式,默认 false(行内公式)
42
+ */
43
+ export declare function mathmlToSvg(mathml: string, fontSize?: number, display?: boolean): LatexResult;
44
+ /**
45
+ * 批量渲染公式(避免阻塞主线程)
46
+ *
47
+ * 将公式列表分成小批次,每批次之间用 requestAnimationFrame 让出主线程。
48
+ * 返回与输入一一对应的结果数组。
49
+ *
50
+ * @param items 待渲染的公式列表
51
+ * @param batchSize 每批渲染数量,默认 20
52
+ */
53
+ export declare function latexToSvgBatch(items: Array<{
54
+ latex: string;
55
+ fontSize?: number;
56
+ display?: boolean;
57
+ }>, batchSize?: number): Promise<LatexResult[]>;
@@ -0,0 +1,21 @@
1
+ import type { PaperSize } from '../types/paper';
2
+ export interface PaperHtmlOptions {
3
+ /** 试卷名称 */
4
+ paperName?: string;
5
+ /** 每页的纯净 SVG 字符串 */
6
+ svgs: string[];
7
+ /** 纸张尺寸,默认 A4 */
8
+ paperSize?: PaperSize;
9
+ }
10
+ /**
11
+ * 构建自包含的 paper.html 字符串
12
+ *
13
+ * 与 PaperPreview 组件共享同一套渲染效果:
14
+ * - 纸张尺寸由 paperSize 决定,使用 PAPER_MM 映射为实际 mm 值
15
+ * - 白底、居中、阴影(对标 .pe-preview__page)
16
+ * - 打印时移除阴影并添加分页符
17
+ *
18
+ * @param options - 构建选项
19
+ * @returns 完整的 HTML 字符串
20
+ */
21
+ export declare function buildPaperHtml(options: PaperHtmlOptions): string;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 生成二维码 base64 图片
3
+ * @param text 二维码内容文本
4
+ * @param size 二维码尺寸(像素),默认 200
5
+ * @returns base64 data URL 字符串,失败返回空字符串
6
+ */
7
+ export declare function generateQRCodeBase64(text: string, size?: number): Promise<string>;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * questionHtmlParser.ts
3
+ *
4
+ * 将题库原始 HTML 格式的题目转换为编辑器内部模型 ContentBlock[]
5
+ *
6
+ * 所有图片(包括根级表格和独立图片)统一处理为 ImageInline 行内元素,
7
+ * 随段落文字流排版,不再使用绝对定位的 ImageAnchor/embeds 层。
8
+ *
9
+ * 支持的 HTML 元素:
10
+ * - <p> 段落 + text-align / font-family / font-size / color
11
+ * - <img class="Wirisformula" data-latex="..." data-mathml="..."> 数学公式(行内)
12
+ * 优先使用 data-latex,为空时回退到 data-mathml(使用 MathJax 标准转换)
13
+ * - <img> 图片 → 行内 ImageInline
14
+ * - <table> → ImageInline + rawHtml(待 html2canvas 渲染)
15
+ * - <span class="blank_tiankong"> 填空题空 → UnderlineInline
16
+ * - <span class="sw-editor-blank-xuanze"> 选择题空 → UnderlineInline
17
+ * - <span class="sw-editor-judge-question-right/wrong"> 判断题空 → UnderlineInline
18
+ * - <span class="under-point"> 加点字 → TextInline + dotAbove
19
+ * - <span class="wavy-line"> 波浪线 → TextInline + wavyLine
20
+ * - 空段落(&nbsp;)→ EnterInline
21
+ */
22
+ import type { ContentBlock } from '../types/paper';
23
+ export interface ParseQuestionHtmlOptions {
24
+ /** 题号,如 "21" */
25
+ questionNo?: string;
26
+ /** 分数,如 "5" */
27
+ score?: string;
28
+ /** 可用内容宽度(列宽 - 缩进),单位 px */
29
+ contentWidth?: number;
30
+ /** 默认字号,默认 14 */
31
+ fontSize?: number;
32
+ /** 默认行距倍数,默认 1.5 */
33
+ lineSpacing?: number;
34
+ /** 跳过自动追加作答空段落(由调用方自行控制) */
35
+ skipAnswerArea?: boolean;
36
+ }
37
+ /**
38
+ * 将题库原始 HTML 转换为 ContentBlock[],可直接插入画板
39
+ */
40
+ export declare function parseQuestionHtml(html: string, options?: ParseQuestionHtmlOptions): ContentBlock[];
@@ -0,0 +1,61 @@
1
+ /**
2
+ * questionJsonParser.ts
3
+ *
4
+ * 将题库完整 JSON 数据转换为 ContentBlock[]
5
+ * 数据结构:
6
+ * - question 字段 → 题干 HTML → parseQuestionHtml
7
+ * - extra_json.options → 选择题选项 → 每个选项独立解析为 Paragraph[]
8
+ * - extra_json.quest_type → 题目类型
9
+ * - qid → 保留在 metadata 中,用于批改
10
+ */
11
+ import type { ContentBlock } from '../types/paper';
12
+ export interface QuestionJsonData {
13
+ id?: number;
14
+ qid: string;
15
+ question: string;
16
+ answer?: string;
17
+ explain?: string | null;
18
+ analysis?: string | null;
19
+ status?: number;
20
+ tenant_id?: number;
21
+ create_time?: string;
22
+ update_time?: string;
23
+ extra_json?: {
24
+ answer?: string;
25
+ options?: Array<{
26
+ label: string;
27
+ content: string;
28
+ }>;
29
+ quest_type?: string;
30
+ [key: string]: any;
31
+ };
32
+ tags?: Array<{
33
+ tag_code_list: string;
34
+ tag_name_list: string;
35
+ tag_category_code: string;
36
+ tag_category_name: string;
37
+ }>;
38
+ [key: string]: any;
39
+ }
40
+ export interface ParseQuestionJsonOptions {
41
+ /** 题号,如 "1" */
42
+ questionNo?: string;
43
+ /** 分数,如 "5" */
44
+ score?: string;
45
+ /** 可用内容宽度(列宽 - 缩进),单位 px */
46
+ contentWidth?: number;
47
+ /** 默认字号,默认 14 */
48
+ fontSize?: number;
49
+ /** 默认行距倍数,默认 1.5 */
50
+ lineSpacing?: number;
51
+ }
52
+ /**
53
+ * 将题库完整 JSON 转换为 ContentBlock[]
54
+ *
55
+ * 流程:
56
+ * 1. 解析题干 HTML → stem paragraphs + anchors
57
+ * 2. 判断题目类型(选择题 / 非选择题)
58
+ * 3. 选择题:解析每个选项 HTML → OptionItem[]
59
+ * 4. 保留 qid、answer 等元数据到 metadata
60
+ */
61
+ export declare function parseQuestionJson(data: QuestionJsonData, options?: ParseQuestionJsonOptions): ContentBlock[];
@@ -0,0 +1,11 @@
1
+ /**
2
+ * serialization/buildBody.ts
3
+ *
4
+ * 构建文档 body 部分(column children 组装)
5
+ */
6
+ import type { ContentBlock, PageSettings } from '../../types/paper';
7
+ import type { BodySection } from '../../types/document';
8
+ /**
9
+ * 构建 body 部分
10
+ */
11
+ export declare function buildBody(blocks: ContentBlock[], settings: PageSettings): BodySection;