PFont
Data / CompositeDescription
Datatype for storing fonts. Load with loadFont() or create with createFont().
Syntax
PFont font = loadFont("font.ttf")
PFont* font = createFont("Arial", 24)
Parameters
None
Returns
PFontMethods
| PFont::list() | Returns a vector of all available font names |
Related
Under the Hood
From Processing.h:
struct PFont {
static std::vector<std::string> list() {
std::vector<std::string> fonts;
std::vector<std::string> dirs = {"data",".","/usr/share/fonts","/usr/local/share/fonts"};
#ifdef _WIN32
dirs.push_back("C:/Windows/Fonts");
#elif defined(__APPLE__)
dirs.push_back("/Library/Fonts"); dirs.push_back("/System/Library/Fonts");
#endif
if(getenv("HOME")){ dirs.push_back(std::string(getenv("HOME"))+"/.fonts"); }
std::function<void(const std::string&)> scan=[&](const std::string& dir){
#ifndef _WIN32
DIR* d=opendir(dir.c_str()); if(!d) return;
struct dirent* e;
while((e=readdir(d))!=nullptr){
std::string nm=e->d_name; if(nm=="."||nm=="..") continue;
std::string path=dir+"/"+nm;
if(e->d_type==DT_DIR){ scan(path); continue; }
if(nm.size()>4){std::string ext=nm.substr(nm.size()-4);
if(ext==".ttf"||ext==".otf"||ext==".TTF"||ext==".OTF") fonts.push_back(nm);}
} closedir(d);
#else
WIN32_FIND_DATAA fd; HANDLE h2=FindFirstFileA((dir+"\\*").c_str(),&fd);
if(h2==INVALID_HANDLE_VALUE) return;
do { std::string nm=fd.cFileName; if(nm=="."||nm=="..") continue;
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY){scan(dir+"\\"+nm);continue;}
if(nm.size()>4){std::string ext=nm.substr(nm.size()-4);
if(ext==".ttf"||ext==".otf"||ext==".TTF"||ext==".OTF") fonts.push_back(nm);}
} while(FindNextFileA(h2,&fd)); FindClose(h2);
#endif
};
for(auto& d:dirs) scan(d);
std::sort(fonts.begin(),fonts.end());
fonts.erase(std::unique(fonts.begin(),fonts.end()),fonts.end());
return fonts;
}
std::string name;
float size = 12;
bool loaded = false;
PFont() = default;
PFont(const std::string& n, float s) : name(n), size(s), loaded(true) {}
};