Module: DYI::Shape::Markable

Included in:
Line, Path, Polyline
Defined in:
lib/dyi/shape/base.rb

Overview

This module defines the method to attach a marker symbol to the lines, the polylines, the polygons or the paths.

Since:

  • 1.2.0

Instance Method Summary collapse

Instance Method Details

#has_marker?(position) ⇒ Boolean

Returns whether this shape has a marker symbol.

Parameters:

  • position (Symbol)

    the position where a marker symbol is drawn. Specifies the following values: :start, :mid, :end

Returns:

  • (Boolean)

    true if the shape has a marker at the cpecified point, false otherwise

Since:

  • 1.2.0



111
112
113
# File 'lib/dyi/shape/base.rb', line 111

def has_marker?(position)
  !@marker[position].nil?
end

#marker(position) ⇒ Marker

Returns a marker symbol at the specified position.

Parameters:

  • position (Symbol)

    the position where a marker symbol is drawn. Specifies the following values: :start, :mid, :end

Returns:

  • (Marker)

    a marker symbol at the specified position

Since:

  • 1.2.0



36
37
38
# File 'lib/dyi/shape/base.rb', line 36

def marker(position)
  @marker[position]
end

#set_marker(position, marker) ⇒ Object #set_marker(position, marker_type, options = {}) ⇒ Object

Attaches a marker symbol to the shape.

Overloads:

  • #set_marker(position, marker) ⇒ Object

    Attaches the specified marker symbol at the specified position.

    Parameters:

    • position (Symbol)

      the position where a marker symbol is drawn. Specifies the following values: :start, :mid, :end, :start_end, :start_mid, :mid_end, :all

    • marker (Marker)

      the marker symbol that is attached

  • #set_marker(position, marker_type, options = {}) ⇒ Object

    Attaches a pre-defined marker symbol at the specified position.

    Parameters:

    • position (Symbol)

      the position where a marker symbol is drawn. Specifies the following values: :start, :mid, :end, :start_end, :start_mid, :mid_end, :all

    • marker_type (Symbol)

      the type of pre-defined marker symbol that :square is attached. Specifies the following values: :circle, :triangle, :rhombus, :pentagon, :hexagon

    • options (Hash) (defaults to: {})

      a customizable set of options

    Options Hash (options):

    • :size (Number)

      size of the marker symbol. Specifies the relative size to line width

    • :painting (Painting)

      painting of the marker symbol

    • :orient (Number, "auto")

      how the marker is rotated. Specifies a rotated angle or "auto". "auto" means the marker symbol rotate the orientation of the line

Since:

  • 1.2.0



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dyi/shape/base.rb', line 62

def set_marker(position, *args)
  pos = case position
          when :start then 0x1
          when :mid then 0x2
          when :end then 0x4
          when :start_mid then 0x3
          when :start_end then 0x5
          when :mid_end then 0x6
          when :all then 0x7
          else raise ArgumentError, "illegal argument: #{position.inspect}"
        end
  case args.first
  when Symbol
    opts = args[1].clone || {}
    opts[:painting] ||= Painting.new(:fill => painting.stroke,
                                     :fill_opacity => painting.stroke_opacity,
                                     :opacity => painting.opacity)
    if opts[:orient] == 'auto'
      opts[:direction] = position == :end ? :to_end : :to_start
    end
    marker = Marker.new(args.first, opts)
  when Marker
    marker = args.first
  else
    raise TypeError, "illegal argument: #{value}"
  end
  marker.set_canvas(canvas)
  @marker[:start] = marker if pos & 0x01 != 0
  @marker[:mid] = marker if pos & 0x02 != 0
  if pos & 0x04 != 0
    if pos & 0x01 != 0 && args.first.is_a?(Symbol) && opts[:orient] == 'auto'
      opts[:painting] ||= Painting.new(:fill => painting.stroke,
                                       :fill_opacity => painting.stroke_opacity,
                                       :opacity => painting.opacity)
      opts[:direction] = :to_end
      marker = Marker.new(args.first, opts)
      marker.set_canvas(canvas)
      @marker[:end] = marker
    else
      @marker[:end] = marker
    end
  end
end