@contrail/document-util 1.0.0 → 1.0.2
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/lib/document-style-util/document-style-util.service.d.ts +10 -0
- package/lib/document-style-util/document-style-util.service.js +45 -0
- package/lib/{measure-text/measure-text.d.ts → document-text-util/document-text-util.d.ts} +7 -5
- package/lib/document-text-util/document-text-util.js +67 -0
- package/lib/{measure-text → document-text-util}/widths-map.d.ts +282 -282
- package/lib/document-text-util/widths-map.js +2333 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/package.json +1 -1
- package/lib/measure-text/measure-text.js +0 -62
- package/lib/measure-text/widths-map.js +0 -5658
package/lib/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './
|
|
1
|
+
export * from './document-text-util/document-text-util';
|
|
2
|
+
export * from './document-style-util/document-style-util.service';
|
package/lib/index.js
CHANGED
|
@@ -14,4 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./document-text-util/document-text-util"), exports);
|
|
18
|
+
__exportStar(require("./document-style-util/document-style-util.service"), exports);
|
package/package.json
CHANGED
|
@@ -1,62 +0,0 @@
|
|
|
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;
|