1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-11-21 16:37:12 +01:00

Mutable cache for Ruler's chosen fonts

This commit is contained in:
Paul Licameli
2020-01-23 11:11:54 -05:00
parent 21306d6186
commit 99064d6dd5
2 changed files with 50 additions and 25 deletions

View File

@@ -109,8 +109,6 @@ Ruler::Ruler()
fontSize = 8; fontSize = 8;
#endif #endif
mUserFonts = false;
mLength = 0; mLength = 0;
mValid = false; mValid = false;
@@ -275,17 +273,16 @@ void FindFontHeights(
void Ruler::SetFonts(const wxFont &minorFont, const wxFont &majorFont, const wxFont &minorMinorFont) void Ruler::SetFonts(const wxFont &minorFont, const wxFont &majorFont, const wxFont &minorMinorFont)
{ {
mFonts.minorMinor = minorMinorFont;
mFonts.minor = minorFont;
mFonts.major = majorFont;
// Won't override these fonts // Won't override these fonts
mUserFonts = true;
mpUserFonts = std::make_unique<Fonts>(
Fonts{ majorFont, minorFont, minorMinorFont, 0 } );
wxScreenDC dc; wxScreenDC dc;
wxCoord height; wxCoord height;
FindFontHeights( height, mFonts.lead, dc, majorFont.GetPointSize() ); FindFontHeights( height, mpUserFonts->lead, dc, majorFont.GetPointSize() );
mpFonts.reset();
Invalidate(); Invalidate();
} }
@@ -869,7 +866,7 @@ struct Ruler::Updater {
const bool mFlip = mRuler.mFlip; const bool mFlip = mRuler.mFlip;
const bool mCustom = mRuler.mCustom; const bool mCustom = mRuler.mCustom;
const Fonts mFonts = mRuler.mFonts; const Fonts &mFonts = *mRuler.mpFonts;
const bool mLog = mRuler.mLog; const bool mLog = mRuler.mLog;
const double mHiddenMin = mRuler.mHiddenMin; const double mHiddenMin = mRuler.mHiddenMin;
const double mHiddenMax = mRuler.mHiddenMax; const double mHiddenMax = mRuler.mHiddenMax;
@@ -891,7 +888,9 @@ struct Ruler::Updater {
TickOutputs outputs TickOutputs outputs
) const; ) const;
static void ChooseFonts( Fonts &fonts, wxDC &dc, int desiredPixelHeight ); static void ChooseFonts(
std::unique_ptr<Fonts> &pFonts, const Fonts *pUserFonts,
wxDC &dc, int desiredPixelHeight );
struct UpdateOutputs; struct UpdateOutputs;
@@ -992,8 +991,21 @@ static constexpr int MaxPixelHeight =
#endif #endif
; ;
void Ruler::Updater::ChooseFonts( Fonts &fonts, wxDC &dc, int desiredPixelHeight ) void Ruler::Updater::ChooseFonts(
std::unique_ptr<Fonts> &pFonts, const Fonts *pUserFonts,
wxDC &dc, int desiredPixelHeight )
{ {
if ( pFonts )
return;
if ( pUserFonts ) {
pFonts = std::make_unique<Fonts>( *pUserFonts );
return;
}
pFonts = std::make_unique<Fonts>( Fonts{ {}, {}, {}, 0 } );
auto &fonts = *pFonts;
int fontSize = 4; int fontSize = 4;
desiredPixelHeight = desiredPixelHeight =
@@ -1300,6 +1312,15 @@ void Ruler::Updater::Update(
update( label ); update( label );
} }
void Ruler::ChooseFonts( wxDC &dc ) const
{
Updater::ChooseFonts( mpFonts, mpUserFonts.get(), dc,
mOrientation == wxHORIZONTAL
? mBottom - mTop - 5 // height less ticks and 1px gap
: MaxPixelHeight
);
}
void Ruler::Update( void Ruler::Update(
wxDC &dc, const Envelope* envelope )// Envelope *speedEnv, long minSpeed, long maxSpeed ) wxDC &dc, const Envelope* envelope )// Envelope *speedEnv, long minSpeed, long maxSpeed )
{ {
@@ -1311,13 +1332,7 @@ void Ruler::Update(
// (i.e. we've been invalidated). Recompute all // (i.e. we've been invalidated). Recompute all
// tick positions and font size. // tick positions and font size.
if (!mUserFonts) { ChooseFonts( dc );
Updater::ChooseFonts( mFonts, dc,
mOrientation == wxHORIZONTAL
? mBottom - mTop - 5 // height less ticks and 1px gap
: MaxPixelHeight
);
}
// If ruler is being resized, we could end up with it being too small. // If ruler is being resized, we could end up with it being too small.
// Values of mLength of zero or below cause bad array allocations and // Values of mLength of zero or below cause bad array allocations and
@@ -1356,6 +1371,16 @@ void Ruler::Update(
mValid = true; mValid = true;
} }
auto Ruler::GetFonts() const -> Fonts
{
if ( !mpFonts ) {
wxScreenDC dc;
ChooseFonts( dc );
}
return *mpFonts;
}
void Ruler::Draw(wxDC& dc) void Ruler::Draw(wxDC& dc)
{ {
Draw( dc, NULL); Draw( dc, NULL);
@@ -1398,7 +1423,7 @@ void Ruler::Draw(wxDC& dc, const Envelope* envelope)
} }
} }
dc.SetFont(mFonts.major); dc.SetFont( mpFonts->major );
// We may want to not show the ticks at the extremes, // We may want to not show the ticks at the extremes,
// though still showing the labels. // though still showing the labels.
@@ -1436,12 +1461,12 @@ void Ruler::Draw(wxDC& dc, const Envelope* envelope)
drawLabel( label, 4 ); drawLabel( label, 4 );
if( mbMinor ) { if( mbMinor ) {
dc.SetFont(mFonts.minor); dc.SetFont( mpFonts->minor );
for( const auto &label : mMinorLabels ) for( const auto &label : mMinorLabels )
drawLabel( label, 2 ); drawLabel( label, 2 );
} }
dc.SetFont(mFonts.minorMinor); dc.SetFont( mpFonts->minorMinor );
for( const auto &label : mMinorMinorLabels ) for( const auto &label : mMinorMinorLabels )
if ( !label.text.empty() ) if ( !label.text.empty() )

View File

@@ -102,8 +102,7 @@ class AUDACITY_DLL_API Ruler {
wxFont major, minor, minorMinor; wxFont major, minor, minorMinor;
int lead; int lead;
}; };
Fonts GetFonts() const Fonts GetFonts() const;
{ return mFonts; }
void SetNumberScale(const NumberScale &scale); void SetNumberScale(const NumberScale &scale);
@@ -169,6 +168,7 @@ class AUDACITY_DLL_API Ruler {
using Bits = std::vector< bool >; using Bits = std::vector< bool >;
void ChooseFonts( wxDC &dc ) const;
void Update( wxDC &dc, const Envelope* envelope ); void Update( wxDC &dc, const Envelope* envelope );
struct Updater; struct Updater;
@@ -185,8 +185,8 @@ private:
int mLeft, mTop, mRight, mBottom; int mLeft, mTop, mRight, mBottom;
int mLength; int mLength;
Fonts mFonts; std::unique_ptr<Fonts> mpUserFonts;
bool mUserFonts; mutable std::unique_ptr<Fonts> mpFonts;
double mMin, mMax; double mMin, mMax;
double mHiddenMin, mHiddenMax; double mHiddenMin, mHiddenMax;