@sw7d/paper-renderer 0.6.5 → 0.7.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.
@@ -165,6 +165,14 @@ declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {
165
165
  bodyTopEven: number;
166
166
  bodyTopOdd: number;
167
167
  showScore: boolean;
168
+ wrapRules?: {
169
+ headTail: boolean;
170
+ punctCompress: boolean;
171
+ punctHang: boolean;
172
+ noBreakWord: boolean;
173
+ justifyText: boolean;
174
+ widowControl: boolean;
175
+ } | undefined;
168
176
  };
169
177
  selectedBlockId: string | null;
170
178
  selectedBlock: ContentBlock | null;
@@ -6,6 +6,8 @@ type __VLS_Props = {
6
6
  renderMode: string;
7
7
  isImageOnlyLine?: boolean;
8
8
  imageId?: string;
9
+ /** 所在行行高(仅作答位使用) */
10
+ lineHeight?: number;
9
11
  };
10
12
  declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
11
13
  declare const _default: typeof __VLS_export;
@@ -4,6 +4,8 @@ type __VLS_Props = {
4
4
  x: number;
5
5
  baseline: number;
6
6
  renderMode: string;
7
+ /** 所在行行高:作答位高度不能超过它,否则会遮住上一行 */
8
+ lineHeight?: number;
7
9
  };
8
10
  declare const __VLS_export: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
9
11
  declare const _default: typeof __VLS_export;
@@ -4,7 +4,7 @@
4
4
  * usePaperCore 纯函数辅助模块(无 Vue 依赖)
5
5
  * 包含字号转换、段落构建、题块创建、文本解析等工具函数
6
6
  */
7
- import type { ContentBlock, Paragraph, InlineElement, PageSettings } from '../types/paper';
7
+ import type { ContentBlock, Paragraph, InlineElement, PageSettings, WrapRules } from '../types/paper';
8
8
  /** 独立图片段落左右内边距(px):图片不紧贴题框边框 */
9
9
  export declare const STANDALONE_IMAGE_INSET = 8;
10
10
  /**
@@ -30,7 +30,7 @@ export declare function getFontSize(s: PageSettings): number;
30
30
  export declare function getLineSpacing(s: PageSettings): number;
31
31
  export declare function calcLineHeight(fontSize: number, lineSpacing: number): number;
32
32
  export declare function createEmptyParagraph(fontSize: number, lineSpacing: number): Paragraph;
33
- export declare function createParagraph(elements: InlineElement[], fontSize: number, lineSpacing: number, maxWidth?: number, firstLineExtraReduction?: number): Paragraph;
33
+ export declare function createParagraph(elements: InlineElement[], fontSize: number, lineSpacing: number, maxWidth?: number, firstLineExtraReduction?: number, wrapRules?: WrapRules): Paragraph;
34
34
  /**
35
35
  * 从末尾向前跳过尾部空段落(答题区),遇到非空段落即停止
36
36
  */
@@ -66,7 +66,7 @@ export declare function parseInlineSegments(text: string): Array<{
66
66
  value: string;
67
67
  }>;
68
68
  export declare function parseQuestionLine(text: string, fontSize: number): InlineElement[];
69
- export declare function createQuestionBlock(no: string, score: string, textLines: string[], fontSize: number, lineSpacing: number, contentWidth?: number, protectContent?: boolean): ContentBlock;
70
- export declare function createSubQuestionBlocks(no: string, score: string, textLines: string[], fontSize: number, lineSpacing: number, contentWidth?: number, protectContent?: boolean): ContentBlock[];
69
+ export declare function createQuestionBlock(no: string, score: string, textLines: string[], fontSize: number, lineSpacing: number, contentWidth?: number, protectContent?: boolean, wrapRules?: WrapRules): ContentBlock;
70
+ export declare function createSubQuestionBlocks(no: string, score: string, textLines: string[], fontSize: number, lineSpacing: number, contentWidth?: number, protectContent?: boolean, wrapRules?: WrapRules): ContentBlock[];
71
71
  export declare function getBlockPlainText(block: ContentBlock): string;
72
72
  export declare function getBlockLabel(block: ContentBlock): string;
@@ -69,7 +69,8 @@ export declare function usePaperBlockOps(deps: UsePaperBlockOpsDeps): {
69
69
  alignment: "left" | "center" | "right";
70
70
  } | null;
71
71
  updateImageAlignment: (id: string, alignment: "left" | "center" | "right") => void;
72
- updateUnderlineWidth: (id: string, newWidth: number) => void;
72
+ updateUnderlineWidth: (id: string, newWidth: number, snapshot?: boolean) => void;
73
+ getUnderlineTotalWidth: (id: string) => number;
73
74
  updateUnderlineAnswer: (id: string, answer: string) => void;
74
75
  updateUnderlineShow: (id: string, show: boolean) => void;
75
76
  removeUnderline: (id: string) => void;
@@ -80,6 +80,14 @@ export declare function usePaperCore(options?: UsePaperCoreOptions): {
80
80
  bodyTopEven: number;
81
81
  bodyTopOdd: number;
82
82
  showScore: boolean;
83
+ wrapRules?: {
84
+ headTail: boolean;
85
+ punctCompress: boolean;
86
+ punctHang: boolean;
87
+ noBreakWord: boolean;
88
+ justifyText: boolean;
89
+ widowControl: boolean;
90
+ } | undefined;
83
91
  }, PageSettings | {
84
92
  paperSize: PaperSize;
85
93
  columns: ColumnMode;
@@ -93,6 +101,14 @@ export declare function usePaperCore(options?: UsePaperCoreOptions): {
93
101
  bodyTopEven: number;
94
102
  bodyTopOdd: number;
95
103
  showScore: boolean;
104
+ wrapRules?: {
105
+ headTail: boolean;
106
+ punctCompress: boolean;
107
+ punctHang: boolean;
108
+ noBreakWord: boolean;
109
+ justifyText: boolean;
110
+ widowControl: boolean;
111
+ } | undefined;
96
112
  }>;
97
113
  selectedBlockId: import("vue").Ref<string | null, string | null>;
98
114
  selectedUnderlineId: import("vue").Ref<string | null, string | null>;
@@ -162,7 +178,8 @@ export declare function usePaperCore(options?: UsePaperCoreOptions): {
162
178
  floating: import("..").FloatingAnswer;
163
179
  } | null;
164
180
  toggleAnswerArea: (blockId: string, paraIndices: number[]) => void;
165
- updateUnderlineWidth: (id: string, newWidth: number) => void;
181
+ updateUnderlineWidth: (id: string, newWidth: number, snapshot?: boolean) => void;
182
+ getUnderlineTotalWidth: (id: string) => number;
166
183
  updateUnderlineAnswer: (id: string, answer: string) => void;
167
184
  updateUnderlineShow: (id: string, show: boolean) => void;
168
185
  removeUnderline: (id: string) => void;
@@ -9,8 +9,8 @@
9
9
  * // 或导入底层模块
10
10
  * import { usePaperCore, PaperLayout } from 'paper-renderer'
11
11
  */
12
- export type { PaperSize, ColumnMode, PaperDimensions, PageSettings, PaperPreset, InlineType, BaseInline, TextInline, FormulaInline, UnderlineInline, ImageInline, EnterInline, ObjectFillInline, InlineElement, Line, Paragraph, OptionItem, QuestionType, BlockType, ContentBlock, FloatingAnswer, PageBlock, CornerMark, BBox, RenderedRegion, Page, PaperDocument, SelectionState, EditorState, RenderMode } from './types/paper';
13
- export { PAPER_DIMENSIONS, PAPER_MM, getPaperPreset, toBBox } from './types/paper';
12
+ export type { PaperSize, ColumnMode, PaperDimensions, PageSettings, PaperPreset, InlineType, BaseInline, TextInline, FormulaInline, UnderlineInline, ImageInline, EnterInline, ObjectFillInline, InlineElement, Line, Paragraph, OptionItem, QuestionType, BlockType, ContentBlock, FloatingAnswer, PageBlock, CornerMark, BBox, RenderedRegion, Page, PaperDocument, SelectionState, EditorState, RenderMode, WrapRules } from './types/paper';
13
+ export { PAPER_DIMENSIONS, PAPER_MM, DEFAULT_WRAP_RULES, getPaperPreset, toBBox } from './types/paper';
14
14
  export type { SwDocument, DocInfo, HeaderSection, BodySection, ColumnNode, VuePlugNode, QuestionNode, QuestionProps, QuestionStemNode, CorrectionBoxNode, OptionNode, ParagraphNode, InlineNode, TextNode, LatexNode, UnderlineNode, ImageNode, AnchorNode, Theme, Styles } from './types/document';
15
15
  export { PaperLayout } from './engine/PaperLayout';
16
16
  export { AutoLayout } from './engine/AutoLayout';
@@ -3,6 +3,7 @@
3
3
  * 对齐 parsed-data.json 的结构
4
4
  * 这是编辑器的 "保存格式" 模型
5
5
  */
6
+ import type { WrapRules } from './paper';
6
7
  export interface SwDocument {
7
8
  info: DocInfo;
8
9
  header: HeaderSection;
@@ -59,6 +60,8 @@ export interface PaperConfig {
59
60
  bodyTopEven?: number;
60
61
  /** 其他页body Y偏移 */
61
62
  bodyTopOdd?: number;
63
+ /** 折行/中文排版规则(旧文档缺省时按默认值补齐) */
64
+ wrapRules?: WrapRules;
62
65
  }
63
66
  export interface MarginConfig {
64
67
  top: number;
@@ -32,7 +32,29 @@ export interface PageSettings {
32
32
  bodyTopOdd: number;
33
33
  /** 是否显示分数 */
34
34
  showScore: boolean;
35
+ /** 折行/排版规则(缺省时按 DEFAULT_WRAP_RULES 处理,旧文档反序列化后自动补齐) */
36
+ wrapRules?: WrapRules;
35
37
  }
38
+ /**
39
+ * 折行与中文排版规则开关。
40
+ * 全部关闭时退化为「逐字符硬折行」(最接近旧行为,但仍保证不溢出)。
41
+ */
42
+ export interface WrapRules {
43
+ /** 避头尾(行首禁则 + 行尾禁则) */
44
+ headTail: boolean;
45
+ /** 标点挤压:行末标点压缩,为行首标点挤出空间 */
46
+ punctCompress: boolean;
47
+ /** 标点悬挂:允许行末标点超出右边界 ≤ 0.5 字宽 */
48
+ punctHang: boolean;
49
+ /** 西文/数字不在词中断行(数字串、英文单词、选项标签视为整体) */
50
+ noBreakWord: boolean;
51
+ /** 两端对齐:非末行按剩余空间均匀拉开字距 */
52
+ justifyText: boolean;
53
+ /** 孤行控制:段末只剩 1 个字时把上一行末字带下来 */
54
+ widowControl: boolean;
55
+ }
56
+ /** 折行规则默认值:试卷正文按中文排版惯例,两端对齐默认开启 */
57
+ export declare const DEFAULT_WRAP_RULES: WrapRules;
36
58
  /**
37
59
  * 每款纸张的默认配置预设
38
60
  * 切换纸张时自动应用,但用户可手动覆盖
@@ -5,10 +5,11 @@
5
5
  */
6
6
  import type { ContentBlock, Paragraph } from '../../types/paper';
7
7
  import type { ParagraphNode, QuestionNode } from '../../types/document';
8
+ import type { WrapRules } from '../../types/paper';
8
9
  /**
9
10
  * 将 QuestionNode 反序列化为 ContentBlock[]
10
11
  */
11
- export declare function questionNodeToBlocks(qNode: QuestionNode, colWidth?: number, fontSize?: number, lineHeight?: number): ContentBlock[];
12
+ export declare function questionNodeToBlocks(qNode: QuestionNode, colWidth?: number, fontSize?: number, lineHeight?: number, wrapRules?: WrapRules): ContentBlock[];
12
13
  /**
13
14
  * 从 QuestionNode 中提取分数
14
15
  */
@@ -16,6 +17,6 @@ export declare function extractScore(qNode: QuestionNode): string;
16
17
  /**
17
18
  * 反序列化段落节点
18
19
  */
19
- export declare function parseParagraphNode(pn: ParagraphNode, contentWidth?: number, _fontSize?: number, _lineHeight?: number): Paragraph;
20
+ export declare function parseParagraphNode(pn: ParagraphNode, contentWidth?: number, _fontSize?: number, _lineHeight?: number, wrapRules?: WrapRules): Paragraph;
20
21
  /** 重置块 ID 计数器(测试用) */
21
22
  export declare function resetBlockIdCounter(): void;
@@ -19,7 +19,7 @@
19
19
  * - <span class="wavy-line"> 波浪线 → TextInline + wavyLine
20
20
  * - 空段落(&nbsp;)→ EnterInline
21
21
  */
22
- import type { ContentBlock } from '../types/paper';
22
+ import type { ContentBlock, WrapRules } from '../types/paper';
23
23
  export interface ParseQuestionHtmlOptions {
24
24
  /** 题号,如 "21" */
25
25
  questionNo?: string;
@@ -35,6 +35,8 @@ export interface ParseQuestionHtmlOptions {
35
35
  protectContent?: boolean;
36
36
  /** 行内模式:纯图片段落不撑满、不对齐,适用于选项等已有标签前缀的场景 */
37
37
  inlineOnly?: boolean;
38
+ /** 折行规则(缺省 DEFAULT_WRAP_RULES,与编辑器当前设置保持一致) */
39
+ wrapRules?: WrapRules;
38
40
  }
39
41
  /**
40
42
  * 将题库原始 HTML 转换为 ContentBlock[],可直接插入画板
@@ -9,6 +9,7 @@
9
9
  * - qid → 保留在 metadata 中,用于批改
10
10
  */
11
11
  import type { ContentBlock } from '../types/paper';
12
+ import type { WrapRules } from '../types/paper';
12
13
  export interface QuestionJsonData {
13
14
  id?: number;
14
15
  qid: string;
@@ -50,6 +51,8 @@ export interface ParseQuestionJsonOptions {
50
51
  fontSize?: number;
51
52
  /** 默认行距倍数,默认 1.5 */
52
53
  lineSpacing?: number;
54
+ /** 折行规则(缺省 DEFAULT_WRAP_RULES) */
55
+ wrapRules?: WrapRules;
53
56
  /** 是否启用内容保护,默认 true */
54
57
  protectContent?: boolean;
55
58
  }
@@ -8,8 +8,14 @@
8
8
  * - usePaperCore.ts (createParagraph → wrapElements)
9
9
  * - documentSerializer.ts (createParagraph → wrapElementsSimple)
10
10
  * - questionHtmlParser.ts (parseQuestionHtml → wrapElements)
11
+ *
12
+ * 折行规则(按中文排版惯例,可通过 WrapRules 关闭):
13
+ * 1. 原子化:数字串/英文单词/选项标签/成对标点(——、……)整体不可拆;
14
+ * 2. 行首禁则 + 行尾禁则(避头尾);
15
+ * 3. 避头尾处理优先级:标点挤压 → 标点悬挂 → 整字回退;
16
+ * 4. 两端对齐:非末行按剩余空间均匀拉开字距(限制最大字距,避免稀字)。
11
17
  */
12
- import type { InlineElement, Line } from '../types/paper';
18
+ import type { InlineElement, Line, WrapRules } from '../types/paper';
13
19
  /**
14
20
  * 从内联元素列表创建单行,计算 baseline 并处理公式/图片撑高行高
15
21
  * 图片和公式都处理上下双向溢出,确保行高能完整容纳所有元素
@@ -18,19 +24,15 @@ export declare function createLineFromElements(elements: InlineElement[], lineHe
18
24
  /**
19
25
  * 将内联元素按可用宽度换行分割为多行
20
26
  *
21
- * - 包含中文避头避尾规则
22
- * - 支持公式/图片行高撑大
23
- * - 支持首行缩减(如分数文本占位)
24
- * - 超出列宽的空白自动拆分为多段跨行(填空题折行)
25
- *
26
- * @param elements 内联元素列表
27
+ * @param elements 内联元素列表(可为单字符元素,也可为整段文本元素)
27
28
  * @param maxWidth 可用宽度(px)
28
29
  * @param lineHeight 行高(px)
29
- * @param firstLineExtraReduction 首行缩减宽度(可选)
30
+ * @param firstLineExtraReduction 首行缩减宽度(可选,段首缩进)
31
+ * @param rules 折行规则(缺省 DEFAULT_WRAP_RULES)
30
32
  */
31
- export declare function wrapElements(elements: InlineElement[], maxWidth: number, lineHeight: number, firstLineExtraReduction?: number): Line[];
33
+ export declare function wrapElements(elements: InlineElement[], maxWidth: number, lineHeight: number, firstLineExtraReduction?: number, rules?: WrapRules): Line[];
32
34
  /**
33
35
  * 仅做宽度换行(无首行缩减)
34
36
  * 供序列化等不需要首行缩减的场景使用。
35
37
  */
36
- export declare function wrapElementsSimple(elements: InlineElement[], maxWidth: number, lineHeight: number, firstLineExtraReduction?: number): Line[];
38
+ export declare function wrapElementsSimple(elements: InlineElement[], maxWidth: number, lineHeight: number, firstLineExtraReduction?: number, rules?: WrapRules): Line[];
@@ -0,0 +1,75 @@
1
+ /**
2
+ * wrapRules.ts — 折行规则(避头尾字符集 + 断行原子分类)
3
+ *
4
+ * 与 textWrap.ts 的分工:
5
+ * - 本文件只做「规则数据 + 纯函数」:字符集判定、把内联元素展开为断行原子(Atom);
6
+ * - textWrap.ts 负责按行宽放置原子、处理禁则回退/挤压/悬挂、两端对齐。
7
+ *
8
+ * 设计要点:
9
+ * 1. 行首/行尾禁则使用真实字符(全角 + 半角),不依赖 Unicode 转义,避免引号写错;
10
+ * 2. 原子(Atom)是折行的最小单位:数字串、英文单词、选项标签、成对标点(——、……)
11
+ * 各自为一个原子,永不在内部断行;
12
+ * 3. 原子化在这里完成,因此无论调用方传进来的是「单字符元素」(编辑器 reflow)
13
+ * 还是「整段文本元素」(HTML 导入 / 文档反序列化),折行结果都一致。
14
+ */
15
+ import type { InlineElement, TextInline, UnderlineInline, WrapRules } from '../types/paper';
16
+ export declare function isNoStartChar(ch: string | undefined): boolean;
17
+ export declare function isNoEndChar(ch: string | undefined): boolean;
18
+ export declare function isCompressibleChar(ch: string | undefined): boolean;
19
+ export type AtomKind = 'text' | 'underline' | 'formula' | 'image' | 'enter' | 'score' | 'question-no' | 'option-label' | 'object-fill';
20
+ /** 折行最小单位:要么整体放进一行,要么整体换行(必要时允许按字符硬切) */
21
+ export interface Atom {
22
+ /** 参与渲染的元素(text 原子为切片后的副本,可安全修改 width) */
23
+ elem: InlineElement;
24
+ kind: AtomKind;
25
+ width: number;
26
+ /** 首字符是否行首禁则字符 */
27
+ noStart: boolean;
28
+ /** 末字符是否行尾禁则字符 */
29
+ noEnd: boolean;
30
+ /** 单字符且为可压缩标点 */
31
+ compressible: boolean;
32
+ /** 强制换行标记(<br>) */
33
+ isLineBreak?: boolean;
34
+ }
35
+ /**
36
+ * 从整段 TextInline 中切出 [from, to) 字符区间,构造新的 TextInline。
37
+ * - 宽度与逐字 x 坐标优先复用原元素的 xPositions(真实字宽);
38
+ * - 无 xPositions 时用 calcCharPositions 重算。
39
+ */
40
+ export declare function sliceText(te: TextInline, from: number, to: number): TextInline;
41
+ export declare function createTextAtom(te: TextInline, from: number, to: number): Atom;
42
+ /**
43
+ * 把超宽的文本原子按可用宽度硬切为两段(用于整段文本元素 / 超长单词兜底)。
44
+ * 非文本原子或无需切分时返回 null。
45
+ */
46
+ export declare function splitTextAtomByWidth(atom: Atom, availWidth: number): [Atom, Atom] | null;
47
+ /**
48
+ * 扫描一段文本,切成「不可拆的 token 区间」:
49
+ * - 数字串(含小数、千分位)+ 百分号/度数 + 紧邻短单位:`2023`、`1.5`、`50%`、`3cm`
50
+ * - 英文单词(含缩写点、连字符、撇号):`Hello`、`U.S.`、`well-known`
51
+ * - 选项标签:`A.`、`B.`
52
+ * - 成对标点:`——`、`……`、`~~`
53
+ * - 其余按单字符
54
+ */
55
+ export declare function scanTokens(text: string): Array<{
56
+ from: number;
57
+ to: number;
58
+ }>;
59
+ /**
60
+ * 将内联元素展开为断行原子。
61
+ * 不修改入参数组,text 元素一律浅拷贝,调用方可安全复用原数据。
62
+ */
63
+ export declare function atomize(elements: InlineElement[], rules: WrapRules): Atom[];
64
+ /**
65
+ * 居中/居右段落不参与两端对齐:
66
+ * 整行被拉满会失去居中的意义,这类段落只做普通折行。
67
+ */
68
+ export declare function rulesWithoutJustify(rules?: WrapRules): WrapRules;
69
+ /** 取该段文本的主要字号(用于挤压/悬挂/对齐阈值) */
70
+ export declare function pickFontSize(elements: InlineElement[]): number;
71
+ /**
72
+ * 把下划线空白按行宽切成若干段(保留原 id 与 isSplitPart 语义)
73
+ * @param forceSplitPart 为 true 时所有段都标记 isSplitPart=true(用于「已填满前一行」后的续段)
74
+ */
75
+ export declare function splitUnderlineAtom(ue: UnderlineInline, availWidth: number, forceSplitPart?: boolean): Atom[];
package/dist/style.css CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/dist/style.css
3
3
  size: 63298808 bytes
4
- sha256: 61fea218ca835cd93d568e72370b8cb2b7430273b04f29ab9b666469145c58e9
4
+ sha256: 265e846b66dd044a389fa27169715785c21920b19d44a30b9057c53c34201d64
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sw7d/paper-renderer",
3
- "version": "0.6.5",
3
+ "version": "0.7.1",
4
4
  "description": "题卡设计器核心组件 - 纯编辑表面",
5
5
  "type": "module",
6
6
  "main": "./dist/paper-renderer.js",