registerFromBridge static method

void registerFromBridge({
  1. required int sheetId,
  2. required String fontFamily,
  3. required String src,
  4. required String? fontWeight,
  5. required String? fontStyle,
  6. required double contextId,
  7. String? baseHref,
})

Implementation

static void registerFromBridge({
  required int sheetId,
  required String fontFamily,
  required String src,
  required String? fontWeight,
  required String? fontStyle,
  required double contextId,
  String? baseHref,
}) {
  if (fontFamily.isEmpty || src.isEmpty) return;

  final String cleanFamily = removeQuotationMark(fontFamily);

  final FontWeight weight = _parseFontWeight(fontWeight);
  final FontStyle style = (fontStyle == 'italic') ? FontStyle.italic : FontStyle.normal;

  List<CSSFunctionalNotation> functions = CSSFunction.parseFunction(src);
  List<FontSource> fonts = [];
  for (final CSSFunctionalNotation notation in functions) {
    if (notation.name == 'url' && notation.args.isNotEmpty) {
      String tmpSrc = removeQuotationMark(notation.args[0]);
      if (tmpSrc.startsWith('data')) {
        // data:...;base64,<...>
        String tmpContent = tmpSrc.split(';').last;
        if (tmpContent.startsWith('base64')) {
          String base64 = tmpSrc.split(',').last;
          try {
            Uint8List decoded = base64Decode(base64);
            if (decoded.isNotEmpty) {
              fonts.add(FontSource.content(decoded));
            }
          } catch (_) {}
        }
      } else {
        String formatFromExt = tmpSrc.split('.').last;
        fonts.add(FontSource(tmpSrc, formatFromExt));
      }
    }
  }

  FontSource? targetFont = fonts.firstWhereOrNull((f) => supportedFonts.contains(f.format));
  if (targetFont == null) {
    return;
  }

  final descriptor = FontFaceDescriptor(
    fontFamily: cleanFamily,
    fontWeight: weight,
    fontStyle: style,
    font: targetFont,
    contextId: contextId,
    baseHref: baseHref,
    sheetId: sheetId,
  );

  _fontFaceRegistry.putIfAbsent(cleanFamily, () => []).add(descriptor);
  _sheetRegistry.putIfAbsent(sheetId, () => []).add(descriptor);
}