Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add line_widths to Prawn::Text::Formatted::Box #1283

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def everything_printed?
attr_reader :descender
# The leading used during printing
attr_reader :leading
# The width of each line
attr_reader :line_widths

def line_gap
line_height - (ascender + descender)
Expand Down Expand Up @@ -194,6 +196,8 @@ def initialize(formatted_text, options = {})
style: options[:style]
}

@line_widths = []

super(formatted_text, options)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/prawn/text/formatted/wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def print_line
accumulated_width += fragment_this_line.width
end

@line_widths.push(accumulated_width)

@printed_lines << printed_fragments.map do |s|
s.dup.force_encoding(::Encoding::UTF_8)
end.join
Expand Down
48 changes: 48 additions & 0 deletions spec/prawn/text/formatted/box_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,52 @@ def render_in_front(fragment); end
)
end
end

describe 'Text::Formatted::box#line_widths' do
it 'handles newlines' do
accented_char = win1252_string("\xE9"); # é in win-1252
array = [
{
text: "Oh hai t#{accented_char}xt rect.\nOh hai text rect thats longer.",
font: 'Times-Roman',
size: 12
}
]

options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render
expect(text_box.line_widths).to eq(
[
77.136,
135.804
]
)
end

it 'handles fallback fonts' do
file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
pdf.font_families['Kai'] = {
normal: { file: file, font: 'Kai' }
}
array = [
{
text: "hello你好\n再见goodbye",
font: 'Times-Roman',
size: 12
}
]

options = { document: pdf, fallback_fonts: ['Kai'] }
text_box = described_class.new(array, options)
text_box.render

expect(text_box.line_widths).to eq(
[
48.000,
65.328
]
)
end
end
end