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 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
|
# frozen_string_literal: true
require 'spec_helper'
describe Prawn::Text::Box do
let(:pdf) { create_pdf }
it 'is able to set leading document-wide' do
pdf.default_leading(7)
pdf.default_leading = 7
text_box = described_class.new('hello world', document: pdf)
expect(text_box.leading).to eq(7)
end
it 'option should be able to override document-wide leading' do
pdf.default_leading = 7
text_box = described_class.new(
'hello world',
document: pdf,
leading: 20
)
expect(text_box.leading).to eq(20)
end
it 'is able to set text direction document-wide' do
pdf.text_direction(:rtl)
pdf.text_direction = :rtl
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(string, document: pdf)
text_box.render
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('?uoy era woh ,dlrow olleH')
expect(text.strings[1]).to eq(".uoy knaht ,enif m'I")
end
it 'is able to reverse multi-byte text' do
pdf.text_direction(:rtl)
pdf.text_direction = :rtl
pdf.text_direction = :rtl
pdf.font("/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf", size: 16) do
pdf.text '写个小'
end
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('小个写')
end
it 'option should be able to override document-wide text direction' do
pdf.text_direction = :rtl
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(
string,
document: pdf,
direction: :ltr
)
text_box.render
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings[0]).to eq('Hello world, how are you?')
expect(text.strings[1]).to eq("I'm fine, thank you.")
end
it 'only requires enough space for the descender and the ascender '\
'when determining whether a line can fit' do
text = 'Oh hai text rect'
options = {
document: pdf,
height: pdf.font.ascender + pdf.font.descender
}
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to eq('Oh hai text rect')
text = "Oh hai text rect\nOh hai text rect"
options = {
document: pdf,
height: pdf.font.height + pdf.font.ascender + pdf.font.descender
}
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to eq("Oh hai text rect\nOh hai text rect")
end
describe '#nothing_printed?' do
it 'returns true when nothing printed' do
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(string, height: 2, document: pdf)
text_box.render
expect(text_box.nothing_printed?).to eq true
end
it 'returns false when something printed' do
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(string, height: 14, document: pdf)
text_box.render
expect(text_box.nothing_printed?).to eq false
end
end
describe '#everything_printed?' do
it 'returns false when not everything printed' do
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(string, height: 14, document: pdf)
text_box.render
expect(text_box.everything_printed?).to eq false
end
it 'returns true when everything printed' do
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(string, document: pdf)
text_box.render
expect(text_box.everything_printed?).to eq true
end
end
describe '#line_gap' do
it '==S the line gap of the font when using a single font and font size' do
string = "Hello world, how are you?\nI'm fine, thank you."
text_box = described_class.new(string, document: pdf)
text_box.render
expect(text_box.line_gap).to be_within(0.0001).of(pdf.font.line_gap)
end
end
describe '#render with :align => :justify' do
it 'draws the word spacing to the document' do
string = 'hello world ' * 20
options = { document: pdf, align: :justify }
text_box = described_class.new(string, options)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.word_spacing[0]).to be > 0
end
it 'does not justify the last line of a paragraph' do
string = 'hello world '
options = { document: pdf, align: :justify }
text_box = described_class.new(string, options)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.word_spacing).to be_empty
end
end
describe '#height without leading' do
it 'is the sum of the height of each line, not including the space below '\
'the last line' do
text = "Oh hai text rect.\nOh hai text rect."
options = { document: pdf }
text_box = described_class.new(text, options)
text_box.render
expect(text_box.height).to be_within(0.001)
.of(pdf.font.height * 2 - pdf.font.line_gap)
end
end
describe '#height with leading' do
it 'is the sum of the height of each line plus leading, but not including '\
'the space below the last line' do
text = "Oh hai text rect.\nOh hai text rect."
leading = 12
options = { document: pdf, leading: leading }
text_box = described_class.new(text, options)
text_box.render
expect(text_box.height).to be_within(0.001).of(
(pdf.font.height + leading) * 2 - pdf.font.line_gap - leading
)
end
end
context 'with :draw_text_callback' do
it 'hits the callback whenever text is drawn' do
draw_block = instance_spy('Draw block')
pdf.text_box 'this text is long enough to span two lines',
width: 150,
draw_text_callback: ->(text, _) { draw_block.kick(text) }
expect(draw_block).to have_received(:kick)
.with('this text is long enough to')
expect(draw_block).to have_received(:kick).with('span two lines')
end
it 'hits the callback once per fragment for :inline_format' do
draw_block = instance_spy('Draw block')
pdf.text_box 'this text has <b>fancy</b> formatting',
inline_format: true,
width: 500,
draw_text_callback: ->(text, _) { draw_block.kick(text) }
expect(draw_block).to have_received(:kick).with('this text has ')
expect(draw_block).to have_received(:kick).with('fancy')
expect(draw_block).to have_received(:kick).with(' formatting')
end
it 'does not call #draw_text!' do
allow(pdf).to receive(:draw_text!)
pdf.text_box 'some text',
width: 500,
draw_text_callback: ->(_, _) {}
expect(pdf).to_not have_received(:draw_text!)
end
end
describe '#valid_options' do
it 'returns an array' do
text_box = described_class.new('', document: pdf)
expect(text_box.valid_options).to be_a_kind_of(Array)
end
end
describe '#render' do
it 'does not fail if height is smaller than 1 line' do
text = 'Oh hai text rect. ' * 10
options = {
height: pdf.font.height * 0.5,
document: pdf
}
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to eq('')
end
it 'draws content to the page' do
text = 'Oh hai text rect. ' * 10
options = { document: pdf }
text_box = described_class.new(text, options)
text_box.render
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
it 'does not draw a transformation matrix' do
text = 'Oh hai text rect. ' * 10
options = { document: pdf }
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
expect(matrices.matrices.length).to eq(0)
end
end
describe '#render(:single_line => true)' do
it 'draws only one line to the page' do
text = 'Oh hai text rect. ' * 10
options = {
document: pdf,
single_line: true
}
text_box = described_class.new(text, options)
text_box.render
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings.length).to eq(1)
end
end
describe '#render(:dry_run => true)' do
it 'does not draw any content to the page' do
text = 'Oh hai text rect. ' * 10
options = { document: pdf }
text_box = described_class.new(text, options)
text_box.render(dry_run: true)
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to be_empty
end
it 'subsequent calls to render do not raise an ArgumentError exception' do
text = '™©'
options = { document: pdf }
text_box = described_class.new(text, options)
text_box.render(dry_run: true)
expect do
text_box.render
end.to_not raise_exception
end
end
describe '#render(:valign => :bottom)' do
it '#at should be the same from one dry run to the next' do
text = 'this is center text ' * 12
options = {
width: 162,
valign: :bottom,
document: pdf
}
text_box = described_class.new(text, options)
text_box.render(dry_run: true)
original_at = text_box.at.dup
text_box.render(dry_run: true)
expect(text_box.at).to eq(original_at)
end
end
describe '#render(:valign => :center)' do
it '#at should be the same from one dry run to the next' do
text = 'this is center text ' * 12
options = {
width: 162,
valign: :center,
document: pdf
}
text_box = described_class.new(text, options)
text_box.render(dry_run: true)
original_at = text_box.at.dup
text_box.render(dry_run: true)
expect(text_box.at).to eq(original_at)
end
end
describe '#render with :rotate option of 30)' do
let(:cos) { Math.cos(30 * Math::PI / 180) }
let(:sin) { Math.sin(30 * Math::PI / 180) }
let(:text) { 'Oh hai text rect. ' * 10 }
let(:options) do
{
document: pdf,
rotate: 30,
at: [300, 70],
width: 100,
height: 50
}
end
context 'with :rotate_around option of :center' do
it 'draws content to the page rotated about the center of the text' do
options[:rotate_around] = :center
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
x = 350
y = 45
x_prime = x * cos - y * sin
y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
reduce_precision(x - x_prime),
reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
reduce_precision(cos),
0, 0
])
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
end
context 'with :rotate_around option of :upper_left' do
it 'draws content to the page rotated about the upper left corner of '\
'the text' do
options[:rotate_around] = :upper_left
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
x_prime = 300 * cos - 70 * sin
y_prime = 300 * sin + 70 * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
reduce_precision(300 - x_prime),
reduce_precision(70 - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
reduce_precision(cos),
0, 0
])
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
end
context 'with default :rotate_around' do
it 'draws content to the page rotated about the upper left corner of '\
'the text' do
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
x_prime = 300 * cos - 70 * sin
y_prime = 300 * sin + 70 * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
reduce_precision(300 - x_prime),
reduce_precision(70 - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
reduce_precision(cos),
0, 0
])
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
end
context 'with :rotate_around option of :upper_right' do
it 'draws content to the page rotated about the upper right corner of '\
'the text' do
options[:rotate_around] = :upper_right
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
x = 400
y = 70
x_prime = x * cos - y * sin
y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
reduce_precision(x - x_prime),
reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
reduce_precision(cos),
0, 0
])
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
end
context 'with :rotate_around option of :lower_right' do
it 'draws content to the page rotated about the lower right corner of '\
'the text' do
options[:rotate_around] = :lower_right
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
x = 400
y = 20
x_prime = x * cos - y * sin
y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
reduce_precision(x - x_prime),
reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
reduce_precision(cos),
0, 0
])
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
end
context 'with :rotate_around option of :lower_left' do
it 'draws content to the page rotated about the lower left corner of '\
'the text' do
options[:rotate_around] = :lower_left
text_box = described_class.new(text, options)
text_box.render
matrices = PDF::Inspector::Graphics::Matrix.analyze(pdf.render)
x = 300
y = 20
x_prime = x * cos - y * sin
y_prime = x * sin + y * cos
expect(matrices.matrices[0]).to eq([
1, 0, 0, 1,
reduce_precision(x - x_prime),
reduce_precision(y - y_prime)
])
expect(matrices.matrices[1]).to eq([
reduce_precision(cos),
reduce_precision(sin),
reduce_precision(-sin),
reduce_precision(cos),
0, 0
])
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.strings).to_not be_empty
end
end
end
describe 'default height' do
it 'is the height from the bottom bound to document.y' do
target_height = pdf.y - pdf.bounds.bottom
text = "Oh hai\n" * 60
text_box = described_class.new(text, document: pdf)
text_box.render
expect(text_box.height).to be_within(pdf.font.height).of(target_height)
end
it 'uses the margin-box bottom if only in a stretchy bbox' do
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do
target_height = pdf.y - pdf.bounds.bottom
text = "Oh hai\n" * 60
text_box = described_class.new(text, document: pdf)
text_box.render
expect(text_box.height).to be_within(pdf.font.height).of(target_height)
end
end
it 'uses the parent-box bottom if in a stretchy bbox and overflow is '\
':expand, even with an explicit height' do
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do
target_height = pdf.y - pdf.bounds.bottom
text = "Oh hai\n" * 60
text_box = described_class.new(
text,
document: pdf,
height: 100,
overflow: :expand
)
text_box.render
expect(text_box.height).to be_within(pdf.font.height).of(target_height)
end
end
it 'uses the innermost non-stretchy bbox, not the margin box' do
pdf.bounding_box(
[0, pdf.cursor],
width: pdf.bounds.width,
height: 200
) do
pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width) do
text = "Oh hai\n" * 60
text_box = described_class.new(text, document: pdf)
text_box.render
expect(text_box.height).to be_within(pdf.font.height).of(200)
end
end
end
end
describe 'default at' do
it 'is the left corner of the bounds, and the current document.y' do
target_at = [pdf.bounds.left, pdf.y]
text = 'Oh hai text rect. ' * 100
options = { document: pdf }
text_box = described_class.new(text, options)
text_box.render
expect(text_box.at).to eq(target_at)
end
end
context 'with text than can fit in the box' do
let(:text) { 'Oh hai text rect. ' * 10 }
let(:options) do
{
width: 162.0,
height: 162.0,
document: pdf
}
end
it 'printed text should match requested text, except that preceding and ' \
'trailing white space will be stripped from each line, and newlines ' \
'may be inserted' do
text_box = described_class.new(" #{text}", options)
text_box.render
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
end
it 'render returns an empty string because no text remains unprinted' do
text_box = described_class.new(text, options)
expect(text_box.render).to eq('')
end
it 'is truncated when the leading is set high enough to prevent all the '\
'lines from being printed' do
options[:leading] = 40
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text.tr("\n", ' ')).to_not eq(text.strip)
end
end
context 'with text that fits exactly in the box' do
let(:lines) { 3 }
let(:interlines) { lines - 1 }
let(:text) { (1..lines).to_a.join("\n") }
let(:options) do
{
width: 162.0,
height: pdf.font.ascender + pdf.font.height * interlines +
pdf.font.descender,
document: pdf
}
end
it 'has the expected height' do
expected_height = options.delete(:height)
text_box = described_class.new(text, options)
text_box.render
expect(text_box.height).to be_within(0.0001).of(expected_height)
end
it 'prints everything' do
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to eq(text)
end
describe 'with leading' do
before do
options[:leading] = 15
end
it 'does not overflow when enough height is added' do
options[:height] += options[:leading] * interlines
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to eq(text)
end
it 'overflows when insufficient height is added' do
options[:height] += options[:leading] * interlines - 1
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to_not eq(text)
end
end
context 'with negative leading' do
before do
options[:leading] = -4
end
it 'does not overflow when enough height is removed' do
options[:height] += options[:leading] * interlines
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to eq(text)
end
it 'overflows when too much height is removed' do
options[:height] += options[:leading] * interlines - 1
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text).to_not eq(text)
end
end
end
context 'when printing UTF-8 string with higher bit characters' do
let(:text) { '©' }
let(:text_box) do
# not enough height to print any text, so we can directly compare against
# the input string
bounding_height = 1.0
options = {
height: bounding_height,
document: pdf
}
described_class.new(text, options)
end
before do
file = "#{Prawn::DATADIR}/fonts/Panic+Sans.dfont"
pdf.font_families['Panic Sans'] = {
normal: { file: file, font: 'PanicSans' },
italic: { file: file, font: 'PanicSans-Italic' },
bold: { file: file, font: 'PanicSans-Bold' },
bold_italic: { file: file, font: 'PanicSans-BoldItalic' }
}
end
describe 'when using a TTF font' do
it 'unprinted text should be in UTF-8 encoding' do
pdf.font('Panic Sans')
remaining_text = text_box.render
expect(remaining_text).to eq(text)
end
end
describe 'when using an AFM font' do
it 'unprinted text should be in UTF-8 encoding' do
remaining_text = text_box.render
expect(remaining_text).to eq(text)
end
end
end
context 'with more text than can fit in the box' do
let(:text) { 'Oh hai text rect. ' * 30 }
let(:bounding_height) { 162.0 }
let(:options) do
{
width: 162.0,
height: bounding_height,
document: pdf
}
end
context 'when truncated overflow' do
let(:text_box) do
described_class.new(text, options.merge(overflow: :truncate))
end
it 'is truncated' do
text_box.render
expect(text_box.text.tr("\n", ' ')).to_not eq(text.strip)
end
it 'render does not return an empty string because some text remains '\
'unprinted' do
expect(text_box.render).to_not be_empty
end
it '#height should be no taller than the specified height' do
text_box.render
expect(text_box.height).to be <= bounding_height
end
it '#height should be within one font height of the specified height' do
text_box.render
expect(bounding_height).to be_within(pdf.font.height)
.of(text_box.height)
end
context 'with :rotate option' do
it 'unrendered text should be the same as when not rotated' do
remaining_text = text_box.render
rotate = 30
options[:document] = pdf
options[:rotate] = rotate
options[:at] = [300, 70]
rotated_text_box = described_class.new(text, options)
expect(rotated_text_box.render).to eq(remaining_text)
end
end
end
context 'when truncated with text and size taken from the manual' do
it 'returns the right text' do
text = 'This is the beginning of the text. It will be cut somewhere ' \
'and the rest of the text will procede to be rendered this time by '\
'calling another method.' + ' . ' * 50
options[:width] = 300
options[:height] = 50
options[:size] = 18
text_box = described_class.new(text, options)
remaining_text = text_box.render
expect(remaining_text).to eq(
'text will procede to be rendered this time by calling another ' \
'method. . . . . . . . . . . . . . . . . . . . ' \
'. . . . . . . . . . . . . . . . . . . . . . ' \
'. . . . . . . . . '
)
end
end
context 'when expand overflow' do
let(:text_box) do
described_class.new(text, options.merge(overflow: :expand))
end
it 'height expands to encompass all the text '\
'(but not exceed the height of the page)' do
text_box.render
expect(text_box.height).to be > bounding_height
end
it 'displays the entire string (as long as there was space remaining on '\
'the page to print all the text)' do
text_box.render
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
end
it 'render returns an empty string because no text remains unprinted '\
'(as long as there was space remaining on the page to print all '\
'the text)' do
expect(text_box.render).to eq('')
end
end
context 'when shrink_to_fit overflow' do
let(:text_box) do
described_class.new(
text,
options.merge(
overflow: :shrink_to_fit,
min_font_size: 2
)
)
end
it 'displays the entire text' do
text_box.render
expect(text_box.text.tr("\n", ' ')).to eq(text.strip)
end
it 'render returns an empty string because no text remains unprinted' do
expect(text_box.render).to eq('')
end
it 'does not drop below the minimum font size' do
options[:overflow] = :shrink_to_fit
options[:min_font_size] = 10.1
text_box = described_class.new(text, options)
text_box.render
actual_text = PDF::Inspector::Text.analyze(pdf.render)
expect(actual_text.font_settings[0][:size]).to eq(10.1)
end
end
end
context 'with enough space to fit the text but using the ' \
'shrink_to_fit overflow' do
it 'does not shrink the text when there is no need to' do
bounding_height = 162.0
options = {
width: 162.0,
height: bounding_height,
overflow: :shrink_to_fit,
min_font_size: 5,
document: pdf
}
text_box = described_class.new("hello\nworld", options)
text_box.render
text = PDF::Inspector::Text.analyze(pdf.render)
expect(text.font_settings[0][:size]).to eq(12)
end
end
context 'with a solid block of Chinese characters' do
it 'printed text should match requested text, except for newlines' do
text = '写中国字' * 10
options = {
width: 162.0,
height: 162.0,
document: pdf,
overflow: :truncate
}
pdf.font "/usr/share/fonts/truetype/arphic-gkai00mp/gkai00mp.ttf"
text_box = described_class.new(text, options)
text_box.render
expect(text_box.text.delete("\n")).to eq(text)
end
end
describe 'drawing bounding boxes' do
it 'restores the margin box when bounding box exits' do
margin_box = pdf.bounds
pdf.text_box 'Oh hai text box. ' * 11, height: pdf.font.height * 10
expect(pdf.bounds).to eq(margin_box)
end
end
describe '#render with :character_spacing option' do
it 'draws the character spacing to the document' do
string = 'hello world'
options = { document: pdf, character_spacing: 10 }
text_box = described_class.new(string, options)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.character_spacing[0]).to eq(10)
end
it 'takes character spacing into account when wrapping' do
pdf.font 'Courier'
text_box = described_class.new(
'hello world',
width: 100,
overflow: :expand,
character_spacing: 10,
document: pdf
)
text_box.render
expect(text_box.text).to eq("hello\nworld")
end
end
describe 'wrapping' do
it 'wraps text' do
text = 'Please wrap this text about HERE. ' \
'More text that should be wrapped'
expect = "Please wrap this text about\n"\
"HERE. More text that should be\nwrapped"
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 220,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
# white space was being stripped after the entire line was generated,
# meaning that leading white space characters reduced the amount of space on
# the line for other characters, so wrapping "hello hello" resulted in
# "hello\n\nhello", rather than "hello\nhello"
#
it 'white space at beginning of line should not be taken into account ' \
'when computing line width' do
text = 'hello hello'
expect = "hello\nhello"
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 40,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
it 'respects end of line when wrapping text' do
text = "Please wrap only before\nTHIS word. Don't wrap this"
expect = text
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 220,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
it 'respects multiple newlines when wrapping text' do
text = "Please wrap only before THIS\n\nword. Don't wrap this"
expect = "Please wrap only before\nTHIS\n\nword. Don't wrap this"
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 200,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
it 'respects multiple newlines when wrapping text when those newlines '\
'coincide with a line break' do
text = "Please wrap only before\n\nTHIS word. Don't wrap this"
expect = text
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 220,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
it 'respects initial newlines' do
text = "\nThis should be on line 2"
expect = text
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 220,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
it 'wraps lines comprised of a single word of the bounds when '\
'wrapping text' do
text = 'You_can_wrap_this_text_HERE'
expect = "You_can_wrap_this_text_HE\nRE"
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 180,
overflow: :expand,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
it 'wraps lines comprised of a single non-alpha word of the bounds when '\
'wrapping text' do
text = '©' * 30
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 180,
overflow: :expand,
document: pdf
)
text_box.render
expected = "#{'©' * 25}\n#{'©' * 5}"
expected = pdf.font.normalize_encoding(expected)
expected = expected.force_encoding(Encoding::UTF_8)
expect(text_box.text).to eq(expected)
end
it 'wraps non-unicode strings using single-byte word-wrapping' do
text = 'continúa esforzandote ' * 5
text_box = described_class.new(
text,
width: 180,
document: pdf
)
text_box.render
results_with_accent = text_box.text
text = 'continua esforzandote ' * 5
text_box = described_class.new(
text,
width: 180,
document: pdf
)
text_box.render
results_without_accent = text_box.text
expect(first_line(results_with_accent).length)
.to eq(first_line(results_without_accent).length)
end
it 'allows you to disable wrapping by char' do
text = 'You_cannot_wrap_this_text_at_all_because_we_are_disabling_' \
'wrapping_by_char_and_there_are_no_word_breaks'
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 180,
overflow: :shrink_to_fit,
disable_wrap_by_char: true,
document: pdf
)
expect { text_box.render }.to raise_error(Prawn::Errors::CannotFit)
end
it 'retains full words with :shrink_to_fit if char wrapping is disabled' do
text = 'Wrapped_words'
expect = 'Wrapped_words'
pdf.font 'Courier'
text_box = described_class.new(
text,
width: 50,
height: 50,
size: 50,
overflow: :shrink_to_fit,
disable_wrap_by_char: true,
document: pdf
)
text_box.render
expect(text_box.text).to eq(expect)
end
end
describe 'Text::Box#render with :mode option' do
it 'alters the text rendering mode of the document' do
string = 'hello world'
options = { document: pdf, mode: :fill_stroke }
text_box = described_class.new(string, options)
text_box.render
contents = PDF::Inspector::Text.analyze(pdf.render)
expect(contents.text_rendering_mode).to eq([2, 0])
end
end
def reduce_precision(float)
float.round(5)
end
def first_line(str)
str.lines.first
end
end
|