Skip to content

Commit

Permalink
Merge pull request #19388 from Snuffleupagus/readInt16
Browse files Browse the repository at this point in the history
Introduce a `readInt16` helper function in the `src/core/core_utils.js` file
  • Loading branch information
Snuffleupagus authored Jan 29, 2025
2 parents b0b9552 + 237a17a commit 786ac2f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/core/cff_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ISOAdobeCharset,
} from "./charsets.js";
import { ExpertEncoding, StandardEncoding } from "./encodings.js";
import { readInt16 } from "./core_utils.js";

// Maximum subroutine call depth of type 2 charstrings. Matches OTS.
const MAX_SUBR_NESTING = 10;
Expand Down Expand Up @@ -359,8 +360,8 @@ class CFFParser {
if (value === 30) {
return parseFloatOperand();
} else if (value === 28) {
value = dict[pos++];
value = ((value << 24) | (dict[pos++] << 16)) >> 16;
value = readInt16(dict, pos);
pos += 2;
return value;
} else if (value === 29) {
value = dict[pos++];
Expand Down Expand Up @@ -510,7 +511,7 @@ class CFFParser {
}
} else if (value === 28) {
// number (16 bit)
stack[stackSize] = ((data[j] << 24) | (data[j + 1] << 16)) >> 16;
stack[stackSize] = readInt16(data, j);
j += 2;
stackSize++;
} else if (value === 14) {
Expand Down
5 changes: 5 additions & 0 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ function readInt8(data, offset) {
return (data[offset] << 24) >> 24;
}

function readInt16(data, offset) {
return ((data[offset] << 24) | (data[offset + 1] << 16)) >> 16;
}

function readUint16(data, offset) {
return (data[offset] << 8) | data[offset + 1];
}
Expand Down Expand Up @@ -733,6 +737,7 @@ export {
ParserEOFException,
parseXFAPath,
PDF_VERSION_REGEXP,
readInt16,
readInt8,
readUint16,
readUint32,
Expand Down
19 changes: 8 additions & 11 deletions src/core/font_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "../shared/util.js";
import {
isNumberArray,
readInt16,
readInt8,
readUint16,
readUint32,
Expand All @@ -35,12 +36,8 @@ import { Stream } from "./stream.js";

// TODO: use DataView and its methods.

function getInt16(data, offset) {
return ((data[offset] << 24) | (data[offset + 1] << 16)) >> 16;
}

function getFloat214(data, offset) {
return getInt16(data, offset) / 16384;
return readInt16(data, offset) / 16384;
}

function getSubroutineBias(subrs) {
Expand Down Expand Up @@ -185,7 +182,7 @@ function compileGlyf(code, cmds, font) {
}

let i = 0;
const numberOfContours = getInt16(code, i);
const numberOfContours = readInt16(code, i);
let flags;
let firstPoint = null;
let x = 0,
Expand All @@ -200,8 +197,8 @@ function compileGlyf(code, cmds, font) {
let arg1, arg2;
if (flags & 0x01) {
if (flags & 0x02) {
arg1 = getInt16(code, i);
arg2 = getInt16(code, i + 2);
arg1 = readInt16(code, i);
arg2 = readInt16(code, i + 2);
} else {
arg1 = readUint16(code, i);
arg2 = readUint16(code, i + 2);
Expand Down Expand Up @@ -279,7 +276,7 @@ function compileGlyf(code, cmds, font) {
for (j = 0; j < numberOfPoints; j++) {
switch (points[j].flags & 0x12) {
case 0x00:
x += getInt16(code, i);
x += readInt16(code, i);
i += 2;
break;
case 0x02:
Expand All @@ -294,7 +291,7 @@ function compileGlyf(code, cmds, font) {
for (j = 0; j < numberOfPoints; j++) {
switch (points[j].flags & 0x24) {
case 0x00:
y += getInt16(code, i);
y += readInt16(code, i);
i += 2;
break;
case 0x04:
Expand Down Expand Up @@ -653,7 +650,7 @@ function compileCharString(charStringCode, cmds, font, glyphId) {
}
break;
case 28:
stack.push(((code[i] << 24) | (code[i + 1] << 16)) >> 16);
stack.push(readInt16(code, i));
i += 2;
break;
case 29: // callgsubr
Expand Down

0 comments on commit 786ac2f

Please sign in to comment.