PdfObjectWriter
Serializes the PdfObject model back to spec-compliant PDF syntax (ISO 32000-1 §7.3) — the inverse of io.github.yuroyami.kitepdf.parser.Parser.
The output is designed to round-trip exactly through KitePDF's own Lexer/io.github.yuroyami.kitepdf.parser.Parser and to be accepted by other conformant readers (verified against MuPDF's mutool). Two non-obvious constraints drive the formatting choices here:
Reals never use exponent notation. Kotlin's
Double.toString()happily emits1.0E-5, but PDF reals are digits-and-one-dot only (§7.3.3) and our lexer rejects theE. See formatReal.Strings and names are escaped losslessly. A literal string is only used when every byte is printable ASCII; otherwise we emit a hex string, so control bytes (and UTF-16BE text) survive without the lexer's CR→LF normalization corrupting them.
Streams are written with a /Length that always matches the raw byte count, overriding any stale or indirect /Length in the source dictionary.
Functions
Format a double as a PDF real: a sign, digits, an optional dot, and more digits — never an exponent. Rounded to 6 decimal places (ample for coordinates and matrices) with trailing zeros trimmed. NaN/Infinity are not representable in PDF, so they degrade to "0".