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 ev_bearing ($$) {
151 # returns bearing of B from A
152 # value returned is in [ A->{A}, A->{A} + 2*$pi >
153 # A->{A} and B->{A} are otherwise ignored
156 $r= atan2($b->{Y} - $a->{Y},
159 while ($r < $a->{A}) { $r += 2.0 * $pi; }
164 # returns distance from A to B
165 # A->{A} and B->{A} are ignored
168 $xd= $b->{X} - $a->{X};
169 $yd= $b->{Y} - $a->{Y};
170 return sqrt($xd*$xd + $yd*$yd);
175 $$limr= $now unless defined $$limr && $$limr <= $now;
179 $$limr= $now unless defined $$limr && $$limr >= $now;
183 my ($converter,$defaulter)=@_;
185 return &$defaulter unless @al;
187 $v= &$converter($spec);
188 dv('canf ','$spec',$spec, '$v',$v);
191 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
192 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
194 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
198 my ($min_x, $max_x, $min_y, $max_y);
200 foreach $loc (values %$objhash) {
201 upd_min(\$min_x, $loc->{X} - abs($allwidthmax * sin($loc->{A})));
202 upd_max(\$max_x, $loc->{X} + abs($allwidthmax * sin($loc->{A})));
203 upd_min(\$min_y, $loc->{Y} - abs($allwidthmax * cos($loc->{A})));
204 upd_max(\$max_y, $loc->{Y} + abs($allwidthmax * cos($loc->{A})));
206 return ($min_x, $max_x, $min_y, $max_y);
209 our %units_len= qw(- mm mm 1 cm 10 m 1000);
210 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
212 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
213 sub cva_identity ($) { my ($sp)=@_; $sp; }
214 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
215 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
219 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
220 or die "lexically invalid quantity";
222 $u=$ua->{'-'} unless length $u;
223 defined $ua->{$u} or die "unknown unit $u";
225 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
230 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
235 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
236 if ($sp =~ s/^(\^?)(\w+)\!//) {
239 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
240 $locs= $objs{$obj_id}{Loc};
245 $neg= $sp =~ s/^\-//;
247 die "unknown $id" unless defined $locs->{$id};
250 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
251 printf DEBUG "%s\n", $d;
253 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
257 $na -= 2*$pi if $na >= 2*$pi;
258 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
265 $neg = $sp =~ s/^\-//;
267 die "duplicate $id" if exists $ctx->{Loc}{$id};
268 exists $ctx->{Loc}{$id}{X};
269 push @{ $ctx->{LocsMade} }, { Id => $id, Neg => $neg };
270 return $ctx->{Loc}{$id};
272 sub cva_cmd ($) { return cva_idstr($_[0]); }
275 return $sp if grep { $_ eq $sp } @$el;
276 die "invalid option (permitted: @$el)";
278 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
282 $nl= can(\&cva_idnew);
283 $i->{X}= can(\&cva_len);
284 $i->{Y}= can(\&cva_len);
285 $i->{A}= can(\&cva_ang);
286 ev_compose($nl, $ctx->{Trans}, $i);
289 my ($from,$to,$len,$right,$turn);
290 $from= can(\&cva_idex);
291 $to= can(\&cva_idnew);
292 $len= cano(\&cva_len,0);
293 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
294 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
295 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
296 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
301 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
305 return 'undef' if !defined $v;
306 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
307 return $v if $v =~ m/^[0-9.]+/;
308 $v =~ s/[\\\']/\\$&/g;
312 sub dv1_kind ($$$$$$$) {
313 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
315 return 0 if $ref ne $ref_exp;
317 foreach $ix (&$ixesfn) {
319 my ($v)= &$ixmapfn($ix);
320 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
321 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
324 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
329 return 0 unless $debug;
330 my ($pfx,$expr,$v) = @_;
333 #print STDERR "dv1 >$pfx|$ref<\n";
335 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
337 } elsif ($ref eq 'SCALAR') {
338 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
341 $expr.='->' unless $expr =~ m/\]$|\}$/;
342 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
343 sub { ($[ .. $#$v) },
344 sub { $v->[$_[0]] });
345 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
346 sub { sort keys %$v },
347 sub { $v->{$_[0]} });
348 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
360 sub o ($) { $o .= $_[0]; }
361 sub ol ($) { $ol .= $_[0]; }
363 print $o, $ol, " showpage\n"
370 sub o_path_begin () {
372 $o_path_verb= 'moveto';
374 sub o_path_point ($) {
376 o(" $pt $o_path_verb\n");
377 $o_path_verb= 'lineto';
379 sub o_path_stroke ($) {
381 o(" $width setlinewidth stroke\n");
385 my ($a,$b,$width)=@_;
389 o_path_stroke($width);
392 sub psu_coords ($$$) {
393 my ($ends,$inunit,$across)=@_;
394 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
395 # $across in mm, +ve to right.
396 my (%ea_zo, $zo, $prop);
397 $ea_zo{X}=$ea_zo{Y}=0;
398 foreach $zo (qw(0 1)) {
399 $prop= $zo ? $inunit : (1.0 - $inunit);
400 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
401 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
403 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
404 # '\\%ea_zo', \%ea_zo);
405 return $ea_zo{X}." ".$ea_zo{Y};
408 sub parametric__o_pt ($) {
410 o_path_point("$pt->{X} $pt->{Y}");
413 sub parametric_segment ($$$$$) {
414 my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
415 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
416 # $lenperp is the length of one unit p, ie the curve
417 # must have a uniform `density' in parameter space
418 # $calcfn is invoked with $p set and should return a loc
419 # (ie, ref to X =>, Y =>, A =>).
420 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
421 return unless $ctx->{Draw} =~ m/[ARSC]/;
422 $ppu= $psu_ulen/$lenperp;
423 $allwidth= allwidth($minradius);
424 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
425 my ($tickend)=($allwidth - $psu_ticklen);
426 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
427 my ($sleeperctr)=($psu_ulen*0.5);
428 my ($sleeperend)=($psu_sleeperlen*0.5);
429 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
433 o(" $psu_thinlw setlinewidth\n");
435 for ($param=$p0; $param<$p1; $param += $ppu) {
436 parametric__o_pt(&$calcfn);
439 parametric__o_pt(&$calcfn);
442 return unless $draw =~ m/[ARS]/;
443 for ($pa= $p0; $pa<$p1; $pa=$pb) {
445 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
446 $param= $pb; $ends[1]= &$calcfn;
447 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
448 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
449 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
452 o_path_point(psu_coords(\@ends,0,-$allwidth));
453 o_path_point(psu_coords(\@ends,0,$allwidth));
454 o_path_point(psu_coords(\@ends,$e,$allwidth));
455 o_path_point(psu_coords(\@ends,$e,-$allwidth));
456 o(" closepath clip\n");
457 foreach $side qw(-1 1) {
459 o_line(psu_coords(\@ends,0,$side*$railctr),
460 psu_coords(\@ends,1.5,$side*$railctr),
465 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
466 psu_coords(\@ends,$sleeperctr,+$sleeperend),
471 foreach $side qw(-1 1) {
472 o_line(psu_coords(\@ends,0,$side*$allwidth),
473 psu_coords(\@ends,1.5,$side*$allwidth),
475 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
476 o_line(psu_coords(\@ends,$tick,$side*$allwidth),
477 psu_coords(\@ends,$tick,$side*$tickend),
487 my ($to, $ctr,$from, $radius,$delta) = @_;
488 # does parametric_segment to draw an arc centred on $ctr
489 # ($ctr->{A} ignored)
490 # from $from with radius $radius (this must be consistent!)
491 # and directionally-subtending an angle $delta.
492 # sets $to->... to be the other end, and returns $to
494 $to->{A}= $beta= $from->{A} + $delta;
495 $to->{X}= $ctr->{X} - $radius * sin($beta);
496 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
497 return if abs($delta*$radius) < 1E-9;
498 parametric_segment(0.0,1.0, abs($radius*$delta), $radius, sub {
499 my ($beta) = $from->{A} + $delta * $param;
500 return { X => $ctr->{X} - $radius * sin($beta),
501 Y => $ctr->{Y} + $radius * cos($beta),
507 my ($from,$to,$how,$minradius);
508 $from= can(\&cva_idex);
509 $to= can(\&cva_idex);
510 $minradius= can(\&cva_len);
511 my (@paths,@solkinds);
513 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
514 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
515 $sigma= ev_bearing($from,$to);
516 $distfact= v_dist($from,$to);
517 $theta= 0.5 * $pi - ($from->{A} - $sigma);
518 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
519 $a= 2 * (1 + cos($theta - $phi));
520 $b= 2 * (cos($theta) - cos($phi));
522 $d= sqrt($b*$b - 4*$a*$c);
523 foreach $m (qw(-1 1)) {
525 $r= -0.5 * (-$b + $m*$d) / $a;
526 $radius= -$r * $distfact;
527 next if abs($radius) < $minradius;
528 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
529 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
530 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
531 $reverse= signum($r);
536 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
537 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
543 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
544 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
546 push @solkinds, 'twoarcs';
549 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
550 my ($crit,$cs,$i,$cmp);
551 foreach $path (@paths) {
552 $sk= shift @solkinds;
553 o("% possible path $sk $path\n");
556 foreach $segment (@$path) {
557 if ($segment->{T} eq Arc) {
558 o("% Arc C ".loc2dbg($segment->{C}).
559 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
560 $len += abs($segment->{R} * $segment->{D});
561 push @bends, signum($segment->{R} * $segment->{D}); # right +ve
563 die "unknown segment $segment->{T}";
566 o("% length $len\n");
568 foreach $crit (@al, 'short') {
569 if ($crit eq 'long') { $cs= $len; }
570 elsif ($crit eq 'short') { $cs= -$len; }
571 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
572 if ($1 eq 'begin') { $cs= $bends[0]; }
573 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
574 else { $cs=0; map { $cs += $_ } @bends; }
575 $cs= -$cs if $2 eq 'left';
576 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
577 $cs= ($2 eq $sk) != ($1 eq '!');
581 o("% scores @$scores\n");
582 if (defined $bestpath) {
583 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
584 $cmp= $scores->[$i] <=> $bestscores->[$i];
589 $bestscores= $scores;
591 die "no solution" unless defined $bestpath;
592 o("% chose path $bestpath @al\n");
594 foreach $segment (@$bestpath) {
595 if ($segment->{T} eq 'Arc') {
596 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
598 die "unknown segment";
604 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
605 $from= can(\&cva_idex);
606 $to= can(\&cva_idnew);
607 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
608 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
609 if ($how eq 'len') { $len= can(\&cva_len); }
610 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
611 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
612 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
613 if ($radius eq 'Inf') {
614 # print DEBUG "extend inf $len\n";
615 if ($how eq 'upto') {
616 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
617 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
618 } elsif ($how eq 'len') {
620 die "len of straight spec by angle";
622 printf DEBUG "len $len\n";
623 $to->{X}= $from->{X} + $len * cos($from->{A});
624 $to->{Y}= $from->{Y} + $len * sin($from->{A});
625 $to->{A}= $from->{A};
626 parametric_segment(0.0, 1.0, abs($len), undef, sub {
627 ev_lincomb({}, $from, $to, $param);
630 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
631 print DEBUG "radius >$radius<\n";
632 $radius *= $ctx->{Trans}{R};
633 $sign_r= signum($radius);
635 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
636 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
637 if ($how eq 'upto') {
638 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
639 $sign_r * ($upto->{Y} - $ctr->{Y}));
641 } elsif ($how eq 'parallel') {
644 } elsif ($how eq 'uptoang') {
645 $beta= input_absang($ang);
647 } elsif ($how eq 'len') {
648 $sign_ang= signum($len);
649 $beta= $from->{A} - $sign_r * $len / abs($radius);
652 $sign_ang= signum($ang);
653 $beta= $from->{A} - $sign_r * $ang;
656 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
657 $beta += $sign_ang * $sign_r * 4.0 * $pi;
659 $delta= $beta - $from->{A};
660 last if $sign_ang * $sign_r * $delta <= 0;
661 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
663 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
664 arc($to, ,$ctr,$from, $radius,$delta);
666 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
671 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
674 return $_[0] * 180 / $pi;
676 sub input_absang ($) {
677 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
679 sub input_abscoords ($$) {
681 ($in->{X}, $in->{Y}) = @_;
683 $out= ev_compose({}, $ctx->{Trans}, $in);
684 return ($out->{X}, $out->{Y});
690 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
693 %{ $ctx->{Layer} }= %{ $ctx_save->{Layer} }
694 if defined $ctx_save;
700 sub cmd_defobj { cmd__defobj(0); }
701 sub cmd_defpart { cmd__defobj(1); }
702 sub cmd__defobj ($) {
705 $id= can(\&cva_idstr);
706 die "nested defobj" if $defobj_save;
707 die "repeated defobj" if exists $objs{$id};
709 $defobj_ispart= $ispart;
710 newctx($defobj_save);
712 $ctx->{InDefObj}= $id;
714 $ctx->{Layer}= { Level => 5, Kind => '' };
719 $id= $ctx->{InDefObj};
720 die "unmatched enddef" unless defined $id;
721 foreach $bit (qw(CmdLog Loc)) {
722 $objs{$id}{$bit}= $ctx->{$bit};
724 $objs{$id}{Part}= $defobj_ispart;
727 $defobj_ispart= undef;
730 sub cmd__runobj ($) {
734 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
735 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
737 next if $al[0] eq 'enddef';
743 my ($kl, $k,$l, $eo,$cc);
744 $kl= can(\&cva_identity);
745 $kl =~ m/^([A-Za-z_]*)(\d*|\=)$/ or die "invalid layer spec";
747 $l= $ctx->{Layer}{Level} if $l =~ m/^\=?$/;
748 $ctx->{Layer}{Kind}= $l;
749 $ctx->{Layer}{Level}= $l;
750 return if $ctx->{Draw} =~ m/X/;
751 if ($output_layer ne '*' && $l != $output_layer) {
754 $ctx->{Draw}= 'RLMN';
755 } elsif ($k eq 's') {
757 } elsif ($k eq 'l') {
758 $ctx->{Draw}= 'CLMN';
760 $ctx->{Draw}= 'ARSCLMNO';
762 foreach $eo (@eopts) {
763 next unless $k =~ m/^$eo->{GlobRe}$/;
764 next unless &{ $eo->{LayerCheck} }($l);
765 foreach $cc (split //, $eo->{DrawMods}) {
766 $ctx->{Draw} =~ s/$cc//ig;
767 $ctx->{Draw} .= $cc if $cc =~ m/[A-Z]/;
772 sub cmd_part { cmd__obj(Part); }
773 sub cmd_obj { cmd__obj(1); }
774 sub cmd_objflip { cmd__obj(-1); }
778 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
779 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
781 $part_name= can(\&cva_idstr);
782 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
784 $obj_id= can(\&cva_idstr);
785 if (defined $part_name) {
786 $formal_id= can(\&cva_idstr);
787 $actual= cano(\&cva_idex, undef);
788 if (!defined $actual) {
789 $actual= cva_idex("${part_name}_${formal_id}");
792 $actual= can(\&cva_idex);
793 $formal_id= can(\&cva_idstr);
795 $obj= $objs{$obj_id};
796 dv("cmd__obj ",'$obj',$obj);
797 die "unknown obj $obj_id" unless $obj;
798 $formal= $obj->{Loc}{$formal_id};
799 die "unknown formal $formal_id" unless $formal;
802 $how *= $ctx_save->{Trans}{R};
803 $ctx->{Trans}{R}= $how;
804 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
805 $formcv= ev_compose({}, $ctx->{Trans},$formal);
806 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
807 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
808 if (defined $part_name) {
809 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
811 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
813 $ctx->{Draw}= $ctx_save->{Draw};
815 $ctx->{Draw} =~ s/[LMN]//g;
816 $ctx->{Draw} =~ s/O/MNO/;
818 $ctx->{Draw} =~ s/[LM]//g;
819 $ctx->{Draw} =~ s/N/MN/;
821 cmd__runobj($obj_id);
822 if (defined $part_name) {
823 $pfx= $part_name.'_';
825 if (@al && $al[0] eq '=') {
828 $pfx= cano(\&cva_idstr,undef);
834 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
835 next if $id eq $formal_id;
837 next if exists $ctx_save->{Loc}{$newid};
838 $newpt= cva_idnew($newid);
839 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
842 if (defined $part_name) {
843 my ($formalr_id, $actualr_id, $formalr, $actualr);
845 die "part results come in pairs\n" unless @al>=2;
846 ($formalr_id, $actualr_id, @al) = @al;
847 if ($actualr_id =~ s/^\-//) {
848 $formalr_id= "-$formalr_id";
849 $formalr_id =~ s/^\-\-//;
852 local ($ctx) = $ctx_inobj;
853 $formalr= cva_idex($formalr_id);
855 $actualr= cva_idnew($actualr_id);
856 %$actualr= %$formalr;
863 dv("cmd__do $ctx @al ",'$ctx',$ctx);
864 $cmd= can(\&cva_cmd);
865 my ($lm,$id,$loc,$io,$ad);
866 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
867 o("%L cmd $io $cmd @al\n");
868 $ctx->{LocsMade}= [ ];
873 die "too many args" if @al;
874 foreach $lm (@{ $ctx->{LocsMade} }) {
876 $loc= $ctx->{Loc}{$id};
877 $loc->{A} += $pi if $lm->{Neg};
878 $ad= ang2deg($loc->{A});
879 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
880 if ($ctx->{Draw} =~ m/[LM]/) {
882 " $loc->{X} $loc->{Y} translate $ad rotate\n");
883 if ($ctx->{Draw} =~ m/M/) {
884 ol(" 0 $allwidthmin newpath moveto\n".
885 " 0 -$allwidthmin lineto\n".
886 " $lmu_marklw setlinewidth stroke\n");
888 if ($ctx->{Draw} =~ m/L/) {
889 ol(" /s ($id) def\n".
891 " /sx5 s stringwidth pop\n".
892 " 0.5 mul $lmu_txtboxpadx add def\n".
893 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
894 " sx5 neg 0 moveto\n".
895 " sx5 neg $lmu_txtboxh lineto\n".
896 " sx5 $lmu_txtboxh lineto\n".
897 " sx5 0 lineto closepath\n".
898 " gsave 1 setgray fill grestore\n".
899 " $lmu_txtboxlw setlinewidth stroke\n".
900 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
908 sub cmd_showlibrary {
909 my ($obj_id, $y, $x, $ctx_save, $width, $height);
910 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
912 $x=$olu_left; $y=$olu_bottom; undef $nxty;
914 foreach $obj_id (sort keys %objs) {
916 foreach $glob (@al) {
918 $got= !($pat =~ s/^\!//);
919 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
920 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
921 last if $obj_id =~ m/^$pat$/;
925 $obj= $objs{$obj_id};
926 next unless $obj->{Part};
927 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
931 $width= $max_x - $min_x;
932 $height= $max_y - $min_y;
933 if ($width < $height) {
935 $ctx->{Trans}{X}= $x - $min_x;
936 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
938 ($width,$height)=($height,$width);
939 $ctx->{Trans}{A}= 0.5 * $pi;
940 $ctx->{Trans}{X}= $x + $max_y;
941 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
943 $adj= length($obj_id) * $olu_textallowperc - $width;
946 $ctx->{Trans}{X} += 0.5 * $adj;
947 if ($x + $width > $olu_right && defined $nxty) {
951 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
953 $x= $olu_left; $y= $olu_bottom;
960 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
961 $ctx->{Draw}= $ctx_save->{Draw};
962 cmd__runobj($obj_id);
964 " /s ($obj_id) def\n".
966 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
967 " s stringwidth pop -0.5 mul 0 rmoveto\n".
968 " s show grestore\n");
969 $x += $width + $olu_gap_x;
970 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
982 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
983 " 615 0 translate 90 rotate\n".
984 " $ptscale $ptscale scale\n"
989 our $drawers= 'arsclmno';
990 our %chdraw_emap= qw(A ARSc
1004 while (@ARGV && $ARGV[0] =~ m/^\-/) {
1005 last if $ARGV[0] eq '-';
1010 if (s/^D(\d+)//) { $debug= $1; }
1011 elsif (s/^D//) { $debug++; }
1012 elsif (s/^q//) { $quiet=1; }
1014 ((?:[a-z]|\*|\?|\[[a-z][-a-z]*\])*?)
1015 (\~?) (\d*) (\=*|\-+|\++) (\d*)
1017 my ($ee,$g,$n,$d,$c,$v,$cc) = ($1,$2,$3,$4,$5,$6,$7);
1018 my ($eo, $invert, $lfn, $ccc, $sense,$limit);
1019 $g =~ s/[?*]/\\$&/g;
1020 $d= $output_layer if !length $d;
1023 $c= '=' if !length $c;
1025 die '-[eE]GN[D]CCV not allowed' if length $c > 1;
1028 if ($c =~ m/^[-+]/) {
1029 $sense= ($c.'1') + 0;
1030 $limit= ($sense * $d) + length($c) - 1;
1032 ($output_layer eq '*' ? $d
1033 : $_[0]) * $sense >= $limit
1037 $limit= length($c) - 1;
1039 ($output_layer eq '*' ? 1
1040 : abs($_[0] - $d) <= $limit)
1045 foreach $c (split //, $cc) {
1047 die "bad -e option $c" unless defined $chdraw_emap{$c};
1048 $ccc .= $chdraw_emap{$c};
1050 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
1055 $eo->{LayerCheck}= $lfn;
1056 $eo->{DrawMods}= $ccc;
1059 die "unknown option -$_";
1064 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
1067 select(DEBUG); $|=1;
1068 select(STDOUT); $|=1;
1078 chomp; s/^\s+//; s/\s+$//;
1079 @al= split /\s+/, $_;
1081 print DEBUG "=== @al\n";
1082 last if $al[0] eq 'eof';
1083 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1090 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1092 if (defined $min_x) {
1093 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1094 "height %.2d (%.2d..%2.d)\n",
1095 $max_x - $min_x, $min_x, $max_x,
1096 $max_y - $min_y, $min_y, $max_y);
1098 $bboxstr= "no locs, no bbox\n";
1100 if (!$quiet) { print STDERR $bboxstr; }
1101 $bboxstr =~ s/^/\%L bbox /mg;
1102 print $bboxstr or die $!;