blib/lib/UML/Sequence/Raster.pm untested

filter: Statementstmt 0.0% 0 / 471 Branchbran 0.0% 0 / 174 Conditioncond 0.0% 0 / 97 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 837 CC 87 SCAR 59.8
CC
87
Cov
0%
seq2raster
1056.0
draw_arrows
342.0
drawLabel
182.0
CRAP 395.9
1 #2345678901234567890123456789012345678901234567890123456789012345678901234567890
2 =head1 NAME
3  
4 UML::Sequence::Raster - converts xml sequence files to a raster image
5  
6 =head1 SYNOPSIS
7  
8 use UML::Sequence::Raster;
9  
10 seq2raster @ARGV;
11  
12 =head1 DESCRIPTION
13  
14 This module supports the seq2rast.pl script like Pod::Html supports pod2html.
15 The array passed to seq2rast.pl should have the following form:
16  
17 ([ I<options-pairs>, ] [input_file_name])
18  
19 where I<options-pairs> are any of
20  
21 =over 4
22  
23 =item C<-a color>
24  
25 specifies a color to be used to fill the activation boxes. Must be a string
26 of either hexadecimal RGB values, I<e.g.,> B<'#FF00AC0024B1'>, or a name from
27 the following list of supported colors:
28  
29 white lyellow lpurple lbrown
30 lgray yellow purple dbrown
31 gray dyellow dpurple transparent
32 dgray lgreen lorange
33 black green orange
34 lblue dgreen pink
35 blue lred dpink
36 dblue red marine
37 gold dred cyan
38  
39 =item C<-c color>
40  
41 specifies a color to be used to fill the class boxes. See the C<-a>
42 option for acceptable color specifications.
43  
44 =item C<-e>
45  
46 specifies that embedded annotations are to be applied
47 to the raster image. Only valid when the C<-m> or C<-M>
48 option is also specified. When an arrow label has associated annotations,
49 the labels with be suffixed with a superscript number linking to
50 a text section containing the annotation text.
51  
52 =item C<-f "font">, C<-F fontfile>
53  
54 Specifies a font to be used to render text. C<-f> uses a "named"
55 font, e.g., "Times New Roman", which can be resolved to a TrueType
56 font file. The special fontname B<gd> may be used to specify the
57 GD builtin fonts (which are also the default on non-Win32 platforms).
58 Values other than B<gd> are currently only supported on Win32 platforms
59 with L<Win32::Font::NameToFile> installed.
60  
61 C<-F> specifies the actual fontfile name, e.g., 'ARIALBD.TTF'.
62  
63 Note that, on Win32 platforms with L<Win32::Font::NameToFile> installed,
64 using a named font will cause this module to attempt to locate
65 bold (for class name labels) and italic (for event labels) versions
66 of the font file. The base font will be used if either
67 is not found.
68  
69 =item C<-g arrow-gap>
70  
71 If C<arrow-gap> is an integer value, specifies number of pixels between arrows
72 (default is 40 pixels).
73 If C<arrow-gap> is a fractional value, specifies a scaling factor for the
74 default number of pixels between arrows.
75  
76 =item C<-j>
77  
78 specifies that Javascript'ed tooltip annotations are to be applied
79 to the raster image. Only valid when the C<-m> or C<-M>
80 option is also specified. When an arrow label has associated annotations,
81 the hyperlinks in the areamap for the label will include
82 C<onmouseover()> function calls containing the annotation text for use with the
83 Javascript tooltip package available at
84 L<http://www.walterzorn.com/tooltip/tooltip_e.htm>. Note that annotated
85 labels will be underlined in the image.
86  
87 =item C<-m areamap-path>, C<-M areamap-path>
88  
89 specifies the name of a file to receive HTML containing
90 an image element, areamap, and (optionally)
91 either an ordered list of annotations (if L<-e> was specified)
92 or a script tag linking to the
93 Javascript tooltip script (if L<-j> was specified) to be applied to
94 the raster image. Only valid when either the L<-p> or L<-P> option is specified.
95 C<-M> specifies append mode for the output file.
96  
97 =item C<-o output_file_name>
98  
99 specifies the output file name. The format of the image is determined
100 by the file qualifier as follows:
101  
102 .png, .PNG => PNG format
103 .gif, .GIF => GIF format
104 .jpg, .jpeg, .JPG, .JPEG => JPEG format
105  
106 If not specified, output is sent to STDOUT in PNG format.
107  
108 =item C<-p classdocs-path>, C<-P classdocs-path>
109  
110 specifies a base path to classdocs generated by psichedoc.
111 Adds an areamap to the HTML output file specified by the C<-m> or C<-M>
112 option with hyperlinks to the documents for individual classes and/or methods,
113 excluding method labels w/ embedded whitespace. Additionally, the specified
114 path is used with any generated HTML imagemap. Both C<-p> and C<-P> behave
115 the same; both are supported for compatibility with L<UML::Sequence::Svg>.
116  
117 =item C<-s "Signature">
118  
119 specifies a signature to apply to the lower right corner in small text,
120 e.g., "Copyright(C) 2006, GOWI Corp.".
121  
122 =item C<-w box-width>
123  
124 specifies width of class box in pixels; default is 125. Used to compute
125 class header boxes and areamap coordinates.
126  
127 =item C<-x char-width>
128  
129 specifies width of characters in pixels; default is 6. Used to compute
130 class header boxes and areamap coordinates.
131  
132 =item C<-y char-height>
133  
134 specifies height of characters in pixels; default is 14. Used to compute
135 class header boxes and areamap coordinates.
136  
137 =back
138  
139 By default input is from standard in and output is to standard out.
140  
141 =cut
142  
143 package UML::Sequence::Raster;
144  
145 use Exporter;
146 @ISA = qw(Exporter);
147 @EXPORT = qw(seq2raster);
148  
149 use XML::DOM;
150 use Getopt::Std;
151 use GD;
152 use GD qw(gdSmallFont gdLargeFont gdMediumBoldFont gdTinyFont gdGiantFont);
153 use GD::Text::Wrap;
154  
155 if ($^O eq 'MSWin32') {
156 require Win32::Font::NameToFile;
157 import Win32::Font::NameToFile qw(get_ttf_abs_path get_ttf_bold get_ttf_italic);
158 }
159  
160 use strict;
161 use warnings;
162  
163 our $VERSION = '0.01';
164  
165 # Constant declarations.
166 my $CLASS_TEXT_Y = 40;
167 my $CLASS_BOX_Y = 25;
168 my $CLASS_BOX_HEIGHT = 20;
169 my $CLASS_BOX_WIDTH = 125;
170 my $CLASS_SPACING = 3;
171  
172 my $LEFT_EDGE = 92;
173  
174 my $ACTIVATION_WIDTH = 15;
175 my $ACTIVATION_OFFSET = 10;
176  
177 my $FIRST_ARROW = 55;
178 my $ARROW_SPACING = 60;
179 my $ARROW_DELTA = 0;
180  
181 use constant LINE_THICKNESS => 2;
182  
183 use constant LEFT_ARROW => 0;
184 use constant RIGHT_ARROW => 1;
185 use constant HALF_LEFT_ARROW => 2;
186 use constant HALF_RIGHT_ARROW => 3;
187 my @arrowpts = (
188 [0, 0, 12, 6, 0, 14],
189 [0, 6, 12, 0, 12, 14],
190 [0, 4, 12, 4, 12, 14],
191 [0, 14, 12, 4, 0, 4],
192 );
193 my @arrows = ();
194  
195 # Global variable:
196 my $output_file = "-";
197 my $classcolor = 'white';
198 my $actcolor = 'white';
199 my $docpath; # path to assoc. classdocs
200 my $mappath; # path to write areamap file
201 my $mapname; # imagemap name derived from raster output filename
202 my $classcharwidth = 9; # width of class header characters
203 my $charwidth = 8; # width of characters
204 my $charht = 10; # height of characters
205 my $annot; # annotations behavior:
206 # 'e' : embed in specified file;
207 # 'j' : apply javascript tooltips in specified file
208 # NOTE: specified file may be same as image map file
209 my $white;
210 my $black;
211  
212 my %colors = (
213 white => [255,255,255],
214 lgray => [191,191,191],
215 gray => [127,127,127],
216 dgray => [63,63,63],
217 black => [0,0,0],
218 lblue => [0,0,255],
219 blue => [0,0,191],
220 dblue => [0,0,127],
221 gold => [255,215,0],
222 lyellow => [255,255,125],
223 yellow => [255,255,0],
224 dyellow => [127,127,0],
225 lgreen => [0,255,0],
226 green => [0,191,0],
227 dgreen => [0,127,0],
228 lred => [255,0,0],
229 red => [191,0,0],
230 dred => [127,0,0],
231 lpurple => [255,0,255],
232 purple => [191,0,191],
233 dpurple => [127,0,127],
234 lorange => [255,183,0],
235 orange => [255,127,0],
236 pink => [255,183,193],
237 dpink => [255,105,180],
238 marine => [127,127,255],
239 cyan => [0,255,255],
240 lbrown => [210,180,140],
241 dbrown => [165,42,42],
242 transparent => [1,1,1]
243 );
244 #
245 # font names
246 #
247 my $fontfile;
248 my $fontname;
249 my $titlefont;
250 my $classfont;
251 my $methodfont;
252 my $eventfont;
253 my $tinyfont;
254  
255 my $sig;
256 #
257 # caches of text elements, to optimize
258 # rendering
259 #
260 my @class_hdrs = ();
261 my @classtext = ();
262 my @labeltext = ();
263 my @italtext = ();
264 my @supertext = ();
265  
266 sub seq2raster {
267 local (@ARGV) = @_;
268 my $opts = parse_command_line();
269  
270 $classcolor = $opts->{c} || 'white';
271 $actcolor = $opts->{a} || 'white';
272 $docpath = $opts->{p} || $opts->{P};
273 $mappath = $opts->{m} || $opts->{M};
274 $charwidth = $opts->{x} || 6;
275 $charht = $opts->{y} || 14;
276 $CLASS_BOX_WIDTH = $opts->{w} || 125;
277 $annot = $opts->{e} ? 'e' : $opts->{j} ? 'j' : undef;
278 $fontfile = $opts->{F};
279 $fontname = $opts->{f};
280 $sig = $opts->{s};
281  
282 die "Annotation requested without output path."
283 if ($annot && (! $mappath));
284  
285 $ARROW_SPACING = (index($opts->{g}, '.') >= 0) ?
286 int($ARROW_SPACING * $opts->{g}) : $opts->{g}
287 if $opts->{g};
288  
289 $docpath .= '/'
290 if ($docpath && (substr($docpath, -1, 1) ne '/'));
291  
292 my $input_file = shift @ARGV;
293  
294 if (defined $input_file) {
295 open INPUT, "$input_file"
296 or die "Couldn't open $input_file for input: $!\n";
297 }
298 else {
299 *INPUT = *STDIN;
300 }
301 #
302 # DAA Add HTML image map rendering
303 #
304 my $mapfd;
305 if (defined($mappath)) {
306 $mappath = $opts->{M} ? ">>$mappath" : ">$mappath";
307 die "Cannot open image map file $mappath: $!"
308 unless open($mapfd, $mappath);
309 #
310 # define a mapname from output file name (if any)
311 #
312 $mapname = ($output_file=~/(\w+)\.\w+$/) ? $1 : 'mapname';
313 print $mapfd
314 "<html>
315 <body>
316 <img border=0 src='$mapname\.png' usemap='#$mapname'>
317 <MAP NAME='$mapname'>
318 ";
319 }
320  
321 my $parser = XML::DOM::Parser->new();
322 my $doc = $parser->parse(*INPUT);
323  
324 my $sequence = $doc->getDocumentElement();
325 my $title = $sequence->getAttribute("title");
326  
327 my $classes = $doc->getElementsByTagName("class");
328 #
329 # DAA added to track previous class for drawing async arrows
330 #
331 my %priors = ();
332 my $class_hash = build_class_name_hash($classes);
333 my $class_count = scalar (keys %$class_hash);
334 #
335 # if external events, but not first, then trim
336 #
337 $class_count--
338 if exists($class_hash->{_EXTERNAL}) && $class_hash->{_EXTERNAL};
339 #
340 # compute class header text now, so we get get true box sizes
341 #
342 my $maxClassHt = 1;
343 while (my ($class, $idx) = each %$class_hash) {
344 if ($class eq '_EXTERNAL') {
345 $ARROW_DELTA = 1 if ($idx == 0);
346 next;
347 }
348 $class_hdrs[$idx] = _wrapText($class);
349 my $lines = ($class_hdrs[$idx]=~tr/\n//) + 1;
350 $maxClassHt = $lines if ($lines > $maxClassHt);
351 }
352 $CLASS_BOX_HEIGHT *= $maxClassHt;
353 $FIRST_ARROW = $CLASS_TEXT_Y + $CLASS_BOX_HEIGHT;
354  
355 my $arrow_count = count_arrows($doc);
356 my $width = ($class_count + 0.5) *
357 ($CLASS_BOX_WIDTH + $CLASS_SPACING) + 40;
358 my $height = $FIRST_ARROW + ($arrow_count + 1) * ($ARROW_SPACING);
359  
360 my $format = ($output_file eq '-') ? 'png' :
361 ($output_file=~/\.(gif|jpg|jpeg|png)$/i) ? lc $1 : 'png';
362  
363 my $img = GD::Image->new($width, $height);
364 $white = $img->colorAllocate(@{$colors{white}});
365 $black = $img->colorAllocate(@{$colors{black}});
366 $classcolor = ($classcolor eq 'black') ? $black :
367 ($classcolor eq 'white') ? $white :
368 (substr($classcolor,0,1) eq '#') ?
369 $img->colorAllocate(_dehex($classcolor)) :
370 $img->colorAllocate(@{$colors{$classcolor}});
371  
372 $actcolor = ($actcolor eq 'black') ? $black :
373 ($actcolor eq 'white') ? $white :
374 (substr($actcolor,0,1) eq '#') ?
375 $img->colorAllocate(_dehex($actcolor)) :
376 $img->colorAllocate(@{$colors{$actcolor}});
377 #
378 # create arrowhead markers
379 #
380 my ($arrow, $trans, $arrowblack, $poly);
381  
382 foreach (@arrowpts) {
383 $arrow = GD::Image->new(12,14);
384 $trans = $arrow->colorAllocate(128, 128, 128);
385 $arrowblack = $arrow->colorAllocate(0,0,0);
386 $arrow->transparent($trans);
387 $arrow->filledRectangle(0,0,12,14, $trans);
388 $poly = GD::Polygon->new();
389 $poly->addPt(shift @$_, shift @$_)
390 while scalar @$_;
391 $arrow->filledPolygon($poly, $arrowblack);
392 push @arrows, $arrow;
393 }
394 #
395 # load fonts
396 # set default to GD fonts
397 #
398 $titlefont = gdGiantFont;
399 $classfont = gdGiantFont;
400 $methodfont = gdSmallFont;
401 $eventfont = gdSmallFont;
402 $tinyfont = gdTinyFont;
403 my ($boldfont, $italfont);
404 if (($^O eq 'MSWin32') &&
405 ((!$fontname) || (lc $fontname ne 'gd'))) {
406 if ($fontfile) {
407 $boldfont = $fontfile;
408 $italfont = $fontfile;
409 }
410 else {
411 $fontname = 'Arial' unless $fontname;
412  
413 $fontfile = get_ttf_abs_path($fontname);
414 $fontname = 'Arial',
415 $fontfile = get_ttf_abs_path($fontname)
416 unless $fontfile;
417  
418 $boldfont = get_ttf_bold($fontname) || $fontfile;
419 $italfont = get_ttf_italic($fontname) || $fontfile;
420 }
421 $titlefont = $classfont = $boldfont;
422 $methodfont = $eventfont = $tinyfont = $fontfile;
423 }
424 elsif (($^O ne 'MSWin32') && $fontfile) {
425 # don't really know what to do about non-Win32
426 # $boldfont = $fontfile;
427 # $italfont = $fontfile;
428 $titlefont = $classfont = $boldfont;
429 $methodfont = $eventfont = $tinyfont = $fontfile;
430 }
431  
432 draw_classes($img, $classes, $mapfd, \%priors);
433 draw_arrows($img, $doc, $class_hash, $mapfd, \%priors);
434 render_text($img, $title);
435 #
436 # DAA terminate areamap
437 #
438 if (defined($mapfd)) {
439 print $mapfd '
440 <script language="JavaScript" type="text/javascript" src="../wz_tooltip.js"></script>
441  
442 </body>
443 </html>
444 '
445 if $opts->{j};
446 close $mapfd;
447 }
448  
449 open GDOUT, ">$output_file";
450 binmode GDOUT;
451 print GDOUT $img->$format();
452 close GDOUT unless $output_file eq '-';
453 }
454  
455 sub _dehex {
456 my $color = substr($_[0], 1);
457  
458 my ($len, $off1, $off2) = (length($color) == 6) ? (2, 2, 4) : (2, 4, 8);
459 return ((length($color) == 6) || (length($color) == 12)) ?
460 (hex(substr($color, 0, $len)),
461 hex(substr($color, $len, $len)),
462 hex(substr($color, $len << 1, $len))) :
463 (0,0,0);
464 }
465  
466 sub draw_classes {
467 my ($img, $classes, $mapfd, $priors) = @_;
468  
469 my $x = $LEFT_EDGE;
470 my $box_left = $x - 8;
471 my $max_extent;
472 my $boxht = $CLASS_BOX_HEIGHT;
473 my $boxtext;
474  
475 my $prior = '_EXTERNAL';
476 for (my $i = 0; $i < $classes->getLength(); $i++) {
477 my $class = $classes->item($i);
478 my $life_x = int($x + $CLASS_BOX_WIDTH / 2);
479 my $class_name = $class ->getAttribute("name");
480  
481 next if ($class_name eq '_EXTERNAL');
482  
483 $priors->{$class_name} = $prior;
484 $prior = $class_name;
485 #
486 # DAA add hyperlink to psichedocs
487 #
488 my $class_path = ($docpath && ($class_name!~/\s/)) ?
489 $docpath . join('/', split(/::/, $class_name)) . '.html' : undef;
490  
491 my $born = $class ->getAttribute("born")
492 * $ARROW_SPACING + $FIRST_ARROW;
493 my $extends_to = ($class ->getAttribute("extends-to") + 1)
494 * $ARROW_SPACING + $FIRST_ARROW;
495  
496 if (not defined $max_extent) { $max_extent = $extends_to; }
497 #
498 # DAA rearranged to place text on top of rectangle
499 # for fill purposes
500 #
501 $img->setThickness(LINE_THICKNESS);
502 $img->filledRectangle($box_left, $CLASS_BOX_Y,
503 $box_left + $CLASS_BOX_WIDTH, $CLASS_BOX_Y + $boxht, $classcolor);
504  
505 $img->rectangle($box_left, $CLASS_BOX_Y,
506 $box_left + $CLASS_BOX_WIDTH, $CLASS_BOX_Y + $boxht, $black);
507 #
508 # draw class name
509 #
510 push @classtext, [$class_hdrs[$i], $x, 26, $CLASS_BOX_WIDTH];
511 #
512 # DAA support areamaps
513 #
514 print $mapfd "<AREA TITLE='$class_name'",
515 ($class_path ? " HREF='$class_path'" : ''),
516 " SHAPE=RECT COORDS='$box_left,$CLASS_BOX_Y,",
517 $box_left + $CLASS_BOX_WIDTH, ',', $CLASS_BOX_Y + $boxht, "'>\n"
518 if $mapfd;
519  
520 $img->dashedLine($life_x, $born, $life_x, $max_extent, $black);
521  
522 my $activation_x = int($box_left + $CLASS_BOX_WIDTH / 2);
523 my @activations = $class->getElementsByTagName("activation");
524 foreach my $activation (@activations) {
525 $born = $activation->getAttribute("born");
526 $extends_to = $activation->getAttribute("extends-to");
527 my $offset = $activation->getAttribute("offset");
528 my $top = $FIRST_ARROW + $born * $ARROW_SPACING;
529 my $height = ($extends_to - $born + .5) * $ARROW_SPACING;
530 my $left = $activation_x + $offset * $ACTIVATION_OFFSET;
531  
532 $img->filledRectangle($left, $top,
533 $left + $ACTIVATION_WIDTH, $top + $height, $actcolor);
534 $img->rectangle($left, $top,
535 $left + $ACTIVATION_WIDTH, $top + $height, $black);
536 }
537 # }
538 $x += $CLASS_BOX_WIDTH + $CLASS_SPACING;
539 $box_left += $CLASS_BOX_WIDTH + $CLASS_SPACING;
540 }
541 return $img;
542 }
543  
544 sub count_arrows {
545 my $doc = shift;
546 my $arrows = $doc->getElementsByTagName("arrow");
547 return $arrows->getLength();
548 }
549  
550 sub draw_arrows {
551 my ($img, $doc, $class_hash, $mapfd, $priors) = @_;
552  
553 my $arrows = $doc->getElementsByTagName("arrow");
554 my $annotnum = 1;
555 my $annotspan = "\n<ol>\n" if ($annot && ($annot eq 'e'));
556  
557 for (my $i = 0; $i < $arrows->getLength(); $i++) {
558 my $arrow = $arrows->item($i);
559 my $from = $arrow->getAttribute("from" );
560 my $to = $arrow->getAttribute("to" );
561 #
562 # DAA 12/24/2005
563 # use type attribute to specify returnvalue or external
564 # which changes the line style to dashed or folded, respectively
565 # also note that $from for external events originate at the far left
566 #
567 my $type = $arrow->getAttribute("type" );
568 my $label = $arrow->getAttribute("label" );
569 my $from_offset = $arrow->getAttribute("from-offset");
570 my $to_offset = $arrow->getAttribute("to-offset" );
571 my $annots = $arrow->getElementsByTagName('annotation');
572 $annots = $annots->item(0) if $annots;
573 my $y = $FIRST_ARROW + ($i + 1) * $ARROW_SPACING;
574  
575 my $from_number = ($from eq '_EXTERNAL') ?
576 $class_hash->{$priors->{$to}} : $class_hash->{$from};
577 my $to_number = $class_hash->{$to};
578  
579 $from_number--, $to_number-- if $ARROW_DELTA;
580  
581 $label =~ s/</&lt;/g;
582 $label =~ s/>/&gt;/g;
583 my $class_path;
584 #
585 # DAA add hyperlink to psichedocs
586 #
587 if ($docpath && ($to!~/\s/)) {
588 my $doclabel = $label;
589 $doclabel=~s/[\*!]//g;
590 $doclabel=~s/^\s*\[[^\]]*\]\s*//;
591 $doclabel=~s/^\s+//;
592 $doclabel=~s/\s+$//;
593 $class_path = $docpath . join('/', split(/::/, $to)) .
594 ".html#$doclabel"
595 unless ($doclabel=~/\s/);
596 }
597  
598 if ($from_number < $to_number) { # arrow from left to right
599 my $x1 = $from_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE
600 + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2
601 + $from_offset * $ACTIVATION_OFFSET;
602 $x1 += 20 if ($from eq '_EXTERNAL');
603 my $x2 = $to_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE
604 + ($CLASS_BOX_WIDTH - $ACTIVATION_WIDTH)/2;
605 #
606 # DAA cuddle the label to the arrowhead
607 #
608 my $xlab = $x2 - $CLASS_SPACING - 6;
609 my $ylab = $y - $charht;
610 #
611 # DAA changed to support call vs. return vs. async activations
612 #
613 $img->setThickness(LINE_THICKNESS);
614 ($type eq 'return') ?
615 $img->dashedLine($x1, $y, $x2, $y, $black) :
616 $img->line($x1, $y, $x2, $y, $black);
617 my $arrowimg = $arrows[($type eq 'async') ?
618 HALF_RIGHT_ARROW : LEFT_ARROW];
619 $img->copy($arrowimg, $x2-14, $y-6, 0,0,
620 $arrowimg->width, $arrowimg->height);
621 #
622 # DAA need to add hrefs for methods, but we'll need to track the "to" class
623 # name, and ignore names w/ whitespace
624 #
625 drawLabel->($img, $mapfd, $xlab, $ylab, 1,
626 $type, $label, $class_path, $mapname, $i,
627 \$annotnum, $annots, \$annotspan)
628 if defined($label);
629 }
630 elsif ($from_number > $to_number) { # arrow from right to left
631 my $x1 = $from_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE
632 + ($CLASS_BOX_WIDTH - $ACTIVATION_WIDTH)/2;
633 my $x2 = $to_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE
634 + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2
635 + $to_offset * $ACTIVATION_OFFSET;
636 #
637 # DAA changed to support call vs. return vs. async activations
638 # (note async activations always go from left to right, so we won't get
639 # any here...)
640 #
641 $img->setThickness(LINE_THICKNESS);
642 ($type eq 'return') ?
643 $img->dashedLine($x1, $y, $x2, $y, $black) :
644 $img->line($x1, $y, $x2, $y, $black);
645 my $arrowimg = $arrows[RIGHT_ARROW];
646 $img->copy($arrowimg, $x2, $y-6, 0,0,
647 $arrowimg->width, $arrowimg->height);
648  
649 my $xlab = $x2 + $CLASS_SPACING + 6;
650 my $ylab = $y - $charht;
651 #
652 # DAA need to add hrefs for methods, but we'll need to track the "to" class
653 # name, and ignore names w/ whitespace
654 #
655 drawLabel->($img, $mapfd, $xlab, $ylab, 0,
656 $type, $label, $class_path, $mapname, $i,
657 \$annotnum, $annots, \$annotspan)
658 if defined($label);
659 }
660 else { # arrow from and to same class
661 my $x1 = $from_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE
662 + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2
663 + $from_offset * $ACTIVATION_OFFSET;
664 my $x2 = $to_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE
665 + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2
666 + $to_offset * $ACTIVATION_OFFSET;
667 $y -= 10;
668 my $y2 = $y + 20;
669 my $x1padded = $x1 + $ACTIVATION_OFFSET + 15;
670 $img->setThickness(LINE_THICKNESS);
671 $img->line($x1, $y, $x1padded, $y, $black);
672 $img->line($x1padded, $y, $x1padded, $y2, $black);
673 $img->line($x1padded, $y2, $x2, $y2, $black);
674 my $arrowimg = $arrows[RIGHT_ARROW];
675 $img->copy($arrowimg, $x2, $y2-6, 0,0,
676 $arrowimg->width, $arrowimg->height);
677  
678 my $xlab = $x1padded + $CLASS_SPACING;
679 my $ylab = ($y + $y2) / 2;
680 #
681 # DAA need to add hrefs for methods, but we'll need to track the "to" class
682 # name, and ignore names w/ whitespace
683 #
684 drawLabel->($img, $mapfd, $xlab, $ylab, 0,
685 $type, $label, $class_path, $mapname, $i,
686 \$annotnum, $annots, \$annotspan)
687 if defined($label);
688 }
689 }
690 #
691 # DAA save annotations if any
692 #
693 if ($mapfd) {
694 print $mapfd "</MAP>\n\n";
695 print $mapfd "$annotspan\n</ol>\n"
696 if $annotspan && ($annotspan ne '<ol>');
697 }
698  
699 return $img;
700 }
701  
702 sub build_class_name_hash {
703 my $class_nodes = shift;
704 my %classes; # keyed by class name store left to right position
705 my ($class, $class_name);
706  
707 $class = $class_nodes->item($_),
708 $class_name = $class->getAttribute('name'),
709 $classes{$class_name} = $_
710 for (0..$class_nodes->getLength() - 1);
711  
712 return \%classes;
713 }
714  
715 sub parse_command_line {
716 my %opts;
717 getopts('a:c:ef:F:g:jm:M:o:p:P:s:w:x:y:', \%opts);
718 $output_file = $opts{o} if defined $opts{o};
719 $classcolor = $opts{c} || 'white';
720 $actcolor = $opts{a} || 'white';
721 $docpath = $opts{p} || $opts{P};
722 $annot = $opts{e} || $opts{j};
723 $fontfile = $opts{F};
724 $fontname = $opts{f};
725 $sig = $opts{s};
726 return \%opts;
727 }
728  
729 sub _createLabelMap {
730 my ($x1, $y1, $x2, $y2, $title, $name, $path,
731 $annot_text, $annot_name, $annot_span) = @_;
732 #
733 # NOTE: Batik seems to render the text coords about 10px lower than raster,
734 # so we'll cheat here...may not be needed for GD
735 #
736 $y1 -= 10, $y2 -= 10;
737 #
738 # also, if label has a newline, hyperlink bottom line only
739 #
740 if ($title=~/\n/) {
741 $y1 += 16;
742 $y2 += 16;
743 $x2 = $x1 + ((length($title) - index($title, "\n") - 1) * $charwidth);
744 }
745 $title=~tr/\n/ /;
746 my $maptext =
747 "<AREA NAME='$name' TITLE='$title' SHAPE=RECT COORDS='$x1,$y1,$x2,$y2' " .
748 ($path ? "HREF='$path' " : '');
749  
750 if ($annot_text) {
751 if ($annot && ($annot eq 'j')) {
752 #
753 # Scripted annotation
754 #
755 $annot_text =~s/'/\\'/g; # escape single quotes
756 $title =~s/'/\\'/g; # escape single quotes
757 #
758 # NOTE: this must all be on 1 line!!!
759 #
760 $maptext .="
761 onmouseover=\"this.T_STATIC=true;this.T_FONTCOLOR='black';this.T_FONTSIZE='12px';this.T_BGCOLOR='#e0e0e0';this.T_OPACITY=90;this.T_SHADOWWIDTH=8;return escape('$annot_text')\" >
762 ";
763 }
764 else { # annot eq 'e'
765 #
766 # embedded annotation, add anchor and annotation text
767 # NOTE: the offsets used here are heuristic, need a bbox jscript
768 #
769 my ($x3, $y3, $y4) = ($x2+4, $y1 + (($y2 - $y1)>>1), $y1 - 4);
770  
771 $maptext .= ">
772 <AREA SHAPE=RECT COORDS='$x2,$y4,$x3,$y3' HREF='#$annot_name'>
773 ";
774  
775 $annot_text=~s/</&lt;/g;
776 $annot_text=~s/>/&gt;/g;
777 $$annot_span .= "
778 <a name='$annot_name'></a>
779 <li>$annot_text
780 <p>
781 ";
782 }
783 }
784 else {
785 $maptext .= ">\n";
786 }
787  
788 return $maptext;
789 }
790  
791 sub render_text {
792 my $img = shift;
793 my $title = shift;
794 my ($align, $len, $nl, $oldlen, $ullen, $x, $xul, $y, $yul);
795  
796 my $textimg = GD::Text::Wrap->new($img,
797 preserve_nl => 1,
798 line_space => 0,
799 align => 'left',
800 color => $black
801 );
802 #
803 # render title (if any)
804 #
805 $textimg->set_font($titlefont, 12);
806 $textimg->set(text => $title),
807 $textimg->draw(5, 15)
808 if $title;
809 #
810 # render classnames
811 #
812 $textimg->set_font($classfont, 12);
813 foreach (@classtext) {
814 $textimg->set(
815 text => $_->[0],
816 width => $_->[3]);
817 $textimg->draw($_->[1], $_->[2]);
818 }
819 #
820 # render labels
821 #
822 $img->setThickness(1);
823 $textimg->set_font($methodfont, 10);
824 foreach (@labeltext) {
825 #
826 # we need to set alignment based on the arrow direction
827 #
828 $align = $_->[5] ? 'right' : 'left';
829  
830 $nl = index($_->[0], "\n");
831 $x = $xul = $_->[1];
832 ($y, $yul) = ($_->[2] - 4, $_->[2] + 10);
833 $ullen = $len = $textimg->width($_->[0]);
834  
835 if (($nl > 0) && $_->[4]) {
836 $yul += $charht + 4,
837 my @segs = split(/\n/, $_->[0]);
838 $oldlen = $ullen;
839 $ullen = $textimg->width($segs[1]);
840 $xul += ($oldlen - $ullen)
841 if ($align eq 'right') && ($ullen < $oldlen);
842 }
843  
844 $textimg->set(
845 text => $_->[0],
846 align => $align,
847 width => $len);
848  
849 $textimg->draw($x, $y);
850 $img->line($xul, $yul, $xul + $ullen, $yul, $black)
851 if $_->[4];
852 }
853 #
854 # render italics
855 #
856 $textimg->set_font($eventfont, 10);
857 foreach (@italtext) {
858 $len = $textimg->width($_->[0]);
859 $textimg->set(
860 text => $_->[0],
861 width => $len);
862 $textimg->draw($_->[1], $_->[2] - 4);
863 $img->line($_->[1], $_->[2] + 10, $_->[1] + $len, $_->[2] + 10, $black)
864 if $_->[4];
865 }
866 #
867 # render superscripts
868 #
869 $textimg->set_font($tinyfont, 8);
870 $textimg->set(
871 text => $_->[0],
872 align => 'left',
873 width => 10),
874 $textimg->draw($_->[1], $_->[2])
875 foreach (@supertext);
876 #
877 # render signature (if any)
878 #
879 if ($sig) {
880 $textimg->set_font($tinyfont, 6);
881 $len = $textimg->width($sig);
882 $textimg->set(
883 text => $sig,
884 width => $len,
885 align => 'left');
886 my ($x, $y) = ($img->width - $len - 20, $img->height - 20);
887 $textimg->draw($x, $y)
888 }
889  
890 return 1;
891 }
892  
893 sub drawLabel {
894 my ($img, $mapfd, $xlab, $ylab, $align, $type, $label, $path,
895 $mapname, $i, $annotnum, $annots, $annotspan) = @_;
896  
897 $xlab -= ($charwidth * length($label)) if $align;
898 my $xend = $xlab + ($charwidth * length($label));
899 my $yend = $ylab + $charht;
900 my $xmap = $xlab;
901  
902 if ($type eq 'async') {
903 push @italtext,
904 [ $label, $xlab, $ylab, $xend - $xlab - 6,
905 (($annot && ($annot eq 'j') && $annots) ? 1 : undef), $align ];
906 }
907 else {
908 #
909 # if conditional, add line break
910 #
911 if ($label=~s/^(\[[^\]]+\])\s*(.*)$/$1\n$2/) {
912 #
913 # and adjust offsets
914 #
915 my ($top, $bot) = (length($1), length($2));
916 $top = $bot if ($top < $bot);
917 $xlab = $xend - ($top * $charwidth),
918 $xmap = $xend - ($bot * $charwidth)
919 if $align;
920 $ylab -= ($charht + 6);
921 }
922 push @labeltext,
923 [ $label, $xlab, $ylab, $xend - $xlab - 6,
924 (($annot && ($annot eq 'j') && $annots) ? 1 : undef), $align ];
925 }
926 #
927 # if annotated, add superscript if embedded
928 #
929 push @supertext, [ $$annotnum++, $xend, $ylab - 5 ]
930 if ($annot && ($annot eq 'e') && $annots);
931 #
932 # DAA support areamaps
933 #
934 $ylab += $charht,
935 $yend += $charht
936 unless ($yend - $ylab > $charht);
937 print $mapfd _createLabelMap(
938 $xmap, $ylab, $xend, $yend, # location
939 $label, "$mapname\_$i", (($type eq 'call') ? $path : undef),
940 ($annots ? $annots->getAttribute('text') : undef),
941 "$mapname\_annot_$i", $annotspan) # annotation
942 if $mapfd;
943  
944 return $img;
945 }
946 #
947 # to wrap long class names in the header boxes
948 #
949 sub _wrapText {
950 my ($text, $xpos) = @_;
951  
952 return $text
953 if ((length($text) * $classcharwidth) < ($CLASS_BOX_WIDTH - 6));
954 #
955 # split on whitespace, dot/colon/underscore, or
956 # lowercaseUppercase
957 #
958 my $maxChars = int(($CLASS_BOX_WIDTH - 10)/$classcharwidth);
959 my @lines = ();
960 my @pieces = ();
961 while ($text=~s/([^:\.\s_]+)((:+)|\.|_|\s+)?//) {
962 my ($t, $p) = ($1, $2);
963 push @pieces, $t;
964 #
965 # if the text is still too long, look for lowerUpper
966 #
967 if (length($pieces[-1]) <= $maxChars) {
968 push(@pieces, $p) if $p;
969 next;
970 }
971  
972 $t = pop @pieces;
973 push @pieces, $1
974 while ($t=~s/^(.*?[a-z])([A-Z].*)$/$2/);
975 push @pieces, $t if ($t ne '');
976  
977 push(@pieces, $p) if $p;
978 }
979 #
980 # now reassemble to minimize box height
981 #
982 $lines[0] = shift @pieces;
983 foreach (@pieces) {
984 $lines[-1] .= $_,
985 next
986 if (length($lines[-1]) <= $maxChars) &&
987 ((length($lines[-1]) + length($_)) <= $maxChars);
988 push @lines, $_ unless /^\s+$/;
989 }
990  
991 return join("\n", @lines);
992 }
993 1;
994  
995 =head1 Application Notes
996  
997 The diagram layout of output of this module differs slightly
998 from the layout of L<UML::Sequence::Svg> output; specifically,
999 this module stacks conditionals atop the activation label in
1000 order to avoid collisions with activation bars.
1001  
1002 =head1 SEE ALSO
1003  
1004 L<UML::Sequence::Svg>
1005  
1006 =head1 AUTHOR
1007  
1008 Dean Arnold L<mailto:darnold@presicient.com>
1009  
1010 =head1 COPYRIGHT
1011  
1012 Copyright (C) 2006, Dean Arnold, Presicient Corp., USA. All rights reserved.
1013  
1014 Portions Copyright 2003, Philip Crow, all rights reserved.
1015  
1016 You may modify and/or redistribute this code in the same manner as Perl itself.
1017  
1018 =cut