| 1 | =head1 NAME | ||
| 2 | |||
| 3 | UML::Sequence::Svg - converts xml sequence files to svg | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use UML::Sequence::Svg; | ||
| 8 | |||
| 9 | seq2svg @ARGV; | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This module supports the seq2svg.pl script like Pod::Html supports pod2html. | ||
| 14 | The array passed to seq2svg.pl should have the following form: | ||
| 15 | |||
| 16 | ([ I<options-pairs>, ] [input_file_name]) | ||
| 17 | |||
| 18 | where I<options-pairs> are any of | ||
| 19 | |||
| 20 | =over 4 | ||
| 21 | |||
| 22 | =item C<-a color> | ||
| 23 | |||
| 24 | specifies a color to be used to fill the activation boxes. Must be in a form | ||
| 25 | acceptable to SVG. | ||
| 26 | |||
| 27 | =item C<-c color> | ||
| 28 | |||
| 29 | specifies a color to be used to fill the class boxes. Must be in a form | ||
| 30 | acceptable to SVG. | ||
| 31 | |||
| 32 | =item C<-e> | ||
| 33 | |||
| 34 | specifies that embedded annotations are to be applied | ||
| 35 | to any rasterized version of the SVG image. Only valid when the C<-m> or C<-M> | ||
| 36 | option is also specified. When an arrow label has associated annotations, | ||
| 37 | the labels with be suffixed with a superscript number linking to | ||
| 38 | a text section containing the annotation text. | ||
| 39 | |||
| 40 | =item C<-g arrow-gap> | ||
| 41 | |||
| 42 | If C<arrow-gap> is an integer value, specifies number of pixels between arrows | ||
| 43 | (default is 40 pixels). | ||
| 44 | If C<arrow-gap> is a fractional value, specifies a scaling factor for the | ||
| 45 | default number of pixels between arrows. | ||
| 46 | |||
| 47 | =item C<-j> | ||
| 48 | |||
| 49 | specifies that Javascript'ed tooltip annotations are to be applied | ||
| 50 | to any rasterized version of the SVG image. Only valid when the C<-m> or C<-M> | ||
| 51 | option is also specified. When an arrow label has associated annotations, | ||
| 52 | the hyperlinks in the areamap for the label will include | ||
| 53 | C<onmouseover()> function calls containing the annotation text for use with the | ||
| 54 | Javascript tooltip package available at | ||
| 55 | L<http://www.walterzorn.com/tooltip/tooltip_e.htm>. | ||
| 56 | |||
| 57 | =item C<-m areamap-path>, C<-M areamap-path> | ||
| 58 | |||
| 59 | specifies the name of a file to receive HTML containing an image element, | ||
| 60 | areamap, and (optionally) either an ordered list of annotations (if L<-e> was | ||
| 61 | specified) or a script tag linking to the Javascript tooltip script (if L<-j> | ||
| 62 | was specified). to be applied to any rasterized version of the SVG image. | ||
| 63 | Only valid when either the L<-p> or L<-P> option is specified. | ||
| 64 | C<-M> specifies append mode for the output file. | ||
| 65 | |||
| 66 | =item C<-o output_file_name> | ||
| 67 | |||
| 68 | specifies the output file name. | ||
| 69 | |||
| 70 | =item C<-p classdocs-path>, C<-P classdocs-path> | ||
| 71 | |||
| 72 | specifies a base path to classdocs generated by psichedoc. C<-p> causes | ||
| 73 | hyperlinks to the documents for individual classes and/or methods to be | ||
| 74 | embedded in the SVG file for both the class labels and method labels, excluding | ||
| 75 | method labels w/ embedded whitespace. Additionally, the specified path is used | ||
| 76 | with any generated HTML imagemap. C<-P> is the same, except it does B<not> | ||
| 77 | embed hyperlinks in the SVG (due to Batik's current inability to handle | ||
| 78 | rasterization of SVG's with embeded hyperlinks). In other words, use C<-P> | ||
| 79 | to specify a path for HTML imagemaps when the SVG output will be further | ||
| 80 | processed by Batik. | ||
| 81 | |||
| 82 | =item C<-w box-width> | ||
| 83 | |||
| 84 | specifies width of class box in pixels; default is 125. Used to compute | ||
| 85 | class header boxes and areamap coordinates. | ||
| 86 | |||
| 87 | =item C<-x char-width> | ||
| 88 | |||
| 89 | specifies width of characters in pixels; default is 6. Used to compute | ||
| 90 | class header boxes and areamap coordinates. | ||
| 91 | |||
| 92 | =item C<-y char-height> | ||
| 93 | |||
| 94 | specifies height of characters in pixels; default is 14. Used to compute | ||
| 95 | class header boxes and areamap coordinates. | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | By default input is from standard in and output is to standard out. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | package UML::Sequence::Svg; | ||
| 104 | |||
| 105 | 3 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 106 | @ISA = qw(Exporter); | ||
| 107 | @EXPORT = qw(seq2svg); | ||
| 108 | |||
| 109 | 0 | ▶ | use XML::DOM; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 110 | use Getopt::Std; | ||
| 111 | |||
| 112 | use strict; | ||
| 113 | use warnings; | ||
| 114 | |||
| 115 | our $VERSION = '0.02'; | ||
| 116 | |||
| 117 | # Constant declarations. | ||
| 118 | my $CLASS_TEXT_Y = 40; | ||
| 119 | my $CLASS_BOX_Y = 25; | ||
| 120 | my $CLASS_BOX_HEIGHT = 20; | ||
| 121 | my $CLASS_BOX_WIDTH = 125; | ||
| 122 | my $CLASS_SPACING = 3; | ||
| 123 | |||
| 124 | my $LEFT_EDGE = 30; | ||
| 125 | |||
| 126 | my $ACTIVATION_WIDTH = 15; | ||
| 127 | my $ACTIVATION_OFFSET = 10; | ||
| 128 | |||
| 129 | my $FIRST_ARROW = 55; | ||
| 130 | my $ARROW_SPACING = 40; | ||
| 131 | |||
| 132 | # Global variable: | ||
| 133 | my $output_file = "-"; | ||
| 134 | my $classcolor = 'white'; | ||
| 135 | my $actcolor = 'white'; | ||
| 136 | my $docpath; # path to assoc. classdocs | ||
| 137 | my $hyperlink = 0; # true => embed hyperlinks | ||
| 138 | my $mappath; # path to write areamap file | ||
| 139 | my $mapname; # imagemap name derived from SVG output filename | ||
| 140 | my $charwidth = 10; # width of characters | ||
| 141 | my $charht = 10; # height of characters | ||
| 142 | my $annot; # annotations behavior: | ||
| 143 | # 'e' : embed in specified file; | ||
| 144 | # 'j' : apply javascript tooltips in specified file | ||
| 145 | # NOTE: specified file may be same as image map file | ||
| 146 | |||
| 147 | sub seq2svg { | ||
| 148 | local (@ARGV) = @_; | ||
| 149 | my $opts = parse_command_line(); | ||
| 150 | |||
| 151 | $classcolor = $opts->{c} || 'white'; | ||
| 152 | $actcolor = $opts->{a} || 'white'; | ||
| 153 | $docpath = $opts->{p} || $opts->{P}; | ||
| 154 | $hyperlink = defined($opts->{p}); | ||
| 155 | $mappath = $opts->{m} || $opts->{M}; | ||
| 156 | $charwidth = $opts->{x} || 6; | ||
| 157 | $charht = $opts->{y} || 14; | ||
| 158 | $CLASS_BOX_WIDTH = $opts->{w} || 125; | ||
| 159 | $annot = $opts->{e} ? 'e' : $opts->{j} ? 'j' : undef; | ||
| 160 | |||
| 161 | die "Annotation requested without output path." | ||
| 162 | if ($annot && (! $mappath)); | ||
| 163 | |||
| 164 | $ARROW_SPACING = (index($opts->{g}, '.') >= 0) ? | ||
| 165 | int($ARROW_SPACING * $opts->{g}) : $opts->{g} | ||
| 166 | if $opts->{g}; | ||
| 167 | |||
| 168 | $docpath .= '/' | ||
| 169 | if ($docpath && (substr($docpath, -1, 1) ne '/')); | ||
| 170 | |||
| 171 | my $input_file = shift @ARGV; | ||
| 172 | |||
| 173 | if (defined $input_file) { | ||
| 174 | open INPUT, "$input_file" | ||
| 175 | or die "Couldn't open $input_file for input: $!\n"; | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | *INPUT = *STDIN; | ||
| 179 | } | ||
| 180 | # | ||
| 181 | # DAA Add HTML image map rendering | ||
| 182 | # | ||
| 183 | my $mapfd; | ||
| 184 | if (defined($mappath)) { | ||
| 185 | $mappath = $opts->{M} ? ">>$mappath" : ">$mappath"; | ||
| 186 | die "Cannot open image map file $mappath: $!" | ||
| 187 | unless open($mapfd, $mappath); | ||
| 188 | # | ||
| 189 | # define a mapname from output file name (if any) | ||
| 190 | # | ||
| 191 | $mapname = ($output_file=~/(\w+)\.\w+$/) ? $1 : 'mapname'; | ||
| 192 | print $mapfd | ||
| 193 | "<html> | ||
| 194 | <body> | ||
| 195 | <img src='$mapname\.png' usemap='#$mapname'> | ||
| 196 | <MAP NAME='$mapname'> | ||
| 197 | "; | ||
| 198 | } | ||
| 199 | |||
| 200 | my $parser = XML::DOM::Parser->new(); | ||
| 201 | my $doc = $parser->parse(*INPUT); | ||
| 202 | |||
| 203 | my $sequence = $doc->getDocumentElement(); | ||
| 204 | my $title = $sequence->getAttribute("title"); | ||
| 205 | |||
| 206 | my $classes = $doc->getElementsByTagName("class"); | ||
| 207 | # | ||
| 208 | # DAA added to track previous class for drawing async arrows | ||
| 209 | # | ||
| 210 | my %priors = (); | ||
| 211 | my $class_output = draw_classes($classes, $mapfd, \%priors); | ||
| 212 | my $class_hash = build_class_name_hash($classes); | ||
| 213 | my $arrow_output = draw_arrows($doc, $class_hash, $mapfd, \%priors); | ||
| 214 | my $class_count = scalar (keys %$class_hash); | ||
| 215 | my $arrow_count = count_arrows($doc); | ||
| 216 | my $width = | ||
| 217 | ($class_count + 1) * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + 40; | ||
| 218 | my $height = 2.5 * $CLASS_TEXT_Y + $arrow_count * ($ARROW_SPACING); | ||
| 219 | |||
| 220 | open SVGOUT, ">$output_file"; | ||
| 221 | |||
| 222 | print SVGOUT <<EOJ; | ||
| 223 | <?xml version="1.0"?> | ||
| 224 | <svg xmlns="http://www.w3.org/2000/svg" height="$height" width="$width"> | ||
| 225 | <defs> | ||
| 226 | <style type="text/css"> | ||
| 227 | rect, line, path { stroke-width: 2; stroke: black } | ||
| 228 | text { font-weight: bold } | ||
| 229 | <marker orient="auto" refY="2.5" refX="4" markerHeight="7" markerWidth="6" id="mArrow"> | ||
| 230 | <path style="fill: black; stroke: none" d="M 0 0 6 3 0 7"/> | ||
| 231 | </marker> | ||
| 232 | <marker orient="auto" refY="2.5" refX="4" markerHeight="7" markerWidth="6" id="mRtHalfArrow"> | ||
| 233 | <path style="fill: black; stroke: none" d="M 0 7 6 2 0 2"/> | ||
| 234 | </marker> | ||
| 235 | |||
| 236 | <marker orient="0 deg" refY="2.5" refX="4" markerHeight="7" markerWidth="6" id="mLtHalfArrow"> | ||
| 237 | <path style="fill: black; stroke: none" d="M 0 2 6 2 6 7"/> | ||
| 238 | </marker> | ||
| 239 | </style> | ||
| 240 | </defs> | ||
| 241 | EOJ | ||
| 242 | |||
| 243 | if ($title) { | ||
| 244 | print SVGOUT <<EOJ; | ||
| 245 | <text x="5" y="15"> | ||
| 246 | $title | ||
| 247 | </text> | ||
| 248 | EOJ | ||
| 249 | } | ||
| 250 | |||
| 251 | print SVGOUT <<EOJ; | ||
| 252 | $class_output$arrow_output</svg> | ||
| 253 | EOJ | ||
| 254 | # | ||
| 255 | # DAA terminate areamap | ||
| 256 | # | ||
| 257 | if (defined($mapfd)) { | ||
| 258 | print $mapfd ' | ||
| 259 | <script language="JavaScript" type="text/javascript" src="../wz_tooltip.js"></script> | ||
| 260 | |||
| 261 | </body> | ||
| 262 | </html> | ||
| 263 | ' | ||
| 264 | if $opts->{j}; | ||
| 265 | close $mapfd; | ||
| 266 | } | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | sub draw_classes { | ||
| 271 | my $classes = shift; | ||
| 272 | my $mapfd = shift; | ||
| 273 | my $priors = shift; | ||
| 274 | my $retval; | ||
| 275 | |||
| 276 | my $x = $LEFT_EDGE; | ||
| 277 | my $box_left = $LEFT_EDGE - 8; | ||
| 278 | my $y = $CLASS_TEXT_Y; | ||
| 279 | my $max_extent; | ||
| 280 | my $boxht; | ||
| 281 | my $boxtext; | ||
| 282 | |||
| 283 | my $prior = '_EXTERNAL'; | ||
| 284 | for (my $i = 0; $i < $classes->getLength(); $i++) { | ||
| 285 | my $class = $classes->item($i); | ||
| 286 | my $life_x = int($x + $CLASS_BOX_WIDTH / 2); | ||
| 287 | my $class_name = $class ->getAttribute("name"); | ||
| 288 | |||
| 289 | if ($class_name eq '_EXTERNAL') { | ||
| 290 | $retval = ''; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | |||
| 294 | $priors->{$class_name} = $prior; | ||
| 295 | $prior = $class_name; | ||
| 296 | |||
| 297 | ($boxht, $boxtext) = _wrapText($class_name, $x); | ||
| 298 | # | ||
| 299 | # DAA add hyperlink to psichedocs | ||
| 300 | # | ||
| 301 | my $class_path = ($docpath && ($class_name!~/\s/)) ? | ||
| 302 | $docpath . join('/', split(/::/, $class_name)) . '.html' : undef; | ||
| 303 | |||
| 304 | my $born = $class ->getAttribute("born") | ||
| 305 | * $ARROW_SPACING + $FIRST_ARROW; | ||
| 306 | my $extends_to = ($class ->getAttribute("extends-to") + 1) | ||
| 307 | * $ARROW_SPACING + $FIRST_ARROW; | ||
| 308 | |||
| 309 | if (not defined $max_extent) { $max_extent = $extends_to; } | ||
| 310 | # | ||
| 311 | # DAA rearranged to place text on top of rectangle | ||
| 312 | # for fill purposes | ||
| 313 | # | ||
| 314 | $retval .= " <rect style='fill: $classcolor' height='$boxht' " | ||
| 315 | . "width='$CLASS_BOX_WIDTH' y='$CLASS_BOX_Y' x='$box_left' />\n"; | ||
| 316 | |||
| 317 | $retval .= "<a xlink:href='$class_path'>" | ||
| 318 | if $hyperlink; | ||
| 319 | |||
| 320 | $retval .= "<text y='$CLASS_TEXT_Y' x='$x'>$boxtext</text>\n"; | ||
| 321 | |||
| 322 | $retval .= "</a>" | ||
| 323 | if $hyperlink; | ||
| 324 | # | ||
| 325 | # DAA support areamaps | ||
| 326 | # | ||
| 327 | print $mapfd "<AREA TITLE='$class_name'", | ||
| 328 | ($class_path ? " HREF='$class_path'" : ''), | ||
| 329 | " SHAPE=RECT COORDS='$box_left,$CLASS_BOX_Y,", | ||
| 330 | $box_left + $CLASS_BOX_WIDTH, ',', $CLASS_BOX_Y + $boxht, "'>\n" | ||
| 331 | if $mapfd; | ||
| 332 | |||
| 333 | $retval .= " <line style='stroke-dasharray: 4,4; ' fill='none' " | ||
| 334 | . "stroke='black' x1='$life_x' y1='$born' x2='$life_x' " | ||
| 335 | . "y2='$max_extent' />\n"; | ||
| 336 | |||
| 337 | my $activation_x = int($box_left + $CLASS_BOX_WIDTH / 2); | ||
| 338 | my @activations = $class->getElementsByTagName("activation"); | ||
| 339 | foreach my $activation (@activations) { | ||
| 340 | my $born = $activation->getAttribute("born"); | ||
| 341 | my $extends_to = $activation->getAttribute("extends-to"); | ||
| 342 | my $offset = $activation->getAttribute("offset"); | ||
| 343 | my $top = $FIRST_ARROW + $born * $ARROW_SPACING; | ||
| 344 | my $height = ($extends_to - $born + .5) * $ARROW_SPACING; | ||
| 345 | my $left = $activation_x + $offset * $ACTIVATION_OFFSET; | ||
| 346 | $retval .= " <rect style='fill: $actcolor' height='$height' " | ||
| 347 | . "width='$ACTIVATION_WIDTH' y='$top' x='$left'/>\n"; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | $x += $CLASS_BOX_WIDTH + $CLASS_SPACING; | ||
| 351 | $box_left += $CLASS_BOX_WIDTH + $CLASS_SPACING; | ||
| 352 | $retval .= "\n"; | ||
| 353 | } | ||
| 354 | return $retval; | ||
| 355 | } | ||
| 356 | |||
| 357 | sub count_arrows { | ||
| 358 | my $doc = shift; | ||
| 359 | my $arrows = $doc->getElementsByTagName("arrow"); | ||
| 360 | return $arrows->getLength(); | ||
| 361 | } | ||
| 362 | |||
| 363 | sub draw_arrows { | ||
| 364 | my $doc = shift; | ||
| 365 | my $class_hash = shift; | ||
| 366 | my $mapfd = shift; | ||
| 367 | my $priors = shift; | ||
| 368 | my $retval; | ||
| 369 | |||
| 370 | my $arrows = $doc->getElementsByTagName("arrow"); | ||
| 371 | my $annotnum = 1; | ||
| 372 | my $annotspan = "\n<ol>\n" if ($annot && ($annot eq 'e')); | ||
| 373 | |||
| 374 | for (my $i = 0; $i < $arrows->getLength(); $i++) { | ||
| 375 | my $arrow = $arrows->item($i); | ||
| 376 | my $from = $arrow->getAttribute("from" ); | ||
| 377 | my $to = $arrow->getAttribute("to" ); | ||
| 378 | # | ||
| 379 | # DAA 12/24/2005 | ||
| 380 | # use type attribute to specify returnvalue or external | ||
| 381 | # which changes the line style to dashed or folded, respectively | ||
| 382 | # also note that $from for external events originate at the far left | ||
| 383 | # | ||
| 384 | my $type = $arrow->getAttribute("type" ); | ||
| 385 | my $label = $arrow->getAttribute("label" ); | ||
| 386 | my $from_offset = $arrow->getAttribute("from-offset"); | ||
| 387 | my $to_offset = $arrow->getAttribute("to-offset" ); | ||
| 388 | my $annots = $arrow->getElementsByTagName('annotation'); | ||
| 389 | $annots = $annots->item(0) if $annots; | ||
| 390 | my $y = $FIRST_ARROW + ($i + 1) * $ARROW_SPACING; | ||
| 391 | |||
| 392 | my $from_number = ($from eq '_EXTERNAL') ? | ||
| 393 | $class_hash->{$priors->{$to}} : $class_hash->{$from}; | ||
| 394 | my $to_number = $class_hash->{$to}; | ||
| 395 | $label =~ s/</</g; | ||
| 396 | $label =~ s/>/>/g; | ||
| 397 | my $class_path; | ||
| 398 | my $labmethod = $hyperlink ? \&drawHyperLabel : \&drawLabel; | ||
| 399 | # | ||
| 400 | # DAA add hyperlink to psichedocs | ||
| 401 | # | ||
| 402 | if ($docpath && ($to!~/\s/)) { | ||
| 403 | my $doclabel = $label; | ||
| 404 | $doclabel=~s/[\*!]//g; | ||
| 405 | $doclabel=~s/^\s*\[[^\]]*\]\s*//; | ||
| 406 | $doclabel=~s/^\s+//; | ||
| 407 | $doclabel=~s/\s+$//; | ||
| 408 | $class_path = $docpath . join('/', split(/::/, $to)) . | ||
| 409 | ".html#$doclabel" | ||
| 410 | unless ($doclabel=~/\s/); | ||
| 411 | } | ||
| 412 | |||
| 413 | if ($from_number < $to_number) { # arrow from left to right | ||
| 414 | my $x1 = $from_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE | ||
| 415 | + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2 | ||
| 416 | + $from_offset * $ACTIVATION_OFFSET; | ||
| 417 | $x1 += 20 if ($from eq '_EXTERNAL'); | ||
| 418 | my $x2 = $to_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE | ||
| 419 | + ($CLASS_BOX_WIDTH - $ACTIVATION_WIDTH)/2; | ||
| 420 | # | ||
| 421 | # DAA cuddle the label to the arrowhead | ||
| 422 | # | ||
| 423 | my $xlab = $x2 - $CLASS_SPACING - 6 - | ||
| 424 | ($label ? ($charwidth * length($label)) : $charwidth); | ||
| 425 | my $ylab = $y - 6; | ||
| 426 | my $xend = $label ? $xlab + ($charwidth * length($label)) : undef; | ||
| 427 | my $yend = $ylab + $charht; | ||
| 428 | # | ||
| 429 | # DAA changed to support call vs. return vs. async activations | ||
| 430 | # | ||
| 431 | $retval .= "<line x1='$x1' y1='$y' x2='$x2' y2='$y' style='" . | ||
| 432 | (($type eq 'return') ? "stroke-dasharray: 4,4; " : ' ') . | ||
| 433 | 'marker-end: url(#' . | ||
| 434 | (($type eq 'async') ? "mRtHalfArrow);' />\n" : "mArrow);' />\n"); | ||
| 435 | # | ||
| 436 | # DAA need to add hrefs for methods, but we'll need to track the "to" class | ||
| 437 | # name, and ignore names w/ whitespace | ||
| 438 | # | ||
| 439 | $retval .= $labmethod->($mapfd, $xlab, $ylab, $xend, $yend, | ||
| 440 | $type, $label, $class_path, $mapname, $i, | ||
| 441 | \$annotnum, $annots, \$annotspan) | ||
| 442 | if defined($label); | ||
| 443 | } | ||
| 444 | elsif ($from_number > $to_number) { # arrow from right to left | ||
| 445 | my $x1 = $from_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE | ||
| 446 | + ($CLASS_BOX_WIDTH - $ACTIVATION_WIDTH)/2; | ||
| 447 | my $x2 = $to_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE | ||
| 448 | + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2 | ||
| 449 | + $to_offset * $ACTIVATION_OFFSET; | ||
| 450 | # | ||
| 451 | # DAA changed to support call vs. return vs. async activations | ||
| 452 | # (note async activations always go from left to right, so we won't get | ||
| 453 | # any here...) | ||
| 454 | # | ||
| 455 | $retval .= "<line x1='$x1' y1='$y' x2='$x2' y2='$y' style='" . | ||
| 456 | (($type eq 'return') ? "stroke-dasharray: 4,4; " : ' ') . | ||
| 457 | "marker-end: url(#mArrow);' />\n"; | ||
| 458 | |||
| 459 | my $xlab = $x2 + $CLASS_SPACING + 6; | ||
| 460 | my $ylab = $y - 6; | ||
| 461 | my $xend = $label ? $xlab + ($charwidth * length($label)) : undef; | ||
| 462 | my $yend = $ylab + $charht; | ||
| 463 | # | ||
| 464 | # DAA need to add hrefs for methods, but we'll need to track the "to" class | ||
| 465 | # name, and ignore names w/ whitespace | ||
| 466 | # | ||
| 467 | $retval .= $labmethod->($mapfd, $xlab, $ylab, $xend, $yend, | ||
| 468 | $type, $label, $class_path, $mapname, $i, | ||
| 469 | \$annotnum, $annots, \$annotspan) | ||
| 470 | if defined($label); | ||
| 471 | } | ||
| 472 | else { # arrow from and to same class | ||
| 473 | my $x1 = $from_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE | ||
| 474 | + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2 | ||
| 475 | + $from_offset * $ACTIVATION_OFFSET; | ||
| 476 | my $x2 = $to_number * ($CLASS_BOX_WIDTH + $CLASS_SPACING) + $LEFT_EDGE | ||
| 477 | + ($CLASS_BOX_WIDTH + $ACTIVATION_WIDTH)/2 | ||
| 478 | + $to_offset * $ACTIVATION_OFFSET; | ||
| 479 | $y -= 10; | ||
| 480 | my $y2 = $y + 20; | ||
| 481 | my $x1padded = $x1 + $ACTIVATION_OFFSET + 15; | ||
| 482 | $retval .= "<line x1='$x1' y1='$y' x2='$x1padded' y2='$y' />\n" | ||
| 483 | . "<line x1='$x1padded' y1='$y' x2='$x1padded' y2='$y2' />\n" | ||
| 484 | . "<line x1='$x1padded' y1='$y2' x2='$x2' y2='$y2' " | ||
| 485 | . "style='marker-end: url(#mArrow);' />\n"; | ||
| 486 | my $xlab = $x1padded + $CLASS_SPACING; | ||
| 487 | my $ylab = ($y + $y2) / 2; | ||
| 488 | my $xend = $label ? $xlab + ($charwidth * length($label)) : undef; | ||
| 489 | my $yend = $ylab + $charht; | ||
| 490 | # | ||
| 491 | # DAA need to add hrefs for methods, but we'll need to track the "to" class | ||
| 492 | # name, and ignore names w/ whitespace | ||
| 493 | # | ||
| 494 | $retval .= $labmethod->($mapfd, $xlab, $ylab, $xend, $yend, | ||
| 495 | $type, $label, $class_path, $mapname, $i, | ||
| 496 | \$annotnum, $annots, \$annotspan) | ||
| 497 | if defined($label); | ||
| 498 | |||
| 499 | } | ||
| 500 | } | ||
| 501 | # | ||
| 502 | # DAA save annotations if any | ||
| 503 | # | ||
| 504 | if ($mapfd) { | ||
| 505 | print $mapfd "</MAP>\n\n"; | ||
| 506 | print $mapfd "$annotspan\n</ol>\n" | ||
| 507 | if $annotspan && ($annotspan ne '<ol>'); | ||
| 508 | } | ||
| 509 | |||
| 510 | return $retval; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub build_class_name_hash { | ||
| 514 | my $class_nodes = shift; | ||
| 515 | my %classes; # keyed by class name store left to right position | ||
| 516 | |||
| 517 | for (my $i = 0; $i < $class_nodes->getLength(); $i++) { | ||
| 518 | my $class = $class_nodes->item($i); | ||
| 519 | my $class_name = $class->getAttribute("name"); | ||
| 520 | $classes{$class_name} = $i; | ||
| 521 | } | ||
| 522 | return \%classes; | ||
| 523 | } | ||
| 524 | # | ||
| 525 | # DAA to wrap long class names in the header boxes | ||
| 526 | # | ||
| 527 | sub _wrapText { | ||
| 528 | my ($text, $xpos) = @_; | ||
| 529 | |||
| 530 | return ($CLASS_BOX_HEIGHT, $text) | ||
| 531 | if ((length($text) * $charwidth) < ($CLASS_BOX_WIDTH - 10)); | ||
| 532 | # | ||
| 533 | # split on whitespace, dot/colon/underscore, or | ||
| 534 | # lowercaseUppercase | ||
| 535 | # | ||
| 536 | my $maxChars = int(($CLASS_BOX_WIDTH - 10)/$charwidth); | ||
| 537 | my @lines = (); | ||
| 538 | my @pieces = (); | ||
| 539 | while ($text=~s/([^:\.\s_]+)((:+)|\.|_|\s+)?//) { | ||
| 540 | my ($t, $p) = ($1, $2); | ||
| 541 | push @pieces, $t; | ||
| 542 | # | ||
| 543 | # if the text is still too long, look for lowerUpper | ||
| 544 | # | ||
| 545 | if (length($pieces[-1]) <= $maxChars) { | ||
| 546 | push(@pieces, $p) if $p; | ||
| 547 | next; | ||
| 548 | } | ||
| 549 | |||
| 550 | $t = pop @pieces; | ||
| 551 | push @pieces, $1 | ||
| 552 | while ($t=~s/^(.*?[a-z])([A-Z].*)$/$2/); | ||
| 553 | push @pieces, $t if ($t ne ''); | ||
| 554 | |||
| 555 | push(@pieces, $p) if $p; | ||
| 556 | } | ||
| 557 | # | ||
| 558 | # now reassemble to minimize box height | ||
| 559 | # | ||
| 560 | $lines[0] = shift @pieces; | ||
| 561 | foreach (@pieces) { | ||
| 562 | $lines[-1] .= $_, | ||
| 563 | next | ||
| 564 | if (length($lines[-1]) <= $maxChars) && | ||
| 565 | ((length($lines[-1]) + length($_)) <= $maxChars); | ||
| 566 | push @lines, $_ unless /^\s+$/; | ||
| 567 | } | ||
| 568 | |||
| 569 | my $otext = "<tspan x='$xpos'>" . | ||
| 570 | join("</tspan>\n<tspan x='$xpos' dy='1em'>", @lines) . '</tspan>'; | ||
| 571 | return (($charht + 2) * scalar @lines, $otext); | ||
| 572 | |||
| 573 | } | ||
| 574 | |||
| 575 | sub parse_command_line { | ||
| 576 | my %opts; | ||
| 577 | getopts('a:c:eg:jm:M:o:p:P:w:x:y:', \%opts); | ||
| 578 | $output_file = $opts{o} if defined $opts{o}; | ||
| 579 | $classcolor = $opts{c} || 'white'; | ||
| 580 | $actcolor = $opts{a} || 'white'; | ||
| 581 | $docpath = $opts{p} || $opts{P}; | ||
| 582 | $annot = $opts{e} || $opts{j}; | ||
| 583 | return \%opts; | ||
| 584 | } | ||
| 585 | |||
| 586 | sub _createLabelMap { | ||
| 587 | my ($x1, $y1, $x2, $y2, $title, $name, $path, $annot_text, | ||
| 588 | $annot_name, $annot_span) = @_; | ||
| 589 | # | ||
| 590 | # NOTE: Batik seems to render the text coords about 10px lower than SVG, | ||
| 591 | # so we'll cheat here... | ||
| 592 | # | ||
| 593 | $y1 -= 10, $y2 -= 10; | ||
| 594 | |||
| 595 | my $maptext = | ||
| 596 | "<AREA NAME='$name' TITLE='$title' SHAPE=RECT COORDS='$x1,$y1,$x2,$y2' " . | ||
| 597 | ($path ? "HREF='$path' " : ''); | ||
| 598 | |||
| 599 | if ($annot_text) { | ||
| 600 | if ($annot && ($annot eq 'j')) { | ||
| 601 | # | ||
| 602 | # Scripted annotation | ||
| 603 | # | ||
| 604 | $annot_text =~s/'/\\'/g; # escape single quotes | ||
| 605 | $title =~s/'/\\'/g; # escape single quotes | ||
| 606 | # | ||
| 607 | # NOTE: this must all be on 1 line!!! | ||
| 608 | # | ||
| 609 | $maptext .=" | ||
| 610 | 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')\" > | ||
| 611 | "; | ||
| 612 | } | ||
| 613 | else { # annot eq 'e' | ||
| 614 | # | ||
| 615 | # embedded annotation, add anchor and annotation text | ||
| 616 | # NOTE: the offsets used here are heuristic, need a bbox jscript | ||
| 617 | # | ||
| 618 | my ($x3, $y3, $y4) = ($x2+4, $y1 + (($y2 - $y1)>>1), $y1 - 4); | ||
| 619 | |||
| 620 | $maptext .= "> | ||
| 621 | <AREA SHAPE=RECT COORDS='$x2,$y4,$x3,$y3' HREF='#$annot_name'> | ||
| 622 | "; | ||
| 623 | |||
| 624 | $annot_text=~s/</</g; | ||
| 625 | $annot_text=~s/>/>/g; | ||
| 626 | $$annot_span .= " | ||
| 627 | <a name='$annot_name'></a> | ||
| 628 | <li>$annot_text | ||
| 629 | <p> | ||
| 630 | "; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | else { | ||
| 634 | $maptext .= ">\n"; | ||
| 635 | } | ||
| 636 | |||
| 637 | return $maptext; | ||
| 638 | } | ||
| 639 | |||
| 640 | sub drawLabel { | ||
| 641 | my ($mapfd, $xlab, $ylab, $xend, $yend, $type, $label, $path, | ||
| 642 | $mapname, $i, $annotnum, $annots, $annotspan) = @_; | ||
| 643 | |||
| 644 | my $retval = "<text x='$xlab' y='$ylab' " . | ||
| 645 | # | ||
| 646 | # use italics for asyncs | ||
| 647 | # | ||
| 648 | (($type eq 'async') ? | ||
| 649 | "style='font-style: italic;'><tspan>" : | ||
| 650 | '><tspan>') . $label; | ||
| 651 | # | ||
| 652 | # if annotated, add superscript if embedded | ||
| 653 | # | ||
| 654 | $retval .= "<tspan baseline-shift=\"super\">$$annotnum</tspan>", | ||
| 655 | $$annotnum++ | ||
| 656 | if ($annot && ($annot eq 'e') && $annots); | ||
| 657 | |||
| 658 | $retval .= "</tspan></text>\n"; | ||
| 659 | # | ||
| 660 | # DAA support areamaps | ||
| 661 | # | ||
| 662 | print $mapfd _createLabelMap( | ||
| 663 | $xlab, $ylab, $xend, $yend, # location | ||
| 664 | $label, "$mapname\_$i", (($type eq 'call') ? $path : undef), | ||
| 665 | ($annots ? $annots->getAttribute('text') : undef), | ||
| 666 | "$mapname\_annot_$i", $annotspan) # annotation | ||
| 667 | if $mapfd; | ||
| 668 | |||
| 669 | return $retval; | ||
| 670 | } | ||
| 671 | |||
| 672 | sub drawHyperLabel { | ||
| 673 | my ($mapfd, $xlab, $ylab, $xend, $yend, $type, $label, $path, | ||
| 674 | $mapname, $i, $annotnum, $annots, $annotspan) = @_; | ||
| 675 | |||
| 676 | my $retval = "<text x='$xlab' y='$ylab' " . | ||
| 677 | # | ||
| 678 | # use italics for asyncs | ||
| 679 | # | ||
| 680 | (($type eq 'async') ? | ||
| 681 | "style='font-style: italic;'><tspan>" : '><tspan>') . | ||
| 682 | ($path ? "<a xlink:href='$path'>$label</a>" : $label); | ||
| 683 | # | ||
| 684 | # if annotated, add superscript if embedded | ||
| 685 | # | ||
| 686 | $retval .= "<tspan baseline-shift=\"super\"> | ||
| 687 | <a xlink:href='#$mapname\_annot_$i'>$$annotnum</a> | ||
| 688 | </tspan>", | ||
| 689 | $$annotnum++ | ||
| 690 | if ($annot && ($annot eq 'e') && $annots); | ||
| 691 | |||
| 692 | $retval .= "</tspan></text>\n"; | ||
| 693 | # | ||
| 694 | # DAA support areamaps | ||
| 695 | # | ||
| 696 | print $mapfd _createLabelMap( | ||
| 697 | $xlab, $ylab, $xend, $yend, # location | ||
| 698 | $label, "$mapname\_$i", (($type eq 'call') ? $path : undef), | ||
| 699 | ($annots ? $annots->getAttribute('text') : undef), | ||
| 700 | "$mapname\_annot_$i", $annotspan) # annotation | ||
| 701 | if $mapfd; | ||
| 702 | |||
| 703 | return $retval; | ||
| 704 | } | ||
| 705 | |||
| 706 | 1; | ||
| 707 | |||
| 708 | =head1 SEE ALSO | ||
| 709 | |||
| 710 | L<UML::Sequence::Raster> | ||
| 711 | |||
| 712 | =head1 AUTHOR | ||
| 713 | |||
| 714 | Phil Crow, <philcrow2000@yahoo.com> | ||
| 715 | |||
| 716 | Version 0.02 Updates by Dean Arnold, <darnold@presicient.com> | ||
| 717 | |||
| 718 | =head1 COPYRIGHT | ||
| 719 | |||
| 720 | Copyright 2003-2006, Philip Crow, all rights reserved. You may modify and/or | ||
| 721 | redistribute this code in the same manner as Perl itself. | ||
| 722 | |||
| 723 | =cut | ||
| 724 | |||