mirror of
https://github.com/processing/processing4.git
synced 2026-06-08 08:31:28 +02:00
text api work, not necessarily complete
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+111
-69
@@ -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++;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user