| 1 | #----------------------------------------------------------------------------- | ||
| 2 | # | ||
| 3 | # $Id : Document.pm 2.025 2010-07-08 JMG$ | ||
| 4 | # | ||
| 5 | # Created and maintained by Jean-Marie Gouarne | ||
| 6 | # Copyright 2008 by Genicorp, S.A. (www.genicorp.com) | ||
| 7 | # | ||
| 8 | #----------------------------------------------------------------------------- | ||
| 9 | |||
| 10 | use OpenOffice::OODoc::Text 2.243; | ||
| 11 | use OpenOffice::OODoc::Image 2.020; | ||
| 12 | use OpenOffice::OODoc::Styles 2.027; | ||
| 13 | |||
| 14 | package OpenOffice::OODoc::Document; | ||
| 15 | use 5.008_000; | ||
| 16 | use strict; | ||
| 17 | our @ISA = qw ( | ||
| 18 | OpenOffice::OODoc::Text | ||
| 19 | OpenOffice::OODoc::Image | ||
| 20 | OpenOffice::OODoc::Styles | ||
| 21 | ); | ||
| 22 | our $VERSION = '2.025'; | ||
| 23 | |||
| 24 | #----------------------------------------------------------------------------- | ||
| 25 | # constructor | ||
| 26 | |||
| 27 | sub new | ||
| 28 | { | ||
| 29 | my $caller = shift; | ||
| 30 | my $class = ref ($caller) || $caller; | ||
| 31 | my %options = | ||
| 32 | ( | ||
| 33 | part => 'content', | ||
| 34 | @_ | ||
| 35 | ); | ||
| 36 | my $object = $class->SUPER::new(%options); | ||
| 37 | return $object ? | ||
| 38 | bless $object, $class : | ||
| 39 | undef; | ||
| 40 | } | ||
| 41 | |||
| 42 | #----------------------------------------------------------------------------- | ||
| 43 | # create and append a new image style | ||
| 44 | |||
| 45 | sub createImageStyle | ||
| 46 | { | ||
| 47 | my $self = shift; | ||
| 48 | my $stylename = shift; | ||
| 49 | my %opt = | ||
| 50 | ( | ||
| 51 | %OpenOffice::OODoc::Image::DEFAULT_IMAGE_STYLE, | ||
| 52 | @_ | ||
| 53 | ); | ||
| 54 | $opt{'family'} = $self->{'opendocument'} ? 'graphic' : 'graphics'; | ||
| 55 | return $self->createStyle($stylename, %opt); | ||
| 56 | } | ||
| 57 | |||
| 58 | #----------------------------------------------------------------------------- | ||
| 59 | # create and append a new text style | ||
| 60 | # default attributes come from the 'Standard' style of the document | ||
| 61 | |||
| 62 | sub createTextStyle | ||
| 63 | { | ||
| 64 | my $self = shift; | ||
| 65 | my $stylename = shift; | ||
| 66 | my $proto = $self->getStyleElement('Standard'); | ||
| 67 | my %default_options = | ||
| 68 | $proto ? | ||
| 69 | $self->getStyleAttributes($proto) : | ||
| 70 | %OpenOffice::OODoc::Text::DEFAULT_TEXT_STYLE; | ||
| 71 | |||
| 72 | my %opt = | ||
| 73 | ( | ||
| 74 | %default_options, | ||
| 75 | @_ | ||
| 76 | ); | ||
| 77 | |||
| 78 | return $self->createStyle($stylename, %opt); | ||
| 79 | } | ||
| 80 | |||
| 81 | #----------------------------------------------------------------------------- | ||
| 82 | # set a page break before a paragraph | ||
| 83 | |||
| 84 | sub setPageBreak | ||
| 85 | { | ||
| 86 | my $self = shift; | ||
| 87 | my $p1 = shift; | ||
| 88 | my $pos = ref $p1 ? undef : shift; | ||
| 89 | my %opt = (position => 'before', @_); | ||
| 90 | my $element = $self->getElement($p1, $pos) or return undef; | ||
| 91 | my $stylename = $self->getStyle($element); | ||
| 92 | unless ($stylename) | ||
| 93 | { | ||
| 94 | warn "[" . __PACKAGE__ . "::setPageBreak] " . | ||
| 95 | "Element has no style\n"; | ||
| 96 | return undef; | ||
| 97 | } | ||
| 98 | my $style = undef; | ||
| 99 | if ($opt{'style'}) | ||
| 100 | { | ||
| 101 | $style = $self->createStyle | ||
| 102 | ( | ||
| 103 | $opt{'style'}, | ||
| 104 | family => 'paragraph', | ||
| 105 | parent => $stylename, | ||
| 106 | category => 'auto', | ||
| 107 | properties => | ||
| 108 | { | ||
| 109 | 'style:page-number' => '0' | ||
| 110 | } | ||
| 111 | ); | ||
| 112 | unless ($style) | ||
| 113 | { | ||
| 114 | warn "[" . __PACKAGE__ . "::setPageBreak] " . | ||
| 115 | "Style $stylename creation failure\n"; | ||
| 116 | return undef; | ||
| 117 | } | ||
| 118 | $self->textStyle($element, $opt{'style'}); | ||
| 119 | } | ||
| 120 | else | ||
| 121 | { | ||
| 122 | $style = $self->getStyleElement($stylename); | ||
| 123 | unless ($style) | ||
| 124 | { | ||
| 125 | warn "[" . __PACKAGE__ . "::setPageBreak] " . | ||
| 126 | "Element style not found\n"; | ||
| 127 | return undef; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | if ($opt{'page'}) | ||
| 131 | { | ||
| 132 | my $name = undef; | ||
| 133 | if (ref $opt{'page'}) | ||
| 134 | { | ||
| 135 | unless ($opt{'page'}->isMasterPage) | ||
| 136 | { | ||
| 137 | warn "[" . __PACKAGE__ . "::setPageBreak] " . | ||
| 138 | "Style element is not master page\n"; | ||
| 139 | return undef; | ||
| 140 | } | ||
| 141 | $name = $self->getAttribute | ||
| 142 | ($opt{'page'}, 'style:name'); | ||
| 143 | } | ||
| 144 | else | ||
| 145 | { | ||
| 146 | $name = $opt{'page'}; | ||
| 147 | } | ||
| 148 | $self->setAttribute($style, 'style:master-page-name', $name); | ||
| 149 | } | ||
| 150 | else | ||
| 151 | { | ||
| 152 | $self->styleProperties | ||
| 153 | ( | ||
| 154 | $style, | ||
| 155 | ('fo:break-' . $opt{'position'}) => 'page' | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | return $element; | ||
| 159 | } | ||
| 160 | |||
| 161 | #----------------------------------------------------------------------------- | ||
| 162 | # removes a page break from a paragraph | ||
| 163 | |||
| 164 | sub removePageBreak | ||
| 165 | { | ||
| 166 | my $self = shift; | ||
| 167 | my $element = $self->getElement(@_) or return undef; | ||
| 168 | my $stylename = $self->getStyle($element); | ||
| 169 | my $style = $self->getStyleElement($self->getStyle($element)); | ||
| 170 | unless ($style) | ||
| 171 | { | ||
| 172 | warn "[" . __PACKAGE__ . "::removePageBreak] " . | ||
| 173 | "Element style not found in the active document\n"; | ||
| 174 | return undef; | ||
| 175 | } | ||
| 176 | $self->setAttribute($style, 'style:master-page-name' => undef); | ||
| 177 | my $prop = $self->getStyleNode($style, 'properties'); | ||
| 178 | $self->setAttribute($prop, 'fo:break-before' => undef) if $prop; | ||
| 179 | |||
| 180 | return $element; | ||
| 181 | } | ||
| 182 | |||
| 183 | #----------------------------------------------------------------------------- | ||
| 184 | # get/set the style of a text or image element | ||
| 185 | |||
| 186 | sub style | ||
| 187 | { | ||
| 188 | my $self = shift; | ||
| 189 | my $path = shift; | ||
| 190 | unless ($path) | ||
| 191 | { | ||
| 192 | warn "[" . __PACKAGE__ . "::style] Missing object\n"; | ||
| 193 | return undef; | ||
| 194 | } | ||
| 195 | my $element = undef; | ||
| 196 | if (ref $path) | ||
| 197 | { | ||
| 198 | $element = $path; | ||
| 199 | } | ||
| 200 | else | ||
| 201 | { | ||
| 202 | $element = ($path =~ /^\//) ? | ||
| 203 | $self->getElement($path, shift) : | ||
| 204 | $self->getImageElement($path); | ||
| 205 | } | ||
| 206 | return undef unless $element; | ||
| 207 | |||
| 208 | my $value = shift; | ||
| 209 | my $namespace = $element->getPrefix; | ||
| 210 | unless ($namespace) | ||
| 211 | { | ||
| 212 | warn "[" . __PACKAGE__ . "::style] Unknown class\n"; | ||
| 213 | return undef; | ||
| 214 | } | ||
| 215 | |||
| 216 | my $attribute = $namespace . ":" . 'style-name'; | ||
| 217 | return defined $value ? | ||
| 218 | $self->setAttribute($element, $attribute => $value) : | ||
| 219 | $self->getAttribute($element, $attribute); | ||
| 220 | } | ||
| 221 | |||
| 222 | #----------------------------------------------------------------------------- | ||
| 223 | # get the style name of any content element | ||
| 224 | |||
| 225 | sub getStyle | ||
| 226 | { | ||
| 227 | my $self = shift; | ||
| 228 | my $path = shift; | ||
| 229 | my $element = ref $path ? | ||
| 230 | $path : | ||
| 231 | $self->getElement($path, shift) | ||
| 232 | or return undef; | ||
| 233 | my $fullname = $element->getName || ""; | ||
| 234 | my ($namespace, $name) = split /:/, $fullname; | ||
| 235 | my $style_attribute = undef; | ||
| 236 | if ($name eq 'master-page') | ||
| 237 | { | ||
| 238 | $style_attribute = $namespace . 'page-master-name'; | ||
| 239 | } | ||
| 240 | else | ||
| 241 | { | ||
| 242 | $style_attribute = $namespace . ':style-name'; | ||
| 243 | } | ||
| 244 | return $self->getAttribute($element, $style_attribute); | ||
| 245 | } | ||
| 246 | |||
| 247 | #----------------------------------------------------------------------------- | ||
| 248 | 1; |