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

Constant Summary collapse

@@regexp =
Regexp.new('([^ \\t\\r\\n\\0\\[\\]<>()%\\/]+)')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, *operands) ⇒ Instruction

Returns a new instance of Instruction.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/origami/graphics/instruction.rb', line 36

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 not opdef.include?('*') and 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.



31
32
33
# File 'lib/origami/graphics/instruction.rb', line 31

def operands
  @operands
end

#operatorObject (readonly)

Returns the value of attribute operator.



30
31
32
# File 'lib/origami/graphics/instruction.rb', line 30

def operator
  @operator
end

Class Method Details

.get_operands(operator) ⇒ Object



75
76
77
# File 'lib/origami/graphics/instruction.rb', line 75

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

.get_render_proc(operator) ⇒ Object



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

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

.has_op?(operator) ⇒ Boolean

Returns:



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

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

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



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

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

.parse(stream) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/origami/graphics/instruction.rb', line 79

def parse(stream)
  operands = []
  while type = Object.typeof(stream, true)
    operands.push type.parse(stream)
  end
  
  if not stream.eos?
    if stream.scan(@@regexp).nil?
      raise InvalidPDFInstructionError, 
        "Operator: #{(stream.peek(10) + '...').inspect}"
    end

    operator = stream[1]
    PDF::Instruction.new(operator, *operands)
  else
    if not operands.empty?
      raise InvalidPDFInstructionError,
        "No operator given for operands: #{operands.join}"
    end
  end
end

Instance Method Details

#render(canvas) ⇒ Object



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

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

  self
end

#to_sObject



56
57
58
# File 'lib/origami/graphics/instruction.rb', line 56

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