2009-12-30 18:38:41 +00:00
|
|
|
require 'zlib'
|
2008-08-23 17:00:46 +00:00
|
|
|
|
2009-04-03 03:20:29 +00:00
|
|
|
module Compass
|
|
|
|
|
2009-12-30 18:38:41 +00:00
|
|
|
# A simple class to represent and create a PNG-File
|
|
|
|
# No drawing features given
|
|
|
|
# Just subclass and write [R,G,B]-Byte-Values into the <tt>@data</tt> matrix
|
|
|
|
# Build for compactness, so not much error checking!
|
|
|
|
#
|
|
|
|
# Code based on seattlerb's png, see http://seattlerb.rubyforge.org/png/
|
|
|
|
class PNG
|
|
|
|
CRC_TABLE = (0..255).map do |n|
|
|
|
|
8.times.inject(n){|x,i| x = ((x & 1) == 1) ? 0xedb88320 ^ (x >> 1) : x >> 1}
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def crc(chunkdata='')
|
2010-01-04 21:40:37 +00:00
|
|
|
chunkdata.unpack('C*').to_a.inject(0xffffffff){|crc, byte| CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >> 8) } ^ 0xffffffff
|
2009-12-30 18:38:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def chunk(type, data="")
|
|
|
|
[data.size, type, data, crc(type + data)].pack("Na*a*N")
|
|
|
|
end
|
2008-08-23 17:00:46 +00:00
|
|
|
end
|
|
|
|
|
2009-12-30 18:38:41 +00:00
|
|
|
# Initiates a new PNG-Object
|
|
|
|
# * <tt>width</tt>: Width of the image in pixels
|
|
|
|
# * <tt>height</tt>: Height of the image in pixels
|
|
|
|
# * <tt>background</tt>: Background-color represented as [R,G,B]-Byte-Array
|
|
|
|
def initialize(width, height, background = [255,255,255])
|
|
|
|
@height = height
|
|
|
|
@width = width
|
|
|
|
@data = Array.new(@height) { |x| Array.new(@width, background) }
|
|
|
|
end
|
|
|
|
|
|
|
|
BITS = 8
|
|
|
|
RGB = 2 # Color Types ( RGBA = 6)
|
|
|
|
NONE = 0 # Filter
|
|
|
|
|
|
|
|
# binary representation of the PNG, write to file with binary mode
|
|
|
|
def to_blob
|
|
|
|
blob = []
|
|
|
|
blob << [137, 80, 78, 71, 13, 10, 26, 10].pack("C*")
|
|
|
|
blob << PNG.chunk('IHDR', [@width, @height, BITS, RGB, NONE, NONE, NONE].pack("N2C5"))
|
|
|
|
blob << PNG.chunk('IDAT', Zlib::Deflate.deflate(self.png_join))
|
|
|
|
blob << PNG.chunk('IEND', '')
|
|
|
|
blob.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def png_join
|
|
|
|
@data.map { |row| "\0" + row.map { |p| "%c%c%c" % p}.join }.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GridBuilder < PNG
|
|
|
|
include Actions
|
|
|
|
|
2009-10-05 07:56:49 +00:00
|
|
|
attr_reader :column_width, :gutter_width, :filename, :able_to_generate, :options
|
2008-08-23 17:00:46 +00:00
|
|
|
|
|
|
|
# ==== Options
|
|
|
|
# * <tt>options</tt>
|
|
|
|
# * <tt>:column_width</tt> -- Width (in pixels) of current grid column
|
|
|
|
# * <tt>:gutter_width</tt> -- Width (in pixels) of current grid gutter
|
2009-09-02 03:20:53 +00:00
|
|
|
# * <tt>:height</tt> -- Height (in pixels) of a row
|
2009-10-05 07:56:49 +00:00
|
|
|
# * <tt>:filename</tt> -- Output path of grid.png file
|
2008-08-23 17:00:46 +00:00
|
|
|
def initialize(options={})
|
2009-04-03 03:20:29 +00:00
|
|
|
@column_width = options[:column_width]
|
2009-12-30 18:38:41 +00:00
|
|
|
gutter_width = options[:gutter_width]
|
|
|
|
|
|
|
|
height = options[:height] || 20
|
2009-10-05 07:56:49 +00:00
|
|
|
@filename = options[:filename]
|
2009-04-03 03:20:29 +00:00
|
|
|
@options = options
|
2009-12-30 18:38:41 +00:00
|
|
|
|
|
|
|
super(@column_width + gutter_width, height, [0xe9,0xe9,0xe9])
|
2009-04-03 03:20:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def working_path
|
|
|
|
options[:working_path]
|
2008-08-23 17:00:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# generates (overwriting if necessary) grid.png image to be tiled in background
|
|
|
|
def generate!
|
2009-12-30 18:38:41 +00:00
|
|
|
(0...@height-1).each do |line|
|
|
|
|
@data[line] = Array.new(@width){|x| x < @column_width ? [0xe8, 0xef, 0xfb] : [0xff,0xff,0xff] }
|
2008-08-23 17:00:46 +00:00
|
|
|
end
|
2009-04-03 03:20:29 +00:00
|
|
|
|
|
|
|
if File.exists?(filename)
|
|
|
|
if options[:force]
|
|
|
|
overwrite = true
|
|
|
|
else
|
|
|
|
msg = "#{filename} already exists. Overwrite with --force."
|
|
|
|
raise Compass::FilesystemConflict.new(msg)
|
|
|
|
end
|
|
|
|
end
|
2009-10-05 07:56:49 +00:00
|
|
|
directory File.dirname(filename)
|
2009-04-03 03:20:29 +00:00
|
|
|
logger.record((overwrite ? :overwrite : :create), basename(filename))
|
2009-12-30 18:38:41 +00:00
|
|
|
unless options[:dry_run]
|
|
|
|
write_file(filename, self.to_blob)
|
2009-10-05 07:56:49 +00:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2008-08-23 17:00:46 +00:00
|
|
|
end
|
|
|
|
end
|
2009-10-05 07:56:49 +00:00
|
|
|
end
|