Class: Fontisan::Ufo::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/fontisan/ufo/cli.rb

Overview

CLI subcommand for UFO source operations.

fontisan ufo build font.ufo --output out.ttf [--format otf]
fontisan ufo convert font.ttf font.ufo
fontisan ufo validate font.ufo

Instance Method Summary collapse

Instance Method Details

#build(ufo) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fontisan/ufo/cli.rb', line 18

def build(ufo)
  font = Font.open(ufo)
  format = (options[:to] || "ttf").to_s.downcase.to_sym
  Convert.convert(font, to: format, output_path: options[:output])
  puts "wrote #{options[:output]} (#{File.size(options[:output])} bytes)"
rescue ArgumentError => e
  warn e.message
  exit 1
rescue Errno::ENOENT
  warn "UFO not found: #{ufo}"
  exit 1
end

#convert(input, output) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fontisan/ufo/cli.rb', line 34

def convert(input, output)
  if ufo?(input)
    font = Font.open(input)
    format = options[:to] || File.extname(output).delete(".").downcase
    Convert.convert(font, to: format.to_sym, output_path: output)
  else
    # Binary → UFO
    loaded = Fontisan::FontLoader.load(input)
    ufo = Convert::FromBinData.convert(loaded)
    Writer.new(ufo).write(output)
  end
  puts "wrote #{output}"
rescue ArgumentError => e
  warn e.message
  exit 1
end

#extract(ufo, glyph_name, output) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fontisan/ufo/cli.rb', line 70

def extract(ufo, glyph_name, output)
  font = Font.open(ufo)
  glyph = font.glyph(glyph_name)
  raise ArgumentError, "glyph not found: #{glyph_name.inspect}" unless glyph

  head = font.info
  renderer = Fontisan::Svg::StandaloneGlyph.new(
    units_per_em: head.units_per_em || 1000,
    ascent: head.ascender || 800,
    descent: head.descender || -200,
  )
  File.write(output, renderer.generate(glyph))
  puts "wrote #{output} (#{File.size(output)} bytes)"
rescue ArgumentError => e
  warn e.message
  exit 1
rescue Errno::ENOENT
  warn "UFO not found: #{ufo}"
  exit 1
end

#validate(ufo) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fontisan/ufo/cli.rb', line 52

def validate(ufo)
  font = Font.open(ufo)
  issues = []
  issues << "no glyphs in default layer" if font.glyphs.empty?
  issues << "no family name" unless font.info.family_name
  issues << "unitsPerEm not set" unless font.info.units_per_em
  issues << "missing .notdef glyph" unless font.glyph(".notdef")

  if issues.empty?
    puts "OK  #{ufo}"
  else
    issues.each { |i| warn "FAIL  #{i}" }
    exit 1
  end
end