macostexteditnsfont

How to find the default font for East Asian character in MacOS


When using a text edit application, a font (such as "Menlo") is selected to present glyphs, when the selected font doesn't contain a special glyph(such as “𠹷”, it's a simple Chinese glyph, "Menlo" doesn't contain it), Application will pick up a font for you to present it, In MacOS(Catalina), there are about 62 fonts (STBaoliSC-Regular, STKaiti, STSong, PingFangSC-Regular...) contain this glyph "𠹷", I found that almost every text edit application (vscode, sublime text, TextEdit) pick up the same font -- "PingFangSC-Regular" ,so I consider if it is every glyph has its own default font? if so, how can I get the font name?


Solution

  • This is handled by the "cascade list." If you want the default list, it's available through CTFontCopyDefaultCascadeListForLanguages, on a per-font basis:

    import CoreText
    let font = CTFontCreateWithName("Helvetica" as CFString, 12, nil)
    let descriptors = CTFontCopyDefaultCascadeListForLanguages(font, nil)! as! [CTFontDescriptor]
    

    If you wanted to see the list, you could do it this way (Core Text does not have very nice bridging to Swift):

    for descriptor in descriptors {
        let attributes = CTFontDescriptorCopyAttributes(descriptor) as! [String: Any]
        print(attributes[kCTFontNameAttribute as String]!)
    }
    

    ==>

    LucidaGrande
    .AppleSymbolsFB
    GeezaPro
    NotoNastaliqUrdu
    Thonburi
    Kailasa
    PingFangSC-Regular
    PingFangTC-Regular
    AppleSDGothicNeo-Regular
    PingFangTC-Regular
    PingFangSC-Regular
    PingFangHK-Regular
    PingFangSC-Regular
    HiraginoSans-W3
    HiraginoSansGB-W3
    KohinoorBangla-Regular
    KohinoorDevanagari-Regular
    KohinoorGujarati-Regular
    MuktaMahee-Regular
    NotoSansKannada-Regular
    KhmerSangamMN
    LaoSangamMN
    MalayalamSangamMN
    NotoSansMyanmar-Regular
    NotoSansZawgyi-Regular
    NotoSansOriya
    SinhalaSangamMN
    TamilSangamMN
    KohinoorTelugu-Regular
    NotoSansArmenian-Regular
    EuphemiaUCAS
    Menlo-Regular
    STIXGeneral-Regular
    Galvji
    Kefa-Regular
    .NotoSansUniversal
    AppleColorEmoji
    

    PingFangSC-Regular is the first East Asian font in the list, so it's going to be the one picked to replace Helvetica. It's also the first East Asian font in the cascade list for Lucida Grande, and Helvetica Neue. And it's kind of straight-forward, even boring, font. But if you were using a bit more unusual font, like American Typewriter? Well, that would be replaced with Songti, which is a bit lighter. Marker Felt replaces with Kaiti, which has more variation on the stoke widths (though IMO it would be much better to replace it with Kaiti Black rather than Regular). I don't know of any built-in Asian fonts that are as "fun" as the available Latin fonts, but if you had one, you can customize the cascade list to choose it instead (using NSFontDescriptor on Mac, or UIFontDescriptor on iOS).

    If you want more details about cascade lists, see the WWDC 2018 video, Creating Apps for a Global Audience.