Files
Reims-Go/internal/host/fonts_windows.go
T
NwflowerandClaude Opus 5.5 c639682a88 refactor: remove unused functions and keep the codebase free of them
Delete functions that no product code calls, and fold "simple plus
WithOptions" pairs into the one signature callers actually use. Rename
the live macOS lifecycle functions that were labelled Legacy.

The architecture tests now share one source loader and reject functions
without a product caller and catalog messages nobody reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:55:03 -07:00

99 lines
2.9 KiB
Go

//go:build windows
package host
import (
"strings"
"sync"
"syscall"
"unicode/utf16"
"unsafe"
"github.com/Nwflower/reims-go/internal/i18n"
)
var (
modGDI32Font = syscall.NewLazyDLL("gdi32.dll")
modUser32Font = syscall.NewLazyDLL("user32.dll")
procEnumFontFamiliesEx = modGDI32Font.NewProc("EnumFontFamiliesExW")
procGetDCFont = modUser32Font.NewProc("GetDC")
procReleaseDCFont = modUser32Font.NewProc("ReleaseDC")
fontDetectMu sync.Mutex
fontDetectHit bool
fontDetectMemo = map[string]bool{}
fontDetectCB = syscall.NewCallback(func(lpelfe, lpntme, fontType, lparam uintptr) uintptr {
fontDetectHit = true
return 0
})
)
type gdiLogFontW struct {
height, width, escapement, orientation, weight int32
italic, underline, strikeOut, charSet byte
outPrecision, clipPrecision, quality, pitchFam byte
face [32]uint16
}
func hasInstalledFontFamily(name string) bool {
fontDetectMu.Lock()
defer fontDetectMu.Unlock()
if ok, cached := fontDetectMemo[name]; cached {
return ok
}
dc, _, _ := procGetDCFont.Call(0)
if dc == 0 {
return true // fail-open if desktop DC unavailable
}
defer procReleaseDCFont.Call(0, dc)
var lf gdiLogFontW
lf.charSet = 1 // DEFAULT_CHARSET
u16 := utf16.Encode([]rune(name))
if len(u16) >= len(lf.face) {
u16 = u16[:len(lf.face)-1]
}
copy(lf.face[:], u16)
fontDetectHit = false
procEnumFontFamiliesEx.Call(dc, uintptr(unsafe.Pointer(&lf)), fontDetectCB, 0, 0)
if fontDetectHit {
// Only memoize positive hits so installing Windows FoD fonts at runtime
// clears the missing-font hint without restarting Reims-Go.
fontDetectMemo[name] = true
}
return fontDetectHit
}
// CheckMissingLocaleFontsForUI checks whether Windows has fonts for targetLocale
// and returns the localized script name in uiLocale when missing.
func CheckMissingLocaleFontsForUI(targetLocale string, uiLocale i18n.Locale) string {
low := strings.ToLower(strings.TrimSpace(targetLocale))
var candidates []string
var scriptKey string
switch {
case low == "zh-tw" || low == "zh-hk" || low == "zh-mo" || strings.HasPrefix(low, "zh-hant"):
scriptKey = "font.script.zh_hant"
candidates = []string{"Microsoft JhengHei UI", "Microsoft JhengHei", "Noto Sans TC", "PingFang TC", "Source Han Sans TC"}
case strings.HasPrefix(low, "zh"):
scriptKey = "font.script.zh_hans"
candidates = []string{"Microsoft YaHei UI", "Microsoft YaHei", "Noto Sans SC", "PingFang SC", "HarmonyOS Sans SC", "MiSans"}
case strings.HasPrefix(low, "ja"):
scriptKey = "font.script.ja"
candidates = []string{"Yu Gothic UI", "Yu Gothic", "Meiryo UI", "Meiryo", "BIZ UDPGothic", "Noto Sans JP"}
case strings.HasPrefix(low, "ko"):
scriptKey = "font.script.ko"
candidates = []string{"Malgun Gothic", "Noto Sans KR", "Apple SD Gothic Neo"}
default:
return ""
}
for _, f := range candidates {
if hasInstalledFontFamily(f) {
return ""
}
}
return i18n.T(uiLocale, scriptKey)
}