Don't hold all existing system fonts open.

On Windows, this exhausts file descriptors and everything (e.g.
opening and saving files) breaks.
pull/4/head
whitequark 2016-05-07 04:20:06 +00:00
parent a21a327a97
commit 7da5cfbaae
2 changed files with 11 additions and 2 deletions

View File

@ -88,6 +88,7 @@ void TtfFontList::PlotString(const std::string &font, const std::string &str,
[&](const TtfFont &tf) { return tf.FontFileBaseName() == font; }); [&](const TtfFont &tf) { return tf.FontFileBaseName() == font; });
if(!str.empty() && tf != &l.elem[l.n]) { if(!str.empty() && tf != &l.elem[l.n]) {
tf->LoadFromFile(fontLibrary, /*nameOnly=*/false);
tf->PlotString(str, sbl, origin, u, v); tf->PlotString(str, sbl, origin, u, v);
} else { } else {
// No text or no font; so draw a big X for an error marker. // No text or no font; so draw a big X for an error marker.
@ -116,7 +117,7 @@ std::string TtfFont::FontFileBaseName() const {
// the letter shapes, and about the mappings that determine which glyph goes // the letter shapes, and about the mappings that determine which glyph goes
// with which character. // with which character.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool TtfFont::LoadFromFile(FT_Library fontLibrary) { bool TtfFont::LoadFromFile(FT_Library fontLibrary, bool nameOnly) {
FT_Open_Args args = {}; FT_Open_Args args = {};
args.flags = FT_OPEN_PATHNAME; args.flags = FT_OPEN_PATHNAME;
args.pathname = &fontFile[0]; // FT_String is char* for historical reasons args.pathname = &fontFile[0]; // FT_String is char* for historical reasons
@ -139,6 +140,12 @@ bool TtfFont::LoadFromFile(FT_Library fontLibrary) {
name = std::string(fontFace->family_name) + name = std::string(fontFace->family_name) +
" (" + std::string(fontFace->style_name) + ")"; " (" + std::string(fontFace->style_name) + ")";
if(nameOnly) {
FT_Done_Face(fontFace);
fontFace = NULL;
}
return true; return true;
} }
@ -211,6 +218,8 @@ static const FT_Outline_Funcs outline_funcs = {
void TtfFont::PlotString(const std::string &str, void TtfFont::PlotString(const std::string &str,
SBezierList *sbl, Vector origin, Vector u, Vector v) SBezierList *sbl, Vector origin, Vector u, Vector v)
{ {
if(fontFace == NULL) oops();
FT_Pos dx = 0; FT_Pos dx = 0;
for(char32_t chr : ReadUTF8(str)) { for(char32_t chr : ReadUTF8(str)) {
uint32_t gid = FT_Get_Char_Index(fontFace, chr); uint32_t gid = FT_Get_Char_Index(fontFace, chr);

View File

@ -16,7 +16,7 @@ public:
FT_FaceRec_ *fontFace; FT_FaceRec_ *fontFace;
std::string FontFileBaseName() const; std::string FontFileBaseName() const;
bool LoadFromFile(FT_LibraryRec_ *fontLibrary); bool LoadFromFile(FT_LibraryRec_ *fontLibrary, bool nameOnly = true);
void PlotString(const std::string &str, void PlotString(const std::string &str,
SBezierList *sbl, Vector origin, Vector u, Vector v); SBezierList *sbl, Vector origin, Vector u, Vector v);