hsu-utils 0.0.45 → 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.44
3
+ * hsu-utils v0.0.46
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1,10 +1,13 @@
1
1
  interface Font {
2
2
  style?: string;
3
- variant?: string;
4
3
  weight?: string;
5
4
  size?: number;
6
5
  family?: string;
7
6
  }
7
+ export declare function get_string_size_async(str: string, font?: Font): Promise<{
8
+ width: number;
9
+ height: number;
10
+ }>;
8
11
  export default function get_string_size(str: string, font?: Font): {
9
12
  width: number;
10
13
  height: number;
@@ -1,8 +1,31 @@
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
+ import { loadFont } from '..';
11
+ export function get_string_size_async(str, font = {}) {
12
+ return __awaiter(this, void 0, void 0, function* () {
13
+ const { style = 'normal', weight = 'normal', size = 10, family: fontFamily = 'sans-serif' } = font;
14
+ const canvas = document.createElement('canvas');
15
+ const ctx = canvas.getContext('2d');
16
+ yield loadFont({ ctx, font, text: str });
17
+ ctx.font = `${style} ${weight} ${size}px ${fontFamily}`;
18
+ const metrics = ctx.measureText(str);
19
+ let width = +(+metrics.width.toFixed(2)).toFixed(2);
20
+ const height = +(+metrics.actualBoundingBoxAscent.toFixed(2) + +metrics.actualBoundingBoxDescent.toFixed(2)).toFixed(2);
21
+ return { width: +width.toFixed(2), height: +height.toFixed(2) };
22
+ });
23
+ }
1
24
  export default function get_string_size(str, font = {}) {
2
- const { style = 'normal', variant = 'normal', weight = 'normal', size = 10, family: fontFamily = 'sans-serif' } = font;
25
+ const { style = 'normal', weight = 'normal', size = 10, family: fontFamily = 'sans-serif' } = font;
3
26
  const canvas = document.createElement('canvas');
4
27
  const ctx = canvas.getContext('2d');
5
- ctx.font = `${style} ${variant} ${weight} ${size}px ${fontFamily}`;
28
+ ctx.font = `${style} ${weight} ${size}px ${fontFamily}`;
6
29
  const metrics = ctx.measureText(str);
7
30
  let width = +(+metrics.width.toFixed(2)).toFixed(2);
8
31
  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
@@ -2,7 +2,7 @@ import console_table from './ConsoleTable';
2
2
  import deepCopy from './DeepCopy';
3
3
  import Equal from './Equal';
4
4
  import Typeof from './Typeof';
5
- import get_string_size from './GetStrSize';
5
+ import get_string_size, { get_string_size_async } from './GetStrSize';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
7
  import loadImage from './LoadImage';
8
8
  import RenderPDF from './RenderPDF';
@@ -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, 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
@@ -2,7 +2,7 @@ import console_table from './ConsoleTable';
2
2
  import deepCopy from './DeepCopy';
3
3
  import Equal from './Equal';
4
4
  import Typeof from './Typeof';
5
- import get_string_size from './GetStrSize';
5
+ import get_string_size, { get_string_size_async } from './GetStrSize';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
7
  import loadImage from './LoadImage';
8
8
  import RenderPDF from './RenderPDF';
@@ -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, 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 };
@@ -1,10 +1,13 @@
1
1
  interface Font {
2
2
  style?: string;
3
- variant?: string;
4
3
  weight?: string;
5
4
  size?: number;
6
5
  family?: string;
7
6
  }
7
+ export declare function get_string_size_async(str: string, font?: Font): Promise<{
8
+ width: number;
9
+ height: number;
10
+ }>;
8
11
  export default function get_string_size(str: string, font?: Font): {
9
12
  width: number;
10
13
  height: number;
@@ -1,11 +1,72 @@
1
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
+ };
2
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.get_string_size_async = void 0;
40
+ var __1 = require("..");
41
+ function get_string_size_async(str, font) {
42
+ if (font === void 0) { font = {}; }
43
+ return __awaiter(this, void 0, void 0, function () {
44
+ var _a, style, _b, weight, _c, size, _d, fontFamily, canvas, ctx, metrics, width, height;
45
+ return __generator(this, function (_e) {
46
+ switch (_e.label) {
47
+ case 0:
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;
49
+ canvas = document.createElement('canvas');
50
+ ctx = canvas.getContext('2d');
51
+ return [4, (0, __1.loadFont)({ ctx: ctx, font: font, text: str })];
52
+ case 1:
53
+ _e.sent();
54
+ ctx.font = "".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily);
55
+ metrics = ctx.measureText(str);
56
+ width = +(+metrics.width.toFixed(2)).toFixed(2);
57
+ height = +(+metrics.actualBoundingBoxAscent.toFixed(2) + +metrics.actualBoundingBoxDescent.toFixed(2)).toFixed(2);
58
+ return [2, { width: +width.toFixed(2), height: +height.toFixed(2) }];
59
+ }
60
+ });
61
+ });
62
+ }
63
+ exports.get_string_size_async = get_string_size_async;
3
64
  function get_string_size(str, font) {
4
65
  if (font === void 0) { font = {}; }
5
- var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _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.family, fontFamily = _e === void 0 ? 'sans-serif' : _e;
66
+ var _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;
6
67
  var canvas = document.createElement('canvas');
7
68
  var ctx = canvas.getContext('2d');
8
- ctx.font = "".concat(style, " ").concat(variant, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily);
69
+ ctx.font = "".concat(style, " ").concat(weight, " ").concat(size, "px ").concat(fontFamily);
9
70
  var metrics = ctx.measureText(str);
10
71
  var width = +(+metrics.width.toFixed(2)).toFixed(2);
11
72
  var 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
@@ -2,7 +2,7 @@ import console_table from './ConsoleTable';
2
2
  import deepCopy from './DeepCopy';
3
3
  import Equal from './Equal';
4
4
  import Typeof from './Typeof';
5
- import get_string_size from './GetStrSize';
5
+ import get_string_size, { get_string_size_async } from './GetStrSize';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
7
  import loadImage from './LoadImage';
8
8
  import RenderPDF from './RenderPDF';
@@ -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, 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
@@ -1,9 +1,32 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
5
28
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getTimeDifference = exports.generateRandomStr = exports.array_is_includes = exports.downloadFile = exports.RenderPDF = exports.loadImage = exports.ConvertNumbers = 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;
7
30
  var ConsoleTable_1 = __importDefault(require("./ConsoleTable"));
8
31
  exports.console_table = ConsoleTable_1.default;
9
32
  var DeepCopy_1 = __importDefault(require("./DeepCopy"));
@@ -12,8 +35,9 @@ var Equal_1 = __importDefault(require("./Equal"));
12
35
  exports.Equal = Equal_1.default;
13
36
  var Typeof_1 = __importDefault(require("./Typeof"));
14
37
  exports.Typeof = Typeof_1.default;
15
- var GetStrSize_1 = __importDefault(require("./GetStrSize"));
38
+ var GetStrSize_1 = __importStar(require("./GetStrSize"));
16
39
  exports.get_string_size = GetStrSize_1.default;
40
+ Object.defineProperty(exports, "get_string_size_async", { enumerable: true, get: function () { return GetStrSize_1.get_string_size_async; } });
17
41
  var ConvertNumbers_1 = __importDefault(require("./ConvertNumbers"));
18
42
  exports.ConvertNumbers = ConvertNumbers_1.default;
19
43
  var LoadImage_1 = __importDefault(require("./LoadImage"));
@@ -28,3 +52,5 @@ var GenerateRandomStr_1 = __importDefault(require("./GenerateRandomStr"));
28
52
  exports.generateRandomStr = GenerateRandomStr_1.default;
29
53
  var GetTimeDifference_1 = __importDefault(require("./GetTimeDifference"));
30
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.45",
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>",