| 1 | package Chart::Sequence::Renderer::Imager; | ||
| 2 | |||
| 3 | $VERSION = 0.000_1; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Chart::Sequence::Renderer::Imager - Render a sequence diagram with Imager.pm | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | use POSIX qw( floor ceil ); | ||
| 16 | use Chart::Sequence::Object (); | ||
| 17 | use Imager (); | ||
| 18 | |||
| 19 | @ISA = qw( Chart::Sequence::Object ); | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | |||
| 23 | use constant arrow_length => 6; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $self = shift->SUPER::new( @_ ); | ||
| 27 | } | ||
| 28 | |||
| 29 | =head2 METHODS | ||
| 30 | |||
| 31 | =over | ||
| 32 | |||
| 33 | =item lay_out | ||
| 34 | |||
| 35 | $renderer->lay_out( $sequence ); | ||
| 36 | $renderer->lay_out( $sequence, { | ||
| 37 | Layout => $layout, | ||
| 38 | } ); | ||
| 39 | |||
| 40 | Uses Chart::Sequence::Layout if no $layout module reference is provided. | ||
| 41 | |||
| 42 | Prepares a sequence for rendering with this renderer. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub lay_out { | ||
| 47 | my $self = shift; | ||
| 48 | my ( $sequence, $options ) = @_; | ||
| 49 | $options ||= {}; | ||
| 50 | |||
| 51 | my $layout = $options->{layout} || do { | ||
| 52 | require Chart::Sequence::Layout; | ||
| 53 | Chart::Sequence::Layout->new; | ||
| 54 | }; | ||
| 55 | |||
| 56 | $layout->lay_out( $sequence, $self, $options ); | ||
| 57 | } | ||
| 58 | |||
| 59 | =item render_to_file | ||
| 60 | |||
| 61 | Given a layed out sequence, generate an image file. | ||
| 62 | |||
| 63 | $renderer->render_to_file( $s, "foo.png" ); | ||
| 64 | |||
| 65 | Lays out the image if it's not layed out. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub _render { | ||
| 70 | my $self = shift; | ||
| 71 | my ( $sequence, $options ) = @_; | ||
| 72 | |||
| 73 | $self->lay_out( $sequence, $options ) | ||
| 74 | unless $sequence->_layout_info; | ||
| 75 | |||
| 76 | my $l = $sequence->_layout_info; | ||
| 77 | my $i = Imager->new( | ||
| 78 | xsize => $l->{w}, | ||
| 79 | ysize => $l->{h}, | ||
| 80 | |||
| 81 | ); | ||
| 82 | |||
| 83 | $i->box( color => "#ffffff", filled => 1 ); | ||
| 84 | |||
| 85 | ## greybar the background | ||
| 86 | for ( map $_->_layout_info, $sequence->messages ) { | ||
| 87 | next if $_->{gb_index}; | ||
| 88 | |||
| 89 | $i->box( | ||
| 90 | xmin => $_->{gbx1}, | ||
| 91 | ymin => $_->{gby1}, | ||
| 92 | xmax => $_->{gbx2} - 1, | ||
| 93 | ymax => $_->{gby2} - 1, | ||
| 94 | filled => 1, | ||
| 95 | color => "#E0FFE0", | ||
| 96 | ); | ||
| 97 | } | ||
| 98 | |||
| 99 | ## Node boxes along the top | ||
| 100 | my $node_outline_color = Imager::Color->new( "#808080" ); | ||
| 101 | for ( map $_->_layout_info, $sequence->nodes ) { | ||
| 102 | my $ly = $_->{y} + $_->{h} - 1; | ||
| 103 | $i->box( | ||
| 104 | color => $node_outline_color, | ||
| 105 | filled => 0, | ||
| 106 | xmin => $_->{x}, | ||
| 107 | xmax => $_->{x} + $_->{w} - 1, | ||
| 108 | ymin => $_->{y}, | ||
| 109 | ymax => $ly, | ||
| 110 | ); | ||
| 111 | |||
| 112 | $i->string( | ||
| 113 | text => $_->{label}, | ||
| 114 | font => $_->{font}, | ||
| 115 | x => $_->{lx}, | ||
| 116 | y => $_->{ly} + $_->{label_metrics}->{b_offs}, | ||
| 117 | color => $_->{fontcolor}, | ||
| 118 | align => 1, | ||
| 119 | aa => 1, | ||
| 120 | ); | ||
| 121 | |||
| 122 | $i->line( | ||
| 123 | x1 => $_->{cx}, | ||
| 124 | y1 => $ly, | ||
| 125 | x2 => $_->{cx}, | ||
| 126 | y2 => $_->{end_y}, | ||
| 127 | aa => 0, | ||
| 128 | color => "#808080", | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | |||
| 132 | for ( map $_->_layout_info, $sequence->messages ) { | ||
| 133 | ## Message lines. | ||
| 134 | $i->line( | ||
| 135 | x1 => $_->{x1}, | ||
| 136 | y1 => $_->{y1}, | ||
| 137 | x2 => $_->{x2}, | ||
| 138 | y2 => $_->{y2}, | ||
| 139 | aa => 1, | ||
| 140 | color => $_->{fontcolor}, | ||
| 141 | ); | ||
| 142 | |||
| 143 | my @arrowhead_points = do { | ||
| 144 | my $angle = atan2 | ||
| 145 | $_->{x1} - $_->{x2}, | ||
| 146 | $_->{y1} - $_->{y2}; | ||
| 147 | my $cw_angle = $angle + 0.5; | ||
| 148 | my $ccw_angle = $angle - 0.5; | ||
| 149 | ( | ||
| 150 | [ $_->{x2}, | ||
| 151 | $_->{y2}, | ||
| 152 | ], | ||
| 153 | [ $_->{x2} + floor( 0.5 + arrow_length * sin $ccw_angle ), | ||
| 154 | $_->{y2} + floor( 0.5 + arrow_length * cos $ccw_angle ) | ||
| 155 | ], | ||
| 156 | [ $_->{x2} + floor( 0.5 + arrow_length * 0.75 * sin $angle ), | ||
| 157 | $_->{y2} + floor( 0.5 + arrow_length * 0.75 * cos $angle ) | ||
| 158 | ], | ||
| 159 | [ $_->{x2} + floor( 0.5 + arrow_length * sin $cw_angle ), | ||
| 160 | $_->{y2} + floor( 0.5 + arrow_length * cos $cw_angle ) | ||
| 161 | ], | ||
| 162 | [ $_->{x2}, | ||
| 163 | $_->{y2}, | ||
| 164 | ], | ||
| 165 | ); | ||
| 166 | }; | ||
| 167 | |||
| 168 | # my $l; | ||
| 169 | # my @colors = qw( #ffe0e0 #e0ffe0 #e0e0ff #e0e0e0 #c0c0c0 ); | ||
| 170 | # for ( reverse @arrowhead_points ) { | ||
| 171 | # if ( $l ) { | ||
| 172 | # $i->line( | ||
| 173 | # x1 => $l->[0], | ||
| 174 | # y1 => $l->[1], | ||
| 175 | # x2 => $_->[0], | ||
| 176 | # y2 => $_->[1], | ||
| 177 | # color => shift @colors, | ||
| 178 | # aa => 0, | ||
| 179 | # ); | ||
| 180 | # } | ||
| 181 | # $l = $_; | ||
| 182 | # } | ||
| 183 | |||
| 184 | $i->polygon( | ||
| 185 | points => \@arrowhead_points, | ||
| 186 | aa => 0, | ||
| 187 | color => $_->{fontcolor}, | ||
| 188 | ); | ||
| 189 | |||
| 190 | $i->string( | ||
| 191 | text => $_->{label}, | ||
| 192 | font => $_->{font}, | ||
| 193 | x => $_->{lx}, | ||
| 194 | y => $_->{ly} + $_->{label_metrics}->{b_offs}, | ||
| 195 | color => $_->{fontcolor}, | ||
| 196 | align => 1, | ||
| 197 | aa => 1, | ||
| 198 | ); | ||
| 199 | #$i->line( | ||
| 200 | # x1 => $_->{lx}, | ||
| 201 | # y1 => $_->{ly}, | ||
| 202 | # x2 => $_->{lx} + 10, | ||
| 203 | # y2 => $_->{ly}, | ||
| 204 | # aa => 0, | ||
| 205 | # color => "#000000", | ||
| 206 | #); | ||
| 207 | #$i->line( | ||
| 208 | # x1 => $_->{lx}, | ||
| 209 | # y1 => $_->{ly} + $_->{label_metrics}->{b_offs}, | ||
| 210 | # x2 => $_->{lx} + 10, | ||
| 211 | # y2 => $_->{ly} + $_->{label_metrics}->{b_offs}, | ||
| 212 | # aa => 0, | ||
| 213 | # color => "#000000", | ||
| 214 | #); | ||
| 215 | } | ||
| 216 | |||
| 217 | return $i; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub render_to_file { | ||
| 221 | my $self = shift; | ||
| 222 | my $options = @_ > 2 && ref $_[-1] eq "HASH" ? pop : {}; | ||
| 223 | my ( $sequence, $fn, $type ) = @_; | ||
| 224 | |||
| 225 | my $i = $self->_render( $sequence, $options ); | ||
| 226 | $i->write( file => $fn, defined $type ? ( type => $type ) : () ) | ||
| 227 | or die $i->errstr, ": $fn\n"; | ||
| 228 | } | ||
| 229 | |||
| 230 | =item render | ||
| 231 | |||
| 232 | $renderer->render( $s, "png" ); | ||
| 233 | |||
| 234 | Given a layed out sequence, render it and return it. | ||
| 235 | |||
| 236 | Lays out image if not layed out. | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | sub render { | ||
| 241 | my $self = shift; | ||
| 242 | my $options = @_ > 1 && ref $_[-1] eq "HASH" ? pop : {}; | ||
| 243 | my ( $sequence, $type ) = @_; | ||
| 244 | |||
| 245 | my $i = $self->_render( $sequence, $options ); | ||
| 246 | $i->write( data => \(my $data), type => $type ) | ||
| 247 | or die $i->errstr, " rendering $type image\n"; | ||
| 248 | return \$data; | ||
| 249 | } | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =head2 Methods called by the layout engine | ||
| 254 | |||
| 255 | =over | ||
| 256 | |||
| 257 | =cut | ||
| 258 | |||
| 259 | =item string_metrics | ||
| 260 | |||
| 261 | Returns the bounding box for a string: | ||
| 262 | |||
| 263 | { | ||
| 264 | w => $width, | ||
| 265 | h => $height, | ||
| 266 | xoffs => $x_offset, | ||
| 267 | yoffs => $y_offset, | ||
| 268 | font => $font_handle, | ||
| 269 | } | ||
| 270 | |||
| 271 | The height is normalized for the entire font so that the strings | ||
| 272 | will always line up. | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub string_metrics { | ||
| 277 | my $self = shift; | ||
| 278 | my ( %options ) = @_; | ||
| 279 | |||
| 280 | my $font = $options{Font}; | ||
| 281 | $font ||= $self->_default_font; | ||
| 282 | |||
| 283 | my ( | ||
| 284 | $neg_width, | ||
| 285 | $global_descent, | ||
| 286 | $pos_width, | ||
| 287 | $global_ascent, | ||
| 288 | $descent, | ||
| 289 | $ascent, | ||
| 290 | ) = $font->bounding_box( string => $options{string} ); | ||
| 291 | |||
| 292 | ## Fudge this. | ||
| 293 | $global_ascent -= 2; | ||
| 294 | |||
| 295 | my $s = { | ||
| 296 | w => $pos_width - $neg_width, | ||
| 297 | h => $global_ascent - $global_descent, | ||
| 298 | b_offs => $global_ascent, | ||
| 299 | x_offs => $neg_width, | ||
| 300 | y_offs => 0, | ||
| 301 | font => $font, | ||
| 302 | color => Imager::Color->new( | ||
| 303 | defined $options{color} ? $options{color} : '#000000' | ||
| 304 | ), | ||
| 305 | }; | ||
| 306 | |||
| 307 | return $s; | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | sub _default_font { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | ## TODO: implement mulitplatform and font path searching | ||
| 315 | $self->{_DefaultFont} ||= do { | ||
| 316 | my $fontfile = "verdana.ttf"; | ||
| 317 | # file => "/usr/share/fonts/default/Type1/c059013l.pfb", | ||
| 318 | my $font = Imager::Font->new( | ||
| 319 | file => $fontfile, | ||
| 320 | size => 14, | ||
| 321 | color => "#000000", | ||
| 322 | aa => 1, | ||
| 323 | ); | ||
| 324 | die "Unable to load font '$fontfile'\n" | ||
| 325 | unless defined $font; | ||
| 326 | $font; | ||
| 327 | }; | ||
| 328 | } | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =head1 LIMITATIONS | ||
| 333 | |||
| 334 | =head1 COPYRIGHT | ||
| 335 | |||
| 336 | Copyright 2002, R. Barrie Slaymaker, Jr., All Rights Reserved | ||
| 337 | |||
| 338 | =head1 LICENSE | ||
| 339 | |||
| 340 | You may use this module under the terms of the BSD, Artistic, oir GPL licenses, | ||
| 341 | any version. | ||
| 342 | |||
| 343 | =head1 AUTHOR | ||
| 344 | |||
| 345 | Barrie Slaymaker <barries@slaysys.com> | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | 1; |