PDF-Writer

repository·main·Indexed 21 days ago

https://github.com/galkahana/pdf-writer

A high-performance C++ library, formerly known as PDFHummus, designed for creating, parsing, and manipulating PDF files and streams. It supports various image formats and encryption standards, including PDF 2.0 encryption via OpenSSL. The library can be integrated into C++ projects using CMake FetchContent, find_package, or by copying sources.

Tokens
3.4K
Snippets
6
Records
19
Agent score
77%

What's inside pdf-writer

  1. Integrate PDF-Writer into your CMake project

    main

    There are three primary ways to use PDF-Writer in your own C++ projects:

    Automatically downloads and integrates the library during the configuration step. This is the easiest way to manage the dependency.

    Option 2: find_package

    Use this if you have already installed PDF-Writer on your system.

    Option 3: Copy Sources

    Manually copy the PDFWriter source folder into your project and include it in your build system.

    # Option 1: FetchContent
    include(FetchContent)
    FetchContent_Declare(
      PDFHummus
      GIT_REPOSITORY https://github.com/galkahana/PDF-Writer.git
      GIT_TAG        v4.6.2
      FIND_PACKAGE_ARGS
    )
    FetchContent_MakeAvailable(PDFHummus)
    target_link_libraries(YourTarget PDFHummus::PDFWriter)
  2. Install and build PDF-Writer (PDFHummus)

    main

    To build PDF-Writer from source, use CMake to configure the project and then build it using your preferred compiler. Ensure you have a compatible compiler (Visual Studio on Windows, GCC/Clang on Linux/macOS) and CMake installed. If you require PDF 2.0 encryption, ensure OpenSSL is available; otherwise, you can disable it via CMake options.

    Quick Start Build Sequence

    1. Create a build directory.
    2. Configure with CMake.
    3. Build the project in Release mode.
    4. (Optional) Run tests using ctest.
    5. (Optional) Install to a local prefix.
    # Build
    mkdir build && cd build
    cmake ..
    cmake --build . --config Release
    
    # Test
    ctest --test-dir . -C Release
    
    # Install
    cmake --install . --prefix ./install --config Release
  3. What is gxvalid and how to use it

    main

    gxvalid is a module used to validate TrueType GX tables, which are collections of additional tables in TrueType fonts used by Apple Advanced Typography (AAT) and QuickDraw GX Text. It also validates extended kern tables used for AAT.

    Usage Patterns

    • Library Integration: You can link gxvalid with your own program. By validating a font file with gxvalid before running your layout engine, you can reduce the amount of error-checking code required within the engine itself.
    • Stand-alone Validator: You can use gxvalid as a standalone font validator. The ftvalid test program (included in the ft2demo bundle) calls gxvalid internally, making it useful for font developers.
  4. What is ftrandom and how does it work?

    main

    ftrandom is a testing utility for FreeType. It automates the process of generating erroneous font files to test the robustness of the library.

    Workflow:

    1. It selects a directory containing 'good' fonts.
    2. It picks a font randomly based on specified extensions.
    3. It creates a copy of the font and introduces errors (either a specific count of single-byte errors or a fraction of the total file size).
    4. It forks a new tester process to validate the corrupted font. The tester performs the following steps:
      • Initializes the FreeType library.
      • Opens the font file.
      • Loads each glyph.
      • Optionally reviews glyph contours (--check-outlines).
      • Optionally rasterizes the glyph (--rasterize).
      • Closes the face.

    Timeout Handling: If a tester process exceeds 20 seconds, ftrandom saves the erroneous font to the result directory and moves to the next one. If the tester exits normally or with an error within the time limit, the corrupted font is removed.

  5. Handle anti-aliased BDF bitmaps

    main

    The driver supports an extension to the BDF format (used by tools like xmbdfed and Microsoft's SBIT) that allows for anti-aliased bitmaps. This extension adds a fourth field to the SIZE keyword specifying the bits per pixel (bpp).

    Supported bpp values:

    • 1: Default (1 bit per pixel bitmap).
    • 2: 4 gray levels.
    • 4: 16 gray levels.
    • 8: 256 gray levels.

    The driver returns either a 1-bit per pixel bitmap or an 8-bit per pixel pixmap depending on the bpp value provided in the font file.

  6. Understanding kern table versions and dialects

    main

    gxvalid includes a specialized validator for kern tables to handle different versions and platform-specific dialects.

    Versions

    • Classic (16-bit): Version number starts with 0x0000. The number of subtables is a 16-bit value.
    • New (32-bit): Version number starts with 0x00010000. The number of subtables is a 32-bit value. Subtable headers include a tupleIndex variable not present in the classic version.

    Dialects

    Because bit interpretations of the coverage field differ by platform, gxvalid must distinguish between three dialects:

    1. New Apple dialect: 32-bit version.
    2. Classic Apple dialect: 16-bit version; uses specific bit masks for horizontal/vertical and cross-stream settings.
    3. Classic Microsoft dialect: 16-bit version; uses different bit interpretations for coverage.

    Auto-detection Algorithm

    gxvalid uses the first 16 bits to identify the version. If it is a classic version, it attempts to decode the coverage field using the Classic Apple bit masks. If reserved bits are set or the subtable format is incompatible with the Apple dialect, it retries using the Classic Microsoft dialect.

  7. Error handling and validation levels in gxvalid

    main

    gxvalid is designed to be permissive, continuing validation even when encountering broken GX tables, similar to how Apple's rendering engines behave. The behavior depends on the configured validation level:

    • FT_VALIDATE_DEFAULT: Warns about errors and attempts a fallback procedure or continues validation.
    • FT_VALIDATE_TIGHT: Warns and may ignore certain segments or abort on specific errors (like invalid feature numbers in some cases).
    • FT_VALIDATE_PARANOID: Aborts immediately upon finding errors (e.g., too-short LookupTable format 0, broken prop version, or invalid feature numbers).
  8. Validation limitations of gxvalid

    main

    gxvalid checks if layout data in a font conforms to the TrueType GX format specified by Apple, but it has several functional limitations:

    State Machine Validation

    gxvalid can check for 'expression' errors in the state transition diagram (stored in StateTable), such as:

    • Transitions to undefined states.
    • Existence of glyph IDs the State Machine cannot handle.
    • Inability to compute layout information from a given diagram.

    It cannot check:

    • States that the State Machine never actually transits to.
    • Whether the State Machine ever reaches the end of text state.
    • Stack underflow/overflow (the State Machine can store up to 16 glyphs on its stack).
    • temporary glyph IDs used in chained State Machines (e.g., in mort and morx tables).

    Relationship Validation

    gxvalid does not validate the relationship between multiple layout features. It cannot check for conflicts or interactions between different features (e.g., conflicting spacing rules like Text Spacing=Monospace and Ideographic Spacing=Proportional).

  9. Configure PDF-Writer build with CMake options

    main

    You can customize the PDF-Writer build by passing specific flags to CMake. This is useful for reducing dependencies or disabling specific image format support.

    OptionDefaultDescription
    PDFHUMMUS_NO_DCTFALSEExclude DCT decoding support/Detach LibJpeg dependency
    PDFHUMMUS_NO_TIFFFALSEExclude TIFF Images support/Detach LibTiff dependency
    PDFHUMMUS_NO_PNGFALSEExclude PNG Images support/Detatch LibgPng dependency
    PDFHUMMUS_NO_OPENSSLFALSEExclude PDF2.0 encryption/Detach OpenSSL dependency
    USE_BUNDLEDTRUEUse bundled dependencies
    USE_UNBUNDLED_FALLBACK_BUNDLEDFALSEFallback to bundled if system libs not found
    BUILD_FUZZING_HARNESSFALSEEnable fuzz testing

    Example of disabling bundled dependencies: cmake .. -DUSE_BUNDLED=FALSE

    cmake .. -DUSE_BUNDLED=FALSE
  10. Optimize large BDF fonts using PCF conversion

    main

    A known limitation of the BDF driver is that the entire font is loaded into memory at once, which can be inefficient for large fonts.

    To avoid high memory usage, convert your BDF fonts to the PCF (Portable Compiled Format) format using the bdftopcf utility. The FreeType PCF driver supports incremental glyph loading, which is more memory-efficient than the BDF driver.

  11. Compile ftrandom for debugging

    main

    When compiling ftrandom, it is recommended to use debugging tools to catch memory issues or library errors:

    1. Valgrind: Run ftrandom within valgrind to detect memory leaks and invalid accesses.
    2. Sanitizers: Compile FreeType with sanitizer flags (provided by gcc or clang) and then link it with ftrandom to catch runtime errors.