diff --git a/processing/app/PdeRuntime.java b/processing/app/PdeRuntime.java index 2689e50e8..c6e60b842 100644 --- a/processing/app/PdeRuntime.java +++ b/processing/app/PdeRuntime.java @@ -17,8 +17,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ @@ -79,27 +79,27 @@ public class PdeRuntime implements PdeMessageConsumer { // using something like --external=e30,20 where the e stands for // exact. otherwise --external=x,y for just the regular positioning. /* - String location = (windowLocation != null) ? - (PApplet.EXTERNAL_EXACT_LOCATION + - windowLocation.x + "," + windowLocation.y) : - (x1 + "," + y1); + String location = (windowLocation != null) ? + (PApplet.EXTERNAL_EXACT_LOCATION + + windowLocation.x + "," + windowLocation.y) : + (x1 + "," + y1); */ - String location = - (windowLocation != null) ? - (PApplet.EXT_EXACT_LOCATION + + String location = + (windowLocation != null) ? + (PApplet.EXT_EXACT_LOCATION + windowLocation.x + "," + windowLocation.y) : (PApplet.EXT_LOCATION + x1 + "," + y1); //System.out.println("library path is " + sketch.libraryPath); String command[] = new String[] { - //"cmd", "/c", "start", + //"cmd", "/c", "start", "java", - "-Djava.library.path=" + + "-Djava.library.path=" + // sketch.libraryPath might be "" // librariesClassPath will always have sep char prepended - sketch.libraryPath + - //PdeSketchbook.librariesClassPath + + sketch.libraryPath + + //PdeSketchbook.librariesClassPath + File.pathSeparator + System.getProperty("java.library.path"), "-cp", sketch.classPath + PdeSketchbook.librariesClassPath, @@ -159,7 +159,7 @@ public class PdeRuntime implements PdeMessageConsumer { //System.out.println(meth[i].getName()); if (meth[i].getName().equals("draw")) drawMode = true; } - } catch (SecurityException e) { + } catch (SecurityException e) { e.printStackTrace(); } // if it's a draw mode app, don't even show on-screen @@ -229,7 +229,7 @@ public class PdeRuntime implements PdeMessageConsumer { window.addKeyListener(applet); */ - Dimension screen = + Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); //System.out.println(SystemColor.windowBorder.toString()); @@ -256,9 +256,9 @@ public class PdeRuntime implements PdeMessageConsumer { int minW = PdePreferences.getInteger("run.window.width.minimum"); int minH = PdePreferences.getInteger("run.window.height.minimum"); - int windowW = + int windowW = Math.max(applet.width, minW) + insets.left + insets.right; - int windowH = + int windowH = Math.max(applet.height, minH) + insets.top + insets.bottom; if (x1 - windowW > 10) { // if it fits to the left of the window @@ -282,10 +282,10 @@ public class PdeRuntime implements PdeMessageConsumer { Color windowBgColor = PdePreferences.getColor("run.window.bgcolor"); window.setBackground(windowBgColor); - applet.setBounds((windowW - applet.width)/2, - insets.top + ((windowH - + applet.setBounds((windowW - applet.width)/2, + insets.top + ((windowH - insets.top - insets.bottom) - - applet.height)/2, + applet.height)/2, windowW, windowH); } @@ -318,7 +318,7 @@ public class PdeRuntime implements PdeMessageConsumer { applet.stop(); //if (window != null) window.hide(); - // above avoids NullPointerExceptions + // above avoids NullPointerExceptions // but still threading is too complex, and so // some boogers are being left behind @@ -385,20 +385,23 @@ public class PdeRuntime implements PdeMessageConsumer { // these are used for debugging, in case there are concerns // that som errors aren't coming through properly - //System.err.println("message " + s.length() + ":" + s); - //if (s.length() > 2) System.err.println(s); + /* + if (s.length() > 2) { + System.err.println("message " + s.length() + ":" + s); + } + */ - // this is PApplet sending a message saying "i'm about to spew + // this is PApplet sending a message saying "i'm about to spew // a stack trace because an error occurred during PApplet.run()" if (s.indexOf(PApplet.LEECH_WAKEUP) == 0) { - // newMessage being set to 'true' means that the next time + // newMessage being set to 'true' means that the next time // message() is called, expect the first line of the actual // error message & stack trace to be sent from the applet. newMessage = true; return; // this line ignored } - // if s.length <=2, ignore it because that probably means + // if s.length <=2, ignore it because that probably means // that it's just the platform line-terminators. if (newMessage && s.length() > 2) { exception = new PdeException(s); // type of java ex @@ -482,7 +485,7 @@ java.lang.NullPointerException lineNumberStr = lineNumberStr.substring(0, index); try { exception.line = Integer.parseInt(lineNumberStr) - 1; //2; - } catch (NumberFormatException e) { } + } catch (NumberFormatException e) { } //e.printStackTrace(); // a recursive error waiting to happen? // if nfe occurs, who cares, still send the error on up editor.error(exception); @@ -496,14 +499,14 @@ java.lang.NullPointerException // would also probably get: // at Temporary_484_3845.loop // which (i believe) is used by the mac and/or jview - String functionStr = s.substring(index + + String functionStr = s.substring(index + (className + ".class").length() + 1); index = functionStr.indexOf('('); if (index != -1) { functionStr = functionStr.substring(0, index); } exception = new PdeException(//"inside \"" + functionStr + "()\": " + - exception.getMessage() + + exception.getMessage() + " inside " + functionStr + "() " + "[add Compiler.disable() to setup()]"); editor.error(exception); @@ -511,12 +514,12 @@ java.lang.NullPointerException // at Temporary_4636_9696.pootie(Compiled Code) // at Temporary_4636_9696.loop(Temporary_4636_9696.java:24) // because pootie() (re)sets the exception title - // and throws it, but then the line number gets set + // and throws it, but then the line number gets set // because of the line that comes after */ - + } else if (messageLineCount > 5) { - // this means the class name may not be mentioned + // this means the class name may not be mentioned // in the stack trace.. this is just a general purpose // error, but needs to make it through anyway. // so if five lines have gone past, might as well signal @@ -526,7 +529,7 @@ java.lang.NullPointerException } else { //System.err.print(s); - } + } //System.out.println("got it " + s); } } diff --git a/processing/core/PApplet.java b/processing/core/PApplet.java index a57575f29..c96f2d67f 100644 --- a/processing/core/PApplet.java +++ b/processing/core/PApplet.java @@ -76,7 +76,7 @@ public class PApplet extends Applet * Last key pressed. If it's a coded key * (arrows or ctrl/shift/alt, this will be set to 0xffff or 65535). */ - public int key; + public char key; /** * If the key is a coded key, i.e. up/down/ctrl/shift/alt @@ -515,7 +515,7 @@ public class PApplet extends Applet " 1b draw"); for (int i = 0; i < libraryCount; i++) { - if (libraryCalls[PLibrary.PRE][i]) libraries[i].pre(); + if (libraryCalls[i][PLibrary.PRE]) libraries[i].pre(); } draw(); @@ -530,7 +530,7 @@ public class PApplet extends Applet " 2b endFrame"); for (int i = 0; i < libraryCount; i++) { - if (libraryCalls[PLibrary.DRAW][i]) libraries[i].draw(); + if (libraryCalls[i][PLibrary.DRAW]) libraries[i].draw(); } g.endFrame(); @@ -555,7 +555,7 @@ public class PApplet extends Applet //frameCount++; for (int i = 0; i < libraryCount; i++) { - if (libraryCalls[PLibrary.POST][i]) libraries[i].post(); + if (libraryCalls[i][PLibrary.POST]) libraries[i].post(); } } } @@ -4496,13 +4496,13 @@ public class PApplet extends Applet } - public void textFont(PFont which, float size) { - g.textFont(which, size); + public void textSize(float size) { + g.textSize(size); } - public void textSize(float size) { - g.textSize(size); + public void textFont(PFont which, float size) { + g.textFont(which, size); } diff --git a/processing/core/PFont.java b/processing/core/PFont.java index 63f8b3806..bae934dea 100644 --- a/processing/core/PFont.java +++ b/processing/core/PFont.java @@ -65,8 +65,8 @@ public class PFont implements PConstants { float fwidth, fheight; // mbox is just the font size (i.e. 48 for most vlw fonts) - public int mbox2; // next power of 2 - public int mbox; // font size + public int mbox2; // next power of 2 over the max image size + public int mbox; // actual "font size" of source font public int value[]; // char code public int height[]; // height of the bitmap data @@ -81,10 +81,16 @@ public class PFont implements PConstants { // scaling, for convenience public float size; public float leading; + public int align; + public int space; int ascii[]; // quick lookup for the ascii chars boolean cached; + // used by the text() functions to avoid over-allocation of memory + private char textBuffer[] = new char[8 * 1024]; + private char widthBuffer[] = new char[8 * 1024]; + public PFont() { } // for PFontAI subclass and font builder @@ -195,8 +201,11 @@ public class PFont implements PConstants { //System.out.println(); } cached = false; + resetSize(); resetLeading(); // ?? + space = OBJECT_SPACE; + align = ALIGN_LEFT; } //static boolean isSpace(int c) { @@ -280,13 +289,28 @@ public class PFont implements PConstants { } + public void space(int which) { + this.space = which; + if (space == SCREEN_SPACE) { + resetSize(); + resetLeading(); + } + } + + + public void align(int which) { + this.align = which; + } + + public float kern(char a, char b) { return 0; // * size, but since zero.. } public void resetSize() { - size = 12; + //size = 12; + size = mbox; // default size for the font } @@ -296,8 +320,6 @@ public class PFont implements PConstants { public void resetLeading() { - //leading = size * ((float)mbox / fheight) * 1.2f; - // by trial & error, this seems close to illustrator leading = (ascent() + descent()) * 1.275f; } @@ -318,9 +340,6 @@ public class PFont implements PConstants { } - // supposedly this should be ok even in SCREEN_SPACE mode - // since the applet will set the 'size' of the font to twidth - // (though this prolly breaks any sort of 'height' measurements) public float width(char c) { if (c == 32) return width('i'); @@ -331,28 +350,39 @@ public class PFont implements PConstants { } - public float width(String string) { - //if (!valid) return 0; - float wide = 0; - float pwide = 0; - char previous = 0; - - char s[] = string.toCharArray(); - for (int i = 0; i < s.length; i++) { - if (s[i] == '\n') { - if (wide > pwide) pwide = wide; - wide = 0; - previous = 0; - - } else { - wide += width(s[i]); - if (previous != 0) { - wide += kern(previous, s[i]); - } - previous = s[i]; - } + public float width(String str) { + int length = str.length(); + if (length > widthBuffer.length) { + widthBuffer = new char[length + 10]; } - return (pwide > wide) ? pwide : wide; + str.getChars(0, length, widthBuffer, 0); + + float wide = 0; + //float pwide = 0; + int index = 0; + int start = 0; + + while (index < length) { + if (widthBuffer[index] == '\n') { + wide = Math.max(wide, calcWidth(widthBuffer, start, index)); + start = index+1; + } + index++; + } + //System.out.println(start + " " + length + " " + index); + if (start < length) { + wide = Math.max(wide, calcWidth(widthBuffer, start, index)); + } + return wide; + } + + + private float calcWidth(char buffer[], int start, int stop) { + float wide = 0; + for (int i = start; i < stop; i++) { + wide += width(buffer[i]); + } + return wide; } @@ -377,7 +407,7 @@ public class PFont implements PConstants { cached = true; } - if (parent.text_space == OBJECT_SPACE) { + if (space == OBJECT_SPACE) { float high = (float) height[glyph] / fheight; float bwidth = (float) width[glyph] / fwidth; float lextent = (float) leftExtent[glyph] / fwidth; @@ -476,41 +506,52 @@ public class PFont implements PConstants { } - // used by the text() functions to avoid over-allocation of memory - private char c[] = new char[8192]; - - public void text(String str, float x, float y, PGraphics parent) { text(str, x, y, 0, parent); } public void text(String str, float x, float y, float z, PGraphics parent) { - float startX = x; - int index = 0; - char previous = 0; - int length = str.length(); - if (length > c.length) { - c = new char[length + 10]; + if (length > textBuffer.length) { + textBuffer = new char[length + 10]; } - str.getChars(0, length, c, 0); + str.getChars(0, length, textBuffer, 0); + int start = 0; + int index = 0; while (index < length) { - if (c[index] == '\n') { - x = startX; + if (textBuffer[index] == '\n') { + textLine(start, index, x, y, z, parent); + start = index + 1; y += leading; - //y += ascent(); - previous = 0; - } else { - text(c[index], x, y, z, parent); - x += width(c[index]); - if (previous != 0) - x += kern(previous, c[index]); - previous = c[index]; } index++; } + if (start < length) { + textLine(start, index, x, y, z, parent); + } + } + + + private void textLine(int start, int stop, + float x, float y, float z, + PGraphics parent) { + //float startX = x; + //int index = 0; + //char previous = 0; + + if (align == ALIGN_CENTER) { + x -= calcWidth(textBuffer, start, stop) / 2f; + + } else if (align == ALIGN_RIGHT) { + x -= calcWidth(textBuffer, start, stop); + } + + for (int index = start; index < stop; index++) { + text(textBuffer[index], x, y, z, parent); + x += width(textBuffer[index]); + } } @@ -525,7 +566,8 @@ public class PFont implements PConstants { /** * Draw text in a box that is constrained to a - * particular width and height. + * particular size. The current rectMode() determines + * what the coordinates mean (whether x1y1-x2y2 or x/y/w/h). * * Note that the x,y coords of the start of the box * will align with the *ascent* of the text, @@ -552,7 +594,7 @@ public class PFont implements PConstants { xx = x; yy += leading; //yy += ascent() * 1.2f; - if (yy > h) return; // too big for box + if (yy > y + h) return; // too big for box } text(words[j], xx, yy, z, parent); xx += size + space; @@ -578,23 +620,23 @@ public class PFont implements PConstants { char previous = 0; int length = str.length(); - if (length > c.length) { - c = new char[length + 10]; + if (length > textBuffer.length) { + textBuffer = new char[length + 10]; } - str.getChars(0, length, c, 0); + str.getChars(0, length, textBuffer, 0); while (index < length) { - if (c[index] == '\n') { + if (textBuffer[index] == '\n') { y = startY; x += leading; previous = 0; } else { - ltext(c[index], x, y, parent); - y -= width(c[index]); + ltext(textBuffer[index], x, y, parent); + y -= width(textBuffer[index]); if (previous != 0) - y -= kern(previous, c[index]); - previous = c[index]; + y -= kern(previous, textBuffer[index]); + previous = textBuffer[index]; } index++; } @@ -696,23 +738,23 @@ public class PFont implements PConstants { char previous = 0; int length = str.length(); - if (length > c.length) { - c = new char[length + 10]; + if (length > textBuffer.length) { + textBuffer = new char[length + 10]; } - str.getChars(0, length, c, 0); + str.getChars(0, length, textBuffer, 0); while (index < length) { - if (c[index] == '\n') { + if (textBuffer[index] == '\n') { y = startY; x += leading; previous = 0; } else { - rtext(c[index], x, y, parent); - y += width(c[index]); + rtext(textBuffer[index], x, y, parent); + y += width(textBuffer[index]); if (previous != 0) - y += kern(previous, c[index]); - previous = c[index]; + y += kern(previous, textBuffer[index]); + previous = textBuffer[index]; } index++; } diff --git a/processing/core/PFont2.java b/processing/core/PFont2.java index 51bd97fd9..7080e4a43 100644 --- a/processing/core/PFont2.java +++ b/processing/core/PFont2.java @@ -177,6 +177,7 @@ public class PFont2 extends PFont { height[index] = (maxY - minY) + 1; width[index] = (maxX - minX) + 1; setWidth[index] = metrics.charWidth(c); + //System.out.println((char)c + " " + setWidth[index]); // cache locations of the ascii charset //if (value[i] < 128) ascii[value[i]] = i; diff --git a/processing/core/PGraphics.java b/processing/core/PGraphics.java index e6644a08a..4370042c8 100644 --- a/processing/core/PGraphics.java +++ b/processing/core/PGraphics.java @@ -47,7 +47,7 @@ public class PGraphics extends PImage // specifics for java memoryimagesource DirectColorModel cm; MemoryImageSource mis; - Image image; + public Image image; // ........................................................ @@ -301,9 +301,11 @@ public class PGraphics extends PImage int sphere_detail = 0; float sphereX[], sphereY[], sphereZ[]; - int text_mode; - int text_space; + //int text_mode; + //int text_space; PFont text_font; + + // used by PFont/PGraphics.. forces higher quality texture rendering boolean drawing_text = false; // used by PFont @@ -495,8 +497,8 @@ public class PGraphics extends PImage rect_mode = CORNER; ellipse_mode = CENTER; angle_mode = RADIANS; - text_mode = ALIGN_LEFT; - text_space = OBJECT_SPACE; + //text_mode = ALIGN_LEFT; + //text_space = OBJECT_SPACE; for (int i = 2; i < MAX_LIGHTS; i++) { lightKind[i] = DISABLED; @@ -4189,67 +4191,59 @@ public class PGraphics extends PImage public void textFont(PFont which) { - if (which == null) { - System.err.println("Ignoring improperly loaded font in textFont()"); - return; - } - text_font = which; - if (text_space != SCREEN_SPACE) { + if (which != null) { + text_font = which; text_font.resetSize(); + text_font.resetLeading(); + } else { - //text_font.size(text_font.iwidth); - text_font.size(text_font.mbox); + System.err.println("Ignoring improperly loaded font in textFont()"); } - text_font.resetLeading(); } - public void textFont(PFont which, float size) { - if (which == null) { - System.err.println("Ignoring improperly loaded font in textFont()"); - return; - } - text_font = which; - if (text_space != SCREEN_SPACE) { - text_font.size(size); - } else { - System.err.println("Cannot set size of SCREEN_SPACE fonts"); - //text_font.size(text_font.iwidth); - text_font.size(text_font.mbox); - } - text_font.resetLeading(); - } public void textSize(float size) { - if (text_font == null) { + if (text_font != null) { + text_font.size(size); + + } else { System.err.println("First set a font before setting its size."); - return; } - if (text_space == SCREEN_SPACE) { - System.err.println("Cannot set size of SCREEN_SPACE fonts."); - return; - } - text_font.size(size); } + + public void textFont(PFont which, float size) { + textFont(which); + textSize(size); + } + + public void textLeading(float leading) { - if (text_font == null) { + if (text_font != null) { + text_font.leading(leading); + + } else { System.err.println("First set a font before setting its leading."); - return; } - text_font.leading(leading); } + public void textMode(int mode) { - text_mode = mode; + if (text_font != null) { + text_font.align(mode); + + } else { + System.err.println("First set a font before setting its mode."); + } } - public void textSpace(int space) { - text_space = space; - if ((space == SCREEN_SPACE) && (text_font != null)) { - //text_font.size(text_font.iwidth); - text_font.size(text_font.mbox); - text_font.resetLeading(); + public void textSpace(int space) { + if (text_font != null) { + text_font.space(space); + + } else { + System.err.println("First set a font before setting the space."); } } @@ -4259,17 +4253,12 @@ public class PGraphics extends PImage } public void text(char c, float x, float y, float z) { - if (text_font == null) { - System.err.println("text(): first set a font before drawing text"); - return; - } - if (text_mode == ALIGN_CENTER) { - x -= text_font.width(c) / 2f; + if (text_font != null) { + text_font.text(c, x, y, z, this); - } else if (text_mode == ALIGN_RIGHT) { - x -= text_font.width(c); + } else { + System.err.println("text(): first set a font before drawing text"); } - text_font.text(c, x, y, z, this); } @@ -4278,17 +4267,12 @@ public class PGraphics extends PImage } public void text(String s, float x, float y, float z) { - if (text_font == null) { - System.err.println("text(): first set a font before drawing text"); - return; - } - if (text_mode == ALIGN_CENTER) { - x -= text_font.width(s) / 2f; + if (text_font != null) { + text_font.text(s, x, y, z, this); - } else if (text_mode == ALIGN_RIGHT) { - x -= text_font.width(s); + } else { + System.err.println("text(): first set a font before drawing text"); } - text_font.text(s, x, y, z, this); } @@ -4297,18 +4281,12 @@ public class PGraphics extends PImage } public void text(String s, float x, float y, float z, float w, float h) { - if (text_font == null) { - System.err.println("text(): first set a font before drawing text"); - return; - } - if (text_mode == ALIGN_CENTER) { - x -= text_font.width(s) / 2f; + if (text_font != null) { + text_font.text(s, x, y, z, w, h, this); - } else if (text_mode == ALIGN_RIGHT) { - x -= text_font.width(s); + } else { + System.err.println("text(): first set a font before drawing text"); } - //text_font.text(s, x, y, z, this); - text_font.text(s, x, y, z, w, h, this); } diff --git a/processing/core/PMethods.java b/processing/core/PMethods.java index 9ef8e1379..19230d931 100755 --- a/processing/core/PMethods.java +++ b/processing/core/PMethods.java @@ -169,10 +169,10 @@ public interface PMethods { public void textFont(PFont which); - public void textFont(PFont which, float size); - public void textSize(float size); + public void textFont(PFont which, float size); + public void textLeading(float leading); public void textMode(int mode); diff --git a/processing/core/todo.txt b/processing/core/todo.txt index 24591271f..cd4daceef 100644 --- a/processing/core/todo.txt +++ b/processing/core/todo.txt @@ -4,18 +4,15 @@ X make cursor() commands public X ltext and rtext for screen space stuff X ltext is broken when it goes y < 0 or y > height X ltext & rtext completely working - -_ make sure font creator is making fonts properly fixed width -_ probably not using java charwidth for the char's width - -_ now that it's actually a key, should it be a char? (what's keyChar?) -_ that way println(c) would work a little better.. +X make sure font creator is making fonts properly fixed width +X probably not using java charwidth for the char's width +X probably wasn't using textFont() properly +X now that it's actually a key, should it be a char? (what's keyChar?) +X that way println(c) would work a little better.. +X libraryCalls() not properly working for pre, post, or draw() _ y2 position of rectangles not same as y2 position of lines -_ write PApplet2, a full screen version of PApplet -_ this might be used for presentation mode - text fixes _ make text rect use rectMode for placement _ need to resolve rotated text in SCREEN_SPACE @@ -44,6 +41,9 @@ _ and the illustrator stuff _ 404 error because first searches applet directory on zipdecode _ image(String name) and textFont(String name) +_ write PApplet2, a full screen version of PApplet +_ this might be used for presentation mode + _ before graphics engine change, attach jogl stuff? _ massive graphics engine changes _ explicitly state depth()/nodepth() @@ -52,8 +52,6 @@ _ test with rgb cube, shut off smoothing _ make sure line artifacts are because of smoothing _ implement 2x oversampling for anti-aliasing -_ make PApplet2 that handles full screen 1.4 mode drawing - _ api for file-based renderers _ need to work this out since it will affect other api changes _ size(0, 0) and then ai.size(10000, 20000) diff --git a/processing/todo.txt b/processing/todo.txt index 3dd3c9236..b090be820 100644 --- a/processing/todo.txt +++ b/processing/todo.txt @@ -1,5 +1,6 @@ 0071 o get linux version for 0071 +X suppress "bad file descriptor" error on mac os x _ make notes about preproc _ subclasses need to use "public void keyPressed" not "void keyPressed"