hsu-utils 0.0.46 → 0.0.47

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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.45
3
+ * hsu-utils v0.0.46
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -7,15 +7,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ import { loadFont } from '..';
10
11
  export function get_string_size_async(str, font = {}) {
11
12
  return __awaiter(this, void 0, void 0, function* () {
12
13
  const { style = 'normal', weight = 'normal', size = 10, family: fontFamily = 'sans-serif' } = font;
13
14
  const canvas = document.createElement('canvas');
14
15
  const ctx = canvas.getContext('2d');
15
- yield document.fonts.load(`${style} ${weight} ${size}px ${fontFamily}`);
16
+ yield loadFont({ ctx, font, text: str });
16
17
  ctx.font = `${style} ${weight} ${size}px ${fontFamily}`;
17
- ctx.fillText('', -999, -999);
18
- yield new Promise(requestAnimationFrame);
19
18
  const metrics = ctx.measureText(str);
20
19
  let width = +(+metrics.width.toFixed(2)).toFixed(2);
21
20
  const height = +(+metrics.actualBoundingBoxAscent.toFixed(2) + +metrics.actualBoundingBoxDescent.toFixed(2)).toFixed(2);
@@ -0,0 +1,13 @@
1
+ interface Font {
2
+ style?: string;
3
+ weight?: string;
4
+ size?: number;
5
+ fontFamily?: string;
6
+ }
7
+ export interface LoadFontOptions {
8
+ ctx?: CanvasRenderingContext2D;
9
+ font?: Font;
10
+ text?: string;
11
+ }
12
+ export default function loadFont(options: LoadFontOptions): Promise<void>;
13
+ export {};
@@ -0,0 +1,20 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ export default function loadFont(options) {
11
+ return __awaiter(this, void 0, void 0, function* () {
12
+ const { ctx, font = {}, text } = options;
13
+ const { style = 'normal', weight = 'normal', size = 10, fontFamily = 'sans-serif' } = font;
14
+ yield document.fonts.load(`${style} ${weight} ${size}px ${fontFamily}`);
15
+ const _ctx = ctx || document.createElement('canvas').getContext('2d');
16
+ _ctx.font = `${style} ${weight} ${size}px ${fontFamily}`;
17
+ _ctx.fillText(text || '', -999, -999);
18
+ yield new Promise(requestAnimationFrame);
19
+ });
20
+ }
package/es/index.d.ts CHANGED
@@ -10,6 +10,7 @@ import downloadFile from './DownloadFile';
10
10
  import array_is_includes from './ArrayIsIncludes';
11
11
  import generateRandomStr from './GenerateRandomStr';
12
12
  import getTimeDifference from './GetTimeDifference';
13
- export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
13
+ import loadFont from './LoadFont';
14
+ export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
14
15
  import { ConsoleData } from './ConsoleTable';
15
16
  export type { ConsoleData };
package/es/index.js CHANGED
@@ -10,4 +10,5 @@ import downloadFile from './DownloadFile';
10
10
  import array_is_includes from './ArrayIsIncludes';
11
11
  import generateRandomStr from './GenerateRandomStr';
12
12
  import getTimeDifference from './GetTimeDifference';
13
- export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
13
+ import loadFont from './LoadFont';
14
+ export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
@@ -37,6 +37,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.get_string_size_async = void 0;
40
+ var __1 = require("..");
40
41
  function get_string_size_async(str, font) {
41
42
  if (font === void 0) { font = {}; }
42
43
  return __awaiter(this, void 0, void 0, function () {
@@ -47,14 +48,10 @@ function get_string_size_async(str, font) {
47
48
  _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.weight, weight = _b === void 0 ? 'normal' : _b, _c = font.size, size = _c === void 0 ? 10 : _c, _d = font.family, fontFamily = _d === void 0 ? 'sans-serif' : _d;
48
49
  canvas = document.createElement('canvas');
49
50
  ctx = canvas.getContext('2d');
50
- return [4, document.fonts.load("".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily))];
51
+ return [4, (0, __1.loadFont)({ ctx: ctx, font: font, text: str })];
51
52
  case 1:
52
53
  _e.sent();
53
54
  ctx.font = "".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily);
54
- ctx.fillText('', -999, -999);
55
- return [4, new Promise(requestAnimationFrame)];
56
- case 2:
57
- _e.sent();
58
55
  metrics = ctx.measureText(str);
59
56
  width = +(+metrics.width.toFixed(2)).toFixed(2);
60
57
  height = +(+metrics.actualBoundingBoxAscent.toFixed(2) + +metrics.actualBoundingBoxDescent.toFixed(2)).toFixed(2);
@@ -0,0 +1,13 @@
1
+ interface Font {
2
+ style?: string;
3
+ weight?: string;
4
+ size?: number;
5
+ fontFamily?: string;
6
+ }
7
+ export interface LoadFontOptions {
8
+ ctx?: CanvasRenderingContext2D;
9
+ font?: Font;
10
+ text?: string;
11
+ }
12
+ export default function loadFont(options: LoadFontOptions): Promise<void>;
13
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ function loadFont(options) {
40
+ return __awaiter(this, void 0, void 0, function () {
41
+ var ctx, _a, font, text, _b, style, _c, weight, _d, size, _e, fontFamily, _ctx;
42
+ return __generator(this, function (_f) {
43
+ switch (_f.label) {
44
+ case 0:
45
+ ctx = options.ctx, _a = options.font, font = _a === void 0 ? {} : _a, text = options.text;
46
+ _b = font.style, style = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 10 : _d, _e = font.fontFamily, fontFamily = _e === void 0 ? 'sans-serif' : _e;
47
+ return [4, document.fonts.load("".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily))];
48
+ case 1:
49
+ _f.sent();
50
+ _ctx = ctx || document.createElement('canvas').getContext('2d');
51
+ _ctx.font = "".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily);
52
+ _ctx.fillText(text || '', -999, -999);
53
+ return [4, new Promise(requestAnimationFrame)];
54
+ case 2:
55
+ _f.sent();
56
+ return [2];
57
+ }
58
+ });
59
+ });
60
+ }
61
+ exports.default = loadFont;
package/lib/index.d.ts CHANGED
@@ -10,6 +10,7 @@ import downloadFile from './DownloadFile';
10
10
  import array_is_includes from './ArrayIsIncludes';
11
11
  import generateRandomStr from './GenerateRandomStr';
12
12
  import getTimeDifference from './GetTimeDifference';
13
- export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
13
+ import loadFont from './LoadFont';
14
+ export { console_table, deepCopy, Equal, Typeof, get_string_size, get_string_size_async, loadFont, ConvertNumbers, loadImage, RenderPDF, downloadFile, array_is_includes, generateRandomStr, getTimeDifference };
14
15
  import { ConsoleData } from './ConsoleTable';
15
16
  export type { ConsoleData };
package/lib/index.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.getTimeDifference = exports.generateRandomStr = exports.array_is_includes = exports.downloadFile = exports.RenderPDF = exports.loadImage = exports.ConvertNumbers = exports.get_string_size_async = exports.get_string_size = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
29
+ exports.getTimeDifference = exports.generateRandomStr = exports.array_is_includes = exports.downloadFile = exports.RenderPDF = exports.loadImage = exports.ConvertNumbers = exports.loadFont = exports.get_string_size_async = exports.get_string_size = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
30
30
  var ConsoleTable_1 = __importDefault(require("./ConsoleTable"));
31
31
  exports.console_table = ConsoleTable_1.default;
32
32
  var DeepCopy_1 = __importDefault(require("./DeepCopy"));
@@ -52,3 +52,5 @@ var GenerateRandomStr_1 = __importDefault(require("./GenerateRandomStr"));
52
52
  exports.generateRandomStr = GenerateRandomStr_1.default;
53
53
  var GetTimeDifference_1 = __importDefault(require("./GetTimeDifference"));
54
54
  exports.getTimeDifference = GetTimeDifference_1.default;
55
+ var LoadFont_1 = __importDefault(require("./LoadFont"));
56
+ exports.loadFont = LoadFont_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.46",
3
+ "version": "0.0.47",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",