Skip to content

Commit

Permalink
resolves asciidoctor#2547 allow value of base-font-size-min and <cate…
Browse files Browse the repository at this point in the history
…gory>-font-size-min theme keys to be relative
  • Loading branch information
mojavelinux committed Dec 10, 2024
1 parent b154248 commit 6129e71
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Improvements::
* drop support for the unmaintained payment font (`pf`) for use in font-based icons
* refactor formatted text transform to simplify how inner space is collapsed; verify only inner hard breaks are preserved
* allow relative font size for sub and sup to be set independently; support combined setting for backwards compatibility
* allow value of `base-font-size-min` and `<category>-font-size-min` theme keys to be relative (e.g., 0.75em) (#2547)

Bug Fixes::

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
3 changes: 3 additions & 0 deletions docs/modules/theme/pages/text.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ The font size may be specified either as a fixed value (e.g., `12`) or a relativ
Font sizes are most often specified as a fixed value, though there are important cases when a relative value is more suitable.
The font size always resolves to a point value when text is written to the PDF.

IMPORTANT: The `base-font-size` must always be a number.
That's because the value of this key is the reference value on which relative font values (e.g., `0.75rem`) are based.

If the font size is specified as a number, or a number with a fixed unit of measurement (e.g., `px`), that value is converted to points.
The conversion to points is done when the theme is loaded.
The `font-size` key on the `base` category, as well as min, large, and small font size keys, must always be specified as a fixed value.
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 @@ -4836,14 +4836,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 @@ -341,6 +341,30 @@
end
end

it 'should allow base minimum font size to be specified relative to base font size' do
pdf = to_pdf <<~'END', 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
----
END

(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 <<~'END', 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
----
END

(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 <<~'END', pdf_theme: { base_font_color: 'AA0000', code_font_color: nil }, analyze: true
before
Expand Down

0 comments on commit 6129e71

Please sign in to comment.