Skip to content

Commit

Permalink
wip: add description support to embed_image()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Feb 17, 2024
1 parent 9e3a2c2 commit 71245b7
Show file tree
Hide file tree
Showing 13 changed files with 338 additions and 25 deletions.
4 changes: 3 additions & 1 deletion lib/Excel/Writer/XLSX/Package/Packager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ sub _write_rich_value_file {
my $dir = $self->{_package_dir};
my $rich_value = Excel::Writer::XLSX::Package::RichValue->new();

$rich_value->{_value_count} = @{ $self->{_workbook}->{_embedded_images} };
$rich_value->{_embedded_images} = $self->{_workbook}->{_embedded_images};

$rich_value->_set_xml_writer( $dir . '/xl/richData/rdrichvalue.xml' );
$rich_value->_assemble_xml_file();
Expand All @@ -543,6 +543,8 @@ sub _write_rich_value_structure_file {
my $dir = $self->{_package_dir};
my $rich_value = Excel::Writer::XLSX::Package::RichValueStructure->new();

$rich_value->{_has_embedded_descriptions} = $self->{_workbook}->{_has_embedded_descriptions};

$rich_value->_set_xml_writer( $dir . '/xl/richData/rdrichvaluestructure.xml' );
$rich_value->_assemble_xml_file();
}
Expand Down
28 changes: 21 additions & 7 deletions lib/Excel/Writer/XLSX/Package/RichValue.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sub new {
my $fh = shift;
my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );

$self->{_value_count} = 0;
$self->{_embedded_images} = [];

bless $self, $class;

Expand Down Expand Up @@ -78,15 +78,18 @@ sub _write_rv_data {

my @attributes = (
'xmlns' => $xmlns,
'count' => $self->{_value_count},
'count' => scalar @{ $self->{_embedded_images} },
);

$self->xml_start_tag( 'rvData', @attributes );

for my $index ( 0 .. $self->{_value_count} - 1 ) {
my $index = 0;
for my $image ( @{ $self->{_embedded_images} } ) {

# Write the rv element.
$self->_write_rv( $index );
$self->_write_rv( $index, $image->[2], $image->[3] );

$index++;
}


Expand All @@ -101,15 +104,26 @@ sub _write_rv_data {
#
sub _write_rv {

my $self = shift;
my $index = shift;
my $self = shift;
my $index = shift;
my $description = shift;
my $decorative = shift;
my $value = 5;

if ( $decorative ) {
$value = 6;
}

my @attributes = ( 's' => 0 );

$self->xml_start_tag( 'rv', @attributes );

$self->_write_v( $index );
$self->_write_v( 5 );
$self->_write_v( $value );

if ( $description ) {
$self->_write_v( $description );
}

$self->xml_end_tag( 'rv' );
}
Expand Down
12 changes: 9 additions & 3 deletions lib/Excel/Writer/XLSX/Package/RichValueStructure.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ sub new {
my $fh = shift;
my $self = Excel::Writer::XLSX::Package::XMLwriter->new( $fh );

$self->{_has_embedded_descriptions} = 0;

bless $self, $class;

return $self;
Expand Down Expand Up @@ -104,8 +106,12 @@ sub _write_s {

$self->xml_start_tag( 's', @attributes );

$self->_write_k( '_rvRel:LocalImageIdentifier' );
$self->_write_k( 'CalcOrigin' );
$self->_write_k( '_rvRel:LocalImageIdentifier', 'i' );
$self->_write_k( 'CalcOrigin', 'i' );

if ( $self->{_has_embedded_descriptions} ) {
$self->_write_k( 'Text', 's' );
}

$self->xml_end_tag( 's' );
}
Expand All @@ -121,7 +127,7 @@ sub _write_k {

my $self = shift;
my $n = shift;
my $t = 'i';
my $t = shift;

my @attributes = (
'n' => $n,
Expand Down
14 changes: 11 additions & 3 deletions lib/Excel/Writer/XLSX/Workbook.pm
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ sub new {
$self->{_max_url_length} = 2079;
$self->{_has_comments} = 0;
$self->{_read_only} = 0;
$self->{_has_metadata} = 0;

$self->{_has_metadata} = 0;
$self->{_has_dynamic_functions} = 0;
$self->{_has_embedded_images} = 0;
$self->{_has_embedded_images} = 0;
$self->{_has_embedded_descriptions} = 0;

$self->{_default_format_properties} = {};

Expand Down Expand Up @@ -1802,7 +1804,6 @@ sub _prepare_drawings {

my $self = shift;
my $chart_ref_id = 0;
my $image_ref_id = 0;
my $drawing_id = 0;
my $ref_id = 0;
my %image_ids = ();
Expand All @@ -1812,8 +1813,15 @@ sub _prepare_drawings {
# Store the image types for any embeded images.
for my $image_data ( @{ $self->{_embedded_images} } ) {
$self->_store_image_types( $image_data->[1] );

if ( $image_data->[2] ) {
$self->{_has_embedded_descriptions} = 1;
}
}

# The image IDs start from after the embedded images.
my $image_ref_id = scalar @{ $self->{_embedded_images} };

for my $sheet ( @{ $self->{_worksheets} } ) {

my $chart_count = scalar @{ $sheet->{_charts} };
Expand Down
26 changes: 15 additions & 11 deletions lib/Excel/Writer/XLSX/Worksheet.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6560,17 +6560,6 @@ sub embed_image {
croak "Insufficient arguments in embed_image()" unless @_ >= 3;
croak "Couldn't locate $image: $!" unless -e $image;

my ( $type, $width, $height, $name, $x_dpi, $y_dpi, $md5 ) =
get_image_properties( $image );

my $image_index = ${ $self->{_embedded_images_indexes} }->{$md5};

if ( !$image_index ) {
push @{ ${ $self->{_embedded_images} } }, [ $image, $type ];
$image_index = scalar @{ ${ $self->{_embedded_images} } };
${ $self->{_embedded_images_indexes} }->{$md5} = $image_index;
}

if ( ref $_[3] eq 'HASH' ) {
# Newer hashref bashed options.
my $options = $_[3];
Expand All @@ -6586,6 +6575,21 @@ sub embed_image {
$decorative = $options->{decorative};
}


my ( $type, $width, $height, $name, $x_dpi, $y_dpi, $md5 ) =
get_image_properties( $image );

my $image_index = ${ $self->{_embedded_images_indexes} }->{$md5};

if ( !$image_index ) {
push @{ ${ $self->{_embedded_images} } },
[ $image, $type, $description, $decorative ];

$image_index = scalar @{ ${ $self->{_embedded_images} } };
${ $self->{_embedded_images_indexes} }->{$md5} = $image_index;
}


# Write the cell placeholder.
$self->{_table}->{$row}->{$col} = [ 'e', "#VALUE!", $xf, $image_index];
$self->{_has_embedded_images} = 1;
Expand Down
70 changes: 70 additions & 0 deletions t/regression/embed_image06.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, [email protected]
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'embed_image06.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];
my $ignore_elements = {};


###############################################################################
#
# Test the creation of a simple Excel::Writer::XLSX file with image(s).
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );
my $worksheet = $workbook->add_worksheet();

$worksheet->embed_image( 0, 0, $dir . 'images/red.png' );
$worksheet->insert_image( 8, 4, $dir . 'images/red.png' );

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__
71 changes: 71 additions & 0 deletions t/regression/embed_image07.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
###############################################################################
#
# Tests the output of Excel::Writer::XLSX against Excel generated files.
#
# Copyright 2000-2023, John McNamara, [email protected]
#
# SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
#

use lib 't/lib';
use TestFunctions qw(_compare_xlsx_files _is_deep_diff);
use strict;
use warnings;

use Test::More tests => 1;

###############################################################################
#
# Tests setup.
#
my $filename = 'embed_image07.xlsx';
my $dir = 't/regression/';
my $got_filename = $dir . "ewx_$filename";
my $exp_filename = $dir . 'xlsx_files/' . $filename;

my $ignore_members = [];
my $ignore_elements = {};


###############################################################################
#
# Test the creation of a simple Excel::Writer::XLSX file with image(s).
#
use Excel::Writer::XLSX;

my $workbook = Excel::Writer::XLSX->new( $got_filename );
my $worksheet = $workbook->add_worksheet();

$worksheet->embed_image( 0, 0, $dir . 'images/red.png' );
$worksheet->embed_image( 2, 0, $dir . 'images/blue.png' );
$worksheet->insert_image( 8, 4, $dir . 'images/red.png' );

$workbook->close();


###############################################################################
#
# Compare the generated and existing Excel files.
#

my ( $got, $expected, $caption ) = _compare_xlsx_files(

$got_filename,
$exp_filename,
$ignore_members,
$ignore_elements,
);

_is_deep_diff( $got, $expected, $caption );


###############################################################################
#
# Cleanup.
#
unlink $got_filename;

__END__
Loading

0 comments on commit 71245b7

Please sign in to comment.