1 | begin |
---|
2 | require "rubygems" |
---|
3 | gem "rmagick" |
---|
4 | require "rvg/rvg" |
---|
5 | rescue Exception => e |
---|
6 | puts " #{"*" * 100}" |
---|
7 | puts " **" |
---|
8 | puts " ** Warning:" |
---|
9 | puts " ** Could not load the Rmagick gem. Please check your installation." |
---|
10 | puts " ** grid.png will not be generated." |
---|
11 | puts " **" |
---|
12 | puts " #{"*" * 100}\n" |
---|
13 | end |
---|
14 | |
---|
15 | module Blueprint |
---|
16 | # Uses ImageMagick and RMagick to generate grid.png file |
---|
17 | class GridBuilder |
---|
18 | begin |
---|
19 | include Magick |
---|
20 | rescue Exception => e |
---|
21 | end |
---|
22 | |
---|
23 | attr_reader :column_width, :gutter_width, :output_path, :able_to_generate |
---|
24 | |
---|
25 | # ==== Options |
---|
26 | # * <tt>options</tt> |
---|
27 | # * <tt>:column_width</tt> -- Width (in pixels) of current grid column |
---|
28 | # * <tt>:gutter_width</tt> -- Width (in pixels) of current grid gutter |
---|
29 | # * <tt>:output_path</tt> -- Output path of grid.png file |
---|
30 | def initialize(options={}) |
---|
31 | @able_to_generate = Magick::Long_version rescue false |
---|
32 | return unless @able_to_generate |
---|
33 | @column_width = options[:column_width] || Blueprint::COLUMN_WIDTH |
---|
34 | @gutter_width = options[:gutter_width] || Blueprint::GUTTER_WIDTH |
---|
35 | @output_path = options[:output_path] || Blueprint::SOURCE_PATH |
---|
36 | end |
---|
37 | |
---|
38 | # generates (overwriting if necessary) grid.png image to be tiled in background |
---|
39 | def generate! |
---|
40 | return false unless self.able_to_generate |
---|
41 | total_width = self.column_width + self.gutter_width |
---|
42 | height = 18 |
---|
43 | RVG::dpi = 100 |
---|
44 | |
---|
45 | width_in_inches = (total_width.to_f/RVG::dpi).in |
---|
46 | height_in_inches = (height.to_f/RVG::dpi).in |
---|
47 | rvg = RVG.new(width_in_inches, height_in_inches).viewbox(0, 0, total_width, height) do |canvas| |
---|
48 | canvas.background_fill = "white" |
---|
49 | |
---|
50 | canvas.g do |column| |
---|
51 | column.rect(self.column_width - 1, height).styles(:fill => "#e8effb") |
---|
52 | end |
---|
53 | |
---|
54 | canvas.g do |baseline| |
---|
55 | baseline.line(0, (height - 1), total_width, (height - 1)).styles(:fill => "#e9e9e9") |
---|
56 | end |
---|
57 | end |
---|
58 | |
---|
59 | FileUtils.mkdir self.output_path unless File.exists? self.output_path |
---|
60 | rvg.draw.write(File.join(self.output_path, "grid.png")) |
---|
61 | end |
---|
62 | end |
---|
63 | end |
---|