UNPKG

@pdfme/pdf-lib

Version:

Create and modify PDF files with JavaScript

134 lines 5.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../../../src/index"); const layout_1 = require("../../../src/api/text/layout"); const MIN_FONT_SIZE = 4; const MAX_FONT_SIZE = 500; describe(`layoutMultilineText`, () => { it('should layout the text on one line when it fits near-perfectly', async () => { const pdfDoc = await index_1.PDFDocument.create(); const font = await pdfDoc.embedFont(index_1.StandardFonts.Helvetica); const alignment = index_1.TextAlignment.Left; const padding = 0; const borderWidth = 0; const text = 'Super Mario Bros.'; for (let fontSize = MIN_FONT_SIZE; fontSize <= MAX_FONT_SIZE; fontSize++) { const height = font.heightAtSize(fontSize) - (borderWidth + padding) * 2; const width = font.widthOfTextAtSize(text, fontSize) - (borderWidth + padding) * 2; const bounds = { x: borderWidth + padding, y: borderWidth + padding, width, height, }; const multilineTextLayout = (0, layout_1.layoutMultilineText)(text, { alignment, bounds, font, }); expect(multilineTextLayout.lines.length).toStrictEqual(1); } }); it('should layout the text on one line when it fits comfortably', async () => { const pdfDoc = await index_1.PDFDocument.create(); const font = await pdfDoc.embedFont(index_1.StandardFonts.Helvetica); const alignment = index_1.TextAlignment.Left; const padding = 0; const borderWidth = 0; const text = 'Super Mario Bros.'; const fontSize = 12; const height = font.heightAtSize(fontSize) - (borderWidth + padding) * 2; // Bounds width twice that of the text const width = (font.widthOfTextAtSize(text, fontSize) - (borderWidth + padding) * 2) * 2; const bounds = { x: borderWidth + padding, y: borderWidth + padding, width, height, }; const multilineTextLayout = (0, layout_1.layoutMultilineText)(text, { alignment, bounds, font, }); expect(multilineTextLayout.lines.length).toStrictEqual(1); }); it('should layout the text on multiple lines when it does not fit horizontally but there is space vertically', async () => { const pdfDoc = await index_1.PDFDocument.create(); const font = await pdfDoc.embedFont(index_1.StandardFonts.Helvetica); const alignment = index_1.TextAlignment.Left; const padding = 0; const borderWidth = 0; const text = 'Super Mario Bros.'; for (let fontSize = MIN_FONT_SIZE; fontSize <= MAX_FONT_SIZE; fontSize++) { // Height twice that of the text const height = (font.heightAtSize(fontSize) - (borderWidth + padding) * 2) * 2; // Width half that of the text const width = (font.widthOfTextAtSize(text, fontSize) - (borderWidth + padding) * 2) / 2; const bounds = { x: borderWidth + padding, y: borderWidth + padding, width, height, }; const multilineTextLayout = (0, layout_1.layoutMultilineText)(text, { alignment, bounds, font, }); expect(multilineTextLayout.lines.length).toStrictEqual(2); } }); it('should never exceed the maximum font size', async () => { const pdfDoc = await index_1.PDFDocument.create(); const font = await pdfDoc.embedFont(index_1.StandardFonts.Helvetica); const alignment = index_1.TextAlignment.Left; const padding = 0; const borderWidth = 0; const text = 'Super Mario Bros.'; const height = Number.MAX_VALUE; const width = Number.MAX_VALUE; const bounds = { x: borderWidth + padding, y: borderWidth + padding, width, height, }; const multilineTextLayout = (0, layout_1.layoutMultilineText)(text, { alignment, bounds, font, }); expect(multilineTextLayout.lines.length).toStrictEqual(1); expect(multilineTextLayout.fontSize).toStrictEqual(MAX_FONT_SIZE); }); it('should respect empty lines', async () => { const pdfDoc = await index_1.PDFDocument.create(); const font = await pdfDoc.embedFont(index_1.StandardFonts.Helvetica); const alignment = index_1.TextAlignment.Left; const padding = 0; const borderWidth = 0; const lines = ['Super Mario Bros.', '', 'Boop']; const text = lines.join('\n'); for (let fontSize = MIN_FONT_SIZE; fontSize <= MAX_FONT_SIZE; fontSize++) { const height = font.heightAtSize(fontSize) - (borderWidth + padding) * 2; const width = font.widthOfTextAtSize(lines[0], fontSize) - (borderWidth + padding) * 2; const bounds = { x: borderWidth + padding, y: borderWidth + padding, width, height, }; const multilineTextLayout = (0, layout_1.layoutMultilineText)(text, { alignment, bounds, font, }); expect(multilineTextLayout.lines.length).toStrictEqual(3); } }); }); //# sourceMappingURL=layout.spec.js.map