Skip to content

Commit 866678c

Browse files
[google_fonts] Add google_fonts_lite file to allow tree-shaking of the other huge files (flutter#11433)
Adds a `google_fonts_lite.dart` generated file, which contains a map of `fontFamily` to fonts variants and a `getFont` function that simply forwards the call to the internal `googleFontsTextStyle` function which does the downloading and loading of the font. *List which issues are fixed by this PR. You must list at least one issue.* flutter#184337 I have not done documentation, testing etc because this PR is for discussion purposes. I'd like approval of the concept before doing the paperwork. The idea is to provide a _lite_ version of the `GoogleFonts` class. This lite version does not depend on any of the other generated code, which is absolutely huge and takes up a massive amount of space (8MB on android for v8.0.0). By using this lite version only, the compiler is able to tree-shake all that additionnal code while you retain the ability to invoke any font from the package via `getFont`. For me this brings the package's footprint from 8MB/target platform to less than a 1MB/target when building a release apk.
1 parent 1240bb5 commit 866678c

42 files changed

Lines changed: 69883 additions & 8173 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/google_fonts/CHANGELOG.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
1+
## NEXT
2+
3+
- Adds the `GoogleFontsLite` class to allow tree-shaking unused font code.
4+
- Added fonts:
5+
- `Akt`
6+
- `Alien Block`
7+
- `Allkin`
8+
- `Alyamama`
9+
- `BJCree`
10+
- `Betania Patmos`
11+
- `Betania Patmos GDL`
12+
- `Betania Patmos In`
13+
- `Betania Patmos In GDL`
14+
- `Bpmf Huninn`
15+
- `Bpmf Iansui`
16+
- `Bpmf Zihi Kai Std`
17+
- `Datatype`
18+
- `Estedad`
19+
- `Finlandica Headline`
20+
- `Finlandica Text`
21+
- `Geist Pixel`
22+
- `Gveret Levin`
23+
- `Idiqlat`
24+
- `Iosevka Charon`
25+
- `Iosevka Charon Mono`
26+
- `LINE Seed JP`
27+
- `M PLUS U`
28+
- `Miranda Sans`
29+
- `Montenegrin Gothic One`
30+
- `Playwrite NZ Basic`
31+
- `Playwrite NZ Basic Guides`
32+
- `Pliant`
33+
- `Ramsina`
34+
- `SN Pro`
35+
- `STIX Two Math`
36+
- `Saira Stencil`
37+
- `Strichpunkt Sans`
38+
- `Yuyu`
39+
- `Yuyu Short`
40+
- Removed fonts:
41+
- `Finlandica`
42+
- `Saira Stencil One`
43+
144
## 8.2.1
245

346
- Fixes `pendingFonts()` retaining failed font loads.

‎packages/google_fonts/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ return MaterialApp(
114114
);
115115
```
116116

117+
### Lower build size
118+
The `GoogleFontsLite` class is a replacement for the `GoogleFonts` class containing only a map of all fonts and the `getFont` function. Using *only* `GoogleFontsLite` allows the Dart compiler to tree-shake most of the package's code, yielding a significant reduction in build size.
119+
117120
### Visual font swapping
118121
To avoid visual font swaps that occur when a font is loading, use [FutureBuilder](https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html) and [GoogleFonts.pendingFonts()](https://pub.dev/documentation/google_fonts/latest/google_fonts/GoogleFonts/pendingFonts.html).
119122

‎packages/google_fonts/generator/families_supported‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Akaya Kanadaka
2222
Akaya Telivigala
2323
Akronim
2424
Akshar
25+
Akt
2526
Aladin
2627
Alan Sans
2728
Alata
@@ -38,6 +39,7 @@ Alex Brush
3839
Alexandria
3940
Alfa Slab One
4041
Alice
42+
Alien Block
4143
Alike
4244
Alike Angular
4345
Alkalami
@@ -46,6 +48,7 @@ Allan
4648
Allerta
4749
Allerta Stencil
4850
Allison
51+
Allkin
4952
Allura
5053
Almarai
5154
Almendra
@@ -56,6 +59,7 @@ Alumni Sans Collegiate One
5659
Alumni Sans Inline One
5760
Alumni Sans Pinstripe
5861
Alumni Sans SC
62+
Alyamama
5963
Amarante
6064
Amaranth
6165
Amarna
@@ -146,6 +150,7 @@ BIZ UDGothic
146150
BIZ UDMincho
147151
BIZ UDPGothic
148152
BIZ UDPMincho
153+
BJCree
149154
Babylonica
150155
Bacasime Antique
151156
Bad Script
@@ -195,6 +200,10 @@ Benne
195200
Bentham
196201
Berkshire Swash
197202
Besley
203+
Betania Patmos
204+
Betania Patmos GDL
205+
Betania Patmos In
206+
Betania Patmos In GDL
198207
Beth Ellen
199208
Bevan
200209
BhuTuka Expanded One
@@ -241,6 +250,9 @@ Boogaloo
241250
Borel
242251
Bowlby One
243252
Bowlby One SC
253+
Bpmf Huninn
254+
Bpmf Iansui
255+
Bpmf Zihi Kai Std
244256
Braah One
245257
Brawler
246258
Bree Serif
@@ -386,6 +398,7 @@ Danfo
386398
Dangrek
387399
Darker Grotesque
388400
Darumadrop One
401+
Datatype
389402
David Libre
390403
Dawning of a New Day
391404
Days One
@@ -457,6 +470,7 @@ Epunda Sans
457470
Epunda Slab
458471
Erica One
459472
Esteban
473+
Estedad
460474
Estonia
461475
Euphoria Script
462476
Ewert
@@ -484,7 +498,8 @@ Fenix
484498
Festive
485499
Figtree
486500
Finger Paint
487-
Finlandica
501+
Finlandica Headline
502+
Finlandica Text
488503
Fira Code
489504
Fira Mono
490505
Fira Sans
@@ -537,6 +552,7 @@ Gasoek One
537552
Gayathri
538553
Geist
539554
Geist Mono
555+
Geist Pixel
540556
Gelasio
541557
Gemunu Libre
542558
Genos
@@ -592,6 +608,7 @@ Gugi
592608
Gulzar
593609
Gupter
594610
Gurajada
611+
Gveret Levin
595612
Gwendolyn
596613
Habibi
597614
Hachi Maru Pop
@@ -654,6 +671,7 @@ Iansui
654671
Ibarra Real Nova
655672
Iceberg
656673
Iceland
674+
Idiqlat
657675
Imbue
658676
Imperial Script
659677
Imprima
@@ -672,6 +690,8 @@ Instrument Serif
672690
Intel One Mono
673691
Inter
674692
Inter Tight
693+
Iosevka Charon
694+
Iosevka Charon Mono
675695
Irish Grover
676696
Island Moments
677697
Istok Web
@@ -777,6 +797,7 @@ Kumar One
777797
Kumar One Outline
778798
Kumbh Sans
779799
Kurale
800+
LINE Seed JP
780801
LXGW Marker Gothic
781802
LXGW WenKai Mono TC
782803
LXGW WenKai TC
@@ -864,6 +885,7 @@ M PLUS 1p
864885
M PLUS 2
865886
M PLUS Code Latin
866887
M PLUS Rounded 1c
888+
M PLUS U
867889
Ma Shan Zheng
868890
Macondo
869891
Macondo Swash Caps
@@ -926,6 +948,7 @@ Miltonian Tattoo
926948
Mina
927949
Mingzat
928950
Miniver
951+
Miranda Sans
929952
Miriam Libre
930953
Mirza
931954
Miss Fajardose
@@ -953,6 +976,7 @@ Monsieur La Doulaise
953976
Montaga
954977
Montagu Slab
955978
MonteCarlo
979+
Montenegrin Gothic One
956980
Montez
957981
Montserrat
958982
Montserrat Alternates
@@ -1403,6 +1427,8 @@ Playwrite NL Guides
14031427
Playwrite NO
14041428
Playwrite NO Guides
14051429
Playwrite NZ
1430+
Playwrite NZ Basic
1431+
Playwrite NZ Basic Guides
14061432
Playwrite NZ Guides
14071433
Playwrite PE
14081434
Playwrite PE Guides
@@ -1424,6 +1450,7 @@ Playwrite VN
14241450
Playwrite VN Guides
14251451
Playwrite ZA
14261452
Playwrite ZA Guides
1453+
Pliant
14271454
Plus Jakarta Sans
14281455
Pochaevsk
14291456
Podkova
@@ -1484,6 +1511,7 @@ Ramaraja
14841511
Rambla
14851512
Rammetto One
14861513
Rampart One
1514+
Ramsina
14871515
Ranchers
14881516
Rancho
14891517
Ranga
@@ -1570,14 +1598,16 @@ Russo One
15701598
Ruthie
15711599
Ruwudu
15721600
Rye
1601+
SN Pro
1602+
STIX Two Math
15731603
STIX Two Text
15741604
SUSE
15751605
SUSE Mono
15761606
Sacramento
15771607
Sahitya
15781608
Sail
15791609
Saira
1580-
Saira Stencil One
1610+
Saira Stencil
15811611
Salsa
15821612
Sanchez
15831613
Sancreek
@@ -1705,6 +1735,7 @@ Stint Ultra Expanded
17051735
Stoke
17061736
Story Script
17071737
Strait
1738+
Strichpunkt Sans
17081739
Style Script
17091740
Stylish
17101741
Sue Ellen Francisco
@@ -1870,6 +1901,8 @@ Yuji Hentaigana Akebono
18701901
Yuji Mai
18711902
Yuji Syuku
18721903
Yusei Magic
1904+
Yuyu
1905+
Yuyu Short
18731906
ZCOOL KuaiLe
18741907
ZCOOL QingKe HuangYou
18751908
ZCOOL XiaoWei

‎packages/google_fonts/generator/generator.dart‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:mustache_template/mustache.dart';
1313
import 'fonts.pb.dart';
1414

1515
const String _generatedAllPartsFilePath = 'lib/src/google_fonts_all_parts.dart';
16+
const String _generatedLiteFilePath = 'lib/src/google_fonts_lite.dart';
1617
String _generatedPartFilePath(String part) => 'lib/src/google_fonts_parts/part_$part.dart';
1718
const String _familiesSupportedPath = 'generator/families_supported';
1819
const String _familiesDiffPath = 'generator/families_diff';
@@ -38,11 +39,11 @@ Future<void> main() async {
3839
File(_familiesDiffPath).writeAsStringSync(familiesDelta.markdownDiff());
3940
print(_success);
4041

41-
print('\nGenerating $_generatedAllPartsFilePath and part files...');
42+
print('\nGenerating $_generatedAllPartsFilePath, $_generatedLiteFilePath and part files...');
4243
_generateDartCode(fontDirectory);
4344
print(_success);
4445

45-
print('\nFormatting $_generatedAllPartsFilePath and part files...');
46+
print('\nFormatting $_generatedAllPartsFilePath, $_generatedLiteFilePath and part files...');
4647
await Process.run('dart', <String>['format', 'lib']);
4748
print(_success);
4849
}
@@ -291,6 +292,16 @@ void _generateDartCode(Directory fontDirectory) {
291292
_writeDartFile(_generatedPartFilePath(letter), renderedTemplate);
292293
});
293294

295+
// Generate lite file.
296+
final liteTemplate = Template(
297+
File('generator/google_fonts_lite.tmpl').readAsStringSync(),
298+
htmlEscapeValues: false,
299+
);
300+
final String renderedLiteTemplate = liteTemplate.renderString(
301+
<String, List<Map<String, dynamic>>>{'fontEntries': methods},
302+
);
303+
_writeDartFile(_generatedLiteFilePath, renderedLiteTemplate);
304+
294305
// Generate main file.
295306
final template = Template(
296307
File('generator/google_fonts.tmpl').readAsStringSync(),
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// GENERATED CODE - DO NOT EDIT
2+
3+
// Copyright 2013 The Flutter Authors
4+
// Use of this source code is governed by a BSD-style license that can be
5+
// found in the LICENSE file.
6+
7+
import 'dart:ui' as ui;
8+
9+
import 'package:flutter/material.dart';
10+
11+
import 'google_fonts_base.dart';
12+
import 'google_fonts_descriptor.dart';
13+
import 'google_fonts_variant.dart';
14+
15+
/// Provides a lightweight entry point to [TextStyle]s from Google Fonts.
16+
///
17+
/// Unlike the main `GoogleFonts` class which declares dedicated methods for
18+
/// each font, [GoogleFontsLite] provides dynamic font lookup via [getFont]
19+
/// and [fontsMap], allowing unused font methods to be tree-shaken by the compiler.
20+
abstract final class GoogleFontsLite {
21+
/// Map of all available Google Fonts families to their variant file descriptors.
22+
static final Map<String, Map<GoogleFontsVariant, GoogleFontsFile>> fontsMap = {
23+
{{#fontEntries}}
24+
'{{fontFamilyDisplay}}': {
25+
{{#fontUrls}}
26+
const GoogleFontsVariant(fontWeight: FontWeight.w{{variantWeight}}, fontStyle: FontStyle.{{variantStyle}},) : const GoogleFontsFile('{{hash}}', {{length}},),
27+
{{/fontUrls}}
28+
},
29+
{{/fontEntries}}
30+
};
31+
32+
/// Retrieve a font by family name.
33+
///
34+
/// Applies the given font family from Google Fonts to the given [textStyle]
35+
/// and returns the resulting [TextStyle].
36+
///
37+
/// Note: [fontFamily] is case-sensitive.
38+
static TextStyle getFont(String fontFamily, {
39+
TextStyle? textStyle,
40+
Color? color,
41+
Color? backgroundColor,
42+
double? fontSize,
43+
FontWeight? fontWeight,
44+
FontStyle? fontStyle,
45+
double? letterSpacing,
46+
double? wordSpacing,
47+
TextBaseline? textBaseline,
48+
double? height,
49+
Locale? locale,
50+
Paint? foreground,
51+
Paint? background,
52+
List<ui.Shadow>? shadows,
53+
List<ui.FontFeature>? fontFeatures,
54+
TextDecoration? decoration,
55+
Color? decorationColor,
56+
TextDecorationStyle? decorationStyle,
57+
double? decorationThickness,
58+
}) {
59+
if (!fontsMap.containsKey(fontFamily)) {
60+
throw Exception("No font family by name '$fontFamily' was found.");
61+
}
62+
63+
return googleFontsTextStyle(
64+
// googleFontsTextStyle expects the font identifier, which does not have spaces.
65+
// e.g. the identifier of "AR One Sans" is "AROneSans".
66+
fontFamily: fontFamily.replaceAll(' ', ''),
67+
fonts: fontsMap[fontFamily]!,
68+
textStyle: textStyle,
69+
color: color,
70+
backgroundColor: backgroundColor,
71+
fontSize: fontSize,
72+
fontWeight: fontWeight,
73+
fontStyle: fontStyle,
74+
letterSpacing: letterSpacing,
75+
wordSpacing: wordSpacing,
76+
textBaseline: textBaseline,
77+
height: height,
78+
locale: locale,
79+
foreground: foreground,
80+
background: background,
81+
shadows: shadows,
82+
fontFeatures: fontFeatures,
83+
decoration: decoration,
84+
decorationColor: decorationColor,
85+
decorationStyle: decorationStyle,
86+
decorationThickness: decorationThickness,
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)