Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Some documentation added :)

  • Loading branch information...
commit 0280e77fd3e7a9ba8b7800f2408a2ec6667bcd10 1 parent e5c8924
@colszowka authored
Showing with 15 additions and 1 deletion.
  1. +3 −1 lib/rack-fontserve.rb
  2. +12 −0 lib/rack-fontserve/font.rb
View
4 lib/rack-fontserve.rb
@@ -38,7 +38,7 @@ def set_cache
'Expires' => (Time.now + Rack::Fontserve.max_age).httpdate,
'Access-Control-Allow-Origin' => '*'
end
-
+
def format?(f)
@font.formats.include?(f.to_s)
end
@@ -48,12 +48,14 @@ def format?(f)
erb :demo
end
+ # Render the generated or custom css for given font with caching
get '/:font_name.css' do
@font = Font.new(params[:font_name])
prepare_headers :css
@font.custom_css? ? @font.custom_css : erb(:stylesheet)
end
+ # Render the font file for given font and format with caching
get '/:font_name.:format' do
@font = Font.new(params[:font_name])
@data = open(@font.format_path(params[:format]))
View
12 lib/rack-fontserve/font.rb
@@ -1,6 +1,9 @@
+# Representation of fonts in the Rack::Fontserve.fonts_path, including various
+# helpers to check and retrieve formats, license and custom css
class Rack::Fontserve::Font
attr_reader :name
+ # Returns an array of all present and valid fonts
def self.all
Dir[File.join(Rack::Fontserve.fonts_path.to_s, '*')].map do |path|
begin
@@ -16,41 +19,50 @@ def initialize(name)
raise Rack::Fontserve::InvalidFontError unless valid?
end
+ # Returns the base path of this font
def path
@path ||= File.join(Rack::Fontserve.fonts_path, name)
end
+ # Retruns a list of available formats
def formats
@formats ||= Dir[File.join(path, "#{name}.{otf,svg,ttf,woff,eot}")].map {|file| File.extname(file)[1..-1] }.sort
end
+ # Returns the path to the font file in given format. Raises InvalidFormatError if the font is not available in this format
def format_path(format)
raise Rack::Fontserve::InvalidFormatError unless formats.include?(format)
file_path "#{name}.#{format}"
end
+ # custom css file exists?
def custom_css?
File.exist? file_path("#{name}.css")
end
+ # Returns the content of the custom CSS file if present, nil otherwise
def custom_css
File.read file_path("#{name}.css") if custom_css?
end
+ # LICENSE file exists?
def license?
File.exist? file_path('LICENSE')
end
+ # Returns the content of the LICENSE file if present, nil otherwise
def license
File.read file_path('LICENSE') if license?
end
private
+ # Checks whether this font is valid - which is the case when the font has any formats present
def valid?
formats.count > 0
end
+ # Helper for paths to files in the font's home dir
def file_path(filename)
File.join(path, filename)
end
Please sign in to comment.
Something went wrong with that request. Please try again.