Newer
Older
Import / research / ui / toolkit / src / backup / FontManager.cpp
//
//  FontManager.cpp
//  DescriptionHere
//
//  Created by John Ryland (jryland@xiaofrog.com) on 02/12/2017.
//  Copyright 2017 InvertedLogic. All rights reserved.
//

#include "TrueType.h"

/*
// 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);

    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);
        }
      }
    }
  }

  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;
}