forked from asciidoctor/asciidoctor-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves asciidoctor#353 add example with tests of extended converter…
… that supports image float
- Loading branch information
1 parent
a1553c0
commit 7ad8889
Showing
8 changed files
with
815 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class PDFConverterImageFloat < (Asciidoctor::Converter.for 'pdf') | ||
register_for 'pdf' | ||
|
||
def convert_paragraph node | ||
prev_sibling = (self_idx = (siblings = node.parent.blocks).index node) > 0 ? siblings[self_idx - 1] : nil | ||
if (prev_sibling&.context == :image && (float_to = prev_sibling.attr 'float')) || (@float_box ||= nil) | ||
start_cursor = cursor # start_cursor is always start of block below image | ||
roles = node.roles | ||
text_align = (resolve_text_align_from_role roles, query_theme: true, remove_predefined: true) || @base_text_align.to_sym | ||
para_font_descender = para_font_size = nil | ||
if roles.empty? | ||
para_font_descender = font.descender | ||
para_font_size = font_size | ||
line_metrics = calc_line_metrics @base_line_height | ||
else | ||
line_metrics = theme_font_cascade (role_keys = roles.map {|role| %(role_#{role}).to_sym }) do | ||
para_font_descender = font.descender | ||
para_font_size = font_size | ||
calc_line_metrics @base_line_height | ||
end | ||
end | ||
if (float_box = @float_box) | ||
@float_box = nil | ||
float_box[:at][1] -= line_metrics.padding_top # typeset_text with box doesn't add line_metrics padding | ||
else | ||
float_gap_s, float_gap_b = ::Array === (float_gap = theme.image_float_gap) ? float_gap : [float_gap || 12, float_gap || 6] | ||
float_w = (prev_sibling.attr 'rendered-width') + float_gap_s | ||
float_h = (prev_sibling.attr 'rendered-height') + (prev_sibling.attr 'caption-height', 0) + float_gap_b | ||
if (box_w = bounds.width - float_w) < para_font_size | ||
prev_sibling.remove_attr 'float' | ||
return convert_paragraph node | ||
end | ||
box_t = start_cursor + (float_h - float_gap_b) + theme.block_margin_bottom | ||
box_l = float_to == 'right' ? 0 : float_w | ||
box_t -= line_metrics.padding_top # typeset_text with box doesn't add line_metrics padding | ||
# NOTE: allocate at least one whole empty line below image | ||
line_height_length = line_metrics.height + line_metrics.leading + line_metrics.padding_top | ||
float_box = { at: [box_l, box_t], width: box_w, height: [box_t, float_h + line_height_length].min } | ||
end | ||
float_box[:final_gap] = para_font_descender + line_metrics.padding_bottom | ||
add_dest_for_block node if node.id | ||
end_cursor = nil | ||
prose_opts = float_box.merge align: text_align, hyphenate: true, margin_bottom: 0, draw_text_callback: (proc do |text, opts| | ||
draw_text! text, opts | ||
end_cursor = opts[:at][1] | ||
end) | ||
if role_keys | ||
remaining = theme_font_cascade(role_keys) { ink_prose node.content, prose_opts } | ||
else | ||
remaining = ink_prose node.content, prose_opts | ||
end | ||
end_cursor = end_cursor ? end_cursor - float_box[:final_gap] : start_cursor | ||
if remaining.empty? | ||
if siblings[self_idx + 1]&.context == :paragraph | ||
end_cursor -= theme.prose_margin_bottom | ||
inked_height = float_box[:at][1] - end_cursor | ||
if (float_box[:height] -= inked_height) > 0 | ||
float_box[:at][1] -= inked_height | ||
float_box[:line_metrics] = line_metrics | ||
@float_box = float_box | ||
else | ||
move_cursor_to end_cursor | ||
end | ||
else | ||
move_cursor_to start_cursor | ||
end | ||
else | ||
move_cursor_to end_cursor | ||
if role_keys | ||
theme_font_cascade(role_keys) { typeset_formatted_text remaining, line_metrics, align: text_align } | ||
else | ||
typeset_formatted_text remaining, line_metrics, align: text_align | ||
end | ||
theme_margin :prose, :bottom, (next_enclosed_block node) | ||
end | ||
else | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.