Class: Rack::Fontserve::Font

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-fontserve/font.rb

Overview

Representation of fonts in the Rack::Fontserve.fonts_path, including various helpers to check and retrieve formats, license and custom css

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Font

Returns a new instance of Font.



17
18
19
20
# File 'lib/rack-fontserve/font.rb', line 17

def initialize(name)
  @name = File.basename(name) # Ensure we remove any path sections that might get burned into here
  raise Rack::Fontserve::InvalidFontError unless valid?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/rack-fontserve/font.rb', line 4

def name
  @name
end

Class Method Details

.allObject

Returns an array of all present and valid fonts



7
8
9
10
11
12
13
14
15
# File 'lib/rack-fontserve/font.rb', line 7

def self.all
  Dir[File.join(Rack::Fontserve.fonts_path.to_s, '*')].map do |path|
    begin
      new(File.basename(path)) 
    rescue Rack::Fontserve::InvalidFontError
      nil
    end  
  end.compact
end

Instance Method Details

#custom_cssObject

Returns the content of the custom CSS file if present, nil otherwise



44
45
46
# File 'lib/rack-fontserve/font.rb', line 44

def custom_css
  File.read file_path("#{name}.css") if custom_css?
end

#custom_css?Boolean

custom css file exists?

Returns:

  • (Boolean)


39
40
41
# File 'lib/rack-fontserve/font.rb', line 39

def custom_css?
  File.exist? file_path("#{name}.css")
end

#format_path(format) ⇒ Object

Returns the path to the font file in given format. Raises InvalidFormatError if the font is not available in this format



33
34
35
36
# File 'lib/rack-fontserve/font.rb', line 33

def format_path(format)
  raise Rack::Fontserve::InvalidFormatError unless formats.include?(format)
  file_path "#{name}.#{format}"
end

#formatsObject

Retruns a list of available formats



28
29
30
# File 'lib/rack-fontserve/font.rb', line 28

def formats
  @formats ||= Dir[File.join(path, "#{name}.{otf,svg,ttf,woff,eot}")].map {|file| File.extname(file)[1..-1] }.sort
end

#licenseObject

Returns the content of the LICENSE file if present, nil otherwise



54
55
56
# File 'lib/rack-fontserve/font.rb', line 54

def license
  File.read file_path('LICENSE') if license?
end

#license?Boolean

LICENSE file exists?

Returns:

  • (Boolean)


49
50
51
# File 'lib/rack-fontserve/font.rb', line 49

def license?
  File.exist? file_path('LICENSE')
end

#pathObject

Returns the base path of this font



23
24
25
# File 'lib/rack-fontserve/font.rb', line 23

def path
  @path ||= File.join(Rack::Fontserve.fonts_path, name)
end