Class: SassFontimage::FontImage

Inherits:
Object
  • Object
show all
Defined in:
lib/sass_fontimage/font_image.rb

Instance Method Summary collapse

Constructor Details

#initialize(font, options = {}) ⇒ FontImage

Returns a new instance of FontImage.

Parameters:

  • font

    String Path to a font (one that RMagick can use)

  • options (defaults to: {})

    Options string

Options Hash (options):

  • size (Object)

    Integer Default font size

  • color (Object)

    String Default color

  • :write_path (Object)

    The path to write the icon to with write (default = “”)

  • :file_prefix (Object)

    Prefix to use for filename (default = icon)

  • :file_type (Object)

    Extension to use when writing file (default = png)

Raises:

  • (ArgumentError)


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, options = {})
  @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"
  }
  @options = defaults.update(options)
end

Instance Method Details

#render(char, color = @options[:color], size = @options[:size]) ⇒ Object

Renders a character on a RMagick canvas

Returns:

  • Magick::Image the image with the charactor drawn.

See Also:



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 = @options[:color], size = @options[: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

Parameters:

  • char

    String A one character string, can also be a CSS encoded value (0000)

  • color (defaults to: @options[:color])

    String An optional color, takes default value otherwise

  • size (defaults to: @options[:size])

    Integer An optional fontsize

Returns:

  • String The path of the written image



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 = @options[:color], size = @options[: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