text()
Typography / Loading & DisplayingDescription
Draws text to the screen. With a bounding box (x, y, w, h), text wraps automatically.
Syntax
void text(std::string str, float x, float y)
void text(std::string str, float x, float y, float w, float h)
Parameters
| Name | Type | Description |
|---|---|---|
| str | std::string | the string to display |
| x | float | x-coordinate |
| y | float | y-coordinate |
| w | float | width of text box (optional) |
| h | float | height of text box (optional) |
Returns
voidRelated
Under the Hood
From Processing.h:
void text(const std::string& s, float x, float y);
void text(float,float,float);
void text(const std::string&,float,float);
void text(const std::string&,float,float,float,float);
inline void text(const std::string& s,A x,B y){ text(s,(float)x,(float)y);
inline void text(const char* s,A x,B y){ text(std::string(s),(float)x,(float)y);
inline void text(const std::string& s,A x,B y,C w,D h2){ text(s,(float)x,(float)y,(float)w,(float)h2);
inline void text(const char* s,A x,B y,C w,D h2){ text(std::string(s),(float)x,(float)y,(float)w,(float)h2);
inline void text(V val,A x,B y){ text((float)val,(float)x,(float)y);
template<typename A,typename B>
inline void text(char c,A x,B y){ text(std::string(1,c),(float)x,(float)y); }
template<typename A,typename B>
inline void text(char c,A x,B y){ text(std::string(1,c),(float)x,(float)y);
template<typename A,typename B,typename C,typename D>
inline void text(char c,A x,B y,C w,D h2){ text(std::string(1,c),(float)x,(float)y,(float)w,(float)h2); }
template<typename A,typename B,typename C,typename D>
inline void text(char c,A x,B y,C w,D h2){ text(std::string(1,c),(float)x,(float)y,(float)w,(float)h2);
void text(const std::string& msg, float x, float y);
void text(const std::string& msg, float x, float y, float w, float h);
void text(int val, float x, float y);
void text(float val, float x, float y);
void text(char c, float x, float y) { text(std::string(1,c), x, y); }
void text(char c, float x, float y) { text(std::string(1,c), x, y);
void text(char c, float x, float y, float w, float h) { text(std::string(1,c), x, y, w, h); }
void text(char c, float x, float y, float w, float h) { text(std::string(1,c), x, y, w, h);
template<typename X,typename Y,typename=std::enable_if_t<std::is_arithmetic_v<X>&&std::is_arithmetic_v<Y>>>
void text(char c, X x, Y y) { text(std::string(1,c),(float)x,(float)y);
template<typename X,typename Y,typename=std::enable_if_t<std::is_arithmetic_v<X>&&std::is_arithmetic_v<Y>>>
void text(const std::string& s,X x,Y y){ text(s,(float)x,(float)y);
template<typename X,typename Y,typename=std::enable_if_t<std::is_arithmetic_v<X>&&std::is_arithmetic_v<Y>>>
void text(int v,X x,Y y){ text(v,(float)x,(float)y);
template<typename X,typename Y,typename=std::enable_if_t<std::is_arithmetic_v<X>&&std::is_arithmetic_v<Y>>>
void text(float v,X x,Y y){ text(v,(float)x,(float)y);
inline void text(float v,float x,float y){ if(PApplet::g_papplet) PApplet::g_papplet->text(v,x,y); }
inline void text(const std::string& s,float x,float y){ if(PApplet::g_papplet) PApplet::g_papplet->text(s,x,y); }
inline void text(const std::string& s,float x,float y,float w,float h){ if(PApplet::g_papplet) PApplet::g_papplet->text(s,x,y,w,h); }
inline void PGraphics::text(const std::string& s, float x, float y) { if(PApplet::g_papplet) PApplet::g_papplet->text(s,x,y); }
Under the Hood
From Processing.cpp:
void PApplet::text(const std::string& msg, float x, float y) { renderText(msg, x, y); }
void PApplet::text(int v, float x, float y) { renderText(std::to_string(v), x, y); }
void PApplet::text(float v, float x, float y) {
std::ostringstream ss; ss << v; renderText(ss.str(), x, y);
}
void PApplet::text(const std::string& msg, float x, float y, float w, float h) {
if (msg.empty() || w <= 0) return;
float leading = (g_textLeading > 0) ? g_textLeading : g_textSize * 1.4f;
float maxY = (h > 0) ? y + h : 1e9f;
// Split message into tokens: words and explicit newlines
std::vector<std::string> tokens;
{
std::string cur;
for (char c : msg) {
if (c == '\n') {
if (!cur.empty()) { tokens.push_back(cur); cur.clear(); }
tokens.push_back("\n");
} else if (c == ' ') {
if (!cur.empty()) { tokens.push_back(cur); cur.clear(); }
} else {
cur += c;
}
}
if (!cur.empty()) tokens.push_back(cur);
}
// Layout: accumulate words into lines, wrap when line width exceeds w
float cy = y;
std::string line;
auto flushLine = [&](){
if (!line.empty() && cy + g_textSize <= maxY) {
renderText(line, x, cy);
}
line.clear();
cy += leading;
};
for (auto& tok : tokens) {
if (tok == "\n") {
// Explicit newline: flush current line and advance
if (cy + g_textSize <= maxY) renderText(line, x, cy);
line.clear();
cy += leading;
continue;
}
// Try adding this word to the current line
std::string candidate = line.empty() ? tok : line + " " + tok;
float cw = getLineWidth(candidate);
if (!line.empty() && cw > w) {
// Word doesn't fit: flush current line, start new line with this word
if (cy + g_textSize <= maxY) renderText(line, x, cy);
line.clear();
cy += leading;
// If the word itself is wider than w, wrap it character by character
if (getLineWidth(tok) > w) {
std::string charLine;
for (char c : tok) {
std::string test2 = charLine + c;
if (!charLine.empty() && getLineWidth(test2) > w) {
if (cy + g_textSize <= maxY) renderText(charLine, x, cy);
charLine.clear();
cy += leading;
}
charLine += c;
}
line = charLine;
} else {
line = tok;
}
} else if (line.empty() && cw > w) {
// First word on line is too wide: wrap char by char
std::string charLine;
for (char c : tok) {
std::string test2 = charLine + c;
if (!charLine.empty() && getLineWidth(test2) > w) {
if (cy + g_textSize <= maxY) renderText(charLine, x, cy);
charLine.clear();
cy += leading;
}
charLine += c;
}
line = charLine;
} else {
line = candidate;
}
}
// Flush remaining text
if (!line.empty() && cy + g_textSize <= maxY) {
renderText(line, x, cy);
}
}
std::string text;