#!/usr/local/bin/perl -w
# GDSII generation
#
use GDS2;
use strict;
use Getopt::Std;

require '../pm/text2gds.pm';

package Stripes;

my $H  = 4000; # height
my $DEBUG = 0;
my $VERBOSE = 0;
my %AVOIDDUPE;

sub stripex10 ($$$) {
    my ($w)	= shift;
    my ($r)	= shift;  # ratio  (1) 1:1  (2) 1:2 (3) 1:3
    my ($gds2File) = shift;

    my ($structure) = sprintf("stripeX10-%4.2f-%d", $w, $r);

    if (!defined $AVOIDDUPE{$w.'-'.$r}) {
	$AVOIDDUPE{$w.'-'.$r}++;
#    print __LINE__, ' ', $structure,"\n";
    $gds2File -> printBgnstr( -name => $structure);
    foreach my $i (0..9)   {
	my ($x) = $w * $i * ($r + 1);
	my ($y) = 0;
	$gds2File -> printBoundary(
	    -layer=> 0,
	    -xy=>[ $x,      $y, 
		   $x + $w, $y,
		   $x + $w, $y + $H,
		   $x,      $y + $H,
		   $x,      $y,]
	);
    }
    $gds2File -> printEndstr();
    }
    return $structure;
}

sub stripex10_Filled ($$$$) {
    my ($w)	= shift;
    my ($r)	= shift;  # ratio  (1) 1:1  (2) 1:2 (3) 1:3
    my ($H)	= shift;
    my ($gds2File) = shift;

    my ($structure) = sprintf("stripeX10-Filled-%4.2f-%d", $w, $r);

#    print __LINE__, ' ' , $r, ' -> ', $structure,"\n";
#    print __LINE__.  join ' ', (0.. ($r * 10 - 1 )),"\n";
    if (!defined $AVOIDDUPE{'F-'.$w.'-'.$r}) {
	$AVOIDDUPE{'F-'.$w.'-'.$r}++;
    $gds2File -> printBgnstr( -name => $structure);
    foreach my $i (0.. ( ($r + 1 ) * 10 - $r - 1 ) )  {
	my ($x) = $w * $i;
	my ($y) = 0;
#	print __LINE__, ' ' , $i, ' -> ', ' ', $w, ' ', $i,"\n";

	$gds2File -> printBoundary(
	    -layer=> 0,
	    -xy=>[ $x,      $y, 
		   $x + $w, $y,
		   $x + $w, $y + $H,
		   $x,      $y + $H,
		   $x,      $y,]
	);
    }
    $gds2File -> printEndstr();
    }
    return $structure;
}

# prepare structure of each character with specified size
sub letters_by_name($$$$){
    my($prefix, $width, $c, $gds2File) = @_;
#    print STDERR sprintf("%04d ", __LINE__). ' prefix: '. $prefix . ' width '. $width . ' c: '. $c . "\n";
                  #layer dotsize, structure name text
    my ($dotsize) = int($width/50*1000)/1000;
    if ($dotsize > 100 ) { $dotsize = 100;}
    text2gds::text(0, $dotsize , $prefix . $width .'-'.$c, $c, $gds2File );
}
sub marker($$$$){
    my ($w)     = shift;  # 15 or 5 (large and middle)
    my ($count) = shift;  # 1 2 3
    my ($x)     = shift;  # start location
    my ($gds2File) = shift;    

    my ($y)     = 0;

    foreach my $i ( 0 .. $count -1 ) {
	$x += $w + 5 ;
    $gds2File -> printBoundary(
	    -layer=> 0,
	    -xy=>[ $x,      $y, 
		   $x + $w, $y,
		   $x + $w, $y + $H,
		   $x,      $y + $H,
		   $x,      $y,]
	);
    }
    return $x + 15;
}
sub Stripes($$$){
    my $width = shift;
    my $ratio = shift;
    my ($gds2File) = shift;    

    my ($structure) = sprintf("Stripes-%4.2f-%d", $width, $ratio);

    my (@names);
    my (@namesF);
    my (@location);
           #          w     r   name
    my ($span);
# prepare 10 stripe block, storing structure name and width

#   text2gds::text(0, $dotsize , $prefix . $width .'-'.$c, $c, $gds2File );

    my $location = 0;
    foreach my $i (0..9){
	$names [$i] = stripex10       ($width * ($i + 1) , $ratio,    $gds2File);
	$namesF[$i] = stripex10_Filled($width * ($i + 1) , $ratio, 2, $gds2File);
	   $span = $width * ($i + 1) * ($ratio + 1) * 9;
	$location += $span;
	$location[$i] = $location;  # keep value for char placement
    }

    $gds2File -> printBgnstr(-name => $structure);
    foreach my $i (0..9){
	$gds2File -> printSref(-name=> $names[$i],
			       -xy=>   [$i * 5 + $location[$i], 0]);

	$gds2File -> printSref(-name=> $namesF[$i],
			       -xy=>   [$i * 5 + $location[$i], $H + 4]);

	$span = $width * ($i + 1) * ($ratio + 1) * 9; # see above,

# place 1 .. 9 0 chars
	my($c) = $i + 1; if ($c == 10) { $c = 0;}
	$gds2File -> printSref(-name=> 'char-0.8-'. $c,
			       -xy=>   [$i * 5 + $location[$i] + $span / 2 - 2.0,
					$H+7]);
    }

# sub place_text($$$$$$){
#     my ($string) = shift;
# 
#     my ($x)     = shift;
#     my ($y)     = shift;
#     my ($pitch) = shift;
#     my ($size)  = shift;
# 
#     my ($gds2File) = shift;
    $gds2File -> printEndstr;

    my ($offset) = ($location[9] + $span) * 5;
    return ($offset, $structure);

}
# whole block of stripes

sub StripeBlock($) {
    my ($gds2File) = shift;
# -------------------------------------------------------
#                      M A I N 
# -------------------------------------------------------
my ($filename) = $0 . '.gds';

# ------------------------------------------------------
#          P L A C E   N A M E S  on T O P
# ------------------------------------------------------
my $shift;
my $baseX = 0;
my $baseY = 0;
my ($location) = 0;

   common::letters(0.8, $gds2File);
my @NAMES;   
my @OFFSET;
      #     w     ratio

    ($OFFSET[0], $NAMES[0]) =  Stripes(0.01, 1, $gds2File);
    ($OFFSET[1], $NAMES[1]) =  Stripes(0.1,  1, $gds2File);

    ($OFFSET[2], $NAMES[2]) =  Stripes(0.01, 2, $gds2File);
    ($OFFSET[3], $NAMES[3]) =  Stripes(0.1,  2, $gds2File);
 
    ($OFFSET[4], $NAMES[4]) =  Stripes(0.01, 3, $gds2File);
    ($OFFSET[5], $NAMES[5]) =  Stripes(0.1,  3, $gds2File);

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


# --------------------------------------------------
$location = marker(15, 1, $location, $gds2File);
$location = marker(5, 1, $location, $gds2File);

    common::place_text('1:1',   $location,      $H + 30.0, 5, 0.8, 0, $gds2File);
    common::place_text('10nm',  $location,      $H + 15.0, 5, 0.8, 0, $gds2File);
    common::place_text('100nm', $location + 30, $H + 15.0, 5, 0.8, 0, $gds2File);


$gds2File -> printSref(-name=> $NAMES[0],
			   -xy=>   [$location,0]);
$location += $OFFSET[0];
# --------------------------------------------------
$location = marker(5, 2, $location, $gds2File);

    common::place_text('1:1',    $location,      $H + 30.0,   5, 0.8, 0, $gds2File);
    common::place_text('100nm',  $location,      $H + 15.0, 5, 0.8, 0, $gds2File);
    common::place_text('1um',    $location + 30, $H + 15.0, 5, 0.8, 0, $gds2File);


$gds2File -> printSref(-name=> $NAMES[1],
			   -xy=>   [$location,0]);
$location += $OFFSET[1]/3.5;
# --------------------------------------------------
$location = marker(5, 3, $location, $gds2File);

    $location += 40;
# --------------------------------------------------
$location =     marker(15, 2, $location, $gds2File);
$location =     marker(5, 1, $location, $gds2File);    

 my(@shiftY) = ( 40, 25);

    common::place_text('1:2',    $location,      $H + $shiftY[0], 5, 0.8, 0, $gds2File);
    common::place_text('10nm',   $location,      $H + $shiftY[1], 5, 0.8, 0, $gds2File);
    common::place_text('100nm',  $location + 30, $H + $shiftY[1], 5, 0.8, 0, $gds2File);


$gds2File -> printSref(-name=> $NAMES[2],
		       -xy=>   [$location,0]);
$location += $OFFSET[2];
# --------------------------------------------------
$location =     marker(5, 2, $location, $gds2File);    

    common::place_text('1:2',    $location,      $H + $shiftY[0], 5, 0.8, 0, $gds2File);
    common::place_text('100nm',  $location,      $H + $shiftY[1], 5, 0.8, 0, $gds2File);
    common::place_text('1umm',   $location + 30, $H + $shiftY[1], 5, 0.8, 0, $gds2File);

$gds2File -> printSref(-name=> $NAMES[3],
		   -xy=>   [$location,0]);

$location += $OFFSET[3]/3.5 - 25;

$location =     marker(5, 3, $location, $gds2File); 

$location += 60;
# --------------------------------------------------
$location =     marker(15, 3, $location, $gds2File);
$location =     marker(5, 1, $location, $gds2File);    

    common::place_text('1:3',    $location,      $H + $shiftY[0], 5, 0.8, 0, $gds2File);
    common::place_text('10nm',   $location,      $H + $shiftY[1], 5, 0.8, 0, $gds2File);
    common::place_text('100nm',  $location + 30, $H + $shiftY[1], 5, 0.8, 0, $gds2File);

$gds2File -> printSref(-name=> $NAMES[4],
		       -xy=>   [$location,0]);
$location += $OFFSET[4]/2 + 10;

$location =     marker(5, 2, $location, $gds2File);

    common::place_text('1:3',    $location,      $H + $shiftY[0], 5, 0.8, 0, $gds2File);
    common::place_text('100nm',  $location,      $H + $shiftY[1], 5, 0.8, 0, $gds2File);
    common::place_text('1um',    $location + 30, $H + $shiftY[1], 5, 0.8, 0, $gds2File);

$gds2File -> printSref(-name=> $NAMES[5],
		       -xy=>   [$location,0]);
$location += $OFFSET[5]/3.5 - 50;
$location =     marker(5, 3, $location, $gds2File);
# --------------------------------------------------
    $gds2File -> printEndstr;

}
1;
__END__
