From 7b05f351f26849ab31d9425e585f7a418496a574 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Mon, 2 May 2022 09:32:54 +0800 Subject: [PATCH] Make the NS font dialog return more correct values * src/nsfns.m (Fx_select_font): Update doc string. * src/nsterm.m (ns_font_desc_to_font_spec): New function. ([EmacsView showFontPanel]): Return selected font as a font spec instead. --- src/nsfns.m | 2 +- src/nsterm.m | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/nsfns.m b/src/nsfns.m index b71a3d7376d..41fea6f0fe6 100644 --- a/src/nsfns.m +++ b/src/nsfns.m @@ -1595,7 +1595,7 @@ Some window managers may refuse to restack windows. */) DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0, doc: /* Read a font using a Nextstep dialog. -Return a string describing the selected font. +Return a font specification describing the selected font. FRAME is the frame on which to pop up the font chooser. If omitted or nil, it defaults to the selected frame. */) diff --git a/src/nsterm.m b/src/nsterm.m index 5e70e0d5665..730472d261e 100644 --- a/src/nsterm.m +++ b/src/nsterm.m @@ -6055,6 +6055,63 @@ not_in_argv (NSString *arg) @end /* EmacsApp */ +static Lisp_Object +ns_font_desc_to_font_spec (NSFontDescriptor *desc, NSFont *font) +{ + NSFontSymbolicTraits traits = [desc symbolicTraits]; + NSDictionary *dict = [desc objectForKey: NSFontTraitsAttribute]; + NSString *family = [font familyName]; + Lisp_Object lwidth, lslant, lweight, lheight; + NSNumber *tem; + + lwidth = Qnil; + lslant = Qnil; + lweight = Qnil; + lheight = Qnil; + + if (traits & NSFontBoldTrait) + lweight = Qbold; + + if (traits & NSFontItalicTrait) + lslant = Qitalic; + + if (traits & NSFontCondensedTrait) + lwidth = Qcondensed; + else if (traits & NSFontExpandedTrait) + lwidth = Qexpanded; + + if (dict != nil) + { + tem = [dict objectForKey: NSFontSlantTrait]; + + if (tem != nil) + lslant = ([tem floatValue] > 0 + ? Qitalic : ([tem floatValue] < 0 + ? intern ("reverse-italic") + : Qnormal)); + + tem = [dict objectForKey: NSFontWeightTrait]; + + if (tem != nil) + lweight = ([tem floatValue] > 0 + ? Qbold : ([tem floatValue] < -0.4f + ? Qlight : Qnormal)); + + tem = [dict objectForKey: NSFontWidthTrait]; + + if (tem != nil) + lwidth = ([tem floatValue] > 0 + ? Qexpanded : ([tem floatValue] < 0 + ? Qnormal : Qcondensed)); + } + + lheight = make_float ([font pointSize]); + + return CALLN (Ffont_spec, + QCwidth, lwidth, QCslant, lslant, + QCweight, lweight, QCsize, lheight, + QCfamily, [family lispString]); +} /* ========================================================================== @@ -6151,9 +6208,9 @@ not_in_argv (NSString *arg) [[fm fontPanel: YES] setIsVisible: NO]; font_panel_active = NO; - /* TODO: return a font spec instead of a string. */ if (result) - return [[result familyName] lispString]; + return ns_font_desc_to_font_spec ([result fontDescriptor], + result); return Qnil; } -- 2.39.2