// MakePointType
// Nathaniel Vaughn KELSO
// 2007.July.8 
// at Hyattsville, MD
// Version 0.3
// DESC: Takes a area type box and extracts the text and turns into point type
// TODO: Fix baseline offset issue (where the text frame moves up from where the point text was)
// TODO: Extend to work with text on a line (PATHTEXT)
// TODO: Look inside groups
// TODO: Duplicate the angle of angled text boxes

// Version 0.2
// (c) vd [vd(kot)online.com.ua]

rotatePathType = true;

if(documents.length > 0) {
	doc = activeDocument;
	mySelection = activeDocument.selection;

	// If there are enough to process
	if (mySelection instanceof Array)

	// For each of the selected items
	for(i=0; i<mySelection.length; i++)
	// That are textFrames
	if (mySelection[i].typename == "TextFrame") {
		obj = mySelection[i];
		
			objTop = obj.top;
		objLeft = obj.left;
		objMiddleX = obj.left + .5 * obj.width;
		objMiddleY = obj.top - .5 * obj.height;
		objRight = obj.left + obj.width;
		objBottom = obj.top - obj.height;
		opp = 0.0;
		adj = 0.0
		myAngle = 0.0;
	
		// Make the new point type object and locate it
		copy1 = obj.parent.textFrames.add();

		// And keep it selected for after the script runs
		copy1.selected = true;

		// Run thru the lines
		for(j=0;j<obj.lines.length;j++) {
			frame = obj.lines[j];
			// If there is more than one line add a char return
			// TODO: Does this mess with formating to be Paragraph?
			if(j>0) copy1.paragraphs.add("");
			frame.duplicate(copy1);
		}
		
		if( obj.kind == TextType.AREATEXT ) {
			if( obj.textPath.pathPoints.length == 4 ) {
				// As long as the text is left justified
				if( obj.paragraphs[0].paragraphAttributes.justification == Justification.LEFT ) {
					copy1.left = objLeft;
					copy1.top = objTop;
 				} else if( obj.paragraphs[0].paragraphAttributes.justification == Justification.RIGHT ) {
 					// copy1.left = objRight;
 					copy1.left = objLeft;
					copy1.top = objTop;
 				} else if( obj.paragraphs[0].paragraphAttributes.justification == Justification.CENTER ) {
 					// copy1.left = objMiddleX;
 					copy1.left = objLeft;
					copy1.top = objTop;
 				}
 				
 				pX = obj.textPath.pathPoints[0].anchor[0];
				pY = obj.textPath.pathPoints[0].anchor[1];
				qX = obj.textPath.pathPoints[1].anchor[0];
				qY = obj.textPath.pathPoints[1].anchor[1];
  				// These two didn't work
// 				myAngle = Math.acos( (pX * qX + pY * qY) / a.distance(0,0) / b.distance(0,0) );
// 				myAngle = Math.atan2(pY,pX)-Math.atan2(qY,qX);
  				// NOTE: the angle returned depends on relationship of vectorInputs!
  				opp = -(qY - pY);
  				adj = qX - pX;
  				myAngle = Math.atan2(opp, adj);
 				myAngle = -1 * myAngle/Math.PI*180;
				copy1.rotate( myAngle );
 				// obj.remove();
 			}	
			obj.remove();
		} else if( obj.kind == TextType.PATHTEXT ) {
//			var dupPathRef = doc.pathItems.duplicate( obj.textPath );

			endPointElement = obj.textPath.pathPoints.length - 1;
		
			// TO LINE BEGINNING
			// copy1.left = obj.textPath.pathPoints[0].anchor[0];
			// copy1.top  = obj.textPath.pathPoints[0].anchor[1];
			
			// TO LINE TOP LEFT
			// copy1.left = objLeft;
			// copy1.top = objTop;
			
			// TO LINE MIDDLE
			// copy1.left = objMiddleX;
			// copy1.top  = objMiddleY;
			
			// TODO: Account for Polarity of line!!!
			// Begining of line
			// if( obj.textPath.polarity == PolarityValues.POSITIVE ) {
				if( obj.startTValue <= .25 ) {
					copy1.left = obj.textPath.pathPoints[0].anchor[0];
					copy1.top  = obj.textPath.pathPoints[0].anchor[1];
				// End of Line
				} else if( obj.startTValue >= .75 ) {
					copy1.left = obj.textPath.pathPoints[endPointElement].anchor[0];
					copy1.top  = obj.textPath.pathPoints[endPointElement].anchor[1];
				// Middle of Line
				} else {
					copy1.left = objMiddleX;
					copy1.top  = objMiddleY;
				}
			/* } else if( obj.textPath.polarity == PolarityValues.NEGATIVE ) {
				if( obj.startTValue <= .75 ) {
					copy1.left = obj.textPath.pathPoints[endPointElement].anchor[0];
					copy1.top  = obj.textPath.pathPoints[endPointElement].anchor[1];
				// End of Line
				} else if( obj.startTValue >= .25 ) {
					copy1.left = obj.textPath.pathPoints[0].anchor[0];
					copy1.top  = obj.textPath.pathPoints[0].anchor[1];
				// Middle of Line
				} else {
					copy1.left = objMiddleX;
					copy1.top  = objMiddleY;
				}
			}
			*/
			
			
			if( rotatePathType ) {
				pX = obj.textPath.pathPoints[0].anchor[0];
				pY = obj.textPath.pathPoints[0].anchor[1];
				qX = obj.textPath.pathPoints[endPointElement].anchor[0];
				qY = obj.textPath.pathPoints[endPointElement].anchor[1];
  				// NOTE: the angle returned depends on relationship of vectorInputs!
  				opp = -(qY - pY);
  				adj = qX - pX;
  				myAngle = Math.atan2(opp, adj);
 				myAngle = -1 * myAngle/Math.PI*180;
				copy1.rotate( myAngle );
			}

			// DELETE ENTIRE ORIG OBJECT
			obj.remove();
			
			// KEEP LINE, but DELETE TEXT ON IT
//			obj.textRange.remove();
		}
	}
}