Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep more specific cell styling instead of it being overwritten by the table cell_style #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Master

* Bugfix: Use a cell's custom style over table styles. (PR [#59](https://github.com/prawnpdf/prawn-table/pull/59), issue [#65](https://github.com/prawnpdf/prawn-table/issues/56))
* Bugfix: Use the cell's specified font to calculate the cell width. (Jesse Doyle, PR [#60](https://github.com/prawnpdf/prawn-table/pull/60), issue [#42](https://github.com/prawnpdf/prawn-table/issues/42))

## 0.2.3
Expand Down
24 changes: 14 additions & 10 deletions lib/prawn/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module Errors
# end
#
class Table
module Interface
module Interface
# @group Experimental API

# Set up and draw a table on this document. A block can be given, which will
Expand Down Expand Up @@ -136,7 +136,7 @@ def make_table(data, options={}, &block)
#
def initialize(data, document, options={}, &block)
@pdf = document
@cells = make_cells(data)
@cells = make_cells(data, options.delete(:cell_style) || {})
@header = false
options.each { |k, v| send("#{k}=", v) }

Expand Down Expand Up @@ -291,7 +291,7 @@ def draw
cells_this_page = []

@cells.each do |cell|
if start_new_page?(cell, offset, ref_bounds)
if start_new_page?(cell, offset, ref_bounds)
# draw cells on the current page and then start a new one
# this will also add a header to the new page if a header is set
# reset array of cells for the new page
Expand Down Expand Up @@ -381,7 +381,7 @@ def row_heights
end

protected

# sets the background color (if necessary) for the given cell
def set_background_color(cell, started_new_page_at_row)
if defined?(@row_colors) && @row_colors && (!@header || cell.row > 0)
Expand Down Expand Up @@ -429,9 +429,9 @@ def ink_and_draw_cells(cells_this_page, draw_cells = true)
def ink_and_draw_cells_and_start_new_page(cells_this_page, cell)
# don't draw only a header
draw_cells = (@header_row.nil? || cells_this_page.size > @header_row.size)

ink_and_draw_cells(cells_this_page, draw_cells)

# start a new page or column
@pdf.bounds.move_past_bottom

Expand Down Expand Up @@ -509,7 +509,7 @@ def header_rows
# Prawn::Table::Cell, and sets up their in-table properties so that they
# know their own position in the table.
#
def make_cells(data)
def make_cells(data, cell_style = {})
assert_proper_table_data(data)

cells = Cells.new
Expand All @@ -523,7 +523,11 @@ def make_cells(data)
column_number += 1 until cells[row_number, column_number].nil?

# Build the cell and store it in the Cells collection.
cell = Cell.make(@pdf, cell_data)
cell = if cell_data.is_a?(Hash)
Cell.make(@pdf, cell_style.merge(cell_data))
else
Cell.make(@pdf, cell_data, cell_style)
end
cells[row_number, column_number] = cell

# Add dummy cells for the rest of the cells in the span group. This
Expand Down Expand Up @@ -576,7 +580,7 @@ def add_header(row_number, cells_this_page)
number_of_header_rows.times do |h|
additional_header_height = add_one_header_row(cells_this_page, x_offset, y_coord-header_height, row_number-1, h)
header_height += additional_header_height
end
end
end
header_height
end
Expand All @@ -593,7 +597,7 @@ def add_one_header_row(page_of_cells, x_offset, y, row, row_of_header=nil)
rows_to_operate_on = @header_row.rows(row_of_header) if row_of_header
rows_to_operate_on.each do |cell|
cell.row = row
cell.dummy_cells.each {|c|
cell.dummy_cells.each {|c|
if cell.rowspan > 1
# be sure to account for cells that span multiple rows
# in this case you need multiple row numbers
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/table/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def initialize(pdf, point, options={})
@rowspan = 1
@dummy_cells = []

options.each { |k, v| send("#{k}=", v) }
style(options)

@initializer_run = true
end
Expand Down
11 changes: 11 additions & 0 deletions manual/table/style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
# also accepts a block that will be called for each cell and can be used for
# some complex styling.
#
# Individual cell styles can also be applied when defining the data for the
# table using a hash syntax for the cell. This style will take precedence over
# any table level cell styles. See the "cell_text" section for a list of
# options.

require File.expand_path(File.join(File.dirname(__FILE__),
%w[.. example_helper]))

Expand All @@ -19,4 +24,10 @@
c.background_color = ((c.row + c.column) % 2).zero? ? '000000' : 'ffffff'
end
end
move_down 20

table(
[['A', 'B'],['C', { content: 'D', text_color: 'ff0000' }]],
cell_style: { text_color: '0000ff' }
)
end
10 changes: 10 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1604,4 +1604,14 @@
pdf.render
pdf.page_count.should == 1
end

it 'illustrates issue #56 cell style should not be overwritten by table style', issue: 56 do
t = @pdf.table([['col1', 'col2'],
['val1', { content: 'val2', align: :left }]],
cell_style: { align: :center })
t.cells[0, 0].align.should == :center
t.cells[0, 1].align.should == :center
t.cells[1, 0].align.should == :center
t.cells[1, 1].align.should == :left
end
end