Files
magpie/internal/fonts/fonts_darwin.m
T
tsuixl bd33e575f1 gui: let users choose installed interface and code fonts (#1007)
* gui: choose installed interface and code fonts and styles

Discover installed styles through DirectWrite, CoreText and Fontconfig. Keep choices per computer, apply them before paint and notify both desktop webviews after saving; defaults retain platform and Omarchy fallbacks.

Verified seven focused Go tests, including real Windows discovery (90 families, 276 styles), 14 font browser cases in Chromium/WebKit, 68 settings/zoom/Omarchy/web cases, go vet, Windows amd64/arm64 and Linux nogui builds. Against the original assets, the new UI test fails with Settings must offer an interface font picker (0 != 1). Full Windows suites have separately reproduced upstream failures; macOS/Linux native discovery is covered by the added CI step.

* gui: retain bundled Mac fonts and variable style precision

Only filter an undownloaded CoreText face when it is explicitly downloadable; bundled fonts are installed without being downloaded. Use resolved font names when descriptors omit them. Preserve fractional weights and nonnegative CSS widths for named variable styles.

* gui: restore the authored font cascade when returning to defaults

Apply family-only trait overrides only while a custom face is active. The browser regression catches code under a bold parent becoming 700 instead of its original 400. Keep the compatibility shim first, and apply boot fonts before visible content.

* gui: keep font picker review separate from CI integration

Leave the native test sources and manual verification command with the feature. Move the added workflow step to a follow-up so the font picker can take the normal review path requested on PR #1007.
2026-10-06 19:32:56 +08:00

113 lines
5.7 KiB
Objective-C

//go:build darwin && cgo && !nogui
#import <AppKit/AppKit.h>
#import <CoreText/CoreText.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
static NSString *fontText(CFTypeRef value) {
NSString *text = value && CFGetTypeID(value) == CFStringGetTypeID()
? [(__bridge NSString *)value copy] : nil;
if (value) CFRelease(value);
return [text autorelease];
}
static NSString *fontName(CTFontDescriptorRef descriptor, CFStringRef key) {
return fontText(CTFontDescriptorCopyAttribute(descriptor, key));
}
static double axis(CFDictionaryRef variation, uint32_t tag, double fallback) {
if (!variation) return fallback;
int64_t number = tag;
CFNumberRef key = CFNumberCreate(NULL, kCFNumberSInt64Type, &number);
CFNumberRef value = CFDictionaryGetValue(variation, key);
double out = fallback;
if (value && CFGetTypeID(value) == CFNumberGetTypeID()) CFNumberGetValue(value, kCFNumberDoubleType, &out);
CFRelease(key);
return out;
}
// Apple AAT fonts need not have an OS/2 table. Their native trait uses
// AppKit's weight scale, not the OpenType/CSS numeric scale.
static int traitWeight(CTFontRef font) {
const CGFloat native[] = {NSFontWeightUltraLight, NSFontWeightThin, NSFontWeightLight,
NSFontWeightRegular, NSFontWeightMedium, NSFontWeightSemibold,
NSFontWeightBold, NSFontWeightHeavy, NSFontWeightBlack};
CFDictionaryRef traits = CTFontCopyTraits(font);
CFNumberRef value = traits ? CFDictionaryGetValue(traits, kCTFontWeightTrait) : NULL;
double weight = 0;
if (value) CFNumberGetValue(value, kCFNumberDoubleType, &weight);
if (traits) CFRelease(traits);
int best = 3;
for (int i = 0; i < 9; ++i) if (fabs(weight - native[i]) < fabs(weight - native[best])) best = i;
return (best + 1) * 100;
}
char *magpieInstalledFonts(void) {
@autoreleasepool {
CTFontCollectionRef collection = CTFontCollectionCreateFromAvailableFonts(NULL);
if (!collection) return NULL;
CFArrayRef descriptors = CTFontCollectionCreateMatchingFontDescriptors(collection);
CFRelease(collection);
if (!descriptors) return NULL;
NSMutableArray *faces = [NSMutableArray array];
const double widths[] = {100, 50, 62.5, 75, 87.5, 100, 112.5, 125, 150, 200};
for (CFIndex i = 0; i < CFArrayGetCount(descriptors); ++i) {
CTFontDescriptorRef descriptor = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descriptors, i);
CFTypeRef downloadable = CTFontDescriptorCopyAttribute(descriptor, kCTFontDownloadableAttribute);
CFTypeRef downloaded = CTFontDescriptorCopyAttribute(descriptor, kCTFontDownloadedAttribute);
// A bundled font was never downloaded either. Only a font
// explicitly offered for download can be a remote-only face.
BOOL remote = downloadable && CFEqual(downloadable, kCFBooleanTrue)
&& downloaded && CFEqual(downloaded, kCFBooleanFalse);
if (downloadable) CFRelease(downloadable);
if (downloaded) CFRelease(downloaded);
if (remote) continue;
CTFontRef font = CTFontCreateWithFontDescriptor(descriptor, 0, NULL);
if (!font) continue;
NSString *family = fontName(descriptor, kCTFontFamilyNameAttribute);
NSString *name = fontName(descriptor, kCTFontStyleNameAttribute);
if (!family.length) family = fontText(CTFontCopyFamilyName(font));
if (!name.length) name = fontText(CTFontCopyName(font, kCTFontSubFamilyNameKey));
// Dot-prefixed faces are private system UI implementations, not
// families the webview can request by name.
if (!family.length || !name.length || [family hasPrefix:@"."]) { CFRelease(font); continue; }
CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font);
double weight = traitWeight(font);
double stretch = traits & kCTFontCondensedTrait ? 75 : traits & kCTFontExpandedTrait ? 125 : 100;
NSString *slope = traits & kCTFontItalicTrait ? @"italic" : @"normal";
// OpenType weights are CSS weights; CoreText's floating trait
// scale is not. Keep the original table values where available.
CFDataRef table = CTFontCopyTable(font, kCTFontTableOS2, kCTFontTableOptionNoOptions);
if (table) {
const UInt8 *p = CFDataGetBytePtr(table);
if (CFDataGetLength(table) >= 8) {
int w = (p[4] << 8) | p[5], width = (p[6] << 8) | p[7];
if (w > 0 && w <= 1000) weight = w;
if (width > 0 && width <= 9) stretch = widths[width];
}
if (CFDataGetLength(table) >= 64 && (p[62] & 2)) slope = @"oblique";
CFRelease(table);
}
// Named variable instances can override the base font's table.
CFDictionaryRef variation = CTFontCopyVariation(font);
weight = axis(variation, 'wght', weight);
stretch = axis(variation, 'wdth', stretch);
if (axis(variation, 'ital', 0) != 0) slope = @"italic";
else if (axis(variation, 'slnt', 0) != 0) slope = @"oblique";
if (variation) CFRelease(variation);
CFRelease(font);
[faces addObject:@{@"family": family, @"name": name, @"weight": @(weight), @"style": slope, @"stretch": @(stretch)}];
}
CFRelease(descriptors);
NSData *json = [NSJSONSerialization dataWithJSONObject:faces options:0 error:NULL];
if (!json) return NULL;
char *out = malloc(json.length + 1);
if (!out) return NULL;
memcpy(out, json.bytes, json.length);
out[json.length] = 0;
return out;
}
}