8 our $ptscale= 72/25.4 / $scale;
13 our $psu_ticksperu= 1;
14 our $psu_ticklen= 5.0;
15 our $psu_allwidth= 37.0/2;
17 our $psu_sleeperlen= 17;
18 our $psu_sleeperlw= 15;
24 our $lmu_txtboxtxty= $lmu_marktpt * 0.300;
25 our $lmu_txtboxh= $lmu_marktpt * 1.100;
26 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
27 our $lmu_txtboxoff= $lmu_marklw / 2;
30 our $olu_left= 10 * $scale;
31 our $olu_right= 217 * $scale - $olu_left;
32 our $olu_bottom= 20 * $scale;
33 our $olu_top= 270 * $scale - $olu_bottom;
36 our $olu_textheight= 15;
37 our $olu_textallowperc= $lmu_marktpt * 5.0/11;
45 # $ctx->{CmdLog}= undef } not in defobj
46 # $ctx->{CmdLog}[]= [ command args ] } in defobj
47 # $ctx->{LocsMade}[]= $id
51 # $ctx->{Trans}{X} # transformation. is ev representing
52 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
53 # $ctx->{Trans}{A} # not at plot-time)
54 # $ctx->{Trans}{R} # but multiply all y coords by this!
55 # $ctx->{Draw} # sequence of 0 or more of:
56 # # restrictions: T => !C, L => M
57 # $ctx->{Draw}{T} # 1 or T for drawing track or thin lines, or ''
58 # $ctx->{Draw}{L} # L1 or 1 for labelling or drawing locs, or ''
62 # $objs{$id}{Part} # 1 iff object is a part
66 our @al; # current cmd
71 our $param; # for parametric_curve
76 # Operate on Enhanced Vectors which are a location (coordinates) and a
77 # direction at that location. Representation is a hash with members X
78 # Y and A (angle of the direction in radians, anticlockwise from
79 # East). May be absolute, or interpreted as relative, according to
82 # Each function's first argument is a hashref whose X Y A members will
83 # be created or overwritten; this hashref will be returned (so you can
84 # use it `functionally' by passing {}). The other arguments may be ev
85 # hashrefs, or other info. The results are in general undefined if
86 # one of the arguments is the same hash as the result.
89 # ev_byang(R, ANG,[LEN])
90 # result is evec of specified angle and length (default=1.0)
91 my ($res,$ang,$len)=@_;
92 $len=1.0 unless defined $len;
93 $res->{X}= $len * cos($ang);
94 $res->{Y}= $len * sin($ang);
98 sub ev_compose ($$$) {
99 # ev_compose(SUM_R, A,B);
100 # appends B to A, result is end of new B
101 # (B's X is forwards from end of A, Y is translating left from end of A)
102 # A may have a member R, which if provided then it should be 1.0 or -1.0,
103 # and B's Y and A will be multiplied by R first (ie, we can reflect);
104 my ($sum,$a,$b) = @_;
106 $r= defined $a->{R} ? $a->{R} : 1.0;
107 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
108 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
109 $sum->{A}= $a->{A} + $r * $b->{A};
112 sub ev_decompose ($$$) {
113 # ev_decompose(B_R, A,SUM)
114 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
117 $r= defined $a->{R} ? $a->{R} : 1.0;
118 $brx= $sum->{X} - $a->{X};
119 $bry= $r * ($sum->{Y} - $a->{Y});
120 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
121 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
122 $b->{A}= $r * ($sum->{A} - $a->{A});
125 sub ev_lincomb ($$$$) {
126 # ev_linkcomb(RES,A,B,P)
127 # gives P*A + (1-P)*B
128 my ($r,$a,$b,$p) = @_;
130 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
133 sub ev_bearing ($$) {
135 # returns bearing of B from A
136 # value returned is in [ A->{A}, A->{A} + 2*$pi >
137 # A->{A} and B->{A} are otherwise ignored
140 $r= atan2($b->{Y} - $a->{Y},
143 while ($r < $a->{A}) { $r += 2.0 * $pi; }
148 # returns distance from A to B
149 # A->{A} and B->{A} are ignored
152 $xd= $b->{X} - $a->{X};
153 $yd= $b->{Y} - $a->{Y};
154 return sqrt($xd*$xd + $yd*$yd);
159 $$limr= $now unless defined $$limr && $$limr <= $now;
163 $$limr= $now unless defined $$limr && $$limr >= $now;
167 my ($converter,$defaulter)=@_;
169 return &$defaulter unless @al;
171 $v= &$converter($spec);
172 dv('canf ','$spec',$spec, '$v',$v);
175 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
176 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
178 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
182 my ($min_x, $max_x, $min_y, $max_y);
184 foreach $loc (values %$objhash) {
185 upd_min(\$min_x, $loc->{X} - abs($psu_allwidth * sin($loc->{A})));
186 upd_max(\$max_x, $loc->{X} + abs($psu_allwidth * sin($loc->{A})));
187 upd_min(\$min_y, $loc->{Y} - abs($psu_allwidth * cos($loc->{A})));
188 upd_max(\$max_y, $loc->{Y} + abs($psu_allwidth * cos($loc->{A})));
190 return ($min_x, $max_x, $min_y, $max_y);
193 our %units_len= qw(- mm mm 1 cm 10 m 1000);
194 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
196 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
197 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
198 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
202 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
203 or die "lexically invalid quantity";
205 $u=$ua->{'-'} unless length $u;
206 defined $ua->{$u} or die "unknown unit $u";
208 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
213 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
218 my ($r,$d,$k,$neg,$na);
219 $neg= $sp =~ s/^\-//;
221 die "unknown $id" unless defined $ctx->{Loc}{$id};
222 $r= $ctx->{Loc}{$id};
224 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
225 printf DEBUG "%s\n", $d;
228 $na -= 2*$pi if $na >= 2*$pi;
229 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
237 die "duplicate $id" if exists $ctx->{Loc}{$id};
238 exists $ctx->{Loc}{$id}{X};
239 push @{ $ctx->{LocsMade} }, $id;
240 return $ctx->{Loc}{$id};
242 sub cva_cmd ($) { return cva_idstr($_[0]); }
245 return $sp if grep { $_ eq $sp } @$el;
246 die "invalid option (permitted: @$el)";
248 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
252 $nl= can(\&cva_idnew);
253 $i->{X}= can(\&cva_len);
254 $i->{Y}= can(\&cva_len);
255 $i->{A}= can(\&cva_ang);
256 ev_compose($nl, $ctx->{Trans}, $i);
259 my ($from,$to,$len,$right,$turn);
260 $from= can(\&cva_idex);
261 $to= can(\&cva_idnew);
262 $len= cano(\&cva_len,0);
263 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
264 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
265 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
266 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
271 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
275 return 'undef' if !defined $v;
276 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
277 return $v if $v =~ m/^[0-9.]+/;
278 $v =~ s/[\\\']/\\$&/g;
282 sub dv1_kind ($$$$$$$) {
283 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
285 return 0 if $ref ne $ref_exp;
287 foreach $ix (&$ixesfn) {
289 my ($v)= &$ixmapfn($ix);
290 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
291 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
294 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
299 return 0 unless $debug;
300 my ($pfx,$expr,$v) = @_;
303 #print STDERR "dv1 >$pfx|$ref<\n";
305 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
307 } elsif ($ref eq 'SCALAR') {
308 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
311 $expr.='->' unless $expr =~ m/\]$|\}$/;
312 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
313 sub { ($[ .. $#$v) },
314 sub { $v->[$_[0]] });
315 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
316 sub { sort keys %$v },
317 sub { $v->{$_[0]} });
318 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
330 sub o ($) { $o .= $_[0]; }
331 sub ol ($) { $ol .= $_[0]; }
333 print $o, $ol, " showpage\n"
340 sub o_path_begin () {
342 $o_path_verb= 'moveto';
344 sub o_path_point ($) {
346 o(" $pt $o_path_verb\n");
347 $o_path_verb= 'lineto';
349 sub o_path_stroke ($) {
351 o(" $width setlinewidth stroke\n");
355 my ($a,$b,$width)=@_;
359 o_path_stroke($width);
362 sub psu_coords ($$$) {
363 my ($ends,$inunit,$across)=@_;
364 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
365 # $across in mm, +ve to right.
366 my (%ea_zo, $zo, $prop);
367 $ea_zo{X}=$ea_zo{Y}=0;
368 foreach $zo (qw(0 1)) {
369 $prop= $zo ? $inunit : (1.0 - $inunit);
370 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
371 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
373 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
374 # '\\%ea_zo', \%ea_zo);
375 return $ea_zo{X}." ".$ea_zo{Y};
378 sub parametric__o_pt ($) {
380 o_path_point("$pt->{X} $pt->{Y}");
383 sub parametric_segment ($$$$) {
384 my ($p0,$p1,$lenperp,$calcfn) = @_;
385 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
386 # $lenperp is the length of one unit p, ie the curve
387 # must have a uniform `density' in parameter space
388 # $calcfn is invoked with $p set and should return a loc
389 # (ie, ref to X =>, Y =>, A =>).
390 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw);
391 return unless $ctx->{Draw} =~ m/[ARSC]/;
392 $ppu= $psu_ulen/$lenperp;
393 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
394 my ($tickend)=($psu_allwidth - $psu_ticklen);
395 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
396 my ($sleeperctr)=($psu_ulen*0.5);
397 my ($sleeperend)=($psu_sleeperlen*0.5);
398 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
402 o(" $psu_thinlw setlinewidth\n");
404 for ($param=$p0; $param<$p1; $param += $ppu) {
405 parametric__o_pt(&$calcfn);
408 parametric__o_pt(&$calcfn);
411 return unless $draw =~ m/[ARS]/;
412 for ($pa= $p0; $pa<$p1; $pa=$pb) {
414 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
415 $param= $pb; $ends[1]= &$calcfn;
416 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
417 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
418 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
421 o_path_point(psu_coords(\@ends,0,-$psu_allwidth));
422 o_path_point(psu_coords(\@ends,0,$psu_allwidth));
423 o_path_point(psu_coords(\@ends,$e,$psu_allwidth));
424 o_path_point(psu_coords(\@ends,$e,-$psu_allwidth));
425 o(" closepath clip\n");
426 foreach $side qw(-1 1) {
428 o_line(psu_coords(\@ends,0,$side*$railctr),
429 psu_coords(\@ends,1.5,$side*$railctr),
433 o_line(psu_coords(\@ends,0,$side*$psu_allwidth),
434 psu_coords(\@ends,1.5,$side*$psu_allwidth),
436 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
437 o_line(psu_coords(\@ends,$tick,$side*$psu_allwidth),
438 psu_coords(\@ends,$tick,$side*$tickend),
444 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
445 psu_coords(\@ends,$sleeperctr,+$sleeperend),
453 my ($to, $ctr,$from, $radius,$delta) = @_;
454 # does parametric_segment to draw an arc centred on $ctr
455 # ($ctr->{A} ignored)
456 # from $from with radius $radius (this must be consistent!)
457 # and directionally-subtending an angle $delta.
458 # sets $to->... to be the other end, and returns $to
460 $to->{A}= $beta= $from->{A} + $delta;
461 $to->{X}= $ctr->{X} - $radius * sin($beta);
462 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
463 return if abs($delta*$radius) < 1E-9;
464 parametric_segment(0.0,1.0, abs($radius*$delta), sub {
465 my ($beta) = $from->{A} + $delta * $param;
466 return { X => $ctr->{X} - $radius * sin($beta),
467 Y => $ctr->{Y} + $radius * cos($beta),
473 my ($from,$to,$how,$minradius);
474 $from= can(\&cva_idex);
475 $to= can(\&cva_idex);
476 $minradius= can(\&cva_len);
477 my (@paths,@solkinds);
479 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
480 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
481 $sigma= ev_bearing($from,$to);
482 $distfact= v_dist($from,$to);
483 $theta= 0.5 * $pi - ($from->{A} - $sigma);
484 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
485 $a= 2 * (1 + cos($theta - $phi));
486 $b= 2 * (cos($theta) - cos($phi));
488 $d= sqrt($b*$b - 4*$a*$c);
489 foreach $m (qw(-1 1)) {
491 $r= -0.5 * (-$b + $m*$d) / $a;
492 $radius= -$r * $distfact;
493 next if abs($radius) < $minradius;
494 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
495 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
496 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
497 $reverse= signum($r);
502 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
503 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
509 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
510 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
512 push @solkinds, 'twoarcs';
515 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$sk);
516 my ($crit,$cs,$i,$cmp);
517 foreach $path (@paths) {
518 $sk= shift @solkinds;
519 o("% possible path $sk $path\n");
522 foreach $segment (@$path) {
523 if ($segment->{T} eq Arc) {
524 o("% Arc C ".loc2dbg($segment->{C}).
525 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
526 $len += abs($segment->{R} * $segment->{D});
527 push @bends, signum($segment->{R} * $segment->{D}); # right +ve
529 die "unknown segment $segment->{T}";
532 o("% length $len\n");
534 foreach $crit (@al, 'short') {
535 if ($crit eq 'long') { $cs= $len; }
536 elsif ($crit eq 'short') { $cs= -$len; }
537 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
538 if ($1 eq 'begin') { $cs= $bends[0]; }
539 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
540 else { $cs=0; map { $cs += $_ } @bends; }
541 $cs= -$cs if $2 eq 'left';
542 } elsif ($crit =~ m/^(\!?)(twoarcs|arcline|arcsline)$/) {
543 $cs= ($2 eq $sk) != ($1 eq '!');
547 o("% scores @$scores\n");
548 if (defined $bestpath) {
549 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
550 $cmp= $scores->[$i] <=> $bestscores->[$i];
555 $bestscores= $scores;
557 die "no solution" unless defined $bestpath;
558 o("% chose path $bestpath @al\n");
560 foreach $segment (@$bestpath) {
561 if ($segment->{T} eq 'Arc') {
562 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
564 die "unknown segment";
570 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
571 $from= can(\&cva_idex);
572 $to= can(\&cva_idnew);
573 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
574 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
575 if ($how eq 'len') { $len= can(\&cva_len); }
576 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
577 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
578 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
579 if ($radius eq 'Inf') {
580 # print DEBUG "extend inf $len\n";
581 if ($how eq 'upto') {
582 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
583 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
584 } elsif ($how eq 'len') {
586 die "len of straight spec by angle";
588 printf DEBUG "len $len\n";
589 $to->{X}= $from->{X} + $len * cos($from->{A});
590 $to->{Y}= $from->{Y} + $len * sin($from->{A});
591 $to->{A}= $from->{A};
592 parametric_segment(0.0, 1.0, abs($len), sub {
593 ev_lincomb({}, $from, $to, $param);
596 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
597 print DEBUG "radius >$radius<\n";
598 $radius *= $ctx->{Trans}{R};
599 $sign_r= signum($radius);
601 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
602 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
603 if ($how eq 'upto') {
604 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
605 $sign_r * ($upto->{Y} - $ctr->{Y}));
607 } elsif ($how eq 'parallel') {
610 } elsif ($how eq 'uptoang') {
611 $beta= input_absang($ang);
613 } elsif ($how eq 'len') {
614 $sign_ang= signum($len);
615 $beta= $from->{A} - $sign_r * $len / abs($radius);
618 $sign_ang= signum($ang);
619 $beta= $from->{A} - $sign_r * $ang;
622 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
623 $beta += $sign_ang * $sign_r * 4.0 * $pi;
625 $delta= $beta - $from->{A};
626 last if $sign_ang * $sign_r * $delta <= 0;
627 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
629 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
630 arc($to, ,$ctr,$from, $radius,$delta);
632 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
637 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
640 return $_[0] * 180 / $pi;
642 sub input_absang ($) {
643 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
645 sub input_abscoords ($$) {
647 ($in->{X}, $in->{Y}) = @_;
649 $out= ev_compose({}, $ctx->{Trans}, $in);
650 return ($out->{X}, $out->{Y});
655 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
663 sub cmd_defobj { cmd__defobj(0); }
664 sub cmd_defpart { cmd__defobj(1); }
665 sub cmd__defobj ($) {
668 $id= can(\&cva_idstr);
669 die "nested defobj" if $defobj_save;
670 die "repeated defobj" if exists $objs{$id};
672 $defobj_ispart= $ispart;
675 $ctx->{InDefObj}= $id;
681 $id= $ctx->{InDefObj};
682 die "unmatched enddefobj" unless defined $id;
683 foreach $bit (qw(CmdLog Loc)) {
684 $objs{$id}{$bit}= $ctx->{$bit};
686 $objs{$id}{Part}= $defobj_ispart;
691 sub cmd__runobj ($) {
695 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
696 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
698 next if $al[0] eq 'enddefobj';
703 sub cmd_obj { cmd__obj(1); }
704 sub cmd_objflip { cmd__obj(-1); }
707 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
708 my ($ctx_inobj, $obj, $id, $newid, $newpt);
709 $obj_id= can(\&cva_idstr);
710 $actual= can(\&cva_idex);
711 $formal_id= can(\&cva_idstr);
712 $obj= $objs{$obj_id};
713 dv("cmd__obj ",'$obj',$obj);
714 die "unknown obj $obj_id" unless $obj;
715 $formal= $obj->{Loc}{$formal_id};
716 die "unknown formal $formal_id" unless $formal;
719 $ctx->{Trans}{R}= $flipsignum;
720 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$flipsignum;
721 $formcv= ev_compose({}, $ctx->{Trans},$formal);
722 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
723 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
724 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
725 $ctx->{Draw}= $ctx_save->{Draw};
727 $ctx->{Draw} =~ s/[LMN]//g;
728 $ctx->{Draw} =~ s/O/MNO/;
730 $ctx->{Draw} =~ s/[LM]//g;
731 $ctx->{Draw} =~ s/N/MN/;
733 cmd__runobj($obj_id);
734 if (@al && $al[0] eq '=') {
737 $pfx= cano(\&cva_idstr,undef);
742 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
743 next if $id eq $formal_id;
745 next if exists $ctx_save->{Loc}{$newid};
746 $newpt= cva_idnew($newid);
747 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
754 dv("cmd__do $ctx @al ",'$ctx',$ctx);
755 $cmd= can(\&cva_cmd);
756 my ($id,$loc,$io,$ad);
757 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
758 o("%L cmd $io $cmd @al\n");
759 $ctx->{LocsMade}= [ ];
764 die "too many args" if @al;
765 foreach $id (@{ $ctx->{LocsMade} }) {
766 $loc= $ctx->{Loc}{$id};
767 $ad= ang2deg($loc->{A});
768 ol("%L point $io$id ".loc2dbg($loc)."\n");
769 if ($ctx->{Draw} =~ m/[LM]/) {
771 " $loc->{X} $loc->{Y} translate $ad rotate\n");
772 if ($ctx->{Draw} =~ m/M/) {
773 ol(" 0 $psu_allwidth newpath moveto\n".
774 " 0 -$psu_allwidth lineto\n".
775 " $lmu_marklw setlinewidth stroke\n");
777 if ($ctx->{Draw} =~ m/L/) {
778 ol(" /s ($id) def\n".
780 " /sx5 s stringwidth pop\n".
781 " 0.5 mul $lmu_txtboxpadx add def\n".
782 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
783 " sx5 neg 0 moveto\n".
784 " sx5 neg $lmu_txtboxh lineto\n".
785 " sx5 $lmu_txtboxh lineto\n".
786 " sx5 0 lineto closepath\n".
787 " gsave 1 setgray fill grestore\n".
788 " $lmu_txtboxlw setlinewidth stroke\n".
789 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
797 sub cmd_showlibrary {
798 my ($obj_id, $y, $x, $ctx_save, $width, $height);
799 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
801 $x=$olu_left; $y=$olu_bottom; undef $nxty;
803 foreach $obj_id (sort keys %objs) {
805 foreach $glob (@al) {
807 $got= !($pat =~ s/^\!//);
808 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
809 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
810 last if $obj_id =~ m/^$pat$/;
814 $obj= $objs{$obj_id};
815 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
819 $width= $max_x - $min_x;
820 $height= $max_y - $min_y;
821 if ($width < $height) {
823 $ctx->{Trans}{X}= $x - $min_x;
824 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
826 ($width,$height)=($height,$width);
827 $ctx->{Trans}{A}= 0.5 * $pi;
828 $ctx->{Trans}{X}= $x + $max_y;
829 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
831 $adj= length($obj_id) * $olu_textallowperc - $width;
834 $ctx->{Trans}{X} += 0.5 * $adj;
835 if ($x + $width > $olu_right && defined $nxty) {
839 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
841 $x= $olu_left; $y= $olu_bottom;
848 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
849 $ctx->{Draw}= $ctx_save->{Draw};
850 cmd__runobj($obj_id);
852 " /s ($obj_id) def\n".
854 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
855 " s stringwidth pop -0.5 mul 0 rmoveto\n".
856 " s show grestore\n");
857 $x += $width + $olu_gap_x;
858 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
870 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
871 " 615 0 translate 90 rotate\n".
872 " $ptscale $ptscale scale\n"
877 our $drawers= 'arsclmno';
878 our %chdraw_emap= qw(A ARSc
890 $ctx->{Draw}= uc $drawers;
894 while (@ARGV && $ARGV[0] =~ m/^\-/) {
895 last if $ARGV[0] eq '-';
900 if (s/^D(\d+)//) { $debug= $1; }
901 elsif (s/^D//) { $debug++; }
902 elsif (s/^b//) { $bbox=1; }
903 elsif (s/^([Ee])([a-zA-Z]+)//) {
906 foreach $c (split //, $2) {
908 die "bad -e option $c" unless defined $chdraw_emap{$c};
909 $c= $chdraw_emap{$c};
911 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
913 $ctx->{Draw} =~ s/$c//ig;
914 $ctx->{Draw} .= $c if $c =~ m/[A-Z]/;
917 die "unknown option -$_";
922 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
926 select(STDOUT); $|=1;
931 chomp; s/^\s+//; s/\s+$//;
932 @al= split /\s+/, $_;
934 print DEBUG "=== @al\n";
935 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
942 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
943 if (defined $min_x) {
945 "width %.2d (%.2d..%2.d)\n".
946 "height %.2d (%.2d..%2.d)\n",
947 $max_x - $min_x, $min_x, $max_x,
948 $max_y - $min_y, $min_y, $max_y);
950 print STDERR "no points, bbox\n";