pdf-lib
Version:
Create and modify PDF files with JavaScript
51 lines (39 loc) • 1.15 kB
text/typescript
import PDFObject from 'src/core/objects/PDFObject';
import CharCodes from 'src/core/syntax/CharCodes';
import {
copyStringIntoBuffer,
toHexStringOfMinLength,
utf16Encode,
} from 'src/utils';
class PDFHexString extends PDFObject {
static of = (value: string) => new PDFHexString(value);
static fromText = (value: string) => {
const encoded = utf16Encode(value);
let hex = '';
for (let idx = 0, len = encoded.length; idx < len; idx++) {
hex += toHexStringOfMinLength(encoded[idx], 4);
}
return new PDFHexString(hex);
};
private readonly value: string;
constructor(value: string) {
super();
this.value = value;
}
clone(): PDFHexString {
return PDFHexString.of(this.value);
}
toString(): string {
return `<${this.value}>`;
}
sizeInBytes(): number {
return this.value.length + 2;
}
copyBytesInto(buffer: Uint8Array, offset: number): number {
buffer[offset++] = CharCodes.LessThan;
offset += copyStringIntoBuffer(this.value, buffer, offset);
buffer[offset++] = CharCodes.GreaterThan;
return this.value.length + 2;
}
}
export default PDFHexString;