Class: SassFontimage::FontImage
- Inherits:
-
Object
- Object
- SassFontimage::FontImage
- Defined in:
- lib/sass_fontimage/font_image.rb
Instance Method Summary collapse
-
#initialize(font, options = {}) ⇒ FontImage
constructor
A new instance of FontImage.
-
#render(char, color = @options[:color], size = @options[:size]) ⇒ Object
Renders a character on a RMagick canvas.
-
#write(char, color = @options[:color], size = @options[:size]) ⇒ Object
Writes the character out to a image file.
Constructor Details
#initialize(font, options = {}) ⇒ FontImage
Returns a new instance of FontImage.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/sass_fontimage/font_image.rb', line 11 def initialize(font, = {}) @font = Pathname.new(font) raise ArgumentError, "Font '#{font}' not found on disk" unless File.exist?(font) defaults = { :size => 16, :color => "#000000", :write_path => "", :file_prefix => "icon", :file_type => "png" } = defaults.update() end |
Instance Method Details
#render(char, color = @options[:color], size = @options[:size]) ⇒ Object
Renders a character on a RMagick canvas
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sass_fontimage/font_image.rb', line 31 def render(char, color = [:color], size = [:size]) img = Magick::Image.new(size.to_i, size.to_i, Magick::HatchFill.new('transparent', 'transparent')) draw = Magick::Draw.new char = convert_to_unicode(char) draw.font = @font.to_s draw.interline_spacing = 0 draw.pointsize = size.to_i draw.gravity = Magick::CenterGravity draw.fill = color draw.text_antialias = true draw.annotate(img, 0, 0, 0, 0, char) img end |
#write(char, color = @options[:color], size = @options[:size]) ⇒ Object
Writes the character out to a image file.
The resulting filename has the following format: PREFIX-SIZExSIZE-COLOR-HEXCODEPOINT.FILETYPE
Example: icon-16x16-000000-f001.png
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sass_fontimage/font_image.rb', line 64 def write(char, color = [:color], size = [:size]) path = image_path(char, color, size) # Let's not regenerate the same image if the font hasn't changed return path if(path.exist? && @font.mtime <= path.mtime) img = self.render(char, color, size) img.write(path.to_s) do self.format = "PNG32" end path end |