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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
|
# frozen_string_literal: true
require 'spec_helper'
describe Prawn::Text::Formatted::Box do
let(:pdf) { create_pdf }
describe 'wrapping' do
it 'does not wrap between two fragments' do
texts = [
{ text: 'Hello ' },
{ text: 'World' },
{ text: '2', styles: [:superscript] }
]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Hello World')
)
text_box.render
expect(text_box.text).to eq("Hello\nWorld2")
end
it 'does not raise an Encoding::CompatibilityError when keeping a TTF and '\
'an AFM font together' do
file = "/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf"
pdf.font_families['Kai'] = {
normal: { file: file, font: 'Kai' }
}
texts = [
{ text: 'Hello ' },
{ text: '再见', font: 'Kai' },
{ text: 'World' }
]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Hello World')
)
text_box.render
end
it 'wraps between two fragments when the preceding fragment ends with '\
'a white space' do
texts = [
{ text: 'Hello ' },
{ text: 'World ' },
{ text: '2', styles: [:superscript] }
]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Hello World')
)
text_box.render
expect(text_box.text).to eq("Hello World\n2")
texts = [
{ text: 'Hello ' },
{ text: "World\n" },
{ text: '2', styles: [:superscript] }
]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Hello World')
)
text_box.render
expect(text_box.text).to eq("Hello World\n2")
end
it 'wraps between two fragments when the final fragment begins with '\
'a white space' do
texts = [
{ text: 'Hello ' },
{ text: 'World' },
{ text: ' 2', styles: [:superscript] }
]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Hello World')
)
text_box.render
expect(text_box.text).to eq("Hello World\n2")
texts = [
{ text: 'Hello ' },
{ text: 'World' },
{ text: "\n2", styles: [:superscript] }
]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Hello World')
)
text_box.render
expect(text_box.text).to eq("Hello World\n2")
end
it 'properlies handle empty slices using default encoding' do
texts = [{
text: 'Noua Delineatio Geographica generalis | Apostolicarum ' \
'peregrinationum | S FRANCISCI XAUERII | Indiarum & Iaponiæ Apostoli',
font: 'Courier',
size: 10
}]
text_box = described_class.new(
texts,
document: pdf,
width: pdf.width_of('Noua Delineatio Geographica gen')
)
expect do
text_box.render
end.to_not raise_error
expect(text_box.text).to eq(
"Noua Delineatio Geographica\ngeneralis | Apostolicarum\n" \
"peregrinationum | S FRANCISCI\nXAUERII | Indiarum & Iaponi\346\n" \
'Apostoli'
)
end
end
describe 'Text::Formatted::Box with :fallback_fonts option that includes' \
'a Chinese font and set of Chinese glyphs not in the current font' do
it 'changes the font to the Chinese font for the Chinese glyphs' do
file = "/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf"
pdf.font_families['Kai'] = {
normal: { file: file, font: 'Kai' }
}
formatted_text = [
{ text: 'hello你好' },
{ text: '再见goodbye' }
]
pdf.formatted_text_box(formatted_text, fallback_fonts: ['Kai'])
text = PDF::Inspector::Text.analyze(pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
expect(fonts_used.length).to eq(4)
expect(fonts_used[0]).to eq(:Helvetica)
expect(fonts_used[1].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[2].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[3]).to eq(:Helvetica)
expect(text.strings[0]).to eq('hello')
expect(text.strings[1]).to eq('你好')
expect(text.strings[2]).to eq('再见')
expect(text.strings[3]).to eq('goodbye')
end
end
describe 'Text::Formatted::Box with :fallback_fonts option that includes' \
'an AFM font and Win-Ansi glyph not in the current Chinese font' do
it 'changes the font to the AFM font for the Win-Ansi glyph' do
file = "/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf"
pdf.font_families['Kai'] = {
normal: { file: file, font: 'Kai' }
}
pdf.font('Kai')
formatted_text = [
{ text: 'hello你好' },
{ text: '再见€' }
]
pdf.formatted_text_box(formatted_text, fallback_fonts: ['Helvetica'])
text = PDF::Inspector::Text.analyze(pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
expect(fonts_used.length).to eq(4)
expect(fonts_used[0].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[1].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[2].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[3]).to eq(:Helvetica)
expect(text.strings[0]).to eq('hello')
expect(text.strings[1]).to eq('你好')
expect(text.strings[2]).to eq('再见')
expect(text.strings[3]).to eq('€')
end
end
describe 'Text::Formatted::Box with :fallback_fonts option and fragment ' \
'level font' do
it 'uses the fragment level font except for glyphs not in that font' do
file = "/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf"
pdf.font_families['Kai'] = {
normal: { file: file, font: 'Kai' }
}
file = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
pdf.font_families['DejaVu Sans'] = {
normal: { file: file }
}
formatted_text = [
{ text: 'hello你好' },
{ text: '再见goodbye', font: 'Times-Roman' }
]
pdf.formatted_text_box(formatted_text, fallback_fonts: ['Kai'])
text = PDF::Inspector::Text.analyze(pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
expect(fonts_used.length).to eq(4)
expect(fonts_used[0]).to eq(:Helvetica)
expect(fonts_used[1].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[2].to_s).to match(/GBZenKai-Medium/)
expect(fonts_used[3]).to eq(:"Times-Roman")
expect(text.strings[0]).to eq('hello')
expect(text.strings[1]).to eq('你好')
expect(text.strings[2]).to eq('再见')
expect(text.strings[3]).to eq('goodbye')
end
end
describe 'Text::Formatted::Box' do
let(:formatted_text) { [{ text: 'hello你好' }] }
before do
file = "/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf"
pdf.font_families['Kai'] = {
normal: { file: file, font: 'Kai' }
}
file = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
pdf.font_families['DejaVu Sans'] = {
normal: { file: file }
}
pdf.fallback_fonts(['Kai'])
pdf.fallback_fonts = ['Kai']
end
it '#fallback_fonts should return the document-wide fallback fonts' do
expect(pdf.fallback_fonts).to eq(['Kai'])
end
it 'is able to set text fallback_fonts document-wide' do
pdf.formatted_text_box(formatted_text)
text = PDF::Inspector::Text.analyze(pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
expect(fonts_used.length).to eq(2)
expect(fonts_used[0]).to eq(:Helvetica)
expect(fonts_used[1].to_s).to match(/GBZenKai-Medium/)
end
it 'is able to override document-wide fallback_fonts' do
pdf.fallback_fonts = ['DejaVu Sans']
pdf.formatted_text_box(formatted_text, fallback_fonts: ['Kai'])
text = PDF::Inspector::Text.analyze(pdf.render)
fonts_used = text.font_settings.map { |e| e[:name] }
expect(fonts_used.length).to eq(2)
expect(fonts_used[0]).to eq(:Helvetica)
expect(fonts_used[1]).to match(/Kai/)
end
it 'omits the fallback fonts overhead when passing an empty array ' \
'as the :fallback_fonts' do
pdf.font('Kai')
box = described_class.new(
formatted_text,
document: pdf,
fallback_fonts: []
)
allow(box).to receive(:process_fallback_fonts)
box.render
expect(box).to_not have_received(:process_fallback_fonts)
end
it 'is able to clear document-wide fallback_fonts' do
pdf.fallback_fonts([])
box = described_class.new(formatted_text, document: pdf)
pdf.font('Kai')
allow(box).to receive(:process_fallback_fonts)
box.render
expect(box).to_not have_received(:process_fallback_fonts)
end
end
describe 'Text::Formatted::Box with :fallback_fonts option ' \
'with glyphs not in the primary or the fallback fonts' do
it 'raises an exception' do
formatted_text = [{ text: 'hello world. 世界你好。' }]
expect do
pdf.formatted_text_box(formatted_text, fallback_fonts: ['Courier'])
end.to raise_error(Prawn::Errors::IncompatibleStringEncoding)
end
end
describe 'Text::Formatted::Box#extensions' do
let(:formatted_wrap_override) do
Module.new do
# rubocop: disable RSpec/InstanceVariable
def wrap(_array)
initialize_wrap([{ text: 'all your base are belong to us' }])
@line_wrap.wrap_line(
document: @document,
kerning: @kerning,
width: 10_000,
arranger: @arranger
)
fragment = @arranger.retrieve_fragment
format_and_draw_fragment(fragment, 0, @line_wrap.width, 0)
[]
end
# rubocop: enable RSpec/InstanceVariable
end
end
it 'is able to override default line wrapping' do
described_class.extensions << formatted_wrap_override
pdf.formatted_text_box([{ text: 'hello world' }], {})
described_class.extensions.delete(formatted_wrap_override)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('all your base are belong to us')
end
it 'overrides Text::Formatted::Box line wrapping does not affect ' \
'Text::Box wrapping' do
described_class.extensions << formatted_wrap_override
pdf.text_box('hello world', {})
described_class.extensions.delete(formatted_wrap_override)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('hello world')
end
it "overring Text::Box line wrapping doesn't override Text::Box wrapping" do
Prawn::Text::Box.extensions << formatted_wrap_override
pdf.text_box('hello world', {})
Prawn::Text::Box.extensions.delete(formatted_wrap_override)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('all your base are belong to us')
end
end
describe 'Text::Formatted::Box#render' do
let(:fragment_callback_class) do
Class.new do
def initialize(_string, _number, _options); end
def render_behind(fragment); end
def render_in_front(fragment); end
end
end
it 'handles newlines' do
array = [{ text: "hello\nworld" }]
options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render
expect(text_box.text).to eq("hello\nworld")
end
it 'omits spaces from the beginning of the line' do
array = [{ text: " hello\n world" }]
options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render
expect(text_box.text).to eq("hello\nworld")
end
it 'is okay printing a line of whitespace' do
array = [{ text: "hello\n \nworld" }]
options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render
expect(text_box.text).to eq("hello\n\nworld")
array = [
{ text: "hello#{' ' * 500}" },
{ text: ' ' * 500 },
{ text: "#{' ' * 500}\n" },
{ text: 'world' }
]
options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render
expect(text_box.text).to eq("hello\n\nworld")
end
it 'enables fragment level direction setting' do
number_of_hellos = 18
array = [
{ text: 'hello ' * number_of_hellos },
{ text: 'world', direction: :ltr },
{ text: ', how are you?' }
]
options = { document: pdf, direction: :rtl }
text_box = described_class.new(array, options)
text_box.render
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('era woh ,')
expect(text.strings[1]).to eq('world')
expect(text.strings[2]).to eq(' olleh' * number_of_hellos)
expect(text.strings[3]).to eq('?uoy')
end
it 'is able to perform fragment callbacks' do
callback_object =
fragment_callback_class.new('something', 7, document: pdf)
allow(callback_object).to receive(:render_behind)
allow(callback_object).to receive(:render_in_front)
array = [
{ text: 'hello world ' },
{ text: 'callback now', callback: callback_object }
]
text_box = described_class.new(array, document: pdf)
text_box.render
expect(callback_object).to have_received(:render_behind).with(
kind_of(Prawn::Text::Formatted::Fragment)
)
expect(callback_object).to have_received(:render_in_front).with(
kind_of(Prawn::Text::Formatted::Fragment)
)
end
it 'is able to perform fragment callbacks on multiple objects' do
callback_object =
fragment_callback_class.new('something', 7, document: pdf)
allow(callback_object).to receive(:render_behind)
allow(callback_object).to receive(:render_in_front)
callback_object2 = fragment_callback_class.new(
'something else', 14, document: pdf
)
allow(callback_object2).to receive(:render_behind)
allow(callback_object2).to receive(:render_in_front)
array = [
{ text: 'hello world ' },
{ text: 'callback now', callback: [callback_object, callback_object2] }
]
text_box = described_class.new(array, document: pdf)
text_box.render
expect(callback_object).to have_received(:render_behind).with(
kind_of(Prawn::Text::Formatted::Fragment)
)
expect(callback_object).to have_received(:render_in_front).with(
kind_of(Prawn::Text::Formatted::Fragment)
)
expect(callback_object2).to have_received(:render_behind).with(
kind_of(Prawn::Text::Formatted::Fragment)
)
expect(callback_object2).to have_received(:render_in_front).with(
kind_of(Prawn::Text::Formatted::Fragment)
)
end
it 'fragment callbacks is able to define only the callback they need' do
behind = (
Class.new do
def initialize(_string, _number, _options); end
def render_behind(fragment); end
end
).new(
'something',
7,
document: pdf
)
in_front = (
Class.new do
def initialize(_string, _number, _options); end
def render_in_front(fragment); end
end
).new(
'something',
7,
document: pdf
)
array = [
{ text: 'hello world ' },
{ text: 'callback now', callback: [behind, in_front] }
]
text_box = described_class.new(array, document: pdf)
text_box.render # trigger callbacks
end
it 'is able to set the font' do
array = [
{ text: 'this contains ' },
{
text: 'Times-Bold',
styles: [:bold],
font: 'Times-Roman'
},
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
fonts = contents.font_settings.map { |e| e[:name] }
expect(fonts).to eq(%i[Helvetica Times-Bold Helvetica])
expect(contents.strings[0]).to eq('this contains ')
expect(contents.strings[1]).to eq('Times-Bold')
expect(contents.strings[2]).to eq(' text')
end
it 'is able to set bold' do
array = [
{ text: 'this contains ' },
{ text: 'bold', styles: [:bold] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
fonts = contents.font_settings.map { |e| e[:name] }
expect(fonts).to eq(%i[Helvetica Helvetica-Bold Helvetica])
expect(contents.strings[0]).to eq('this contains ')
expect(contents.strings[1]).to eq('bold')
expect(contents.strings[2]).to eq(' text')
end
it 'is able to set italics' do
array = [
{ text: 'this contains ' },
{ text: 'italic', styles: [:italic] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
fonts = contents.font_settings.map { |e| e[:name] }
expect(fonts).to eq(%i[Helvetica Helvetica-Oblique Helvetica])
end
it 'is able to set subscript' do
array = [
{ text: 'this contains ' },
{ text: 'subscript', size: 18, styles: [:subscript] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.font_settings[0][:size]).to eq(12)
expect(contents.font_settings[1][:size])
.to be_within(0.0001).of(18 * 0.583)
end
it 'is able to set superscript' do
array = [
{ text: 'this contains ' },
{ text: 'superscript', size: 18, styles: [:superscript] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.font_settings[0][:size]).to eq(12)
expect(contents.font_settings[1][:size])
.to be_within(0.0001).of(18 * 0.583)
end
it 'is able to set compound bold and italic text' do
array = [
{ text: 'this contains ' },
{ text: 'bold italic', styles: %i[bold italic] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
fonts = contents.font_settings.map { |e| e[:name] }
expect(fonts).to eq(%i[Helvetica Helvetica-BoldOblique Helvetica])
end
it 'is able to underline' do
array = [
{ text: 'this contains ' },
{ text: 'underlined', styles: [:underline] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
line_drawing = PDF::Inspector::Graphics::Line.analyze(pdf.render)
expect(line_drawing.points.length).to eq(2)
end
it 'is able to strikethrough' do
array = [
{ text: 'this contains ' },
{ text: 'struckthrough', styles: [:strikethrough] },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
line_drawing = PDF::Inspector::Graphics::Line.analyze(pdf.render)
expect(line_drawing.points.length).to eq(2)
end
it 'is able to add URL links' do
allow(pdf).to receive(:link_annotation)
array = [
{ text: 'click ' },
{ text: 'here', link: 'http://example.com' },
{ text: ' to visit' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
expect(pdf).to have_received(:link_annotation).with(
kind_of(Array),
Border: [0, 0, 0],
A: {
Type: :Action,
S: :URI,
URI: 'http://example.com'
}
)
end
it 'is able to add destination links' do
allow(pdf).to receive(:link_annotation)
array = [
{ text: 'Go to the ' },
{ text: 'Table of Contents', anchor: 'ToC' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
expect(pdf).to have_received(:link_annotation).with(
kind_of(Array),
Border: [0, 0, 0],
Dest: 'ToC'
)
end
it 'is able to add local actions' do
allow(pdf).to receive(:link_annotation)
array = [
{ text: 'click ' },
{ text: 'here', local: '../example.pdf' },
{ text: ' to open a local file' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
expect(pdf).to have_received(:link_annotation).with(
kind_of(Array),
Border: [0, 0, 0],
A: {
Type: :Action,
S: :Launch,
F: '../example.pdf',
NewWindow: true
}
)
end
it 'is able to set font size' do
array = [
{ text: 'this contains ' },
{ text: 'sized', size: 24 },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.font_settings[0][:size]).to eq(12)
expect(contents.font_settings[1][:size]).to eq(24)
end
it 'sets the baseline based on the tallest fragment on a given line' do
array = [
{ text: 'this contains ' },
{ text: 'sized', size: 24 },
{ text: ' text' }
]
text_box = described_class.new(array, document: pdf)
text_box.render
pdf.font_size(24) do
expect(text_box.height).to be_within(0.001)
.of(pdf.font.ascender + pdf.font.descender)
end
end
it 'is able to set color via an rgb hex string' do
array = [{
text: 'rgb',
color: 'ff0000'
}]
text_box = described_class.new(array, document: pdf)
text_box.render
colors = PDF::Inspector::Graphics::Color.analyze(pdf.render)
expect(colors.fill_color_count).to eq(2)
expect(colors.stroke_color_count).to eq(2)
end
it 'is able to set color using a cmyk array' do
array = [{
text: 'cmyk',
color: [100, 0, 0, 0]
}]
text_box = described_class.new(array, document: pdf)
text_box.render
colors = PDF::Inspector::Graphics::Color.analyze(pdf.render)
expect(colors.fill_color_count).to eq(2)
expect(colors.stroke_color_count).to eq(2)
end
end
describe 'Text::Formatted::Box#render(:dry_run => true)' do
it 'does not change the graphics state of the document' do
state_before = PDF::Inspector::Graphics::Color.analyze(pdf.render)
fill_color_count = state_before.fill_color_count
stroke_color_count = state_before.stroke_color_count
stroke_color_space_count = state_before.stroke_color_space_count
array = [{
text: 'Foo',
color: [0, 0, 0, 100]
}]
options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render(dry_run: true)
state_after = PDF::Inspector::Graphics::Color.analyze(pdf.render)
expect(state_after.fill_color_count).to eq(fill_color_count)
expect(state_after.stroke_color_count).to eq(stroke_color_count)
expect(state_after.stroke_color_space_count)
.to eq(stroke_color_space_count)
end
end
describe 'Text::Formatted::Box#render with fragment level '\
':character_spacing option' do
it 'draws the character spacing to the document' do
array = [{
text: 'hello world',
character_spacing: 7
}]
options = { document: pdf }
text_box = described_class.new(array, options)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.character_spacing[0]).to eq(7)
end
it 'lays out text properly' do
array = [{
text: 'hello world',
font: 'Courier',
character_spacing: 10
}]
options = {
document: pdf,
width: 100,
overflow: :expand
}
text_box = described_class.new(array, options)
text_box.render
expect(text_box.text).to eq("hello\nworld")
end
end
describe 'Text::Formatted::Box#render with :align => :justify' do
it 'does not justify the last line of a paragraph' do
array = [
{ text: 'hello world ' },
{ text: "\n" },
{ text: 'goodbye' }
]
options = { document: pdf, align: :justify }
text_box = described_class.new(array, options)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.word_spacing).to be_empty
end
it 'raise an exception when align value is not a symbol' do
array = [
{ text: 'hello world ' },
{ text: "\n" },
{ text: 'goodbye' }
]
options = { document: pdf, align: 'justify' }
text_box = described_class.new(array, options)
expect { text_box.render }.to raise_error(
ArgumentError,
'align must be one of :left, :right, :center or :justify symbols'
)
end
end
describe 'Text::Formatted::Box#render with :valign => :center' do
it 'has a bottom gap equal to baseline and bottom of box' do
box_height = 100
y = 450
array = [{ text: 'Vertical Align' }]
options = {
document: pdf,
valign: :center,
at: [0, y],
width: 100,
height: box_height,
size: 16
}
text_box = described_class.new(array, options)
text_box.render
line_padding = (box_height - text_box.height + text_box.descender) * 0.5
baseline = y - line_padding
expect(text_box.at[1]).to be_within(0.01).of(baseline)
end
end
describe 'Text::Formatted::Box#render with :valign => :bottom' do
it 'does not render a gap between the text and bottom of box' do
box_height = 100
y = 450
array = [{ text: 'Vertical Align' }]
options = {
document: pdf,
valign: :bottom,
at: [0, y],
width: 100,
height: box_height,
size: 16
}
text_box = described_class.new(array, options)
text_box.render
top_padding = y - (box_height - text_box.height)
expect(text_box.at[1]).to be_within(0.01).of(top_padding)
end
end
end
|