resolveFontFamilyFallback static method

List<String> resolveFontFamilyFallback(
  1. String? fontFamily
)

Implementation

static List<String> resolveFontFamilyFallback(String? fontFamily) {
  fontFamily = fontFamily ?? 'sans-serif';
  List<String> values = fontFamily.split(_commaRegExp);
  List<String> resolvedFamily = List.empty(growable: true);

  for (int i = 0; i < values.length; i++) {
    String familyName = values[i];
    // Remove wrapping quotes: "Gill Sans" -> Gill Sans
    if (familyName[0] == '"' || familyName[0] == '\'') {
      familyName = familyName.substring(1, familyName.length - 1);
    }

    switch (familyName) {
      case 'sans-serif':
      // Default sans-serif font in iOS (9 and newer)and iPadOS: Helvetica
      // Default sans-serif font in Android (4.0+): Roboto
        resolvedFamily.addAll(['Helvetica', 'Roboto', 'PingFang SC', 'PingFang TC']);
        break;
      case 'serif':
      // Default serif font in iOS and iPadOS: Times
      // Default serif font in Android (4.0+): Noto Serif
        resolvedFamily.addAll([
          'Times',
          'Times New Roman',
          'Noto Serif',
          'Songti SC',
          'Songti TC',
          'Hiragino Mincho ProN',
          'AppleMyungjo',
          'Apple SD Gothic Neo'
        ]);
        break;
      case 'monospace':
      // Default monospace font in iOS and iPadOS: Courier
        resolvedFamily.addAll(['Courier', 'Courier New', 'DroidSansMono', 'Monaco', 'Heiti SC', 'Heiti TC']);
        break;
      case 'cursive':
      // Default cursive font in iOS and iPadOS: Snell Roundhand
        resolvedFamily.addAll(['Snell Roundhand', 'Apple Chancery', 'DancingScript', 'Comic Sans MS']);
        break;
      case 'fantasy':
      // Default fantasy font in iOS and iPadOS:
      // Default fantasy font in MacOS: Papyrus
        resolvedFamily.addAll(['Papyrus', 'Impact']);
        break;
      default:
        resolvedFamily.add(familyName);
    }
  }

  // Only for internal use.
  if (resolvedFamily.isEmpty && CSSText.DEFAULT_FONT_FAMILY_FALLBACK != null) {
    return CSSText.DEFAULT_FONT_FAMILY_FALLBACK!;
  }
  return resolvedFamily;
}