Class: Blacksmith::FontBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil, &block) ⇒ FontBuilder

Returns a new instance of FontBuilder.



10
11
12
13
14
15
16
17
18
# File 'lib/blacksmith/font_builder.rb', line 10

def initialize(filename = nil, &block)
  raise "Expects filename or block" unless filename || block
  raise "Expects either a block or a filename - not both" if filename and block
  raise "File not found: #{filename}" if filename && !File.exist?(filename)
  
  @_instructions = block || File.read(filename)
  @_attributes = {}
  @_glyphs = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/blacksmith/font_builder.rb', line 45

def method_missing(name, *args)
  if args.length == 1
    @_attributes[name] = args[0]
  else
    super
  end
end

Class Method Details

.execute(*args, &block) ⇒ Object



4
5
6
# File 'lib/blacksmith/font_builder.rb', line 4

def execute(*args, &block)
  new(*args, &block).execute
end

Instance Method Details

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/blacksmith/font_builder.rb', line 20

def execute
  case @_instructions
  when String
    instance_eval(@_instructions)
  when Proc
    instance_eval(&@_instructions)
  end
  
  font = Blacksmith::Font.new(@_attributes)
  
  @_glyphs.each do |filename, attrs|
    attrs[:scale]  ||= font.scale
    attrs[:offset] ||= font.offset
    attrs[:source] ||= File.join(font.source, filename)
    
    font << Blacksmith::Glyph.new(attrs)
  end
  
  font
end

#glyph(filename, attrs) ⇒ Object



41
42
43
# File 'lib/blacksmith/font_builder.rb', line 41

def glyph(filename, attrs)
  @_glyphs[filename] = attrs
end