Subject: | Patch with additional background_func and header alignment implementation |
While implementing some PDF reporting functionality to our web
applications we were in need of having the following additional features
for PDF::ReportWriter:
1. A background coloring function similar to the colour_func
implementation. Additionally the current cell rendering options should
be passed to both rendering functions.
2. The possibility to set the aligment of the row header.
I've implemented and hopefully POD documented correctly the new features
based on ReportWriter V 1.4.
A patch is attached.
Best regards and thank you for the gread module and a happy new year.
Subject: | ReportWriter.pm.diff |
--- ReportWriter.pm.orig 2007-12-05 09:18:02.985704000 +0100
+++ ReportWriter.pm 2008-01-07 10:47:23.237946000 +0100
@@ -530,7 +530,7 @@
bold => TRUE,
font_size => $field->{font_size},
text_whitespace => $field->{text_whitespace},
- align => 'centre',
+ align => $field->{header_align},
colour => $field->{header_colour}
};
}
@@ -1438,17 +1438,31 @@
my ( $self, $cell, $opt ) = @_;
- unless ( exists $cell->{background} ) {
+ my $background;
+
+ if ( $cell->{background_func} ) {
+ if ( $self->{debug} ) {
+ print "\nRunning background_func() \n";
+ }
+
+ $background = $cell->{background_func}($opt->{current_value}, $opt->{current_row}, $opt);
+ }
+ else {
+ $background = $cell->{background};
+ }
+
+ unless ( defined $background ) {
return;
}
my $current_height = $opt->{cell_full_height};
+
- if ( $cell->{background}->{shape} ) {
+ if ( $background->{shape} ) {
- if ( $cell->{background}->{shape} eq "ellipse" ) {
+ if ( $background->{shape} eq "ellipse" ) {
- $self->{shape}->fillcolor( $cell->{background}->{colour} );
+ $self->{shape}->fillcolor( $background->{colour} );
$self->{shape}->ellipse(
$cell->{x_border} + ( $cell->{full_width} >> 1 ), # x centre
@@ -1459,9 +1473,9 @@
$self->{shape}->fill;
- } elsif ( $cell->{background}->{shape} eq "box" ) {
+ } elsif ( $background->{shape} eq "box" ) {
- $self->{shape}->fillcolor( $cell->{background}->{colour} );
+ $self->{shape}->fillcolor( $background->{colour} );
$self->{shape}->rect(
$cell->{x_border}, # left border
@@ -1479,10 +1493,10 @@
#
# Now render cell background borders
#
- if ( $cell->{background}->{border} ) {
+ if ( $background->{border} ) {
# Cell Borders
- $self->{line}->strokecolor( $cell->{background}->{border} );
+ $self->{line}->strokecolor( $background->{border} );
# TODO Move the regex setuff into setup_cell_definitions()
# so we don't have to regex per cell, which is
@@ -1491,9 +1505,9 @@
# If the 'borders' key does not exist then draw all borders
# to support code written before this was added.
# A value of 'all' can also be used.
- if ( ( ! exists $cell->{background}->{borders} ) || ( uc $cell->{background}->{borders} eq 'ALL' ) )
+ if ( ( ! exists $background->{borders} ) || ( uc $background->{borders} eq 'ALL' ) )
{
- $cell->{background}->{borders} = "tblr";
+ $background->{borders} = "tblr";
}
# The 'borders' key looks for the following chars in the string
@@ -1502,7 +1516,7 @@
# l or L - Left Border Line
# r or R - Right Border Line
- my $cell_bb = $cell->{background}->{borders};
+ my $cell_bb = $background->{borders};
# Bottom Horz Line
if ( $cell_bb =~ /[bB]/ ) {
@@ -1787,7 +1801,7 @@
if ( $self->{debug} ) {
print "\nRunning colour_func() on data: " . $string . "\n";
}
- $self->{txt}->fillcolor( $cell->{colour_func}( $string, $row ) || "black" );
+ $self->{txt}->fillcolor( $cell->{colour_func}( $string, $row, $opt ) || "black" );
} else {
$self->{txt}->fillcolor( $cell->{colour} || "black" );
}
@@ -2595,6 +2609,15 @@
=back
+=head2 header_align
+
+=over 4
+
+The alignment of the data headers ( ie field names ).
+Possible values are "left", "right" and "centre" ( or now "center", also ).
+
+=back
+
=head2 text
=over 4
@@ -2651,10 +2674,72 @@
=back
+=head3 options
+
+=over 4
+
+a hash containing the current rendering options:
+
+ {
+ current_row - the current row of data
+ row_type - the current row type (data, group_header, ...)
+ current_value - the current value of this cell
+ cell - the cell definition ( get x position and width from this )
+ cell_counter - position of the current cell in the row ( 0 .. n - 1 )
+ cell_y_border - the bottom of the cell
+ cell_full_height - the height of the cell
+ page - the current page ( a PDF::API2 page )
+ page_no - the current page number
+ }
+
+=back
+
Note that prior to version 1.4, we only passed the value.
=back
+=head2 background_func
+
+=over 4
+
+A user-defined sub that returns a colour for the cell background. Your background_func will be passed:
+
+=head3 value
+
+=over 4
+
+The current cell value
+
+=back
+
+=head3 row
+
+=over 4
+
+an array reference containing the current row
+
+=back
+
+=head3 options
+
+=over 4
+
+a hash containing the current rendering options:
+
+ {
+ current_row - the current row of data
+ row_type - the current row type (data, group_header, ...)
+ current_value - the current value of this cell
+ cell - the cell definition ( get x position and width from this )
+ cell_counter - position of the current cell in the row ( 0 .. n - 1 )
+ cell_y_border - the bottom of the cell
+ cell_full_height - the height of the cell
+ page - the current page ( a PDF::API2 page )
+ page_no - the current page number
+ }
+
+=back
+
=head2 custom_render_func
=over 4