Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlessone committed Dec 14, 2023
1 parent 194f3cf commit 54bcc79
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions examples/character-or-string-width.rb → examples/string_width.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
require 'rubygems'
require 'ttfunk'
require 'valuable'
# frozen_string_literal: true

# Everything you never wanted to know about glyphs:
# http://chanae.walon.org/pub/ttf/ttf_glyphs.htm

# this code is a substantial reworking of:
# https://github.com/prawnpdf/ttfunk/blob/master/examples/metrics.rb

require 'ttfunk'

class Font
attr_reader :file

def initialize(path_to_file)
@file = TTFunk::File.open(path_to_file)
end
def width_of( string )
string.split('').map{|char| character_width( char )}.inject{|sum, x| sum + x}

def width_of(string)
string.split('').sum { |char| character_width(char) }
end

def character_width( character )
width_in_units = ( horizontal_metrics.for( glyph_id( character )).advance_width )
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 units_per_em
@u_per_em ||= file.header.units_per_em
@units_per_em ||= file.header.units_per_em
end

def horizontal_metrics
@hm = file.horizontal_metrics
@horizontal_metrics ||= file.horizontal_metrics
end

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

0 comments on commit 54bcc79

Please sign in to comment.