Skip to content

Commit

Permalink
Validate additional font-dictionary properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffleupagus committed Apr 28, 2024
1 parent 90d4b9c commit 85a45bf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ function isWhiteSpace(ch) {
return ch === 0x20 || ch === 0x09 || ch === 0x0d || ch === 0x0a;
}

function isNumberArray(arr, len) {
return (
Array.isArray(arr) &&
arr.length === len &&
arr.every(x => typeof x === "number")
);
}

/**
* AcroForm field names use an array like notation to refer to
* repeated XFA elements e.g. foo.bar[nnn].
Expand Down Expand Up @@ -637,6 +645,7 @@ export {
getRotationMatrix,
getSizeInBytes,
isAscii,
isNumberArray,
isWhiteSpace,
log2,
MissingDataException,
Expand Down
60 changes: 50 additions & 10 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import { getGlyphsUnicode } from "./glyphlist.js";
import { getMetrics } from "./metrics.js";
import { getUnicodeForGlyph } from "./unicode.js";
import { ImageResizer } from "./image_resizer.js";
import { isNumberArray } from "./core_utils.js";
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
import { OperatorList } from "./operator_list.js";
import { PDFImage } from "./image.js";
Expand Down Expand Up @@ -4077,8 +4078,14 @@ class PartialEvaluator {
composite = true;
}

const firstChar = dict.get("FirstChar") || 0,
lastChar = dict.get("LastChar") || (composite ? 0xffff : 0xff);
let firstChar = dict.get("FirstChar");
if (!Number.isInteger(firstChar)) {
firstChar = 0;
}
let lastChar = dict.get("LastChar");
if (!Number.isInteger(lastChar)) {
lastChar = composite ? 0xffff : 0xff;
}
const descriptor = dict.get("FontDescriptor");
const toUnicode = dict.get("ToUnicode") || baseDict.get("ToUnicode");

Expand Down Expand Up @@ -4391,6 +4398,39 @@ class PartialEvaluator {
}
}

let fontMatrix = dict.getArray("FontMatrix");
if (!isNumberArray(fontMatrix, 6)) {
fontMatrix = FONT_IDENTITY_MATRIX;
}
let bbox = descriptor.getArray("FontBBox") || dict.getArray("FontBBox");
if (!isNumberArray(bbox, 4)) {
bbox = undefined;
}
let ascent = descriptor.get("Ascent");
if (typeof ascent !== "number") {
ascent = undefined;
}
let descent = descriptor.get("Descent");
if (typeof descent !== "number") {
descent = undefined;
}
let xHeight = descriptor.get("XHeight");
if (typeof xHeight !== "number") {
xHeight = 0;
}
let capHeight = descriptor.get("CapHeight");
if (typeof capHeight !== "number") {
capHeight = 0;
}
let flags = descriptor.get("Flags");
if (!Number.isInteger(flags)) {
flags = 0;
}
let italicAngle = descriptor.get("ItalicAngle");
if (typeof italicAngle !== "number") {
italicAngle = 0;
}

const properties = {
type,
name: fontName.name,
Expand All @@ -4403,17 +4443,17 @@ class PartialEvaluator {
loadedName: baseDict.loadedName,
composite,
fixedPitch: false,
fontMatrix: dict.getArray("FontMatrix") || FONT_IDENTITY_MATRIX,
fontMatrix,
firstChar,
lastChar,
toUnicode,
bbox: descriptor.getArray("FontBBox") || dict.getArray("FontBBox"),
ascent: descriptor.get("Ascent"),
descent: descriptor.get("Descent"),
xHeight: descriptor.get("XHeight") || 0,
capHeight: descriptor.get("CapHeight") || 0,
flags: descriptor.get("Flags"),
italicAngle: descriptor.get("ItalicAngle") || 0,
bbox,
ascent,
descent,
xHeight,
capHeight,
flags,
italicAngle,
isType3Font,
cssFontInfo,
scaleFactors: glyphScaleFactors,
Expand Down
14 changes: 4 additions & 10 deletions src/core/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "../shared/util.js";
import { PostScriptLexer, PostScriptParser } from "./ps_parser.js";
import { BaseStream } from "./base_stream.js";
import { isNumberArray } from "./core_utils.js";
import { LocalFunctionCache } from "./image_utils.js";

class PDFFunctionFactory {
Expand Down Expand Up @@ -117,16 +118,9 @@ function toNumberArray(arr) {
if (!Array.isArray(arr)) {
return null;
}
const length = arr.length;
for (let i = 0; i < length; i++) {
if (typeof arr[i] !== "number") {
// Non-number is found -- convert all items to numbers.
const result = new Array(length);
for (let j = 0; j < length; j++) {
result[j] = +arr[j];
}
return result;
}
if (!isNumberArray(arr, arr.length)) {
// Non-number is found -- convert all items to numbers.
return arr.map(x => +x);
}
return arr;
}
Expand Down

0 comments on commit 85a45bf

Please sign in to comment.