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