chiark / gitweb /
improved distance label placement when centrelines drawn
[trains.git] / layout / layout
index 20f4ede294ed9c2d7d3397921c0602697f331e89..31d1daf83046261aadb66e548b9abe00225e4fe0 100755 (executable)
@@ -1,6 +1,9 @@
 #!/usr/bin/perl -w
 
 use POSIX;
+use IO::Handle;
+use IO::File;
+
 use strict;
 no strict 'subs';
 
@@ -11,6 +14,8 @@ our $quiet=0;
 our $debug=0;
 our $output_layer= '*';
 our $subsegcmapreq=0;
+our $subsegmovfeatpos='f';
+our $subsegcmapangscale;
 
 our $ps_page_shift= 615;
 our $ps_page_xmul= 765.354;
@@ -20,12 +25,12 @@ our @eopts;
 our @segments= ('/');
 our %subsegcmap;
 
-our $drawers= 'arsclmnog';
-our %chdraw_emap= qw(A ARScg
-                    R aRscg
-                    S aRScg
-                    C arsCg
-                    c Arscg
+our $drawers= 'arscldmnog';
+our %chdraw_emap= qw(A ARScgd
+                    R aRscgD
+                    S aRScgd
+                    C arsCgd
+                    c Arscgd
                     r arcs
                     L LMg
                     l l
@@ -47,17 +52,33 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) {
        elsif (s/^l(\d+|\*)//) { $output_layer=$1; }
        elsif (s/^S([0-9.]+)$//) { $scale= $1 * 1.0; }
        elsif (s/^P(\d+)x(\d+)$//) { $page_x= $1; $page_y= $2; }
-       elsif (s/^GL//) { $subsegcmapreq=1; }
-       elsif (s/^GR(.*)$//) {
+       elsif (s/^GR//) { $subsegcmapreq=1; }
+       elsif (s/^GP(\d+|f)$//) { $subsegmovfeatpos=$1; }
+       elsif (s/^GL(.*)$//) {
            my ($sscmfn) = $1;
-           my ($sscmf);
+           my ($sscmf, $datum, $csss, $angbits);
            local ($_);
            $sscmf= new IO::File $sscmfn, 'r'
                or die "$sscmfn: cannot open: $!\n";
-           while (<$sscmf>) {
-               m,^\s*(\w+/(?:[a-z]+\d+))\s+(\S.*\S)\s*$,
+           for (;;) {
+               $!=0; $_= <$sscmf>; die $! unless defined $_;
+               last if m/^E/;
+               next unless m/^C/;
+               m,^C\s+(\w*/(?:[A-Za-z_]+)?)\s+(0x[0-9a-f]+)\s+(\d+)\s*$,
                    or die "$sscmfn:$.: syntax error in subseg cmap\n";
-               $subsegcmap{$1}= $2;
+               ($csss,$datum,$angbits)= ($1,$2,$3);
+               if (!defined $subsegcmapangscale) {
+                   $subsegcmapangscale= 1<<$angbits;
+               } else {
+                   die "angbits varies" if $subsegcmapangscale != 1<<$angbits;
+               }
+               $datum= hex($datum);
+               if ($datum & 0x0ff) {
+                   die "sorry, cannot put any movfeatpos or segment in red";
+               }
+               $subsegcmap{$csss}= sprintf("%.6f %.6f",
+                                           (($datum >> 8) & 0xff)/255.0,
+                                           (($datum >> 16) & 0xff)/255.0);
            }
            $sscmf->error and die "$sscmfn: error reading: $!\n";
            close $sscmf;
@@ -136,7 +157,9 @@ our $psu_sleeperlen= 17;
 our $psu_sleeperlw= 15;
 our $psu_raillw= 1.0;
 our $psu_thinlw= 1.0;
-our $psu_subseglw= 2.0;
+our %psu_subseglw;
+$psu_subseglw{'e'}= 20.0;
+$psu_subseglw{'m'}= 15.0;
 
 our $lmu_marklw= 4;
 our $lmu_marktpt= 11;
@@ -145,6 +168,8 @@ our $lmu_txtboxh= $lmu_marktpt * 1.100;
 our $lmu_txtboxpadx= $lmu_marktpt * 0.335;
 our $lmu_txtboxoff= $lmu_marklw / 2;
 our $lmu_txtboxlw= 1;
+our $lmu_lenlabeloffctr= -$lmu_marklw * 1.0;
+our $lmu_lenlabeloff=     $lmu_marklw * 0.5;
 
 our $olu_left= 10 * $scale;
 our $olu_right= 217 * $scale - $olu_left;
@@ -173,6 +198,7 @@ our $allwidthmin= allwidth(undef);
 # Data structures:
 #  $ctx->{CmdLog}= undef                  } not in defobj
 #  $ctx->{CmdLog}[]= [ command args ]     } in defobj
+#  $ctx->{Parent}= $parent_ctx or undef
 #  $ctx->{LocsMade}[]{Id}= $id
 #  $ctx->{LocsMade}[]{Neg}= 1 or 0
 #  $ctx->{Loc}{$id}{X}
@@ -190,6 +216,11 @@ our $allwidthmin= allwidth(undef);
 #                         #  &$fn($drawchrs_spec_by_layer_cmdline)
 #                         #   = $drawchrs_we_should_use_due_to_obj_etc
 #  $ctx->{SegName}        # initial segment name (at start of object or file)
+#                         #  or nonexistent if in object in unknown segment
+#                         #  may have leading `-'
+#  $ctx->{SegMapN}{$s}= $o
+#  $ctx->{SegMapNM}{$s}= $o
+#  $ctx->{SavedSegment}   # exists iff segment command used, is a $csss
 #  $ctx->{Layer}{Level}
 #  $ctx->{Layer}{Kind}
 #
@@ -202,6 +233,7 @@ our $allwidthmin= allwidth(undef);
 #  $eopts[]{DrawMods}     # modifier chars for drawing
 #
 #  @segments= ( $csss0, $dist0, $csss1, $dist1, ..., $csssn )
+#                         # here each csss may have preceding `-'
 #
 #  $subsegcmap{$csss} = "$green $blue"
 #                         # $csss is canonical subseg spec; always has '/'
@@ -213,7 +245,7 @@ our @al; # current cmd
 our $o='';
 our $ol='';
 
-our $param; # for parametric_curve
+our $param; # for parametric_segment
 
 # ev_... functions
 #
@@ -595,20 +627,71 @@ sub parametric__o_pt ($) {
     o_path_point("$pt->{X} $pt->{Y}");
 }
 
-sub segment_used_len ($) {
-    my ($used) = @_;
+our $segused_incurrent;
+our $segused_currentpt;
+our $segmentpart_counter=0;
+our $segused_restorecounter;
+
+sub segment_used__print ($) {
+    my ($pt) = @_;
+    if ($segused_incurrent > 0 && $segused_restorecounter==1) {
+       o("%L segmentpart ".
+         $segmentpart_counter++." ".
+         $ctx->{Layer}{Level}.$ctx->{Layer}{Kind}." ".
+         $segments[0]." ".
+         $segused_incurrent." ".
+         loc2dbg($segused_currentpt)." ".
+         loc2dbg($pt)."\n");
+    }
+    $segused_incurrent= undef;
+    $segused_currentpt= undef;
+}
+    
+sub segment_used__len ($$) {
+    my ($used,$pt) = @_;
+    $segused_incurrent++;
+
     return if @segments < 3;
     $segments[1] -= $used;
     return if $segments[1] > 0;
-    @segments= @segments[2..-1];
+
+    segment_used__print($pt);
+    segment_used_begin($pt);
+
+    @segments= @segments[2..$#segments];
+    o("% segments @segments\n");
 }
     
+sub segment_state_save () {
+    return [ 0, $segused_incurrent, $segused_currentpt,
+            $segmentpart_counter, @segments ];
+}
+sub segment_state_restore ($) {
+    my ($r) = @_;
+    ($segused_restorecounter, $segused_incurrent, $segused_currentpt,
+     $segmentpart_counter, @segments) = @$r;
+    $r->[0]++;
+}
+
+sub segment_used_begin ($) {
+    $segused_incurrent= 0;
+    $segused_currentpt= $_[0];
+}
+sub segment_used_middle ($$) {
+    my ($used,$pt) = @_;
+    segment_used__len($used,$pt);
+}
+sub segment_used_end ($$) {
+    my ($used,$pt) = @_;
+    segment_used__len($used,$pt);
+    segment_used__print($pt);
+}
 sub parametric_segment ($$$$$) {
     my ($p0,$p1,$lenperp,$minradius,$calcfn) = @_;
-    # makes $p (global) go from $p0 to $p1  ($p1>$p0)
+    # makes $param (global) go from $p0 to $p1  ($p1>$p0)
     # $lenperp is the length of one unit p, ie the curve
     # must have a uniform `density' in parameter space
-    # $calcfn is invoked with $p set and should return a loc
+    # $calcfn is invoked with $param set and should return a loc
     # (ie, ref to X =>, Y =>, A =>).
     my ($pa,$pb,@ends,$side,$ppu,$e,$v,$tick,$draw,$allwidth);
     return unless $ctx->{Draw} =~ m/[ARSCG]/;
@@ -622,39 +705,61 @@ sub parametric_segment ($$$$$) {
 print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
     $draw= current_draw();
     if ($draw =~ m/G/) {
-       my ($pt,$going,$red);
-       $going=0;
-       o("    $psu_subseglw setlinewidth\n");
-       $param=$p0;
-       $pt= &$calcfn;
-       for (;;) {
-           if ($subsegcmapreq) {
-               next if $subsegcmap{$segments[0]}++;
-               print "$segments[0]\n" or die $!;
-           } elsif (exists $subsegcmap{$segments[0]}) {
-               $red= $pt->{A} / (2*$pi);
-               $red *= 64;
-               $red %= 64;
-               $red /= 64.0;
-               $red= sprintf("%f", $red);
-               o("    $red $subsegcmap{$segments[0]} setrgbcolor\n");
-           } else {
-               die "unknown subsegment colour for $segments[0]\n";
+       my ($pt,$going,$red,$csegbare,$movfeat,$movstroke);
+       my ($used_last,$me,$segsave);
+       $segsave= segment_state_save();
+       foreach $me (qw(e m)) {
+           segment_state_restore($segsave);
+           $going=0;
+           o("% segments @segments\n");
+           $param=$p0;
+           $pt= &$calcfn;
+           segment_used_begin($pt);
+           for (;;) {
+               $movstroke= "      cmapreq-stroke\n";
+               $csegbare= $segments[0];
+               $csegbare =~ s/^\-//;
+               if ($subsegcmapreq) {
+                   if (!exists $subsegcmap{$csegbare}) {
+                       print "$csegbare\n" or die $!;
+                       $subsegcmap{$csegbare}++;
+                   }
+               } else {
+                   $movfeat= $csegbare =~ s,(/\D+)(\d+)$,$1, ? $2 : 'f';
+                   die "unknown subsegment colour for $csegbare\n"
+                       unless exists $subsegcmap{$csegbare};
+                   $red= $pt->{A} / (2*$pi);
+                   $red *= $subsegcmapangscale;
+                   $red += $subsegcmapangscale*2;
+                   $red += $subsegcmapangscale/2 if $segments[0] =~ m/^\-/;
+                   $red %= $subsegcmapangscale;
+                   $red += $subsegcmapangscale if $me eq 'e';
+                   $red= sprintf("%f", $red / 255.0);
+                   $movstroke=
+                       ("    $red $subsegcmap{$csegbare} setrgbcolor\n".
+                        "    $psu_subseglw{$me} setlinewidth stroke\n");
+                   if ($subsegmovfeatpos ne $movfeat ||
+                       ($me eq 'e' && $csegbare =~ m,^/,)) {
+                       $movstroke= "%     no-stroke\n";
+                   }
+               }
+               o_path_begin();
+               parametric__o_pt($pt);
+       
+               $param += $ppu;
+               last if $param>=$p1;
+               $pt= &$calcfn;
+               segment_used_middle($psu_ulen,$pt);
+               parametric__o_pt($pt);
+               o($movstroke);
            }
-           o_path_begin();
-           parametric__o_pt($pt);
-
-           $param += $ppu;
-           last if $param>=$p1;
-           segment_used_len($psu_ulen);
+           $used_last= $p1-($param-$ppu);
+           $param=$p1;
            $pt= &$calcfn;
+           segment_used_end($used_last * $lenperp, $pt);
            parametric__o_pt($pt);
-           o_path_strokeonly();
+           o($movstroke);
        }
-       segment_used_len(($p1-($param-$ppu)) * $lenperp);
-       $param=$p1;
-       parametric__o_pt(&$calcfn);
-       o_path_strokeonly();
     }
     if ($draw =~ m/C/) {
        my ($pt);
@@ -667,6 +772,21 @@ print DEBUG "ps $p0 $p1 $lenperp ($ppu)\n";
        parametric__o_pt(&$calcfn);
        o("      stroke\n");
     }
+    if ($draw =~ m/D/) {
+       my ($pt,$ad,$len,$off);
+       $param= ($p0+$p1)*0.5;
+       $pt= &$calcfn;
+       $ad= ang2deg($pt->{A});
+       $len= sprintf "%.0f", $lenperp * abs($p1-$p0);
+       $off= $draw =~ m/C/ ? $lmu_lenlabeloff : $lmu_lenlabeloffctr;
+       ol("      gsave\n".
+          "        $pt->{X} $pt->{Y} translate\n".
+          "        $ad rotate\n".
+          "        lf setfont\n".
+          "        0 $off moveto\n".
+          "        ($len) show\n".
+          "      grestore\n");
+    }
     return unless $draw =~ m/[ARS]/;
     for ($pa= $p0; $pa<$p1; $pa=$pb) {
        $pb= $pa + $ppu;
@@ -956,7 +1076,10 @@ sub cmd_join {
 
 sub line ($$$) {
     my ($from,$to,$len) = @_;
-    parametric_segment(0.0, 1.0, abs($len) + 1e-6, undef, sub {
+    if ($len < 0) {
+       ($from,$to,$len) = ($to,$from,-$len);
+    }
+    parametric_segment(0.0, 1.0, $len + 1e-6, undef, sub {
        ev_lincomb({}, $from, $to, $param);
     });
 }
@@ -1049,10 +1172,13 @@ sub newctx (;$) {
        Trans => { X => 0.0, Y => 0.0, A => 0.0, R => 1.0 },
        InRunObj => "",
        DrawMap => sub { $_[0]; },
-       SegName => ""
+       SegMapN => { },
+       SegMapNM => { }
        };
-    %{ $ctx->{Layer} }= %{ $ctx_save->{Layer} }
-        if defined $ctx_save;
+    if (defined $ctx_save) {
+       %{ $ctx->{Layer} }= %{ $ctx_save->{Layer} };
+       $ctx->{Parent}= $ctx_save;
+    }
 }
 
 our $defobj_save;
@@ -1074,8 +1200,6 @@ sub cmd__defobj ($) {
     $ctx->{Draw}= $defobj_save->{Draw}.'X';
     $ctx->{DrawMap}= sub { ''; };
     $ctx->{Layer}= { Level => 5, Kind => '' };
-    $segments[0] =~ m,/, or die;
-    $ctx->{SegName}= $`;
 }
 
 sub cmd_enddef {
@@ -1103,6 +1227,87 @@ sub cmd__runobj ($) {
     }
 }
 
+sub cva_subsegspec ($) {
+    my ($sp)=@_;
+    die "invalid subsegment spec" unless
+       $sp =~ m,^(\-?)([0-9A-Za-z_]*)(?:/(?:([A-Za-z_]+)(\d+))?)?$,;
+    my ($sign,$segname,$movfeat,$movconf)=($1,$2,$3,$4);
+
+    if (!exists $ctx->{SegName}) {
+       $segname= '';
+       $sign= '';
+    } else {
+       my ($map_ctx);
+       
+       $ctx->{SegName} =~ m/^\-?/ or die;
+       $sign .= $&;
+       $segname= $'.$segname;
+       
+       for ($map_ctx= $ctx;
+            defined $map_ctx;
+            $map_ctx= $map_ctx->{Parent}) {
+           if (defined $movfeat &&
+               exists $map_ctx->{SegMapNM}{"$segname/$movfeat"}) {
+               $movfeat= $map_ctx->{SegMapNM}{"$segname/$movfeat"};
+           }
+           if (exists $map_ctx->{SegMapN}{$segname}) {
+               $map_ctx->{SegMapN}{$segname} =~ m/^\-?/ or die;
+               $sign .= $&;
+               $segname= $';
+           }
+       }
+       $sign =~ s/\-\-//g;
+    }
+
+    return $sign.$segname.'/'.
+       (defined $movfeat ? sprintf "%s%d", $movfeat, $movconf : '');
+}
+
+sub cmd_segment {
+    my ($csss,$length);
+    $ctx->{SavedSegment}= pop @segments
+       unless exists $ctx->{SavedSegment};
+    @segments= ();
+    while (@al>1) {
+       $csss= can(\&cva_subsegspec);
+       $length= can(\&cva_len);
+       push @segments, $csss, $length;
+    }
+    $csss= can(\&cva_subsegspec);
+    push @segments, $csss;
+}
+
+sub cva_segmap_s {
+    my ($sp) = @_;
+    $sp =~ m,^\w+(?:/[a-zA-Z_]+)?$,
+        or die "invalid (sub)segment mapping S \`$sp'";
+    return $sp;
+}
+
+sub cva_segmap_n {
+    my ($sp) = @_;
+    $sp =~ m,^\-?\w+$, or die "invalid segment mapping N' \`$sp'";
+    return $sp;
+}
+    
+sub cva_segmap_m {
+    my ($sp) = @_;
+    $sp =~ m,^[a-zA-Z_]+$, or die "invalid segment mapping M' \`$sp'";
+    return $sp;
+}
+    
+sub cmd_segmap {
+    my ($s,$d);
+    while (@al) {
+       $s= can(\&cva_segmap_s);
+       if ($s =~ m,/,) {
+           $ctx->{SegMapNM}{$s}= can(\&cva_segmap_m);
+       } else {
+           $ctx->{SegMapN}{$s}= can(\&cva_segmap_n);
+       }
+    }
+}
+
 sub layer_draw ($$) {
     my ($k,$l) = @_;
     my ($eo,$cc, $r);
@@ -1184,6 +1389,9 @@ sub cmd__obj ($) {
     } else {
        $ctx->{InRunObj}= $ctx_save->{InRunObj}."${obj_id}::";
     }
+    if ($segments[0] =~ m,(.*[^-]+)/,) {
+       $ctx->{SegName}= $1;
+    }
     $ctx->{DrawMap}= sub {
        my ($i) = @_;
        $i= &{ $ctx_save->{DrawMap} }($i);
@@ -1207,6 +1415,9 @@ sub cmd__obj ($) {
            $pfx= cano(\&cva_idstr,undef);
        }
     }
+    if (exists $ctx->{SavedSegment}) {
+       @segments= ($ctx->{SavedSegment});
+    }
     $ctx_inobj= $ctx;
     $ctx= $ctx_save;
     if (defined $pfx) {
@@ -1379,6 +1590,7 @@ if ($debug) {
 }
 
 $ctx->{Draw}= '';
+$ctx->{SegName}= '';
 
 @al= qw(layer 5);
 cmd__one();