PDF-Forge/src/models/Primitive.svelte.ts
2025-01-31 22:14:00 +01:00

63 lines
1.7 KiB
TypeScript

export default class Primitive {
public key: string;
public ptype: string;
public sub_type: string;
public value: string;
public children: Primitive[];
public trace: Trace[] = $state([]);
constructor(
p: Primitive
) {
this.key = p.key;
this.ptype = p.ptype;
this.sub_type = p.sub_type;
this.value = p.value;
this.children = [];
for (let child of p.children) {
this.children.push(new Primitive(child));
}
this.trace = [];
for (let path of p.trace) {
this.trace.push(path);
}
}
public isContainer() {
return this.ptype === "Dictionary" || this.ptype === "Array" || this.ptype === "Reference" || this.ptype === "Stream";
}
public getPath(): string[] {
return this.trace.map(path => path.key);
}
public getLastJump(): string | number {
let path = this.trace[this.trace.length - 1].last_jump;
if (path === "/") { return path };
return +path;
}
public isPage(): boolean {
return this.trace[0].key.startsWith("Page") && !isNaN(+this.trace[0].key.replace("Page", ""));
}
public getPageNumber(): number {
if (!this.isPage()) {
throw new Error("Primitive is not a page");
}
return +this.trace[0].key.replace("Page", "");
}
public getFirstJump(): string | number | undefined {
let path = this.trace[0].last_jump;
if (path === "Trailer") { return path };
if (path.startsWith("Page")) { return path };
return +path;
}
}
export interface Trace {
readonly key: string;
readonly last_jump: string;
}