prep 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ # Rectangle クラスのソースファイル
2
+ # Author:: maki-tetsu
3
+ # Date:: 2011/03/11
4
+ # Copyright:: Copyright (c) 2011 maki-tetsu
5
+
6
+ require File.join(File.dirname(__FILE__), "drawable")
7
+ require File.join(File.dirname(__FILE__), "color")
8
+ require File.join(File.dirname(__FILE__), "region")
9
+ require File.join(File.dirname(__FILE__), "..", "mm2pixcel")
10
+
11
+ module PREP # nodoc
12
+ module Core # nodoc
13
+ # 矩形描画構成要素クラス
14
+ class Rectangle < Drawable
15
+ STYLES = {
16
+ :solid => "solid",
17
+ }
18
+
19
+ FILL_PATTERNS = {
20
+ :flat => "flat",
21
+ }
22
+
23
+ @@default_values = {
24
+ :line_color => { :red => 0, :green => 0, :blue => 0 },
25
+ :line_width => 1,
26
+ :line_style => STYLES[:solid],
27
+ :fill_pattern => FILL_PATTERNS[:flat],
28
+ :fill_color => { :red => 1, :green => 1, :blue => 1 },
29
+ :layer => 1,
30
+ :expand => false,
31
+ }
32
+
33
+ attr_reader :region, :line_color, :line_width, :line_style, :fill_pattern, :fill_color, :expand
34
+
35
+ def initialize(identifier, values = { })
36
+ values = @@default_values.merge(key_string_to_symbol(values))
37
+ super(identifier, values[:layer])
38
+
39
+ @region = Region.new(values[:region][:x].mm2pixcel,
40
+ values[:region][:y].mm2pixcel,
41
+ values[:region][:width].mm2pixcel,
42
+ values[:region][:height].mm2pixcel)
43
+ @line_color = Color.new(values[:line_color][:red],
44
+ values[:line_color][:green],
45
+ values[:line_color][:blue])
46
+ self.line_width = values[:line_width]
47
+ self.line_style = values[:line_style]
48
+ self.fill_pattern = values[:fill_pattern]
49
+ @fill_color = Color.new(values[:fill_color][:red],
50
+ values[:fill_color][:green],
51
+ values[:fill_color][:blue])
52
+ @expand = values[:expand]
53
+ end
54
+
55
+ def expand_region(setting)
56
+ @expand_region = @region.dup
57
+ @expand_region.width = setting[:width] if setting[:width]
58
+ @expand_region.height = setting[:height] if setting[:height]
59
+ end
60
+
61
+ def line_width=(w)
62
+ if w > 0
63
+ @line_width = w
64
+ else
65
+ raise "Rectangle line width must be grater than zero."
66
+ end
67
+ end
68
+
69
+ def line_style=(s)
70
+ if STYLES.values.include?(s)
71
+ @line_style = s
72
+ else
73
+ raise "Rectangle line style \"#{s}\" is unknown."
74
+ end
75
+ end
76
+
77
+ def fill_pattern=(fp)
78
+ if FILL_PATTERNS.values.include?(fp)
79
+ @fill_pattern = fp
80
+ else
81
+ raise "Rectangle fill pattern \"#{fp}\" is unknown."
82
+ end
83
+ end
84
+
85
+ def calculate_region(prep, region, value, stop_on_drawable = nil)
86
+ if self === stop_on_drawable
87
+ raise ReRenderJump.new(region)
88
+ end
89
+ puts "Calculate region for #{self.class}: #{self.identifier} region: #{region}" if ENV["DEBUG"]
90
+ ret_region = Region.new(0, 0,
91
+ region.width - (@region.x + @region.width),
92
+ region.height - (@region.y + @region.height))
93
+ return @region.x + @region.width, @region.y + @region.height
94
+ end
95
+
96
+ # 矩形の描画
97
+ def draw(prep, region, values, stop_on_drawable = nil)
98
+ if self === stop_on_drawable
99
+ raise ReRenderJump.new(region)
100
+ end
101
+ STDERR.puts("Draw on #{self.class} #{self.identifier}") if ENV['DEBUG']
102
+ # 領域判定
103
+ calculate_region(prep, region, values)
104
+ prep.current_page.set_line_width(@line_width.to_f)
105
+ unless @line_color.white?
106
+ prep.current_page.set_rgb_stroke(@line_color.red.to_f,
107
+ @line_color.green.to_f,
108
+ @line_color.blue.to_f)
109
+ end
110
+ unless @fill_color.white?
111
+ prep.current_page.set_rgb_fill(@fill_color.red.to_f,
112
+ @fill_color.green.to_f,
113
+ @fill_color.blue.to_f)
114
+ end
115
+ region_backup = @region.dup
116
+ if @expand_region
117
+ @region = @expand_region.dup
118
+ @expand_region = nil
119
+ end
120
+ pos_x, pos_y = calculate_pos(prep.current_page, region, @region.x, @region.y)
121
+ prep.current_page.rectangle(pos_x, pos_y - @region.height, @region.width, @region.height)
122
+
123
+ if @fill_color.white?
124
+ unless @line_color.white?
125
+ prep.current_page.stroke
126
+ end
127
+ elsif @line_color.white?
128
+ prep.current_page.fill
129
+ else
130
+ prep.current_page.fill_stroke
131
+ end
132
+ @region = region_backup
133
+ prep.current_page.drawed = true
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,54 @@
1
+ # Region クラスのソースファイル
2
+ # Author:: maki-tetsu
3
+ # Date:: 2011/03/11
4
+ # Copyright:: Copyright (c) 2011 maki-tetsu
5
+
6
+ module PREP # nodoc
7
+ module Core # nodoc
8
+
9
+ # リージョンの幅がマイナスになった場合に発行される例外
10
+ class RegionWidthOverflowError < StandardError; end
11
+ # リージョンの高さがマイナスになった場合に発行される例外
12
+ class RegionHeightOverflowError < StandardError; end
13
+
14
+ class Region
15
+ attr_reader :x, :y, :width, :height
16
+
17
+ def initialize(x, y, width, height)
18
+ self.x = x
19
+ self.y = y
20
+
21
+ self.width = width
22
+ self.height = height
23
+ end
24
+
25
+ def x=(x)
26
+ @x = x
27
+ end
28
+
29
+ def y=(y)
30
+ @y = y
31
+ end
32
+
33
+ def width=(width)
34
+ if width >= 0
35
+ @width = width
36
+ else
37
+ raise RegionWidthOverflowError.new("Region width must be grater than zero.")
38
+ end
39
+ end
40
+
41
+ def height=(height)
42
+ if height >= 0
43
+ @height = height
44
+ else
45
+ raise RegionHeightOverflowError.new("Region height must be grater than zero.")
46
+ end
47
+ end
48
+
49
+ def to_s
50
+ "[x=#{x},y=#{y},w=#{width},h=#{height}]"
51
+ end
52
+ end
53
+ end
54
+ end
data/lib/mm2pixcel.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Numeric
2
+ def mm2pixcel(dpi = 72, inchi_in_mm = 25.4)
3
+ return (self.to_f / inchi_in_mm) * dpi
4
+ end
5
+ end
data/lib/prep.rb ADDED
@@ -0,0 +1,175 @@
1
+ require "rubygems"
2
+ gem "hpdf"
3
+ require "hpdf"
4
+ require File.join(File.dirname(__FILE__), "core", "prep")
5
+
6
+ # require "rubygems"
7
+ # gem "hpdf"
8
+ # require "hpdf"
9
+ # require "yaml"
10
+ # require "nkf"
11
+
12
+ # module PREP
13
+ # class Report
14
+ # def initialize(config_file_path)
15
+ # @config = YAML.load_file(config_file_path)
16
+
17
+ # raise "Configuration Error" unless valid_configuration?
18
+ # end
19
+
20
+ # def generate(file_path, values)
21
+ # @pdf = HPDFDoc.new
22
+ # @pdf.use_jp_fonts
23
+ # @pdf.use_jp_encodings
24
+ # page = @pdf.add_page
25
+ # page.set_size(HPDFDoc::HPDF_PAGE_SIZE_A4, HPDFDoc::HPDF_PAGE_PORTRAIT)
26
+
27
+ # draw_rectangles(page)
28
+ # draw_lines(page)
29
+ # draw_labels(page, values)
30
+
31
+ # @pdf.save_to_file(file_path)
32
+ # ensure
33
+ # @pdf = nil
34
+ # end
35
+
36
+ # def self.generate_guide(file_path)
37
+ # pdf = HPDFDoc.new
38
+ # pdf.use_jp_fonts
39
+ # pdf.use_jp_encodings
40
+ # page = pdf.add_page
41
+ # page.set_size(HPDFDoc::HPDF_PAGE_SIZE_A4, HPDFDoc::HPDF_PAGE_PORTRAIT)
42
+
43
+ # # 10 ピクセルごとに方眼
44
+ # height = page.get_height
45
+ # width = page.get_width
46
+ # x = 0; y = 0
47
+ # guide_label_pos = 50
48
+ # page.set_font_and_size(pdf.get_font("Helvetica", nil), 5)
49
+ # step = 5
50
+ # while x < width
51
+ # x += step
52
+ # page.set_line_width(1)
53
+ # if x % 50 == 0
54
+ # if x % 100 == 0
55
+ # page.set_rgb_stroke(0.5, 0.5, 0.5)
56
+ # else
57
+ # page.set_rgb_stroke(0.8, 0.8, 0.8)
58
+ # end
59
+ # else
60
+ # page.set_rgb_stroke(0.8, 1.0, 0.8)
61
+ # end
62
+ # page.move_to(x, 0)
63
+ # page.line_to(x, height)
64
+ # page.stroke
65
+ # end
66
+ # while y < height
67
+ # y += step
68
+ # page.set_line_width(1)
69
+ # if y % 50 == 0
70
+ # if y % 100 == 0
71
+ # page.set_rgb_stroke(0.5, 0.5, 0.5)
72
+ # page.begin_text
73
+ # page.move_text_pos(guide_label_pos, y)
74
+ # page.show_text(y.to_s)
75
+ # page.end_text
76
+ # page.begin_text
77
+ # page.move_text_pos(((width.to_f / guide_label_pos).floor * guide_label_pos), y)
78
+ # page.show_text(y.to_s)
79
+ # page.end_text
80
+ # else
81
+ # page.set_rgb_stroke(0.8, 0.8, 0.8)
82
+ # end
83
+ # else
84
+ # page.set_rgb_stroke(0.8, 1.0, 0.8)
85
+ # end
86
+ # page.move_to(0, y)
87
+ # page.line_to(width, y)
88
+ # page.stroke
89
+ # end
90
+ # x = 0
91
+ # while x < width
92
+ # x += step
93
+ # if x % 50 == 0
94
+ # if x % 100 == 0
95
+ # page.begin_text
96
+ # page.move_text_pos(x, guide_label_pos)
97
+ # page.show_text(x.to_s)
98
+ # page.end_text
99
+ # page.begin_text
100
+ # page.move_text_pos(x, (height.to_f / guide_label_pos).floor * guide_label_pos)
101
+ # page.show_text(x.to_s)
102
+ # page.end_text
103
+ # end
104
+ # end
105
+ # end
106
+
107
+ # pdf.save_to_file(file_path)
108
+ # end
109
+
110
+ # private
111
+
112
+ # def draw_rectangles(page)
113
+ # @config.keys.each do |key|
114
+ # if @config[key]["type"] == "rectangle"
115
+ # rect = @config[key]
116
+ # page.set_line_width(rect["line_width"].to_f)
117
+ # page.set_rgb_stroke(rect["line_color"]["red"].to_f,
118
+ # rect["line_color"]["green"].to_f,
119
+ # rect["line_color"]["blue"].to_f)
120
+ # page.set_rgb_fill(rect["fill_color"]["red"].to_f,
121
+ # rect["fill_color"]["green"].to_f,
122
+ # rect["fill_color"]["blue"].to_f)
123
+ # page.rectangle(rect["x_pos"].to_f, rect["y_pos"].to_f,
124
+ # rect["width"].to_f, rect["height"].to_f)
125
+ # if rect["fill"]
126
+ # if rect["stroke"]
127
+ # page.fill_stroke
128
+ # else
129
+ # page.fill
130
+ # end
131
+ # elsif rect["stroke"]
132
+ # page.stroke
133
+ # end
134
+ # end
135
+ # end
136
+ # end
137
+
138
+ # def draw_lines(page)
139
+ # @config.keys.each do |key|
140
+ # if @config[key]["type"] == "line"
141
+ # line = @config[key]
142
+ # page.set_line_width(line["line_width"].to_f)
143
+ # page.set_rgb_stroke(line["line_color"]["red"].to_f,
144
+ # line["line_color"]["green"].to_f,
145
+ # line["line_color"]["blue"].to_f)
146
+ # page.move_to(line["start_x_pos"].to_f, line["start_y_pos"].to_f)
147
+ # page.line_to(line["end_x_pos"].to_f, line["end_y_pos"].to_f)
148
+ # page.stroke
149
+ # end
150
+ # end
151
+ # end
152
+
153
+ # def draw_labels(page, values)
154
+ # values.keys.each do |key|
155
+ # if @config.has_key?(key.to_s)
156
+ # label = @config[key.to_s]
157
+ # # フォントの取得
158
+ # font = @pdf.get_font(label["font"], "90ms-RKSJ-H")
159
+ # page.begin_text
160
+ # page.move_text_pos(label["x_pos"].to_f, label["y_pos"].to_f)
161
+ # page.set_font_and_size(font, label["size"].to_f)
162
+ # page.show_text(NKF.nkf("--oc=cp932", values[key]))
163
+ # page.end_text
164
+ # else
165
+ # raise ArgumentError.new("Unknown label key \"#{key.to_s}\"")
166
+ # end
167
+ # end
168
+ # end
169
+
170
+ # def valid_configuration?
171
+ # # 未実装(常に true)
172
+ # return true
173
+ # end
174
+ # end
175
+ # end
data/prep.gemspec ADDED
@@ -0,0 +1,108 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{prep}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tetsuhisa MAKINO"]
12
+ s.date = %q{2011-03-26}
13
+ s.description = %q{PREP is PDF Report generator depends on HPDF.}
14
+ s.email = %q{tim.makino@gmail.com}
15
+ s.executables = ["prep-test", "prep-helper"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/prep-helper",
30
+ "bin/prep-test",
31
+ "examples/generate_group_sample.rb",
32
+ "examples/generate_sample.rb",
33
+ "examples/generate_sample2.rb",
34
+ "examples/generate_sample3.rb",
35
+ "examples/generate_sample4.rb",
36
+ "examples/generate_sample5.rb",
37
+ "examples/generate_sample6.rb",
38
+ "examples/generate_sample_dataset.rb",
39
+ "examples/group_sample.yml",
40
+ "examples/sample.yml",
41
+ "examples/sample2.yml",
42
+ "examples/sample3.yml",
43
+ "examples/sample5.yml",
44
+ "examples/sample6.yml",
45
+ "lib/core/color.rb",
46
+ "lib/core/drawable.rb",
47
+ "lib/core/group.rb",
48
+ "lib/core/label.rb",
49
+ "lib/core/line.rb",
50
+ "lib/core/loop.rb",
51
+ "lib/core/page.rb",
52
+ "lib/core/page_ext.rb",
53
+ "lib/core/point.rb",
54
+ "lib/core/prep.rb",
55
+ "lib/core/rectangle.rb",
56
+ "lib/core/region.rb",
57
+ "lib/mm2pixcel.rb",
58
+ "lib/prep.rb",
59
+ "prep.gemspec",
60
+ "spec/component_spec.rb",
61
+ "spec/prep_spec.rb",
62
+ "spec/spec_helper.rb"
63
+ ]
64
+ s.homepage = %q{http://github.com/maki-tetsu/prep}
65
+ s.licenses = ["MIT"]
66
+ s.require_paths = ["lib"]
67
+ s.rubygems_version = %q{1.3.7}
68
+ s.summary = %q{PREP is PDF Report generator depends on HPDF.}
69
+ s.test_files = [
70
+ "examples/generate_group_sample.rb",
71
+ "examples/generate_sample.rb",
72
+ "examples/generate_sample2.rb",
73
+ "examples/generate_sample3.rb",
74
+ "examples/generate_sample4.rb",
75
+ "examples/generate_sample5.rb",
76
+ "examples/generate_sample6.rb",
77
+ "examples/generate_sample_dataset.rb",
78
+ "spec/component_spec.rb",
79
+ "spec/prep_spec.rb",
80
+ "spec/spec_helper.rb"
81
+ ]
82
+
83
+ if s.respond_to? :specification_version then
84
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
85
+ s.specification_version = 3
86
+
87
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
88
+ s.add_runtime_dependency(%q<hpdf>, [">= 2.0.8"])
89
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
90
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
91
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
92
+ s.add_development_dependency(%q<rcov>, [">= 0"])
93
+ else
94
+ s.add_dependency(%q<hpdf>, [">= 2.0.8"])
95
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
96
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
97
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
98
+ s.add_dependency(%q<rcov>, [">= 0"])
99
+ end
100
+ else
101
+ s.add_dependency(%q<hpdf>, [">= 2.0.8"])
102
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
103
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
104
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
105
+ s.add_dependency(%q<rcov>, [">= 0"])
106
+ end
107
+ end
108
+