Skip to content

Commit c9391fb

Browse files
Bia503rustdeskCopilot
authored
fix(arm64-linux): fix CJK font rendering on flutter-elinux (#15324)
* fix(arm64-linux): fix CJK font rendering on flutter-elinux The flutter-elinux engine used for ARM64 Linux builds is compiled without --enable-fontconfig, so Flutter's text shaper cannot discover system fonts. This causes CJK characters to render as tofu boxes even when fonts such as Noto Sans CJK are installed. See flutter/flutter#139293. Fix by loading a CJK font at startup via FontLoader (bypassing fontconfig) and propagating it through two paths so all text widgets are covered: 1. MyTheme.applyFontFallback() — updates textTheme on both light and dark ThemeData so Material components receive the fallback through the theme. 2. _mergeCjkFallback() in GetMaterialApp builders — wraps child widgets in DefaultTextStyle.merge so bare Text() widgets and those with inherit:true also render CJK characters correctly. Font discovery queries fc-list for zh, ja, and ko separately, preferring fonts present in all three sets (true pan-CJK fonts such as NotoSansCJK or SourceHanSans) over Chinese-only fonts that may lack Japanese kana or Korean hangul glyphs. Falls back to a hardcoded search-path list covering Debian/Ubuntu, Fedora/RHEL, Arch Linux, and WenQuanYi font layouts. This is an app-level workaround. The engine-level fix is tracked at flutter/flutter#180235 (open as of 2026-06). Fixes #10666 Signed-off-by: Bia503 <[email protected]> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <[email protected]> --------- Signed-off-by: Bia503 <[email protected]> Co-authored-by: RustDesk <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]>
1 parent 8a95588 commit c9391fb

4 files changed

Lines changed: 155 additions & 0 deletions

File tree

‎flutter/lib/common.dart‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,22 @@ class MyTheme {
598598
}
599599
}
600600

601+
/// Applies [fallbacks] as fontFamilyFallback to every text style in both
602+
/// themes. Called once at startup on ARM64 Linux after a CJK font has been
603+
/// loaded via FontLoader (see flutter/flutter#139293).
604+
static void applyFontFallback(List<String> fallbacks) {
605+
lightTheme = lightTheme.copyWith(
606+
textTheme: lightTheme.textTheme.apply(fontFamilyFallback: fallbacks),
607+
primaryTextTheme:
608+
lightTheme.primaryTextTheme.apply(fontFamilyFallback: fallbacks),
609+
);
610+
darkTheme = darkTheme.copyWith(
611+
textTheme: darkTheme.textTheme.apply(fontFamilyFallback: fallbacks),
612+
primaryTextTheme:
613+
darkTheme.primaryTextTheme.apply(fontFamilyFallback: fallbacks),
614+
);
615+
}
616+
601617
static ThemeMode currentThemeMode() {
602618
final preference = getThemeModePreference();
603619
if (preference == ThemeMode.system) {

‎flutter/lib/main.dart‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import 'mobile/pages/home_page.dart';
2929
import 'mobile/pages/server_page.dart';
3030
import 'mobile/widgets/deploy_dialog.dart';
3131
import 'models/platform_model.dart';
32+
import 'native/font_manager.dart'
33+
if (dart.library.html) 'web/font_manager.dart';
3234

3335
import 'package:flutter_hbb/plugin/handlers.dart'
3436
if (dart.library.html) 'package:flutter_hbb/web/plugin/handlers.dart';
@@ -37,10 +39,15 @@ import 'package:flutter_hbb/plugin/handlers.dart'
3739
int? kWindowId;
3840
WindowType? kWindowType;
3941
late List<String> kBootArgs;
42+
bool _cjkFontLoaded = false;
4043

4144
Future<void> main(List<String> args) async {
4245
earlyAssert();
4346
WidgetsFlutterBinding.ensureInitialized();
47+
_cjkFontLoaded = await loadSystemCJKFonts();
48+
if (_cjkFontLoaded) {
49+
MyTheme.applyFontFallback([kLinuxCjkFontFamily]);
50+
}
4451

4552
debugPrint("launch args: $args");
4653
kBootArgs = List.from(args);
@@ -383,6 +390,7 @@ void _runApp(
383390
builder: (context, child) {
384391
child = _keepScaleBuilder(context, child);
385392
child = botToastBuilder(context, child);
393+
if (_cjkFontLoaded) child = _mergeCjkFallback(context, child);
386394
return child;
387395
},
388396
),
@@ -533,6 +541,7 @@ class _AppState extends State<App> with WidgetsBindingObserver {
533541
: (context, child) {
534542
child = _keepScaleBuilder(context, child);
535543
child = botToastBuilder(context, child);
544+
if (_cjkFontLoaded) child = _mergeCjkFallback(context, child);
536545
if ((isDesktop && desktopType == DesktopType.main) ||
537546
isWebDesktop) {
538547
child = keyListenerBuilder(context, child);
@@ -586,6 +595,19 @@ _registerEventHandler() {
586595
}
587596
}
588597

598+
/// Merges the theme's fontFamilyFallback into [DefaultTextStyle] so that
599+
/// bare [Text] widgets (and those with inherit:true styles) also pick up the
600+
/// CJK fallback font loaded on ARM64 Linux.
601+
Widget _mergeCjkFallback(BuildContext context, Widget? child) {
602+
final result = child ?? Container();
603+
final fallback = Theme.of(context).textTheme.bodyMedium?.fontFamilyFallback;
604+
if (fallback == null || fallback.isEmpty) return result;
605+
return DefaultTextStyle.merge(
606+
style: TextStyle(fontFamilyFallback: fallback),
607+
child: result,
608+
);
609+
}
610+
589611
Widget keyListenerBuilder(BuildContext context, Widget? child) {
590612
return RawKeyboardListener(
591613
focusNode: FocusNode(),
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import 'dart:ffi' show Abi;
2+
import 'dart:io';
3+
import 'dart:typed_data';
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/services.dart';
7+
8+
/// Font family name registered with [FontLoader] when a system CJK font is
9+
/// successfully loaded on ARM64 Linux.
10+
const kLinuxCjkFontFamily = 'SystemCJK';
11+
12+
const _kFontSearchPaths = [
13+
// Debian / Ubuntu (noto-fonts / fonts-noto-cjk)
14+
'/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',
15+
'/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc',
16+
'/usr/share/fonts/opentype/noto/NotoSansCJKsc-Regular.otf',
17+
// Fedora / RHEL / Rocky (google-noto-sans-cjk-fonts)
18+
'/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc',
19+
'/usr/share/fonts/google-noto-sans-cjk-fonts/NotoSansCJK-Regular.ttc',
20+
// Arch Linux (noto-fonts-cjk)
21+
'/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc',
22+
'/usr/share/fonts/noto-cjk/NotoSansCJKsc-Regular.otf',
23+
// Generic fallback paths
24+
'/usr/share/fonts/noto/NotoSansCJK-Regular.ttc',
25+
'/usr/share/fonts/noto/NotoSansCJKsc-Regular.otf',
26+
// WenQuanYi — commonly pre-installed on CJK-locale systems
27+
'/usr/share/fonts/truetype/wqy/wqy-microhei.ttc',
28+
'/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc',
29+
'/usr/share/fonts/wqy-microhei/wqy-microhei.ttc',
30+
'/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc',
31+
];
32+
33+
/// Loads a system CJK font on ARM64 Linux into Flutter's font registry via
34+
/// [FontLoader], working around the missing fontconfig support in the
35+
/// flutter-elinux engine (https://github.com/flutter/flutter/issues/139293).
36+
///
37+
/// Returns true if a CJK font was successfully loaded; false otherwise.
38+
/// On all other platforms this is a no-op and returns false immediately.
39+
Future<bool> loadSystemCJKFonts() async {
40+
if (Abi.current() != Abi.linuxArm64) return false;
41+
42+
final path = await _findCjkFontPath();
43+
if (path == null) {
44+
debugPrint('ARM64 Linux: no CJK font found; CJK text may not render');
45+
return false;
46+
}
47+
48+
try {
49+
final loader = FontLoader(kLinuxCjkFontFamily);
50+
final bytes = await File(path).readAsBytes();
51+
loader.addFont(Future.value(ByteData.view(bytes.buffer, bytes.offsetInBytes, bytes.lengthInBytes)));
52+
await loader.load();
53+
debugPrint('ARM64 Linux: loaded CJK font from $path');
54+
return true;
55+
} catch (e) {
56+
debugPrint('ARM64 Linux: failed to load CJK font: $e');
57+
return false;
58+
}
59+
}
60+
61+
Future<String?> _findCjkFontPath() async {
62+
// Query fc-list for each CJK script separately. Fonts present in all three
63+
// sets (zh ∩ ja ∩ ko) are true pan-CJK fonts; prefer them so we don't
64+
// accidentally pick a Chinese-only font that lacks Japanese kana or Korean
65+
// hangul glyphs. fc-list is a fontconfig CLI tool available on most Linux
66+
// systems independent of whether the Flutter engine was built with fontconfig.
67+
final byLang = <String, Set<String>>{};
68+
for (final lang in const ['zh', 'ja', 'ko']) {
69+
final paths = <String>{};
70+
try {
71+
final r =
72+
await Process.run('fc-list', [':lang=$lang', '--format=%{file}\n']);
73+
if (r.exitCode == 0) {
74+
for (final line in r.stdout.toString().split('\n')) {
75+
final p = line.trim();
76+
if (p.isNotEmpty && File(p).existsSync()) paths.add(p);
77+
}
78+
}
79+
} catch (e) {
80+
debugPrint('ARM64 Linux: fc-list failed for lang=$lang: $e');
81+
}
82+
byLang[lang] = paths;
83+
}
84+
85+
final panCjk = byLang['zh']!
86+
.intersection(byLang['ja']!)
87+
.intersection(byLang['ko']!);
88+
final anyCjk =
89+
byLang.values.fold(<String>{}, (acc, s) => acc..addAll(s));
90+
91+
// Among candidates, prefer well-known pan-CJK font families.
92+
String? pick(Iterable<String> pool) {
93+
const preferred = ['notosanscjk', 'sourcehansans', 'sourcehanserif'];
94+
for (final name in preferred) {
95+
for (final p in pool) {
96+
if (p.toLowerCase().contains(name)) return p;
97+
}
98+
}
99+
return pool.isNotEmpty ? pool.first : null;
100+
}
101+
102+
final found = pick(panCjk) ?? pick(anyCjk);
103+
if (found != null) return found;
104+
105+
for (final p in _kFontSearchPaths) {
106+
if (File(p).existsSync()) return p;
107+
}
108+
return null;
109+
}

‎flutter/lib/web/font_manager.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Web stub for `native/font_manager.dart`.
2+
///
3+
/// The native implementation depends on `dart:io` (Process/File/Platform) to
4+
/// load a system CJK font on ARM64 Linux, which cannot compile for the web
5+
/// target. The web build has no such fontconfig limitation, so this is a no-op.
6+
const kLinuxCjkFontFamily = 'SystemCJK';
7+
8+
Future<bool> loadSystemCJKFonts() async => false;

0 commit comments

Comments
 (0)