Support for font urls and embedding fonts inline within the file

This commit is contained in:
Chris Eppstein 2009-11-27 17:44:22 -08:00
parent 070f39c6b7
commit 7b95f34aa3
3 changed files with 44 additions and 2 deletions

View File

@ -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 raise Sass::SyntaxError, "An even number of arguments must be passed to font_files()" unless args.size % 2 == 0
files = [] files = []
while args.size > 0 while args.size > 0
files << "#{stylesheet_url(args.shift)} format('#{args.shift}')" files << "#{font_url(args.shift)} format('#{args.shift}')"
end end
Sass::Script::String.new(files.join(", ")) Sass::Script::String.new(files.join(", "))
end end
end end

View File

@ -3,11 +3,24 @@ module Compass::SassExtensions::Functions::InlineImage
def inline_image(path, mime_type = nil) def inline_image(path, mime_type = nil)
path = path.value 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)}')" url = "url('data:#{compute_mime_type(path,mime_type)};base64,#{data(real_path)}')"
Sass::Script::String.new(url) Sass::Script::String.new(url)
end 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 private
def compute_mime_type(path, mime_type) def compute_mime_type(path, mime_type)
return mime_type if mime_type return mime_type if mime_type
@ -18,6 +31,14 @@ private
'image/jpeg' 'image/jpeg'
when /\.gif$/i when /\.gif$/i
'image/gif' '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]+)$/ when /\.([a-zA-Z]+)$/
"image/#{Regexp.last_match(1).downcase}" "image/#{Regexp.last_match(1).downcase}"
else else

View File

@ -14,8 +14,28 @@ module Compass::SassExtensions::Functions::Urls
url("#{http_stylesheets_path}/#{path}") url("#{http_stylesheets_path}/#{path}")
end 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) def image_url(path)
path = path.value # get to the string value of the literal. path = path.value # get to the string value of the literal.
# Short curcuit if they have provided an absolute url. # Short curcuit if they have provided an absolute url.
if absolute_path?(path) if absolute_path?(path)
return Sass::Script::String.new("url(#{path})") return Sass::Script::String.new("url(#{path})")