three-base 1.0.81 → 1.0.82

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.
@@ -4,197 +4,332 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2022-06-08 14:33:45
6
6
  */
7
+ var __extends = (this && this.__extends) || (function () {
8
+ var extendStatics = function (d, b) {
9
+ extendStatics = Object.setPrototypeOf ||
10
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
12
+ return extendStatics(d, b);
13
+ };
14
+ return function (d, b) {
15
+ if (typeof b !== "function" && b !== null)
16
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
17
+ extendStatics(d, b);
18
+ function __() { this.constructor = d; }
19
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20
+ };
21
+ })();
22
+ var __assign = (this && this.__assign) || function () {
23
+ __assign = Object.assign || function(t) {
24
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
25
+ s = arguments[i];
26
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
27
+ t[p] = s[p];
28
+ }
29
+ return t;
30
+ };
31
+ return __assign.apply(this, arguments);
32
+ };
33
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
34
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
35
+ return new (P || (P = Promise))(function (resolve, reject) {
36
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
37
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
38
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
39
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
40
+ });
41
+ };
42
+ var __generator = (this && this.__generator) || function (thisArg, body) {
43
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
44
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
45
+ function verb(n) { return function (v) { return step([n, v]); }; }
46
+ function step(op) {
47
+ if (f) throw new TypeError("Generator is already executing.");
48
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
49
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
50
+ if (y = 0, t) op = [op[0] & 2, t.value];
51
+ switch (op[0]) {
52
+ case 0: case 1: t = op; break;
53
+ case 4: _.label++; return { value: op[1], done: false };
54
+ case 5: _.label++; y = op[1]; op = [0]; continue;
55
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
56
+ default:
57
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
58
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
59
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
60
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
61
+ if (t[2]) _.ops.pop();
62
+ _.trys.pop(); continue;
63
+ }
64
+ op = body.call(thisArg, _);
65
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
66
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
67
+ }
68
+ };
69
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
70
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
71
+ if (ar || !(i in from)) {
72
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
73
+ ar[i] = from[i];
74
+ }
75
+ }
76
+ return to.concat(ar || Array.prototype.slice.call(from));
77
+ };
7
78
  import Dexie from "dexie";
8
79
  import { filter, first, isEmpty } from "lodash";
9
80
  import { Font } from "three/examples/jsm/loaders/FontLoader.js";
10
81
  import threeUtils from "../threeUtils";
11
82
  import gentilisJSON from "./gentilis.json";
12
- class indexBD extends Dexie {
13
- constructor(dbName) {
14
- super(dbName);
83
+ var indexBD = /** @class */ (function (_super) {
84
+ __extends(indexBD, _super);
85
+ function indexBD(dbName) {
86
+ return _super.call(this, dbName) || this;
15
87
  }
16
- threeFont;
17
- }
88
+ return indexBD;
89
+ }(Dexie));
18
90
  // 字体加载器
19
- class fontContorl {
20
- constructor() { }
21
- THREEFontMap = new Map();
22
- // 专门存储字体的数据库
23
- fontBD = (() => {
24
- let fontDB = new indexBD("three-base-font");
25
- fontDB.version(1).stores({ threeFont: "++KeyPath,fontType,fontName,fontJson" });
26
- return fontDB;
27
- })();
91
+ var fontContorl = /** @class */ (function () {
92
+ function fontContorl() {
93
+ this.THREEFontMap = new Map();
94
+ // 专门存储字体的数据库
95
+ this.fontBD = (function () {
96
+ var fontDB = new indexBD("three-base-font");
97
+ fontDB.version(1).stores({ threeFont: "++KeyPath,fontType,fontName,fontJson" });
98
+ return fontDB;
99
+ })();
100
+ // 加载字体的回调函数们
101
+ this._loadTextCall = new Map();
102
+ }
28
103
  /**
29
104
  * 保存 字体json到本地数据库中
30
105
  * @param {saveFontParam} { fontType = "simhei", fontName, fontJson }
31
106
  * @memberof fontContorl
32
107
  */
33
- async saveFont({ fontType = "simhei", fontName, fontJson = "" }) {
34
- let findResult = await this.getFont({ fontType, fontName });
35
- if (isEmpty(findResult)) {
36
- await this.fontBD.threeFont.add({ fontType, fontName, fontJson });
37
- }
38
- }
108
+ fontContorl.prototype.saveFont = function (_a) {
109
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, fontName = _a.fontName, _c = _a.fontJson, fontJson = _c === void 0 ? "" : _c;
110
+ return __awaiter(this, void 0, void 0, function () {
111
+ var findResult;
112
+ return __generator(this, function (_d) {
113
+ switch (_d.label) {
114
+ case 0: return [4 /*yield*/, this.getFont({ fontType: fontType, fontName: fontName })];
115
+ case 1:
116
+ findResult = _d.sent();
117
+ if (!isEmpty(findResult)) return [3 /*break*/, 3];
118
+ return [4 /*yield*/, this.fontBD.threeFont.add({ fontType: fontType, fontName: fontName, fontJson: fontJson })];
119
+ case 2:
120
+ _d.sent();
121
+ _d.label = 3;
122
+ case 3: return [2 /*return*/];
123
+ }
124
+ });
125
+ });
126
+ };
39
127
  /**
40
128
  * 通过 fontType 和 fontName 查询 fontJson
41
129
  * @param {getFontParam} { fontType = "simhei", fontName }
42
130
  * @returns
43
131
  * @memberof fontContorl
44
132
  */
45
- async getFont({ fontType = "simhei", fontName }) {
46
- let fondResult = await this.fontBD.threeFont
47
- .filter(d => (d.fontType === fontType && d.fontName === fontName))
48
- .toArray();
49
- if (isEmpty(fondResult)) {
50
- return undefined;
51
- }
52
- else {
53
- return first(fondResult).fontJson;
54
- }
55
- }
56
- // 加载字体的回调函数们
57
- _loadTextCall = new Map();
133
+ fontContorl.prototype.getFont = function (_a) {
134
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, fontName = _a.fontName;
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ var fondResult;
137
+ return __generator(this, function (_c) {
138
+ switch (_c.label) {
139
+ case 0: return [4 /*yield*/, this.fontBD.threeFont
140
+ .filter(function (d) { return (d.fontType === fontType && d.fontName === fontName); })
141
+ .toArray()];
142
+ case 1:
143
+ fondResult = _c.sent();
144
+ if (isEmpty(fondResult)) {
145
+ return [2 /*return*/, undefined];
146
+ }
147
+ else {
148
+ return [2 /*return*/, first(fondResult).fontJson];
149
+ }
150
+ return [2 /*return*/];
151
+ }
152
+ });
153
+ });
154
+ };
58
155
  // 加载文字 提供给需要创建字体模型的方法使用 以节流的形式调用 回调函数
59
- async loadText({ fontType = "simhei", text = "", fontBasePath, callback }) {
60
- // 加载字体容器
61
- await this._loadFontIndexJson({ fontType, fontBasePath });
62
- let newCallback = () => {
63
- let fontFamily = this.THREEFontMap.get(fontType);
64
- if (callback && fontFamily) {
65
- callback(fontFamily);
66
- }
67
- };
68
- // 遍历文本
69
- filter(text).map(fontName => {
70
- // 没有的就去加载
71
- if (!this.hasFont({ fontType, fontName })) {
72
- // 添加文字的默认模型 避免报错
73
- let fontFamily = this.THREEFontMap.get(fontType);
74
- // 创建 three 字体对象
75
- if (fontFamily) {
76
- //@ts-ignore
77
- fontFamily.data.glyphs[fontName] = { "ha": 1389, "x_min": 477, "x_max": 949, "o": "m 700 901 q 906 770 830 906 q 770 477 949 618 q 711 288 711 412 l 613 288 q 781 608 613 494 q 819 722 825 651 q 700 808 808 803 l 684 808 q 581 673 575 803 l 477 673 q 613 884 477 836 q 700 901 629 901 m 732 184 l 732 71 l 591 71 l 591 184 l 732 184 z " };
156
+ fontContorl.prototype.loadText = function (_a) {
157
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, _c = _a.text, text = _c === void 0 ? "" : _c, fontBasePath = _a.fontBasePath, callback = _a.callback;
158
+ return __awaiter(this, void 0, void 0, function () {
159
+ var newCallback;
160
+ var _this = this;
161
+ return __generator(this, function (_d) {
162
+ switch (_d.label) {
163
+ case 0:
164
+ // 加载字体容器
165
+ return [4 /*yield*/, this._loadFontIndexJson({ fontType: fontType, fontBasePath: fontBasePath })];
166
+ case 1:
167
+ // 加载字体容器
168
+ _d.sent();
169
+ newCallback = function () {
170
+ var fontFamily = _this.THREEFontMap.get(fontType);
171
+ if (callback && fontFamily) {
172
+ callback(fontFamily);
173
+ }
174
+ };
175
+ // 遍历文本
176
+ filter(text).map(function (fontName) {
177
+ // 没有的就去加载
178
+ if (!_this.hasFont({ fontType: fontType, fontName: fontName })) {
179
+ // 添加文字的默认模型 避免报错
180
+ var fontFamily = _this.THREEFontMap.get(fontType);
181
+ // 创建 three 字体对象
182
+ if (fontFamily) {
183
+ //@ts-ignore
184
+ fontFamily.data.glyphs[fontName] = { "ha": 1389, "x_min": 477, "x_max": 949, "o": "m 700 901 q 906 770 830 906 q 770 477 949 618 q 711 288 711 412 l 613 288 q 781 608 613 494 q 819 722 825 651 q 700 808 808 803 l 684 808 q 581 673 575 803 l 477 673 q 613 884 477 836 q 700 901 629 901 m 732 184 l 732 71 l 591 71 l 591 184 l 732 184 z " };
185
+ }
186
+ _this._loadFontJson({ fontType: fontType, fontName: fontName, fontBasePath: fontBasePath });
187
+ }
188
+ // 加入待执行的回调
189
+ var origin_callbacks = _this._loadTextCall.get("".concat(fontType, "_").concat(fontName)) || [];
190
+ if (newCallback) {
191
+ _this._loadTextCall.set("".concat(fontType, "_").concat(fontName), __spreadArray(__spreadArray([], origin_callbacks, true), [newCallback], false));
192
+ }
193
+ });
194
+ // 执行任务队列
195
+ threeUtils.addWork(newCallback);
196
+ return [2 /*return*/];
78
197
  }
79
- this._loadFontJson({ fontType, fontName, fontBasePath });
80
- }
81
- // 加入待执行的回调
82
- let origin_callbacks = this._loadTextCall.get(`${fontType}_${fontName}`) || [];
83
- if (newCallback) {
84
- this._loadTextCall.set(`${fontType}_${fontName}`, [...origin_callbacks, newCallback]);
85
- }
198
+ });
86
199
  });
87
- // 执行任务队列
88
- threeUtils.addWork(newCallback);
89
- }
200
+ };
90
201
  // 加载字体
91
- async _loadFontIndexJson({ fontType = "simhei", fontBasePath }) {
92
- let fontFamily = this.THREEFontMap.get(fontType);
93
- // 创建 three 字体对象
94
- if (!fontFamily) {
95
- // 初始字体结构
96
- fontFamily = new Font({
97
- "underlineThickness": 69,
98
- "underlinePosition": -174,
99
- "resolution": 1000,
100
- "original_font_information": {},
101
- "familyName": "Source Han Sans CN Normal",
102
- "descender": -167,
103
- "cssFontWeight": "normal",
104
- "cssFontStyle": "normal",
105
- "boundingBox": {
106
- "yMin": -700,
107
- "xMin": -1389,
108
- "yMax": 700,
109
- "xMax": 4067
110
- },
111
- "glyphs": {}
202
+ fontContorl.prototype._loadFontIndexJson = function (_a) {
203
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, fontBasePath = _a.fontBasePath;
204
+ return __awaiter(this, void 0, void 0, function () {
205
+ var fontFamily, fontFamilyIndexJson;
206
+ return __generator(this, function (_c) {
207
+ switch (_c.label) {
208
+ case 0:
209
+ fontFamily = this.THREEFontMap.get(fontType);
210
+ if (!!fontFamily) return [3 /*break*/, 2];
211
+ // 初始字体结构
212
+ fontFamily = new Font({
213
+ "underlineThickness": 69,
214
+ "underlinePosition": -174,
215
+ "resolution": 1000,
216
+ "original_font_information": {},
217
+ "familyName": "Source Han Sans CN Normal",
218
+ "descender": -167,
219
+ "cssFontWeight": "normal",
220
+ "cssFontStyle": "normal",
221
+ "boundingBox": {
222
+ "yMin": -700,
223
+ "xMin": -1389,
224
+ "yMax": 700,
225
+ "xMax": 4067
226
+ },
227
+ "glyphs": {}
228
+ });
229
+ this.THREEFontMap.set(fontType, fontFamily);
230
+ return [4 /*yield*/, this.getFont({ fontType: fontType, fontName: "index" })];
231
+ case 1:
232
+ fontFamilyIndexJson = _c.sent();
233
+ // 加载外部字体初始结构文件
234
+ if (!fontFamilyIndexJson) {
235
+ this._loadFontJson({ fontType: fontType, fontName: "index", fontBasePath: fontBasePath });
236
+ }
237
+ _c.label = 2;
238
+ case 2: return [2 /*return*/];
239
+ }
112
240
  });
113
- this.THREEFontMap.set(fontType, fontFamily);
114
- // 读取本地数据库中的字体初始结构文件
115
- let fontFamilyIndexJson = await this.getFont({ fontType, fontName: "index" });
116
- // 加载外部字体初始结构文件
117
- if (!fontFamilyIndexJson) {
118
- this._loadFontJson({ fontType, fontName: "index", fontBasePath });
119
- }
120
- }
121
- }
241
+ });
242
+ };
122
243
  // 加载外部字体.js
123
- async _loadFontJson({ fontType = "simhei", fontName, fontBasePath }) {
124
- let fontJson = await this.getFont({ fontType, fontName });
125
- if (!fontJson) {
126
- fontJson = gentilisJSON.glyphs[fontName];
127
- }
128
- // 数据库中没有 就去加载字体js
129
- if (!fontJson) {
130
- // 加载字体js
131
- await new Promise(resolve => {
132
- let script = document.createElement("script");
133
- Object.assign(script, {
134
- type: "text/javascript",
135
- src: `${fontBasePath}${fontType}/${fontName}.js`,
136
- async: true,
137
- onload: () => {
138
- script.remove();
139
- resolve();
140
- },
141
- onerror: () => {
142
- }
143
- });
144
- document.head.appendChild(script);
244
+ fontContorl.prototype._loadFontJson = function (_a) {
245
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, fontName = _a.fontName, fontBasePath = _a.fontBasePath;
246
+ return __awaiter(this, void 0, void 0, function () {
247
+ var fontJson, fontFamily, origin_callbacks;
248
+ return __generator(this, function (_c) {
249
+ switch (_c.label) {
250
+ case 0: return [4 /*yield*/, this.getFont({ fontType: fontType, fontName: fontName })];
251
+ case 1:
252
+ fontJson = _c.sent();
253
+ if (!fontJson) {
254
+ fontJson = gentilisJSON.glyphs[fontName];
255
+ }
256
+ if (!!fontJson) return [3 /*break*/, 3];
257
+ // 加载字体js
258
+ return [4 /*yield*/, new Promise(function (resolve) {
259
+ var script = document.createElement("script");
260
+ Object.assign(script, {
261
+ type: "text/javascript",
262
+ src: "".concat(fontBasePath).concat(fontType, "/").concat(fontName, ".js"),
263
+ async: true,
264
+ onload: function () {
265
+ script.remove();
266
+ resolve();
267
+ },
268
+ onerror: function () {
269
+ }
270
+ });
271
+ document.head.appendChild(script);
272
+ })];
273
+ case 2:
274
+ // 加载字体js
275
+ _c.sent();
276
+ return [3 /*break*/, 4];
277
+ case 3:
278
+ fontFamily = this.THREEFontMap.get(fontType);
279
+ // 创建 three 字体对象
280
+ if (fontFamily) {
281
+ //@ts-ignore
282
+ fontFamily.data.glyphs[fontName] = fontJson;
283
+ }
284
+ _c.label = 4;
285
+ case 4:
286
+ origin_callbacks = this._loadTextCall.get("".concat(fontType, "_").concat(fontName)) || [];
287
+ // // 删除待执行的回调
288
+ this._loadTextCall.delete("".concat(fontType, "_").concat(fontName));
289
+ origin_callbacks.map(function (callback) {
290
+ // 执行任务队列
291
+ threeUtils.addWork(callback);
292
+ });
293
+ return [2 /*return*/];
294
+ }
145
295
  });
146
- }
147
- else {
148
- // 数据库或者默认字体中 有就加入字体文件中
149
- let fontFamily = this.THREEFontMap.get(fontType);
150
- // 创建 three 字体对象
151
- if (fontFamily) {
152
- //@ts-ignore
153
- fontFamily.data.glyphs[fontName] = fontJson;
154
- }
155
- }
156
- // 执行相应的回调函数
157
- let origin_callbacks = this._loadTextCall.get(`${fontType}_${fontName}`) || [];
158
- // // 删除待执行的回调
159
- this._loadTextCall.delete(`${fontType}_${fontName}`);
160
- origin_callbacks.map(callback => {
161
- // 执行任务队列
162
- threeUtils.addWork(callback);
163
296
  });
164
- }
297
+ };
165
298
  // 判断是否有这个文字
166
- hasFont({ fontType, fontName }) {
299
+ fontContorl.prototype.hasFont = function (_a) {
300
+ var fontType = _a.fontType, fontName = _a.fontName;
167
301
  return !isEmpty(this.THREEFontMap.get(fontType) &&
168
302
  //@ts-ignore
169
303
  this.THREEFontMap.get(fontType).data.glyphs[fontName]);
170
- }
304
+ };
171
305
  // 这个方法是提供给外部字体文件调用的
172
- addFont({ fontType = "simhei", fontName, fontJson }) {
306
+ fontContorl.prototype.addFont = function (_a) {
307
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, fontName = _a.fontName, fontJson = _a.fontJson;
173
308
  // 保存字体json到本地数据中
174
- this.saveFont({ fontType, fontName, fontJson });
175
- let fontFamily = this.THREEFontMap.get(fontType);
309
+ this.saveFont({ fontType: fontType, fontName: fontName, fontJson: fontJson });
310
+ var fontFamily = this.THREEFontMap.get(fontType);
176
311
  // 创建 three 字体对象
177
312
  if (fontFamily) {
178
313
  //@ts-ignore
179
314
  fontFamily.data.glyphs[fontName] = fontJson;
180
315
  }
181
- }
316
+ };
182
317
  // 这个方法是提供给外部字体文件调用的
183
- addFontFamily({ fontType = "simhei", fontFamilyJson }) {
318
+ fontContorl.prototype.addFontFamily = function (_a) {
319
+ var _b = _a.fontType, fontType = _b === void 0 ? "simhei" : _b, fontFamilyJson = _a.fontFamilyJson;
184
320
  // 保存字体初始结构到数据库中
185
- this.saveFont({ fontType, fontName: "index", fontJson: fontFamilyJson });
186
- let fontFamily = this.THREEFontMap.get(fontType);
321
+ this.saveFont({ fontType: fontType, fontName: "index", fontJson: fontFamilyJson });
322
+ var fontFamily = this.THREEFontMap.get(fontType);
187
323
  // 创建 three 字体对象
188
324
  if (fontFamily) {
189
325
  //@ts-ignore
190
- fontFamily.data = {
191
- ...fontFamilyJson,
326
+ fontFamily.data = __assign(__assign({}, fontFamilyJson), {
192
327
  //@ts-ignore
193
- glyphs: fontFamily.data.glyphs
194
- };
328
+ glyphs: fontFamily.data.glyphs });
195
329
  }
196
- }
197
- }
198
- const fontContorller = new fontContorl();
199
- Object.assign(window, { fontContorller });
330
+ };
331
+ return fontContorl;
332
+ }());
333
+ var fontContorller = new fontContorl();
334
+ Object.assign(window, { fontContorller: fontContorller });
200
335
  export default fontContorller;