@pdfme/common 1.1.9 → 1.2.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/dist/cjs/__tests__/calculateDynamicFontSize.test.js +97 -0
- package/dist/cjs/__tests__/calculateDynamicFontSize.test.js.map +1 -0
- package/dist/cjs/src/calculateDynamicFontSize.js +103 -0
- package/dist/cjs/src/calculateDynamicFontSize.js.map +1 -0
- package/dist/cjs/src/constants.js +6 -2
- package/dist/cjs/src/constants.js.map +1 -1
- package/dist/cjs/src/helper.js +3 -10
- package/dist/cjs/src/helper.js.map +1 -1
- package/dist/cjs/src/index.js +8 -2
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/schema.js +11 -6
- package/dist/cjs/src/schema.js.map +1 -1
- package/dist/cjs/src/type.js.map +1 -1
- package/dist/esm/__tests__/calculateDynamicFontSize.test.js +72 -0
- package/dist/esm/__tests__/calculateDynamicFontSize.test.js.map +1 -0
- package/dist/esm/src/calculateDynamicFontSize.js +76 -0
- package/dist/esm/src/calculateDynamicFontSize.js.map +1 -0
- package/dist/esm/src/constants.js +5 -1
- package/dist/esm/src/constants.js.map +1 -1
- package/dist/esm/src/helper.js +4 -11
- package/dist/esm/src/helper.js.map +1 -1
- package/dist/esm/src/index.js +3 -2
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/schema.js +10 -5
- package/dist/esm/src/schema.js.map +1 -1
- package/dist/esm/src/type.js.map +1 -1
- package/dist/types/__tests__/calculateDynamicFontSize.test.d.ts +1 -0
- package/dist/types/src/calculateDynamicFontSize.d.ts +6 -0
- package/dist/types/src/constants.d.ts +5 -1
- package/dist/types/src/index.d.ts +5 -4
- package/dist/types/src/schema.d.ts +386 -43
- package/dist/types/src/type.d.ts +6 -1
- package/package.json +7 -2
- package/src/calculateDynamicFontSize.ts +101 -0
- package/src/constants.ts +6 -2
- package/src/helper.ts +4 -11
- package/src/index.ts +14 -2
- package/src/schema.ts +12 -5
- package/src/type.ts +2 -0
- package/tsconfig.json +6 -0
@@ -0,0 +1,97 @@
|
|
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
|
+
};
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
32
|
+
});
|
33
|
+
};
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
35
|
+
const fs_1 = require("fs");
|
36
|
+
const path = __importStar(require("path"));
|
37
|
+
const calculateDynamicFontSize_1 = require("../src/calculateDynamicFontSize");
|
38
|
+
const helper_1 = require("../src/helper");
|
39
|
+
const sansData = (0, fs_1.readFileSync)(path.join(__dirname, `/assets/fonts/SauceHanSansJP.ttf`));
|
40
|
+
const serifData = (0, fs_1.readFileSync)(path.join(__dirname, `/assets/fonts/SauceHanSerifJP.ttf`));
|
41
|
+
const getSampleFont = () => ({
|
42
|
+
SauceHanSansJP: { fallback: true, data: sansData },
|
43
|
+
SauceHanSerifJP: { data: serifData },
|
44
|
+
});
|
45
|
+
const getTextSchema = () => {
|
46
|
+
const textSchema = {
|
47
|
+
position: { x: 0, y: 0 }, type: 'text', fontSize: 14, characterSpacing: 1, width: 50, height: 50
|
48
|
+
};
|
49
|
+
return textSchema;
|
50
|
+
};
|
51
|
+
describe('calculateDynamicFontSize with Default font', () => {
|
52
|
+
const font = (0, helper_1.getDefaultFont)();
|
53
|
+
it('should return default font size when dynamicFontSizeSetting is not provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
54
|
+
const textSchema = getTextSchema();
|
55
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'test' });
|
56
|
+
expect(result).toBe(14);
|
57
|
+
}));
|
58
|
+
it('should return smaller font size when dynamicFontSizeSetting is provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
59
|
+
const textSchema = Object.assign(getTextSchema(), { dynamicFontSize: { max: 20, min: 10 } });
|
60
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'testtesttesttesttest' });
|
61
|
+
expect(result).toBe(14.25);
|
62
|
+
}));
|
63
|
+
it('should return min font size when dynamicFontSizeSetting and long text are provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
64
|
+
const textSchema = Object.assign(getTextSchema(), { dynamicFontSize: { max: 20, min: 10 } });
|
65
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'testtesttesttesttesttesttesttesttesttesttest' });
|
66
|
+
expect(result).toBe(10);
|
67
|
+
}));
|
68
|
+
it('should return max font size when dynamicFontSizeSetting and short text are provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
69
|
+
const textSchema = Object.assign(getTextSchema(), { dynamicFontSize: { max: 20, min: 10 } });
|
70
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'a' });
|
71
|
+
expect(result).toBe(20);
|
72
|
+
}));
|
73
|
+
});
|
74
|
+
describe('calculateDynamicFontSize with Custom font', () => {
|
75
|
+
const font = getSampleFont();
|
76
|
+
it('should return default font size when dynamicFontSizeSetting is not provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
77
|
+
const textSchema = getTextSchema();
|
78
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'あいう' });
|
79
|
+
expect(result).toBe(14);
|
80
|
+
}));
|
81
|
+
it('should return smaller font size when dynamicFontSizeSetting is provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
82
|
+
const textSchema = Object.assign(getTextSchema(), { dynamicFontSize: { max: 20, min: 10 } });
|
83
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'あいうあいうあいうあいうあいうあいう' });
|
84
|
+
expect(result).toBe(15.5);
|
85
|
+
}));
|
86
|
+
it('should return min font size when dynamicFontSizeSetting and long text are provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
87
|
+
const textSchema = Object.assign(getTextSchema(), { dynamicFontSize: { max: 20, min: 10 } });
|
88
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'あいうあいうあいうあいうあいうあいうあいうあいうあいう' });
|
89
|
+
expect(result).toBe(10);
|
90
|
+
}));
|
91
|
+
it('should return max font size when dynamicFontSizeSetting and short text are provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
92
|
+
const textSchema = Object.assign(getTextSchema(), { dynamicFontSize: { max: 20, min: 10 } });
|
93
|
+
const result = yield (0, calculateDynamicFontSize_1.calculateDynamicFontSize)({ textSchema, font, input: 'あ' });
|
94
|
+
expect(result).toBe(20);
|
95
|
+
}));
|
96
|
+
});
|
97
|
+
//# sourceMappingURL=calculateDynamicFontSize.test.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"calculateDynamicFontSize.test.js","sourceRoot":"","sources":["../../../__tests__/calculateDynamicFontSize.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2BAAkC;AAClC,2CAA6B;AAC7B,8EAA0E;AAE1E,0CAA8C;AAE9C,MAAM,QAAQ,GAAG,IAAA,iBAAY,EAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,kCAAkC,CAAC,CAAC,CAAC;AACxF,MAAM,SAAS,GAAG,IAAA,iBAAY,EAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,mCAAmC,CAAC,CAAC,CAAC;AAE1F,MAAM,aAAa,GAAG,GAAS,EAAE,CAAC,CAAC;IACjC,cAAc,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE;IAClD,eAAe,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;CACrC,CAAC,CAAC;AAEH,MAAM,aAAa,GAAG,GAAG,EAAE;IACzB,MAAM,UAAU,GAAe;QAC7B,QAAQ,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,gBAAgB,EAAE,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE;KACjG,CAAC;IACF,OAAO,UAAU,CAAA;AACnB,CAAC,CAAA;AAED,QAAQ,CAAC,4CAA4C,EAAE,GAAG,EA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AC,oFAAoF,EAAE,GAAS,EAAE;QAClG,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;QAC7F,MAAM,MAAM,GAAG,MAAM,IAAA,mDAAwB,EAAC,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,6BAA6B,EAAE,CAAC,CAAC;QAE1G,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1B,CAAC,CAAA,CAAC,CAAC;IAEH,EAAE,CAAC,qFAAqF,EAAE,GAAS,EAAE;QACnG,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,EAAE,EAAE,eAAe,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;QAC7F,MAAM,MAAM,GAAG,MAAM,IAAA,mDAAwB,EAAC,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;QAEhF,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC1B,CAAC,CAAA,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
@@ -0,0 +1,103 @@
|
|
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
|
+
};
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
32
|
+
});
|
33
|
+
};
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
35
|
+
exports.calculateDynamicFontSize = void 0;
|
36
|
+
const fontkit = __importStar(require("fontkit"));
|
37
|
+
const buffer_1 = require("buffer");
|
38
|
+
const constants_1 = require("./constants");
|
39
|
+
const _1 = require(".");
|
40
|
+
const widthOfTextAtSize = (input, fontKitFont, fontSize) => {
|
41
|
+
const { glyphs } = fontKitFont.layout(input);
|
42
|
+
const scale = 1000 / fontKitFont.unitsPerEm;
|
43
|
+
return glyphs.reduce((totalWidth, glyph) => totalWidth + glyph.advanceWidth * scale, 0) * (fontSize / 1000);
|
44
|
+
};
|
45
|
+
const calculateCharacterSpacing = (textContent, textCharacterSpacing) => {
|
46
|
+
const numberOfCharacters = textContent.length;
|
47
|
+
return (numberOfCharacters - 1) * textCharacterSpacing;
|
48
|
+
};
|
49
|
+
const calculateTextWidthInMm = (textContent, textWidth, textCharacterSpacing) => (textWidth + calculateCharacterSpacing(textContent, textCharacterSpacing)) * constants_1.DEFAULT_PT_TO_MM_RATIO;
|
50
|
+
const getLongestLine = (textContentRows, fontKitFont, fontSize, characterSpacingCount) => {
|
51
|
+
let longestLine = '';
|
52
|
+
let maxLineWidth = 0;
|
53
|
+
textContentRows.forEach((line) => {
|
54
|
+
const textWidth = widthOfTextAtSize(line, fontKitFont, fontSize);
|
55
|
+
const lineWidth = calculateTextWidthInMm(line, textWidth, characterSpacingCount);
|
56
|
+
if (lineWidth > maxLineWidth) {
|
57
|
+
longestLine = line;
|
58
|
+
maxLineWidth = lineWidth;
|
59
|
+
}
|
60
|
+
});
|
61
|
+
return longestLine;
|
62
|
+
};
|
63
|
+
const fontKitFontCache = {};
|
64
|
+
const createFontKitFont = (font, fontName = constants_1.DEFAULT_FONT_NAME) => __awaiter(void 0, void 0, void 0, function* () {
|
65
|
+
var _a;
|
66
|
+
if (fontKitFontCache[fontName]) {
|
67
|
+
return fontKitFontCache[fontName];
|
68
|
+
}
|
69
|
+
let fontData = ((_a = font[fontName]) === null || _a === void 0 ? void 0 : _a.data) || constants_1.DEFAULT_FONT_VALUE;
|
70
|
+
if (typeof fontData === 'string') {
|
71
|
+
fontData = fontData.startsWith('http') ? yield fetch(fontData).then((res) => res.arrayBuffer()) : (0, _1.b64toUint8Array)(fontData);
|
72
|
+
}
|
73
|
+
const fontKitFont = fontkit.create(fontData instanceof buffer_1.Buffer ? fontData : buffer_1.Buffer.from(fontData));
|
74
|
+
fontKitFontCache[fontName] = fontKitFont;
|
75
|
+
return fontKitFont;
|
76
|
+
});
|
77
|
+
const getTextContent = (input, fontKitFont, fontSize, characterSpacingCount) => {
|
78
|
+
const textContentRows = input.split('\n');
|
79
|
+
return textContentRows.length > 1 ? getLongestLine(textContentRows, fontKitFont, fontSize, characterSpacingCount) : input;
|
80
|
+
};
|
81
|
+
const calculateDynamicFontSize = ({ textSchema, font, input }) => __awaiter(void 0, void 0, void 0, function* () {
|
82
|
+
const { fontName, fontSize: _fontSize, dynamicFontSize: dynamicFontSizeSetting, characterSpacing, width } = textSchema;
|
83
|
+
const fontSize = _fontSize || constants_1.DEFAULT_FONT_SIZE;
|
84
|
+
if (!dynamicFontSizeSetting)
|
85
|
+
return fontSize;
|
86
|
+
const characterSpacingCount = characterSpacing !== null && characterSpacing !== void 0 ? characterSpacing : constants_1.DEFAULT_CHARACTER_SPACING;
|
87
|
+
const fontKitFont = yield createFontKitFont(font, fontName);
|
88
|
+
const textContent = getTextContent(input, fontKitFont, fontSize, characterSpacingCount);
|
89
|
+
const textWidth = widthOfTextAtSize(textContent, fontKitFont, fontSize);
|
90
|
+
let dynamicFontSize = fontSize;
|
91
|
+
let textWidthInMm = calculateTextWidthInMm(textContent, textWidth, characterSpacingCount);
|
92
|
+
while (textWidthInMm > width - constants_1.DEFAULT_TOLERANCE && dynamicFontSize > dynamicFontSizeSetting.min) {
|
93
|
+
dynamicFontSize -= constants_1.DEFAULT_FONT_SIZE_ADJUSTMENT;
|
94
|
+
textWidthInMm = calculateTextWidthInMm(textContent, widthOfTextAtSize(textContent, fontKitFont, dynamicFontSize), characterSpacingCount);
|
95
|
+
}
|
96
|
+
while (textWidthInMm < width - constants_1.DEFAULT_TOLERANCE && dynamicFontSize < dynamicFontSizeSetting.max) {
|
97
|
+
dynamicFontSize += constants_1.DEFAULT_FONT_SIZE_ADJUSTMENT;
|
98
|
+
textWidthInMm = calculateTextWidthInMm(textContent, widthOfTextAtSize(textContent, fontKitFont, dynamicFontSize), characterSpacingCount);
|
99
|
+
}
|
100
|
+
return dynamicFontSize;
|
101
|
+
});
|
102
|
+
exports.calculateDynamicFontSize = calculateDynamicFontSize;
|
103
|
+
//# sourceMappingURL=calculateDynamicFontSize.js.map
|
@@ -0,0 +1 @@
|
|
1
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