Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Set font size and color for text widget annotations #12828

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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],
Expand All @@ -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;
Expand Down Expand Up @@ -1456,7 +1454,7 @@ class WidgetAnnotation extends Annotation {
PDFJSDev.test("!PRODUCTION || TESTING")
) {
assert(
this._defaultAppearanceData,
this.data.defaultAppearanceData,
"Expected `_defaultAppearanceData` to have been set."
);
}
Expand All @@ -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;
}
Expand Down
45 changes: 13 additions & 32 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
}

render() {
const TEXT_ALIGNMENT = ["left", "center", "right"];
const storage = this.annotationStorage;
const id = this.data.id;

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
dhufnagel marked this conversation as resolved.
Show resolved Hide resolved
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];
}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,4 @@
!issue11555.pdf
!issue12337.pdf
!pr12564.pdf
!pr12828.pdf
Binary file added test/pdfs/pr12828.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5005,5 +5005,13 @@
"rounds": 1,
"type": "eq",
"annotations": true
},
{
"id": "pr12828",
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
"file": "pdfs/pr12828.pdf",
"md5": "e44d364fba2f146aed04f9d9abbb0f28",
"rounds": 1,
"type": "eq",
"forms": true
}
]