Skip to content

Commit

Permalink
Support comb textfields for printing
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Aug 7, 2020
1 parent 0255383 commit 06b0c71
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,16 @@ class WidgetAnnotation extends Annotation {
const vPadding = defaultPadding + Math.abs(descent) * fontSize;
const defaultAppearance = this.data.defaultAppearance;

if (this.data.comb) {
return this._getCombAppearance(
defaultAppearance,
value,
totalWidth,
hPadding,
vPadding
);
}

if (this.data.multiLine) {
return this._getMultilineAppearance(
defaultAppearance,
Expand Down Expand Up @@ -1129,6 +1139,22 @@ class TextWidgetAnnotation extends WidgetAnnotation {
this.data.maxLen !== null;
}

_getCombAppearance(defaultAppearance, text, width, hPadding, vPadding) {
const combWidth = (width / this.data.maxLen).toFixed(2);
const buf = [];
for (const character of text) {
buf.push(`(${escapeString(character)}) Tj`);
}

const renderedComb = buf.join(` ${combWidth} 0 Td `);
return (
"/Tx BMC q BT " +
defaultAppearance +
` 1 0 0 1 ${hPadding} ${vPadding} Tm ${renderedComb}` +
" ET Q EMC"
);
}

_getMultilineAppearance(
defaultAppearance,
text,
Expand Down
40 changes: 40 additions & 0 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,46 @@ describe("annotation", function () {
done();
}, done.fail);
});

it("should render comb for printing", function (done) {
textWidgetDict.set("Ff", AnnotationFieldFlag.COMB);
textWidgetDict.set("MaxLen", 4);

const textWidgetRef = Ref.get(271, 0);
const xref = new XRefMock([
{ ref: textWidgetRef, data: textWidgetDict },
fontRefObj,
]);
const task = new WorkerTask("test print");
partialEvaluator.xref = xref;

AnnotationFactory.create(
xref,
textWidgetRef,
pdfManagerMock,
idFactoryMock
)
.then(annotation => {
const id = annotation.data.id;
const annotationStorage = {};
annotationStorage[id] = "aa(aa)a\\";
return annotation._getAppearance(
partialEvaluator,
task,
annotationStorage
);
}, done.fail)
.then(appearance => {
expect(appearance).toEqual(
"/Tx BMC q BT /Helv 5 Tf 1 0 0 1 2 2 Tm" +
" (a) Tj 8.00 0 Td (a) Tj 8.00 0 Td (\\() Tj" +
" 8.00 0 Td (a) Tj 8.00 0 Td (a) Tj" +
" 8.00 0 Td (\\)) Tj 8.00 0 Td (a) Tj" +
" 8.00 0 Td (\\\\) Tj ET Q EMC"
);
done();
}, done.fail);
});
});

describe("ButtonWidgetAnnotation", function () {
Expand Down

0 comments on commit 06b0c71

Please sign in to comment.