Skip to content

Commit

Permalink
backport fix for #2547 allow value of base-font-size-min and <categor…
Browse files Browse the repository at this point in the history
…y>-font-size-min theme keys to be relative
  • Loading branch information
mojavelinux committed Feb 14, 2025
1 parent 5485290 commit 62b5eef
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co

== Unreleased

_No changes since previous release._
Improvements::

* allow value of `base-font-size-min` and `<category>-font-size-min` theme keys to be relative (e.g., 0.75em) (#2547)

== 2.3.19 (2024-10-11) - @mojavelinux

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/theme/pages/base.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ base:
font-size: 10.5

|font-size-min
|xref:language.adoc#values[Number] +
|xref:text.adoc#font-size[Font size] +
(default: `6`)
|[source]
base:
font-size-min: $base-font-size * 0.75
font-size-min: 0.75rem

|font-style
|xref:text.adoc#font-style[Font style] +
Expand Down
15 changes: 14 additions & 1 deletion lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4805,14 +4805,27 @@ def compute_autofit_font_size fragments, category
padding = expand_padding_value @theme[%(#{category}_padding)]
if actual_width > (available_width = bounds.width - padding[3].to_f - padding[1].to_f)
adjusted_font_size = ((available_width * font_size).to_f / actual_width).truncate 4
if (min = @theme[%(#{category}_font_size_min)] || @theme.base_font_size_min) && adjusted_font_size < min
if (min = @theme[%(#{category}_font_size_min)] || @theme.base_font_size_min) && adjusted_font_size < (min = resolve_font_size min)
min
else
adjusted_font_size
end
end
end

def resolve_font_size value
return value unless ::String === value
if value.end_with? 'rem'
@root_font_size * value.to_f
elsif value.end_with? 'em'
font_size * value.to_f
elsif value.end_with? '%'
font_size * (value.to_f / 100)
else
value.to_f
end
end

def consolidate_ranges nums
if nums.size > 1
prev = nil
Expand Down
24 changes: 24 additions & 0 deletions spec/listing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@
end
end

it 'should allow base minimum font size to be specified relative to base font size' do
pdf = to_pdf <<~'EOS', pdf_theme: { base_font_size: 12, base_font_size_min: '0.5rem' }, analyze: true
[%autofit]
----
play_symbol = (node.document.attr? 'icons', 'font') ? %(<font name="fas">#{(icon_font_data 'fas').unicode 'play'}</font>) : RightPointer
----
EOS

(expect pdf.text).to have_size 1
(expect pdf.text[0][:font_size].floor).to be 7
end

it 'should allow base minimum font size to be specified relative to current font size' do
pdf = to_pdf <<~'EOS', pdf_theme: { base_font_size: 15, code_font_size: 12, base_font_size_min: '0.5em' }, analyze: true
[%autofit]
----
play_symbol = (node.document.attr? 'icons', 'font') ? %(<font name="fas">#{(icon_font_data 'fas').unicode 'play'}</font>) : RightPointer
----
EOS

(expect pdf.text).to have_size 1
(expect pdf.text[0][:font_size].floor).to be 7
end

it 'should use base font color if font color is not specified' do
pdf = to_pdf <<~'EOS', pdf_theme: { base_font_color: 'AA0000', code_font_color: nil }, analyze: true
before
Expand Down

0 comments on commit 62b5eef

Please sign in to comment.