Support for font urls and embedding fonts inline within the file
This commit is contained in:
parent
070f39c6b7
commit
7b95f34aa3
@ -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
|
||||
|
@ -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
|
||||
|
@ -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})")
|
||||
|
Loading…
Reference in New Issue
Block a user