class Text2Path::Converter

Attributes

font[RW]
text[RW]

Public Class Methods

new( text, font, opt={} ) click to toggle source
# File lib/text2path/converter.rb, line 7
def initialize( text, font, opt={} )
  @text = text
  @font = font
  @opt  = opt
end

Public Instance Methods

letter_to_path( lt ) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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

Private Instance Methods

advance_x() click to toggle source
# File lib/text2path/converter.rb, line 55
def advance_x
  @advance_x
end
font_size() click to toggle source
# File lib/text2path/converter.rb, line 59
def font_size
  @font_size ||= @opt[:font_size] || font.units_per_em
end
path_element( path ) click to toggle source
# File lib/text2path/converter.rb, line 51
def path_element( path )
  %Q{<path d="#{path.to_command}" />}
end