Skip to content

Commit ed594f5

Browse files
committed
textAlign: implement vertical alignment (TOP/BOTTOM/CENTER/BASELINE)
1 parent 71d0e21 commit ed594f5

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/Processing.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,13 +2292,17 @@ void PApplet::renderText(const ::std::string& msg, float x, float y) {
22922292
for (char c : msg) { if(c=='\n'){ls.push_back(cur);cur.clear();}else cur+=c; }
22932293
ls.push_back(cur);
22942294

2295+
float totalH = (float)ls.size() * leading;
2296+
float yOff = 0;
2297+
if (g_textAlignY == TOP_ALIGN) yOff = g_textSize;
2298+
else if (g_textAlignY == CENTER_ALIGN) yOff = g_textSize * 0.5f - totalH * 0.5f + leading * 0.5f;
2299+
else if (g_textAlignY == BOTTOM_ALIGN) yOff = -totalH + leading;
22952300
for (int li = 0; li < (int)ls.size(); li++) {
22962301
float lw = getLineWidth(ls[li]);
22972302
float dx = x;
22982303
if (g_textAlignX == RIGHT_ALIGN) dx = x - lw;
22992304
else if (g_textAlignX == CENTER_ALIGN) dx = x - lw * 0.5f;
2300-
2301-
float dy = y + li * leading;
2305+
float dy = y + yOff + li * leading;
23022306

23032307
#if PROCESSING_HAS_STB_TRUETYPE
23042308
if (g_ttf.loaded) { drawTTFStr(dx, dy, ls[li]); continue; }

0 commit comments

Comments
 (0)