Skip to content

Commit 4bfde2d

Browse files
committed
add axis orientation
1 parent b01a1aa commit 4bfde2d

File tree

2 files changed

+185
-3
lines changed

2 files changed

+185
-3
lines changed

core/src/processing/core/PGraphics.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3574,8 +3574,8 @@ public void text(String str, float x1, float y1, float x2, float y2) {
35743574
* Emit a sentence of text, defined as a chunk of text without any newlines.
35753575
* @param stop non-inclusive, the end of the text in question
35763576
*/
3577-
private boolean textSentence(char[] buffer, int start, int stop,
3578-
float boxWidth, float spaceWidth) {
3577+
protected boolean textSentence(char[] buffer, int start, int stop,
3578+
float boxWidth, float spaceWidth) {
35793579
float runningX = 0;
35803580

35813581
// Keep track of this separately from index, since we'll need to back up
@@ -3640,7 +3640,7 @@ private boolean textSentence(char[] buffer, int start, int stop,
36403640
}
36413641

36423642

3643-
private void textSentenceBreak(int start, int stop) {
3643+
protected void textSentenceBreak(int start, int stop) {
36443644
if (textBreakCount == textBreakStart.length) {
36453645
textBreakStart = PApplet.expand(textBreakStart);
36463646
textBreakStop = PApplet.expand(textBreakStop);

core/src/processing/opengl/PGraphicsOpenGL.java

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,188 @@ protected boolean textModeCheck(int mode) {
34593459
// TEXT IMPL
34603460

34613461

3462+
@Override
3463+
public void text(char c, float x, float y) {
3464+
if (textFont == null) {
3465+
defaultFontOrDeath("text");
3466+
}
3467+
3468+
int sign = Y_AXIS_DOWN ? +1 : -1;
3469+
3470+
if (textAlignY == CENTER) {
3471+
y += sign * textAscent() / 2;
3472+
} else if (textAlignY == TOP) {
3473+
y += sign * textAscent();
3474+
} else if (textAlignY == BOTTOM) {
3475+
y -= sign * textDescent();
3476+
//} else if (textAlignY == BASELINE) {
3477+
// do nothing
3478+
}
3479+
3480+
textBuffer[0] = c;
3481+
textLineAlignImpl(textBuffer, 0, 1, x, y);
3482+
}
3483+
3484+
3485+
@Override
3486+
public void text(String str, float x, float y) {
3487+
if (textFont == null) {
3488+
defaultFontOrDeath("text");
3489+
}
3490+
3491+
int sign = Y_AXIS_DOWN ? +1 : -1;
3492+
3493+
int length = str.length();
3494+
if (length > textBuffer.length) {
3495+
textBuffer = new char[length + 10];
3496+
}
3497+
str.getChars(0, length, textBuffer, 0);
3498+
3499+
// If multiple lines, sum the height of the additional lines
3500+
float high = 0; //-textAscent();
3501+
for (int i = 0; i < length; i++) {
3502+
if (textBuffer[i] == '\n') {
3503+
high += sign * textLeading;
3504+
}
3505+
}
3506+
if (textAlignY == CENTER) {
3507+
// for a single line, this adds half the textAscent to y
3508+
// for multiple lines, subtract half the additional height
3509+
//y += (textAscent() - textDescent() - high)/2;
3510+
y += sign * (textAscent() - high)/2;
3511+
} else if (textAlignY == TOP) {
3512+
// for a single line, need to add textAscent to y
3513+
// for multiple lines, no different
3514+
y += sign * textAscent();
3515+
} else if (textAlignY == BOTTOM) {
3516+
// for a single line, this is just offset by the descent
3517+
// for multiple lines, subtract leading for each line
3518+
y -= sign * textDescent() + high;
3519+
}
3520+
3521+
int start = 0;
3522+
int index = 0;
3523+
while (index < length) {
3524+
if (textBuffer[index] == '\n') {
3525+
textLineAlignImpl(textBuffer, start, index, x, y);
3526+
start = index + 1;
3527+
y += sign * textLeading;
3528+
}
3529+
index++;
3530+
}
3531+
if (start < length) {
3532+
textLineAlignImpl(textBuffer, start, index, x, y);
3533+
}
3534+
}
3535+
3536+
3537+
@Override
3538+
public void text(String str, float x1, float y1, float x2, float y2) {
3539+
if (textFont == null) {
3540+
defaultFontOrDeath("text");
3541+
}
3542+
3543+
int sign = Y_AXIS_DOWN ? +1 : -1;
3544+
3545+
float hradius, vradius;
3546+
switch (rectMode) {
3547+
case CORNER:
3548+
x2 += x1; y2 += y1;
3549+
break;
3550+
case RADIUS:
3551+
hradius = x2;
3552+
vradius = y2;
3553+
x2 = x1 + hradius;
3554+
y2 = y1 + vradius;
3555+
x1 -= hradius;
3556+
y1 -= vradius;
3557+
break;
3558+
case CENTER:
3559+
hradius = x2 / 2.0f;
3560+
vradius = y2 / 2.0f;
3561+
x2 = x1 + hradius;
3562+
y2 = y1 + vradius;
3563+
x1 -= hradius;
3564+
y1 -= vradius;
3565+
}
3566+
if (x2 < x1) {
3567+
float temp = x1; x1 = x2; x2 = temp;
3568+
}
3569+
if (y2 < y1) {
3570+
float temp = y1; y1 = y2; y2 = temp;
3571+
}
3572+
3573+
float boxWidth = x2 - x1;
3574+
3575+
float spaceWidth = textWidth(' ');
3576+
3577+
if (textBreakStart == null) {
3578+
textBreakStart = new int[20];
3579+
textBreakStop = new int[20];
3580+
}
3581+
textBreakCount = 0;
3582+
3583+
int length = str.length();
3584+
if (length + 1 > textBuffer.length) {
3585+
textBuffer = new char[length + 1];
3586+
}
3587+
str.getChars(0, length, textBuffer, 0);
3588+
// add a fake newline to simplify calculations
3589+
textBuffer[length++] = '\n';
3590+
3591+
int sentenceStart = 0;
3592+
for (int i = 0; i < length; i++) {
3593+
if (textBuffer[i] == '\n') {
3594+
boolean legit =
3595+
textSentence(textBuffer, sentenceStart, i, boxWidth, spaceWidth);
3596+
if (!legit) break;
3597+
sentenceStart = i + 1;
3598+
}
3599+
}
3600+
3601+
// lineX is the position where the text starts, which is adjusted
3602+
// to left/center/right based on the current textAlign
3603+
float lineX = x1; //boxX1;
3604+
if (textAlign == CENTER) {
3605+
lineX = lineX + boxWidth/2f;
3606+
} else if (textAlign == RIGHT) {
3607+
lineX = x2; //boxX2;
3608+
}
3609+
3610+
float boxHeight = y2 - y1;
3611+
// incorporate textAscent() for the top (baseline will be y1 + ascent)
3612+
// and textDescent() for the bottom, so that lower parts of letters aren't
3613+
// outside the box. [0151]
3614+
float topAndBottom = textAscent() + textDescent();
3615+
int lineFitCount = 1 + PApplet.floor((boxHeight - topAndBottom) / textLeading);
3616+
int lineCount = Math.min(textBreakCount, lineFitCount);
3617+
3618+
if (textAlignY == CENTER) {
3619+
lineX = 0;
3620+
float lineHigh = textAscent() + textLeading * (lineCount - 1);
3621+
float y = Y_AXIS_DOWN ? y1 + textAscent() + (boxHeight - lineHigh) / 2 : y2 - textAscent() - (boxHeight - lineHigh) / 2;
3622+
for (int i = 0; i < lineCount; i++) {
3623+
textLineAlignImpl(textBuffer, textBreakStart[i], textBreakStop[i], lineX, y);
3624+
y += sign * textLeading;
3625+
}
3626+
3627+
} else if (textAlignY == BOTTOM) {
3628+
float y = Y_AXIS_DOWN ? y2 - textDescent() - textLeading * (lineCount - 1) : y1 + textDescent() + textLeading * (lineCount - 1);
3629+
for (int i = 0; i < lineCount; i++) {
3630+
textLineAlignImpl(textBuffer, textBreakStart[i], textBreakStop[i], lineX, y);
3631+
y += sign * textLeading;
3632+
}
3633+
3634+
} else { // TOP or BASELINE just go to the default
3635+
float y = Y_AXIS_DOWN ? y1 + textAscent() : y2 - textAscent();
3636+
for (int i = 0; i < lineCount; i++) {
3637+
textLineAlignImpl(textBuffer, textBreakStart[i], textBreakStop[i], lineX, y);
3638+
y += sign * textLeading;
3639+
}
3640+
}
3641+
}
3642+
3643+
34623644
@Override
34633645
public float textAscent() {
34643646
if (textFont == null) defaultFontOrDeath("textAscent");

0 commit comments

Comments
 (0)