OODoc/Image.pm untested

filter: Statementstmt 0.0% 0 / 280 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 547 CC 62 SCAR 49.1
CC
62
Cov
0%
createImageElement
506.0
exportImages
72.0
importImage
56.0
CRAP 135.4
1 #-----------------------------------------------------------------------------
2 #
3 # $Id : Image.pm 2.020 2009-07-07 JMG$
4 #
5 # Created and maintained by Jean-Marie Gouarne
6 # Copyright 2009 by Genicorp, S.A. (www.genicorp.com)
7 #
8 #-----------------------------------------------------------------------------
9  
10 package OpenOffice::OODoc::Image;
11 use 5.008_000;
12 use strict;
13 use OpenOffice::OODoc::XPath 2.237;
14 use File::Basename;
15 our @ISA = qw ( OpenOffice::OODoc::XPath );
16 our $VERSION = '2.020';
17  
18 #-----------------------------------------------------------------------------
19 # default attributes for image style
20  
21 our %DEFAULT_IMAGE_STYLE =
22 (
23 references =>
24 {
25 'style:family' => 'graphics',
26 'style:parent-style-name' => 'Graphics'
27 },
28 properties =>
29 {
30 'fo:clip' => 'rect(0cm 0cm 0cm 0cm)',
31 'style:vertical-rel' => 'paragraph',
32 'style:horizontal-rel' => 'paragraph',
33 'style:vertical-pos' => 'from-top',
34 'style:horizontal-pos' => 'from-left',
35 'draw:color-mode' => 'standard',
36 'draw:luminance' => '0%',
37 'draw:red' => '0%',
38 'draw:green' => '0%',
39 'draw:blue' => '0%',
40 'draw:gamma' => '1',
41 'draw:color-inversion' => 'false'
42 }
43 );
44  
45 #-----------------------------------------------------------------------------
46 # constructor : calling OO XPath constructor
47  
48 sub new
49 {
50 my $caller = shift;
51 my $class = ref ($caller) || $caller;
52 my %options =
53 (
54 @_
55 );
56 my $object = $class->SUPER::new(%options);
57 return $object ?
58 bless $object, $class :
59 undef;
60 }
61  
62 #-----------------------------------------------------------------------------
63 # create & insert a new image element
64  
65 sub createImageElement
66 {
67 my $self = shift;
68 my $name = shift;
69 my %opt = @_;
70  
71 my $content_class = $self->contentClass;
72 my $container = $self->{'image_container'};
73 my $attachment = undef;
74 my $firstnode = undef;
75 my $element = undef;
76 my $description = undef;
77 my $size = undef;
78 my $position = undef;
79 my $import = undef;
80 my $path = undef;
81  
82 if (
83 ($content_class eq 'presentation')
84 or
85 ($content_class eq 'drawing')
86 )
87 {
88 my $target = $opt{'page'} || '';
89 my $page = ref $target ?
90 $target :
91 $self->getNodeByXPath
92 ("//draw:page[\@draw:name=\"$target\"]");
93  
94 delete $opt{'page'};
95 $path = $page;
96 }
97 else
98 {
99 $path = $opt{'attachment'};
100 }
101 delete $opt{'attachment'};
102 unless ($path)
103 {
104 $attachment =
105 ($self->{'body'})
106 ||
107 ($self->getElement('//style:header', 0))
108 ||
109 ($self->getElement('//style:footer', 0));
110 if ($attachment && defined $opt{'page'})
111 {
112 $firstnode = $self->selectChildElementByName
113 ($attachment, 'text:(p|h)');
114 }
115 }
116 else
117 {
118 $attachment = ref $path ? $path : $self->getElement($path, 0);
119 }
120 unless ($attachment)
121 {
122 warn "[" . __PACKAGE__ .
123 "::createImageElement] No valid attachment\n";
124 return undef;
125 }
126  
127 # parameters translation
128 $opt{'draw:name'} = $name;
129 if ($opt{'description'})
130 {
131 $description = $opt{'description'};
132 delete $opt{'description'};
133 }
134 if ($opt{'style'})
135 {
136 $opt{'draw:style-name'} = $opt{'style'};
137 delete $opt{'style'};
138 }
139 if ($opt{'size'})
140 {
141 $size = $opt{'size'};
142 delete $opt{'size'};
143 }
144 if ($opt{'position'})
145 {
146 $position = $opt{'position'};
147 $opt{'text:anchor-type'} = 'paragraph';
148 delete $opt{'position'};
149 }
150 if ($opt{'link'})
151 {
152 $opt{'xlink:href'} = $opt{'link'};
153 delete $opt{'link'};
154 }
155 if ($opt{'import'})
156 {
157 $import = $opt{'import'};
158 delete $opt{'import'};
159 }
160
161 if ($opt{'page'}) # create appropriate parameters if anchor=page
162 { # and insert before the 1st text element
163 $opt{'text:anchor-type'} = 'page';
164 $opt{'text:anchor-page-number'} = $opt{'page'};
165 $opt{'draw:z-index'} = "1";
166 $element = $firstnode ? # is there a text element ?
167 $self->insertElement # yes, insert before it
168 (
169 $firstnode,
170 $container,
171 position => 'before'
172 )
173 : # no, append to parent element
174 $self->appendElement
175 (
176 $attachment,
177 $container
178 );
179 delete $opt{'page'};
180 }
181 else
182 {
183 if ($path) # append to the given attachment if any
184 {
185 $element = $self->appendElement
186 ($attachment, $container);
187 }
188 else # else append to a new paragraph at the end
189 {
190 my $p = $self->appendElement($attachment, 'text:p');
191 $element = $self->appendElement($p, $container);
192 }
193 }
194  
195 if ($self->{'opendocument'})
196 {
197 my $img = $self->appendElement($element, 'draw:image');
198 foreach my $a (keys %opt)
199 {
200 if ($a =~ /^xlink:/)
201 {
202 $self->setAttribute($img, $a, $opt{$a});
203 delete $opt{$a};
204 }
205 }
206 }
207  
208 $self->setAttributes($element, %opt);
209 $self->setImageDescription($element, $description)
210 if (defined $description);
211 $self->setImageSize($element, $size)
212 if (defined $size);
213 $self->setImagePosition($element, $position)
214 if (defined $position);
215 $self->importImage($element, $import)
216 if (defined $import);
217 return $element;
218 }
219  
220 sub insertImageElement
221 {
222 my $self = shift;
223 return $self->createImageElement(@_);
224 }
225  
226 #-----------------------------------------------------------------------------
227 # image list
228  
229 sub getImageElementList
230 {
231 my $self = shift;
232 return $self->getElementList($self->{'image_xpath'}, @_);
233 }
234  
235 #-----------------------------------------------------------------------------
236 # select an individual image element by name
237  
238 sub selectImageElementByName
239 {
240 my $self = shift;
241 my $text = shift;
242  
243 unless ($self->{'opendocument'})
244 {
245 return $self->selectNodeByXPath
246 ("//draw:image\[\@draw:name=\"$text\"\]", @_);
247 }
248 else
249 {
250 my $frame = $self->selectFrameElementByName($text);
251 my $child = $frame->first_child('draw:image');
252 return $child ? $frame : undef;
253 }
254 }
255  
256 #-----------------------------------------------------------------------------
257 # select a list of image elements by name
258  
259 sub selectImageElementsByName
260 {
261 my $self = shift;
262 return $self->selectElementsByAttribute
263 ($self->{'image_xpath'}, 'draw:name', @_);
264 }
265  
266 #-----------------------------------------------------------------------------
267 # select a list of image elements by description
268  
269 sub selectImageElementsByDescription
270 {
271 my $self = shift;
272 my $filter = shift;
273 my @result = ();
274 foreach my $i ($self->getImageElementList)
275 {
276 my $desc = $self->getXPathValue($i, 'svg:desc');
277 push @result, $i if ($desc =~ /$filter/);
278 }
279 return @result;
280 }
281  
282 #-----------------------------------------------------------------------------
283 # select the 1st image element matching a given description
284  
285 sub selectImageElementByDescription
286 {
287 my $self = shift;
288 my $filter = shift;
289 my @result = ();
290 foreach my $i ($self->getImageElementList)
291 {
292 my $desc = $self->getXPathValue($i, 'svg:desc');
293 return $i if ($desc =~ /$filter/);
294 }
295 return undef;
296 }
297  
298 #-----------------------------------------------------------------------------
299 # gets image element (name or ref, with type checking)
300  
301 sub getImageElement
302 {
303 my $self = shift;
304 my $image = shift;
305 return undef unless $image;
306 my $element = undef;
307 if (ref $image)
308 {
309 $element = $image;
310 }
311 else
312 {
313 $element = ($image =~ /^\//) ?
314 $self->getElement($image, @_) :
315 $self->selectImageElementByName($image, @_);
316 }
317 return undef unless $element;
318 my $name = $element->name;
319 return ($name eq $self->{'image_container'}) ? $element : undef;
320 }
321  
322 #-----------------------------------------------------------------------------
323 # basic image attribute accessor
324  
325 sub imageAttribute
326 {
327 my $self = shift;
328 my $image = shift;
329 my $attribute = shift;
330 my $value = shift;
331 my $element = $self->getImageElement($image);
332 if ($self->{'opendocument'} && ($attribute =~ /^xlink:/))
333 {
334 $element = $element->first_child('draw:image');
335 }
336 return undef unless $element;
337 return (defined $value) ?
338 $self->setAttribute($element, $attribute => $value) :
339 $self->getAttribute($element, $attribute);
340 }
341  
342 #-----------------------------------------------------------------------------
343 # selects image element by image URL
344  
345 sub selectImageElementByLink
346 {
347 my $self = shift;
348 my $link = shift;
349
350 my $node = $self->selectNodeByXPath
351 ("//draw:image\[\@xlink:href=\"$link\"\]", @_);
352 return $self->{'opendocument'} ? $node->parent : $node;
353 }
354  
355 #-----------------------------------------------------------------------------
356 # select image element list by image URL
357  
358 sub selectImageElementsByLink
359 {
360 my $self = shift;
361  
362 if ($self->{'opendocument'})
363 {
364 my @list1 = $self->selectElementsByAttribute
365 ('//draw:image', 'xlink:href', @_);
366 my @list2 = ();
367 foreach my $frame (@list1)
368 {
369 push @list2, $frame if $frame;
370 }
371 return @list2;
372 }
373 else
374 {
375 return $self->selectElementsByAttribute
376 ($self->{'image_xpath'}, 'xlink:href', @_);
377 }
378 }
379  
380 #-----------------------------------------------------------------------------
381 # get/set image URL
382  
383 sub imageLink
384 {
385 my $self = shift;
386 return $self->imageAttribute(shift, 'xlink:href', @_);
387 }
388  
389 #-----------------------------------------------------------------------------
390 # return the internal filepath in canonical form ('Pictures/xxxx')
391  
392 sub getInternalImagePath
393 {
394 my $self = shift;
395 my $image = shift;
396 my $link = $self->imageLink($image);
397 my $tmpl = $self->{'image_fpath'};
398 if ($link && ($link =~ /^$tmpl/))
399 {
400 $link =~ s/^#//;
401 return $link;
402 }
403 else
404 {
405 return undef;
406 }
407 }
408  
409 #-----------------------------------------------------------------------------
410 # return image coordinates
411  
412 sub getImagePosition
413 {
414 my $self = shift;
415 my $image = shift;
416 my $element = $self->getImageElement($image);
417 return $self->getObjectCoordinates($element, @_);
418 }
419  
420 #-----------------------------------------------------------------------------
421 # update image coordinates
422  
423 sub setImagePosition
424 {
425 my $self = shift;
426 my $image = shift;
427 my $element = $self->getImageElement($image);
428 return $self->setObjectCoordinates($element, @_);
429 }
430  
431 #-----------------------------------------------------------------------------
432 # get/set image coordinates
433  
434 sub imagePosition
435 {
436 my $self = shift;
437 my $image = shift;
438 my $x = shift;
439 my $y = shift;
440  
441 return (defined $x) ?
442 $self->setImagePosition($image, $x, $y, @_) :
443 $self->getImagePosition($image);
444 }
445  
446 #-----------------------------------------------------------------------------
447 # get image size
448  
449 sub getImageSize
450 {
451 my $self = shift;
452 my $image = shift;
453 my $element = $self->getImageElement($image);
454 return $self->getObjectSize($element, @_);
455 }
456  
457 #-----------------------------------------------------------------------------
458 # update image size
459  
460 sub setImageSize
461 {
462 my $self = shift;
463 my $image = shift;
464 my $element = $self->getImageElement($image);
465 return $self->setObjectSize($element, @_);
466 }
467  
468 #-----------------------------------------------------------------------------
469 # get/set image size
470  
471 sub imageSize
472 {
473 my $self = shift;
474 my $image = shift;
475 my $w = shift;
476 my $h = shift;
477  
478 return (defined $w) ?
479 $self->setImageSize($image, $w, $h, @_) :
480 $self->getImageSize($image);
481 }
482  
483 #-----------------------------------------------------------------------------
484 # get/set image name
485  
486 sub imageName
487 {
488 my $self = shift;
489 return $self->imageAttribute(shift, 'draw:name', @_);
490 }
491  
492 #-----------------------------------------------------------------------------
493 # get/set image stylename
494  
495 sub imageStyle
496 {
497 my $self = shift;
498 return $self->imageAttribute(shift, 'draw:style-name', @_);
499 }
500  
501 #-----------------------------------------------------------------------------
502 # get image description
503  
504 sub getImageDescription
505 {
506 my $self = shift;
507 my $image = shift;
508 my $element = $self->getImageElement($image);
509 return $self->getObjectDescription($element);
510 }
511  
512 #-----------------------------------------------------------------------------
513 # set/update image description
514  
515 sub setImageDescription
516 {
517 my $self = shift;
518 my $image = shift;
519 my $element = $self->getImageElement($image);
520 return $self->setObjectDescription($element, @_);
521 }
522  
523 #-----------------------------------------------------------------------------
524 # delete image description
525  
526 sub removeImageDescription
527 {
528 my $self = shift;
529 $self->setImageDescription(shift);
530 }
531  
532 #-----------------------------------------------------------------------------
533 # get/set accessor for image description
534  
535 sub imageDescription
536 {
537 my $self = shift;
538 my $image = shift;
539 my $desc = shift;
540 return (defined $desc) ?
541 $self->setImageDescription($image, $desc, @_) :
542 $self->getImageDescription($image, @_);
543 }
544  
545 #-----------------------------------------------------------------------------
546 # export a selected image file from ODF container
547  
548 sub exportImage
549 {
550 my $self = shift;
551 my $element = $self->getImageElement(shift);
552 return undef unless $element;
553 my $path = $self->imageLink($element) or return undef;
554 my $tmpl = $self->{'image_fpath'};
555 unless ($path =~ /^$tmpl/)
556 {
557 warn "[" . __PACKAGE__ . "::exportImage] " .
558 "Image content $path is an external link. " .
559 "Can't be exported\n";
560 return undef;
561 }
562 my $target = shift;
563 unless ($target)
564 {
565 my $name = $self->imageName($element);
566 if ($name)
567 {
568 $path =~ /(\..*$)/;
569 $target = $name . ($1 || '');
570 }
571 else
572 {
573 $target = $path;
574 }
575 }
576 return $self->raw_export($path, $target, @_);
577 }
578  
579 #-----------------------------------------------------------------------------
580 # export all the internal image files, or a subset of them selected by name
581 # return the list of exported files
582  
583 sub exportImages
584 {
585 my $self = shift;
586 my %opt = @_;
587 my $filter = $opt{'filter'} || $opt{'name'} || $opt{'selection'};
588 my $basename = $opt{'path'} || $opt{'target'};
589 my $suffix = $opt{'suffix'} || $opt{'extension'};
590 my $number = defined $opt{'start_count'} ?
591 $opt{'start_count'} : 1;
592 my @list = ();
593 my $count = 0;
594  
595 my @to_export = $filter ?
596 $self->selectImageElementsByName($filter)
597 :
598 $self->getImageElementList();
599  
600 my $tmpl = $self->{'image_fpath'};
601  
602 IMAGE_LOOP: foreach my $image (@to_export)
603 {
604 my $link = $self->imageLink($image);
605 next IMAGE_LOOP unless ($link && ($link =~ /^$tmpl/));
606 my $filename = undef;
607 my $extension = undef;
608 my $target = undef;
609 if (defined $suffix)
610 {
611 $extension = $suffix;
612 }
613 else
614 {
615 $link =~ /(\..*$)/;
616 $extension = $1 || '';
617 }
618 if (defined $basename)
619 {
620 $target = $basename . $number . $extension;
621 }
622 else
623 {
624 my $name = $self->imageName($image) || "Image$number";
625 $target = $name . $extension;
626 }
627 $filename = $self->exportImage($image, $target);
628 push @list, $filename if $filename;
629 $count++; $number++;
630 }
631 return wantarray ? @list : $count;
632 }
633  
634 #-----------------------------------------------------------------------------
635 # import image file
636  
637 sub importImage
638 {
639 my $self = shift;
640 my $element = $self->getImageElement(shift);
641 return undef unless $element;
642 my $filename = shift;
643 my $tmpl = $self->{'image_fpath'};
644 unless ($filename)
645 {
646 my $source = $self->imageLink($element);
647 unless ($source)
648 {
649 warn "[" . __PACKAGE__ . "::importImage] " .
650 "Missing source path\n";
651 return undef;
652 }
653 $source =~ s/%(..)/{ chr(hex($1)) }/eg;
654 $source =~ s/^\.\.[\\\/]/\.\//;
655 $filename = $source;
656 }
657 my ($base, $path, $suffix) =
658 File::Basename::fileparse($filename, '\..*');
659 my $link = shift;
660 if ($link)
661 {
662 $link = $tmpl . $link unless $link =~ /^$tmpl/;
663 $self->imageLink($element, $link);
664 }
665 else
666 {
667 $link = $self->imageLink($element);
668 unless ($link && $link =~ /^$tmpl/)
669 {
670 $link = $tmpl . $base . $suffix;
671 $self->imageLink($element, $link);
672 }
673 }
674 $self->raw_import($link, $filename);
675 return $link;
676 }
677  
678 #-----------------------------------------------------------------------------
679 package OpenOffice::OODoc::Element;
680 #-----------------------------------------------------------------------------
681  
682 sub isImage
683 {
684 my $element = shift;
685 my $name = $element->getName or return undef;
686 if ($name eq 'draw:frame')
687 {
688 my $child = $element->first_child('draw:image');
689 return $child ? 1 : undef;
690 }
691 else
692 {
693 return ($name eq 'draw:image') ? 1 : undef;
694 }
695 }
696  
697 #-----------------------------------------------------------------------------
698 1;