#!/usr/bin/env python2
""" Glyph Plot
UFO print tree
Prints a glyph in drawbotPt.anchor
    print aPt.bcpIn
    print aPt.bcpOut
    print aPt.type path instructions
Usage:
$ ./UFO-drawBot file.ufo glyphname > somedrawbotcode.py

"""


import numpy as np
import argparse, robofab, math, sys
from robofab.world import RFont

parser = argparse.ArgumentParser()
parser.add_argument(
	"ufo_in",
	help = "The ufo file to process"
)
parser.add_argument(
	"glyph",
	help = "The glyph to print for DrawBot"
)
parser.add_argument(
	"-v", "--verbose",
	help = "Increase output verbosity",
	action = "store_true"
)
args = parser.parse_args()

ufo_path   = args.ufo_in
glyph_name = args.glyph


def main():

	font = RFont( ufo_path )

	if font == None:
		print "You should open a font first, there's nothing to look at now!"
	else:
		print '#####'
		print "# Font:",font.info.postscriptFullName
		print '# Glyph:', glyph_name,'\n'
		if args.verbose:
			print "# The number of glyphs:", len(font)
			print "# Full name of this font:", font.info.postscriptFullName
			print '# Ascender:', font.info.ascender
			print '# Descender:', font.info.descender,'\n'
			print '# List of glyph names:', font.keys()
			print '#####\n'


	glyph = font[glyph_name]

	plotear_glifo(
		glyph,
		font
	)



# Subs

def plotear_glifo(
	self,
	font,
	offset = (0,0),
	scale = (1,1)
):
	
	print '#','RContours','en glyph: ',self.name,'#####'
	for contour in self:
		print contour
		
		print '\n'
		print '#'*2,'RSegments','#####'
		for segment in contour:
			# print len(segment)
			print ' '*2,segment
			print ' '*4,'tipo :',segment.type
			print ' '*4,'smooth :',segment.smooth
			print ' '*4,'onCurve :',segment.onCurve.x,segment.onCurve.y
			print ' '*4,'selected :',segment.selected

			#print '\n'
			#print '###','RPoints','#####'
			#for pt in contour.points:
			#	print pt
			#	print pt.x
			#	print pt.y
			#	print pt.type
			#	print pt.selected

			#print '\n','## RBPoints', self.name, '#####'
			#for bPt in contour.bPoints:
			#	print ' ',bPt
			#	print ' ',bPt.anchor
			#	print ' ',bPt.bcpIn
			#	print ' ',bPt.bcpOut
			#	print ' ',bPt.type



# 		VERTS = []
# 		for point in contour.points:
# 			# Dirty fix for vertical offset
# 			pathVert      = ( point.x, point.y ) 
# 			pathResultado = tuple(
# 				( v1 + v2 ) * v3 for
# 				v1, v2, v3 in
# 				zip( pathVert, offset, scale )
# 			)
# 			pathCode = pType2pCode[point.type]
# 		
# 		print '# RPoints', self.name, '#####'
# 		print pathCode, pathResultado
# 
# 	for component in self.components:
# 		componentName   = component._baseGlyph
# 		componentnGlyph = font[componentName]
# 		componentOffset = [
# 			( v1 + v2 ) for
# 			v1, v2, in
# 			zip( component._offset, offset )
# 		]
# 		componentScale  = [
# 			( v1 * v2 ) for
# 			v1, v2, in
# 			zip( component._scale, scale )
# 		]
# 
# 		plotear_glifo(
# 			componentnGlyph,
# 			font,
# 			componentOffset,
# 			componentScale
# 		)

		if args.verbose:
			print vars(component)

######
# robofab point type to matplotlib path code maping
# http://matplotlib.org/api/path_api.html

pType2pCode = {
	'stop' : 0,
	'move' : 1,
	'line' : 2,
	# 'offcurve': 3,
	'offcurve' : 3,
	'curve' : 4,
	'close' : 79,
}

main()
