@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
package/src/ofd/ofd.ts ADDED
@@ -0,0 +1,200 @@
1
+ import JSZip from 'jszip';
2
+ import OFDErrors from '../errors/OFDErrors';
3
+ import fetchs from '../lib/fetch';
4
+ import getMime from '../lib/mime';
5
+ import isElement from '../lib/is-element';
6
+ import parseOfd from './parse-ofd';
7
+ import { ResultData } from '../core/ofd-core';
8
+ import UnitCoversion from '../lib/UnitCoversion';
9
+ import LoadFontType from '../lib/load-opentype';
10
+ import ConverterDpi from './ConverterDpi';
11
+
12
+ // import render from './render';
13
+
14
+ interface Options {
15
+ file: string | File | ArrayBuffer;
16
+ ofd?: string | File | ArrayBuffer;
17
+ // dom节点
18
+ content?: Element;
19
+ // 节点ID
20
+ id?: string;
21
+ // 不建议使用
22
+ fail?: (err: Error) => undefined;
23
+ // 不建议使用
24
+ success?: (s: HTMLElement) => undefined;
25
+ // 显示宽度
26
+ screenWidth?: number;
27
+ // Ftech请求配置
28
+ requestOptions?: undefined | { [key: string]: any };
29
+ // 请求额外参数(用户自定义,签名或者token)
30
+ requestData?: undefined | { [key: string]: any };
31
+ // 暂时未使用
32
+ responseFilter?: (key: { [key: string]: string | number }) => string;
33
+ }
34
+ const zip = new JSZip();
35
+ const OfdDecompress = ({
36
+ file,
37
+ requestData,
38
+ requestOptions,
39
+ }: Options): Promise<{ code: number; data: any }> => {
40
+ return new Promise((resolve, reject) => {
41
+ //
42
+ if (typeof file === 'string') {
43
+ fetchs(file, { ...requestData }, { ...requestOptions }).then(res => {
44
+ // OFD 只能返回文件流
45
+ if (res && res instanceof ArrayBuffer) {
46
+ // OFD 需要解压
47
+ zip
48
+ .loadAsync(res)
49
+ .then(r => {
50
+ if (r && r.files) {
51
+ resolve({
52
+ code: 200,
53
+ data: r.files,
54
+ });
55
+ }
56
+ })
57
+ .catch(err => reject(new OFDErrors(500, err.message || 'OFD解析失败')));
58
+ } else {
59
+ reject(new OFDErrors(404));
60
+ }
61
+ });
62
+ }
63
+ if (file instanceof File) {
64
+ // 优先获取魔数判断文件类型
65
+ getMime(file).then(fileType => {
66
+ if (
67
+ fileType === 'application/ofd' ||
68
+ fileType === 'ofd' ||
69
+ fileType === 'application/dicom'
70
+ ) {
71
+ zip
72
+ .loadAsync(file)
73
+ .then(r => {
74
+ if (r && r.files) {
75
+ resolve({
76
+ code: 200,
77
+ data: r.files,
78
+ });
79
+ }
80
+ })
81
+ .catch(err => {
82
+ console.error('err:', err);
83
+ reject(new OFDErrors(500, err.message || 'OFD解析失败'));
84
+ });
85
+ }
86
+ });
87
+ }
88
+
89
+ if (file instanceof ArrayBuffer) {
90
+ // OFD 需要解压
91
+ zip
92
+ .loadAsync(file)
93
+ .then(r => {
94
+ if (r && r.files) {
95
+ resolve({
96
+ code: 200,
97
+ data: r.files,
98
+ });
99
+ }
100
+ })
101
+ .catch(err => reject(new OFDErrors(500, err.message || 'OFD解析失败')));
102
+ }
103
+ });
104
+ };
105
+
106
+ /**
107
+ * 直接输出渲染节点
108
+ * @param param0
109
+ * @returns
110
+ */
111
+ export const getSVGDoms: (r: Options) => Promise<HTMLElement> = ({
112
+ file,
113
+ ofd,
114
+ content,
115
+ id,
116
+ screenWidth,
117
+ ...restOptions
118
+ }: Options) =>
119
+ new Promise((resolve, reject) => {
120
+ LoadFontType.destroy();
121
+ // 兼容ofd老版本
122
+ if (!ofd && !file) {
123
+ reject(new OFDErrors(400, 'file 参数不可为空'));
124
+ return;
125
+ }
126
+ if (ofd) {
127
+ console.warn('注意:ofd参数已废弃。1.0.4之后版本,请使用file替换ofd参数!');
128
+ }
129
+ const ofdfile = ofd || file;
130
+ if (id && content) {
131
+ reject(new OFDErrors(500, 'id 和 content不能同时出现'));
132
+ return;
133
+ }
134
+ // 初始化缩放比例
135
+ ConverterDpi.setInitScale(1);
136
+ // 初始化缩放比例end
137
+ OfdDecompress({ file: ofdfile, ...restOptions })
138
+ .then(res => {
139
+ if (res && res.code === 200) {
140
+ let millWidth = screenWidth;
141
+ if (millWidth) {
142
+ millWidth = UnitCoversion.CoversionMill(millWidth);
143
+ }
144
+ parseOfd(res.data, zip, millWidth).then(pres => {
145
+ let container = content;
146
+ if (id) {
147
+ container = document.querySelector(`#${id}`) as HTMLElement;
148
+ }
149
+ if (container) {
150
+ if (isElement(container)) {
151
+ // 清空 container 防止xml重复渲染
152
+ container.innerHTML = '';
153
+ container.appendChild(pres as HTMLElement);
154
+ if (restOptions.success) {
155
+ restOptions.success(pres as HTMLElement);
156
+ }
157
+ resolve(pres as HTMLElement);
158
+ return;
159
+ }
160
+ }
161
+ if (restOptions.fail) {
162
+ restOptions.fail(new OFDErrors(403, 'content is not Element 或者 id 为空'));
163
+ }
164
+ reject(new OFDErrors(403, 'content is not Element 或者 id 为空'));
165
+ });
166
+ }
167
+ })
168
+ .catch(reject);
169
+ });
170
+
171
+ /**
172
+ * 输出格式化OFD JSON
173
+ * @param param0
174
+ * @returns
175
+ */
176
+ const OFDParse: (p: Options) => Promise<{ code: number; data: ResultData }> = ({
177
+ file,
178
+ ...restOptions
179
+ }: Omit<Options, 'fail' | 'success'>) =>
180
+ new Promise((resolve, reject) => {
181
+ if (!file) {
182
+ reject(new OFDErrors(400));
183
+ return;
184
+ }
185
+ OfdDecompress({ file, ...restOptions })
186
+ .then(res => {
187
+ if (res && res.code === 200) {
188
+ parseOfd(res.data, zip, undefined, true).then(res => {
189
+ if (res) {
190
+ resolve({ code: 200, data: res as ResultData });
191
+ } else {
192
+ reject(new OFDErrors(400));
193
+ }
194
+ });
195
+ }
196
+ })
197
+ .catch(reject);
198
+ });
199
+
200
+ export default OFDParse;
@@ -0,0 +1,162 @@
1
+ {
2
+ "root": {
3
+ "data": {
4
+ "id": "clarynd4f8g0",
5
+ "created": 1657334258123,
6
+ "text": "OFD.xml"
7
+ },
8
+ "children": [
9
+ {
10
+ "data": {
11
+ "id": "clarypirpqo0",
12
+ "created": 1657334262818,
13
+ "text": "attributes",
14
+ "layout_mind_offset": {
15
+ "x": -146,
16
+ "y": -57
17
+ }
18
+ },
19
+ "children": [
20
+ {
21
+ "data": {
22
+ "id": "clarz8ce7ow0",
23
+ "created": 1657334303792,
24
+ "text": "DocType"
25
+ },
26
+ "children": [
27
+ {
28
+ "data": {
29
+ "id": "clas5ff4li80",
30
+ "created": 1657334789379,
31
+ "text": "OFD"
32
+ },
33
+ "children": []
34
+ }
35
+ ]
36
+ },
37
+ {
38
+ "data": {
39
+ "id": "clarznsib8o0",
40
+ "created": 1657334337418,
41
+ "text": "Version"
42
+ },
43
+ "children": []
44
+ },
45
+ {
46
+ "data": {
47
+ "id": "clarzrv2ltc0",
48
+ "created": 1657334346280,
49
+ "text": "xmlns:ofd"
50
+ },
51
+ "children": []
52
+ }
53
+ ]
54
+ },
55
+ {
56
+ "data": {
57
+ "id": "clas09sd4a00",
58
+ "created": 1657334385298,
59
+ "text": "elements\n(1-∞)",
60
+ "layout_mind_offset": {
61
+ "x": 307,
62
+ "y": 69
63
+ },
64
+ "expandState": "expand"
65
+ },
66
+ "children": [
67
+ {
68
+ "data": {
69
+ "id": "clas1ufri280",
70
+ "created": 1657334508613,
71
+ "text": "ofd:DocBody",
72
+ "layout_right_offset": {
73
+ "x": 266,
74
+ "y": 5
75
+ }
76
+ },
77
+ "children": [
78
+ {
79
+ "data": {
80
+ "id": "clas2lx3wkg0",
81
+ "created": 1657334568435,
82
+ "text": "ofd:Signatures",
83
+ "layout_right_offset": {
84
+ "x": 239,
85
+ "y": -14
86
+ }
87
+ },
88
+ "children": [
89
+ {
90
+ "data": {
91
+ "id": "clas4vuxdpk0",
92
+ "created": 1657334746799,
93
+ "text": "Doc_N/Signs/Signatures.xml",
94
+ "layout_right_offset": {
95
+ "x": 359,
96
+ "y": 0
97
+ }
98
+ },
99
+ "children": []
100
+ }
101
+ ]
102
+ },
103
+ {
104
+ "data": {
105
+ "id": "clas2pu873s0",
106
+ "created": 1657334576968,
107
+ "text": "ofd:DocInfo",
108
+ "layout_right_offset": {
109
+ "x": 229,
110
+ "y": 11
111
+ }
112
+ },
113
+ "children": [
114
+ {
115
+ "data": {
116
+ "id": "clas48r2ht40",
117
+ "created": 1657334696500,
118
+ "text": "ofd:DocID",
119
+ "layout_right_offset": {
120
+ "x": 215,
121
+ "y": -3
122
+ }
123
+ },
124
+ "children": []
125
+ }
126
+ ]
127
+ },
128
+ {
129
+ "data": {
130
+ "id": "clas2u16rc00",
131
+ "created": 1657334586096,
132
+ "text": "ofd:DocRoot",
133
+ "layout_right_offset": {
134
+ "x": 228,
135
+ "y": 19
136
+ }
137
+ },
138
+ "children": [
139
+ {
140
+ "data": {
141
+ "id": "clas4dse6fk0",
142
+ "created": 1657334707465,
143
+ "text": "Doc_N/Document.xml",
144
+ "layout_right_offset": {
145
+ "x": 320,
146
+ "y": -2
147
+ }
148
+ },
149
+ "children": []
150
+ }
151
+ ]
152
+ }
153
+ ]
154
+ }
155
+ ]
156
+ }
157
+ ]
158
+ },
159
+ "template": "default",
160
+ "theme": "fresh-blue",
161
+ "version": "1.4.43"
162
+ }
@@ -0,0 +1,103 @@
1
+ import { xml2js } from 'xml-js';
2
+ import JSZip from 'jszip';
3
+ import OFDError from '../errors/OFDErrors';
4
+ // import OFDElement from '../core/ofd-core/OFDElement';
5
+ import OFDXml from '../core/ofd-core/Ofd/OFDXml';
6
+ import { OFDRender } from './render';
7
+ import { ResultData } from '../core/ofd-core';
8
+
9
+ interface OFDXML {
10
+ 'OFD.xml': any;
11
+ [key: string]: any;
12
+ }
13
+
14
+ const xmlOptions = { compact: false, spaces: 4 };
15
+
16
+ /**
17
+ * @description 解析ofd.xml
18
+ */
19
+ // const OFDXMLParse = () => {};
20
+
21
+ /**
22
+ * @description 解析ofd xml 文件
23
+ * @param data OFD解压出来的文件
24
+ * @param zip jszip 实例化对象
25
+ * @param isParse true:直接返回解析json,false 返回dom节点
26
+ * @param isSeal true:签章渲染,false:非签章渲染
27
+ */
28
+ const parse: (
29
+ data: OFDXML,
30
+ zip: JSZip,
31
+ defaultWidth?: number,
32
+ isParse?: boolean,
33
+ isSeal?: boolean,
34
+ ) => Promise<HTMLElement | ResultData> = (
35
+ data,
36
+ zip,
37
+ defaultWidth,
38
+ isParse = false,
39
+ isSeal = false,
40
+ ) => {
41
+ return new Promise((resolve, reject) => {
42
+ try {
43
+ if (!data) {
44
+ throw new OFDError(400, 'ofd xml不可为空');
45
+ }
46
+ const XMLMap: { [key: string]: any } = { ...data };
47
+ const zipPromise: Promise<string>[] = [];
48
+ let current = 0;
49
+ Object.entries(data).forEach(([key, val]) => {
50
+ if (!val.dir) {
51
+ XMLMap[key.replace(/^\//, '')] = val;
52
+ // 格式化每一个XML文件
53
+ if (/\.xml$|\.xbrl$/.test(key)) {
54
+ // @ts-ignore
55
+ zipPromise.push(zip.file(key).async('string'));
56
+ } else if (/\.dat$|\.esl$|\.jb2$|\.bmp$/.test(key)) {
57
+ // 签章文件转 binarystring
58
+ // @ts-ignore
59
+ zipPromise.push(zip.file(key).async('uint8array'));
60
+ } else if (/\.ttf$|\.svg$|\.wof$|\.eot$|\.tof$|\.otf$/i.test(key)) {
61
+ // 字体文件转 arraybuffer
62
+ // @ts-ignore
63
+ zipPromise.push(zip.file(key).async('uint8array'));
64
+ } else {
65
+ // 图片或资源文件转base64
66
+ // @ts-ignore
67
+ zipPromise.push(zip.file(key).async('base64'));
68
+ }
69
+ XMLMap[current] = key.replace(/^\//, '');
70
+ current += 1;
71
+ } else {
72
+ // 删除文件夹
73
+ delete XMLMap[key];
74
+ }
75
+ });
76
+ Promise.all(zipPromise).then(res => {
77
+ if (res && res.length) {
78
+ for (let i = 0; i < res.length; i++) {
79
+ XMLMap[XMLMap[i]] = res[i];
80
+ // xml 格式文件需要格式化
81
+ if (/\.xml$|\.xbrl$/i.test(XMLMap[i])) {
82
+ XMLMap[XMLMap[i]] = xml2js(res[i], xmlOptions);
83
+ }
84
+ delete XMLMap[i];
85
+ }
86
+ const OFDXML = new OFDXml(XMLMap);
87
+
88
+ const data = OFDXML.getData();
89
+ if (isParse) {
90
+ resolve(data);
91
+ } else {
92
+ resolve(OFDRender(data, defaultWidth, isSeal));
93
+ }
94
+ }
95
+ });
96
+ } catch (err) {
97
+ console.error(err);
98
+ reject(err);
99
+ }
100
+ });
101
+ };
102
+
103
+ export default parse;