diff --git a/modules/juce_graphics/fonts/juce_Typeface.h b/modules/juce_graphics/fonts/juce_Typeface.h index 9a9411b56f..fb694e54ae 100644 --- a/modules/juce_graphics/fonts/juce_Typeface.h +++ b/modules/juce_graphics/fonts/juce_Typeface.h @@ -129,6 +129,11 @@ public: /** Clears any fonts that are currently cached in memory. */ static void clearTypefaceCache(); + /** On some platforms, this allows a specific path to be scanned. + Currently only available when using FreeType. + */ + static void scanFolderForFonts (const File& folder); + protected: //============================================================================== String name, style; diff --git a/modules/juce_graphics/native/juce_android_Fonts.cpp b/modules/juce_graphics/native/juce_android_Fonts.cpp index bc4aa1cc6a..8dd1022d3e 100644 --- a/modules/juce_graphics/native/juce_android_Fonts.cpp +++ b/modules/juce_graphics/native/juce_android_Fonts.cpp @@ -57,9 +57,9 @@ Typeface::Ptr Font::getDefaultTypefaceForFont (const Font& font) //============================================================================== #if JUCE_USE_FREETYPE -void FontFileIterator::findFontDirectories() +StringArray FTTypefaceList::getDefaultFontDirectories() { - fontDirs.add ("/system/fonts"); + return StringArray ("/system/fonts"); } Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) @@ -67,6 +67,11 @@ Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) return new FreeTypeTypeface (font); } +void Typeface::scanFolderForFonts (const File& folder) +{ + FTTypefaceList::getInstance()->scanFontPaths (StringArray (folder.getFullPathName())); +} + StringArray Font::findAllTypefaceNames() { return FTTypefaceList::getInstance()->findAllFamilyNames(); @@ -311,6 +316,11 @@ Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) return new AndroidTypeface (font); } +void Typeface::scanFolderForFonts (const File&) +{ + jassertfalse; // not available unless using FreeType +} + bool TextLayout::createNativeLayout (const AttributedString&) { return false; diff --git a/modules/juce_graphics/native/juce_freetype_Fonts.cpp b/modules/juce_graphics/native/juce_freetype_Fonts.cpp index 80e59f2b61..50be6aff1a 100644 --- a/modules/juce_graphics/native/juce_freetype_Fonts.cpp +++ b/modules/juce_graphics/native/juce_freetype_Fonts.cpp @@ -71,75 +71,13 @@ struct FTFaceWrapper : public ReferenceCountedObject JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FTFaceWrapper) }; -//============================================================================== -class FontFileIterator -{ -public: - FontFileIterator() : index (0) - { - findFontDirectories(); - } - - bool next() - { - if (iter != nullptr) - { - while (iter->next()) - if (getFile().hasFileExtension ("ttf;pfb;pcf;otf")) - return true; - } - - if (index >= fontDirs.size()) - return false; - - iter = new DirectoryIterator (File::getCurrentWorkingDirectory() - .getChildFile (fontDirs [index++]), true); - return next(); - } - - File getFile() const { jassert (iter != nullptr); return iter->getFile(); } - -private: - StringArray fontDirs; - int index; - ScopedPointer iter; - - void findFontDirectories(); - - JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FontFileIterator) -}; - //============================================================================== class FTTypefaceList : private DeletedAtShutdown { public: - FTTypefaceList() - : library (new FTLibWrapper()) + FTTypefaceList() : library (new FTLibWrapper()) { - FontFileIterator fontFileIterator; - - while (fontFileIterator.next()) - { - int faceIndex = 0; - int numFaces = 0; - - do - { - FTFaceWrapper face (library, fontFileIterator.getFile(), faceIndex); - - if (face.face != 0) - { - if (faceIndex == 0) - numFaces = face.face->num_faces; - - if ((face.face->face_flags & FT_FACE_FLAG_SCALABLE) != 0) - faces.add (new KnownTypeface (fontFileIterator.getFile(), faceIndex, face)); - } - - ++faceIndex; - } - while (faceIndex < numFaces); - } + scanFontPaths (getDefaultFontDirectories()); } ~FTTypefaceList() @@ -217,6 +155,19 @@ public: return s; } + void scanFontPaths (const StringArray& paths) + { + for (int i = 0; i < paths.size(); ++i) + { + DirectoryIterator iter (File::getCurrentWorkingDirectory() + .getChildFile (paths[i]), true); + + while (iter.next()) + if (iter.getFile().hasFileExtension ("ttf;pfb;pcf;otf")) + scanFont (iter.getFile()); + } + } + void getMonospacedNames (StringArray& monoSpaced) const { for (int i = 0; i < faces.size(); i++) @@ -244,6 +195,31 @@ private: FTLibWrapper::Ptr library; OwnedArray faces; + static StringArray getDefaultFontDirectories(); + + void scanFont (const File& file) + { + int faceIndex = 0; + int numFaces = 0; + + do + { + FTFaceWrapper face (library, file, faceIndex); + + if (face.face != 0) + { + if (faceIndex == 0) + numFaces = face.face->num_faces; + + if ((face.face->face_flags & FT_FACE_FLAG_SCALABLE) != 0) + faces.add (new KnownTypeface (file, faceIndex, face)); + } + + ++faceIndex; + } + while (faceIndex < numFaces); + } + const KnownTypeface* matchTypeface (const String& familyName, const String& style) const noexcept { for (int i = 0; i < faces.size(); ++i) diff --git a/modules/juce_graphics/native/juce_linux_Fonts.cpp b/modules/juce_graphics/native/juce_linux_Fonts.cpp index 42cb765360..a0716c58c2 100644 --- a/modules/juce_graphics/native/juce_linux_Fonts.cpp +++ b/modules/juce_graphics/native/juce_linux_Fonts.cpp @@ -23,8 +23,10 @@ ============================================================================== */ -void FontFileIterator::findFontDirectories() +StringArray FTTypefaceList::getDefaultFontDirectories() { + StringArray fontDirs; + fontDirs.addTokens (CharPointer_UTF8 (getenv ("JUCE_FONT_PATH")), ";,", String::empty); fontDirs.removeEmptyStrings (true); @@ -60,6 +62,7 @@ void FontFileIterator::findFontDirectories() fontDirs.add ("/usr/X11R6/lib/X11/fonts"); fontDirs.removeDuplicates (false); + return fontDirs; } Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) @@ -67,6 +70,11 @@ Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) return new FreeTypeTypeface (font); } +void Typeface::scanFolderForFonts (const File& folder) +{ + FTTypefaceList::getInstance()->scanFontPaths (StringArray (folder.getFullPathName())); +} + StringArray Font::findAllTypefaceNames() { return FTTypefaceList::getInstance()->findAllFamilyNames(); diff --git a/modules/juce_graphics/native/juce_mac_Fonts.mm b/modules/juce_graphics/native/juce_mac_Fonts.mm index c3913b4806..1cbff8ea09 100644 --- a/modules/juce_graphics/native/juce_mac_Fonts.mm +++ b/modules/juce_graphics/native/juce_mac_Fonts.mm @@ -1134,6 +1134,11 @@ Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) return new OSXTypeface (font); } +void Typeface::scanFolderForFonts (const File&) +{ + jassertfalse; // not implemented on this platform +} + struct DefaultFontNames { DefaultFontNames() diff --git a/modules/juce_graphics/native/juce_win32_Fonts.cpp b/modules/juce_graphics/native/juce_win32_Fonts.cpp index ef6f773f55..6a540ef732 100644 --- a/modules/juce_graphics/native/juce_win32_Fonts.cpp +++ b/modules/juce_graphics/native/juce_win32_Fonts.cpp @@ -499,3 +499,8 @@ Typeface::Ptr Typeface::createSystemTypefaceFor (const Font& font) return new WindowsTypeface (font); } + +void Typeface::scanFolderForFonts (const File&) +{ + jassertfalse; // not implemented on this platform +}