1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
|
# frozen_string_literal: true
require 'spec_helper'
describe Prawn::Text do
describe 'NBSP' do
it 'is defined' do
expect(Prawn::Text::NBSP).to eq("\u00a0")
end
end
describe '#height_of' do
let(:pdf) { create_pdf }
it 'returns the height that would be required to print a' \
'particular string of text' do
original_y = pdf.y
pdf.text('Foo')
new_y = pdf.y
expect(pdf.height_of('Foo')).to be_within(0.0001).of(original_y - new_y)
end
it 'omits the gap below the last descender if :final_gap => false ' \
'is given' do
original_y = pdf.y
pdf.text('Foo', final_gap: false)
new_y = pdf.y
expect(pdf.height_of('Foo', final_gap: false))
.to be_within(0.0001).of(original_y - new_y)
end
it 'raise_errors CannotFit if a too-small width is given' do
expect do
pdf.height_of('text', width: 1)
end.to raise_error(Prawn::Errors::CannotFit)
end
it 'raises NotImplementedError if :indent_paragraphs option is provided' do
expect do
pdf.height_of(
'hai',
width: 300,
indent_paragraphs: 60
)
end.to raise_error(NotImplementedError)
end
it 'does not raise Prawn::Errors::UnknownOption if :final_gap option '\
'is provided' do
expect do
pdf.height_of('hai', width: 300, final_gap: true)
end.to_not raise_error
end
end
describe '#text' do
let(:pdf) { create_pdf }
it 'does not fail when @output is nil when '\
'PDF::Core::Text::LineWrap#finalize_line is called' do
# need a document with margins for these particulars to produce the
# condition that was throwing the error
pdf = Prawn::Document.new
pdf.text 'transparency ' * 150, size: 18
end
it 'allows drawing empty strings to the page' do
pdf.text ' '
text = PDF::Inspector::Text.analyze(pdf.render)
# If anything is rendered to the page, it should be whitespace.
expect(text.strings).to all(match(/\A\s*\z/))
end
it 'ignores call when string is nil' do
expect(pdf.text(nil)).to eq false
end
it 'correctlies render empty paragraphs' do
pdf.text "text\n\ntext"
text = PDF::Inspector::Text.analyze(pdf.render)
expect(pdf.page_count).to eq(1)
expect(text.strings.reject(&:empty?)).to eq(%w[text text])
end
it 'correctlies render empty paragraphs with :indent_paragraphs' do
pdf.text "text\n\ntext", indent_paragraphs: 5
text = PDF::Inspector::Text.analyze(pdf.render)
expect(pdf.page_count).to eq(1)
expect(text.strings.reject(&:empty?)).to eq(%w[text text])
end
it 'correctly renders strings ending with empty paragraphs and ' \
':inline_format and :indent_paragraphs' do
pdf.text "text\n\n", inline_format: true, indent_paragraphs: 5
text = PDF::Inspector::Text.analyze(pdf.render)
expect(pdf.page_count).to eq(1)
expect(text.strings).to eq(['text'])
end
it 'defaults to use kerning information' do
pdf.text 'hello world'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.kerned[0]).to eq true
end
it 'is able to disable kerning with an option' do
pdf.text 'hello world', kerning: false
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.kerned[0]).to eq false
end
it 'is able to disable kerning document-wide' do
pdf.default_kerning(false)
pdf.default_kerning = false
pdf.text 'hello world'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.kerned[0]).to eq false
end
it 'option should be able to override document-wide kerning disabling' do
pdf.default_kerning = false
pdf.text 'hello world', kerning: true
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.kerned[0]).to eq true
end
it 'raise_errors ArgumentError if :at option included' do
expect { pdf.text('hai', at: [0, 0]) }.to raise_error(ArgumentError)
end
it 'advances down the document based on font_height' do
position = pdf.y
pdf.text 'Foo'
expect(pdf.y).to be_within(0.0001).of(position - pdf.font.height)
position = pdf.y
pdf.text "Foo\nBar\nBaz"
expect(pdf.y).to be_within(0.0001).of(position - 3 * pdf.font.height)
end
it 'advances down the document based on font_height with size option' do
position = pdf.y
pdf.text 'Foo', size: 15
pdf.font_size = 15
expect(pdf.y).to be_within(0.0001).of(position - pdf.font.height)
position = pdf.y
pdf.text "Foo\nBar\nBaz"
expect(pdf.y).to be_within(0.0001).of(position - 3 * pdf.font.height)
end
it 'advances down the document based on font_height with leading option' do
position = pdf.y
leading = 2
pdf.text 'Foo', leading: leading
expect(pdf.y).to be_within(0.0001)
.of(position - pdf.font.height - leading)
position = pdf.y
pdf.text "Foo\nBar\nBaz"
expect(pdf.y).to be_within(0.0001).of(position - 3 * pdf.font.height)
end
it 'advances only to the bottom of the final descender if final_gap '\
'is false' do
position = pdf.y
pdf.text 'Foo', final_gap: false
expect(pdf.y).to be_within(0.0001)
.of(position - pdf.font.ascender - pdf.font.descender)
position = pdf.y
pdf.text "Foo\nBar\nBaz", final_gap: false
expect(pdf.y).to be_within(0.0001)
.of(
position - 2 * pdf.font.height - pdf.font.ascender -
pdf.font.descender
)
end
it 'is able to print text starting at the last line of a page' do
pdf.move_cursor_to(pdf.font.height)
pdf.text('hello world')
pages = PDF::Inspector::Page.analyze(pdf.render).pages
expect(pages.size).to eq(1)
end
it 'defaults to 12 point helvetica' do
pdf.text 'Blah'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:name]).to eq(:Helvetica)
expect(text.font_settings[0][:size]).to eq(12)
expect(text.strings.first).to eq('Blah')
end
it 'allows setting font size' do
pdf.text 'Blah', size: 16
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:size]).to eq(16)
end
it 'allows setting a default font size' do
pdf.font_size = 16
pdf.text 'Blah'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:size]).to eq(16)
end
it 'allows overriding default font for a single instance' do
pdf.font_size = 16
pdf.text 'Blah', size: 11
pdf.text 'Blaz'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:size]).to eq(11)
expect(text.font_settings[1][:size]).to eq(16)
end
it 'allows setting a font size transaction with a block' do
pdf.font_size 16 do
pdf.text 'Blah'
end
pdf.text 'blah'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:size]).to eq(16)
expect(text.font_settings[1][:size]).to eq(12)
end
it 'allows manual setting the font size when in a font size block' do
pdf.font_size(16) do
pdf.text 'Foo'
pdf.text 'Blah', size: 11
pdf.text 'Blaz'
end
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:size]).to eq(16)
expect(text.font_settings[1][:size]).to eq(11)
expect(text.font_settings[2][:size]).to eq(16)
end
it 'allows registering of built-in font_settings on the fly' do
pdf.font 'Times-Roman'
pdf.text 'Blah'
pdf.font 'Courier'
pdf.text 'Blaz'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:name]).to eq(:"Times-Roman")
expect(text.font_settings[1][:name]).to eq(:Courier)
end
it 'utilises the same default font across multiple pages' do
pdf.text 'Blah'
pdf.start_new_page
pdf.text 'Blaz'
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings.size).to eq(2)
expect(text.font_settings[0][:name]).to eq(:Helvetica)
expect(text.font_settings[1][:name]).to eq(:Helvetica)
end
it 'raise_errors an exception when an unknown font is used' do
expect { pdf.font 'Pao bu' }.to raise_error(Prawn::Errors::UnknownFont)
end
it 'does not raise an exception when providing Pathname instance as font' do
pdf.font Pathname.new("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf")
end
it 'correctlies render a utf-8 string when using a built-in font' do
str = '©' # copyright symbol
pdf.text str
# grab the text from the rendered PDF and ensure it matches
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings.first).to eq(str)
end
it 'correctlies render a utf-8 string when using a TTF font' do
str = '©' # copyright symbol
pdf.font "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
pdf.text str
# grab the text from the rendered PDF and ensure it matches
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings.first).to eq(str)
end
it 'subsets mixed low-ASCII and non-ASCII characters when they can '\
'be subsetted together' do
str = 'It’s super effective!'
pdf.font "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
pdf.text str
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings.first).to eq(str)
end
it 'correctly renders a string with higher bit characters across a page '\
'break when using a built-in font' do
str = '©'
pdf.move_cursor_to(pdf.font.height)
pdf.text("#{str}\n#{str}")
pages = PDF::Inspector::Page.analyze(pdf.render).pages
expect(pages.size).to eq(2)
expect(pages[0][:strings]).to eq([str])
expect(pages[1][:strings]).to eq([str])
end
it 'correctly renders a string with higher bit characters across ' \
'a page break when using a built-in font and :indent_paragraphs option' do
str = '©'
pdf.move_cursor_to(pdf.font.height)
pdf.text("#{str}\n#{str}", indent_paragraphs: 20)
pages = PDF::Inspector::Page.analyze(pdf.render).pages
expect(pages.size).to eq(2)
expect(pages[0][:strings]).to eq([str])
expect(pages[1][:strings]).to eq([str])
end
it 'raises an exception when a utf-8 incompatible string is rendered' do
str = "Blah \xDD"
expect { pdf.text str }.to raise_error(
Prawn::Errors::IncompatibleStringEncoding
)
end
it 'does not raise an exception when a shift-jis string is rendered' do
datafile = "#{Prawn::DATADIR}/shift_jis_text.txt"
sjis_str = File.open(datafile, 'r:shift_jis', &:gets)
pdf.font("/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf")
# Expect that the call to text will not raise an encoding error
pdf.text(sjis_str)
end
it 'calls move_past_bottom when printing more text than can fit' \
' between the current document.y and bounds.bottom' do
pdf.y = pdf.font.height
pdf.text 'Hello'
pdf.text 'World'
pages = PDF::Inspector::Page.analyze(pdf.render).pages
expect(pages.size).to eq(2)
expect(pages[0][:strings]).to eq(['Hello'])
expect(pages[1][:strings]).to eq(['World'])
end
describe 'with :indent_paragraphs option' do
it 'indents the paragraphs' do
hello = 'hello ' * 50
hello2 = 'hello ' * 50
pdf.text("#{hello}\n#{hello2}", indent_paragraphs: 60)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq(('hello ' * 19).strip)
expect(text.strings[1]).to eq(('hello ' * 21).strip)
expect(text.strings[3]).to eq(('hello ' * 19).strip)
expect(text.strings[4]).to eq(('hello ' * 21).strip)
end
it 'indents from right side when using :rtl direction' do
para1 = 'The rain in spain falls mainly on the plains ' * 3
para2 = 'The rain in spain falls mainly on the plains ' * 3
pdf.text("#{para1}\n#{para2}", indent_paragraphs: 60, direction: :rtl)
text = PDF::Inspector::Text.analyze(pdf.render)
lines = text.strings
x_positions = text.positions.map { |e| e[0] }
# NOTE: The code below reflects Prawn's current kerning behavior for RTL
# text, which isn't necessarily correct. If we change that behavior,
# this test will need to be updated.
expect(x_positions[0]).to(
be_within(0.001).of(pdf.bounds.absolute_right - 60 -
pdf.width_of(lines[0].reverse, kerning: true))
)
expect(x_positions[1]).to(
be_within(0.001).of(pdf.bounds.absolute_right -
pdf.width_of(lines[1].reverse, kerning: true))
)
expect(x_positions[2]).to(
be_within(0.001).of(pdf.bounds.absolute_right - 60 -
pdf.width_of(lines[2].reverse, kerning: true))
)
expect(x_positions[3]).to(
be_within(0.001).of(pdf.bounds.absolute_right -
pdf.width_of(lines[3].reverse, kerning: true))
)
end
it 'indents from right side when document has :rtl direction' do
para1 = 'The rain in spain falls mainly on the plains ' * 3
para2 = 'The rain in spain falls mainly on the plains ' * 3
pdf.text_direction = :rtl
pdf.text("#{para1}\n#{para2}", indent_paragraphs: 60)
text = PDF::Inspector::Text.analyze(pdf.render)
lines = text.strings
x_positions = text.positions.map { |e| e[0] }
# NOTE: The code below reflects Prawn's current kerning behavior for RTL
# text, which isn't necessarily correct. If we change that behavior,
# this test will need to be updated.
expect(x_positions[0]).to(
be_within(0.001).of(pdf.bounds.absolute_right - 60 -
pdf.width_of(lines[0].reverse, kerning: true))
)
expect(x_positions[1]).to(
be_within(0.001).of(pdf.bounds.absolute_right -
pdf.width_of(lines[1].reverse, kerning: true))
)
expect(x_positions[2]).to(
be_within(0.001).of(pdf.bounds.absolute_right - 60 -
pdf.width_of(lines[2].reverse, kerning: true))
)
expect(x_positions[3]).to(
be_within(0.001).of(pdf.bounds.absolute_right -
pdf.width_of(lines[3].reverse, kerning: true))
)
end
it 'indents from left side when using :ltr direction' do
para1 = 'The rain in spain falls mainly on the plains ' * 3
para2 = 'The rain in spain falls mainly on the plains ' * 3
pdf.text("#{para1}\n#{para2}", indent_paragraphs: 60, direction: :ltr)
text = PDF::Inspector::Text.analyze(pdf.render)
x_positions = text.positions.map { |e| e[0] }
expect(x_positions[0]).to eq(60)
expect(x_positions[1]).to eq(0)
expect(x_positions[2]).to eq(60)
expect(x_positions[3]).to eq(0)
end
it 'indents from left side when document has :ltr direction' do
para1 = 'The rain in spain falls mainly on the plains ' * 3
para2 = 'The rain in spain falls mainly on the plains ' * 3
pdf.text_direction = :ltr
pdf.text("#{para1}\n#{para2}", indent_paragraphs: 60)
text = PDF::Inspector::Text.analyze(pdf.render)
x_positions = text.positions.map { |e| e[0] }
expect(x_positions[0]).to eq(60)
expect(x_positions[1]).to eq(0)
expect(x_positions[2]).to eq(60)
expect(x_positions[3]).to eq(0)
end
describe 'when paragraph has only one line, it should not add ' \
'additional leading' do
let(:leading) { 100 }
it 'adds leading only once' do
original_y = pdf.y
pdf.text('hello', indent_paragraphs: 10, leading: leading)
expect(original_y - pdf.y).to be < leading * 2
end
end
describe 'when wrap to new page, and first line of new page' \
' is not the start of a new paragraph, that line should' \
' not be indented' do
it 'indents the paragraphs' do
hello = 'hello ' * 50
hello2 = 'hello ' * 50
pdf.move_cursor_to(pdf.font.height)
pdf.text("#{hello}\n#{hello2}", indent_paragraphs: 60)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq(('hello ' * 19).strip)
expect(text.strings[1]).to eq(('hello ' * 21).strip)
expect(text.strings[3]).to eq(('hello ' * 19).strip)
expect(text.strings[4]).to eq(('hello ' * 21).strip)
end
end
describe 'when wrap to new page, and first line of new page' \
' is the start of a new paragraph, that line should' \
' be indented' do
it 'indents the paragraphs' do
hello = 'hello ' * 50
hello2 = 'hello ' * 50
pdf.move_cursor_to(pdf.font.height * 3)
pdf.text("#{hello}\n#{hello2}", indent_paragraphs: 60)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq(('hello ' * 19).strip)
expect(text.strings[1]).to eq(('hello ' * 21).strip)
expect(text.strings[3]).to eq(('hello ' * 19).strip)
expect(text.strings[4]).to eq(('hello ' * 21).strip)
end
end
end
describe 'kerning' do
it 'respects text kerning setting (document default)' do
allow(pdf.font).to receive(:compute_width_of)
.with('VAT', hash_including(kerning: true))
.and_return(10)
pdf.text 'VAT'
expect(pdf.font).to have_received(:compute_width_of)
.with('VAT', hash_including(kerning: true))
end
it 'respects text kerning setting (kerning=true)' do
allow(pdf.font).to receive(:compute_width_of)
.with('VAT', hash_including(kerning: true))
.at_least(:once)
.and_return(10)
pdf.text 'VAT', kerning: true
expect(pdf.font).to have_received(:compute_width_of)
.with('VAT', hash_including(kerning: true))
.at_least(:once)
end
it 'respects text kerning setting (kerning=false)' do
allow(pdf.font).to receive(:compute_width_of)
.with('VAT', hash_including(kerning: false))
.at_least(:once)
.and_return(10)
pdf.text 'VAT', kerning: false
expect(pdf.font).to have_received(:compute_width_of)
.with('VAT', hash_including(kerning: false))
.at_least(:once)
end
end
describe '#shrink_to_fit with special utf-8 text' do
it 'does not throw an exception', :unresolved, issue: 603 do
expect do
Prawn::Document.new(page_size: 'A4', margin: [2, 2, 2, 2]) do |pdf|
add_unicode_fonts(pdf)
pdf.bounding_box([1, 1], width: 90, height: 50) do
pdf.text(
"Sample Text\nSAMPLE SAMPLE SAMPLEoddělení ZMĚN\nSAMPLE",
overflow: :shrink_to_fit
)
end
end
end.to_not raise_error
end
end
def add_unicode_fonts(pdf)
dejavu = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
pdf.font_families.update(
'dejavu' => {
normal: dejavu,
italic: dejavu,
bold: dejavu,
bold_italic: dejavu
}
)
pdf.fallback_fonts = ['dejavu']
end
describe 'fallback_fonts' do
it 'preserves font style' do
create_pdf
pdf.fallback_fonts ['Helvetica']
pdf.font 'Times-Roman', style: :italic do
pdf.text 'hello'
end
text = PDF::Inspector::Text.analyze(pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
expect(fonts_used.length).to eq(1)
expect(fonts_used[0]).to eq(:"Times-Italic")
expect(text.strings[0]).to eq('hello')
end
end
end
end
|