class Origami::PDF::Instruction

Attributes

operands[RW]
operator[R]

Public Class Methods

get_operands(operator) click to toggle source
# File lib/origami/graphics/instruction.rb, line 72
def get_operands(operator)
    @insns[operator][:operands]
end
get_render_proc(operator) click to toggle source
# File lib/origami/graphics/instruction.rb, line 68
def get_render_proc(operator)
    @insns[operator][:render]
end
has_op?(operator) click to toggle source
# File lib/origami/graphics/instruction.rb, line 64
def has_op?(operator)
    @insns.has_key? operator
end
insn(operator, *operands, &render_proc) click to toggle source
# 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
new(operator, *operands) click to toggle source
# 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
parse(stream) click to toggle source
# 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

Public Instance Methods

render(canvas) click to toggle source
# File lib/origami/graphics/instruction.rb, line 47
def render(canvas)
    self.class.get_render_proc(@operator)[canvas, *@operands]

    self
end
to_s() click to toggle source
# 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