//
// FontManager.cpp
// DescriptionHere
//
// Created by John Ryland (jryland@xiaofrog.com) on 02/12/2017.
// Copyright 2017 InvertedLogic. All rights reserved.
//
//#include "TrueType.h"
#include "Font.h"
#include "Common.h"
BEGIN_NAMESPACE
class TtfFontList
{
public:
bool loaded = false;
// std::vector<Font> l;
Vector<String> fonts;
void LoadAll(void);
private:
void GetFontList();
void ScanFontDirectory(const char* dir);
};
/*
// FileSystem class
class Directory
{
public:
Directory(const char* a_dirName);
};
*/
#ifdef _WIN32
void TtfFontList::ScanDirectory(const char* dir)
{
}
void TtfFontList::GetFontList()
{
WIN32_FIND_DATA wfd;
char dir[MAX_PATH];
GetWindowsDirectory(dir, MAX_PATH - 30);
strcat(dir, "\\fonts\\*.ttf");
HANDLE h = FindFirstFile(dir, &wfd);
while(h != INVALID_HANDLE_VALUE) {
//TtfFont tf;
//ZERO(&tf);
char fullPath[MAX_PATH];
GetWindowsDirectory(fullPath, MAX_PATH - (30 + (UINT)strlen(wfd.cFileName)));
strcat(fullPath, "\\fonts\\");
strcat(fullPath, wfd.cFileName);
//strcpy(tf.fontFile, fullPath);
//l.Add(&tf);
fonts.Add(tf.fontFile);
if(!FindNextFile(h, &wfd)) break;
}
}
#else
#include <sys/types.h>
#include <sys/dir.h>
void TtfFontList::ScanFontDirectory(const char *dir)
{
DIR* d = opendir(dir);
if (!d)
return;
dirent* ent;
while ((ent = readdir(d)) != nullptr)
{
std::string path = dir;
path += "/";
path += ent->d_name;
if (ent->d_type == DT_DIR && ent->d_name[0] != '.')
{
ScanFontDirectory(path.c_str());
}
else
{
if (path.size() > 5) {
std::string ext = path.c_str() + path.size() - 4;
if (ext == ".TTF" || ext == ".ttf")
{
//TtfFont tf;
//tf.fontFile = path;
//l.push_back(tf);
fonts.push_back(path);
}
}
}
}
closedir(d);
}
void TtfFontList::GetFontList()
{
#ifdef __APPLE__
ScanFontDirectory("/System/Library/Fonts");
ScanFontDirectory("/Library/Fonts");
#else
ScanFontDirectory("/usr/lib/X11/fonts");
ScanFontDirectory("/usr/openwin/lib/X11/fonts/TrueType");
ScanFontDirectory("/usr/share/fonts/truetype");
#endif
}
#endif
//-----------------------------------------------------------------------------
// Get the list of available font filenames, and load the name for each of
// them. Only that, though, not the glyphs too.
//-----------------------------------------------------------------------------
/*
void TtfFontList::LoadAll(void) {
if(loaded) return;
// Get the list of font files from the platform-specific code.
GetFontList();
size_t i;
for(i = 0; i < l.size(); i++) {
TtfFont *tf = &(l[i]);
tf->LoadFontFromFile(true);
}
loaded = true;
}
*/
/*
bool TtfFontList::PlotString(char *font, char *str, double spacing, GlyphOutline& outline)
{
// TODO: not require loading all the fonts
LoadAll();
size_t i;
for(i = 0; i < l.size(); i++) {
TtfFont *tf = &(l[i]);
// TODO: use a dict/map to lookup from font name
//printf("DEBUG: %s:%i : Testing font %s\n", __FILE__, __LINE__, tf->FontFileBaseName());
if (tf->FontFileBaseName() == font) {
//printf("DEBUG: %s:%i : Found font %s\n", __FILE__, __LINE__, tf->FontFileBaseName());
tf->LoadFontFromFile(false);
tf->PlotString(str, spacing, outline);//, sbl, origin, u, v);
return true;
}
}
return false;
}
*/
namespace {
void AppendFontsFromDirectory(const char *dir, Vector<String>& fonts)
{
DIR* d = opendir(dir);
if (!d)
return;
dirent* ent;
while ((ent = readdir(d)) != nullptr)
{
std::string path = dir;
path += "/";
path += ent->d_name;
if (ent->d_type == DT_DIR && ent->d_name[0] != '.')
{
AppendFontsFromDirectory(path.c_str(), fonts);
}
else
{
if (path.size() > 5)
{
std::string ext = path.c_str() + path.size() - 4;
if (ext == ".TTF" || ext == ".ttf")
{
fonts.push_back(path);
}
}
}
}
closedir(d);
}
}
FontManager::FontManager()
{
}
FontManager::~FontManager()
{
}
Vector<String> FontManager::getAvailableFontNames()
{
Vector<String> fonts;
#ifdef __APPLE__
AppendFontsFromDirectory("/System/Library/Fonts", fonts);
// AppendFontsFromDirectory("/System/Library/Fonts/Supplemental", fonts);
AppendFontsFromDirectory("/Library/Fonts", fonts);
#else
AppendFontsFromDirectory("/usr/lib/X11/fonts", fonts);
AppendFontsFromDirectory("/usr/openwin/lib/X11/fonts/TrueType", fonts);
AppendFontsFromDirectory("/usr/share/fonts/truetype", fonts);
#endif
return fonts;
}
ManagedFont& FontManager::getFont(String fontName)
{
return m_loadedFonts[fontName];
}
END_NAMESPACE