#!/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-02-06 08:28" ; # written by emacs time-stamp
$VERSION =~ s/ /-/;
$VERSION =~ s/:/./;
our %HASH; # avoid duplication
our $TEXT_DONE = 0  ; # avoid duplication

my $LAST_LAYER = 131;
my $markLayer = 120;
my $LENGTH = 2500;
sub vertical {
    my ($args)    = @_;
    my $width     = $args -> { -width };
    my $length    = $args -> { -length };

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

    my $name = sprintf("V-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 => [ -$width/2, -$length/2,
		  $width/2, -$length/2,
		  $width/2,  $length/2,
		 -$width/2,  $length/2,],
	-layer => $layer);
    $gds2File -> printEndstr();
    return $name ;
}

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);

    foreach my $index ( 0 .. $#RATIO  ) {
	# dektak version
	$gds2File -> printSref ( -name => $NAMES[$index  ],  -xy => [ 0, -$y_shift ]);

	if ($TEXT_DONE == 0 ) {
	    printf(TEXT "%d, %3.4f\n", $index + 1, $RATIO[$index]/1000 * 2.5) ;
	}
	$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);
    }
    if ($WIDTH < 40 ) {
    my $label  = sprintf("%3.2f"      ,$WIDTH);
    my $label2 = sprintf("%3.2f/%3.2f", $WIDTH, $SPACE);
    text2gds::text_flat(0, 4500 -18350, $LAST_LAYER, 60, $label, $gds2File);
    text2gds::text_flat(0, 5100 -18350, $LAST_LAYER, 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..$LAST_LAYER - 1 ) {
	push(@RATIO, $LAST_LAYER * 1.03 ** $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, 0.05, 0.06, 0.08, 0.1);

# -----------------------------------------------------------
#   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],
		    -gds2File => $gds2File});
	    push @{$ref->[$index]}, $name;
	}
    };

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

    my @global;
    foreach my $index (0..$#TABLE) {
	
	my  $width = $TABLE[$index];
	my  $space;
	if ($width > 40) { $space = 48;}
	else {
	    $space = 50 - $width;
	}
	my $name = 
	    global_placement ({
			-SPACE => $space,
		        -WIDTH => $width,
			-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 $mark =  vertical ({
	    -layer => $markLayer,
	    -length => 3000,
	    -width => 0.8,
	    -gds2File => $gds2File});

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

    my ($X, $Y) = (-3800, -7500);
    foreach my $index (0..$#global) {
	my $X_loc = 0;
	if ($index == 0)  { $X_loc -= 10800;}
	$gds2File -> printSref  ( -name => $global[$index],    -xy => [$X_loc + $X, $index * $LENGTH * 1.2 + $Y],
				  -angle => 90);
    }
    text2gds::text_flat( 0, -$LENGTH  - 3000 -3600, $LAST_LAYER, 10, $filename, $gds2File);

    my $columns = 60;  # 1.6 * 60 = 96 um
    my $xWidth  = $columns * 0.8;
    $gds2File ->
    printAref ( -name => $mark, -columns => $columns, -rows => 1,
		-xy => [ - $xWidth, 1510, $xWidth, 1510, - $xWidth, 1510]);

    $gds2File ->
    printAref ( -name => $mark, -columns => $columns, -rows => 1,
		-xy => [ - $xWidth, -1510, $xWidth, -1510, - $xWidth, -1510]);

    $gds2File ->  printPath ( -xy => [   -9, 0,   9, 0]   , -layer => $markLayer, -width => 0.8);
    $gds2File ->  printPath ( -xy => [       0, - 9, 0, 9], -layer => $markLayer, -width => 0.8);

    $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
