|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * SPDX-FileCopyrightText: Copyright 2025 Fcitx5 for Android Contributors |
| 4 | + */ |
| 5 | +package org.fcitx.fcitx5.android.data.fonts |
| 6 | + |
| 7 | +import android.app.AlertDialog |
| 8 | +import android.content.Context |
| 9 | +import androidx.annotation.StringRes |
| 10 | +import org.fcitx.fcitx5.android.R |
| 11 | +import org.fcitx.fcitx5.android.data.prefs.AppPrefs |
| 12 | +import org.fcitx.fcitx5.android.input.AutoScaleTextView |
| 13 | +import org.fcitx.fcitx5.android.utils.appContext |
| 14 | +import org.json.JSONObject |
| 15 | +import java.io.File |
| 16 | + |
| 17 | +object FontManager { |
| 18 | + |
| 19 | + private val fontsDir = File(appContext.getExternalFilesDir(null), "fonts") |
| 20 | + private val fontConfigFile = File(fontsDir, "fontset.json") |
| 21 | + |
| 22 | + init { |
| 23 | + if (!fontsDir.exists()) { |
| 24 | + fontsDir.mkdirs() |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * 获取可用字体列表 |
| 30 | + */ |
| 31 | + fun getAvailableFonts(): List<FontInfo> { |
| 32 | + val fonts = mutableListOf<FontInfo>() |
| 33 | + |
| 34 | + // 添加系统默认字体 |
| 35 | + fonts.add(FontInfo("", "System Default")) |
| 36 | + |
| 37 | + // 扫描 fonts 目录 |
| 38 | + if (fontsDir.exists()) { |
| 39 | + fontsDir.listFiles { file -> |
| 40 | + file.isFile && (file.extension.lowercase() in listOf("ttf", "otf")) |
| 41 | + }?.forEach { file -> |
| 42 | + fonts.add(FontInfo(file.name, file.nameWithoutExtension)) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return fonts.sortedBy { it.displayName } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 显示字体选择对话框 |
| 51 | + */ |
| 52 | + fun showFontPickerDialog( |
| 53 | + context: Context, |
| 54 | + @StringRes titleRes: Int, |
| 55 | + onFontSelected: (String) -> Unit |
| 56 | + ) { |
| 57 | + val fonts = getAvailableFonts() |
| 58 | + val fontNames = fonts.map { it.displayName }.toTypedArray() |
| 59 | + |
| 60 | + AlertDialog.Builder(context) |
| 61 | + .setTitle(titleRes) |
| 62 | + .setItems(fontNames) { _, which -> |
| 63 | + val selectedFont = fonts[which].fileName |
| 64 | + onFontSelected(selectedFont) |
| 65 | + } |
| 66 | + .setNegativeButton(android.R.string.cancel, null) |
| 67 | + .show() |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 更新字体配置文件 |
| 72 | + */ |
| 73 | + fun updateFontConfiguration() { |
| 74 | + try { |
| 75 | + val fontPrefs = AppPrefs.getInstance().fonts |
| 76 | + val config = JSONObject().apply { |
| 77 | + val candFont = fontPrefs.candFont.getValue() |
| 78 | + val preeditFont = fontPrefs.preeditFont.getValue() |
| 79 | + val popupKeyFont = fontPrefs.popupKeyFont.getValue() |
| 80 | + val keyMainFont = fontPrefs.keyMainFont.getValue() |
| 81 | + val keyAltFont = fontPrefs.keyAltFont.getValue() |
| 82 | + val defaultFont = fontPrefs.defaultFont.getValue() |
| 83 | + |
| 84 | + put("cand_font", candFont) |
| 85 | + put("font", defaultFont) |
| 86 | + put("preedit_font", preeditFont) |
| 87 | + put("popup_key_font", popupKeyFont) |
| 88 | + put("key_main_font", keyMainFont) |
| 89 | + put("key_alt_font", keyAltFont) |
| 90 | + } |
| 91 | + |
| 92 | + if (!fontsDir.exists()) { |
| 93 | + fontsDir.mkdirs() |
| 94 | + } |
| 95 | + |
| 96 | + fontConfigFile.writeText(config.toString(2)) |
| 97 | + |
| 98 | + // 清除字体缓存,触发重新加载 |
| 99 | + AutoScaleTextView.clearFontCache() |
| 100 | + } catch (e: Exception) { |
| 101 | + // 记录错误但不崩溃 |
| 102 | + e.printStackTrace() |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * 字体信息 |
| 108 | + */ |
| 109 | + data class FontInfo( |
| 110 | + val fileName: String, |
| 111 | + val displayName: String |
| 112 | + ) |
| 113 | +} |
0 commit comments