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

Fix/headings in tables #60

Merged
merged 5 commits into from
Jan 8, 2025
Merged
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
4 changes: 4 additions & 0 deletions lib/prawn/markup/processor/tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def inside_container?
def add_cell_text_node(cell, options = {})
return unless buffered_text?

# only allow on supported options of prawn-table; See https://github.com/prawnpdf/prawn-table/blob/master/manual/table/cell_text.rb
options = options.slice(*%i[font font_style inline_format kerning leading min_font_size size
overflow rotate rotate_around single_line text_color valign])

cell.nodes << options.merge(content: dump_text.strip)
end

Expand Down
18 changes: 16 additions & 2 deletions spec/prawn/markup/processor/tables_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
processor.parse('<table><tr><td>boot<p>hello</p><p>world</p>and universe</td>' \
'<td><p>other</p><p><br/></p><p>last</p></td></tr></table>')
expect(text.strings).to eq(['boot', 'hello', 'world', 'and universe', 'other', 'last'])
first_sub_col_left = first_col_left + table_padding
expect(left_positions)
.to eq([first_col_left, first_col_left, first_col_left, first_col_left, 342, 342])
expect(top_positions)
Expand All @@ -77,11 +76,26 @@
first_row_top - 4 * line].map(&:round))
end

describe 'nested headings' do
let(:options) do
{
heading1: { size: 36, style: :bold },
heading2: { size: 24, style: :bold_italic }
}
end

it 'creates headings inside tables' do
processor.parse('<table><tr><td><h1>hello</h1><h2>world</h2></td></tr></table>')
expect(text.strings).to eq(['hello', 'world'])
expect(left_positions).to eq([first_col_left, first_col_left])
expect(top_positions).to eq([first_row_top - 17, first_row_top - 50].map(&:round))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do the headings start at offset 17 and (36 + 14)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heading top positions are indeed complex, even in the headings specs we use the "magic" values. I'm fine with these hard-coded values here.

end
end

it 'creates divs inside tables' do
processor.parse('<table><tr><td>boot<div>hello<div>world</div><div>and universe</div></div>all the rest</td>' \
'<td><div>other</div></td></tr></table>')
expect(text.strings).to eq(['boot', 'hello', 'world', 'and universe', 'all the rest', 'other'])
first_sub_col_left = first_col_left + table_padding
expect(left_positions)
.to eq([first_col_left, first_col_left, first_col_left, first_col_left, first_col_left, 368])
expect(top_positions)
Expand Down
Loading