from gk_global import *

from gk_fonts import *
from gk_output_gdsii import *
from gk_output_key import *

import gk_input_gdsii
import gk_input_key


KEY_File_Format = 1
GDS_File_Format = 2

#----------------------------------------------------------------------------
# output
#----------------------------------------------------------------------------

# deprecated: use output.write()
def gds_write (s, output = None):
	if output == None: output = get_current_output()
	output.write(s)

# deprecated: use gds_structure(...)
def make_str (name,elements_str, library = None):
	if library == None: library = get_current_library()
	return gds_structure(name, elements_str, library)

# Deprecated: use set_current_output
def set_file_format (format = KEY_File_Format):
	if format == KEY_File_Format:
		set_current_output(gds_output_key())
	elif format == GDS_File_Format:
		set_current_output(gds_output_gdsii())

# deprecated: use library method directly
def round_value(value, library = None):
	if library == None: library = get_current_library()
	return library.snap_value(value)

# deprecated: use library method directly
def round_coordinate(coordinate, library = None):
	if library == None: library = get_current_library()
	return library.snap_coordinate(coordinate)

# deprecated: use library method directly
def round_coordinates(coordinates, library = None):
	if library == None: library = get_current_library()
	return library.snap_shape(coordinates)

# deprecated: use library method directly
def structure_exists(name, library = None):
	if library == None: library = get_current_library()
	return library.structure_exists(name)

# deprecated: use library method directly
def check_references(library = None):
	if library == None: library = get_current_library()
	return library.check_references()

# deprecated: use library method directly
def add_structure(structure, library = None):
	if library == None: library = get_current_library()
	library.register_structure(structure)

#----------------------------------------------------------------------------
# Getting and Setting output variables
#----------------------------------------------------------------------------

# deprecated: get vallue directly from output
def get_paths_to_boundaries (output = None):
	if output == None: output = get_current_output()
	return output.paths_to_boundaries

# deprecated: get vallue directly from output
def get_name_scrambling (output = None):
	if output == None: output = get_current_output()
	return output.name_scrambling

# deprecated: get value directly from output
def get_header_written(output = None):
	if output == None: output = get_current_output()
	return output.header_written

# deprecated: set value directly in output
def set_name_scrambling (status = Key_Name_Scrambling_Off, output = None):
	if output == None: output = get_current_output()
	output.name_scrambling = status

# deprecated: set value directly in output
def set_paths_to_boundaries (status = Key_Paths_To_Boundaries_Off, output = None):
	if output == None: output = get_current_output()
	output.paths_to_boundaries = status

# deprecated: set value directly in output
def set_header_written(status, output = None):
	if output == None: output = get_current_output()
	output.header_written = status

# deprecated: use output method directly
def make_str_name(name, output = None):
	if output == None: output = get_current_output()
	return output.scramble_structure_name(name)

################################################################################
## Parts of a GDSII File
################################################################################

def comment(info):
	return ""

def header(library = None, output = None ):
	if library == None: library = get_current_library()
	if output == None: output = get_current_output()
	return output.__str_library_header__(library)

def footer(library = None, output = None):
	if library == None: library = get_current_library()
	if output == None: output = get_current_output()
	library.collect_references()
	library.check_references()
	return output.__str_library_footer__()

def str_header(name, commentstr = "", output = None):
	if output == None: output = get_current_output()
	s = gds_structure(name, [])
	return output.__str_structure_header__(s)

def str_footer(name):
	s = gds_structure(name, [])
	return output.__str_structure_footer__(s)

################################################################################
## Import
###################################################################################

def import_gds(name, filename, prefix="", scaling = 1.0, layer_map = {} ):
	f = open(filename,"rb")
	input = gk_input_gdsii.gds_input_gdsii(f)
	input.prefix = prefix
	input.scaling = abs(scaling)
	input.layer_map = layer_map

	library = input.read()
	f.close()
	library.top_layout().name = name
	get_current_library().add(library.structures)
	return library.structures

def import_key(name, filename, prefix="", scaling = 1.0, layer_map = {} ):
	f = open(filename,"r")
	input = gk_input_key.gds_input_key(f)
	input.prefix = prefix
	input.scaling = abs(scaling)
	input.layer_map = layer_map

	library = input.read()
	f.close()
	library.top_layout().name = name
	get_current_library().add(library.structures)
	return library.structures



