|
| 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 | +} |
0 commit comments