From 1ce998f37f46a1b22a37f5fb95ca337968a7139a Mon Sep 17 00:00:00 2001 From: tsingliu <410869548@qq.com> Date: Sat, 7 Feb 2026 23:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=AD=E6=96=87=E5=92=8Cem?= =?UTF-8?q?oji=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools/screenshot.ts | 47 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/tools/screenshot.ts b/src/tools/screenshot.ts index 8e4a25d..e297268 100644 --- a/src/tools/screenshot.ts +++ b/src/tools/screenshot.ts @@ -40,6 +40,43 @@ const checkLinuxFonts = () => { } }; +const installFonts = (missing: { cjk: boolean, emoji: boolean }) => { + const child_process = require('child_process'); + try { + let installCmd = ''; + if (fs.existsSync('/etc/alpine-release')) { + // Alpine + const pkgs = []; + if (!missing.cjk) pkgs.push('font-noto-cjk'); + if (!missing.emoji) pkgs.push('font-noto-emoji'); + if (pkgs.length > 0) { + installCmd = `apk add --no-cache ${pkgs.join(' ')}`; + } + } else if (fs.existsSync('/etc/debian_version')) { + // Debian/Ubuntu + const pkgs = []; + if (!missing.cjk) pkgs.push('fonts-noto-cjk', 'fonts-wqy-zenhei'); + if (!missing.emoji) pkgs.push('fonts-noto-color-emoji'); + if (pkgs.length > 0) { + // apt-get update is often needed first in clean containers + installCmd = `apt-get update && apt-get install -y ${pkgs.join(' ')}`; + } + } + + if (installCmd) { + console.log(`Creating font environment... (${installCmd})`); + console.log("This may take a few moments..."); + child_process.execSync(installCmd, { stdio: 'inherit' }); + console.log('✅ Fonts installed successfully.'); + return true; + } + } catch (e: any) { + console.warn(`⚠️ Failed to auto-install fonts: ${e.message}`); + console.warn('Please install them manually to fix "tofu" characters.'); + } + return false; +}; + export const ScreenshotTool: ToolModule = { name: "Screenshot Tool", configKeys: [], @@ -72,13 +109,9 @@ export const ScreenshotTool: ToolModule = { // Check for fonts on Linux to prevent "tofu" characters if (os.platform() === 'linux') { const fonts = checkLinuxFonts(); - if (!fonts.cjk) { - console.warn("⚠️ Warning: No CJK fonts detected. Chinese characters may appear as squares (tofu)."); - console.warn(" Run 'apk add font-noto-cjk' (Alpine) or 'apt-get install fonts-noto-cjk' (Debian/Ubuntu)."); - } - if (!fonts.emoji) { - console.warn("⚠️ Warning: No Emoji fonts detected. Emojis may appear as squares."); - console.warn(" Run 'apk add font-noto-emoji' (Alpine) or 'apt-get install fonts-noto-color-emoji' (Debian/Ubuntu)."); + if (!fonts.cjk || !fonts.emoji) { + console.log("Missing fonts detected. Attempting to fix environment..."); + installFonts(fonts); } }