The Perl Toolchain Summit 2025 Needs You: You can help 🙏 Learn more

#!/usr/bin/perl
use strict;
use Test::More tests => 11;
#use Config; no longer needed
use lib 't/lib'; # Needed for 'make test' from project dirs
use TestData qw();
use PDFAPI2Mock; # provide dummy PDF::API2. obviously a real PDF::API2 or
# PDF::Builder installation will be needed in order to run
BEGIN {
use_ok('PDF::Table')
}
my ($col_widths);
($col_widths, undef) = PDF::Table::ColumnWidth::CalcColumnWidths(
400,
[ 50, 50, 50, 50 ],
[ 200, 200, 200, 200 ],
[ -1, -1, -1, -1 ]
);
is_deeply( $col_widths, [ 100, 100, 100, 100 ], 'CalcColumnWidths - even');
($col_widths, undef) = PDF::Table::ColumnWidth::CalcColumnWidths(
400,
[ 41, 58, 48 ],
[ 51, 600, 48 ],
[ -1, -1, -1 ]
);
# some float values round to small number of decimal places, so no difference
# between regular, long double, and quad math Perls
$col_widths->[0] = int(1000*$col_widths->[0] + 0.5)/1000;
$col_widths->[1] = int(1000*$col_widths->[1] + 0.5)/1000;
$col_widths->[2] = int(1000*$col_widths->[2] + 0.5)/1000;
# was 125.333, 142.333, 132.333
is_deeply( $col_widths, [ 41, 311, 48 ], 'CalcColumnWidths - uneven');
($col_widths, undef) = PDF::Table::ColumnWidth::CalcColumnWidths(
400,
[ 50, 0 ], # undef min_w value
[ 50, 50 ],
[ -1, -1 ]
);
# was 225, 175
is_deeply( $col_widths, [ 200, 200 ], 'CalcColumnWidths - undef');
my ($pdf, $page, $tab, @data, @required);
@data = (
[ 'foo', 'bar', 'baz' ],
);
@required = (
x => 10,
w => 300,
start_y => 750,
next_y => 700,
start_h => 40,
next_h => 500,
);
$pdf = PDF::API2->new;
$page = $pdf->page;
$tab = PDF::Table->new($pdf, $page);
#
# this tickles a bug (#34017) which causes an infinite loop
#
'foo' =~ /(o)(o)/;
$tab->table($pdf, $page, [@data], @required,
border => 1,
border_color => 'black',
font_size => 12,
background_color => 'gray',
column_props => [
{}, { background_color => 'red' }, {},
],
cell_props => [
[ {}, {}, { background_color => 'blue' } ],
],
);
ok($pdf->match(
[[qw(translate 12 736)],[qw(text foo)]],
[[qw(translate 112 736)],[qw(text bar)]],
[[qw(translate 212 736)],[qw(text baz)]],
), 'text position') or note explain $pdf;
ok($pdf->match(
[[qw(rect 10 731 100 19)],[qw(fillcolor gray)]],
[[qw(rect 110 731 100 19)],[qw(fillcolor red)]],
[[qw(rect 210 731 100 19)],[qw(fillcolor blue)]],
), 'default header fillcolor') or note explain $pdf;
ok($pdf->match(
[[qw(gfx)],[qw(strokecolor black)],[qw(linewidth 1)]],
[[qw(stroke)]],
), "draw borders");
$pdf = PDF::API2->new;
$page = $pdf->page;
$tab->table($pdf, $page, [@data], @required,
border => 0,
border_color => 'black',
font_size => 12,
column_props => [
{}, { justify => 'center' }, { justify => 'center' },
],
cell_props => [
[ { justify => 'center' }, {}, { justify => 'right' } ],
],
);
ok($pdf->match(
[[qw(translate 60 736)],[qw(text_center foo)]],
[[qw(translate 160 736)],[qw(text_center bar)]],
[[qw(translate 308 736)],[qw(text_right baz)]],
), 'justify right and center') or note explain $pdf;
ok(!$pdf->match(
[[qw(gfx)],[qw(strokecolor black)],[qw(linewidth 0)]],
), "don't set up zero-width borders");
# table is only 3 lines high (4*12 > 40).
@data = (
[ 'foo', 'bar' ],
[ 'one', 'two' ],
[ 'thr', 'four score and seven years ago our fathers brought forth' ],
[ 'fiv', 'six' ],
[ 'sev', 'abcdefghijklmnopqrstuvwxyz' ],
);
$pdf = PDF::API2->new;
$page = $pdf->page;
$tab->table($pdf, $page, [@data], @required,
border => 0,
font_size => 12,
max_word_length => 13,
cell_props => [
[],
[ { background_color => 'blue' }, {} ],
],
);
ok(1,'Skip test because the one below is not working and must be fixed');
#ok($pdf->match(
# [[qw(page)]],
# [[qw(rect 10 714 20 12)],[qw(fillcolor blue)]],
# [[qw(translate 10 714)],[qw(text thr)]],
# [[qw(page)]],
# [[qw(rect 10 688 20 12)],[qw(fillcolor blue)]],
# [[qw(translate 10 688)],[qw(text -)]],
#), 'keep cell_props values when row spans a page');
ok($pdf->match(
[['text', 'abcdefghijklm nopqrstuvwxyz']],
), 'break long words on max_word_length');
1;