@pdfme/schemas 6.0.3 → 6.0.4-dev.1

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 (116) hide show
  1. package/dist/barcodes/constants.d.ts +4 -0
  2. package/dist/barcodes/helper.d.ts +21 -0
  3. package/dist/barcodes/index.d.ts +4 -0
  4. package/dist/barcodes/pdfRender.d.ts +3 -0
  5. package/dist/barcodes/propPanel.d.ts +3 -0
  6. package/{src/barcodes/types.ts → dist/barcodes/types.d.ts} +5 -7
  7. package/dist/barcodes/uiRender.d.ts +3 -0
  8. package/dist/builtins-CgaZ0UX3.js +613 -0
  9. package/dist/builtins-CgaZ0UX3.js.map +1 -0
  10. package/dist/builtins.d.ts +4 -0
  11. package/dist/builtins.js +2 -0
  12. package/dist/checkbox/index.d.ts +6 -0
  13. package/dist/constants.d.ts +2 -0
  14. package/dist/date/date.d.ts +2 -0
  15. package/dist/date/dateTime.d.ts +2 -0
  16. package/dist/date/helper.d.ts +20 -0
  17. package/dist/date/time.d.ts +2 -0
  18. package/dist/date/types.d.ts +17 -0
  19. package/dist/dynamicTemplate-D_DHR3-X.js +1128 -0
  20. package/dist/dynamicTemplate-D_DHR3-X.js.map +1 -0
  21. package/dist/graphics/image.d.ts +5 -0
  22. package/dist/graphics/imagehelper.d.ts +4 -0
  23. package/dist/graphics/signature.d.ts +4 -0
  24. package/dist/graphics/svg.d.ts +4 -0
  25. package/{src/index.ts → dist/index.d.ts} +1 -22
  26. package/dist/index.js +5383 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/multiVariableText/helper.d.ts +3 -0
  29. package/dist/multiVariableText/index.d.ts +4 -0
  30. package/dist/multiVariableText/pdfRender.d.ts +3 -0
  31. package/dist/multiVariableText/propPanel.d.ts +3 -0
  32. package/{src/multiVariableText/types.ts → dist/multiVariableText/types.d.ts} +2 -3
  33. package/dist/multiVariableText/uiRender.d.ts +3 -0
  34. package/dist/multiVariableText/variables.d.ts +10 -0
  35. package/dist/radioGroup/index.d.ts +7 -0
  36. package/dist/sanitize.d.ts +1 -0
  37. package/dist/select/index.d.ts +7 -0
  38. package/dist/shapes/line.d.ts +6 -0
  39. package/dist/shapes/rectAndEllipse.d.ts +11 -0
  40. package/dist/tables/cell.d.ts +4 -0
  41. package/dist/tables/classes.d.ts +69 -0
  42. package/dist/tables/dynamicTemplate.d.ts +7 -0
  43. package/dist/tables/helper.d.ts +265 -0
  44. package/dist/tables/index.d.ts +4 -0
  45. package/dist/tables/pdfRender.d.ts +3 -0
  46. package/dist/tables/propPanel.d.ts +3 -0
  47. package/dist/tables/tableHelper.d.ts +10 -0
  48. package/dist/tables/types.d.ts +88 -0
  49. package/dist/tables/uiRender.d.ts +3 -0
  50. package/dist/tables.js +2 -0
  51. package/dist/text/constants.d.ts +23 -0
  52. package/dist/text/extraFormatter.d.ts +25 -0
  53. package/dist/text/helper.d.ts +40 -0
  54. package/dist/text/icons/index.d.ts +9 -0
  55. package/dist/text/index.d.ts +4 -0
  56. package/dist/text/pdfRender.d.ts +3 -0
  57. package/dist/text/propPanel.d.ts +3 -0
  58. package/dist/text/types.d.ts +28 -0
  59. package/dist/text/uiRender.d.ts +11 -0
  60. package/dist/utils.d.ts +40 -0
  61. package/dist/utils.js +215 -0
  62. package/dist/utils.js.map +1 -0
  63. package/package.json +5 -1
  64. package/src/barcodes/constants.ts +0 -20
  65. package/src/barcodes/helper.ts +0 -187
  66. package/src/barcodes/index.ts +0 -23
  67. package/src/barcodes/pdfRender.ts +0 -37
  68. package/src/barcodes/propPanel.ts +0 -249
  69. package/src/barcodes/uiRender.ts +0 -94
  70. package/src/builtins.ts +0 -8
  71. package/src/checkbox/index.ts +0 -70
  72. package/src/constants.ts +0 -2
  73. package/src/date/date.ts +0 -9
  74. package/src/date/dateTime.ts +0 -9
  75. package/src/date/helper.ts +0 -544
  76. package/src/date/time.ts +0 -9
  77. package/src/date/types.ts +0 -19
  78. package/src/graphics/image.ts +0 -201
  79. package/src/graphics/imagehelper.ts +0 -156
  80. package/src/graphics/signature.ts +0 -136
  81. package/src/graphics/svg.ts +0 -121
  82. package/src/multiVariableText/helper.ts +0 -65
  83. package/src/multiVariableText/index.ts +0 -16
  84. package/src/multiVariableText/pdfRender.ts +0 -21
  85. package/src/multiVariableText/propPanel.ts +0 -169
  86. package/src/multiVariableText/uiRender.ts +0 -157
  87. package/src/multiVariableText/variables.ts +0 -63
  88. package/src/radioGroup/index.ts +0 -115
  89. package/src/sanitize.ts +0 -50
  90. package/src/select/index.ts +0 -205
  91. package/src/shapes/line.ts +0 -94
  92. package/src/shapes/rectAndEllipse.ts +0 -152
  93. package/src/tables/cell.ts +0 -152
  94. package/src/tables/classes.ts +0 -402
  95. package/src/tables/dynamicTemplate.ts +0 -88
  96. package/src/tables/helper.ts +0 -216
  97. package/src/tables/index.ts +0 -15
  98. package/src/tables/pdfRender.ts +0 -144
  99. package/src/tables/propPanel.ts +0 -111
  100. package/src/tables/tableHelper.ts +0 -289
  101. package/src/tables/types.ts +0 -87
  102. package/src/tables/uiRender.ts +0 -436
  103. package/src/text/constants.ts +0 -104
  104. package/src/text/extraFormatter.ts +0 -83
  105. package/src/text/helper.ts +0 -573
  106. package/src/text/icons/index.ts +0 -30
  107. package/src/text/index.ts +0 -16
  108. package/src/text/pdfRender.ts +0 -240
  109. package/src/text/propPanel.ts +0 -184
  110. package/src/text/types.ts +0 -30
  111. package/src/text/uiRender.ts +0 -292
  112. package/src/utils.ts +0 -354
  113. package/tsconfig.build.json +0 -14
  114. package/tsconfig.json +0 -16
  115. package/vite.config.mts +0 -51
  116. /package/{src/tables.ts → dist/tables.d.ts} +0 -0
@@ -1,573 +0,0 @@
1
- import * as fontkit from 'fontkit';
2
- import type { Font as FontKitFont } from 'fontkit';
3
- import {
4
- b64toUint8Array,
5
- mm2pt,
6
- pt2mm,
7
- pt2px,
8
- Font,
9
- getFallbackFontName,
10
- getDefaultFont,
11
- DEFAULT_FONT_NAME,
12
- isUrlSafeToFetch,
13
- } from '@pdfme/common';
14
- import { Buffer } from 'buffer';
15
- import type { TextSchema, FontWidthCalcValues } from './types.js';
16
- import {
17
- DEFAULT_FONT_SIZE,
18
- DEFAULT_CHARACTER_SPACING,
19
- DEFAULT_LINE_HEIGHT,
20
- FONT_SIZE_ADJUSTMENT,
21
- DEFAULT_DYNAMIC_FIT,
22
- DYNAMIC_FIT_HORIZONTAL,
23
- DYNAMIC_FIT_VERTICAL,
24
- VERTICAL_ALIGN_TOP,
25
- LINE_END_FORBIDDEN_CHARS,
26
- LINE_START_FORBIDDEN_CHARS,
27
- } from './constants.js';
28
-
29
- export const getBrowserVerticalFontAdjustments = (
30
- fontKitFont: FontKitFont,
31
- fontSize: number,
32
- lineHeight: number,
33
- verticalAlignment: string,
34
- ) => {
35
- const { ascent, descent, unitsPerEm } = fontKitFont;
36
-
37
- // Fonts have a designed line height that the browser renders when using `line-height: normal`
38
- const fontBaseLineHeight = (ascent - descent) / unitsPerEm;
39
-
40
- // For vertical alignment top
41
- // To achieve consistent positioning between browser and PDF, we apply the difference between
42
- // the font's actual height and the font size in pixels.
43
- // Browsers middle the font within this height, so we only need half of it to apply to the top.
44
- // This means the font renders a bit lower in the browser, but achieves PDF alignment
45
- const topAdjustment = (fontBaseLineHeight * fontSize - fontSize) / 2;
46
-
47
- if (verticalAlignment === VERTICAL_ALIGN_TOP) {
48
- return { topAdj: pt2px(topAdjustment), bottomAdj: 0 };
49
- }
50
-
51
- // For vertical alignment bottom and middle
52
- // When browsers render text in a non-form element (such as a <div>), some of the text may be
53
- // lowered below and outside the containing element if the line height used is less than
54
- // the base line-height of the font.
55
- // This behaviour does not happen in a <textarea> though, so we need to adjust the positioning
56
- // for consistency between editing and viewing to stop text jumping up and down.
57
- // This portion of text is half of the difference between the base line height and the used
58
- // line height. If using the same or higher line-height than the base font, then line-height
59
- // takes over in the browser and this adjustment is not needed.
60
- // Unlike the top adjustment - this is only driven by browser behaviour, not PDF alignment.
61
- let bottomAdjustment = 0;
62
- if (lineHeight < fontBaseLineHeight) {
63
- bottomAdjustment = ((fontBaseLineHeight - lineHeight) * fontSize) / 2;
64
- }
65
-
66
- return { topAdj: 0, bottomAdj: pt2px(bottomAdjustment) };
67
- };
68
-
69
- export const getFontDescentInPt = (fontKitFont: FontKitFont, fontSize: number) => {
70
- const { descent, unitsPerEm } = fontKitFont;
71
-
72
- return (descent / unitsPerEm) * fontSize;
73
- };
74
-
75
- export const heightOfFontAtSize = (fontKitFont: FontKitFont, fontSize: number) => {
76
- const { ascent, descent, bbox, unitsPerEm } = fontKitFont;
77
-
78
- const scale = 1000 / unitsPerEm;
79
- const yTop = (ascent || bbox.maxY) * scale;
80
- const yBottom = (descent || bbox.minY) * scale;
81
-
82
- let height = yTop - yBottom;
83
- height -= Math.abs(descent * scale) || 0;
84
-
85
- return (height / 1000) * fontSize;
86
- };
87
-
88
- const calculateCharacterSpacing = (textContent: string, textCharacterSpacing: number) => {
89
- return (textContent.length - 1) * textCharacterSpacing;
90
- };
91
-
92
- export const widthOfTextAtSize = (
93
- text: string,
94
- fontKitFont: FontKitFont,
95
- fontSize: number,
96
- characterSpacing: number,
97
- ) => {
98
- const { glyphs } = fontKitFont.layout(text);
99
- const scale = 1000 / fontKitFont.unitsPerEm;
100
- const standardWidth =
101
- glyphs.reduce((totalWidth, glyph) => totalWidth + glyph.advanceWidth * scale, 0) *
102
- (fontSize / 1000);
103
- return standardWidth + calculateCharacterSpacing(text, characterSpacing);
104
- };
105
-
106
- const getFallbackFont = (font: Font) => {
107
- const fallbackFontName = getFallbackFontName(font);
108
- return font[fallbackFontName];
109
- };
110
-
111
- const getCacheKey = (fontName: string) => `getFontKitFont-${fontName}`;
112
-
113
- export const fetchRemoteFontData = async (url: string): Promise<ArrayBuffer> => {
114
- if (!isUrlSafeToFetch(url)) {
115
- throw Error(
116
- '[@pdfme/schemas] Invalid or unsafe URL for font data. Only http: and https: URLs pointing to public hosts are allowed.',
117
- );
118
- }
119
-
120
- try {
121
- const response = await fetch(url);
122
- if (!response.ok) {
123
- throw new Error(`HTTP ${response.status}`);
124
- }
125
-
126
- return await response.arrayBuffer();
127
- } catch (error) {
128
- const reason = error instanceof Error ? error.message : String(error);
129
- throw Error(`[@pdfme/schemas] Failed to fetch remote font data from ${url}. ${reason}`);
130
- }
131
- };
132
-
133
- export const getFontKitFont = async (
134
- fontName: string | undefined,
135
- font: Font,
136
- _cache: Map<string | number, fontkit.Font>,
137
- ) => {
138
- const fntNm = fontName || getFallbackFontName(font);
139
- const cacheKey = getCacheKey(fntNm);
140
- if (_cache.has(cacheKey)) {
141
- return _cache.get(cacheKey) as fontkit.Font;
142
- }
143
-
144
- const currentFont = font[fntNm] || getFallbackFont(font) || getDefaultFont()[DEFAULT_FONT_NAME];
145
- let fontData = currentFont.data;
146
- if (typeof fontData === 'string') {
147
- if (fontData.startsWith('http')) {
148
- fontData = await fetchRemoteFontData(fontData);
149
- } else {
150
- fontData = b64toUint8Array(fontData);
151
- }
152
- }
153
-
154
- // Convert fontData to Buffer if it's not already a Buffer
155
- let fontDataBuffer: Buffer;
156
- if (fontData instanceof Buffer) {
157
- fontDataBuffer = fontData;
158
- } else {
159
- fontDataBuffer = Buffer.from(fontData as ArrayBufferLike);
160
- }
161
- const fontKitFont = fontkit.create(fontDataBuffer) as fontkit.Font;
162
- _cache.set(cacheKey, fontKitFont);
163
-
164
- return fontKitFont;
165
- };
166
-
167
- const isTextExceedingBoxWidth = (text: string, calcValues: FontWidthCalcValues) => {
168
- const { font, fontSize, characterSpacing, boxWidthInPt } = calcValues;
169
- const textWidth = widthOfTextAtSize(text, font, fontSize, characterSpacing);
170
- return textWidth > boxWidthInPt;
171
- };
172
-
173
- /**
174
- * Incrementally checks the current line for its real length
175
- * and returns the position where it exceeds the box width.
176
- * Returns `null` to indicate if textLine is shorter than the available box.
177
- */
178
- const getOverPosition = (textLine: string, calcValues: FontWidthCalcValues) => {
179
- for (let i = 0; i <= textLine.length; i++) {
180
- if (isTextExceedingBoxWidth(textLine.slice(0, i + 1), calcValues)) {
181
- return i;
182
- }
183
- }
184
-
185
- return null;
186
- };
187
-
188
- /**
189
- * Line breakable chars depend on the language and writing system.
190
- * Western writing systems typically use spaces and hyphens as line breakable chars.
191
- * Other writing systems often break on word boundaries so the following
192
- * does not negatively impact them.
193
- * However, this might need to be revisited for broader language support.
194
- */
195
- const isLineBreakableChar = (char: string) => {
196
- const lineBreakableChars = [' ', '-', '\u2014', '\u2013'];
197
- return lineBreakableChars.includes(char);
198
- };
199
-
200
- /**
201
- * Gets the position of the split. Splits the exceeding line at
202
- * the last breakable char prior to it exceeding the bounding box width.
203
- */
204
- const getSplitPosition = (textLine: string, calcValues: FontWidthCalcValues) => {
205
- const overPos = getOverPosition(textLine, calcValues);
206
- if (overPos === null) return textLine.length; // input line is shorter than the available space
207
-
208
- if (textLine[overPos] === ' ') {
209
- // if the character immediately beyond the boundary is a space, split
210
- return overPos;
211
- }
212
-
213
- let overPosTmp = overPos - 1;
214
- while (overPosTmp >= 0) {
215
- if (isLineBreakableChar(textLine[overPosTmp])) {
216
- return overPosTmp + 1;
217
- }
218
- overPosTmp--;
219
- }
220
-
221
- // For very long lines with no breakable chars use the original overPos
222
- return overPos;
223
- };
224
-
225
- /**
226
- * Recursively splits the line at getSplitPosition.
227
- * If there is some leftover, split the rest again in the same manner.
228
- */
229
- export const getSplittedLines = (textLine: string, calcValues: FontWidthCalcValues): string[] => {
230
- const splitPos = getSplitPosition(textLine, calcValues);
231
- const splittedLine = textLine.substring(0, splitPos).trimEnd();
232
- const rest = textLine.substring(splitPos).trimStart();
233
-
234
- if (rest === textLine) {
235
- // if we went so small that we want to split on the first char
236
- // then end recursion to avoid infinite loop
237
- return [textLine];
238
- }
239
-
240
- if (rest.length === 0) {
241
- // end recursion if there is no leftover
242
- return [splittedLine];
243
- }
244
-
245
- return [splittedLine, ...getSplittedLines(rest, calcValues)];
246
- };
247
-
248
- /**
249
- * If using dynamic font size, iteratively increment or decrement the
250
- * font size to fit the containing box.
251
- * Calculating space usage involves splitting lines where they exceed
252
- * the box width based on the proposed size.
253
- */
254
- export const calculateDynamicFontSize = ({
255
- textSchema,
256
- fontKitFont,
257
- value,
258
- startingFontSize,
259
- }: {
260
- textSchema: TextSchema;
261
- fontKitFont: FontKitFont;
262
- value: string;
263
- startingFontSize?: number | undefined;
264
- }) => {
265
- const {
266
- fontSize: schemaFontSize,
267
- dynamicFontSize: dynamicFontSizeSetting,
268
- characterSpacing: schemaCharacterSpacing,
269
- width: boxWidth,
270
- height: boxHeight,
271
- lineHeight = DEFAULT_LINE_HEIGHT,
272
- } = textSchema;
273
- const fontSize = startingFontSize || schemaFontSize || DEFAULT_FONT_SIZE;
274
- if (!dynamicFontSizeSetting) return fontSize;
275
- if (dynamicFontSizeSetting.max < dynamicFontSizeSetting.min) return fontSize;
276
-
277
- const characterSpacing = schemaCharacterSpacing ?? DEFAULT_CHARACTER_SPACING;
278
- const paragraphs = value.split('\n');
279
-
280
- let dynamicFontSize = fontSize;
281
- if (dynamicFontSize < dynamicFontSizeSetting.min) {
282
- dynamicFontSize = dynamicFontSizeSetting.min;
283
- } else if (dynamicFontSize > dynamicFontSizeSetting.max) {
284
- dynamicFontSize = dynamicFontSizeSetting.max;
285
- }
286
- const dynamicFontFit = dynamicFontSizeSetting.fit ?? DEFAULT_DYNAMIC_FIT;
287
-
288
- const calculateConstraints = (size: number) => {
289
- let totalWidthInMm = 0;
290
- let totalHeightInMm = 0;
291
-
292
- const boxWidthInPt = mm2pt(boxWidth);
293
- const firstLineTextHeight = heightOfFontAtSize(fontKitFont, size);
294
- const firstLineHeightInMm = pt2mm(firstLineTextHeight * lineHeight);
295
- const otherRowHeightInMm = pt2mm(size * lineHeight);
296
-
297
- paragraphs.forEach((paragraph, paraIndex) => {
298
- const lines = getSplittedLinesBySegmenter(paragraph, {
299
- font: fontKitFont,
300
- fontSize: size,
301
- characterSpacing,
302
- boxWidthInPt,
303
- });
304
-
305
- lines.forEach((line, lineIndex) => {
306
- if (dynamicFontFit === DYNAMIC_FIT_VERTICAL) {
307
- // For vertical fit we want to consider the width of text lines where we detect a split
308
- const textWidth = widthOfTextAtSize(
309
- line.replace('\n', ''),
310
- fontKitFont,
311
- size,
312
- characterSpacing,
313
- );
314
- const textWidthInMm = pt2mm(textWidth);
315
- totalWidthInMm = Math.max(totalWidthInMm, textWidthInMm);
316
- }
317
-
318
- if (paraIndex + lineIndex === 0) {
319
- totalHeightInMm += firstLineHeightInMm;
320
- } else {
321
- totalHeightInMm += otherRowHeightInMm;
322
- }
323
- });
324
- if (dynamicFontFit === DYNAMIC_FIT_HORIZONTAL) {
325
- // For horizontal fit we want to consider the line's width 'unsplit'
326
- const textWidth = widthOfTextAtSize(paragraph, fontKitFont, size, characterSpacing);
327
- const textWidthInMm = pt2mm(textWidth);
328
- totalWidthInMm = Math.max(totalWidthInMm, textWidthInMm);
329
- }
330
- });
331
-
332
- return { totalWidthInMm, totalHeightInMm };
333
- };
334
-
335
- const shouldFontGrowToFit = (totalWidthInMm: number, totalHeightInMm: number) => {
336
- if (dynamicFontSize >= dynamicFontSizeSetting.max) {
337
- return false;
338
- }
339
- if (dynamicFontFit === DYNAMIC_FIT_HORIZONTAL) {
340
- return totalWidthInMm < boxWidth;
341
- }
342
- return totalHeightInMm < boxHeight;
343
- };
344
-
345
- const shouldFontShrinkToFit = (totalWidthInMm: number, totalHeightInMm: number) => {
346
- if (dynamicFontSize <= dynamicFontSizeSetting.min || dynamicFontSize <= 0) {
347
- return false;
348
- }
349
- return totalWidthInMm > boxWidth || totalHeightInMm > boxHeight;
350
- };
351
-
352
- let { totalWidthInMm, totalHeightInMm } = calculateConstraints(dynamicFontSize);
353
-
354
- // Attempt to increase the font size up to desired fit
355
- while (shouldFontGrowToFit(totalWidthInMm, totalHeightInMm)) {
356
- dynamicFontSize += FONT_SIZE_ADJUSTMENT;
357
- const { totalWidthInMm: newWidth, totalHeightInMm: newHeight } =
358
- calculateConstraints(dynamicFontSize);
359
-
360
- if (newHeight < boxHeight) {
361
- totalWidthInMm = newWidth;
362
- totalHeightInMm = newHeight;
363
- } else {
364
- dynamicFontSize -= FONT_SIZE_ADJUSTMENT;
365
- break;
366
- }
367
- }
368
-
369
- // Attempt to decrease the font size down to desired fit
370
- while (shouldFontShrinkToFit(totalWidthInMm, totalHeightInMm)) {
371
- dynamicFontSize -= FONT_SIZE_ADJUSTMENT;
372
- ({ totalWidthInMm, totalHeightInMm } = calculateConstraints(dynamicFontSize));
373
- }
374
-
375
- return dynamicFontSize;
376
- };
377
-
378
- export const splitTextToSize = (arg: {
379
- value: string;
380
- characterSpacing: number;
381
- boxWidthInPt: number;
382
- fontSize: number;
383
- fontKitFont: fontkit.Font;
384
- }) => {
385
- const { value, characterSpacing, fontSize, fontKitFont, boxWidthInPt } = arg;
386
- const fontWidthCalcValues: FontWidthCalcValues = {
387
- font: fontKitFont,
388
- fontSize,
389
- characterSpacing,
390
- boxWidthInPt,
391
- };
392
- let lines: string[] = [];
393
- value.split(/\r\n|\r|\n|\f|\v/g).forEach((line: string) => {
394
- lines = lines.concat(getSplittedLinesBySegmenter(line, fontWidthCalcValues));
395
- });
396
- return lines;
397
- };
398
- export const isFirefox = () => navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
399
-
400
- const getSplittedLinesBySegmenter = (line: string, calcValues: FontWidthCalcValues): string[] => {
401
- // nothing to process but need to keep this for new lines.
402
- if (line.trim() === '') {
403
- return [''];
404
- }
405
-
406
- const { font, fontSize, characterSpacing, boxWidthInPt } = calcValues;
407
- const segmenter = new Intl.Segmenter(undefined, { granularity: 'word' });
408
- const iterator = segmenter.segment(line.trimEnd())[Symbol.iterator]();
409
-
410
- let lines: string[] = [];
411
- let lineCounter: number = 0;
412
- let currentTextSize: number = 0;
413
-
414
- while (true) {
415
- const chunk = iterator.next();
416
- if (chunk.done) break;
417
- const segment = chunk.value.segment;
418
- const textWidth = widthOfTextAtSize(segment, font, fontSize, characterSpacing);
419
- if (currentTextSize + textWidth <= boxWidthInPt) {
420
- // the size of boxWidth is large enough to add the segment
421
- if (lines[lineCounter]) {
422
- lines[lineCounter] += segment;
423
- currentTextSize += textWidth + characterSpacing;
424
- } else {
425
- lines[lineCounter] = segment;
426
- currentTextSize = textWidth + characterSpacing;
427
- }
428
- } else if (segment.trim() === '') {
429
- // a segment can be consist of multiple spaces like ' '
430
- // if they overflow the box, treat them as a line break and move to the next line
431
- lines[++lineCounter] = '';
432
- currentTextSize = 0;
433
- } else if (textWidth <= boxWidthInPt) {
434
- // the segment is small enough to be added to the next line
435
- lines[++lineCounter] = segment;
436
- currentTextSize = textWidth + characterSpacing;
437
- } else {
438
- // the segment is too large to fit in the boxWidth, we wrap the segment
439
- for (const char of segment) {
440
- const size = widthOfTextAtSize(char, font, fontSize, characterSpacing);
441
- if (currentTextSize + size <= boxWidthInPt) {
442
- if (lines[lineCounter]) {
443
- lines[lineCounter] += char;
444
- currentTextSize += size + characterSpacing;
445
- } else {
446
- lines[lineCounter] = char;
447
- currentTextSize = size + characterSpacing;
448
- }
449
- } else {
450
- lines[++lineCounter] = char;
451
- currentTextSize = size + characterSpacing;
452
- }
453
- }
454
- }
455
- }
456
-
457
- if (lines.some(containsJapanese)) {
458
- return adjustEndOfLine(filterEndJP(filterStartJP(lines)));
459
- } else {
460
- return adjustEndOfLine(lines);
461
- }
462
- };
463
-
464
- // add a newline if the line is the end of the paragraph
465
- const adjustEndOfLine = (lines: string[]): string[] => {
466
- return lines.map((line, index) => {
467
- if (index === lines.length - 1) {
468
- return line.trimEnd() + '\n';
469
- } else {
470
- return line.trimEnd();
471
- }
472
- });
473
- };
474
-
475
- function containsJapanese(text: string): boolean {
476
- return /[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]/u.test(text);
477
- }
478
- //
479
- // 日本語禁則処理
480
- //
481
- // https://www.morisawa.co.jp/blogs/MVP/8760
482
- //
483
- // 行頭禁則
484
- export const filterStartJP = (lines: string[]): string[] => {
485
- const filtered: string[] = [];
486
- let charToAppend: string | null = null;
487
-
488
- lines
489
- .slice()
490
- .reverse()
491
- .forEach((line) => {
492
- if (line.trim().length === 0) {
493
- filtered.push('');
494
- } else {
495
- const charAtStart: string = line.charAt(0);
496
- if (LINE_START_FORBIDDEN_CHARS.includes(charAtStart)) {
497
- if (line.trim().length === 1) {
498
- filtered.push(line);
499
- charToAppend = null;
500
- } else {
501
- if (charToAppend) {
502
- filtered.push(line.slice(1) + charToAppend);
503
- } else {
504
- filtered.push(line.slice(1));
505
- }
506
- charToAppend = charAtStart;
507
- }
508
- } else {
509
- if (charToAppend) {
510
- filtered.push(line + charToAppend);
511
- charToAppend = null;
512
- } else {
513
- filtered.push(line);
514
- }
515
- }
516
- }
517
- });
518
-
519
- if (charToAppend) {
520
- // Handle the case where filtered might be empty
521
- const firstItem = filtered.length > 0 ? filtered[0] : '';
522
- // Ensure we're concatenating strings
523
- const combinedItem = String(charToAppend) + String(firstItem);
524
- return [combinedItem, ...filtered.slice(1)].reverse();
525
- } else {
526
- return filtered.reverse();
527
- }
528
- };
529
-
530
- // 行末禁則
531
- export const filterEndJP = (lines: string[]): string[] => {
532
- const filtered: string[] = [];
533
- let charToPrepend: string | null = null;
534
-
535
- lines.forEach((line) => {
536
- if (line.trim().length === 0) {
537
- filtered.push('');
538
- } else {
539
- const chartAtEnd = line.slice(-1);
540
-
541
- if (LINE_END_FORBIDDEN_CHARS.includes(chartAtEnd)) {
542
- if (line.trim().length === 1) {
543
- filtered.push(line);
544
- charToPrepend = null;
545
- } else {
546
- if (charToPrepend) {
547
- filtered.push(charToPrepend + line.slice(0, -1));
548
- } else {
549
- filtered.push(line.slice(0, -1));
550
- }
551
- charToPrepend = chartAtEnd;
552
- }
553
- } else {
554
- if (charToPrepend) {
555
- filtered.push(charToPrepend + line);
556
- charToPrepend = null;
557
- } else {
558
- filtered.push(line);
559
- }
560
- }
561
- }
562
- });
563
-
564
- if (charToPrepend) {
565
- // Handle the case where filtered might be empty
566
- const lastItem = filtered.length > 0 ? filtered[filtered.length - 1] : '';
567
- // Ensure we're concatenating strings
568
- const combinedItem = String(lastItem) + String(charToPrepend);
569
- return [...filtered.slice(0, -1), combinedItem];
570
- } else {
571
- return filtered;
572
- }
573
- };
@@ -1,30 +0,0 @@
1
- import {
2
- Strikethrough,
3
- Underline,
4
- AlignLeft,
5
- AlignCenter,
6
- AlignRight,
7
- ArrowUpToLine,
8
- ArrowDownToLine,
9
- AlignJustify,
10
- } from 'lucide';
11
- import { createSvgStr } from '../../utils.js';
12
-
13
- export const TextStrikethroughIcon = createSvgStr(Strikethrough);
14
-
15
- export const TextUnderlineIcon = createSvgStr(Underline);
16
-
17
- export const TextAlignLeftIcon = createSvgStr(AlignLeft);
18
-
19
- export const TextAlignCenterIcon = createSvgStr(AlignCenter);
20
-
21
- export const TextAlignRightIcon = createSvgStr(AlignRight);
22
-
23
- export const TextAlignJustifyIcon = createSvgStr(AlignJustify);
24
-
25
- export const TextVerticalAlignTopIcon = createSvgStr(ArrowUpToLine);
26
-
27
- // svg icons are material icons from https://www.xicons.org
28
- export const TextVerticalAlignMiddleIcon = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><path d="M8 19h3v4h2v-4h3l-4-4l-4 4zm8-14h-3V1h-2v4H8l4 4l4-4zM4 11v2h16v-2H4z" fill="currentColor"></path></svg>`;
29
-
30
- export const TextVerticalAlignBottomIcon = createSvgStr(ArrowDownToLine);
package/src/text/index.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { Plugin } from '@pdfme/common';
2
- import { pdfRender } from './pdfRender.js';
3
- import { propPanel } from './propPanel.js';
4
- import { uiRender } from './uiRender.js';
5
- import type { TextSchema } from './types.js';
6
- import { TextCursorInput } from 'lucide';
7
- import { createSvgStr } from '../utils.js';
8
-
9
- const textSchema: Plugin<TextSchema> = {
10
- pdf: pdfRender,
11
- ui: uiRender,
12
- propPanel,
13
- icon: createSvgStr(TextCursorInput),
14
- };
15
-
16
- export default textSchema;