Class: Jitai::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/jitai.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Font

Returns a new instance of Font.



45
46
47
48
# File 'lib/jitai.rb', line 45

def initialize(path)
  @name = path.downcase.split(".")[0]
  @path = path
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



43
44
45
# File 'lib/jitai.rb', line 43

def name
  @name
end

#pathObject

Returns the value of attribute path.



43
44
45
# File 'lib/jitai.rb', line 43

def path
  @path
end

Instance Method Details

#find_font(file, search_font) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jitai.rb', line 66

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

#to_cssObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jitai.rb', line 50

def to_css
  # TODO WOFF url('/fonts/#{@name}.woff') format('woff'), /* Modern Browsers */
  # TODO SVG  url('/fonts/#{@name}.svg#svgFontName') format('svg'); /* Legacy iOS */
  
  css = "@font-face {
           font-family: '#{@name}';
           src: url('/fonts/#{@name}.eot'); /* IE9 Compat Modes */
           src: url('/fonts/#{@name}.eot?iefix') format('eot'), /* IE6-IE8 */
           url('/fonts/#{@name}.ttf')  format('truetype'), /* Safari, Android, iOS */
        }"

  # TODO make configurable for sinatra apps, etc
  manager = Jitai::FontManager.new
  find_font(manager.css_path, css)
end