Skip to content

Commit ef34fdb

Browse files
committed
Adjust leading per font family
1 parent fd634c7 commit ef34fdb

8 files changed

Lines changed: 8 additions & 4 deletions

File tree

examples/headings.pdf

0 Bytes
Binary file not shown.

examples/misc_elements.pdf

0 Bytes
Binary file not shown.

examples/random_content.pdf

25 Bytes
Binary file not shown.

examples/styles.pdf

0 Bytes
Binary file not shown.

lib/prawn-html.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
module PrawnHtml
4+
ADJUST_LEADING = { nil => 0.18, 'Courier' => -0.07, 'Helvetica' => -0.17, 'Times-Roman' => 0.03 }.freeze
45
PX = 0.6 # conversion constant for pixel sixes
56

67
COLORS = {

lib/prawn_html/document_renderer.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ def apply_callbacks(buffer)
133133
def adjust_leading(buffer, leading)
134134
return leading if leading
135135

136-
(buffer.map { |item| item[:size] || Context::DEFAULT_STYLES[:size] }.max * 0.055).round(4)
136+
leadings = buffer.map do |item|
137+
(item[:size] || Context::DEFAULT_STYLES[:size]) * (ADJUST_LEADING[item[:font]] || ADJUST_LEADING[nil])
138+
end
139+
leadings.max.round(4)
137140
end
138141

139142
def bounds(buffer, options, block_styles)

spec/support/test_utils.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
module TestUtils
44
extend self
55

6-
def adjust_leading(size = PrawnHtml::Context::DEFAULT_STYLES[:size])
7-
(size * 0.055).round(4)
6+
def adjust_leading(size = PrawnHtml::Context::DEFAULT_STYLES[:size], font = nil)
7+
(size * PrawnHtml::ADJUST_LEADING[font]).round(4)
88
end
99

1010
def default_font

spec/units/styles_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
let(:html) { '<div style="font-family: Courier">Some content...</div>' }
2727

2828
let(:expected_buffer) { [{ font: 'Courier', size: TestUtils.default_font_size, text: "Some content..." }] }
29-
let(:expected_options) { { leading: TestUtils.adjust_leading } }
29+
let(:expected_options) { { leading: TestUtils.adjust_leading(TestUtils.default_font_size, 'Courier') } }
3030
let(:expected_extra) { { bounding_box: nil, left_indent: 0 } }
3131

3232
it 'sends the expected buffer elements to Prawn pdf' do

0 commit comments

Comments
 (0)