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 6765bf9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 40 deletions.
14 changes: 6 additions & 8 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 @@ -1296,14 +1294,14 @@ 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
);
}

const font = await this._getFontData(evaluator, task);
const fontSize = this._computeFontSize(font, totalHeight);
this._fontName = this._defaultAppearanceData.fontName.name;
this._fontName = this.data.defaultAppearanceData.fontName.name;

let descent = font.descent;
if (isNaN(descent)) {
Expand Down Expand Up @@ -1380,7 +1378,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 @@ -1395,9 +1393,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
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 6765bf9

Please sign in to comment.