annotate vendor/plugins/rfpdf/lib/core/rfpdf.rb @ 507:0c939c159af4 redmine-1.2

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