@contrail/document-util 1.0.0

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/README.md ADDED
@@ -0,0 +1 @@
1
+ Document util library.
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './measure-text/measure-text';
package/lib/index.js ADDED
@@ -0,0 +1,17 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./measure-text/measure-text"), exports);
@@ -0,0 +1,15 @@
1
+ export declare class MeasureTextSettings {
2
+ fontFamily?: string;
3
+ fallbackFontFamily?: string;
4
+ fontSize?: number;
5
+ width?: number;
6
+ scale?: number;
7
+ italic?: boolean;
8
+ bold?: boolean;
9
+ map?: any;
10
+ }
11
+ declare const measureText: (string: any, settings: MeasureTextSettings) => {
12
+ width: number;
13
+ height: number;
14
+ };
15
+ export default measureText;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MeasureTextSettings = void 0;
4
+ const widths_map_1 = require("./widths-map");
5
+ const settingsDefaults = { fontFamily: 'Arial', fontSize: 14 };
6
+ class MeasureTextSettings {
7
+ }
8
+ exports.MeasureTextSettings = MeasureTextSettings;
9
+ const measureText = (string, settings) => {
10
+ if (string === null || string === undefined) {
11
+ return { width: 0, height: 0 };
12
+ }
13
+ const sett = Object.assign(Object.assign({}, settingsDefaults), settings);
14
+ let font = sett.fontFamily.toLowerCase();
15
+ const fontSize = sett.fontSize;
16
+ const variant = 0 + (sett.bold ? 1 : 0) + (sett.italic ? 2 : 0);
17
+ const map = sett.map || widths_map_1.WIDTHS_MAP;
18
+ const available = Object.keys(map);
19
+ if (available.indexOf(font) === -1) {
20
+ if (sett.fallbackFontFamily && available.indexOf(sett.fallbackFontFamily) !== -1) {
21
+ font = sett.fallbackFontFamily;
22
+ }
23
+ else {
24
+ font = 'arial';
25
+ }
26
+ }
27
+ let totalWidth = 0;
28
+ let lines = 1;
29
+ let lineWidth = 0;
30
+ const str = string.toString().normalize('NFD');
31
+ const words = str.split(/(\s|-)/);
32
+ const fixedWidth = settings.width ? (settings.width * 100) / fontSize : null;
33
+ words.forEach((word, index) => {
34
+ let wordWidth = 0;
35
+ word.split('').forEach((char) => {
36
+ if (/[\x00-\x1F]/.test(char)) {
37
+ return true;
38
+ }
39
+ const widths = map[font][char] || map[font].x;
40
+ const width = widths[variant];
41
+ totalWidth += width;
42
+ wordWidth += width;
43
+ return true;
44
+ });
45
+ if (fixedWidth != null) {
46
+ lineWidth = lineWidth + wordWidth;
47
+ if (lineWidth > fixedWidth) {
48
+ lineWidth = wordWidth % fixedWidth;
49
+ lines = (index === 0 ? 0 : lines) + Math.max(1, Math.ceil(wordWidth / fixedWidth));
50
+ }
51
+ }
52
+ });
53
+ let width = Math.ceil(totalWidth * (fontSize / 100));
54
+ let height = Math.ceil(fontSize * 1.16);
55
+ if ((settings === null || settings === void 0 ? void 0 : settings.width) != null) {
56
+ width = settings.width;
57
+ height = height * lines;
58
+ }
59
+ height = height + 1 * ((settings === null || settings === void 0 ? void 0 : settings.scale) || 1);
60
+ return { width, height };
61
+ };
62
+ exports.default = measureText;