Class: Text2Path::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/text2path/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, font, opt = {}) ⇒ Converter

Returns a new instance of Converter.



7
8
9
10
11
# File 'lib/text2path/converter.rb', line 7

def initialize( text, font, opt={} )
  @text = text
  @font = font
  @opt  = opt
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



5
6
7
# File 'lib/text2path/converter.rb', line 5

def font
  @font
end

#textObject

Returns the value of attribute text.



5
6
7
# File 'lib/text2path/converter.rb', line 5

def text
  @text
end

Instance Method Details

#letter_to_path(lt) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/text2path/converter.rb', line 32

def letter_to_path( lt )
  glyph = @font.glyph( lt )
  scale = font_size / @font.units_per_em

  if glyph.empty?
    @advance_x += glyph.horiz_adv_x * scale
    nil
  else
    SvgPath.parse( glyph.path ) do |path|
      path.scale scale, -scale
      # TODO: support other text directions
      path.translate advance_x, @font.default_glyph_height * scale
      @advance_x += glyph.horiz_adv_x * scale
    end
  end
end

#to_pathsObject



25
26
27
28
29
30
# File 'lib/text2path/converter.rb', line 25

def to_paths
  @advance_x = 0
  @text.each_char.map do |letter|
    letter_to_path( letter )
  end.compact
end

#to_svgObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/text2path/converter.rb', line 13

def to_svg
  paths = to_paths
    %Q{<?xml version="1.0" standalone="no"?>} <<
    <<-SVG
      <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
      <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
        <g>#{paths.map {|p| path_element(p) }.join("\n") }</g>
      </svg>
    SVG
  
end