Class: Compass::Fontcustom::FontImporter

Inherits:
Sass::Importers::Base
  • Object
show all
Defined in:
lib/compass/fontcustom/font_importer.rb

Overview

The Sass Importer responsible to find svg and eps files and to start the Fontcustom font generator.

Constant Summary collapse

GLYPH_FILE_REGEX =

Regexp matching uri’s of svg and eps files. Matching group ‘$1` contains the path, `$3` the file name.

%r{((.+/)?([^\*.]+))/(.+?)\.(svg|eps)}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.content_for_font(uri, name) ⇒ String

Renders the stylesheet for font ‘name` at `uri`

Parameters:

  • uri (String)

    the uri to glob files from

  • name (String)

    the font’s name

Returns:

  • (String)


98
99
100
101
102
# File 'lib/compass/fontcustom/font_importer.rb', line 98

def content_for_font(uri, name)
  erb    = File.read File.join(template_path, 'stylesheet.scss.erb')
  binder = TemplateData.new(uri: uri, name: name, path: fonts_path, glyph_names: glyph_names(uri))
  ERB.new(erb).result(binder.expose_binding)
end

.files(uri) ⇒ Array

Returns an array of font files.

Parameters:

  • uri (String)

    the uri to glob files from

Returns:

  • (Array)


119
120
121
122
123
124
# File 'lib/compass/fontcustom/font_importer.rb', line 119

def files(uri)
  [].tap do |ary|
    ary.concat Dir[uri]
    search_paths.each { |p| ary.concat Dir[File.join p, uri] }
  end
end

.font_name(uri) ⇒ String

Returns only the file name component of ‘uri`.

Returns:

  • (String)

See Also:



44
45
46
47
# File 'lib/compass/fontcustom/font_importer.rb', line 44

def font_name(uri)
  _, name = path_and_name(uri)
  name
end

.fonts_pathString

Returns the fonts path

Returns:

  • (String)


106
107
108
# File 'lib/compass/fontcustom/font_importer.rb', line 106

def fonts_path
  Compass.configuration.fonts_dir.to_s
end

.glyph_names(uri) ⇒ Array

Returns all glyph names inside the folder at ‘uri`.

Parameters:

  • uri (String)

    the uri to glob files from

Returns:

  • (Array)


64
65
66
67
68
69
70
71
72
# File 'lib/compass/fontcustom/font_importer.rb', line 64

def glyph_names(uri)
  images = files(uri).sort

  if images.empty?
    raise Compass::SpriteException, %Q{No glyph images were found matching "#{uri}" in the images path. Your current images path is: #{folder}}
  end

  images.map { |f| File.basename(f)[0..-5] }
end

.path(uri) ⇒ String

Returns only the path component of ‘uri`

Returns:

  • (String)

See Also:



52
53
54
55
# File 'lib/compass/fontcustom/font_importer.rb', line 52

def path(uri)
  path, _ = path_and_name(uri)
  path
end

.path_and_name(uri) ⇒ Array

Returns an array with two elements. First the path, second the file name of the ‘uri`.

Parameters:

  • uri (String)

    an uri to match glyph image

Returns:

  • (Array)

    a two-element array containing the path and file name components



33
34
35
36
37
38
39
# File 'lib/compass/fontcustom/font_importer.rb', line 33

def path_and_name(uri)
  if uri =~ GLYPH_FILE_REGEX
    [$1, $3]
  else
    raise Compass::Error, "Invalid glyph image uri"
  end
end

.sass_engine(uri, name, importer, options) ⇒ Sass::Engine

Returns a ‘Sass::Engine`

Parameters:

  • uri (String)

    the uri to glob files from

  • name (String)

    the font’s name

  • importer (Sass::Importers::Base)

    the importer to use (in this case ‘self`)

  • options (Hash)

    options to merge with

Returns:

  • (Sass::Engine)


89
90
91
92
# File 'lib/compass/fontcustom/font_importer.rb', line 89

def sass_engine(uri, name, importer, options)
  content = content_for_font(uri, name)
  Sass::Engine.new(content, sass_options(uri, importer, options))
end

.sass_options(uri, importer, options) ⇒ Hash

Returns ‘Sass::Engine` options with defaults

Parameters:

  • uri (String)

    the uri to glob files from

  • importer (Sass::Importers::Base)

    the importer to use (in this case ‘self`)

  • options (Hash)

    options to merge with

Returns:

  • (Hash)


79
80
81
# File 'lib/compass/fontcustom/font_importer.rb', line 79

def sass_options(uri, importer, options)
  options.merge!(:filename => uri.gsub(%r{\*/},"*\\/"), :syntax => :scss, :importer => importer)
end

.search_pathsObject



57
58
59
# File 'lib/compass/fontcustom/font_importer.rb', line 57

def search_paths
  Compass.configuration.fontcustom_input_paths.to_a
end

.template_pathString

Returns the gems’ internal template path.

Returns:

  • (String)


112
113
114
# File 'lib/compass/fontcustom/font_importer.rb', line 112

def template_path
  File.expand_path('../templates', __FILE__)
end

Instance Method Details

#eql?(other) ⇒ Boolean

Compares this instance with another object.

Parameters:

  • other (Object)

    another object

Returns:

  • (Boolean)


164
165
166
# File 'lib/compass/fontcustom/font_importer.rb', line 164

def eql?(other)
  other.class == self.class
end

#find(uri, options) ⇒ Sass::Engine

Resolves incoming uri from an ‘@import “…”` directive.

Parameters:

  • uri (String)

    the uri from the import directive

  • options (Hash)

    options for the returned Sass::Engine`

Returns:

  • (Sass::Engine)


132
133
134
135
136
137
# File 'lib/compass/fontcustom/font_importer.rb', line 132

def find(uri, options)
  if uri =~ GLYPH_FILE_REGEX
    return self.class.sass_engine(uri, self.class.font_name(uri), self, options)
  end
  nil
end

#find_relative(uri, base, options) ⇒ Object

Unused. Just returns nil.



145
146
147
# File 'lib/compass/fontcustom/font_importer.rb', line 145

def find_relative(uri, base, options)
  nil
end

#hashString

Returns the hash of this instance.

Returns:

  • (String)


157
158
159
# File 'lib/compass/fontcustom/font_importer.rb', line 157

def hash
  self.class.name.hash
end

#key(uri, options = {}) ⇒ Array

This instance’s Compass cache key

Parameters:

  • uri (String)

    the uri to glob files from

  • options (Hash) (defaults to: {})

    hash of options

Returns:

  • (Array)


178
179
180
# File 'lib/compass/fontcustom/font_importer.rb', line 178

def key(uri, options={})
  [self.class.name + ":fontcustom:" + File.dirname(File.expand_path(uri)), File.basename(uri)]
end

#mtime(uri, options) ⇒ Object



168
169
170
171
172
# File 'lib/compass/fontcustom/font_importer.rb', line 168

def mtime(uri, options)
  self.class.files(uri).inject(Time.at(0)) do |max_time, file|
    (t = File.mtime(file)) > max_time ? t : max_time
  end
end

#public_url(*args) ⇒ Object

Unused



140
141
142
# File 'lib/compass/fontcustom/font_importer.rb', line 140

def public_url(*args)
  nil
end

#to_sString

Returns the string representation of this instance.

Returns:

  • (String)


151
152
153
# File 'lib/compass/fontcustom/font_importer.rb', line 151

def to_s
  self.class.name
end