prawnpdf / ttfunk

Font Metrics Parser for Prawn
http://prawnpdf.org
Other
125 stars 75 forks source link

font weights? #25

Closed mustmodify closed 9 years ago

mustmodify commented 9 years ago

This works perfectly:

def width_of( string )
  if string.nil? || (string == '')
    0
  else
    string.split('').map{|char| character_width( char )}.inject{|sum, x| sum + x}
  end
end

def character_width( character )
  width_in_units = ( horizontal_metrics.for( glyph_id( character )).advance_width )
  width_in_units.to_f / units_per_em
end

def horizontal_metrics
  @hm = file.horizontal_metrics
end

def glyph_id(character)
  character_code = character.unpack("U*").first
  file.cmap.unicode.first[character_code]
end

def file
  @file
end

def filename=(path_to_file)
  @file = TTFunk::File.open(path_to_file)
end

as long as I'm not using a different weight. How can I specify a font weight?

mustmodify commented 9 years ago

/cc @practicingruby

packetmonkey commented 9 years ago

If I understand you correctly, font 'weights' are actually different fonts, so if you want to compare the width of regular text vs bold text, you need to load both fonts and run the check that way.

Does that help?

mustmodify commented 9 years ago

ok, maybe I have the wrong file and that's why my widths are off. I'll dig further. Thanks.