@pdfme/schemas 5.3.3-dev.5 → 5.3.3

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.
Files changed (32) hide show
  1. package/dist/cjs/__tests__/text.test.js +37 -25
  2. package/dist/cjs/__tests__/text.test.js.map +1 -1
  3. package/dist/cjs/src/multiVariableText/uiRender.js +5 -9
  4. package/dist/cjs/src/multiVariableText/uiRender.js.map +1 -1
  5. package/dist/cjs/src/shapes/rectAndEllipse.js +2 -3
  6. package/dist/cjs/src/shapes/rectAndEllipse.js.map +1 -1
  7. package/dist/cjs/src/text/helper.js +2 -1
  8. package/dist/cjs/src/text/helper.js.map +1 -1
  9. package/dist/cjs/src/text/pdfRender.js +4 -4
  10. package/dist/cjs/src/text/pdfRender.js.map +1 -1
  11. package/dist/cjs/src/text/uiRender.js +14 -7
  12. package/dist/cjs/src/text/uiRender.js.map +1 -1
  13. package/dist/esm/__tests__/text.test.js +37 -25
  14. package/dist/esm/__tests__/text.test.js.map +1 -1
  15. package/dist/esm/src/multiVariableText/uiRender.js +2 -6
  16. package/dist/esm/src/multiVariableText/uiRender.js.map +1 -1
  17. package/dist/esm/src/shapes/rectAndEllipse.js +2 -3
  18. package/dist/esm/src/shapes/rectAndEllipse.js.map +1 -1
  19. package/dist/esm/src/text/helper.js +2 -1
  20. package/dist/esm/src/text/helper.js.map +1 -1
  21. package/dist/esm/src/text/pdfRender.js +4 -4
  22. package/dist/esm/src/text/pdfRender.js.map +1 -1
  23. package/dist/esm/src/text/uiRender.js +14 -7
  24. package/dist/esm/src/text/uiRender.js.map +1 -1
  25. package/dist/types/src/text/helper.d.ts +4 -3
  26. package/dist/types/src/text/uiRender.d.ts +1 -2
  27. package/package.json +1 -1
  28. package/src/multiVariableText/uiRender.ts +2 -7
  29. package/src/shapes/rectAndEllipse.ts +2 -3
  30. package/src/text/helper.ts +6 -3
  31. package/src/text/pdfRender.ts +8 -7
  32. package/src/text/uiRender.ts +14 -7
@@ -1,4 +1,4 @@
1
- import { getDefaultFont, UIRenderProps } from '@pdfme/common';
1
+ import { UIRenderProps } from '@pdfme/common';
2
2
  import { MultiVariableTextSchema } from './types';
3
3
  import {
4
4
  uiRender as parentUiRender,
@@ -6,7 +6,6 @@ import {
6
6
  makeElementPlainTextContentEditable
7
7
  } from '../text/uiRender';
8
8
  import { isEditable } from '../utils';
9
- import { getFontKitFont } from '../text/helper';
10
9
  import { substituteVariables } from './helper';
11
10
 
12
11
  export const uiRender = async (arg: UIRenderProps<MultiVariableTextSchema>) => {
@@ -66,8 +65,6 @@ const formUiRender = async (arg: UIRenderProps<MultiVariableTextSchema>) => {
66
65
  onChange,
67
66
  stopEditing,
68
67
  theme,
69
- _cache,
70
- options,
71
68
  } = arg;
72
69
  const rawText = schema.text;
73
70
 
@@ -79,10 +76,8 @@ const formUiRender = async (arg: UIRenderProps<MultiVariableTextSchema>) => {
79
76
  const variables: Record<string, string> = JSON.parse(value) || {}
80
77
  const variableIndices = getVariableIndices(rawText);
81
78
  const substitutedText = substituteVariables(rawText, variables);
82
- const font = options?.font || getDefaultFont();
83
- const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
84
79
 
85
- const textBlock = buildStyledTextContainer(arg, fontKitFont, substitutedText);
80
+ const textBlock = await buildStyledTextContainer(arg, substitutedText);
86
81
 
87
82
  // Construct content-editable spans for each variable within the string
88
83
  let inVarString = false;
@@ -1,7 +1,6 @@
1
1
  import { Plugin, Schema, mm2pt } from '@pdfme/common';
2
2
  import { HEX_COLOR_PATTERN } from '../constants.js';
3
3
  import { hex2PrintingColor, convertForPdfLayoutProps, createSvgStr } from '../utils.js';
4
- import { toRadians } from '@pdfme/pdf-lib';
5
4
  import { Circle, Square } from 'lucide';
6
5
 
7
6
  interface ShapeSchema extends Schema {
@@ -58,8 +57,8 @@ const shape: Plugin<ShapeSchema> = {
58
57
  });
59
58
  } else if (schema.type === 'rectangle') {
60
59
  page.drawRectangle({
61
- x: position.x + borderWidth * ((1 - Math.sin(toRadians(rotate))) / 2) + Math.tan(toRadians(rotate)) * (Math.PI ** 2),
62
- y: position.y + borderWidth * ((1 + Math.sin(toRadians(rotate))) / 2) + Math.tan(toRadians(rotate)) * (Math.PI ** 2),
60
+ x: position.x + borderWidth / 2,
61
+ y: position.y + borderWidth / 2,
63
62
  width: width - borderWidth,
64
63
  height: height - borderWidth,
65
64
  ...drawOptions,
@@ -221,16 +221,18 @@ export const getSplittedLines = (textLine: string, calcValues: FontWidthCalcValu
221
221
  * Calculating space usage involves splitting lines where they exceed
222
222
  * the box width based on the proposed size.
223
223
  */
224
- export const calculateDynamicFontSize = ({
224
+ export const calculateDynamicFontSize = async ({
225
225
  textSchema,
226
- fontKitFont,
226
+ font,
227
227
  value,
228
228
  startingFontSize,
229
+ _cache,
229
230
  }: {
230
231
  textSchema: TextSchema;
231
- fontKitFont: FontKitFont;
232
+ font: Font;
232
233
  value: string;
233
234
  startingFontSize?: number | undefined;
235
+ _cache: Map<any, any>;
234
236
  }) => {
235
237
  const {
236
238
  fontSize: schemaFontSize,
@@ -245,6 +247,7 @@ export const calculateDynamicFontSize = ({
245
247
  if (dynamicFontSizeSetting.max < dynamicFontSizeSetting.min) return fontSize;
246
248
 
247
249
  const characterSpacing = schemaCharacterSpacing ?? DEFAULT_CHARACTER_SPACING;
250
+ const fontKitFont = await getFontKitFont(textSchema.fontName, font, _cache);
248
251
  const paragraphs = value.split('\n');
249
252
 
250
253
  let dynamicFontSize = fontSize;
@@ -1,5 +1,4 @@
1
1
  import { PDFFont, PDFDocument } from '@pdfme/pdf-lib';
2
- import type { Font as FontKitFont } from 'fontkit';
3
2
  import type { TextSchema } from './types';
4
3
  import {
5
4
  PDFRenderProps,
@@ -61,19 +60,21 @@ const embedAndGetFontObj = async (arg: {
61
60
  return fontObj;
62
61
  };
63
62
 
64
- const getFontProp = ({
63
+ const getFontProp = async ({
65
64
  value,
66
- fontKitFont,
65
+ font,
67
66
  schema,
68
67
  colorType,
68
+ _cache,
69
69
  }: {
70
70
  value: string;
71
- fontKitFont: FontKitFont;
71
+ font: Font;
72
72
  colorType?: ColorType;
73
73
  schema: TextSchema;
74
+ _cache: Map<any, any>;
74
75
  }) => {
75
76
  const fontSize = schema.dynamicFontSize
76
- ? calculateDynamicFontSize({ textSchema: schema, fontKitFont, value })
77
+ ? await calculateDynamicFontSize({ textSchema: schema, font, value, _cache })
77
78
  : schema.fontSize ?? DEFAULT_FONT_SIZE;
78
79
  const color = hex2PrintingColor(schema.fontColor || DEFAULT_FONT_COLOR, colorType);
79
80
 
@@ -93,11 +94,11 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
93
94
 
94
95
  const { font = getDefaultFont(), colorType } = options;
95
96
 
96
- const [pdfFontObj, fontKitFont] = await Promise.all([
97
+ const [pdfFontObj, fontKitFont, fontProp] = await Promise.all([
97
98
  embedAndGetFontObj({ pdfDoc, font, _cache }),
98
99
  getFontKitFont(schema.fontName, font, _cache),
100
+ getFontProp({ value, font, schema, _cache, colorType }),
99
101
  ]);
100
- const fontProp = getFontProp({ value, fontKitFont, schema, colorType });
101
102
 
102
103
  const { fontSize, color, alignment, verticalAlignment, lineHeight, characterSpacing } = fontProp;
103
104
 
@@ -69,8 +69,10 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
69
69
  return text;
70
70
  };
71
71
  const font = options?.font || getDefaultFont();
72
- const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
73
- const textBlock = buildStyledTextContainer(arg, fontKitFont, usePlaceholder ? placeholder : value);
72
+ const [fontKitFont, textBlock] = await Promise.all([
73
+ getFontKitFont(schema.fontName, font, _cache),
74
+ buildStyledTextContainer(arg, usePlaceholder ? placeholder : value),
75
+ ]);
74
76
 
75
77
  const processedText = replaceUnsupportedChars(value, fontKitFont);
76
78
 
@@ -98,16 +100,19 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
98
100
 
99
101
  if (schema.dynamicFontSize) {
100
102
  let dynamicFontSize: undefined | number = undefined;
103
+ const font = options?.font || getDefaultFont();
104
+ const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
101
105
 
102
106
  textBlock.addEventListener('keyup', () => {
103
107
  setTimeout(() => {
104
108
  void (async () => {
105
109
  if (!textBlock.textContent) return;
106
- dynamicFontSize = calculateDynamicFontSize({
110
+ dynamicFontSize = await calculateDynamicFontSize({
107
111
  textSchema: schema,
108
- fontKitFont,
112
+ font,
109
113
  value: getText(textBlock),
110
114
  startingFontSize: dynamicFontSize,
115
+ _cache,
111
116
  });
112
117
  textBlock.style.fontSize = `${dynamicFontSize}pt`;
113
118
 
@@ -150,21 +155,23 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
150
155
  }
151
156
  };
152
157
 
153
- export const buildStyledTextContainer = (arg: UIRenderProps<TextSchema>, fontKitFont: FontKitFont, value: string) => {
158
+ export const buildStyledTextContainer = async (arg: UIRenderProps<TextSchema>, value: string) => {
154
159
  const { schema, rootElement, mode, options, _cache } = arg;
155
160
  const font = options?.font || getDefaultFont();
156
161
 
157
162
  let dynamicFontSize: undefined | number = undefined;
158
163
 
159
164
  if (schema.dynamicFontSize && value) {
160
- dynamicFontSize = calculateDynamicFontSize({
165
+ dynamicFontSize = await calculateDynamicFontSize({
161
166
  textSchema: schema,
162
- fontKitFont,
167
+ font,
163
168
  value,
164
169
  startingFontSize: dynamicFontSize,
170
+ _cache,
165
171
  });
166
172
  }
167
173
 
174
+ const fontKitFont = await getFontKitFont(schema.fontName, font, _cache);
168
175
  // Depending on vertical alignment, we need to move the top or bottom of the font to keep
169
176
  // it within it's defined box and align it with the generated pdf.
170
177
  const { topAdj, bottomAdj } = getBrowserVerticalFontAdjustments(