| 1 | package RTF::HTMLConverter; | ||
| 2 | use 5.8.0; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Error qw(:try); | ||
| 6 | use Encode; | ||
| 7 | use Encode::CN; | ||
| 8 | use Encode::KR; | ||
| 9 | use Encode::Symbol; | ||
| 10 | use RTF::Lexer qw(:all); | ||
| 11 | |||
| 12 | our @ISA = qw(RTF::Lexer); | ||
| 13 | our $VERSION = '0.05'; | ||
| 14 | |||
| 15 | # The values provided by DOM implementation are not imported into current | ||
| 16 | # namespace in this realization. Also there is no guarantee that these | ||
| 17 | # values are defined in the namespace specified in 'DOMImplementation' | ||
| 18 | # parameter. So the most reliable way is to specify them by hand (see W3C | ||
| 19 | # DOM spec). | ||
| 20 | use constant { | ||
| 21 | ELEMENT_NODE => 1, | ||
| 22 | TEXT_NODE => 3, | ||
| 23 | COMMENT_NODE => 8, | ||
| 24 | }; | ||
| 25 | |||
| 26 | sub init { | ||
| 27 | my ($self, %opts) = @_; | ||
| 28 | $self->SUPER::init(%opts); | ||
| 29 | $self->{debug_level} = exists $opts{debug} ? $opts{debug} : 0; | ||
| 30 | $self->{optimize_html} = exists $opts{optimize_html} ? $opts{optimize_html} : 1; | ||
| 31 | |||
| 32 | $self->{DOMImplementation} = $opts{DOMImplementation} || 'XML::GDOME'; | ||
| 33 | |||
| 34 | unless(($self->{discard_images} = $opts{discard_images})){ | ||
| 35 | require File::Spec; | ||
| 36 | require File::Temp; | ||
| 37 | require Image::Info; | ||
| 38 | $self->{image_count} = 0; | ||
| 39 | $self->{image_names} = $opts{image_names} || 'img%d'; | ||
| 40 | $self->{image_dir} = $opts{image_dir} || ''; | ||
| 41 | $self->{image_uri} = $opts{image_uri} || ''; | ||
| 42 | $self->{image_convert} = $opts{image_convert} if exists $opts{image_convert}; | ||
| 43 | $self->{image_mogrify} = $opts{image_mogrify} if exists $opts{image_mogrify}; | ||
| 44 | $self->{image_wmf2eps} = $opts{image_wmf2eps} if exists $opts{image_wmf2eps}; | ||
| 45 | $self->{screen_resolution} = $opts{screen_resolution} || 100; # dpi | ||
| 46 | } | ||
| 47 | |||
| 48 | if(exists $opts{out} && !ref($opts{out})){ | ||
| 49 | open my $fh, "> $opts{out}" or throw Error::Simple("Can't open '$opts{out}': $!!\n"); | ||
| 50 | $opts{out} = $fh; | ||
| 51 | } | ||
| 52 | $self->set_sink(exists $opts{out} ? $opts{out} : \*STDOUT); | ||
| 53 | if(exists $opts{err} && !ref($opts{err})){ | ||
| 54 | open my $fh, "> $opts{err}" or throw Error::Simple("Can't open '$opts{err}': $!!\n"); | ||
| 55 | $opts{err} = $fh; | ||
| 56 | } | ||
| 57 | $self->set_log(exists $opts{err} ? $opts{err} : sub { print STDERR $_[1] }); | ||
| 58 | |||
| 59 | $self->{parse_stack} = []; | ||
| 60 | $self->{doc_codepage} = 'iso-8859-1'; | ||
| 61 | $self->{notes} = {}; | ||
| 62 | $self->{on_leave1} = []; | ||
| 63 | $self->{on_leave2} = []; | ||
| 64 | $self->{sect_on_leave} = []; | ||
| 65 | $self->{char_stack} = $self->get_char_stack_class()->new($self); | ||
| 66 | $self->{par_stack} = []; | ||
| 67 | $self->{sect_stack} = []; | ||
| 68 | |||
| 69 | my $root = $self->init_dom(%opts); | ||
| 70 | $self->{doc_head} = $self->create_element('head', $root); | ||
| 71 | $self->{doc_body} = $self->create_element('body', $root); | ||
| 72 | |||
| 73 | $self->{buffers} = []; | ||
| 74 | $self->set_new_buffer(); | ||
| 75 | $self->{tables} = []; | ||
| 76 | |||
| 77 | $self->{final_out} = $self->set_sink(sub { shift()->append_text(@_) }); | ||
| 78 | } | ||
| 79 | |||
| 80 | # DOM Implementation support | ||
| 81 | { | ||
| 82 | my %dom_supported = ( | ||
| 83 | 'XML::GDOME' => '_init_xml_gdome', | ||
| 84 | 'XML::DOM' => '_init_xml_dom', | ||
| 85 | ); | ||
| 86 | sub is_dom_supported { $dom_supported{$_[1]} } | ||
| 87 | sub add_dom_supported { $dom_supported{$_[1]} = $_[2] } | ||
| 88 | sub del_dom_supported { delete $dom_supported{$_[1]} } | ||
| 89 | |||
| 90 | sub init_dom { | ||
| 91 | my $self = shift; | ||
| 92 | my $impl = $self->getDOMImplementation(); | ||
| 93 | my $meth = $dom_supported{$impl}; | ||
| 94 | throw Error::Simple("Unsupported DOM Implementation: '$impl'\n") unless $meth; | ||
| 95 | throw Error::Simple("DOM initialization method is not defined for Implementation '$impl' \n") | ||
| 96 | unless UNIVERSAL::can($self, $meth); | ||
| 97 | return $self->$meth(@_); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _init_xml_gdome { | ||
| 101 | my ($self, %opts) = @_; | ||
| 102 | my $doctype = $opts{doctype} || ['HTML', '-//W3C//DTD HTML 4.01 Transitional//EN', | ||
| 103 | 'http://www.w3.org/TR/html4/loose.dtd']; | ||
| 104 | my $dtd = $self->getDOMImplementation()->createDocumentType(@$doctype); | ||
| 105 | $self->{doc} = $self->getDOMImplementation()->createDocument(undef, 'html', $dtd); | ||
| 106 | $self->{codepage} = $opts{codepage} || 'utf8'; | ||
| 107 | $self->{formatting} = exists $opts{formatting} ? $opts{formatting} : 1; | ||
| 108 | return $self->{doc}->documentElement(); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _init_xml_dom { | ||
| 112 | my ($self, %opts) = @_; | ||
| 113 | my $doc = XML::DOM::Document->new(); | ||
| 114 | $self->{doc} = $doc; | ||
| 115 | my $doctype = $opts{doctype} || ['HTML', 'http://www.w3.org/TR/html4/loose.dtd', | ||
| 116 | '-//W3C//DTD HTML 4.01 Transitional//EN']; | ||
| 117 | my $dtd = $doc->createDocumentType(@$doctype); | ||
| 118 | $doc->setDoctype($dtd); | ||
| 119 | my $el = $doc->createElement('html'); | ||
| 120 | $doc->appendChild($el); | ||
| 121 | return $el; | ||
| 122 | } | ||
| 123 | |||
| 124 | my %dom_stringify = ( | ||
| 125 | 'XML::GDOME' => '_stringify_xml_gdome', | ||
| 126 | 'XML::DOM' => '_stringify_xml_dom', | ||
| 127 | ); | ||
| 128 | sub is_dom_stringify { $dom_stringify{$_[1]} } | ||
| 129 | sub add_dom_stringify { $dom_stringify{$_[1]} = $_[2] } | ||
| 130 | sub del_dom_stringify { delete $dom_stringify{$_[1]} } | ||
| 131 | |||
| 132 | sub _stringify_document { | ||
| 133 | my $self = shift; | ||
| 134 | my $impl = $self->getDOMImplementation(); | ||
| 135 | my $meth = $dom_stringify{$impl}; | ||
| 136 | throw Error::Simple("Stringify method is not defined for DOM Implementation '$impl'\n") | ||
| 137 | unless $meth && UNIVERSAL::can($self, $meth); | ||
| 138 | return $self->$meth(@_); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _stringify_xml_gdome{ | ||
| 142 | $_[0]->get_document()->toStringEnc($_[0]->{codepage}, $_[0]->{formatting}) | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _stringify_xml_dom { $_[0]->get_document()->toString() } | ||
| 146 | } | ||
| 147 | |||
| 148 | ## OO methods | ||
| 149 | |||
| 150 | sub getDOMImplementation { $_[0]->{DOMImplementation} } | ||
| 151 | sub get_buffer_class { 'RTF::HTMLConverter::Buffer' } | ||
| 152 | sub get_char_stack_class { 'RTF::HTMLConverter::CharStack' } | ||
| 153 | sub get_element_class { 'RTF::HTMLConverter::Element' } | ||
| 154 | sub get_modelement_class { 'RTF::HTMLConverter::ModElement' } | ||
| 155 | sub get_table_buf_class { 'RTF::HTMLConverter::TableBuffer' } | ||
| 156 | sub get_token_class { 'RTF::HTMLConverter::Token' } | ||
| 157 | |||
| 158 | ## IO methods | ||
| 159 | |||
| 160 | sub out { $_[0]->{_OUT}(@_) if $_[0]->{_OUT} } | ||
| 161 | sub log { $_[0]->{_LOG}(@_) if $_[0]->{_LOG} && $_[0]->{debug_level} >= $_[2] } | ||
| 162 | |||
| 163 | sub debug { | ||
| 164 | if(@_ > 1){ | ||
| 165 | my $old = $_[0]->{debug_level}; | ||
| 166 | $_[0]->{debug_level} = $_[1]; | ||
| 167 | return $old; | ||
| 168 | } | ||
| 169 | return $_[0]->{debug_level} | ||
| 170 | } | ||
| 171 | |||
| 172 | sub get_log { $_[0]->{_LOG} } | ||
| 173 | |||
| 174 | sub set_log { | ||
| 175 | my ($self, $log) = @_; | ||
| 176 | my $oldlog = $self->{_LOG}; | ||
| 177 | if(ref($log) eq 'CODE' || !$log){ | ||
| 178 | $self->{_LOG} = $log; | ||
| 179 | }elsif(ref($log) eq 'SCALAR'){ | ||
| 180 | $self->{_LOG} = sub { $$log .= $_[1] }; | ||
| 181 | }elsif(fileno $log){ | ||
| 182 | $self->{_LOG} = sub { print $log $_[1] }; | ||
| 183 | }else{ | ||
| 184 | die "set_log: second argument is not a subroutine ref, scalar ref or filehandle.\n"; | ||
| 185 | } | ||
| 186 | return $oldlog; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub get_sink { $_[0]->{_OUT} } | ||
| 190 | |||
| 191 | sub set_sink { | ||
| 192 | my ($self, $sink) = @_; | ||
| 193 | my $oldsink = $self->{_OUT}; | ||
| 194 | if(ref($sink) eq 'CODE' || !$sink){ | ||
| 195 | $self->{_OUT} = $sink; | ||
| 196 | }elsif(ref($sink) eq 'SCALAR'){ | ||
| 197 | $self->{_OUT} = sub { $$sink .= $_[1] }; | ||
| 198 | }elsif(fileno $sink){ | ||
| 199 | $self->{_OUT} = sub { print $sink $_[1] }; | ||
| 200 | }else{ | ||
| 201 | die "set_sink: second argument is not a subroutine ref, scalar ref or filehandle.\n"; | ||
| 202 | } | ||
| 203 | return $oldsink; | ||
| 204 | } | ||
| 205 | |||
| 206 | ## Core methods | ||
| 207 | |||
| 208 | { | ||
| 209 | my %enter_tokens = map { $_ => 1 } (ENTER); | ||
| 210 | sub is_enter_token { $enter_tokens{ref($_[1]) ? $_[1]->type() : $_[1]} } | ||
| 211 | sub add_enter_token { $enter_tokens{ref($_[1]) ? $_[1]->type() : $_[1]} = 1 } | ||
| 212 | sub del_enter_token { delete $enter_tokens{ref($_[1]) ? $_[1]->type() : $_[1]} } | ||
| 213 | } | ||
| 214 | |||
| 215 | { | ||
| 216 | my %leave_tokens = map { $_ => 1 } (LEAVE, DESTN); | ||
| 217 | sub is_leave_token { $leave_tokens{ref($_[1]) ? $_[1]->type() : $_[1]} } | ||
| 218 | sub add_leave_token { $leave_tokens{ref($_[1]) ? $_[1]->type() : $_[1]} = 1 } | ||
| 219 | sub del_leave_token { delete $leave_tokens{ref($_[1]) ? $_[1]->type() : $_[1]} } | ||
| 220 | } | ||
| 221 | |||
| 222 | sub get_token { | ||
| 223 | my $self = shift; | ||
| 224 | my $token = $self->get_token_class()->new(@{$self->SUPER::get_token() || []}); | ||
| 225 | return $token if $_[0]; | ||
| 226 | if($self->is_enter_token($token)){ | ||
| 227 | $self->{parse_stack}[0]++; | ||
| 228 | }elsif($self->is_leave_token($token)){ | ||
| 229 | $self->{parse_stack}[0]--; | ||
| 230 | } | ||
| 231 | return $token; | ||
| 232 | } | ||
| 233 | |||
| 234 | { | ||
| 235 | my %symfunc = ( | ||
| 236 | q[-] => 'cs_dash', | ||
| 237 | q[~] => 'cs_tilde', | ||
| 238 | q['] => 'cs_quoteright', | ||
| 239 | q[*] => 'cs_asterisk', | ||
| 240 | q[\\] => 'cs_backslash', | ||
| 241 | q[:] => 'cs_colon', | ||
| 242 | q[_] => 'cs_underscore', | ||
| 243 | q[{] => 'cs_braceleft', | ||
| 244 | q[|] => 'cs_bar', | ||
| 245 | q[}] => 'cs_braceright', | ||
| 246 | "\r" => 'cs_par', | ||
| 247 | "\n" => 'cs_par', | ||
| 248 | ); | ||
| 249 | sub get_symfunc { $symfunc{ref($_[1]) ? $_[1]->text() : $_[1]} } | ||
| 250 | sub del_symfunc { delete $symfunc{ref($_[1]) ? $_[1]->text() : $_[1]} } | ||
| 251 | sub set_symfunc { | ||
| 252 | my ($self, $symb, $func) = @_; | ||
| 253 | $symb = $symb->text() if ref($symb); | ||
| 254 | my $old = $symfunc{$symb}; | ||
| 255 | $symfunc{$symb} = $func; | ||
| 256 | return $old; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | { | ||
| 261 | my %token_handlers = ( | ||
| 262 | CWORD, 'th_cword', # Control word | ||
| 263 | CSYMB, 'th_csymb', # Control symbol | ||
| 264 | CUNDF, 'th_cundf', # Undefined control symbol | ||
| 265 | PTEXT, 'th_ptext', # Text or binary data | ||
| 266 | ENTER, 'th_enter', # Start group | ||
| 267 | LEAVE, 'th_leave', # End group | ||
| 268 | DESTN, 'th_leave', # End destination group | ||
| 269 | UNBRC, 'th_unbrc', # Unexpected right brace | ||
| 270 | UNEOF, 'th_uneof', # Unexpected end of file | ||
| 271 | OKEOF, 'th_okeof', # End of file | ||
| 272 | ); | ||
| 273 | sub get_token_handler { $token_handlers{ref($_[1]) ? $_[1]->type() : $_[1]} } | ||
| 274 | sub del_token_handler { delete $token_handlers{ref($_[1]) ? $_[1]->type() : $_[1]} } | ||
| 275 | sub set_token_handler { | ||
| 276 | my ($self, $type, $handler) = @_; | ||
| 277 | $type = $type->type() if ref($type); | ||
| 278 | my $old = $token_handlers{$type}; | ||
| 279 | $token_handlers{$type} = $handler; | ||
| 280 | return $old; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | { | ||
| 285 | ## Destinations defined in the March 1987 RTF Specification | ||
| 286 | my %destwords = ( map { $_ => 1 } | ||
| 287 | qw(author buptim colortbl comment creatim doccomm fonttbl | ||
| 288 | footer footerf footerl footerr footnote ftncn ftnsep | ||
| 289 | ftnsepc header headerf headerl headerr info keywords | ||
| 290 | operator pict printim revtim rxe stylesheet subject tc | ||
| 291 | title txe xe) | ||
| 292 | ); | ||
| 293 | sub is_destword { $destwords{$_[1]} } | ||
| 294 | sub add_destword { $destwords{$_[1]} = 1 } | ||
| 295 | sub del_destword { delete $destwords{$_[1]} } | ||
| 296 | } | ||
| 297 | |||
| 298 | sub parse { | ||
| 299 | my $self = shift; | ||
| 300 | $self->{parse_stack} ||= []; | ||
| 301 | unshift @{$self->{parse_stack}}, 0; | ||
| 302 | while(my $token = $self->get_token()){ | ||
| 303 | my $meth = $self->get_token_handler($token); | ||
| 304 | if($meth && $self->can($meth)){ | ||
| 305 | $self->$meth($token); | ||
| 306 | }else{ | ||
| 307 | $self->log("Unhandled token: ".$token->type()."!\n", 1); | ||
| 308 | } | ||
| 309 | last if $self->is_stop_token($token) || $self->{parse_stack}[0] < 0; | ||
| 310 | } | ||
| 311 | shift @{$self->{parse_stack}}; | ||
| 312 | $self->{parse_stack}[0]--; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub unget_tokens { $_[0]->unget_token($_) foreach @{$_[1]} } | ||
| 316 | |||
| 317 | ## Token Handlers | ||
| 318 | |||
| 319 | sub th_cword { | ||
| 320 | my ($self, $token) = @_; | ||
| 321 | return unless $token; | ||
| 322 | my $word = $token->text() || return; | ||
| 323 | my $meth = 'cw_'.$word; | ||
| 324 | if(UNIVERSAL::can($self, $meth)){ | ||
| 325 | $self->$meth($token->param()) | ||
| 326 | }elsif($self->is_destword($word)){ | ||
| 327 | $self->set_destination(); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | sub th_csymb { | ||
| 332 | my ($self, $token) = @_; | ||
| 333 | my $meth = $self->get_symfunc($token); | ||
| 334 | $self->$meth if $meth && UNIVERSAL::can($self, $meth); | ||
| 335 | } | ||
| 336 | |||
| 337 | sub th_cundf { | ||
| 338 | my ($self, $token) = @_; | ||
| 339 | my $symb = $token->text(); | ||
| 340 | $self->log("Undefined control symbol: '$symb'.\n", 1); | ||
| 341 | $self->out($symb); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub th_enter { | ||
| 345 | my $self = shift; | ||
| 346 | for my $name (qw(sect_stack par_stack)){ | ||
| 347 | my $top = $self->{$name}[0]; | ||
| 348 | unshift @{$self->{$name}}, $top ? $top->clone() : $self->get_element_class()->new(); | ||
| 349 | } | ||
| 350 | $self->{char_stack}->dup(); | ||
| 351 | unshift @{$self->{$_}}, [] foreach (qw(on_leave1 on_leave2)); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub th_leave { | ||
| 355 | my $self = shift; | ||
| 356 | $self->{char_stack}->drop(); | ||
| 357 | shift @{$self->{par_stack}}; | ||
| 358 | shift @{$self->{sect_stack}}; | ||
| 359 | $self->_do_on_leave(); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub th_ptext { # Binary data in HEX | ||
| 363 | my ($self, $token) = @_; | ||
| 364 | my $text = $token->text(); | ||
| 365 | unless($self->hex_mode()){ | ||
| 366 | $text = $self->_encode_text($text) unless $self->raw_mode(); | ||
| 367 | $self->out($text); | ||
| 368 | return; | ||
| 369 | } | ||
| 370 | my $pc = $self->{hex_char}; | ||
| 371 | $text = $pc.$text if defined($pc) && length($pc); | ||
| 372 | return unless defined($text) && length($text); | ||
| 373 | $self->{hex_char} = chop($text) if length($text)%2; | ||
| 374 | $self->out(pack("H*", $text)); | ||
| 375 | } | ||
| 376 | |||
| 377 | sub _call_okeof { | ||
| 378 | my ($self, $caller) = @_; | ||
| 379 | my $meth = $self->get_token_handler(OKEOF); | ||
| 380 | if(ref($meth)){ | ||
| 381 | $caller = UNIVERSAL::can($self, $caller); | ||
| 382 | return if $caller && $caller == $meth; | ||
| 383 | }else{ | ||
| 384 | return if $meth eq $caller; | ||
| 385 | } | ||
| 386 | $self->$meth() if UNIVERSAL::can($self, $meth); | ||
| 387 | } | ||
| 388 | |||
| 389 | sub th_uneof { | ||
| 390 | $_[0]->log("Unexpected end of file!\n", 1); | ||
| 391 | $_[0]->_call_okeof('th_uneof'); | ||
| 392 | } | ||
| 393 | |||
| 394 | sub th_unbrc { ### { - to match in text editors | ||
| 395 | $_[0]->log("Extra '}' found!\n", 1); | ||
| 396 | $_[0]->_call_okeof('th_unbrc'); | ||
| 397 | } | ||
| 398 | |||
| 399 | sub th_okeof { | ||
| 400 | my $self = shift; | ||
| 401 | $self->flush_section(); | ||
| 402 | return unless $self->{final_out}; | ||
| 403 | $self->optimize_tree($self->{doc_body}) if $self->{optimize_html}; | ||
| 404 | for my $tag (qw(body head title)){ | ||
| 405 | my $el = $self->get_document()->getElementsByTagName($tag)->item(0); | ||
| 406 | next unless $el; | ||
| 407 | $self->_check_paired_tag($el); | ||
| 408 | } | ||
| 409 | $self->set_sink($self->{final_out}); | ||
| 410 | $self->out($self->_stringify_document()); | ||
| 411 | } | ||
| 412 | |||
| 413 | sub pth_cword { # th_cword with another prefixes | ||
| 414 | my ($self, $token) = @_; | ||
| 415 | my $word; | ||
| 416 | return unless $token && ($word = $token->text()); | ||
| 417 | my $param = $token->param(); | ||
| 418 | my $meth = ($self->{cword_prefix} || 'hcw_').$word; | ||
| 419 | my $nm = 'cw_'.$word; | ||
| 420 | if(UNIVERSAL::can($self, $meth)){ | ||
| 421 | $self->$meth($param); | ||
| 422 | }elsif($self->is_destword($word)){ | ||
| 423 | $self->set_destination(); | ||
| 424 | }elsif($word eq 'u'){ | ||
| 425 | $self->$nm($param) if UNIVERSAL::can($self, $nm); | ||
| 426 | }elsif(UNIVERSAL::can($self, $nm) && (my $cwmeth = $self->{orig_cword_handler})){ | ||
| 427 | $self->$cwmeth($word, $param); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | ## Accessory Methods | ||
| 432 | |||
| 433 | sub get_document { $_[0]->{doc} } | ||
| 434 | |||
| 435 | sub get_char_stack { $_[0]->{char_stack} } | ||
| 436 | sub get_par_stack { $_[0]->{par_stack} } | ||
| 437 | sub get_sect_stack { $_[0]->{sect_stack} } | ||
| 438 | |||
| 439 | sub get_par { $_[0]->{par_stack}[0] } | ||
| 440 | |||
| 441 | sub set_group_token_handler { | ||
| 442 | my ($self, $type, $handler) = @_; | ||
| 443 | my $oldh = $self->set_token_handler($type, $handler); | ||
| 444 | $self->add_on_leave_handler('set_token_handler', [$type, $oldh]); | ||
| 445 | return $oldh; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub hex_mode { | ||
| 449 | my $self = shift; | ||
| 450 | return $self->{hex_mode} unless @_; | ||
| 451 | $self->{hex_char} = ''; | ||
| 452 | my $old = $self->{hex_mode}; | ||
| 453 | $self->{hex_mode} = $_[0] ? 1 : 0; | ||
| 454 | return $old; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub raw_mode { | ||
| 458 | my $self = shift; | ||
| 459 | return $self->{raw_mode} unless @_; | ||
| 460 | my $old = $self->{raw_mode}; | ||
| 461 | $self->{raw_mode} = $_[0] ? 1 : 0; | ||
| 462 | return $old; | ||
| 463 | } | ||
| 464 | |||
| 465 | sub _current_stack { | ||
| 466 | my ($self, $name, $el) = @_; | ||
| 467 | return $self->{$name}[0] unless $el; | ||
| 468 | unshift @{$self->{$name}}, $el; | ||
| 469 | return $el; | ||
| 470 | } | ||
| 471 | |||
| 472 | sub current_buffer { shift()->_current_stack('buffers', @_) } | ||
| 473 | sub current_table { shift()->_current_stack('tables', @_) } | ||
| 474 | |||
| 475 | sub _set_new_stack { | ||
| 476 | my ($self, $class, $name) = splice(@_, 0, 3); | ||
| 477 | my $buf = $class->new(@_); | ||
| 478 | return unless $buf; | ||
| 479 | unshift @{$self->{$name}}, $buf; | ||
| 480 | return $buf; | ||
| 481 | } | ||
| 482 | |||
| 483 | sub set_new_buffer { $_[0]->_set_new_stack($_[0]->get_buffer_class(), 'buffers', $_[0]) } | ||
| 484 | sub set_new_table { $_[0]->_set_new_stack($_[0]->get_table_buf_class(), 'tables', $_[0]) } | ||
| 485 | |||
| 486 | sub remove_buffer { | ||
| 487 | my ($self, $buf) = @_; | ||
| 488 | return unless $buf; | ||
| 489 | @{$self->{buffers}} = grep { $_ != $buf } @{$self->{buffers}}; | ||
| 490 | } | ||
| 491 | |||
| 492 | sub set_new_group_buffer { | ||
| 493 | my $self = shift; | ||
| 494 | my $buf = $self->set_new_buffer(); | ||
| 495 | $self->add_on_leave_handler('remove_buffer', [$buf]); | ||
| 496 | return $buf; | ||
| 497 | } | ||
| 498 | |||
| 499 | sub get_parsed_group_text { | ||
| 500 | my $self = shift; | ||
| 501 | my $buf = $self->set_new_group_buffer(); | ||
| 502 | $self->parse(); | ||
| 503 | return $buf->get_text(); | ||
| 504 | } | ||
| 505 | |||
| 506 | sub get_parsed_group_pcdata { $_[0]->set_pcdata_mode(); $_[0]->get_parsed_group_text() } | ||
| 507 | |||
| 508 | sub set_group_cwhandler { | ||
| 509 | my ($self, $prefix) = @_; | ||
| 510 | my @oldvalues; | ||
| 511 | $oldvalues[0] = $self->set_token_handler(CWORD, 'pth_cword'); | ||
| 512 | $oldvalues[1] = $self->{cword_prefix}; | ||
| 513 | $self->{cword_prefix} = $prefix; | ||
| 514 | $self->add_on_leave_handler('_restore_cwhandler', \@oldvalues); | ||
| 515 | } | ||
| 516 | |||
| 517 | sub _restore_cwhandler { | ||
| 518 | $_[0]->set_token_handler(CWORD, $_[1]); | ||
| 519 | $_[0]->{cword_prefix} = $_[2]; | ||
| 520 | } | ||
| 521 | |||
| 522 | sub set_group_orig_cword_handler { | ||
| 523 | my ($self, $handler) = @_; | ||
| 524 | my $oldh = $self->{orig_cword_handler}; | ||
| 525 | $self->{orig_cword_handler} = $handler; | ||
| 526 | $self->add_on_leave_handler(sub { $_[0]->{orig_cword_handler} = $_[1] }, [$oldh]); | ||
| 527 | } | ||
| 528 | |||
| 529 | sub set_group_sink { | ||
| 530 | my ($self, $sink) = @_; | ||
| 531 | my $old_sink = $self->set_sink(ref($sink) eq 'SCALAR' ? sub { $$sink .= $_[1] } : $sink); | ||
| 532 | $self->add_on_leave_handler(sub { $_[0]->set_sink($_[1]) }, [$old_sink]); | ||
| 533 | } | ||
| 534 | |||
| 535 | sub _encode_text { | ||
| 536 | my ($self, $text, $enc) = @_; | ||
| 537 | $enc ||= $self->_get_text_encoding(); | ||
| 538 | return $enc eq 'utf8' ? $text : Encode::decode($enc, $text); | ||
| 539 | } | ||
| 540 | |||
| 541 | sub notes { | ||
| 542 | my $self = shift; | ||
| 543 | my $notes = $self->{notes}; | ||
| 544 | if(@_ == 2){ | ||
| 545 | my $old = $notes->{$_[0]}; | ||
| 546 | $notes->{$_[0]} = $_[1]; | ||
| 547 | return $old; | ||
| 548 | } | ||
| 549 | return $notes->{$_[0]}; | ||
| 550 | } | ||
| 551 | |||
| 552 | sub del_note { delete $_[0]->{notes}{$_[1]} } | ||
| 553 | |||
| 554 | sub group_notes { | ||
| 555 | my ($self, $key, $value) = @_; | ||
| 556 | unless(exists $self->{notes}{$key}){ | ||
| 557 | $self->notes($key => $value); | ||
| 558 | $self->_add_on_leave('on_leave2', sub { $_[0]->del_note($_[1]) }, [$key]); | ||
| 559 | return; | ||
| 560 | } | ||
| 561 | my $old = $self->notes($key => $value); | ||
| 562 | $self->_add_on_leave('on_leave2', sub { $_[0]->notes($_[1], $_[2]) }, [$key, $old]); | ||
| 563 | return $old; | ||
| 564 | } | ||
| 565 | |||
| 566 | sub store_notes { | ||
| 567 | my ($self, $arr) = splice(@_, 0, 2); | ||
| 568 | $self->add_on_leave_handler(sub { push @{$_[1]}, map { $_[0]->notes($_) } @{$_[2]} }, [$arr, [@_]]); | ||
| 569 | } | ||
| 570 | |||
| 571 | sub set_pcdata_mode { $_[0]->group_notes(pcdata => 1) } | ||
| 572 | |||
| 573 | sub _add_on_leave { unshift @{$_[0]->{$_[1]}[0]}, [$_[2], $_[3] || []] } | ||
| 574 | |||
| 575 | sub add_on_leave_handler { shift()->_add_on_leave('on_leave1', @_) } | ||
| 576 | |||
| 577 | sub add_sect_on_leave_handler { | ||
| 578 | my ($self, $sub, $args) = @_; | ||
| 579 | unshift @{$self->{sect_on_leave}}, [$sub, $args || []]; | ||
| 580 | } | ||
| 581 | |||
| 582 | sub _do_on_leave { | ||
| 583 | my $self = shift; | ||
| 584 | for my $name (qw(on_leave1 on_leave2)){ | ||
| 585 | my $on_leave = shift @{$self->{$name}}; | ||
| 586 | next unless $on_leave && @$on_leave; | ||
| 587 | my $meth; | ||
| 588 | for my $rec (@$on_leave){ | ||
| 589 | $meth = $rec->[0]; | ||
| 590 | $self->$meth(@{$rec->[1]}); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | } | ||
| 594 | |||
| 595 | sub exec_program { | ||
| 596 | shift if ref($_[0]); | ||
| 597 | local $/; | ||
| 598 | my $dirname = shift; | ||
| 599 | my $pid = open(my $fh, '-|'); | ||
| 600 | throw Error::Simple("Can't fork: $!!\n") unless defined $pid; | ||
| 601 | unless($pid){ | ||
| 602 | throw Error::Simple("Can't dup STDERR: $!!\n") unless open(STDERR, ">&STDOUT"); | ||
| 603 | if($dirname){ | ||
| 604 | throw Error::Simple("Can't chdir to '$dirname': $!!\n") unless chdir $dirname; | ||
| 605 | } | ||
| 606 | no warnings 'syntax'; | ||
| 607 | exec @_; | ||
| 608 | throw Error::Simple("Can't exec '".join(' ', @_)."': $!!\n"); | ||
| 609 | } | ||
| 610 | my $output = <$fh>; | ||
| 611 | $fh->close(); | ||
| 612 | return $output; | ||
| 613 | } | ||
| 614 | |||
| 615 | sub get_next_image_name { | ||
| 616 | my ($self, $extn) = @_; | ||
| 617 | $self->{image_count}++; | ||
| 618 | my $name = ''; | ||
| 619 | if(ref($self->{image_names}) eq 'CODE'){ | ||
| 620 | $name = $self->{image_names}($self->{image_count}); | ||
| 621 | }else{ | ||
| 622 | $name = sprintf($self->{image_names}, $self->{image_count}); | ||
| 623 | } | ||
| 624 | $name .= ".$extn" if length $extn; | ||
| 625 | my $url = join('/', grep { length } ($self->{image_uri}, $name)); | ||
| 626 | $name = File::Spec->catfile($self->{image_dir}, $name) if length $self->{image_dir}; | ||
| 627 | return ($name, $url); | ||
| 628 | } | ||
| 629 | |||
| 630 | sub get_color_triplet { | ||
| 631 | my ($self, $num) = @_; | ||
| 632 | my $triplet = ($self->notes('colortbl') || [])->[$num]; | ||
| 633 | return unless $triplet && scalar(grep { defined $_ } @$triplet) == 3; | ||
| 634 | return '#'.join('', @$triplet); | ||
| 635 | } | ||
| 636 | |||
| 637 | sub split_color_triplet { map { substr($_[1], $_, 2) } (1, 3, 5) } | ||
| 638 | |||
| 639 | sub twips2pt { shift; wantarray ? map { sprintf("%.0f", $_/12) } @_ : sprintf("%.0f", $_[0]/12) } | ||
| 640 | |||
| 641 | ## Tree Manipulation Methods | ||
| 642 | |||
| 643 | sub get_head_element { $_[0]->{doc_head} } | ||
| 644 | sub get_body_element { $_[0]->{doc_body} } | ||
| 645 | |||
| 646 | sub get_style_element { | ||
| 647 | my $self = shift; | ||
| 648 | my $style = $self->get_document()->getElementsByTagName('style')->item(0); | ||
| 649 | return $style if $style; | ||
| 650 | my $head = $self->get_document()->getElementsByTagName('head')->item(0); | ||
| 651 | return unless $head; | ||
| 652 | $style = $self->create_element('style', $head); | ||
| 653 | $style->setAttribute(type => 'text/css'); | ||
| 654 | my $comm = $self->get_document()->createComment("\n"); | ||
| 655 | $style->appendChild($comm); | ||
| 656 | return $style; | ||
| 657 | } | ||
| 658 | |||
| 659 | |||
| 660 | sub append_text_node { | ||
| 661 | my ($self, $el, $text, $is_comment) = @_; | ||
| 662 | return unless $el && defined($text); | ||
| 663 | my $type = $is_comment ? COMMENT_NODE : TEXT_NODE; | ||
| 664 | my $child = $el->getLastChild(); | ||
| 665 | if($child && $child->getNodeType() == $type){ | ||
| 666 | $child->appendData($text); | ||
| 667 | return; | ||
| 668 | } | ||
| 669 | my $create = $is_comment ? 'createComment' : 'createTextNode'; | ||
| 670 | $child = $self->get_document()->$create($text); | ||
| 671 | $el->appendChild($child); | ||
| 672 | } | ||
| 673 | |||
| 674 | sub create_element { | ||
| 675 | my ($self, $tag, $parent) = @_; | ||
| 676 | my $el = $self->get_document()->createElement($tag); | ||
| 677 | $parent->appendChild($el) if $parent; | ||
| 678 | return $el; | ||
| 679 | } | ||
| 680 | |||
| 681 | sub _check_paired_tag { | ||
| 682 | my ($self, $el) = @_; | ||
| 683 | return if $el->hasChildNodes(); | ||
| 684 | $el->appendChild($self->get_document()->createTextNode('')); | ||
| 685 | return $el; | ||
| 686 | } | ||
| 687 | |||
| 688 | |||
| 689 | sub append_text { | ||
| 690 | my ($self, $txt, $enc) = @_; | ||
| 691 | return unless $self->current_buffer() && defined($txt) && length($txt); | ||
| 692 | $txt = $self->_encode_text($txt, $enc) if $enc || !$self->raw_mode(); | ||
| 693 | $self->current_buffer()->add_text($txt); | ||
| 694 | } | ||
| 695 | |||
| 696 | sub append_element { | ||
| 697 | my ($self, $el) = @_; | ||
| 698 | return unless $self->current_buffer() && defined($el) && ref($el); | ||
| 699 | return $self->current_buffer()->add_node($el); | ||
| 700 | } | ||
| 701 | |||
| 702 | sub append_tag { | ||
| 703 | my ($self, $tag) = @_; | ||
| 704 | return unless $self->current_buffer() && defined($tag) && length($tag); | ||
| 705 | return $self->current_buffer()->add_tag($tag); | ||
| 706 | } | ||
| 707 | |||
| 708 | sub append_entity { | ||
| 709 | my ($self, $name) = @_; | ||
| 710 | return unless $self->current_buffer() && defined($name) && length($name); | ||
| 711 | return $self->current_buffer()->add_entity($name); | ||
| 712 | } | ||
| 713 | |||
| 714 | sub open_char_tag { shift()->get_char_stack()->open_tag(@_) } | ||
| 715 | sub close_char_tag { shift()->get_char_stack()->close_tag(@_) } | ||
| 716 | sub char_tag_notes { shift()->get_char_stack()->tag_notes(@_) } | ||
| 717 | sub char_tag_attr { shift()->get_char_stack()->tag_attr(@_) } | ||
| 718 | sub char_tag_style { shift()->get_char_stack()->tag_style(@_) } | ||
| 719 | |||
| 720 | sub _apppend_paragraphs { | ||
| 721 | my ($self, $root, $buf) = @_; | ||
| 722 | my $pars = $buf->get_paragraphs(); | ||
| 723 | my $pe; | ||
| 724 | for my $par (@$pars){ | ||
| 725 | $pe = $par->data(); | ||
| 726 | $pe->appendChild($self->get_document()->createEntityReference('nbsp')) | ||
| 727 | unless $pe->hasChildNodes(); | ||
| 728 | $root->appendChild($pe); | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | sub flush_section { | ||
| 733 | my $self = shift; | ||
| 734 | my $body = $self->get_body_element(); | ||
| 735 | $self->_apppend_paragraphs($body, $self->{buffers}[-1]); | ||
| 736 | my $meth; | ||
| 737 | for my $rec (@{$self->{sect_on_leave}}){ | ||
| 738 | $meth = $rec->[0]; | ||
| 739 | $self->$meth(@{$rec->[1]}); | ||
| 740 | } | ||
| 741 | $self->{sect_on_leave} = []; | ||
| 742 | } | ||
| 743 | |||
| 744 | # DOM tree optimization. | ||
| 745 | { | ||
| 746 | ## This lists the tags that can't be removed or merged while optimizing document tree. | ||
| 747 | my %fixed_tags = map { $_ => 1 } qw(body p table tr td th ol ul li pre dd dt dl); | ||
| 748 | sub is_fixed_tag { $fixed_tags{ref($_[1]) ? $_[1]->getNodeName() : $_[1]} } | ||
| 749 | sub add_fixed_tag { $fixed_tags{ref($_[1]) ? $_[1]->getNodeName() : $_[1]} = 1 } | ||
| 750 | sub del_fixed_tag { delete $fixed_tags{ref($_[1]) ? $_[1]->getNodeName() : $_[1]} } | ||
| 751 | } | ||
| 752 | |||
| 753 | { | ||
| 754 | my $stcount = 0; | ||
| 755 | my %styles = (); | ||
| 756 | |||
| 757 | sub _style_to_class { | ||
| 758 | my ($self, $style) = @_; | ||
| 759 | return $styles{$style} if $styles{$style}; | ||
| 760 | my $el = $self->get_style_element(); | ||
| 761 | return unless $el; | ||
| 762 | my $comm = $el->getFirstChild(); | ||
| 763 | while($comm && $comm->getNodeType() != COMMENT_NODE){ | ||
| 764 | $comm = $comm->getNextSibling(); | ||
| 765 | } | ||
| 766 | unless($comm){ | ||
| 767 | $comm = $self->get_document()->createComment("\n"); | ||
| 768 | $el->appendChild($comm); | ||
| 769 | } | ||
| 770 | my $cname = 'c'.++$stcount; | ||
| 771 | $styles{$style} = $cname; | ||
| 772 | $comm->appendData(" .$cname { $style }\n"); | ||
| 773 | return $cname; | ||
| 774 | } | ||
| 775 | } | ||
| 776 | |||
| 777 | { | ||
| 778 | my %skip_styles = map { $_ => 1 } qw(width height); | ||
| 779 | sub is_skip_style { $skip_styles{$_[1]} } | ||
| 780 | sub add_skip_style { $skip_styles{$_[1]} = 1 } | ||
| 781 | sub del_skip_style { delete $skip_styles{$_[1]} } | ||
| 782 | } | ||
| 783 | |||
| 784 | sub optimize_tree { | ||
| 785 | my ($self, $node) = @_; | ||
| 786 | return unless $node && $node->getNodeType() == ELEMENT_NODE && $node->hasChildNodes(); | ||
| 787 | my $parent = $node->getParentNode(); | ||
| 788 | unless($self->is_fixed_tag($node)){ # <b>qq</b><b>ww</b> => <b>qqww</b> | ||
| 789 | while(my $sibling = $node->getNextSibling()){ | ||
| 790 | last unless $self->_nodes_are_equal($node, $sibling); | ||
| 791 | my $nlist = $sibling->getChildNodes(); | ||
| 792 | while($nlist->getLength()){ | ||
| 793 | $node->appendChild($nlist->item(0)); | ||
| 794 | } | ||
| 795 | $parent->removeChild($sibling); | ||
| 796 | } | ||
| 797 | } | ||
| 798 | my $child = $node->getFirstChild(); | ||
| 799 | my $prevcld; | ||
| 800 | while($child){ | ||
| 801 | if($child->getNodeType() == TEXT_NODE){ | ||
| 802 | if($child->getNodeValue() eq '' && !$self->is_fixed_tag($node)){ | ||
| 803 | $node->removeChild($child); # <b></b>qq => qq | ||
| 804 | unless($node->hasChildNodes()){ | ||
| 805 | $parent->removeChild($node); | ||
| 806 | return; | ||
| 807 | } | ||
| 808 | } | ||
| 809 | }else{ | ||
| 810 | $self->optimize_tree($child); | ||
| 811 | } | ||
| 812 | if($child->getParentNode()){ | ||
| 813 | $prevcld = $child; | ||
| 814 | $child = $child->getNextSibling(); | ||
| 815 | }else{ | ||
| 816 | $child = $prevcld ? $prevcld->getNextSibling() : $node->getFirstChild(); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | if(my $st = $node->getAttribute('style')){ # <b style="..."> => <b class="..."> | ||
| 820 | my %sh = $self->get_element_class()->style_hash($st); | ||
| 821 | my @ss = grep { $self->is_skip_style($_) } keys %sh; | ||
| 822 | if(@ss){ | ||
| 823 | $node->setAttribute(style => $self->get_element_class()->style_value({map {$_ => $sh{$_}} @ss})); | ||
| 824 | delete @sh{@ss}; | ||
| 825 | }else{ | ||
| 826 | $node->removeAttribute('style'); | ||
| 827 | } | ||
| 828 | if(keys %sh){ | ||
| 829 | my $class = $self->_style_to_class($self->get_element_class()->style_value(\%sh)); | ||
| 830 | my $ca = $node->getAttribute('class'); | ||
| 831 | $ca .= ' ' if $ca; | ||
| 832 | $ca .= $class; | ||
| 833 | $node->setAttribute(class => $ca); | ||
| 834 | } | ||
| 835 | } | ||
| 836 | return if $self->is_fixed_tag($parent); | ||
| 837 | if($self->_nodes_are_equal($parent, $node)){# <b><b>qq</b></b> => <b>qq</b> | ||
| 838 | $self->_move_children_nodes_up($parent, $node); | ||
| 839 | $parent->removeChild($node); | ||
| 840 | }else{ # <b><i><b>qq</b></i></b> => <b><i>qq</i></b> | ||
| 841 | $child = $node->getFirstChild(); | ||
| 842 | while($child){ | ||
| 843 | if($self->_nodes_are_equal($parent, $child)){ | ||
| 844 | $self->_move_children_nodes_up($node, $child); | ||
| 845 | my $nextcld = $child->getNextSibling(); | ||
| 846 | $node->removeChild($child); | ||
| 847 | $child = $nextcld; | ||
| 848 | }else{ | ||
| 849 | $child = $child->getNextSibling(); | ||
| 850 | } | ||
| 851 | } | ||
| 852 | } | ||
| 853 | } | ||
| 854 | |||
| 855 | sub _move_children_nodes_up { | ||
| 856 | my ($self, $parent, $node) = @_; | ||
| 857 | my $nlist = $node->getChildNodes(); | ||
| 858 | while($nlist->getLength()){ | ||
| 859 | $parent->insertBefore($nlist->item(0), $node); | ||
| 860 | } | ||
| 861 | } | ||
| 862 | |||
| 863 | sub _nodes_are_equal { | ||
| 864 | my ($self, $n1, $n2) = @_; | ||
| 865 | return unless $n1->getNodeType() == $n2->getNodeType(); | ||
| 866 | return unless $n1->getNodeName() eq $n2->getNodeName(); | ||
| 867 | my $attrs1 = $n1->getAttributes(); | ||
| 868 | my $attrs2 = $n2->getAttributes(); | ||
| 869 | my $len = $attrs1->getLength(); | ||
| 870 | return unless $len == $attrs2->getLength(); | ||
| 871 | my (%h1, %h2, $attr); | ||
| 872 | for (my $i = 0; $i < $len; $i++){ | ||
| 873 | $attr = $attrs1->item($i); | ||
| 874 | $h1{$attr->getNodeName()} = $attr->getNodeValue(); | ||
| 875 | $attr = $attrs2->item($i); | ||
| 876 | $h2{$attr->getNodeName()} = $attr->getNodeValue(); | ||
| 877 | } | ||
| 878 | for my $k (keys %h1){ | ||
| 879 | return unless exists $h2{$k} && $h1{$k} eq $h2{$k}; | ||
| 880 | } | ||
| 881 | return 1; | ||
| 882 | } | ||
| 883 | |||
| 884 | # RTF control handlers | ||
| 885 | |||
| 886 | ## Special Character Handlers | ||
| 887 | |||
| 888 | sub cs_asterisk { | ||
| 889 | my $self = shift; | ||
| 890 | my $token = $self->get_token(); | ||
| 891 | if($token && $token->type() == CWORD && defined(my $text = $token->text())){ | ||
| 892 | if($self->can('cw_'.$text)){ | ||
| 893 | $self->unget_token($token); | ||
| 894 | }else{ | ||
| 895 | $self->set_destination(); | ||
| 896 | } | ||
| 897 | }else{ | ||
| 898 | $self->unget_token($token); | ||
| 899 | } | ||
| 900 | } | ||
| 901 | |||
| 902 | sub cs_quoteright{ | ||
| 903 | my $self = shift; | ||
| 904 | my $text; | ||
| 905 | my $token = $self->get_token(); | ||
| 906 | unless($token && $token->type() == ENHEX && length($text = $token->text())){ | ||
| 907 | $self->unget_token($token); | ||
| 908 | $self->log("Hex code expected!\n", 1); | ||
| 909 | return; | ||
| 910 | } | ||
| 911 | my $char = chr(hex($text)); | ||
| 912 | # Either the spec is not clear at this point or I can't read it :-) | ||
| 913 | # Anyway it seems that characters with accents are accompanied with | ||
| 914 | # their ASCII equivalents without accents. So we need not output | ||
| 915 | # characters in [A-Za-z] range. | ||
| 916 | $self->out($char) unless $char =~ /[A-Za-z]/; | ||
| 917 | } | ||
| 918 | |||
| 919 | sub cs_backslash { $_[0]->out('\\') } | ||
| 920 | sub cs_braceleft { $_[0]->out('{') } | ||
| 921 | sub cs_braceright { $_[0]->out('}') } | ||
| 922 | sub cs_underscore { $_[0]->out('-') } | ||
| 923 | sub cs_tilde { $_[0]->append_entity('nbsp') } | ||
| 924 | sub cs_par { $_[0]->cw_par if UNIVERSAL::can($_[0], 'cw_par') } | ||
| 925 | sub cs_colon { } | ||
| 926 | sub cs_dash { } | ||
| 927 | sub cs_bar { } | ||
| 928 | |||
| 929 | ## Contents of an RTF File | ||
| 930 | |||
| 931 | #### RTF Header | ||
| 932 | |||
| 933 | ###### Character Set | ||
| 934 | |||
| 935 | sub cw_ansi { $_[0]->{doc_codepage} = 'iso-8859-1' } | ||
| 936 | sub cw_mac { $_[0]->{doc_codepage} = 'iso-8859-1' } | ||
| 937 | sub cw_pc { $_[0]->{doc_codepage} = 'cp437' } | ||
| 938 | sub cw_pca { $_[0]->{doc_codepage} = 'cp850' } | ||
| 939 | |||
| 940 | ###### Unicode RTF | ||
| 941 | |||
| 942 | { | ||
| 943 | my %supported_cp = map { $_ => 1 } qw(437 850 852 860 862 863 864 865 | ||
| 944 | 866 874 932 936 949 950 1250 1251 | ||
| 945 | 1252 1253 1254 1255 1256 1257 1258); | ||
| 946 | # Unsupported: 708 709 710 711 720 819 1361 (see perldoc Encode::Supported) | ||
| 947 | sub is_supported_cp { $supported_cp{$_[1]} } | ||
| 948 | sub add_supported_cp { $supported_cp{$_[1]} = 1 } | ||
| 949 | sub del_supported_cp { delete $supported_cp{$_[1]} } | ||
| 950 | |||
| 951 | sub cw_ansicpg { $_[0]->notes(ansicpg => 'cp'.$_[1]) if $supported_cp{$_[1]} } | ||
| 952 | } | ||
| 953 | |||
| 954 | sub cw_upr { | ||
| 955 | my $self = shift; | ||
| 956 | while(1) { | ||
| 957 | my $token = $self->get_token(); | ||
| 958 | if(!$token || $self->is_stop_token($token)){ | ||
| 959 | $self->unget_token($token); | ||
| 960 | last; | ||
| 961 | } | ||
| 962 | if($token->type() == ENTER){ | ||
| 963 | my $h = $self->get_token_handler(ENTER); | ||
| 964 | $self->$h($token) if $h && $self->can($h); | ||
| 965 | $self->set_destination(); | ||
| 966 | last; | ||
| 967 | } | ||
| 968 | } | ||
| 969 | } | ||
| 970 | |||
| 971 | sub cw_ud { } # By default this is destination word. | ||
| 972 | |||
| 973 | sub cw_u { | ||
| 974 | my ($self, $char) = @_; | ||
| 975 | $char = 65536 + $char if $char < 0; | ||
| 976 | $self->out(pack('U', $char), 'utf8'); | ||
| 977 | my $uc = $self->notes('uc'); | ||
| 978 | $uc = 1 unless defined $uc; | ||
| 979 | my $string = ''; | ||
| 980 | while(length($string) < $uc){ | ||
| 981 | my $token = $self->get_token(1); | ||
| 982 | my ($type, $text) = ($token->type(), $token->text()); | ||
| 983 | if($type == PTEXT){ | ||
| 984 | $string .= $text; | ||
| 985 | next; | ||
| 986 | }elsif($self->is_enter_token($token) || $self->is_leave_token($token) || $self->is_stop_token($token)){ | ||
| 987 | $self->unget_token($token); | ||
| 988 | return; | ||
| 989 | }elsif($type == CWORD){ | ||
| 990 | if($text eq 'bin'){ | ||
| 991 | while(my $tk = $self->get_token()){ | ||
| 992 | last if $tk->type() == ENBIN || $self->is_stop_token($tk); | ||
| 993 | } | ||
| 994 | } | ||
| 995 | }elsif($type == CSYMB){ | ||
| 996 | if($text eq "'" && ($token = $self->get_token())){ | ||
| 997 | unless($token->type() == ENHEX){ | ||
| 998 | $self->unget_token($token); | ||
| 999 | } | ||
| 1000 | } | ||
| 1001 | } | ||
| 1002 | $string .= ' '; # Count tokens | ||
| 1003 | } | ||
| 1004 | substr($string, 0, $uc) = ''; | ||
| 1005 | $self->out($string) if length $string; | ||
| 1006 | } | ||
| 1007 | |||
| 1008 | sub cw_uc { $_[0]->group_notes(uc => $_[1]) } | ||
| 1009 | |||
| 1010 | ###### Font Table | ||
| 1011 | |||
| 1012 | sub cw_fonttbl { | ||
| 1013 | my $self = shift; | ||
| 1014 | $self->set_pcdata_mode(); | ||
| 1015 | $self->set_new_group_buffer(); | ||
| 1016 | $self->set_group_cwhandler('fcw_'); | ||
| 1017 | $self->parse(); | ||
| 1018 | my $deff = $self->notes('deff'); | ||
| 1019 | if(length $deff){ | ||
| 1020 | my $font = $self->notes('f'.$deff); | ||
| 1021 | return unless $font; | ||
| 1022 | $self->notes(f => $font); | ||
| 1023 | my $ff = $self->_get_font_family($font); | ||
| 1024 | return unless $ff; | ||
| 1025 | my $style = $self->get_style_element(); | ||
| 1026 | $self->append_text_node($style, " BODY { font-family: $ff; font-size: 10pt }\n", 1) if $style; | ||
| 1027 | } | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | sub fcw_f { | ||
| 1031 | my ($self, $num) = @_; | ||
| 1032 | my $font = {}; | ||
| 1033 | $self->notes(f => $font); | ||
| 1034 | $self->notes('f'.$num => $font); | ||
| 1035 | my $txt = $self->get_parsed_group_pcdata(); | ||
| 1036 | $txt =~ s/;$//; | ||
| 1037 | $font->{face} = $txt; | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | sub fcw_fnil { delete (($_[0]->notes('f') || {})->{family}) } | ||
| 1041 | sub fcw_froman { ($_[0]->notes('f') || {})->{family} = 'serif' } | ||
| 1042 | sub fcw_fswiss { ($_[0]->notes('f') || {})->{family} = 'sans-serif' } | ||
| 1043 | sub fcw_fmodern { ($_[0]->notes('f') || {})->{family} = 'monospace' } | ||
| 1044 | sub fcw_fscript { ($_[0]->notes('f') || {})->{family} = 'cursive' } | ||
| 1045 | sub fcw_fdecor { ($_[0]->notes('f') || {})->{family} = 'fantasy' } | ||
| 1046 | |||
| 1047 | sub cw_deff { | ||
| 1048 | my ($self, $num) = @_; | ||
| 1049 | $self->notes(deff => $num); | ||
| 1050 | my $font = $self->notes('f'.$num); | ||
| 1051 | return unless $font; | ||
| 1052 | $self->notes(f => $font); | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | { | ||
| 1056 | my %charsets = ( | ||
| 1057 | 0 => 'iso-8859-1', # ?? ANSI | ||
| 1058 | 1 => 'iso-8859-1', # ?? Default | ||
| 1059 | 2 => 'symbol', # Symbol | ||
| 1060 | #3 => '??', # Invalid | ||
| 1061 | #77 => '??', # Mac | ||
| 1062 | 128 => 'Shift_JIS', # Shift JIS | ||
| 1063 | #129 => '??', # Hangul | ||
| 1064 | 130 => 'johab', # Johab | ||
| 1065 | 134 => 'euc-cn', # GB2312 | ||
| 1066 | 136 => 'Big5', # Big5 | ||
| 1067 | 161 => 'cp1253', # Greek | ||
| 1068 | 162 => 'cp1254', # Turkish | ||
| 1069 | 163 => 'cp1258', # Vietnamese | ||
| 1070 | 177 => 'cp1255', # Hebrew | ||
| 1071 | 178 => 'cp1256', # Arabic | ||
| 1072 | #179 => '??', # Arabic Traditional | ||
| 1073 | #180 => '??', # Arabic user | ||
| 1074 | #181 => '??', # Hebrew user | ||
| 1075 | 186 => 'cp1257', # Baltic | ||
| 1076 | 204 => 'cp1251', # Russian | ||
| 1077 | 222 => 'cp874', # Thai | ||
| 1078 | 238 => 'cp1250', # Eastern European | ||
| 1079 | 254 => 'cp437', # PC 437 | ||
| 1080 | #255 => '??', # OEM | ||
| 1081 | ); | ||
| 1082 | sub fcw_fcharset { | ||
| 1083 | my ($self, $num) = @_; | ||
| 1084 | my $font = $self->notes('f'); | ||
| 1085 | return unless $font; | ||
| 1086 | $font->{codepage} = $charsets{$num}; | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | sub fcw_falt { | ||
| 1091 | my $self = shift; | ||
| 1092 | my $font = $self->notes('f'); | ||
| 1093 | my $txt = $self->get_parsed_group_pcdata(); | ||
| 1094 | ($font || {})->{falt} = $txt; | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | sub _get_text_encoding { | ||
| 1098 | ($_[0]->notes('pcdata') ? {} : $_[0]->notes('f') || {})->{codepage} | ||
| 1099 | || $_[0]->notes('ansicpg') || $_[0]->{doc_codepage} | ||
| 1100 | } | ||
| 1101 | |||
| 1102 | sub _get_font_family { $_[1]->{family} } | ||
| 1103 | |||
| 1104 | ###### Color Table | ||
| 1105 | |||
| 1106 | sub cw_colortbl { | ||
| 1107 | my $self = shift; | ||
| 1108 | $self->set_pcdata_mode(); | ||
| 1109 | $self->notes(colortbl => []); | ||
| 1110 | $self->group_notes(colortbl_current => [(undef)x3]); | ||
| 1111 | $self->set_group_token_handler(PTEXT, 'th_ptext_colortbl'); | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | sub cw_red { ($_[0]->notes('colortbl_current') || [])->[0] = sprintf("%02x", $_[1]) } | ||
| 1115 | sub cw_green { ($_[0]->notes('colortbl_current') || [])->[1] = sprintf("%02x", $_[1]) } | ||
| 1116 | sub cw_blue { ($_[0]->notes('colortbl_current') || [])->[2] = sprintf("%02x", $_[1]) } | ||
| 1117 | |||
| 1118 | sub th_ptext_colortbl { | ||
| 1119 | my ($self, $token) = @_; | ||
| 1120 | my $text = $token->text(); | ||
| 1121 | return unless $text =~ /;/; | ||
| 1122 | push @{$self->notes('colortbl')}, $self->notes('colortbl_current'); | ||
| 1123 | $self->notes(colortbl_current => [(undef)x3]); | ||
| 1124 | } | ||
| 1125 | |||
| 1126 | ###### Style Sheet | ||
| 1127 | |||
| 1128 | sub cw_stylesheet { | ||
| 1129 | my $self = shift; | ||
| 1130 | $self->set_pcdata_mode(); | ||
| 1131 | $self->set_new_group_buffer(); | ||
| 1132 | $self->set_group_cwhandler('scw_'); | ||
| 1133 | my $oldh = $self->set_group_token_handler(ENTER, 'th_enter_style'); | ||
| 1134 | $self->group_notes(stylesheet_enter => $oldh); | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | sub th_enter_style { | ||
| 1138 | my $self = shift; | ||
| 1139 | my $oldh = $self->notes('stylesheet_enter'); | ||
| 1140 | $self->$oldh(); | ||
| 1141 | my $style = { formatting => [] }; | ||
| 1142 | $self->notes(style => $style); | ||
| 1143 | $self->notes(style_name => ['s', '0']) unless $self->notes('s0'); | ||
| 1144 | $self->set_group_orig_cword_handler('_style_format_collector'); | ||
| 1145 | $self->set_group_token_handler(ENTER, $oldh); | ||
| 1146 | $self->parse(); | ||
| 1147 | my $stname = $self->notes('style_name'); | ||
| 1148 | return unless $stname; | ||
| 1149 | $style->{type} = $stname->[0]; | ||
| 1150 | $self->notes($stname->[0].$stname->[1] => $style); | ||
| 1151 | $self->del_note('style_name'); | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | sub _style_format_collector { | ||
| 1155 | my ($self, $word, $param) = @_; | ||
| 1156 | my $style = $self->notes('style'); | ||
| 1157 | return unless $style; | ||
| 1158 | $style->{formatting} = [] unless $style->{formatting}; | ||
| 1159 | push @{$style->{formatting}}, $self->get_token_class()->new(CWORD, $word, $param); | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | sub scw_cs { $_[0]->notes(style_name => ['cs', $_[1]]) } | ||
| 1163 | sub scw_s { $_[0]->notes(style_name => ['s', $_[1]]) } | ||
| 1164 | sub scw_ds { $_[0]->notes(style_name => ['ds', $_[1]]) } | ||
| 1165 | sub scw_ts { $_[0]->notes(style_name => ['ts', $_[1]]) } | ||
| 1166 | |||
| 1167 | sub scw_additive { ($_[0]->notes('style') || {})->{additive} = 1 } | ||
| 1168 | |||
| 1169 | sub scw_sbasedon { | ||
| 1170 | my ($self, $num) = @_; | ||
| 1171 | return if $num == 222; | ||
| 1172 | my $curstyle = $self->notes('style'); | ||
| 1173 | return unless $curstyle; | ||
| 1174 | my $sn = $self->notes('style_name'); | ||
| 1175 | return unless $sn; | ||
| 1176 | my $basestyle = $self->notes($sn->[0].$num); | ||
| 1177 | return unless $basestyle; | ||
| 1178 | unshift @{$curstyle->{formatting}}, @{$basestyle->{formatting} || []}; | ||
| 1179 | for my $key (keys %$basestyle){ | ||
| 1180 | next if exists $curstyle->{$key}; | ||
| 1181 | $curstyle->{$key} = $basestyle->{$key}; | ||
| 1182 | } | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | ###### Table Styles | ||
| 1186 | |||
| 1187 | sub _ts_set_padding { | ||
| 1188 | my ($self, $num, $val) = @_; | ||
| 1189 | my $dim = $self->notes($val); | ||
| 1190 | return unless defined($dim) && $dim == 3; | ||
| 1191 | $self->_td_style($self->twips2pt($num).'pt'); | ||
| 1192 | } | ||
| 1193 | |||
| 1194 | sub cw_tscellpaddt { $_[0]->_ts_set_padding($_[1], 'tscellpaddft') } | ||
| 1195 | sub cw_tscellpaddl { $_[0]->_ts_set_padding($_[1], 'tscellpaddfl') } | ||
| 1196 | sub cw_tscellpaddr { $_[0]->_ts_set_padding($_[1], 'tscellpaddfr') } | ||
| 1197 | sub cw_tscellpaddb { $_[0]->_ts_set_padding($_[1], 'tscellpaddfb') } | ||
| 1198 | |||
| 1199 | sub cw_tscellpaddft { $_[0]->group_notes(tscellpaddft => $_[1]) } | ||
| 1200 | sub cw_tscellpaddfl { $_[0]->group_notes(tscellpaddfl => $_[1]) } | ||
| 1201 | sub cw_tscellpaddfr { $_[0]->group_notes(tscellpaddfr => $_[1]) } | ||
| 1202 | sub cw_tscellpaddfb { $_[0]->group_notes(tscellpaddfb => $_[1]) } | ||
| 1203 | |||
| 1204 | sub cw_tsvertalt { $_[0]->_td_attr(valign => 'top' ) } | ||
| 1205 | sub cw_tsvertalc { $_[0]->_td_attr(valign => 'center') } | ||
| 1206 | sub cw_tsvertalb { $_[0]->_td_attr(valign => 'bottom') } | ||
| 1207 | |||
| 1208 | sub cw_tsnowrap { $_[0]->_td_style('white-space' => 'nowrap') } | ||
| 1209 | |||
| 1210 | sub cw_tscellcfpat { $_[0]->_td_cfpat($_[1], 'tscellpct') } | ||
| 1211 | sub cw_tscellpct { $_[0]->_td_shdng($_[1], 'tscellpct') } | ||
| 1212 | |||
| 1213 | sub cw_tsbrdrt { $_[0]->_set_td_border_style('top') } | ||
| 1214 | sub cw_tsbrdrb { $_[0]->_set_td_border_style('bottom') } | ||
| 1215 | sub cw_tsbrdrl { $_[0]->_set_td_border_style('left') } | ||
| 1216 | sub cw_tsbrdrr { $_[0]->_set_td_border_style('right') } | ||
| 1217 | |||
| 1218 | ###### List Table | ||
| 1219 | |||
| 1220 | sub _get_current_list { $_[0]->notes($_[0]->notes('list_name') || 'list') } | ||
| 1221 | |||
| 1222 | sub cw_listtable { | ||
| 1223 | my $self = shift; | ||
| 1224 | $self->set_new_group_buffer(); | ||
| 1225 | $self->set_group_cwhandler('ltcw_'); | ||
| 1226 | $self->set_group_orig_cword_handler('_list_format_collector'); | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | sub _list_format_collector { | ||
| 1230 | my ($self, $word, $param) = @_; | ||
| 1231 | my $list = $self->_get_current_list(); | ||
| 1232 | return unless $list && @{$list->{levels}}; | ||
| 1233 | $list->{levels}[-1]{formatting} ||= []; | ||
| 1234 | push @{$list->{levels}[-1]{formatting}}, $self->get_token_class()->new(CWORD, $word, $param); | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | ######## Top-Level List Properties | ||
| 1238 | |||
| 1239 | sub ltcw_list { $_[0]->group_notes(list => { levels => [] }) } | ||
| 1240 | |||
| 1241 | sub ltcw_listid { | ||
| 1242 | my ($self, $num) = @_; | ||
| 1243 | my $list = $self->notes('list'); | ||
| 1244 | return unless $list; | ||
| 1245 | $list->{id} = $num; | ||
| 1246 | $self->notes('list'.($num || 0) => $list); | ||
| 1247 | } | ||
| 1248 | |||
| 1249 | sub ltcw_listtemplateid { ($_[0]->notes('list') || {})->{templateid} = $_[1] } | ||
| 1250 | |||
| 1251 | sub ltcw_listsimple { ($_[0]->notes('list') || {})->{simple} = $_[1] } | ||
| 1252 | sub ltcw_listhybrid { ($_[0]->notes('list') || {})->{hybrid} = 1 } | ||
| 1253 | |||
| 1254 | sub ltcw_listname { | ||
| 1255 | my $self = shift; | ||
| 1256 | my $txt = $self->get_parsed_group_pcdata(); | ||
| 1257 | $txt =~ s/;$//; | ||
| 1258 | ($self->notes('list') || {})->{name} = $txt; | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | ######## List Levels | ||
| 1262 | |||
| 1263 | sub ltcw_listlevel { | ||
| 1264 | my $self = shift; | ||
| 1265 | my $list = $self->_get_current_list(); | ||
| 1266 | unless($list){ | ||
| 1267 | $self->set_destination(); | ||
| 1268 | return; | ||
| 1269 | } | ||
| 1270 | push @{$list->{levels}}, {}; | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | sub _set_list_level_prop { | ||
| 1274 | my ($self, $name, $value) = @_; | ||
| 1275 | my $list = $self->_get_current_list(); | ||
| 1276 | return unless $list && @{$list->{levels}}; | ||
| 1277 | $list->{levels}[-1]{$name} = $value; | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | sub ltcw_levelstartat { shift()->_set_list_level_prop('start', @_) } | ||
| 1281 | |||
| 1282 | { | ||
| 1283 | my %list_numbering = ( | ||
| 1284 | 0 => ['decimal', '1', 'ol'], | ||
| 1285 | 1 => ['upper-roman', 'I', 'ol'], | ||
| 1286 | 2 => ['lower-roman', 'i', 'ol'], | ||
| 1287 | 3 => ['upper-alpha', 'A', 'ol'], | ||
| 1288 | 4 => ['lower-alpha', 'a', 'ol'], | ||
| 1289 | 12 => ['katakana'], | ||
| 1290 | 13 => ['katakana-iroha'], | ||
| 1291 | 22 => ['decimal-leading-zero'], | ||
| 1292 | 23 => ['disk', 'disk'], | ||
| 1293 | 45 => ['hebrew'], ## ?? | ||
| 1294 | 47 => ['hebrew'], ## ?? | ||
| 1295 | ); | ||
| 1296 | sub get_list_numbering { $list_numbering{$_[1]} } | ||
| 1297 | sub add_list_numbering { $list_numbering{$_[1]} = $_[2] } | ||
| 1298 | sub del_list_numbering { delete $list_numbering{$_[1]} } | ||
| 1299 | } | ||
| 1300 | |||
| 1301 | sub ltcw_levelnfc { shift()->_set_list_level_prop('num', @_) } | ||
| 1302 | |||
| 1303 | sub ltcw_levelnfcn { shift()->ltcw_levelnfc(@_) } | ||
| 1304 | |||
| 1305 | sub ltcw_leveljc { shift()->_set_list_level_prop('align', { 0 => 'left', 1 => 'center', 2 => 'right' }->{$_[0]}) } | ||
| 1306 | |||
| 1307 | sub ltcw_leveljcn { shift()->ltcw_leveljc(@_) } | ||
| 1308 | |||
| 1309 | sub ltcw_leveltext { $_[0]->set_destination() } | ||
| 1310 | sub ltcw_levelnumbers { $_[0]->set_destination() } | ||
| 1311 | |||
| 1312 | ######## List Override Table | ||
| 1313 | |||
| 1314 | sub cw_listoverridetable { | ||
| 1315 | my $self = shift; | ||
| 1316 | $self->group_notes(list_name => 'listoverride'); | ||
| 1317 | $self->set_new_group_buffer(); | ||
| 1318 | $self->set_group_cwhandler('lotcw_'); | ||
| 1319 | $self->set_group_orig_cword_handler('_list_format_collector'); | ||
| 1320 | } | ||
| 1321 | |||
| 1322 | sub lotcw_listoverride { $_[0]->group_notes(listoverride => { levels => [] }) } | ||
| 1323 | |||
| 1324 | sub lotcw_listid { ($_[0]->notes('listoverride') || {})->{lid} = $_[1] } | ||
| 1325 | |||
| 1326 | sub lotcw_ls { | ||
| 1327 | my ($self, $num) = @_; | ||
| 1328 | my $lo = $self->notes('listoverride'); | ||
| 1329 | return unless $lo; | ||
| 1330 | $lo->{id} = $num; | ||
| 1331 | $self->notes('listoverride'.($num || 0) => $lo); | ||
| 1332 | } | ||
| 1333 | |||
| 1334 | ######## List Override Level | ||
| 1335 | |||
| 1336 | sub lotcw_lfolevel { | ||
| 1337 | my $self = shift; | ||
| 1338 | unless($self->_get_current_list()){ | ||
| 1339 | $self->set_destination(); | ||
| 1340 | return; | ||
| 1341 | } | ||
| 1342 | $self->set_group_cwhandler('ltcw_'); | ||
| 1343 | } | ||
| 1344 | |||
| 1345 | sub ltcw_listoverridestartat { | ||
| 1346 | my $self = shift; | ||
| 1347 | my $lo = $self->_get_current_list(); | ||
| 1348 | return unless $lo; | ||
| 1349 | push @{$lo->{lists}}, {}; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | ###### Paragraph Group Properties | ||
| 1353 | |||
| 1354 | |||
| 1355 | #### Document Area | ||
| 1356 | |||
| 1357 | ###### Information Group | ||
| 1358 | |||
| 1359 | sub cw_info { } | ||
| 1360 | |||
| 1361 | sub cw_title { | ||
| 1362 | my $self = shift; | ||
| 1363 | my $text = $self->get_parsed_group_pcdata(); | ||
| 1364 | my $head = $self->get_head_element(); | ||
| 1365 | return unless $head; | ||
| 1366 | my $title = $self->create_element('title', $head); | ||
| 1367 | $self->append_text_node($title, $text); | ||
| 1368 | } | ||
| 1369 | |||
| 1370 | ###### Document Formatting Properties | ||
| 1371 | |||
| 1372 | sub cw_private { $_[0]->set_destination() if $_[1] == 1 } | ||
| 1373 | |||
| 1374 | ###### Section Text | ||
| 1375 | |||
| 1376 | sub cw_sect { $_[0]->flush_section() } | ||
| 1377 | |||
| 1378 | sub cw_sectd { $_[0]->get_sect_stack()->[0] = $_[0]->get_element_class()->new() } | ||
| 1379 | |||
| 1380 | ######## Headers and Footers | ||
| 1381 | |||
| 1382 | ###### Paragraph Text | ||
| 1383 | |||
| 1384 | ######## Paragraph Formatting Properties | ||
| 1385 | |||
| 1386 | sub cw_par { | ||
| 1387 | my $self = shift; | ||
| 1388 | my ($ls, $ilvl) = map { $self->get_par()->notes($_) } qw(ls ilvl); | ||
| 1389 | unless(defined $ls){ | ||
| 1390 | $self->current_buffer()->create_paragraph(); | ||
| 1391 | return; | ||
| 1392 | } | ||
| 1393 | my $buf = $self->current_buffer(); | ||
| 1394 | $buf->create_paragraph('li'); | ||
| 1395 | my $pl = $buf->get_paragraphs(); | ||
| 1396 | my ($ppar, $par) = @{$pl}[-3,-2]; | ||
| 1397 | $ilvl = 0 if $ilvl < 0; | ||
| 1398 | my $listlevels = $self->notes('par_listlevels'); | ||
| 1399 | if($ppar && $listlevels && $listlevels->[0] == $ppar->data()){ | ||
| 1400 | splice @$listlevels, $ilvl+1 if $#$listlevels > $ilvl; | ||
| 1401 | for (my $i = @$listlevels; $i<=$ilvl; $i++){ | ||
| 1402 | $self->_append_list_levels($listlevels, $ls, $i); | ||
| 1403 | } | ||
| 1404 | splice @$pl, @$pl-2, 1; | ||
| 1405 | }else{ | ||
| 1406 | $listlevels = []; | ||
| 1407 | $self->notes(par_listlevels => $listlevels); | ||
| 1408 | for (my $i=0; $i<=$ilvl; $i++){ | ||
| 1409 | $self->_append_list_levels($listlevels, $ls, $i); | ||
| 1410 | } | ||
| 1411 | my $el = $self->get_element_class()->new(); | ||
| 1412 | $el->data($listlevels->[0]); | ||
| 1413 | splice @$pl, @$pl-2, 1, $el; | ||
| 1414 | } | ||
| 1415 | $listlevels->[$ilvl]->appendChild($par->data()); | ||
| 1416 | } | ||
| 1417 | |||
| 1418 | sub _append_list_levels { | ||
| 1419 | my ($self, $listlevels, $ls, $i) = @_; | ||
| 1420 | my $start = $self->get_list_level_prop($ls, $i, 'start'); | ||
| 1421 | my $num = $self->get_list_level_prop($ls, $i, 'num'); | ||
| 1422 | my $lnum = $self->get_list_numbering($num) || []; | ||
| 1423 | my $le = $self->get_document()->createElement($lnum->[2] || 'ul'); | ||
| 1424 | $le->setAttribute(type => $lnum->[1]) if $lnum->[1]; | ||
| 1425 | $le->setAttribute(style => $self->get_element_class()->style_value({'list-style-type' => $lnum->[0]})) if $lnum->[0]; | ||
| 1426 | $le->setAttribute(start => $start) if $lnum->[2] && $start && $lnum->[2] eq 'ol'; | ||
| 1427 | $listlevels->[$i-1]->appendChild($le) if $i; | ||
| 1428 | $listlevels->[$i] = $le; | ||
| 1429 | } | ||
| 1430 | |||
| 1431 | sub cw_pard { $_[0]->get_par_stack()->[0] = $_[0]->get_element_class()->new() } | ||
| 1432 | |||
| 1433 | sub cw_intbl { $_[0]->get_par()->notes(intbl => 1) } | ||
| 1434 | sub cw_qc { $_[0]->get_par()->attr(align => 'center') } | ||
| 1435 | sub cw_qj { $_[0]->get_par()->attr(align => 'justified') } | ||
| 1436 | sub cw_ql { $_[0]->get_par()->attr(align => 'left') } | ||
| 1437 | sub cw_qr { $_[0]->get_par()->attr(align => 'right') } | ||
| 1438 | |||
| 1439 | ######## Bullets and Numbering | ||
| 1440 | |||
| 1441 | ########## Word 6.0 and Word 95 RTF | ||
| 1442 | |||
| 1443 | sub cw_pntext { $_[0]->set_destination() } | ||
| 1444 | |||
| 1445 | ########## Word 97 through Word 2002 RTF | ||
| 1446 | |||
| 1447 | sub get_list_level_prop { | ||
| 1448 | my ($self, $ls, $ilvl, $name) = @_; | ||
| 1449 | my $lo = $self->notes('listoverride'.($ls || 0)); | ||
| 1450 | return unless $lo; | ||
| 1451 | my $lolevel = $lo->{levels}[$ilvl]; | ||
| 1452 | my $lilevel; | ||
| 1453 | if(defined($lo->{lid}) && (my $list = $self->notes('list'.($lo->{lid} || 0)))){ | ||
| 1454 | $lilevel = $list->{levels}[$ilvl]; | ||
| 1455 | } | ||
| 1456 | return $lolevel->{$name} if exists $lolevel->{$name}; | ||
| 1457 | return $lilevel->{$name} if $lilevel && exists $lilevel->{$name}; | ||
| 1458 | return; | ||
| 1459 | } | ||
| 1460 | |||
| 1461 | sub cw_ls { | ||
| 1462 | my ($self, $ls) = @_; | ||
| 1463 | my $tokens = []; | ||
| 1464 | my $ilvl = 0; | ||
| 1465 | while((my $token = $self->get_token(1))){ | ||
| 1466 | if($token->type() == CWORD && $token->text() eq 'ilvl'){ | ||
| 1467 | $ilvl = $token->param(); | ||
| 1468 | last; | ||
| 1469 | } | ||
| 1470 | push @$tokens, $token; | ||
| 1471 | last if $token->type() != CWORD || $token->text() eq 'par'; | ||
| 1472 | } | ||
| 1473 | $self->get_par()->notes(ls => $ls); | ||
| 1474 | $self->get_par()->notes(ilvl => $ilvl); | ||
| 1475 | $self->unget_tokens($tokens); | ||
| 1476 | } | ||
| 1477 | |||
| 1478 | sub cw_listtext { $_[0]->set_destination() } | ||
| 1479 | |||
| 1480 | ######## Paragraph Borders | ||
| 1481 | |||
| 1482 | sub _set_border_style { | ||
| 1483 | my ($self, $type, $value) = @_; | ||
| 1484 | my $bname = $self->notes('border_name'); | ||
| 1485 | return unless $bname; | ||
| 1486 | $bname .= '-'.$type; | ||
| 1487 | my $el = $self->notes('border_element'); | ||
| 1488 | return if !$el || $el->style($bname); | ||
| 1489 | $el->style($bname => $value); | ||
| 1490 | } | ||
| 1491 | |||
| 1492 | sub cw_brdrs { $_[0]->_set_border_style(style => 'solid') } | ||
| 1493 | sub cw_brdrw { $_[0]->_set_border_style(width => $_[0]->twips2pt($_[1]).'pt') } | ||
| 1494 | |||
| 1495 | ######## Table Definitions | ||
| 1496 | |||
| 1497 | sub _get_table_node { | ||
| 1498 | my ($self, $skip_last_par) = @_; | ||
| 1499 | my $buf = $self->current_buffer(); | ||
| 1500 | my $pl = $buf->get_paragraphs(); | ||
| 1501 | my $table = $self->current_table(); | ||
| 1502 | return undef unless $table; | ||
| 1503 | my $tab = $table->get_element(); | ||
| 1504 | for my $par (reverse @$pl){ | ||
| 1505 | if($par->notes('table_element')){ | ||
| 1506 | last unless $par == $tab; | ||
| 1507 | return $par->data(); | ||
| 1508 | } | ||
| 1509 | last unless $par->notes('intbl') || ($skip_last_par && $par == $pl->[-1]); | ||
| 1510 | } | ||
| 1511 | return undef; | ||
| 1512 | } | ||
| 1513 | |||
| 1514 | sub _table_collect_pars { | ||
| 1515 | my $self = shift; | ||
| 1516 | my $buf = $self->current_buffer(); | ||
| 1517 | my $pb = $buf->get_paragraphs(); | ||
| 1518 | my $pl = []; | ||
| 1519 | while(@$pb){ | ||
| 1520 | my $par = $pb->[-1]; | ||
| 1521 | last if !$par->notes('intbl') || $par->notes('table_element'); | ||
| 1522 | unshift @$pl, pop @$pb; | ||
| 1523 | } | ||
| 1524 | return $pl; | ||
| 1525 | } | ||
| 1526 | |||
| 1527 | sub _start_table { | ||
| 1528 | my $self = shift; | ||
| 1529 | my $table = $self->get_table_buf_class()->new($self); | ||
| 1530 | $self->current_table($table); | ||
| 1531 | my $tab = $table->get_element(); | ||
| 1532 | $tab->notes(table_element => 1); | ||
| 1533 | $tab->notes(no_par_stack => 1); | ||
| 1534 | $tab->attr(cellpadding => 0); | ||
| 1535 | $tab->attr(cellspacing => 0); | ||
| 1536 | $tab->attr(border => 0); | ||
| 1537 | $tab->set_element_attrs($self->get_document()); | ||
| 1538 | my $buf = $self->current_buffer(); | ||
| 1539 | my $pb = $buf->get_paragraphs(); | ||
| 1540 | my $lastpar = pop @$pb; | ||
| 1541 | $buf->add_paragraphs([$tab, $lastpar]); | ||
| 1542 | return $tab; | ||
| 1543 | } | ||
| 1544 | |||
| 1545 | sub _td_meth { | ||
| 1546 | my ($self, $meth) = splice(@_, 0, 2); | ||
| 1547 | my $table = $self->current_table(); | ||
| 1548 | return unless $table; | ||
| 1549 | my $td = $table->get_cellx(); | ||
| 1550 | return unless $td; | ||
| 1551 | $td->$meth(@_); | ||
| 1552 | } | ||
| 1553 | |||
| 1554 | sub _td_notes { shift()->_td_meth('notes', @_) } | ||
| 1555 | sub _td_attr { shift()->_td_meth('attr', @_) } | ||
| 1556 | sub _td_style { shift()->_td_meth('style', @_) } | ||
| 1557 | |||
| 1558 | sub _tr_meth { | ||
| 1559 | my ($self, $meth) = splice(@_, 0, 2); | ||
| 1560 | my $table = $self->current_table(); | ||
| 1561 | return unless $table; | ||
| 1562 | my $tr = $table->get_row(); | ||
| 1563 | return unless $tr; | ||
| 1564 | $tr->$meth(@_); | ||
| 1565 | } | ||
| 1566 | |||
| 1567 | sub _tr_notes { shift()->_tr_meth('notes', @_) } | ||
| 1568 | sub _tr_attr { shift()->_tr_meth('attr', @_) } | ||
| 1569 | sub _tr_style { shift()->_tr_meth('style', @_) } | ||
| 1570 | |||
| 1571 | sub cw_trowd { | ||
| 1572 | my $self = shift; | ||
| 1573 | unless($self->_get_table_node(1)){ | ||
| 1574 | $self->_start_table(); | ||
| 1575 | return; | ||
| 1576 | } | ||
| 1577 | my $table = $self->current_table(); | ||
| 1578 | $table->clear_row_formatting(); | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | sub cw_trrh { | ||
| 1582 | my ($self, $val) = @_; | ||
| 1583 | return unless $val; | ||
| 1584 | $self->_tr_notes(height => $self->twips2pt(abs($val)).'pt'); | ||
| 1585 | } | ||
| 1586 | |||
| 1587 | sub cw_row { | ||
| 1588 | my $self = shift; | ||
| 1589 | my $buf = $self->current_buffer(); | ||
| 1590 | my $tab = $self->_get_table_node(); | ||
| 1591 | my $table = $self->current_table(); | ||
| 1592 | return unless $tab && $table; | ||
| 1593 | $self->_table_collect_pars(); # Discard anything between \cell and \row. | ||
| 1594 | $buf->create_paragraph(); | ||
| 1595 | $table->add_row($self->get_document()); | ||
| 1596 | } | ||
| 1597 | |||
| 1598 | sub cw_cellx { | ||
| 1599 | my $self = shift; | ||
| 1600 | my $table = $self->current_table(); | ||
| 1601 | return unless $table; | ||
| 1602 | $table->add_cellx(@_) | ||
| 1603 | } | ||
| 1604 | |||
| 1605 | sub cw_cell { | ||
| 1606 | my $self = shift; | ||
| 1607 | my $buf = $self->current_buffer(); | ||
| 1608 | my $doc = $self->get_document(); | ||
| 1609 | my $pb = $buf->get_paragraphs(); | ||
| 1610 | my $par = $pb->[-1]->clone(); | ||
| 1611 | $par->data($doc->createElement('p')); | ||
| 1612 | my $pars = $self->_table_collect_pars(); | ||
| 1613 | $buf->add_paragraphs([$par]); | ||
| 1614 | $self->_start_table() unless $self->_get_table_node(); | ||
| 1615 | my $table = $self->current_table(); | ||
| 1616 | my $cell; | ||
| 1617 | if(@$pars == 1){ | ||
| 1618 | $cell = $pars->[0]; | ||
| 1619 | $cell->set_element_attrs($doc, 'td'); | ||
| 1620 | }else{ | ||
| 1621 | $cell = $self->get_element_class()->new(); | ||
| 1622 | my $td = $doc->createElement('td'); | ||
| 1623 | $cell->data($td); | ||
| 1624 | for my $p (@$pars){ | ||
| 1625 | $td->appendChild($p->set_element_attrs($doc)); | ||
| 1626 | } | ||
| 1627 | } | ||
| 1628 | $table->add_cell($cell); | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | sub cw_clvmgf { $_[0]->_td_notes(clvmgf => 1) } | ||
| 1632 | sub cw_clvmrg { $_[0]->_td_notes(clvmrg => 1) } | ||
| 1633 | |||
| 1634 | sub _set_td_width { | ||
| 1635 | my ($self, $width, $clfts) = @_; | ||
| 1636 | if($clfts == 2){ | ||
| 1637 | $self->_td_notes(width => sprintf("%.0f%%", $width/50)); | ||
| 1638 | }elsif($clfts == 3){ | ||
| 1639 | $self->_td_notes(width => $self->twips2pt($width).'pt'); | ||
| 1640 | } | ||
| 1641 | } | ||
| 1642 | |||
| 1643 | sub cw_trftsWidth { $_[0]->_tr_notes(trftsWidth => $_[1]) } | ||
| 1644 | sub cw_clftsWidth { $_[0]->_td_notes(clftsWidth => $_[1]) } | ||
| 1645 | |||
| 1646 | sub cw_clwWidth { | ||
| 1647 | my ($self, $val) = @_; | ||
| 1648 | my $fts = $self->_td_notes('clftsWidth'); | ||
| 1649 | $fts = $self->_td_notes('trftsWidth') unless defined $fts; | ||
| 1650 | $self->_set_td_width($val, $fts) if defined $fts; | ||
| 1651 | } | ||
| 1652 | |||
| 1653 | sub _set_td_border_style { | ||
| 1654 | my ($self, $loc) = @_; | ||
| 1655 | my $table = $self->current_table(); | ||
| 1656 | return unless $table; | ||
| 1657 | my $cx = $table->get_cellx(); | ||
| 1658 | $self->group_notes(border_element => $cx); | ||
| 1659 | $self->group_notes(border_name => 'border-'.$loc); | ||
| 1660 | } | ||
| 1661 | |||
| 1662 | sub cw_clbrdrb { $_[0]->_set_td_border_style('bottom') } | ||
| 1663 | sub cw_clbrdrt { $_[0]->_set_td_border_style('top') } | ||
| 1664 | sub cw_clbrdrl { $_[0]->_set_td_border_style('left') } | ||
| 1665 | sub cw_clbrdrr { $_[0]->_set_td_border_style('right') } | ||
| 1666 | |||
| 1667 | sub _get_shdng_color { | ||
| 1668 | my ($self, $color, $shad) = @_; | ||
| 1669 | $shad /= 10000; | ||
| 1670 | my @clrs = $self->split_color_triplet($color); | ||
| 1671 | return '#'.join('', map { sprintf("%02x", 255-(255-$_)*$shad) } @clrs); | ||
| 1672 | } | ||
| 1673 | |||
| 1674 | sub _td_shdng { | ||
| 1675 | my ($self, $val, $name) = @_; | ||
| 1676 | return if $val == 10000; | ||
| 1677 | my $color = $self->_td_attr('bgcolor'); | ||
| 1678 | if($color){ | ||
| 1679 | $color = $self->_get_shdng_color($color, $val); | ||
| 1680 | $self->_td_attr(bgcolor => $color); | ||
| 1681 | }else{ | ||
| 1682 | $self->_td_notes($name => $val); | ||
| 1683 | } | ||
| 1684 | } | ||
| 1685 | |||
| 1686 | sub _td_cfpat { | ||
| 1687 | my ($self, $par, $name) = @_; | ||
| 1688 | my $color = $self->get_color_triplet($par); | ||
| 1689 | return unless $color; | ||
| 1690 | if($self->_td_notes($name)){ | ||
| 1691 | $color = $self->_get_shdng_color($color, $self->_td_notes($name)); | ||
| 1692 | } | ||
| 1693 | $self->_td_attr(bgcolor => $color) if $color; | ||
| 1694 | } | ||
| 1695 | |||
| 1696 | sub cw_clshdng { $_[0]->_td_shdng($_[1], 'clshdng') } | ||
| 1697 | sub cw_clcfpat { $_[0]->_td_cfpat($_[1], 'clshdng') } | ||
| 1698 | |||
| 1699 | sub cw_clvertalt { $_[0]->_td_attr(valign => 'top') } | ||
| 1700 | sub cw_clvertalc { $_[0]->_td_attr(valign => 'center') } | ||
| 1701 | sub cw_clvertalb { $_[0]->_td_attr(valign => 'bottom') } | ||
| 1702 | |||
| 1703 | ###### Character Text | ||
| 1704 | |||
| 1705 | sub _apply_style { | ||
| 1706 | my ($self, $type, $num) = @_; | ||
| 1707 | my $style = $self->notes($type.$num); | ||
| 1708 | return unless $style; | ||
| 1709 | $self->unget_tokens($style->{formatting}); | ||
| 1710 | } | ||
| 1711 | |||
| 1712 | sub cw_cs { | ||
| 1713 | my ($self, $n) = @_; | ||
| 1714 | return unless defined $n; | ||
| 1715 | my $style = $self->notes('cs'.$n); | ||
| 1716 | return unless $style; | ||
| 1717 | $self->_clear_char_format() unless $style->{additive}; | ||
| 1718 | $self->_apply_style('cs', $n) ; | ||
| 1719 | } | ||
| 1720 | |||
| 1721 | sub cw_s { $_[0]->_apply_style('s', $_[1]) } | ||
| 1722 | sub cw_ds { $_[0]->_apply_style('ds', $_[1]) } | ||
| 1723 | sub cw_ts { $_[0]->_apply_style('ts', $_[1]) } | ||
| 1724 | |||
| 1725 | ######## Font (Character) Formatting Properties | ||
| 1726 | |||
| 1727 | sub cw_plain { | ||
| 1728 | my $self = shift; | ||
| 1729 | my $deff = $self->notes('deff') || '0'; | ||
| 1730 | my $font = $self->notes('f'.$deff); | ||
| 1731 | $self->group_notes(f => $font) if $font; | ||
| 1732 | $self->_clear_char_format(); | ||
| 1733 | } | ||
| 1734 | |||
| 1735 | sub _clear_char_format { $_[0]->get_char_stack()->clear(sub {$_[0]->notes('not_char_format')}) } | ||
| 1736 | |||
| 1737 | sub _manage_tag { | ||
| 1738 | defined($_[2]) && $_[2] eq '0' ? $_[0]->close_char_tag($_[1]) : $_[0]->open_char_tag($_[1]) | ||
| 1739 | } | ||
| 1740 | |||
| 1741 | sub cw_b { shift()->_manage_tag('b', @_) } | ||
| 1742 | |||
| 1743 | sub cw_cb { | ||
| 1744 | my ($self, $num) = @_; | ||
| 1745 | my $color = $self->get_color_triplet($num); | ||
| 1746 | return unless $color; | ||
| 1747 | $self->char_tag_style('font', 'background-color' => $color); | ||
| 1748 | } | ||
| 1749 | |||
| 1750 | sub cw_f { | ||
| 1751 | my ($self, $num) = @_; | ||
| 1752 | my $font = $self->notes('f'.$num); | ||
| 1753 | return unless $font; | ||
| 1754 | $self->group_notes(f => $font); | ||
| 1755 | my $deff = $self->notes('deff'); | ||
| 1756 | unless(length($deff) && $num == $deff){ | ||
| 1757 | my $ff = $self->_get_font_family($font); | ||
| 1758 | $self->char_tag_style('font', 'font-family' => $ff) if $ff; | ||
| 1759 | } | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | sub cw_cf { | ||
| 1763 | my ($self, $num) = @_; | ||
| 1764 | my $color = $self->get_color_triplet($num); | ||
| 1765 | return unless $color; | ||
| 1766 | $self->char_tag_attr('font', color => $color); | ||
| 1767 | } | ||
| 1768 | |||
| 1769 | sub cw_fs { $_[0]->char_tag_style('font', 'font-size' => (int($_[1]/2)+$_[1]%2).'pt') } | ||
| 1770 | |||
| 1771 | sub cw_i { shift()->_manage_tag('i', @_) } | ||
| 1772 | sub cw_sub { $_[0]->open_char_tag('sub') } | ||
| 1773 | sub cw_super { $_[0]->open_char_tag('sup') } | ||
| 1774 | sub cw_ul { shift()->_manage_tag('u', @_) } | ||
| 1775 | |||
| 1776 | ######## Special Characters | ||
| 1777 | |||
| 1778 | sub cw_line { $_[0]->append_tag('br') } | ||
| 1779 | |||
| 1780 | sub cw_lbr { | ||
| 1781 | my ($self, $par) = @_; | ||
| 1782 | my $el = $self->append_tag('br'); | ||
| 1783 | my $clear = $par == 3 ? 'all' : | ||
| 1784 | $par == 2 ? 'right' : | ||
| 1785 | $par == 1 ? 'left' : 0; | ||
| 1786 | $el->setAttribute(clear => $clear) if $clear; | ||
| 1787 | } | ||
| 1788 | |||
| 1789 | sub cw_tab { $_[0]->append_entity('nbsp') foreach 1..8 } | ||
| 1790 | sub cw_emdash { $_[0]->append_entity('mdash') } | ||
| 1791 | sub cw_endash { $_[0]->append_entity('ndash') } | ||
| 1792 | sub cw_emspace { $_[0]->append_entity('emsp') } | ||
| 1793 | sub cw_enspace { $_[0]->append_entity('ensp') } | ||
| 1794 | sub cw_qmspace { $_[0]->append_entity('thinsp') } | ||
| 1795 | sub cw_bullet { $_[0]->append_entity('bull') } | ||
| 1796 | sub cw_lquote { $_[0]->append_entity('lsquo') } | ||
| 1797 | sub cw_rquote { $_[0]->append_entity('rsquo') } | ||
| 1798 | sub cw_ldblquote { $_[0]->append_entity('ldquo') } | ||
| 1799 | sub cw_rdblquote { $_[0]->append_entity('rdquo') } | ||
| 1800 | sub cw_zwj { $_[0]->append_entity('zwj') } | ||
| 1801 | sub cw_zwnj { $_[0]->append_entity('zwnj') } | ||
| 1802 | |||
| 1803 | ###### Bookmarks | ||
| 1804 | |||
| 1805 | sub _get_ancor_name { | ||
| 1806 | my ($self, $txt) = @_; | ||
| 1807 | $self->{ancor_count} = 0 unless defined $self->{ancor_count}; | ||
| 1808 | $self->{ancors} ||= {}; | ||
| 1809 | my $name = $self->{ancors}{$txt}; | ||
| 1810 | return $name if defined $name; | ||
| 1811 | return $self->{ancors}{$txt} = 'a'.$self->{ancor_count}++; | ||
| 1812 | } | ||
| 1813 | |||
| 1814 | sub cw_bkmkstart { | ||
| 1815 | my $self = shift; | ||
| 1816 | $self->set_pcdata_mode(); | ||
| 1817 | my $txt = $self->get_parsed_group_text(); | ||
| 1818 | my $name = $self->_get_ancor_name($txt); | ||
| 1819 | my $el= $self->create_element('a'); | ||
| 1820 | $el->setAttribute(name => $name); | ||
| 1821 | $self->append_text_node($el, ' '); | ||
| 1822 | $self->append_element($el); | ||
| 1823 | } | ||
| 1824 | |||
| 1825 | ###### Pictures | ||
| 1826 | |||
| 1827 | sub cw_shppict { $_[0]->notes(shppict => -1) } | ||
| 1828 | |||
| 1829 | sub cw_nonshppict { | ||
| 1830 | my $self = shift; | ||
| 1831 | my $shppict = $self->notes(shppict => 0); | ||
| 1832 | $self->set_destination() if $shppict > 0; | ||
| 1833 | } | ||
| 1834 | |||
| 1835 | sub cw_pict { | ||
| 1836 | my $self = shift; | ||
| 1837 | if($self->{discard_images}){ | ||
| 1838 | $self->set_destination(); | ||
| 1839 | }else{ | ||
| 1840 | my $buf = $self->set_new_group_buffer(); | ||
| 1841 | $self->group_notes(pict_buf => $buf); | ||
| 1842 | } | ||
| 1843 | } | ||
| 1844 | |||
| 1845 | sub cw_picscalex { $_[0]->group_notes(picscalex => $_[1]) } | ||
| 1846 | sub cw_picscaley { $_[0]->group_notes(picscaley => $_[1]) } | ||
| 1847 | sub cw_picwgoal { $_[0]->group_notes(picwgoal => $_[1]) } | ||
| 1848 | sub cw_pichgoal { $_[0]->group_notes(pichgoal => $_[1]) } | ||
| 1849 | |||
| 1850 | { | ||
| 1851 | my %res_coeff = (i => 1, m => 0.0254, cm => 2.54); | ||
| 1852 | sub _get_image_dim { | ||
| 1853 | my ($self, $w, $h, $sx, $sy, $res) = @_; | ||
| 1854 | my ($rx, $ry, $un); | ||
| 1855 | my $scr = $self->{screen_resolution} || 100; # dpi | ||
| 1856 | if($res =~ /(\d+)\s+dp(\w+)/){ | ||
| 1857 | $rx = $ry = $1; | ||
| 1858 | $un = $res_coeff{$2}; | ||
| 1859 | }elsif($res =~ m|(\d+)\s+/\s+(\d+)\s+dp(\w+)|){ | ||
| 1860 | $rx = $1; | ||
| 1861 | $ry = $2; | ||
| 1862 | $un = $res_coeff{$3}; | ||
| 1863 | }elsif($res eq '1/1'){ | ||
| 1864 | $rx = $ry = $scr; | ||
| 1865 | $un = 1; | ||
| 1866 | } | ||
| 1867 | return unless $rx && $ry && $un; | ||
| 1868 | return ($scr/$un/$rx*$sx/100*$w, $scr/$un/$ry*$sy/100*$h); | ||
| 1869 | } | ||
| 1870 | } | ||
| 1871 | |||
| 1872 | sub _picblib { | ||
| 1873 | my ($self, $ext) = @_; | ||
| 1874 | my ($imgname, $imgurl) = $self->get_next_image_name($ext); | ||
| 1875 | return unless open my $fh, '>', $imgname; | ||
| 1876 | $self->set_group_sink($fh); | ||
| 1877 | my $hm = $self->hex_mode(1); | ||
| 1878 | $self->add_on_leave_handler('_convert_picture', [$imgname, $imgurl, $fh, $hm]); | ||
| 1879 | } | ||
| 1880 | |||
| 1881 | sub cw_pngblip { shift()->_picblib('png') } | ||
| 1882 | sub cw_jpegblip { shift()->_picblib('jpg') } | ||
| 1883 | |||
| 1884 | sub get_image_scale_command { | ||
| 1885 | my ($self, $w, $h, $imgname) = @_; | ||
| 1886 | my $mogrify = exists $self->{image_mogrify} ? $self->{image_mogrify} : 'mogrify'; | ||
| 1887 | return unless $mogrify; | ||
| 1888 | return ($mogrify, '-geometry', sprintf("%dx%d!", $w, $h), $imgname); | ||
| 1889 | } | ||
| 1890 | |||
| 1891 | sub get_wmf2ps_command { | ||
| 1892 | my ($self, $wmfname, $psname) = @_; | ||
| 1893 | my $wmf2eps = exists $self->{image_wmf2eps} ? $self->{image_wmf2eps} : 'wmf2eps'; | ||
| 1894 | return unless $wmf2eps; | ||
| 1895 | return ($wmf2eps, qw(--ps --centre --maxpect -o), $psname , $wmfname); | ||
| 1896 | } | ||
| 1897 | |||
| 1898 | sub get_ps2gif_command { | ||
| 1899 | my ($self, $psname, $gifname, $w, $h) = @_; | ||
| 1900 | my $convert = exists $self->{image_convert} ? $self->{image_convert} : 'convert'; | ||
| 1901 | return unless $convert; | ||
| 1902 | my @geom = $w && $h ? ('-geometry', sprintf("%dx%d!", $w, $h)): (); | ||
| 1903 | return ($convert, qw(-crop 0x0 -page +0+0), @geom, $psname, $gifname); | ||
| 1904 | } | ||
| 1905 | |||
| 1906 | sub _convert_picture { | ||
| 1907 | my ($self, $imgname, $imgurl, $fh, $hexmode) = @_; | ||
| 1908 | $fh->close(); | ||
| 1909 | $self->hex_mode($hexmode); | ||
| 1910 | return unless -s $imgname; | ||
| 1911 | my $info = Image::Info::image_info($imgname); | ||
| 1912 | my ($width, $height, $res) = @{$info}{qw(width height resolution)}; | ||
| 1913 | my ($w, $h); | ||
| 1914 | if($width && $height){ | ||
| 1915 | my ($sx, $sy) = map { $self->notes($_) } qw(picscalex picscaley); | ||
| 1916 | ($w, $h) = $self->_get_image_dim($width, $height, $sx, $sy, $res); | ||
| 1917 | ($w, $h) = map { sprintf("%.0f", $_) } ($w || $width, $h || $height); | ||
| 1918 | if($w != $width || $h != $height){ | ||
| 1919 | my @cmd = $self->get_image_scale_command($w, $h, $imgname); | ||
| 1920 | return unless @cmd; | ||
| 1921 | if(my $err = $self->exec_program(undef, @cmd)){ | ||
| 1922 | $self->log($err, 1); | ||
| 1923 | return; | ||
| 1924 | } | ||
| 1925 | } | ||
| 1926 | } | ||
| 1927 | $self->remove_buffer($self->notes('pict_buf')); | ||
| 1928 | my $img = $self->append_tag('img'); | ||
| 1929 | $img->setAttribute(src => $imgurl); | ||
| 1930 | $img->setAttribute(width => $w) if $w; | ||
| 1931 | $img->setAttribute(height => $h) if $h; | ||
| 1932 | $img->setAttribute(alt => ''); | ||
| 1933 | $self->notes(shppict => 1) if $self->notes('shppict'); | ||
| 1934 | } | ||
| 1935 | |||
| 1936 | sub cw_wmetafile { | ||
| 1937 | my ($self, $type) = @_; | ||
| 1938 | my ($gifname, $gifurl) = $self->get_next_image_name('gif'); | ||
| 1939 | my ($w, $h); | ||
| 1940 | my $hexmode = $self->hex_mode(); | ||
| 1941 | try { | ||
| 1942 | my $wmf = File::Temp->new(SUFFIX => '.wmf', UNLINK => 1); | ||
| 1943 | throw Error::Simple("Can't open wmf file: $!!\n") unless $wmf; | ||
| 1944 | $self->set_group_sink($wmf); | ||
| 1945 | $self->hex_mode(1); | ||
| 1946 | my @dims; | ||
| 1947 | $self->store_notes(\@dims, qw(picwgoal pichgoal picscalex picscaley)); | ||
| 1948 | $self->parse(); | ||
| 1949 | $wmf->close(); | ||
| 1950 | $self->hex_mode($hexmode); | ||
| 1951 | my $ps = File::Temp->new(SUFFIX => '.ps', UNLINK => 1); | ||
| 1952 | throw Error::Simple("Can't open ps file: $!!\n") unless $ps; | ||
| 1953 | $ps->close(); | ||
| 1954 | my @command = $self->get_wmf2ps_command($wmf->filename(), $ps->filename()); | ||
| 1955 | return unless @command; | ||
| 1956 | my $err = $self->exec_program(undef, @command); | ||
| 1957 | throw Error::Simple($err) if length $err; | ||
| 1958 | return unless -s $ps->filename(); | ||
| 1959 | my ($width, $height, $sx, $sy) = @dims; | ||
| 1960 | if($width && $height){ | ||
| 1961 | ($w, $h) = $self->twips2pt($sx?$width*$sx/100:$width, $sy?$height*$sy/100:$height); | ||
| 1962 | } | ||
| 1963 | @command = $self->get_ps2gif_command($ps->filename(), $gifname, $w, $h); | ||
| 1964 | return unless @command; | ||
| 1965 | $err = $self->exec_program(undef, @command); | ||
| 1966 | throw Error::Simple($err) if length $err; | ||
| 1967 | } catch Error::Simple with { | ||
| 1968 | my $err = shift; | ||
| 1969 | $self->hex_mode($hexmode); | ||
| 1970 | $self->log($err, 1) if length $err; | ||
| 1971 | return; | ||
| 1972 | }; | ||
| 1973 | return unless -s $gifname; | ||
| 1974 | $self->remove_buffer($self->notes('pict_buf')); | ||
| 1975 | my $img = $self->append_tag('img'); | ||
| 1976 | $img->setAttribute(src => $gifurl); | ||
| 1977 | $img->setAttribute(width => $w) if $w; | ||
| 1978 | $img->setAttribute(height => $h) if $h; | ||
| 1979 | $img->setAttribute(alt => ''); | ||
| 1980 | $self->notes(shppict => 1) if $self->notes('shppict'); | ||
| 1981 | } | ||
| 1982 | |||
| 1983 | sub cw_bin { | ||
| 1984 | my $self = shift; | ||
| 1985 | while(my $token = $self->get_token()){ | ||
| 1986 | $self->out($token->text()); | ||
| 1987 | last if $token->type() == ENBIN || $self->is_stop_token($token); | ||
| 1988 | } | ||
| 1989 | } | ||
| 1990 | |||
| 1991 | ###### Word 97 through Word 2002 RTF for Drawing Objects (Shapes) | ||
| 1992 | |||
| 1993 | sub cw_shp { $_[0]->notes(shp => 0) } | ||
| 1994 | |||
| 1995 | sub cw_shpinst { | ||
| 1996 | my $self = shift; | ||
| 1997 | my $buf = $self->set_new_group_buffer(); | ||
| 1998 | $self->parse(); | ||
| 1999 | return if $buf->is_empty(); | ||
| 2000 | $self->notes(shp => 1); | ||
| 2001 | my $cbuf = $self->current_buffer(); | ||
| 2002 | $cbuf->merge($buf); | ||
| 2003 | } | ||
| 2004 | |||
| 2005 | sub cw_sn { $_[0]->notes(sn => $_[0]->get_parsed_group_text()) } | ||
| 2006 | |||
| 2007 | sub cw_sv { | ||
| 2008 | my $self = shift; | ||
| 2009 | unless($self->notes('sn') eq 'pib'){ | ||
| 2010 | $self->set_destination(); | ||
| 2011 | return; | ||
| 2012 | } | ||
| 2013 | } | ||
| 2014 | |||
| 2015 | # This is not correct, but it looks like \shptxt duplicates the information. | ||
| 2016 | sub cw_shptxt { $_[0]->set_destination() } | ||
| 2017 | |||
| 2018 | sub cw_shprslt { $_[0]->set_destination() if $_[0]->notes('shp') } | ||
| 2019 | |||
| 2020 | ###### Footnotes | ||
| 2021 | |||
| 2022 | sub cw_footnote { | ||
| 2023 | my $self = shift; | ||
| 2024 | unless($self->notes('footnote_list')){ | ||
| 2025 | $self->notes(footnote => 1); | ||
| 2026 | $self->notes(footnote_list => []); | ||
| 2027 | $self->add_sect_on_leave_handler('_add_footnotes'); | ||
| 2028 | } | ||
| 2029 | my $buf = $self->set_new_group_buffer(); | ||
| 2030 | $self->parse(); | ||
| 2031 | my $num = $self->notes('footnote'); | ||
| 2032 | $self->notes(footnote => $num+1); | ||
| 2033 | push @{$self->notes('footnote_list')}, [$num, $buf]; | ||
| 2034 | my $name = $self->_get_footnote_name($num); | ||
| 2035 | my $el = $self->create_element('a'); | ||
| 2036 | $el->setAttribute(href => '#'.$name); | ||
| 2037 | $el->setAttribute(name => $name.'u'); | ||
| 2038 | $self->append_text_node($el, $num); | ||
| 2039 | $self->append_element($el); | ||
| 2040 | } | ||
| 2041 | |||
| 2042 | sub _get_footnote_name { 's'.($_[0]->notes('footnote_snum') || 0).'f'.$_[1] } | ||
| 2043 | |||
| 2044 | sub _add_footnotes { | ||
| 2045 | my $self = shift; | ||
| 2046 | my $list = $self->notes('footnote_list'); | ||
| 2047 | return unless $list && @$list; | ||
| 2048 | my $body = $self->get_body_element(); | ||
| 2049 | my $table = $self->create_element('table'); | ||
| 2050 | $table->setAttribute(cellpadding => 0); | ||
| 2051 | $table->setAttribute(cellspacing => 3); | ||
| 2052 | $table->setAttribute(border => 0); | ||
| 2053 | for my $item (@$list){ | ||
| 2054 | my $tr = $self->create_element('tr', $table); | ||
| 2055 | $tr->setAttribute(valign => 'top'); | ||
| 2056 | my $td = $self->create_element('td', $tr); | ||
| 2057 | my $el = $self->create_element('a', $td); | ||
| 2058 | my $name = $self->_get_footnote_name($item->[0]); | ||
| 2059 | $el->setAttribute(href => '#'.$name.'u'); | ||
| 2060 | $el->setAttribute(name => $name); | ||
| 2061 | $self->append_text_node($el, '['.$item->[0].']'); | ||
| 2062 | $td = $self->create_element('td', $tr); | ||
| 2063 | $self->_apppend_paragraphs($td, $item->[1]); | ||
| 2064 | } | ||
| 2065 | $body->appendChild($table) if $table->hasChildNodes(); | ||
| 2066 | my $sectnum = $self->notes('footnote_snum') || 0; | ||
| 2067 | $self->notes(footnote_snum => $sectnum+1); | ||
| 2068 | $self->del_note('footnote'); | ||
| 2069 | $self->del_note('footnote_list'); | ||
| 2070 | } | ||
| 2071 | |||
| 2072 | ###### Fields | ||
| 2073 | |||
| 2074 | sub cw_field { $_[0]->group_notes(fldrslt_skip => 0) } | ||
| 2075 | |||
| 2076 | sub cw_fldinst { | ||
| 2077 | my $self = shift; | ||
| 2078 | my $txt = $self->get_parsed_group_text(); | ||
| 2079 | if($txt =~ /HYPERLINK\s+"?([^\s"]+)"?\s*$/){ | ||
| 2080 | $self->char_tag_attr('a', href => "$1"); | ||
| 2081 | $self->char_tag_notes('a', not_char_format => 1); | ||
| 2082 | }elsif($txt =~ /HYPERLINK\s+\\l\s+"?([^"]+)"?\s*$/){ | ||
| 2083 | my $name = $self->_get_ancor_name($1); | ||
| 2084 | $self->char_tag_attr('a', href => '#'.$name); | ||
| 2085 | $self->char_tag_notes('a', not_char_format => 1); | ||
| 2086 | }elsif($txt =~ /SYMBOL\s+(\d+)/){ | ||
| 2087 | my $code = $1; | ||
| 2088 | my ($fname) = $txt =~ /\\f\s+"([^"]+)"/; | ||
| 2089 | if($fname && (my ($font) = grep { $_->{face} eq $fname } | ||
| 2090 | map { $self->{notes}{$_} } | ||
| 2091 | grep { /^f\d+$/ } keys %{$self->{notes}})){ | ||
| 2092 | $self->notes(fldrslt_skip => 1); | ||
| 2093 | my $oldf = $self->notes(f => $font); | ||
| 2094 | if($txt =~ /\\s\s+(\d+)/){ | ||
| 2095 | my $size = $1; | ||
| 2096 | my $el= $self->create_element('font'); | ||
| 2097 | $el->setAttribute(style => "font-size: ${size}pt"); | ||
| 2098 | $self->append_text_node($el, $self->_encode_text(chr($code))); | ||
| 2099 | $self->append_element($el); | ||
| 2100 | }else{ | ||
| 2101 | $self->append_text(chr($code)); | ||
| 2102 | } | ||
| 2103 | $self->notes(f => $oldf); | ||
| 2104 | } | ||
| 2105 | }elsif($txt =~ /PAGEREF\s+(\S+)\s*\\h/){ | ||
| 2106 | my $href = $1; | ||
| 2107 | $self->notes(fldrslt_skip => 1); | ||
| 2108 | my $name = $self->_get_ancor_name($href); | ||
| 2109 | my $el = $self->create_element('a'); | ||
| 2110 | $el->setAttribute(href => '#'.$name); | ||
| 2111 | $self->append_text_node($el, '*'); | ||
| 2112 | $self->append_element($el); | ||
| 2113 | }elsif($txt =~ /\\import\s+([^}]+)/){ | ||
| 2114 | my $fname = $1; | ||
| 2115 | return if $self->{discard_images}; | ||
| 2116 | my $imgname = $fname; | ||
| 2117 | $imgname = File::Spec->catfile($self->{image_dir}, $imgname) if length $self->{image_dir}; | ||
| 2118 | return unless -s $imgname; | ||
| 2119 | my $info = Image::Info::image_info($imgname); | ||
| 2120 | my ($w, $h) = @{$info}{qw(width height)}; | ||
| 2121 | return unless $w && $h; | ||
| 2122 | $self->notes(fldrslt_skip => 1); | ||
| 2123 | my $url = join('/', grep { length } ($self->{image_uri}, $fname)); | ||
| 2124 | my $img = $self->append_tag('img'); | ||
| 2125 | $img->setAttribute(src => $url); | ||
| 2126 | $img->setAttribute(width => $w); | ||
| 2127 | $img->setAttribute(height => $h); | ||
| 2128 | $img->setAttribute(alt => ''); | ||
| 2129 | } | ||
| 2130 | } | ||
| 2131 | |||
| 2132 | sub cw_fldrslt { $_[0]->set_destination() if $_[0]->notes('fldrslt_skip') } | ||
| 2133 | |||
| 2134 | package RTF::HTMLConverter::Token; | ||
| 2135 | |||
| 2136 | sub new { | ||
| 2137 | my $class = shift; | ||
| 2138 | return bless [@_], ref($class) || $class; | ||
| 2139 | } | ||
| 2140 | |||
| 2141 | sub type { $_[0]->[0] } | ||
| 2142 | sub text { $_[0]->[1] } | ||
| 2143 | sub param { $_[0]->[2] } | ||
| 2144 | |||
| 2145 | package RTF::HTMLConverter::Element; | ||
| 2146 | |||
| 2147 | use constant { | ||
| 2148 | _NTS => 0, | ||
| 2149 | _ATR => 1, | ||
| 2150 | _STY => 2, | ||
| 2151 | _DAT => 3, | ||
| 2152 | }; | ||
| 2153 | |||
| 2154 | sub new { bless [{}, {}, {} ], ref($_[0]) || $_[0] } | ||
| 2155 | |||
| 2156 | sub modified { } | ||
| 2157 | |||
| 2158 | sub _notes { | ||
| 2159 | my ($self, $i) = splice(@_, 0, 2); | ||
| 2160 | if(@_ == 2){ | ||
| 2161 | my $old = $self->[$i]{$_[0]}; | ||
| 2162 | $self->[$i]{$_[0]} = $_[1]; | ||
| 2163 | $self->modified(1); | ||
| 2164 | return $old; | ||
| 2165 | } | ||
| 2166 | $self->[$i]{$_[0]}; | ||
| 2167 | } | ||
| 2168 | |||
| 2169 | sub notes { shift()->_notes(_NTS, @_) } | ||
| 2170 | sub attr { shift()->_notes(_ATR, @_) } | ||
| 2171 | sub style { shift()->_notes(_STY, @_) } | ||
| 2172 | |||
| 2173 | sub data { | ||
| 2174 | my $self = shift; | ||
| 2175 | if(@_){ | ||
| 2176 | my $old = $self->[_DAT]; | ||
| 2177 | $self->[_DAT] = $_[0]; | ||
| 2178 | $self->modified(1); | ||
| 2179 | return $old; | ||
| 2180 | } | ||
| 2181 | return $self->[_DAT]; | ||
| 2182 | } | ||
| 2183 | |||
| 2184 | # This method does not clone associated data in case the data is an | ||
| 2185 | # object or complex structure - it just stores a reference to the data. | ||
| 2186 | sub clone { | ||
| 2187 | my ($self, $wd) = @_; | ||
| 2188 | my $el = $self->new(); | ||
| 2189 | for my $i (_NTS, _ATR, _STY){ | ||
| 2190 | @{$el->[$i]}{keys %{$self->[$i]}} = values %{$self->[$i]} if %{$self->[$i]}; | ||
| 2191 | } | ||
| 2192 | $el->[_DAT] = $self->[_DAT] if $wd && exists $self->[_DAT]; | ||
| 2193 | return $el; | ||
| 2194 | } | ||
| 2195 | |||
| 2196 | # See comment to 'clone' method. | ||
| 2197 | sub merge { | ||
| 2198 | my ($self, $el, $wd) = @_; | ||
| 2199 | return unless $el; | ||
| 2200 | for my $i (_NTS, _ATR, _STY){ | ||
| 2201 | if(%{$el->[$i]}){ | ||
| 2202 | @{$self->[$i]}{keys %{$el->[$i]}} = values %{$el->[$i]}; | ||
| 2203 | $self->modified(1); | ||
| 2204 | } | ||
| 2205 | } | ||
| 2206 | if($wd && exists $el->[_DAT]){ | ||
| 2207 | $self->[_DAT] = $el->[_DAT]; | ||
| 2208 | $self->modified(1); | ||
| 2209 | } | ||
| 2210 | } | ||
| 2211 | |||
| 2212 | { | ||
| 2213 | my $pv = ': '; | ||
| 2214 | my $st = '; '; | ||
| 2215 | sub style_value { my $s=$_[1]||$_[0]->[_STY];join($st, map { $_.$pv.$s->{$_} } sort keys %$s) } | ||
| 2216 | sub style_hash { map { split(/\Q$pv/, $_) } split(/\Q$st/, $_[1] || $_[0]->[2]) } | ||
| 2217 | } | ||
| 2218 | |||
| 2219 | # This method assumes $elem->data() to be a DOM Node or a string. | ||
| 2220 | # In the latter case the string is assumed to be a tag name. | ||
| 2221 | sub set_element_attrs { | ||
| 2222 | my ($self, $doc, $name) = @_; | ||
| 2223 | my $el; | ||
| 2224 | if($name){ | ||
| 2225 | $el = $doc->createElement($name); | ||
| 2226 | my $dat = $self->data(); | ||
| 2227 | if(ref($dat) && UNIVERSAL::can($dat, 'getFirstChild')){ | ||
| 2228 | while((my $child = $dat->getFirstChild())){ | ||
| 2229 | $el->appendChild($child); | ||
| 2230 | } | ||
| 2231 | } | ||
| 2232 | }else{ | ||
| 2233 | $el = $self->data(); | ||
| 2234 | $el = $doc->createElement($el) unless ref $el; | ||
| 2235 | } | ||
| 2236 | my $hr = $self->[_ATR]; | ||
| 2237 | while(my ($k, $v) = each %{$hr || {}}){ | ||
| 2238 | $el->setAttribute($k => $v); | ||
| 2239 | } | ||
| 2240 | my $style = $self->style_value(); | ||
| 2241 | $el->setAttribute(style => $style) if length $style; | ||
| 2242 | $self->data($el); | ||
| 2243 | return $el; | ||
| 2244 | } | ||
| 2245 | |||
| 2246 | package RTF::HTMLConverter::ModificationTracker; | ||
| 2247 | |||
| 2248 | sub modified { | ||
| 2249 | my $self = shift; | ||
| 2250 | return $self->[$self->_MOD] unless @_; | ||
| 2251 | my $old = $self->[$self->_MOD]; | ||
| 2252 | $self->[$self->_MOD] = $_[0]; | ||
| 2253 | return $old; | ||
| 2254 | } | ||
| 2255 | |||
| 2256 | package RTF::HTMLConverter::ModElement; | ||
| 2257 | |||
| 2258 | use constant _MOD => 4; | ||
| 2259 | |||
| 2260 | our @ISA = qw(RTF::HTMLConverter::Element RTF::HTMLConverter::ModificationTracker); | ||
| 2261 | |||
| 2262 | sub clone { | ||
| 2263 | my $self = shift; | ||
| 2264 | my $el = $self->SUPER::clone(@_); | ||
| 2265 | $el->[_MOD] = $self->[_MOD]; | ||
| 2266 | return $el; | ||
| 2267 | } | ||
| 2268 | |||
| 2269 | package RTF::HTMLConverter::CharStack; | ||
| 2270 | |||
| 2271 | use constant { | ||
| 2272 | _STK => 0, | ||
| 2273 | _MST => 1, | ||
| 2274 | _MOD => 2, | ||
| 2275 | _CLA => 3, | ||
| 2276 | }; | ||
| 2277 | |||
| 2278 | our @ISA = qw(RTF::HTMLConverter::ModificationTracker); | ||
| 2279 | |||
| 2280 | sub new { bless [[[]], [0], 0, $_[1]->get_modelement_class()], ref($_[0]) || $_[0] } | ||
| 2281 | |||
| 2282 | sub _get_top { $_[0]->[_STK][0] || [] } | ||
| 2283 | |||
| 2284 | sub get_element_class { $_[0]->[_CLA] } | ||
| 2285 | |||
| 2286 | sub item { [ map { $_->clone(1) } @{$_[0]->_get_top()} ] } | ||
| 2287 | |||
| 2288 | sub modified { $_[0]->[_MST][0] = 1 if $_[1]; shift()->SUPER::modified(@_) } | ||
| 2289 | |||
| 2290 | sub dup { | ||
| 2291 | my $self = shift; | ||
| 2292 | my $top = $self->_get_top(); | ||
| 2293 | my $new = []; | ||
| 2294 | unshift @{$self->[_STK]}, $new; | ||
| 2295 | unshift @{$self->[_MST]}, 0; | ||
| 2296 | my $nel; | ||
| 2297 | for my $el (@$top) { | ||
| 2298 | $nel = $el->clone(1); | ||
| 2299 | push @$new, $nel; | ||
| 2300 | } | ||
| 2301 | } | ||
| 2302 | |||
| 2303 | sub drop { $_[0]->[_MOD] = 1 if shift @{$_[0]->[_MST]}; shift @{$_[0]->[_STK]} } | ||
| 2304 | |||
| 2305 | sub clear { | ||
| 2306 | my ($self, $sub) = @_; | ||
| 2307 | my $top = $self->_get_top(); | ||
| 2308 | my $count = @$top; | ||
| 2309 | return unless $count; | ||
| 2310 | if($sub){ | ||
| 2311 | @$top = grep { $sub->($_) } @$top; | ||
| 2312 | }else{ | ||
| 2313 | @$top = (); | ||
| 2314 | } | ||
| 2315 | $self->modified(1) if @$top != $count; | ||
| 2316 | } | ||
| 2317 | |||
| 2318 | sub _get_element { (grep { $_->data() eq $_[1] } @{$_[0]->_get_top()})[0] } | ||
| 2319 | |||
| 2320 | sub open_tag { | ||
| 2321 | my ($self, $tag) = @_; | ||
| 2322 | return if $self->_get_element($tag); | ||
| 2323 | my $el = $self->get_element_class()->new(); | ||
| 2324 | $el->data($tag); | ||
| 2325 | push @{$self->_get_top()}, $el; | ||
| 2326 | $self->modified(1); | ||
| 2327 | return; | ||
| 2328 | } | ||
| 2329 | |||
| 2330 | sub close_tag { | ||
| 2331 | my ($self, $tag) = @_; | ||
| 2332 | my $els = $self->_get_top(); | ||
| 2333 | my $count = @$els; | ||
| 2334 | @$els = grep { $_->data() ne $tag } @$els; | ||
| 2335 | $self->modified(1) if @$els != $count; | ||
| 2336 | return undef; | ||
| 2337 | } | ||
| 2338 | |||
| 2339 | sub _tag_notes { | ||
| 2340 | my ($self, $meth, $tag) = splice(@_, 0 , 3); | ||
| 2341 | my $created; | ||
| 2342 | my $el = $self->_get_element($tag); | ||
| 2343 | unless($el){ | ||
| 2344 | $el = $self->get_element_class()->new(); | ||
| 2345 | $el->data($tag); | ||
| 2346 | $created = 1; | ||
| 2347 | } | ||
| 2348 | my $ret = $el->$meth(@_); | ||
| 2349 | if($el->modified() || $created){ | ||
| 2350 | $self->modified(1); | ||
| 2351 | $el->modified(0); | ||
| 2352 | push @{$self->_get_top()}, $el if $created; | ||
| 2353 | } | ||
| 2354 | return $ret; | ||
| 2355 | } | ||
| 2356 | |||
| 2357 | sub tag_notes { shift()->_tag_notes('notes', @_) } | ||
| 2358 | sub tag_attr { shift()->_tag_notes('attr', @_) } | ||
| 2359 | sub tag_style { shift()->_tag_notes('style', @_) } | ||
| 2360 | |||
| 2361 | package RTF::HTMLConverter::Buffer; | ||
| 2362 | |||
| 2363 | # See comment before analogous definition in package RTF::HTMLConverter. | ||
| 2364 | use constant { | ||
| 2365 | ELEMENT_NODE => 1, | ||
| 2366 | TEXT_NODE => 3, | ||
| 2367 | }; | ||
| 2368 | |||
| 2369 | use constant { | ||
| 2370 | _DOC => 0, | ||
| 2371 | _CPS => 1, | ||
| 2372 | _DFR => 2, | ||
| 2373 | _TXT => 3, | ||
| 2374 | _CST => 4, | ||
| 2375 | _PST => 5, | ||
| 2376 | _FMT => 6, | ||
| 2377 | _ELC => 7, | ||
| 2378 | }; | ||
| 2379 | |||
| 2380 | sub new { | ||
| 2381 | my ($class, $conv) = @_; | ||
| 2382 | $conv = $class->get_document() if !$conv && ref($class); | ||
| 2383 | my $doc = $conv->get_document(); | ||
| 2384 | my $self = bless [ | ||
| 2385 | $doc, | ||
| 2386 | [], # Collection of paragraphs (of type 'Element') | ||
| 2387 | $doc->createDocumentFragment(), # Latest text mixed with tags and/or entities | ||
| 2388 | '', # Current plain text | ||
| 2389 | $conv->get_char_stack(), # converter's Character STack | ||
| 2390 | $conv->get_par_stack(), # converter's Paragrapth STack | ||
| 2391 | undef, # current character stack top (ForMaTting) | ||
| 2392 | $conv->get_element_class(), # ELement Class | ||
| 2393 | ], ref($class) || $class; | ||
| 2394 | $self->_create_par(); | ||
| 2395 | $self->[_FMT] = $self->[_CST]->item(); | ||
| 2396 | return $self; | ||
| 2397 | } | ||
| 2398 | |||
| 2399 | sub _create_par { | ||
| 2400 | my $self = shift; | ||
| 2401 | my $par = $self->[_ELC]->new(); | ||
| 2402 | $par->data($self->[_DOC]->createElement('p')); | ||
| 2403 | push @{$self->[_CPS]}, $par; | ||
| 2404 | return $par; | ||
| 2405 | } | ||
| 2406 | |||
| 2407 | sub _flush_text { | ||
| 2408 | my $self = shift; | ||
| 2409 | return unless length($self->[_TXT]); | ||
| 2410 | my $child = $self->[_DFR]->getLastChild(); | ||
| 2411 | if($child && $child->getNodeType() == TEXT_NODE){ | ||
| 2412 | $child->appendData($self->[_TXT]); | ||
| 2413 | }else{ | ||
| 2414 | $child = $self->[_DOC]->createTextNode($self->[_TXT]); | ||
| 2415 | $self->[_DFR]->appendChild($child); | ||
| 2416 | } | ||
| 2417 | $self->[_TXT] = ''; | ||
| 2418 | return 1; | ||
| 2419 | } | ||
| 2420 | |||
| 2421 | sub _flush_fragment { | ||
| 2422 | my $self = shift; | ||
| 2423 | $self->_flush_text(); | ||
| 2424 | my $fmt = $self->[_FMT]; | ||
| 2425 | $self->[_FMT] = $self->[_CST]->item(); | ||
| 2426 | return unless $self->[_DFR]->hasChildNodes() && @{$self->[_CPS]}; | ||
| 2427 | $self->[_CST]->modified(0); | ||
| 2428 | my $parent = $self->[_CPS][-1]->data(); | ||
| 2429 | for my $t (@{$fmt || []}){ | ||
| 2430 | my $el = $t->set_element_attrs($self->[_DOC]); | ||
| 2431 | $parent->appendChild($el); | ||
| 2432 | $parent = $el; | ||
| 2433 | } | ||
| 2434 | $parent->appendChild($self->[_DFR]); | ||
| 2435 | } | ||
| 2436 | |||
| 2437 | sub _flush_par { | ||
| 2438 | my $self = shift; | ||
| 2439 | $self->_flush_fragment(); | ||
| 2440 | my $par = $self->[_CPS][-1]; | ||
| 2441 | return unless $par; | ||
| 2442 | my $pe = $par->data(); | ||
| 2443 | $par->merge($self->[_PST][0]) unless $par->notes('no_par_stack'); | ||
| 2444 | $par->set_element_attrs($self->[_DOC], @_); | ||
| 2445 | return $par; | ||
| 2446 | } | ||
| 2447 | |||
| 2448 | sub _check_mod { $_[0]->_flush_fragment() if $_[0]->[_CST]->modified() } | ||
| 2449 | |||
| 2450 | sub get_document { $_[0]->[_DOC] } | ||
| 2451 | |||
| 2452 | sub add_text { $_[0]->_check_mod(); $_[0]->[_TXT] .= $_[1] } | ||
| 2453 | |||
| 2454 | sub add_node { $_[0]->_check_mod(); $_[0]->_flush_text(); $_[0]->[_DFR]->appendChild($_[1]) } | ||
| 2455 | |||
| 2456 | sub add_tag { | ||
| 2457 | my ($self, $tag) = @_; | ||
| 2458 | my $node = $self->[_DOC]->createElement($tag); | ||
| 2459 | $self->add_node($node); | ||
| 2460 | return $node; | ||
| 2461 | } | ||
| 2462 | |||
| 2463 | sub add_entity { | ||
| 2464 | my ($self, $name) = @_; | ||
| 2465 | my $entity = $self->[_DOC]->createEntityReference($name); | ||
| 2466 | $self->add_node($entity); | ||
| 2467 | return $entity; | ||
| 2468 | } | ||
| 2469 | |||
| 2470 | sub create_paragraph { | ||
| 2471 | my $self = shift; | ||
| 2472 | $self->_flush_par(@_); | ||
| 2473 | $self->_create_par(); | ||
| 2474 | return $self->[_CPS][-2]; | ||
| 2475 | } | ||
| 2476 | |||
| 2477 | sub _get_text_contents { | ||
| 2478 | my ($self, $el) = @_; | ||
| 2479 | my $txt = ''; | ||
| 2480 | my $child = $el->getFirstChild(); | ||
| 2481 | return $txt unless $child; | ||
| 2482 | do { | ||
| 2483 | my $type = $child->getNodeType(); | ||
| 2484 | if($type == TEXT_NODE){ | ||
| 2485 | $txt .= $child->getNodeValue(); | ||
| 2486 | }elsif($type == ELEMENT_NODE){ | ||
| 2487 | $txt .= $self->_get_text_contents($child); | ||
| 2488 | } | ||
| 2489 | } while($child = $child->getNextSibling()); | ||
| 2490 | return $txt; | ||
| 2491 | } | ||
| 2492 | |||
| 2493 | sub get_text { | ||
| 2494 | my $self = shift; | ||
| 2495 | my $txt = ''; | ||
| 2496 | for my $p (@{$self->[_CPS]}){ | ||
| 2497 | $txt .= $self->_get_text_contents($p->data()); | ||
| 2498 | } | ||
| 2499 | $txt .= $self->_get_text_contents($self->[_DFR]); | ||
| 2500 | $txt .= $self->[_TXT]; | ||
| 2501 | return $txt; | ||
| 2502 | } | ||
| 2503 | |||
| 2504 | sub get_paragraphs { $_[0]->_flush_par(); $_[0]->[_CPS] } | ||
| 2505 | |||
| 2506 | sub add_paragraphs { $_[0]->_flush_par(); push @{$_[0]->[_CPS]}, @{$_[1] || []} } | ||
| 2507 | |||
| 2508 | sub is_empty { | ||
| 2509 | @{$_[0]->[_CPS]} < 2 && | ||
| 2510 | !$_[0]->[_CPS][0]->data()->hasChildNodes() && | ||
| 2511 | !$_[0]->[_DFR]->hasChildNodes() && | ||
| 2512 | !length($_[0]->[_TXT]) | ||
| 2513 | } | ||
| 2514 | |||
| 2515 | sub merge { | ||
| 2516 | my ($self, $buf) = @_; | ||
| 2517 | $self->_flush_fragment(); | ||
| 2518 | my $cp = $self->[_CPS][-1]->data(); | ||
| 2519 | my $pars = $buf->get_paragraphs(); | ||
| 2520 | my $par = shift @$pars; | ||
| 2521 | my $p = $par->data(); | ||
| 2522 | while(my $child = $p->getFirstChild()){ | ||
| 2523 | $cp->appendChild($child); | ||
| 2524 | } | ||
| 2525 | push @{$self->[_CPS]}, @$pars; | ||
| 2526 | @$pars = (); | ||
| 2527 | } | ||
| 2528 | |||
| 2529 | package RTF::HTMLConverter::TableBuffer; | ||
| 2530 | |||
| 2531 | use constant { | ||
| 2532 | _TAB => 0, | ||
| 2533 | _TCS => 1, | ||
| 2534 | _TCP => 2, | ||
| 2535 | _CTR => 3, | ||
| 2536 | _CTC => 4, | ||
| 2537 | _CTX => 5, | ||
| 2538 | _CCC => 6, | ||
| 2539 | _CXC => 7, | ||
| 2540 | _CCL => 8, | ||
| 2541 | }; | ||
| 2542 | |||
| 2543 | sub new { | ||
| 2544 | my ($class, $conv) = @_; | ||
| 2545 | my $self = bless [ | ||
| 2546 | $conv->get_element_class()->new($conv), # TABle node | ||
| 2547 | [], # Table Cell nodeS | ||
| 2548 | [], # Table Cell Positions | ||
| 2549 | $conv->get_element_class()->new($conv), # Current Table Row node | ||
| 2550 | [], # Current Table row Cells | ||
| 2551 | [$conv->get_element_class()->new($conv)], # Current Table row cellXes | ||
| 2552 | 0, # Current Cell Count | ||
| 2553 | 0, # Current cellX Count | ||
| 2554 | ref($conv) || $conv, # Converter's CLass | ||
| 2555 | ], ref($class) || $class; | ||
| 2556 | $self->[_TAB]->data('table'); | ||
| 2557 | $self->[_CTR]->data('tr'); | ||
| 2558 | return $self; | ||
| 2559 | } | ||
| 2560 | |||
| 2561 | sub get_element { $_[0]->[_TAB] } | ||
| 2562 | sub get_row { $_[0]->[_CTR] } | ||
| 2563 | |||
| 2564 | sub clear_row_formatting { | ||
| 2565 | my $self = shift; | ||
| 2566 | $self->[_CTR] = $self->get_element()->new(); | ||
| 2567 | $self->[_CTR]->data('tr'); | ||
| 2568 | $self->[_CTX] = [$self->get_element()->new()]; | ||
| 2569 | $self->[_CXC] = 0; | ||
| 2570 | } | ||
| 2571 | |||
| 2572 | sub get_cellx { $_[0]->[_CTX][$_[0]->[_CXC]] ||= $_[0]->get_element()->new() } | ||
| 2573 | |||
| 2574 | sub add_cellx { | ||
| 2575 | my ($self, $pos) = @_; | ||
| 2576 | my $cell = $self->get_cellx(); | ||
| 2577 | $cell->notes(cellx => $pos); | ||
| 2578 | unless(grep { $_ == $pos } @{$self->[_TCP]}){ | ||
| 2579 | @{$self->[_TCP]} = sort { $a <=> $b } @{$self->[_TCP]}, $pos; | ||
| 2580 | $self->get_element()->notes(cellx_new => 1); | ||
| 2581 | } | ||
| 2582 | $self->[_CXC]++; | ||
| 2583 | } | ||
| 2584 | |||
| 2585 | sub get_cell { $_[0]->[_CTC][$_[0]->[_CCC]] } | ||
| 2586 | |||
| 2587 | sub add_cell { | ||
| 2588 | my ($self, $cell) = @_; | ||
| 2589 | $self->[_CTC][$self->[_CCC]] = $cell; | ||
| 2590 | $self->[_CCC]++; | ||
| 2591 | return $cell; | ||
| 2592 | } | ||
| 2593 | |||
| 2594 | # Currently we use only '\cellxN' to determine cells' width. | ||
| 2595 | # 'trftsWidth' and 'clftsWidth' will be taken into account later. | ||
| 2596 | sub add_row { | ||
| 2597 | my ($self, $doc) = @_; | ||
| 2598 | my $row = $self->get_row(); | ||
| 2599 | my $tr = $row->set_element_attrs($doc); | ||
| 2600 | my $row_height = $row->notes('height'); | ||
| 2601 | for (my $i = 0; $i < @{$self->[_CTC]}; $i++){ | ||
| 2602 | my $c = $self->[_CTC][$i]; | ||
| 2603 | my $fc = $self->[_CTX][$i]; | ||
| 2604 | $c->merge($fc) if $fc; | ||
| 2605 | if($c->notes('clvmrg')){ | ||
| 2606 | my $cx = $c->notes('cellx'); | ||
| 2607 | my $prevrow = $self->[_TCS][-1]; | ||
| 2608 | for my $pc (@$prevrow){ | ||
| 2609 | my $pcx = $pc->notes('cellx'); | ||
| 2610 | next if $pcx < $cx; | ||
| 2611 | last if $pcx > $cx; | ||
| 2612 | if($pc->notes('clvmgf') || $pc->notes('clvmrg')){ | ||
| 2613 | my $td = $pc->data(); | ||
| 2614 | last unless $td; | ||
| 2615 | my $rs = $td->getAttribute('rowspan'); | ||
| 2616 | $rs ||= 1; | ||
| 2617 | $rs++; | ||
| 2618 | $td->setAttribute(rowspan => $rs); | ||
| 2619 | $c->data($td); | ||
| 2620 | } | ||
| 2621 | last; | ||
| 2622 | } | ||
| 2623 | next; | ||
| 2624 | } | ||
| 2625 | next unless $c->data(); | ||
| 2626 | $c->style(height => $row_height) if $row_height; | ||
| 2627 | $c->set_element_attrs($doc); | ||
| 2628 | $tr->appendChild($c->data()); | ||
| 2629 | } | ||
| 2630 | if($self->get_element()->notes('cellx_new')){ | ||
| 2631 | $self->get_element()->notes(cellx_new => 0); | ||
| 2632 | push @{$self->[_TCS]}, $self->[_CTC]; | ||
| 2633 | for my $rw (@{$self->[_TCS]}){ | ||
| 2634 | $self->_format_row($rw); | ||
| 2635 | } | ||
| 2636 | }else{ | ||
| 2637 | $self->_format_row($self->[_CTC]); | ||
| 2638 | push @{$self->[_TCS]}, $self->[_CTC]; | ||
| 2639 | } | ||
| 2640 | $self->get_element()->data()->appendChild($tr); | ||
| 2641 | $self->[_CTR] = $row->clone(); | ||
| 2642 | $self->[_CTR]->data('tr'); | ||
| 2643 | $self->[_CTC] = []; | ||
| 2644 | $self->[_CCC] = 0; | ||
| 2645 | } | ||
| 2646 | |||
| 2647 | sub _format_row { | ||
| 2648 | my ($self, $row) = @_; | ||
| 2649 | my $cp = 0; | ||
| 2650 | my $px = 0; | ||
| 2651 | for my $c (@$row){ | ||
| 2652 | my $cx = $c->notes('cellx'); | ||
| 2653 | return unless defined $cx; | ||
| 2654 | my $td = $c->data(); | ||
| 2655 | next unless $td; | ||
| 2656 | $td->setAttribute(width => $self->[_CCL]->twips2pt($cx-$px)); | ||
| 2657 | $px = $cx; | ||
| 2658 | if($self->[_TCP][$cp] == $cx){ | ||
| 2659 | $cp++; | ||
| 2660 | next; | ||
| 2661 | } | ||
| 2662 | my $ci = 0; | ||
| 2663 | while($self->[_TCP][$cp] && $self->[_TCP][$cp] <= $cx){ | ||
| 2664 | $ci++; | ||
| 2665 | $cp++; | ||
| 2666 | } | ||
| 2667 | $td->setAttribute(colspan => $ci) if $ci > 1; | ||
| 2668 | } | ||
| 2669 | } | ||
| 2670 | |||
| 2671 | 1; | ||
| 2672 | |||
| 2673 | =head1 NAME | ||
| 2674 | |||
| 2675 | RTF::HTMLConverter - Converter from RTF format to HTML. | ||
| 2676 | |||
| 2677 | =head1 SYNOPSIS | ||
| 2678 | |||
| 2679 | use XML::GDOME; | ||
| 2680 | use RTF::HTMLConverter; | ||
| 2681 | my $parser = RTF::HTMLConverter->new(in => 'test.rtf', | ||
| 2682 | out => 'test.html'); | ||
| 2683 | $parser->parse(); | ||
| 2684 | |||
| 2685 | use XML::DOM; | ||
| 2686 | use RTF::HTMLConverter; | ||
| 2687 | open my $in, 'test.rtf' or die; | ||
| 2688 | my $parser = RTF::HTMLConverter->new( | ||
| 2689 | in => $in, | ||
| 2690 | out => 'test.html', | ||
| 2691 | DOMImplementation => 'XML::DOM', | ||
| 2692 | image_uri => "http://somewhere.net/images", | ||
| 2693 | codepage => 'iso-8859-1', | ||
| 2694 | ); | ||
| 2695 | $parser->parse(); | ||
| 2696 | |||
| 2697 | use XML::GDOME; | ||
| 2698 | use RTF::HTMLConverter; | ||
| 2699 | my $html = ''; | ||
| 2700 | my $parser = RTF::HTMLConverter->new( | ||
| 2701 | in => 'test.rtf', | ||
| 2702 | out => \$html, | ||
| 2703 | discard_images => 1, | ||
| 2704 | ); | ||
| 2705 | $parser->parse(); | ||
| 2706 | |||
| 2707 | |||
| 2708 | =head1 DESCRIPTION | ||
| 2709 | |||
| 2710 | RTF::HTMLConverter is a high-level RTF to HTML format converter. It is | ||
| 2711 | based on the low-level RTF parser module RTF::Lexer. Additionally, it | ||
| 2712 | requires the W3C's DOM implementation and it is known to work with either | ||
| 2713 | XML::DOM or XML::GDOME. | ||
| 2714 | |||
| 2715 | =head1 METHODS | ||
| 2716 | |||
| 2717 | =over 4 | ||
| 2718 | |||
| 2719 | =item new | ||
| 2720 | |||
| 2721 | The constructor. The following parameters are recognized: | ||
| 2722 | |||
| 2723 | =over 8 | ||
| 2724 | |||
| 2725 | =item in | ||
| 2726 | |||
| 2727 | Input file handle or a file name. Default value is C<\*STDIN>. | ||
| 2728 | See C<RTF::Lexer> for more information. | ||
| 2729 | |||
| 2730 | =item out | ||
| 2731 | |||
| 2732 | Output file handler or file name or scalar reference. If this parameter | ||
| 2733 | is a string it is treated as a file name and the constructor tries to | ||
| 2734 | open that file. If that file already exists, it is truncated. In the | ||
| 2735 | case of failure while opening the file an exception is thrown. If this | ||
| 2736 | parameter is a scalar reference the resulting html is stored in that | ||
| 2737 | scalar. | ||
| 2738 | |||
| 2739 | =item DOMImplementation | ||
| 2740 | |||
| 2741 | The DOM implementation module name. Supported values are C<XML::DOM> and | ||
| 2742 | C<XML::GDOME>. The default value is C<XML::GDOME>. | ||
| 2743 | |||
| 2744 | =item codepage | ||
| 2745 | |||
| 2746 | The charset of the resulted html-document. By default is C<utf8>. | ||
| 2747 | This parameter is recognized only if DOMImplementation is C<XML::GDOME>. | ||
| 2748 | |||
| 2749 | =item formatting | ||
| 2750 | |||
| 2751 | The formatting of the resulted html-document. This parameter is recognized | ||
| 2752 | only if DOMImplementation is C<XML::GDOME>. Possible values are: | ||
| 2753 | C<GDOME_SAVE_STANDARD> and C<GDOME_SAVE_LIBXML_INDENT>. See C<XML::GDOME::Document> | ||
| 2754 | for more information. Default value is C<GDOME_SAVE_LIBXML_INDENT>. | ||
| 2755 | |||
| 2756 | =item doctype | ||
| 2757 | |||
| 2758 | A reference to an array (C<$name>, C<$publicId>, C<$systemId>) if | ||
| 2759 | DOMImplementation is C<XML::GDOME> or (C<$name>, C<$systemId>, C<$publicId>) | ||
| 2760 | if DOMImplementation is C<XML::DOM>. Default values are: | ||
| 2761 | |||
| 2762 | =over 12 | ||
| 2763 | |||
| 2764 | =item $name | ||
| 2765 | |||
| 2766 | C<HTML> | ||
| 2767 | |||
| 2768 | =item $publicId | ||
| 2769 | |||
| 2770 | C<-//W3C//DTD HTML 4.01 Transitional//EN> | ||
| 2771 | |||
| 2772 | =item $systemId | ||
| 2773 | |||
| 2774 | C<http://www.w3.org/TR/html4/loose.dtd> | ||
| 2775 | |||
| 2776 | =back | ||
| 2777 | |||
| 2778 | =item discard_images | ||
| 2779 | |||
| 2780 | Being set, this parameter disables any image processing. By default | ||
| 2781 | it is unset. | ||
| 2782 | |||
| 2783 | =item image_uri | ||
| 2784 | |||
| 2785 | The string that being concatenated with the image name gives this | ||
| 2786 | image's URL. Default value is empty string. | ||
| 2787 | |||
| 2788 | =item image_dir | ||
| 2789 | |||
| 2790 | A directory name where the images are generated. Default value is empty | ||
| 2791 | string which means the current directory. | ||
| 2792 | |||
| 2793 | =item image_names | ||
| 2794 | |||
| 2795 | The pattern for generating image names from there number. Default value | ||
| 2796 | is C<img%d>. | ||
| 2797 | |||
| 2798 | =item image_convert | ||
| 2799 | |||
| 2800 | A path to ImageMagick's C<convert> utility. Default value is simply | ||
| 2801 | C<convert> assuming it is in one of the $ENV{PATH} directories. | ||
| 2802 | |||
| 2803 | =item image_mogrify | ||
| 2804 | |||
| 2805 | A path to ImageMagick's C<mogrify> utility. If the value is C<undef> or | ||
| 2806 | the specified file does not exists, the images extracted from RTF will | ||
| 2807 | not be scaled. Default value is C<mogrify>. | ||
| 2808 | |||
| 2809 | =item image_wmf2eps | ||
| 2810 | |||
| 2811 | A path to libwmf's C<wmf2eps> utility. If the value is C<undef> or the | ||
| 2812 | specified file does not exists, the WMF-images will not be extracted | ||
| 2813 | from RTF. Default value is C<wmf2eps>. | ||
| 2814 | |||
| 2815 | =item screen_resolution | ||
| 2816 | |||
| 2817 | The display resolution in dpi. Default value is 100. | ||
| 2818 | |||
| 2819 | =back | ||
| 2820 | |||
| 2821 | =item parse | ||
| 2822 | |||
| 2823 | Parses the input RTF stream until the end of file. | ||
| 2824 | |||
| 2825 | =back | ||
| 2826 | |||
| 2827 | =head1 SEE ALSO | ||
| 2828 | |||
| 2829 | RTF::Lexer, Rich Text Format (RTF) Specification (version 1.7), The_RTF_Cookbook, | ||
| 2830 | RTF::Parser, RTF::Tokenizer. | ||
| 2831 | |||
| 2832 | =head1 KNOWN BUGS | ||
| 2833 | |||
| 2834 | =over 4 | ||
| 2835 | |||
| 2836 | =item - | ||
| 2837 | |||
| 2838 | The symbols that absent in Unicode character set will be displayed incorrectly. | ||
| 2839 | |||
| 2840 | =item - | ||
| 2841 | |||
| 2842 | The images that are stored in RTF file in WMF format may be scaled incorrectly. | ||
| 2843 | |||
| 2844 | =item - | ||
| 2845 | |||
| 2846 | The text in WMF images in non-ASCII charset may be displayed incorrectly. | ||
| 2847 | |||
| 2848 | =back | ||
| 2849 | |||
| 2850 | And there should be lots of unknown bugs;) | ||
| 2851 | |||
| 2852 | =head1 AUTHOR | ||
| 2853 | |||
| 2854 | Vadim O. Ustiansky <ustiansky@cpan.org> | ||
| 2855 |