Class: DYI::Painting

Inherits:
Object
  • Object
show all
Defined in:
lib/dyi/painting.rb,
lib/ironruby.rb

Overview

Painting is used to specify how the drawing of the interior and the outline of the shape. Shape::Base objects (or objects of its subclass) have painting attribute whose value is instance of this class.

Since:

  • 0.0.0

Constant Summary collapse

IMPLEMENT_ATTRIBUTES =

Since:

  • 0.0.0

[:opacity, :fill, :fill_opacity, :fill_rule,
:stroke, :stroke_dasharray, :stroke_dashoffset,
:stroke_linecap, :stroke_linejoin, :stroke_miterlimit,
:stroke_opacity, :stroke_width,
:display, :visibility]
VALID_VALUES =

Since:

  • 0.0.0

{:fill_rule => ['nonzero','evenodd'],
:stroke_linecap => ['butt','round','square'],
:stroke_linejoin => ['miter','round','bevel'],
:display => ['block','none'],
:visibility => ['visible','hidden']}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Painting

Creates and returns a new instace of Paintng. If the argutment is a instance of Painting, returns a copy of the argument.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :display (String)

    the value of attribute display; one of following values: "block", "none"

  • :fill (Color, #write_as)

    the value of attribute fill

  • :fill_opacity (#to_f)

    the value of attribute fill_opacity

  • :fill_rule (String)

    the value of attribute fill_rule; one of following values: "nonzero", "evenodd"

  • :opacity (#to_f)

    the value of attribute opacity

  • :stroke (Color, #write_as)

    the value of attribute stroke

  • :stroke_dasharray (Array<Length>, String)

    the value of attribute stroke_dasharray

  • :stroke_dashoffset (Length)

    the value of attribute stroke_dashoffset

  • :stroke_linecap (String)

    the value of attribute stroke_linecap; one of following values: "butt", "round", "square"

  • :stroke_linejoin (String)

    the value of attribute stroke_linejoin; one of following values: "miter", "round", "bevel"

  • :stroke_miterlimit (#to_f)

    the value of attribute stroke_miterlimit

  • :stroke_opacity (#to_f)

    the value of attribute stroke_opacity

  • :stroke_width (Length)

    the value of attribute stroke_width

  • :visible (String)

    the value of attribute visibility; one of following values: "visible", "hidden"

Since:

  • 0.0.0



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dyi/painting.rb', line 143

def initialize(options={})
  case options
  when Painting
    IMPLEMENT_ATTRIBUTES.each do |attr|
      instance_variable_set("@#{attr}", options.__send__(attr))
    end
  when Hash
    options.each do |attr, value|
      __send__("#{attr}=", value) if IMPLEMENT_ATTRIBUTES.include?(attr.to_sym)
    end
  else
    raise TypeError, "#{options.class} can't be coerced into #{self.class}"
  end
end

Instance Attribute Details

#arrayObject

Parameters:

  • array (Array<Legth>, String)

    the value of attribute stroke_dasharray



228
229
230
231
232
233
234
235
236
# File 'lib/dyi/painting.rb', line 228

def stroke_dasharray=(array)
  if (array.nil? || array.empty?)
    @stroke_dasharray = nil
  elsif array.kind_of?(String)
    @stroke_dasharray = array.split(/\s*,\s*/).map {|len| Length.new(len)}
  else
    @stroke_dasharray = array.map {|len| Length.new(len)}
  end
end

#colorObject

Parameters:

  • color (Color, #write_as)

    the value of attribute stroke



185
186
187
# File 'lib/dyi/painting.rb', line 185

def fill=(color)
  @fill = color.respond_to?(:color?) && color.color? ? color : Color.new_or_nil(color)
end

#display=(value) ⇒ Object (writeonly)

+++ +++ +++ +++

Parameters:

  • value (String)

    the value of attribute fill_rule

  • value (String)

    the value of attribute stroke_linecap

  • value (String)

    the value of attribute stroke_linejoin

  • value (String)

    the value of attribute display

  • value (String)

    the value of attribute visibility



172
173
174
175
176
177
178
179
180
181
# File 'lib/dyi/painting.rb', line 172

VALID_VALUES.each do |attr, valid_values|
  define_method("#{attr.to_s}=") {|value|
    if (value = value.to_s).size == 0
      instance_variable_set("@#{attr}", nil)
    else
      raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value)
      instance_variable_set("@#{attr}", value)
    end
  }
end

#fill_rule=(value) ⇒ Object (writeonly)

+++ +++ +++ +++

Parameters:

  • value (String)

    the value of attribute fill_rule

  • value (String)

    the value of attribute stroke_linecap

  • value (String)

    the value of attribute stroke_linejoin

  • value (String)

    the value of attribute display

  • value (String)

    the value of attribute visibility



172
173
174
175
176
177
178
179
180
181
# File 'lib/dyi/painting.rb', line 172

VALID_VALUES.each do |attr, valid_values|
  define_method("#{attr.to_s}=") {|value|
    if (value = value.to_s).size == 0
      instance_variable_set("@#{attr}", nil)
    else
      raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value)
      instance_variable_set("@#{attr}", value)
    end
  }
end

#miterlimitObject

Parameters:

  • miterlimit (Float)

    the value of attribute stroke_miterlimit



222
223
224
# File 'lib/dyi/painting.rb', line 222

def stroke_miterlimit=(miterlimit)
  @stroke_miterlimit = miterlimit.nil? ? nil : [miterlimit.to_f, 1].max
end

#offsetObject

Parameters:

  • offset (Length)

    the value of attribute stroke_dashoffset



240
241
242
# File 'lib/dyi/painting.rb', line 240

def stroke_dashoffset=(offset)
  @stroke_dashoffset = Length.new_or_nil(offset)
end

#opacityObject

Parameters:

  • opacity (Float)

    the value of attribute stroke_opacity



198
199
200
# File 'lib/dyi/painting.rb', line 198

def opacity=(opacity)
  @opacity = opacity.nil? ? nil : opacity.to_f
end

#stroke_linecap=(value) ⇒ Object (writeonly)

+++ +++ +++ +++

Parameters:

  • value (String)

    the value of attribute fill_rule

  • value (String)

    the value of attribute stroke_linecap

  • value (String)

    the value of attribute stroke_linejoin

  • value (String)

    the value of attribute display

  • value (String)

    the value of attribute visibility



172
173
174
175
176
177
178
179
180
181
# File 'lib/dyi/painting.rb', line 172

VALID_VALUES.each do |attr, valid_values|
  define_method("#{attr.to_s}=") {|value|
    if (value = value.to_s).size == 0
      instance_variable_set("@#{attr}", nil)
    else
      raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value)
      instance_variable_set("@#{attr}", value)
    end
  }
end

#stroke_linejoin=(value) ⇒ Object (writeonly)

+++ +++ +++ +++

Parameters:

  • value (String)

    the value of attribute fill_rule

  • value (String)

    the value of attribute stroke_linecap

  • value (String)

    the value of attribute stroke_linejoin

  • value (String)

    the value of attribute display

  • value (String)

    the value of attribute visibility



172
173
174
175
176
177
178
179
180
181
# File 'lib/dyi/painting.rb', line 172

VALID_VALUES.each do |attr, valid_values|
  define_method("#{attr.to_s}=") {|value|
    if (value = value.to_s).size == 0
      instance_variable_set("@#{attr}", nil)
    else
      raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value)
      instance_variable_set("@#{attr}", value)
    end
  }
end

#visibility=(value) ⇒ Object (writeonly)

+++ +++ +++ +++

Parameters:

  • value (String)

    the value of attribute fill_rule

  • value (String)

    the value of attribute stroke_linecap

  • value (String)

    the value of attribute stroke_linejoin

  • value (String)

    the value of attribute display

  • value (String)

    the value of attribute visibility



172
173
174
175
176
177
178
179
180
181
# File 'lib/dyi/painting.rb', line 172

VALID_VALUES.each do |attr, valid_values|
  define_method("#{attr.to_s}=") {|value|
    if (value = value.to_s).size == 0
      instance_variable_set("@#{attr}", nil)
    else
      raise ArgumentError, "`#{value}' is invalid #{attr}" unless VALID_VALUES[attr].include?(value)
      instance_variable_set("@#{attr}", value)
    end
  }
end

#widthObject

Parameters:

  • width (Length)

    the value of attribute stroke_width



216
217
218
# File 'lib/dyi/painting.rb', line 216

def stroke_width=(width)
  @stroke_width = Length.new_or_nil(width)
end

Class Method Details

.new_or_nil(*args) ⇒ Painting?

Returns a new instace of Painting if the argments is not nil (calls Painting.new method), but returns nil if the argument is nil.

Returns:

  • (Painting, nil)

    a new instace of Painting if the argments is not nil, nil otherwise

See Also:

Since:

  • 0.0.0



273
274
275
# File 'lib/dyi/painting.rb', line 273

def new_or_nil(*args)
  (args.size == 1 && args.first.nil?) ? nil : new(*args)
end

Instance Method Details

#attributesHash{Symbol => Object}

Returns the hash of the attribute values. Even if the return value of this method is modified, the attribute value of the object is not modify.

Returns:

  • (Hash{Symbol => Object})

    the copy of the attribute values

Since:

  • 0.0.0



247
248
249
250
251
252
253
254
255
# File 'lib/dyi/painting.rb', line 247

def attributes
  IMPLEMENT_ATTRIBUTES.inject({}) do |hash, attr|
    value = instance_variable_get("@#{attr}")
    unless value.nil?
      hash[attr] = value
    end
    hash
  end
end

#cls_brush(shape) ⇒ Object

Since:

  • 0.0.0



113
114
115
# File 'lib/ironruby.rb', line 113

def cls_brush(shape)
  fill ? fill.create_cls_brush(fill_opacity, shape) : System::Drawing::SolidBrush.new(System::Drawing::Color.black)
end

#cls_dash_patternObject

Since:

  • 0.0.0



94
95
96
97
98
99
100
101
# File 'lib/ironruby.rb', line 94

def cls_dash_pattern
  return nil if !stroke_dasharray || stroke_dasharray.size == 0
  pattern = System::Array[System::Single].new(stroke_dasharray.size)
  stroke_dasharray.each_with_index do |dash, i|
    pattern[i] = dash.to_f
  end
  pattern
end

#cls_fill_modeObject

Since:

  • 0.0.0



86
87
88
89
90
91
92
# File 'lib/ironruby.rb', line 86

def cls_fill_mode
  case fill_rule
    when 'nonzero' then System::Drawing::Drawing2D::FillMode.winding
    when 'evenodd' then System::Drawing::Drawing2D::FillMode.alternate
    else System::Drawing::Drawing2D::FillMode.winding
  end
end

#cls_line_capObject

Since:

  • 0.0.0



77
78
79
80
81
82
83
84
# File 'lib/ironruby.rb', line 77

def cls_line_cap
  case stroke_linecap
    when 'butt' then System::Drawing::Drawing2D::LineCap.flat
    when 'round' then System::Drawing::Drawing2D::LineCap.round
    when 'square' then System::Drawing::Drawing2D::LineCap.square
    else System::Drawing::Drawing2D::LineCap.flat
  end
end

#cls_line_joinObject

Since:

  • 0.0.0



68
69
70
71
72
73
74
75
# File 'lib/ironruby.rb', line 68

def cls_line_join
  case stroke_linejoin
    when 'miter' then System::Drawing::Drawing2D::LineJoin.miter
    when 'round' then System::Drawing::Drawing2D::LineJoin.round
    when 'bevel' then System::Drawing::Drawing2D::LineJoin.bevel
    else System::Drawing::Drawing2D::LineJoin.miter
  end
end

#cls_penObject

Since:

  • 0.0.0



103
104
105
106
107
108
109
110
111
# File 'lib/ironruby.rb', line 103

def cls_pen
  return nil unless stroke && (stroke_width != DYI::Length::ZERO)
  pen = stroke.create_cls_pen(stroke_opacity)
  pen.width = stroke_width ? stroke_width.to_f : 1.0
  pen.start_cap = pen.end_cap = cls_line_cap
  pen.line_join = cls_line_join
  pen.dash_pattern = cls_dash_pattern if cls_dash_pattern
  pen
end

#empty?Boolean

Returns whether a value has been set some attribute in this object.

Returns:

  • (Boolean)

    true if a value has been set some attribute in this object, false otherwise

Since:

  • 0.0.0



260
261
262
263
264
# File 'lib/dyi/painting.rb', line 260

def empty?
  IMPLEMENT_ATTRIBUTES.all? do |attr|
    not instance_variable_get("@#{attr}")
  end
end

#fill=(color) ⇒ Object

Parameters:

  • color (Color, #write_as)

    the value of attribute fill

Since:

  • 0.0.0



185
186
187
# File 'lib/dyi/painting.rb', line 185

def fill=(color)
  @fill = color.respond_to?(:color?) && color.color? ? color : Color.new_or_nil(color)
end

#fill_opacity=(opacity) ⇒ Object

Parameters:

  • opacity (Float)

    the value of attribute fill_opacity

Since:

  • 0.0.0



204
205
206
# File 'lib/dyi/painting.rb', line 204

def fill_opacity=(opacity)
  @fill_opacity = opacity.nil? ? nil : opacity.to_f
end

#stroke=(color) ⇒ Object

Parameters:

  • color (Color, #write_as)

    the value of attribute stroke

Since:

  • 0.0.0



191
192
193
# File 'lib/dyi/painting.rb', line 191

def stroke=(color)
  @stroke = color.respond_to?(:color?) && color.color? ? color : Color.new_or_nil(color)
end

#stroke_dasharray=(array) ⇒ Object

Parameters:

  • array (Array<Legth>, String)

    the value of attribute stroke_dasharray

Since:

  • 0.0.0



228
229
230
231
232
233
234
235
236
# File 'lib/dyi/painting.rb', line 228

def stroke_dasharray=(array)
  if (array.nil? || array.empty?)
    @stroke_dasharray = nil
  elsif array.kind_of?(String)
    @stroke_dasharray = array.split(/\s*,\s*/).map {|len| Length.new(len)}
  else
    @stroke_dasharray = array.map {|len| Length.new(len)}
  end
end

#stroke_dashoffset=(offset) ⇒ Object

Parameters:

  • offset (Length)

    the value of attribute stroke_dashoffset

Since:

  • 0.0.0



240
241
242
# File 'lib/dyi/painting.rb', line 240

def stroke_dashoffset=(offset)
  @stroke_dashoffset = Length.new_or_nil(offset)
end

#stroke_miterlimit=(miterlimit) ⇒ Object

Parameters:

  • miterlimit (Float)

    the value of attribute stroke_miterlimit

Since:

  • 0.0.0



222
223
224
# File 'lib/dyi/painting.rb', line 222

def stroke_miterlimit=(miterlimit)
  @stroke_miterlimit = miterlimit.nil? ? nil : [miterlimit.to_f, 1].max
end

#stroke_opacity=(opacity) ⇒ Object

Parameters:

  • opacity (Float)

    the value of attribute stroke_opacity

Since:

  • 0.0.0



210
211
212
# File 'lib/dyi/painting.rb', line 210

def stroke_opacity=(opacity)
  @stroke_opacity = opacity.nil? ? nil : opacity.to_f
end

#stroke_width=(width) ⇒ Object

Parameters:

  • width (Length)

    the value of attribute stroke_width

Since:

  • 0.0.0



216
217
218
# File 'lib/dyi/painting.rb', line 216

def stroke_width=(width)
  @stroke_width = Length.new_or_nil(width)
end