Class: Arrolio::Renderer::LinkAnnotator

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

Overview

Collects hyperlink annotations during rendering and emits them as /Annot entries on each PDF page. The renderer calls record_link for each text run that has an href; after the page is fully rendered, flush emits the /Annots array.

Defined Under Namespace

Classes: Link

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ LinkAnnotator

Returns a new instance of LinkAnnotator.



14
15
16
17
# File 'lib/arrolio/renderer/link_annotator.rb', line 14

def initialize(document)
  @document = document
  @pending_links = []
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



12
13
14
# File 'lib/arrolio/renderer/link_annotator.rb', line 12

def document
  @document
end

Returns the value of attribute pending_links.



12
13
14
# File 'lib/arrolio/renderer/link_annotator.rb', line 12

def pending_links
  @pending_links
end

Instance Method Details

#flush(pdfrb_page) ⇒ Object

Emits collected links as /Annot entries on the given pdfrb page. Clears the pending list after emitting.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arrolio/renderer/link_annotator.rb', line 25

def flush(pdfrb_page)
  return if @pending_links.empty?

  annots = @pending_links.map { |link| build_link_annot(link) }
  existing = pdfrb_page.value[:Annots]
  if existing.is_a?(Array)
    existing.concat(annots)
  else
    pdfrb_page.value[:Annots] = annots
  end
  @pending_links.clear
end


19
20
21
# File 'lib/arrolio/renderer/link_annotator.rb', line 19

def record_link(x:, y:, width:, height:, uri:)
  @pending_links << Link.new(x: x, y: y, width: width, height: height, uri: uri)
end