Class: GlyphImager::Imager

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Imager

Returns a new instance of Imager.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/glyph_imager.rb', line 79

def initialize(opts = {})
  @options = { 
    :size => "80x80", 
    :pointsize_percentage => 100, 
    :gravity => "center",  
    :background => "none"
  }.merge(opts)
  %w[code_point font_path output_dir].each do |k|
    if @options[k.to_sym].nil?
      raise ArgumentError, "missing value for :#{k}"
    end
  end
end

Instance Method Details

#command_stringObject



112
113
114
# File 'lib/glyph_imager.rb', line 112

def command_string
  "convert -font #{@options[:font_path]} -background #{@options[:background]} -size #{@options[:size]} -gravity #{@options[:gravity]} -pointsize #{pointsize} #{label} #{output_path}"
end

#create_imageObject



116
117
118
119
# File 'lib/glyph_imager.rb', line 116

def create_image
  %x[#{command_string}]
  return self
end

#labelObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/glyph_imager.rb', line 101

def label
  case @options[:code_point]
  when "0027"
    "label:\\#{[@options[:code_point].hex].pack("U*")}"
  when "005C"
    "label:'\\#{[@options[:code_point].hex].pack("U*")}'"
  else
    "label:'#{[@options[:code_point].hex].pack("U*")}'"
  end
end

#output_pathObject



93
94
95
# File 'lib/glyph_imager.rb', line 93

def output_path
  "#{@options[:output_dir]}/#{@options[:code_point]}-#{@options[:size]}.png"
end

#pointsizeObject



97
98
99
# File 'lib/glyph_imager.rb', line 97

def pointsize
  @options[:size].split("x").last.to_i * @options[:pointsize_percentage] / 100.0
end