15 our $output_layer= '*';
17 our $subsegmovfeatpos='f';
18 our $subsegcmapangscale;
20 our $ps_page_shift= 615;
21 our $ps_page_xmul= 765.354;
22 our $ps_page_ymul= 538.583;
28 our $drawers= 'arsclmnog';
29 our %chdraw_emap= qw(A ARScg
43 while (@ARGV && $ARGV[0] =~ m/^\-/) {
44 last if $ARGV[0] eq '-';
49 if (s/^D(\d+)//) { $debug= $1; }
50 elsif (s/^D//) { $debug++; }
51 elsif (s/^q//) { $quiet=1; }
52 elsif (s/^l(\d+|\*)//) { $output_layer=$1; }
53 elsif (s/^S([0-9.]+)$//) { $scale= $1 * 1.0; }
54 elsif (s/^P(\d+)x(\d+)$//) { $page_x= $1; $page_y= $2; }
55 elsif (s/^GR//) { $subsegcmapreq=1; }
56 elsif (s/^GP(\d+|f)$//) { $subsegmovfeatpos=$1; }
57 elsif (s/^GL(.*)$//) {
59 my ($sscmf, $datum, $csss, $angbits);
61 $sscmf= new IO::File $sscmfn, 'r'
62 or die "$sscmfn: cannot open: $!\n";
64 $!=0; $_= <$sscmf>; die $! unless defined $_;
67 m,^C\s+(\w*/(?:[A-Za-z_]+)?)\s+(0x[0-9a-f]+)\s+(\d+)\s*$,
68 or die "$sscmfn:$.: syntax error in subseg cmap\n";
69 ($csss,$datum,$angbits)= ($1,$2,$3);
70 if (!defined $subsegcmapangscale) {
71 $subsegcmapangscale= 1<<$angbits;
73 die "angbits varies" if $subsegcmapangscale != 1<<$angbits;
76 $subsegcmap{$csss}= sprintf("%.6f %.6f",
77 (($datum >> 8) & 0xff)/255.0,
78 (($datum >> 16) & 0xff)/255.0);
80 $sscmf->error and die "$sscmfn: error reading: $!\n";
83 ((?:[a-z]|\*|\?|\[[a-z][-a-z]*\])*?)
84 (\~?) (\d*) (\=*|\-+|\++) (\d*|\*)
86 my ($ee,$g,$n,$d,$c,$v,$cc) = ($1,$2,$3,$4,$5,$6,$7);
87 my ($eo, $invert, $lfn, $ccc, $sense,$limit);
88 $g =~ s/\?/\./g; $g =~ s/\*/\.\*/g;
89 die '-[eE]GND[=]* not allowed' if $v eq '*' && length $d;
90 $d= $output_layer if !length $d;
93 $c= '=' if !length $c;
94 if (length $v && $v ne '*') {
95 die '-[eE]GN[D]CCV not allowed' if length $c > 1;
99 die '-[eE]GN+/-* not allowed' if $v eq '*';
100 $sense= ($&.'1') + 0;
101 $limit= ($sense * $d) + length($c) - 1;
103 ($output_layer eq '*' ? $d
104 : $_[0]) * $sense >= $limit
107 } elsif ($v eq '*') {
108 $lfn= sub { !$invert; };
110 $limit= length($c) - 1;
113 ($output_layer eq '*' ? 1
114 : abs($_[0] - $d) <= $limit)
118 #print STDERR "output layer $output_layer; asking re $_[0] rel $d lim $limit invert $invert result $lfn_result\n";
123 foreach $c (split //, $cc) {
125 die "bad -e option $c" unless defined $chdraw_emap{$c};
126 $ccc .= $chdraw_emap{$c};
128 die "bad -E option $c" unless $c =~ m/[$drawers]/i;
133 $eo->{LayerCheck}= $lfn;
134 $eo->{DrawMods}= $ccc;
135 #print STDERR "created eo $eo re $eo->{GlobRe} n=$n d=$d v=$v c=$c limit=$limit cc=$cc\n";
138 die "-S option must come right at the start and have numeric arg";
140 die "unknown option -$_";
145 our $ptscale= 72/25.4 / $scale;
148 our $psu_edgelw= 0.5;
149 our $psu_ticklw= 0.1;
150 our $psu_ticksperu= 1;
151 our $psu_ticklen= 5.0;
153 our $psu_sleeperlen= 17;
154 our $psu_sleeperlw= 15;
155 our $psu_raillw= 1.0;
156 our $psu_thinlw= 1.0;
157 our $psu_subseglw= 15.0;
160 our $lmu_marktpt= 11;
161 our $lmu_txtboxtxty= $lmu_marktpt * 0.300;
162 our $lmu_txtboxh= $lmu_marktpt * 1.100;
163 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
164 our $lmu_txtboxoff= $lmu_marklw / 2;
165 our $lmu_txtboxlw= 1;
167 our $olu_left= 10 * $scale;
168 our $olu_right= 217 * $scale - $olu_left;
169 our $olu_bottom= 20 * $scale;
170 our $olu_top= 270 * $scale - $olu_bottom;
173 our $olu_textheight= 15;
174 our $olu_textallowperc= $lmu_marktpt * 5.0/11;
176 our $pi= atan2(0,-1);
180 return 27 unless defined $radius;
181 $radius= abs($radius);
182 return ($radius >= 450 ? 33 :
183 $radius >= 400 ? 35 :
186 sub allwidth ($) { return allwidth2($_[0]) * 0.5; }
188 our $allwidthmax= allwidth(0);
189 our $allwidthmin= allwidth(undef);
192 # $ctx->{CmdLog}= undef } not in defobj
193 # $ctx->{CmdLog}[]= [ command args ] } in defobj
194 # $ctx->{LocsMade}[]{Id}= $id
195 # $ctx->{LocsMade}[]{Neg}= 1 or 0
196 # $ctx->{Loc}{$id}{X}
197 # $ctx->{Loc}{$id}{Y}
198 # $ctx->{Loc}{$id}{A}
199 # $ctx->{Loc}{$id}{LayerKind}
200 # $ctx->{Trans}{X} # transformation. is ev representing
201 # $ctx->{Trans}{Y} # new origin. (is applied at _input_
202 # $ctx->{Trans}{A} # not at plot-time)
203 # $ctx->{Trans}{R} # but multiply all y coords by this!
204 # $ctx->{Draw} # sequence of one or more chrs from uc $drawers
205 # # possibly including X meaning never draw
206 # # anything now (eg in defobj)
207 # $ctx->{DrawMap} # =$fn s.t.
208 # # &$fn($drawchrs_spec_by_layer_cmdline)
209 # # = $drawchrs_we_should_use_due_to_obj_etc
210 # $ctx->{SegName} # initial segment name (at start of object or file)
211 # # or nonexistent if in object in unknown segment
212 # # may have leading `-'
213 # $ctx->{SavedSegment} # exists iff segment command used, is a $csss
214 # $ctx->{Layer}{Level}
215 # $ctx->{Layer}{Kind}
219 # $objs{$id}{Part} # 1 iff object is a part
221 # $eopts[]{GlobRe} # regexp for K
222 # $eopts[]{LayerCheck} # =$fn where &$fn($l) is true iff layer matches
223 # $eopts[]{DrawMods} # modifier chars for drawing
225 # @segments= ( $csss0, $dist0, $csss1, $dist1, ..., $csssn )
226 # # here each csss may have preceding `-'
228 # $subsegcmap{$csss} = "$green $blue"
229 # # $csss is canonical subseg spec; always has '/'
233 our @al; # current cmd
238 our $param; # for parametric_curve
242 # Operate on Enhanced Vectors which are a location (coordinates) and a
243 # direction at that location. Representation is a hash with members X
244 # Y and A (angle of the direction in radians, anticlockwise from
245 # East). May be absolute, or interpreted as relative, according to
248 # Each function's first argument is a hashref whose X Y A members will
249 # be created or overwritten; this hashref will be returned (so you can
250 # use it `functionally' by passing {}). The other arguments may be ev
251 # hashrefs, or other info. The results are in general undefined if
252 # one of the arguments is the same hash as the result.
254 sub ev_byang ($$;$) {
255 # ev_byang(R, ANG,[LEN])
256 # result is evec LEN (default=1.0) from origin pointing in direction ANG
257 my ($res,$ang,$len)=@_;
258 $len=1.0 unless defined $len;
259 $res->{X}= $len * cos($ang);
260 $res->{Y}= $len * sin($ang);
264 sub ev_compose ($$$) {
265 # ev_compose(SUM_R, A,B);
266 # appends B to A, result is end of new B
267 # (B's X is forwards from end of A, Y is translating left from end of A)
268 # A may have a member R, which if provided then it should be 1.0 or -1.0,
269 # and B's Y and A will be multiplied by R first (ie, we can reflect);
270 my ($sum,$a,$b) = @_;
272 $r= defined $a->{R} ? $a->{R} : 1.0;
273 $sum->{X}= $a->{X} + $b->{X} * cos($a->{A}) - $r * $b->{Y} * sin($a->{A});
274 $sum->{Y}= $a->{Y} + $r * $b->{Y} * cos($a->{A}) + $b->{X} * sin($a->{A});
275 $sum->{A}= $a->{A} + $r * $b->{A};
278 sub ev_decompose ($$$) {
279 # ev_decompose(B_R, A,SUM)
280 # computes B_R s.t. ev_compose({}, A, B_R) gives SUM
283 $r= defined $a->{R} ? $a->{R} : 1.0;
284 $brx= $sum->{X} - $a->{X};
285 $bry= $r * ($sum->{Y} - $a->{Y});
286 $b->{X}= $brx * cos($a->{A}) + $bry * sin($a->{A});
287 $b->{Y}= $bry * cos($a->{A}) - $brx * sin($a->{A});
288 $b->{A}= $r * ($sum->{A} - $a->{A});
291 sub ev_lincomb ($$$$) {
292 # ev_linkcomb(RES,A,B,P)
293 # gives P*A + (1-P)*B
294 my ($r,$a,$b,$p) = @_;
296 map { $r->{$_} = $q * $a->{$_} + $p * $b->{$_} } qw(X Y A);
299 sub a_normalise ($$) {
301 # adds or subtracts 2*$pi to/from A until it is in [ Z , Z+2*$pi >
304 $r= $z + fmod($a - $z, 2.0*$pi);
305 $r += 2*$pi if $r < $z;
308 sub ev_bearing ($$) {
310 # returns bearing of B from A
311 # value returned is in [ A->{A}, A->{A} + 2*$pi >
312 # A->{A} and B->{A} are otherwise ignored
315 $r= atan2($b->{Y} - $a->{Y},
317 $r= a_normalise($r,$a->{A});
321 sub v_rotateright ($) {
323 # returns image of A rotated 90 deg clockwise
325 return { X => $a->{Y}, Y => -$a->{X} };
327 sub v_dotproduct ($$) {
330 return $a->{X} * $b->{X} + $a->{Y} * $b->{Y};
332 sub v_scalarmult ($$) {
334 # multiplies V by scalar S and returns product
336 return { X => $s * $v->{X}, Y => $s * $v->{Y} };
340 # vector sum of all inputs
343 $r= { X => 0.0, Y => 0.0 };
344 foreach $i (@i) { $r->{X} += $i->{X}; $r->{Y} += $i->{Y}; }
347 sub v_subtract ($$) {
349 # returns vector from A to B, ie B - A
351 return { X => $b->{X} - $a->{X},
352 Y => $b->{Y} - $a->{Y} };
358 my ($x,$y) = ($v->{X}, $v->{Y});
359 return sqrt($x*$x + $y*$y);
363 # returns distance from A to B
364 return v_len(v_subtract($_[0],$_[1]));
369 $$limr= $now unless defined $$limr && $$limr <= $now;
373 $$limr= $now unless defined $$limr && $$limr >= $now;
377 my ($converter,$defaulter)=@_;
379 return &$defaulter unless @al;
381 $v= &$converter($spec);
382 dv('canf ','$spec',$spec, '$v',$v);
385 sub can ($) { my ($c)=@_; canf($c, sub { die "too few args"; }); }
386 sub cano ($$) { my ($c,$def)=@_; canf($c, sub { return $def }); }
388 sub signum ($) { return ($_[0] > 0) - ($_[0] < 0); }
392 my ($min_x, $max_x, $min_y, $max_y);
394 foreach $loc (values %$objhash) {
395 upd_min(\$min_x, $loc->{X} - abs($allwidthmax * sin($loc->{A})));
396 upd_max(\$max_x, $loc->{X} + abs($allwidthmax * sin($loc->{A})));
397 upd_min(\$min_y, $loc->{Y} - abs($allwidthmax * cos($loc->{A})));
398 upd_max(\$max_y, $loc->{Y} + abs($allwidthmax * cos($loc->{A})));
400 return ($min_x, $max_x, $min_y, $max_y);
403 our %units_len= qw(- mm mm 1 cm 10 m 1000);
404 our %units_ang= qw(- d r 1); $units_ang{'d'}= 2*$pi / 360;
406 sub cva_len ($) { my ($sp)=@_; cva_units($sp,\%units_len); }
407 sub cva_identity ($) { my ($sp)=@_; $sp; }
408 sub cva_ang ($) { my ($sp)=@_; cva_units($sp,\%units_ang); }
409 sub cva_absang ($) { input_absang(cva_ang($_[0])) }
413 $sp =~ m/^([-0-9eE.]*[0-9.])([A-Za-z]*)$/
414 or die "lexically invalid quantity";
416 $u=$ua->{'-'} unless length $u;
417 defined $ua->{$u} or die "unknown unit $u";
419 print DEBUG "cva_units($sp,)=$r ($n $u $ua->{$u})\n";
424 die "invalid id" unless $sp =~ m/^[a-z][_0-9A-Za-z]*$/;
427 sub cva_subsegspec ($) {
429 die "invalid subsegment spec" unless
430 $sp =~ m,^(\-?)([0-9A-Za-z_]*)(?:/(?:([A-Za-z_]+)(\d+))?)?$,;
431 my ($sign,$segname,$movfeat,$movconf)=($1,$2,$3,$4);
432 $segname= exists $ctx->{SegName} ?
433 $sign.$ctx->{SegName}.$segname
435 $segname =~ s/^\-(.*)\-/$1/;
437 (defined $movfeat ? sprintf "%s%d", $movfeat, $movconf : '');
441 my ($id,$r,$d,$k,$neg,$na,$obj_id,$vflip,$locs);
442 if ($sp =~ s/^(\^?)(\w+)\!//) {
445 die "invalid obj $obj_id in loc" unless exists $objs{$obj_id};
446 $locs= $objs{$obj_id}{Loc};
451 $neg= $sp =~ s/^\-//;
453 die "unknown $id" unless defined $locs->{$id};
456 foreach $k (sort keys %$r) { $d .= " $k=$r->{$k}"; }
457 printf DEBUG "%s\n", $d;
459 $r= { X => $r->{X}, Y => -$r->{Y}, A => -$r->{A} };
463 $na= a_normalise($na,0);
464 $r= { X => $r->{X}, Y => $r->{Y}, A => $na };
471 $neg = $sp =~ s/^\-//;
473 die "duplicate $id" if exists $ctx->{Loc}{$id};
474 $ctx->{Loc}{$id}{LayerKind}= $ctx->{Layer}{Kind};
475 push @{ $ctx->{LocsMade} }, {
479 return $ctx->{Loc}{$id};
481 sub cva_cmd ($) { return cva_idstr($_[0]); }
484 return $sp if grep { $_ eq $sp } @$el;
485 die "invalid option (permitted: @$el)";
487 sub cvam_enum { my (@e) = @_; return sub { cva__enum($_[0],\@e); }; }
491 $nl= can(\&cva_idnew);
492 $i->{X}= can(\&cva_len);
493 $i->{Y}= can(\&cva_len);
494 $i->{A}= can(\&cva_ang);
495 ev_compose($nl, $ctx->{Trans}, $i);
498 my ($from,$to,$len,$right,$turn);
499 $from= can(\&cva_idex);
500 $to= can(\&cva_idnew);
501 $len= cano(\&cva_len,0);
502 $right= cano(\&cva_len,0) * $ctx->{Trans}{R};
503 $turn= cano(\&cva_ang, 0) * $ctx->{Trans}{R};
504 my ($u)= ev_compose({}, $from, { X => $len, Y => -$right, A => 0 });
505 ev_compose($to, $u, { X => 0, Y => 0, A => $turn });
510 $pfx . ($pfx =~ m/\}$|\]$/ ? '' : '->');
514 return 'undef' if !defined $v;
515 return $v if $v !~ m/\W/ && $v =~ m/[A-Z]/ && $v =~ m/^[a-z_]/i;
516 return $v if $v =~ m/^[0-9.]+/;
517 $v =~ s/[\\\']/\\$&/g;
521 sub dv1_kind ($$$$$$$) {
522 my ($pfx,$expr,$ref,$ref_exp,$ixfmt,$ixesfn,$ixmapfn) = @_;
524 return 0 if $ref ne $ref_exp;
526 foreach $ix (&$ixesfn) {
528 my ($v)= &$ixmapfn($ix);
529 #print STDERR "dv1_kind($pfx,$expr,$ref,$ref_exp,$ixmapfn) ix=$ix v=$v\n";
530 dv1($pfx,$expr.sprintf($ixfmt,dv__evr($ix)),$v);
533 printf DEBUG "%s%s= $ixfmt\n", $pfx, $expr, ' ';
538 return 0 unless $debug;
539 my ($pfx,$expr,$v) = @_;
542 #print STDERR "dv1 >$pfx|$ref<\n";
544 printf DEBUG "%s%s= %s\n", $pfx,$expr, dv__evr($v);
546 } elsif ($ref eq 'SCALAR') {
547 dv1($pfx, ($expr =~ m/^\$/ ? "\$$expr" : '${'.$expr.'}'), $$v);
550 $expr.='->' unless $expr =~ m/\]$|\}$/;
551 return if dv1_kind($pfx,$expr,$ref,'ARRAY','[%s]',
552 sub { ($[ .. $#$v) },
553 sub { $v->[$_[0]] });
554 return if dv1_kind($pfx,$expr,$ref,'HASH','{%s}',
555 sub { sort keys %$v },
556 sub { $v->{$_[0]} });
557 printf DEBUG "%s%s is %s\n", $pfx, $expr, $ref;
569 sub o ($) { $o .= $_[0]; }
570 sub ol ($) { $ol .= $_[0]; }
572 return if $subsegcmapreq;
573 print $o, $ol, " showpage\n"
580 sub o_path_begin () {
582 $o_path_verb= 'moveto';
584 sub o_path_point ($) {
586 o(" $pt $o_path_verb\n");
587 $o_path_verb= 'lineto';
589 sub o_path_stroke ($) {
591 o(" $width setlinewidth stroke\n");
593 sub o_path_strokeonly () {
598 my ($a,$b,$width)=@_;
602 o_path_stroke($width);
605 sub current_draw () {
607 $r= $ctx->{Draw} =~ m/X/ ? '' : $ctx->{Draw};
611 sub psu_coords ($$$) {
612 my ($ends,$inunit,$across)=@_;
613 # $ends->[0]{X} etc.; $inunit 0 to 1 (but go to 1.5);
614 # $across in mm, +ve to right.
615 my (%ea_zo, $zo, $prop);
616 $ea_zo{X}=$ea_zo{Y}=0;
617 foreach $zo (qw(0 1)) {
618 $prop= $zo ? $inunit : (1.0 - $inunit);
619 $ea_zo{X} += $prop * ($ends->[$zo]{X} - $across * sin($ends->[0]{A}));
620 $ea_zo{Y} += $prop * ($ends->[$zo]{Y} + $across * cos($ends->[0]{A}));
622 # dv("psu_coords ", '$ends',$ends, '$inunit',$inunit, '$across',$across,
623 # '\\%ea_zo', \%ea_zo);
624 return $ea_zo{X}." ".$ea_zo{Y};
627 sub parametric__o_pt ($) {
629 o_path_point("$pt->{X} $pt->{Y}");
632 sub segment_used_len ($) {
634 return if @segments < 3;
635 $segments[1] -= $used;
636 return if $segments[1] > 0;
637 @segments= @segments[2..$#segments];
638 o("% segments @segments\n");
641 sub parametric_segment ($$$$$) {
642 my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
643 # makes $p (global) go from $p0 to $p1 ($p1>$p0)
644 # $lenperp is the length of one unit p, ie the curve
645 # must have a uniform `density' in parameter space
646 # $calcfn is invoked with $p set and should return a loc
647 # (ie, ref to X =>, Y =>, A =>).
648 my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
649 return unless $ctx->{Draw} =~ m/[ARSCG]/;
650 $ppu= $psu_ulen/$lenperp;
651 $allwidth= allwidth($minradius);
652 my ($railctr)=($psu_gauge + $psu_raillw)*0.5;
653 my ($tickend)=($allwidth - $psu_ticklen);
654 my ($tickpitch)=($psu_ulen / $psu_ticksperu);
655 my ($sleeperctr)=($psu_ulen*0.5);
656 my ($sleeperend)=($psu_sleeperlen*0.5);
657 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
658 $draw= current_draw();
660 my ($pt,$going,$red,$csegbare,$movfeat,$movstroke);
662 o("% segments @segments\n");
663 o(" $psu_subseglw setlinewidth\n");
667 $movstroke= " stroke\n";
668 $csegbare= $segments[0];
669 $csegbare =~ s/^\-//;
670 if ($subsegcmapreq) {
671 if (!exists $subsegcmap{$csegbare}) {
672 print "$csegbare\n" or die $!;
673 $subsegcmap{$csegbare}++;
676 $movfeat= $csegbare =~ s,(/\D+)(\d+)$,$1, ? $2 : 'f';
677 if ($subsegmovfeatpos ne $movfeat) {
678 $movstroke= "% no-stroke\n";
680 die "unknown subsegment colour for $csegbare\n"
681 unless exists $subsegcmap{$csegbare};
682 $red= $pt->{A} / (2*$pi);
683 $red *= $subsegcmapangscale;
684 $red += $subsegcmapangscale*2;
685 $red += $subsegcmapangscale/2 if $segments[0] =~ m/^\-/;
686 $red %= $subsegcmapangscale;
688 $red= sprintf("%f", $red);
689 o(" $red $subsegcmap{$csegbare} setrgbcolor\n");
692 parametric__o_pt($pt);
696 segment_used_len($psu_ulen);
698 parametric__o_pt($pt);
701 segment_used_len(($p1-($param-$ppu)) * $lenperp);
703 parametric__o_pt(&$calcfn);
708 o(" $psu_thinlw setlinewidth\n");
710 for ($param=$p0; $param<$p1; $param += $ppu) {
711 parametric__o_pt(&$calcfn);
714 parametric__o_pt(&$calcfn);
717 return unless $draw =~ m/[ARS]/;
718 for ($pa= $p0; $pa<$p1; $pa=$pb) {
720 $param= $pa; $ends[0]= @ends ? $ends[1] : &$calcfn;
721 $param= $pb; $ends[1]= &$calcfn;
722 #print DEBUG "pa $pa $ends[0]{X} $ends[0]{Y} $ends[0]{A}\n";
723 #print DEBUG "pb $pb $ends[1]{X} $ends[1]{Y} $ends[1]{A}\n";
724 $e= $pb<=$p1 ? 1.0 : ($p1-$pa)/$ppu;
727 o_path_point(psu_coords(\@ends,0,-$allwidth));
728 o_path_point(psu_coords(\@ends,0,$allwidth));
729 o_path_point(psu_coords(\@ends,$e,$allwidth));
730 o_path_point(psu_coords(\@ends,$e,-$allwidth));
731 o(" closepath clip\n");
732 foreach $side qw(-1 1) {
734 o_line(psu_coords(\@ends,0,$side*$railctr),
735 psu_coords(\@ends,1.5,$side*$railctr),
740 o_line(psu_coords(\@ends,$sleeperctr,-$sleeperend),
741 psu_coords(\@ends,$sleeperctr,+$sleeperend),
746 foreach $side qw(-1 1) {
747 o_line(psu_coords(\@ends,0,$side*$allwidth),
748 psu_coords(\@ends,1.5,$side*$allwidth),
750 for ($tick=0; $tick<1.5; $tick+=$tickpitch/$psu_ulen) {
751 o_line(psu_coords(\@ends,$tick,$side*$allwidth),
752 psu_coords(\@ends,$tick,$side*$tickend),
762 my ($to, $ctr,$from, $radius,$delta) = @_;
763 # does parametric_segment to draw an arc centred on $ctr
764 # ($ctr->{A} ignored)
765 # from $from with radius $radius (this must be consistent!)
766 # and directionally-subtending an angle $delta.
767 # sets $to->... to be the other end, and returns $to
769 $to->{A}= $beta= $from->{A} + $delta;
770 $to->{X}= $ctr->{X} - $radius * sin($beta);
771 $to->{Y}= $ctr->{Y} + $radius * cos($beta);
772 return if abs($delta*$radius) < 1e-9;
773 parametric_segment(0.0,1.0, abs($radius*$delta), $radius, sub {
774 my ($beta) = $from->{A} + $delta * $param;
775 return { X => $ctr->{X} - $radius * sin($beta),
776 Y => $ctr->{Y} + $radius * cos($beta),
781 # joins_xxx all take $results, $from, $to, $minradius
782 # where $results->[]{Path}{K} etc. and $results->[]{SolKinds}[]
784 sub joins_twoarcs ($$$$) {
785 my ($results, $from,$to,$minradius) = @_;
786 # two circular arcs of equal maximum possible radius
787 # algorithm courtesy of Simon Tatham (`Railway problem',
788 # pers.comm. to ijackson@chiark 23.1.2004)
789 my ($sigma,$distfact, $theta,$phi, $a,$b,$c,$d, $m,$r, $radius);
790 my ($cvec,$cfrom,$cto,$midpt, $delta1,$delta2, $path,$reverse);
791 $sigma= ev_bearing($from,$to);
792 $distfact= v_dist($from,$to);
793 $theta= 0.5 * $pi - ($from->{A} - $sigma);
794 $phi= 0.5 * $pi - ($to->{A} + $pi - $sigma);
795 $a= 2 * (1 + cos($theta - $phi));
796 $b= 2 * (cos($theta) - cos($phi));
798 $d= sqrt($b*$b - 4*$a*$c);
799 o("% twoarcs theta=".ang2deg($theta)." phi=".ang2deg($phi).
800 " ${a}r^2 + ${b}r + ${c} = 0\n");
801 foreach $m (qw(-1 1)) {
803 o("% twoarcs $m insoluble\n");
806 $r= -0.5 * (-$b + $m*$d) / $a;
807 $radius= -$r * $distfact;
808 o("% twoarcs $m radius $radius ");
809 if (abs($radius) < $minradius) { o("too-small\n"); next; }
810 $cfrom= ev_compose({}, $from, { X=>0, Y=>-$radius, A=>-0.5*$pi });
811 $cto= ev_compose({}, $to, { X=>0, Y=> $radius, A=> 0.5*$pi });
812 $midpt= ev_lincomb({}, $cfrom, $cto, 0.5);
813 $reverse= signum($r);
818 $delta1= ev_bearing($cfrom, $midpt) - $cfrom->{A};
819 $delta2= ev_bearing($cto, $midpt) - $cto->{A};
820 o("ok deltas ".ang2deg($delta1)." ".ang2deg($delta2)."\n");
826 $path= [{ T=>Arc, F=>$from, C=>$cfrom, R=> $radius, D=>$delta1 },
827 { T=>Arc, F=>$to, C=>$cto, R=>-$radius, D=>$delta2 }];
828 push @$results, { Path => $path,
829 SolKinds => [ 'twoarcs', 'cross' ] };
833 sub joins_arcsline ($$$$) {
834 my ($results, $from,$to,$minradius) = @_;
835 # two circular arcs of specified radius
836 # with an intervening straight
837 my ($lr,$inv, $c,$d,$alpha,$t,$k,$l,$rpmsina,$rcosa,$linelen, $path);
838 if ($minradius<=1e-6) { o("% arcsline no-radius\n"); return; }
839 foreach $lr (qw(-1 +1)) {
840 foreach $inv (qw(-1 +1)) {
841 $c=ev_compose({},$from,{X=>0,Y=>-$lr*$minradius, A=>0 });
842 $d=ev_compose({},$to,{X=>0, Y=>-$inv*$lr*$minradius, A=>$pi });
844 o("% arcsline $lr $inv t=$t ");
845 if ($t < 1e-6) { o("concentric"); next; }
846 $c->{A}= $d->{A}= ev_bearing($c,$d);
847 o("bearing ".ang2deg($c->{A}));
850 $k= ev_compose({}, $c, { X=>0, Y=>$lr*$minradius, A=>0 });
851 $l= ev_compose({}, $d, { X=>0, Y=>$lr*$minradius, A=>0 });
854 my ($cosalpha) = 2.0 * $minradius / $t;
855 if ($cosalpha > (1.0 - 1e-6)) { o(" too-close\n"); next; }
856 $alpha= acos($cosalpha);
857 $rpmsina= $lr * $minradius * sin($alpha);
858 $rcosa= $minradius * $cosalpha;
859 $k= ev_compose({}, $c, { X=>$rcosa, Y=>$rpmsina, A=>0 });
860 $l= ev_compose({}, $d, { X=>-$rcosa, Y=>-$rpmsina, A=>0 });
861 $k->{A}= $l->{A}= ev_bearing($k,$l);
862 o(" alpha=".ang2deg($alpha)." kl^=".ang2deg($k->{A})."\n");
863 $linelen= v_dist($k,$l);
865 $path= [{ T => Arc, F => $from, C => $c,
867 D => -$lr * a_normalise
868 ($lr * ($from->{A} - $k->{A}), 0) },
869 { T => Line, A => $k, B => $l, L => $linelen },
870 { T => Arc, F => $l, C => $d,
871 R => $inv*$lr*$minradius,
872 D => -$lr*$inv * a_normalise
873 (-$lr*$inv * ($to->{A} - $l->{A}), 0) }];
876 SolKinds => [ 'arcsline', ($inv<0 ? 'cross' : 'loop') ] };
881 sub joins_arcline ($$$$) {
882 my ($results, $from,$to,$minradius) = @_;
883 # one circular arc and a straight line
884 my ($swap,$echoice,$path, $ap,$bp,$av,$bv, $e,$f, $ae,$af,$afae);
885 my ($dak,$ak,$kj,$k,$j,$aja,$jl,$l,$jc,$lc,$c,$rj,$rb);
886 foreach $swap (qw(-1 +1)) {
887 foreach $echoice (qw(0 1)) {
888 $ap= $from; $bp= { %$to }; $bp->{A} += $pi;
889 ($ap,$bp)= ($bp,$ap) if $swap<0;
890 $av= ev_byang({}, $ap->{A});
891 $bv= ev_byang({}, $bp->{A});
892 $e= ev_byang({}, 0.5 * ($ap->{A} + $bp->{A} + $echoice * $pi));
893 $f= v_rotateright($e);
894 o("% arcline $swap $echoice e ".loc2dbg($e)."\n");
895 $ae= v_dotproduct($av,$e);
896 $af= v_dotproduct($av,$f);
897 o("% arcline $swap $echoice a.e=$ae a.f=$af ");
898 if (abs($ae) < 1e-6) { o(" singular\n"); next; }
900 o("a.f/a.e=$afae\n");
901 $dak= v_dotproduct(v_subtract($ap,$bp), $e);
902 $ak= v_scalarmult($dak, $e);
903 $kj= v_scalarmult($dak * $afae, $f);
906 $aja= v_dotproduct(v_subtract($ap,$j), $av);
907 o("% arcline $swap $echoice d_ak=$dak aj.a=$aja ");
908 if ($aja < 0) { o(" backwards aj\n"); next; }
909 $jl= v_scalarmult(0.5, v_subtract($j, $bp));
910 $lc= v_scalarmult(-v_dotproduct($jl, $f) * $afae, $e);
913 $rj= v_dotproduct(v_subtract($j,$c), v_rotateright($av));
914 $rb= v_dotproduct(v_subtract($c,$bp), v_rotateright($bv));
915 o("r_j=$rj r_b=$rb ");
916 if ($rj * $rb < 0) { o(" backwards b\n"); next; }
917 if (abs($rj) < $minradius) { o(" too-small\n"); next; }
921 $path= [{ T => Line, A => $ap, B => $j, L => $aja },
922 { T => Arc, F => $j, C => $c, R => $rj,
923 D => -signum($rj) * a_normalise
924 (-signum($rj) * ($bp->{A} + $pi - $j->{A}), 0) }];
925 $path= [ reverse @$path ] if $swap<0;
926 push @$results, { Path => $path, SolKinds => [ 'arcline' ] };
932 my ($from,$to,$minradius);
933 my (@results,$result);
934 my ($path,$segment,$bestpath,$len,$scores,$bestscores,@bends,$skl);
935 my ($crit,$cs,$i,$cmp);
936 $from= can(\&cva_idex);
937 $to= can(\&cva_idex);
938 $minradius= can(\&cva_len);
939 o("% join ".loc2dbg($from)."..".loc2dbg($to)." $minradius\n");
940 joins_twoarcs(\@results, $from,$to,$minradius);
941 joins_arcsline(\@results, $from,$to,$minradius);
942 joins_arcline(\@results, $from,$to,$minradius);
943 foreach $result (@results) {
944 $path= $result->{Path};
945 $skl= $result->{SolKinds};
946 o("% possible path @$skl $path\n");
949 foreach $segment (@$path) {
950 if ($segment->{T} eq Arc) {
951 o("% Arc C ".loc2dbg($segment->{C}).
952 " R $segment->{R} D ".ang2deg($segment->{D})."\n");
953 $len += abs($segment->{R} * $segment->{D});
954 push @bends, -abs($segment->{R}) * $segment->{D}; # right +ve
955 } elsif ($segment->{T} eq Line) {
956 o("% Line A ".loc2dbg($segment->{A}).
957 " B ".loc2dbg($segment->{A})." L $segment->{L}\n");
958 $len += abs($segment->{L});
960 die "unknown segment $segment->{T}";
963 o("% length $len bends @bends.\n");
965 foreach $crit (@al, 'short') {
966 if ($crit eq 'long') { $cs= $len; }
967 elsif ($crit eq 'short') { $cs= -$len; }
968 elsif ($crit =~ m/^(begin|end|)(left|right)$/) {
969 if ($1 eq 'begin') { $cs= $bends[0]; }
970 elsif ($1 eq 'end') { $cs= $bends[$#bends]; }
971 else { $cs=0; map { $cs += $_ } @bends; }
972 $cs= -$cs if $2 eq 'left';
973 } elsif ($crit =~ m/^(\!?)(twoarcs|arcs?line|cross|loop)$/) {
974 $cs= !!(grep { $2 eq $_ } @$skl) != ($1 eq '!');
976 die "unknown sort criterion $crit";
980 o("% scores @$scores\n");
981 if (defined $bestpath) {
982 for ($i=0,$cmp=0; !$cmp && $i<@$scores; $i++) {
983 $cmp= $scores->[$i] <=> $bestscores->[$i];
988 $bestscores= $scores;
990 die "no solution" unless defined $bestpath;
991 o("% chose path $bestpath @al\n");
993 foreach $segment (@$bestpath) {
994 if ($segment->{T} eq 'Arc') {
995 arc({}, $segment->{C},$segment->{F},$segment->{R},$segment->{D});
996 } elsif ($segment->{T} eq 'Line') {
997 line($segment->{A}, $segment->{B}, $segment->{L});
999 die "unknown segment";
1005 my ($from,$to,$len) = @_;
1006 parametric_segment(0.0, 1.0, abs($len) + 1e-6, undef, sub {
1007 ev_lincomb({}, $from, $to, $param);
1012 my ($from,$to,$radius,$len,$upto,$ctr,$beta,$ang,$how,$sign_r);
1013 $from= can(\&cva_idex);
1014 $to= can(\&cva_idnew);
1015 printf DEBUG "from $from->{X} $from->{Y} $from->{A}\n";
1016 $how= can(cvam_enum(qw(len upto ang uptoang parallel)));
1017 if ($how eq 'len') { $len= can(\&cva_len); }
1018 elsif ($how =~ m/ang$/) { $ang= can(\&cva_ang); }
1019 elsif ($how eq 'parallel' || $how eq 'upto') { $upto= can(\&cva_idex); }
1020 $radius= cano(\&cva_len, 'Inf'); # +ve is right hand bend
1021 if ($radius eq 'Inf') {
1022 # print DEBUG "extend inf $len\n";
1023 if ($how eq 'upto') {
1024 $len= ($upto->{X} - $from->{X}) * cos($from->{A})
1025 + ($upto->{Y} - $from->{Y}) * sin($from->{A});
1026 } elsif ($how eq 'len') {
1028 die "len of straight spec by angle";
1030 printf DEBUG "len $len\n";
1031 $to->{X}= $from->{X} + $len * cos($from->{A});
1032 $to->{Y}= $from->{Y} + $len * sin($from->{A});
1033 $to->{A}= $from->{A};
1034 line($from,$to,$len);
1036 my ($sign_r, $sign_ang, $ctr, $beta_interval, $beta, $delta);
1037 print DEBUG "radius >$radius<\n";
1038 $radius *= $ctx->{Trans}{R};
1039 $sign_r= signum($radius);
1041 $ctr->{X}= $from->{X} + $radius * sin($from->{A});
1042 $ctr->{Y}= $from->{Y} - $radius * cos($from->{A});
1043 if ($how eq 'upto') {
1044 $beta= atan2(-$sign_r * ($upto->{X} - $ctr->{X}),
1045 $sign_r * ($upto->{Y} - $ctr->{Y}));
1046 $beta_interval= 1.0;
1047 } elsif ($how eq 'parallel') {
1049 $beta_interval= 1.0;
1050 } elsif ($how eq 'uptoang') {
1051 $beta= input_absang($ang);
1052 $beta_interval= 2.0;
1053 } elsif ($how eq 'len') {
1054 $sign_ang= signum($len);
1055 $beta= $from->{A} - $sign_r * $len / abs($radius);
1056 $beta_interval= 2.0;
1058 $sign_ang= signum($ang);
1059 $beta= $from->{A} - $sign_r * $ang;
1060 $beta_interval= 2.0;
1062 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
1063 $beta += $sign_ang * $sign_r * 4.0 * $pi;
1065 $delta= $beta - $from->{A};
1066 last if $sign_ang * $sign_r * $delta <= 0;
1067 $beta -= $sign_ang * $sign_r * $beta_interval * $pi;
1069 printf DEBUG "ctr->{Y}=$ctr->{Y} radius=$radius beta=$beta\n";
1070 arc($to, ,$ctr,$from, $radius,$delta);
1072 printf DEBUG "to $to->{X} $to->{Y} $to->{A}\n";
1077 return "$loc->{X} $loc->{Y} ".ang2deg($loc->{A});
1080 return $_[0] * 180 / $pi;
1082 sub input_absang ($) {
1083 return $_[0] * $ctx->{Trans}{R} + $ctx->{Trans}{A};
1085 sub input_abscoords ($$) {
1087 ($in->{X}, $in->{Y}) = @_;
1089 $out= ev_compose({}, $ctx->{Trans}, $in);
1090 return ($out->{X}, $out->{Y});
1094 my ($ctx_save) = @_;
1096 Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
1098 DrawMap => sub { $_[0]; }
1100 %{ $ctx->{Layer} }= %{ $ctx_save->{Layer} }
1101 if defined $ctx_save;
1107 sub cmd_defobj { cmd__defobj(0); }
1108 sub cmd_defpart { cmd__defobj(1); }
1109 sub cmd__defobj ($) {
1112 $id= can(\&cva_idstr);
1113 die "nested defobj" if $defobj_save;
1114 die "repeated defobj" if exists $objs{$id};
1116 $defobj_ispart= $ispart;
1117 newctx($defobj_save);
1118 $ctx->{CmdLog}= [ ];
1119 $ctx->{InDefObj}= $id;
1120 $ctx->{Draw}= $defobj_save->{Draw}.'X';
1121 $ctx->{DrawMap}= sub { ''; };
1122 $ctx->{Layer}= { Level => 5, Kind => '' };
1127 $id= $ctx->{InDefObj};
1128 die "unmatched enddef" unless defined $id;
1129 foreach $bit (qw(CmdLog Loc)) {
1130 $objs{$id}{$bit}= $ctx->{$bit};
1132 $objs{$id}{Part}= $defobj_ispart;
1134 $defobj_save= undef;
1135 $defobj_ispart= undef;
1138 sub cmd__runobj ($) {
1142 dv("cmd__runobj $obj_id ",'$ctx',$ctx);
1143 foreach $c (@{ $objs{$obj_id}{CmdLog} }) {
1145 next if $al[0] eq 'enddef';
1152 $ctx->{SavedSegment}= pop @segments
1153 unless exists $ctx->{SavedSegment};
1156 $csss= can(\&cva_subsegspec);
1157 $length= can(\&cva_len);
1158 push @segments, $csss, $length;
1160 $csss= can(\&cva_subsegspec);
1161 push @segments, $csss;
1164 sub layer_draw ($$) {
1169 } elsif ($k eq 's') {
1171 } elsif ($k eq 'l') {
1176 foreach $eo (@eopts) {
1177 #print STDERR "$. layer $k$l eo $eo re $eo->{GlobRe} then $eo->{DrawMods} now $r\n";
1178 next unless $k =~ m/^$eo->{GlobRe}$/;
1179 #print STDERR "$. layer $k$l eo re $eo->{GlobRe} match\n";
1180 next unless &{ $eo->{LayerCheck} }($l);
1181 #print STDERR "$. layer $k$l eo re $eo->{GlobRe} checked\n";
1182 foreach $cc (split //, $eo->{DrawMods}) {
1184 $r .= $cc if $cc =~ m/[A-Z]/;
1187 #print STDERR "layer $k$l gives $r (before map)\n";
1188 $r= &{ $ctx->{DrawMap} }($r);
1194 $kl= can(\&cva_identity);
1195 $kl =~ m/^([A-Za-z_]*)(\d*|\=|\*)$/ or die "invalid layer spec";
1197 $l= $output_layer if $l eq '*';
1198 $l= $ctx->{Layer}{Level} if $l =~ m/^\=?$/;
1199 $ctx->{Layer}{Kind}= $k;
1200 $ctx->{Layer}{Level}= $l;
1201 $ctx->{Draw}= layer_draw($k,$l);
1204 sub cmd_part { cmd__obj(Part); }
1205 sub cmd_obj { cmd__obj(1); }
1206 sub cmd_objflip { cmd__obj(-1); }
1210 my ($obj_id, $ctx_save, $pfx, $actual, $formal_id, $formal, $formcv);
1211 my ($part_name, $ctx_inobj, $obj, $id, $newid, $newpt);
1213 $part_name= can(\&cva_idstr);
1214 $how= (@al && $al[0] =~ s/^\^//) ? -1 : +1;
1216 $obj_id= can(\&cva_idstr);
1217 if (defined $part_name) {
1218 $formal_id= can(\&cva_idstr);
1219 $actual= cano(\&cva_idex, undef);
1220 if (!defined $actual) {
1221 $actual= cva_idex("${part_name}_${formal_id}");
1224 $actual= can(\&cva_idex);
1225 $formal_id= can(\&cva_idstr);
1227 $obj= $objs{$obj_id};
1228 dv("cmd__obj ",'$obj',$obj);
1229 die "unknown obj $obj_id" unless $obj;
1230 $formal= $obj->{Loc}{$formal_id};
1231 die "unknown formal $formal_id" unless $formal;
1234 $how *= $ctx_save->{Trans}{R};
1235 $ctx->{Trans}{R}= $how;
1236 $ctx->{Trans}{A}= $actual->{A} - $formal->{A}/$how;
1237 $formcv= ev_compose({}, $ctx->{Trans},$formal);
1238 $ctx->{Trans}{X}= $actual->{X} - $formcv->{X};
1239 $ctx->{Trans}{Y}= $actual->{Y} - $formcv->{Y};
1240 if (defined $part_name) {
1241 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${part_name}:";
1243 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
1245 if ($segments[0] =~ m,(.*[^-]+)/,) {
1246 $ctx->{SegName}= $1;
1248 $ctx->{DrawMap}= sub {
1250 $i= &{ $ctx_save->{DrawMap} }($i);
1260 $ctx->{Draw}= &{ $ctx->{DrawMap} }($ctx_save->{Draw});
1261 cmd__runobj($obj_id);
1262 if (defined $part_name) {
1263 $pfx= $part_name.'_';
1265 if (@al && $al[0] eq '=') {
1266 $pfx= ''; shift @al;
1268 $pfx= cano(\&cva_idstr,undef);
1271 if (exists $ctx->{SavedSegment}) {
1272 @segments= ($ctx->{SavedSegment});
1277 foreach $id (keys %{ $ctx_inobj->{Loc} }) {
1278 next if $id eq $formal_id;
1280 next if exists $ctx_save->{Loc}{$newid};
1281 $newpt= cva_idnew($newid);
1282 %$newpt= %{ $ctx_inobj->{Loc}{$id} };
1285 if (defined $part_name) {
1286 my ($formalr_id, $actualr_id, $formalr, $actualr);
1288 die "part results come in pairs\n" unless @al>=2;
1289 ($formalr_id, $actualr_id, @al) = @al;
1290 if ($actualr_id =~ s/^\-//) {
1291 $formalr_id= "-$formalr_id";
1292 $formalr_id =~ s/^\-\-//;
1295 local ($ctx) = $ctx_inobj;
1296 $formalr= cva_idex($formalr_id);
1298 $actualr= cva_idnew($actualr_id);
1299 %$actualr= %$formalr;
1306 dv("cmd__do $ctx @al ",'$ctx',$ctx);
1307 $cmd= can(\&cva_cmd);
1308 my ($lm,$id,$loc,$io,$ad,$draw,$thendrawre);
1309 $io= defined $ctx->{InDefObj} ? "$ctx->{InDefObj}!" : $ctx->{InRunObj};
1310 o("%L cmd $io $cmd @al\n");
1311 $ctx->{LocsMade}= [ ];
1316 die "too many args" if @al;
1317 foreach $lm (@{ $ctx->{LocsMade} }) {
1319 $loc= $ctx->{Loc}{$id};
1320 $loc->{A} += $pi if $lm->{Neg};
1321 $ad= ang2deg($loc->{A});
1322 ol("%L point $io$id ".loc2dbg($loc)." ($lm->{Neg})\n");
1323 $draw= layer_draw($loc->{LayerKind}, $ctx->{Layer}{Level});
1324 if ($draw =~ m/[LM]/) {
1326 " $loc->{X} $loc->{Y} translate $ad rotate\n");
1327 if ($draw =~ m/M/) {
1328 ol(" 0 $allwidthmin newpath moveto\n".
1329 " 0 -$allwidthmin lineto\n".
1330 " $lmu_marklw setlinewidth stroke\n");
1332 if ($draw =~ m/L/) {
1333 ol(" /s ($id) def\n".
1335 " /sx5 s stringwidth pop\n".
1336 " 0.5 mul $lmu_txtboxpadx add def\n".
1337 " -90 rotate 0 $lmu_txtboxoff translate newpath\n".
1338 " sx5 neg 0 moveto\n".
1339 " sx5 neg $lmu_txtboxh lineto\n".
1340 " sx5 $lmu_txtboxh lineto\n".
1341 " sx5 0 lineto closepath\n".
1342 " gsave 1 setgray fill grestore\n".
1343 " $lmu_txtboxlw setlinewidth stroke\n".
1344 " sx5 neg $lmu_txtboxpadx add $lmu_txtboxtxty\n".
1345 " moveto s show\n");
1352 sub cmd_showlibrary {
1353 my ($obj_id, $y, $x, $ctx_save, $width, $height);
1354 my ($max_x, $min_x, $max_y, $min_y, $nxty, $obj, $loc, $pat, $got, $glob);
1356 $x=$olu_left; $y=$olu_bottom; undef $nxty;
1358 foreach $obj_id (sort keys %objs) {
1360 foreach $glob (@al) {
1362 $got= !($pat =~ s/^\!//);
1363 die "bad pat" if $pat =~ m/[^0-9a-zA-Z_*?]/;
1364 $pat =~ s/\*/\.*/g; $pat =~ s/\?/./g;
1365 last if $obj_id =~ m/^$pat$/;
1369 $obj= $objs{$obj_id};
1370 next unless $obj->{Part};
1371 ($min_x, $max_x, $min_y, $max_y) = bbox($obj->{Loc});
1375 $width= $max_x - $min_x;
1376 $height= $max_y - $min_y;
1377 if ($width < $height) {
1378 $ctx->{Trans}{A}= 0;
1379 $ctx->{Trans}{X}= $x - $min_x;
1380 $ctx->{Trans}{Y}= $y - $min_y + $olu_textheight;
1382 ($width,$height)=($height,$width);
1383 $ctx->{Trans}{A}= 0.5 * $pi;
1384 $ctx->{Trans}{X}= $x + $max_y;
1385 $ctx->{Trans}{Y}= $y - $min_x + $olu_textheight;
1387 $adj= length($obj_id) * $olu_textallowperc - $width;
1390 $ctx->{Trans}{X} += 0.5 * $adj;
1391 if ($x + $width > $olu_right && defined $nxty) {
1395 } elsif ($y + $height > $olu_top && $y > $olu_bottom) {
1397 $x= $olu_left; $y= $olu_bottom;
1404 $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}//";
1405 $ctx->{Draw}= $ctx_save->{Draw};
1406 cmd__runobj($obj_id);
1408 " /s ($obj_id) def\n".
1410 ($x + 0.5*$width)." ".($y - $olu_textheight)." moveto\n".
1411 " s stringwidth pop -0.5 mul 0 rmoveto\n".
1412 " s show grestore\n");
1413 $x += $width + $olu_gap_x;
1414 upd_max(\$nxty, $y + $height + $olu_gap_y + $olu_textheight);
1425 " /lf /Courier-New findfont $lmu_marktpt scalefont def\n".
1426 " $ps_page_shift 0 translate 90 rotate\n");
1428 if ($page_x || $page_y) {
1429 o(" /Courier-New findfont 15 scalefont setfont\n".
1430 " 30 30 moveto (${page_x}x${page_y}) show\n");
1433 o(" -$ps_page_xmul $page_x mul -$ps_page_ymul $page_y mul translate\n".
1434 " $ptscale $ptscale scale\n");
1438 open DEBUG, ($debug ? ">&2" : ">/dev/null") or die $!;
1441 select(DEBUG); $|=1;
1442 select(STDOUT); $|=1;
1446 $ctx->{SegName}= '';
1453 chomp; s/^\s+//; s/\s+$//;
1454 @al= split /\s+/, $_;
1456 print DEBUG "=== @al\n";
1457 last if $al[0] eq 'eof';
1458 push @{ $ctx->{CmdLog} }, [ @al ] if exists $ctx->{CmdLog};
1463 my ($min_x, $max_x, $min_y, $max_y) = bbox($ctx->{Loc});
1465 if (defined $min_x) {
1466 $bboxstr= sprintf("width %.2d (%.2d..%2.d)\n".
1467 "height %.2d (%.2d..%2.d)\n",
1468 $max_x - $min_x, $min_x, $max_x,
1469 $max_y - $min_y, $min_y, $max_y);
1471 $bboxstr= "no locs, no bbox\n";
1473 if (!$quiet) { print STDERR $bboxstr; }
1474 $bboxstr =~ s/^/\%L bbox /mg;
1475 o($bboxstr) or die $!;
1478 my ($tick_x, $tick_y, $ticklen);
1480 printf(" gsave 0.5 setgray 0.33 setlinewidth\n".
1482 " newpath moveto\n".
1483 " -%d 0 rmoveto %d 0 rlineto\n".
1484 " -%d -%d rmoveto 0 %d rlineto stroke\n".
1486 $ticklen, $ticklen*2, $ticklen, $ticklen, $ticklen*2)
1488 for ($tick_x= $min_x; $tick_x < $max_x; $tick_x += 150) {
1489 for ($tick_y= $min_y; $tick_y < $max_y; $tick_y += 150) {
1490 printf(" %f %f regmark\n",
1495 printf(" grestore\n")