set font size and color on annotation layer
use the default appearance to set the font size and color of a text annotation widget
This commit is contained in:
parent
6ffb6b1c0c
commit
c5083cda02
@ -997,9 +997,7 @@ class WidgetAnnotation extends Annotation {
|
|||||||
data.defaultAppearance = isString(defaultAppearance)
|
data.defaultAppearance = isString(defaultAppearance)
|
||||||
? defaultAppearance
|
? defaultAppearance
|
||||||
: "";
|
: "";
|
||||||
this._defaultAppearanceData = parseDefaultAppearance(
|
data.defaultAppearanceData = parseDefaultAppearance(data.defaultAppearance);
|
||||||
data.defaultAppearance
|
|
||||||
);
|
|
||||||
|
|
||||||
const fieldType = getInheritableProperty({ dict, key: "FT" });
|
const fieldType = getInheritableProperty({ dict, key: "FT" });
|
||||||
data.fieldType = isName(fieldType) ? fieldType.name : null;
|
data.fieldType = isName(fieldType) ? fieldType.name : null;
|
||||||
@ -1294,7 +1292,7 @@ class WidgetAnnotation extends Annotation {
|
|||||||
// Doing so prevents exceptions and allows saving/printing
|
// Doing so prevents exceptions and allows saving/printing
|
||||||
// the file as expected.
|
// the file as expected.
|
||||||
this.data.defaultAppearance = "/Helvetica 0 Tf 0 g";
|
this.data.defaultAppearance = "/Helvetica 0 Tf 0 g";
|
||||||
this._defaultAppearanceData = parseDefaultAppearance(
|
this.data.defaultAppearanceData = parseDefaultAppearance(
|
||||||
this.data.defaultAppearance
|
this.data.defaultAppearance
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1377,7 +1375,7 @@ class WidgetAnnotation extends Annotation {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const { fontName, fontSize } = this._defaultAppearanceData;
|
const { fontName, fontSize } = this.data.defaultAppearanceData;
|
||||||
await evaluator.handleSetFont(
|
await evaluator.handleSetFont(
|
||||||
this._fieldResources.mergedResources,
|
this._fieldResources.mergedResources,
|
||||||
[fontName, fontSize],
|
[fontName, fontSize],
|
||||||
@ -1392,9 +1390,9 @@ class WidgetAnnotation extends Annotation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_computeFontSize(font, height) {
|
_computeFontSize(font, height) {
|
||||||
let fontSize = this._defaultAppearanceData.fontSize;
|
let fontSize = this.data.defaultAppearanceData.fontSize;
|
||||||
if (!fontSize) {
|
if (!fontSize) {
|
||||||
const { fontColor, fontName } = this._defaultAppearanceData;
|
const { fontColor, fontName } = this.data.defaultAppearanceData;
|
||||||
let capHeight;
|
let capHeight;
|
||||||
if (font.capHeight) {
|
if (font.capHeight) {
|
||||||
capHeight = font.capHeight;
|
capHeight = font.capHeight;
|
||||||
@ -1456,7 +1454,7 @@ class WidgetAnnotation extends Annotation {
|
|||||||
PDFJSDev.test("!PRODUCTION || TESTING")
|
PDFJSDev.test("!PRODUCTION || TESTING")
|
||||||
) {
|
) {
|
||||||
assert(
|
assert(
|
||||||
this._defaultAppearanceData,
|
this.data.defaultAppearanceData,
|
||||||
"Expected `_defaultAppearanceData` to have been set."
|
"Expected `_defaultAppearanceData` to have been set."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -1467,7 +1465,8 @@ class WidgetAnnotation extends Annotation {
|
|||||||
} = this._fieldResources;
|
} = this._fieldResources;
|
||||||
|
|
||||||
const fontNameStr =
|
const fontNameStr =
|
||||||
this._defaultAppearanceData && this._defaultAppearanceData.fontName.name;
|
this.data.defaultAppearanceData &&
|
||||||
|
this.data.defaultAppearanceData.fontName.name;
|
||||||
if (!fontNameStr) {
|
if (!fontNameStr) {
|
||||||
return localResources || Dict.empty;
|
return localResources || Dict.empty;
|
||||||
}
|
}
|
||||||
|
@ -595,7 +595,6 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const TEXT_ALIGNMENT = ["left", "center", "right"];
|
|
||||||
const storage = this.annotationStorage;
|
const storage = this.annotationStorage;
|
||||||
const id = this.data.id;
|
const id = this.data.id;
|
||||||
|
|
||||||
@ -834,20 +833,9 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
|
|||||||
element.textContent = this.data.fieldValue;
|
element.textContent = this.data.fieldValue;
|
||||||
element.style.verticalAlign = "middle";
|
element.style.verticalAlign = "middle";
|
||||||
element.style.display = "table-cell";
|
element.style.display = "table-cell";
|
||||||
|
|
||||||
let font = null;
|
|
||||||
if (
|
|
||||||
this.data.fontRefName &&
|
|
||||||
this.page.commonObjs.has(this.data.fontRefName)
|
|
||||||
) {
|
|
||||||
font = this.page.commonObjs.get(this.data.fontRefName);
|
|
||||||
}
|
|
||||||
this._setTextStyle(element, font);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.data.textAlignment !== null) {
|
this._setTextStyle(element);
|
||||||
element.style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
|
|
||||||
}
|
|
||||||
|
|
||||||
this.container.appendChild(element);
|
this.container.appendChild(element);
|
||||||
return this.container;
|
return this.container;
|
||||||
@ -858,32 +846,25 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
|
|||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {HTMLDivElement} element
|
* @param {HTMLDivElement} element
|
||||||
* @param {Object} font
|
|
||||||
* @memberof TextWidgetAnnotationElement
|
* @memberof TextWidgetAnnotationElement
|
||||||
*/
|
*/
|
||||||
_setTextStyle(element, font) {
|
_setTextStyle(element) {
|
||||||
// TODO: This duplicates some of the logic in CanvasGraphics.setFont().
|
const TEXT_ALIGNMENT = ["left", "center", "right"];
|
||||||
|
const { fontSize, fontColor } = this.data.defaultAppearanceData;
|
||||||
const style = element.style;
|
const style = element.style;
|
||||||
style.fontSize = `${this.data.fontSize}px`;
|
|
||||||
style.direction = this.data.fontDirection < 0 ? "rtl" : "ltr";
|
|
||||||
|
|
||||||
if (!font) {
|
// TODO: If the font-size is zero, calculate it based on the height and
|
||||||
return;
|
// width of the element.
|
||||||
|
// Not setting `style.fontSize` will use the default font-size for now.
|
||||||
|
if (fontSize) {
|
||||||
|
style.fontSize = `${fontSize}px`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let bold = "normal";
|
style.color = Util.makeHexColor(fontColor[0], fontColor[1], fontColor[2]);
|
||||||
if (font.black) {
|
|
||||||
bold = "900";
|
|
||||||
} else if (font.bold) {
|
|
||||||
bold = "bold";
|
|
||||||
}
|
|
||||||
style.fontWeight = bold;
|
|
||||||
style.fontStyle = font.italic ? "italic" : "normal";
|
|
||||||
|
|
||||||
// Use a reasonable default font if the font doesn't specify a fallback.
|
if (this.data.textAlignment !== null) {
|
||||||
const fontFamily = font.loadedName ? `"${font.loadedName}", ` : "";
|
style.textAlign = TEXT_ALIGNMENT[this.data.textAlignment];
|
||||||
const fallbackName = font.fallbackName || "Helvetica, sans-serif";
|
}
|
||||||
style.fontFamily = fontFamily + fallbackName;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
test/pdfs/.gitignore
vendored
1
test/pdfs/.gitignore
vendored
@ -418,3 +418,4 @@
|
|||||||
!issue11555.pdf
|
!issue11555.pdf
|
||||||
!issue12337.pdf
|
!issue12337.pdf
|
||||||
!pr12564.pdf
|
!pr12564.pdf
|
||||||
|
!pr12828.pdf
|
||||||
|
BIN
test/pdfs/pr12828.pdf
Normal file
BIN
test/pdfs/pr12828.pdf
Normal file
Binary file not shown.
@ -5005,5 +5005,13 @@
|
|||||||
"rounds": 1,
|
"rounds": 1,
|
||||||
"type": "eq",
|
"type": "eq",
|
||||||
"annotations": true
|
"annotations": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "pr12828",
|
||||||
|
"file": "pdfs/pr12828.pdf",
|
||||||
|
"md5": "e44d364fba2f146aed04f9d9abbb0f28",
|
||||||
|
"rounds": 1,
|
||||||
|
"type": "eq",
|
||||||
|
"forms": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user