Class: Fontcustom::Generator::Font

Inherits:
Thor::Group
  • Object
show all
Includes:
Util, Thor::Actions
Defined in:
lib/fontcustom/generator/font.rb

Instance Method Summary collapse

Methods included from Util

#check_fontforge, #expand_path, #overwrite_file, #relative_to_root, #say_changed, #say_message, #symbolize_hash

Instance Method Details

#announce_filesObject



73
74
75
# File 'lib/fontcustom/generator/font.rb', line 73

def announce_files
  say_changed :create, @data[:fonts]
end

#collect_dataObject



66
67
68
69
70
71
# File 'lib/fontcustom/generator/font.rb', line 66

def collect_data
  json = JSON.parse(@json, :symbolize_names => true)
  @data.merge! json
  @data[:glyphs].map! { |glyph| glyph.gsub(/\W/, "-") }
  @data[:fonts].map! { |font| File.join(opts.output[:fonts], font) }
end

#generateObject

Raises:



56
57
58
59
60
61
62
63
64
# File 'lib/fontcustom/generator/font.rb', line 56

def generate
  cmd = "fontforge -script #{Fontcustom.gem_lib}/scripts/generate.py #{opts.input[:vectors]} #{opts.output[:fonts]} --name #{opts.font_name}" 
  cmd += " --nohash" if opts.no_hash
  cmd += " --debug" if opts.debug
  output, err, status = execute_and_clean(cmd)
  @json = output.delete_at(0)
  say_status :debug, "#{err}\n#{' ' * 14}#{output}", :red if opts.debug
  raise Fontcustom::Error, "`fontforge` compilation failed. Try again with --debug for more details." unless status.success?
end

#get_dataObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fontcustom/generator/font.rb', line 26

def get_data
  if File.exists? opts.data_cache
    begin
      data = File.read opts.data_cache
      data = JSON.parse(data, :symbolize_names => true) unless data.empty?
      @data = data.is_a?(Hash) ? symbolize_hash(data) : Fontcustom::DATA_MODEL.dup
    rescue
      raise Fontcustom::Error, "Couldn't parse `#{relative_to_root(opts.data_cache)}`. Delete it to start from scratch. Any previously generated files will need to be deleted manually."
    end
  else
    @data = Fontcustom::DATA_MODEL.dup
  end
end

#prepare_output_dirsObject



17
18
19
20
21
22
23
24
# File 'lib/fontcustom/generator/font.rb', line 17

def prepare_output_dirs
  dirs = opts.output.values.uniq
  dirs.each do |dir|
    unless File.directory? dir
      empty_directory dir, :verbose => ! opts.quiet
    end
  end
end

#reset_outputObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fontcustom/generator/font.rb', line 40

def reset_output
  return if @data[:fonts].empty?
  begin
    deleted = []
    @data[:fonts].each do |file|
      remove_file file, :verbose => false
      deleted << file
    end
  ensure
    @data[:fonts] = @data[:fonts] - deleted
    json = JSON.pretty_generate @data
    overwrite_file opts.data_cache, json
    say_changed :delete, deleted
  end
end

#save_dataObject



77
78
79
80
# File 'lib/fontcustom/generator/font.rb', line 77

def save_data
  json = JSON.pretty_generate @data
  overwrite_file opts.data_cache, json
end