Chris@441: module Core::RFPDF Chris@441: COLOR_PALETTE = { Chris@441: :black => [0x00, 0x00, 0x00], Chris@441: :white => [0xff, 0xff, 0xff], Chris@441: }.freeze Chris@441: Chris@441: # Draw a circle at (mid_x, mid_y) with radius. Chris@441: # Chris@441: # Options are: Chris@441: # * :border - Draw a border, 0 = no, 1 = yes? Default value is 1. Chris@441: # * :border_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :border_width - Default value is 0.5. Chris@441: # * :fill - Fill the box, 0 = no, 1 = yes? Default value is 1. Chris@441: # * :fill_color - Default value is nothing or COLOR_PALETTE[:white]. Chris@441: # * :fill_colorspace - Default value is :rgb or ''. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_circle(x, y, radius, :border_color => ReportHelper::COLOR_PALETTE[:dark_blue], :border_width => 1) Chris@441: # Chris@441: def draw_circle(mid_x, mid_y, radius, options = {}) Chris@441: options[:border] ||= 1 Chris@441: options[:border_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:border_width] ||= 0.5 Chris@441: options[:fill] ||= 1 Chris@441: options[:fill_color] ||= Core::RFPDF::COLOR_PALETTE[:white] Chris@441: options[:fill_colorspace] ||= :rgb Chris@441: SetLineWidth(options[:border_width]) Chris@441: set_draw_color_a(options[:border_color]) Chris@441: set_fill_color_a(options[:fill_color], options[:colorspace]) Chris@441: fd = "" Chris@441: fd = "D" if options[:border] == 1 Chris@441: fd += "F" if options[:fill] == 1 Chris@441: Circle(mid_x, mid_y, radius, fd) Chris@441: end Chris@441: Chris@441: # Draw a line from (x1, y1) to (x2, y2). Chris@441: # Chris@441: # Options are: Chris@441: # * :line_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :line_width - Default value is 0.5. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_line(x1, y1, x1, y1+h, :line_color => ReportHelper::COLOR_PALETTE[:dark_blue], :line_width => 1) Chris@441: # Chris@441: def draw_line(x1, y1, x2, y2, options = {}) Chris@441: options[:line_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:line_width] ||= 0.5 Chris@441: set_draw_color_a(options[:line_color]) Chris@441: SetLineWidth(options[:line_width]) Chris@441: Line(x1, y1, x2, y2) Chris@441: end Chris@441: Chris@441: # Draw a string of text at (x, y). Chris@441: # Chris@441: # Options are: Chris@441: # * :font_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :font_size - Default value is 10. Chris@441: # * :font_style - Default value is nothing or ''. Chris@441: # * :colorspace - Default value is :rgb or ''. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_text(x, y, header_left, :font_size => 10) Chris@441: # Chris@441: def draw_text(x, y, text, options = {}) Chris@441: options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:font] ||= default_font Chris@441: options[:font_size] ||= 10 Chris@441: options[:font_style] ||= '' Chris@441: set_text_color_a(options[:font_color], options[:colorspace]) Chris@441: SetFont(options[:font], options[:font_style], options[:font_size]) Chris@441: SetXY(x, y) Chris@441: Write(options[:font_size] + 4, text) Chris@441: end Chris@441: Chris@441: # Draw a block of text at (x, y) bounded by left_margin and right_margin_from_right_edge. Both Chris@441: # margins are measured from their corresponding edge. Chris@441: # Chris@441: # Options are: Chris@441: # * :font_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :font_size - Default value is 10. Chris@441: # * :font_style - Default value is nothing or ''. Chris@441: # * :colorspace - Default value is :rgb or ''. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_text_block(left_margin, 85, "question", left_margin, 280, Chris@441: # :font_color => ReportHelper::COLOR_PALETTE[:dark_blue], Chris@441: # :font_size => 12, Chris@441: # :font_style => 'I') Chris@441: # Chris@441: def draw_text_block(x, y, text, left_margin, right_margin_from_right_edge, options = {}) Chris@441: options[:font] ||= default_font Chris@441: options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:font_size] ||= 10 Chris@441: options[:font_style] ||= '' Chris@441: set_text_color_a(options[:font_color], options[:colorspace]) Chris@441: SetFont(options[:font], options[:font_style], options[:font_size]) Chris@441: SetXY(x, y) Chris@441: SetLeftMargin(left_margin) Chris@441: SetRightMargin(right_margin_from_right_edge) Chris@441: Write(options[:font_size] + 4, text) Chris@441: SetMargins(0,0,0) Chris@441: end Chris@441: Chris@441: # Draw a box at (x, y), w wide and h high. Chris@441: # Chris@441: # Options are: Chris@441: # * :border - Draw a border, 0 = no, 1 = yes? Default value is 1. Chris@441: # * :border_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :border_width - Default value is 0.5. Chris@441: # * :fill - Fill the box, 0 = no, 1 = yes? Default value is 1. Chris@441: # * :fill_color - Default value is nothing or COLOR_PALETTE[:white]. Chris@441: # * :fill_colorspace - Default value is :rgb or ''. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_box(x, y - 1, 38, 22) Chris@441: # Chris@441: def draw_box(x, y, w, h, options = {}) Chris@441: options[:border] ||= 1 Chris@441: options[:border_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:border_width] ||= 0.5 Chris@441: options[:fill] ||= 1 Chris@441: options[:fill_color] ||= Core::RFPDF::COLOR_PALETTE[:white] Chris@441: options[:fill_colorspace] ||= :rgb Chris@441: SetLineWidth(options[:border_width]) Chris@441: set_draw_color_a(options[:border_color]) Chris@441: set_fill_color_a(options[:fill_color], options[:fill_colorspace]) Chris@441: fd = "" Chris@441: fd = "D" if options[:border] == 1 Chris@441: fd += "F" if options[:fill] == 1 Chris@441: Rect(x, y, w, h, fd) Chris@441: end Chris@441: Chris@441: # Draw a string of text at (x, y) in a box w wide and h high. Chris@441: # Chris@441: # Options are: Chris@441: # * :align - Vertical alignment 'C' = center, 'L' = left, 'R' = right. Default value is 'C'. Chris@441: # * :border - Draw a border, 0 = no, 1 = yes? Default value is 0. Chris@441: # * :border_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :border_width - Default value is 0.5. Chris@441: # * :fill - Fill the box, 0 = no, 1 = yes? Default value is 1. Chris@441: # * :fill_color - Default value is nothing or COLOR_PALETTE[:white]. Chris@441: # * :font_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :font_size - Default value is nothing or 8. Chris@441: # * :font_style - 'B' = bold, 'I' = italic, 'U' = underline. Default value is nothing ''. Chris@441: # * :padding - Default value is nothing or 2. Chris@441: # * :x_padding - Default value is nothing. Chris@441: # * :valign - 'M' = middle, 'T' = top, 'B' = bottom. Default value is nothing or 'M'. Chris@441: # * :colorspace - Default value is :rgb or ''. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_text_box(x, y - 1, 38, 22, Chris@441: # "your_score_title", Chris@441: # :fill => 0, Chris@441: # :font_color => ReportHelper::COLOR_PALETTE[:blue], Chris@441: # :font_line_spacing => 0, Chris@441: # :font_style => "B", Chris@441: # :valign => "M") Chris@441: # Chris@441: def draw_text_box(x, y, w, h, text, options = {}) Chris@441: options[:align] ||= 'C' Chris@441: options[:border] ||= 0 Chris@441: options[:border_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:border_width] ||= 0.5 Chris@441: options[:fill] ||= 1 Chris@441: options[:fill_color] ||= Core::RFPDF::COLOR_PALETTE[:white] Chris@441: options[:font] ||= default_font Chris@441: options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:font_size] ||= 8 Chris@441: options[:font_line_spacing] ||= options[:font_size] * 0.3 Chris@441: options[:font_style] ||= '' Chris@441: options[:padding] ||= 2 Chris@441: options[:x_padding] ||= 0 Chris@441: options[:valign] ||= "M" Chris@441: if options[:fill] == 1 or options[:border] == 1 Chris@441: draw_box(x, y, w, h, options) Chris@441: end Chris@441: SetMargins(0,0,0) Chris@441: set_text_color_a(options[:font_color], options[:colorspace]) Chris@441: font_size = options[:font_size] Chris@441: SetFont(options[:font], options[:font_style], font_size) Chris@441: font_size += options[:font_line_spacing] Chris@441: case options[:valign] Chris@441: when "B", "bottom" Chris@441: y -= options[:padding] Chris@441: when "T", "top" Chris@441: y += options[:padding] Chris@441: end Chris@441: case options[:align] Chris@441: when "L", "left" Chris@441: x += options[:x_padding] Chris@441: w -= options[:x_padding] Chris@441: w -= options[:x_padding] Chris@441: when "R", "right" Chris@441: x += options[:x_padding] Chris@441: w -= options[:x_padding] Chris@441: w -= options[:x_padding] Chris@441: end Chris@441: SetXY(x, y) Chris@441: if GetStringWidth(text) < w or not text["\n"].nil? and (options[:valign] == "T" || options[:valign] == "top") Chris@441: text = text + "\n" Chris@441: end Chris@441: if GetStringWidth(text) > w or not text["\n"].nil? or (options[:valign] == "B" || options[:valign] == "bottom") Chris@441: font_size += options[:font_size] * 0.1 Chris@441: # TODO 2006-07-21 Level=1 - this is assuming a 2 line text Chris@441: SetXY(x, y + ((h - (font_size * 2)) / 2)) if (options[:valign] == "M" || options[:valign] == "middle") Chris@441: MultiCell(w, font_size, text, 0, options[:align]) Chris@441: else Chris@441: Cell(w, h, text, 0, 0, options[:align]) Chris@441: end Chris@441: end Chris@441: Chris@441: # Draw a string of text at (x, y) as a title. Chris@441: # Chris@441: # Options are: Chris@441: # * :font_color - Default value is COLOR_PALETTE[:black]. Chris@441: # * :font_size - Default value is 18. Chris@441: # * :font_style - Default value is nothing or ''. Chris@441: # * :colorspace - Default value is :rgb or ''. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # draw_title(left_margin, 60, Chris@441: # "title:", Chris@441: # :font_color => ReportHelper::COLOR_PALETTE[:dark_blue]) Chris@441: # Chris@441: def draw_title(x, y, title, options = {}) Chris@441: options[:font_color] ||= Core::RFPDF::COLOR_PALETTE[:black] Chris@441: options[:font] ||= default_font Chris@441: options[:font_size] ||= 18 Chris@441: options[:font_style] ||= '' Chris@441: set_text_color_a(options[:font_color], options[:colorspace]) Chris@441: SetFont(options[:font], options[:font_style], options[:font_size]) Chris@441: SetXY(x, y) Chris@441: Write(options[:font_size] + 2, title) Chris@441: end Chris@441: Chris@441: # Set the draw color. Default value is COLOR_PALETTE[:black]. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # set_draw_color_a(ReportHelper::COLOR_PALETTE[:dark_blue]) Chris@441: # Chris@441: def set_draw_color_a(color = Core::RFPDF::COLOR_PALETTE[:black]) Chris@441: SetDrawColor(color[0], color[1], color[2]) Chris@441: end Chris@441: Chris@441: # Set the fill color. Default value is COLOR_PALETTE[:white]. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # set_fill_color_a(ReportHelper::COLOR_PALETTE[:dark_blue]) Chris@441: # Chris@441: def set_fill_color_a(color = Core::RFPDF::COLOR_PALETTE[:white], colorspace = :rgb) Chris@441: if colorspace == :cmyk Chris@441: SetCmykFillColor(color[0], color[1], color[2], color[3]) Chris@441: else Chris@441: SetFillColor(color[0], color[1], color[2]) Chris@441: end Chris@441: end Chris@441: Chris@441: # Set the text color. Default value is COLOR_PALETTE[:white]. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # set_text_color_a(ReportHelper::COLOR_PALETTE[:dark_blue]) Chris@441: # Chris@441: def set_text_color_a(color = Core::RFPDF::COLOR_PALETTE[:black], colorspace = :rgb) Chris@441: if colorspace == :cmyk Chris@441: SetCmykTextColor(color[0], color[1], color[2], color[3]) Chris@441: else Chris@441: SetTextColor(color[0], color[1], color[2]) Chris@441: end Chris@441: end Chris@441: Chris@441: # Write a string containing html characters. Default value is COLOR_PALETTE[:white]. Chris@441: # Chris@441: # Options are: Chris@441: # * :height - Line height. Default value is 20. Chris@441: # Chris@441: # Example: Chris@441: # Chris@441: # write_html_with_options(html, :height => 12) Chris@441: # Chris@441: #FIXME 2007-08-07 (EJM) Level=0 - This needs to call the TCPDF version. Chris@441: def write_html_with_options(html, options = {}) Chris@441: options[:fill] ||= 0 Chris@441: options[:height] ||= 20 Chris@441: options[:new_line_after] ||= false Chris@441: write_html(html, options[:new_line_after], options[:fill], options[:height]) Chris@441: return Chris@441: end Chris@441: end