| 1 | #----------------------------------------------------------------------------- | ||
| 2 | # | ||
| 3 | # $Id : Styles.pm 2.027 2010-07-12 JMG$ | ||
| 4 | # | ||
| 5 | # Created and maintained by Jean-Marie Gouarne | ||
| 6 | # Copyright 2010 by Genicorp, S.A. (www.genicorp.com) | ||
| 7 | # | ||
| 8 | #----------------------------------------------------------------------------- | ||
| 9 | |||
| 10 | package OpenOffice::OODoc::Styles; | ||
| 11 | use 5.008_000; | ||
| 12 | use strict; | ||
| 13 | our $VERSION = '2.027'; | ||
| 14 | |||
| 15 | use OpenOffice::OODoc::XPath 2.237; | ||
| 16 | use File::Basename; | ||
| 17 | require Exporter; | ||
| 18 | our @ISA = qw ( Exporter OpenOffice::OODoc::XPath ); | ||
| 19 | our @EXPORT = qw | ||
| 20 | ( | ||
| 21 | odfLoadColorMap ooLoadColorMap | ||
| 22 | oo2rgb rgb2oo rgbColor odfColor | ||
| 23 | ); | ||
| 24 | |||
| 25 | #----------------------------------------------------------------------------- | ||
| 26 | |||
| 27 | our $COLORMAP = undef; | ||
| 28 | our %COLORMAP = | ||
| 29 | ( | ||
| 30 | 'red' => '255,0,0', | ||
| 31 | 'green' => '0,255,0', | ||
| 32 | 'blue' => '0,0,255', | ||
| 33 | 'white' => '255,255,255', | ||
| 34 | 'black' => '0,0,0', | ||
| 35 | 'brown' => '165,42,42', | ||
| 36 | 'cyan' => '0,255,255', | ||
| 37 | 'grey' => '190,190,190', | ||
| 38 | 'magenta' => '255,0,255', | ||
| 39 | 'orange' => '255,165,0', | ||
| 40 | 'pink' => '255,192,203', | ||
| 41 | 'violet' => '238,130,238', | ||
| 42 | 'yellow' => '255,255,0' | ||
| 43 | ); | ||
| 44 | |||
| 45 | #----------------------------------------------------------------------------- | ||
| 46 | |||
| 47 | BEGIN { | ||
| 48 | *odfLoadColorMap = *ooLoadColorMap; | ||
| 49 | *rgbColor = *oo2rgb; | ||
| 50 | *odfColor = *rgb2oo; | ||
| 51 | *getMasterPageElement = *getMasterPage; | ||
| 52 | *getPageMasterElement = *getPageLayoutElement; | ||
| 53 | *getPageMasterAttributes = *getPageLayoutAttributes; | ||
| 54 | *createPageMaster = *createPageLayout; | ||
| 55 | *updatePageMaster = *updatePageLayout; | ||
| 56 | *pageMasterStyle = *pageLayout; | ||
| 57 | } | ||
| 58 | |||
| 59 | #----------------------------------------------------------------------------- | ||
| 60 | # loading a color map from an external file | ||
| 61 | # the file format must be "%d %d %d %s" | ||
| 62 | |||
| 63 | sub ooLoadColorMap | ||
| 64 | { | ||
| 65 | my $filename = shift || $COLORMAP or return undef; | ||
| 66 | unless ( -e $filename && -r $filename ) | ||
| 67 | { | ||
| 68 | warn "[" . __PACKAGE__ . "::ooLoadColorMap] " . | ||
| 69 | "Color map file non existent or unreadable\n"; | ||
| 70 | return undef; | ||
| 71 | } | ||
| 72 | my $r = open COLORS, "<", $filename; | ||
| 73 | unless ($r) | ||
| 74 | { | ||
| 75 | warn "[" . __PACKAGE__ . "::ooLoadColorMap] " . | ||
| 76 | "Error opening $filename\n"; | ||
| 77 | return undef; | ||
| 78 | } | ||
| 79 | while (my $line = <COLORS>) | ||
| 80 | { | ||
| 81 | $line =~ s/^\s*//; $line =~ s/\s*$//; | ||
| 82 | next unless $line =~ /^[0-9]/; | ||
| 83 | $line =~ /(\d*)\s*(\d*)\s*(\d*)\s*(.*)/; | ||
| 84 | my $name = $4; | ||
| 85 | $COLORMAP{$name} = "$1,$2,$3" if $name; | ||
| 86 | } | ||
| 87 | close COLORS; | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | #----------------------------------------------------------------------------- | ||
| 92 | # converting an hexadecimal OOo color code to decimal RGB | ||
| 93 | |||
| 94 | sub oo2rgb | ||
| 95 | { | ||
| 96 | my $hexcolor = shift; return undef unless $hexcolor; | ||
| 97 | return undef unless $hexcolor =~ /^#[0-9A-Fa-f]{6}$/; | ||
| 98 | $hexcolor =~ /#(..)(..)(..)/; | ||
| 99 | my ($red, $green, $blue) = ($1, $2, $3); | ||
| 100 | my @rgb = (hex($red), hex($green), hex($blue)); | ||
| 101 | if (wantarray) | ||
| 102 | { | ||
| 103 | return @rgb; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | my $color = join(",", @rgb); | ||
| 108 | foreach my $k (keys %COLORMAP) | ||
| 109 | { | ||
| 110 | return $k if ($COLORMAP{$k} eq $color); | ||
| 111 | } | ||
| 112 | return $color; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | #----------------------------------------------------------------------------- | ||
| 117 | # converting a decimal RGB expression to an hexadecimal OOo color | ||
| 118 | |||
| 119 | sub rgb2oo | ||
| 120 | { | ||
| 121 | my $colour = shift; | ||
| 122 | my ($red, $green, $blue); | ||
| 123 | if ($colour =~ /,/) | ||
| 124 | { | ||
| 125 | $colour =~ s/ //g; | ||
| 126 | ($red, $green, $blue) = split(",", $colour); | ||
| 127 | } | ||
| 128 | elsif ($colour =~ /^[a-zA-Z]/) | ||
| 129 | { | ||
| 130 | if (defined $COLORMAP{$colour}) | ||
| 131 | { | ||
| 132 | ($red, $green, $blue) = split(",", $COLORMAP{$colour}); | ||
| 133 | } | ||
| 134 | else | ||
| 135 | { | ||
| 136 | return undef; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | elsif ($colour =~ /^#/) | ||
| 140 | { | ||
| 141 | my $rgb = oo2rgb($colour); | ||
| 142 | return undef unless $rgb; | ||
| 143 | my $hexrgb = rgb2oo($rgb); | ||
| 144 | return undef unless $hexrgb; | ||
| 145 | return (lc $hexrgb eq lc $colour) ? $colour : undef; | ||
| 146 | } | ||
| 147 | else | ||
| 148 | { | ||
| 149 | $red = $colour; ($green, $blue) = @_; | ||
| 150 | } | ||
| 151 | return sprintf("#%02x%02x%02x", $red, $green, $blue); | ||
| 152 | } | ||
| 153 | |||
| 154 | #----------------------------------------------------------------------------- | ||
| 155 | package OpenOffice::OODoc::Element; | ||
| 156 | #----------------------------------------------------------------------------- | ||
| 157 | |||
| 158 | sub isStyle | ||
| 159 | { | ||
| 160 | my $element = shift; | ||
| 161 | my $fullname = $element->getName; | ||
| 162 | my ($prefix, $name) = split ':', $fullname; | ||
| 163 | return | ||
| 164 | ( | ||
| 165 | $prefix | ||
| 166 | && | ||
| 167 | (($prefix eq 'style') || ($prefix eq 'number')) | ||
| 168 | && | ||
| 169 | $name | ||
| 170 | && | ||
| 171 | ($name ne 'properties') | ||
| 172 | ) | ||
| 173 | ? 1 : undef; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub isOutlineStyle | ||
| 177 | { | ||
| 178 | my $element = shift; | ||
| 179 | return $element->hasTag('text:outline-level-style'); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub isMasterPage | ||
| 183 | { | ||
| 184 | my $element = shift; | ||
| 185 | return ( | ||
| 186 | $element->isElementNode | ||
| 187 | && | ||
| 188 | $element->getName eq 'style:master-page' | ||
| 189 | ) | ||
| 190 | ? 1 : undef; | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | #----------------------------------------------------------------------------- | ||
| 195 | package OpenOffice::OODoc::Styles; | ||
| 196 | #----------------------------------------------------------------------------- | ||
| 197 | # constructor | ||
| 198 | |||
| 199 | sub new | ||
| 200 | { | ||
| 201 | my $caller = shift; | ||
| 202 | my $class = ref($caller) || $caller; | ||
| 203 | my %options = | ||
| 204 | ( | ||
| 205 | part => 'styles', # XML member | ||
| 206 | @_ | ||
| 207 | ); | ||
| 208 | my $object = $class->SUPER::new(%options); | ||
| 209 | return $object ? | ||
| 210 | bless $object, $class : | ||
| 211 | undef; | ||
| 212 | } | ||
| 213 | |||
| 214 | #----------------------------------------------------------------------------- | ||
| 215 | # get the tag name of the "properties" style sub-element | ||
| 216 | |||
| 217 | sub _properties_tagname | ||
| 218 | { | ||
| 219 | my $self = shift; | ||
| 220 | my $element = shift; | ||
| 221 | my $part = shift; | ||
| 222 | |||
| 223 | my $prefix = $element->getPrefix; | ||
| 224 | if ($prefix eq 'number') | ||
| 225 | { | ||
| 226 | return 'number:number'; | ||
| 227 | } | ||
| 228 | elsif ($self->{'opendocument'}) | ||
| 229 | { | ||
| 230 | unless ($part) | ||
| 231 | { | ||
| 232 | $part = $element->att('style:family'); | ||
| 233 | } | ||
| 234 | return $part ? | ||
| 235 | $prefix . ':' . $part . '-properties' : | ||
| 236 | $element->name() . '-properties'; | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { | ||
| 240 | return 'style:properties'; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | #----------------------------------------------------------------------------- | ||
| 245 | # get the path of an individual style property node | ||
| 246 | |||
| 247 | sub _get_property_path | ||
| 248 | { | ||
| 249 | my $self = shift; | ||
| 250 | my $element = shift; | ||
| 251 | my $nodename = shift; | ||
| 252 | my $part = shift; | ||
| 253 | |||
| 254 | if (($nodename eq 'header') || ($nodename eq 'footer')) | ||
| 255 | { | ||
| 256 | return 'style:' . $nodename . '-style/style:properties'; | ||
| 257 | } | ||
| 258 | my $path = $self->_properties_tagname($element, $part); | ||
| 259 | $path .= ('/style:' . $nodename) if $nodename; | ||
| 260 | return $path; | ||
| 261 | } | ||
| 262 | |||
| 263 | #----------------------------------------------------------------------------- | ||
| 264 | # get a particular node in a main style element | ||
| 265 | |||
| 266 | sub getStyleNode | ||
| 267 | { | ||
| 268 | my $self = shift; | ||
| 269 | my $element = shift; | ||
| 270 | my $nodename = shift; | ||
| 271 | my $xpath = $self->_get_property_path($element, $nodename); | ||
| 272 | return $self->getNodeByXPath($element, $xpath); | ||
| 273 | } | ||
| 274 | |||
| 275 | #----------------------------------------------------------------------------- | ||
| 276 | # create the path for a particular node in a main style element | ||
| 277 | |||
| 278 | sub setStyleNode | ||
| 279 | { | ||
| 280 | my $self = shift; | ||
| 281 | my $element = shift; | ||
| 282 | my $nodename = shift; | ||
| 283 | my $xpath = $self->_get_property_path($element, $nodename); | ||
| 284 | return $self->makeXPath($element, $xpath); | ||
| 285 | } | ||
| 286 | |||
| 287 | #----------------------------------------------------------------------------- | ||
| 288 | # get named styles root element | ||
| 289 | |||
| 290 | sub getNamedStyleRoot | ||
| 291 | { | ||
| 292 | my $self = shift; | ||
| 293 | return $self->getElement($self->{'named_style_path'}, 0); | ||
| 294 | } | ||
| 295 | |||
| 296 | #----------------------------------------------------------------------------- | ||
| 297 | # get automatic styles root element | ||
| 298 | |||
| 299 | sub getAutoStyleRoot | ||
| 300 | { | ||
| 301 | my $self = shift; | ||
| 302 | return $self->getElement($self->{'auto_style_path'}, 0); | ||
| 303 | } | ||
| 304 | |||
| 305 | #----------------------------------------------------------------------------- | ||
| 306 | # get master styles root element | ||
| 307 | |||
| 308 | sub getMasterStyleRoot | ||
| 309 | { | ||
| 310 | my $self = shift; | ||
| 311 | return $self->getElement($self->{'master_style_path'}, 0); | ||
| 312 | } | ||
| 313 | |||
| 314 | #----------------------------------------------------------------------------- | ||
| 315 | # get the root of font declarations | ||
| 316 | |||
| 317 | sub getFontDeclarationBody | ||
| 318 | { | ||
| 319 | my $self = shift; | ||
| 320 | |||
| 321 | my $path = $self->{'opendocument'} ? | ||
| 322 | '//office:font-face-decls' : '//office:font-decls'; | ||
| 323 | return $self->getElement($path, 0); | ||
| 324 | } | ||
| 325 | |||
| 326 | #----------------------------------------------------------------------------- | ||
| 327 | # get a font declaration element | ||
| 328 | |||
| 329 | sub getFontDeclaration | ||
| 330 | { | ||
| 331 | my $self = shift; | ||
| 332 | my $font = shift or return undef; | ||
| 333 | my $tag = $self->{'opendocument'} ? | ||
| 334 | "style:font-face" : "style:font-decl"; | ||
| 335 | if (ref $font) | ||
| 336 | { | ||
| 337 | my $n = $font->name; | ||
| 338 | if ($n && ($n eq $tag)) | ||
| 339 | { | ||
| 340 | return $font; | ||
| 341 | } | ||
| 342 | else | ||
| 343 | { | ||
| 344 | warn "[" . __PACKAGE__ . "::getFontDeclaration] " . | ||
| 345 | "Invalid font declaration element\n"; | ||
| 346 | return undef; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | else | ||
| 350 | { | ||
| 351 | my $context = $self->getFontDeclarationBody; | ||
| 352 | my $path = "//$tag\[\@style:name=\"$font\"]"; | ||
| 353 | return $self->getNodeByXPath($context, $path); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | #----------------------------------------------------------------------------- | ||
| 358 | |||
| 359 | sub getFontDeclarations | ||
| 360 | { | ||
| 361 | my $self = shift; | ||
| 362 | my $context = $self->getFontDeclarationBody; | ||
| 363 | my $path = $self->{'opendocument'} ? | ||
| 364 | '//style:font-face' : 'style:font-decl'; | ||
| 365 | return $self->getNodesByXPath($context, $path); | ||
| 366 | } | ||
| 367 | |||
| 368 | #----------------------------------------------------------------------------- | ||
| 369 | |||
| 370 | sub getFontName | ||
| 371 | { | ||
| 372 | my $self = shift; | ||
| 373 | my $fd = $self->getFontDeclaration(@_) | ||
| 374 | or return undef; | ||
| 375 | return $self->getAttribute($fd, 'style:name'); | ||
| 376 | } | ||
| 377 | |||
| 378 | #----------------------------------------------------------------------------- | ||
| 379 | # imports a copy of an existing font declaration | ||
| 380 | |||
| 381 | sub importFontDeclaration | ||
| 382 | { | ||
| 383 | my $self = shift; | ||
| 384 | my $p1 = shift or return undef; | ||
| 385 | |||
| 386 | my $font_element = undef; | ||
| 387 | |||
| 388 | if (ref $p1) | ||
| 389 | { | ||
| 390 | my $e = undef; | ||
| 391 | if ($p1->isa('OpenOffice::OODoc::Styles')) | ||
| 392 | { # copy from another document | ||
| 393 | my $font_name = shift; | ||
| 394 | $e = $p1->getFontDeclaration($font_name); | ||
| 395 | } | ||
| 396 | else | ||
| 397 | { # replicate from the same document | ||
| 398 | $e = $self->getFontDeclaration($p1); | ||
| 399 | } | ||
| 400 | $font_element = $e->copy if $e; | ||
| 401 | } | ||
| 402 | else | ||
| 403 | { # from anything (we hope XML) | ||
| 404 | $font_element = | ||
| 405 | OpenOffice::OODoc::XPath::new_element($p1); | ||
| 406 | } | ||
| 407 | # check the element type | ||
| 408 | $font_element = $self->getFontDeclaration($font_element); | ||
| 409 | $font_element->paste_last_child($self->getFontDeclarationBody); | ||
| 410 | |||
| 411 | return $font_element; | ||
| 412 | } | ||
| 413 | |||
| 414 | #----------------------------------------------------------------------------- | ||
| 415 | # select a list of style elements matching a given attribute, value pair | ||
| 416 | # $path may be 'auto' or 'named' to search only in automatic or named styles | ||
| 417 | # without args, returns the full style list | ||
| 418 | |||
| 419 | sub selectStyleElementsByAttribute | ||
| 420 | { | ||
| 421 | my $self = shift; | ||
| 422 | my $attribute = shift; | ||
| 423 | my $value = shift; | ||
| 424 | my %opt = | ||
| 425 | ( | ||
| 426 | namespace => 'style', | ||
| 427 | type => 'style', | ||
| 428 | @_ | ||
| 429 | ); | ||
| 430 | my $path = $opt{'category'}; | ||
| 431 | |||
| 432 | return $self->getStyleList unless ($attribute && $value); | ||
| 433 | |||
| 434 | unless ($path) | ||
| 435 | { | ||
| 436 | return ($self->selectElementsByAttribute | ||
| 437 | ( | ||
| 438 | $self->{'named_style_path'} . | ||
| 439 | "/$opt{'namespace'}:$opt{'type'}", | ||
| 440 | $attribute, $value | ||
| 441 | ) | ||
| 442 | , | ||
| 443 | $self->selectElementsByAttribute | ||
| 444 | ( | ||
| 445 | $self->{'auto_style_path'} . | ||
| 446 | "/$opt{'namespace'}:$opt{'type'}", | ||
| 447 | $attribute, $value | ||
| 448 | ) | ||
| 449 | ); | ||
| 450 | } | ||
| 451 | else | ||
| 452 | { | ||
| 453 | $path = lc $path; | ||
| 454 | if ($path =~ /^named/) | ||
| 455 | { | ||
| 456 | $path = $self->{'named_style_path'}; | ||
| 457 | } | ||
| 458 | elsif ($path =~ /^auto/) | ||
| 459 | { | ||
| 460 | $path = $self->{'auto_style_path'}; | ||
| 461 | } | ||
| 462 | else | ||
| 463 | { | ||
| 464 | return undef; | ||
| 465 | } | ||
| 466 | return $self->selectElementsByAttribute | ||
| 467 | ( | ||
| 468 | "$path/$opt{'namespace'}:$opt{'type'}", | ||
| 469 | $attribute, $value | ||
| 470 | ); | ||
| 471 | } | ||
| 472 | } | ||
| 473 | |||
| 474 | #----------------------------------------------------------------------------- | ||
| 475 | # select a style element by name | ||
| 476 | # $path may be 'auto' or 'named' to search only in automatic or named styles | ||
| 477 | |||
| 478 | sub selectStyleElementByAttribute | ||
| 479 | { | ||
| 480 | my $self = shift; | ||
| 481 | my $attribute = shift; | ||
| 482 | my $value = shift; | ||
| 483 | my %opt = | ||
| 484 | ( | ||
| 485 | namespace => 'style', | ||
| 486 | type => 'style', | ||
| 487 | @_ | ||
| 488 | ); | ||
| 489 | |||
| 490 | unless ($attribute) | ||
| 491 | { | ||
| 492 | warn "[" . __PACKAGE__ . | ||
| 493 | "::selectStyleElementByAttribute] Missing attribute\n"; | ||
| 494 | return undef; | ||
| 495 | } | ||
| 496 | |||
| 497 | my $path = $opt{'category'}; | ||
| 498 | unless ($path) | ||
| 499 | { | ||
| 500 | return $self->selectElementByAttribute | ||
| 501 | ( | ||
| 502 | $self->{'named_style_path'} . '/style:style', | ||
| 503 | $attribute, $value | ||
| 504 | ) | ||
| 505 | || | ||
| 506 | $self->selectElementByAttribute | ||
| 507 | ( | ||
| 508 | $self->{'auto_style_path'} . '/style:style', | ||
| 509 | $attribute, $value | ||
| 510 | ); | ||
| 511 | } | ||
| 512 | else | ||
| 513 | { | ||
| 514 | $path = lc $path; | ||
| 515 | if ($path =~ /^named/) | ||
| 516 | { | ||
| 517 | $path = $self->{'named_style_path'}; | ||
| 518 | } | ||
| 519 | elsif ($path =~ /^auto/) | ||
| 520 | { | ||
| 521 | $path = $self->{'auto_style_path'}; | ||
| 522 | } | ||
| 523 | else | ||
| 524 | { | ||
| 525 | return undef; | ||
| 526 | } | ||
| 527 | return $self->selectElementByAttribute | ||
| 528 | ( | ||
| 529 | "$path/$opt{'namespace'}:$opt{'type'}", | ||
| 530 | $attribute, $value | ||
| 531 | ) | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 535 | #----------------------------------------------------------------------------- | ||
| 536 | |||
| 537 | sub selectStyleElementByName | ||
| 538 | { | ||
| 539 | my $self = shift; | ||
| 540 | return $self->selectStyleElementByAttribute('style:name', @_); | ||
| 541 | } | ||
| 542 | |||
| 543 | #----------------------------------------------------------------------------- | ||
| 544 | |||
| 545 | sub selectStyleElementByFamily | ||
| 546 | { | ||
| 547 | my $self = shift; | ||
| 548 | return $self->selectStyleElementByAttribute('style:family', @_); | ||
| 549 | } | ||
| 550 | |||
| 551 | #----------------------------------------------------------------------------- | ||
| 552 | |||
| 553 | sub selectStyleElementsByName | ||
| 554 | { | ||
| 555 | my $self = shift; | ||
| 556 | return $self->selectStyleElementsByAttribute('style:name', @_); | ||
| 557 | } | ||
| 558 | |||
| 559 | #----------------------------------------------------------------------------- | ||
| 560 | |||
| 561 | sub selectStyleElementsByFamily | ||
| 562 | { | ||
| 563 | my $self = shift; | ||
| 564 | return $self->selectStyleElementsByAttribute('style:family', @_); | ||
| 565 | } | ||
| 566 | |||
| 567 | #----------------------------------------------------------------------------- | ||
| 568 | # get style element by exact internal name or display name | ||
| 569 | |||
| 570 | sub getStyleElement | ||
| 571 | { | ||
| 572 | my $self = shift; | ||
| 573 | my $style = shift; | ||
| 574 | return undef unless $style; | ||
| 575 | return $style->isStyle ? $style : undef if ref $style; | ||
| 576 | $style = $self->inputTextConversion($style); | ||
| 577 | my %opt = (retry => 1, @_); | ||
| 578 | if ($opt{'retry'}) | ||
| 579 | { | ||
| 580 | delete $opt{'retry'} | ||
| 581 | unless | ||
| 582 | (($opt{'retry'} eq 1) || ($opt{'retry'} eq 'true')); | ||
| 583 | } | ||
| 584 | |||
| 585 | my $root = undef; | ||
| 586 | my $type = $opt{'type'} || 'style'; | ||
| 587 | my $namespace = $opt{'namespace'} || 'style'; | ||
| 588 | |||
| 589 | if ($opt{'category'}) | ||
| 590 | { | ||
| 591 | my $path = '//office:' ; | ||
| 592 | if ($opt{'category'} =~ /^auto/) | ||
| 593 | { $path .= 'automatic-styles'; } | ||
| 594 | elsif ($opt{'category'} =~ /^named/) | ||
| 595 | { $path .= 'styles'; } | ||
| 596 | else | ||
| 597 | { $path = $opt{'category'}; } | ||
| 598 | $root = $self->getElement($path, 0); | ||
| 599 | unless ($root) | ||
| 600 | { | ||
| 601 | warn "[" . __PACKAGE__ . "::getStyleElement] " . | ||
| 602 | "Unknown search space\n"; | ||
| 603 | return undef; | ||
| 604 | } | ||
| 605 | } | ||
| 606 | my $attr = $self->{'retrieve_by'} || 'name'; | ||
| 607 | my $xpath = "//$namespace" . ':' . | ||
| 608 | "$type\[\@style:$attr\=\"$style\"\]"; | ||
| 609 | my $e = $self->getNodeByXPath($xpath, $root); | ||
| 610 | return $e if ((defined $e) || !$opt{'retry'}); | ||
| 611 | if ($attr eq 'name') | ||
| 612 | { | ||
| 613 | $attr = 'display-name'; | ||
| 614 | } | ||
| 615 | elsif ($attr eq 'display-name') | ||
| 616 | { | ||
| 617 | $attr = 'name'; | ||
| 618 | } | ||
| 619 | else | ||
| 620 | { | ||
| 621 | return undef; | ||
| 622 | } | ||
| 623 | $xpath = "//$namespace" . ':' . | ||
| 624 | "$type\[\@style:$attr\=\"$style\"\]"; | ||
| 625 | return $self->getNodeByXPath($xpath, $root); | ||
| 626 | } | ||
| 627 | |||
| 628 | #----------------------------------------------------------------------------- | ||
| 629 | |||
| 630 | sub getOutlineStyleElement | ||
| 631 | { | ||
| 632 | my $self = shift; | ||
| 633 | my $level = shift or return undef; | ||
| 634 | |||
| 635 | if (ref $level) | ||
| 636 | { | ||
| 637 | return $level->isOutlineStyle ? $level : undef; | ||
| 638 | } | ||
| 639 | |||
| 640 | my $xpath = "//text:outline-level-style\[\@text:level\=\"$level\"\]"; | ||
| 641 | return $self->getNodeByXPath($xpath); | ||
| 642 | } | ||
| 643 | |||
| 644 | #----------------------------------------------------------------------------- | ||
| 645 | |||
| 646 | sub updateOutlineStyle | ||
| 647 | { | ||
| 648 | my $self = shift; | ||
| 649 | my $style = $self->getOutlineStyleElement(shift) | ||
| 650 | or return undef; | ||
| 651 | my %attr = @_; | ||
| 652 | foreach my $k (keys %attr) | ||
| 653 | { | ||
| 654 | unless ($k =~ /:/) | ||
| 655 | { | ||
| 656 | my $v = $attr{$k}; delete $attr{$k}; | ||
| 657 | $k = 'style:' . $k; | ||
| 658 | $attr{$k} = $v; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | return $self->setAttributes($style, %attr); | ||
| 662 | } | ||
| 663 | |||
| 664 | #----------------------------------------------------------------------------- | ||
| 665 | # get the name of the parent style, if any | ||
| 666 | |||
| 667 | sub getParentStyle | ||
| 668 | { | ||
| 669 | my $self = shift; | ||
| 670 | my $style = $self->getStyleElement(@_); | ||
| 671 | unless ($style) | ||
| 672 | { | ||
| 673 | warn "[" . __PACKAGE__ . | ||
| 674 | "::getParentStyle] Unknown style\n"; | ||
| 675 | return undef; | ||
| 676 | } | ||
| 677 | return $self->getAttribute($style, 'style:parent-style-name'); | ||
| 678 | } | ||
| 679 | |||
| 680 | #----------------------------------------------------------------------------- | ||
| 681 | # get the name of the primary ancestor | ||
| 682 | # (returns the style name if it doesn't have any ancestor) | ||
| 683 | |||
| 684 | sub getAncestorStyle | ||
| 685 | { | ||
| 686 | my $self = shift; | ||
| 687 | my $style = $self->getStyleElement(@_); | ||
| 688 | unless ($style) | ||
| 689 | { | ||
| 690 | warn "[" . __PACKAGE__ . | ||
| 691 | "::getAncestorStyle] Unknown style\n"; | ||
| 692 | return undef; | ||
| 693 | } | ||
| 694 | my $name = $self->styleName($style); | ||
| 695 | my $parent_name = $self->getParentStyle($style); | ||
| 696 | |||
| 697 | while ($parent_name) | ||
| 698 | { | ||
| 699 | $name = $parent_name; | ||
| 700 | $style = $self->getStyleElement($name); | ||
| 701 | $parent_name = $style ? | ||
| 702 | $self->getParentStyle($style) : | ||
| 703 | undef; | ||
| 704 | } | ||
| 705 | |||
| 706 | return $name; | ||
| 707 | } | ||
| 708 | |||
| 709 | #----------------------------------------------------------------------------- | ||
| 710 | |||
| 711 | sub styleName | ||
| 712 | { | ||
| 713 | my $self = shift; | ||
| 714 | my $p1 = shift; | ||
| 715 | my $style = undef; | ||
| 716 | my $newname = undef; | ||
| 717 | if (ref $p1) | ||
| 718 | { | ||
| 719 | $style = $self->getStyleElement($p1) or return undef; | ||
| 720 | $newname = shift; | ||
| 721 | } | ||
| 722 | else | ||
| 723 | { | ||
| 724 | my %opt = @_; | ||
| 725 | $style = $self->getStyleElement($p1, %opt) or return undef; | ||
| 726 | $newname = $opt{'newname'}; | ||
| 727 | } | ||
| 728 | $self->setAttribute($style, 'style:name', $newname) if $newname; | ||
| 729 | return $self->getAttribute($style, 'style:name'); | ||
| 730 | } | ||
| 731 | |||
| 732 | #----------------------------------------------------------------------------- | ||
| 733 | |||
| 734 | sub getAutoStyleList | ||
| 735 | { | ||
| 736 | my $self = shift; | ||
| 737 | my %opt = | ||
| 738 | ( | ||
| 739 | namespace => 'style', | ||
| 740 | type => 'style', | ||
| 741 | @_ | ||
| 742 | ); | ||
| 743 | my $path = $self->{'auto_style_path'} . '/' . | ||
| 744 | $opt{'namespace'} . ':' . $opt{'type'}; | ||
| 745 | return $self->getElementList($path); | ||
| 746 | } | ||
| 747 | |||
| 748 | #----------------------------------------------------------------------------- | ||
| 749 | |||
| 750 | sub getNamedStyleList | ||
| 751 | { | ||
| 752 | my $self = shift; | ||
| 753 | my %opt = | ||
| 754 | ( | ||
| 755 | namespace => 'style', | ||
| 756 | type => 'style', | ||
| 757 | @_ | ||
| 758 | ); | ||
| 759 | my $path = $self->{'named_style_path'} . '/' . | ||
| 760 | $opt{'namespace'} . ':' . $opt{'type'}; | ||
| 761 | return $self->getElementList($path); | ||
| 762 | } | ||
| 763 | |||
| 764 | #----------------------------------------------------------------------------- | ||
| 765 | |||
| 766 | sub getMasterStyleList | ||
| 767 | { | ||
| 768 | my $self = shift; | ||
| 769 | my %opt = | ||
| 770 | ( | ||
| 771 | namespace => 'style', | ||
| 772 | type => 'master-page', | ||
| 773 | @_ | ||
| 774 | ); | ||
| 775 | my $path = $self->{'master_style_path'} . '/' . | ||
| 776 | $opt{'namespace'} . ':' . $opt{'type'}; | ||
| 777 | return $self->getElementList($path); | ||
| 778 | } | ||
| 779 | |||
| 780 | #----------------------------------------------------------------------------- | ||
| 781 | |||
| 782 | sub getStyleList | ||
| 783 | { | ||
| 784 | my $self = shift; | ||
| 785 | return ($self->getNamedStyleList(@_), $self->getAutoStyleList(@_)); | ||
| 786 | } | ||
| 787 | |||
| 788 | #----------------------------------------------------------------------------- | ||
| 789 | |||
| 790 | sub styleProperties | ||
| 791 | { | ||
| 792 | my $self = shift; | ||
| 793 | my $style = shift; | ||
| 794 | my %opt = @_; | ||
| 795 | my $namespace = $opt{'namespace'}; | ||
| 796 | my $type = $opt{'type'}; | ||
| 797 | my $path = $opt{'path'}; | ||
| 798 | my $part_name = $opt{'-area'} || $opt{'area'}; | ||
| 799 | my $element = $self->getStyleElement | ||
| 800 | ( | ||
| 801 | $style, | ||
| 802 | namespace => $namespace, | ||
| 803 | type => $type, | ||
| 804 | category => $path | ||
| 805 | ); | ||
| 806 | return undef unless $element; | ||
| 807 | delete $opt{'namespace'}; | ||
| 808 | delete $opt{'type'}; | ||
| 809 | delete $opt{'path'}; | ||
| 810 | delete $opt{'-area'}; | ||
| 811 | delete $opt{'area'}; | ||
| 812 | |||
| 813 | my $change = undef; | ||
| 814 | my $e_prefix = $element->getPrefix; | ||
| 815 | my $tag_name = undef; | ||
| 816 | |||
| 817 | unless ($part_name) | ||
| 818 | { | ||
| 819 | if ($e_prefix eq 'number') | ||
| 820 | { | ||
| 821 | $tag_name = 'number:number'; | ||
| 822 | } | ||
| 823 | elsif ($self->{'opendocument'}) | ||
| 824 | { | ||
| 825 | my $family = $element->att('style:family'); | ||
| 826 | $tag_name = $family ? | ||
| 827 | $e_prefix . ':' . $family . '-properties' : | ||
| 828 | $element->name() . '-properties'; | ||
| 829 | } | ||
| 830 | else | ||
| 831 | { | ||
| 832 | $tag_name = 'style:properties'; | ||
| 833 | } | ||
| 834 | } | ||
| 835 | else | ||
| 836 | { | ||
| 837 | $tag_name = $self->{'opendocument'} ? | ||
| 838 | $e_prefix . ':' . $part_name . '-properties' : | ||
| 839 | 'style:properties'; | ||
| 840 | } | ||
| 841 | |||
| 842 | my $properties = $self->getChildElementByName($element, $tag_name); | ||
| 843 | my %attr = (); | ||
| 844 | foreach my $k (keys %opt) | ||
| 845 | { | ||
| 846 | my $a = $k =~ /:/ ? $k : $e_prefix . ':' . $k; | ||
| 847 | $attr{$a} = $opt{$k}; $change = 1; | ||
| 848 | } | ||
| 849 | if ($change) | ||
| 850 | { | ||
| 851 | $properties = $self->appendElement($element, $tag_name) | ||
| 852 | unless $properties; | ||
| 853 | $self->setAttributes($properties, %attr); | ||
| 854 | } | ||
| 855 | return $properties ? $self->getAttributes($properties) : undef; | ||
| 856 | } | ||
| 857 | |||
| 858 | #----------------------------------------------------------------------------- | ||
| 859 | |||
| 860 | sub getStyleAttributes | ||
| 861 | { | ||
| 862 | my $self = shift; | ||
| 863 | my $name = shift; | ||
| 864 | my %style = (); | ||
| 865 | my $element = $self->getStyleElement($name, @_); | ||
| 866 | unless ($element) | ||
| 867 | { | ||
| 868 | warn "[" . __PACKAGE__ . | ||
| 869 | "::getStyleAttributes] Unknown style\n"; | ||
| 870 | return %style; | ||
| 871 | } | ||
| 872 | %{$style{'properties'}} = $self->styleProperties($element) | ||
| 873 | if $self->styleProperties($element); | ||
| 874 | %{$style{'references'}} = $self->getAttributes($element); | ||
| 875 | return %style; | ||
| 876 | } | ||
| 877 | |||
| 878 | #----------------------------------------------------------------------------- | ||
| 879 | |||
| 880 | sub getDefaultStyleElement | ||
| 881 | { | ||
| 882 | my $self = shift; | ||
| 883 | my $style = shift; | ||
| 884 | if (ref $style) | ||
| 885 | { | ||
| 886 | return ($style->getName eq 'style:default-style') ? | ||
| 887 | $style : undef; | ||
| 888 | } | ||
| 889 | else | ||
| 890 | { | ||
| 891 | return $self->getNodeByXPath | ||
| 892 | ("//style:default-style\[\@style:family=\"$style\"\]", @_); | ||
| 893 | } | ||
| 894 | } | ||
| 895 | |||
| 896 | #----------------------------------------------------------------------------- | ||
| 897 | |||
| 898 | sub getDefaultStyleAttributes | ||
| 899 | { | ||
| 900 | my $self = shift; | ||
| 901 | my $style = $self->getDefaultStyleElement(@_); | ||
| 902 | unless ($style) | ||
| 903 | { | ||
| 904 | warn "[" . __PACKAGE__ . "::getDefaultStyleAttributes] " . | ||
| 905 | "No available default style in the context\n"; | ||
| 906 | return undef; | ||
| 907 | } | ||
| 908 | return $self->getStyleAttributes($style, @_); | ||
| 909 | } | ||
| 910 | |||
| 911 | #----------------------------------------------------------------------------- | ||
| 912 | # create a new style with given $name and %options | ||
| 913 | # by default, the style is regarded as an 'named style' if $self is | ||
| 914 | # 'styles.xml'but if $opt{path} or $opt{category} is 'auto', then | ||
| 915 | # the style is inserted as an automatic style | ||
| 916 | # if $self is a 'content.xml' object, the style is automatic | ||
| 917 | |||
| 918 | sub createStyle | ||
| 919 | { | ||
| 920 | my $self = shift; | ||
| 921 | my $name = shift; | ||
| 922 | |||
| 923 | unless ($name) | ||
| 924 | { | ||
| 925 | warn "[" . __PACKAGE__ . "::createStyle] " . | ||
| 926 | "Missing style name\n"; | ||
| 927 | return undef; | ||
| 928 | } | ||
| 929 | my %opt = (check => 'false', @_); | ||
| 930 | |||
| 931 | my $check = lc $opt{'check'} eq 'true'; delete $opt{'check'}; | ||
| 932 | my $replace = lc $opt{'replace'} eq 'true'; delete $opt{'replace'}; | ||
| 933 | if ($check || $replace) | ||
| 934 | { | ||
| 935 | my $old = $self->getStyleElement($name, %opt); | ||
| 936 | if (defined $old) | ||
| 937 | { | ||
| 938 | unless ($replace) | ||
| 939 | { | ||
| 940 | warn "[" . __PACKAGE__ . "::createStyle] " . | ||
| 941 | "Style $name exists\n"; | ||
| 942 | return undef; | ||
| 943 | } | ||
| 944 | else | ||
| 945 | { | ||
| 946 | $self->removeElement($old); | ||
| 947 | } | ||
| 948 | } | ||
| 949 | } | ||
| 950 | |||
| 951 | my $element = undef; | ||
| 952 | my $path = undef; | ||
| 953 | my $type = $opt{'type'} || 'style'; | ||
| 954 | |||
| 955 | my $namespace = $opt{'namespace'} || 'style'; | ||
| 956 | my $context = $self->{'xpath'}; | ||
| 957 | |||
| 958 | my $part_name = $self->getPartName; | ||
| 959 | if ($part_name eq 'content') | ||
| 960 | { | ||
| 961 | $path = $self->{'auto_style_path'}; | ||
| 962 | } | ||
| 963 | elsif ($part_name eq 'styles') | ||
| 964 | { | ||
| 965 | $path = | ||
| 966 | ($opt{'path'} && $opt{'path'} =~ /auto/) | ||
| 967 | || | ||
| 968 | ($opt{'category'} && $opt{'category'} =~ /auto/) | ||
| 969 | ? | ||
| 970 | $self->{'auto_style_path'} : | ||
| 971 | $self->{'named_style_path'}; | ||
| 972 | } | ||
| 973 | else | ||
| 974 | { | ||
| 975 | warn "[" . __PACKAGE__ . "::createStyle] " . | ||
| 976 | "Style creation is not allowed in the area\n"; | ||
| 977 | return undef; | ||
| 978 | } | ||
| 979 | |||
| 980 | if ($opt{'prototype'} || $opt{'source'}) | ||
| 981 | { | ||
| 982 | my $p = $opt{'prototype'} || $name; | ||
| 983 | delete $opt{'prototype'}; | ||
| 984 | my $source = $opt{'source'} || $self; | ||
| 985 | delete $opt{'source'}; | ||
| 986 | my $proto = $source->getStyleElement($p, %opt); | ||
| 987 | unless ($proto) | ||
| 988 | { | ||
| 989 | warn "[" . __PACKAGE__ . "::createStyle] " . | ||
| 990 | "Unknown prototype style\n"; | ||
| 991 | return undef; | ||
| 992 | } | ||
| 993 | $element = $proto->copy; | ||
| 994 | } | ||
| 995 | else | ||
| 996 | { | ||
| 997 | $element = $self->createElement($namespace . ':' . $type); | ||
| 998 | } | ||
| 999 | my $attachment = $self->getElement($path, 0); | ||
| 1000 | $element->paste_last_child($attachment); | ||
| 1001 | if ($type eq 'default-style') | ||
| 1002 | { $opt{'family'} = $name; } | ||
| 1003 | elsif ($type eq 'number-style') | ||
| 1004 | { | ||
| 1005 | $opt{'references'}{'style:name'} = $name; | ||
| 1006 | $opt{'family'} = 'data-style'; | ||
| 1007 | } | ||
| 1008 | else | ||
| 1009 | { $opt{'references'}{'style:name'} = $name; } | ||
| 1010 | |||
| 1011 | delete $opt{'type'}; | ||
| 1012 | delete $opt{'namespace'}; | ||
| 1013 | delete $opt{'path'}; | ||
| 1014 | delete $opt{'category'}; | ||
| 1015 | |||
| 1016 | $self->updateStyle($element, %opt); | ||
| 1017 | return $element; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | #----------------------------------------------------------------------------- | ||
| 1021 | # set style attributes | ||
| 1022 | |||
| 1023 | sub updateStyle | ||
| 1024 | { | ||
| 1025 | my $self = shift; | ||
| 1026 | my $style = shift; | ||
| 1027 | my %opt = @_; | ||
| 1028 | my $namespace = $opt{'namespace'}; | ||
| 1029 | my $type = $opt{'type'}; | ||
| 1030 | my $path = $opt{'path'} || $opt{'category'}; | ||
| 1031 | my $element = $self->getStyleElement | ||
| 1032 | ( | ||
| 1033 | $style, | ||
| 1034 | namespace => $namespace, | ||
| 1035 | type => $type, | ||
| 1036 | category => $path | ||
| 1037 | ); | ||
| 1038 | |||
| 1039 | unless ($element) | ||
| 1040 | { | ||
| 1041 | warn "[" . __PACKAGE__ . "::updateStyle] " . | ||
| 1042 | "Unknown style\n"; | ||
| 1043 | return undef; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | if ($opt{'prototype'}) | ||
| 1047 | { | ||
| 1048 | my $sv_name = $self->getAttribute($element, 'style:name'); | ||
| 1049 | my %proto = $self->getStyleAttributes($opt{'prototype'}); | ||
| 1050 | while (my ($key, $value) = each %proto) | ||
| 1051 | { | ||
| 1052 | if (ref $value) | ||
| 1053 | { | ||
| 1054 | while (my ($k, $v) = each %{$value}) | ||
| 1055 | { | ||
| 1056 | $opt{$key}{$k} = $v | ||
| 1057 | unless $opt{$key}{$k}; | ||
| 1058 | } | ||
| 1059 | } | ||
| 1060 | else | ||
| 1061 | { | ||
| 1062 | $opt{$key} = $value unless $opt{$key}; | ||
| 1063 | } | ||
| 1064 | } | ||
| 1065 | delete $opt{'prototype'}; | ||
| 1066 | $opt{'references'}{'style:name'} = $sv_name if $sv_name; | ||
| 1067 | } | ||
| 1068 | $opt{'references'}{'style:family'} = $opt{'family'} | ||
| 1069 | if $opt{'family'}; | ||
| 1070 | $opt{'references'}{'style:class'} = $opt{'class'} | ||
| 1071 | if $opt{'class'}; | ||
| 1072 | $opt{'references'}{'style:display-name'} = $opt{'display-name'} | ||
| 1073 | if $opt{'display-name'}; | ||
| 1074 | if ($opt{'next'}) | ||
| 1075 | { | ||
| 1076 | $opt{'references'}{'style:next-style-name'} = | ||
| 1077 | ref $opt{'next'} ? | ||
| 1078 | $self->styleName($opt{'next'}) : | ||
| 1079 | $opt{'next'}; | ||
| 1080 | } | ||
| 1081 | if ($opt{'parent'}) | ||
| 1082 | { | ||
| 1083 | $opt{'references'}{'style:parent-style-name'} = | ||
| 1084 | ref $opt{'parent'} ? | ||
| 1085 | $self->styleName($opt{'parent'}) : | ||
| 1086 | $opt{'parent'}; | ||
| 1087 | } | ||
| 1088 | $self->setAttributes($element, %{$opt{'references'}}); | ||
| 1089 | $self->styleProperties($element, %{$opt{'properties'}}) | ||
| 1090 | if ($opt{'properties'}); | ||
| 1091 | |||
| 1092 | return $self->getStyleAttributes($element); | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | #----------------------------------------------------------------------------- | ||
| 1096 | # get a page layout descriptor (pagemaster) element. | ||
| 1097 | # the argument $page could be already a page layout; | ||
| 1098 | # if $page appears to be a master page (or master page name), the method | ||
| 1099 | # tries to get the linked page layout. | ||
| 1100 | |||
| 1101 | sub getPageLayoutElement | ||
| 1102 | { | ||
| 1103 | my $self = shift; | ||
| 1104 | my $page = shift; | ||
| 1105 | my $name = undef; | ||
| 1106 | my $pagemaster = undef; | ||
| 1107 | |||
| 1108 | my $l = $self->{'opendocument'} ? 'layout' : 'master'; | ||
| 1109 | my $layout_tag_name = 'style:page-' . $l; | ||
| 1110 | my $layout_key = $layout_tag_name . '-name'; | ||
| 1111 | my $layout_path = '//' . $layout_tag_name; | ||
| 1112 | |||
| 1113 | if (ref $page) | ||
| 1114 | { # it is an element | ||
| 1115 | $name = $page->getName || ""; | ||
| 1116 | # is it pagemaster element ? | ||
| 1117 | if ($name eq $layout_tag_name) | ||
| 1118 | { # OK, return it | ||
| 1119 | return $page; | ||
| 1120 | } | ||
| 1121 | # is it a master page element ? | ||
| 1122 | elsif ($name eq 'style:master-page') | ||
| 1123 | { # yes, get the page master name | ||
| 1124 | $page = $self->getAttribute($page, $layout_key) | ||
| 1125 | or return undef; | ||
| 1126 | } | ||
| 1127 | } | ||
| 1128 | # here we have a name | ||
| 1129 | $pagemaster = $self->selectElementByAttribute | ||
| 1130 | ($layout_path, 'style:name', $page); | ||
| 1131 | return $pagemaster if $pagemaster; | ||
| 1132 | # it's not a page master name, | ||
| 1133 | # so we try it as a master page name | ||
| 1134 | my $masterpage = $self->selectElementByAttribute | ||
| 1135 | ('//style:master-page', 'style:name', $page) | ||
| 1136 | or return undef; | ||
| 1137 | # great! we got the master page, so get the page master name | ||
| 1138 | $name = $self->getAttribute($masterpage, $layout_key); | ||
| 1139 | # and cross the fingers | ||
| 1140 | return $self->selectElementByAttribute | ||
| 1141 | ($layout_path, 'style:name', $name); | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | #----------------------------------------------------------------------------- | ||
| 1145 | |||
| 1146 | sub getPageLayoutAttributes | ||
| 1147 | { | ||
| 1148 | my $self = shift; | ||
| 1149 | my %attributes = (); | ||
| 1150 | my $pagemaster = $self->getPageLayoutElement(shift); | ||
| 1151 | unless ($pagemaster) | ||
| 1152 | { | ||
| 1153 | warn "[" . __PACKAGE__ . "::getPageLayoutAttributes] " . | ||
| 1154 | "Unknown page master\n"; | ||
| 1155 | return %attributes; | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | my $node = undef; | ||
| 1159 | %{$attributes{'references'}} = $self->getAttributes($pagemaster); | ||
| 1160 | %{$attributes{'properties'}} = $self->styleProperties($pagemaster); | ||
| 1161 | $node = $self->getStyleNode($pagemaster, 'background-image'); | ||
| 1162 | %{$attributes{'background-image'}} = $node ? | ||
| 1163 | $self->getAttributes($node) : (); | ||
| 1164 | $node = $self->getStyleNode($pagemaster, 'footnote-sep'); | ||
| 1165 | %{$attributes{'footnote-sep'}} = $node ? | ||
| 1166 | $self->getAttributes($node) : (); | ||
| 1167 | $node = $self->getStyleNode($pagemaster, 'header'); | ||
| 1168 | %{$attributes{'header'}} = $node ? | ||
| 1169 | $self->getAttributes($node) : (); | ||
| 1170 | $node = $self->getStyleNode($pagemaster, 'footer'); | ||
| 1171 | %{$attributes{'footer'}} = $node ? | ||
| 1172 | $self->getAttributes($node) : (); | ||
| 1173 | |||
| 1174 | return %attributes; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | #----------------------------------------------------------------------------- | ||
| 1178 | |||
| 1179 | sub createPageLayout | ||
| 1180 | { | ||
| 1181 | my $self = shift; | ||
| 1182 | my $name = shift; | ||
| 1183 | my $layout_name = $self->{'opendocument'} ? | ||
| 1184 | 'page-layout' : 'page-master'; | ||
| 1185 | my %opt = | ||
| 1186 | ( | ||
| 1187 | category => 'auto', | ||
| 1188 | namespace => 'style', | ||
| 1189 | type => $layout_name, | ||
| 1190 | @_ | ||
| 1191 | ); | ||
| 1192 | my $pagemaster = undef; | ||
| 1193 | |||
| 1194 | if ($opt{'prototype'}) | ||
| 1195 | { | ||
| 1196 | my $proto = $self->getStyleElement | ||
| 1197 | ($opt{'prototype'}, type => $layout_name); | ||
| 1198 | unless ($proto) | ||
| 1199 | { | ||
| 1200 | warn "[" . __PACKAGE__ . "::createPageMaster] " . | ||
| 1201 | "Improper prototype style\n"; | ||
| 1202 | return undef; | ||
| 1203 | } | ||
| 1204 | my $attachment = $self->getAutoStyleRoot; | ||
| 1205 | $pagemaster = $self->replicateElement($proto, $attachment); | ||
| 1206 | $self->setAttribute($pagemaster, 'style:name', $name); | ||
| 1207 | delete $opt{'prototype'}; | ||
| 1208 | } | ||
| 1209 | else | ||
| 1210 | { | ||
| 1211 | $pagemaster = $self->createStyle($name, %opt) or return undef; | ||
| 1212 | } | ||
| 1213 | |||
| 1214 | delete $opt{'namespace'}; | ||
| 1215 | delete $opt{'type'}; | ||
| 1216 | delete $opt{'category'}; | ||
| 1217 | |||
| 1218 | $self->updatePageMaster($pagemaster, %opt); | ||
| 1219 | return $pagemaster; | ||
| 1220 | } | ||
| 1221 | |||
| 1222 | #----------------------------------------------------------------------------- | ||
| 1223 | |||
| 1224 | sub updatePageLayout | ||
| 1225 | { | ||
| 1226 | my $self = shift; | ||
| 1227 | my $pagemaster = $self->getPageLayoutElement(shift) or return undef; | ||
| 1228 | my %opt = @_; | ||
| 1229 | if ($opt{'prototype'}) | ||
| 1230 | { | ||
| 1231 | my $sv_name = $self->getAttribute($pagemaster, 'style:name'); | ||
| 1232 | my %proto = $self->getPageLayoutAttributes($opt{'prototype'}); | ||
| 1233 | while (my ($key, $value) = each %proto) | ||
| 1234 | { | ||
| 1235 | if (ref $value) | ||
| 1236 | { | ||
| 1237 | while (my ($k, $v) = each %{$value}) | ||
| 1238 | { | ||
| 1239 | $opt{$key}{$k} = $v | ||
| 1240 | unless $opt{$key}{$k}; | ||
| 1241 | } | ||
| 1242 | } | ||
| 1243 | else | ||
| 1244 | { | ||
| 1245 | $opt{$key} = $value unless $opt{$key}; | ||
| 1246 | } | ||
| 1247 | } | ||
| 1248 | delete $opt{'prototype'}; | ||
| 1249 | $opt{'references'}{'style:name'} = $sv_name if $sv_name; | ||
| 1250 | } | ||
| 1251 | $self->setAttributes($pagemaster, %{$opt{'references'}}); | ||
| 1252 | delete $opt{'references'}; | ||
| 1253 | $self->styleProperties($pagemaster, %{$opt{'properties'}}); | ||
| 1254 | delete $opt{'properties'}; | ||
| 1255 | my %p = (); | ||
| 1256 | $p{'background-image'} = | ||
| 1257 | $self->setStyleNode($pagemaster, 'background-image'); | ||
| 1258 | $p{'footnote-sep'} = | ||
| 1259 | $self->setStyleNode($pagemaster, 'footnote-sep'); | ||
| 1260 | $p{'header'} = | ||
| 1261 | $self->setStyleNode($pagemaster, 'header'); | ||
| 1262 | $p{'footer'} = | ||
| 1263 | $self->setStyleNode($pagemaster, 'footer'); | ||
| 1264 | |||
| 1265 | foreach my $k (keys %opt) | ||
| 1266 | { | ||
| 1267 | my $node = $p{$k} or next; | ||
| 1268 | my %parm = %{$opt{$k}}; my %attr = (); | ||
| 1269 | foreach my $name (keys %parm) | ||
| 1270 | { | ||
| 1271 | if ($name eq 'link') | ||
| 1272 | { | ||
| 1273 | $attr{'xlink:href'} = $parm{'link'}; | ||
| 1274 | } | ||
| 1275 | elsif (! ($name =~ /:/)) | ||
| 1276 | { | ||
| 1277 | $attr{"style:$name"} = $parm{$name}; | ||
| 1278 | } | ||
| 1279 | else | ||
| 1280 | { | ||
| 1281 | $attr{$name} = $parm{$name}; | ||
| 1282 | } | ||
| 1283 | } | ||
| 1284 | $self->setAttributes($node, %attr); | ||
| 1285 | } | ||
| 1286 | |||
| 1287 | return $self->getPageLayoutAttributes($pagemaster); | ||
| 1288 | } | ||
| 1289 | |||
| 1290 | #----------------------------------------------------------------------------- | ||
| 1291 | # switch page orientation (portrait -> landscape or landscape -> portrait) | ||
| 1292 | |||
| 1293 | sub switchPageOrientation | ||
| 1294 | { | ||
| 1295 | my $self = shift; | ||
| 1296 | my $page = $self->getPageLayoutElement(shift); | ||
| 1297 | my %op = $self->styleProperties($page); | ||
| 1298 | my %np = (); | ||
| 1299 | $np{'fo:page-width'} = $op{'fo:page-height'}; | ||
| 1300 | $np{'fo:page-height'} = $op{'fo:page-width'}; | ||
| 1301 | my $o = $op{'style:print-orientation'}; | ||
| 1302 | if ($o) | ||
| 1303 | { | ||
| 1304 | if ($o eq 'portrait') | ||
| 1305 | { | ||
| 1306 | $np{'style:print-orientation'} = 'landscape'; | ||
| 1307 | } | ||
| 1308 | elsif ($o eq 'landscape') | ||
| 1309 | { | ||
| 1310 | $np{'style:print-orientation'} = 'portrait'; | ||
| 1311 | } | ||
| 1312 | } | ||
| 1313 | return $self->styleProperties($page, %np); | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | #----------------------------------------------------------------------------- | ||
| 1317 | # get the page content for a given page style | ||
| 1318 | |||
| 1319 | sub getMasterPage | ||
| 1320 | { | ||
| 1321 | my $self = shift; | ||
| 1322 | my $name = shift; | ||
| 1323 | if (ref $name) | ||
| 1324 | { | ||
| 1325 | return $name->getName eq 'style:master-page' ? | ||
| 1326 | $name : undef; | ||
| 1327 | } | ||
| 1328 | else | ||
| 1329 | { | ||
| 1330 | $name = $self->inputTextConversion($name); | ||
| 1331 | return $self->getNodeByXPath | ||
| 1332 | ( | ||
| 1333 | "//style:master-page[\@style:name=\"$name\"]", | ||
| 1334 | $self->getRoot | ||
| 1335 | ); | ||
| 1336 | } | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | #----------------------------------------------------------------------------- | ||
| 1340 | # get/set the page master name of a given master page | ||
| 1341 | |||
| 1342 | sub pageLayout | ||
| 1343 | { | ||
| 1344 | my $self = shift; | ||
| 1345 | my $masterpage = $self->getMasterPageElement(shift) or return undef; | ||
| 1346 | my $pagemaster = shift; | ||
| 1347 | my $ln = $self->{'opendocument'} ? 'layout' : 'master'; | ||
| 1348 | my $layout_key = 'style:page-' . $ln . '-name'; | ||
| 1349 | unless ($pagemaster) | ||
| 1350 | { | ||
| 1351 | return $self->getAttribute($masterpage, $layout_key); | ||
| 1352 | } | ||
| 1353 | else | ||
| 1354 | { | ||
| 1355 | my $pm_name = ref $pagemaster ? | ||
| 1356 | $self->getAttribute($pagemaster, 'style:name') : | ||
| 1357 | $pagemaster; | ||
| 1358 | $self->setAttribute($masterpage, $layout_key => $pm_name); | ||
| 1359 | return $pm_name; | ||
| 1360 | } | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | #----------------------------------------------------------------------------- | ||
| 1364 | # get the background image node in a given page master | ||
| 1365 | |||
| 1366 | sub getBackgroundImageElement | ||
| 1367 | { | ||
| 1368 | my $self = shift; | ||
| 1369 | my $page = shift; | ||
| 1370 | my $pagemaster = $self->getPageLayoutElement($page); | ||
| 1371 | unless ($pagemaster) | ||
| 1372 | { | ||
| 1373 | my $masterpage = $self->getMasterPageElement($page) | ||
| 1374 | or return undef; | ||
| 1375 | my $name = $self->pageLayout($masterpage); | ||
| 1376 | $pagemaster = $self->getPageLayoutElement($name) | ||
| 1377 | or return undef; | ||
| 1378 | } | ||
| 1379 | return $self->getStyleNode($pagemaster, 'background-image'); | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | #----------------------------------------------------------------------------- | ||
| 1383 | # get/set a background image link | ||
| 1384 | |||
| 1385 | sub backgroundImageLink | ||
| 1386 | { | ||
| 1387 | my $self = shift; | ||
| 1388 | my $page = shift; | ||
| 1389 | my $pagemaster = $self->getPageLayoutElement($page); | ||
| 1390 | unless ($pagemaster) | ||
| 1391 | { | ||
| 1392 | my $masterpage = $self->getMasterPageElement($page) | ||
| 1393 | or return undef; | ||
| 1394 | my $name = $self->pageLayout($masterpage); | ||
| 1395 | $pagemaster = $self->getPageLayoutElement($name) | ||
| 1396 | or return undef; | ||
| 1397 | } | ||
| 1398 | my $newlink = shift; | ||
| 1399 | my $node = $self->getStyleNode($pagemaster, 'background-image'); | ||
| 1400 | unless (defined $newlink) | ||
| 1401 | { | ||
| 1402 | return $node ? | ||
| 1403 | $self->getAttribute($node, 'xlink:href') : | ||
| 1404 | undef; | ||
| 1405 | } | ||
| 1406 | else | ||
| 1407 | { | ||
| 1408 | my $xpath = $self->_get_property_path | ||
| 1409 | ($pagemaster, 'background-image') . | ||
| 1410 | '[@xlink:href="' . $newlink . '"]'; | ||
| 1411 | |||
| 1412 | return $self->makeXPath($pagemaster, $xpath); | ||
| 1413 | } | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | #----------------------------------------------------------------------------- | ||
| 1417 | |||
| 1418 | sub getBackgroundImageAttributes | ||
| 1419 | { | ||
| 1420 | my $self = shift; | ||
| 1421 | my $node = $self->getBackgroundImageElement(@_) | ||
| 1422 | or return undef; | ||
| 1423 | return $self->getAttributes($node); | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | #----------------------------------------------------------------------------- | ||
| 1427 | # create or update a backgound image element associated to a given pagemaster | ||
| 1428 | |||
| 1429 | sub setBackgroundImage | ||
| 1430 | { | ||
| 1431 | my $self = shift; | ||
| 1432 | my $page = shift; | ||
| 1433 | my $pagemaster = $self->getPageLayoutElement($page); | ||
| 1434 | unless ($pagemaster) | ||
| 1435 | { | ||
| 1436 | my $masterpage = $self->getMasterPageElement($page) | ||
| 1437 | or return undef; | ||
| 1438 | my $name = $self->pageLayout($masterpage); | ||
| 1439 | $pagemaster = $self->getPageLayoutElement($name) | ||
| 1440 | or return undef; | ||
| 1441 | } | ||
| 1442 | my %opt = | ||
| 1443 | ( | ||
| 1444 | 'style:position' => 'center center', | ||
| 1445 | 'style:repeat' => 'no-repeat', | ||
| 1446 | 'xlink:type' => 'simple', | ||
| 1447 | 'xlink:actuate' => 'onLoad', | ||
| 1448 | @_ | ||
| 1449 | ); | ||
| 1450 | |||
| 1451 | my $node = $self->makeXPath | ||
| 1452 | ( | ||
| 1453 | $pagemaster, | ||
| 1454 | $self->_get_property_path | ||
| 1455 | ($pagemaster, 'background-image') | ||
| 1456 | ) | ||
| 1457 | or return undef; | ||
| 1458 | if ($opt{'link'}) | ||
| 1459 | { | ||
| 1460 | $opt{'xlink:href'} = $opt{'link'}; | ||
| 1461 | delete $opt{'link'}; | ||
| 1462 | } | ||
| 1463 | if ($opt{'import'}) | ||
| 1464 | { | ||
| 1465 | $self->importBackgroundImage($pagemaster, $opt{'import'}); | ||
| 1466 | delete $opt{'import'}; | ||
| 1467 | } | ||
| 1468 | $self->setAttributes($node, %opt); | ||
| 1469 | return $node; | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | #----------------------------------------------------------------------------- | ||
| 1473 | |||
| 1474 | sub exportBackgroundImage | ||
| 1475 | { | ||
| 1476 | my $self = shift; | ||
| 1477 | my $source = $self->backgroundImageLink(shift) | ||
| 1478 | or return undef; | ||
| 1479 | $self->raw_export($source, @_); | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | #----------------------------------------------------------------------------- | ||
| 1483 | |||
| 1484 | sub importBackgroundImage | ||
| 1485 | { | ||
| 1486 | my $self = shift; | ||
| 1487 | my $page = shift; | ||
| 1488 | my $pagemaster = $self->getPageLayoutElement($page); | ||
| 1489 | unless ($pagemaster) | ||
| 1490 | { | ||
| 1491 | my $masterpage = $self->getMasterPageElement($page) | ||
| 1492 | or return undef; | ||
| 1493 | my $name = $self->pageLayout($masterpage); | ||
| 1494 | $pagemaster = $self->getPageLayoutElement($name) | ||
| 1495 | or return undef; | ||
| 1496 | } | ||
| 1497 | my $filename = shift; | ||
| 1498 | unless ($filename) | ||
| 1499 | { | ||
| 1500 | warn "[" . __PACKAGE__ . "::importBackgroundImage] " . | ||
| 1501 | "No source file name\n"; | ||
| 1502 | return undef; | ||
| 1503 | } | ||
| 1504 | my ($base, $path, $suffix) = | ||
| 1505 | File::Basename::fileparse($filename, '\..*'); | ||
| 1506 | |||
| 1507 | my $link = shift; | ||
| 1508 | my $fpath = $self->{'image_fpath'}; | ||
| 1509 | if ($link) | ||
| 1510 | { | ||
| 1511 | $link = $fpath . $link unless $link =~ /^$fpath/; | ||
| 1512 | $self->backgroundImageLink($pagemaster, $link); | ||
| 1513 | } | ||
| 1514 | else | ||
| 1515 | { | ||
| 1516 | $link = $self->backgroundImageLink($pagemaster); | ||
| 1517 | unless ($link && $link =~ /^$fpath/) | ||
| 1518 | { | ||
| 1519 | $link = $fpath . $base . $suffix; | ||
| 1520 | $self->backgroundImageLink($pagemaster, $link); | ||
| 1521 | } | ||
| 1522 | } | ||
| 1523 | $self->raw_import($link, $filename); | ||
| 1524 | return $link; | ||
| 1525 | } | ||
| 1526 | |||
| 1527 | #----------------------------------------------------------------------------- | ||
| 1528 | |||
| 1529 | sub createMasterPage | ||
| 1530 | { | ||
| 1531 | my $self = shift; | ||
| 1532 | my $name = shift; | ||
| 1533 | my $element = $self->getMasterPageElement($name); | ||
| 1534 | if ($element) | ||
| 1535 | { | ||
| 1536 | warn "[" . __PACKAGE__ . "::createMasterPage] " . | ||
| 1537 | "Master page $name exists\n"; | ||
| 1538 | return undef; | ||
| 1539 | } | ||
| 1540 | my %opt = @_; | ||
| 1541 | my $root = $self->getElement('//office:master-styles', 0); | ||
| 1542 | unless ($root) | ||
| 1543 | { | ||
| 1544 | warn "[" . __PACKAGE__ . "::createMasterPage] " . | ||
| 1545 | "No master styles space in the document\n"; | ||
| 1546 | return undef; | ||
| 1547 | } | ||
| 1548 | |||
| 1549 | $opt{'style:name'} = $name; | ||
| 1550 | my $page_layout = $opt{'layout'} || | ||
| 1551 | $opt{'page-layout'} || | ||
| 1552 | $opt{'page-master'}; | ||
| 1553 | if ($page_layout) | ||
| 1554 | { | ||
| 1555 | my $ln = $self->{'opendocument'} ? 'layout' : 'master'; | ||
| 1556 | my $layout_key = 'style:page-' . $ln . '-name'; | ||
| 1557 | $opt{$layout_key} = $page_layout; | ||
| 1558 | delete $opt{'layout'}; | ||
| 1559 | delete $opt{'page-layout'}; | ||
| 1560 | delete $opt{'page-master'}; | ||
| 1561 | } | ||
| 1562 | if ($opt{'next'}) | ||
| 1563 | { | ||
| 1564 | $opt{'style:next-style-name'} = $opt{'next'}; | ||
| 1565 | delete $opt{'next'}; | ||
| 1566 | } | ||
| 1567 | return $self->appendElement | ||
| 1568 | ( | ||
| 1569 | $root, | ||
| 1570 | 'style:master-page', | ||
| 1571 | attribute => { %opt } | ||
| 1572 | ); | ||
| 1573 | } | ||
| 1574 | |||
| 1575 | #----------------------------------------------------------------------------- | ||
| 1576 | |||
| 1577 | sub masterPageExtension | ||
| 1578 | { | ||
| 1579 | my $self = shift; | ||
| 1580 | my $masterpage = $self->getMasterPageElement(shift) or return undef; | ||
| 1581 | my $position = shift or return undef; | ||
| 1582 | my $element = shift; | ||
| 1583 | my $tag = 'style:' . $position; | ||
| 1584 | unless ($element) | ||
| 1585 | { | ||
| 1586 | return $self->getNodeByXPath($masterpage, "//$tag"); | ||
| 1587 | } | ||
| 1588 | else | ||
| 1589 | { | ||
| 1590 | my $node = $self->makeXPath($masterpage, "/$tag"); | ||
| 1591 | return $self->appendElement($node, $element, @_); | ||
| 1592 | } | ||
| 1593 | } | ||
| 1594 | |||
| 1595 | #----------------------------------------------------------------------------- | ||
| 1596 | |||
| 1597 | sub masterPageHeader | ||
| 1598 | { | ||
| 1599 | my $self = shift; | ||
| 1600 | return $self->masterPageExtension(shift, 'header', @_); | ||
| 1601 | } | ||
| 1602 | |||
| 1603 | sub masterPageFooter | ||
| 1604 | { | ||
| 1605 | my $self = shift; | ||
| 1606 | return $self->masterPageExtension(shift, 'footer', @_); | ||
| 1607 | } | ||
| 1608 | |||
| 1609 | sub masterPageHeaderLeft | ||
| 1610 | { | ||
| 1611 | my $self = shift; | ||
| 1612 | return $self->masterPageExtension(shift, 'header-left', @_); | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | sub masterPageFooterLeft | ||
| 1616 | { | ||
| 1617 | my $self = shift; | ||
| 1618 | return $self->masterPageExtension(shift, 'footer-left', @_); | ||
| 1619 | } | ||
| 1620 | |||
| 1621 | #----------------------------------------------------------------------------- | ||
| 1622 | |||
| 1623 | sub getHeaderParagraph | ||
| 1624 | { | ||
| 1625 | my $self = shift; | ||
| 1626 | my $root = $self->masterPageHeader(shift) or return undef; | ||
| 1627 | my $n = shift; | ||
| 1628 | return $self->getElement('text:p', $n, $root); | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | #----------------------------------------------------------------------------- | ||
| 1632 | |||
| 1633 | sub getFooterParagraph | ||
| 1634 | { | ||
| 1635 | my $self = shift; | ||
| 1636 | my $root = $self->masterPageFooter(shift) or return undef; | ||
| 1637 | my $n = shift; | ||
| 1638 | return $self->getElement('text:p', $n, $root); | ||
| 1639 | } | ||
| 1640 | |||
| 1641 | #----------------------------------------------------------------------------- | ||
| 1642 | |||
| 1643 | sub updateDefaultStyle | ||
| 1644 | { | ||
| 1645 | my $self = shift; | ||
| 1646 | my $style = $self->getDefaultStyleElement(shift); | ||
| 1647 | unless ($style) | ||
| 1648 | { | ||
| 1649 | warn "[" . __PACKAGE__ . "::updateDefaultStyle] " . | ||
| 1650 | "Unavailable default style in the context\n"; | ||
| 1651 | return undef; | ||
| 1652 | } | ||
| 1653 | return $self->updateStyle($style, @_); | ||
| 1654 | } | ||
| 1655 | |||
| 1656 | #----------------------------------------------------------------------------- | ||
| 1657 | # remove a given style element (with element type checking) | ||
| 1658 | |||
| 1659 | sub removeStyle | ||
| 1660 | { | ||
| 1661 | my $self = shift; | ||
| 1662 | my $element = $self->getStyleElement(@_); | ||
| 1663 | if ($element && $element->isStyle) | ||
| 1664 | { | ||
| 1665 | return $self->removeElement($element); | ||
| 1666 | } | ||
| 1667 | else | ||
| 1668 | { | ||
| 1669 | warn "[" . __PACKAGE__ . "::removeStyle] " . | ||
| 1670 | "Unknown style or non-style element\n"; | ||
| 1671 | return undef; | ||
| 1672 | } | ||
| 1673 | } | ||
| 1674 | |||
| 1675 | #----------------------------------------------------------------------------- | ||
| 1676 | 1; |