module Jitai require 'jitai/railtie.rb' if defined?(Rails) class FontManager attr_accessor :moz_path, :ie_path, :cwd def initialize(path= "public") @moz_path = "#{path}/stylesheets/fonts_moz.css" @ie_path = "#{path}/stylesheets/fonts_ie.css" @cwd = "public/fonts/" end def refresh Dir.mkdir(@cwd) if !(File.directory? @cwd) Dir.foreach(@cwd) do |font_file| if !(File.directory?(font_file)) # Search for two different formats conversion_type = (File.extname(font_file).downcase.eql? ".ttf")? ".eot" : ".ttf" other_basename = font_file.split(".")[0] + conversion_type other_file = "./#{@cwd}#{other_basename}" file = "./#{@cwd}#{font_file}" # TODO edit ttf2eot so that it's actually installed in the system if !File.exists?(other_file) puts "===== #{conversion_type.upcase} format not found. Attempting to convert... =====" # TODO fix incredibly bad hack curr_dir = Dir.getcwd ttf2eot_dir = "/usr/lib/ruby/gems/1.8/gems/jitai-0.2.28/lib/ttf2eot/" %x{cd #{ttf2eot_dir} && ./ttf2eot < #{'../' + @cwd + font_file} > #{'../' + @cwd + font_file.split(".")[0] + conversion_type} && cd #{curr_dir}} end # Rename files, account for conventions. %x{mv #{other_file} #{other_file.downcase}} if !(File.exists?(other_file.downcase)) %x{mv #{file} #{file.downcase}} if !(File.exists?(file.downcase)) font = Jitai::Font.new(font_file) font.to_css end end end end class Font attr_accessor :name, :path def initialize(path) @name = path.downcase.split(".")[0] @path = path end def to_css moz = "@font-face {font-family:'#{@name}'; src:url('#{name}.ttf');}" ie = "@font-face {font-family:'#{@name}'; src:url('#{name}.eot');}" manager = Jitai::FontManager.new find_font(manager.moz_path, moz) find_font(manager.ie_path, ie) end def find_font(file, search_font) File.open(file, "w") if !File.exists?(file) # touch file in_file = false File.open(file) do |font| test = font.gets if !test.nil? return (in_file = true) if search_font.eql? test.strip end end unless in_file File.open(file, "a") do |f| f.puts search_font puts search_font puts "Was added to #{file}" end end end end end