class SugarPNG
Constants
- Canvas
- DEFAULT_BG
- DEFAULT_FG
Public Class Methods
new(h={}) { |self| ... }
click to toggle source
# File lib/sugar_png.rb, line 24 def initialize h={}, &block @bg = DEFAULT_BG @fg = DEFAULT_FG @zoom = 1 clear if block_given? if block.arity == 1 yield self else instance_eval &block end end # boring non-magic hash h.each do |k,v| self.send "#{k}=", v end end
Public Instance Methods
[]=(ax, ay, color)
click to toggle source
set pixels accepted coordinate values: a) boring Integers b) neat Arrays c) long Ranges d) super Enumerators
accepted color values: see SugarPNG::Color
# File lib/sugar_png.rb, line 58 def []= ax, ay, color Array(ay).each do |y| Array(ax).each do |x| @pixels[y][x] = color end end end
border(size, color = nil)
click to toggle source
draw image border with specified color
# File lib/sugar_png.rb, line 82 def border size, color = nil color ||= @fg || @bg @borders << Border.new( size.is_a?(Hash) ? size : {:size => size, :color => color} ) end
clear()
click to toggle source
reset all drawings
# File lib/sugar_png.rb, line 45 def clear @pixels = Hash.new{ |k,v| k[v] = {} } @borders = [] end
draw_glyph(glyph, x0, y, color)
click to toggle source
draw a single glyph, used from within text()
# File lib/sugar_png.rb, line 93 def draw_glyph glyph, x0, y, color #TODO: optimize? glyph.to_a.each do |row| x = x0 row.each do |bit| self[x,y] = color if bit == 1 x += 1 end y += 1 end end
padding(size, color = @bg)
click to toggle source
same as border, but default color is background
# File lib/sugar_png.rb, line 88 def padding size, color = @bg border size, color end
pixel(ax, ay, color = @fg)
click to toggle source
same as above, but color argument can be optional
# File lib/sugar_png.rb, line 67 def pixel ax, ay, color = @fg Array(ay).each do |y| Array(ax).each do |x| @pixels[y][x] = color end end end
Also aliased as: pixels
save(fname)
click to toggle source
export PNG to file
# File lib/sugar_png.rb, line 122 def save fname File.open(fname, "wb"){ |f| f<<to_s } end
text(text, h = {})
click to toggle source
draws text, optional arguments are :color, :x, :y
# File lib/sugar_png.rb, line 106 def text text, h = {} font = @font ||= Font.new color = h[:color] || @fg y = h[:y] || 0 text.split(/[\r\n]+/).each do |line| x = h[:x] || 0 line.each_char do |c| glyph = font[c] draw_glyph glyph, x, y, color x += glyph.width end y += font.height end end
to_s()
click to toggle source
get PNG as bytestream, for saving it to file manually, or for sending via HTTP
# File lib/sugar_png.rb, line 127 def to_s height = @height || ((t=@pixels.keys.max) && t+1 ) || 0 width = @width || ((t=@pixels.values.map(&:keys).map(&:max).max) && t+1 ) || 0 xofs = yofs = 0 xmax = width-1 ymax = height-1 if @borders.any? width += @borders.map(&:width).inject(&:+) height += @borders.map(&:height).inject(&:+) xofs += @borders.map(&:left).inject(&:+) yofs += @borders.map(&:top).inject(&:+) end raise(Exception.new("invalid image height #{height}")) if height <= 0 raise(Exception.new("invalid image width #{width}")) if width <= 0 img = Image.new :width => width, :height => height, :depth => @depth img.clear(_color(@bg)) if @bg img.draw_borders(@borders.each{ |b| b.color = _color(b.color)} ) @pixels.each do |y, xh| next if y>ymax xh.each do |x, c| next if x>xmax img[x+xofs,y+yofs] = _color(c) end end img.zoom(@zoom).export end
Also aliased as: export
Private Instance Methods
_color(c)
click to toggle source
create color from any of the supported color representations
# File lib/sugar_png.rb, line 164 def _color c c = c.is_a?(ZPNG::Color) ? c : Color.new(c, :depth => @depth) end