Class: TaskJuggler::Painter

Inherits:
Object show all
Includes:
Primitives
Defined in:
lib/taskjuggler/Painter.rb,
lib/taskjuggler/Painter/Text.rb,
lib/taskjuggler/Painter/Color.rb,
lib/taskjuggler/Painter/Group.rb,
lib/taskjuggler/Painter/Points.rb,
lib/taskjuggler/Painter/Element.rb,
lib/taskjuggler/Painter/FontData.rb,
lib/taskjuggler/Painter/Primitives.rb,
lib/taskjuggler/Painter/SVGSupport.rb,
lib/taskjuggler/Painter/BasicShapes.rb,
lib/taskjuggler/Painter/FontMetrics.rb,
lib/taskjuggler/Painter/FontMetricsData.rb

Overview

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.

Defined Under Namespace

Modules: Primitives, SVGSupport Classes: Circle, Color, Element, Ellipse, FontMetrics, FontMetricsData, Group, Line, Points, PolyLine, Rect, Text

Constant Summary

Constants included from Primitives

Primitives::FillAndStrokeAttrs, Primitives::FillAttrs, Primitives::StrokeAttrs, Primitives::TextAttrs

Instance Method Summary collapse

Methods included from Primitives

#circle, #color, #ellipse, #group, #line, #points, #polyline, #rect, #text

Constructor Details

#initialize(width, height, &block) ⇒ Painter

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/taskjuggler/Painter.rb', line 36

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegator to @originalSelf.



60
61
62
# File 'lib/taskjuggler/Painter.rb', line 60

def method_missing(method, *args, &block)
  @originalSelf.send(method, *args, &block)
end

Instance Method Details

#to_svgObject

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



65
66
67
68
69
70
# File 'lib/taskjuggler/Painter.rb', line 65

def to_svg
  XMLElement.new('svg', 'width' => "#{@width}px",
                        'height' => "#{@height}px") do
    @elements.map { |el| el.to_svg }
  end
end