8 our $ptscale= 72/25.4 / $scale;
13 our $psu_ticksperu= 1;
14 our $psu_ticklen= 5.0;
16 our $psu_sleeperlen= 17;
17 our $psu_sleeperlw= 15;
23 our $lmu_txtboxtxty= $lmu_marktpt * 0.300;
24 our $lmu_txtboxh= $lmu_marktpt * 1.100;
25 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
26 our $lmu_txtboxoff= $lmu_marklw / 2;
29 our $olu_left= 10 * $scale;
30 our $olu_right= 217 * $scale - $olu_left;
31 our $olu_bottom= 20 * $scale;
32 our $olu_top= 270 * $scale - $olu_bottom;
35 our $olu_textheight= 15;
36 our $olu_textallowperc= $lmu_marktpt * 5.0/11;
39 our $output_layer= '*';
43 return 27 unless defined $radius;
44 $radius= abs($radius);
45 return ($radius >= 450 ? 33 :
49 sub allwidth ($) { return allwidth2($_[0]) * 0.5; }
51 our $allwidthmax= allwidth(0);
52 our $allwidthmin= allwidth(undef);
55 # $ctx->{CmdLog}= undef } not in defobj
56 # $ctx->{CmdLog}[]= [ command args ] } in defobj
57 # $ctx->{LocsMade}[]{Id}= $id
58 # $ctx->{LocsMade}[]{Neg}= $id
62 # $ctx->{Trans}{X} # transformation. is ev representing
63 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
64 # $ctx->{Trans}{A} # not at plot-time)
65 # $ctx->{Trans}{R} # but multiply all y coords by this!
66 # $ctx->{Draw} # sequence of one or more chrs from uc $drawers
67 # # or X meaning never draw anything (eg in defobj)
68 # $ctx->{Layer}{Level}
73 # $objs{$id}{Part} # 1 iff object is a part
75 # $eopts[]{GlobRe} # regexp for K
76 # $eopts[]{LayerCheck} # =$fn where &$fn($l) is true iff layer matches
77 # $eopts[]{DrawMods} # modifier chars for drawing
82 our @al; # current cmd
87 our $param; # for parametric_curve
92 # Operate on Enhanced Vectors which are a location (coordinates) and a
93 # direction at that location. Representation is a hash with members X
94 # Y and A (angle of the direction in radians, anticlockwise from
95 # East). May be absolute, or interpreted as relative, according to
98 # Each function's first argument is a hashref whose X Y A members will
99 # be created or overwritten; this hashref will be returned (so you can
100 # use it `functionally' by passing {}). The other arguments may be ev
101 # hashrefs, or other info. The results are in general undefined if
102 # one of the arguments is the same hash as the result.
104 sub ev_byang ($$;$) {
105 # ev_byang(R, ANG,[LEN])
106 # result is evec of specified angle and length (default=1.0)
107 my ($res,$ang,$len)=@_;
108 $len=1.0 unless defined $len;
109 $res->{X}= $len * cos($ang);
110 $res->{Y}= $len * sin($ang);
114 sub ev_compose ($$$) {
115 # ev_compose(SUM_R, A,B);
116 # appends B to A, result is end of new B
117 # (B's X is forwards from end of A, Y is translating left from end of A)
118 # A may have a member R, which if provided then it should be 1.0 or -1.0,
119 # and B's Y and A will be multiplied by R first (ie, we can reflect);
120 my ($sum,$a,$b) = @_;
122 $r= defined $a->{R} ? $a->{R} : 1.0;
123 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
124 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
125 $sum->{A}= $a->{A} + $r * $b->{A};
128 sub ev_decompose ($$$) {
129 # ev_decompose(B_R, A,SUM)
130 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
133 $r= defined $a->{R} ? $a->{R} : 1.0;
134 $brx= $sum->{X} - $a->{X};
135 $bry= $r * ($sum->{Y} - $a->{Y});
136 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
137 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
138 $b->{A}= $r * ($sum->{A} - $a->{A});
141 sub ev_lincomb ($$$$) {
142 # ev_linkcomb(RES,A,B,P)
143 # gives P*A + (1-P)*B
144 my ($r,$a,$b,$p) = @_;
146 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
149 sub a_normalise ($$) {
151 # adds or subtracts 2*$pi to/from A until it is in [ Z , Z+2*$pi >
154 $r= $z + fmod($a - $z, 2.0*$pi);
155 $r += 2*$pi if $r < $z;
158 sub ev_bearing ($$) {
160 # returns bearing of B from A
161 # value returned is in [ A->{A}, A->{A} + 2*$pi >
162 # A->{A} and B->{A} are otherwise ignored
165 $r= atan2($b->{Y} - $a->{Y},
167 $r= a_normalise($r,$a->{A});
172 # returns distance from A to B
173 # A->{A} and B->{A} are ignored
176 $xd= $b->{X} - $a->{X};
177 $yd= $b->{Y} - $a->{Y};
178 return sqrt($xd*$xd + $yd*$yd);
183 $$limr= $now unless defined $$limr && $$limr <= $now;
187 $$limr= $now unless defined $$limr && $$limr >= $now;
191 my ($converter,$defaulter)=@_;
193 return &$defaulter unless @al;
195 $v= &$converter($spec);
196 dv('canf ','$spec',$spec, '$v',$v);
199 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
200 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
202 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
206 my ($min_x, $max_x, $min_y, $max_y);
208 foreach $loc (values %$objhash) {
209 upd_min(\$min_x, $loc->{X} - abs($allwidthmax * sin($loc->{A})));
210 upd_max(\$max_x, $loc->{X} + abs($allwidthmax * sin($loc->{A})));
211 upd_min(\$min_y, $loc->{Y} - abs($allwidthmax * cos($loc->{A})));
212 upd_max(\$max_y, $loc->{Y} + abs($allwidthmax * cos($loc->{A})));
214 return ($min_x, $max_x, $min_y, $max_y);
217 our %units_len= qw(- mm mm 1 cm 10 m 1000);
218 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
220 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
221 sub cva_identity ($) { my ($sp)=@_; $sp; }
222 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
223 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
227 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
228 or die "lexically invalid quantity";
230 $u=$ua->{'-'} unless length $u;
231 defined $ua->{$u} or die "unknown unit $u";
233 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
238 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
243 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
244 if ($sp =~ s/^(\^?)(\w+)\!//) {
247 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
248 $locs= $objs{$obj_id}{Loc};
253 $neg= $sp =~ s/^\-//;
255 die "unknown $id" unless defined $locs->{$id};
258 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
259 printf DEBUG "%s\n", $d;
261 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
265 $na= a_normalise($na,0);
266 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
273 $neg = $sp =~ s/^\-//;
275 die "duplicate $id" if exists $ctx->{Loc}{$id};
276 exists $ctx->{Loc}{$id}{X};
277 push @{ $ctx->{LocsMade} }, { Id => $id, Neg => $neg };
278 return $ctx->{Loc}{$id};
280 sub cva_cmd ($) { return cva_idstr($_[0]); }
283 return $sp if grep { $_ eq $sp } @$el;
284 die "invalid option (permitted: @$el)";
286 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
290 $nl= can(\&cva_idnew);
291 $i->{X}= can(\&cva_len);
292 $i->{Y}= can(\&cva_len);
293 $i->{A}= can(\&cva_ang);
294 ev_compose($nl, $ctx->{Trans}, $i);
297 my ($from,$to,$len,$right,$turn);
298 $from= can(\&cva_idex);
299 $to= can(\&cva_idnew);
300 $len= cano(\&cva_len,0);
301 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
302 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
303 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
304 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
309 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
313 return 'undef' if !defined $v;
314 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
315 return $v if $v =~ m/^[0-9.]+/;
316 $v =~ s/[\\\']/\\$&/g;
320 sub dv1_kind ($$$$$$$) {
321 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
323 return 0 if $ref ne $ref_exp;
325 foreach $ix (&$ixesfn) {
327 my ($v)= &$ixmapfn($ix);
328 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
329 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
332 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
337 return 0 unless $debug;
338 my ($pfx,$expr,$v) = @_;
341 #print STDERR "dv1 >$pfx|$ref<\n";
343 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
345 } elsif ($ref eq 'SCALAR') {
346 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
349 $expr.='->' unless $expr =~ m/\]$|\}$/;
350 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
351 sub { ($[ .. $#$v) },
352 sub { $v->[$_[0]] });
353 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
354 sub { sort keys %$v },
355 sub { $v->{$_[0]} });
356 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
368 sub o ($) { $o .= $_[0]; }
369 sub ol ($) { $ol .= $_[0]; }
371 print $o, $ol, " showpage\n"
378 sub o_path_begin () {
380 $o_path_verb= 'moveto';
382 sub o_path_point ($) {
384 o(" $pt $o_path_verb\n");
385 $o_path_verb= 'lineto';
387 sub o_path_stroke ($) {
389 o(" $width setlinewidth stroke\n");
393 my ($a,$b,$width)=@_;
397 o_path_stroke($width);
400 sub psu_coords ($$$) {
401 my ($ends,$inunit,$across)=@_;
402 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
403 # $across in mm, +ve to right.
404 my (%ea_zo, $zo, $prop);
405 $ea_zo{X}=$ea_zo{Y}=0;
406 foreach $zo (qw(0 1)) {
407 $prop= $zo ? $inunit : (1.0 - $inunit);
408 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
409 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
411 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
412 # '\\%ea_zo', \%ea_zo);
413 return $ea_zo{X}." ".$ea_zo{Y};
416 sub parametric__o_pt ($) {
418 o_path_point("$pt->{X} $pt->{Y}");
421 sub parametric_segment ($$$$$) {
422 my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
423 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
424 # $lenperp is the length of one unit p, ie the curve
425 # must have a uniform `density' in parameter space
426 # $calcfn is invoked with $p set and should return a loc
427 # (ie, ref to X =>, Y =>, A =>).
428 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
429 return unless $ctx->{Draw} =~ m/[ARSC]/;
430 $ppu= $psu_ulen/$lenperp;
431 $allwidth= allwidth($minradius);
432 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
433 my ($tickend)=($allwidth - $psu_ticklen);
434 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
435 my ($sleeperctr)=($psu_ulen*0.5);
436 my ($sleeperend)=($psu_sleeperlen*0.5);
437 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
441 o(" $psu_thinlw setlinewidth\n");
443 for ($param=$p0; $param<$p1; $param += $ppu) {
444 parametric__o_pt(&$calcfn);
447 parametric__o_pt(&$calcfn);
450 return unless $draw =~ m/[ARS]/;
451 for ($pa= $p0; $pa<$p1; $pa=$pb) {
453 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
454 $param= $pb; $ends[1]= &$calcfn;
455 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
456 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
457 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
460 o_path_point(psu_coords(\@ends,0,-$allwidth));
461 o_path_point(psu_coords(\@ends,0,$allwidth));
462 o_path_point(psu_coords(\@ends,$e,$allwidth));
463 o_path_point(psu_coords(\@ends,$e,-$allwidth));
464 o(" closepath clip\n");
465 foreach $side qw(-1 1) {
467 o_line(psu_coords(\@ends,0,$side*$railctr),
468 psu_coords(\@ends,1.5,$side*$railctr),
473 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
474 psu_coords(\@ends,$sleeperctr,+$sleeperend),
479 foreach $side qw(-1 1) {
480 o_line(psu_coords(\@ends,0,$side*$allwidth),
481 psu_coords(\@ends,1.5,$side*$allwidth),
483 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
484 o_line(psu_coords(\@ends,$tick,$side*$allwidth),
485 psu_coords(\@ends,$tick,$side*$tickend),
495 my ($to, $ctr,$from, $radius,$delta) = @_;
496 # does parametric_segment to draw an arc centred on $ctr
497 # ($ctr->{A} ignored)
498 # from $from with radius $radius (this must be consistent!)
499 # and directionally-subtending an angle $delta.
500 # sets $to->... to be the other end, and returns $to
502 $to->{A}= $beta= $from->{A} + $delta;
503 $to->{X}= $ctr->{X} - $radius * sin($beta);
504 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
505 return if abs($delta*$radius) < 1e-9;
506 parametric_segment(0.0,1.0, abs($radius*$delta), $radius, sub {
507 my ($beta) = $from->{A} + $delta * $param;
508 return { X => $ctr->{X} - $radius * sin($beta),
509 Y => $ctr->{Y} + $radius * cos($beta),
515 my ($from,$to,$how,$minradius);
516 $from= can(\&cva_idex);
517 $to= can(\&cva_idex);
518 $minradius= can(\&cva_len);
519 my (@paths,@solkinds);
520 o("% join ".loc2dbg($from)."..".loc2dbg($to)." $minradius\n");
522 # two circular arcs of equal maximum possible radius
523 # algorithm courtesy of Simon Tatham (`Railway problem',
524 # pers.comm. to ijackson@chiark 23.1.2004)
525 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
526 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
527 $sigma= ev_bearing($from,$to);
528 $distfact= v_dist($from,$to);
529 $theta= 0.5 * $pi - ($from->{A} - $sigma);
530 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
531 $a= 2 * (1 + cos($theta - $phi));
532 $b= 2 * (cos($theta) - cos($phi));
534 $d= sqrt($b*$b - 4*$a*$c);
535 o("% twoarcs theta=".ang2deg($theta)." phi=".ang2deg($phi).
536 " ${a}r^2 + ${b}r + ${c} = 0\n");
537 foreach $m (qw(-1 1)) {
539 o("% twoarcs $m insoluble\n");
542 $r= -0.5 * (-$b + $m*$d) / $a;
543 $radius= -$r * $distfact;
544 o("% twoarcs $m radius $radius ");
545 if (abs($radius) < $minradius) { o("too-small"); next; }
546 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
547 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
548 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
549 $reverse= signum($r);
554 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
555 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
556 o("ok deltas ".ang2deg($delta1)." ".ang2deg($delta2)."\n");
562 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
563 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
565 push @solkinds, 'twoarcs';
568 if ($minradius<=1e-6) {
569 o("% arcsline no-radius\n");
571 # two circular arcs of specified radius in same direction
572 # with an intervening straight
573 my ($lr, $c_cd,$d_cd,$t,$k,$l, $path);
574 foreach $lr (qw(-1 +1)) {
575 $c_cd= ev_compose({}, $from, { X=>0, Y=>-$lr*$minradius, A=>0 });
576 $d_cd= ev_compose({}, $to, { X=>0, Y=>-$lr*$minradius, A=>$pi });
577 $t= v_dist($c_cd,$d_cd);
578 o("% arcsline $lr t=$t ");
579 if ($t < 1e-6) { o("concentric"); next; }
580 $c_cd->{A}= $d_cd->{A}= ev_bearing($c_cd,$d_cd);
581 o("bearing ".ang2deg($c_cd->{A})."\n");
582 $k= ev_compose({}, $c_cd,
583 { X=>0, Y=>$lr*$minradius, A=>0 });
584 $l= ev_compose({}, $d_cd,
585 { X=>0, Y=>$lr*$minradius, A=>0 });
586 $path= [{ T=>Arc, F=>$from, C=>$c_cd,
588 D=> -$lr*a_normalise($lr * ($from->{A} - $k->{A}), 0) },
589 { T=>Line, A=>$k, B=>$l, L=>$t },
590 { T=>Arc, F=>$l, C=>$d_cd,
592 D=> -$lr*a_normalise(-$lr * ($to->{A} - $l->{A}), 0) }];
594 push @solkinds, 'arcsline';
597 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
598 my ($crit,$cs,$i,$cmp);
599 foreach $path (@paths) {
600 $sk= shift @solkinds;
601 o("% possible path $sk $path\n");
604 foreach $segment (@$path) {
605 if ($segment->{T} eq Arc) {
606 o("% Arc C ".loc2dbg($segment->{C}).
607 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
608 $len += abs($segment->{R} * $segment->{D});
609 push @bends, -abs($segment->{R}) * $segment->{D}; # right +ve
610 } elsif ($segment->{T} eq Line) {
611 o("% Line A ".loc2dbg($segment->{A}).
612 " B ".loc2dbg($segment->{A})." L $segment->{L}\n");
613 $len += abs($segment->{L});
615 die "unknown segment $segment->{T}";
618 o("% length $len bends @bends.\n");
620 foreach $crit (@al, 'short') {
621 if ($crit eq 'long') { $cs= $len; }
622 elsif ($crit eq 'short') { $cs= -$len; }
623 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
624 if ($1 eq 'begin') { $cs= $bends[0]; }
625 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
626 else { $cs=0; map { $cs += $_ } @bends; }
627 $cs= -$cs if $2 eq 'left';
628 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
629 $cs= ($2 eq $sk) != ($1 eq '!');
631 die "unknown sort criterion $crit";
635 o("% scores @$scores\n");
636 if (defined $bestpath) {
637 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
638 $cmp= $scores->[$i] <=> $bestscores->[$i];
643 $bestscores= $scores;
645 die "no solution" unless defined $bestpath;
646 o("% chose path $bestpath @al\n");
648 foreach $segment (@$bestpath) {
649 if ($segment->{T} eq 'Arc') {
650 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
651 } elsif ($segment->{T} eq 'Line') {
652 line($segment->{A}, $segment->{B}, $segment->{L});
654 die "unknown segment";
660 my ($from,$to,$len) = @_;
661 parametric_segment(0.0, 1.0, abs($len), undef, sub {
662 ev_lincomb({}, $from, $to, $param);
667 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
668 $from= can(\&cva_idex);
669 $to= can(\&cva_idnew);
670 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
671 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
672 if ($how eq 'len') { $len= can(\&cva_len); }
673 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
674 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
675 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
676 if ($radius eq 'Inf') {
677 # print DEBUG "extend inf $len\n";
678 if ($how eq 'upto') {
679 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
680 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
681 } elsif ($how eq 'len') {
683 die "len of straight spec by angle";
685 printf DEBUG "len $len\n";
686 $to->{X}= $from->{X} + $len * cos($from->{A});
687 $to->{Y}= $from->{Y} + $len * sin($from->{A});
688 $to->{A}= $from->{A};
689 line($from,$to,$len);
691 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
692 print DEBUG "radius >$radius<\n";
693 $radius *= $ctx->{Trans}{R};
694 $sign_r= signum($radius);
696 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
697 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
698 if ($how eq 'upto') {
699 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
700 $sign_r * ($upto->{Y} - $ctr->{Y}));
702 } elsif ($how eq 'parallel') {
705 } elsif ($how eq 'uptoang') {
706 $beta= input_absang($ang);
708 } elsif ($how eq 'len') {
709 $sign_ang= signum($len);
710 $beta= $from->{A} - $sign_r * $len / abs($radius);
713 $sign_ang= signum($ang);
714 $beta= $from->{A} - $sign_r * $ang;
717 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
718 $beta += $sign_ang * $sign_r * 4.0 * $pi;
720 $delta= $beta - $from->{A};
721 last if $sign_ang * $sign_r * $delta <= 0;
722 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
724 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
725 arc($to, ,$ctr,$from, $radius,$delta);
727 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
732 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
735 return $_[0] * 180 / $pi;
737 sub input_absang ($) {
738 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
740 sub input_abscoords ($$) {
742 ($in->{X}, $in->{Y}) = @_;
744 $out= ev_compose({}, $ctx->{Trans}, $in);
745 return ($out->{X}, $out->{Y});
751 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
754 %{ $ctx->{Layer} }= %{ $ctx_save->{Layer} }
755 if defined $ctx_save;
761 sub cmd_defobj { cmd__defobj(0); }
762 sub cmd_defpart { cmd__defobj(1); }
763 sub cmd__defobj ($) {
766 $id= can(\&cva_idstr);
767 die "nested defobj" if $defobj_save;
768 die "repeated defobj" if exists $objs{$id};
770 $defobj_ispart= $ispart;
771 newctx($defobj_save);
773 $ctx->{InDefObj}= $id;
775 $ctx->{Layer}= { Level => 5, Kind => '' };
780 $id= $ctx->{InDefObj};
781 die "unmatched enddef" unless defined $id;
782 foreach $bit (qw(CmdLog Loc)) {
783 $objs{$id}{$bit}= $ctx->{$bit};
785 $objs{$id}{Part}= $defobj_ispart;
788 $defobj_ispart= undef;
791 sub cmd__runobj ($) {
795 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
796 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
798 next if $al[0] eq 'enddef';
804 my ($kl, $k,$l, $eo,$cc);
805 $kl= can(\&cva_identity);
806 $kl =~ m/^([A-Za-z_]*)(\d*|\=)$/ or die "invalid layer spec";
808 $l= $ctx->{Layer}{Level} if $l =~ m/^\=?$/;
809 $ctx->{Layer}{Kind}= $l;
810 $ctx->{Layer}{Level}= $l;
811 return if $ctx->{Draw} =~ m/X/;
812 if ($output_layer ne '*' && $l != $output_layer) {
815 $ctx->{Draw}= 'RLMN';
816 } elsif ($k eq 's') {
818 } elsif ($k eq 'l') {
819 $ctx->{Draw}= 'CLMN';
821 $ctx->{Draw}= 'ARSCLMNO';
823 foreach $eo (@eopts) {
824 next unless $k =~ m/^$eo->{GlobRe}$/;
825 next unless &{ $eo->{LayerCheck} }($l);
826 foreach $cc (split //, $eo->{DrawMods}) {
827 $ctx->{Draw} =~ s/$cc//ig;
828 $ctx->{Draw} .= $cc if $cc =~ m/[A-Z]/;
833 sub cmd_part { cmd__obj(Part); }
834 sub cmd_obj { cmd__obj(1); }
835 sub cmd_objflip { cmd__obj(-1); }
839 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
840 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
842 $part_name= can(\&cva_idstr);
843 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
845 $obj_id= can(\&cva_idstr);
846 if (defined $part_name) {
847 $formal_id= can(\&cva_idstr);
848 $actual= cano(\&cva_idex, undef);
849 if (!defined $actual) {
850 $actual= cva_idex("${part_name}_${formal_id}");
853 $actual= can(\&cva_idex);
854 $formal_id= can(\&cva_idstr);
856 $obj= $objs{$obj_id};
857 dv("cmd__obj ",'$obj',$obj);
858 die "unknown obj $obj_id" unless $obj;
859 $formal= $obj->{Loc}{$formal_id};
860 die "unknown formal $formal_id" unless $formal;
863 $how *= $ctx_save->{Trans}{R};
864 $ctx->{Trans}{R}= $how;
865 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
866 $formcv= ev_compose({}, $ctx->{Trans},$formal);
867 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
868 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
869 if (defined $part_name) {
870 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
872 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
874 $ctx->{Draw}= $ctx_save->{Draw};
876 $ctx->{Draw} =~ s/[LMN]//g;
877 $ctx->{Draw} =~ s/O/MNO/;
879 $ctx->{Draw} =~ s/[LM]//g;
880 $ctx->{Draw} =~ s/N/MN/;
882 cmd__runobj($obj_id);
883 if (defined $part_name) {
884 $pfx= $part_name.'_';
886 if (@al && $al[0] eq '=') {
889 $pfx= cano(\&cva_idstr,undef);
895 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
896 next if $id eq $formal_id;
898 next if exists $ctx_save->{Loc}{$newid};
899 $newpt= cva_idnew($newid);
900 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
903 if (defined $part_name) {
904 my ($formalr_id, $actualr_id, $formalr, $actualr);
906 die "part results come in pairs\n" unless @al>=2;
907 ($formalr_id, $actualr_id, @al) = @al;
908 if ($actualr_id =~ s/^\-//) {
909 $formalr_id= "-$formalr_id";
910 $formalr_id =~ s/^\-\-//;
913 local ($ctx) = $ctx_inobj;
914 $formalr= cva_idex($formalr_id);
916 $actualr= cva_idnew($actualr_id);
917 %$actualr= %$formalr;
924 dv("cmd__do $ctx @al ",'$ctx',$ctx);
925 $cmd= can(\&cva_cmd);
926 my ($lm,$id,$loc,$io,$ad);
927 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
928 o("%L cmd $io $cmd @al\n");
929 $ctx->{LocsMade}= [ ];
934 die "too many args" if @al;
935 foreach $lm (@{ $ctx->{LocsMade} }) {
937 $loc= $ctx->{Loc}{$id};
938 $loc->{A} += $pi if $lm->{Neg};
939 $ad= ang2deg($loc->{A});
940 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
941 if ($ctx->{Draw} =~ m/[LM]/) {
943 " $loc->{X} $loc->{Y} translate $ad rotate\n");
944 if ($ctx->{Draw} =~ m/M/) {
945 ol(" 0 $allwidthmin newpath moveto\n".
946 " 0 -$allwidthmin lineto\n".
947 " $lmu_marklw setlinewidth stroke\n");
949 if ($ctx->{Draw} =~ m/L/) {
950 ol(" /s ($id) def\n".
952 " /sx5 s stringwidth pop\n".
953 " 0.5 mul $lmu_txtboxpadx add def\n".
954 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
955 " sx5 neg 0 moveto\n".
956 " sx5 neg $lmu_txtboxh lineto\n".
957 " sx5 $lmu_txtboxh lineto\n".
958 " sx5 0 lineto closepath\n".
959 " gsave 1 setgray fill grestore\n".
960 " $lmu_txtboxlw setlinewidth stroke\n".
961 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
969 sub cmd_showlibrary {
970 my ($obj_id, $y, $x, $ctx_save, $width, $height);
971 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
973 $x=$olu_left; $y=$olu_bottom; undef $nxty;
975 foreach $obj_id (sort keys %objs) {
977 foreach $glob (@al) {
979 $got= !($pat =~ s/^\!//);
980 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
981 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
982 last if $obj_id =~ m/^$pat$/;
986 $obj= $objs{$obj_id};
987 next unless $obj->{Part};
988 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
992 $width= $max_x - $min_x;
993 $height= $max_y - $min_y;
994 if ($width < $height) {
996 $ctx->{Trans}{X}= $x - $min_x;
997 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
999 ($width,$height)=($height,$width);
1000 $ctx->{Trans}{A}= 0.5 * $pi;
1001 $ctx->{Trans}{X}= $x + $max_y;
1002 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
1004 $adj= length($obj_id) * $olu_textallowperc - $width;
1007 $ctx->{Trans}{X} += 0.5 * $adj;
1008 if ($x + $width > $olu_right && defined $nxty) {
1012 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
1014 $x= $olu_left; $y= $olu_bottom;
1021 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
1022 $ctx->{Draw}= $ctx_save->{Draw};
1023 cmd__runobj($obj_id);
1025 " /s ($obj_id) def\n".
1027 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
1028 " s stringwidth pop -0.5 mul 0 rmoveto\n".
1029 " s show grestore\n");
1030 $x += $width + $olu_gap_x;
1031 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
1043 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
1044 " 615 0 translate 90 rotate\n".
1045 " $ptscale $ptscale scale\n"
1050 our $drawers= 'arsclmno';
1051 our %chdraw_emap= qw(A ARSc
1065 while (@ARGV && $ARGV[0] =~ m/^\-/) {
1066 last if $ARGV[0] eq '-';
1071 if (s/^D(\d+)//) { $debug= $1; }
1072 elsif (s/^D//) { $debug++; }
1073 elsif (s/^q//) { $quiet=1; }
1075 ((?:[a-z]|\*|\?|\[[a-z][-a-z]*\])*?)
1076 (\~?) (\d*) (\=*|\-+|\++) (\d*)
1078 my ($ee,$g,$n,$d,$c,$v,$cc) = ($1,$2,$3,$4,$5,$6,$7);
1079 my ($eo, $invert, $lfn, $ccc, $sense,$limit);
1080 $g =~ s/[?*]/\\$&/g;
1081 $d= $output_layer if !length $d;
1084 $c= '=' if !length $c;
1086 die '-[eE]GN[D]CCV not allowed' if length $c > 1;
1089 if ($c =~ m/^[-+]/) {
1090 $sense= ($c.'1') + 0;
1091 $limit= ($sense * $d) + length($c) - 1;
1093 ($output_layer eq '*' ? $d
1094 : $_[0]) * $sense >= $limit
1098 $limit= length($c) - 1;
1100 ($output_layer eq '*' ? 1
1101 : abs($_[0] - $d) <= $limit)
1106 foreach $c (split //, $cc) {
1108 die "bad -e option $c" unless defined $chdraw_emap{$c};
1109 $ccc .= $chdraw_emap{$c};
1111 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
1116 $eo->{LayerCheck}= $lfn;
1117 $eo->{DrawMods}= $ccc;
1120 die "unknown option -$_";
1125 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
1128 select(DEBUG); $|=1;
1129 select(STDOUT); $|=1;
1139 chomp; s/^\s+//; s/\s+$//;
1140 @al= split /\s+/, $_;
1142 print DEBUG "=== @al\n";
1143 last if $al[0] eq 'eof';
1144 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1151 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1153 if (defined $min_x) {
1154 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1155 "height %.2d (%.2d..%2.d)\n",
1156 $max_x - $min_x, $min_x, $max_x,
1157 $max_y - $min_y, $min_y, $max_y);
1159 $bboxstr= "no locs, no bbox\n";
1161 if (!$quiet) { print STDERR $bboxstr; }
1162 $bboxstr =~ s/^/\%L bbox /mg;
1163 print $bboxstr or die $!;