Class: DYI::Canvas

Inherits:
GraphicalElement show all
Defined in:
lib/dyi/canvas.rb,
lib/ironruby.rb

Overview

The body of Vector-Image. This class is a container for all graphical elements that make up the image.

Since:

  • 0.0.0

Constant Summary collapse

IMPLEMENT_ATTRIBUTES =

Since:

  • 0.0.0

[:view_box, :preserve_aspect_ratio]

Constants inherited from GraphicalElement

GraphicalElement::CLASS_REGEXP

Constants inherited from Element

Element::ID_REGEXP

Instance Attribute Summary collapse

Attributes inherited from GraphicalElement

#css_class

Attributes inherited from Element

#description, #title

Instance Method Summary collapse

Methods inherited from GraphicalElement

#add_css_class, #add_event_listener, #css_classes, #event_target?, #remove_css_class, #remove_event_listener

Methods inherited from Element

#has_uri_reference?, #id, #id=, #include_external_file?, #inner_id

Constructor Details

#initialize(width, height, real_width = nil, real_height = nil, preserve_aspect_ratio = 'none', options = {}) ⇒ Canvas

Returns a new instance of Canvas.

Parameters:

  • width (Length)

    width of the canvas on user unit

  • height (Length)

    height of the canvas on user unit

  • real_width (Length) (defaults to: nil)

    width of the image. When this value is nil, uses a value that equals value of width parameter.

  • real_height (Length) (defaults to: nil)

    height of the image. When this value is nil, uses a value that equals value of height parameter.

  • preserve_aspect_ratio (String) (defaults to: 'none')

    value that indicates whether or not to force uniform scaling

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

    a customizable set of options

Options Hash (options):

  • :css_class (String)

    CSS class of body element

Since:

  • 0.0.0



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dyi/canvas.rb', line 81

def initialize(width, height,
               real_width = nil, real_height = nil,
               preserve_aspect_ratio='none', options={})
  self.width = width
  self.height = height
  @view_box = "0 0 #{width} #{height}"
  @preserve_aspect_ratio = preserve_aspect_ratio
  @child_elements = []
  @scripts = []
  @event_listeners = {}
  @stylesheets = []
  @seed_of_id = -1
  @receive_event = false
  self.css_class = options[:css_class]
  self.real_width = real_width
  self.real_height = real_height
end

Instance Attribute Details

#child_elementsArray<Element> (readonly)

Returns an array of child elements.

Returns:

  • (Array<Element>)

    an array of child elements

Since:

  • 0.0.0



50
51
52
# File 'lib/dyi/canvas.rb', line 50

def child_elements
  @child_elements
end

#event_listenersHash (readonly)

Returns hash of event listners.

Returns:

  • (Hash)

    hash of event listners

Since:

  • 1.0.0



55
56
57
# File 'lib/dyi/canvas.rb', line 55

def event_listeners
  @event_listeners
end

#metadataObject

Returns a metadata object that the image has.

Returns:

  • (Object)

    a metadata object that the image has.

Since:

  • 1.1.1



70
71
72
# File 'lib/dyi/canvas.rb', line 70

def 
  
end

#scriptsArray<Script::SimpleScript> (readonly)

Returns an array of scripts.

Returns:

Since:

  • 1.0.0



65
66
67
# File 'lib/dyi/canvas.rb', line 65

def scripts
  @scripts
end

#stylesheetsArray<Stylesheet::Style> (readonly)

Returns an array of stylesheets.

Returns:

Since:

  • 1.0.0



60
61
62
# File 'lib/dyi/canvas.rb', line 60

def stylesheets
  @stylesheets
end

Instance Method Details

#add_initialize_script(script_body) ⇒ Object

Registers a script with the image for initialization.

Parameters:

  • script_body (String)

    a string that is a script body for initialization

Since:

  • 1.0.0



258
259
260
261
262
263
264
265
# File 'lib/dyi/canvas.rb', line 258

def add_initialize_script(script_body)
  if @init_script
    @init_script.append_body(script_body)
  else
    @init_script = Script::EcmaScript::EventListener.new(script_body)
    add_event_listener(:load, @init_script)
  end
end

#add_script(script_body, content_type = 'application/ecmascript') ⇒ Object

Registers a script.

Parameters:

  • script_body (String, Script::SimpleScript)

    a string that is a script body or a script object that is registered

  • content_type (String) (defaults to: 'application/ecmascript')

    a content-type of the script. If parameter ‘script_body’ is Script::SimpleScript object, this parameter is ignored

Since:

  • 1.0.0



222
223
224
225
226
227
228
# File 'lib/dyi/canvas.rb', line 222

def add_script(script_body, content_type = 'application/ecmascript')
  if script_body.respond_to?(:include_external_file?)
    @scripts << script_body unless @scripts.include?(script_body)
  else
    @scripts << Script::SimpleScript.new(script_body, content_type)
  end
end

#add_stylesheet(style_body, content_type = 'text/css') ⇒ Object

Registers a stylesheet with the image.

Parameters:

  • style_body (String)

    a string that is a stylesheet body

  • content_type (String) (defaults to: 'text/css')

    a content-type of the stylesheet

Since:

  • 1.0.0



242
243
244
# File 'lib/dyi/canvas.rb', line 242

def add_stylesheet(style_body, content_type = 'text/css')
  @stylesheets << Stylesheet::Style.new(style_body, content_type)
end

#attributesHash

Returns optional attributes.

Returns:

  • (Hash)

    optional attributes

Since:

  • 0.0.0



185
186
187
188
189
190
191
192
# File 'lib/dyi/canvas.rb', line 185

def attributes
  IMPLEMENT_ATTRIBUTES.inject({}) do |hash, attribute|
    variable_name = '@' + attribute.to_s.split(/(?=[A-Z])/).map{|str| str.downcase}.join('_')
    value = instance_variable_get(variable_name)
    hash[attribute] = value.to_s if value
    hash
  end
end

#canvasCanvas

Returns the canvas where the shape is drawn.

Returns:

Since:

  • 1.0.0



141
142
143
# File 'lib/dyi/canvas.rb', line 141

def canvas
  self
end

#publish_shape_idString

Create a new id for a descendant element.

Returns:

  • (String)

    new id for a descendant element

Since:

  • 1.0.0



197
198
199
# File 'lib/dyi/canvas.rb', line 197

def publish_shape_id
  'elm%04d' % (@seed_of_id += 1)
end

#puts_in_io(format = nil, io = $>, options = {}) ⇒ Object

Puts in io.

Parameters:

  • format (Symbol) (defaults to: nil)

    an image format. When this parameter is nil, saves as SVG. This method supports following values: :svg, :eps, :xaml, :png

  • io (IO) (defaults to: $>)

    an io to be written

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

    a customizable set of options

Options Hash (options):

  • :indent (Integer)

    indent of XML output. Defualt to 2

Since:

  • 1.0.0



169
170
171
# File 'lib/dyi/canvas.rb', line 169

def puts_in_io(format=nil, io=$>, options={})
  get_formatter(format, options).puts(io)
end

#real_heightLength

Returns height of the image.

Returns:

  • (Length)

    height of the image

Since:

  • 0.0.0



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

def real_height
  @real_height || height
end

#real_height=(height) ⇒ Object

Sets height of the image.

Parameters:

  • height (Length)

    height of the image

Since:

  • 0.0.0



119
120
121
# File 'lib/dyi/canvas.rb', line 119

def real_height=(height)
  @real_height = Length.new_or_nil(height)
end

#real_widthLength

Returns width of the image.

Returns:

  • (Length)

    width of the image

Since:

  • 0.0.0



101
102
103
# File 'lib/dyi/canvas.rb', line 101

def real_width
  @real_width || width
end

#real_width=(width) ⇒ Object

Sets width of the image.

Parameters:

  • width (Length)

    width of the image

Since:

  • 0.0.0



107
108
109
# File 'lib/dyi/canvas.rb', line 107

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

#receive_event?Boolean

Returns whether an event is set to the shape.

Returns:

  • (Boolean)

    true if an event set to the shape, false otherwise.

Since:

  • 1.0.0



212
213
214
# File 'lib/dyi/canvas.rb', line 212

def receive_event?
  @receive_event
end

#reference_script_file(reference_path, content_type = 'application/ecmascript') ⇒ Object

Registers a reference to a script file with the image.

Parameters:

  • reference_path (String)

    a file path of a script file

  • content_type (String) (defaults to: 'application/ecmascript')

    a content-type of the script

Since:

  • 1.0.0



234
235
236
# File 'lib/dyi/canvas.rb', line 234

def reference_script_file(reference_path, content_type = 'application/ecmascript')
  @scripts << Script::ScriptReference.new(reference_path, content_type)
end

#reference_stylesheet_file(reference_path, content_type = 'text/css') ⇒ Object

Registers a reference to a stylesheet file with the image.

Parameters:

  • reference_path (String)

    a file path of a stylesheet file

  • content_type (String) (defaults to: 'text/css')

    a content-type of the stylesheet

Since:

  • 1.0.0



250
251
252
# File 'lib/dyi/canvas.rb', line 250

def reference_stylesheet_file(reference_path, content_type = 'text/css')
  @stylesheets << Stylesheet::StyleReference.new(reference_path, content_type)
end

#root_element?Boolean

Returns whether this instance is root element of the shape.

Returns:

  • (Boolean)

    always true.

Since:

  • 1.0.0



134
135
136
# File 'lib/dyi/canvas.rb', line 134

def root_element?
  true
end

#root_node?Boolean

Deprecated.

Use #root_element? instead.

Returns:

  • (Boolean)

Since:

  • 0.0.0



124
125
126
127
128
129
# File 'lib/dyi/canvas.rb', line 124

def root_node?
  msg = [__FILE__, __LINE__, ' waring']
  msg << ' DYI::Canvas#root_node? is deprecated; use DYI::Canvas#root_element?'
  warn(msg.join(':'))
  true
end

#save(file_name, format = nil, options = {}) ⇒ Object

Saves as image file.

Parameters:

  • file_name (String)

    a name of an image file

  • format (Symbol) (defaults to: nil)

    an image format. When this parameter is nil, saves as SVG. This method supports following values: :svg, :eps, :xaml, :png

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

    a customizable set of options

Options Hash (options):

  • :indent (Integer)

    indent of XML output. Defualt to 2

Since:

  • 1.0.0



159
160
161
# File 'lib/dyi/canvas.rb', line 159

def save(file_name, format=nil, options={})
  get_formatter(format, options).save(file_name)
end

#set_event(event) ⇒ Object

Sets event to the image.

Parameters:

  • event (Event)

    an event that is set to this image

Since:

  • 1.0.0



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

def set_event(event)
  super
  @receive_event = true
end

#string(format = nil, options = {}) ⇒ String

Returns data that means the image.

Parameters:

  • format (Symbol) (defaults to: nil)

    an image format. When this parameter is nil, saves as SVG. This method supports following values: :svg, :eps, :xaml, :png

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

    a customizable set of options

Options Hash (options):

  • :indent (Integer)

    indent of XML output. Defualt to 2

Returns:

  • (String)

    data that means the image

Since:

  • 1.0.0



179
180
181
# File 'lib/dyi/canvas.rb', line 179

def string(format=nil, options={})
  get_formatter(format, options).string
end

#to_reused_sourceObject

Since:

  • 1.3.0



268
269
270
271
272
273
274
# File 'lib/dyi/canvas.rb', line 268

def to_reused_source
  options = {}
  options[:css_class] = css_class
  template = Shape::GraphicalTemplate.new(width, height, preserve_aspect_ratio, options)
  template.instance_variable_set('@child_elements', child_elements)
  template
end

#write_as(formatter, io = $>) ⇒ Object

Writes image on io object.

Parameters:

  • formatter (Formatter::Base)

    an object that defines the image format

  • io (IO) (defaults to: $>)

    an io to be written

Since:

  • 1.0.0



149
150
151
# File 'lib/dyi/canvas.rb', line 149

def write_as(formatter, io=$>)
  formatter.write_canvas(self, io)
end