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.



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

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.



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

def operands
  @operands
end

#operatorObject (readonly)

Returns the value of attribute operator.



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

def operator
  @operator
end

Class Method Details

.get_operands(operator) ⇒ Object



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

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

.get_render_proc(operator) ⇒ Object



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

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

.has_op?(operator) ⇒ Boolean

Returns:



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

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

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



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

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

.parse(stream) ⇒ Object



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

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

    if not 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



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

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

    self
end

#to_sObject



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

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