Class: Origami::PDF::Instruction

Inherits:
Object
  • Object
show all
Defined in:
lib/origami/graphics/text.rb,
lib/origami/graphics/path.rb,
lib/origami/graphics/state.rb,
lib/origami/graphics/colors.rb,
lib/origami/graphics/patterns.rb,
lib/origami/graphics/instruction.rb

Overview

module Graphics

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, *operands) ⇒ Instruction

Returns a new instance of Instruction.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/origami/graphics/instruction.rb', line 32

def initialize(operator, *operands)
  @operator = operator
  @operands = operands.map! { |arg| arg.is_a?(Origami::Object) ? arg.value : arg }

  if self.class.has_op?(operator)
    opdef = self.class.get_operands(operator)

    if !opdef.include?('*') && (opdef.size != operands.size)
      raise InvalidPDFInstructionError,
        "Numbers of operands mismatch for #{operator}: #{operands.inspect}"
    end
  end
end

Instance Attribute Details

#operandsObject

Returns the value of attribute operands.



28
29
30
# File 'lib/origami/graphics/instruction.rb', line 28

def operands
  @operands
end

#operatorObject (readonly)

Returns the value of attribute operator.



27
28
29
# File 'lib/origami/graphics/instruction.rb', line 27

def operator
  @operator
end

Class Method Details

.get_operands(operator) ⇒ Object



71
72
73
# File 'lib/origami/graphics/instruction.rb', line 71

def get_operands(operator)
  @insns[operator][:operands]
end

.get_render_proc(operator) ⇒ Object



67
68
69
# File 'lib/origami/graphics/instruction.rb', line 67

def get_render_proc(operator)
  @insns[operator][:render]
end

.has_op?(operator) ⇒ Boolean

Returns:



63
64
65
# File 'lib/origami/graphics/instruction.rb', line 63

def has_op?(operator)
  @insns.has_key? operator
end

.insn(operator, *operands, &render_proc) ⇒ Object



57
58
59
60
61
# File 'lib/origami/graphics/instruction.rb', line 57

def insn(operator, *operands, &render_proc)
  @insns[operator] = {}
  @insns[operator][:operands] = operands
  @insns[operator][:render] = render_proc || lambda {}
end

.parse(stream) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/origami/graphics/instruction.rb', line 75

def parse(stream)
  operands = []
  while type = Object.typeof(stream, true)
    operands.push type.parse(stream)
  end

  if !stream.eos?
    if stream.scan(/(?<operator>[[:graph:]&&[^\[\]<>()%\/]]+)/).nil?
      raise InvalidPDFInstructionError, "Operator: #{(stream.peek(10) + "...").inspect}"
    end

    operator = stream['operator']
    PDF::Instruction.new(operator, *operands)
  else
    unless operands.empty?
      raise InvalidPDFInstructionError, "No operator given for operands: #{operands.map(&:to_s).join(" ")}"
    end
  end
end

Instance Method Details

#render(canvas) ⇒ Object



46
47
48
49
50
# File 'lib/origami/graphics/instruction.rb', line 46

def render(canvas)
  self.class.get_render_proc(@operator)[canvas, *@operands]

  self
end

#to_sObject



52
53
54
# File 'lib/origami/graphics/instruction.rb', line 52

def to_s
  "#{operands.map { |op| op.to_o.to_s }.join(" ")}#{" " unless operands.empty?}#{operator}\n"
end