Class: Arrolio::Renderer::StructureTreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/renderer/structure_tree_builder.rb

Overview

Builds a PDF structure tree (/StructTreeRoot) for PDF/UA compliance. Walks the Output::Page tree and creates /StructElem entries for each content block (paragraphs, headings, tables, figures). The structure tree maps marked content sequences to semantic roles so assistive technology can navigate the document.

The builder is a foundation — full PDF/UA compliance also requires /Alt text (AccessibilityTagger), reading order, and /ActualText for non-text content. This builder creates the tree skeleton; the renderer fills in marked content refs.

Defined Under Namespace

Classes: StructEntry

Constant Summary collapse

STRUCT_TYPES =

Standard PDF structure types from PDF 1.7 Table 10.20.

{
  document: :Document,
  part: :Part,
  section: :Sect,
  heading: :H,
  heading1: :H1,
  heading2: :H2,
  heading3: :H3,
  paragraph: :P,
  table: :Table,
  table_row: :TR,
  table_header: :TH,
  table_data: :TD,
  list: :L,
  list_item: :LI,
  figure: :Figure,
  caption: :Caption,
  footnote: :Footnote,
  link: :Link,
  quote: :BlockQuote,
  code: :Code
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ StructureTreeBuilder

Returns a new instance of StructureTreeBuilder.



43
44
45
46
47
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 43

def initialize(document)
  @document = document
  @entries = []
  @next_mcid = 0
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



41
42
43
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 41

def document
  @document
end

#entriesObject (readonly)

Returns the value of attribute entries.



41
42
43
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 41

def entries
  @entries
end

Instance Method Details

#buildObject

Builds and attaches the /StructTreeRoot to the document catalog. Returns the root reference or nil if no entries.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 69

def build
  return nil if @entries.empty?

  struct_elements = @entries.map { |e| build_struct_elem(e) }
  root = @document.add(
    { Type: :StructTreeRoot,
      K: struct_elements.map { |ref| ref_for(ref) } },
    type: Pdfrb::Model::Cos::Dictionary
  )
  @document.catalog.value[:StructTreeRoot] =
    Pdfrb::Model::Reference.new(root.oid, root.gen)
  root
end

#countObject



83
84
85
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 83

def count
  @entries.length
end

#empty?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 87

def empty?
  @entries.empty?
end

#record(type:, page_number:, text: nil, alt: nil) ⇒ Object

Records a structure element for a content block. type: one of STRUCT_TYPES keys. page_number: the page where the content appears. text: optional text content for the /Alt entry. alt: optional alt text for non-text content.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arrolio/renderer/structure_tree_builder.rb', line 54

def record(type:, page_number:, text: nil, alt: nil)
  entry = StructEntry.new(
    type: struct_type_for(type),
    page_number: page_number,
    mcid: @next_mcid,
    text: text,
    alt: alt
  )
  @entries << entry
  @next_mcid += 1
  entry
end