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;
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)
71 # $objs{$id}{Part} # 1 iff object is a part
73 # $eopts[]{GlobRe} # regexp for K
74 # $eopts[]{LayerCheck} # =$fn where &$fn($l) is true iff layer matches
75 # $eopts[]{DrawMods} # modifier chars for drawing
80 our @al; # current cmd
85 our $param; # for parametric_curve
90 # Operate on Enhanced Vectors which are a location (coordinates) and a
91 # direction at that location. Representation is a hash with members X
92 # Y and A (angle of the direction in radians, anticlockwise from
93 # East). May be absolute, or interpreted as relative, according to
96 # Each function's first argument is a hashref whose X Y A members will
97 # be created or overwritten; this hashref will be returned (so you can
98 # use it `functionally' by passing {}). The other arguments may be ev
99 # hashrefs, or other info. The results are in general undefined if
100 # one of the arguments is the same hash as the result.
102 sub ev_byang ($$;$) {
103 # ev_byang(R, ANG,[LEN])
104 # result is evec of specified angle and length (default=1.0)
105 my ($res,$ang,$len)=@_;
106 $len=1.0 unless defined $len;
107 $res->{X}= $len * cos($ang);
108 $res->{Y}= $len * sin($ang);
112 sub ev_compose ($$$) {
113 # ev_compose(SUM_R, A,B);
114 # appends B to A, result is end of new B
115 # (B's X is forwards from end of A, Y is translating left from end of A)
116 # A may have a member R, which if provided then it should be 1.0 or -1.0,
117 # and B's Y and A will be multiplied by R first (ie, we can reflect);
118 my ($sum,$a,$b) = @_;
120 $r= defined $a->{R} ? $a->{R} : 1.0;
121 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
122 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
123 $sum->{A}= $a->{A} + $r * $b->{A};
126 sub ev_decompose ($$$) {
127 # ev_decompose(B_R, A,SUM)
128 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
131 $r= defined $a->{R} ? $a->{R} : 1.0;
132 $brx= $sum->{X} - $a->{X};
133 $bry= $r * ($sum->{Y} - $a->{Y});
134 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
135 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
136 $b->{A}= $r * ($sum->{A} - $a->{A});
139 sub ev_lincomb ($$$$) {
140 # ev_linkcomb(RES,A,B,P)
141 # gives P*A + (1-P)*B
142 my ($r,$a,$b,$p) = @_;
144 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
147 sub ev_bearing ($$) {
149 # returns bearing of B from A
150 # value returned is in [ A->{A}, A->{A} + 2*$pi >
151 # A->{A} and B->{A} are otherwise ignored
154 $r= atan2($b->{Y} - $a->{Y},
157 while ($r < $a->{A}) { $r += 2.0 * $pi; }
162 # returns distance from A to B
163 # A->{A} and B->{A} are ignored
166 $xd= $b->{X} - $a->{X};
167 $yd= $b->{Y} - $a->{Y};
168 return sqrt($xd*$xd + $yd*$yd);
173 $$limr= $now unless defined $$limr && $$limr <= $now;
177 $$limr= $now unless defined $$limr && $$limr >= $now;
181 my ($converter,$defaulter)=@_;
183 return &$defaulter unless @al;
185 $v= &$converter($spec);
186 dv('canf ','$spec',$spec, '$v',$v);
189 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
190 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
192 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
196 my ($min_x, $max_x, $min_y, $max_y);
198 foreach $loc (values %$objhash) {
199 upd_min(\$min_x, $loc->{X} - abs($allwidthmax * sin($loc->{A})));
200 upd_max(\$max_x, $loc->{X} + abs($allwidthmax * sin($loc->{A})));
201 upd_min(\$min_y, $loc->{Y} - abs($allwidthmax * cos($loc->{A})));
202 upd_max(\$max_y, $loc->{Y} + abs($allwidthmax * cos($loc->{A})));
204 return ($min_x, $max_x, $min_y, $max_y);
207 our %units_len= qw(- mm mm 1 cm 10 m 1000);
208 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
210 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
211 sub cva_identity ($) { my ($sp)=@_; $sp; }
212 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
213 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
217 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
218 or die "lexically invalid quantity";
220 $u=$ua->{'-'} unless length $u;
221 defined $ua->{$u} or die "unknown unit $u";
223 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
228 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
233 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
234 if ($sp =~ s/^(\^?)(\w+)\!//) {
237 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
238 $locs= $objs{$obj_id}{Loc};
243 $neg= $sp =~ s/^\-//;
245 die "unknown $id" unless defined $locs->{$id};
248 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
249 printf DEBUG "%s\n", $d;
251 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
255 $na -= 2*$pi if $na >= 2*$pi;
256 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
263 $neg = $sp =~ s/^\-//;
265 die "duplicate $id" if exists $ctx->{Loc}{$id};
266 exists $ctx->{Loc}{$id}{X};
267 push @{ $ctx->{LocsMade} }, { Id => $id, Neg => $neg };
268 return $ctx->{Loc}{$id};
270 sub cva_cmd ($) { return cva_idstr($_[0]); }
273 return $sp if grep { $_ eq $sp } @$el;
274 die "invalid option (permitted: @$el)";
276 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
280 $nl= can(\&cva_idnew);
281 $i->{X}= can(\&cva_len);
282 $i->{Y}= can(\&cva_len);
283 $i->{A}= can(\&cva_ang);
284 ev_compose($nl, $ctx->{Trans}, $i);
287 my ($from,$to,$len,$right,$turn);
288 $from= can(\&cva_idex);
289 $to= can(\&cva_idnew);
290 $len= cano(\&cva_len,0);
291 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
292 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
293 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
294 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
299 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
303 return 'undef' if !defined $v;
304 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
305 return $v if $v =~ m/^[0-9.]+/;
306 $v =~ s/[\\\']/\\$&/g;
310 sub dv1_kind ($$$$$$$) {
311 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
313 return 0 if $ref ne $ref_exp;
315 foreach $ix (&$ixesfn) {
317 my ($v)= &$ixmapfn($ix);
318 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
319 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
322 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
327 return 0 unless $debug;
328 my ($pfx,$expr,$v) = @_;
331 #print STDERR "dv1 >$pfx|$ref<\n";
333 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
335 } elsif ($ref eq 'SCALAR') {
336 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
339 $expr.='->' unless $expr =~ m/\]$|\}$/;
340 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
341 sub { ($[ .. $#$v) },
342 sub { $v->[$_[0]] });
343 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
344 sub { sort keys %$v },
345 sub { $v->{$_[0]} });
346 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
358 sub o ($) { $o .= $_[0]; }
359 sub ol ($) { $ol .= $_[0]; }
361 print $o, $ol, " showpage\n"
368 sub o_path_begin () {
370 $o_path_verb= 'moveto';
372 sub o_path_point ($) {
374 o(" $pt $o_path_verb\n");
375 $o_path_verb= 'lineto';
377 sub o_path_stroke ($) {
379 o(" $width setlinewidth stroke\n");
383 my ($a,$b,$width)=@_;
387 o_path_stroke($width);
390 sub psu_coords ($$$) {
391 my ($ends,$inunit,$across)=@_;
392 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
393 # $across in mm, +ve to right.
394 my (%ea_zo, $zo, $prop);
395 $ea_zo{X}=$ea_zo{Y}=0;
396 foreach $zo (qw(0 1)) {
397 $prop= $zo ? $inunit : (1.0 - $inunit);
398 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
399 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
401 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
402 # '\\%ea_zo', \%ea_zo);
403 return $ea_zo{X}." ".$ea_zo{Y};
406 sub parametric__o_pt ($) {
408 o_path_point("$pt->{X} $pt->{Y}");
411 sub parametric_segment ($$$$$) {
412 my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
413 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
414 # $lenperp is the length of one unit p, ie the curve
415 # must have a uniform `density' in parameter space
416 # $calcfn is invoked with $p set and should return a loc
417 # (ie, ref to X =>, Y =>, A =>).
418 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
419 return unless $ctx->{Draw} =~ m/[ARSC]/;
420 $ppu= $psu_ulen/$lenperp;
421 $allwidth= allwidth($minradius);
422 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
423 my ($tickend)=($allwidth - $psu_ticklen);
424 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
425 my ($sleeperctr)=($psu_ulen*0.5);
426 my ($sleeperend)=($psu_sleeperlen*0.5);
427 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
431 o(" $psu_thinlw setlinewidth\n");
433 for ($param=$p0; $param<$p1; $param += $ppu) {
434 parametric__o_pt(&$calcfn);
437 parametric__o_pt(&$calcfn);
440 return unless $draw =~ m/[ARS]/;
441 for ($pa= $p0; $pa<$p1; $pa=$pb) {
443 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
444 $param= $pb; $ends[1]= &$calcfn;
445 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
446 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
447 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
450 o_path_point(psu_coords(\@ends,0,-$allwidth));
451 o_path_point(psu_coords(\@ends,0,$allwidth));
452 o_path_point(psu_coords(\@ends,$e,$allwidth));
453 o_path_point(psu_coords(\@ends,$e,-$allwidth));
454 o(" closepath clip\n");
455 foreach $side qw(-1 1) {
457 o_line(psu_coords(\@ends,0,$side*$railctr),
458 psu_coords(\@ends,1.5,$side*$railctr),
463 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
464 psu_coords(\@ends,$sleeperctr,+$sleeperend),
469 foreach $side qw(-1 1) {
470 o_line(psu_coords(\@ends,0,$side*$allwidth),
471 psu_coords(\@ends,1.5,$side*$allwidth),
473 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
474 o_line(psu_coords(\@ends,$tick,$side*$allwidth),
475 psu_coords(\@ends,$tick,$side*$tickend),
485 my ($to, $ctr,$from, $radius,$delta) = @_;
486 # does parametric_segment to draw an arc centred on $ctr
487 # ($ctr->{A} ignored)
488 # from $from with radius $radius (this must be consistent!)
489 # and directionally-subtending an angle $delta.
490 # sets $to->... to be the other end, and returns $to
492 $to->{A}= $beta= $from->{A} + $delta;
493 $to->{X}= $ctr->{X} - $radius * sin($beta);
494 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
495 return if abs($delta*$radius) < 1E-9;
496 parametric_segment(0.0,1.0, abs($radius*$delta), $radius, sub {
497 my ($beta) = $from->{A} + $delta * $param;
498 return { X => $ctr->{X} - $radius * sin($beta),
499 Y => $ctr->{Y} + $radius * cos($beta),
505 my ($from,$to,$how,$minradius);
506 $from= can(\&cva_idex);
507 $to= can(\&cva_idex);
508 $minradius= can(\&cva_len);
509 my (@paths,@solkinds);
511 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
512 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
513 $sigma= ev_bearing($from,$to);
514 $distfact= v_dist($from,$to);
515 $theta= 0.5 * $pi - ($from->{A} - $sigma);
516 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
517 $a= 2 * (1 + cos($theta - $phi));
518 $b= 2 * (cos($theta) - cos($phi));
520 $d= sqrt($b*$b - 4*$a*$c);
521 foreach $m (qw(-1 1)) {
523 $r= -0.5 * (-$b + $m*$d) / $a;
524 $radius= -$r * $distfact;
525 next if abs($radius) < $minradius;
526 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
527 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
528 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
529 $reverse= signum($r);
534 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
535 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
541 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
542 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
544 push @solkinds, 'twoarcs';
547 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
548 my ($crit,$cs,$i,$cmp);
549 foreach $path (@paths) {
550 $sk= shift @solkinds;
551 o("% possible path $sk $path\n");
554 foreach $segment (@$path) {
555 if ($segment->{T} eq Arc) {
556 o("% Arc C ".loc2dbg($segment->{C}).
557 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
558 $len += abs($segment->{R} * $segment->{D});
559 push @bends, signum($segment->{R} * $segment->{D}); # right +ve
561 die "unknown segment $segment->{T}";
564 o("% length $len\n");
566 foreach $crit (@al, 'short') {
567 if ($crit eq 'long') { $cs= $len; }
568 elsif ($crit eq 'short') { $cs= -$len; }
569 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
570 if ($1 eq 'begin') { $cs= $bends[0]; }
571 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
572 else { $cs=0; map { $cs += $_ } @bends; }
573 $cs= -$cs if $2 eq 'left';
574 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
575 $cs= ($2 eq $sk) != ($1 eq '!');
579 o("% scores @$scores\n");
580 if (defined $bestpath) {
581 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
582 $cmp= $scores->[$i] <=> $bestscores->[$i];
587 $bestscores= $scores;
589 die "no solution" unless defined $bestpath;
590 o("% chose path $bestpath @al\n");
592 foreach $segment (@$bestpath) {
593 if ($segment->{T} eq 'Arc') {
594 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
596 die "unknown segment";
602 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
603 $from= can(\&cva_idex);
604 $to= can(\&cva_idnew);
605 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
606 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
607 if ($how eq 'len') { $len= can(\&cva_len); }
608 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
609 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
610 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
611 if ($radius eq 'Inf') {
612 # print DEBUG "extend inf $len\n";
613 if ($how eq 'upto') {
614 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
615 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
616 } elsif ($how eq 'len') {
618 die "len of straight spec by angle";
620 printf DEBUG "len $len\n";
621 $to->{X}= $from->{X} + $len * cos($from->{A});
622 $to->{Y}= $from->{Y} + $len * sin($from->{A});
623 $to->{A}= $from->{A};
624 parametric_segment(0.0, 1.0, abs($len), undef, sub {
625 ev_lincomb({}, $from, $to, $param);
628 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
629 print DEBUG "radius >$radius<\n";
630 $radius *= $ctx->{Trans}{R};
631 $sign_r= signum($radius);
633 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
634 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
635 if ($how eq 'upto') {
636 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
637 $sign_r * ($upto->{Y} - $ctr->{Y}));
639 } elsif ($how eq 'parallel') {
642 } elsif ($how eq 'uptoang') {
643 $beta= input_absang($ang);
645 } elsif ($how eq 'len') {
646 $sign_ang= signum($len);
647 $beta= $from->{A} - $sign_r * $len / abs($radius);
650 $sign_ang= signum($ang);
651 $beta= $from->{A} - $sign_r * $ang;
654 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
655 $beta += $sign_ang * $sign_r * 4.0 * $pi;
657 $delta= $beta - $from->{A};
658 last if $sign_ang * $sign_r * $delta <= 0;
659 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
661 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
662 arc($to, ,$ctr,$from, $radius,$delta);
664 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
669 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
672 return $_[0] * 180 / $pi;
674 sub input_absang ($) {
675 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
677 sub input_abscoords ($$) {
679 ($in->{X}, $in->{Y}) = @_;
681 $out= ev_compose({}, $ctx->{Trans}, $in);
682 return ($out->{X}, $out->{Y});
687 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
695 sub cmd_defobj { cmd__defobj(0); }
696 sub cmd_defpart { cmd__defobj(1); }
697 sub cmd__defobj ($) {
700 $id= can(\&cva_idstr);
701 die "nested defobj" if $defobj_save;
702 die "repeated defobj" if exists $objs{$id};
704 $defobj_ispart= $ispart;
707 $ctx->{InDefObj}= $id;
713 $id= $ctx->{InDefObj};
714 die "unmatched enddef" unless defined $id;
715 foreach $bit (qw(CmdLog Loc)) {
716 $objs{$id}{$bit}= $ctx->{$bit};
718 $objs{$id}{Part}= $defobj_ispart;
723 sub cmd__runobj ($) {
727 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
728 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
730 next if $al[0] eq 'enddef';
736 my ($kl, $k,$l, $eo,$cc);
737 return if $ctx->{Draw} =~ m/X/;
738 $kl= can(\&cva_identity);
739 $kl =~ m/^([A-Za-z_]*)(\d+)$/ or die "invalid layer spec";
741 if ($l != $output_layer) {
744 $ctx->{Draw}= 'RLMN';
745 } elsif ($k eq 's') {
747 } elsif ($k eq 'l') {
748 $ctx->{Draw}= 'CLMN';
750 $ctx->{Draw}= 'ARSCLMNO';
752 foreach $eo (@eopts) {
753 next unless $k =~ m/^$eo->{GlobRe}$/;
754 next unless &{ $eo->{LayerCheck} }($l);
755 foreach $cc (split //, $eo->{DrawMods}) {
756 $ctx->{Draw} =~ s/$cc//ig;
757 $ctx->{Draw} .= $cc if $cc =~ m/[A-Z]/;
762 sub cmd_part { cmd__obj(Part); }
763 sub cmd_obj { cmd__obj(1); }
764 sub cmd_objflip { cmd__obj(-1); }
768 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
769 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
771 $part_name= can(\&cva_idstr);
772 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
774 $obj_id= can(\&cva_idstr);
775 if (defined $part_name) {
776 $formal_id= can(\&cva_idstr);
777 $actual= cano(\&cva_idex, undef);
778 if (!defined $actual) {
779 $actual= cva_idex("${part_name}_${formal_id}");
782 $actual= can(\&cva_idex);
783 $formal_id= can(\&cva_idstr);
785 $obj= $objs{$obj_id};
786 dv("cmd__obj ",'$obj',$obj);
787 die "unknown obj $obj_id" unless $obj;
788 $formal= $obj->{Loc}{$formal_id};
789 die "unknown formal $formal_id" unless $formal;
792 $how *= $ctx_save->{Trans}{R};
793 $ctx->{Trans}{R}= $how;
794 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
795 $formcv= ev_compose({}, $ctx->{Trans},$formal);
796 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
797 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
798 if (defined $part_name) {
799 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
801 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
803 $ctx->{Draw}= $ctx_save->{Draw};
805 $ctx->{Draw} =~ s/[LMN]//g;
806 $ctx->{Draw} =~ s/O/MNO/;
808 $ctx->{Draw} =~ s/[LM]//g;
809 $ctx->{Draw} =~ s/N/MN/;
811 cmd__runobj($obj_id);
812 if (defined $part_name) {
813 $pfx= $part_name.'_';
815 if (@al && $al[0] eq '=') {
818 $pfx= cano(\&cva_idstr,undef);
824 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
825 next if $id eq $formal_id;
827 next if exists $ctx_save->{Loc}{$newid};
828 $newpt= cva_idnew($newid);
829 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
832 if (defined $part_name) {
833 my ($formalr_id, $actualr_id, $formalr, $actualr);
835 die "part results come in pairs\n" unless @al>=2;
836 ($formalr_id, $actualr_id, @al) = @al;
837 if ($actualr_id =~ s/^\-//) {
838 $formalr_id= "-$formalr_id";
839 $formalr_id =~ s/^\-\-//;
842 local ($ctx) = $ctx_inobj;
843 $formalr= cva_idex($formalr_id);
845 $actualr= cva_idnew($actualr_id);
846 %$actualr= %$formalr;
853 dv("cmd__do $ctx @al ",'$ctx',$ctx);
854 $cmd= can(\&cva_cmd);
855 my ($lm,$id,$loc,$io,$ad);
856 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
857 o("%L cmd $io $cmd @al\n");
858 $ctx->{LocsMade}= [ ];
863 die "too many args" if @al;
864 foreach $lm (@{ $ctx->{LocsMade} }) {
866 $loc= $ctx->{Loc}{$id};
867 $loc->{A} += $pi if $lm->{Neg};
868 $ad= ang2deg($loc->{A});
869 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
870 if ($ctx->{Draw} =~ m/[LM]/) {
872 " $loc->{X} $loc->{Y} translate $ad rotate\n");
873 if ($ctx->{Draw} =~ m/M/) {
874 ol(" 0 $allwidthmin newpath moveto\n".
875 " 0 -$allwidthmin lineto\n".
876 " $lmu_marklw setlinewidth stroke\n");
878 if ($ctx->{Draw} =~ m/L/) {
879 ol(" /s ($id) def\n".
881 " /sx5 s stringwidth pop\n".
882 " 0.5 mul $lmu_txtboxpadx add def\n".
883 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
884 " sx5 neg 0 moveto\n".
885 " sx5 neg $lmu_txtboxh lineto\n".
886 " sx5 $lmu_txtboxh lineto\n".
887 " sx5 0 lineto closepath\n".
888 " gsave 1 setgray fill grestore\n".
889 " $lmu_txtboxlw setlinewidth stroke\n".
890 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
898 sub cmd_showlibrary {
899 my ($obj_id, $y, $x, $ctx_save, $width, $height);
900 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
902 $x=$olu_left; $y=$olu_bottom; undef $nxty;
904 foreach $obj_id (sort keys %objs) {
906 foreach $glob (@al) {
908 $got= !($pat =~ s/^\!//);
909 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
910 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
911 last if $obj_id =~ m/^$pat$/;
915 $obj= $objs{$obj_id};
916 next unless $obj->{Part};
917 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
921 $width= $max_x - $min_x;
922 $height= $max_y - $min_y;
923 if ($width < $height) {
925 $ctx->{Trans}{X}= $x - $min_x;
926 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
928 ($width,$height)=($height,$width);
929 $ctx->{Trans}{A}= 0.5 * $pi;
930 $ctx->{Trans}{X}= $x + $max_y;
931 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
933 $adj= length($obj_id) * $olu_textallowperc - $width;
936 $ctx->{Trans}{X} += 0.5 * $adj;
937 if ($x + $width > $olu_right && defined $nxty) {
941 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
943 $x= $olu_left; $y= $olu_bottom;
950 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
951 $ctx->{Draw}= $ctx_save->{Draw};
952 cmd__runobj($obj_id);
954 " /s ($obj_id) def\n".
956 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
957 " s stringwidth pop -0.5 mul 0 rmoveto\n".
958 " s show grestore\n");
959 $x += $width + $olu_gap_x;
960 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
972 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
973 " 615 0 translate 90 rotate\n".
974 " $ptscale $ptscale scale\n"
979 our $drawers= 'arsclmno';
980 our %chdraw_emap= qw(A ARSc
994 while (@ARGV && $ARGV[0] =~ m/^\-/) {
995 last if $ARGV[0] eq '-';
1000 if (s/^D(\d+)//) { $debug= $1; }
1001 elsif (s/^D//) { $debug++; }
1002 elsif (s/^q//) { $quiet=1; }
1004 ((?:[a-z]|\*|\?|\[[a-z][-a-z]*\])*?)
1005 (\~?) (\d*) (\=*|\-+|\++)
1007 my ($ee,$g,$n,$d,$c,$cc) = ($1,$2,$3,$4,$5,$6);
1008 my ($eo, $invert, $lfn, $ccc, $sense,$limit);
1009 $g =~ s/[?*]/\\$&/g;
1010 $d= $output_layer if !length $d;
1012 if ($c =~ m/^[-+]/) {
1013 $sense= ($c.'1') + 0;
1014 $limit= ($sense * $d) + length($c) - 1;
1015 $lfn= sub { $_[0] * $sense >= $limit xor $invert };
1017 $limit= length($c) - 1;
1018 $limit=0 if $limit<0;
1019 $lfn= sub { abs($_[0] - $d) <= $limit xor $invert };
1022 foreach $c (split //, $cc) {
1024 die "bad -e option $c" unless defined $chdraw_emap{$c};
1025 $ccc .= $chdraw_emap{$c};
1027 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
1032 $eo->{LayerCheck}= $lfn;
1033 $eo->{DrawMods}= $ccc;
1036 die "unknown option -$_";
1041 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
1044 select(DEBUG); $|=1;
1045 select(STDOUT); $|=1;
1055 chomp; s/^\s+//; s/\s+$//;
1056 @al= split /\s+/, $_;
1058 print DEBUG "=== @al\n";
1059 last if $al[0] eq 'eof';
1060 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1067 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1069 if (defined $min_x) {
1070 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1071 "height %.2d (%.2d..%2.d)\n",
1072 $max_x - $min_x, $min_x, $max_x,
1073 $max_y - $min_y, $min_y, $max_y);
1075 $bboxstr= "no locs, no bbox\n";
1077 if (!$quiet) { print STDERR $bboxstr; }
1078 $bboxstr =~ s/^/\%L bbox /mg;
1079 print $bboxstr or die $!;