comparison vendor/plugins/rfpdf/lib/core/rfpdf.rb @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents
children
comparison
equal deleted inserted replaced
245:051f544170fe 441:cbce1fd3b1b7
1 module Core::RFPDF
2 COLOR_PALETTE = {
3 :black => [0x00, 0x00, 0x00],
4 :white => [0xff, 0xff, 0xff],
5 }.freeze
6
7 # Draw a circle at (<tt>mid_x, mid_y</tt>) with <tt>radius</tt>.
8 #
9 # Options are:
10 # * <tt>:border</tt> - Draw a border, 0 = no, 1 = yes? Default value is <tt>1</tt>.
11 # * <tt>:border_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
12 # * <tt>:border_width</tt> - Default value is <tt>0.5</tt>.
13 # * <tt>:fill</tt> - Fill the box, 0 = no, 1 = yes? Default value is <tt>1</tt>.
14 # * <tt>:fill_color</tt> - Default value is nothing or <tt>COLOR_PALETTE[:white]</tt>.
15 # * <tt>:fill_colorspace</tt> - Default value is :rgb or <tt>''</tt>.
16 #
17 # Example:
18 #
19 # draw_circle(x, y, radius, :border_color => ReportHelper::COLOR_PALETTE[:dark_blue], :border_width => 1)
20 #
21 def draw_circle(mid_x, mid_y, radius, options = {})
22 options[:border] ||= 1
23 options[:border_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
24 options[:border_width] ||= 0.5
25 options[:fill] ||= 1
26 options[:fill_color] ||= Core::RFPDF::COLOR_PALETTE[:white]
27 options[:fill_colorspace] ||= :rgb
28 SetLineWidth(options[:border_width])
29 set_draw_color_a(options[:border_color])
30 set_fill_color_a(options[:fill_color], options[:colorspace])
31 fd = ""
32 fd = "D" if options[:border] == 1
33 fd += "F" if options[:fill] == 1
34 Circle(mid_x, mid_y, radius, fd)
35 end
36
37 # Draw a line from (<tt>x1, y1</tt>) to (<tt>x2, y2</tt>).
38 #
39 # Options are:
40 # * <tt>:line_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
41 # * <tt>:line_width</tt> - Default value is <tt>0.5</tt>.
42 #
43 # Example:
44 #
45 # draw_line(x1, y1, x1, y1+h, :line_color => ReportHelper::COLOR_PALETTE[:dark_blue], :line_width => 1)
46 #
47 def draw_line(x1, y1, x2, y2, options = {})
48 options[:line_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
49 options[:line_width] ||= 0.5
50 set_draw_color_a(options[:line_color])
51 SetLineWidth(options[:line_width])
52 Line(x1, y1, x2, y2)
53 end
54
55 # Draw a string of <tt>text</tt> at (<tt>x, y</tt>).
56 #
57 # Options are:
58 # * <tt>:font_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
59 # * <tt>:font_size</tt> - Default value is <tt>10</tt>.
60 # * <tt>:font_style</tt> - Default value is nothing or <tt>''</tt>.
61 # * <tt>:colorspace</tt> - Default value is :rgb or <tt>''</tt>.
62 #
63 # Example:
64 #
65 # draw_text(x, y, header_left, :font_size => 10)
66 #
67 def draw_text(x, y, text, options = {})
68 options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
69 options[:font] ||= default_font
70 options[:font_size] ||= 10
71 options[:font_style] ||= ''
72 set_text_color_a(options[:font_color], options[:colorspace])
73 SetFont(options[:font], options[:font_style], options[:font_size])
74 SetXY(x, y)
75 Write(options[:font_size] + 4, text)
76 end
77
78 # Draw a block of <tt>text</tt> at (<tt>x, y</tt>) bounded by <tt>left_margin</tt> and <tt>right_margin_from_right_edge</tt>. Both
79 # margins are measured from their corresponding edge.
80 #
81 # Options are:
82 # * <tt>:font_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
83 # * <tt>:font_size</tt> - Default value is <tt>10</tt>.
84 # * <tt>:font_style</tt> - Default value is nothing or <tt>''</tt>.
85 # * <tt>:colorspace</tt> - Default value is :rgb or <tt>''</tt>.
86 #
87 # Example:
88 #
89 # draw_text_block(left_margin, 85, "question", left_margin, 280,
90 # :font_color => ReportHelper::COLOR_PALETTE[:dark_blue],
91 # :font_size => 12,
92 # :font_style => 'I')
93 #
94 def draw_text_block(x, y, text, left_margin, right_margin_from_right_edge, options = {})
95 options[:font] ||= default_font
96 options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
97 options[:font_size] ||= 10
98 options[:font_style] ||= ''
99 set_text_color_a(options[:font_color], options[:colorspace])
100 SetFont(options[:font], options[:font_style], options[:font_size])
101 SetXY(x, y)
102 SetLeftMargin(left_margin)
103 SetRightMargin(right_margin_from_right_edge)
104 Write(options[:font_size] + 4, text)
105 SetMargins(0,0,0)
106 end
107
108 # Draw a box at (<tt>x, y</tt>), <tt>w</tt> wide and <tt>h</tt> high.
109 #
110 # Options are:
111 # * <tt>:border</tt> - Draw a border, 0 = no, 1 = yes? Default value is <tt>1</tt>.
112 # * <tt>:border_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
113 # * <tt>:border_width</tt> - Default value is <tt>0.5</tt>.
114 # * <tt>:fill</tt> - Fill the box, 0 = no, 1 = yes? Default value is <tt>1</tt>.
115 # * <tt>:fill_color</tt> - Default value is nothing or <tt>COLOR_PALETTE[:white]</tt>.
116 # * <tt>:fill_colorspace</tt> - Default value is :rgb or <tt>''</tt>.
117 #
118 # Example:
119 #
120 # draw_box(x, y - 1, 38, 22)
121 #
122 def draw_box(x, y, w, h, options = {})
123 options[:border] ||= 1
124 options[:border_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
125 options[:border_width] ||= 0.5
126 options[:fill] ||= 1
127 options[:fill_color] ||= Core::RFPDF::COLOR_PALETTE[:white]
128 options[:fill_colorspace] ||= :rgb
129 SetLineWidth(options[:border_width])
130 set_draw_color_a(options[:border_color])
131 set_fill_color_a(options[:fill_color], options[:fill_colorspace])
132 fd = ""
133 fd = "D" if options[:border] == 1
134 fd += "F" if options[:fill] == 1
135 Rect(x, y, w, h, fd)
136 end
137
138 # Draw a string of <tt>text</tt> at (<tt>x, y</tt>) in a box <tt>w</tt> wide and <tt>h</tt> high.
139 #
140 # Options are:
141 # * <tt>:align</tt> - Vertical alignment 'C' = center, 'L' = left, 'R' = right. Default value is <tt>'C'</tt>.
142 # * <tt>:border</tt> - Draw a border, 0 = no, 1 = yes? Default value is <tt>0</tt>.
143 # * <tt>:border_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
144 # * <tt>:border_width</tt> - Default value is <tt>0.5</tt>.
145 # * <tt>:fill</tt> - Fill the box, 0 = no, 1 = yes? Default value is <tt>1</tt>.
146 # * <tt>:fill_color</tt> - Default value is nothing or <tt>COLOR_PALETTE[:white]</tt>.
147 # * <tt>:font_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
148 # * <tt>:font_size</tt> - Default value is nothing or <tt>8</tt>.
149 # * <tt>:font_style</tt> - 'B' = bold, 'I' = italic, 'U' = underline. Default value is nothing <tt>''</tt>.
150 # * <tt>:padding</tt> - Default value is nothing or <tt>2</tt>.
151 # * <tt>:x_padding</tt> - Default value is nothing.
152 # * <tt>:valign</tt> - 'M' = middle, 'T' = top, 'B' = bottom. Default value is nothing or <tt>'M'</tt>.
153 # * <tt>:colorspace</tt> - Default value is :rgb or <tt>''</tt>.
154 #
155 # Example:
156 #
157 # draw_text_box(x, y - 1, 38, 22,
158 # "your_score_title",
159 # :fill => 0,
160 # :font_color => ReportHelper::COLOR_PALETTE[:blue],
161 # :font_line_spacing => 0,
162 # :font_style => "B",
163 # :valign => "M")
164 #
165 def draw_text_box(x, y, w, h, text, options = {})
166 options[:align] ||= 'C'
167 options[:border] ||= 0
168 options[:border_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
169 options[:border_width] ||= 0.5
170 options[:fill] ||= 1
171 options[:fill_color] ||= Core::RFPDF::COLOR_PALETTE[:white]
172 options[:font] ||= default_font
173 options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
174 options[:font_size] ||= 8
175 options[:font_line_spacing] ||= options[:font_size] * 0.3
176 options[:font_style] ||= ''
177 options[:padding] ||= 2
178 options[:x_padding] ||= 0
179 options[:valign] ||= "M"
180 if options[:fill] == 1 or options[:border] == 1
181 draw_box(x, y, w, h, options)
182 end
183 SetMargins(0,0,0)
184 set_text_color_a(options[:font_color], options[:colorspace])
185 font_size = options[:font_size]
186 SetFont(options[:font], options[:font_style], font_size)
187 font_size += options[:font_line_spacing]
188 case options[:valign]
189 when "B", "bottom"
190 y -= options[:padding]
191 when "T", "top"
192 y += options[:padding]
193 end
194 case options[:align]
195 when "L", "left"
196 x += options[:x_padding]
197 w -= options[:x_padding]
198 w -= options[:x_padding]
199 when "R", "right"
200 x += options[:x_padding]
201 w -= options[:x_padding]
202 w -= options[:x_padding]
203 end
204 SetXY(x, y)
205 if GetStringWidth(text) < w or not text["\n"].nil? and (options[:valign] == "T" || options[:valign] == "top")
206 text = text + "\n"
207 end
208 if GetStringWidth(text) > w or not text["\n"].nil? or (options[:valign] == "B" || options[:valign] == "bottom")
209 font_size += options[:font_size] * 0.1
210 # TODO 2006-07-21 Level=1 - this is assuming a 2 line text
211 SetXY(x, y + ((h - (font_size * 2)) / 2)) if (options[:valign] == "M" || options[:valign] == "middle")
212 MultiCell(w, font_size, text, 0, options[:align])
213 else
214 Cell(w, h, text, 0, 0, options[:align])
215 end
216 end
217
218 # Draw a string of <tt>text</tt> at (<tt>x, y</tt>) as a title.
219 #
220 # Options are:
221 # * <tt>:font_color</tt> - Default value is <tt>COLOR_PALETTE[:black]</tt>.
222 # * <tt>:font_size</tt> - Default value is <tt>18</tt>.
223 # * <tt>:font_style</tt> - Default value is nothing or <tt>''</tt>.
224 # * <tt>:colorspace</tt> - Default value is :rgb or <tt>''</tt>.
225 #
226 # Example:
227 #
228 # draw_title(left_margin, 60,
229 # "title:",
230 # :font_color => ReportHelper::COLOR_PALETTE[:dark_blue])
231 #
232 def draw_title(x, y, title, options = {})
233 options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black]
234 options[:font] ||= default_font
235 options[:font_size] ||= 18
236 options[:font_style] ||= ''
237 set_text_color_a(options[:font_color], options[:colorspace])
238 SetFont(options[:font], options[:font_style], options[:font_size])
239 SetXY(x, y)
240 Write(options[:font_size] + 2, title)
241 end
242
243 # Set the draw color. Default value is <tt>COLOR_PALETTE[:black]</tt>.
244 #
245 # Example:
246 #
247 # set_draw_color_a(ReportHelper::COLOR_PALETTE[:dark_blue])
248 #
249 def set_draw_color_a(color = Core::RFPDF::COLOR_PALETTE[:black])
250 SetDrawColor(color[0], color[1], color[2])
251 end
252
253 # Set the fill color. Default value is <tt>COLOR_PALETTE[:white]</tt>.
254 #
255 # Example:
256 #
257 # set_fill_color_a(ReportHelper::COLOR_PALETTE[:dark_blue])
258 #
259 def set_fill_color_a(color = Core::RFPDF::COLOR_PALETTE[:white], colorspace = :rgb)
260 if colorspace == :cmyk
261 SetCmykFillColor(color[0], color[1], color[2], color[3])
262 else
263 SetFillColor(color[0], color[1], color[2])
264 end
265 end
266
267 # Set the text color. Default value is <tt>COLOR_PALETTE[:white]</tt>.
268 #
269 # Example:
270 #
271 # set_text_color_a(ReportHelper::COLOR_PALETTE[:dark_blue])
272 #
273 def set_text_color_a(color = Core::RFPDF::COLOR_PALETTE[:black], colorspace = :rgb)
274 if colorspace == :cmyk
275 SetCmykTextColor(color[0], color[1], color[2], color[3])
276 else
277 SetTextColor(color[0], color[1], color[2])
278 end
279 end
280
281 # Write a string containing html characters. Default value is <tt>COLOR_PALETTE[:white]</tt>.
282 #
283 # Options are:
284 # * <tt>:height</tt> - Line height. Default value is <tt>20</tt>.
285 #
286 # Example:
287 #
288 # write_html_with_options(html, :height => 12)
289 #
290 #FIXME 2007-08-07 (EJM) Level=0 - This needs to call the TCPDF version.
291 def write_html_with_options(html, options = {})
292 options[:fill] ||= 0
293 options[:height] ||= 20
294 options[:new_line_after] ||= false
295 write_html(html, options[:new_line_after], options[:fill], options[:height])
296 return
297 end
298 end