Class: Fontcustom::Generator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



15
16
17
# File 'lib/fontcustom/generator.rb', line 15

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#cleanup_output_dirObject



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

def cleanup_output_dir
  css = File.join(@output, 'fontcustom.scss')
  old_name = if File.exists? css
               line = IO.readlines(css)[5]                           # font-family: "Example Font";
               line.scan(/".+"/)[0][1..-2].gsub(/\W/, '-').downcase  # => 'example-font'
             else
               'fontcustom'
             end

  old_files = Dir[File.join(@output, old_name + '-*.{woff,ttf,eot,svg}')]
  old_files << css if File.exists?(css)
  old_files.each {|file| remove_file file }
end

#create_stylesheetObject



79
80
81
82
83
84
85
# File 'lib/fontcustom/generator.rb', line 79

def create_stylesheet
  files = Dir[File.join(input, '*.{svg,eps}')]
  @classes = files.map {|file| File.basename(file)[0..-5].gsub(/\W/, '-').downcase }
  @path = File.basename(@path)

  template('templates/fontcustom.scss', File.join(@output, '../../sass/typography/_fontcustom.scss'))
end

#generateObject



60
61
62
63
64
65
66
67
68
# File 'lib/fontcustom/generator.rb', line 60

def generate
  gem_file_path = File.expand_path(File.join(File.dirname(__FILE__)))
  name = options.name ? ' --name ' + @name : ''
  nohash = options.nohash ? ' --nohash' : ''

  # suppress fontforge message
  # TODO get font name and classes from script (without showing fontforge message)
  `fontforge -script #{gem_file_path}/scripts/generate.py #{input} #{@output + name + nohash} > /dev/null 2>&1`
end

#normalize_nameObject



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

def normalize_name
  @name = if options.name
    options.name.gsub(/\W/, '-').downcase
  else
    'fontcustom'
  end
end

#show_pathsObject



70
71
72
73
74
75
76
77
# File 'lib/fontcustom/generator.rb', line 70

def show_paths
  file = Dir[File.join(@output, @name + '*.ttf')].first
  @path = file.chomp('.ttf')

  ['woff','ttf','eot','svg'].each do |type|
    say_status(:create, @path + '.' + type)
  end
end

#verify_fontforgeObject



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

def verify_fontforge
  if `which fontforge` == ''
    raise Thor::Error, 'Please install fontforge first.'
  end
end

#verify_input_dirObject



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

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

#verify_or_create_output_dirObject



33
34
35
36
# File 'lib/fontcustom/generator.rb', line 33

def verify_or_create_output_dir
  @output = options.output.nil? ? File.join(File.dirname(input), 'public/fonts/') : options.output
  empty_directory(@output) unless File.directory?(@output)
end