Class: Emfsvg::Translation::Handlers::PathFlattener

Inherits:
Object
  • Object
show all
Defined in:
lib/emfsvg/translation/handlers/path_flattener.rb

Overview

Flatten a list of Svg::PathData::Command into a single shape (Polyline/Polygon/PolyBezier) suitable for one EMF record.

v1 limitations:

* All commands converted to absolute (relative resolved).
* Arc/quadratic commands approximated as lines (TODO).
* Multiple subpaths merged into one (TODO: PolyPolygon).

Defined Under Namespace

Classes: Bounds, Result

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.flatten(commands) ⇒ Object



16
17
18
# File 'lib/emfsvg/translation/handlers/path_flattener.rb', line 16

def self.flatten(commands)
  new.flatten(commands)
end

Instance Method Details

#flatten(commands) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/emfsvg/translation/handlers/path_flattener.rb', line 20

def flatten(commands)
  pen = PenState.new
  closed = false
  has_curve = false
  points = []

  commands.each do |command|
    case command.letter.upcase
    when "M"
      pen.move_to(command, absolute: command.absolute?)
      points << pen.current.dup
    when "L"
      pen.line_to(command, absolute: command.absolute?)
      points << pen.current.dup
    when "H"
      pen.h_line_to(command, absolute: command.absolute?)
      points << pen.current.dup
    when "V"
      pen.v_line_to(command, absolute: command.absolute?)
      points << pen.current.dup
    when "C"
      has_curve = true
      pen.cubic_to(command, absolute: command.absolute?)
      points.concat(pen.flush_curve_points)
    when "Z"
      closed = true
    end
  end

  Result.new(
    shape_kind: shape_kind(closed, has_curve),
    points: points,
    bounds: bounds_for(points)
  )
end