1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Shorten some if-statements in the src/core/ folder

Thanks to modern JavaScript there's some existing code that can be shortened slightly.
This commit is contained in:
Jonas Jenwald 2024-12-26 18:37:25 +01:00
parent 4547f230ba
commit 3a797317c8
6 changed files with 16 additions and 26 deletions

View file

@ -1686,12 +1686,12 @@ class MarkupAnnotation extends Annotation {
buffer.push(`${fillColor[0]} ${fillColor[1]} ${fillColor[2]} rg`); buffer.push(`${fillColor[0]} ${fillColor[1]} ${fillColor[2]} rg`);
} }
let pointsArray = this.data.quadPoints; // If there are no quadpoints, the rectangle should be used instead.
if (!pointsArray) { // Convert the rectangle definition to a points array similar to how the
// If there are no quadpoints, the rectangle should be used instead. // quadpoints are defined.
// Convert the rectangle definition to a points array similar to how the const pointsArray =
// quadpoints are defined. this.data.quadPoints ||
pointsArray = Float32Array.from([ Float32Array.from([
this.rectangle[0], this.rectangle[0],
this.rectangle[3], this.rectangle[3],
this.rectangle[2], this.rectangle[2],
@ -1701,7 +1701,6 @@ class MarkupAnnotation extends Annotation {
this.rectangle[2], this.rectangle[2],
this.rectangle[1], this.rectangle[1],
]); ]);
}
for (let i = 0, ii = pointsArray.length; i < ii; i += 8) { for (let i = 0, ii = pointsArray.length; i < ii; i += 8) {
const [mX, MX, mY, MY] = pointsCallback( const [mX, MX, mY, MY] = pointsCallback(
@ -1755,11 +1754,8 @@ class MarkupAnnotation extends Annotation {
} }
static async createNewAnnotation(xref, annotation, changes, params) { static async createNewAnnotation(xref, annotation, changes, params) {
if (!annotation.ref) { const annotationRef = (annotation.ref ||= xref.getNewTemporaryRef());
annotation.ref = xref.getNewTemporaryRef();
}
const annotationRef = annotation.ref;
const ap = await this.createNewAppearanceStream(annotation, xref, params); const ap = await this.createNewAppearanceStream(annotation, xref, params);
let annotationDict; let annotationDict;

View file

@ -567,10 +567,7 @@ class Catalog {
const onParsed = []; const onParsed = [];
if (Array.isArray(refs)) { if (Array.isArray(refs)) {
for (const value of refs) { for (const value of refs) {
if (!(value instanceof Ref)) { if (value instanceof Ref && groupRefCache.has(value)) {
continue;
}
if (groupRefCache.has(value)) {
onParsed.push(value.toString()); onParsed.push(value.toString());
} }
} }
@ -629,7 +626,7 @@ class Catalog {
return null; return null;
} }
const nestedOrder = parseOrder(value.slice(1), nestedLevels); const nestedOrder = parseOrder(value.slice(1), nestedLevels);
if (!nestedOrder || !nestedOrder.length) { if (!nestedOrder?.length) {
return null; return null;
} }
return { name: stringToPDFString(nestedName), order: nestedOrder }; return { name: stringToPDFString(nestedName), order: nestedOrder };

View file

@ -466,7 +466,7 @@ const blackTable3 = [
*/ */
class CCITTFaxDecoder { class CCITTFaxDecoder {
constructor(source, options = {}) { constructor(source, options = {}) {
if (!source || typeof source.next !== "function") { if (typeof source?.next !== "function") {
throw new Error('CCITTFaxDecoder - invalid "source" parameter.'); throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
} }
this.source = source; this.source = source;

View file

@ -36,7 +36,7 @@ class DecryptStream extends DecodeStream {
chunk = this.str.getBytes(chunkSize); chunk = this.str.getBytes(chunkSize);
this.initialized = true; this.initialized = true;
} }
if (!chunk || chunk.length === 0) { if (!chunk?.length) {
this.eof = true; this.eof = true;
return; return;
} }

View file

@ -818,7 +818,7 @@ class CompiledFont {
} }
compileGlyph(code, glyphId) { compileGlyph(code, glyphId) {
if (!code || code.length === 0 || code[0] === 14) { if (!code?.length || code[0] === 14) {
return NOOP; return NOOP;
} }

View file

@ -310,18 +310,15 @@ class XRef {
if (!("streamState" in this)) { if (!("streamState" in this)) {
// Stores state of the stream as we process it so we can resume // Stores state of the stream as we process it so we can resume
// from middle of stream in case of missing data error // from middle of stream in case of missing data error
const streamParameters = stream.dict; const { dict, pos } = stream;
const byteWidths = streamParameters.get("W"); const byteWidths = dict.get("W");
let range = streamParameters.get("Index"); const range = dict.get("Index") || [0, dict.get("Size")];
if (!range) {
range = [0, streamParameters.get("Size")];
}
this.streamState = { this.streamState = {
entryRanges: range, entryRanges: range,
byteWidths, byteWidths,
entryNum: 0, entryNum: 0,
streamPos: stream.pos, streamPos: pos,
}; };
} }
this.readXRefStream(stream); this.readXRefStream(stream);