@syzlm/ofdparser 0.0.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.
Files changed (213) hide show
  1. package/.babelrc +14 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +18 -0
  4. package/.prettierrc.json +9 -0
  5. package/dist/OFD.d.ts +8 -0
  6. package/dist/Pages.d.ts +7 -0
  7. package/dist/RootDocumnet.d.ts +1 -0
  8. package/dist/bundle.cjs.js +64123 -0
  9. package/dist/bundle.esm.js +64115 -0
  10. package/dist/bundle.iife.js +14743 -0
  11. package/dist/bundle.umd.js +64045 -0
  12. package/dist/constant/index.d.ts +7 -0
  13. package/dist/constant.d.ts +6 -0
  14. package/dist/core/ofd-core/Annotations/AnnotationsXml.d.ts +16 -0
  15. package/dist/core/ofd-core/Asn.1Utils.d.ts +47 -0
  16. package/dist/core/ofd-core/Documents/DocumentResXml.d.ts +15 -0
  17. package/dist/core/ofd-core/Documents/DocumnetXml.d.ts +25 -0
  18. package/dist/core/ofd-core/Documents/PublicResXml.d.ts +15 -0
  19. package/dist/core/ofd-core/OFDElement.d.ts +232 -0
  20. package/dist/core/ofd-core/Ofd/OFDXml.d.ts +19 -0
  21. package/dist/core/ofd-core/Pages/PageXml.d.ts +20 -0
  22. package/dist/core/ofd-core/Signatures/Signatures.d.ts +20 -0
  23. package/dist/core/ofd-core/TemplatePages/TemplatePages.d.ts +20 -0
  24. package/dist/core/ofd-core/constant.d.ts +29 -0
  25. package/dist/core/ofd-core/utils.d.ts +21 -0
  26. package/dist/dicom/dicom.d.ts +15 -0
  27. package/dist/dicom/index.d.ts +1 -0
  28. package/dist/errors/ErrorHandle.d.ts +6 -0
  29. package/dist/errors/ErrorMsg.d.ts +16 -0
  30. package/dist/errors/FetchErrors.d.ts +4 -0
  31. package/dist/errors/MimeError.d.ts +4 -0
  32. package/dist/errors/OFDErrors.d.ts +4 -0
  33. package/dist/errors/UnzipErrors.d.ts +4 -0
  34. package/dist/errors/XmlErrors.d.ts +4 -0
  35. package/dist/errors/error-msg.d.ts +9 -0
  36. package/dist/es.d.ts +2 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/lib/ParseFile.d.ts +7 -0
  39. package/dist/lib/UnitCoversion.d.ts +31 -0
  40. package/dist/lib/asn1/Asn.1.d.ts +34 -0
  41. package/dist/lib/asn1/Stream.d.ts +27 -0
  42. package/dist/lib/asn1/asn1.d.ts +4 -0
  43. package/dist/lib/asn1/base64.d.ts +4 -0
  44. package/dist/lib/asn1/hex.d.ts +5 -0
  45. package/dist/lib/asn1/index.d.ts +5 -0
  46. package/dist/lib/asn1/int10.d.ts +34 -0
  47. package/dist/lib/asn1/oids.d.ts +2 -0
  48. package/dist/lib/asn1/string-cut.d.ts +2 -0
  49. package/dist/lib/decode-html.d.ts +7 -0
  50. package/dist/lib/fetch.d.ts +17 -0
  51. package/dist/lib/is-element.d.ts +5 -0
  52. package/dist/lib/load-files.d.ts +3 -0
  53. package/dist/lib/load-opentype.d.ts +34 -0
  54. package/dist/lib/load-script.d.ts +1 -0
  55. package/dist/lib/mime.d.ts +8 -0
  56. package/dist/lib/read-buffer.d.ts +2 -0
  57. package/dist/lib/toFixeds.d.ts +7 -0
  58. package/dist/libs/Asn.1Utils.d.ts +47 -0
  59. package/dist/libs/asn1/Asn.1.d.ts +34 -0
  60. package/dist/libs/asn1/Stream.d.ts +27 -0
  61. package/dist/libs/asn1/asn1.d.ts +4 -0
  62. package/dist/libs/asn1/base64.d.ts +4 -0
  63. package/dist/libs/asn1/hex.d.ts +5 -0
  64. package/dist/libs/asn1/index.d.ts +5 -0
  65. package/dist/libs/asn1/int10.d.ts +34 -0
  66. package/dist/libs/asn1/oids.d.ts +2 -0
  67. package/dist/libs/asn1/string-cut.d.ts +2 -0
  68. package/dist/libs/decode-html.d.ts +7 -0
  69. package/dist/libs/fetch.d.ts +17 -0
  70. package/dist/libs/mime.d.ts +8 -0
  71. package/dist/libs/ofdArray.d.ts +10 -0
  72. package/dist/libs/ofdImageObject.d.ts +9 -0
  73. package/dist/libs/ofdLayer.d.ts +3 -0
  74. package/dist/libs/ofdName.d.ts +17 -0
  75. package/dist/libs/ofdPathObject.d.ts +11 -0
  76. package/dist/libs/ofdTextObject.d.ts +6 -0
  77. package/dist/libs/parseAnnotation.d.ts +5 -0
  78. package/dist/libs/parseColor.d.ts +5 -0
  79. package/dist/libs/parsePageArea.d.ts +17 -0
  80. package/dist/libs/parser-elements.d.ts +8 -0
  81. package/dist/libs/parserElements.d.ts +8 -0
  82. package/dist/libs/pipeExtensions.d.ts +4 -0
  83. package/dist/libs/read-buffer.d.ts +2 -0
  84. package/dist/main.d.ts +15 -0
  85. package/dist/ofd/ConverterDpi.d.ts +16 -0
  86. package/dist/ofd/index.d.ts +1 -0
  87. package/dist/ofd/ofd-utils.d.ts +137 -0
  88. package/dist/ofd/ofd.d.ts +35 -0
  89. package/dist/ofd/parse-ofd.d.ts +18 -0
  90. package/dist/ofd/render.d.ts +18 -0
  91. package/dist/ofd/svg-image.d.ts +12 -0
  92. package/dist/ofd/svg-path.d.ts +5 -0
  93. package/dist/ofd/svg-text.d.ts +8 -0
  94. package/dist/ofd.parse.d.ts +6 -0
  95. package/dist/ofd.xml.d.ts +1 -0
  96. package/dist/ofdParse.d.ts +6 -0
  97. package/dist/pipe/annotations.d.ts +13 -0
  98. package/dist/pipe/attachments.d.ts +13 -0
  99. package/dist/pipe/commonData.d.ts +18 -0
  100. package/dist/pipe/customTags.d.ts +14 -0
  101. package/dist/pipe/extensions.d.ts +14 -0
  102. package/dist/pipe/output.d.ts +7 -0
  103. package/dist/pipe/pages.d.ts +13 -0
  104. package/dist/pipe/permissions.d.ts +14 -0
  105. package/dist/pipe/pipeline.d.ts +63 -0
  106. package/dist/pipe/startPipe.d.ts +11 -0
  107. package/dist/xml/index.d.ts +2 -0
  108. package/dist/xml/render.d.ts +6 -0
  109. package/dist/xml/xml.d.ts +18 -0
  110. package/package.json +47 -0
  111. package/public/index.html +75 -0
  112. package/public/opentype.min.js +2 -0
  113. package/rollup.config.dev.mjs +47 -0
  114. package/rollup.config.mjs +70 -0
  115. package/src/constant/index.ts +13 -0
  116. package/src/core/ofd-core/Annotations/Annotation.km +206 -0
  117. package/src/core/ofd-core/Annotations/Annotations.km +59 -0
  118. package/src/core/ofd-core/Annotations/Annotations.md +13 -0
  119. package/src/core/ofd-core/Annotations/AnnotationsXml.ts +114 -0
  120. package/src/core/ofd-core/Asn.1Utils.ts +225 -0
  121. package/src/core/ofd-core/Documents/DocumentResXml.ts +55 -0
  122. package/src/core/ofd-core/Documents/Documnet.km +862 -0
  123. package/src/core/ofd-core/Documents/DocumnetXml.ts +118 -0
  124. package/src/core/ofd-core/Documents/Documnets.md +296 -0
  125. package/src/core/ofd-core/Documents/PublicResXml.ts +63 -0
  126. package/src/core/ofd-core/Documents//345/233/276/345/261/202/346/270/262/346/237/223/351/241/272/345/272/217.png +0 -0
  127. package/src/core/ofd-core/Documents//350/241/25022/347/272/277/346/235/241/350/277/236/346/216/245/346/240/267/345/274/217.png +0 -0
  128. package/src/core/ofd-core/Documents//350/241/25023/347/272/277/346/235/241/350/231/232/347/272/277/346/240/267/345/274/217.png +0 -0
  129. package/src/core/ofd-core/Documents//350/241/25023/347/273/223/345/220/210/347/202/271/351/225/277/345/272/246.png +0 -0
  130. package/src/core/ofd-core/Documents//350/241/25024/347/272/277/346/235/241/347/253/257/347/202/271/346/240/267/345/274/217.png +0 -0
  131. package/src/core/ofd-core/Documents//351/241/265/350/276/271/347/225/214/345/261/202/346/254/241/347/273/223/346/236/204.png +0 -0
  132. package/src/core/ofd-core/OFDElement.ts +724 -0
  133. package/src/core/ofd-core/Ofd/CT_DocInfo.km +197 -0
  134. package/src/core/ofd-core/Ofd/OFD.km +114 -0
  135. package/src/core/ofd-core/Ofd/OFD.md +99 -0
  136. package/src/core/ofd-core/Ofd/OFDXml.ts +69 -0
  137. package/src/core/ofd-core/Pages/PAGE.md +40 -0
  138. package/src/core/ofd-core/Pages/PageXml.ts +59 -0
  139. package/src/core/ofd-core/Signatures/Signature.km +308 -0
  140. package/src/core/ofd-core/Signatures/Signature.md +21 -0
  141. package/src/core/ofd-core/Signatures/Signatures.km +315 -0
  142. package/src/core/ofd-core/Signatures/Signatures.md +24 -0
  143. package/src/core/ofd-core/Signatures/Signatures.ts +247 -0
  144. package/src/core/ofd-core/TemplatePages/TemplatePages.ts +56 -0
  145. package/src/core/ofd-core/asn.md +60 -0
  146. package/src/core/ofd-core/constant.ts +35 -0
  147. package/src/core/ofd-core/index.d.ts +867 -0
  148. package/src/core/ofd-core/utils.ts +86 -0
  149. package/src/dicom/dicom.ts +42 -0
  150. package/src/dicom/index.ts +1 -0
  151. package/src/errors/ErrorHandle.ts +14 -0
  152. package/src/errors/FetchErrors.ts +8 -0
  153. package/src/errors/MimeError.ts +9 -0
  154. package/src/errors/OFDErrors.ts +11 -0
  155. package/src/errors/UnzipErrors.ts +9 -0
  156. package/src/errors/XmlErrors.ts +11 -0
  157. package/src/errors/error-msg.ts +17 -0
  158. package/src/es.ts +3 -0
  159. package/src/global.d.ts +7 -0
  160. package/src/index.ts +7 -0
  161. package/src/lib/ParseFile.ts +12 -0
  162. package/src/lib/README.md +69 -0
  163. package/src/lib/UnitCoversion.ts +55 -0
  164. package/src/lib/asn1/Asn.1.ts +284 -0
  165. package/src/lib/asn1/README.md +1 -0
  166. package/src/lib/asn1/Stream.ts +314 -0
  167. package/src/lib/asn1/asn1.ts +120 -0
  168. package/src/lib/asn1/base64.ts +97 -0
  169. package/src/lib/asn1/hex.ts +60 -0
  170. package/src/lib/asn1/index.ts +5 -0
  171. package/src/lib/asn1/int10.ts +103 -0
  172. package/src/lib/asn1/oids.ts +6164 -0
  173. package/src/lib/asn1/string-cut.ts +8 -0
  174. package/src/lib/decode-html.ts +37 -0
  175. package/src/lib/fetch.ts +70 -0
  176. package/src/lib/is-element.ts +13 -0
  177. package/src/lib/jbig/README.md +1 -0
  178. package/src/lib/jbig/arithmetic_decoder.js +184 -0
  179. package/src/lib/jbig/base_stream.js +117 -0
  180. package/src/lib/jbig/ccitt.js +1068 -0
  181. package/src/lib/jbig/ccitt_stream.js +60 -0
  182. package/src/lib/jbig/compatibility.js +108 -0
  183. package/src/lib/jbig/core_utils.js +591 -0
  184. package/src/lib/jbig/decode_stream.js +184 -0
  185. package/src/lib/jbig/is_node.js +28 -0
  186. package/src/lib/jbig/jbig2.js +2582 -0
  187. package/src/lib/jbig/jbig2_stream.js +78 -0
  188. package/src/lib/jbig/primitives.js +441 -0
  189. package/src/lib/jbig/stream.js +101 -0
  190. package/src/lib/jbig/util.js +1231 -0
  191. package/src/lib/load-files.ts +26 -0
  192. package/src/lib/load-opentype.ts +161 -0
  193. package/src/lib/load-script.ts +33 -0
  194. package/src/lib/mime.ts +97 -0
  195. package/src/lib/read-buffer.ts +19 -0
  196. package/src/lib/toFixeds.ts +56 -0
  197. package/src/ofd/ConverterDpi.ts +35 -0
  198. package/src/ofd/OFD.md +38 -0
  199. package/src/ofd/TODO.md +10 -0
  200. package/src/ofd/index.ts +1 -0
  201. package/src/ofd/ofd-utils.ts +627 -0
  202. package/src/ofd/ofd.demo.md +12 -0
  203. package/src/ofd/ofd.ts +200 -0
  204. package/src/ofd/ofdxml.km +162 -0
  205. package/src/ofd/parse-ofd.ts +103 -0
  206. package/src/ofd/render.ts +393 -0
  207. package/src/ofd/svg-image.ts +318 -0
  208. package/src/ofd/svg-path.ts +283 -0
  209. package/src/ofd/svg-text.ts +272 -0
  210. package/src/xml/index.ts +3 -0
  211. package/src/xml/render.ts +171 -0
  212. package/src/xml/xml.ts +136 -0
  213. package/tsconfig.json +21 -0
@@ -0,0 +1,724 @@
1
+ import VaildOFDError from 'src/errors/OFDErrors';
2
+ import { Element } from 'xml-js';
3
+ import { OFD_Q } from './constant';
4
+ import {
5
+ Page,
6
+ CT_Layer,
7
+ LayerType,
8
+ CT_PageArea,
9
+ Res,
10
+ LayerPageBlock,
11
+ Signatures
12
+ } from './index.d';
13
+
14
+ import { NameREG, recursion, recursionGet } from './utils';
15
+
16
+ /**
17
+ * @desc 解析ofd 工具类
18
+ */
19
+
20
+ /**
21
+ * Description placeholder
22
+ * @date 2022/7/22 - 13:29:35
23
+ *
24
+ * @class OFDElement
25
+ * @typedef {OFDElement}
26
+ */
27
+ export class OFDElement {
28
+ /**
29
+ * OFD文件xml合集
30
+ * @date 2022/7/22 - 14:30:53
31
+ *
32
+ * @type {Element}
33
+ */
34
+ static OFDElements: { [key: string]: Element };
35
+
36
+ /**
37
+ * OFDxml转换成json
38
+ * @date 7/25/2022 - 7:42:51 PM
39
+ *
40
+ * @type {(null | { [key: string]: any })}
41
+ */
42
+ static OFDElemetJSON: null | { [key: string]: any };
43
+
44
+ /**
45
+ * OFD文档区域坐标
46
+ * @date 2022/8/1 - 19:01:44
47
+ *
48
+ * @static
49
+ * @type {(null | CT_PageArea)}
50
+ */
51
+ static PageArea: null | CT_PageArea;
52
+
53
+ /**
54
+ * ofd page数组
55
+ * @date 2022/7/26 - 17:17:13
56
+ *
57
+ * @type {(null | Page)}
58
+ */
59
+ static Pages: null | Page[];
60
+
61
+ /**
62
+ * ofd Tpls 对象
63
+ * @date 2022/7/26 - 17:17:13
64
+ *
65
+ * @type {(null | Tpls)}
66
+ */
67
+ static Tpls: null | { [k: string]: Page[] };
68
+
69
+ /**
70
+ * DocumnetRes.xml、PublicRes.xml 解析合并生成数组
71
+ * @date 2022/8/1 - 13:24:15
72
+ *
73
+ * @static
74
+ * @type {(null|Res[])}
75
+ */
76
+ static Res: null | Res[];
77
+
78
+ /**
79
+ * 签章信息
80
+ * @date 2022/8/19 - 10:27:27
81
+ *
82
+ * @static
83
+ * @type {(null | Signatures[])}
84
+ */
85
+ static Signatures: null | Signatures[];
86
+
87
+ static PageSignatures: null | { [key: string]: Signatures[] };
88
+
89
+ /**
90
+ * documnetRes资源文件路径
91
+ * @date 2022/8/1 - 15:18:10
92
+ *
93
+ * @static
94
+ * @type {string}
95
+ */
96
+ static DocumnetResRoot: string;
97
+
98
+ /**
99
+ * PublicRes资源文件路径
100
+ * @date 2022/8/1 - 15:18:10
101
+ *
102
+ * @static
103
+ * @type {string}
104
+ */
105
+ static PublicResRoot: string;
106
+
107
+ /**
108
+ * AnnotationsRoot 文件路径
109
+ * @date 2023/8/7 - 14:23:52
110
+ *
111
+ * @static
112
+ * @type {string}
113
+ */
114
+ static AnnotationsRoot: string;
115
+
116
+ /**
117
+ * OFDxml转换成json 平级
118
+ * @date 7/25/2022 - 7:42:51 PM
119
+ *
120
+ * @type {(null | { [key: string]: any })}
121
+ */
122
+ static OFDElemetFlatJSON: null | { [key: string]: any };
123
+
124
+ /**
125
+ * 根目录地址 以"/"结尾
126
+ * @date 2022/7/27 - 16:00:31
127
+ *
128
+ * @static
129
+ * @type {string}
130
+ */
131
+ static STLoc: string;
132
+
133
+ constructor(ofdXML?: { [key: string]: Element }) {
134
+ if (ofdXML) {
135
+ OFDElement.OFDElements = ofdXML;
136
+ }
137
+
138
+ this.XmlChangeJson = this.XmlChangeJson.bind(this);
139
+ }
140
+
141
+ /**
142
+ * @desc 初始化
143
+ */
144
+ static init() {
145
+ // @ts-ignore
146
+ this.OFDElements = undefined;
147
+ this.STLoc = '';
148
+ this.OFDElemetFlatJSON = null;
149
+ this.OFDElemetJSON = null;
150
+ this.Res = null;
151
+ this.Tpls = null;
152
+ this.Pages = null;
153
+ this.PageArea = null;
154
+ this.Signatures = null;
155
+ this.PageSignatures = null;
156
+ this.DocumnetResRoot = '';
157
+ this.PublicResRoot = '';
158
+ this.AnnotationsRoot = '';
159
+ }
160
+
161
+ /**
162
+ * @desc 设置文档根目录名称
163
+ */
164
+ setST_Loc(str: string) {
165
+ const ST = str.substring(0, str.indexOf('/'));
166
+ OFDElement.STLoc = ST + '/';
167
+ }
168
+
169
+ /**
170
+ *
171
+ * @param name 节点名称
172
+ * @returns ofd:xxx
173
+ */
174
+ OFDCommonQName(name: string) {
175
+ return `${OFD_Q}${name}`;
176
+ }
177
+
178
+ /**
179
+ * 获取指定文件xml
180
+ * @param name
181
+ * @param fileName
182
+ * @returns
183
+ */
184
+ getElements(name: string, fileName: string): string {
185
+ if (OFDElement.OFDElemetFlatJSON?.[fileName]) {
186
+ // 遍历当前xml获取数据
187
+ const xmlJSON = OFDElement.OFDElemetFlatJSON[fileName];
188
+ if (xmlJSON) {
189
+ return xmlJSON[this.OFDCommonQName(name)];
190
+ }
191
+ }
192
+ return '';
193
+ }
194
+
195
+ /**
196
+ * 格式化xml文件
197
+ * @param ofdXml
198
+ */
199
+ XmlChangeJson(xmlKey: string) {
200
+ const ofdXml = OFDElement.OFDElements[xmlKey];
201
+ if (ofdXml && ofdXml.elements && ofdXml.elements.length) {
202
+ const result: { [key: string]: any } = {};
203
+ const flatResult: { [key: string]: any } = {};
204
+ const elements = ofdXml.elements;
205
+ recursion(elements, result, flatResult);
206
+ if (!OFDElement.OFDElemetJSON) {
207
+ OFDElement.OFDElemetJSON = {};
208
+ }
209
+ OFDElement.OFDElemetJSON[xmlKey] = result;
210
+ if (!OFDElement.OFDElemetFlatJSON) {
211
+ OFDElement.OFDElemetFlatJSON = {};
212
+ }
213
+ OFDElement.OFDElemetFlatJSON[xmlKey] = flatResult;
214
+ }
215
+ }
216
+
217
+ /**
218
+ * 获取OFD的元素
219
+ * <p>
220
+ * 若无法在OFD命名空间下获取同名元素,则尝试从默认命名空间获取。
221
+ *
222
+ * @param name OFD元素名称
223
+ * @return OFD元素或null
224
+ */
225
+ getOFDElements(fileName: string, name: string) {
226
+ const currentXml = OFDElement.OFDElements[fileName];
227
+ if (currentXml?.elements) {
228
+ return recursionGet(currentXml.elements, this.OFDCommonQName(name));
229
+ }
230
+
231
+ return null;
232
+ }
233
+
234
+ /**
235
+ * 获取OFD的元素
236
+ * <p>
237
+ * 若无法在OFD命名空间下获取同名元素,则尝试从默认命名空间获取。
238
+ * 无法获取同名数据
239
+ * @param name OFD元素名称
240
+ * @return OFD元素或null
241
+ */
242
+ getJSONElement(elements: Element[]) {
243
+ if (elements) {
244
+ const result = {};
245
+ const flatResult: { [k: string]: string } = {};
246
+ recursion(elements, result, flatResult);
247
+
248
+ Object.entries(flatResult).forEach(([keys, val]) => {
249
+ if (keys) {
250
+ flatResult[keys.replace(NameREG, '')] = val;
251
+ delete flatResult[keys];
252
+ }
253
+ });
254
+ return flatResult;
255
+ }
256
+
257
+ return null;
258
+ }
259
+
260
+ /**
261
+ * 获取OFD元素中的文本
262
+ *
263
+ * @param elements 元素节点
264
+ * @return 文本
265
+ */
266
+ getOFDElementText(
267
+ elements: Element[] | null,
268
+ fileName?: string
269
+ // name?: string
270
+ ) {
271
+ if (!elements && !fileName) {
272
+ throw new VaildOFDError(9999, 'fileName is required');
273
+ }
274
+ let result: { [k: string]: unknown } = {};
275
+ const diffElements =
276
+ elements || OFDElement.OFDElements[fileName || ''].elements;
277
+ if (diffElements?.length) {
278
+ diffElements.forEach((item: Element) => {
279
+ if (item) {
280
+ const { attributes, elements, name } = item;
281
+ if (name) {
282
+ const simpleName = name.replace(NameREG, '');
283
+ if (attributes && attributes.Value) {
284
+ result[simpleName] = attributes.Value;
285
+ }
286
+ Object.assign(result, attributes);
287
+ if (elements && elements[0].type === 'text') {
288
+ result[simpleName] = elements[0].text;
289
+ }
290
+ }
291
+ }
292
+ });
293
+ }
294
+ return result;
295
+ }
296
+
297
+ getPageAction() {}
298
+
299
+ getPageRes() {}
300
+
301
+ /**
302
+ * @description 递归获取剪裁区域信息
303
+ */
304
+ getClipsObject(
305
+ elements: Element[],
306
+ cInfo: { [k: string]: string | { [k: string]: string } }
307
+ ) {
308
+ if (elements?.length) {
309
+ elements.forEach(item => {
310
+ const { name, elements } = item;
311
+
312
+ if (name && elements?.length === 1) {
313
+ const simpleName = name.replace(NameREG, '');
314
+ // @ts-ignore
315
+ cInfo[simpleName] = { ...elements[0].attributes };
316
+ if (simpleName === 'AbbreviatedData') {
317
+ // @ts-ignore
318
+ cInfo['AbbreviatedData'] = elements[0].text;
319
+ return;
320
+ }
321
+ if (elements[0].elements?.length) {
322
+ if (elements[0].type === 'element') {
323
+ // @ts-ignore
324
+ Object.assign(
325
+ cInfo,
326
+ this.getClipsObject(elements[0].elements, cInfo)
327
+ );
328
+ }
329
+ }
330
+ }
331
+ });
332
+ }
333
+ return cInfo;
334
+ }
335
+
336
+ /**
337
+ * 获取TextObject、ImageObject、PathObject
338
+ */
339
+ getLayerObject(elements: Element[]) {
340
+ const result: { [k: string]: unknown } = {};
341
+ elements.forEach(item => {
342
+ if (item) {
343
+ const { name, attributes, elements } = item;
344
+ if (name) {
345
+ const simpleName = name.replace(NameREG, '');
346
+ if (attributes) {
347
+ result[simpleName] = { ...attributes };
348
+ }
349
+ // 裁剪区域 渲染
350
+ if (simpleName === 'Clips') {
351
+ /**
352
+ * Clips 图元对象的裁剪区域序列•采用对象空间坐标系,
353
+ * 当存在多个Clip对象时,最终裁剪区为所有Clip 区城的交集
354
+ * CT_Clip 裁剪区域
355
+ */
356
+ // 计算出交叉剪裁区域
357
+ if (elements?.length) {
358
+ if (result[simpleName]) {
359
+ // @ts-ignore
360
+ result[simpleName]['clips'] = [];
361
+ }
362
+
363
+ elements.forEach(cItem => {
364
+ const {
365
+ name: cName,
366
+ attributes: cAttributes,
367
+ elements: cElements
368
+ } = cItem;
369
+ if (cName) {
370
+ const cSimpleName = cName.replace(NameREG, '');
371
+ const cInfo = { ...cAttributes };
372
+ if (cSimpleName === 'Clip') {
373
+ Object.assign(
374
+ cInfo,
375
+ // @ts-ignore
376
+ this.getClipsObject(cElements as Element[], cInfo)
377
+ );
378
+ if (cElements?.length === 1) {
379
+ if (
380
+ cElements[0] &&
381
+ cElements[0].name &&
382
+ cElements[0].elements?.length
383
+ ) {
384
+ const ccSimpleName = cElements[0].name.replace(
385
+ NameREG,
386
+ ''
387
+ );
388
+ if (ccSimpleName === 'AbbreviatedData') {
389
+ cInfo['AbbreviatedData'] = cElements[0].elements[0]
390
+ .text as string;
391
+ }
392
+ }
393
+ }
394
+ }
395
+ if (result[simpleName]) {
396
+ // @ts-ignore
397
+ result[simpleName]['clips'].push({ ...cInfo });
398
+ }
399
+ }
400
+ });
401
+ }
402
+ return;
403
+ }
404
+ if (elements) {
405
+ // 深度递归 elements
406
+ if (elements.length === 1 && elements[0].type) {
407
+ const typeKeys = elements[0].type as string;
408
+ // xml 文件cdata渲染
409
+ if (
410
+ // @ts-ignore
411
+ elements[0][typeKeys] &&
412
+ // @ts-ignore
413
+ typeof elements[0][typeKeys] === 'string'
414
+ ) {
415
+ result[simpleName] = {
416
+ ...attributes,
417
+ // @ts-ignore
418
+ text: elements[0][typeKeys]
419
+ };
420
+ }
421
+ } else {
422
+ result[simpleName] = Object.assign(
423
+ {},
424
+ result[simpleName],
425
+ this.getOFDElementText(elements)
426
+ );
427
+ }
428
+ }
429
+ }
430
+ }
431
+ });
432
+ return result;
433
+ }
434
+
435
+ /**
436
+ * 获取layer节点信息
437
+ * @param elements
438
+ * @returns
439
+ */
440
+ getPageBlock(
441
+ elements: Element[]
442
+ ): LayerPageBlock[] & { [k: string]: unknown }[] {
443
+ let result: LayerPageBlock[] & { [k: string]: unknown }[] = [];
444
+ if (elements && elements.length) {
445
+ elements.forEach(item => {
446
+ if (item) {
447
+ const { name, elements, attributes } = item;
448
+ if (name) {
449
+ const simpleName = name.replace(NameREG, '');
450
+ if (
451
+ simpleName === 'PathObject' ||
452
+ simpleName === 'TextObject' ||
453
+ simpleName === 'ImageObject' ||
454
+ simpleName === 'CompositeObject'
455
+ ) {
456
+ const info: LayerPageBlock & { [k: string]: any } = {
457
+ Type: simpleName
458
+ };
459
+ if (simpleName === 'TextObject') {
460
+ Object.assign(info, {
461
+ Weight: '400',
462
+ Fill: 'true',
463
+ HScale: '1.0',
464
+ ReadDirection: '0',
465
+ CharDirection: '0',
466
+ Italic: 'fasle',
467
+ Stroke: 'false'
468
+ });
469
+ }
470
+ if (simpleName === 'PathObject') {
471
+ Object.assign(info, {
472
+ Fill: 'false',
473
+ Rule: 'NonZero',
474
+ Stroke: 'true'
475
+ });
476
+ }
477
+ Object.assign(info, attributes);
478
+ if (elements) {
479
+ Object.assign(info, this.getLayerObject(elements));
480
+ }
481
+ result.push(info);
482
+ }
483
+ // PageBlock 需要递归
484
+ if (simpleName === 'PageBlock' && elements) {
485
+ // @ts-ignore
486
+ result = result.concat(this.getPageBlock(elements));
487
+ }
488
+ }
489
+ }
490
+ });
491
+ }
492
+
493
+ return result;
494
+ }
495
+
496
+ /**
497
+ * 获取Content下Layer,多个Layer 合并进入数组
498
+ * @param elements
499
+ * @returns [{Type:'ImageObject'|'TextObject'|'PathObject',ID:'',Boundary:'', }]
500
+ */
501
+ getPageContent(elements: Element[]): CT_Layer[] {
502
+ const result: CT_Layer[] = [];
503
+ if (elements && elements.length) {
504
+ elements.forEach(item => {
505
+ const info: CT_Layer & { [k: string]: any } = {};
506
+ if (item) {
507
+ const { name, elements, attributes } = item;
508
+ if (name === this.OFDCommonQName('Layer') && elements) {
509
+ if (attributes) {
510
+ const {
511
+ ID,
512
+ Type = 'Body',
513
+ DrawParam
514
+ } = attributes as {
515
+ Type?: LayerType;
516
+ ID: string;
517
+ DrawParam?: string;
518
+ };
519
+ info.ID = ID;
520
+ info.Type = Type;
521
+ info.DrawParam = DrawParam;
522
+ }
523
+ info.PageBlock = this.getPageBlock(elements);
524
+ result.push(info);
525
+ }
526
+ }
527
+ });
528
+ }
529
+ return result;
530
+ }
531
+
532
+ /**
533
+ * 获取ofd:Page Elemnet数据
534
+ * @param pageElement ofd:Page节点
535
+ * @param type 类型 page|template type=template:不返回 Template
536
+ * @return
537
+ * ```{Area: CT_PageArea;Content: PageLayer[];Template?: Template;}```
538
+ */
539
+ getPageAttr(pageElement: Element, type = 'page') {
540
+ const result: Page & { [k: string]: any } = {
541
+ Area: {
542
+ PhysicalBox: ''
543
+ },
544
+ Content: null,
545
+ PageID: ''
546
+ };
547
+ if (pageElement?.elements) {
548
+ pageElement.elements.forEach(item => {
549
+ if (item) {
550
+ const { name = '', attributes, elements } = item;
551
+ // 忽略template属性 详见 ./Pages/PAGE.md
552
+ if (type === 'template' && name === this.OFDCommonQName('Template')) {
553
+ return false;
554
+ }
555
+ result[name] = {};
556
+ const info: { [k: string]: any } = {};
557
+ if (attributes) {
558
+ Object.assign(info, attributes);
559
+ }
560
+ result[name.replace(NameREG, '')] = { ...info };
561
+ // 获取 Area内属性
562
+ if (name === this.OFDCommonQName('Area') && elements) {
563
+ result.Area = { PhysicalBox: '', ...this.getJSONElement(elements) };
564
+ }
565
+ // 获取 Area内属性
566
+ if (name === this.OFDCommonQName('Content') && elements) {
567
+ result.Content = this.getPageContent(elements);
568
+ }
569
+ }
570
+ });
571
+ }
572
+ return result;
573
+ }
574
+
575
+ setTpls(tplElement: Element, tplId: string) {
576
+ if (tplElement && tplElement.elements?.length) {
577
+ tplElement.elements.forEach(item => {
578
+ if (item) {
579
+ if (!OFDElement.Tpls) {
580
+ OFDElement.Tpls = {};
581
+ }
582
+ OFDElement.Tpls[tplId] = [];
583
+ OFDElement.Tpls[tplId].push(this.getPageAttr(item));
584
+ }
585
+ });
586
+ }
587
+ }
588
+
589
+ /**
590
+ * @desc 设置Pages
591
+ * @param pageElements
592
+ */
593
+ setPages(pageElement: Element & { PageID: string }) {
594
+ if (pageElement && pageElement.elements?.length) {
595
+ pageElement.elements.forEach(item => {
596
+ if (item) {
597
+ if (!OFDElement.Pages) {
598
+ OFDElement.Pages = [];
599
+ }
600
+ OFDElement.Pages.push({
601
+ ...this.getPageAttr(item),
602
+ PageID: pageElement.PageID
603
+ });
604
+ }
605
+ });
606
+ }
607
+ }
608
+
609
+ /**
610
+ * 设置资源内容
611
+ * @param documnetResXml
612
+ */
613
+ setRes(resElement: Element[], rootPath: string) {
614
+ // console.log('resElement:', resElement);
615
+ if (resElement) {
616
+ //
617
+ resElement.forEach(item => {
618
+ if (item?.name) {
619
+ const { name, attributes, elements } = item;
620
+ // 资源文件限制死五种类型(国标时间2022-08-01)
621
+ const simpleName = name.replace(NameREG, '');
622
+ if (
623
+ simpleName !== 'MediaFile' &&
624
+ simpleName !== 'Font' &&
625
+ simpleName !== 'DrawParam' &&
626
+ simpleName !== 'CompositeGraphicUnit' &&
627
+ simpleName !== 'ColorSpace' &&
628
+ simpleName !== 'MultiMedia'
629
+ ) {
630
+ if (elements?.length) {
631
+ this.setRes(elements, rootPath);
632
+ }
633
+ } else {
634
+ if (attributes && attributes.ID) {
635
+ const result: Res = {
636
+ ...attributes,
637
+ OFDType: simpleName,
638
+ ID: String(attributes.ID)
639
+ };
640
+ if (elements?.length) {
641
+ // (MediaFile文件)图片 直接获取 ofd:MediaFile节点text
642
+ if (simpleName === 'MultiMedia') {
643
+ if (elements[0]?.name === this.OFDCommonQName('MediaFile')) {
644
+ const reg = new RegExp(rootPath as string);
645
+ const eleTextPath = elements[0].elements?.[0]?.text || '';
646
+ if (reg.test(eleTextPath as string)) {
647
+ result.Path = eleTextPath as string;
648
+ } else {
649
+ result.Path =
650
+ rootPath +
651
+ '/' +
652
+ String(elements[0].elements?.[0]?.text || '').replace(
653
+ /^Doc_0\/|^\/Doc_0\//,
654
+ ''
655
+ );
656
+ }
657
+ if (!/^\//.test(rootPath)) {
658
+ result.Path = result.Path.replace(/^\//, '');
659
+ }
660
+ }
661
+ }
662
+ // ColorSpace
663
+ if (simpleName === 'ColorSpace') {
664
+ if (elements[0].name === this.OFDCommonQName('Palette')) {
665
+ const PaletteElem = elements[0].elements || [];
666
+ // @ts-ignore
667
+ result.Palette = {
668
+ ...elements[0].attributes,
669
+ CV: PaletteElem[0].elements?.[0]?.text
670
+ };
671
+ }
672
+ }
673
+ // Font
674
+ if (simpleName === 'Font') {
675
+ // 字体文件,保留字体文件路径,使用opentype.js加载字体文件
676
+ if (elements[0].name === 'ofd:FontFile') {
677
+ result.Path =
678
+ rootPath +
679
+ '/' +
680
+ String(elements[0].elements?.[0]?.text || '').replace(
681
+ /^Doc_0\/|^\/Doc_0\//,
682
+ ''
683
+ );
684
+ }
685
+ }
686
+ // DrawParam
687
+ if (simpleName === 'DrawParam') {
688
+ // 设置 DrawParam默认值, 详见
689
+ Object.assign(
690
+ {
691
+ Join: 'Miter',
692
+ LineWidth: ' 0.353',
693
+ Cap: 'Butt',
694
+ Relative: null,
695
+ DashOffset: 0,
696
+ DashPattern: null,
697
+ MiterLimit: '3.528'
698
+ },
699
+ result
700
+ );
701
+ // 按节点解析DrawParam->Element
702
+ if (elements?.length) {
703
+ Object.assign(result, this.getLayerObject(elements));
704
+ }
705
+ }
706
+ result.Elements = elements.map(cItem => ({ ...cItem }));
707
+ }
708
+ if (!OFDElement.Res) {
709
+ OFDElement.Res = [];
710
+ }
711
+ OFDElement.Res.push(result);
712
+ } else {
713
+ // 资源未设置ID,不渲染资源
714
+ console.error(item, `解析资源文件失败,未找到资源ID`);
715
+ // throw new VaildOFDError(9999, '解析资源文件失败,未找到资源ID');
716
+ }
717
+ }
718
+ }
719
+ });
720
+ }
721
+ }
722
+ }
723
+
724
+ export default OFDElement;