From 325aa536c47b7bccd80752d1abd17f44d22d99bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 23 Nov 2022 19:57:55 +0800 Subject: [PATCH] Font: improve performance (Windows) --- CMakeLists.txt | 2 +- src/detection/font/font.h | 2 +- src/detection/font/font_windows.c | 22 ++++++++++++++++++++ src/detection/font/font_windows.cpp | 32 ----------------------------- src/modules/font.c | 9 ++++++-- 5 files changed, 31 insertions(+), 36 deletions(-) create mode 100644 src/detection/font/font_windows.c delete mode 100644 src/detection/font/font_windows.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 815059e90..04e4d1950 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -434,7 +434,7 @@ elseif(WIN32) src/detection/cursor/cursor_windows.c src/detection/disk/disk_windows.c src/detection/displayserver/displayserver_windows.c - src/detection/font/font_windows.cpp + src/detection/font/font_windows.c src/detection/gpu/gpu_windows.cpp src/detection/host/host_windows.c src/detection/localip/localip_windows.c diff --git a/src/detection/font/font.h b/src/detection/font/font.h index 80bed7a39..822815d2a 100644 --- a/src/detection/font/font.h +++ b/src/detection/font/font.h @@ -14,7 +14,7 @@ typedef struct FFFontResult /** * Linux / BSD: QT, GTK2, GTK3, GTK4 * MacOS: System, User, System Mono, User Mono - * Windows: Desktop, Unset, Unset, Unset + * Windows: Caption, Menu, Message, Status * Other: Unset, Unset, Unset, Unset */ FFstrbuf fonts[FF_DETECT_FONT_NUM_FONTS]; diff --git a/src/detection/font/font_windows.c b/src/detection/font/font_windows.c new file mode 100644 index 000000000..b2595d04d --- /dev/null +++ b/src/detection/font/font_windows.c @@ -0,0 +1,22 @@ +#include "font.h" +#include "util/windows/unicode.h" + +#include + +void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result) +{ + FF_UNUSED(instance); + + NONCLIENTMETRICSW info = { .cbSize = sizeof(info) }; + if(!SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(info), &info, 0)) + ffStrbufAppendS(&result->error, "SystemParametersInfoW(SPI_GETNONCLIENTMETRICS) failed"); + + LOGFONTW* fonts[4] = { &info.lfCaptionFont, &info.lfMenuFont, &info.lfMessageFont, &info.lfStatusFont }; + + for(uint32_t i = 0; i < sizeof(fonts) / sizeof(fonts[0]); ++i) + { + ffWcharToUtf8(fonts[i]->lfFaceName, &result->fonts[i]); + if(fonts[i]->lfHeight < 0) + ffStrbufAppendF(&result->fonts[i], " (%dpt)", (int)-fonts[i]->lfHeight); + } +} diff --git a/src/detection/font/font_windows.cpp b/src/detection/font/font_windows.cpp deleted file mode 100644 index fd400fb71..000000000 --- a/src/detection/font/font_windows.cpp +++ /dev/null @@ -1,32 +0,0 @@ -extern "C" { -#include "font.h" -#include "common/font.h" -} -#include "util/windows/wmi.hpp" - -#include - -extern "C" -void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result) -{ - wchar_t sql[256] = {}; - swprintf(sql, 256, L"SELECT IconTitleFaceName, IconTitleSize FROM Win32_Desktop WHERE Name LIKE '%%\\\\%s'", instance->state.passwd->pw_name); - - FFWmiQuery query(sql, &result->error); - if(!query) - return; - - if(FFWmiRecord record = query.next()) - { - FF_STRBUF_AUTO_DESTROY fontName; - ffStrbufInit(&fontName); - record.getString(L"IconTitleFaceName", &fontName); - - uint64_t fontSize; - record.getUnsigned(L"IconTitleSize", &fontSize); - - ffStrbufAppendF(&result->fonts[0], "%*s (%upt)", fontName.length, fontName.chars, (unsigned)fontSize); - } - else - ffStrbufInitS(&result->error, "No WMI result returned"); -} diff --git a/src/modules/font.c b/src/modules/font.c index 9ca20b547..25f394f61 100644 --- a/src/modules/font.c +++ b/src/modules/font.c @@ -45,9 +45,14 @@ static void printFont(const FFFontResult* font) static void printFont(const FFFontResult* font) { - if(font->fonts[0].length > 0) + const char* types[] = { "Caption", "Menu", "Message", "Status" }; + for(uint32_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { - printf("%s [Desktop]", font->fonts[0].chars); + if(font->fonts[i].length > 0) + { + printf("%s [%s]", font->fonts[i].chars, types[i]); + break; + } } }