Sha256: 03f1f2a2b2cea26b6efb17e0a13a0992b8d0eb7075c1d61af70d1fc22ed23c65

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

require 'sinatra/base'
require 'rack-fontserve/version'

module Rack
  class Fontserve < Sinatra::Base
    class InvalidFontError < StandardError; end;
    class InvalidFormatError < StandardError; end;
    autoload :Font, 'rack-fontserve/font'
    
    CONTENT_TYPES = {'ttf' => 'font/truetype', 
                     'otf' => 'font/opentype', 
                     'woff' => 'font/woff', 
                     'eot' => 'application/vnd.ms-fontobject',
                     'svg' => 'image/svg+xml',
                     'css' => 'text/css;charset=utf-8'}
                     
    set :max_age, 365 * 24 * 60 * 60
    set :views, ::File.join(::File.dirname(__FILE__), 'rack-fontserve/views')
    set :demo, true
    
    not_found do
      [404, '']
    end
    
    error(InvalidFontError)   { not_found }
    error(InvalidFormatError) { not_found }
    
    helpers do
      def prepare_headers(format)
        set_content_type(format)
        set_cache
      end
      
      def set_content_type(format)
        headers['Content-Type'] = CONTENT_TYPES[format.to_s]
      end
      
      def set_cache
        headers 'Cache-Control' => "public, max-age=#{Rack::Fontserve.max_age}",
                'Expires' => (Time.now + Rack::Fontserve.max_age).httpdate,
                'Access-Control-Allow-Origin' => '*'        
      end

      def format?(f)
        @font.formats.include?(f.to_s)
      end
    end
    
    get '/' do
      not_found unless Rack::Fontserve.demo
      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]))
      prepare_headers params[:format]
      @data.read
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-fontserve-0.2.0 lib/rack-fontserve.rb
rack-fontserve-0.1.4 lib/rack-fontserve.rb
rack-fontserve-0.1.3 lib/rack-fontserve.rb