Permalink
Browse files

Merge changes

  • Loading branch information...
2 parents 3d08678 + 37c99e2 commit c525aa75dd3b985132994f13561e36fd625f71ae @practicingruby committed Nov 10, 2009
View
@@ -0,0 +1,3 @@
+[submodule "vendor/prawn-core"]
+ path = vendor/prawn-core
+ url = git://github.com/sandal/prawn.git
View
@@ -4,7 +4,7 @@ require 'rake/testtask'
require "rake/rdoctask"
require "rake/gempackagetask"
-PRAWN_LAYOUT_VERSION = "0.2.1"
+PRAWN_LAYOUT_VERSION = "0.3.1"
task :default => [:test]
@@ -0,0 +1,17 @@
+# As of b8e6f2f1333e03cc3990307b6ce3e52481fcf367, a visible gap was left between
+# cells when using :underline_header
+#
+# This was fixed by a series of patches from:
+# http://github.com/corny/prawn-layout
+
+$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
+
+require "rubygems"
+require "prawn"
+require "prawn/layout"
+
+Prawn::Document.generate("table.pdf") do
+ table [%w[a b c], %w[d e f]], :border_style => :underline_header,
+ :row_colors => :pdf_writer,
+ :headers => %w[foo bar baz]
+end
@@ -1,6 +1,10 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
+ %w[.. vendor prawn-core lib]))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
require "rubygems"
-require "prawn"
+
+require "prawn/core"
require "prawn/layout"
Prawn.debug = true
@@ -0,0 +1,21 @@
+# encoding: utf-8
+
+require "#{File.dirname(__FILE__)}/../example_helper.rb"
+
+Prawn::Document.generate('bounding_box_grid.pdf') do |p|
+ p.define_grid(:columns => 5, :rows => 8, :gutter => 10)
+
+ p.stroke_color = "ff0000"
+
+ p.grid.rows.times do |i|
+ p.grid.columns.times do |j|
+ p.grid(i,j).bounding_box do
+ p.text p.grid(i,j).name
+ p.stroke do
+ p.rectangle(p.bounds.top_left, p.bounds.width, p.bounds.height)
+ end
+ end
+ end
+ end
+end
+
@@ -0,0 +1,20 @@
+# encoding: utf-8
+
+require "#{File.dirname(__FILE__)}/../example_helper.rb"
+
+Prawn::Document.generate('column_gutter_grid.pdf') do |p|
+ p.define_grid(:columns => 3, :rows => 10, :column_gutter => 10)
+
+ p.grid.rows.times do |i|
+ p.grid.columns.times do |j|
+ b = p.grid(i,j)
+ p.bounding_box b.top_left, :width => b.width, :height => b.height do
+ p.text b.name
+ p.stroke do
+ p.rectangle(p.bounds.top_left, b.width, b.height)
+ end
+ end
+ end
+ end
+end
+
@@ -0,0 +1,51 @@
+# encoding: utf-8
+
+require "#{File.dirname(__FILE__)}/../example_helper.rb"
+
+Prawn::Document.generate('multi_boxes.pdf') do |p|
+ p.define_grid(:columns => 5, :rows => 8, :gutter => 10)
+
+ p.grid.rows.times do |i|
+ p.grid.columns.times do |j|
+ p.grid(i,j).bounding_box do
+ p.text p.grid(i,j).name
+ p.stroke_color = "cccccc"
+ p.stroke do
+ p.rectangle(p.bounds.top_left, p.bounds.width, p.bounds.height)
+ end
+ end
+ end
+ end
+
+ g = p.grid([0,0], [1,1])
+ g.bounding_box do
+ p.move_down 12
+ p.text g.name
+ p.stroke_color = "333333"
+ p.stroke do
+ p.rectangle(p.bounds.top_left, p.bounds.width, p.bounds.height)
+ end
+ end
+
+ g = p.grid([3,0], [3,3])
+ g.bounding_box do
+ p.move_down 12
+ p.text g.name
+ p.stroke_color = "333333"
+ p.stroke do
+ p.rectangle(p.bounds.top_left, p.bounds.width, p.bounds.height)
+ end
+ end
+
+ g = p.grid([4,0], [5,1])
+ g.bounding_box do
+ p.move_down 12
+ p.text g.name
+ p.stroke_color = "333333"
+ p.stroke do
+ p.rectangle(p.bounds.top_left, p.bounds.width, p.bounds.height)
+ end
+ end
+
+end
+
View
@@ -0,0 +1,13 @@
+# encoding: utf-8
+
+require "#{File.dirname(__FILE__)}/../example_helper.rb"
+
+Prawn::Document.generate('show_grid.pdf') do |p|
+ p.define_grid(:columns => 5, :rows => 8, :gutter => 10)
+
+ p.grid.show_all
+
+ p.grid(2,4).show("FF0000")
+ p.grid([3,0], [5,3]).show("0000FF")
+end
+
View
@@ -16,6 +16,6 @@ module Errors
end
module Layout
- VERSION = "0.2.1"
+ VERSION = "0.3.1"
end
end
View
@@ -1,18 +1,21 @@
module Prawn
class Document
- # Defines the grid system for a particular document. Takes the number of rows and columns and the
- # width to use for the gutter as the keys :rows, :columns, :gutter
+
+ # Defines the grid system for a particular document. Takes the number of
+ # rows and columns and the width to use for the gutter as the
+ # keys :rows, :columns, :gutter
#
def define_grid(options = {})
@grid = Grid.new(self, options)
end
- # A method that can either be used to access a particular grid on the page or interogate the grid
- # system directly.
+ # A method that can either be used to access a particular grid on the page
+ # or work with the grid system directly.
#
# @pdf.grid # Get the Grid directly
# @pdf.grid([0,1]) # Get the box at [0,1]
# @pdf.grid([0,1], [1,2]) # Get a multi-box spanning from [0,1] to [1,2]
+ #
def grid(*args)
@boxes ||= {}
@boxes[args] ||= if args.empty?
@@ -28,28 +31,29 @@ def grid(*args)
end
end
- # A Grid represents the entire grid system of a Page and calculates the column width and row height
- # of the base box.
+ # A Grid represents the entire grid system of a Page and calculates
+ # the column width and row height of the base box.
class Grid
- attr_reader :pdf, :columns, :rows, :gutter
+ attr_reader :pdf, :columns, :rows, :gutter, :row_gutter, :column_gutter
# :nodoc
def initialize(pdf, options = {})
- Prawn.verify_options([:columns, :rows, :gutter], options)
+ valid_options = [:columns, :rows, :gutter, :row_gutter, :column_gutter]
+ Prawn.verify_options valid_options, options
@pdf = pdf
@columns = options[:columns]
@rows = options[:rows]
- @gutter = options[:gutter].to_f
+ set_gutter(options)
end
# Calculates the base width of boxes.
def column_width
- @column_width ||= subdivide(pdf.bounds.width, columns)
+ @column_width ||= subdivide(pdf.bounds.width, columns, column_gutter)
end
# Calculates the base height of boxes.
def row_height
- @row_height ||= subdivide(pdf.bounds.height, rows)
+ @row_height ||= subdivide(pdf.bounds.height, rows, row_gutter)
end
# Diagnostic tool to show all of the grids. Defaults to gray.
@@ -62,14 +66,26 @@ def show_all(color = "CCCCCC")
end
private
- def subdivide(total, num)
+
+ def subdivide(total, num, gutter)
(total.to_f - (gutter * (num - 1).to_f)) / num.to_f
end
+
+ def set_gutter(options)
+ if options.has_key?(:gutter)
+ @gutter = options[:gutter].to_f
+ @row_gutter, @column_gutter = @gutter, @gutter
+ else
+ @row_gutter = options[:row_gutter].to_f
+ @column_gutter = options[:column_gutter].to_f
+ @gutter = 0
+ end
+ end
end
- # A Box is a class that represents a bounded area of a page. A Grid object has methods that allow
- # easy access to the coordinates of its corners, which can be plugged into most existing prawn
- # methods.
+ # A Box is a class that represents a bounded area of a page.
+ # A Grid object has methods that allow easy access to the coordinates of
+ # its corners, which can be plugged into most existing prawnmethods.
#
class Box
attr_reader :pdf
@@ -80,7 +96,9 @@ def initialize(pdf, i, j)
@j = j
end
- # Mostly diagnostic method that outputs the name of a box as col_num, row_num
+ # Mostly diagnostic method that outputs the name of a box as
+ # col_num, row_num
+ #
def name
"#{@i.to_s},#{@j.to_s}"
end
@@ -107,7 +125,7 @@ def gutter
# x-coordinate of left side
def left
- @left ||= (width + gutter) * @j.to_f
+ @left ||= (width + grid.column_gutter) * @j.to_f
end
# x-coordinate of right side
@@ -117,7 +135,7 @@ def right
# y-coordinate of the top
def top
- @top ||= total_height - ((height + gutter) * @i.to_f)
+ @top ||= total_height - ((height + grid.row_gutter) * @i.to_f)
end
# y-coordinate of the bottom
View
@@ -45,7 +45,8 @@ class Document
def table(data, options={})
if data.nil? || data.empty?
raise Prawn::Errors::EmptyTable,
- "data must be a non-empty, non-nil, two dimensional array of Prawn::Cells or strings"
+ "data must be a non-empty, non-nil, two dimensional array " +
+ "of Prawn::Cells or strings"
end
Prawn::Table.new(data,self,options).draw
end
@@ -103,15 +104,17 @@ class Table
# <tt>:border_style</tt>:: If set to :grid, fills in all borders. If set to :underline_header, underline header only. Otherwise, borders are drawn on columns only, not rows
# <tt>:border_color</tt>:: Sets the color of the borders.
# <tt>:position</tt>:: One of <tt>:left</tt>, <tt>:center</tt> or <tt>n</tt>, where <tt>n</tt> is an x-offset from the left edge of the current bounding box
- # <tt>:width:</tt> A set width for the table, defaults to the sum of all column widths
- # <tt>:column_widths:</tt> A hash of indices and widths in PDF points. E.g. <tt>{ 0 => 50, 1 => 100 }</tt>
- # <tt>:row_colors</tt>:: An array of row background colors which are used cyclicly.
+ # <tt>:width</tt>:: A set width for the table, defaults to the sum of all column widths
+ # <tt>:column_widths</tt>:: A hash of indices and widths in PDF points. E.g. <tt>{ 0 => 50, 1 => 100 }</tt>
+ # <tt>:row_colors</tt>:: Used to specify background colors for rows. See below for usage.
# <tt>:align</tt>:: Alignment of text in columns, for entire table (<tt>:center</tt>) or by column (<tt>{ 0 => :left, 1 => :center}</tt>)
#
- # Row colors are specified as html encoded values, e.g.
- # ["ffffff","aaaaaa","ccaaff"]. You can also specify
- # <tt>:row_colors => :pdf_writer</tt> if you wish to use the default color
- # scheme from the PDF::Writer library.
+ # Row colors (<tt>:row_colors</tt>) are specified as HTML hex color values,
+ # e.g., "ccaaff". They can take several forms:
+ #
+ # * An array of colors, used cyclically to "zebra stripe" the table: <tt>['ffffff', 'cccccc', '336699']</tt>.
+ # * A hash taking 0-based row numbers to colors: <tt>{ 0 => 'ffffff', 2 => 'cccccc'}</tt>.
+ # * The symbol <tt>:pdf_writer</tt>, for PDF::Writer's default color scheme.
#
# See Document#table for typical usage, as directly using this class is
# not recommended unless you know why you want to do it.
@@ -252,6 +255,13 @@ def generate_table
renderable_data.each_with_index do |row,index|
c = Prawn::Table::CellBlock.new(@document)
+ if C(:row_colors).is_a?(Hash)
+ real_index = index
+ real_index -= 1 if C(:headers)
+ color = C(:row_colors)[real_index]
+ c.background_color = color if color
+ end
+
col_index = 0
row.each do |e|
case C(:align)
@@ -379,6 +389,8 @@ def draw_page(contents)
def next_row_color
+ return if C(:row_colors).is_a?(Hash)
+
color = C(:row_colors).shift
C(:row_colors).push(color)
color
View
@@ -133,9 +133,7 @@ def draw
@document.fill_color @background_color
h = borders.include?(:bottom) ?
height - ( 2 * margin ) : height + margin
- @document.fill_rectangle [x,
- y ],
- width, h
+ @document.fill_rectangle [x, y ], width, h
end
end
View
@@ -7,7 +7,8 @@
require "mocha"
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
-require "prawn"
+$LOAD_PATH << File.join(File.dirname(__FILE__), %w[.. vendor prawn-core lib])
+require "prawn/core"
require "prawn/layout"
$LOAD_PATH << File.join(Prawn::BASEDIR, 'vendor','pdf-inspector','lib')
Oops, something went wrong.

0 comments on commit c525aa7

Please sign in to comment.