Download src/platform/dpi.cpp from fava21/hf_repo4: direct link, hf CLI and curl.
- Browser
- Download file 9.36 kB
-
https://huggingface.co/datasets/fava21/hf_repo4/resolve/main/src/platform/dpi.cpp
- Command line
-
hf download hf://datasets/fava21/hf_repo4/src/platform/dpi.cpp
-
curl -L -o dpi.cpp https://huggingface.co/datasets/fava21/hf_repo4/resolve/main/src/platform/dpi.cpp
9.36 kB
| // FCS Glass - dpi.cpp | |
| // | |
| // Implements: P1, P2, P3, W5, A2. (See dpi.h for the measured research notes.) | |
| // | |
| // Every *ForDpi export is Win10 1607+, which is ABOVE the project's 0x0602 | |
| // target floor, so none of them may be linked or referenced directly. They are | |
| // all resolved with GetProcAddress, and every absence is a supported path. | |
| namespace fcs { | |
| namespace { | |
| // --- resolved function pointers (null == unavailable, never an error) ------- | |
| using SetProcessDpiAwarenessContextFn = | |
| BOOL(WINAPI*)(DPI_AWARENESS_CONTEXT); // D-14: NOT int | |
| using GetDpiForWindowFn = UINT(WINAPI*)(HWND); | |
| using GetDpiForSystemFn = UINT(WINAPI*)(void); | |
| using GetSystemMetricsForDpiFn = int(WINAPI*)(int, UINT); | |
| using AdjustWindowRectExForDpiFn = BOOL(WINAPI*)(LPRECT, DWORD, BOOL, DWORD, UINT); | |
| using SystemParametersInfoForDpiFn = | |
| BOOL(WINAPI*)(UINT, UINT, PVOID, UINT, UINT); | |
| SetProcessDpiAwarenessContextFn g_set_ctx = nullptr; | |
| GetDpiForWindowFn g_dpi_for_window = nullptr; | |
| GetDpiForSystemFn g_dpi_for_system = nullptr; | |
| GetSystemMetricsForDpiFn g_metrics_for_dpi = nullptr; | |
| AdjustWindowRectExForDpiFn g_adjust_for_dpi = nullptr; | |
| SystemParametersInfoForDpiFn g_spi_for_dpi = nullptr; | |
| DpiState g_state = {}; | |
| // D-13. These metrics genuinely have a per-dpi variant in | |
| // GetSystemMetricsForDpi. For anything else that function returns the | |
| // UNSCALED system value, so the arithmetic fallback must be used instead. | |
| bool MetricHasDpiVariant(int index) { | |
| switch (index) { | |
| case SM_CXICON: | |
| case SM_CYICON: | |
| case SM_CXSMICON: | |
| case SM_CYSMICON: | |
| case SM_CXSIZEFRAME: | |
| case SM_CYSIZEFRAME: | |
| case SM_CXPADDEDBORDER: | |
| case SM_CXDLGFRAME: | |
| case SM_CYDLGFRAME: | |
| case SM_CXMENUCHECK: | |
| case SM_CYMENUCHECK: | |
| case SM_CXMINIMIZED: | |
| case SM_CYMINIMIZED: | |
| case SM_CXSCREEN: | |
| case SM_CYSCREEN: | |
| // SM_CXSCREEN/SM_CYSCREEN are here ONLY because the measured host | |
| // returned the unscaled value for them; see D-13. Treated as | |
| // no-variant by ScaleMetric below. | |
| return index != SM_CXSCREEN && index != SM_CYSCREEN; | |
| default: | |
| return false; | |
| } | |
| } | |
| } // namespace | |
| // REQ P1 - resolve, then opt in to per-monitor v2, falling back through | |
| // per-monitor v1, shcore, and finally system-aware. | |
| DpiState InitDpiAwareness() { | |
| HMODULE user32 = ::GetModuleHandleW(L"user32.dll"); | |
| if (user32) { | |
| g_set_ctx = reinterpret_cast<SetProcessDpiAwarenessContextFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(user32, "SetProcessDpiAwarenessContext"))); | |
| g_dpi_for_window = reinterpret_cast<GetDpiForWindowFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(user32, "GetDpiForWindow"))); | |
| g_dpi_for_system = reinterpret_cast<GetDpiForSystemFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(user32, "GetDpiForSystem"))); | |
| g_metrics_for_dpi = reinterpret_cast<GetSystemMetricsForDpiFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(user32, "GetSystemMetricsForDpi"))); | |
| g_adjust_for_dpi = reinterpret_cast<AdjustWindowRectExForDpiFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(user32, "AdjustWindowRectExForDpi"))); | |
| g_spi_for_dpi = reinterpret_cast<SystemParametersInfoForDpiFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(user32, "SystemParametersInfoForDpi"))); | |
| } | |
| g_state.has_dpi_for_window = (g_dpi_for_window != nullptr); | |
| g_state.has_adjust_for_dpi = (g_adjust_for_dpi != nullptr); | |
| g_state.has_for_dpi_metrics = (g_metrics_for_dpi != nullptr); | |
| if (g_set_ctx) { | |
| // Must happen before any window exists. Failure here is not fatal: a later | |
| // window or a manifest may already have set awareness. | |
| if (g_set_ctx(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)) { | |
| g_state.per_monitor_v2 = true; | |
| g_state.awareness_set = true; | |
| } else if (g_set_ctx(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE)) { | |
| g_state.awareness_set = true; | |
| } | |
| } | |
| if (!g_state.awareness_set) { | |
| // P1 fallback: shcore!SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE). | |
| // | |
| // RESEARCH: the declaration is hidden at our target floor. It lives in | |
| // ShellScalingApi.h behind `#if (NTDDI_VERSION >= NTDDI_WINBLUE)` (6.3), | |
| // while CMake sets NTDDI_VERSION=0x06020000 (6.2) for the Server 2012 R2 | |
| // floor of HC1. The export itself exists on every OS from 8.1 onward and | |
| // is exactly what P1 asks for as the fallback, so it is declared and | |
| // resolved locally rather than raising NTDDI_VERSION and dragging in every | |
| // other 6.3 symbol with it. | |
| using SetProcessDpiAwarenessShcoreFn = HRESULT(WINAPI*)(int); | |
| static SetProcessDpiAwarenessShcoreFn shcore_set = nullptr; | |
| static bool shcore_tried = false; | |
| if (!shcore_tried) { | |
| shcore_tried = true; | |
| HMODULE shcore = ::LoadLibraryW(L"shcore.dll"); | |
| if (shcore) { | |
| shcore_set = reinterpret_cast<SetProcessDpiAwarenessShcoreFn>( | |
| reinterpret_cast<void*>(::GetProcAddress(shcore, "SetProcessDpiAwareness"))); | |
| } | |
| } | |
| // PROCESS_PER_MONITOR_DPI_AWARE == 2 (PROCESS_DPI_AWARENESS enum). | |
| if (shcore_set && SUCCEEDED(shcore_set(2))) { | |
| g_state.awareness_set = true; | |
| } | |
| } | |
| if (g_dpi_for_system) { | |
| const UINT sys = g_dpi_for_system(); | |
| if (sys != 0) g_state.system_dpi = sys; | |
| } | |
| return g_state; | |
| } | |
| // REQ P1 - per-window scale. Windows without GetDpiForWindow fall back to the | |
| // system value, which is correct under system-aware and wrong only under | |
| // per-monitor, where the API is present anyway. | |
| float WindowScale(HWND hwnd) { | |
| UINT dpi = g_state.system_dpi; | |
| if (hwnd && g_dpi_for_window) { | |
| const UINT w = g_dpi_for_window(hwnd); | |
| if (w != 0) dpi = w; | |
| } | |
| if (dpi == 0) dpi = kDpiBaseline; | |
| return static_cast<float>(dpi) / static_cast<float>(kDpiBaseline); | |
| } | |
| // REQ W5 - see D-13. Only genuinely dpi-variant metrics go through | |
| // GetSystemMetricsForDpi; the rest use the measured fallback formula, with | |
| // round-half-up so it matches A2. | |
| int Metric(int index, float scale) { | |
| if (g_metrics_for_dpi && MetricHasDpiVariant(index)) { | |
| const UINT dpi = | |
| static_cast<UINT>(scale * static_cast<float>(kDpiBaseline) + 0.5f); | |
| return g_metrics_for_dpi(index, dpi); | |
| } | |
| const int base = ::GetSystemMetrics(index); | |
| if (base <= 0) return base; | |
| const float sys = static_cast<float>(g_state.system_dpi); | |
| if (sys <= 0.0f) return base; | |
| // BUG-07: the documented formula (dpi.h W5, LEDGER W5) is | |
| // GetSystemMetrics * dpi / systemDpi. `scale` is dpi/96, so multiply by | |
| // kDpiBaseline and divide by the system dpi. The old code computed | |
| // `sys` and then never used it, double-scaling by systemDpi/96 on any | |
| // host whose system dpi is not 96. | |
| const float dpi = scale * static_cast<float>(kDpiBaseline); | |
| return static_cast<int>(((static_cast<float>(base) * dpi) / sys) + 0.5f); | |
| } | |
| // REQ W5 - measured: 100x100 client -> 116x139 at 96 dpi, 126x171 at 192 dpi. | |
| bool AdjustWindowRect(RECT* rect, DWORD style, BOOL menu, DWORD ex_style, UINT dpi) { | |
| if (!rect) return false; | |
| if (g_adjust_for_dpi) return g_adjust_for_dpi(rect, style, menu, ex_style, dpi); | |
| // BUG-08: the client extent is captured BEFORE the system-dpi call - the | |
| // fallback rescales ONLY the frame the API added, never the client. The | |
| // old code scaled the whole rect, silently inflating the requested | |
| // client size whenever dpi != systemDpi. | |
| const LONG client_w = rect->right - rect->left; | |
| const LONG client_h = rect->bottom - rect->top; | |
| if (!::AdjustWindowRectEx(rect, style, menu, ex_style)) return false; | |
| if (dpi != 0 && g_state.system_dpi != 0 && dpi != g_state.system_dpi) { | |
| const float factor = static_cast<float>(dpi) / static_cast<float>(g_state.system_dpi); | |
| const LONG frame_w = (rect->right - rect->left) - client_w; | |
| const LONG frame_h = (rect->bottom - rect->top) - client_h; | |
| const LONG nf_w = static_cast<LONG>((static_cast<float>(frame_w) * factor) + 0.5f); | |
| const LONG nf_h = static_cast<LONG>((static_cast<float>(frame_h) * factor) + 0.5f); | |
| rect->right = rect->left + client_w + nf_w; | |
| rect->bottom = rect->top + client_h + nf_h; | |
| } | |
| return true; | |
| } | |
| // REQ P2 - uniform scale only OUTSIDE the 960..1600 dp band, clamped 0.85..1.25. | |
| float ContentScaleForWidthDp(float width_dp) { | |
| if (width_dp <= 0.0f) return 1.0f; | |
| if (width_dp >= kScaleBandLow && width_dp <= kScaleBandHigh) return 1.0f; | |
| float s = 1.0f; | |
| if (width_dp < kScaleBandLow) { | |
| s = width_dp / kScaleBandLow; | |
| } else { | |
| s = width_dp / kScaleBandHigh; | |
| } | |
| return std::min(kScaleMax, std::max(kScaleMin, s)); | |
| } | |
| // REQ P3 - body text stays 12..18 dp, ceiling 24 when the OS text scale is set. | |
| float BodyTextDp(const DpiState& dpi, float width_dp) { | |
| const OsInfo os = QueryOsInfo(); | |
| float ceiling = 18.0f; | |
| if (os.text_scale_known && os.text_scale > 1.0f) ceiling = 24.0f; | |
| float size = 14.0f; // A4 body | |
| const float content = ContentScaleForWidthDp(width_dp); | |
| size *= content; | |
| size *= os.text_scale_known ? os.text_scale : 1.0f; | |
| (void)dpi; | |
| return std::min(ceiling, std::max(12.0f, size)); | |
| } | |
| } // namespace fcs |