-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[google_fonts] Add WOFF and WOFF2 font format support for web #10703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,7 @@ | ||||||||||||||||||||||||||||||||||
| ## 6.4.0 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - Adds support for WOFF2 and WOFF font formats on web platforms for improved performance and smaller bundle sizes. | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| class GoogleFontsFile { | |
| /// Creates a font file descriptor with expected hash and length validation. | |
| /// | |
| /// 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); | |
| /// The expected hash of the font file for validation. | |
| final String expectedFileHash; | |
| /// The expected length in bytes of the font file. | |
| final int expectedLength; | |
| /// The URL from which the font file can be downloaded. | |
| String get url => 'https://fonts.gstatic.com/s/a/$expectedFileHash.ttf'; | |
| } |
In any case, please also create an issue to associate with this PR and any future work regarding HTTP fetching of woff2 files.
I'll do that and add a link to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do that and add a link to this PR.
Done: flutter/flutter#180998.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:google_fonts/src/google_fonts_base.dart'; | ||
| import 'package:google_fonts/src/google_fonts_family_with_variant.dart'; | ||
| import 'package:google_fonts/src/google_fonts_variant.dart'; | ||
|
|
||
| void main() { | ||
| group('findFamilyWithVariantAssetPath', () { | ||
| const familyWithVariant = GoogleFontsFamilyWithVariant( | ||
| family: 'Roboto', | ||
| googleFontsVariant: GoogleFontsVariant( | ||
| fontWeight: FontWeight.w400, | ||
| fontStyle: FontStyle.normal, | ||
| ), | ||
| ); | ||
|
|
||
| group('common behavior', () { | ||
| for (final isWeb in [true, false]) { | ||
| test('returns null when manifestValues is null (web: $isWeb)', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| null, | ||
| isWeb: isWeb, | ||
| ); | ||
| expect(result, isNull); | ||
| }); | ||
|
|
||
| test('returns null when manifestValues is empty (web: $isWeb)', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>[], | ||
| isWeb: isWeb, | ||
| ); | ||
| expect(result, isNull); | ||
| }); | ||
|
|
||
| test('returns null when font family does not match (web: $isWeb)', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>[ | ||
| 'google_fonts/Lato-Regular.ttf', | ||
| 'google_fonts/OpenSans-Regular.ttf', | ||
| ], | ||
| isWeb: isWeb, | ||
| ); | ||
| expect(result, isNull); | ||
| }); | ||
|
|
||
| test('returns null when variant does not match (web: $isWeb)', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>[ | ||
| 'google_fonts/Roboto-Bold.ttf', | ||
| 'google_fonts/Roboto-Italic.ttf', | ||
| ], | ||
| isWeb: isWeb, | ||
| ); | ||
| expect(result, isNull); | ||
| }); | ||
|
|
||
| test('matches correct variant with multiple fonts (web: $isWeb)', () { | ||
| const boldItalicVariant = GoogleFontsFamilyWithVariant( | ||
| family: 'Roboto', | ||
| googleFontsVariant: GoogleFontsVariant( | ||
| fontWeight: FontWeight.w700, | ||
| fontStyle: FontStyle.italic, | ||
| ), | ||
| ); | ||
| final String? result = | ||
| findFamilyWithVariantAssetPath(boldItalicVariant, <String>[ | ||
| 'google_fonts/Roboto-Regular.ttf', | ||
| 'google_fonts/Roboto-Bold.ttf', | ||
| 'google_fonts/Roboto-BoldItalic.ttf', | ||
| 'google_fonts/Roboto-Italic.ttf', | ||
| ], isWeb: isWeb); | ||
| expect(result, equals('google_fonts/Roboto-BoldItalic.ttf')); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| group('on web', () { | ||
| test('supports woff2 format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>['google_fonts/Roboto-Regular.woff2'], | ||
| isWeb: true, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.woff2')); | ||
| }); | ||
|
|
||
| test('supports woff format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>['google_fonts/Roboto-Regular.woff'], | ||
| isWeb: true, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.woff')); | ||
| }); | ||
|
|
||
| test('supports ttf format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>['google_fonts/Roboto-Regular.ttf'], | ||
| isWeb: true, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.ttf')); | ||
| }); | ||
|
|
||
| test('supports otf format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>['google_fonts/Roboto-Regular.otf'], | ||
| isWeb: true, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.otf')); | ||
| }); | ||
|
|
||
| test('returns first matching asset in manifest order', () { | ||
| // Returns the first asset that matches, regardless of file type | ||
| final String? result = | ||
| findFamilyWithVariantAssetPath(familyWithVariant, <String>[ | ||
| 'google_fonts/Roboto-Regular.ttf', | ||
| 'google_fonts/Roboto-Regular.woff2', | ||
| 'google_fonts/Roboto-Regular.woff', | ||
| ], isWeb: true); | ||
| expect(result, equals('google_fonts/Roboto-Regular.ttf')); | ||
| }); | ||
|
|
||
| test('ignores unsupported file extensions', () { | ||
| final String? result = | ||
| findFamilyWithVariantAssetPath(familyWithVariant, <String>[ | ||
| 'google_fonts/Roboto-Regular.eot', | ||
| 'google_fonts/Roboto-Regular.svg', | ||
| 'google_fonts/Roboto-Regular.woff2', | ||
| ], isWeb: true); | ||
| expect(result, equals('google_fonts/Roboto-Regular.woff2')); | ||
| }); | ||
| }); | ||
|
|
||
| group('on non-web', () { | ||
| test('supports ttf format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>['google_fonts/Roboto-Regular.ttf'], | ||
| isWeb: false, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.ttf')); | ||
| }); | ||
|
|
||
| test('supports otf format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>['google_fonts/Roboto-Regular.otf'], | ||
| isWeb: false, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.otf')); | ||
| }); | ||
|
|
||
| test('does not select woff2 format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>[ | ||
| 'google_fonts/Roboto-Regular.woff2', | ||
| 'google_fonts/Roboto-Regular.ttf', | ||
| ], | ||
| isWeb: false, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.ttf')); | ||
| }); | ||
|
|
||
| test('does not select woff format', () { | ||
| final String? result = findFamilyWithVariantAssetPath( | ||
| familyWithVariant, | ||
| <String>[ | ||
| 'google_fonts/Roboto-Regular.woff', | ||
| 'google_fonts/Roboto-Regular.otf', | ||
| ], | ||
| isWeb: false, | ||
| ); | ||
| expect(result, equals('google_fonts/Roboto-Regular.otf')); | ||
| }); | ||
| }); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please pull updates and correct version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 1bac768.