File: grid_builder.rb

package info (click to toggle)
ruby-compass 0.12.2~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,308 kB
  • sloc: ruby: 10,474; makefile: 42; xml: 14
file content (102 lines) | stat: -rw-r--r-- 3,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require 'zlib'

module Compass

  # 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|
      (0...8).inject(n){|x,i| x = ((x & 1) == 1) ? 0xedb88320 ^ (x >> 1) : x >> 1}
    end

    class << self
      def crc(chunkdata='')
        chunkdata.unpack('C*').inject(0xffffffff){|crc, byte| CRC_TABLE[(crc ^ byte)  & 0xff] ^ (crc >> 8) } ^  0xffffffff
      end

      def chunk(type, data="")
        [data.size, type, data, crc(type + data)].pack("Na*a*N")
      end
    end

    # 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

    attr_reader :column_width, :gutter_width, :filename, :able_to_generate, :options

    # ==== 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
    #   * <tt>:height</tt> -- Height (in pixels) of a row
    #   * <tt>:filename</tt> -- Output path of grid.png file
    def initialize(options={})
      @column_width = options[:column_width] || 0
      gutter_width = options[:gutter_width] || 0

      height = options[:height] || 20
      width = @column_width + gutter_width
      width = 10 if width == 0

      @filename = options[:filename]
      @options = options

      super(width, height, [0xe9,0xe9,0xe9])
    end

    def working_path
      options[:working_path]
    end

    # generates (overwriting if necessary) grid.png image to be tiled in background
    def generate!
      (0...@height-1).each do |line|
        @data[line] = Array.new(@width){|x| x < @column_width ? [0xe8, 0xef, 0xfb] : [0xff,0xff,0xff] }
      end

      if File.exists?(filename)
        if options[:force]
          overwrite = true
        else
          msg = "#{filename} already exists. Overwrite with --force."
          raise Compass::FilesystemConflict.new(msg)
        end
      end
      directory File.dirname(filename)
      write_file(filename, self.to_blob, options, true)
    end
  end
end