lib/compass/fontcustom/sass_extensions.rb in compass-fontcustom-1.3.0 vs lib/compass/fontcustom/sass_extensions.rb in compass-fontcustom-1.3.1

- old
+ new

@@ -6,25 +6,26 @@ # Sass function extensions module Functions # Font type format mappings used in css font-face declarations. # @see #glyph_font_sources - FONT_TYPE_FORMATS = { - 'eot?#iefix' => 'embedded-opentype', - 'woff' => 'woff', - 'ttf' => 'truetype', - "svg#%{font_name}" => 'svg' + FONT_TYPE_OPTIONS = { + eot: {format: 'embedded-opentype', postfix: '?#iefix'}, + woff: {format: 'woff'}, + ttf: {format: 'truetype'}, + svg: {format: 'svg', postfix: '#%{font_name}'} } # Returns `:before` pseudo class styles for the letter at `index` of the font. # - # @param index [FixNum] the font's index + # @param map [Compass::Fontcustom::GlyphMap] a glyph map + # @param glyph [String] glyph name # @return [Sass::Script::String] - def glyph(index) - idx = (61696+index.value-1).to_s(16) - css = %Q[&:before { content: "\\#{idx}"; }] - Sass::Script::String.new %Q["\\#{idx}"] + def glyph(map, glyph) + # Name transform should be implemented as in FontCustom + glyph = map.glyphs[Util.sanitize_symbol(glyph).to_sym] + Sass::Script::String.new "'\\#{glyph[:codepoint]}'" end Sass::Script::Functions.declare :letter, [:index] # Returns a `GlyphMap` representing a font. # @@ -40,13 +41,18 @@ # @param map [Compass::Fontcustom::GlyphMap] a glyph map # @return [Sass::Script::String] def glyph_font_sources(map) map.generate src = [] - FONT_TYPE_FORMATS.each do |type, format| - url = glyph_font_type_url map, type - src << "#{url} format('#{format}')" + + fonts = map.fonts + + FONT_TYPE_OPTIONS.each do |font_type, options| + if font = fonts[font_type] + url = glyph_font_type_url("#{font}#{options[:postfix]}" % {font_name: map.name}) + src << "#{url} format('#{options[:format]}')" + end end Sass::Script::String.new src.join ", " end Sass::Script::Functions.declare :glyph_font_sources, [:map] @@ -69,19 +75,16 @@ Sass::Script::Functions.declare :glyph_font_name_quoted, [:map] # Helper method. Returns a `Sass::Script::Functions#font_url for the font of `type` in `map`. # # @return [String] - def glyph_font_type_url(map, type) - type = type % {font_name: map.name} - file_name = "#{map.filename}.#{type}" - font_file = Sass::Script::String.new file_name + def glyph_font_type_url(file_path) + font_file = Sass::Script::String.new File.basename(file_path) font_url(font_file).value end def sanitize_symbol(name) - sanitized = name.value.to_s.gsub(/[.+{};]+/, ' ').strip.gsub(/[ ]+/, '-') - Sass::Script::String.new sanitized + Sass::Script::String.new Util.sanitize_symbol name.value end Sass::Script::Functions.declare :sanitize_symbol, [:name] end