Sign in

    This version of the module is deprecated: Use moonbitlang/pdflite instead

    #pdflite/core

    bobzhang/pdflite/core contains the byte primitives and shared error type used by the rest of pdflite. It deliberately treats PDF data as Bytes and BytesView; callers should decode text only at explicit text-decoding boundaries.

    flowchart LR Ints[integer byte values] --> Guard[pdf_byte_of_int] Guard --> Bytes[pdf_bytes_of_int_array] Bytes --> Views[BytesView APIs] Views --> Names[PdfName] Guard --> Errors[PdfError]

    #Checked Examples

    ///|
    test "byte helpers preserve raw PDF bytes" {
    let bytes = try! pdf_bytes_of_int_array([37, 80, 68, 70])
    if pdf_int_array_of_bytes(bytes) != [37, 80, 68, 70] {
    fail("expected raw PDF header bytes to be preserved")
    }
    inspect(pdf_is_whitespace_byte(32), content="true")
    inspect(pdf_is_delimiter_byte(47), content="true")
    }

    ///|
    test "byte validation raises PdfError" {
    let result : Result[Byte, Error] = try? pdf_byte_of_int(300)
    guard result is Err(PdfError::InvalidByte(300)) else {
    fail("expected InvalidByte for values outside 0..=255")
    }
    }

    #Package Notes

    • PdfBytes is an alias for Bytes to make byte ownership visible at API boundaries.
    • PdfName stores raw name bytes and exposes owned bytes and borrowed views.
    • PdfError is shared by parsing, writing, filtering, text, page, and encryption APIs so higher packages can keep precise failure cases.

    #Pedantic Boundaries

    • This package owns raw PDF byte validation. Values outside 0..=255 must be rejected before they become Byte.
    • This package does not parse PDF objects, streams, pages, fonts, or encryption dictionaries. It only supplies shared primitives for those packages.
    • BytesView parameters are borrowed. Functions returning PdfBytes return owned data that can outlive the caller's source buffer.
    • PdfName does not normalize, unescape, or Unicode-decode names. Higher layers decide when a name has semantic meaning.

    #Verification Notes

    • README examples are blackbox tests for the public core API.
    • Add assertion tests for exact byte arrays and exact PdfError variants.
    • Run moon test core/README.mbt.md after editing this file.
    • Run moon info before review; this README should not change core/pkg.generated.mbti.

    PdfBytes

    type PdfBytes = Bytes

    PDF byte strings and byte streams.

    This is an alias for MoonBit Bytes because PDF strings and streams are byte sequences, not Unicode text. Use String only after an explicit PDF text decoding step.

    PdfError

    pub(all) suberror PdfError {
    InvalidByte(Int)
    InvalidCursorPosition(Int)
    InvalidReadLength(Int)
    InvalidBit(Int)
    InvalidBitCount(Int)
    MatrixNotInvertable
    DictionaryExpected
    DictionaryKeyNotFound(PdfName)
    NumberExpected
    RectangleExpected
    BadRectangle
    MatrixMalformed
    InvalidHexEscape
    InvalidASCII85Data
    FilterNotSupported(PdfName)
    FilterExpected
    CCITTDecodeParmsExpected
    CCITTNotSupported(String)
    InvalidCCITTData
    PredictorExpected
    PredictorNotSupported(Int)
    InvalidLZWData
    InvalidFlateData
    BadJPEGHeader
    BadJPEGString
    InvalidJPEGBlock
    JPEGDimensionsExpected
    BadPNG(String)
    BadJPEG2000Header
    JPEG2000DimensionsExpected
    ParseObjectExpected
    ParseArrayMalformed
    ParseSingleObjectExpected
    ParseIndirectObjectExpected
    ParseEndObjectExpected
    ParseStreamExpected
    ParseEndStreamExpected
    StreamLengthExpected
    StreamDataExpected
    ParsedObjectExpected
    PageTreeExpected
    PageRotationExpected(Int)
    BadPageSpecification(String)
    BadCoordinateSpecification(String)
    BadRectangleSpecification(String)
    BadNumberArgument(String)
    BadPresentationEffect(String)
    SoftError(String)
    HardError(String)
    InsecurePathName(String)
    ContentOperatorExpected
    ContentOperandExpected
    FunctionExpected
    FunctionNotSupported(Int)
    BadFunctionEvaluation(String)
    StartXRefExpected
    XRefExpected
    XRefEntryExpected
    TrailerExpected
    RootExpected
    BadRevision
    InvalidObjectNumber(Int)
    BadDate
    InvalidUnicodeCodepoint(Int)
    InvalidUTF16BE
    InvalidUTF8
    InvalidPDFDocEncoding(Int)
    CryptoKeyExpected
    InvalidCryptoDataLength(Int)
    EncryptionExpected
    EncryptionMethodExpected
    EncryptionDictionaryEntryExpected(PdfName)
    EncryptionDictionaryEntryTooShort(PdfName)
    EncryptionIDExpected
    EncryptionAuthenticationUnsupported(PdfCryptType)
    EncryptionObjectCryptUnsupported(PdfCryptType)
    EncryptionSavedStateExpected
    EncryptionRecryptUnsupported(PdfCryptType)
    EncryptionRecryptFailed
    EncryptionPermissionsCorrupt
    EncryptionPermissionsMismatch(Int, Int)
    RandomBytesUnavailable
    EncodingExpected
    EncodingDifferencesMalformed
    EncodingWriteUnsupported
    FontSubtypeExpected
    FontFlagsExpected
    StandardFontExpected
    BaseFontExpected
    FontFirstCharExpected
    FontLastCharExpected
    FontWidthsExpected
    FontWidthExpected
    FontCharProcsExpected
    FontTypeExpected
    FontWriteUnsupported
    AfmExpected
    ColourSpaceExpected
    BadText
    TitleExpected
    CIDSystemInfoExpected
    CIDRegistryExpected
    CIDOrderingExpected
    CIDSupplementExpected
    CIDBaseFontExpected
    CIDFontDescriptorExpected
    CIDWidthsExpected
    CIDWidths2Expected
    DescendantFontExpected
    CMapEncodingExpected
    PageLabelStyleExpected(String)
    EndOfInput
    } derive(Eq, ToJson,
    Debug
    )

    Error variants raised by pdflite parsing, writing, filtering, text, and encryption APIs.

    The variants intentionally stay specific enough for tests and callers to distinguish malformed input classes, while most public APIs expose them through the ordinary raise PdfError flow.

    PdfCryptType

    pub(all) enum PdfCryptType {
    PdfCryptARC4(Int, Int)
    PdfCryptAESV2
    PdfCryptAESV3(Bool)
    } derive(Eq, ToJson,
    Debug
    )

    Object encryption algorithm used by the PDF security handlers.

    ARC4 carries key length in bits and security-handler revision. AESV3 carries whether the ISO/PDF 2.0 hash variant is in use.

    PdfName

    pub(all) struct PdfName(Bytes) derive(Eq, ToJson,
    Debug
    )

    A PDF name token stored as its raw bytes without the leading slash.

    Names are byte-oriented in the PDF file format. Keeping the original bytes avoids accidental UTF-16 or UTF-8 interpretation while still allowing explicit views and owned-byte access.

    PdfName::byte_length

    fn PdfName::byte_length(self : PdfName) -> Int

    Returns the byte length of this PDF name.

    PdfName::bytes

    fn PdfName::bytes(self : PdfName) -> Bytes

    Returns the owned bytes backing this PDF name.

    PdfName::view

    fn PdfName::view(self : PdfName) -> BytesView

    Returns a read-only view of the bytes backing this PDF name.

    pdf_byte_of_int

    fn pdf_byte_of_int(value : Int) -> Byte raise PdfError

    Converts an integer to a byte.

    Raises PdfError::InvalidByte when value is outside 0..=255.

    pdf_bytes_concat_views

    fn pdf_bytes_concat_views(parts : ArrayView[BytesView]) -> Bytes

    Concatenates byte views into one owned PDF byte sequence.

    pdf_bytes_copy

    fn pdf_bytes_copy(values : BytesView) -> Bytes

    Copies a byte view into owned PDF bytes using indexed construction.

    This is useful when the input may alias data that will be mutated elsewhere.

    pdf_bytes_get

    fn pdf_bytes_get(bytes : Bytes, index : Int) -> Byte?

    Returns the byte at index, or None when the index is out of bounds.

    pdf_bytes_length

    fn pdf_bytes_length(bytes : Bytes) -> Int

    Returns the length of a PDF byte sequence.

    pdf_bytes_make

    fn pdf_bytes_make(length : Int, value? : Int) -> Bytes raise PdfError

    Creates owned PDF bytes of length filled with value.

    The default value is zero. Raises PdfError::InvalidReadLength for negative lengths and PdfError::InvalidByte for values outside 0..=255.

    pdf_bytes_map_int

    fn pdf_bytes_map_int(bytes : BytesView, f : (Int) -> Int) -> Bytes raise PdfError

    Maps every byte through an integer transformation and returns owned bytes.

    The callback receives each input byte as an Int. Raises PdfError::InvalidByte if a mapped value is outside 0..=255.

    pdf_bytes_of_array_view

    fn pdf_bytes_of_array_view(values : ArrayView[Byte]) -> Bytes

    Copies an array view of bytes into owned PDF bytes.

    pdf_bytes_of_int_array

    fn pdf_bytes_of_int_array(values : ArrayView[Int]) -> Bytes raise PdfError

    Converts integer byte values to owned PDF bytes.

    Raises PdfError::InvalidByte if any value is outside 0..=255.

    pdf_bytes_of_int_arrays

    fn pdf_bytes_of_int_arrays(values : ArrayView[ArrayView[Int]]) -> Bytes raise PdfError

    Flattens arrays of integer byte values into owned PDF bytes.

    Raises PdfError::InvalidByte if any value is outside 0..=255.

    pdf_bytes_of_view

    fn pdf_bytes_of_view(values : BytesView) -> Bytes

    Copies a byte view into owned PDF bytes.

    pdf_bytes_to_array

    fn pdf_bytes_to_array(bytes : Bytes) -> Array[Byte]

    Copies PDF bytes into a resizable Array[Byte].

    pdf_bytes_to_fixed_array

    fn pdf_bytes_to_fixed_array(bytes : Bytes) -> FixedArray[Byte]

    Copies PDF bytes into a fixed-size byte array.

    pdf_int_array_of_bytes

    fn pdf_int_array_of_bytes(bytes : BytesView) -> Array[Int]

    Converts bytes to an array of integer byte values.

    pdf_is_delimiter_byte

    fn pdf_is_delimiter_byte(value : Int) -> Bool

    Return whether a byte is a PDF token delimiter.

    Delimiters are parentheses, slash, angle brackets, square brackets, braces, and percent.

    pdf_is_newline_byte

    fn pdf_is_newline_byte(value : Int) -> Bool

    Return whether a byte is a PDF line break byte.

    pdf_is_not_whitespace_byte

    fn pdf_is_not_whitespace_byte(value : Int) -> Bool

    Return whether a byte is not PDF white space.

    pdf_is_whitespace_byte

    fn pdf_is_whitespace_byte(value : Int) -> Bool

    Return whether a byte is PDF white space.

    Recognized values are NUL, horizontal tab, line feed, form feed, carriage return, and space.

    pdf_is_whitespace_or_delimiter_byte

    fn pdf_is_whitespace_or_delimiter_byte(value : Int) -> Bool

    Return whether a byte terminates a regular PDF token.

    pdf_name_of_bytes

    fn pdf_name_of_bytes(bytes : Bytes) -> PdfName

    Wraps owned bytes as a PDF name without copying.

    The bytes are expected to be the name payload without the leading slash.

    pdf_name_of_view

    fn pdf_name_of_view(bytes : BytesView) -> PdfName

    Copies a byte view and wraps it as a PDF name.

    The bytes are expected to be the name payload without the leading slash.

    pdf_string_of_int_array

    fn pdf_string_of_int_array(values : ArrayView[Int]) -> Bytes raise PdfError

    Compatibility alias for converting one integer byte array to PDF bytes.

    pdf_string_of_int_arrays

    fn pdf_string_of_int_arrays(values : ArrayView[ArrayView[Int]]) -> Bytes raise PdfError

    Compatibility alias for pdf_bytes_of_int_arrays.

    CamlPDF models many byte strings as integer lists; this helper preserves that shape for callers ported from OCaml.