Skip to content

Commit

Permalink
set font size and color on annotation layer
Browse files Browse the repository at this point in the history
use the default appearance to set the font size and color of a text
annotation widget
  • Loading branch information
dhufnagel committed Jan 22, 2021
1 parent 1039698 commit 7c5bf99
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ class WidgetAnnotation extends Annotation {
this._defaultAppearanceData = parseDefaultAppearance(
data.defaultAppearance
);
data.defaultAppearanceData = this._defaultAppearanceData;

const fieldType = getInheritableProperty({ dict, key: "FT" });
data.fieldType = isName(fieldType) ? fieldType.name : null;
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) {
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",
"file": "pdfs/pr12828.pdf",
"md5": "e44d364fba2f146aed04f9d9abbb0f28",
"rounds": 1,
"type": "eq",
"forms": true
}
]

0 comments on commit 7c5bf99

Please sign in to comment.