Bug 265182 - [FreeType] Do not special case the "sans" font family name
Summary: [FreeType] Do not special case the "sans" font family name
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Text (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Adrian Perez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2023-11-21 00:48 PST by Adrian Perez
Modified: 2023-11-21 03:16 PST (History)
2 users (show)

See Also:


Attachments
Screenshot of two browser, one of them with the “sans” matching removed (1.42 MB, image/png)
2023-11-21 00:54 PST, Adrian Perez
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Adrian Perez 2023-11-21 00:48:33 PST
In “Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp” the is one
instance where we are special-casing the “sans” font family name, which is NOT
a CSS generic font family name, in the first line of the following function:

  static inline bool isCommonlyUsedGenericFamily(const String& familyNameString)
  {
      return equalLettersIgnoringASCIICase(familyNameString, "sans-serif"_s)
          || equalLettersIgnoringASCIICase(familyNameString, "serif"_s)
          || equalLettersIgnoringASCIICase(familyNameString, "monospace"_s)
          || equalLettersIgnoringASCIICase(familyNameString, "fantasy"_s)
  #if PLATFORM(GTK)
          || equalLettersIgnoringASCIICase(familyNameString, "-webkit-system-font"_s)
          || equalLettersIgnoringASCIICase(familyNameString, "-webkit-system-ui"_s)
  #endif
          || equalLettersIgnoringASCIICase(familyNameString, "cursive"_s);
  }

Matching the “sans” name here skips a part of the CSS font fallback algorithm,
which results in websites which use e.g. “font-family: sans” to end up picking
an apparently semi-random choice of font depending on how FontConfig is
configured. Given that a number of Linux distributions include font packages
which have drop-in FontConfig configuration files which may define replacements
and/or aliases for a family named “sans”, each user may see a slightly different
outcome due to this bug.

The solution is to do like the rest of the WebKit font implementations and
not handle “sans” as a generic family name -- because it is not.
Comment 1 Adrian Perez 2023-11-21 00:54:55 PST
Created attachment 468705 [details]
Screenshot of two browser, one of them with the “sans” matching removed

Here's a screenshot comparing how WebKitGTK behaves today (left), and
with the match for “sans” removed (right). The odd choice on the left
was because this particular system does not have a FontConfig “sans”
alias in the configuration, and FontConfig ends up picking a symbols
font (Font Awesome), which has these oddball latin characters.
Comment 2 Adrian Perez 2023-11-21 01:19:06 PST
(In reply to Adrian Perez from comment #1)
> Created attachment 468705 [details]
> Screenshot of two browser, one of them with the “sans” matching removed
> 
> Here's a screenshot comparing how WebKitGTK behaves today (left), and
> with the match for “sans” removed (right). The odd choice on the left
> was because this particular system does not have a FontConfig “sans”
> alias in the configuration, and FontConfig ends up picking a symbols
> font (Font Awesome), which has these oddball latin characters.

Of course if I make sure that there is a “sans” alias defined in the
FontConfig configuration, that ends up being picked. I think in case
somebody has an oddball configuration like the one I used for testing,
having a serif font picked as fallback is still better than picking
a symbols font.
Comment 3 Adrian Perez 2023-11-21 01:25:10 PST
Pull request: https://github.com/WebKit/WebKit/pull/20776
Comment 4 EWS 2023-11-21 03:15:51 PST
Committed 271007@main (9479bf3bdea7): <https://commits.webkit.org/271007@main>

Reviewed commits have been landed. Closing PR #20776 and removing active labels.
Comment 5 Radar WebKit Bug Importer 2023-11-21 03:16:16 PST
<rdar://problem/118680927>