Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 65f9874

Browse files
Chun-Heng Taichunhtai
authored andcommitted
Add windows font change logic
1 parent c8428ff commit 65f9874

22 files changed

+160
-4
lines changed

shell/common/shell.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,4 +1365,16 @@ fml::Status Shell::WaitForFirstFrame(fml::TimeDelta timeout) {
13651365
}
13661366
}
13671367

1368+
bool Shell::ReloadSystemFonts() {
1369+
FML_DCHECK(is_setup_);
1370+
FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
1371+
1372+
if (!engine_) {
1373+
return false;
1374+
}
1375+
engine_->GetFontCollection().GetFontCollection()->SetupDefaultFontManager();
1376+
engine_->GetFontCollection().GetFontCollection()->ClearFontFamilyCache();
1377+
return true;
1378+
}
1379+
13681380
} // namespace flutter

shell/common/shell.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ class Shell final : public PlatformView::Delegate,
285285
///
286286
fml::Status WaitForFirstFrame(fml::TimeDelta timeout);
287287

288+
//----------------------------------------------------------------------------
289+
/// @brief Used by embedders to reload the system fonts in FontCollection.
290+
/// It will also clear the cached font families.
291+
///
292+
/// @return Returns if shell reloads system fonts successfully.
293+
///
294+
bool ReloadSystemFonts();
295+
288296
//----------------------------------------------------------------------------
289297
/// @brief Used by embedders to get the last error from the Dart UI
290298
/// Isolate, if one exists.

shell/common/shell_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ bool ShellTest::GetNeedsReportTimings(Shell* shell) {
154154
return shell->needs_report_timings_;
155155
}
156156

157+
std::shared_ptr<txt::FontCollection> ShellTest::GetFontCollection(Shell* shell) {
158+
return shell->weak_engine_->GetFontCollection().GetFontCollection();
159+
}
160+
157161
Settings ShellTest::CreateSettingsForFixture() {
158162
Settings settings;
159163
settings.leak_vm = false;

shell/common/shell_test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class ShellTest : public ThreadTest {
5252
static bool GetNeedsReportTimings(Shell* shell);
5353
static void SetNeedsReportTimings(Shell* shell, bool value);
5454

55+
std::shared_ptr<txt::FontCollection> GetFontCollection(Shell* shell);
56+
5557
// Do not assert |UnreportedTimingsCount| to be positive in any tests.
5658
// Otherwise those tests will be flaky as the clearing of unreported timings
5759
// is unpredictive.

shell/common/shell_unittests.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,30 @@ TEST_F(ShellTest, ReportTimingsIsCalledImmediatelyAfterTheFirstFrame) {
519519
ASSERT_EQ(timestamps.size(), FrameTiming::kCount);
520520
}
521521

522+
TEST_F(ShellTest, ReloadSystemFonts) {
523+
auto settings = CreateSettingsForFixture();
524+
525+
fml::MessageLoop::EnsureInitializedForCurrentThread();
526+
auto task_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
527+
TaskRunners task_runners("test", task_runner, task_runner, task_runner,
528+
task_runner);
529+
auto shell = CreateShell(std::move(settings), std::move(task_runners));
530+
531+
auto fontCollection = GetFontCollection(shell.get());
532+
std::vector<std::string> families(1 , "Robotofake");
533+
auto font = fontCollection->GetMinikinFontCollectionForFamilies(families, "en");
534+
ASSERT_EQ(font->getId(), (unsigned int)0);
535+
// Result should be cached.
536+
font = fontCollection->GetMinikinFontCollectionForFamilies(families, "en");
537+
ASSERT_EQ(font->getId(), (unsigned int)0);
538+
bool result = shell->ReloadSystemFonts();
539+
540+
// Cache is cleared, and FontCollection will be assigned a new id in incremental order.
541+
font = fontCollection->GetMinikinFontCollectionForFamilies(families, "en");
542+
ASSERT_EQ(font->getId(), (unsigned int)1);
543+
ASSERT_TRUE(result);
544+
}
545+
522546
TEST_F(ShellTest, WaitForFirstFrame) {
523547
auto settings = CreateSettingsForFixture();
524548
std::unique_ptr<Shell> shell = CreateShell(settings);

shell/platform/embedder/embedder.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,21 @@ FlutterEngineResult FlutterEngineOnVsync(FLUTTER_API_SYMBOL(FlutterEngine)
12721272
return kSuccess;
12731273
}
12741274

1275+
FlutterEngineResult FlutterEngineReloadSystemFonts(
1276+
FLUTTER_API_SYMBOL(FlutterEngine) engine) {
1277+
if (engine == nullptr) {
1278+
return LOG_EMBEDDER_ERROR(kInvalidArguments);
1279+
}
1280+
1281+
TRACE_EVENT0("flutter", "FlutterEngineReloadSystemFonts");
1282+
1283+
if (!reinterpret_cast<flutter::EmbedderEngine*>(engine)->ReloadSystemFonts()) {
1284+
return LOG_EMBEDDER_ERROR(kInternalInconsistency);
1285+
}
1286+
1287+
return kSuccess;
1288+
}
1289+
12751290
void FlutterEngineTraceEventDurationBegin(const char* name) {
12761291
fml::tracing::TraceEvent0("flutter", name);
12771292
}

shell/platform/embedder/embedder.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,17 @@ FlutterEngineResult FlutterEngineOnVsync(FLUTTER_API_SYMBOL(FlutterEngine)
11921192
uint64_t frame_start_time_nanos,
11931193
uint64_t frame_target_time_nanos);
11941194

1195+
//------------------------------------------------------------------------------
1196+
/// @brief Reloads the system fonts in engine.
1197+
///
1198+
/// @param[in] engine. A running engine instance.
1199+
///
1200+
/// @return The result of the call.
1201+
///
1202+
FLUTTER_EXPORT
1203+
FlutterEngineResult FlutterEngineReloadSystemFonts(
1204+
FLUTTER_API_SYMBOL(FlutterEngine) engine);
1205+
11951206
//------------------------------------------------------------------------------
11961207
/// @brief A profiling utility. Logs a trace duration begin event to the
11971208
/// timeline. If the timeline is unavailable or disabled, this has

shell/platform/embedder/embedder_engine.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ bool EmbedderEngine::OnVsyncEvent(intptr_t baton,
186186
frame_target_time);
187187
}
188188

189+
bool EmbedderEngine::ReloadSystemFonts() {
190+
return shell_->ReloadSystemFonts();
191+
}
192+
189193
bool EmbedderEngine::PostRenderThreadTask(fml::closure task) {
190194
if (!IsValid()) {
191195
return false;

shell/platform/embedder/embedder_engine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class EmbedderEngine {
6767
fml::TimePoint frame_start_time,
6868
fml::TimePoint frame_target_time);
6969

70+
bool ReloadSystemFonts();
71+
7072
bool PostRenderThreadTask(fml::closure task);
7173

7274
bool RunTask(const FlutterTask* task);

shell/platform/embedder/tests/embedder_unittests.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ TEST(EmbedderTestNoFixture, CanGetCurrentTimeInNanoseconds) {
228228
ASSERT_LT((point2 - point1), fml::TimeDelta::FromMilliseconds(1));
229229
}
230230

231+
TEST_F(EmbedderTest, CanReloadSystemFonts) {
232+
auto& context = GetEmbedderContext();
233+
EmbedderConfigBuilder builder(context);
234+
auto engine = builder.LaunchEngine();
235+
ASSERT_TRUE(engine.is_valid());
236+
237+
auto result = FlutterEngineReloadSystemFonts(engine.get());
238+
ASSERT_EQ(result, kSuccess);
239+
}
240+
231241
TEST_F(EmbedderTest, CanCreateOpenGLRenderingEngine) {
232242
EmbedderConfigBuilder builder(GetEmbedderContext());
233243
builder.SetOpenGLRendererConfig();

0 commit comments

Comments
 (0)