From 7b95f34aa38721d789076bb332b55db76fd796eb Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 27 Nov 2009 17:44:22 -0800 Subject: [PATCH] Support for font urls and embedding fonts inline within the file --- .../sass_extensions/functions/font_files.rb | 3 ++- .../sass_extensions/functions/inline_image.rb | 23 ++++++++++++++++++- lib/compass/sass_extensions/functions/urls.rb | 20 ++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/compass/sass_extensions/functions/font_files.rb b/lib/compass/sass_extensions/functions/font_files.rb index f46b2721..079e2a10 100644 --- a/lib/compass/sass_extensions/functions/font_files.rb +++ b/lib/compass/sass_extensions/functions/font_files.rb @@ -3,8 +3,9 @@ module Compass::SassExtensions::Functions::FontFiles raise Sass::SyntaxError, "An even number of arguments must be passed to font_files()" unless args.size % 2 == 0 files = [] while args.size > 0 - files << "#{stylesheet_url(args.shift)} format('#{args.shift}')" + files << "#{font_url(args.shift)} format('#{args.shift}')" end Sass::Script::String.new(files.join(", ")) end + end diff --git a/lib/compass/sass_extensions/functions/inline_image.rb b/lib/compass/sass_extensions/functions/inline_image.rb index 97b7b0e2..257de327 100644 --- a/lib/compass/sass_extensions/functions/inline_image.rb +++ b/lib/compass/sass_extensions/functions/inline_image.rb @@ -3,11 +3,24 @@ module Compass::SassExtensions::Functions::InlineImage def inline_image(path, mime_type = nil) path = path.value - real_path = File.join(Compass.configuration.project_path, Compass.configuration.images_dir, path) + real_path = File.join(Compass.configuration.images_path, path) url = "url('data:#{compute_mime_type(path,mime_type)};base64,#{data(real_path)}')" Sass::Script::String.new(url) end + def inline_font_files(*args) + raise Sass::SyntaxError, "An even number of arguments must be passed to font_files()" unless args.size % 2 == 0 + path = path.value + files = [] + while args.size > 0 + path = args.shift.value + real_path = File.join(Compass.configuration.fonts_path, path) + url = "url('data:#{compute_mime_type(path,mime_type)};base64,#{data(real_path)}')" + files << "#{url} format('#{args.shift}')" + end + Sass::Script::String.new(files.join(", ")) + end + private def compute_mime_type(path, mime_type) return mime_type if mime_type @@ -18,6 +31,14 @@ private 'image/jpeg' when /\.gif$/i 'image/gif' + when /\.otf$/i + 'font/opentype' + when /\.ttf$/i + 'font/truetype' + when /\.woff$/i + 'font/woff' + when /\.off$/i + 'font/openfont' when /\.([a-zA-Z]+)$/ "image/#{Regexp.last_match(1).downcase}" else diff --git a/lib/compass/sass_extensions/functions/urls.rb b/lib/compass/sass_extensions/functions/urls.rb index d37e8044..4b15c56b 100644 --- a/lib/compass/sass_extensions/functions/urls.rb +++ b/lib/compass/sass_extensions/functions/urls.rb @@ -14,8 +14,28 @@ module Compass::SassExtensions::Functions::Urls url("#{http_stylesheets_path}/#{path}") end + def font_url(path) + path = path.value # get to the string value of the literal. + + # Short curcuit if they have provided an absolute url. + if absolute_path?(path) + return Sass::Script::String.new("url(#{path})") + end + + # Compute the path to the font file, either root relative or stylesheet relative + # or nil if the http_fonts_path cannot be determined from the configuration. + http_fonts_path = if relative? + compute_relative_path(Compass.configuration.fonts_dir) + else + Compass.configuration.http_fonts_path + end + + url("#{http_fonts_path}/#{path}") + end + def image_url(path) path = path.value # get to the string value of the literal. + # Short curcuit if they have provided an absolute url. if absolute_path?(path) return Sass::Script::String.new("url(#{path})")