15 our $output_layer= '*';
17 our $subsegmovfeatpos='f';
19 our $ps_page_shift= 615;
20 our $ps_page_xmul= 765.354;
21 our $ps_page_ymul= 538.583;
27 our $drawers= 'arsclmnog';
28 our %chdraw_emap= qw(A ARScg
42 while (@ARGV && $ARGV[0] =~ m/^\-/) {
43 last if $ARGV[0] eq '-';
48 if (s/^D(\d+)//) { $debug= $1; }
49 elsif (s/^D//) { $debug++; }
50 elsif (s/^q//) { $quiet=1; }
51 elsif (s/^l(\d+|\*)//) { $output_layer=$1; }
52 elsif (s/^S([0-9.]+)$//) { $scale= $1 * 1.0; }
53 elsif (s/^P(\d+)x(\d+)$//) { $page_x= $1; $page_y= $2; }
54 elsif (s/^GR//) { $subsegcmapreq=1; }
55 elsif (s/^GP(\d+|f)$//) { $subsegmovfeatpos=$1; }
56 elsif (s/^GL(.*)$//) {
60 $sscmf= new IO::File $sscmfn, 'r'
61 or die "$sscmfn: cannot open: $!\n";
63 $!=0; $_= <$sscmf>; die $! unless defined $_;
66 m,^C\s+(\w*/(?:[A-Za-z_]+)?)\s+(\S.*\S)\s*$,
67 or die "$sscmfn:$.: syntax error in subseg cmap\n";
70 $sscmf->error and die "$sscmfn: error reading: $!\n";
73 ((?:[a-z]|\*|\?|\[[a-z][-a-z]*\])*?)
74 (\~?) (\d*) (\=*|\-+|\++) (\d*|\*)
76 my ($ee,$g,$n,$d,$c,$v,$cc) = ($1,$2,$3,$4,$5,$6,$7);
77 my ($eo, $invert, $lfn, $ccc, $sense,$limit);
78 $g =~ s/\?/\./g; $g =~ s/\*/\.\*/g;
79 die '-[eE]GND[=]* not allowed' if $v eq '*' && length $d;
80 $d= $output_layer if !length $d;
83 $c= '=' if !length $c;
84 if (length $v && $v ne '*') {
85 die '-[eE]GN[D]CCV not allowed' if length $c > 1;
89 die '-[eE]GN+/-* not allowed' if $v eq '*';
91 $limit= ($sense * $d) + length($c) - 1;
93 ($output_layer eq '*' ? $d
94 : $_[0]) * $sense >= $limit
98 $lfn= sub { !$invert; };
100 $limit= length($c) - 1;
103 ($output_layer eq '*' ? 1
104 : abs($_[0] - $d) <= $limit)
108 #print STDERR "output layer $output_layer; asking re $_[0] rel $d lim $limit invert $invert result $lfn_result\n";
113 foreach $c (split //, $cc) {
115 die "bad -e option $c" unless defined $chdraw_emap{$c};
116 $ccc .= $chdraw_emap{$c};
118 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
123 $eo->{LayerCheck}= $lfn;
124 $eo->{DrawMods}= $ccc;
125 #print STDERR "created eo $eo re $eo->{GlobRe} n=$n d=$d v=$v c=$c limit=$limit cc=$cc\n";
128 die "-S option must come right at the start and have numeric arg";
130 die "unknown option -$_";
135 our $ptscale= 72/25.4 / $scale;
138 our $psu_edgelw= 0.5;
139 our $psu_ticklw= 0.1;
140 our $psu_ticksperu= 1;
141 our $psu_ticklen= 5.0;
143 our $psu_sleeperlen= 17;
144 our $psu_sleeperlw= 15;
145 our $psu_raillw= 1.0;
146 our $psu_thinlw= 1.0;
147 our $psu_subseglw= 15.0;
150 our $lmu_marktpt= 11;
151 our $lmu_txtboxtxty= $lmu_marktpt * 0.300;
152 our $lmu_txtboxh= $lmu_marktpt * 1.100;
153 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
154 our $lmu_txtboxoff= $lmu_marklw / 2;
155 our $lmu_txtboxlw= 1;
157 our $olu_left= 10 * $scale;
158 our $olu_right= 217 * $scale - $olu_left;
159 our $olu_bottom= 20 * $scale;
160 our $olu_top= 270 * $scale - $olu_bottom;
163 our $olu_textheight= 15;
164 our $olu_textallowperc= $lmu_marktpt * 5.0/11;
166 our $pi= atan2(0,-1);
170 return 27 unless defined $radius;
171 $radius= abs($radius);
172 return ($radius >= 450 ? 33 :
173 $radius >= 400 ? 35 :
176 sub allwidth ($) { return allwidth2($_[0]) * 0.5; }
178 our $allwidthmax= allwidth(0);
179 our $allwidthmin= allwidth(undef);
182 # $ctx->{CmdLog}= undef } not in defobj
183 # $ctx->{CmdLog}[]= [ command args ] } in defobj
184 # $ctx->{LocsMade}[]{Id}= $id
185 # $ctx->{LocsMade}[]{Neg}= 1 or 0
186 # $ctx->{Loc}{$id}{X}
187 # $ctx->{Loc}{$id}{Y}
188 # $ctx->{Loc}{$id}{A}
189 # $ctx->{Loc}{$id}{LayerKind}
190 # $ctx->{Trans}{X} # transformation. is ev representing
191 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
192 # $ctx->{Trans}{A} # not at plot-time)
193 # $ctx->{Trans}{R} # but multiply all y coords by this!
194 # $ctx->{Draw} # sequence of one or more chrs from uc $drawers
195 # # possibly including X meaning never draw
196 # # anything now (eg in defobj)
197 # $ctx->{DrawMap} # =$fn s.t.
198 # # &$fn($drawchrs_spec_by_layer_cmdline)
199 # # = $drawchrs_we_should_use_due_to_obj_etc
200 # $ctx->{SegName} # initial segment name (at start of object or file)
201 # # or nonexistent if in object in unknown segment
202 # # may have leading `-'
203 # $ctx->{SavedSegment} # exists iff segment command used, is a $csss
204 # $ctx->{Layer}{Level}
205 # $ctx->{Layer}{Kind}
209 # $objs{$id}{Part} # 1 iff object is a part
211 # $eopts[]{GlobRe} # regexp for K
212 # $eopts[]{LayerCheck} # =$fn where &$fn($l) is true iff layer matches
213 # $eopts[]{DrawMods} # modifier chars for drawing
215 # @segments= ( $csss0, $dist0, $csss1, $dist1, ..., $csssn )
216 # # here each csss may have preceding `-'
218 # $subsegcmap{$csss} = "$green $blue"
219 # # $csss is canonical subseg spec; always has '/'
223 our @al; # current cmd
228 our $param; # for parametric_curve
232 # Operate on Enhanced Vectors which are a location (coordinates) and a
233 # direction at that location. Representation is a hash with members X
234 # Y and A (angle of the direction in radians, anticlockwise from
235 # East). May be absolute, or interpreted as relative, according to
238 # Each function's first argument is a hashref whose X Y A members will
239 # be created or overwritten; this hashref will be returned (so you can
240 # use it `functionally' by passing {}). The other arguments may be ev
241 # hashrefs, or other info. The results are in general undefined if
242 # one of the arguments is the same hash as the result.
244 sub ev_byang ($$;$) {
245 # ev_byang(R, ANG,[LEN])
246 # result is evec LEN (default=1.0) from origin pointing in direction ANG
247 my ($res,$ang,$len)=@_;
248 $len=1.0 unless defined $len;
249 $res->{X}= $len * cos($ang);
250 $res->{Y}= $len * sin($ang);
254 sub ev_compose ($$$) {
255 # ev_compose(SUM_R, A,B);
256 # appends B to A, result is end of new B
257 # (B's X is forwards from end of A, Y is translating left from end of A)
258 # A may have a member R, which if provided then it should be 1.0 or -1.0,
259 # and B's Y and A will be multiplied by R first (ie, we can reflect);
260 my ($sum,$a,$b) = @_;
262 $r= defined $a->{R} ? $a->{R} : 1.0;
263 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
264 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
265 $sum->{A}= $a->{A} + $r * $b->{A};
268 sub ev_decompose ($$$) {
269 # ev_decompose(B_R, A,SUM)
270 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
273 $r= defined $a->{R} ? $a->{R} : 1.0;
274 $brx= $sum->{X} - $a->{X};
275 $bry= $r * ($sum->{Y} - $a->{Y});
276 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
277 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
278 $b->{A}= $r * ($sum->{A} - $a->{A});
281 sub ev_lincomb ($$$$) {
282 # ev_linkcomb(RES,A,B,P)
283 # gives P*A + (1-P)*B
284 my ($r,$a,$b,$p) = @_;
286 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
289 sub a_normalise ($$) {
291 # adds or subtracts 2*$pi to/from A until it is in [ Z , Z+2*$pi >
294 $r= $z + fmod($a - $z, 2.0*$pi);
295 $r += 2*$pi if $r < $z;
298 sub ev_bearing ($$) {
300 # returns bearing of B from A
301 # value returned is in [ A->{A}, A->{A} + 2*$pi >
302 # A->{A} and B->{A} are otherwise ignored
305 $r= atan2($b->{Y} - $a->{Y},
307 $r= a_normalise($r,$a->{A});
311 sub v_rotateright ($) {
313 # returns image of A rotated 90 deg clockwise
315 return { X => $a->{Y}, Y => -$a->{X} };
317 sub v_dotproduct ($$) {
320 return $a->{X} * $b->{X} + $a->{Y} * $b->{Y};
322 sub v_scalarmult ($$) {
324 # multiplies V by scalar S and returns product
326 return { X => $s * $v->{X}, Y => $s * $v->{Y} };
330 # vector sum of all inputs
333 $r= { X => 0.0, Y => 0.0 };
334 foreach $i (@i) { $r->{X} += $i->{X}; $r->{Y} += $i->{Y}; }
337 sub v_subtract ($$) {
339 # returns vector from A to B, ie B - A
341 return { X => $b->{X} - $a->{X},
342 Y => $b->{Y} - $a->{Y} };
348 my ($x,$y) = ($v->{X}, $v->{Y});
349 return sqrt($x*$x + $y*$y);
353 # returns distance from A to B
354 return v_len(v_subtract($_[0],$_[1]));
359 $$limr= $now unless defined $$limr && $$limr <= $now;
363 $$limr= $now unless defined $$limr && $$limr >= $now;
367 my ($converter,$defaulter)=@_;
369 return &$defaulter unless @al;
371 $v= &$converter($spec);
372 dv('canf ','$spec',$spec, '$v',$v);
375 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
376 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
378 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
382 my ($min_x, $max_x, $min_y, $max_y);
384 foreach $loc (values %$objhash) {
385 upd_min(\$min_x, $loc->{X} - abs($allwidthmax * sin($loc->{A})));
386 upd_max(\$max_x, $loc->{X} + abs($allwidthmax * sin($loc->{A})));
387 upd_min(\$min_y, $loc->{Y} - abs($allwidthmax * cos($loc->{A})));
388 upd_max(\$max_y, $loc->{Y} + abs($allwidthmax * cos($loc->{A})));
390 return ($min_x, $max_x, $min_y, $max_y);
393 our %units_len= qw(- mm mm 1 cm 10 m 1000);
394 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
396 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
397 sub cva_identity ($) { my ($sp)=@_; $sp; }
398 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
399 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
403 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
404 or die "lexically invalid quantity";
406 $u=$ua->{'-'} unless length $u;
407 defined $ua->{$u} or die "unknown unit $u";
409 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
414 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
417 sub cva_subsegspec ($) {
419 die "invalid subsegment spec" unless
420 $sp =~ m,^(\-?)([0-9A-Za-z_]*)(?:/(?:([A-Za-z_]+)(\d+))?)?$,;
421 my ($sign,$segname,$movfeat,$movconf)=($1,$2,$3,$4);
422 $segname= exists $ctx->{SegName} ?
423 $sign.$ctx->{SegName}.$segname
425 $segname =~ s/^\-(.*)\-/$1/;
427 (defined $movfeat ? sprintf "%s%d", $movfeat, $movconf : '');
431 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
432 if ($sp =~ s/^(\^?)(\w+)\!//) {
435 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
436 $locs= $objs{$obj_id}{Loc};
441 $neg= $sp =~ s/^\-//;
443 die "unknown $id" unless defined $locs->{$id};
446 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
447 printf DEBUG "%s\n", $d;
449 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
453 $na= a_normalise($na,0);
454 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
461 $neg = $sp =~ s/^\-//;
463 die "duplicate $id" if exists $ctx->{Loc}{$id};
464 $ctx->{Loc}{$id}{LayerKind}= $ctx->{Layer}{Kind};
465 push @{ $ctx->{LocsMade} }, {
469 return $ctx->{Loc}{$id};
471 sub cva_cmd ($) { return cva_idstr($_[0]); }
474 return $sp if grep { $_ eq $sp } @$el;
475 die "invalid option (permitted: @$el)";
477 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
481 $nl= can(\&cva_idnew);
482 $i->{X}= can(\&cva_len);
483 $i->{Y}= can(\&cva_len);
484 $i->{A}= can(\&cva_ang);
485 ev_compose($nl, $ctx->{Trans}, $i);
488 my ($from,$to,$len,$right,$turn);
489 $from= can(\&cva_idex);
490 $to= can(\&cva_idnew);
491 $len= cano(\&cva_len,0);
492 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
493 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
494 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
495 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
500 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
504 return 'undef' if !defined $v;
505 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
506 return $v if $v =~ m/^[0-9.]+/;
507 $v =~ s/[\\\']/\\$&/g;
511 sub dv1_kind ($$$$$$$) {
512 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
514 return 0 if $ref ne $ref_exp;
516 foreach $ix (&$ixesfn) {
518 my ($v)= &$ixmapfn($ix);
519 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
520 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
523 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
528 return 0 unless $debug;
529 my ($pfx,$expr,$v) = @_;
532 #print STDERR "dv1 >$pfx|$ref<\n";
534 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
536 } elsif ($ref eq 'SCALAR') {
537 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
540 $expr.='->' unless $expr =~ m/\]$|\}$/;
541 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
542 sub { ($[ .. $#$v) },
543 sub { $v->[$_[0]] });
544 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
545 sub { sort keys %$v },
546 sub { $v->{$_[0]} });
547 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
559 sub o ($) { $o .= $_[0]; }
560 sub ol ($) { $ol .= $_[0]; }
562 return if $subsegcmapreq;
563 print $o, $ol, " showpage\n"
570 sub o_path_begin () {
572 $o_path_verb= 'moveto';
574 sub o_path_point ($) {
576 o(" $pt $o_path_verb\n");
577 $o_path_verb= 'lineto';
579 sub o_path_stroke ($) {
581 o(" $width setlinewidth stroke\n");
583 sub o_path_strokeonly () {
588 my ($a,$b,$width)=@_;
592 o_path_stroke($width);
595 sub current_draw () {
597 $r= $ctx->{Draw} =~ m/X/ ? '' : $ctx->{Draw};
601 sub psu_coords ($$$) {
602 my ($ends,$inunit,$across)=@_;
603 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
604 # $across in mm, +ve to right.
605 my (%ea_zo, $zo, $prop);
606 $ea_zo{X}=$ea_zo{Y}=0;
607 foreach $zo (qw(0 1)) {
608 $prop= $zo ? $inunit : (1.0 - $inunit);
609 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
610 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
612 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
613 # '\\%ea_zo', \%ea_zo);
614 return $ea_zo{X}." ".$ea_zo{Y};
617 sub parametric__o_pt ($) {
619 o_path_point("$pt->{X} $pt->{Y}");
622 sub segment_used_len ($) {
624 return if @segments < 3;
625 $segments[1] -= $used;
626 return if $segments[1] > 0;
627 @segments= @segments[2..$#segments];
628 o("% segments @segments\n");
631 sub parametric_segment ($$$$$) {
632 my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
633 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
634 # $lenperp is the length of one unit p, ie the curve
635 # must have a uniform `density' in parameter space
636 # $calcfn is invoked with $p set and should return a loc
637 # (ie, ref to X =>, Y =>, A =>).
638 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
639 return unless $ctx->{Draw} =~ m/[ARSCG]/;
640 $ppu= $psu_ulen/$lenperp;
641 $allwidth= allwidth($minradius);
642 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
643 my ($tickend)=($allwidth - $psu_ticklen);
644 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
645 my ($sleeperctr)=($psu_ulen*0.5);
646 my ($sleeperend)=($psu_sleeperlen*0.5);
647 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
648 $draw= current_draw();
650 my ($pt,$going,$red,$csegbare,$movfeat,$movstroke);
652 o("% segments @segments\n");
653 o(" $psu_subseglw setlinewidth\n");
657 $movstroke= " stroke\n";
658 $csegbare= $segments[0];
659 $csegbare =~ s/^\-//;
660 if ($subsegcmapreq) {
661 if (!exists $subsegcmap{$csegbare}) {
662 print "$csegbare\n" or die $!;
663 $subsegcmap{$csegbare}++;
666 $movfeat= $csegbare =~ s,(/\D+)(\d+)$,$1, ? $2 : 'f';
667 if ($subsegmovfeatpos ne $movfeat) {
668 $movstroke= "% no-stroke\n";
670 die "unknown subsegment colour for $csegbare\n"
671 unless exists $subsegcmap{$csegbare};
672 $red= $pt->{A} / (2*$pi);
675 $red += 32 if $segments[0] =~ m/^\-/;
679 $red= sprintf("%f", $red);
680 o(" $red $subsegcmap{$csegbare} setrgbcolor\n");
683 parametric__o_pt($pt);
687 segment_used_len($psu_ulen);
689 parametric__o_pt($pt);
692 segment_used_len(($p1-($param-$ppu)) * $lenperp);
694 parametric__o_pt(&$calcfn);
699 o(" $psu_thinlw setlinewidth\n");
701 for ($param=$p0; $param<$p1; $param += $ppu) {
702 parametric__o_pt(&$calcfn);
705 parametric__o_pt(&$calcfn);
708 return unless $draw =~ m/[ARS]/;
709 for ($pa= $p0; $pa<$p1; $pa=$pb) {
711 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
712 $param= $pb; $ends[1]= &$calcfn;
713 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
714 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
715 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
718 o_path_point(psu_coords(\@ends,0,-$allwidth));
719 o_path_point(psu_coords(\@ends,0,$allwidth));
720 o_path_point(psu_coords(\@ends,$e,$allwidth));
721 o_path_point(psu_coords(\@ends,$e,-$allwidth));
722 o(" closepath clip\n");
723 foreach $side qw(-1 1) {
725 o_line(psu_coords(\@ends,0,$side*$railctr),
726 psu_coords(\@ends,1.5,$side*$railctr),
731 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
732 psu_coords(\@ends,$sleeperctr,+$sleeperend),
737 foreach $side qw(-1 1) {
738 o_line(psu_coords(\@ends,0,$side*$allwidth),
739 psu_coords(\@ends,1.5,$side*$allwidth),
741 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
742 o_line(psu_coords(\@ends,$tick,$side*$allwidth),
743 psu_coords(\@ends,$tick,$side*$tickend),
753 my ($to, $ctr,$from, $radius,$delta) = @_;
754 # does parametric_segment to draw an arc centred on $ctr
755 # ($ctr->{A} ignored)
756 # from $from with radius $radius (this must be consistent!)
757 # and directionally-subtending an angle $delta.
758 # sets $to->... to be the other end, and returns $to
760 $to->{A}= $beta= $from->{A} + $delta;
761 $to->{X}= $ctr->{X} - $radius * sin($beta);
762 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
763 return if abs($delta*$radius) < 1e-9;
764 parametric_segment(0.0,1.0, abs($radius*$delta), $radius, sub {
765 my ($beta) = $from->{A} + $delta * $param;
766 return { X => $ctr->{X} - $radius * sin($beta),
767 Y => $ctr->{Y} + $radius * cos($beta),
772 # joins_xxx all take $results, $from, $to, $minradius
773 # where $results->[]{Path}{K} etc. and $results->[]{SolKinds}[]
775 sub joins_twoarcs ($$$$) {
776 my ($results, $from,$to,$minradius) = @_;
777 # two circular arcs of equal maximum possible radius
778 # algorithm courtesy of Simon Tatham (`Railway problem',
779 # pers.comm. to ijackson@chiark 23.1.2004)
780 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
781 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
782 $sigma= ev_bearing($from,$to);
783 $distfact= v_dist($from,$to);
784 $theta= 0.5 * $pi - ($from->{A} - $sigma);
785 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
786 $a= 2 * (1 + cos($theta - $phi));
787 $b= 2 * (cos($theta) - cos($phi));
789 $d= sqrt($b*$b - 4*$a*$c);
790 o("% twoarcs theta=".ang2deg($theta)." phi=".ang2deg($phi).
791 " ${a}r^2 + ${b}r + ${c} = 0\n");
792 foreach $m (qw(-1 1)) {
794 o("% twoarcs $m insoluble\n");
797 $r= -0.5 * (-$b + $m*$d) / $a;
798 $radius= -$r * $distfact;
799 o("% twoarcs $m radius $radius ");
800 if (abs($radius) < $minradius) { o("too-small\n"); next; }
801 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
802 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
803 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
804 $reverse= signum($r);
809 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
810 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
811 o("ok deltas ".ang2deg($delta1)." ".ang2deg($delta2)."\n");
817 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
818 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
819 push @$results, { Path => $path,
820 SolKinds => [ 'twoarcs', 'cross' ] };
824 sub joins_arcsline ($$$$) {
825 my ($results, $from,$to,$minradius) = @_;
826 # two circular arcs of specified radius
827 # with an intervening straight
828 my ($lr,$inv, $c,$d,$alpha,$t,$k,$l,$rpmsina,$rcosa,$linelen, $path);
829 if ($minradius<=1e-6) { o("% arcsline no-radius\n"); return; }
830 foreach $lr (qw(-1 +1)) {
831 foreach $inv (qw(-1 +1)) {
832 $c=ev_compose({},$from,{X=>0,Y=>-$lr*$minradius, A=>0 });
833 $d=ev_compose({},$to,{X=>0, Y=>-$inv*$lr*$minradius, A=>$pi });
835 o("% arcsline $lr $inv t=$t ");
836 if ($t < 1e-6) { o("concentric"); next; }
837 $c->{A}= $d->{A}= ev_bearing($c,$d);
838 o("bearing ".ang2deg($c->{A}));
841 $k= ev_compose({}, $c, { X=>0, Y=>$lr*$minradius, A=>0 });
842 $l= ev_compose({}, $d, { X=>0, Y=>$lr*$minradius, A=>0 });
845 my ($cosalpha) = 2.0 * $minradius / $t;
846 if ($cosalpha > (1.0 - 1e-6)) { o(" too-close\n"); next; }
847 $alpha= acos($cosalpha);
848 $rpmsina= $lr * $minradius * sin($alpha);
849 $rcosa= $minradius * $cosalpha;
850 $k= ev_compose({}, $c, { X=>$rcosa, Y=>$rpmsina, A=>0 });
851 $l= ev_compose({}, $d, { X=>-$rcosa, Y=>-$rpmsina, A=>0 });
852 $k->{A}= $l->{A}= ev_bearing($k,$l);
853 o(" alpha=".ang2deg($alpha)." kl^=".ang2deg($k->{A})."\n");
854 $linelen= v_dist($k,$l);
856 $path= [{ T => Arc, F => $from, C => $c,
858 D => -$lr * a_normalise
859 ($lr * ($from->{A} - $k->{A}), 0) },
860 { T => Line, A => $k, B => $l, L => $linelen },
861 { T => Arc, F => $l, C => $d,
862 R => $inv*$lr*$minradius,
863 D => -$lr*$inv * a_normalise
864 (-$lr*$inv * ($to->{A} - $l->{A}), 0) }];
867 SolKinds => [ 'arcsline', ($inv<0 ? 'cross' : 'loop') ] };
872 sub joins_arcline ($$$$) {
873 my ($results, $from,$to,$minradius) = @_;
874 # one circular arc and a straight line
875 my ($swap,$echoice,$path, $ap,$bp,$av,$bv, $e,$f, $ae,$af,$afae);
876 my ($dak,$ak,$kj,$k,$j,$aja,$jl,$l,$jc,$lc,$c,$rj,$rb);
877 foreach $swap (qw(-1 +1)) {
878 foreach $echoice (qw(0 1)) {
879 $ap= $from; $bp= { %$to }; $bp->{A} += $pi;
880 ($ap,$bp)= ($bp,$ap) if $swap<0;
881 $av= ev_byang({}, $ap->{A});
882 $bv= ev_byang({}, $bp->{A});
883 $e= ev_byang({}, 0.5 * ($ap->{A} + $bp->{A} + $echoice * $pi));
884 $f= v_rotateright($e);
885 o("% arcline $swap $echoice e ".loc2dbg($e)."\n");
886 $ae= v_dotproduct($av,$e);
887 $af= v_dotproduct($av,$f);
888 o("% arcline $swap $echoice a.e=$ae a.f=$af ");
889 if (abs($ae) < 1e-6) { o(" singular\n"); next; }
891 o("a.f/a.e=$afae\n");
892 $dak= v_dotproduct(v_subtract($ap,$bp), $e);
893 $ak= v_scalarmult($dak, $e);
894 $kj= v_scalarmult($dak * $afae, $f);
897 $aja= v_dotproduct(v_subtract($ap,$j), $av);
898 o("% arcline $swap $echoice d_ak=$dak aj.a=$aja ");
899 if ($aja < 0) { o(" backwards aj\n"); next; }
900 $jl= v_scalarmult(0.5, v_subtract($j, $bp));
901 $lc= v_scalarmult(-v_dotproduct($jl, $f) * $afae, $e);
904 $rj= v_dotproduct(v_subtract($j,$c), v_rotateright($av));
905 $rb= v_dotproduct(v_subtract($c,$bp), v_rotateright($bv));
906 o("r_j=$rj r_b=$rb ");
907 if ($rj * $rb < 0) { o(" backwards b\n"); next; }
908 if (abs($rj) < $minradius) { o(" too-small\n"); next; }
912 $path= [{ T => Line, A => $ap, B => $j, L => $aja },
913 { T => Arc, F => $j, C => $c, R => $rj,
914 D => -signum($rj) * a_normalise
915 (-signum($rj) * ($bp->{A} + $pi - $j->{A}), 0) }];
916 $path= [ reverse @$path ] if $swap<0;
917 push @$results, { Path => $path, SolKinds => [ 'arcline' ] };
923 my ($from,$to,$minradius);
924 my (@results,$result);
925 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$skl);
926 my ($crit,$cs,$i,$cmp);
927 $from= can(\&cva_idex);
928 $to= can(\&cva_idex);
929 $minradius= can(\&cva_len);
930 o("% join ".loc2dbg($from)."..".loc2dbg($to)." $minradius\n");
931 joins_twoarcs(\@results, $from,$to,$minradius);
932 joins_arcsline(\@results, $from,$to,$minradius);
933 joins_arcline(\@results, $from,$to,$minradius);
934 foreach $result (@results) {
935 $path= $result->{Path};
936 $skl= $result->{SolKinds};
937 o("% possible path @$skl $path\n");
940 foreach $segment (@$path) {
941 if ($segment->{T} eq Arc) {
942 o("% Arc C ".loc2dbg($segment->{C}).
943 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
944 $len += abs($segment->{R} * $segment->{D});
945 push @bends, -abs($segment->{R}) * $segment->{D}; # right +ve
946 } elsif ($segment->{T} eq Line) {
947 o("% Line A ".loc2dbg($segment->{A}).
948 " B ".loc2dbg($segment->{A})." L $segment->{L}\n");
949 $len += abs($segment->{L});
951 die "unknown segment $segment->{T}";
954 o("% length $len bends @bends.\n");
956 foreach $crit (@al, 'short') {
957 if ($crit eq 'long') { $cs= $len; }
958 elsif ($crit eq 'short') { $cs= -$len; }
959 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
960 if ($1 eq 'begin') { $cs= $bends[0]; }
961 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
962 else { $cs=0; map { $cs += $_ } @bends; }
963 $cs= -$cs if $2 eq 'left';
964 } elsif ($crit =~ m/^(\!?)(twoarcs|arcs?line|cross|loop)$/) {
965 $cs= !!(grep { $2 eq $_ } @$skl) != ($1 eq '!');
967 die "unknown sort criterion $crit";
971 o("% scores @$scores\n");
972 if (defined $bestpath) {
973 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
974 $cmp= $scores->[$i] <=> $bestscores->[$i];
979 $bestscores= $scores;
981 die "no solution" unless defined $bestpath;
982 o("% chose path $bestpath @al\n");
984 foreach $segment (@$bestpath) {
985 if ($segment->{T} eq 'Arc') {
986 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
987 } elsif ($segment->{T} eq 'Line') {
988 line($segment->{A}, $segment->{B}, $segment->{L});
990 die "unknown segment";
996 my ($from,$to,$len) = @_;
997 parametric_segment(0.0, 1.0, abs($len) + 1e-6, undef, sub {
998 ev_lincomb({}, $from, $to, $param);
1003 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
1004 $from= can(\&cva_idex);
1005 $to= can(\&cva_idnew);
1006 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
1007 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
1008 if ($how eq 'len') { $len= can(\&cva_len); }
1009 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
1010 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
1011 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
1012 if ($radius eq 'Inf') {
1013 # print DEBUG "extend inf $len\n";
1014 if ($how eq 'upto') {
1015 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
1016 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
1017 } elsif ($how eq 'len') {
1019 die "len of straight spec by angle";
1021 printf DEBUG "len $len\n";
1022 $to->{X}= $from->{X} + $len * cos($from->{A});
1023 $to->{Y}= $from->{Y} + $len * sin($from->{A});
1024 $to->{A}= $from->{A};
1025 line($from,$to,$len);
1027 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
1028 print DEBUG "radius >$radius<\n";
1029 $radius *= $ctx->{Trans}{R};
1030 $sign_r= signum($radius);
1032 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
1033 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
1034 if ($how eq 'upto') {
1035 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
1036 $sign_r * ($upto->{Y} - $ctr->{Y}));
1037 $beta_interval= 1.0;
1038 } elsif ($how eq 'parallel') {
1040 $beta_interval= 1.0;
1041 } elsif ($how eq 'uptoang') {
1042 $beta= input_absang($ang);
1043 $beta_interval= 2.0;
1044 } elsif ($how eq 'len') {
1045 $sign_ang= signum($len);
1046 $beta= $from->{A} - $sign_r * $len / abs($radius);
1047 $beta_interval= 2.0;
1049 $sign_ang= signum($ang);
1050 $beta= $from->{A} - $sign_r * $ang;
1051 $beta_interval= 2.0;
1053 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
1054 $beta += $sign_ang * $sign_r * 4.0 * $pi;
1056 $delta= $beta - $from->{A};
1057 last if $sign_ang * $sign_r * $delta <= 0;
1058 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
1060 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
1061 arc($to, ,$ctr,$from, $radius,$delta);
1063 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
1068 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
1071 return $_[0] * 180 / $pi;
1073 sub input_absang ($) {
1074 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
1076 sub input_abscoords ($$) {
1078 ($in->{X}, $in->{Y}) = @_;
1080 $out= ev_compose({}, $ctx->{Trans}, $in);
1081 return ($out->{X}, $out->{Y});
1085 my ($ctx_save) = @_;
1087 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
1089 DrawMap => sub { $_[0]; }
1091 %{ $ctx->{Layer} }= %{ $ctx_save->{Layer} }
1092 if defined $ctx_save;
1098 sub cmd_defobj { cmd__defobj(0); }
1099 sub cmd_defpart { cmd__defobj(1); }
1100 sub cmd__defobj ($) {
1103 $id= can(\&cva_idstr);
1104 die "nested defobj" if $defobj_save;
1105 die "repeated defobj" if exists $objs{$id};
1107 $defobj_ispart= $ispart;
1108 newctx($defobj_save);
1109 $ctx->{CmdLog}= [ ];
1110 $ctx->{InDefObj}= $id;
1111 $ctx->{Draw}= $defobj_save->{Draw}.'X';
1112 $ctx->{DrawMap}= sub { ''; };
1113 $ctx->{Layer}= { Level => 5, Kind => '' };
1118 $id= $ctx->{InDefObj};
1119 die "unmatched enddef" unless defined $id;
1120 foreach $bit (qw(CmdLog Loc)) {
1121 $objs{$id}{$bit}= $ctx->{$bit};
1123 $objs{$id}{Part}= $defobj_ispart;
1125 $defobj_save= undef;
1126 $defobj_ispart= undef;
1129 sub cmd__runobj ($) {
1133 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
1134 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
1136 next if $al[0] eq 'enddef';
1143 $ctx->{SavedSegment}= pop @segments
1144 unless exists $ctx->{SavedSegment};
1147 $csss= can(\&cva_subsegspec);
1148 $length= can(\&cva_len);
1149 push @segments, $csss, $length;
1151 $csss= can(\&cva_subsegspec);
1152 push @segments, $csss;
1155 sub layer_draw ($$) {
1160 } elsif ($k eq 's') {
1162 } elsif ($k eq 'l') {
1167 foreach $eo (@eopts) {
1168 #print STDERR "$. layer $k$l eo $eo re $eo->{GlobRe} then $eo->{DrawMods} now $r\n";
1169 next unless $k =~ m/^$eo->{GlobRe}$/;
1170 #print STDERR "$. layer $k$l eo re $eo->{GlobRe} match\n";
1171 next unless &{ $eo->{LayerCheck} }($l);
1172 #print STDERR "$. layer $k$l eo re $eo->{GlobRe} checked\n";
1173 foreach $cc (split //, $eo->{DrawMods}) {
1175 $r .= $cc if $cc =~ m/[A-Z]/;
1178 #print STDERR "layer $k$l gives $r (before map)\n";
1179 $r= &{ $ctx->{DrawMap} }($r);
1185 $kl= can(\&cva_identity);
1186 $kl =~ m/^([A-Za-z_]*)(\d*|\=|\*)$/ or die "invalid layer spec";
1188 $l= $output_layer if $l eq '*';
1189 $l= $ctx->{Layer}{Level} if $l =~ m/^\=?$/;
1190 $ctx->{Layer}{Kind}= $k;
1191 $ctx->{Layer}{Level}= $l;
1192 $ctx->{Draw}= layer_draw($k,$l);
1195 sub cmd_part { cmd__obj(Part); }
1196 sub cmd_obj { cmd__obj(1); }
1197 sub cmd_objflip { cmd__obj(-1); }
1201 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
1202 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
1204 $part_name= can(\&cva_idstr);
1205 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
1207 $obj_id= can(\&cva_idstr);
1208 if (defined $part_name) {
1209 $formal_id= can(\&cva_idstr);
1210 $actual= cano(\&cva_idex, undef);
1211 if (!defined $actual) {
1212 $actual= cva_idex("${part_name}_${formal_id}");
1215 $actual= can(\&cva_idex);
1216 $formal_id= can(\&cva_idstr);
1218 $obj= $objs{$obj_id};
1219 dv("cmd__obj ",'$obj',$obj);
1220 die "unknown obj $obj_id" unless $obj;
1221 $formal= $obj->{Loc}{$formal_id};
1222 die "unknown formal $formal_id" unless $formal;
1225 $how *= $ctx_save->{Trans}{R};
1226 $ctx->{Trans}{R}= $how;
1227 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
1228 $formcv= ev_compose({}, $ctx->{Trans},$formal);
1229 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
1230 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
1231 if (defined $part_name) {
1232 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
1234 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
1236 if ($segments[0] =~ m,(.*[^-]+)/,) {
1237 $ctx->{SegName}= $1;
1239 $ctx->{DrawMap}= sub {
1241 $i= &{ $ctx_save->{DrawMap} }($i);
1251 $ctx->{Draw}= &{ $ctx->{DrawMap} }($ctx_save->{Draw});
1252 cmd__runobj($obj_id);
1253 if (defined $part_name) {
1254 $pfx= $part_name.'_';
1256 if (@al && $al[0] eq '=') {
1257 $pfx= ''; shift @al;
1259 $pfx= cano(\&cva_idstr,undef);
1262 if (exists $ctx->{SavedSegment}) {
1263 @segments= ($ctx->{SavedSegment});
1268 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
1269 next if $id eq $formal_id;
1271 next if exists $ctx_save->{Loc}{$newid};
1272 $newpt= cva_idnew($newid);
1273 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
1276 if (defined $part_name) {
1277 my ($formalr_id, $actualr_id, $formalr, $actualr);
1279 die "part results come in pairs\n" unless @al>=2;
1280 ($formalr_id, $actualr_id, @al) = @al;
1281 if ($actualr_id =~ s/^\-//) {
1282 $formalr_id= "-$formalr_id";
1283 $formalr_id =~ s/^\-\-//;
1286 local ($ctx) = $ctx_inobj;
1287 $formalr= cva_idex($formalr_id);
1289 $actualr= cva_idnew($actualr_id);
1290 %$actualr= %$formalr;
1297 dv("cmd__do $ctx @al ",'$ctx',$ctx);
1298 $cmd= can(\&cva_cmd);
1299 my ($lm,$id,$loc,$io,$ad,$draw,$thendrawre);
1300 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
1301 o("%L cmd $io $cmd @al\n");
1302 $ctx->{LocsMade}= [ ];
1307 die "too many args" if @al;
1308 foreach $lm (@{ $ctx->{LocsMade} }) {
1310 $loc= $ctx->{Loc}{$id};
1311 $loc->{A} += $pi if $lm->{Neg};
1312 $ad= ang2deg($loc->{A});
1313 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
1314 $draw= layer_draw($loc->{LayerKind}, $ctx->{Layer}{Level});
1315 if ($draw =~ m/[LM]/) {
1317 " $loc->{X} $loc->{Y} translate $ad rotate\n");
1318 if ($draw =~ m/M/) {
1319 ol(" 0 $allwidthmin newpath moveto\n".
1320 " 0 -$allwidthmin lineto\n".
1321 " $lmu_marklw setlinewidth stroke\n");
1323 if ($draw =~ m/L/) {
1324 ol(" /s ($id) def\n".
1326 " /sx5 s stringwidth pop\n".
1327 " 0.5 mul $lmu_txtboxpadx add def\n".
1328 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
1329 " sx5 neg 0 moveto\n".
1330 " sx5 neg $lmu_txtboxh lineto\n".
1331 " sx5 $lmu_txtboxh lineto\n".
1332 " sx5 0 lineto closepath\n".
1333 " gsave 1 setgray fill grestore\n".
1334 " $lmu_txtboxlw setlinewidth stroke\n".
1335 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
1336 " moveto s show\n");
1343 sub cmd_showlibrary {
1344 my ($obj_id, $y, $x, $ctx_save, $width, $height);
1345 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
1347 $x=$olu_left; $y=$olu_bottom; undef $nxty;
1349 foreach $obj_id (sort keys %objs) {
1351 foreach $glob (@al) {
1353 $got= !($pat =~ s/^\!//);
1354 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
1355 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
1356 last if $obj_id =~ m/^$pat$/;
1360 $obj= $objs{$obj_id};
1361 next unless $obj->{Part};
1362 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
1366 $width= $max_x - $min_x;
1367 $height= $max_y - $min_y;
1368 if ($width < $height) {
1369 $ctx->{Trans}{A}= 0;
1370 $ctx->{Trans}{X}= $x - $min_x;
1371 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
1373 ($width,$height)=($height,$width);
1374 $ctx->{Trans}{A}= 0.5 * $pi;
1375 $ctx->{Trans}{X}= $x + $max_y;
1376 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
1378 $adj= length($obj_id) * $olu_textallowperc - $width;
1381 $ctx->{Trans}{X} += 0.5 * $adj;
1382 if ($x + $width > $olu_right && defined $nxty) {
1386 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
1388 $x= $olu_left; $y= $olu_bottom;
1395 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
1396 $ctx->{Draw}= $ctx_save->{Draw};
1397 cmd__runobj($obj_id);
1399 " /s ($obj_id) def\n".
1401 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
1402 " s stringwidth pop -0.5 mul 0 rmoveto\n".
1403 " s show grestore\n");
1404 $x += $width + $olu_gap_x;
1405 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
1416 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
1417 " $ps_page_shift 0 translate 90 rotate\n");
1419 if ($page_x || $page_y) {
1420 o(" /Courier-New findfont 15 scalefont setfont\n".
1421 " 30 30 moveto (${page_x}x${page_y}) show\n");
1424 o(" -$ps_page_xmul $page_x mul -$ps_page_ymul $page_y mul translate\n".
1425 " $ptscale $ptscale scale\n");
1429 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
1432 select(DEBUG); $|=1;
1433 select(STDOUT); $|=1;
1437 $ctx->{SegName}= '';
1444 chomp; s/^\s+//; s/\s+$//;
1445 @al= split /\s+/, $_;
1447 print DEBUG "=== @al\n";
1448 last if $al[0] eq 'eof';
1449 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1454 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1456 if (defined $min_x) {
1457 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1458 "height %.2d (%.2d..%2.d)\n",
1459 $max_x - $min_x, $min_x, $max_x,
1460 $max_y - $min_y, $min_y, $max_y);
1462 $bboxstr= "no locs, no bbox\n";
1464 if (!$quiet) { print STDERR $bboxstr; }
1465 $bboxstr =~ s/^/\%L bbox /mg;
1466 o($bboxstr) or die $!;
1469 my ($tick_x, $tick_y, $ticklen);
1471 printf(" gsave 0.5 setgray 0.33 setlinewidth\n".
1473 " newpath moveto\n".
1474 " -%d 0 rmoveto %d 0 rlineto\n".
1475 " -%d -%d rmoveto 0 %d rlineto stroke\n".
1477 $ticklen, $ticklen*2, $ticklen, $ticklen, $ticklen*2)
1479 for ($tick_x= $min_x; $tick_x < $max_x; $tick_x += 150) {
1480 for ($tick_y= $min_y; $tick_y < $max_y; $tick_y += 150) {
1481 printf(" %f %f regmark\n",
1486 printf(" grestore\n")