Class: Pdfrb::Content::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/content/processor.rb

Overview

Walks a content stream through the operator registry, updating the current GraphicsState and dispatching to per-operator hooks (paint_path, show_text, ...). Subclasses override the hooks to render / extract / re-emit.

Mirrors HexaPDF::Content::Processor.

Direct Known Subclasses

Task::ExtractText::TextCollector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



14
15
16
17
# File 'lib/pdfrb/content/processor.rb', line 14

def initialize
  @graphics_state = GraphicsState.new
  @state_stack = []
end

Instance Attribute Details

#graphics_stateObject (readonly)

Returns the value of attribute graphics_state.



12
13
14
# File 'lib/pdfrb/content/processor.rb', line 12

def graphics_state
  @graphics_state
end

#state_stackObject (readonly)

Returns the value of attribute state_stack.



12
13
14
# File 'lib/pdfrb/content/processor.rb', line 12

def state_stack
  @state_stack
end

Instance Method Details

#apply_extgstate(_name) ⇒ Object



74
# File 'lib/pdfrb/content/processor.rb', line 74

def apply_extgstate(_name); end

#begin_marked_content(_tag, _properties = nil) ⇒ Object



76
# File 'lib/pdfrb/content/processor.rb', line 76

def begin_marked_content(_tag, _properties = nil); end

#end_marked_contentObject



77
# File 'lib/pdfrb/content/processor.rb', line 77

def end_marked_content; end

#marked_content_point(_tag, _properties = nil) ⇒ Object



78
# File 'lib/pdfrb/content/processor.rb', line 78

def marked_content_point(_tag, _properties = nil); end

#move_text(tx, ty, set_leading: false, neg_leading: false) ⇒ Object

---- Text positioning ----



49
50
51
52
53
54
55
56
57
# File 'lib/pdfrb/content/processor.rb', line 49

def move_text(tx, ty, set_leading: false, neg_leading: false)
  ts = @graphics_state.text_state
  ts = ts.with(leading: -ty.to_f) if set_leading && neg_leading
  ts = ts.with(leading: ty.to_f) if set_leading && !neg_leading
  translation = Pdfrb::Model::Matrix.translate(tx.to_f, ty.to_f)
  new_line = translation * ts.line_matrix
  new_text = translation * ts.text_matrix
  update_text_state(line_matrix: new_line, text_matrix: new_text, leading: ts.leading)
end

#paint_path(fill:, stroke:, close:, rule:) ⇒ Object



67
# File 'lib/pdfrb/content/processor.rb', line 67

def paint_path(fill:, stroke:, close:, rule:); end

#path_closeObject



71
# File 'lib/pdfrb/content/processor.rb', line 71

def path_close; end

#path_curve_to(_p1, _p2, _p3) ⇒ Object



70
# File 'lib/pdfrb/content/processor.rb', line 70

def path_curve_to(_p1, _p2, _p3); end

#path_endObject



73
# File 'lib/pdfrb/content/processor.rb', line 73

def path_end; end

#path_line_to(_x, _y) ⇒ Object



69
# File 'lib/pdfrb/content/processor.rb', line 69

def path_line_to(_x, _y); end

#path_move_to(_x, _y) ⇒ Object



68
# File 'lib/pdfrb/content/processor.rb', line 68

def path_move_to(_x, _y); end

#path_rectangle(_x, _y, _w, _h) ⇒ Object



72
# File 'lib/pdfrb/content/processor.rb', line 72

def path_rectangle(_x, _y, _w, _h); end

#pop_graphics_stateObject



34
35
36
# File 'lib/pdfrb/content/processor.rb', line 34

def pop_graphics_state
  @graphics_state = @state_stack.pop || @graphics_state
end

#process(io_or_string) ⇒ Object

Feed bytes (or IO) through the parser, invoking each operator against this processor.



21
22
23
24
25
26
# File 'lib/pdfrb/content/processor.rb', line 21

def process(io_or_string)
  Parser.parse(io_or_string).each_invocation do |op_class, operands|
    op_class.invoke(self, *operands)
  end
  self
end

#push_graphics_stateObject

---- Graphics-state stack ----



30
31
32
# File 'lib/pdfrb/content/processor.rb', line 30

def push_graphics_state
  @state_stack.push(@graphics_state)
end

#set_text_matrix(m) ⇒ Object



59
60
61
# File 'lib/pdfrb/content/processor.rb', line 59

def set_text_matrix(m)
  update_text_state(text_matrix: m, line_matrix: m)
end

#show_text(_str) ⇒ Object

---- Hooks (subclasses override) ----



65
# File 'lib/pdfrb/content/processor.rb', line 65

def show_text(_str); end

#show_text_array(_array) ⇒ Object



66
# File 'lib/pdfrb/content/processor.rb', line 66

def show_text_array(_array); end

#update_graphics_state(**overrides) ⇒ Object



38
39
40
# File 'lib/pdfrb/content/processor.rb', line 38

def update_graphics_state(**overrides)
  @graphics_state = @graphics_state.with(**overrides)
end

#update_text_state(**overrides) ⇒ Object



42
43
44
45
# File 'lib/pdfrb/content/processor.rb', line 42

def update_text_state(**overrides)
  new_ts = @graphics_state.text_state.with(**overrides)
  @graphics_state = @graphics_state.with(text_state: new_ts)
end