Class: VCSRuby::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/font.rb

Constant Summary collapse

@@fonts =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, size) ⇒ Font

Returns a new instance of Font.



16
17
18
19
20
# File 'lib/font.rb', line 16

def initialize name, size
  @name = name
  @path = find_path
  @size = size
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/font.rb', line 12

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/font.rb', line 12

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



12
13
14
# File 'lib/font.rb', line 12

def size
  @size
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/font.rb', line 22

def exists?
  load_font_cache if @@fonts.length == 0

  !!font_by_name(@name)
end

#find_pathObject



28
29
30
31
32
33
34
35
36
# File 'lib/font.rb', line 28

def find_path
  load_font_cache if @@fonts.length == 0

  if exists?
    font_by_name(@name).glyphs
  else
    nil
  end
end

#font_by_name(name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/font.rb', line 38

def font_by_name name
  if name =~ /\./
    _, font = @@fonts.select{ |k, f| f.glyphs =~ /#{name}\z/ }.first
    return font
  else
    @@fonts[name]
  end
end

#line_heightObject



71
72
73
74
75
76
77
78
79
# File 'lib/font.rb', line 71

def line_height
  MiniMagick::Tool::Convert.new do |convert|
    convert.font path if exists?
    convert.pointsize size
    convert << 'label:F'
    convert.format '%h'
    convert << 'info:'
  end.to_i
end

#load_font_cacheObject



47
48
49
50
51
52
53
54
# File 'lib/font.rb', line 47

def load_font_cache

  fonts = MiniMagick::Tool::Identify.new(whiny: false) do |identify|
    identify.list 'font'
  end

  parse_fonts(fonts)
end

#parse_fonts(fonts) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/font.rb', line 56

def parse_fonts(fonts)
  font = nil
  fonts.lines.each do |line|
    key, value = line.strip.split(':', 2).map(&:strip)

    next if [nil, 'Path'].include? key

    if key == 'Font'
      @@fonts[value] = font = IMFont.new(value)
    else
      font.send("#{key}=", value)
    end
  end
end