Class: KvgCharacterRecognition::KvgParser::Stroke

Inherits:
Object
  • Object
show all
Defined in:
lib/kvg_character_recognition/kvg_parser.rb

Overview

Stroke represent one stroke, which is a series of SVG commands.

Constant Summary collapse

COMMANDS =
["M", "C", "c", "s", "S"]

Instance Method Summary collapse

Constructor Details

#initialize(stroke_as_code) ⇒ Stroke

Returns a new instance of Stroke.



192
193
194
# File 'lib/kvg_character_recognition/kvg_parser.rb', line 192

def initialize(stroke_as_code)
  @command_list = parse(stroke_as_code) 
end

Instance Method Details

#parse(stroke_as_code) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
# File 'lib/kvg_character_recognition/kvg_parser.rb', line 211

def parse(stroke_as_code)
  elements = split_elements(stroke_as_code).delete_if{ |e| e == "" }  
  command_list = Array.new
  current_cursor = Point.new(0,0);

  while elements != [] do

    case elements.slice!(0)
    when "M"
      x,y = elements.slice!(0..1)
      m = SVG_M.new(Point.new(x.to_f,y.to_f))
      current_cursor = m.current_cursor
      command_list.push(m)

    when "C"
      x1,y1,x2,y2,x,y = elements.slice!(0..5)
      c = SVG_C.new(Point.new(x1.to_f,y1.to_f), Point.new(x2.to_f,y2.to_f), Point.new(x.to_f,y.to_f), current_cursor)
      current_cursor = c.current_cursor
      command_list.push(c)

      #handle polybezier
      unless elements.empty? || COMMANDS.include?(elements.first)
        elements.unshift("C")
      end
    when "c"
      x1,y1,x2,y2,x,y = elements.slice!(0..5)
      c = SVG_C.relative(Point.new(x1.to_f,y1.to_f), Point.new(x2.to_f,y2.to_f), Point.new(x.to_f,y.to_f), current_cursor)
      current_cursor = c.current_cursor
      command_list.push(c)

      #handle polybezier
      unless elements.empty? || COMMANDS.include?(elements.first)
        elements.unshift("c")
      end

    when "s"
      x2,y2,x,y = elements.slice!(0..3)
      reflected_point = command_list[-1].second_point
      s = SVG_S.relative(Point.new(x2.to_f,y2.to_f), Point.new(x.to_f,y.to_f), current_cursor, reflected_point)
      current_cursor = s.current_cursor
      command_list.push(s)

    when "S"
      x2,y2,x,y = elements.slice!(0..3)
      reflected_point = command_list[-1].second_point
      s = SVG_S.new(Point.new(x2.to_f,y2.to_f), Point.new(x.to_f,y.to_f), current_cursor,reflected_point)
      current_cursor = s.current_cursor
      command_list.push(s)

    else
      #print "You should not be here\n"

    end

  end

  return command_list
end

#split_elements(line) ⇒ Object



206
207
208
209
# File 'lib/kvg_character_recognition/kvg_parser.rb', line 206

def split_elements(line)
  # This is magic.
  return line.gsub("-",",-").gsub("s",",s,").gsub("S",",S,").gsub("c",",c,").gsub("C",",C,").gsub("m", "M").gsub("M","M,").gsub("[","").gsub(";",",;,").gsub(",,",",").gsub(" ,", ",").gsub(", ", ",").gsub(" ", ",").split(/,/);
end

#to_aObject

to array TODO: better implementation using composite pattern



202
203
204
# File 'lib/kvg_character_recognition/kvg_parser.rb', line 202

def to_a
  to_points.map{|point| point.to_a}
end

#to_pointsObject



196
197
198
# File 'lib/kvg_character_recognition/kvg_parser.rb', line 196

def to_points
  return @command_list.map{|element| element.to_points}.flatten
end