createFont()
Typography / Loading & DisplayingDescription
Creates a PFont from a system font name and size.
Syntax
PFont* createFont(std::string name, float size, bool smooth)
Parameters
| Name | Type | Description |
|---|---|---|
| name | std::string | font name |
| size | float | font size |
| smooth | bool | whether to anti-alias (default true) |
Returns
PFont*Related
Under the Hood
From Processing.h:
PFont* createFont(const std::string& name, float size, bool smooth=true);
Under the Hood
From Processing.cpp:
PFont* PApplet::createFont(const std::string& name, float size, bool /*smooth*/) {
PFont f(name, size);
// Try as file path first, then common system paths
// Strip extension from name if already present, to avoid double extensions
std::string baseName = name;
if (baseName.size() > 4 && (baseName.substr(baseName.size()-4) == ".ttf"
|| baseName.substr(baseName.size()-4) == ".otf")) {
// name already has extension -- use as-is for first attempt
} else {
baseName = ""; // will build paths with extensions below
}
std::string nameNoExt = name;
if (nameNoExt.size() > 4 && (nameNoExt.substr(nameNoExt.size()-4) == ".ttf"
|| nameNoExt.substr(nameNoExt.size()-4) == ".otf"))
nameNoExt = nameNoExt.substr(0, nameNoExt.size()-4);
std::vector<std::string> paths = {
// Try exact name first (may already have extension)
name,
// Try adding extensions if not already present
nameNoExt + ".ttf",
nameNoExt + ".otf",
// Sketch data/ folder
std::string("data/") + name,
std::string("data/") + nameNoExt + ".ttf",
std::string("data/") + nameNoExt + ".otf",
// Sketch fonts/ folder
std::string("fonts/") + name,
std::string("fonts/") + nameNoExt + ".ttf",
std::string("fonts/") + nameNoExt + ".otf",
// Linux system font directories
std::string("/usr/share/fonts/truetype/") + nameNoExt + ".ttf",
std::string("/usr/share/fonts/opentype/") + nameNoExt + ".otf",
std::string("/usr/share/fonts/TTF/") + nameNoExt + ".ttf",
std::string("/usr/share/fonts/OTF/") + nameNoExt + ".otf",
// Windows system fonts
std::string("C:/Windows/Fonts/") + name,
std::string("C:/Windows/Fonts/") + nameNoExt + ".ttf",
std::string("C:/Windows/Fonts/") + nameNoExt + ".otf",
// macOS
std::string("/Library/Fonts/") + name,
std::string("/System/Library/Fonts/") + name,
};
// Also search system font dirs recursively (Linux: fc-list output)
#ifndef _WIN32
{
FILE* fc = popen(("fc-list : file | grep -i '" + nameNoExt + "' | head -5").c_str(), "r");
if (fc) {
char buf[512];
while (fgets(buf, sizeof(buf), fc)) {
std::string line(buf);
// fc-list format: /path/to/font.ttf: Family:style
size_t colon = line.find(':');
if (colon != std::string::npos) {
std::string fpath = line.substr(0, colon);
// strip whitespace
fpath.erase(0, fpath.find_first_not_of(" \t"));
fpath.erase(fpath.find_last_not_of(" \t\n\r") + 1);
if (!fpath.empty()) paths.push_back(fpath);
}
}
pclose(fc);
}
}
#endif
bool found = false;
for (auto& p : paths) {
if (tryLoadTTF(p, size)) { found = true; break; }
}
if (!found) {
std::cerr << "[font] Could not find font: " << name << "\n";
std::cerr << "[font] Put the .ttf file in your sketch's data/ folder\n";
}
_fontPool.push_back(std::move(f)); return &_fontPool.back();
}