Class: Idml::Render::StructureTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/idml/render/structure_tracker.rb

Overview

Allocates MCID integers per page and buffers structure-element registrations until Pipeline is ready to flush them. Created once per pipeline run; enabled? is false when tagged: was not requested, in which case every method is a no-op.

Instance Method Summary collapse

Constructor Details

#initialize(enabled: false) ⇒ StructureTracker

Returns a new instance of StructureTracker.



10
11
12
13
14
# File 'lib/idml/render/structure_tracker.rb', line 10

def initialize(enabled: false)
  @enabled = enabled
  @entries = []
  @page_mcids = Hash.new(0)
end

Instance Method Details

#add(type, page_index:, mcid:, text: nil, alt: nil) ⇒ Object



28
29
30
31
32
# File 'lib/idml/render/structure_tracker.rb', line 28

def add(type, page_index:, mcid:, text: nil, alt: nil)
  return unless @enabled

  @entries << Entry.new(type, page_index, mcid, text, alt)
end

#enabled?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/idml/render/structure_tracker.rb', line 16

def enabled?
  @enabled
end

#flush(writer) ⇒ Object

Push buffered entries into the writer in registration order.



35
36
37
38
39
40
41
42
43
44
# File 'lib/idml/render/structure_tracker.rb', line 35

def flush(writer)
  return unless @enabled

  @entries.each do |entry|
    writer.add_structure_element(entry.type, page_index: entry.page_index,
                                             mcid: entry.mcid,
                                             text: entry.text,
                                             alt: entry.alt)
  end
end

#next_mcid(page_index) ⇒ Object

Returns the next MCID for the given page index. MCIDs are per-page in PDF (reset to 0 on each new page).



22
23
24
25
26
# File 'lib/idml/render/structure_tracker.rb', line 22

def next_mcid(page_index)
  current = @page_mcids[page_index]
  @page_mcids[page_index] = current + 1
  current
end