From 2f523cfae7f464904bf93c7cbcfdf3701c89de77 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 26 Apr 2017 11:14:16 +0100 Subject: [PATCH] Added /usr/share/fonts as an extra font search-path on linux --- .../juce_graphics/native/juce_linux_Fonts.cpp | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/modules/juce_graphics/native/juce_linux_Fonts.cpp b/modules/juce_graphics/native/juce_linux_Fonts.cpp index 39dd384441..872f8fe1cf 100644 --- a/modules/juce_graphics/native/juce_linux_Fonts.cpp +++ b/modules/juce_graphics/native/juce_linux_Fonts.cpp @@ -22,6 +22,18 @@ ============================================================================== */ +static XmlElement* findFontsConfFile() +{ + static const char* pathsToSearch[] = { "/etc/fonts/fonts.conf", + "/usr/share/fonts/fonts.conf" }; + + for (auto* path : pathsToSearch) + if (auto* xml = XmlDocument::parse (File (path))) + return xml; + + return nullptr; +} + StringArray FTTypefaceList::getDefaultFontDirectories() { StringArray fontDirs; @@ -29,21 +41,19 @@ StringArray FTTypefaceList::getDefaultFontDirectories() fontDirs.addTokens (String (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH"))), ";,", ""); fontDirs.removeEmptyStrings (true); - if (fontDirs.size() == 0) + if (fontDirs.isEmpty()) { - const ScopedPointer fontsInfo (XmlDocument::parse (File ("/etc/fonts/fonts.conf"))); - - if (fontsInfo != nullptr) + if (ScopedPointer fontsInfo = findFontsConfFile()) { forEachXmlChildElementWithTagName (*fontsInfo, e, "dir") { - String fontPath (e->getAllSubText().trim()); + auto fontPath = e->getAllSubText().trim(); if (fontPath.isNotEmpty()) { if (e->getStringAttribute ("prefix") == "xdg") { - String xdgDataHome (SystemStats::getEnvironmentVariable ("XDG_DATA_HOME", String())); + auto xdgDataHome = SystemStats::getEnvironmentVariable ("XDG_DATA_HOME", {}); if (xdgDataHome.trimStart().isEmpty()) xdgDataHome = "~/.local/share"; @@ -57,7 +67,7 @@ StringArray FTTypefaceList::getDefaultFontDirectories() } } - if (fontDirs.size() == 0) + if (fontDirs.isEmpty()) fontDirs.add ("/usr/X11R6/lib/X11/fonts"); fontDirs.removeDuplicates (false); @@ -120,19 +130,19 @@ private: { const StringArray choices (choicesArray); - for (int j = 0; j < choices.size(); ++j) - if (names.contains (choices[j], true)) - return choices[j]; + for (auto& choice : choices) + if (names.contains (choice, true)) + return choice; - for (int j = 0; j < choices.size(); ++j) - for (int i = 0; i < names.size(); ++i) - if (names[i].startsWithIgnoreCase (choices[j])) - return names[i]; + for (auto& choice : choices) + for (auto& name : names) + if (name.startsWithIgnoreCase (choice)) + return name; - for (int j = 0; j < choices.size(); ++j) - for (int i = 0; i < names.size(); ++i) - if (names[i].containsIgnoreCase (choices[j])) - return names[i]; + for (auto& choice : choices) + for (auto& name : names) + if (name.containsIgnoreCase (choice)) + return name; return names[0]; }