Merge pull request #12552 from Snuffleupagus/annotation-fixes

Miscellaneous (small) improvements in `src/core/annotation.js`
This commit is contained in:
Tim van der Meij 2020-10-31 00:41:39 +01:00 committed by GitHub
commit 46e60a266c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 22 deletions

View File

@ -25,6 +25,7 @@ import {
escapeString, escapeString,
getModificationDate, getModificationDate,
isString, isString,
objectSize,
OPS, OPS,
shadow, shadow,
stringToPDFString, stringToPDFString,
@ -1326,7 +1327,7 @@ class WidgetAnnotation extends Annotation {
_computeFontSize(font, fontName, fontSize, height) { _computeFontSize(font, fontName, fontSize, height) {
if (fontSize === null || fontSize === 0) { 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) // According to https://en.wikipedia.org/wiki/Em_(typography)
// an average cap height should be 70% of 1em // an average cap height should be 70% of 1em
const capHeight = 0.7 * em; const capHeight = 0.7 * em;
@ -1460,18 +1461,20 @@ class WidgetAnnotation extends Annotation {
if (dict.has("AA")) { if (dict.has("AA")) {
const additionalActions = dict.get("AA"); const additionalActions = dict.get("AA");
for (const key of additionalActions.getKeys()) { for (const key of additionalActions.getKeys()) {
if (key in AnnotationActionEventType) { const action = AnnotationActionEventType[key];
if (!action) {
continue;
}
const actionDict = additionalActions.getRaw(key); const actionDict = additionalActions.getRaw(key);
const parents = new RefSet(); const parents = new RefSet();
const list = []; const list = [];
this._collectJS(actionDict, xref, list, parents); this._collectJS(actionDict, xref, list, parents);
if (list.length > 0) { if (list.length > 0) {
actions[AnnotationActionEventType[key]] = list; 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")) { if (dict.has("A")) {
const actionDict = dict.get("A"); const actionDict = dict.get("A");
const parents = new RefSet(); const parents = new RefSet();
@ -1481,7 +1484,7 @@ class WidgetAnnotation extends Annotation {
actions.Action = list; actions.Action = list;
} }
} }
return actions; return objectSize(actions) > 0 ? actions : null;
} }
getFieldObject() { getFieldObject() {
@ -1597,7 +1600,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
} }
const scale = fontSize / 1000; const scale = fontSize / 1000;
const whitespace = font.charsToGlyphs(" ", true)[0].width * scale; const whitespace = font.charsToGlyphs(" ")[0].width * scale;
const chunks = []; const chunks = [];
let lastSpacePos = -1, let lastSpacePos = -1,
@ -1618,7 +1621,7 @@ class TextWidgetAnnotation extends WidgetAnnotation {
lastSpacePos = i; lastSpacePos = i;
} }
} else { } else {
const charWidth = font.charsToGlyphs(character, false)[0].width * scale; const charWidth = font.charsToGlyphs(character)[0].width * scale;
if (currentWidth + charWidth > width) { if (currentWidth + charWidth > width) {
// We must break to the last white position (if available) // We must break to the last white position (if available)
if (lastSpacePos !== -1) { if (lastSpacePos !== -1) {

View File

@ -3208,7 +3208,10 @@ var Font = (function FontClosure() {
return shadow(this, "spaceWidth", width); return shadow(this, "spaceWidth", width);
}, },
charToGlyph: function Font_charToGlyph(charcode, isSpace) { /**
* @private
*/
_charToGlyph(charcode, isSpace = false) {
var fontCharCode, width, operatorListId; var fontCharCode, width, operatorListId;
var widthCode = charcode; var widthCode = charcode;
@ -3332,13 +3335,13 @@ var Font = (function FontClosure() {
i += length; i += length;
// Space is char with code 0x20 and length 1 in multiple-byte codes. // Space is char with code 0x20 and length 1 in multiple-byte codes.
var isSpace = length === 1 && chars.charCodeAt(i - 1) === 0x20; var isSpace = length === 1 && chars.charCodeAt(i - 1) === 0x20;
glyph = this.charToGlyph(charcode, isSpace); glyph = this._charToGlyph(charcode, isSpace);
glyphs.push(glyph); glyphs.push(glyph);
} }
} else { } else {
for (i = 0, ii = chars.length; i < ii; ++i) { for (i = 0, ii = chars.length; i < ii; ++i) {
charcode = chars.charCodeAt(i); charcode = chars.charCodeAt(i);
glyph = this.charToGlyph(charcode, charcode === 0x20); glyph = this._charToGlyph(charcode, charcode === 0x20);
glyphs.push(glyph); glyphs.push(glyph);
} }
} }

View File

@ -25,7 +25,7 @@ import {
isBool, isBool,
isNum, isNum,
isString, isString,
objectFromEntries, objectSize,
PermissionFlag, PermissionFlag,
shadow, shadow,
stringToPDFString, stringToPDFString,
@ -828,7 +828,7 @@ class Catalog {
*/ */
get openAction() { get openAction() {
const obj = this._catDict.get("OpenAction"); const obj = this._catDict.get("OpenAction");
const openActionMap = new Map(); const openAction = Object.create(null);
if (isDict(obj)) { if (isDict(obj)) {
// Convert the OpenAction dictionary into a format that works with // Convert the OpenAction dictionary into a format that works with
@ -840,17 +840,17 @@ class Catalog {
Catalog.parseDestDictionary({ destDict, resultObj }); Catalog.parseDestDictionary({ destDict, resultObj });
if (Array.isArray(resultObj.dest)) { if (Array.isArray(resultObj.dest)) {
openActionMap.set("dest", resultObj.dest); openAction.dest = resultObj.dest;
} else if (resultObj.action) { } else if (resultObj.action) {
openActionMap.set("action", resultObj.action); openAction.action = resultObj.action;
} }
} else if (Array.isArray(obj)) { } else if (Array.isArray(obj)) {
openActionMap.set("dest", obj); openAction.dest = obj;
} }
return shadow( return shadow(
this, this,
"openAction", "openAction",
openActionMap.size > 0 ? objectFromEntries(openActionMap) : null objectSize(openAction) > 0 ? openAction : null
); );
} }

View File

@ -585,6 +585,10 @@ function string32(value) {
); );
} }
function objectSize(obj) {
return Object.keys(obj).length;
}
// Ensures that the returned Object has a `null` prototype. // Ensures that the returned Object has a `null` prototype.
function objectFromEntries(iterable) { function objectFromEntries(iterable) {
return Object.assign(Object.create(null), Object.fromEntries(iterable)); return Object.assign(Object.create(null), Object.fromEntries(iterable));
@ -1040,6 +1044,7 @@ export {
isString, isString,
isSameOrigin, isSameOrigin,
createValidAbsoluteUrl, createValidAbsoluteUrl,
objectSize,
objectFromEntries, objectFromEntries,
IsLittleEndianCached, IsLittleEndianCached,
IsEvalSupportedCached, IsEvalSupportedCached,