class Text2Path::SvgFont

An SVG font is a font defined using SVG's ‘font’ element. You may be instested in the specification of svg font: – www.w3.org/TR/SVG/fonts.html

Attributes

advance_x[R]
advance_y[R]
glyphs[R]
missing_glyph[R]

“missing_glyph” is the fallback glyph when not letter not in the charmap

units_per_em[R]

Public Class Methods

new( font_path ) { |self| ... } click to toggle source
# File lib/text2path/svg_font.rb, line 24
def initialize( font_path )
  @font_path = font_path
  load_font
  yield self if block_given?
end

Public Instance Methods

default_glyph_height() click to toggle source
# File lib/text2path/svg_font.rb, line 30
def default_glyph_height
  units_per_em
end
glyph( letter ) click to toggle source
# File lib/text2path/svg_font.rb, line 34
def glyph( letter )
  key = letter.respond_to?(:ord) ? letter.ord : letter.to_i
  glyphs[key] || missing_glyph
end

Private Instance Methods

load_font() click to toggle source
# File lib/text2path/svg_font.rb, line 45
def load_font
  @xml = Nokogiri::XML( open( @font_path ) )
  parse_font_face_info
  parse_glyphs
end
parse_font_face_info() click to toggle source
# File lib/text2path/svg_font.rb, line 51
def parse_font_face_info
  font_element = @xml.css('font').first
  face_element = @xml.css('font-face').first
  %w[
    horiz-origin-x    horiz-adv-x
    horiz-origin-y    horiz-adv-y
  ].each do |attr|
    instance_variable_set "@#{attr.gsub('-', '_')}", font_element.attr(attr).to_f
  end

  %w[
    units-per-em
    panose-1
  ].each do |attr|
    instance_variable_set "@#{attr.gsub('-', '_')}", face_element.attr(attr).to_f
  end

end
parse_glyph( node ) click to toggle source
# File lib/text2path/svg_font.rb, line 96
def parse_glyph( node )
  adv_x = node.attr('horiz-adv-x') || @horiz_adv_x
  Glyph.new( node.attr('d'), adv_x.to_f )
end
parse_glyphs() click to toggle source
# File lib/text2path/svg_font.rb, line 70
def parse_glyphs
  decoder = HTMLEntities.new
  @glyphs = @xml.css('glyph').inject({}) do |map, glyph|
    name = glyph.attr('unicode')
    key = case name
          when /^&#x(\w+)$/
            $1.hex
          when /^.$/
            name.ord
          when /^&.+;$/
            # e.g.
            # &  " ..
            decoder.decode( name )[0].ord
          when nil
            # just ignore it
          else
            raise "unknown name: #{name}"
          end

    map[key]  = parse_glyph( glyph )
    map
  end
  
  @missing_glyph = parse_glyph( @xml.css('missing-glyph').first )
end