mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-24 01:54:22 +00:00
Added some thread-safety to typeface caching, and optimised default font construction.
This commit is contained in:
parent
62b3adee81
commit
af87a2eda6
2 changed files with 56 additions and 78 deletions
|
|
@ -202,13 +202,11 @@ bool PropertiesFile::loadAsXml()
|
|||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// must be a pretty broken XML file we're trying to parse here,
|
||||
// or a sign that this object needs an InterProcessLock,
|
||||
// or just a failure reading the file. This last reason is why
|
||||
// we don't jassertfalse here.
|
||||
}
|
||||
|
||||
// must be a pretty broken XML file we're trying to parse here,
|
||||
// or a sign that this object needs an InterProcessLock,
|
||||
// or just a failure reading the file. This last reason is why
|
||||
// we don't jassertfalse here.
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
@ -259,10 +257,9 @@ bool PropertiesFile::loadAsBinary()
|
|||
GZIPDecompressorInputStream gzip (subStream);
|
||||
return loadAsBinary (gzip);
|
||||
}
|
||||
else if (magicNumber == PropertyFileConstants::magicNumber)
|
||||
{
|
||||
|
||||
if (magicNumber == PropertyFileConstants::magicNumber)
|
||||
return loadAsBinary (fileStream);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
@ -295,7 +292,7 @@ bool PropertiesFile::saveAsBinary()
|
|||
return false; // locking failure..
|
||||
|
||||
TemporaryFile tempFile (file);
|
||||
ScopedPointer <OutputStream> out (tempFile.getFile().createOutputStream());
|
||||
ScopedPointer<OutputStream> out (tempFile.getFile().createOutputStream());
|
||||
|
||||
if (out != nullptr)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -51,22 +51,28 @@ public:
|
|||
clearSingletonInstance();
|
||||
}
|
||||
|
||||
juce_DeclareSingleton_SingleThreaded_Minimal (TypefaceCache);
|
||||
juce_DeclareSingleton (TypefaceCache, false);
|
||||
|
||||
void setSize (const int numToCache)
|
||||
{
|
||||
const ScopedWriteLock sl (lock);
|
||||
|
||||
faces.clear();
|
||||
faces.insertMultiple (-1, CachedFace(), numToCache);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
const ScopedWriteLock sl (lock);
|
||||
|
||||
setSize (faces.size());
|
||||
defaultFace = nullptr;
|
||||
}
|
||||
|
||||
Typeface::Ptr findTypefaceFor (const Font& font)
|
||||
{
|
||||
const ScopedReadLock slr (lock);
|
||||
|
||||
const String faceName (font.getTypefaceName());
|
||||
const String faceStyle (font.getTypefaceStyle());
|
||||
|
||||
|
|
@ -86,6 +92,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
const ScopedWriteLock slw (lock);
|
||||
int replaceIndex = 0;
|
||||
size_t bestLastUsageCount = std::numeric_limits<size_t>::max();
|
||||
|
||||
|
|
@ -118,10 +125,7 @@ public:
|
|||
return face.typeface;
|
||||
}
|
||||
|
||||
Typeface::Ptr getDefaultTypeface() const noexcept
|
||||
{
|
||||
return defaultFace;
|
||||
}
|
||||
Typeface::Ptr defaultFace;
|
||||
|
||||
private:
|
||||
struct CachedFace
|
||||
|
|
@ -132,20 +136,19 @@ private:
|
|||
// placeholder rather than a real one, e.g. "<Sans-Serif>" vs the actual typeface name.
|
||||
// Since the typeface itself doesn't know that it may have this alias, the name under
|
||||
// which it was fetched needs to be stored separately.
|
||||
String typefaceName;
|
||||
String typefaceStyle;
|
||||
String typefaceName, typefaceStyle;
|
||||
size_t lastUsageCount;
|
||||
Typeface::Ptr typeface;
|
||||
};
|
||||
|
||||
Array <CachedFace> faces;
|
||||
Typeface::Ptr defaultFace;
|
||||
ReadWriteLock lock;
|
||||
Array<CachedFace> faces;
|
||||
size_t counter;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TypefaceCache)
|
||||
};
|
||||
|
||||
juce_ImplementSingleton_SingleThreaded (TypefaceCache)
|
||||
juce_ImplementSingleton (TypefaceCache)
|
||||
|
||||
void Typeface::setTypefaceCacheSize (int numFontsToCache)
|
||||
{
|
||||
|
|
@ -162,68 +165,62 @@ class Font::SharedFontInternal : public ReferenceCountedObject
|
|||
{
|
||||
public:
|
||||
SharedFontInternal() noexcept
|
||||
: typefaceName (Font::getDefaultSansSerifFontName()),
|
||||
: typeface (TypefaceCache::getInstance()->defaultFace),
|
||||
typefaceName (Font::getDefaultSansSerifFontName()),
|
||||
typefaceStyle (Font::getDefaultStyle()),
|
||||
height (FontValues::defaultFontHeight),
|
||||
horizontalScale (1.0f),
|
||||
kerning (0),
|
||||
ascent (0),
|
||||
underline (false),
|
||||
typeface (TypefaceCache::getInstance()->getDefaultTypeface())
|
||||
horizontalScale (1.0f), kerning (0), ascent (0), underline (false)
|
||||
{
|
||||
}
|
||||
|
||||
SharedFontInternal (const String& style, const float fontHeight,
|
||||
const bool isUnderlined) noexcept
|
||||
SharedFontInternal (int styleFlags, float fontHeight) noexcept
|
||||
: typefaceName (Font::getDefaultSansSerifFontName()),
|
||||
typefaceStyle (style),
|
||||
typefaceStyle (FontStyleHelpers::getStyleName (styleFlags)),
|
||||
height (fontHeight),
|
||||
horizontalScale (1.0f),
|
||||
kerning (0),
|
||||
ascent (0),
|
||||
underline (isUnderlined),
|
||||
typeface (nullptr)
|
||||
horizontalScale (1.0f), kerning (0), ascent (0), underline ((styleFlags & underlined) != 0)
|
||||
{
|
||||
if (styleFlags == plain)
|
||||
typeface = TypefaceCache::getInstance()->defaultFace;
|
||||
}
|
||||
|
||||
SharedFontInternal (const String& name, const String& style,
|
||||
const float fontHeight, const bool isUnderlined) noexcept
|
||||
SharedFontInternal (const String& name, int styleFlags, float fontHeight) noexcept
|
||||
: typefaceName (name),
|
||||
typefaceStyle (style),
|
||||
typefaceStyle (FontStyleHelpers::getStyleName (styleFlags)),
|
||||
height (fontHeight),
|
||||
horizontalScale (1.0f),
|
||||
kerning (0),
|
||||
ascent (0),
|
||||
underline (isUnderlined),
|
||||
typeface (nullptr)
|
||||
horizontalScale (1.0f), kerning (0), ascent (0), underline ((styleFlags & underlined) != 0)
|
||||
{
|
||||
if (styleFlags == plain && typefaceName.isEmpty())
|
||||
typeface = TypefaceCache::getInstance()->defaultFace;
|
||||
}
|
||||
|
||||
SharedFontInternal (const String& name, const String& style, float fontHeight) noexcept
|
||||
: typefaceName (name), typefaceStyle (style), height (fontHeight),
|
||||
horizontalScale (1.0f), kerning (0), ascent (0), underline (false)
|
||||
{
|
||||
if (typefaceName.isEmpty())
|
||||
typefaceName = Font::getDefaultSansSerifFontName();
|
||||
}
|
||||
|
||||
explicit SharedFontInternal (const Typeface::Ptr& face) noexcept
|
||||
: typefaceName (face->getName()),
|
||||
: typeface (face),
|
||||
typefaceName (face->getName()),
|
||||
typefaceStyle (face->getStyle()),
|
||||
height (FontValues::defaultFontHeight),
|
||||
horizontalScale (1.0f),
|
||||
kerning (0),
|
||||
ascent (0),
|
||||
underline (false),
|
||||
typeface (face)
|
||||
horizontalScale (1.0f), kerning (0), ascent (0), underline (false)
|
||||
{
|
||||
jassert (typefaceName.isNotEmpty());
|
||||
}
|
||||
|
||||
SharedFontInternal (const SharedFontInternal& other) noexcept
|
||||
: ReferenceCountedObject(),
|
||||
typeface (other.typeface),
|
||||
typefaceName (other.typefaceName),
|
||||
typefaceStyle (other.typefaceStyle),
|
||||
height (other.height),
|
||||
horizontalScale (other.horizontalScale),
|
||||
kerning (other.kerning),
|
||||
ascent (other.ascent),
|
||||
underline (other.underline),
|
||||
typeface (other.typeface)
|
||||
underline (other.underline)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -237,45 +234,29 @@ public:
|
|||
&& typefaceStyle == other.typefaceStyle;
|
||||
}
|
||||
|
||||
Typeface::Ptr typeface;
|
||||
String typefaceName, typefaceStyle;
|
||||
float height, horizontalScale, kerning, ascent;
|
||||
bool underline;
|
||||
Typeface::Ptr typeface;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
Font::Font()
|
||||
: font (new SharedFontInternal())
|
||||
Font::Font() : font (new SharedFontInternal()) {}
|
||||
Font::Font (const Typeface::Ptr& typeface) : font (new SharedFontInternal (typeface)) {}
|
||||
Font::Font (const Font& other) noexcept : font (other.font) {}
|
||||
|
||||
Font::Font (float fontHeight, int styleFlags)
|
||||
: font (new SharedFontInternal (styleFlags, FontValues::limitFontHeight (fontHeight)))
|
||||
{
|
||||
}
|
||||
|
||||
Font::Font (const float fontHeight, const int styleFlags)
|
||||
: font (new SharedFontInternal (FontStyleHelpers::getStyleName (styleFlags),
|
||||
FontValues::limitFontHeight (fontHeight),
|
||||
(styleFlags & underlined) != 0))
|
||||
{
|
||||
}
|
||||
|
||||
Font::Font (const String& typefaceName, const float fontHeight, const int styleFlags)
|
||||
: font (new SharedFontInternal (typefaceName,
|
||||
FontStyleHelpers::getStyleName (styleFlags),
|
||||
FontValues::limitFontHeight (fontHeight),
|
||||
(styleFlags & underlined) != 0))
|
||||
Font::Font (const String& typefaceName, float fontHeight, int styleFlags)
|
||||
: font (new SharedFontInternal (typefaceName, styleFlags, FontValues::limitFontHeight (fontHeight)))
|
||||
{
|
||||
}
|
||||
|
||||
Font::Font (const String& typefaceName, const String& typefaceStyle, float fontHeight)
|
||||
: font (new SharedFontInternal (typefaceName, typefaceStyle, FontValues::limitFontHeight (fontHeight), false))
|
||||
{
|
||||
}
|
||||
|
||||
Font::Font (const Typeface::Ptr& typeface)
|
||||
: font (new SharedFontInternal (typeface))
|
||||
{
|
||||
}
|
||||
|
||||
Font::Font (const Font& other) noexcept
|
||||
: font (other.font)
|
||||
: font (new SharedFontInternal (typefaceName, typefaceStyle, FontValues::limitFontHeight (fontHeight)))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -496,9 +477,9 @@ void Font::setStyleFlags (const int newFlags)
|
|||
if (getStyleFlags() != newFlags)
|
||||
{
|
||||
dupeInternalIfShared();
|
||||
font->typeface = nullptr;
|
||||
font->typefaceStyle = FontStyleHelpers::getStyleName (newFlags);
|
||||
font->underline = (newFlags & underlined) != 0;
|
||||
font->typeface = nullptr;
|
||||
font->ascent = 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue