Skip to content

Commit 6129e71

Browse files
committed
resolves #2547 allow value of base-font-size-min and <category>-font-size-min theme keys to be relative
1 parent b154248 commit 6129e71

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

CHANGELOG.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Improvements::
3838
* drop support for the unmaintained payment font (`pf`) for use in font-based icons
3939
* refactor formatted text transform to simplify how inner space is collapsed; verify only inner hard breaks are preserved
4040
* allow relative font size for sub and sup to be set independently; support combined setting for backwards compatibility
41+
* allow value of `base-font-size-min` and `<category>-font-size-min` theme keys to be relative (e.g., 0.75em) (#2547)
4142

4243
Bug Fixes::
4344

docs/modules/theme/pages/base.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ base:
5959
font-size: 10.5
6060

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

6868
|font-style
6969
|xref:text.adoc#font-style[Font style] +

docs/modules/theme/pages/text.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ The font size may be specified either as a fixed value (e.g., `12`) or a relativ
4343
Font sizes are most often specified as a fixed value, though there are important cases when a relative value is more suitable.
4444
The font size always resolves to a point value when text is written to the PDF.
4545

46+
IMPORTANT: The `base-font-size` must always be a number.
47+
That's because the value of this key is the reference value on which relative font values (e.g., `0.75rem`) are based.
48+
4649
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.
4750
The conversion to points is done when the theme is loaded.
4851
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.

lib/asciidoctor/pdf/converter.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -4836,14 +4836,27 @@ def compute_autofit_font_size fragments, category
48364836
padding = expand_padding_value @theme[%(#{category}_padding)]
48374837
if actual_width > (available_width = bounds.width - padding[3].to_f - padding[1].to_f)
48384838
adjusted_font_size = ((available_width * font_size).to_f / actual_width).truncate 4
4839-
if (min = @theme[%(#{category}_font_size_min)] || @theme.base_font_size_min) && adjusted_font_size < min
4839+
if (min = @theme[%(#{category}_font_size_min)] || @theme.base_font_size_min) && adjusted_font_size < (min = resolve_font_size min)
48404840
min
48414841
else
48424842
adjusted_font_size
48434843
end
48444844
end
48454845
end
48464846

4847+
def resolve_font_size value
4848+
return value unless ::String === value
4849+
if value.end_with? 'rem'
4850+
@root_font_size * value.to_f
4851+
elsif value.end_with? 'em'
4852+
font_size * value.to_f
4853+
elsif value.end_with? '%'
4854+
font_size * (value.to_f / 100)
4855+
else
4856+
value.to_f
4857+
end
4858+
end
4859+
48474860
def consolidate_ranges nums
48484861
if nums.size > 1
48494862
prev = nil

spec/listing_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,30 @@
341341
end
342342
end
343343

344+
it 'should allow base minimum font size to be specified relative to base font size' do
345+
pdf = to_pdf <<~'END', pdf_theme: { base_font_size: 12, base_font_size_min: '0.5rem' }, analyze: true
346+
[%autofit]
347+
----
348+
play_symbol = (node.document.attr? 'icons', 'font') ? %(<font name="fas">#{(icon_font_data 'fas').unicode 'play'}</font>) : RightPointer
349+
----
350+
END
351+
352+
(expect pdf.text).to have_size 1
353+
(expect pdf.text[0][:font_size].floor).to be 7
354+
end
355+
356+
it 'should allow base minimum font size to be specified relative to current font size' do
357+
pdf = to_pdf <<~'END', pdf_theme: { base_font_size: 15, code_font_size: 12, base_font_size_min: '0.5em' }, analyze: true
358+
[%autofit]
359+
----
360+
play_symbol = (node.document.attr? 'icons', 'font') ? %(<font name="fas">#{(icon_font_data 'fas').unicode 'play'}</font>) : RightPointer
361+
----
362+
END
363+
364+
(expect pdf.text).to have_size 1
365+
(expect pdf.text[0][:font_size].floor).to be 7
366+
end
367+
344368
it 'should use base font color if font color is not specified' do
345369
pdf = to_pdf <<~'END', pdf_theme: { base_font_color: 'AA0000', code_font_color: nil }, analyze: true
346370
before

0 commit comments

Comments
 (0)