From 60084d8490ad79030a149c2f6aa13c172455fada Mon Sep 17 00:00:00 2001 From: Russell Sanders Date: Fri, 16 Oct 2015 13:13:19 -0700 Subject: [PATCH 1/2] Keep more specific cell styling instead of it being overwritten by the table cell_style https://github.com/prawnpdf/prawn-table/issues/56 --- lib/prawn/table.rb | 24 ++++++++++++++---------- lib/prawn/table/cell.rb | 2 +- spec/table_spec.rb | 10 ++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/prawn/table.rb b/lib/prawn/table.rb index 264bf7a..1c9c2cf 100644 --- a/lib/prawn/table.rb +++ b/lib/prawn/table.rb @@ -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 @@ -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) } @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/prawn/table/cell.rb b/lib/prawn/table/cell.rb index 83b940f..493ad4d 100644 --- a/lib/prawn/table/cell.rb +++ b/lib/prawn/table/cell.rb @@ -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 diff --git a/spec/table_spec.rb b/spec/table_spec.rb index d33c245..f6578bc 100644 --- a/spec/table_spec.rb +++ b/spec/table_spec.rb @@ -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 From 95c26f9c6cfc9fe5218e203a157705d8305221ef Mon Sep 17 00:00:00 2001 From: Russell Sanders Date: Mon, 30 Nov 2015 16:39:42 -0800 Subject: [PATCH 2/2] Add a CHANGELOG entry and documentation for the manual --- CHANGELOG.md | 1 + manual/table/style.rb | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d59195..22bcb13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/manual/table/style.rb b/manual/table/style.rb index 22c8f56..b618605 100644 --- a/manual/table/style.rb +++ b/manual/table/style.rb @@ -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])) @@ -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