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