Class: Origami::PDF::Instruction
- Inherits:
-
Object
- Object
- Origami::PDF::Instruction
- 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
-
#operands ⇒ Object
Returns the value of attribute operands.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
Class Method Summary collapse
- .get_operands(operator) ⇒ Object
- .get_render_proc(operator) ⇒ Object
- .has_op?(operator) ⇒ Boolean
- .insn(operator, *operands, &render_proc) ⇒ Object
- .parse(stream) ⇒ Object
Instance Method Summary collapse
-
#initialize(operator, *operands) ⇒ Instruction
constructor
A new instance of Instruction.
- #render(canvas) ⇒ Object
- #to_s ⇒ Object
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
#operands ⇒ Object
Returns the value of attribute operands.
31 32 33 |
# File 'lib/origami/graphics/instruction.rb', line 31 def operands @operands end |
#operator ⇒ Object (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
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.unshift 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_s ⇒ Object
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 |