@pdfme/schemas 4.2.4 → 4.2.5

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 (47) hide show
  1. package/dist/cjs/src/barcodes/propPanel.js +2 -2
  2. package/dist/cjs/src/barcodes/propPanel.js.map +1 -1
  3. package/dist/cjs/src/multiVariableText/propPanel.js +3 -1
  4. package/dist/cjs/src/multiVariableText/propPanel.js.map +1 -1
  5. package/dist/cjs/src/shapes/line.js +1 -1
  6. package/dist/cjs/src/shapes/line.js.map +1 -1
  7. package/dist/cjs/src/shapes/rectAndEllipse.js +2 -2
  8. package/dist/cjs/src/shapes/rectAndEllipse.js.map +1 -1
  9. package/dist/cjs/src/tables/helper.js +4 -4
  10. package/dist/cjs/src/tables/helper.js.map +1 -1
  11. package/dist/cjs/src/tables/propPanel.js +1 -1
  12. package/dist/cjs/src/tables/propPanel.js.map +1 -1
  13. package/dist/cjs/src/text/pdfRender.js +1 -1
  14. package/dist/cjs/src/text/pdfRender.js.map +1 -1
  15. package/dist/cjs/src/text/propPanel.js +6 -3
  16. package/dist/cjs/src/text/propPanel.js.map +1 -1
  17. package/dist/cjs/src/text/uiRender.js +48 -7
  18. package/dist/cjs/src/text/uiRender.js.map +1 -1
  19. package/dist/esm/src/barcodes/propPanel.js +2 -2
  20. package/dist/esm/src/barcodes/propPanel.js.map +1 -1
  21. package/dist/esm/src/multiVariableText/propPanel.js +3 -1
  22. package/dist/esm/src/multiVariableText/propPanel.js.map +1 -1
  23. package/dist/esm/src/shapes/line.js +1 -1
  24. package/dist/esm/src/shapes/line.js.map +1 -1
  25. package/dist/esm/src/shapes/rectAndEllipse.js +2 -2
  26. package/dist/esm/src/shapes/rectAndEllipse.js.map +1 -1
  27. package/dist/esm/src/tables/helper.js +4 -4
  28. package/dist/esm/src/tables/helper.js.map +1 -1
  29. package/dist/esm/src/tables/propPanel.js +1 -1
  30. package/dist/esm/src/tables/propPanel.js.map +1 -1
  31. package/dist/esm/src/text/pdfRender.js +1 -1
  32. package/dist/esm/src/text/pdfRender.js.map +1 -1
  33. package/dist/esm/src/text/propPanel.js +6 -3
  34. package/dist/esm/src/text/propPanel.js.map +1 -1
  35. package/dist/esm/src/text/uiRender.js +48 -7
  36. package/dist/esm/src/text/uiRender.js.map +1 -1
  37. package/dist/types/src/shapes/rectAndEllipse.d.ts +6 -0
  38. package/package.json +1 -1
  39. package/src/barcodes/propPanel.ts +2 -2
  40. package/src/multiVariableText/propPanel.ts +3 -1
  41. package/src/shapes/line.ts +1 -1
  42. package/src/shapes/rectAndEllipse.ts +2 -2
  43. package/src/tables/helper.ts +4 -4
  44. package/src/tables/propPanel.ts +1 -1
  45. package/src/text/pdfRender.ts +1 -1
  46. package/src/text/propPanel.ts +6 -3
  47. package/src/text/uiRender.ts +75 -43
@@ -1,4 +1,5 @@
1
1
  import type * as CSS from 'csstype';
2
+ import type { Font as FontKitFont } from 'fontkit';
2
3
  import { UIRenderProps, getDefaultFont } from '@pdfme/common';
3
4
  import type { TextSchema } from './types';
4
5
  import {
@@ -21,41 +22,79 @@ import {
21
22
  } from './helper.js';
22
23
  import { isEditable } from '../utils.js';
23
24
 
25
+ const replaceUnsupportedChars = (text: string, fontKitFont: FontKitFont): string => {
26
+ const charSupportCache: { [char: string]: boolean } = {};
27
+
28
+ const isCharSupported = (char: string): boolean => {
29
+ if (char in charSupportCache) {
30
+ return charSupportCache[char];
31
+ }
32
+ const isSupported = fontKitFont.hasGlyphForCodePoint(char.codePointAt(0) || 0);
33
+ charSupportCache[char] = isSupported;
34
+ return isSupported;
35
+ };
36
+
37
+ const segments = text.split(/(\r\n|\n|\r)/);
38
+
39
+ return segments
40
+ .map((segment) => {
41
+ if (/\r\n|\n|\r/.test(segment)) {
42
+ return segment;
43
+ }
44
+
45
+ return segment
46
+ .split('')
47
+ .map((char) => {
48
+ if (/\s/.test(char) || char.charCodeAt(0) < 32) {
49
+ return char;
50
+ }
51
+
52
+ return isCharSupported(char) ? char : '〿';
53
+ })
54
+ .join('');
55
+ })
56
+ .join('');
57
+ };
58
+
24
59
  export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
25
- const {
26
- value,
27
- schema,
28
- mode,
29
- onChange,
30
- stopEditing,
31
- tabIndex,
32
- placeholder,
33
- options,
34
- _cache,
35
- } = arg;
60
+ const { value, schema, mode, onChange, stopEditing, tabIndex, placeholder, options, _cache } =
61
+ arg;
36
62
  const usePlaceholder = isEditable(mode, schema) && placeholder && !value;
63
+ const getText = (element: HTMLDivElement) => {
64
+ let text = element.innerText;
65
+ if (text.endsWith('\n')) {
66
+ // contenteditable adds additional newline char retrieved with innerText
67
+ text = text.slice(0, -1);
68
+ }
69
+ return text;
70
+ };
71
+ const font = options?.font || getDefaultFont();
72
+ const [fontKitFont, textBlock] = await Promise.all([
73
+ getFontKitFont(schema.fontName, font, _cache),
74
+ buildStyledTextContainer(arg, usePlaceholder ? placeholder : value),
75
+ ]);
37
76
 
38
- const textBlock = await buildStyledTextContainer(arg, usePlaceholder ? placeholder : value);
77
+ const processedText = replaceUnsupportedChars(value, fontKitFont);
39
78
 
40
79
  if (!isEditable(mode, schema)) {
41
80
  // Read-only mode
42
- textBlock.innerHTML = value
43
- .split('')
44
- .map(
45
- (l: string, i: number) =>
46
- `<span style="letter-spacing:${
47
- String(value).length === i + 1 ? 0 : 'inherit'
48
- };">${l}</span>`
49
- )
50
- .join('');
81
+ textBlock.innerHTML = processedText
82
+ .split('')
83
+ .map(
84
+ (l, i) =>
85
+ `<span style="letter-spacing:${
86
+ String(value).length === i + 1 ? 0 : 'inherit'
87
+ };">${l}</span>`
88
+ )
89
+ .join('');
51
90
  return;
52
91
  }
53
92
 
54
93
  makeElementPlainTextContentEditable(textBlock);
55
94
  textBlock.tabIndex = tabIndex || 0;
56
- textBlock.innerText = value;
95
+ textBlock.innerText = mode === 'designer' ? value : processedText;
57
96
  textBlock.addEventListener('blur', (e: Event) => {
58
- onChange && onChange({ key: 'content', value: (e.target as HTMLDivElement).textContent });
97
+ onChange && onChange({ key: 'content', value: getText(e.target as HTMLDivElement) });
59
98
  stopEditing && stopEditing();
60
99
  });
61
100
 
@@ -71,19 +110,18 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
71
110
  dynamicFontSize = await calculateDynamicFontSize({
72
111
  textSchema: schema,
73
112
  font,
74
- value: textBlock.textContent,
113
+ value: getText(textBlock),
75
114
  startingFontSize: dynamicFontSize,
76
115
  _cache,
77
116
  });
78
117
  textBlock.style.fontSize = `${dynamicFontSize}pt`;
79
118
 
80
- const { topAdj: newTopAdj, bottomAdj: newBottomAdj } =
81
- getBrowserVerticalFontAdjustments(
82
- fontKitFont,
83
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
84
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
85
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
86
- );
119
+ const { topAdj: newTopAdj, bottomAdj: newBottomAdj } = getBrowserVerticalFontAdjustments(
120
+ fontKitFont,
121
+ dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
122
+ schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
123
+ schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
124
+ );
87
125
  textBlock.style.paddingTop = `${newTopAdj}px`;
88
126
  textBlock.style.marginBottom = `${newBottomAdj}px`;
89
127
  })();
@@ -118,13 +156,7 @@ export const uiRender = async (arg: UIRenderProps<TextSchema>) => {
118
156
  };
119
157
 
120
158
  export const buildStyledTextContainer = async (arg: UIRenderProps<TextSchema>, value: string) => {
121
- const {
122
- schema,
123
- rootElement,
124
- mode,
125
- options,
126
- _cache,
127
- } = arg;
159
+ const { schema, rootElement, mode, options, _cache } = arg;
128
160
  const font = options?.font || getDefaultFont();
129
161
 
130
162
  let dynamicFontSize: undefined | number = undefined;
@@ -143,10 +175,10 @@ export const buildStyledTextContainer = async (arg: UIRenderProps<TextSchema>, v
143
175
  // Depending on vertical alignment, we need to move the top or bottom of the font to keep
144
176
  // it within it's defined box and align it with the generated pdf.
145
177
  const { topAdj, bottomAdj } = getBrowserVerticalFontAdjustments(
146
- fontKitFont,
147
- dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
148
- schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
149
- schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
178
+ fontKitFont,
179
+ dynamicFontSize ?? schema.fontSize ?? DEFAULT_FONT_SIZE,
180
+ schema.lineHeight ?? DEFAULT_LINE_HEIGHT,
181
+ schema.verticalAlignment ?? DEFAULT_VERTICAL_ALIGNMENT
150
182
  );
151
183
 
152
184
  const topAdjustment = topAdj.toString();
@@ -231,7 +263,7 @@ export const makeElementPlainTextContentEditable = (element: HTMLElement) => {
231
263
  selection.getRangeAt(0).insertNode(document.createTextNode(paste || ''));
232
264
  selection.collapseToEnd();
233
265
  });
234
- }
266
+ };
235
267
 
236
268
  const mapVerticalAlignToFlex = (verticalAlignmentValue: string | undefined) => {
237
269
  switch (verticalAlignmentValue) {