From: ian Date: Fri, 30 Jan 2004 23:29:15 +0000 (+0000) Subject: bbox option (-b) X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=aa06424ea15cabcbed4464d96d7c3c3aade14ef2;p=trains.git bbox option (-b) --- diff --git a/layout/layout b/layout/layout index 1eb8c3a..3b44d8e 100755 --- a/layout/layout +++ b/layout/layout @@ -177,6 +177,19 @@ sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); } sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); } +sub bbox ($) { + my ($objhash) = @_; + my ($min_x, $max_x, $min_y, $max_y); + my ($loc); + foreach $loc (values %$objhash) { + upd_min(\$min_x, $loc->{X} - abs($psu_allwidth * sin($loc->{A}))); + upd_max(\$max_x, $loc->{X} + abs($psu_allwidth * sin($loc->{A}))); + upd_min(\$min_y, $loc->{Y} - abs($psu_allwidth * cos($loc->{A}))); + upd_max(\$max_y, $loc->{Y} + abs($psu_allwidth * cos($loc->{A}))); + } + return ($min_x, $max_x, $min_y, $max_y); +} + our %units_len= qw(- mm mm 1 cm 10 m 1000); our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360; @@ -799,13 +812,7 @@ sub cmd_showlibrary { } next unless $got; $obj= $objs{$obj_id}; - $max_x=$max_y=$min_x=$min_y=undef; - foreach $loc (values %{ $obj->{Loc} }) { - upd_min(\$min_x, $loc->{X} - abs($psu_allwidth * sin($loc->{A}))); - upd_max(\$max_x, $loc->{X} + abs($psu_allwidth * sin($loc->{A}))); - upd_min(\$min_y, $loc->{Y} - abs($psu_allwidth * cos($loc->{A}))); - upd_max(\$max_y, $loc->{Y} + abs($psu_allwidth * cos($loc->{A}))); - } + ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc}); newctx(); for (;;) { @@ -882,6 +889,8 @@ our %chdraw_emap= qw(A ARSc $ctx->{Draw}= uc $drawers; +our $bbox=0; + while (@ARGV && $ARGV[0] =~ m/^\-/) { last if $ARGV[0] eq '-'; $_= shift @ARGV; @@ -890,6 +899,7 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) { while (length) { if (s/^D(\d+)//) { $debug= $1; } elsif (s/^D//) { $debug++; } + elsif (s/^b//) { $bbox=1; } elsif (s/^([Ee])([a-zA-Z]+)//) { my ($ee,$c); $ee= $1; @@ -927,3 +937,16 @@ while (<>) { } oflushpage(); + +if ($bbox) { + my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc}); + if (defined $min_x) { + printf(STDERR + "width %.2d (%.2d..%2.d)\n". + "height %.2d (%.2d..%2.d)\n", + $max_x - $min_x, $min_x, $max_x, + $max_y - $min_y, $min_y, $max_y); + } else { + print STDERR "no points, bbox\n"; + } +}