Support comb textfields for printing

This commit is contained in:
Calixte Denizet 2020-08-06 16:45:17 +02:00 committed by calixteman
parent b061c300b4
commit 88b112ab0c
2 changed files with 66 additions and 0 deletions

View File

@ -983,6 +983,16 @@ class WidgetAnnotation extends Annotation {
const defaultAppearance = this.data.defaultAppearance; const defaultAppearance = this.data.defaultAppearance;
const alignment = this.data.textAlignment; const alignment = this.data.textAlignment;
if (this.data.comb) {
return this._getCombAppearance(
defaultAppearance,
value,
totalWidth,
hPadding,
vPadding
);
}
if (this.data.multiLine) { if (this.data.multiLine) {
return this._getMultilineAppearance( return this._getMultilineAppearance(
defaultAppearance, defaultAppearance,
@ -1129,6 +1139,22 @@ class TextWidgetAnnotation extends WidgetAnnotation {
this.data.maxLen !== null; 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( _getMultilineAppearance(
defaultAppearance, defaultAppearance,
text, text,

View File

@ -1781,6 +1781,46 @@ describe("annotation", function () {
done(); done();
}, done.fail); }, 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 () { describe("ButtonWidgetAnnotation", function () {