1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added some methods for scanning font folders with FreeType.

This commit is contained in:
jules 2013-04-21 20:47:29 +01:00
parent 9387c7fdaf
commit 2d6135a6db
6 changed files with 76 additions and 67 deletions

View file

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

View file

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

View file

@ -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<DirectoryIterator> 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<KnownTypeface> 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)

View file

@ -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();

View file

@ -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()

View file

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