diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2022-04-15 15:18:06 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-15 15:18:06 -0600 |
| commit | 6891e9e56d1f4df25c95155b173758d74bc064e2 (patch) | |
| tree | 7e409f1fd633071216c5ee82af36bcc2df1207a5 | |
| parent | 9a76eeee9b7202db8ab2a9df7e66e82246c76736 (diff) | |
resolves #1939 honor role and inherited role on inline image (PR #2029)
additionally resolves #1376
| -rw-r--r-- | CHANGELOG.adoc | 1 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/converter.rb | 3 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/formatted_text/transform.rb | 15 | ||||
| -rw-r--r-- | spec/image_spec.rb | 33 | ||||
| -rw-r--r-- | spec/reference/image-inline-background.pdf | bin | 0 -> 32583 bytes | |||
| -rw-r--r-- | spec/reference/image-inline-border.pdf | bin | 0 -> 32593 bytes |
6 files changed, 50 insertions, 2 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index a566a5e0..32701c96 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -36,6 +36,7 @@ Enhancements:: * set `part-numeral` attribute in running content on pages in part if `partnums` attribute is set (#1373) * add support for normal_italic font style (to reset font style to normal, then apply italic) (#1603) * honor text alignment roles (e.g., `text-center`) on block image (#1609) +* honor role and inherited role on inline image (#1939, #1376) * disable running header and/or footer on toc pages if `noheader` and/or `nofooter` option is set on toc macro (#1378) * add support for preamble toc placement * only insert macro toc at location of first toc macro diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index 58e72fa6..26c40079 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -2589,8 +2589,9 @@ module Asciidoctor else width = (intrinsic_image_dimensions image_path, image_format)[:width] end + class_attr = (role = node.role) ? %( class="#{role}") : '' fit_attr = (fit = node.attr 'fit') ? %( fit="#{fit}") : '' - img = %(<img src="#{image_path}" format="#{image_format}" alt="#{encode_quotes node.attr 'alt'}" width="#{width}"#{fit_attr}>) + img = %(<img src="#{image_path}" format="#{image_format}" alt="#{encode_quotes node.attr 'alt'}" width="#{width}"#{class_attr}#{fit_attr}>) else log :warn, %(image to embed not found or not readable: #{image_path}) img = %([#{node.attr 'alt'}]) diff --git a/lib/asciidoctor/pdf/formatted_text/transform.rb b/lib/asciidoctor/pdf/formatted_text/transform.rb index e9a070ed..f44d97f1 100644 --- a/lib/asciidoctor/pdf/formatted_text/transform.rb +++ b/lib/asciidoctor/pdf/formatted_text/transform.rb @@ -177,9 +177,22 @@ module Asciidoctor # a zero-width space in the text will cause the image to be duplicated # NOTE: add enclosing square brackets here to avoid errors in parsing text: %([#{attributes[:alt].delete ZeroWidthSpace}]), - callback: [InlineImageRenderer], object_id: node.object_id, # used to deduplicate if fragment gets split up } + if inherited && (callback = inherited[:callback]) && (callback.include? TextBackgroundAndBorderRenderer) + # NOTE: if we keep InlineTextAligner, it needs to skip draw_text! for image fragment + fragment[:callback] = [TextBackgroundAndBorderRenderer, InlineImageRenderer] + fragment.update inherited.slice :border_color, :border_offset, :border_radius, :border_width, :background_color + else + fragment[:callback] = [InlineImageRenderer] + end + attributes[:class].split.each do |class_name| + next unless @theme_settings.key? class_name + update_fragment fragment, @theme_settings[class_name] + if fragment[:background_color] || (fragment[:border_color] && fragment[:border_width]) + fragment[:callback] = [TextBackgroundAndBorderRenderer] | fragment[:callback] + end + end if attributes.key? :class if inherited && (link = inherited[:link]) fragment[:link] = link end diff --git a/spec/image_spec.rb b/spec/image_spec.rb index 9dd90afc..e2e176d6 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -2079,6 +2079,39 @@ describe 'Asciidoctor::PDF::Converter - Image' do (expect pdf.lines.select {|it| it[:color] == 'DEDEDE' }).to be_empty end + it 'should draw border around inline image if border width and border color are set in the theme', visual: true do + pdf_theme = { + role_enclose_border_width: 0.5, + role_enclose_border_offset: 1, + role_enclose_border_color: '0000FF', + role_enclose_border_radius: 2, + } + + %w([.enclose]#image:tux.png[tux,fit=line]# image:tux.png[tux,fit=line,role=enclose]).each do |image| + to_file = to_pdf_file <<~EOS, 'image-inline-border.pdf', pdf_theme: pdf_theme + before #{image} after + EOS + + (expect to_file).to visually_match 'image-inline-border.pdf' + end + end + + it 'should draw background under inline image if background color is set in the theme', visual: true do + pdf_theme = { + role_enclose_background_color: 'CCCCCC', + role_enclose_border_offset: 1, + role_enclose_border_radius: 2, + } + + %w([.enclose]#image:tux.png[tux,fit=line]# image:tux.png[tux,fit=line,role=enclose]).each do |image| + to_file = to_pdf_file <<~EOS, 'image-inline-background.pdf', pdf_theme: pdf_theme + before #{image} after + EOS + + (expect to_file).to visually_match 'image-inline-background.pdf' + end + end + it 'should ignore :fit option for SVG image if :width is set' do doc = Prawn::Document.new do text 'start' diff --git a/spec/reference/image-inline-background.pdf b/spec/reference/image-inline-background.pdf Binary files differnew file mode 100644 index 00000000..1d7d6b70 --- /dev/null +++ b/spec/reference/image-inline-background.pdf diff --git a/spec/reference/image-inline-border.pdf b/spec/reference/image-inline-border.pdf Binary files differnew file mode 100644 index 00000000..eff2d055 --- /dev/null +++ b/spec/reference/image-inline-border.pdf |
