Class: Rasem::SVGTag

Inherits:
Object
  • Object
show all
Defined in:
lib/rasem/svg_image.rb

Direct Known Subclasses

SVGTagWithParent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attributes = {}, &block) ⇒ SVGTag

Returns a new instance of SVGTag.



79
80
81
82
83
84
85
86
87
# File 'lib/rasem/svg_image.rb', line 79

def initialize(tag, attributes={}, &block)
  @tag = validate_tag(tag)
  @attributes = validate_attributes(attributes)
  @children = []

  if block
    instance_exec &block
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/rasem/svg_image.rb', line 324

def method_missing(meth, *args, &block)
  #if method is a setter or a getter, check valid attributes:
  check = /^(?<name>.*)(?<op>=|\?)$/.match(meth)
  if check
    raise "Passing a code block to setter or getter is not permited!" if block
    name = validate_attribute(check[:name].to_sym)
    if check[:op] == '?'
      @attributes[name]
    elsif check[:op] == '='
      raise "Setting an attribute with multiple values is not permited!" if args.size > 1
      @attributes[name] = args[0]
    end
  elsif child = validate_child_name(meth)
    spawn_child(child, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



77
78
79
# File 'lib/rasem/svg_image.rb', line 77

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



77
78
79
# File 'lib/rasem/svg_image.rb', line 77

def children
  @children
end

#tagObject (readonly)

Returns the value of attribute tag.



77
78
79
# File 'lib/rasem/svg_image.rb', line 77

def tag
  @tag
end

Instance Method Details

#append_child(child) ⇒ Object



276
277
278
279
280
# File 'lib/rasem/svg_image.rb', line 276

def append_child(child)
  @children.push(child)
  child.push_defaults(merge_defaults()) if @defaults
  child
end

#linearGradient(id, attributes = {}, if_exists = :skip, &block) ⇒ Object

special case for linearGradient



220
221
222
223
# File 'lib/rasem/svg_image.rb', line 220

def linearGradient(id, attributes={}, if_exists = :skip, &block)
  raise "image reference isn't set, cannot use 'defs' (and thus linearGradient) !" if @img.nil?
  @img.add_def(id, Rasem::SVGLinearGradient.new(@img, attributes), if_exists, &block)
end

#matrix(a, b, c, d, e, f) ⇒ Object



125
126
127
128
# File 'lib/rasem/svg_image.rb', line 125

def matrix(a, b, c, d, e, f)
  add_transform(:matrix, "#{a}, #{b}, #{c}, #{d}, #{e}, #{f}")
  self
end

#merge_defaultsObject



283
284
285
286
287
288
# File 'lib/rasem/svg_image.rb', line 283

def merge_defaults()
  result = {}
  return result if @defaults.empty?
  @defaults.each { |d| result.merge!(d) }
  result
end

#path(attributes = {}, &block) ⇒ Object

special case for path block



207
208
209
# File 'lib/rasem/svg_image.rb', line 207

def path(attributes = {}, &block)
  append_child Rasem::SVGPath.new(@img, attributes, &block)
end

#pop_defaultsObject



297
298
299
# File 'lib/rasem/svg_image.rb', line 297

def pop_defaults()
  @defaults.pop()
end

#push_defaults(defaults) ⇒ Object



291
292
293
294
# File 'lib/rasem/svg_image.rb', line 291

def push_defaults(defaults)
  @defaults = [] unless @defaults
  @defaults.push(defaults)
end

#radialGradient(id, attributes = {}, if_exists = :skip, &block) ⇒ Object

special case for radialGradient



227
228
229
230
# File 'lib/rasem/svg_image.rb', line 227

def radialGradient(id, attributes={}, if_exists = :skip, &block)
  raise "image reference isn't set, cannot use 'defs' (and thus radialGradient) !" if @img.nil?
  @img.add_def(id, Rasem::SVGRadialGradient.new(@img, attributes), if_exists, &block)
end

#raw(data) ⇒ Object

special case for raw blocks.



201
202
203
# File 'lib/rasem/svg_image.rb', line 201

def raw(data)
  append_child Rasem::SVGRaw.new(@img, data)
end

#rotate(angle, cx = nil, cy = nil) ⇒ Object



107
108
109
110
# File 'lib/rasem/svg_image.rb', line 107

def rotate(angle, cx = nil, cy = nil)
  add_transform(:rotate, "#{angle}#{(cx.nil? or cy.nil?) ? "" : ", #{cx}, #{cy}"}")
  self
end

#scale(sx, sy = 1) ⇒ Object



101
102
103
104
# File 'lib/rasem/svg_image.rb', line 101

def scale(sx, sy = 1)
  add_transform(:scale, "#{sx}, #{sy}")
  self
end

#skewX(angle) ⇒ Object



113
114
115
116
# File 'lib/rasem/svg_image.rb', line 113

def skewX(angle)
  add_transform(:skewX, "#{angle}")
  self
end

#skewY(angle) ⇒ Object



119
120
121
122
# File 'lib/rasem/svg_image.rb', line 119

def skewY(angle)
  add_transform(:skewY, "#{angle}")
  self
end

#spawn_child(tag, *args, &block) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rasem/svg_image.rb', line 235

def spawn_child(tag, *args, &block)
  #expected args: nil, [hash], [...]
  parameters = {} if args.size == 0

  unless parameters #are empty
    parameters = args[0] if args[0].is_a? Hash
  end

  unless parameters #are set
    #try to find args expansion rule
    expansion = Rasem::SVG_EXPANSION[tag.to_sym]
    raise "Unnamed parameters for #{tag} are not allowed!" unless expansion
  
    if expansion.is_a? Array
      raise "Bad unnamed parameter count for #{tag}, expecting #{expansion.size} got #{if args.last.is_a? Hash then args.size-1 else args.size end}" unless (args.size == expansion.size and not args.last.is_a? Hash) or (args.size - 1 == expansion.size and args.last.is_a? Hash)
      parameters = Hash[expansion.zip(args)]
      if args.last.is_a? Hash
        parameters.merge! args.last
      end
    elsif expansion.is_a? Proc
      hash = args.pop if args.last.is_a? Hash
      parameters = expansion.call(args)
      parameters.merge! hash if hash
    else
      raise "Unexpected expansion mechanism: #{expansion.class}"
    end
  end

  # add default parameters if they are not overwritten
  merge_defaults().each do |key, value|
    parameters[key] = value unless parameters[key]
  end if @defaults

  Rasem::SVG_DEFAULTS[tag.to_sym].each do |key, value|
    parameters[key] = value unless parameters[key]
  end if Rasem::SVG_DEFAULTS[tag.to_sym]

  append_child(Rasem::SVGTagWithParent.new(@img, tag, parameters, &block))
end

#to_sObject



369
370
371
372
373
# File 'lib/rasem/svg_image.rb', line 369

def to_s
  str = ""
  write(str)
  return str
end

#translate(tx, ty = 0) ⇒ Object

Provide methods for SVG transformations



95
96
97
98
# File 'lib/rasem/svg_image.rb', line 95

def translate(tx, ty = 0)
  add_transform(:translate, "#{tx}, #{ty}")
  self
end

#use(id, attributes = {}) ⇒ Object

special case for use block



213
214
215
216
# File 'lib/rasem/svg_image.rb', line 213

def use(id, attributes = {})
  id = id.attributes[:id] if id.is_a? Rasem::SVGTag
  append_child Rasem::SVGTagWithParent.new(@img, "use", attributes.merge("xlink:href" => "##{id}"))
end

#validate_attribute(attribute) ⇒ Object



168
169
170
171
# File 'lib/rasem/svg_image.rb', line 168

def validate_attribute(attribute)
  raise "#{@tag} does not support attribute #{attribute}" unless Rasem::SVG_STRUCTURE[@tag.to_sym][:attributes].include?(attribute.to_sym)
  attribute.to_sym
end

#validate_attributes(attributes) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rasem/svg_image.rb', line 137

def validate_attributes(attributes)
  clean_attributes = {}
  transforms = {}
  styles = {}

  attributes.each do |attribute, value|
    if Rasem::SVG_TRANSFORM.include? attribute
      transforms[attribute] = value
    elsif Rasem::CSS_STYLE.include? attribute
      styles[attribute] = value
    else
      clean_attributes[validate_attribute(attribute)] = value
    end
  end

  #always prefer more verbose definition.
  unless transforms.empty?
    transforms.merge!(clean_attributes[:transform]) if clean_attributes[:transform]
    str = ""
    write_transforms(transforms, str)
    clean_attributes[validate_attribute(:transform)] = str
  end

  unless styles.empty?
    styles.merge!(clean_attributes[:style]) if clean_attributes[:style]
    clean_attributes[validate_attribute(:style)] = styles
  end
  clean_attributes
end

#validate_child_name(name) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rasem/svg_image.rb', line 311

def validate_child_name(name)
  #aliases the name (like, group instead of g)
  name = Rasem::SVG_ALIAS[name.to_sym] if Rasem::SVG_ALIAS[name.to_sym]

  #raises only if given name is an actual svg tag. In other case -- assumes user just mistyped.
  if Rasem::SVG_STRUCTURE[@tag.to_sym][:elements].include?(name.to_sym)
    name.to_sym
  elsif Rasem::SVG_ELEMENTS.include?(name.to_sym)
    raise "#{@tag} should not contain child #{name}" 
  end
end

#validate_tag(tag) ⇒ Object



131
132
133
134
# File 'lib/rasem/svg_image.rb', line 131

def validate_tag(tag)
  raise "#{tag} is not a valid tag" unless Rasem::SVG_ELEMENTS.include?(tag.to_sym)
  tag.to_sym
end

#with_style(style = {}, &proc) ⇒ Object



302
303
304
305
306
307
308
# File 'lib/rasem/svg_image.rb', line 302

def with_style(style={}, &proc)
  push_defaults(style)
  # Call the block
  self.instance_exec(&proc)
  # Pop style again to revert changes
  pop_defaults()
end

#write(output) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/rasem/svg_image.rb', line 344

def write(output)
  raise "Can not write to given output!" unless output.respond_to?(:<<)
  output << "<#{@tag.to_s}"
  @attributes.each do
    |attribute, value|
    output << " #{attribute.to_s}=\""
    if attribute == :style
      write_styles(value, output)
    elsif attribute == :points
      write_points(value, output)
    else
      output << "#{value.to_s}"
    end
    output << "\""
  end

  if @children.empty?
    output << "/>"
  else
    output << ">"
    @children.each { |c| c.write(output) }
    output << "</#{@tag.to_s}>"
  end  
end

#write_points(points, output) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/rasem/svg_image.rb', line 191

def write_points(points, output)
  points.each_with_index do |value, index|
    output << value.to_s
    output << ',' if index.even?
    output << ' ' if (index.odd? and (index != points.size-1))
  end
end

#write_styles(styles, output) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/rasem/svg_image.rb', line 174

def write_styles(styles, output)
  styles.each do |attribute, value|
    attribute = attribute.to_s
    attribute.gsub!('_','-')
    output << "#{attribute}:#{value};"
  end
end

#write_transforms(transforms, output) ⇒ Object



183
184
185
186
187
188
# File 'lib/rasem/svg_image.rb', line 183

def write_transforms(transforms, output)
  transforms.each do |attribute, value|
    value = [value] unless value.is_a?(Array)
    output << "#{attribute.to_s}(#{value.join(',')}) "
  end
end