Let BinaryCMapStream extend the Stream class

Looking at the `BinaryCMapStream` implementation, it's basically a "regular" `Stream` but with added functionality for reading compressed CMap data.
Hence, by letting `BinaryCMapStream` extend `Stream`, we can remove an effectively duplicate method and simplify/shorten the code a tiny bit.
This commit is contained in:
Jonas Jenwald 2026-03-05 11:45:29 +01:00
parent 4d0709c174
commit fccee4bffd

View File

@ -14,6 +14,7 @@
*/ */
import { FormatError } from "../shared/util.js"; import { FormatError } from "../shared/util.js";
import { Stream } from "./stream.js";
function hexToInt(a, size) { function hexToInt(a, size) {
let n = 0; let n = 0;
@ -56,26 +57,23 @@ function incHex(a, size) {
const MAX_NUM_SIZE = 16; const MAX_NUM_SIZE = 16;
const MAX_ENCODED_NUM_SIZE = 19; // ceil(MAX_NUM_SIZE * 7 / 8) const MAX_ENCODED_NUM_SIZE = 19; // ceil(MAX_NUM_SIZE * 7 / 8)
class BinaryCMapStream { class BinaryCMapStream extends Stream {
constructor(data) { tmpBuf = new Uint8Array(MAX_ENCODED_NUM_SIZE);
this.buffer = data;
this.pos = 0;
this.end = data.length;
this.tmpBuf = new Uint8Array(MAX_ENCODED_NUM_SIZE);
}
readByte() { constructor(data) {
if (this.pos >= this.end) { super(
return -1; /* arrayBuffer = */ data,
} /* start = */ 0,
return this.buffer[this.pos++]; /* length = */ data.length,
/* dict = */ null
);
} }
readNumber() { readNumber() {
let n = 0; let n = 0;
let last; let last;
do { do {
const b = this.readByte(); const b = this.getByte();
if (b < 0) { if (b < 0) {
throw new FormatError("unexpected EOF in bcmap"); throw new FormatError("unexpected EOF in bcmap");
} }
@ -91,8 +89,7 @@ class BinaryCMapStream {
} }
readHex(num, size) { readHex(num, size) {
num.set(this.buffer.subarray(this.pos, this.pos + size + 1)); num.set(this.getBytes(size + 1));
this.pos += size + 1;
} }
readHexNumber(num, size) { readHexNumber(num, size) {
@ -100,7 +97,7 @@ class BinaryCMapStream {
const stack = this.tmpBuf; const stack = this.tmpBuf;
let sp = 0; let sp = 0;
do { do {
const b = this.readByte(); const b = this.getByte();
if (b < 0) { if (b < 0) {
throw new FormatError("unexpected EOF in bcmap"); throw new FormatError("unexpected EOF in bcmap");
} }
@ -145,7 +142,7 @@ class BinaryCMapStream {
class BinaryCMapReader { class BinaryCMapReader {
async process(data, cMap, extend) { async process(data, cMap, extend) {
const stream = new BinaryCMapStream(data); const stream = new BinaryCMapStream(data);
const header = stream.readByte(); const header = stream.getByte();
cMap.vertical = !!(header & 1); cMap.vertical = !!(header & 1);
let useCMap = null; let useCMap = null;
@ -157,7 +154,7 @@ class BinaryCMapReader {
let code; let code;
let b; let b;
while ((b = stream.readByte()) >= 0) { while ((b = stream.getByte()) >= 0) {
const type = b >> 5; const type = b >> 5;
if (type === 7) { if (type === 7) {
// metadata, e.g. comment or usecmap // metadata, e.g. comment or usecmap