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

Miscellaneous (small) improvements in src/core/annotation.js #12552

Merged
merged 3 commits into from
Oct 30, 2020
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
29 changes: 16 additions & 13 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
escapeString,
getModificationDate,
isString,
objectSize,
OPS,
shadow,
stringToPDFString,
Expand Down Expand Up @@ -1326,7 +1327,7 @@ class WidgetAnnotation extends Annotation {

_computeFontSize(font, fontName, fontSize, height) {
if (fontSize === null || fontSize === 0) {
const em = font.charsToGlyphs("M", true)[0].width / 1000;
const em = font.charsToGlyphs("M")[0].width / 1000;
// According to https://en.wikipedia.org/wiki/Em_(typography)
// an average cap height should be 70% of 1em
const capHeight = 0.7 * em;
Expand Down Expand Up @@ -1460,18 +1461,20 @@ class WidgetAnnotation extends Annotation {
if (dict.has("AA")) {
const additionalActions = dict.get("AA");
for (const key of additionalActions.getKeys()) {
if (key in AnnotationActionEventType) {
const actionDict = additionalActions.getRaw(key);
const parents = new RefSet();
const list = [];
this._collectJS(actionDict, xref, list, parents);
if (list.length > 0) {
actions[AnnotationActionEventType[key]] = list;
}
const action = AnnotationActionEventType[key];
if (!action) {
continue;
}
const actionDict = additionalActions.getRaw(key);
const parents = new RefSet();
const list = [];
this._collectJS(actionDict, xref, list, parents);
if (list.length > 0) {
actions[action] = list;
}
}
}
// Collect the Action if any (we may have one on pushbutton)
// Collect the Action if any (we may have one on pushbutton).
if (dict.has("A")) {
const actionDict = dict.get("A");
const parents = new RefSet();
Expand All @@ -1481,7 +1484,7 @@ class WidgetAnnotation extends Annotation {
actions.Action = list;
}
}
return actions;
return objectSize(actions) > 0 ? actions : null;
}

getFieldObject() {
Expand Down Expand Up @@ -1597,7 +1600,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
}

const scale = fontSize / 1000;
const whitespace = font.charsToGlyphs(" ", true)[0].width * scale;
const whitespace = font.charsToGlyphs(" ")[0].width * scale;
const chunks = [];

let lastSpacePos = -1,
Expand All @@ -1618,7 +1621,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
lastSpacePos = i;
}
} else {
const charWidth = font.charsToGlyphs(character, false)[0].width * scale;
const charWidth = font.charsToGlyphs(character)[0].width * scale;
if (currentWidth + charWidth > width) {
// We must break to the last white position (if available)
if (lastSpacePos !== -1) {
Expand Down
9 changes: 6 additions & 3 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3208,7 +3208,10 @@ var Font = (function FontClosure() {
return shadow(this, "spaceWidth", width);
},

charToGlyph: function Font_charToGlyph(charcode, isSpace) {
/**
* @private
*/
_charToGlyph(charcode, isSpace = false) {
var fontCharCode, width, operatorListId;

var widthCode = charcode;
Expand Down Expand Up @@ -3332,13 +3335,13 @@ var Font = (function FontClosure() {
i += length;
// Space is char with code 0x20 and length 1 in multiple-byte codes.
var isSpace = length === 1 && chars.charCodeAt(i - 1) === 0x20;
glyph = this.charToGlyph(charcode, isSpace);
glyph = this._charToGlyph(charcode, isSpace);
glyphs.push(glyph);
}
} else {
for (i = 0, ii = chars.length; i < ii; ++i) {
charcode = chars.charCodeAt(i);
glyph = this.charToGlyph(charcode, charcode === 0x20);
glyph = this._charToGlyph(charcode, charcode === 0x20);
glyphs.push(glyph);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
isBool,
isNum,
isString,
objectFromEntries,
objectSize,
PermissionFlag,
shadow,
stringToPDFString,
Expand Down Expand Up @@ -787,7 +787,7 @@ class Catalog {
*/
get openAction() {
const obj = this._catDict.get("OpenAction");
const openActionMap = new Map();
const openAction = Object.create(null);

if (isDict(obj)) {
// Convert the OpenAction dictionary into a format that works with
Expand All @@ -799,17 +799,17 @@ class Catalog {
Catalog.parseDestDictionary({ destDict, resultObj });

if (Array.isArray(resultObj.dest)) {
openActionMap.set("dest", resultObj.dest);
openAction.dest = resultObj.dest;
} else if (resultObj.action) {
openActionMap.set("action", resultObj.action);
openAction.action = resultObj.action;
}
} else if (Array.isArray(obj)) {
openActionMap.set("dest", obj);
openAction.dest = obj;
}
return shadow(
this,
"openAction",
openActionMap.size > 0 ? objectFromEntries(openActionMap) : null
objectSize(openAction) > 0 ? openAction : null
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ function string32(value) {
);
}

function objectSize(obj) {
return Object.keys(obj).length;
}

// Ensures that the returned Object has a `null` prototype.
function objectFromEntries(iterable) {
return Object.assign(Object.create(null), Object.fromEntries(iterable));
Expand Down Expand Up @@ -1040,6 +1044,7 @@ export {
isString,
isSameOrigin,
createValidAbsoluteUrl,
objectSize,
objectFromEntries,
IsLittleEndianCached,
IsEvalSupportedCached,
Expand Down