Module: Convert

Defined in:
lib/typefascist/convert.rb

Constant Summary collapse

@@fontforge_enabled =
[
'woff', 'otf', 'ufo', 'svg', 'eot', 'afm', 'bin', 'cff', 
'dfont', 'pdf', 'pfa', 'pfb', 'pfm', 'ps', 'pt3', 'suit', 
't11', 't42', 'tfm', 'ttc', 'ttf'
]

Class Method Summary collapse

Class Method Details

.fontforge(file, from, to) ⇒ Object

////////////////////////////// /// TO BASICALLY ANYTHING //// //////////////////////////////



28
29
30
31
32
33
34
35
# File 'lib/typefascist/convert.rb', line 28

def self.fontforge(file, from, to)
  RubyPython.start()
  fontforge = RubyPython.import('fontforge')
  font = suppress_output { fontforge.open(file) }
  file.sub! from, to
  suppress_output { fontforge.font.generate(font, file) }
  RubyPython.stop
end

.forge(file, from, to) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/typefascist/convert.rb', line 37

def self.forge(file, from, to)
  fonts = [from, to];
  if (fonts & @@fontforge_enabled).length.eql? 2
    Announce.info "Converting from #{from} to #{to}"
    self.fontforge(file, from, to)
    Announce.success "Font converted at #{file}"
  else
    conversion = "to_#{to}"
    if(self.respond_to? :"#{conversion}")
      self.method(conversion).call(file, from)
    else
      Announce.failure "Conversion from #{from} to #{to} not supported"
    end
  end
end

.suppress_outputObject

Suppresses all output to terminal



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/typefascist/convert.rb', line 58

def self.suppress_output
  begin
    original_stderr = $stderr.clone
    original_stdout = $stdout.clone
    $stderr.reopen(File.new('/dev/null', 'w'))
    $stdout.reopen(File.new('/dev/null', 'w'))
    retval = yield
  rescue Exception => e
    $stdout.reopen(original_stdout)
    $stderr.reopen(original_stderr)
    raise e
  ensure
    $stdout.reopen(original_stdout)
    $stderr.reopen(original_stderr)
  end
  retval
end

.to_woff2(file, from) ⇒ Object

///////////////// /// TO WOFF2 //// /////////////////



15
16
17
18
19
20
21
22
# File 'lib/typefascist/convert.rb', line 15

def self.to_woff2(file, from)
  fontforge(file, from, 'otf')
  Announce.info "Converting from #{from} to woff2"
  suppress_output { system "#{Pathname.pwd}/lib/convertors/sfnt_to_woff2 #{file}" }
  suppress_output { system "rm -rf #{file}" }
  file.sub! 'otf', 'woff2'
  Announce.success "Font converted at #{file}"
end