parseLiteDescriptors function
The families and files in the source of google_fonts_lite.dart.
Throws if a family has missing, duplicate, or malformed variants.
Implementation
Map<String, List<FontVariant>> parseLiteDescriptors(String source) {
final families = RegExp(
r"^ '([^']+)': \{",
multiLine: true,
).allMatches(source).toList();
final variants = RegExp(
r"GoogleFontsVariant\(\s*fontWeight: FontWeight\.w(\d+),\s*"
r"fontStyle: FontStyle\.(normal|italic),\s*\): (?:const )?"
r"GoogleFontsFile\(\s*'([0-9a-f]{64})',\s*(\d+),",
);
final result = <String, List<FontVariant>>{};
for (var i = 0; i < families.length; i++) {
final family = families[i].group(1)!;
final block = source.substring(
families[i].end,
i + 1 == families.length ? source.length : families[i + 1].start,
);
final entries = variants
.allMatches(block)
.map(
(match) => FontVariant(
int.parse(match.group(1)!),
match.group(2) == 'italic',
match.group(3)!,
int.parse(match.group(4)!),
),
)
.toList();
if (entries.isEmpty ||
entries.length !=
RegExp(r'GoogleFontsVariant\(').allMatches(block).length ||
result.containsKey(family) ||
entries.map((entry) => entry.pickerName).toSet().length !=
entries.length) {
throw FormatException(
'Missing, duplicate, or malformed variants: $family',
);
}
result[family] = entries;
}
if (result.isEmpty) throw const FormatException('No Lite descriptors found');
return result;
}