| 1 | # App::pod2pdf | ||
| 2 | # | ||
| 3 | # Copyright (C) 2007 Jon Allen <jj@jonallen.info> | ||
| 4 | # | ||
| 5 | # This software is licensed under the terms of the Artistic | ||
| 6 | # License version 2.0. | ||
| 7 | # | ||
| 8 | # For full license details, please read the file 'artistic-2_0.txt' | ||
| 9 | # included with this distribution, or see | ||
| 10 | # http://www.perlfoundation.org/legal/licenses/artistic-2_0.html | ||
| 11 | |||
| 12 | package App::pod2pdf; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use Carp; | ||
| 17 | use List::Util qw/max min/; | ||
| 18 | use PDF::API2; | ||
| 19 | use Pod::Escapes qw/e2char/; | ||
| 20 | use Pod::Parser; | ||
| 21 | use Pod::ParseLink; | ||
| 22 | |||
| 23 | use constant TRUE => 1; | ||
| 24 | use constant FALSE => 0; | ||
| 25 | |||
| 26 | BEGIN { | ||
| 27 | our @ISA = qw/Pod::Parser/; | ||
| 28 | our $VERSION = '0.42'; | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | #----------------------------------------------------------------------- | ||
| 33 | #----------------------------------------------------------------------- | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $invocant = shift; | ||
| 37 | my $class = ref($invocant) || $invocant; | ||
| 38 | |||
| 39 | my %user_options = @_; | ||
| 40 | my %default_options = ( | ||
| 41 | header => TRUE, # Include header on all pages | ||
| 42 | footer => TRUE, # Include footer on all pages | ||
| 43 | page_width => 595, # A4 | ||
| 44 | page_height => 842, # A4 | ||
| 45 | left_margin => $user_options{margins} || 48, # 0.75" | ||
| 46 | right_margin => $user_options{margins} || 48, # 0.75" | ||
| 47 | top_margin => $user_options{margins} || 60, # | ||
| 48 | bottom_margin => $user_options{margins} || 60, # | ||
| 49 | font_face => 'Helvetica', # Sans-Serif text | ||
| 50 | font_size => 10, # Text size = 10 points | ||
| 51 | icon_scale => 0.25, # Icon scaling (%age) | ||
| 52 | ); | ||
| 53 | |||
| 54 | my $self = $class->SUPER::new(%default_options,%user_options); | ||
| 55 | |||
| 56 | $self->create_pdf; | ||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | #----------------------------------------------------------------------- | ||
| 62 | |||
| 63 | sub command { | ||
| 64 | my ($self, $command, $paragraph, $line_num) = @_; | ||
| 65 | my $expansion = $self->interpolate($paragraph, $line_num); | ||
| 66 | |||
| 67 | COMMAND: { | ||
| 68 | if ($command eq 'ff') { | ||
| 69 | $self->formfeed if ($self->print_flag); | ||
| 70 | } | ||
| 71 | if ($command =~ /^head[1234]$/) { | ||
| 72 | $self->indent(0); | ||
| 73 | $self->set_style('default'); | ||
| 74 | $self->newline; | ||
| 75 | my $default_space = $self->{line_spacing}; | ||
| 76 | $self->set_style($command); | ||
| 77 | my $heading_space = $self->{line_spacing}; | ||
| 78 | # Checks to see if there is space for a content line after | ||
| 79 | # the heading - if not then starts a new page | ||
| 80 | if ( ($self->{y_position} - $heading_space - $default_space - $self->{spacer}) < | ||
| 81 | ($self->{bottom_margin} + $self->{footer_height}) ) { | ||
| 82 | $self->formfeed; | ||
| 83 | } else { | ||
| 84 | $self->{y_position} -= ($heading_space - $default_space); | ||
| 85 | } | ||
| 86 | $self->print_text_with_style($expansion,$command); | ||
| 87 | $self->spacer; | ||
| 88 | $self->indent(48); | ||
| 89 | } | ||
| 90 | if ($command eq 'over') { | ||
| 91 | my $indentlevel = $expansion || 4; | ||
| 92 | $self->set_style; | ||
| 93 | $self->push_indent($indentlevel * $self->em); | ||
| 94 | $self->reset_item_textblock_flag; | ||
| 95 | } | ||
| 96 | if ($command eq 'back') { | ||
| 97 | $self->pop_indent; | ||
| 98 | $self->spacer; | ||
| 99 | } | ||
| 100 | if ($command eq 'item') { | ||
| 101 | $self->spacer if ($self->item_textblock_flag); | ||
| 102 | $self->reset_item_textblock_flag; | ||
| 103 | if ($expansion =~ '^\s*\*?\s*$') { | ||
| 104 | # First check to see if there is space for any text | ||
| 105 | if ($self->{y_position} - $self->{line_spacing} < ($self->{bottom_margin} + $self->{footer_height})) { | ||
| 106 | $self->formfeed; | ||
| 107 | } | ||
| 108 | my $indent = $self->pop_indent; | ||
| 109 | $self->bullet($indent); | ||
| 110 | $self->push_indent($indent); | ||
| 111 | } elsif ($expansion =~ '^\s*(\d+\.?)\s*$') { | ||
| 112 | # First check to see if there is space for any text | ||
| 113 | if ($self->{y_position} - $self->{line_spacing} < ($self->{bottom_margin} + $self->{footer_height})) { | ||
| 114 | $self->formfeed; | ||
| 115 | } | ||
| 116 | my $indent = $self->pop_indent; | ||
| 117 | $self->{y_position} -= $self->{line_spacing}; | ||
| 118 | $self->print_text_with_style($1,'default'); | ||
| 119 | $self->push_indent($indent); | ||
| 120 | $self->{y_position} += $self->{line_spacing}; | ||
| 121 | } else { | ||
| 122 | my $indent = $self->pop_indent; | ||
| 123 | $self->set_style; | ||
| 124 | $self->newline; | ||
| 125 | $self->parse_text({-expand_ptree => 'print_tree'},$paragraph,$line_num); | ||
| 126 | $self->spacer; | ||
| 127 | $self->push_indent($indent); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | #----------------------------------------------------------------------- | ||
| 135 | |||
| 136 | sub verbatim { | ||
| 137 | my ($self, $paragraph, $line_num) = @_; | ||
| 138 | if ($paragraph =~ /^[ \t]/) { | ||
| 139 | $self->set_style('verbatim'); | ||
| 140 | $self->reset_space_flag; | ||
| 141 | $self->set_item_textblock_flag; | ||
| 142 | foreach my $line (split /\n/,$paragraph) { | ||
| 143 | # todo: expand tabs | ||
| 144 | if ($line =~ /\S/) { | ||
| 145 | $self->newline; | ||
| 146 | $self->print_text_with_style($line,'verbatim'); | ||
| 147 | $self->reset_space_flag; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | $self->newline; | ||
| 151 | $self->spacer unless ($self->over); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | #----------------------------------------------------------------------- | ||
| 157 | |||
| 158 | sub textblock { | ||
| 159 | my ($self, $text, $line_num) = @_; | ||
| 160 | if ($text =~ /\S/) { # ignore blank paragraphs | ||
| 161 | $self->set_item_textblock_flag; | ||
| 162 | $self->reset_space_flag; | ||
| 163 | $self->set_style; | ||
| 164 | $self->newline; | ||
| 165 | $self->parse_text({-expand_ptree => 'print_tree'},$text,$line_num); | ||
| 166 | $self->spacer; | ||
| 167 | $self->spacer unless ($self->over); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | #----------------------------------------------------------------------- | ||
| 173 | |||
| 174 | sub interior_sequence { | ||
| 175 | my ($self,$command,$text) = @_; | ||
| 176 | # | ||
| 177 | # need to check content of $text, i.e. | ||
| 178 | # is there a nested formatting command? | ||
| 179 | # | ||
| 180 | # also this doesn't handle the L<> formatting | ||
| 181 | # command, check with perlpodspec if this is | ||
| 182 | # allowed in =head blocks | ||
| 183 | # | ||
| 184 | COMMAND: { | ||
| 185 | if ($command eq 'X') { | ||
| 186 | # no-op | ||
| 187 | last COMMAND; | ||
| 188 | } | ||
| 189 | if ($command eq 'Z') { | ||
| 190 | # no-op | ||
| 191 | last COMMAND; | ||
| 192 | } | ||
| 193 | if ($command eq 'E') { | ||
| 194 | return e2char($text); | ||
| 195 | } | ||
| 196 | DEFAULT: { | ||
| 197 | return $text; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | #----------------------------------------------------------------------- | ||
| 204 | |||
| 205 | sub print_tree { | ||
| 206 | my $self = shift; | ||
| 207 | my $tree = shift; | ||
| 208 | NODE: foreach my $node ($tree->children) { | ||
| 209 | if (ref $node) { | ||
| 210 | COMMAND: { | ||
| 211 | my $command = $node->cmd_name; | ||
| 212 | if ($command eq 'L') { | ||
| 213 | #warn("Found link: ".$node->raw_text."\n"); | ||
| 214 | my $left_delimiter = $node->left_delimiter; | ||
| 215 | my $right_delimiter = $node->right_delimiter; | ||
| 216 | (my $link_text = $node->raw_text) =~ s/L$left_delimiter\s*(.*?)\s*$right_delimiter$/$1/s; | ||
| 217 | my ($text, $inferred, $name, $section, $type) = parselink($link_text); | ||
| 218 | $text =~ s/^"(.*?)"$/$1/ if ($text); | ||
| 219 | $inferred =~ s/^"(.*?)"$/$1/ if ($inferred); | ||
| 220 | $name =~ s/^"(.*?)"$/$1/ if ($name); | ||
| 221 | $self->push_format('I'); | ||
| 222 | $self->parse_text({-expand_ptree => 'print_tree'},($text || $inferred || $name)); | ||
| 223 | $self->pop_format; | ||
| 224 | last COMMAND; | ||
| 225 | } | ||
| 226 | if ($command eq 'O') { | ||
| 227 | my $left_delimiter = $node->left_delimiter; | ||
| 228 | my $right_delimiter = $node->right_delimiter; | ||
| 229 | (my $object_text = $node->raw_text) =~ s/O$left_delimiter\s*(.*?)\s*$right_delimiter$/$1/; | ||
| 230 | my ($object_title,$object_location) = parseobject($object_text); | ||
| 231 | if ($object_location =~ /\A\W+:[^:\s]\S*\z/) { | ||
| 232 | # URL - cannot load (yet!) | ||
| 233 | $self->warnonce('HTTP object loading not supported'); | ||
| 234 | $self->print_text_with_style($object_location,'I'); | ||
| 235 | } elsif (-e $object_location) { | ||
| 236 | # Found file | ||
| 237 | if ($self->images) { | ||
| 238 | my $mime_type = File::Type->new->mime_type($object_location); | ||
| 239 | if ($mime_type =~ /^image/) { | ||
| 240 | unless ($self->insert_image($object_location)) { | ||
| 241 | $self->print_text_with_style($object_location,'I'); | ||
| 242 | } | ||
| 243 | } else { | ||
| 244 | $self->print_text_with_style($object_location,'I'); | ||
| 245 | } | ||
| 246 | } else { | ||
| 247 | $self->print_text_with_style($object_location,'I'); | ||
| 248 | } | ||
| 249 | } else { | ||
| 250 | # Non-existant file | ||
| 251 | $self->warnonce("Object not found: $object_location"); | ||
| 252 | $self->print_text_with_style("Object not found: $object_location",'I'); | ||
| 253 | } | ||
| 254 | last COMMAND; | ||
| 255 | } | ||
| 256 | if ($command eq 'X') { | ||
| 257 | # no-op | ||
| 258 | } | ||
| 259 | DEFAULT: { | ||
| 260 | $self->push_format($node->cmd_name); | ||
| 261 | $self->print_tree($node->parse_tree); | ||
| 262 | $self->pop_format; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } else { | ||
| 266 | FORMAT: { | ||
| 267 | $_ = $self->format; | ||
| 268 | if (/X/) { | ||
| 269 | # no-op | ||
| 270 | last FORMAT; | ||
| 271 | } | ||
| 272 | if (/Z/) { | ||
| 273 | # no-op | ||
| 274 | last FORMAT; | ||
| 275 | } | ||
| 276 | if (/E/) { | ||
| 277 | $node = e2char($node); | ||
| 278 | } | ||
| 279 | if (/BC.*I/) { | ||
| 280 | $self->print_text_with_style($node,'BCI'); | ||
| 281 | last FORMAT; | ||
| 282 | } | ||
| 283 | if (/C.*I/) { | ||
| 284 | $self->print_text_with_style($node,'CI'); | ||
| 285 | last FORMAT; | ||
| 286 | } | ||
| 287 | if (/B.*I/) { | ||
| 288 | $self->print_text_with_style($node,'BI'); | ||
| 289 | last FORMAT; | ||
| 290 | } | ||
| 291 | if (/BC/) { | ||
| 292 | $self->print_text_with_style($node,'BC'); | ||
| 293 | last FORMAT; | ||
| 294 | } | ||
| 295 | if (/B/) { | ||
| 296 | $self->print_text_with_style($node,'B'); | ||
| 297 | last FORMAT; | ||
| 298 | } | ||
| 299 | if (/C/) { | ||
| 300 | $self->print_text_with_style($node,'C'); | ||
| 301 | last FORMAT; | ||
| 302 | } | ||
| 303 | if (/I/) { | ||
| 304 | $self->print_text_with_style($node,'I'); | ||
| 305 | last FORMAT; | ||
| 306 | } | ||
| 307 | DEFAULT: { | ||
| 308 | #warn "Line 414: $_\n"; | ||
| 309 | $self->print_text_with_style($node,'default'); | ||
| 310 | last FORMAT; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | #----------------------------------------------------------------------- | ||
| 319 | |||
| 320 | sub insert_image { | ||
| 321 | my $self = shift; | ||
| 322 | my $filename = shift; | ||
| 323 | |||
| 324 | if ($self->images) { | ||
| 325 | if (-e $filename) { | ||
| 326 | my $image; | ||
| 327 | my $type = File::Type->new->checktype_filename($filename); | ||
| 328 | SWITCH: { | ||
| 329 | if ($type eq 'image/jpeg') {$image = $self->{pdf}->image_jpeg($filename); last} | ||
| 330 | if ($type eq 'image/tiff') {$image = $self->{pdf}->image_tiff($filename); last} | ||
| 331 | if ($type eq 'image/gif') {$image = $self->{pdf}->image_gif($filename); last} | ||
| 332 | if ($type eq 'image/x-png') {$image = $self->{pdf}->image_png($filename); last} | ||
| 333 | if ($type eq 'image/x-pnm') {$image = $self->{pdf}->image_pnm($filename); last} | ||
| 334 | $self->warnonce("[Warning] Unknown image format '$type' for image '$filename'"); | ||
| 335 | return FALSE; | ||
| 336 | } | ||
| 337 | |||
| 338 | unless ($image) { | ||
| 339 | $self->warnonce("[Warning] Cannot load image file '$filename'"); | ||
| 340 | return FALSE; | ||
| 341 | } | ||
| 342 | |||
| 343 | my ($width,$height) = imgsize($filename); | ||
| 344 | my $available_width = $self->{page_width} - $self->{right_margin} - $self->{x_position}; | ||
| 345 | my $scale_default = 0.5; | ||
| 346 | my $scale_min = 0.4; | ||
| 347 | my $scale = min($available_width / $width, $scale_default); | ||
| 348 | my $height_in_points = $height * $scale; | ||
| 349 | |||
| 350 | if ($self->{y_position} < ($self->{bottom_margin} + $self->{footer_height} + $height_in_points + ($self->{line_spacing} / 2))) { | ||
| 351 | my $available_height = $self->{y_position} - $self->{bottom_margin} - $self->{footer_height} - $self->{line_spacing}; | ||
| 352 | if ($available_height / $height > $scale_min) { | ||
| 353 | $scale = $available_height / $height; | ||
| 354 | $height_in_points = $height * $scale; | ||
| 355 | } else { | ||
| 356 | $self->formfeed; | ||
| 357 | $self->set_print_flag; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | $self->{y_position} -= $height_in_points; | ||
| 362 | $self->{y_position} += ($self->{line_spacing} / 2); | ||
| 363 | $self->{gfx} = $self->{page}->gfx unless (exists $self->{gfx}); | ||
| 364 | $self->{gfx}->image($image,$self->{x_position},$self->{y_position},$scale); | ||
| 365 | return TRUE; | ||
| 366 | } else { | ||
| 367 | $self->warnonce("Image '$filename' does not exist"); | ||
| 368 | return FALSE; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | #----------------------------------------------------------------------- | ||
| 375 | |||
| 376 | sub images { | ||
| 377 | my $self = shift; | ||
| 378 | |||
| 379 | unless ($self->{image_modules_check}) { | ||
| 380 | # Check if image modules are installed | ||
| 381 | eval "use File::Type;use Image::Size;"; | ||
| 382 | if ($@) { | ||
| 383 | $self->warnonce('Cannot use images, modules Image::Size and/or File::Type not installed'); | ||
| 384 | } else { | ||
| 385 | $self->{image_modules_loaded} = TRUE; | ||
| 386 | } | ||
| 387 | $self->{image_modules_check} = TRUE; | ||
| 388 | } | ||
| 389 | |||
| 390 | return $self->{image_modules_loaded}; | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | #----------------------------------------------------------------------- | ||
| 395 | |||
| 396 | sub warnonce { | ||
| 397 | my $self = shift; | ||
| 398 | my $warning = shift; | ||
| 399 | |||
| 400 | unless ($self->{issued_warnings}->{$warning}) { | ||
| 401 | warn("[Warning] $warning\n"); | ||
| 402 | $self->{issued_warnings}->{$warning} = TRUE; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | |||
| 407 | #----------------------------------------------------------------------- | ||
| 408 | |||
| 409 | sub parseobject { | ||
| 410 | # Parses the O<...> formatting code as specified in perlpodextensions | ||
| 411 | my $object_text = shift; | ||
| 412 | if ($object_text =~ /(.*?)\|(.*)/) { | ||
| 413 | return ($1,$2); | ||
| 414 | } else { | ||
| 415 | return (undef,$object_text); | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | #----------------------------------------------------------------------- | ||
| 421 | |||
| 422 | sub create_pdf { | ||
| 423 | my $self = shift; | ||
| 424 | my $class = ref $self; | ||
| 425 | my $version = $::{$class.'::'}{VERSION} ? ${ $::{$class.'::'}{VERSION} } : 'unknown'; | ||
| 426 | |||
| 427 | # Define styles | ||
| 428 | # | ||
| 429 | # Future enhancement: move the style definitions into a separate | ||
| 430 | # module (e.g. Pod::Pdf::Styles) which can be subclassed to allow | ||
| 431 | # non-core fonts to be used. | ||
| 432 | # | ||
| 433 | $self->{stylist} = { | ||
| 434 | 'header' => {font=>'Helvetica-Bold', size=>10 }, | ||
| 435 | 'footer' => {font=>'Helvetica-Bold', size=>10 }, | ||
| 436 | 'head1' => {font=>'Helvetica-Bold', size=>12 }, | ||
| 437 | 'head2' => {font=>'Helvetica-Bold', size=>11 }, | ||
| 438 | 'head3' => {font=>'Helvetica-Bold', size=>10 }, | ||
| 439 | 'head4' => {font=>'Helvetica', size=>10 }, | ||
| 440 | 'verbatim' => {font=>'Courier', verbatim=>TRUE }, | ||
| 441 | 'B' => {font=>'Helvetica-Bold' }, | ||
| 442 | 'BC' => {font=>'Courier-Bold', verbatim=>TRUE }, | ||
| 443 | 'BI' => {font=>'Helvetica-BoldOblique' }, | ||
| 444 | 'BCI' => {font=>'Courier-BoldOblique', verbatim=>TRUE }, | ||
| 445 | 'C' => {font=>'Courier', verbatim=>TRUE }, | ||
| 446 | 'CI' => {font=>'Courier-Oblique', verbatim=>TRUE }, | ||
| 447 | 'I' => {font=>'Helvetica-Oblique' }, | ||
| 448 | }; | ||
| 449 | |||
| 450 | # Set up first page | ||
| 451 | PAGE_SIZE: { | ||
| 452 | if ($self->{page_size}) { | ||
| 453 | eval "use Paper::Specs 0.10 units=>'pt';"; | ||
| 454 | if ($@) { | ||
| 455 | $self->warnonce("Cannot use '--page-size' option, module Paper::Specs (v0.10) not installed"); | ||
| 456 | } else { | ||
| 457 | if (my $form = Paper::Specs->find(code=>$self->{page_size}, brand=>'standard')) { | ||
| 458 | $self->{page_width} = int($form->sheet_width + 0.5); | ||
| 459 | $self->{page_height} = int($form->sheet_height + 0.5); | ||
| 460 | } else { | ||
| 461 | $self->warnonce("Unknown page size '".$self->{page_size}."'"); | ||
| 462 | } | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | PAGE_ORIENTATION: { | ||
| 468 | if ($self->{page_orientation}) { | ||
| 469 | if (lc $self->{page_orientation} eq 'landscape') { | ||
| 470 | ($self->{page_width},$self->{page_height}) = ( | ||
| 471 | max($self->{page_width},$self->{page_height}), | ||
| 472 | min($self->{page_width},$self->{page_height}) | ||
| 473 | ); | ||
| 474 | last PAGE_ORIENTATION; | ||
| 475 | } | ||
| 476 | if (lc $self->{page_orientation} eq 'portrait') { | ||
| 477 | ($self->{page_width},$self->{page_height}) = ( | ||
| 478 | min($self->{page_width},$self->{page_height}), | ||
| 479 | max($self->{page_width},$self->{page_height}) | ||
| 480 | ); | ||
| 481 | last PAGE_ORIENTATION; | ||
| 482 | } | ||
| 483 | $self->warnonce("Unknown page orientation '".$self->{page_orientation}."', must be 'portrait' or 'landscape'"); | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | $self->{page_number} = 0; | ||
| 488 | $self->{line_spacing} = $self->{font_size}+2 unless ($self->{line_spacing}); | ||
| 489 | $self->{x_position} = $self->{left_margin}; | ||
| 490 | $self->{y_position} = $self->{page_height} - $self->{top_margin}; | ||
| 491 | $self->{indent} = 0; | ||
| 492 | $self->{pdf} = PDF::API2->new; | ||
| 493 | $self->{pdf}->info('Producer'=>"$class version $version"); | ||
| 494 | $self->{pdf}->mediabox($self->{page_width},$self->{page_height}); | ||
| 495 | |||
| 496 | if ($self->{icon} && $self->images) { | ||
| 497 | if (-e $self->{icon}) { | ||
| 498 | my $type = File::Type->new->checktype_filename($self->{icon}); | ||
| 499 | SWITCH: { | ||
| 500 | if ($type eq 'image/jpeg') {$self->{icon_img} = $self->{pdf}->image_jpeg($self->{icon}); last} | ||
| 501 | if ($type eq 'image/tiff') {$self->{icon_img} = $self->{pdf}->image_tiff($self->{icon}); last} | ||
| 502 | if ($type eq 'image/gif') {$self->{icon_img} = $self->{pdf}->image_gif($self->{icon}); last} | ||
| 503 | if ($type eq 'image/x-png') {$self->{icon_img} = $self->{pdf}->image_png($self->{icon}); last} | ||
| 504 | if ($type eq 'image/x-pnm') {$self->{icon_img} = $self->{pdf}->image_pnm($self->{icon}); last} | ||
| 505 | warn "[Warning] Unknown image format '$type' for icon ".$self->{icon}."\n"; | ||
| 506 | } | ||
| 507 | if ($self->{icon_img}) { | ||
| 508 | ($self->{icon_width},$self->{icon_height}) = imgsize($self->{icon}); | ||
| 509 | } | ||
| 510 | } else { | ||
| 511 | warn("[Warning] Cannot open icon file: ".$self->{icon}."\n"); | ||
| 512 | } | ||
| 513 | } | ||
| 514 | |||
| 515 | $self->formfeed; | ||
| 516 | $self->set_style; | ||
| 517 | $self->{indent} = 0; | ||
| 518 | $self->{over} = 0; | ||
| 519 | $self->{spacer} = 4; # default spacing between paragraphs | ||
| 520 | } | ||
| 521 | |||
| 522 | |||
| 523 | #----------------------------------------------------------------------- | ||
| 524 | #----------------------------------------------------------------------- | ||
| 525 | |||
| 526 | # Item_textblock_flag methods | ||
| 527 | # | ||
| 528 | # This flag is used to control line spacing within =over sections. The | ||
| 529 | # flag is cleared after each =item command and set whenever a textblock | ||
| 530 | # is printed. | ||
| 531 | # | ||
| 532 | # At the start of processing an =item command, an extra half line space | ||
| 533 | # (4 points) is inserted if the textblock flag is set. Because half | ||
| 534 | # spacing is the default in =over sections, this extra space between | ||
| 535 | # individual =items acts to visually group the =item paragraphs as a | ||
| 536 | # single element. | ||
| 537 | |||
| 538 | #----------------------------------------------------------------------- | ||
| 539 | |||
| 540 | sub item_textblock_flag { | ||
| 541 | my $self = shift; | ||
| 542 | return $self->{item_textblock_flag}->{$self->over} || 0; | ||
| 543 | } | ||
| 544 | |||
| 545 | |||
| 546 | #----------------------------------------------------------------------- | ||
| 547 | |||
| 548 | sub set_item_textblock_flag { | ||
| 549 | my $self = shift; | ||
| 550 | $self->{item_textblock_flag}->{$self->over} = TRUE; | ||
| 551 | } | ||
| 552 | |||
| 553 | |||
| 554 | #----------------------------------------------------------------------- | ||
| 555 | |||
| 556 | sub reset_item_textblock_flag { | ||
| 557 | my $self = shift; | ||
| 558 | $self->{item_textblock_flag}->{$self->over} = FALSE; | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | #----------------------------------------------------------------------- | ||
| 563 | #----------------------------------------------------------------------- | ||
| 564 | |||
| 565 | # Print_flag methods | ||
| 566 | # | ||
| 567 | # The Print flag is used to prevent blank lines from appearing at the | ||
| 568 | # start of a page, which can happen if a verbatim block or =over list | ||
| 569 | # crosses a page break. | ||
| 570 | # | ||
| 571 | # When a new page is started, the print flag is reset. In this state | ||
| 572 | # any calls to newline() or spacer() will have no effect. Whenever any | ||
| 573 | # text is printed, the print flag will be set, then newlines will | ||
| 574 | # operate nomally. | ||
| 575 | |||
| 576 | #----------------------------------------------------------------------- | ||
| 577 | |||
| 578 | sub print_flag { | ||
| 579 | my $self = shift; | ||
| 580 | return $self->{print_flag} || 0; | ||
| 581 | } | ||
| 582 | |||
| 583 | |||
| 584 | #----------------------------------------------------------------------- | ||
| 585 | |||
| 586 | sub set_print_flag { | ||
| 587 | my $self = shift; | ||
| 588 | $self->{print_flag} = TRUE; | ||
| 589 | } | ||
| 590 | |||
| 591 | |||
| 592 | #----------------------------------------------------------------------- | ||
| 593 | |||
| 594 | sub reset_print_flag { | ||
| 595 | my $self = shift; | ||
| 596 | $self->{print_flag} = FALSE; | ||
| 597 | } | ||
| 598 | |||
| 599 | |||
| 600 | #----------------------------------------------------------------------- | ||
| 601 | #----------------------------------------------------------------------- | ||
| 602 | |||
| 603 | # Space_flag methods | ||
| 604 | # | ||
| 605 | # The space flag is used to prevent the display of whitespace characters | ||
| 606 | # at the end of a paragraph. If these characters are not suppressed, | ||
| 607 | # then occasionally they will wrap onto the next line, causing unsightly | ||
| 608 | # spaces in the finished document. | ||
| 609 | # | ||
| 610 | # Each string presented to the print_text_with_style() method is checked | ||
| 611 | # for trailling whitespace. If so, the space_flag is set. At the next | ||
| 612 | # call to print_text_with_style(), an extra space character is printed | ||
| 613 | # if the space_flag is set. The space_flag is cleared either when the | ||
| 614 | # spacer() method is called (to mark the 'real' end of a text block), or | ||
| 615 | # after the flag has caused a new space to be inserted. | ||
| 616 | |||
| 617 | #----------------------------------------------------------------------- | ||
| 618 | |||
| 619 | sub space_flag { | ||
| 620 | my $self = shift; | ||
| 621 | return $self->{space_flag} || 0; | ||
| 622 | } | ||
| 623 | |||
| 624 | |||
| 625 | #----------------------------------------------------------------------- | ||
| 626 | |||
| 627 | sub set_space_flag { | ||
| 628 | my $self = shift; | ||
| 629 | $self->{space_flag} = TRUE; | ||
| 630 | } | ||
| 631 | |||
| 632 | |||
| 633 | #----------------------------------------------------------------------- | ||
| 634 | |||
| 635 | sub reset_space_flag { | ||
| 636 | my $self = shift; | ||
| 637 | $self->{space_flag} = FALSE; | ||
| 638 | } | ||
| 639 | |||
| 640 | |||
| 641 | #----------------------------------------------------------------------- | ||
| 642 | |||
| 643 | sub flag { | ||
| 644 | my $self = shift; | ||
| 645 | my $flag = shift or return FALSE; | ||
| 646 | return $self->{flags}->{$flag} || FALSE; | ||
| 647 | } | ||
| 648 | |||
| 649 | |||
| 650 | #----------------------------------------------------------------------- | ||
| 651 | |||
| 652 | sub set_flag { | ||
| 653 | my $self = shift; | ||
| 654 | my $flag = shift or return FALSE; | ||
| 655 | $self->{flags}->{$flag} = TRUE; | ||
| 656 | } | ||
| 657 | |||
| 658 | |||
| 659 | #----------------------------------------------------------------------- | ||
| 660 | |||
| 661 | sub clear_flag { | ||
| 662 | my $self = shift; | ||
| 663 | my $flag = shift or return FALSE; | ||
| 664 | $self->{flags}->{$flag} = FALSE; | ||
| 665 | } | ||
| 666 | |||
| 667 | #----------------------------------------------------------------------- | ||
| 668 | #----------------------------------------------------------------------- | ||
| 669 | |||
| 670 | # Text indent methods | ||
| 671 | |||
| 672 | #----------------------------------------------------------------------- | ||
| 673 | |||
| 674 | sub indent { | ||
| 675 | # Sets the current indent (measured in points) | ||
| 676 | my $self = shift; | ||
| 677 | $self->{indent} = shift; | ||
| 678 | $self->{x_position} = $self->{left_margin} + $self->{indent}; | ||
| 679 | } | ||
| 680 | |||
| 681 | |||
| 682 | #----------------------------------------------------------------------- | ||
| 683 | |||
| 684 | sub over { | ||
| 685 | # Returns the current number of nested =over blocks | ||
| 686 | my $self = shift; | ||
| 687 | return $self->{over}; | ||
| 688 | } | ||
| 689 | |||
| 690 | |||
| 691 | #----------------------------------------------------------------------- | ||
| 692 | |||
| 693 | sub em { | ||
| 694 | # Returns the width (in points) of an 'm' character, used by =over X | ||
| 695 | # to decide how much to indent by | ||
| 696 | my $self = shift; | ||
| 697 | return $self->{mspace}; | ||
| 698 | } | ||
| 699 | |||
| 700 | |||
| 701 | #----------------------------------------------------------------------- | ||
| 702 | |||
| 703 | sub push_indent { | ||
| 704 | my $self = shift; | ||
| 705 | my $indent = shift; | ||
| 706 | push @{$self->{indent_list}},$indent; | ||
| 707 | $self->indent($self->{indent} + $indent); | ||
| 708 | $self->{over}++; | ||
| 709 | } | ||
| 710 | |||
| 711 | |||
| 712 | #----------------------------------------------------------------------- | ||
| 713 | |||
| 714 | sub pop_indent { | ||
| 715 | my $self = shift; | ||
| 716 | $self->{over}--; | ||
| 717 | if (@{$self->{indent_list}}) { | ||
| 718 | my $indent = pop @{$self->{indent_list}}; | ||
| 719 | $self->indent($self->{indent} - $indent); | ||
| 720 | return $indent; | ||
| 721 | } else { | ||
| 722 | return 0; | ||
| 723 | } | ||
| 724 | } | ||
| 725 | |||
| 726 | |||
| 727 | #----------------------------------------------------------------------- | ||
| 728 | #----------------------------------------------------------------------- | ||
| 729 | |||
| 730 | # Text format methods | ||
| 731 | # | ||
| 732 | # During parsing, as each Pod::InteriorSequence object is encountered | ||
| 733 | # the formatting code (B, I, etc) is pushed onto a stack. When the | ||
| 734 | # parser gets to the individual text elements, the format() method will | ||
| 735 | # return the complete set of codes which need to be applied to the text. | ||
| 736 | |||
| 737 | #----------------------------------------------------------------------- | ||
| 738 | |||
| 739 | sub push_format { | ||
| 740 | my $self = shift; | ||
| 741 | my $format = shift; | ||
| 742 | push @{$self->{format}},$format; | ||
| 743 | } | ||
| 744 | |||
| 745 | |||
| 746 | #----------------------------------------------------------------------- | ||
| 747 | |||
| 748 | sub pop_format { | ||
| 749 | my $self = shift; | ||
| 750 | return pop @{$self->{format}} if (@{$self->{format}}); | ||
| 751 | } | ||
| 752 | |||
| 753 | |||
| 754 | #----------------------------------------------------------------------- | ||
| 755 | |||
| 756 | sub format { | ||
| 757 | # Returns the current text format as a scalar, e.g. 'BEI' for Bold | ||
| 758 | # Italic with Escapes to be processed. Formatting codes are listed in | ||
| 759 | # alphabetical order with duplicates removed. | ||
| 760 | my $self = shift; | ||
| 761 | my %format; | ||
| 762 | foreach (@{$self->{format}}) { | ||
| 763 | # Treat F<> as a synonym for I<> (renders filenames in italic) | ||
| 764 | tr/F/I/; | ||
| 765 | $format{$_}++; | ||
| 766 | } | ||
| 767 | return join '',sort keys %format; | ||
| 768 | } | ||
| 769 | |||
| 770 | |||
| 771 | #----------------------------------------------------------------------- | ||
| 772 | #----------------------------------------------------------------------- | ||
| 773 | |||
| 774 | sub bullet { | ||
| 775 | # Draws a bullet point (filled circle) at the current text position | ||
| 776 | # | ||
| 777 | # Todo: need to remove the integer values here and replace with | ||
| 778 | # percentages of the current line spacing to handle different fonts | ||
| 779 | |||
| 780 | my $self = shift; | ||
| 781 | my $indent = shift; | ||
| 782 | my $bullet = $self->{page}->gfx; | ||
| 783 | my $x_coord = $self->{left_margin} + $self->{indent} + 4 + $indent - 20; | ||
| 784 | my $y_coord = $self->{y_position} - 9 + ($self->print_flag ? 0 : $self->{line_spacing}); | ||
| 785 | my $radius = 2; | ||
| 786 | |||
| 787 | $bullet->circle($x_coord,$y_coord,$radius); | ||
| 788 | $bullet->fillstroke; | ||
| 789 | } | ||
| 790 | |||
| 791 | |||
| 792 | #----------------------------------------------------------------------- | ||
| 793 | |||
| 794 | sub newline { | ||
| 795 | my $self = shift; | ||
| 796 | |||
| 797 | if ($self->print_flag) { | ||
| 798 | $self->linefeed; | ||
| 799 | $self->set_flag('newline'); | ||
| 800 | } | ||
| 801 | } | ||
| 802 | |||
| 803 | |||
| 804 | #----------------------------------------------------------------------- | ||
| 805 | |||
| 806 | sub linefeed { | ||
| 807 | my $self = shift; | ||
| 808 | |||
| 809 | $self->{y_position} -= $self->{line_spacing}; | ||
| 810 | $self->{x_position} = $self->{left_margin} + $self->{indent}; | ||
| 811 | |||
| 812 | if ($self->{y_position} < ($self->{bottom_margin} + $self->{footer_height})) { | ||
| 813 | my $style = $self->{style}; | ||
| 814 | $self->formfeed; | ||
| 815 | $self->set_style($style); | ||
| 816 | } | ||
| 817 | } | ||
| 818 | |||
| 819 | |||
| 820 | #----------------------------------------------------------------------- | ||
| 821 | |||
| 822 | sub spacer { | ||
| 823 | my $self = shift; | ||
| 824 | $self->reset_space_flag; | ||
| 825 | |||
| 826 | if ($self->print_flag) { | ||
| 827 | $self->{y_position} -= $self->{spacer}; | ||
| 828 | $self->{x_position} = $self->{left_margin} + $self->{indent}; | ||
| 829 | |||
| 830 | if ($self->{y_position} < ($self->{bottom_margin} + $self->{footer_height})) { | ||
| 831 | $self->formfeed; | ||
| 832 | } | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | |||
| 837 | #----------------------------------------------------------------------- | ||
| 838 | |||
| 839 | sub formfeed { | ||
| 840 | my $self = shift; | ||
| 841 | $self->{page} = $self->{pdf}->page; | ||
| 842 | $self->{x_position} = $self->{left_margin} + $self->{indent}; | ||
| 843 | $self->{page_number}++; | ||
| 844 | |||
| 845 | delete $self->{text}; | ||
| 846 | delete $self->{gfx}; | ||
| 847 | $self->{gfx} = $self->{page}->gfx; | ||
| 848 | $self->{text} = $self->{page}->text; | ||
| 849 | |||
| 850 | $self->{y_position} = $self->{page_height} - $self->{top_margin} - $self->{line_spacing}; | ||
| 851 | $self->{header_height} = ($self->{header}) ? $self->generate_header : 0; | ||
| 852 | $self->{footer_height} = ($self->{footer}) ? $self->generate_footer : 0; | ||
| 853 | $self->{y_position} -= $self->{header_height}; | ||
| 854 | |||
| 855 | $self->reset_print_flag; | ||
| 856 | } | ||
| 857 | |||
| 858 | |||
| 859 | #----------------------------------------------------------------------- | ||
| 860 | #----------------------------------------------------------------------- | ||
| 861 | |||
| 862 | # Page header and footer methods | ||
| 863 | # | ||
| 864 | # Future enhancement: pass the page number, filename, etc details as | ||
| 865 | # parameters to generate_header() and generate_footer(), allow these | ||
| 866 | # methods to be overridden by the user for custom page formatting. | ||
| 867 | |||
| 868 | #----------------------------------------------------------------------- | ||
| 869 | |||
| 870 | sub generate_header { | ||
| 871 | my $self = shift; | ||
| 872 | $self->set_style('header'); | ||
| 873 | |||
| 874 | my $header_padding = 2; | ||
| 875 | my $header_spacing = 3; | ||
| 876 | my $header_height = $self->{text_size} + $header_spacing + $header_padding; | ||
| 877 | |||
| 878 | # Draw header icon | ||
| 879 | if ($self->{icon_img}) { | ||
| 880 | my $icon_height_in_points = $self->{icon_height} * $self->{icon_scale}; | ||
| 881 | if ($icon_height_in_points > $self->{text_size}) { | ||
| 882 | $header_height += ($icon_height_in_points - $self->{text_size}); | ||
| 883 | } | ||
| 884 | my $ypos = $self->{page_height} - $self->{top_margin} - $icon_height_in_points; | ||
| 885 | $self->{gfx}->image($self->{icon_img},$self->{left_margin},$ypos,$self->{icon_scale}); | ||
| 886 | } | ||
| 887 | |||
| 888 | # Add page title | ||
| 889 | my $x = $self->{page_width} - $self->{right_margin} - $self->{text}->advancewidth($self->{title}); | ||
| 890 | my $y = $self->{page_height} - $self->{top_margin} - $header_height + $header_spacing + $header_padding; | ||
| 891 | $self->{text}->textlabel($x,$y,$self->{fontcache}->{$self->{font}},$self->{text_size},$self->{title}); | ||
| 892 | |||
| 893 | # Draw horizontal line | ||
| 894 | $self->{gfx}->move($self->{left_margin},$self->{page_height}-$self->{top_margin}-$header_height + $header_padding); | ||
| 895 | $self->{gfx}->line($self->{page_width}-$self->{right_margin},$self->{page_height}-$self->{top_margin}-$header_height + $header_padding); | ||
| 896 | $self->{gfx}->stroke; | ||
| 897 | |||
| 898 | return $header_height; | ||
| 899 | } | ||
| 900 | |||
| 901 | |||
| 902 | #----------------------------------------------------------------------- | ||
| 903 | |||
| 904 | sub generate_footer { | ||
| 905 | my $self = shift; | ||
| 906 | $self->set_style('footer'); | ||
| 907 | |||
| 908 | # Add page footer | ||
| 909 | my $t = 'Page '.$self->{page_number}; | ||
| 910 | my $x = $self->{page_width} - $self->{right_margin} - $self->{text}->advancewidth($t); | ||
| 911 | my $y = $self->{bottom_margin}; | ||
| 912 | $self->{text}->textlabel($x,$y,$self->{fontcache}->{$self->{font}},$self->{text_size},$t); | ||
| 913 | |||
| 914 | if ($self->{footer_text}) { | ||
| 915 | $x = $self->{left_margin}; | ||
| 916 | $self->{text}->textlabel($x,$y,$self->{fontcache}->{$self->{font}},$self->{text_size},$self->{footer_text}); | ||
| 917 | } | ||
| 918 | |||
| 919 | $self->{gfx} = $self->{page}->gfx unless (exists $self->{gfx}); | ||
| 920 | $self->{gfx}->move($self->{left_margin},$self->{bottom_margin}+10); | ||
| 921 | $self->{gfx}->line($self->{page_width}-$self->{right_margin},$self->{bottom_margin}+10); | ||
| 922 | $self->{gfx}->stroke; | ||
| 923 | return 18; # Footer height in points | ||
| 924 | } | ||
| 925 | |||
| 926 | |||
| 927 | #----------------------------------------------------------------------- | ||
| 928 | #----------------------------------------------------------------------- | ||
| 929 | |||
| 930 | # PDF file output | ||
| 931 | # | ||
| 932 | # When the PDF object goes out of scope, the generated PDF file will be | ||
| 933 | # printed to STDOUT. | ||
| 934 | # | ||
| 935 | # Update - this doesn't work with PAR, need explicit $pdf->output() method | ||
| 936 | |||
| 937 | #----------------------------------------------------------------------- | ||
| 938 | |||
| 939 | sub output { | ||
| 940 | my $self = shift; | ||
| 941 | print $self->{pdf}->stringify; | ||
| 942 | #$self->{pdf}->end; | ||
| 943 | } | ||
| 944 | |||
| 945 | |||
| 946 | #----------------------------------------------------------------------- | ||
| 947 | #----------------------------------------------------------------------- | ||
| 948 | |||
| 949 | # Text printing methods | ||
| 950 | |||
| 951 | #----------------------------------------------------------------------- | ||
| 952 | |||
| 953 | sub print { | ||
| 954 | my $self = shift; | ||
| 955 | my $text = shift; | ||
| 956 | |||
| 957 | $self->newline; | ||
| 958 | $self->print_text_with_style($text); | ||
| 959 | } | ||
| 960 | |||
| 961 | |||
| 962 | #----------------------------------------------------------------------- | ||
| 963 | |||
| 964 | sub print_text_with_style { | ||
| 965 | my $self = shift; | ||
| 966 | my $text = shift; | ||
| 967 | my $style = shift; | ||
| 968 | |||
| 969 | #warn "print_text_with_style called with style '$style', text '$text'\n"; | ||
| 970 | |||
| 971 | |||
| 972 | $self->set_style($style); | ||
| 973 | |||
| 974 | # Remove double spaces unless we are printing verbatim text | ||
| 975 | unless ($self->{stylist}->{$self->{style}}->{verbatim}) { | ||
| 976 | $text =~ s/(\s)\s+/$1/g; | ||
| 977 | } | ||
| 978 | |||
| 979 | if ($self->space_flag) { | ||
| 980 | # | ||
| 981 | # Note that this space appears in the default style, | ||
| 982 | # but it should be printed in the previous style. | ||
| 983 | # | ||
| 984 | $self->reset_space_flag; | ||
| 985 | $self->set_style('default'); | ||
| 986 | $self->print_word(' '); | ||
| 987 | $self->set_style($style); | ||
| 988 | } | ||
| 989 | if ($text =~ s/\s+$//) { | ||
| 990 | $self->set_space_flag; | ||
| 991 | } | ||
| 992 | |||
| 993 | while ($text =~ /(\s+|\S+)/g) { | ||
| 994 | my $word = $1; | ||
| 995 | $self->print_word($word); | ||
| 996 | } | ||
| 997 | } | ||
| 998 | |||
| 999 | |||
| 1000 | #----------------------------------------------------------------------- | ||
| 1001 | |||
| 1002 | sub print_word { | ||
| 1003 | my $self = shift; | ||
| 1004 | my $word = shift; | ||
| 1005 | |||
| 1006 | # If we are at the start of a line (newline flag is set) and we are | ||
| 1007 | # NOT printing verbatim text, then suppress any whitespace. | ||
| 1008 | if ($self->flag('newline')) { | ||
| 1009 | #warn "newline flag set\n"; | ||
| 1010 | #warn "x position = $self->{x_position}\n"; | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | |||
| 1014 | $self->set_print_flag; | ||
| 1015 | $self->clear_flag('newline'); | ||
| 1016 | |||
| 1017 | my $width = $self->{text}->advancewidth($word); | ||
| 1018 | if ($self->{x_position} + $width > $self->{page_width} - $self->{right_margin}) { | ||
| 1019 | |||
| 1020 | # If the word will not fit on one line, split it up and recurse the 'print_word' sub | ||
| 1021 | if ($width > ($self->{page_width} - $self->{left_margin} - $self->{right_margin} - $self->{indent})) { | ||
| 1022 | my $fit = int(($self->{page_width} - $self->{left_margin} - $self->{right_margin} - $self->{indent}) / $self->{nspace}); | ||
| 1023 | my @words = (substr($word,0,$fit),substr($word,$fit)); | ||
| 1024 | #warn "Recursing... Word=$word Fit=$fit Xpos=$$self{x_position}\n"; | ||
| 1025 | $self->print_word($_) foreach @words; | ||
| 1026 | return; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | $self->newline; | ||
| 1030 | if ($word =~ /^\s+$/) { | ||
| 1031 | unless ($self->{stylist}->{$self->{style}}->{verbatim}) { | ||
| 1032 | return; | ||
| 1033 | } | ||
| 1034 | } | ||
| 1035 | } | ||
| 1036 | $self->{x_position} += $self->{text}->textlabel($self->{x_position}, | ||
| 1037 | $self->{y_position}, | ||
| 1038 | $self->{fontcache}->{$self->{font}}, | ||
| 1039 | $self->{text_size}, | ||
| 1040 | $word, | ||
| 1041 | -color => $self->{text_color}); | ||
| 1042 | if ($self->{x_position} > $self->{page_width} - $self->{right_margin}) { | ||
| 1043 | $self->newline; | ||
| 1044 | } | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | |||
| 1048 | #----------------------------------------------------------------------- | ||
| 1049 | #----------------------------------------------------------------------- | ||
| 1050 | |||
| 1051 | # Text style methods | ||
| 1052 | |||
| 1053 | #----------------------------------------------------------------------- | ||
| 1054 | |||
| 1055 | sub set_style { | ||
| 1056 | my $self = shift; | ||
| 1057 | my $style = shift || 'default'; | ||
| 1058 | |||
| 1059 | $style = (exists $self->{stylist}->{$style}) ? $style : 'default'; | ||
| 1060 | #carp "Setting style to $style"; | ||
| 1061 | |||
| 1062 | # Create font object if necessary | ||
| 1063 | my $font = ($self->{stylist}->{$style}->{font} || $self->{font_face}) . | ||
| 1064 | ((exists $self->{stylist}->{$style}->{type}) ? '-'.$self->{stylist}->{$style}->{type} : ''); | ||
| 1065 | unless (exists $self->{fontcache}->{$font}) { | ||
| 1066 | $self->{fontcache}->{$font} = $self->{pdf}->corefont($font); | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | $self->{style} = $style; | ||
| 1070 | $self->{font} = $font; | ||
| 1071 | $self->{text_color} = $self->{stylist}->{$style}->{color} || '#000000'; | ||
| 1072 | $self->{text_size} = $self->{stylist}->{$style}->{size} || $self->{font_size}; | ||
| 1073 | |||
| 1074 | $self->{text}->font($self->{fontcache}->{$font},$self->{text_size}); | ||
| 1075 | $self->{nspace} = $self->{text}->advancewidth('n'); | ||
| 1076 | $self->{mspace} = $self->{text}->advancewidth('m'); | ||
| 1077 | } | ||
| 1078 | |||
| 1079 | |||
| 1080 | #----------------------------------------------------------------------- | ||
| 1081 | #----------------------------------------------------------------------- | ||
| 1082 | |||
| 1083 | 1; |