$ show parallax/jsPDF
jsPDF is a JavaScript library for generating PDF documents in browser and Node.js environments. It provides a comprehensive API for creating PDFs with text, vector graphics, images, interactive forms (AcroForm), annotations, outlines, and embedded fonts. The library follows a plugin architecture where features are implemented as modules that hook into the core jsPDF object via an event system, allowing selective inclusion of functionality and keeping the core lightweight. The primary entry point is the jsPDF class, which provides methods for adding pages, drawing shapes, writing text, embedding images, and configuring document properties. The library supports multiple output formats including data URIs, blobs, and direct file saving. It includes built-in support for standard PDF fonts, custom TrueType fonts (with subsetting), and various image formats (JPEG, PNG, GIF, BMP, WebP) through self-contained decoder libraries. Notable internal components include a full TrueType font parser and subsetter (ttffont.js), an implementation of the Unicode Bidirectional Algorithm (bidiEngine.js), a WebP image decoder ported from libwebp (WebPDecoder.js), and a JPEG encoder (JPEGEncoder.js). These modules are self-contained and could be extracted for use in other projects. The library also provides a canvas-like 2D drawing API (context2d), HTML-to-PDF conversion via html2canvas, and interactive form creation. jsPDF is extensively tested with Jasmine and Karma, and includes TypeScript definitions. It is distributed as ES modules, UMD, and Node.js builds. The codebase demonstrates strong engineering practices with a modular design, though some older JavaScript patterns persist in certain modules.
The LLM identified 4 files worth highlighting for reuse:
Implements the Unicode Bidirectional Algorithm for reordering text in left-to-right and right-to-left scripts. Contains a hardcoded Unicode character type table derived from Unicode 8.0.0 and a state machine for logical-to-visual reordering. Self-contained and could be extracted for use in any text rendering system.
A complete TrueType font parser and subsetter. Parses all major TTF tables (head, name, cmap, hhea, maxp, hmtx, post, OS/2, loca, glyf) and provides a Subset class to embed only used glyphs. Self-contained binary data reader/writer. Could be reused in any project requiring font manipulation.
A full JavaScript implementation of WebP image decoding (both lossy VP8 and lossless VP8L formats). Parses RIFF/WEBP containers, handles animation frames, alpha channels, and color transforms. Ported from Google's libwebp C library. Self-contained and could be used independently for WebP decoding.
A JavaScript JPEG encoder that converts raw pixel data to JPEG format. Implements DCT, quantization, Huffman coding, and JFIF header generation. Self-contained and could be extracted for image processing tasks.