class Rpictogrify::Pictogram
Constants
- VERSION
BUMP UP if avatar algorithm changes
Attributes
options[R]
text[R]
theme[R]
uid[R]
Public Class Methods
base_path()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 62 def base_path File.join (Rpictogrify.config.base_path || 'public/system'), 'rpictogrify', "#{VERSION}" end
new(text, options = {})
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 12 def initialize(text, options = {}) @text = text.to_s @options = options @theme = Rpictogrify::Theme.find(options[:theme] || Rpictogrify.config.theme) @uid = generate_uid end
Public Instance Methods
base64()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 40 def base64 "data:image/svg+xml;base64,#{Base64.encode64(svg)[0...-1]}" end
generate()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 19 def generate File.write(path, svg) unless File.exist?(path) path end
path()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 48 def path @path ||= begin dir = File.join(self.class.base_path, theme.ident) FileUtils.mkdir_p(dir) File.join(dir, "#{text_hash}.svg") end end
size()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 44 def size @size ||= theme.width || 300 end
svg()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 24 def svg includes = symbols.map do |shape, symbol| fillable = fill[shape] ? "fill='#{fill[shape]}'" : '' "<svg class='#{shape}' #{fillable} xmlns='http://www.w3.org/2000/svg'>#{symbol}</svg>" end <<-XML.strip <svg viewBox="0 0 #{size} #{size}" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <rect fill="#{colors['background']}" x="0" y="0" width="#{size}" height="#{size}"></rect> #{includes.join("\n")} </g> </svg> XML end
text_hash()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 57 def text_hash @text_hash ||= XXhash.xxh32(text) end
Private Instance Methods
colors()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 80 def colors @colors ||= theme.colors.each_with_index.inject({}) do |result, ((part, values), index)| result[part] = values[uid[index].to_i] || values.first result end end
fill()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 87 def fill @fill ||= theme.shapes.inject({}) do |result, (shape, value)| color = colors[value['fill']] result[shape] = color if color result end end
generate_uid()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 69 def generate_uid text_hash.abs.to_s.gsub('0', '1') end
shapes()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 73 def shapes @shapes ||= theme.shapes.each_with_index.inject({}) do |result, ((shape, value), index)| result[shape] = uid[index].nil? || uid[index].to_i > value['length'] ? '01' : uid[index].rjust(2, '0') result end end
symbols()
click to toggle source
# File lib/rpictogrify/pictogram.rb, line 95 def symbols @symbols ||= shapes.inject({}) do |result, (shape, value)| symbol = theme.symbol("#{shape}-#{value}") result[shape] = symbol&.children.to_s.gsub('\n', '') result end end