// stroke_input.jp
// Sunabe kazumichi 2009
// http://dp48069596.lolipop.jp/
//scriptographer.2.9.073-arrived/

function onInit() {
	count=0;
	tP=new Point(-8000,-8000);
	}
	
function onMouseDown(event){
// Stroke Layer set
	aDoc=activeDocument;
	aLayer=aDoc.activeLayer;	
	if(!(aLayer.name=="Stroke")){
	var aLayer=new Layer();
	aLayer.activate();
	aLayer.name="Stroke";
	}
//	
	var fontSize=components.Size.value;
	var fontFam="SDstroke-latin";
	var fontWeight="latin";
	var fontTracking=components.Tracking.value;
	
	fSt=new CharacterStyle();
	fSt.font = fonts[fontFam][fontWeight];
	fSt.fontSize = fontSize;
	fSt. tracking= fontTracking
	var pos=event.point;
 	oldPos=pos;
	text=new PointText(pos);	
	text.characterStyle=fSt;	
	text.content=values.text;		
}

function onMouseUp(event){		
	StrokeText();
}

function StrokeText(){	
	bkColor = new CMYKColor(0,0,0,1);	
	aDoc=activeDocument;
	sdLayer=aDoc.activeLayer;
	var sel = sdLayer.items
	if (sel.length > 0) {	
		for (var j = 0; j < sel.length; j++) {		
		var	apath = sel[j];	
		apath.selected=true;
		}
	}					
	OutlineText();
	Closeoff();		
}

function OutlineText(){	
	gpath=new Array();
	allObjects =document.getItems({ 
    type: PointText, 
    selected: true
});		
	countOfObjects = allObjects.length;	 
	for (var j = 0; j < countOfObjects; j++)
	{
	var chspath = allObjects[j].characterStyle;

	if(chspath.font.name=="latin"){
	gpath.push(allObjects[j]);	
	}

	try {
		gpath[j].createOutline(); 
	 	gpath[j].remove();		
	 }
	 catch(e){};
	 }		
}

function Closeoff(){	
	aDoc=activeDocument;
	sdLayer=aDoc.activeLayer;
	var sel = sdLayer.items;
	if (sel.length > 0) {	
		for (var j = 0; j < sel.length; j++) {
		
			if (sel[j] instanceof Path){	
			var	apath = sel[j];
	
			apath.closed = false;
			apath.style.fill.color = null;
			apath.style.stroke.width = 0;
			apath.style.stroke.color =  bkColor;
			apath.selected=true;
			}		
		}		 	 
	}	
}

	count=0;

function drawCurves(art){
	var l = art.children.length;
	if(l){
		for(var i=0; i<l; i++){
			drawCurves(art.children[i])
		}
	}else{
		var curveL = art.curves.length;
		for (var i= 0; i< curveL; i++){
			var curve = art.curves[i];
		drawCurve(curve);
		}
	}
}

function drawCurve(curve){
	var pt1 = curve.point1;
	var pt2 = curve.point2;
	var handle1 = curve.handle1.add(pt1);
	var handle2 = curve.handle2.add(pt2);
	
	var tangent = new Path();
	tangent.moveTo(pt1);
	tangent.curveTo(handle1,handle2,pt2);
}

function createTangent(pt,pt2,strokeColor,strokeWidth){
	var tangent = new Path();
	tangent.segments.add(pt);
	tangent.segments.add(pt2);
	tangent.style.stroke.width=strokeWidth;
	tangent.style.stroke.color=strokeColor;
	tangent.style.fill.color=null;
	return tangent;
}

function dbug(msg)
{if (true)
		print('::debug: '+msg);
}

// ------------------------  Dialog stuff  ------------------------ 
var values = {
	text: 'Input',
	size: 10,
	tracking: 10,
	count: 0,
};
var components = {
	text: { type: 'string',
		label: 'Text',
		multiline: true,
		width: 150,
		height: 30,
	},
//~ 	Orientation_Hor: {
//~ 		label: 'Orientation_Hor', type: 'checkbox'
//~ 	},
//~ 	Orientation_Ver: {
//~ 		label: 'Orientation_Ver', type: 'checkbox'
//~ 	},

	Size: { type: 'number', label: 'Size' ,value: 40, units: 'point'
},
	Tracking: { type: 'number', label: 'Tracking' ,value: 0
},

ruler1: {
		type: 'ruler', label: 'Path method'
	},
	Unseg: {type: 'button', value: 'Un-seg',
		onClick: function() {
			var selItems =document.getItems({ 
    type: Path, 
    selected: true
	});	
	var selLen = selItems.length;
	for(var i=0; i<selLen; i++){
		var art = selItems[i];
		drawCurves(art)
		art.remove();	
		}
		}		
	},

	Reverse: {
		type: 'button', value: 'Reverse',
		onClick: function() {
		//
		var revpath = document.getItems({ 
    type: Path, 
    selected: true
	});	
		for (var j = 0; j < revpath.length; j++)
		{
		try {	
		 revpath[j].reverse(); 
		 }
			 catch(e){};
		}
		}	
	},

ruler2: {
		type: 'ruler', label: 'Path selection'
	},
	Length: {
		type: 'button', value: 'Length',
		onClick: function() {
		//
		var fontSize=parseInt(components.Size.value);
		var art = new Path();			
		len1Color = new CMYKColor(0,1,1,1/100);	
		len2Color = new CMYKColor(0,1,1,2/100);		
		len3Color = new CMYKColor(0,1,1,3/100);		
		len4Color = new CMYKColor(0,1,1,4/100);	
		len5Color = new CMYKColor(0,1,1,5/100);	
		len6Color = new CMYKColor(0,1,1,6/100);	
	
		var shapes =  activeDocument.getItems({ 
    type: Path, 
    selected: true
	});	
		for (var j = 0; j < shapes.length; j++) 			
	{			
	var shape = shapes[j];	
//	print('::shape.length::: '+shape.length);		
//un-CompoundPath
	if(shape.parent instanceof CompoundPath){
		var alertMessage =  'PLEASE !! \n\nUN-Group(Ctrl+Shift+G), \nUN-CompoundPath(Ctrl+Shift+Alt+8)';
		Dialog.alert(alertMessage);
	break;
	}	
	var plen = shape.length;
			var len=Math.round(plen*fontSize/420)     
	var art=shape
			switch(len)
			{
			case 0: art.style.stroke.color=len1Color;break;
			case 1: art.style.stroke.color=len1Color;break;
			case 2: art.style.stroke.color=len2Color;break;
			case 3:art.style.stroke.color=len3Color;break;
			case 4: art.style.stroke.color=len4Color;break;
			case 5: art.style.stroke.color=len5Color; break;
			default:art.style.stroke.color=len6Color; break;
			}
		}
		}		
		},
	
	verhorLine: {
		type: 'button', value: 'ver-hor Line',
		onClick: function() {
		
//tangent init ----------------------------------
	var tan1=0.1;
	var tan2=10;
//-------------------------------------
	var vl, hl, tn, sl1, sl2 ,sl3;
	var shape = new Path();
			
		vlColor = new CMYKColor(1,1,0,1/100);	
		hlColor = new CMYKColor(1,1,0,2/100);		
		sl1Color = new CMYKColor(1,1,0,3/100);		
		sl2Color = new CMYKColor(1,1,0,4/100);	
		sl3Color = new CMYKColor(1,1,0,5/100);	
		var shapes =  activeDocument.getItems({ 
    type: Path, 
    selected: true
	});	
		for (var j = 0; j < shapes.length; j++) 
	{			
		 shape = shapes[j];
//un-CompoundPath
	if(shape.parent instanceof CompoundPath){
		var alertMessage =  'PLEASE !! \n\nUN-Group(Ctrl+Shift+G), \nUN-CompoundPath(Ctrl+Shift+Alt+8)';
		dialog.alert(alertMessage);
	break;
	}	
	if (!(shape.bounds.width==0)){ tn=shape.bounds.height/shape.bounds.width;}
	if (shape.bounds.width==0 ) {vl=true;shape.style.stroke.color=vlColor}
	else if (shape.bounds.height==0) {hl=true; shape.style.stroke.color=hlColor;}
	else if (0<tn && tn<tan1){hl=true;shape.style.stroke.color=hlColor; }   
	else if (0>tn && tn>-tan1){hl=true;shape.style.stroke.color=hlColor; }   
	else if (tan2<tn) {vl=true; shape.style.stroke.color=vlColor; }  
	else if (-tan2>tn ) {vl=true; shape.style.stroke.color=vlColor; }    
	else if (tan2>tn && tn>tan1) {sl1=true; shape.style.stroke.color=sl1Color; }
	else if (-tan2<tn && tn<-tan1) {sl2=true;shape.style.stroke.color=sl2Color;}
	tn=0; 
	hl= vl=sl= sl2=false;
	}
	}
	}
}

var palette = new Palette('Stroke Input', components, values);


	

