xl-public-utils 1.0.3 → 1.0.5

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.
package/index.d.ts CHANGED
@@ -1,24 +1,4 @@
1
1
  import vtkRenderer from "@kitware/vtk.js/Rendering/Core/Renderer";
2
-
3
- /**
4
- * @typedef {Object} QrCodeOptions
5
- * @property {string} [bcid='qrcode'] - 二维码类型,具体支持请查看文档
6
- * @property {number} [width=1056] - 最终图片宽度
7
- * @property {number} [height=342] - 最终图片高度
8
- * @property {number} [qrcodeW=300] - 二维码宽度
9
- * @property {number} [qrcodeH=300] - 二维码高度
10
- * @property {boolean} [addText=true] - 是否在右侧添加文字
11
- * @property {number} [top=10] - 顶部距离
12
- * @property {number} [left=10] - 左侧距离
13
- * @property {number} [lineHeight=1] - 文字行高,换行后下一行文字的y位置计算为fontSize * lineHeight
14
- * @property {number} [fontSize=60] - 文字大小
15
- * @property {string} [fontFamily='黑体'] - 字体类型
16
- * @property {string} [bgColor='#fff'] - 背景颜色
17
- * @property {number} [textLeftMargin=20] - 文字距离二维码间隔
18
- * @property {boolean} [fontWeight=700] - 字体加粗
19
- * @property {string} [textColor='#000'] - 字体颜色
20
- * @property {string} [textContent] - 右侧文字内容
21
- */
22
2
  declare module "xl-public-utils" {
23
3
  export interface QrCodeOptions {
24
4
  /** 二维码类型,具体支持请查看文档 */
@@ -232,6 +212,21 @@ declare module "xl-public-utils" {
232
212
  end(): T;
233
213
  }
234
214
 
215
+ export function toSVG(opts: RenderOptions): string;
216
+ export function drawingSVG(): DrawingContext<string>;
217
+
218
+ // platform-specific exports
219
+ export function toCanvas(canvas: string | HTMLCanvasElement, opts: RenderOptions): HTMLCanvasElement;
220
+ export function drawingCanvas(canvas: string | HTMLCanvasElement): DrawingContext<HTMLCanvasElement>;
221
+
222
+ export function datamatrix(canvas: string | HTMLCanvasElement, opts: RenderOptions): HTMLCanvasElement;
223
+ export function datamatrix<T>(opts: RenderOptions, drawing: DrawingContext<Promise<T>>): Promise<T>;
224
+ export function datamatrix<T>(opts: RenderOptions, drawing: DrawingContext<T>): T;
225
+
226
+
227
+ export function qrcode(canvas: string | HTMLCanvasElement, opts: RenderOptions): HTMLCanvasElement;
228
+ export function qrcode<T>(opts: RenderOptions, drawing: DrawingContext<Promise<T>>): Promise<T>;
229
+ export function qrcode<T>(opts: RenderOptions, drawing: DrawingContext<T>): T;
235
230
  export function render<T>(params: RenderOptions, drawing: DrawingContext<T>): T;
236
231
  export function raw(
237
232
  bcid: string,
@@ -285,19 +280,4 @@ declare module "xl-public-utils" {
285
280
  export function loadFont(name: string, mult: number, data: string | Uint8Array): void;
286
281
  export function loadFont(name: string, multy: number, multx: number, data: string | Uint8Array): void;
287
282
  }
288
- export function toSVG(opts: RenderOptions): string;
289
- export function drawingSVG(): DrawingContext<string>;
290
-
291
- // platform-specific exports
292
- export function toCanvas(canvas: string | HTMLCanvasElement, opts: RenderOptions): HTMLCanvasElement;
293
- export function drawingCanvas(canvas: string | HTMLCanvasElement): DrawingContext<HTMLCanvasElement>;
294
-
295
- export function datamatrix(canvas: string | HTMLCanvasElement, opts: RenderOptions): HTMLCanvasElement;
296
- export function datamatrix<T>(opts: RenderOptions, drawing: DrawingContext<Promise<T>>): Promise<T>;
297
- export function datamatrix<T>(opts: RenderOptions, drawing: DrawingContext<T>): T;
298
-
299
-
300
- export function qrcode(canvas: string | HTMLCanvasElement, opts: RenderOptions): HTMLCanvasElement;
301
- export function qrcode<T>(opts: RenderOptions, drawing: DrawingContext<Promise<T>>): Promise<T>;
302
- export function qrcode<T>(opts: RenderOptions, drawing: DrawingContext<T>): T;
303
283
  }
package/index.js CHANGED
@@ -2,4 +2,5 @@ import * as vtkUtils from './src/vtkUtils.mjs';
2
2
  import * as utils from './src/utils.mjs'
3
3
  import * as qrcode from './src/qrcode.mjs'
4
4
  import * as BwipJs from './src/bwip-js.mjs';
5
- export { vtkUtils, utils, qrcode, BwipJs };
5
+ import * as drcUtils from './src/drcUtils.mjs';
6
+ export { vtkUtils, utils, qrcode, BwipJs, drcUtils };
package/package.json CHANGED
@@ -1,16 +1,21 @@
1
1
  {
2
2
  "name": "xl-public-utils",
3
- "version": "1.0.3",
4
- "description": "一些通用的方法",
3
+ "version": "1.0.5",
4
+ "description": "",
5
+ "type": "module",
5
6
  "main": "index.js",
6
7
  "module": "index.js",
7
8
  "types": "index.d.ts",
8
9
  "peerDependencies": {
9
- "@kitware/vtk.js": ">=25.7.2"
10
+ "@kitware/vtk.js": ">=25.7.2",
11
+ "gl-matrix": ">=3.4.3"
10
12
  },
11
13
  "peerDependenciesMeta": {
12
14
  "@kitware/vtk.js": {
13
15
  "optional": true
16
+ },
17
+ "gl-matrix": {
18
+ "optional": true
14
19
  }
15
20
  },
16
21
  "exports": {
@@ -21,11 +26,14 @@
21
26
  "./bwipJs": "./src/bwip-js.mjs"
22
27
  },
23
28
  "scripts": {
24
- "test": "echo \"Error: no test specified\" && exit 1"
29
+ "test": "jest"
25
30
  },
26
31
  "keywords": [
27
32
  "一些通用方法"
28
33
  ],
29
34
  "author": "xl",
30
- "license": "ISC"
35
+ "license": "ISC",
36
+ "devDependencies": {
37
+ "typescript": "^5.6.3"
38
+ }
31
39
  }
package/src/bwip-js.mjs CHANGED
@@ -234,7 +234,9 @@ function _Render(encoder, options, drawing) {
234
234
  }
235
235
  // We use mm rather than inches for height - except pharmacode2 height
236
236
  // which is already in mm.
237
- if (+bwippopts.height && encoder != bwipp_pharmacode2) {
237
+ // exludes bwipp_pharmacode2
238
+ // if (+bwippopts.height && encoder != bwipp_pharmacode2) {
239
+ if (+bwippopts.height) {
238
240
  bwippopts.height = bwippopts.height / 25.4 || 0.5;
239
241
  }
240
242
  // Likewise, width
@@ -0,0 +1,353 @@
1
+ import { vec3 } from "gl-matrix";
2
+ import { int8ArrayToBase64 } from './utils.mjs'
3
+ import vtkPolyData from "@kitware/vtk.js/Common/DataModel/PolyData";
4
+ /**
5
+ * @typedef {Object} AttrOption
6
+ * @property {number[]} [colors] 颜色数组
7
+ * @property {number[]} [generics] 属性数组
8
+ * @property {number[]} [normals] 法向量数组
9
+ */
10
+
11
+ /**
12
+ * 将数据进行压缩
13
+ * @param {number[]} vertices 网格点
14
+ * @param {number[]} faces 网格面,必须是三角化之后的面,每连续的三个点索引标识一个面
15
+ * @param {number} byteLength=14 byteLength 压缩率
16
+ * @param {AttrOption} [attr] 其他需要压缩的属性
17
+ * @returns {Int8Array} 压缩之后的DRC数据
18
+ */
19
+ export function enCodeMeshByVF(vertices, faces, byteLength = 14, attr =undefined) {
20
+ const facesFlat = faces;
21
+ const pointFlat = vertices;
22
+ const encoderModule = window.encoderModule
23
+ const encoder = new encoderModule.Encoder();
24
+ const meshBuilder = new encoderModule.MeshBuilder();
25
+ const dracoMesh = new encoderModule.Mesh();
26
+ const numberFaces = faces.length / 3;
27
+ const numberPoints = vertices.length / 3;
28
+ meshBuilder.AddFacesToMesh(dracoMesh, numberFaces, facesFlat);
29
+ meshBuilder.AddFloatAttributeToMesh(dracoMesh, encoderModule.POSITION, numberPoints, 3, pointFlat);
30
+ if (attr) {
31
+ if (attr.colors && attr.colors.length) {
32
+ meshBuilder.AddFloatAttributeToMesh(dracoMesh, encoderModule.COLOR, numberPoints, 3, attr.colors);
33
+ }
34
+ if (attr.normals && attr.normals.length) {
35
+ meshBuilder.AddFloatAttributeToMesh(dracoMesh, encoderModule.NORMAL, numberPoints, 3, attr.normals);
36
+ }
37
+ if (attr.generics && attr.generics.length) {
38
+ meshBuilder.AddFloatAttributeToMesh(dracoMesh, encoderModule.GENERIC, numberPoints, 3, attr.generics);
39
+ }
40
+ }
41
+
42
+ // encoder.SetSpeedOptions(5, 5);
43
+ encoder.SetAttributeQuantization(encoderModule.POSITION, byteLength);
44
+ encoder.SetEncodingMethod(encoderModule.MESH_EDGEBREAKER_ENCODING);
45
+ const encodedData = new encoderModule.DracoInt8Array();
46
+ const encodedLen = encoder.EncodeMeshToDracoBuffer(dracoMesh, encodedData);
47
+ const outputBuffer = new ArrayBuffer(encodedLen);
48
+ const outputData = new Int8Array(outputBuffer);
49
+ for (let i = 0; i < encodedLen; ++i) {
50
+ outputData[i] = encodedData.GetValue(i);
51
+ }
52
+ encoderModule.destroy(dracoMesh);
53
+ encoderModule.destroy(encoder);
54
+ encoderModule.destroy(meshBuilder);
55
+ return outputData
56
+ }
57
+
58
+ /**
59
+ * 使用drc压缩点
60
+ * @param {vec3[] | number[]} points 点数组
61
+ * @returns {Int8Array} 压缩之后的drc data
62
+ */
63
+ export function enCodePointCloud(points) {
64
+ const encoderModule = window.encoderModule
65
+ const encoder = new encoderModule.Encoder();
66
+ const pointCloudBuilder = new encoderModule.PointCloudBuilder();
67
+ const dracoPointCloud = new encoderModule.PointCloud();
68
+ const pointsFlat = [];
69
+ for(let i= 0; i <points.length; i++) {
70
+ if(typeof points[i] === 'object') {
71
+ pointsFlat.push(points[i][0], points[i][1], points[i][2]);
72
+ } else {
73
+ pointsFlat.push(points[i])
74
+ }
75
+ }
76
+ const numberPoints = pointsFlat.length / 3;
77
+ pointCloudBuilder.AddFloatAttribute(dracoPointCloud, encoderModule.POSITION, numberPoints, 3, pointsFlat);
78
+
79
+ encoder.SetAttributeQuantization(encoderModule.POSITION, 14);
80
+
81
+ encoder.SetEncodingMethod(encoderModule.MESH_SEQUENTIAL_ENCODING);
82
+ const encodedData = new encoderModule.DracoInt8Array();
83
+ const encodedLen = encoder.EncodePointCloudToDracoBuffer(dracoPointCloud, false, encodedData);
84
+ const outputBuffer = new ArrayBuffer(encodedLen);
85
+ const outputData = new Int8Array(outputBuffer);
86
+ for (let i = 0; i < encodedLen; ++i) {
87
+ outputData[i] = encodedData.GetValue(i);
88
+ }
89
+ encoderModule.destroy(dracoPointCloud);
90
+ encoderModule.destroy(encoder);
91
+ encoderModule.destroy(pointCloudBuilder);
92
+ return outputData
93
+ }
94
+ /**
95
+ * 解压drc格式的点云数据
96
+ * @param {ArrayBuffer} byteArray drc点数据
97
+ * @returns {Float32Array} 点数组
98
+ */
99
+ export function decodePointCloud(byteArray) {
100
+ const dracoPointCloud = decodeBuffer(byteArray);
101
+ return getPointCloudFromDracoGeometry(dracoPointCloud);
102
+ }
103
+
104
+ /**
105
+ * @typedef {Object} DrcMeshData
106
+ * @property {Uint32Array} faces 网格面数组
107
+ * @property {Float32Array} vertices 网格点数组
108
+ * @property {Float32Array} [colors] 颜色数组
109
+ * @property {Float32Array} [generics] 属性数组
110
+ * @property {Float32Array} [normals] 法向量数组
111
+ */
112
+
113
+ /**
114
+ * 解压drc格式的网格数据
115
+ * @param {ArrayBuffer} byteArray drc网格数据
116
+ * @returns {DrcMeshData} 网格数据
117
+ */
118
+ export function decodeDrcBufferArray(byteArray) {
119
+ var dracoGeometry = decodeBuffer(byteArray);
120
+ return getPolyDataFromDracoGeometry(dracoGeometry);
121
+ }
122
+ /**
123
+ * @typedef {Object} Decoder
124
+ */
125
+
126
+ /**
127
+ * 输入一个Drc数据,反会对应的解压器
128
+ * @param {ArrayBuffer} arrayBuffer 输入的数据
129
+ * @returns {Decoder} drc解压类
130
+ */
131
+ export function decodeBuffer(arrayBuffer) {
132
+ const decoderModule = window.decoderModule;
133
+ var byteArray = new Int8Array(arrayBuffer);
134
+ var decoder = new decoderModule.Decoder();
135
+ var decoderBuffer = new decoderModule.DecoderBuffer();
136
+ decoderBuffer.Init(byteArray, byteArray.length);
137
+ var geometryType = decoder.GetEncodedGeometryType(decoderBuffer);
138
+ var dracoGeometry;
139
+
140
+ if (geometryType === decoderModule.TRIANGULAR_MESH) {
141
+ dracoGeometry = new decoderModule.Mesh();
142
+ var status = decoder.DecodeBufferToMesh(decoderBuffer, dracoGeometry);
143
+
144
+ if (!status.ok()) {
145
+ console.error("Could not decode Draco file: ".concat(status.error_msg()));
146
+ }
147
+ } else if ( geometryType === decoderModule.POINT_CLOUD ) {
148
+ dracoGeometry = new decoderModule.PointCloud();
149
+ var status =
150
+ decoder.DecodeBufferToPointCloud(decoderBuffer, dracoGeometry);
151
+ if(!status.ok()) {
152
+ console.error('Wrong geometry type, expected mesh, got point cloud.');
153
+ }
154
+ }
155
+
156
+ decoderModule.destroy(decoderBuffer);
157
+ decoderModule.destroy(decoder);
158
+ return dracoGeometry;
159
+ }
160
+
161
+ /**
162
+ * 获取drcDecoder中的网格信息
163
+ * @param {Decoder} dracoGeometry DRC网格的decoder
164
+ * @returns {DrcMeshData} decoder中的网格信息
165
+ */
166
+ function getPolyDataFromDracoGeometry(dracoGeometry) {
167
+ const decoderModule = window.decoderModule;
168
+ var decoder = new decoderModule.Decoder(); // Get position attribute ID
169
+
170
+ var positionAttributeId = decoder.GetAttributeId(dracoGeometry, decoderModule.POSITION);
171
+
172
+ if (positionAttributeId === -1) {
173
+ console.error('No position attribute found in the decoded model.');
174
+ decoderModule.destroy(decoder);
175
+ decoderModule.destroy(dracoGeometry);
176
+ return null;
177
+ }
178
+ var positionArray = getDracoAttributeAsFloat32Array(dracoGeometry, positionAttributeId); // Read indices
179
+ var i = dracoGeometry.num_faces();
180
+ var indices = new Uint32Array(i * 4);
181
+ var indicesArray = new decoderModule.DracoInt32Array();
182
+ while (i--) {
183
+ decoder.GetFaceFromMesh(dracoGeometry, i, indicesArray);
184
+ var index = i * 4;
185
+ indices[index] = 3;
186
+ indices[index + 1] = indicesArray.GetValue(0);
187
+ indices[index + 2] = indicesArray.GetValue(1);
188
+ indices[index + 3] = indicesArray.GetValue(2);
189
+ } // Create polyData and add positions and indinces
190
+ var normalAttributeId = decoder.GetAttributeId(dracoGeometry, decoderModule.NORMAL);
191
+ var normals;
192
+ if (normalAttributeId !== -1) {
193
+ normals = getDracoAttributeAsFloat32Array(dracoGeometry, decoderModule.NORMAL);
194
+ } // Texture coordinates
195
+ var genericattributeId = decoder.GetAttributeId(dracoGeometry, decoderModule.GENERIC);
196
+ let generics;
197
+ if (genericattributeId !== -1) {
198
+ generics = getDracoAttributeAsFloat32Array(dracoGeometry, genericattributeId);
199
+ }
200
+
201
+ // var texCoordAttributeId = decoder.GetAttributeId(dracoGeometry, decoderModule.TEX_COORD);
202
+
203
+ // if (texCoordAttributeId !== -1) {
204
+ // var texCoordArray = getDracoAttributeAsFloat32Array(dracoGeometry, texCoordAttributeId);
205
+ // var texCoords = vtkDataArray.newInstance({
206
+ // numberOfComponents: 2,
207
+ // values: texCoordArray,
208
+ // name: 'TCoords'
209
+ // });
210
+ // } // Scalars
211
+
212
+
213
+ var colorAttributeId = decoder.GetAttributeId(dracoGeometry, decoderModule.COLOR);
214
+ let colors;
215
+ if (colorAttributeId !== -1) {
216
+ colors = getDracoAttributeAsFloat32Array(dracoGeometry, colorAttributeId);
217
+ }
218
+
219
+ decoderModule.destroy(decoder);
220
+ return {
221
+ faces: indices,
222
+ vertices: positionArray,
223
+ normals,
224
+ colors,
225
+ generics
226
+ };
227
+ }
228
+
229
+ /**
230
+ * 解压DRC点云数据
231
+ * @param {Decoder} dracoGeometry DRC点云的Decder
232
+ * @returns {Float32Array} 点
233
+ */
234
+ function getPointCloudFromDracoGeometry(dracoGeometry) {
235
+ const decoderModule = window.decoderModule;
236
+ var decoder = new decoderModule.Decoder(); // Get position attribute ID
237
+
238
+ var positionAttributeId = decoder.GetAttributeId(dracoGeometry, decoderModule.POSITION);
239
+
240
+ if (positionAttributeId === -1) {
241
+ console.error('No position attribute found in the decoded model.');
242
+ decoderModule.destroy(decoder);
243
+ decoderModule.destroy(dracoGeometry);
244
+ return new Float32Array();
245
+ } else {
246
+ const points = getDracoAttributeAsFloat32Array(dracoGeometry, positionAttributeId); // Read indices
247
+ decoderModule.destroy(decoder);
248
+ return points;
249
+ }
250
+ }
251
+ /**
252
+ * 获取decoder中的属性
253
+ * @param {Decoder} dracoGeometry DRC Decoder
254
+ * @param {number} attributeId 需要获取的属性id
255
+ * @returns {Float32Array} 对应decoder中的属性
256
+ */
257
+ export function getDracoAttributeAsFloat32Array(dracoGeometry, attributeId) {
258
+ const decoderModule = window.decoderModule;
259
+ var decoder = new decoderModule.Decoder();
260
+ var attribute = decoder.GetAttribute(dracoGeometry, attributeId);
261
+ var numberOfComponents = attribute.num_components();
262
+ var numberOfPoints = dracoGeometry.num_points();
263
+ var attributeData = new decoderModule.DracoFloat32Array();
264
+ decoder.GetAttributeFloatForAllPoints(dracoGeometry, attribute, attributeData);
265
+ var i = numberOfPoints * numberOfComponents;
266
+ var attributeArray = new Float32Array(i);
267
+ while (i--) {
268
+ attributeArray[i] = attributeData.GetValue(i);
269
+ }
270
+
271
+ return attributeArray;
272
+ }
273
+
274
+
275
+ /**
276
+ * 将点数据使用drc压缩处理为 base64 string
277
+ * @param {vec3[] | number[]} points 点数据
278
+ * @returns {string} drc压缩之后的base64数据
279
+ */
280
+
281
+ export function enCloudPointTobase64(points) {
282
+ return int8ArrayToBase64(enCodePointCloud(points));
283
+ }
284
+
285
+
286
+ /**
287
+ * @typedef {Object} Mesh
288
+ * @property {number[][]} vertices 点信息
289
+ * @property {number[][]} faces 面信息
290
+ */
291
+
292
+ /**
293
+ * 使用drc压缩网格信息
294
+ * @param {Mesh | vtkPolyData} mesh
295
+ * @param {number} byteLength=14 压缩率
296
+ * @param {AttrOption} [attr] 其他需要压缩的属性
297
+ * @returns {Int8Array} 压缩之后的DRC数据
298
+ */
299
+
300
+ export function enCodeMesh(mesh, byteLength = 14, attr = undefined) {
301
+ let faces = [];
302
+ let vertices = [];
303
+ if(mesh.vertices && mesh.faces) {
304
+ vertices = mesh.vertices;
305
+ for(let i = 0; i < mesh.faces.length; i++) {
306
+ const face = mesh.faces[i];
307
+ faces.push(face[0]);
308
+ faces.push(face[1]);
309
+ faces.push(face[2]);
310
+ }
311
+ } else if(mesh.getClassName && mesh.getClassName() === 'vtkPolyData') {
312
+ vertices = mesh.getPoints().getData();
313
+ const _vtkFaces = mesh.getPolys().getData();
314
+ const nbFaces = _vtkFaces.length / 4;
315
+ for(let i = 0; i < nbFaces; i++) {
316
+ faces.push(_vtkFaces[i * 4 + 1]);
317
+ faces.push(_vtkFaces[i * 4 + 2]);
318
+ faces.push(_vtkFaces[i * 4 + 3]);
319
+ }
320
+ }
321
+ return enCodeMeshByVF(vertices, faces, byteLength, attr)
322
+ }
323
+
324
+ /**
325
+ * 初始化Draco文件
326
+ */
327
+ export function initDrcCoder() {
328
+ if (!window.encoderModule) {
329
+ const timerEncoder = setInterval(() => {
330
+ if (window.DracoEncoderModule) {
331
+ window.DracoEncoderModule().then((res) => {
332
+ window.encoderModule = res;
333
+ });
334
+ clearInterval(timerEncoder);
335
+ }
336
+ }, 50);
337
+ }
338
+ if (!window.decoderModule) {
339
+ const timerDecoder = setInterval(() => {
340
+ if (window.DracoDecoderModule) {
341
+ window.DracoDecoderModule().then((res) => {
342
+ window.decoderModule = res;
343
+ function a() {
344
+ return res;
345
+ }
346
+ // show.value = true;
347
+ vtkDracoReader.setDracoDecoder(a);
348
+ });
349
+ clearInterval(timerDecoder);
350
+ }
351
+ }, 50);
352
+ }
353
+ }