@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
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class DocumentStyleUtil {
|
|
2
|
+
static getFirstInlineStyleValue(text: string, pattern: string): string;
|
|
3
|
+
static getFirstFontSize(text: string, toPt?: boolean): string;
|
|
4
|
+
static getFirstFontFamily(text: string): string;
|
|
5
|
+
static getFirstFontColor(text: string): string;
|
|
6
|
+
static isPx(str: any): boolean;
|
|
7
|
+
static toPt(px: any, precision?: number): number;
|
|
8
|
+
static toPx(pt: any, precision?: number): number;
|
|
9
|
+
static convertToPt(fontSize: any, precision?: number): any;
|
|
10
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentStyleUtil = void 0;
|
|
4
|
+
class DocumentStyleUtil {
|
|
5
|
+
static getFirstInlineStyleValue(text, pattern) {
|
|
6
|
+
if (!text)
|
|
7
|
+
return null;
|
|
8
|
+
const regex = new RegExp('(?:\\b' + pattern + ':)\\s*([^;]*)[;"](?=[^>]*>)');
|
|
9
|
+
const found = text === null || text === void 0 ? void 0 : text.match(regex);
|
|
10
|
+
return (found === null || found === void 0 ? void 0 : found.length) > 1 ? found[1] : null;
|
|
11
|
+
}
|
|
12
|
+
static getFirstFontSize(text, toPt = false) {
|
|
13
|
+
const fontSize = this.getFirstInlineStyleValue(text, 'font-size');
|
|
14
|
+
if (!toPt || !fontSize) {
|
|
15
|
+
return fontSize;
|
|
16
|
+
}
|
|
17
|
+
return this.convertToPt(fontSize)
|
|
18
|
+
.replace('pt', '')
|
|
19
|
+
.replace('px', '');
|
|
20
|
+
}
|
|
21
|
+
static getFirstFontFamily(text) {
|
|
22
|
+
return this.getFirstInlineStyleValue(text, 'font-family');
|
|
23
|
+
}
|
|
24
|
+
static getFirstFontColor(text) {
|
|
25
|
+
return this.getFirstInlineStyleValue(text, 'color');
|
|
26
|
+
}
|
|
27
|
+
static isPx(str) {
|
|
28
|
+
return /[0-9.]+px$/.test(str);
|
|
29
|
+
}
|
|
30
|
+
static toPt(px, precision = 0) {
|
|
31
|
+
const factor = Math.pow(10, precision);
|
|
32
|
+
return Math.round(((parseInt(px, 10) * 72) / 96) * factor) / factor;
|
|
33
|
+
}
|
|
34
|
+
static toPx(pt, precision = 0) {
|
|
35
|
+
const factor = Math.pow(10, precision);
|
|
36
|
+
return Math.round((parseInt(pt, 10) / 72) * 96 * factor) / factor;
|
|
37
|
+
}
|
|
38
|
+
static convertToPt(fontSize, precision = 0) {
|
|
39
|
+
if (this.isPx(fontSize)) {
|
|
40
|
+
return this.toPt(fontSize, precision) + 'pt';
|
|
41
|
+
}
|
|
42
|
+
return fontSize;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.DocumentStyleUtil = DocumentStyleUtil;
|
|
@@ -6,10 +6,12 @@ export declare class MeasureTextSettings {
|
|
|
6
6
|
scale?: number;
|
|
7
7
|
italic?: boolean;
|
|
8
8
|
bold?: boolean;
|
|
9
|
+
letterSpacing?: number;
|
|
9
10
|
map?: any;
|
|
10
11
|
}
|
|
11
|
-
declare
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
export declare class DocumentTextUtil {
|
|
13
|
+
static measureText(string: string, settings: MeasureTextSettings): {
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentTextUtil = 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
|
+
class DocumentTextUtil {
|
|
10
|
+
static measureText(string, settings) {
|
|
11
|
+
if (string === null || string === undefined) {
|
|
12
|
+
return { width: 0, height: 0 };
|
|
13
|
+
}
|
|
14
|
+
const sett = Object.assign(Object.assign({}, settingsDefaults), settings);
|
|
15
|
+
let font = sett.fontFamily.toLowerCase();
|
|
16
|
+
const fontSize = sett.fontSize;
|
|
17
|
+
const variant = 0 + (sett.bold ? 1 : 0) + (sett.italic ? 2 : 0);
|
|
18
|
+
const map = sett.map || widths_map_1.WIDTHS_MAP;
|
|
19
|
+
const available = Object.keys(map);
|
|
20
|
+
if (available.indexOf(font) === -1) {
|
|
21
|
+
if (sett.fallbackFontFamily && available.indexOf(sett.fallbackFontFamily.toLowerCase()) !== -1) {
|
|
22
|
+
font = sett.fallbackFontFamily.toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
font = 'arial';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let totalWidth = 0;
|
|
29
|
+
let lines = 1;
|
|
30
|
+
let lineWidth = 0;
|
|
31
|
+
let charLength = 0;
|
|
32
|
+
const str = string.toString().normalize('NFD');
|
|
33
|
+
const words = str.split(/(\s|-)/);
|
|
34
|
+
const fixedWidth = settings.width ? (settings.width * 100) / fontSize : null;
|
|
35
|
+
words.forEach((word, index) => {
|
|
36
|
+
let wordWidth = 0;
|
|
37
|
+
word.split('').forEach(char => {
|
|
38
|
+
if (/[\x00-\x1F]/.test(char)) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
const widths = map[font][char] || map[font].x;
|
|
42
|
+
const width = widths[variant];
|
|
43
|
+
totalWidth += width;
|
|
44
|
+
wordWidth += width;
|
|
45
|
+
charLength++;
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
if (fixedWidth != null) {
|
|
49
|
+
lineWidth = lineWidth + wordWidth;
|
|
50
|
+
if (lineWidth > fixedWidth) {
|
|
51
|
+
lineWidth = wordWidth % fixedWidth;
|
|
52
|
+
lines = (index === 0 ? 0 : lines) + Math.max(1, Math.ceil(wordWidth / fixedWidth));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
let letterSpacingWidth = sett.letterSpacing ? charLength * sett.letterSpacing : 0;
|
|
57
|
+
let width = Math.ceil(totalWidth * (fontSize / 100) + letterSpacingWidth);
|
|
58
|
+
let height = Math.ceil(fontSize * 1.16);
|
|
59
|
+
if ((settings === null || settings === void 0 ? void 0 : settings.width) != null) {
|
|
60
|
+
width = settings.width;
|
|
61
|
+
height = height * lines;
|
|
62
|
+
}
|
|
63
|
+
height = height + 1 * ((settings === null || settings === void 0 ? void 0 : settings.scale) || 1);
|
|
64
|
+
return { width, height };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.DocumentTextUtil = DocumentTextUtil;
|