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