Class: Text2Path::Converter
- Inherits:
-
Object
- Object
- Text2Path::Converter
- Defined in:
- lib/text2path/converter.rb
Instance Attribute Summary collapse
-
#font ⇒ Object
Returns the value of attribute font.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#initialize(text, font, opt = {}) ⇒ Converter
constructor
A new instance of Converter.
- #letter_to_path(lt) ⇒ Object
- #to_paths ⇒ Object
- #to_svg ⇒ Object
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
#font ⇒ Object
Returns the value of attribute font.
5 6 7 |
# File 'lib/text2path/converter.rb', line 5 def font @font end |
#text ⇒ Object
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_paths ⇒ Object
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_svg ⇒ Object
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 |