#!/usr/pkg/bin/perl
use strict;
use GDS2;

use feature qw(declared_refs refaliasing say);
push (@INC, '../../Perl_GDSII_Tutorial/code/');
push (@INC, '../Perl_GDSII_Tutorial/code/');
require "text2gds.pm";

my($VERSION);
$VERSION = "2023-01-17 07:56" ; # written by emacs time-stamp
$VERSION =~ s/ /-/;
$VERSION =~ s/:/./;
our %HASH; # avoid duplication
our $TEXT_DONE = 0  ; # avoid duplication

my $LENGTH = 2500;
sub horizontal {
    my ($args)    = @_;
    my $width     = $args -> { -width };
    my $length    = $args -> { -length };

    my $layer	  = $args -> { -layer };
    my $gds2File  = $args -> { -gds2File };

    my $name = sprintf("W%02.2fuxL%2.1fmm-LY%02d", $width, $length/1000, $layer);

    if ($HASH{$name}) { return $name ;}
    $HASH{$name}++;   # avoid duplication
    $gds2File -> printBgnstr(-name => $name);
    $gds2File -> printBoundary (
	-xy => [ -$length/2, -$width/2,
		  $length/2, -$width/2,
		  $length/2,  $width/2,
		 -$length/2,  $width/2,],
	-layer => $layer);
    $gds2File -> printEndstr();
    return $name ;
}

sub global_placement {
    my ($args)    = @_;
    my $WIDTH     = $args -> {-WIDTH};
    my $SPACE     = $args -> {-SPACE};
    my (\@RATIO)  =  $args -> {-RATIO};
    my (\@NAMES)  =  $args -> {-NAMES};
    my ($TEXT_DONE) =$args -> {-TEXT_DONE};
    my $gds2File  = $args -> {-gds2File};
    my $x_pitch   = $LENGTH * 2;
    
    my $name = '50x50';
#    if ($WIDTH != 50)
    { $name = sprintf("%3.2fx%3.2f", $SPACE, $WIDTH) };
    if ($HASH{$name}) { return $name;}
    $HASH{$name}++;

    $gds2File -> printBgnstr (-name => $name);

    my $pitch   =  $WIDTH + $SPACE;
    my $y_shift = -(int(  (($#NAMES * $pitch) / 2 + 1300)/32 ) * 32);

    my $y_loc;
    my $y_loc_start = - (int( ($pitch - 50 ) * 480 + 20000) / 32) * 32;   # 7100 for 128  128-50 = 78


#  pitch   * 400 now   
#    128    51200   ->  58000
#     50    20000   ->  20000

    foreach my $index ( 0 .. $#RATIO ) {
    $y_loc = log($RATIO[$index] * $pitch * 2)/log(1.02) * $pitch  + $y_loc_start;

# dektak version
    $gds2File -> printSref ( -name => $NAMES[$index  ],  -xy => [ -$x_pitch/2, $y_shift ]);

# true log verson
#    $gds2File -> printSref ( -name => $NAMES[$index  ],  -xy => [  $x_pitch/2, $y_loc]);
#   text2gds::text_flat( $x_pitch/2 + 510, $y_loc,  11, 0.2,  sprintf("%3.3f", $RATIO[$index]/10), $gds2File);
    if ($TEXT_DONE == 0 ) {
	printf(TEXT "%d, %3.2f\n", $index + 1, $RATIO[$index]/1000);
    }
    $y_shift += $pitch;
    if      (($index + 1)%10 == 0) { $y_shift +=  $pitch * 2;}
    elsif   (($index + 1)%5  == 0) { $y_shift +=  $pitch * 1;}

}
    # place lowest mark, because the first low dose shape is hard to recognize

    $y_loc = (log($RATIO[0] * $pitch * 2)/log(1.02) - 1 )  * $pitch + $y_loc_start;

 #  $gds2File -> printSref ( -name => $NAMES[10],  -xy => [ $x_pitch/2, $y_loc ]);    

    if(0){
    $gds2File -> printPath ( -xy => [ -100,0,  100, 0],       -layer => 10, -width => 1.6);
    $gds2File -> printPath ( -xy => [      0, -100, 0, 100],  -layer => 10, -width => 1.6);
    }
    my $label  = sprintf("%3.2f"      ,$WIDTH);
    my $label2 = sprintf("%3.2f/%3.2f", $WIDTH, $SPACE);
    text2gds::text_flat(-$LENGTH, -8800, 17, 60, $label, $gds2File);
    text2gds::text_flat(-$LENGTH, -8200, 17, 20, $label2, $gds2File);

    $gds2File -> printEndstr ();
#    print STDERR __LINE__ . ' ' . $name  . "\n";
    return $name;
}
# ---------------------------------------------------------
#   M A I N 
# ---------------------------------------------------------
sub main  {

# 1  3.125 6.25 12.5 25   1.02^70  100 120 144 173
    my @RATIO; #  = qw (5 9 16.2); # x 1.8  prologue part

    foreach my $i ( 0..130) {
	push(@RATIO, 25 * 1.05 ** $i);
    }
#    push (@RATIO, qw (129 159)) ; # epilog part, max 16.0  12.93 * 5

my              $filename = $0;
                $filename =~ s/\.txt$//;
    $filename =~ s/contrast-curve/CC/;
    $filename .= sprintf("-%2.1fmm", $LENGTH/1000);
                $filename .= '-'.$VERSION;
my $text_file = $filename;    
    $filename .= '.gds';    
    $text_file .= '.txt';
    open(TEXT, ">$text_file");
    print $filename,"\n";

    
my $gds2File = new GDS2(-fileName => '>'. $filename);
$gds2File -> printInitLib (-name => 'libname');

# prepare unit shape
my @NAMES50;
my @NAMES32;

    my $ref;

    my @TABLE = (
	[48,       48],
	[48.4,    1.6],
	[49.2,    0.8],
	[49.6,    0.4],
	[49.904,  0.096],
	[49.952,  0.048],
    );

# -----------------------------------------------------------
#   create horizontal lines for above @TABLE layer by layer
# -----------------------------------------------------------
    foreach my $index (0..$#TABLE) {
	foreach my $layer (1 .. $#RATIO + 1) {
	    my $name =  horizontal ({
		    -layer => $layer,
		    -length => $LENGTH,
		    -width => $TABLE[$index]->[1],
		    -gds2File => $gds2File});
	    push @{$ref->[$index]}, $name;
	}
    };

# -----------------------------------------------------------
#   create global placement for each size of above (by @TABLE)
# -----------------------------------------------------------

    my @global;
    foreach my $index (0..$#TABLE) {
	
	my $name = 
	    global_placement ({
			-SPACE => $TABLE[$index] -> [0],
		        -WIDTH => $TABLE[$index] -> [1],
			-NAMES => \@{$ref->[$index]},
			-RATIO => \@RATIO,
			-TEXT_DONE => $TEXT_DONE,
			-gds2File => $gds2File
		      });
	$TEXT_DONE++;
#	print STDERR __LINE__ .  ' ' . $name . "\n";
#	print STDERR __LINE__ .  ' ' . ${$ref->[$index]}[0]. "\n";
	push(@global, $name);
    }
    my @NAME = qw(64x64R 48.6x1.6R 49.2x0.8R 49.6x0.4R);
    my @NAME = @global;

#    print STDERR __LINE__ .  ' ' . $NAME[0]. "\n";
    my  $OFFSET = -25000;

    if (0){
    $gds2File -> printBgnstr( -name => $NAME[0].'-a');
    $gds2File -> printSref  ( -name => $global[0], 
			      -xy => [0,0 ], -angle => 90,);
    $gds2File -> printEndstr();

    print STDERR __LINE__ .  ' ' . $NAME[1]. "\n";
    $gds2File -> printBgnstr( -name => $NAME[1].'-a');

    $gds2File -> printSref  ( -name => $global[1], -xy => [0,0 ], -angle => 90,);
    $gds2File -> printEndstr();
    }
    
    my $NEGA_PITCH = 7000;
    $gds2File -> printBgnstr( -name => 'nega');


#    $gds2File -> printSref  ( -name => $NAME[1], -xy => [ 	- $NEGA_PITCH, 0]);
#    $gds2File -> printSref  ( -name => $NAME[2], -xy => [ 	0            , 0]);
#    $gds2File -> printSref  ( -name => $NAME[3], -xy => [ 	  $NEGA_PITCH, 0]);


    $gds2File -> printEndstr();


    $gds2File -> printBgnstr( -name => 'top');

    foreach my $index (0..$#global) {
	$gds2File -> printSref  ( -name => $global[$index],    -xy => [ 	 0, $index * $LENGTH * 1.2],
				  -angle => 90);
    }
    text2gds::text_flat( 0, -$LENGTH - 3000, 17, 10, $filename, $gds2File);
    $gds2File -> printEndstr();

    $gds2File -> printEndlib ();
}

main();
__END__

my $ref;
for my $i (1..10) {
   for my $j (1..10) {
        push @{$ref->[$i]}, $j;
    }
}


    See perldoc GDS2 for help

    perl DrieCondPerl
; please note I have copy in ~/.emacs-sub/time-stamp-setup.el
(require 'time-stamp)
(add-hook 'write-file-hooks 'time-stamp)
(setq time-stamp-active t)
(setq time-stamp-time-zone "UTC")
(setq time-stamp-time-zone nil)
(setq time-stamp-format "%04y-%02m-%02d %02H:%02M");
(setq time-stamp-start "$VERSION = \"") ;
(setq time-stamp-end "\"") ;
(setq time-stamp-line-limit 20) ; ; default is 8
