class TaskJuggler::Painter

This is a vector drawing class. It can describe a canvas with lines, rectangles, circles, ellipses and text elements on it. The elements can be grouped. It currently only supports rendering as an SVG output.

Public Class Methods

new(width, height) { |self| ... } click to toggle source

Create a canvas of dimension width times height. The block can be used to add elements to the drawing. If the block has an argument, the block content is evaluated within the current context. If no argument is provided, the newly created object will be the evaluation context of the block. This will make instance variables of the caller inaccessible. Methods of the caller will still be available.

# File lib/taskjuggler/Painter.rb, line 35
def initialize(width, height, &block)
  @width = width
  @height = height

  @elements = []
  if block
    if block.arity == 1
      # This is the traditional case where self is passed to the block.
      # All Primitives methods now must be prefixed with the block
      # variable to call them.
      yield self
    else
      # In order to have the primitives easily available in the block, we
      # use instance_eval to switch self to this object. But this makes the
      # methods of the original self no longer accessible. We work around
      # this by saving the original self and using method_missing to
      # delegate the method call to the original self.
      @originalSelf = eval('self', block.binding)
      instance_eval(&block)
    end
  end
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

Delegator to @originalSelf.

# File lib/taskjuggler/Painter.rb, line 59
def method_missing(method, *args, &block)
  @originalSelf.send(method, *args, &block)
end
to_svg() click to toggle source

Render the canvas as SVG output (tree of XMLElement objects).

# File lib/taskjuggler/Painter.rb, line 64
def to_svg
  XMLElement.new('svg', 'width' => "#{@width}px",
                        'height' => "#{@height}px") do
    @elements.map { |el| el.to_svg }
  end
end