Skip to content

Commit 59c12e5

Browse files
committed
refactor parameter builder
1 parent ed86a97 commit 59c12e5

1 file changed

Lines changed: 45 additions & 27 deletions

File tree

  • lib/thinreports/generator/pdf/document/graphics

lib/thinreports/generator/pdf/document/graphics/text.rb

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,47 @@ module Graphics
2323
# @option attrs [:trancate, :shrink_to_fit, :expand] :overflow (:trancate)
2424
# @option attrs [:none, :break_word] :word_wrap (:none)
2525
def text_box(content, x, y, w, h, attrs = {})
26-
w, h = s2f(w, h)
2726

2827
return if attrs[:color] == 'none'
2928

3029
# Building parameters for box
31-
box_params = {}.tap do |params|
30+
box_params = build_box_params(x, y, w, h, attrs)
31+
# Building parameters for text
32+
text_params = build_text_params(content, attrs)
33+
34+
if need_bold_style_emulation?(text_params[:font], text_params[:styles])
35+
box_params[:mode] = :fill_stroke
36+
37+
emulate_bold_style(text_params[:color], text_params[:size]) do
38+
pdf.formatted_text_box([text_params], box_params)
39+
end
40+
else
41+
pdf.formatted_text_box([text_params], box_params)
42+
end
43+
44+
rescue Prawn::Errors::CannotFit
45+
# Nothing to do.
46+
#
47+
# When the area is too small compared
48+
# with the content and the style of the text.
49+
# (See prawn/core/text/formatted/line_wrap.rb#L185)
50+
end
51+
52+
# @see #text_box
53+
def text(content, x, y, w, h, attrs = {})
54+
# Set the :overflow property to :shirink_to_fit.
55+
text_box(content, x, y, w, h, { overflow: :shirink_to_fit }.merge(attrs))
56+
end
57+
58+
# @private
59+
#
60+
# @param (see #text_box)
61+
# @return [Hash] Returns first parameter (Formatted Text Array) of Prawn::Document#formatted_text_box
62+
# See http://prawnpdf.org/api-docs/2.0/Prawn/Text/Formatted.html
63+
def build_box_params(x, y, w, h, attrs)
64+
w, h = s2f(w, h)
65+
66+
{}.tap do |params|
3267
params[:at] = pos(x, y)
3368
params[:width] = w
3469

@@ -52,38 +87,21 @@ def text_box(content, x, y, w, h, attrs = {})
5287
params[:character_spacing] = attrs[:letter_spacing]
5388
end
5489
end
90+
end
5591

56-
# Building parameters for text
57-
text_params = {}.tap do |params|
92+
# @private
93+
#
94+
# @param (see #text_box)
95+
# @return [Hash] Returns second parameter (Options) of Prawn::Document#formatted_text_box
96+
# See http://prawnpdf.org/api-docs/2.0/Prawn/Text.html#text_box-instance_method
97+
def build_text_params(content, attrs)
98+
{}.tap do |params|
5899
params[:text] = attrs[:word_wrap] == :none ? text_without_line_wrap(content) : content
59100
params[:styles] = attrs[:styles] || []
60101
params[:size] = attrs[:size]
61102
params[:font] = attrs[:font]
62103
params[:color] = parse_color(attrs[:color])
63104
end
64-
65-
if need_bold_style_emulation?(text_params[:font], text_params[:styles])
66-
box_params[:mode] = :fill_stroke
67-
68-
emulate_bold_style(text_params[:color], text_params[:size]) do
69-
pdf.formatted_text_box([text_params], box_params)
70-
end
71-
else
72-
pdf.formatted_text_box([text_params], box_params)
73-
end
74-
75-
rescue Prawn::Errors::CannotFit
76-
# Nothing to do.
77-
#
78-
# When the area is too small compared
79-
# with the content and the style of the text.
80-
# (See prawn/core/text/formatted/line_wrap.rb#L185)
81-
end
82-
83-
# @see #text_box
84-
def text(content, x, y, w, h, attrs = {})
85-
# Set the :overflow property to :shirink_to_fit.
86-
text_box(content, x, y, w, h, { overflow: :shirink_to_fit }.merge(attrs))
87105
end
88106

89107
private

0 commit comments

Comments
 (0)