Skip to content

Commit 90d9045

Browse files
committed
Move base styles Context method in a constant
1 parent c5678c2 commit 90d9045

7 files changed

Lines changed: 11 additions & 15 deletions

File tree

lib/prawn_html/context.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
module PrawnHtml
44
class Context < Array
5-
DEF_FONT_SIZE = 16 * PX
5+
DEFAULT_STYLES = {
6+
size: 16 * PX
7+
}.freeze
68

79
attr_reader :previous_tag
810
attr_accessor :last_text_node
@@ -54,7 +56,7 @@ def block_styles
5456
# @return [Hash] the hash of merged styles
5557
def merged_styles
5658
@merged_styles ||=
57-
each_with_object(base_styles) do |element, res|
59+
each_with_object(DEFAULT_STYLES.dup) do |element, res|
5860
evaluate_element_styles(element, res)
5961
element.update_styles(res)
6062
end
@@ -71,12 +73,6 @@ def remove_last
7173

7274
private
7375

74-
def base_styles
75-
{
76-
size: DEF_FONT_SIZE
77-
}
78-
end
79-
8076
def evaluate_element_styles(element, res)
8177
styles = element.styles.slice(*Attributes::STYLES_APPLY[:text_node])
8278
styles.each do |key, val|

lib/prawn_html/document_renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def apply_callbacks(buffer)
129129
def adjust_leading(buffer, leading)
130130
return leading if leading
131131

132-
(buffer.map { |item| item[:size] || Context::DEF_FONT_SIZE }.max * 0.055).round(4)
132+
(buffer.map { |item| item[:size] || Context::DEFAULT_STYLES[:size] }.max * 0.055).round(4)
133133
end
134134

135135
def bounds(buffer, options, block_styles)

lib/prawn_html/tags/small.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Small < Tag
66
ELEMENTS = [:small].freeze
77

88
def update_styles(context_styles)
9-
size = (context_styles[:size] || Context::DEF_FONT_SIZE) * 0.85
9+
size = (context_styles[:size] || Context::DEFAULT_STYLES[:size]) * 0.85
1010
context_styles[:size] = size
1111
super(context_styles)
1212
end

spec/integrations/blocks_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
RSpec.describe 'Blocks' do
44
let(:pdf) { Prawn::Document.new(page_size: 'A4', page_layout: :portrait) }
5-
let(:size) { PrawnHtml::Context::DEF_FONT_SIZE }
5+
let(:size) { PrawnHtml::Context::DEFAULT_STYLES[:size] }
66

77
before do
88
PrawnHtml.append_html(pdf, html)

spec/support/test_utils.rb

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

6-
def adjust_leading(size = PrawnHtml::Context::DEF_FONT_SIZE)
6+
def adjust_leading(size = PrawnHtml::Context::DEFAULT_STYLES[:size])
77
(size * 0.055).round(4)
88
end
99

@@ -16,7 +16,7 @@ def default_font_family
1616
end
1717

1818
def default_font_size
19-
PrawnHtml::Context::DEF_FONT_SIZE
19+
PrawnHtml::Context::DEFAULT_STYLES[:size]
2020
end
2121

2222
def font_ascender(font_family: 'Helvetica', font_size: default_font_size)

spec/units/prawn_html/context_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def on_context_remove(context)
121121
subject(:merged_styles) { context.merged_styles }
122122

123123
context 'with no elements' do
124-
it { is_expected.to eq(size: PrawnHtml::Context::DEF_FONT_SIZE) }
124+
it { is_expected.to eq(size: PrawnHtml::Context::DEFAULT_STYLES[:size]) }
125125
end
126126

127127
context 'with some elements' do

spec/units/prawn_html/tags/small_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
include_context 'with pdf wrapper'
2828

2929
let(:html) { '<small>Some sample content</small>' }
30-
let(:size) { PrawnHtml::Context::DEF_FONT_SIZE * 0.85 }
30+
let(:size) { PrawnHtml::Context::DEFAULT_STYLES[:size] * 0.85 }
3131

3232
before { append_html_to_pdf(html) }
3333

0 commit comments

Comments
 (0)