Class: Hieroglyph::Glyph

Inherits:
Object
  • Object
show all
Defined in:
lib/hieroglyph/glyph.rb

Constant Summary collapse

SHAPE_HANDLERS =
{
  'circle' => 'report_invalid',
  'ellipse' => 'report_invalid',
  'line' => 'report_invalid',
  'polyline' => 'report_invalid',
  'rect' => 'report_invalid',
  'polygon' => 'convert_polygon',
  'path' => 'convert_path'
}
@@too_many_shapes =
false
@@shape_found =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, source, font) ⇒ Glyph

Returns a new instance of Glyph.



26
27
28
29
30
31
32
# File 'lib/hieroglyph/glyph.rb', line 26

def initialize(file, source, font)
  @font = font
  set_name(file, source)
  @contents = Nokogiri::XML(File.new(file))
  Hieroglyph.log "Parsing #{@name}", 2
  @path = parse_shapes
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



8
9
10
# File 'lib/hieroglyph/glyph.rb', line 8

def contents
  @contents
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/hieroglyph/glyph.rb', line 8

def name
  @name
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/hieroglyph/glyph.rb', line 8

def path
  @path
end

Instance Method Details

#convert_path(type, content) ⇒ Object



74
75
76
77
# File 'lib/hieroglyph/glyph.rb', line 74

def convert_path(type, content)
  path = Savage::Parser.parse(content['d'])
  flip(path)
end

#convert_polygon(type, content) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hieroglyph/glyph.rb', line 59

def convert_polygon(type, content)
  Hieroglyph.log 'polygon found - converting', 9
  points = content['points'].split(" ")
  Savage::Path.new do |path|
    start_position = points.shift.split(",")
    path.move_to(start_position[0], start_position[1])
    points.each do |point|
      position = point.split(",")
      path.line_to(position[0], position[1])
    end
    path.close_path
    flip(path)
  end
end

#flip(path) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/hieroglyph/glyph.rb', line 91

def flip(path)
  path.subpaths.each do |subpath|
    subpath.directions.each do |direction|
      case direction
      when Savage::Directions::MoveTo
        if(direction.absolute?)
          direction.target.y = flip_y(direction.target.y)
        else
          direction.target.y = -1 * direction.target.y
        end
      when Savage::Directions::VerticalTo
        if(direction.absolute?)
          direction.target = flip_y(direction.target)
        else
          direction.target = -1 * direction.target
        end
      when Savage::Directions::LineTo
        if(direction.absolute?)
          direction.target.y = flip_y(direction.target.y)
        else
          direction.target.y = -1 * direction.target.y
        end
      when Savage::Directions::CubicCurveTo
        if(direction.absolute?)
          direction.control.y = flip_y(direction.control.y)
          direction.target.y = flip_y(direction.target.y)
          if(defined?(direction.control_1) && defined?(direction.control_1.y))
            direction.control_1.y = flip_y(direction.control_1.y)
          end
        else
          direction.control.y = -1 * direction.control.y
          direction.target.y = -1 * direction.target.y
          if(defined?(direction.control_1) && defined?(direction.control_1.y))
            direction.control_1.y = -1 * direction.control_1.y
          end
        end
      end
    end
  end
  path
end

#flip_y(value) ⇒ Object



133
134
135
136
137
# File 'lib/hieroglyph/glyph.rb', line 133

def flip_y(value)
  value = value.to_f
  value = (value - 500) * -1 + 500
  value = value - 25
end

#match(str, pattern_start, pattern, pattern_end) ⇒ Object



23
24
# File 'lib/hieroglyph/glyph.rb', line 23

def match(str, pattern_start, pattern, pattern_end)
end

#parse_shapesObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hieroglyph/glyph.rb', line 45

def parse_shapes
  path = Savage::Path.new
  SHAPE_HANDLERS.each do |type, method|
    contents = @contents.root.css(type)
    unless contents.length == 0
      report_too_many if contents.length > 1
      report_too_many if @shape_found
      path = self.method(method).call(type, contents.first)
      @shape_found = true
    end
  end
  path
end

#report_invalid(type, content) ⇒ Object



79
80
81
82
# File 'lib/hieroglyph/glyph.rb', line 79

def report_invalid(type, content)
  Hieroglyph.error "#{type} found - this shape is invalid!", 4
  Hieroglyph.error "'make compound path' in your vector tool to fix", 4
end

#report_too_manyObject



84
85
86
87
88
89
# File 'lib/hieroglyph/glyph.rb', line 84

def report_too_many
  unless @too_many
    Hieroglyph.error 'too many shapes! your icon might look weird as a result', 4
    @too_many = true
  end
end

#set_name(file, source) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/hieroglyph/glyph.rb', line 34

def set_name(file, source)
  @name = file.gsub(source, '').gsub('/', '')
  @name = @name.match(/^..*?(-|\.)/).to_s.chop
  unicode = @name.match(/^&#(x|X).*?;/).to_s.gsub(/^&#(x|X)/, '').gsub(/;/, '')
  unless unicode.empty?
    @font.unicode_values.push(unicode.to_s.upcase)
  else
    @font.characters.push(@name)
  end
end

#to_nodeObject



139
140
141
# File 'lib/hieroglyph/glyph.rb', line 139

def to_node
  @path ? "<glyph unicode=\"#{@name}\" d=\"#{@path.to_command}\" />\n" : ''
end