diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2020-08-24 23:39:25 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2020-08-25 00:19:37 -0600 |
| commit | e0b408e80bddffad71dd05d3f8c19db9fd4751b9 (patch) | |
| tree | c66848030777e9940428cf8f8ba0ae3bde69d347 | |
| parent | 1c331f0eea8629b574ac8c04a2e212d35cae11e3 (diff) | |
follow-up to #1745 to add support for % units as well
| -rw-r--r-- | CHANGELOG.adoc | 2 | ||||
| -rw-r--r-- | lib/asciidoctor/pdf/ext/prawn/formatted_text/arranger.rb | 8 | ||||
| -rw-r--r-- | spec/formatted_text_formatter_spec.rb | 14 |
3 files changed, 21 insertions, 3 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index a2f69545..f2861b39 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -100,7 +100,7 @@ Bug Fixes:: * display decimal list marker correctly when list is reversed (e.g., 10., 09., 08., etc) * use correct spacing for dotted border dash on table (length and spacing should match width) * set color space on page with only image so font color is preserved in running content (#1742) -* compute font size for superscript and subscript correctly when parent element uses em units (#1745) +* compute font size for superscript and subscript correctly when parent element uses em and % units (#1745) * respect hyphenation exceptions when word is adjacent to non-word character (#1715) * fix crash when TOC is enabled and index is empty * align TOC section properly when index exceeds one page and section_indent is positive (#1735) diff --git a/lib/asciidoctor/pdf/ext/prawn/formatted_text/arranger.rb b/lib/asciidoctor/pdf/ext/prawn/formatted_text/arranger.rb index a7831c21..4413fb0c 100644 --- a/lib/asciidoctor/pdf/ext/prawn/formatted_text/arranger.rb +++ b/lib/asciidoctor/pdf/ext/prawn/formatted_text/arranger.rb @@ -21,8 +21,12 @@ Prawn::Text::Formatted::Arranger.prepend (Module.new do def apply_font_size size, styles if (subscript? styles) || (superscript? styles) size ||= @document.font_size - units = (size.end_with? 'em') ? 'em' : '' if String === size - size = units ? %(#{size.to_f * SUB_N_SUP_RELATIVE_SIZE}#{units}) : size * SUB_N_SUP_RELATIVE_SIZE + if String === size + units = (size.end_with? 'em', '%') ? ((size.end_with? '%') ? '%' : 'em') : '' + size = %(#{size.to_f * SUB_N_SUP_RELATIVE_SIZE}#{units}) + else + size *= SUB_N_SUP_RELATIVE_SIZE + end @document.font_size(size) { yield } elsif size @document.font_size(size) { yield } diff --git a/spec/formatted_text_formatter_spec.rb b/spec/formatted_text_formatter_spec.rb index 440856fe..3e28de87 100644 --- a/spec/formatted_text_formatter_spec.rb +++ b/spec/formatted_text_formatter_spec.rb @@ -184,6 +184,13 @@ describe Asciidoctor::PDF::FormattedText::Formatter do (expect superscript_text[:font_size]).to eql expected_font_size end + it 'should compute font size for superscript phrase correctly when parent element uses % units' do + pdf = to_pdf '`x^2^` represents exponential growth', pdf_theme: { base_font_size: 14, literal_font_size: '90%' }, analyze: true + expected_font_size = 14 * 0.9 * 0.583 + superscript_text = pdf.find_unique_text '2' + (expect superscript_text[:font_size]).to eql expected_font_size + end + it 'should format subscript phrase' do pdf = to_pdf 'O~2~', analyze: true (expect pdf.strings).to eql %w(O 2) @@ -199,6 +206,13 @@ describe Asciidoctor::PDF::FormattedText::Formatter do (expect subscript_text[:font_size]).to eql expected_font_size end + it 'should compute font size for subscript phrase correctly when parent element uses % units' do + pdf = to_pdf 'The formula `O~2~` is oxygen', pdf_theme: { base_font_size: 14, literal_font_size: '90%' }, analyze: true + expected_font_size = 14 * 0.9 * 0.583 + subscript_text = pdf.find_unique_text '2' + (expect subscript_text[:font_size]).to eql expected_font_size + end + it 'should add background and border to code as defined in theme', visual: true do theme_overrides = { literal_background_color: 'f5f5f5', |
