[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ff-cvs] fenfire/org/fenfire/view TextNodeView.java Text...
From: |
Asko Soukka |
Subject: |
[ff-cvs] fenfire/org/fenfire/view TextNodeView.java Text... |
Date: |
Sun, 17 Aug 2003 15:50:10 -0400 |
CVSROOT: /cvsroot/fenfire
Module name: fenfire
Branch:
Changes by: Asko Soukka <address@hidden> 03/08/17 15:50:10
Modified files:
org/fenfire/view: TextNodeView.java TextNodeView.test
papercanvas2d.test
Log message:
Immutable TextNodeView. Tests passes, demos are most propably broken.
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/fenfire/fenfire/org/fenfire/view/TextNodeView.java.diff?tr1=1.15&tr2=1.16&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/fenfire/fenfire/org/fenfire/view/TextNodeView.test.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/fenfire/fenfire/org/fenfire/view/papercanvas2d.test.diff?tr1=1.14&tr2=1.15&r1=text&r2=text
Patches:
Index: fenfire/org/fenfire/view/TextNodeView.java
diff -u fenfire/org/fenfire/view/TextNodeView.java:1.15
fenfire/org/fenfire/view/TextNodeView.java:1.16
--- fenfire/org/fenfire/view/TextNodeView.java:1.15 Fri Aug 15 09:44:46 2003
+++ fenfire/org/fenfire/view/TextNodeView.java Sun Aug 17 15:50:10 2003
@@ -1,7 +1,7 @@
/*
TextNodeView.java
*
- * Copyright (c) 2003, Benja Fallenstein
+ * Copyright (c) 2003, Benja Fallenstein, Asko Soukka
*
* This file is part of Fenfire.
*
@@ -23,7 +23,7 @@
*
*/
/*
- * Written by Benja Fallenstein
+ * Written by Benja Fallenstein, Asko Soukka
*/
package org.fenfire.view;
import org.fenfire.swamp.*;
@@ -32,51 +32,114 @@
import org.nongnu.libvob.linebreaking.*;
import org.nongnu.libvob.vobs.*;
+import java.awt.Color;
import java.lang.Math;
-/** A node function returning a vob that shows
- * the given node as text.
- * <em>Should</em> take a width and do linebreaking;
- * currently, places text as a single very long line.
+/**
+ * A node function returning a vob that shows the given node as text.
+ * The maximum size of a single line is determined by <pre>width</pre>.
*/
-public class TextNodeView extends org.fenfire.view.lava.TextHandler
- implements NodeFunction {
-public static final String rcsid = "$Id: TextNodeView.java,v 1.15 2003/08/15
13:44:46 humppake Exp $";
+public class TextNodeView implements NodeFunction {
+ public static final String rcsid = "$Id: TextNodeView.java,v 1.16
2003/08/17 19:50:10 humppake Exp $";
+
public static boolean dbg = false;
private static void pa(String s) { System.out.println("TextNodeView::"+s);
}
- final SimpleLinebreaker breaker = new SimpleLinebreaker();
-
- final NodeFunction nodeContent;
-
- final float width = 300;
+ public final static float DEFAULT_SCALE = 1f;
+ public final static float DEFAULT_WIDTH = 300f;
+ public final static Color DEFAULT_TEXT_COLOR = Color.black;
+
+ private final SimpleLinebreaker breaker = new SimpleLinebreaker();
+ private final NodeFunction nodeContent;
+ private final TextStyle style;
+ private final Color textColor;
+ private final float scale;
+ private final float width;
public TextNodeView(NodeFunction nodeContent,
TextStyle style,
+ Color textColor,
+ float width,
float scale) {
this.nodeContent = nodeContent;
- super.setStyle(style);
- super.setScale(scale);
+ this.style = style;
+ this.textColor = textColor;
+ this.width = width;
+ this.scale = scale;
+ }
+ public TextNodeView(NodeFunction nodeContent, TextStyle style,
+ Color textColor, float width) {
+ this(nodeContent, style, textColor, width, DEFAULT_SCALE);
+ }
+ public TextNodeView(NodeFunction nodeContent, TextStyle style,
+ Color textColor) {
+ this(nodeContent, style, textColor, DEFAULT_WIDTH, DEFAULT_SCALE);
+ }
+ public TextNodeView(NodeFunction nodeContent, TextStyle style,
+ float scale) {
+ this(nodeContent, style, DEFAULT_TEXT_COLOR, scale, DEFAULT_SCALE);
+ }
+ public TextNodeView(NodeFunction nodeContent, TextStyle style) {
+ this(nodeContent, style, DEFAULT_TEXT_COLOR, DEFAULT_WIDTH,
DEFAULT_WIDTH);
}
- private boolean hasContext = true;
- public void setHasNoContext() { hasContext = false; }
+ public Object f(ConstGraph g, Object node) {
+ final Enfilade1D enf = (Enfilade1D)nodeContent.f(g, node);
+ final Object objNode = node;
+ final String s = enf.makeString();
+ if (s.length() == 0)
+ return new org.nongnu.libvob.lava.placeable.Placeable() {
+ public void place(VobScene vs, int into) {
+ }
+ public float getWidth() { return 10; }
+ public float getHeight() { return 10; }
+ };
+
+ final HChain ch = getChain(s);
+ final HBroken br = breaker.breakLines(ch, width, scale);
+ final float height = br.getHeight();
+
+ final float width;
+ if(br.getLineCount() > 1) { // Let's get the longest line
+ float maxWidth = br.getLineWidth(0);
+ for (int i=1; i<br.getLineCount(); i++)
+ if (br.getLineWidth(i) > maxWidth)
+ maxWidth = br.getLineWidth(i);
+ width = maxWidth;
+ } else width = br.getLineWidth(0);
+
+ //// Code of the old single line TextNodeView
+ //final TextVob vob = new TextVob(style, s, false);
+ //final float width = style.getWidth(s, scale);
+ //final float height = style.getHeight(scale);
- private org.fenfire.view.lava.TextHandler.Context context = null;
- public void setContext(org.fenfire.view.lava.TextHandler.Context context)
- { this.context = context; }
+ if(dbg) {
+ pa(" "+s+"' "+width+" "+height+" "+scale+" "+br);
+ }
- /** Get position of the first character placed
- * after given coordinates.
+ return new org.nongnu.libvob.lava.placeable.Placeable() {
+ public void place(VobScene vs, int into) {
+ br.put(vs, into);
+ }
+
+ public float getWidth() { return width; }
+ public float getHeight() { return height; }
+ };
+ }
+
+ /**
+ * Get the position of the first character placed after given coordinates.
*/
- public int getPos(ConstGraph g, Object node, float x, float y) {
+ public void getPos(ConstGraph g, Object node, float x, float y) {
Enfilade1D enf = (Enfilade1D)nodeContent.f(g, node);
- final Object objNode = node;
- String s = enf.makeString();
+ Object objNode = node;
+ getPos(enf.makeString(), x, y);
+ }
+ public int getPos(String s, float x, float y) {
if (s.length() == 0) return 0;
HChain ch = getChain(s);
- final HBroken br = breaker.breakLines(ch, width*scale, scale);
+ HBroken br = breaker.breakLines(ch, width, scale);
int pos = 0;
int line = 0;
@@ -119,13 +182,16 @@
return pos;
}
- /** Get coordinates before the given character position.
- * The Y coordinate is located below the line.
+ /**
+ * Get the coordinates before the given character position.
+ * The Y coordinate will be located just below the the line.
*/
public void getXY(ConstGraph g, Object node, int pos, float[] xy) {
Enfilade1D enf = (Enfilade1D)nodeContent.f(g, node);
- final Object objNode = node;
- String s = enf.makeString();
+ Object objNode = node;
+ getXY(enf.makeString(), pos, xy);
+ }
+ public void getXY(String s, int pos, float[] xy) {
if (s.length() == 0) {
if (xy != null && xy.length >= 2) {
xy[0] = 0;
@@ -135,69 +201,20 @@
}
HChain ch = getChain(s);
- final HBroken br = breaker.breakLines(ch, width*scale, scale);
+ HBroken br = breaker.breakLines(ch, width, scale);
float xoffs[] = new float[1];
int line = br.getLine(pos, xoffs);
if (xy != null && xy.length >= 2) {
- // XXX the x offs doesn't seem to be exact, when there is a lot of
spaces
+ // XXX the x offs doesn't seem to be exact,
+ // when there is a lot of spaces. An old bug.
xy[0] = xoffs[0];
xy[1] = br.getLineOffset(line);
}
}
- public Object f(ConstGraph g, Object node) {
- if (hasContext) {
- if (context == null) throw new Error("No context set");
- context.toBePlaced(node, this);
- }
-
- Enfilade1D enf = (Enfilade1D)nodeContent.f(g, node);
-
- final Object objNode = node;
- String s = enf.makeString();
- if (s.length() == 0)
- return new org.nongnu.libvob.lava.placeable.Placeable() {
- public void place(VobScene vs, int into) {
- }
- public float getWidth() { return 10; }
- public float getHeight() { return 10; }
- };
-
- HChain ch = getChain(s);
- final HBroken br = breaker.breakLines(ch, width*scale, scale);
- final float height = br.getHeight();
-
- final float width;
- if(br.getLineCount() > 1) { // Let's get the longest line
- float maxWidth = br.getLineWidth(0);
- for (int i=1; i<br.getLineCount(); i++)
- if (br.getLineWidth(i) > maxWidth)
- maxWidth = br.getLineWidth(i);
- width = maxWidth;
- } else width = br.getLineWidth(0);
-
- //final TextVob vob = new TextVob(style, s, false);
- //final float width = style.getWidth(s, scale);
- //final float height = style.getHeight(scale);
-
- if(dbg) {
- pa(" "+s+"' "+width+" "+height+" "+scale+" "+br);
- }
-
- return new org.nongnu.libvob.lava.placeable.Placeable() {
- public void place(VobScene vs, int into) {
- br.put(vs, into);
- }
-
- public float getWidth() { return width; }
- public float getHeight() { return height; }
- };
- }
-
-
- protected HChain getChain(String s) {
+ protected HChain getChain(String s) {
HChain ch = new LinebreakableChain();
int pos = 0;
@@ -231,7 +248,7 @@
if(dbg) pa("addVobs: "+start+" "+end+" '"+s+"'");
- TextVob vob = new TextVob(style, s, false, key, color);
+ TextVob vob = new TextVob(style, s, false, key, textColor);
ch.addBox(vob);
}
}
Index: fenfire/org/fenfire/view/TextNodeView.test
diff -u fenfire/org/fenfire/view/TextNodeView.test:1.7
fenfire/org/fenfire/view/TextNodeView.test:1.8
--- fenfire/org/fenfire/view/TextNodeView.test:1.7 Tue Aug 5 05:54:18 2003
+++ fenfire/org/fenfire/view/TextNodeView.test Sun Aug 17 15:50:10 2003
@@ -1,6 +1,6 @@
# -*-python-*-
#
-# Copyright (c) 2003, Tuomas J. Lukka
+# Copyright (c) 2003, Tuomas J. Lukka, Asko Soukka
# This file is part of Fenfire.
#
# Fenfire is free software; you can redistribute it and/or modify it under
@@ -19,9 +19,7 @@
# MA 02111-1307 USA
#
-
-
-import java
+import java, jarray
import vob
from org.fenfire.swamp import Nodes
@@ -34,7 +32,6 @@
gfx.needGL()
-
def testNotEmptyAndEmptyNodes():
# fail: GL
"""
@@ -45,7 +42,7 @@
fen = ff.test.fen.newFen()
textstyle = vob.GraphicsAPI.getInstance().getTextStyle("sans", 0, 24)
- textnodeview = ff.view.TextNodeView(fen.txtfunc, textstyle, 1)
+ textnodeview = ff.view.TextNodeView(fen.txtfunc, textstyle)
view = ff.view.CanvasView2D(fen, textnodeview)
view.cull = 1
paperview = ff.view.PaperView2D(view)
@@ -69,3 +66,19 @@
30, 30, 1, 1, 50, 50)
mainNode.renderMain(vs, into)
gfx.render(vs)
+
+def testTextCursorLocating():
+ """
+ Test that locating the text cursor works back and forth.
+ """
+ fen = ff.test.fen.newFen()
+ textstyle = vob.GraphicsAPI.getInstance().getTextStyle("sans", 0, 24)
+ textnodeview = ff.view.TextNodeView(fen.txtfunc, textstyle)
+
+ note = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy \nrmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
kasd \nbergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
ipsum dolor sit amet, consetetur sadipscing elitr, sed diam\nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores \n ea rebum. Stet\nlita kasd
gubergren, no sea ta\nmata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero
eos e\naccusam et justo duo dolores et ea re\nm. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet."
+
+ xy = jarray.zeros(2, 'f')
+ textnodeview.getXY(note, 200, xy)
+
+ assert textnodeview.getPos(note, xy[0], xy[1]) == 200, \
+ 'Text cursor position 200 was not found at (%s, %s)' % (xy[0],
xy[1])
Index: fenfire/org/fenfire/view/papercanvas2d.test
diff -u fenfire/org/fenfire/view/papercanvas2d.test:1.14
fenfire/org/fenfire/view/papercanvas2d.test:1.15
--- fenfire/org/fenfire/view/papercanvas2d.test:1.14 Tue Aug 5 05:54:18 2003
+++ fenfire/org/fenfire/view/papercanvas2d.test Sun Aug 17 15:50:10 2003
@@ -74,7 +74,6 @@
fen.txtfunc,
vs.gfxapi.getTextStyle("Sans", 0, 50),
1)
- textnodeview.setHasNoContext()
canvasView2D = ff.view.CanvasView2D(fen, textnodeview)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [ff-cvs] fenfire/org/fenfire/view TextNodeView.java Text...,
Asko Soukka <=