[Fencommits] libvob: Add default implementation.

Matti J. Katila majukati at cc.jyu.fi
Tue Jan 17 21:05:03 EET 2006


Tue Jan 17 20:29:42 EET 2006  Matti J. Katila <majukati at cc.jyu.fi>
  * Add default implementation.

diff -rN -u libvob-old/org/nongnu/libvob/impl/terminal/TERMINALAPI.java libvob-new/org/nongnu/libvob/impl/terminal/TERMINALAPI.java
--- libvob-old/org/nongnu/libvob/impl/terminal/TERMINALAPI.java	2006-01-17 21:05:02.467764123 +0200
+++ libvob-new/org/nongnu/libvob/impl/terminal/TERMINALAPI.java	2006-01-17 20:32:47.000000000 +0200
@@ -7,6 +7,8 @@
 
 
 public class TERMINALAPI extends GraphicsAPI {
+    static private void p(String s) { System.out.println("TERMAPI:: "+s); }
+
 
     public void startUpdateManager(java.lang.Runnable r){
 	TerminalUpdateManager.startUpdateManager(r);
@@ -27,6 +29,39 @@
 
     public TextStyle getTextStyle(java.lang.String family, 
 				  int style, int size) {
-	return new RawTextStyle(new ScalableFont(family, style, size), null);
+	//p("STYLE for: "+size);
+	return this.style; //new RawTextStyle(new ScalableFont(family, style, size), null);
     }
+
+
+    TextStyle style = new TextStyle(){
+
+	    /** Get the scale to use to get a font in this style of height h.
+	     */
+	    public float getScaleByHeight(float h) {
+		return 1;
+	    }
+
+	    /** Get a copy of this style, scaled so that getHeight(1)
+	     *  is (approximately) h.
+	     */
+	    public TextStyle getScaledStyle(float h) { return this; }
+
+	    public float getWidth(String s, float scale) {
+		return s.length()*8*scale;
+	    }
+	    public float getWidth(char[] chars, int offs,
+				  int len, float scale){
+		return chars.length*8*scale;
+	    }
+
+	    public float getHeight(float scale) { return 16*scale;}
+
+	    public float getAscent(float scale) { return 15*scale; }
+
+	    public float getDescent(float scale) { return scale; }
+
+	    public float getLeading(float scale) { return 0; }
+	};
+
 }
diff -rN -u libvob-old/org/nongnu/libvob/impl/terminal/TerminalGraphics.java libvob-new/org/nongnu/libvob/impl/terminal/TerminalGraphics.java
--- libvob-old/org/nongnu/libvob/impl/terminal/TerminalGraphics.java	2006-01-17 21:05:02.466764273 +0200
+++ libvob-new/org/nongnu/libvob/impl/terminal/TerminalGraphics.java	2006-01-17 20:32:47.000000000 +0200
@@ -18,35 +18,13 @@
 
 
 
-    /* Some ANSI color codes:
-     *
-     * [30m  set foreground color to black
-     * [31m  set foreground color to red
-     * [32m  set foreground color to green
-     * [33m  set foreground color to yellow
-     * [34m  set foreground color to blue
-     * [35m  set foreground color to magenta (purple)
-     * [36m  set foreground color to cyan
-     * [37m  set foreground color to white
-     * [39m  set foreground color to default (white)
-     *
-     * [40m  set background color to black
-     * [41m  set background color to red
-     * [42m  set background color to green
-     * [43m  set background color to yellow
-     * [44m  set background color to blue
-     * [45m  set background color to magenta (purple)
-     * [46m  set background color to cyan
-     * [47m  set background color to white
-     * [49m  set background color to default (black)
-     */
-
     private Dimension termSize, size;
     private int xBits = 8, yBits = 16;
     private Color buff[];
     private Color innerColor = Color.BLACK;
     private Color outerColor = Color.BLACK;
     private Rectangle clip;
+    private ANSIBuffer ansiBuff;
 
     private Color[] colors = {
         Color.BLACK,
@@ -62,12 +40,12 @@
 
     public TerminalGraphics(Dimension termSize) {
 	this.termSize = termSize;
-	p("size: "+termSize);
+	this.ansiBuff = new ANSIBuffer(termSize);
 	this.size = new Dimension(termSize.width * xBits, 
 				  termSize.height * yBits);
 	this.buff = new Color[size.width * size.height];
 
-	setBeginState();
+	dispose();
 
 	if (dbg) {
 	    dbgFrame = new Frame();
@@ -131,15 +109,97 @@
 	new TerminalGraphics(new Dimension(20,20));
     }
 
+    /** Converts the pixel buffer to character buffer.
+     */
+    public ANSIBuffer convert() {
+	int [] counts= new int[colors.length];
+	Color[] two = new Color[2];
+	int [] shapeCounts = new int[font8x16.length/16];
+	for (int xi = 0; xi<termSize.width; xi++){
+	    for (int yi = 0; yi<termSize.height; yi++){
+		// count the colors in area
+		countColors(xi,yi,counts);
 
+		// reduce
+		reduce(xi, yi, counts, two);
 
+		// find out character
+		char ch = findChar(xi, yi, two, shapeCounts);
 
-    public void setBeginState() {
-	this.clip = new Rectangle(0,0,size.width, size.height);
-	this.innerColor = outerColor = Color.BLACK;
+		// do aalib..
+		ansiBuff.set(xi,yi, two[1], two[0], ch);
+	    }
+	}
+	return ansiBuff;
+    }
+
+    private void countColors(int x, int y, int[] counts) {
+	Arrays.fill(counts, 0);
+	for (int xi = 0; xi<xBits; xi++){
+	    for (int yi = 0; yi<yBits; yi++){
+		Color c = buff[(y+yi)*size.width + (x+xi)];
+		for (int i=0; i<colors.length; i++)
+		    if (colors[i] == c)
+			counts[i] += 1;
+	    }
+	}
+    }
+
+    private void reduce(int x, int y, int[] counts, Color two[]) {
+
+	// find out which colors are the most interesting
+	int tmp[] = new int[counts.length];
+	System.arraycopy(counts, 0, tmp, 0, counts.length);
+	Arrays.sort(tmp);
+
+	for (int i=0; i<colors.length; i++) {
+	    if (counts[i] == tmp[counts.length-1]) two[0] = colors[i];
+	    else if (counts[i] == tmp[counts.length-2]) two[1] = colors[i];
+	}
+
+	for (int xi = 0; xi<xBits; xi++){
+	    for (int yi = 0; yi<yBits; yi++){
+		int index = (y+yi)*size.width + (x+xi);
+		Color c = buff[index];
+		buff[index] = getClosestColor(c, two);
+		if (dbg) dbgArr[index] = buff[index].getRGB();
+	    }
+	}
+    }
+
+    private char findChar(int x, int y, Color [] two, int[] shapeCounts) {
+	Arrays.fill(shapeCounts, 0);
+
+	for (int i=33; i<128; i++)
+	{
+	    for (int yi = 0; yi<yBits; yi++){
+
+		char ch = font8x16[16*i + yi];
+
+		for (int xi = 0; xi<xBits; xi++){
+		    int index = (y+yi)*size.width + (x+7-xi);
+		    Color c = buff[index];
+		    boolean b = (ch & (1<<xi)) != 0;
+		    int inc = b?1:-1;
+		    if (two[0] == c)
+			shapeCounts[i] += inc;
+		    else
+			shapeCounts[i] -= inc;
+		}
+	    }
+	}
 
+	int max = 0;
+	for (int i=0; i<shapeCounts.length; i++)
+	{
+	    int val = shapeCounts[i];
+	    if (val < 0) val = -val;
+	    if (val > shapeCounts[max]) max = i;
+	}
+	return (char)max;
     }
 
+
     public Dimension getSize() { return size; }
 
     private Color getClosestColor(Color out, Color[] colors) {
@@ -198,7 +258,10 @@
 	return this;
     }
 
-    public void dispose() {}
+    public void dispose() {
+	this.clip = new Rectangle(0,0,size.width, size.height);
+	this.innerColor = outerColor = Color.BLACK;
+    }
     
     public void draw3DRect(int x, int y, int width, int height, 
 				   boolean raised) {
@@ -286,6 +349,9 @@
 	    int fontIndex = ch*16;
 	    for (int yi=0; yi<16; yi++) {
 		for (int xi=0; xi<8; xi++) {
+		    if (y+yi >= size.height) break;
+		    if (x+xi >= size.width) break;
+
 		    if (y+yi>= clip.y+clip.height) continue;
 		    if (x+xi>= clip.x+clip.width) continue;
 
diff -rN -u libvob-old/org/nongnu/libvob/impl/terminal/TerminalUpdateManager.java libvob-new/org/nongnu/libvob/impl/terminal/TerminalUpdateManager.java
--- libvob-old/org/nongnu/libvob/impl/terminal/TerminalUpdateManager.java	2006-01-17 21:05:02.463764725 +0200
+++ libvob-new/org/nongnu/libvob/impl/terminal/TerminalUpdateManager.java	2006-01-17 20:32:47.000000000 +0200
@@ -11,7 +11,7 @@
 /** A singleton class update manager for terminal.
  */
 public class TerminalUpdateManager extends AbstractUpdateManager {
-    private static boolean dbg = true;
+    private static boolean dbg = false;
     private static void p(String s) { System.out.println("TermUpdateMgr:: "+s); }
 
     static private TerminalUpdateManager mgr = null;
@@ -19,8 +19,6 @@
     
     private TerminalUpdateManager(Runnable r) { super(r); }
 
-    //static public TerminalUpdateManager getInstance() { return mgr; }
-
     static void startUpdateManager(Runnable r) {
 	if (mgr != null) 
 	    throw new Error("Only one instance of terminal update manager allowed!");
@@ -52,7 +50,7 @@
     }
 
     protected void synchronizeToolkit() {
-	Toolkit.getDefaultToolkit().sync();
+	//Toolkit.getDefaultToolkit().sync();
     }
 
 }
diff -rN -u libvob-old/org/nongnu/libvob/impl/terminal/TerminalWindow.java libvob-new/org/nongnu/libvob/impl/terminal/TerminalWindow.java
--- libvob-old/org/nongnu/libvob/impl/terminal/TerminalWindow.java	2006-01-17 21:05:02.462764876 +0200
+++ libvob-new/org/nongnu/libvob/impl/terminal/TerminalWindow.java	2006-01-17 20:32:47.000000000 +0200
@@ -27,7 +27,7 @@
     VobScene listprev, listnext;
     int[] interplist;
 
-    public TerminalWindow(GraphicsAPI api) {
+    TerminalWindow(GraphicsAPI api) {
 	super(api);
 	try {
 	    terminal = new UnixTerminal();
@@ -36,9 +36,12 @@
 	    g = new TerminalGraphics(terminalSize);
 	    ((TerminalUpdateManager)AbstractUpdateManager.getInstance()).set(terminal);
 	} catch (Exception e) { e.printStackTrace(); }
-
+	instance = this;
     }
 
+    private TerminalWindow instance = null;
+    public TerminalWindow getInstance() { return instance; }
+
     public void renderStill(VobScene prev, float lod) {
 	renderAnim(prev, null, 0, lod, true);
     }
@@ -50,6 +53,8 @@
 	if(dbg) p("renderanim: "+prev+" "+next);
 	if (g == null) return;
 	//canvas.paint(g);
+	// terminal .flush...
+	g.convert().flush();
 	g.dispose();
     }
 




More information about the Fencommits mailing list