package com.haxademic.core.draw.text;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PGraphics;
import processing.core.PImage;

import com.haxademic.core.app.P;
import com.haxademic.core.draw.util.OpenGLUtil;
import com.haxademic.core.image.ImageUtil;

public class CustomFontText2D {
	
	public static final int ALIGN_LEFT = P.LEFT;
	public static final int ALIGN_CENTER = P.CENTER;
	public static final int ALIGN_RIGHT = P.RIGHT;
	
	protected PFont _font;
	protected float _fontSize;
	protected PGraphics _textCanvas;
	protected int _textColor;
	protected int _textAlign;
	protected int _textLeading = 0;

	public CustomFontText2D( PApplet p, String fontFile, float fontSize, int color, int align, int canvasW, int canvasH ) {
		_fontSize = fontSize;
		_textLeading = (int) fontSize;
		_textColor = color;
		_font = p.createFont( fontFile, _fontSize );
		_textAlign = align;
		_textCanvas = p.createGraphics( canvasW, canvasH, P.JAVA2D );
		_textCanvas.smooth( OpenGLUtil.SMOOTH_HIGH );
	}
	
	public void setTextColor( int color ) {
		_textColor = color;
	}
	
	public void setTextAlign( int textAlign ) {
		_textAlign = textAlign;
	}
	
	public void setTextLeading( int textLeading ) {
		_textLeading = textLeading;
	}
	
	public int textAlign() {
		return _textAlign;
	}
	
	public void updateText( String txt ) {
		if( txt == null ) return;
		_textCanvas.beginDraw();
		_textCanvas.background( 0, 0 );		// clear background with alpha = 0 (only works in PGraphics)
		_textCanvas.fill( _textColor );
		_textCanvas.textAlign( _textAlign );
		_textCanvas.textFont( _font, _fontSize );
		_textCanvas.textLeading( _textLeading );
		_textCanvas.text( txt, 1, 1, _textCanvas.width, _textCanvas.height );
		_textCanvas.endDraw();
	}
	
	public PImage getTextPImage() {
		return _textCanvas;
	}
	
	public int getRightmostPixel() {
		int y = Math.round( (float) _textCanvas.height / 2f );
		int rightmost = 0;
		// check pixels across horizontal center to get a rough idea
		for( int i=0; i < _textCanvas.width; i++ ) {
			if( ImageUtil.getPixelColor( _textCanvas, i, y ) != ImageUtil.EMPTY_INT ) rightmost = i;
		}
		// then go down columns to find the real last pixel - allow for a few clear columns before calling it finished
		int columnsClear = 0;
		for( int i = rightmost; i < _textCanvas.width; i++ ) {
			boolean columnClear = true;
			for( int j=0; j < _textCanvas.height; j++ ) {
				if( ImageUtil.getPixelColor( _textCanvas, i, j ) != ImageUtil.EMPTY_INT ) {
					rightmost = i;
					columnsClear = 0;
					columnClear = false;
					break;
				}
			}
			if( columnClear == true ) {
				columnsClear++;
				if( columnsClear > 3 ) break;
			}
		}		
		return rightmost;
	}

}

@@ -197,56 +197,8 @@
 .P;%0A
-import com.haxademic.core.draw.util.OpenGLUtil;%0A
 impo
@@ -530,16 +530,49 @@
 tColor;%0A
+%09protected int _textStroke = -1;%0A
 %09protect
@@ -824,16 +824,46 @@
  color;%0A
+%09%09_textStroke = p.color(255);%0A
 %09%09_font 
@@ -983,24 +983,26 @@
 P.JAVA2D );%0A
+//
 %09%09_textCanva
@@ -1078,16 +1078,33 @@
 nt color
+, int strokeColor
  ) %7B%0A%09%09_
@@ -1122,16 +1122,45 @@
  color;%0A
+%09%09_textStroke = strokeColor;%0A
 %09%7D%0A%09%0A%09pu
@@ -1481,90 +1481,173 @@
 vas.
-background( 0, 0
+clear();%0A%09%09_textCanvas.textAlign( _textAlign
  );
+%0A
 %09%09
-// clear background with alpha = 0 (only works in PGraphics)%0A
+_textCanvas.textFont( _font, _fontSize );%0A%09%09_textCanvas.textLeading( _textLeading );%0A%0A%09%09if( _textStroke != -1 ) %7B%0A%09
 %09%09_t
@@ -1671,17 +1671,19 @@
 text
-Color
+Stroke
  );%0A
+%09
 %09%09_t
@@ -1696,37 +1696,71 @@
 vas.text
-Align( _textAlign
+( txt, 3, 3, _textCanvas.width, _textCanvas.height
  );%0A
+%09
 %09%09_textC
@@ -1773,36 +1773,65 @@
 text
-Font( _font, _fontSize
+( txt, 3, 7, _textCanvas.width, _textCanvas.height
  );%0A%09%09
+%09
 _tex
@@ -1842,37 +1842,179 @@
 vas.text
-Leading( _textLeading
+( txt, -1, 3, _textCanvas.width, _textCanvas.height );%0A%09%09%09_textCanvas.text( txt, -1, 7, _textCanvas.width, _textCanvas.height );%0A%09%09%7D%0A%09%09_textCanvas.fill( _textColor
  );%0A%09%09_t
@@ -2037,17 +2037,17 @@
 txt, 1, 
-1
+8
 , _textC

Add "stroke" to custom text