Class: Fontcustom::Generator::Font

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

Instance Method Summary collapse

Instance Method Details

#announce_filesObject



93
94
95
96
97
# File 'lib/fontcustom/generator/font.rb', line 93

def announce_files
  if opts[:verbose]
    @data[:fonts].each { |file| shell.say_status(:create, File.join(opts[:output], file)) }
  end
end

#check_inputObject



15
16
17
18
19
20
21
# File 'lib/fontcustom/generator/font.rb', line 15

def check_input
  if ! File.directory? opts[:input]
    raise Fontcustom::Error, "#{opts[:input]} doesn't exist or isn't a directory."
  elsif Dir[File.join(opts[:input], "*.{svg,eps}")].empty?
    raise Fontcustom::Error, "#{opts[:input]} doesn't contain any vectors (*.svg or *.eps files)."
  end
end

#check_outputObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/fontcustom/generator/font.rb', line 23

def check_output
  if File.exists? File.join(opts[:output], ".fontcustom-data")
    # Skip ahead, everything is in order
  elsif File.exists?(opts[:output]) && ! File.directory?(opts[:output])
    raise Fontcustom::Error, "#{opts[:output]} already exists but isn't a directory."
  else
    # creates opts[:output] as well
    add_file File.join(opts[:output], ".fontcustom-data"), :verbose => opts[:verbose]
  end
end

#collect_dataObject



87
88
89
90
91
# File 'lib/fontcustom/generator/font.rb', line 87

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

#generateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fontcustom/generator/font.rb', line 60

def generate
  # TODO align option naming conventions with python script
  # TODO remove name arg if default is already set in python (or rm from python)
  name = opts[:font_name] ? " --name " + opts[:font_name] : ""
  hash = opts[:file_hash] ? "" : " --nohash"
  cmd = "fontforge -script #{Fontcustom::Util.gem_lib_path}/scripts/generate.py #{opts[:input]} #{opts[:output] + name + hash} 2>&1"

  output = `#{cmd}`.split("\n")
  @json = output[3] # JSON
  if @json == 'Warning: Font contained no glyphs'
    @json = output[4]
    output = output[5..-1] # Strip fontforge message
  else
    @json = output[3]
    output = output[4..-1] # Strip fontforge message
  end

  if opts[:debug]
    shell.say "DEBUG: (raw output from fontforge)"
    shell.say output
  end

  unless $?.success?
    raise Fontcustom::Error, "Compilation failed unexpectedly. Check your options and try again with --debug get more details."
  end
end

#get_dataObject



34
35
36
37
38
39
40
41
# File 'lib/fontcustom/generator/font.rb', line 34

def get_data
  # file has already been verified/created
  data = File.read File.join(opts[:output], ".fontcustom-data") 
  data = JSON.parse(data, :symbolize_names => true) unless data.empty?
  @data = data.is_a?(Hash) ? data : Fontcustom::DATA_MODEL.dup
rescue JSON::ParserError
  raise Fontcustom::Error, "The .fontcustom-data file in #{opts[:output]} is corrupted. Fix the JSON or delete the file to start from scratch."
end

#reset_outputObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fontcustom/generator/font.rb', line 43

def reset_output
  return if @data[:fonts].empty?
  begin
    deleted = []
    @data[:fonts].each do |file| 
      remove_file File.join(opts[:output], file), :verbose => opts[:verbose]
      deleted << file
    end
  ensure
    @data[:fonts] = @data[:fonts] - deleted
    json = JSON.pretty_generate @data
    file = File.join(opts[:output], ".fontcustom-data")
    Fontcustom::Util.clear_file(file)
    append_to_file file, json, :verbose => false # clear data file silently
  end
end

#save_dataObject



99
100
101
102
103
104
# File 'lib/fontcustom/generator/font.rb', line 99

def save_data
  json = JSON.pretty_generate @data
  file = File.join(opts[:output], ".fontcustom-data")
  Fontcustom::Util.clear_file(file)
  append_to_file file, json, :verbose => opts[:verbose]
end