pdftract/notes/pdftract-cbrbg.md
2026-05-24 07:29:31 -04:00

2.8 KiB

Verification Note: pdftract-cbrbg

Summary

Implemented span flag detector for Phase 4.1 as specified in the plan.

Implementation Details

Files Modified

  • crates/pdftract-core/src/lib.rs - Added span_flags module
  • crates/pdftract-core/src/span_flags.rs - New module (467 lines)

Functionality

Implemented detect_span_flags() function that returns a u8 bitmask combining 5 style flag bits:

  • flags::BOLD (bit 0)
  • flags::ITALIC (bit 1)
  • flags::SMALLCAPS (bit 2)
  • flags::SUBSCRIPT (bit 3)
  • flags::SUPERSCRIPT (bit 4)

Detection Logic (per plan lines 1667-1671)

BOLD detection (set if ANY of):

  • Font name contains "Bold", "Bd", "Black", "Heavy", "ExtraBold", etc.
  • FontDescriptor /Flags bit 18 (ForceBold) set
  • FontDescriptor /StemV > 120

ITALIC detection (set if ANY of):

  • Font name contains "Italic" or "Oblique"
  • FontDescriptor /ItalicAngle != 0

SMALLCAPS detection (set if ANY of):

  • Font name contains "SC", "SmallCaps", or ".sc"
  • FontDescriptor /Flags bit 3 (Symbolic/SmallCap) set

SUBSCRIPT detection:

  • text_rise < -0.1 * font_size

SUPERSCRIPT detection:

  • text_rise > 0.1 * font_size

API

use pdftract_core::span_flags::{detect_span_flags, FontInfo, flags};

let font = FontInfo::new()
    .with_name("Times-Bold".to_string())
    .with_flags(0x40000) // ForceBold bit
    .with_stem_v(150.0);

let span_flags = detect_span_flags(&font, -2.0, 12.0);
assert!(span_flags & flags::BOLD != 0);
assert!(span_flags & flags::SUBSCRIPT != 0);

Acceptance Criteria Status

PASS

  • "Times-Bold" font → BOLD set
  • "Helvetica-Italic" font → ITALIC set
  • "Times-BoldItalic" font → BOLD | ITALIC set
  • text_rise -2pt with font_size 12pt → SUBSCRIPT set (rise/size = -0.167 < -0.1)
  • text_rise +1.5pt with font_size 12pt → SUPERSCRIPT set
  • text_rise -0.5pt with font_size 12pt → NEITHER (rise/size = -0.042, within threshold)
  • font.descriptor.flags bit 18 set → BOLD set
  • /StemV 150 → BOLD set

Additional Test Coverage

  • Subset prefix handling (ABCDEF+Times-Bold)
  • Small caps with ".sc" suffix pattern
  • ItalicAngle detection (non-zero angle)
  • All flags combination (BoldItalic + SmallCaps + Superscript)
  • Zero font_size edge case (no division by zero)
  • Mutually exclusive SUB/SUPER (can't have both)

Test Results

cargo test -p pdftract-core span_flags --lib
test result: ok. 27 passed; 0 failed; 0 ignored; 0 measured

Quality Gates

  • cargo check --all-targets - PASSED
  • cargo clippy --all-targets -- -D warnings - PASSED
  • cargo fmt - APPLIED
  • All tests pass

Git Commit

  • Commit: cad7d2c
  • Message: feat(pdftract-cbrbg): implement span flag detector for Phase 4.1
  • Pushed to: github/main

References

  • Plan: Phase 4.1 Flag detection (lines 1648-1653, 1667-1671)
  • Bead: pdftract-cbrbg