Class: SassFontimage::SassExtensions::FontImageGenerator

Inherits:
Sass::Script::Literal
  • Object
show all
Defined in:
lib/sass_fontimage/sass_extensions/font_image_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(font, env) ⇒ FontImageGenerator

Returns a new instance of FontImageGenerator.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 43

def initialize(font, env)
  # @kwargs = kwargs
  @environment = env    
  @font = font
  @path = self.class.find_font(font, env)
  
  fontname = @path.basename.to_s.split(".")[0..-2].join(".")
  write_path = self.font_image_location + fontname
  
  unless write_path.directory?
    FileUtils.mkdir(write_path)
  end
  
  @fontimage = SassFontimage::FontImage.new(@path, { :write_path => write_path })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 78

def method_missing(meth, *args, &block)
  if @environment.respond_to?(meth)
    @environment.send(meth, *args, &block)
  else
    super
  end
end

Class Method Details

.find_font(file, env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 20

def find_font(file, env)
  load_paths = env.options[:load_paths].map{|p| Pathname.new(p.to_s) }
      
  # Escape glob patterns
  file.gsub(/[\*\[\]\{\}\?]/) do |char|
    "\\#{char}"
  end    

  file = Pathname.new(file)
  
  load_paths.each do |p|
    path = file.absolute? ? file : p + file
    if full_path = Dir[path.to_s].first
      return Pathname.new(full_path)
    end
  end

  Pathname.new("")
end

.get(font, env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 7

def get(font, env)
  @cache ||= {}      
  
  font = font.value
  
  # Stupid cache mechanism, doesn't normalize paths
  return @cache[env][font] if @cache[env] && @cache[env].respond_to?(:[]) && @cache[env][font]
  
  @cache[env] ||= {}
  @cache[env][font] = self.new(font, env)
  
end

Instance Method Details

#font_image_locationObject



103
104
105
106
107
108
109
110
111
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 103

def font_image_location
  if @environment.options[:custom] && @environment.options[:custom][:font_image_location]
    dir = Pathname.new(@environment.options[:custom][:font_image_location])
    raise "Directory :font_image_location ('#{dir}') does not seem to be a directory" unless dir.directory?
    dir
  else
    ""
  end
end

#font_image_url(path) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 86

def font_image_url(path)
  
  url = []
  if  @environment.options[:custom] &&  @environment.options[:custom][:font_image_url]
    url <<  @environment.options[:custom][:font_image_url]
    url << path.to_s
    url.join("/").squeeze("/")
  else
    base = Pathname.new(@environment.options[:css_filename]).dirname
    path.relative_path_from(base)
  end

  
  
end

#generate(char, size, color) ⇒ Object

Generate the file if needed



68
69
70
71
72
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 68

def generate(char, size, color)
  color = "#" + color.rgb.map{|f| f.to_s(16).rjust(2, "0") }.join()
  path = @fontimage.write(char.value, color, size.to_i)
  font_image_url(path)
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 74

def respond_to?(meth)
  super || @environment.respond_to?(meth)
end

#to_s(kwargs = self.kwargs) ⇒ Object



59
60
61
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 59

def to_s(kwargs = self.kwargs)
  ""
end

#valueObject



63
64
65
# File 'lib/sass_fontimage/sass_extensions/font_image_generator.rb', line 63

def value
  self
end