Skip to content

Commit

Permalink
Support Hex String Default Appearances (#693)
Browse files Browse the repository at this point in the history
* Support hexadecimal Default Appearance (#677)

* Add test for PDFAcroField.getDefaultAppearance

* Add test for PDFWidgetAnnotation.getDefaultAppearance

* Cleanup

Co-authored-by: Bj Tecu <[email protected]>
  • Loading branch information
Hopding and btecu authored Dec 6, 2020
1 parent d15b0cb commit d197ac6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/core/acroform/PDFAcroField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ class PDFAcroField {
}

getDefaultAppearance(): string | undefined {
return this.DA()?.asString() ?? '';
const DA = this.DA();

if (DA instanceof PDFHexString) {
return DA.decodeText();
}

return DA?.asString();
}

getFlags(): number {
Expand Down
8 changes: 7 additions & 1 deletion src/core/annotation/PDFWidgetAnnotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ class PDFWidgetAnnotation extends PDFAnnotation {
}

getDefaultAppearance(): string | undefined {
return this.DA()?.asString() ?? '';
const DA = this.DA();

if (DA instanceof PDFHexString) {
return DA.decodeText();
}

return DA?.asString();
}

getAppearanceCharacteristics(): AppearanceCharacteristics | undefined {
Expand Down
44 changes: 44 additions & 0 deletions tests/core/acroform/PDFAcroField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
PDFAcroTerminal,
PDFContext,
PDFString,
PDFHexString,
} from 'src/index';

describe(`PDFAcroField`, () => {
it(`returns undefined for missing (DAs)`, () => {
const context = PDFContext.create();

const dict = context.obj({
DA: null,
});
const dictRef = context.register(dict);
const field = PDFAcroTerminal.fromDict(dict, dictRef);

expect(field.getDefaultAppearance()).toBe(undefined);
});

it(`returns normal direct appearance strings (DAs)`, () => {
const context = PDFContext.create();

const dict = context.obj({
DA: PDFString.of('/ZaDb 10 Tf 0 g'),
});
const dictRef = context.register(dict);
const field = PDFAcroTerminal.fromDict(dict, dictRef);

expect(field.getDefaultAppearance()).toBe('/ZaDb 10 Tf 0 g');
});

it(`returns hexadecimal (non-standard) direct appearance strings (DAs)`, () => {
const context = PDFContext.create();

const dict = context.obj({
DA: PDFHexString.fromText('/ZaDb 10 Tf 0 g'),
});
const dictRef = context.register(dict);
const field = PDFAcroTerminal.fromDict(dict, dictRef);

expect(field.getDefaultAppearance()).toBe('/ZaDb 10 Tf 0 g');
});
});
40 changes: 40 additions & 0 deletions tests/core/annotation/PDFWidgetAnnotation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
PDFWidgetAnnotation,
PDFContext,
PDFString,
PDFHexString,
PDFName,
PDFNull,
} from 'src/index';

describe(`PDFWidgetAnnotation`, () => {
it(`returns undefined for missing (DAs)`, () => {
const context = PDFContext.create();

const parentRef = context.nextRef();
const widget = PDFWidgetAnnotation.create(context, parentRef);
widget.dict.set(PDFName.of('DA'), PDFNull);

expect(widget.getDefaultAppearance()).toBe(undefined);
});

it(`returns normal direct appearance strings (DAs)`, () => {
const context = PDFContext.create();

const parentRef = context.nextRef();
const widget = PDFWidgetAnnotation.create(context, parentRef);
widget.dict.set(PDFName.of('DA'), PDFString.of('/ZaDb 10 Tf 0 g'));

expect(widget.getDefaultAppearance()).toBe('/ZaDb 10 Tf 0 g');
});

it(`returns hexadecimal (non-standard) direct appearance strings (DAs)`, () => {
const context = PDFContext.create();

const parentRef = context.nextRef();
const widget = PDFWidgetAnnotation.create(context, parentRef);
widget.dict.set(PDFName.of('DA'), PDFHexString.fromText('/ZaDb 10 Tf 0 g'));

expect(widget.getDefaultAppearance()).toBe('/ZaDb 10 Tf 0 g');
});
});

0 comments on commit d197ac6

Please sign in to comment.