diff --git a/packages/common/package.json b/packages/common/package.json index 418896b6..a78fa598 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -46,8 +46,8 @@ "prune": "ts-prune src" }, "dependencies": { - "zod": "^3.20.2", - "fontkit": "^2.0.2" + "fontkit": "^2.0.2", + "zod": "^3.20.2" }, "jest": { "resolver": "ts-jest-resolver", diff --git a/packages/common/src/helpers/calculateDynamicFontSize.ts b/packages/common/src/helpers/calculateDynamicFontSize.ts index 8dc3e207..6b626bcc 100644 --- a/packages/common/src/helpers/calculateDynamicFontSize.ts +++ b/packages/common/src/helpers/calculateDynamicFontSize.ts @@ -53,8 +53,6 @@ export const calculateDynamicFontSize: DynamicFontSize = async (activeSchema, fo let textContent; let schemaFontSize = baseFontSizeInPixels; let dynamicFontSize = baseFontSizeInPixels; - - // Detect if multiline text and get the width of each line in mm but only return the largest width const textContentRows = data.split('\n'); const textContentRowMaxWidth = ( diff --git a/packages/common/src/helpers/calculateTextWidthInMm.ts b/packages/common/src/helpers/calculateTextWidthInMm.ts index 8c27d72d..dde56f92 100644 --- a/packages/common/src/helpers/calculateTextWidthInMm.ts +++ b/packages/common/src/helpers/calculateTextWidthInMm.ts @@ -1,4 +1,5 @@ import { PDFFont } from '@pdfme/pdf-lib'; +import * as fontkit from 'fontkit'; import { calculateCharacterSpacing } from './calculateCharacterSpacing'; import { DEFAULT_PT_TO_MM_RATIO } from '../constants'; @@ -7,6 +8,7 @@ type CalculateTextWidthInMm = ( textFontSize: number, textFontFamily: PDFFont, textCharacterSpacing: number + // textFontData?: ArrayBufferLike ) => number; export const calculateTextWidthInMm: CalculateTextWidthInMm = ( @@ -14,10 +16,31 @@ export const calculateTextWidthInMm: CalculateTextWidthInMm = ( textFontSize, textFontFamily, textCharacterSpacing + // textFontData ) => { const characterSpacingWidth = calculateCharacterSpacing(textContent, textCharacterSpacing); - const textContentWidthInPt = textFontFamily.widthOfTextAtSize(textContent, textFontSize) + characterSpacingWidth; + const textContentWidthInPt = + textFontFamily.widthOfTextAtSize(textContent, textFontSize) + characterSpacingWidth; const textContentWidthInMm = textContentWidthInPt * DEFAULT_PT_TO_MM_RATIO; + // Calculate Text Width using Fontkit + // if (textFontData) { + // let font = fontkit.create(new Uint8Array(textFontData)); + + // console.log(font, font.familyName); + + // // Set the font size + // const fontSizeTest = 16; + // const fontText = 'Hello, World! Hello, World!'; + // let widthTest = 0; + + // for (let character of fontText) { + // const glyph = font.glyphForCodePoint(character.codePointAt(0)); + // widthTest += (glyph.advanceWidth / font.unitsPerEm) * fontSizeTest; + // } + + // console.log(`Width of "${fontText}": ${widthTest} in pts`); + // } + return textContentWidthInMm; };