Skip to content

Commit

Permalink
Add option to define AcroForm fontSize. Fixes foliojs#1088
Browse files Browse the repository at this point in the history
  • Loading branch information
Simolation committed May 29, 2021
1 parent 6338314 commit 131df9e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
### Unreleased

- Allow applying 'underline' and 'strike' text styling together on a text
- Allow to specify the AcroForm text fontSize

### [v0.12.0] - 2021-04-04

Expand Down
1 change: 1 addition & 0 deletions docs/forms.md
Expand Up @@ -62,6 +62,7 @@ a hex color, or a named CSS color value for that option.
- `password` [_boolean_] - The text will be masked (_e.g._ with asterisks).
- `noSpell` [_boolean_] - If set, text entered in the field is not spell-checked
- `format` [_object_] - See the section on **Text Field Formatting** below.
- `fontSize` [_number_] - Sets the fontSize (default or 0 means auto sizing)

```js
doc.formText('leaf2', 10, 60, 200, 40, {
Expand Down
6 changes: 5 additions & 1 deletion lib/mixins/acroform.js
Expand Up @@ -333,8 +333,12 @@ export default {
// add current font to field's resource dict (RD) if not the default acroform font
if (this._acroform.defaultFont !== this._font.name) {
options.DR = { Font: {} };

// Get the fontSize option. If not set use auto sizing
const fontSize = options.fontSize || 0;

options.DR.Font[this._font.id] = this._font.ref();
options.DA = new String(`/${this._font.id} 0 Tf 0 g`);
options.DA = new String(`/${this._font.id} ${fontSize} Tf 0 g`);
}
return options;
},
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/acroform.spec.js
Expand Up @@ -222,6 +222,40 @@ describe('acroform', () => {
expect(docData).toContainChunk(expected);
});

test('font size', () => {
const expected = [
'11 0 obj',
'<<\n' +
'/fontSize 16\n' +
'/FT /Tx\n' +
'/DR <<\n' +
'/Font <<\n' +
'/F2 10 0 R\n' +
'>>\n' +
'>>\n' +
'/DA (/F2 16 Tf 0 g)\n' +
'/T (text)\n' +
'/Subtype /Widget\n' +
'/F 4\n' +
'/Type /Annot\n' +
'/Rect [20 752 70 772]\n' +
'/Border [0 0 0]\n' +
'/C [0 0 0]\n' +
'/FontSize 16\n' +
'>>',
'endobj'
];
doc.registerFont('myfont1', 'tests/fonts/Roboto-Regular.ttf');
doc.initForm();
const docData = logData(doc);
let opts = {
fontSize: 16
};
doc.font('myfont1').formText('text', 20, 20, 50, 20, opts);
expect(docData.length).toBe(3);
expect(docData).toContainChunk(expected);
});

test('field heirarchy', () => {
const expected = [
'13 0 obj',
Expand Down

0 comments on commit 131df9e

Please sign in to comment.