Merge pull request #16640 from calixteman/freetext_ap
[Editor] When saving/printing a FreeText, use the identity matrix for the AP and set the cm when rendering it
This commit is contained in:
commit
66fc19b272
@ -3743,7 +3743,7 @@ class FreeTextAnnotation extends MarkupAnnotation {
|
|||||||
evaluator,
|
evaluator,
|
||||||
task,
|
task,
|
||||||
{
|
{
|
||||||
fontName: "Helvetica",
|
fontName: "Helv",
|
||||||
fontSize,
|
fontSize,
|
||||||
},
|
},
|
||||||
resources
|
resources
|
||||||
@ -3783,26 +3783,52 @@ class FreeTextAnnotation extends MarkupAnnotation {
|
|||||||
}
|
}
|
||||||
let vscale = 1;
|
let vscale = 1;
|
||||||
const lineHeight = LINE_FACTOR * fontSize;
|
const lineHeight = LINE_FACTOR * fontSize;
|
||||||
const lineDescent = LINE_DESCENT_FACTOR * fontSize;
|
const lineAscent = (LINE_FACTOR - LINE_DESCENT_FACTOR) * fontSize;
|
||||||
const totalHeight = lineHeight * lines.length;
|
const totalHeight = lineHeight * lines.length;
|
||||||
if (totalHeight > h) {
|
if (totalHeight > h) {
|
||||||
vscale = h / totalHeight;
|
vscale = h / totalHeight;
|
||||||
}
|
}
|
||||||
const fscale = Math.min(hscale, vscale);
|
const fscale = Math.min(hscale, vscale);
|
||||||
const newFontSize = fontSize * fscale;
|
const newFontSize = fontSize * fscale;
|
||||||
|
let firstPoint, clipBox, matrix;
|
||||||
|
switch (rotation) {
|
||||||
|
case 0:
|
||||||
|
matrix = [1, 0, 0, 1];
|
||||||
|
clipBox = [rect[0], rect[1], w, h];
|
||||||
|
firstPoint = [rect[0], rect[3] - lineAscent];
|
||||||
|
break;
|
||||||
|
case 90:
|
||||||
|
matrix = [0, 1, -1, 0];
|
||||||
|
clipBox = [rect[1], -rect[2], w, h];
|
||||||
|
firstPoint = [rect[1], -rect[0] - lineAscent];
|
||||||
|
break;
|
||||||
|
case 180:
|
||||||
|
matrix = [-1, 0, 0, -1];
|
||||||
|
clipBox = [-rect[2], -rect[3], w, h];
|
||||||
|
firstPoint = [-rect[2], -rect[1] - lineAscent];
|
||||||
|
break;
|
||||||
|
case 270:
|
||||||
|
matrix = [0, -1, 1, 0];
|
||||||
|
clipBox = [-rect[3], rect[0], w, h];
|
||||||
|
firstPoint = [-rect[3], rect[2] - lineAscent];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const buffer = [
|
const buffer = [
|
||||||
"q",
|
"q",
|
||||||
`0 0 ${numberToString(w)} ${numberToString(h)} re W n`,
|
`${matrix.join(" ")} 0 0 cm`,
|
||||||
|
`${clipBox.join(" ")} re W n`,
|
||||||
`BT`,
|
`BT`,
|
||||||
`1 0 0 1 0 ${numberToString(h + lineDescent)} Tm 0 Tc ${getPdfColor(
|
`${getPdfColor(color, /* isFill */ true)}`,
|
||||||
color,
|
`0 Tc /Helv ${numberToString(newFontSize)} Tf`,
|
||||||
/* isFill */ true
|
|
||||||
)}`,
|
|
||||||
`/Helv ${numberToString(newFontSize)} Tf`,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
buffer.push(
|
||||||
|
`${firstPoint.join(" ")} Td (${escapeString(encodedLines[0])}) Tj`
|
||||||
|
);
|
||||||
const vShift = numberToString(lineHeight);
|
const vShift = numberToString(lineHeight);
|
||||||
for (const line of encodedLines) {
|
for (let i = 1, ii = encodedLines.length; i < ii; i++) {
|
||||||
|
const line = encodedLines[i];
|
||||||
buffer.push(`0 -${vShift} Td (${escapeString(line)}) Tj`);
|
buffer.push(`0 -${vShift} Td (${escapeString(line)}) Tj`);
|
||||||
}
|
}
|
||||||
buffer.push("ET", "Q");
|
buffer.push("ET", "Q");
|
||||||
@ -3812,13 +3838,9 @@ class FreeTextAnnotation extends MarkupAnnotation {
|
|||||||
appearanceStreamDict.set("FormType", 1);
|
appearanceStreamDict.set("FormType", 1);
|
||||||
appearanceStreamDict.set("Subtype", Name.get("Form"));
|
appearanceStreamDict.set("Subtype", Name.get("Form"));
|
||||||
appearanceStreamDict.set("Type", Name.get("XObject"));
|
appearanceStreamDict.set("Type", Name.get("XObject"));
|
||||||
appearanceStreamDict.set("BBox", [0, 0, w, h]);
|
appearanceStreamDict.set("BBox", rect);
|
||||||
appearanceStreamDict.set("Resources", resources);
|
appearanceStreamDict.set("Resources", resources);
|
||||||
|
appearanceStreamDict.set("Matrix", [1, 0, 0, 1, -rect[0], -rect[1]]);
|
||||||
if (rotation) {
|
|
||||||
const matrix = getRotationMatrix(rotation, w, h);
|
|
||||||
appearanceStreamDict.set("Matrix", matrix);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ap = new StringStream(appearance);
|
const ap = new StringStream(appearance);
|
||||||
ap.dict = appearanceStreamDict;
|
ap.dict = appearanceStreamDict;
|
||||||
|
@ -4110,14 +4110,16 @@ describe("annotation", function () {
|
|||||||
const appearance = data.dependencies[1].data;
|
const appearance = data.dependencies[1].data;
|
||||||
expect(appearance).toEqual(
|
expect(appearance).toEqual(
|
||||||
"3 0 obj\n" +
|
"3 0 obj\n" +
|
||||||
"<< /FormType 1 /Subtype /Form /Type /XObject /BBox [0 0 44 44] " +
|
"<< /FormType 1 /Subtype /Form /Type /XObject /BBox [12 34 56 78] " +
|
||||||
"/Resources << /Font << /Helv 1 0 R>>>> /Length 101>> stream\n" +
|
"/Resources << /Font << /Helv 1 0 R>>>> /Matrix [1 0 0 1 -12 -34] " +
|
||||||
|
"/Length 98>> stream\n" +
|
||||||
"q\n" +
|
"q\n" +
|
||||||
"0 0 44 44 re W n\n" +
|
"1 0 0 1 0 0 cm\n" +
|
||||||
|
"12 34 44 44 re W n\n" +
|
||||||
"BT\n" +
|
"BT\n" +
|
||||||
"1 0 0 1 0 47.5 Tm 0 Tc 0 g\n" +
|
"0 g\n" +
|
||||||
"/Helv 10 Tf\n" +
|
"0 Tc /Helv 10 Tf\n" +
|
||||||
"0 -13.5 Td (Hello PDF.js World!) Tj\n" +
|
"12 68 Td (Hello PDF.js World!) Tj\n" +
|
||||||
"ET\n" +
|
"ET\n" +
|
||||||
"Q\n" +
|
"Q\n" +
|
||||||
"endstream\n" +
|
"endstream\n" +
|
||||||
@ -4153,13 +4155,13 @@ describe("annotation", function () {
|
|||||||
expect(opList.fnArray).toEqual([
|
expect(opList.fnArray).toEqual([
|
||||||
OPS.beginAnnotation,
|
OPS.beginAnnotation,
|
||||||
OPS.save,
|
OPS.save,
|
||||||
|
OPS.transform,
|
||||||
OPS.constructPath,
|
OPS.constructPath,
|
||||||
OPS.clip,
|
OPS.clip,
|
||||||
OPS.endPath,
|
OPS.endPath,
|
||||||
OPS.beginText,
|
OPS.beginText,
|
||||||
OPS.setTextMatrix,
|
|
||||||
OPS.setCharSpacing,
|
|
||||||
OPS.setFillRGBColor,
|
OPS.setFillRGBColor,
|
||||||
|
OPS.setCharSpacing,
|
||||||
OPS.dependency,
|
OPS.dependency,
|
||||||
OPS.setFont,
|
OPS.setFont,
|
||||||
OPS.moveText,
|
OPS.moveText,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user