Skip to content

Commit

Permalink
resolves #2434 honor table caption end placement when unbreakable (or…
Browse files Browse the repository at this point in the history
… breakable) option is set on table
  • Loading branch information
mojavelinux committed Jun 28, 2023
1 parent ee40e00 commit e18de0b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Bug Fixes::
* don't crash if page background image value is empty (interpret as "none")
* prevent special character substitution from interfering with callouts in plain verbatim block (#2390)
* remove deprecated, undocumented `svg-font-family` theme key (the correct key is `svg-fallback-font-family`)
* honor table caption end placement (`table-caption-end` theme key) when `unbreakable` (or `breakable`) option is set on table (#2434)

== 2.3.8 (2023-06-25) - @mojavelinux

Expand Down
9 changes: 4 additions & 5 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,7 @@ def convert_stem node
end

def convert_table node
caption_end = (theme = @theme).table_caption_end&.to_sym || :top
if !at_page_top? && ((unbreakable = node.option? 'unbreakable') || ((node.option? 'breakable') && (node.id || node.title?)))
# NOTE: we use the current node as the parent so we can navigate back into the document model
(table_container = Block.new node, :open) << (table_dup = node.dup)
Expand All @@ -2001,7 +2002,7 @@ def convert_table node
end
table_container.style = 'table-container'
table_container.id, table_dup.id = table_dup.id, nil
if table_dup.title?
if caption_end == :top && table_dup.title?
table_container.title = ''
table_container.instance_variable_set :@converted_title, table_dup.captioned_title
table_dup.title = nil
Expand All @@ -2013,7 +2014,6 @@ def convert_table node
num_rows = node.attr 'rowcount'
num_cols = node.columns.size
table_header_size = false
theme = @theme
prev_font_scale, @font_scale = @font_scale, 1 if node.document.nested?

tbl_bg_color = resolve_theme_color :table_background_color
Expand Down Expand Up @@ -2251,7 +2251,6 @@ def convert_table node
alignment = theme.table_align&.to_sym || :left
end

caption_end = theme.table_caption_end&.to_sym || :top
caption_max_width = theme.table_caption_max_width || 'fit-content'

table_settings = {
Expand Down Expand Up @@ -2283,7 +2282,7 @@ def convert_table node
@column_widths = column_widths unless column_widths.empty?
# NOTE: call width to capture resolved table width
table_width = width
@pdf.ink_table_caption node, alignment, table_width, caption_max_width if node.title? && caption_end == :top
@pdf.ink_table_caption node, alignment, table_width, caption_max_width if caption_end == :top && node.title?
# NOTE: align using padding instead of bounding_box as prawn-table does
# using a bounding_box across pages mangles the margin box of subsequent pages
if alignment != :left && table_width != (this_bounds = @pdf.bounds).width
Expand Down Expand Up @@ -2356,7 +2355,7 @@ def convert_table node
bounds.subtract_left_padding left_padding
bounds.subtract_right_padding right_padding if right_padding
end
ink_table_caption node, alignment, table_width, caption_max_width, caption_end if node.title? && caption_end == :bottom
ink_table_caption node, alignment, table_width, caption_max_width, caption_end if caption_end == :bottom && node.title?
theme_margin :block, :bottom, (next_enclosed_block node)
rescue ::Prawn::Errors::CannotFit
log :error, (message_with_context 'cannot fit contents of table cell into specified column width', source_location: node.source_location)
Expand Down
23 changes: 23 additions & 0 deletions spec/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3291,6 +3291,29 @@
(expect cell_a1_text[:page_number]).to be 2
end

it 'should honor caption end placement if %unbreakable option is set on table' do
pdf_theme = { table_caption_end: 'bottom' }
pdf = to_pdf <<~END, pdf_theme: pdf_theme, analyze: true
image::tall.svg[pdfwidth=75mm]
.Title
[%unbreakable]
|===
| Column A | Column B
#{(1.upto 5).map {|idx| %(| A#{idx} | B#{idx}) }.join %(\n\n)}
|===
END

title_text = pdf.find_unique_text 'Table 1. Title'
(expect title_text[:page_number]).to be 2
column_a_text = pdf.find_text 'Column A'
(expect column_a_text).to have_size 1
column_a_text = column_a_text[0]
(expect column_a_text[:page_number]).to be 2
(expect title_text[:y]).to be < column_a_text[:y]
end

it 'should keep caption with table if %breakable option is set on table' do
pdf = to_pdf <<~END, analyze: true
image::tall.svg[pdfwidth=80mm]
Expand Down

0 comments on commit e18de0b

Please sign in to comment.