Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/google_fonts/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:mustache_template/mustache.dart';
import 'fonts.pb.dart';

const String _generatedAllPartsFilePath = 'lib/src/google_fonts_all_parts.dart';
const String _generatedLiteFilePath = 'lib/src/google_fonts_lite.dart';
String _generatedPartFilePath(String part) =>
'lib/src/google_fonts_parts/part_$part.dart';
const String _familiesSupportedPath = 'generator/families_supported';
Expand Down Expand Up @@ -41,11 +42,11 @@ Future<void> main() async {
File(_familiesDiffPath).writeAsStringSync(familiesDelta.markdownDiff());
print(_success);

print('\nGenerating $_generatedAllPartsFilePath and part files...');
print('\nGenerating $_generatedAllPartsFilePath, $_generatedLiteFilePath and part files...');
_generateDartCode(fontDirectory);
print(_success);

print('\nFormatting $_generatedAllPartsFilePath and part files...');
print('\nFormatting $_generatedAllPartsFilePath, $_generatedLiteFilePath and part files...');
await Process.run('dart', <String>['format', 'lib']);
print(_success);
}
Expand Down Expand Up @@ -314,6 +315,18 @@ void _generateDartCode(Directory fontDirectory) {
_writeDartFile(_generatedPartFilePath(letter), renderedTemplate);
});

// Generate lite file.
final liteTemplate = Template(
File('generator/google_fonts_lite.tmpl').readAsStringSync(),
htmlEscapeValues: false,
);
final String renderedLiteTemplate = liteTemplate.renderString(
<String, List<Map<String, dynamic>>>{
'fontEntries': methods,
},
);
_writeDartFile(_generatedLiteFilePath, renderedLiteTemplate);

// Generate main file.
final template = Template(
File('generator/google_fonts.tmpl').readAsStringSync(),
Expand Down
26 changes: 26 additions & 0 deletions packages/google_fonts/generator/google_fonts_lite.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';

import 'google_fonts_base.dart';
import 'google_fonts_descriptor.dart';
import 'google_fonts_variant.dart';

// Hosts a Map<String, Map<GoogleFontsVariant, GoogleFontsFile>> where the String is the font's name.
// That is used to allow tree-shaking to remove all of the _parts files.
// If you only call GoogleFontsLite.fontsMap or GoogleFontsLite.loadFont(),
// the code in the GoogleFonts class and its parts classes is never called.
// WHat is called is googleFontsTextStyle, which attempts to load a font if not already loaded.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Typo: "WHat" should be "What".

// What is called is googleFontsTextStyle, which attempts to load a font if not already loaded.

class GoogleFontsLite {
static Map<String, Map<GoogleFontsVariant, GoogleFontsFile>> fontsMap = {
{{#fontEntries}}
'{{fontFamily}}': {
{{#fontUrls}}
const GoogleFontsVariant(fontWeight: FontWeight.w{{variantWeight}}, fontStyle: FontStyle.{{variantStyle}},) : const GoogleFontsFile('{{hash}}', {{length}},),
{{/fontUrls}}
},
{{/fontEntries}}
};
Comment on lines +12 to +21
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider making GoogleFontsLite an abstract class to prevent instantiation, as it only contains static members. Additionally, the fontsMap should be declared as static const for better efficiency. Using fontFamilyDisplay (the actual font name with spaces) as the map key is also recommended for better compatibility with font fallback and consistency with the main GoogleFonts API.

abstract class GoogleFontsLite {
  static const Map<String, Map<GoogleFontsVariant, GoogleFontsFile>> fontsMap = {
    {{#fontEntries}}
      '{{fontFamilyDisplay}}': {
      {{#fontUrls}}
      const GoogleFontsVariant(fontWeight: FontWeight.w{{variantWeight}}, fontStyle: FontStyle.{{variantStyle}},) : const GoogleFontsFile('{{hash}}', {{length}},),
      {{/fontUrls}}
      },
    {{/fontEntries}}
  };


static TextStyle getFont(String fontFamily) {
return googleFontsTextStyle(fontFamily: fontFamily, fonts: fontsMap[fontFamily]!);
}
Comment on lines +23 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getFont method uses the null-assertion operator !, which will cause a runtime crash if an unknown font family is requested. It is safer to check for the existence of the font and throw a more descriptive error.

  static TextStyle getFont(String fontFamily) {
    final fonts = fontsMap[fontFamily];
    if (fonts == null) {
      throw ArgumentError('No font family with name $fontFamily was found.');
    }
    return googleFontsTextStyle(fontFamily: fontFamily, fonts: fonts);
  }

}
2 changes: 1 addition & 1 deletion packages/google_fonts/lib/src/google_fonts_descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GoogleFontsFile {
///
/// The [expectedFileHash] is used to verify the integrity of the downloaded
/// file, and [expectedLength] is checked to ensure the file size is correct.
GoogleFontsFile(this.expectedFileHash, this.expectedLength);
const GoogleFontsFile(this.expectedFileHash, this.expectedLength);

/// The expected hash of the font file for validation.
final String expectedFileHash;
Expand Down
Loading