@pdfme/schemas 5.3.4 → 5.3.5-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 (43) hide show
  1. package/dist/cjs/__tests__/text.test.js +82 -0
  2. package/dist/cjs/__tests__/text.test.js.map +1 -1
  3. package/dist/cjs/src/tables/propPanel.js +1 -1
  4. package/dist/cjs/src/text/constants.js +75 -1
  5. package/dist/cjs/src/text/constants.js.map +1 -1
  6. package/dist/cjs/src/text/extraFormatter.js +2 -1
  7. package/dist/cjs/src/text/extraFormatter.js.map +1 -1
  8. package/dist/cjs/src/text/helper.js +182 -5
  9. package/dist/cjs/src/text/helper.js.map +1 -1
  10. package/dist/cjs/src/text/icons/index.js +2 -1
  11. package/dist/cjs/src/text/icons/index.js.map +1 -1
  12. package/dist/cjs/src/text/pdfRender.js +17 -3
  13. package/dist/cjs/src/text/pdfRender.js.map +1 -1
  14. package/dist/cjs/src/text/propPanel.js +3 -3
  15. package/dist/esm/__tests__/text.test.js +83 -1
  16. package/dist/esm/__tests__/text.test.js.map +1 -1
  17. package/dist/esm/src/tables/propPanel.js +1 -1
  18. package/dist/esm/src/text/constants.js +74 -0
  19. package/dist/esm/src/text/constants.js.map +1 -1
  20. package/dist/esm/src/text/extraFormatter.js +4 -3
  21. package/dist/esm/src/text/extraFormatter.js.map +1 -1
  22. package/dist/esm/src/text/helper.js +180 -5
  23. package/dist/esm/src/text/helper.js.map +1 -1
  24. package/dist/esm/src/text/icons/index.js +2 -1
  25. package/dist/esm/src/text/icons/index.js.map +1 -1
  26. package/dist/esm/src/text/pdfRender.js +17 -3
  27. package/dist/esm/src/text/pdfRender.js.map +1 -1
  28. package/dist/esm/src/text/propPanel.js +3 -3
  29. package/dist/types/src/tables/types.d.ts +1 -1
  30. package/dist/types/src/text/constants.d.ts +3 -0
  31. package/dist/types/src/text/helper.d.ts +2 -0
  32. package/dist/types/src/text/icons/index.d.ts +1 -0
  33. package/dist/types/src/text/types.d.ts +1 -1
  34. package/package.json +1 -1
  35. package/src/tables/propPanel.ts +1 -1
  36. package/src/tables/types.ts +1 -1
  37. package/src/text/constants.ts +81 -0
  38. package/src/text/extraFormatter.ts +4 -1
  39. package/src/text/helper.ts +181 -6
  40. package/src/text/icons/index.ts +3 -0
  41. package/src/text/pdfRender.ts +21 -6
  42. package/src/text/propPanel.ts +3 -3
  43. package/src/text/types.ts +1 -1
@@ -120,8 +120,6 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
120
120
  page.drawRectangle({ x, y, width, height, rotate, color });
121
121
  }
122
122
 
123
- page.pushOperators(pdfLib.setCharacterSpacing(characterSpacing ?? DEFAULT_CHARACTER_SPACING));
124
-
125
123
  const firstLineTextHeight = heightOfFontAtSize(fontKitFont, fontSize);
126
124
  const descent = getFontDescentInPt(fontKitFont, fontSize);
127
125
  const halfLineHeightAdjustment = lineHeight === 0 ? 0 : ((lineHeight - 1) * fontSize) / 2;
@@ -150,12 +148,20 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
150
148
  }
151
149
 
152
150
  const pivotPoint = { x: x + width / 2, y: pageHeight - mm2pt(schema.position.y) - height / 2 };
151
+ const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
153
152
 
154
153
  lines.forEach((line, rowIndex) => {
155
- const textWidth = widthOfTextAtSize(line, fontKitFont, fontSize, characterSpacing);
154
+ const trimmed = line.replace('\n', '');
155
+ const textWidth = widthOfTextAtSize(trimmed, fontKitFont, fontSize, characterSpacing);
156
156
  const textHeight = heightOfFontAtSize(fontKitFont, fontSize);
157
157
  const rowYOffset = lineHeight * fontSize * rowIndex;
158
158
 
159
+ // Adobe Acrobat Reader shows an error if `drawText` is called with an empty text
160
+ if (line === '') {
161
+ // return; // this also works
162
+ line = '\r\n';
163
+ }
164
+
159
165
  let xLine = x;
160
166
  if (alignment === 'center') {
161
167
  xLine += (width - textWidth) / 2;
@@ -167,7 +173,7 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
167
173
 
168
174
  // draw strikethrough
169
175
  if (schema.strikethrough && textWidth > 0) {
170
- const _x = xLine + textWidth + 1
176
+ const _x = xLine + textWidth + 1;
171
177
  const _y = yLine + textHeight / 3;
172
178
  page.drawLine({
173
179
  start: rotatePoint({ x: xLine, y: _y }, pivotPoint, rotate.angle),
@@ -180,7 +186,7 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
180
186
 
181
187
  // draw underline
182
188
  if (schema.underline && textWidth > 0) {
183
- const _x = xLine + textWidth + 1
189
+ const _x = xLine + textWidth + 1;
184
190
  const _y = yLine - textHeight / 12;
185
191
  page.drawLine({
186
192
  start: rotatePoint({ x: xLine, y: _y }, pivotPoint, rotate.angle),
@@ -199,7 +205,16 @@ export const pdfRender = async (arg: PDFRenderProps<TextSchema>) => {
199
205
  yLine = rotatedPoint.y;
200
206
  }
201
207
 
202
- page.drawText(line, {
208
+ let spacing = characterSpacing;
209
+ if (alignment === 'justify' && line.slice(-1) !== '\n') {
210
+ // if alignment is `justify` but the end of line is not newline, then adjust the spacing
211
+ const iterator = segmenter.segment(trimmed)[Symbol.iterator]();
212
+ const len = Array.from(iterator).length;
213
+ spacing += (width - textWidth) / len;
214
+ }
215
+ page.pushOperators(pdfLib.setCharacterSpacing(spacing));
216
+
217
+ page.drawText(trimmed, {
203
218
  x: xLine,
204
219
  y: yLine,
205
220
  rotate,
@@ -87,7 +87,7 @@ export const propPanel: PropPanel<TextSchema> = {
87
87
  type: 'number',
88
88
  widget: 'inputNumber',
89
89
  props: { step: 0.1, min: 0 },
90
- span: 7,
90
+ span: 8,
91
91
  },
92
92
  useDynamicFontSize: { type: 'boolean', widget: 'UseDynamicFontSize', bind: false, span: 16 },
93
93
  dynamicFontSize: {
@@ -128,7 +128,7 @@ export const propPanel: PropPanel<TextSchema> = {
128
128
  type: 'string',
129
129
  widget: 'color',
130
130
  props: {
131
- disabledAlpha: true
131
+ disabledAlpha: true,
132
132
  },
133
133
  rules: [
134
134
  {
@@ -142,7 +142,7 @@ export const propPanel: PropPanel<TextSchema> = {
142
142
  type: 'string',
143
143
  widget: 'color',
144
144
  props: {
145
- disabledAlpha: true
145
+ disabledAlpha: true,
146
146
  },
147
147
  rules: [
148
148
  {
package/src/text/types.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { Schema } from '@pdfme/common';
2
2
  import type { Font as FontKitFont } from 'fontkit';
3
3
 
4
- export type ALIGNMENT = 'left' | 'center' | 'right';
4
+ export type ALIGNMENT = 'left' | 'center' | 'right' | 'justify';
5
5
  export type VERTICAL_ALIGNMENT = 'top' | 'middle' | 'bottom';
6
6
  export type DYNAMIC_FONT_SIZE_FIT = 'horizontal' | 'vertical';
7
7