Skip to content

Commit 524e9ce

Browse files
committed
Move font config into fonts key, move generated package dir to .fontsource.
1 parent 7c4db0f commit 524e9ce

File tree

13 files changed

+112
-108
lines changed

13 files changed

+112
-108
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
example/* linguist-vendored
1+
example/** linguist-vendored

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.2.0
2+
3+
- Move font configuration into `fonts` key in fontsource config.
4+
- Move generated package directory to `.fontsource`.
5+
- Fix current working directory resolving incorrectly.
6+
17
## 0.1.1
28

39
- Fix version key not accepting "latest".

example/.fontsource/pubspec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: fontsource_gen
2+
3+
environment:
4+
sdk: '>=2.15.1 <3.0.0'
5+
flutter: '>=1.17.0'
6+
7+
flutter:
8+
fonts:
9+
- family: Alex Brush
10+
fonts:
11+
- asset: ../.dart_tool/fontsource/alex-brush/latin-400-normal.ttf
12+
weight: 400
13+
- asset: ../.dart_tool/fontsource/alex-brush/latin-ext-400-normal.ttf
14+
weight: 400
15+
- asset: ../.dart_tool/fontsource/alex-brush/vietnamese-400-normal.ttf
16+
weight: 400

example/lib/main.dart

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:fontsource/fontsource.dart';
33

4-
const title = 'Fontsource Example';
5-
64
void main() {
75
runApp(const MyApp());
86
}
@@ -13,60 +11,20 @@ class MyApp extends StatelessWidget {
1311
@override
1412
Widget build(BuildContext context) {
1513
return MaterialApp(
16-
title: title,
17-
theme: ThemeData(
18-
primarySwatch: Colors.blue,
19-
),
20-
home: const MyHomePage(title: title),
21-
);
22-
}
23-
}
24-
25-
class MyHomePage extends StatefulWidget {
26-
const MyHomePage({Key? key, required this.title}) : super(key: key);
27-
28-
final String title;
29-
30-
@override
31-
State<MyHomePage> createState() => _MyHomePageState();
32-
}
33-
34-
class _MyHomePageState extends State<MyHomePage> {
35-
int _counter = 0;
36-
37-
void _incrementCounter() {
38-
setState(() {
39-
_counter++;
40-
});
41-
}
42-
43-
@override
44-
Widget build(BuildContext context) {
45-
return Scaffold(
46-
appBar: AppBar(
47-
title: Text(widget.title),
48-
),
49-
body: Center(
50-
child: Column(
51-
mainAxisAlignment: MainAxisAlignment.center,
52-
children: <Widget>[
53-
const Text(
54-
'You have pushed the button this many times:',
55-
style:
56-
FontsourceTextStyle(fontFamily: 'Alex Brush', fontSize: 30),
57-
),
58-
Text(
59-
'$_counter',
60-
style: Theme.of(context).textTheme.headline4,
61-
),
62-
],
14+
home: Scaffold(
15+
body: Center(
16+
child: Column(
17+
mainAxisAlignment: MainAxisAlignment.center,
18+
children: const <Widget>[
19+
Text(
20+
'Sphinx of black quartz, judge my vow.',
21+
style:
22+
FontsourceTextStyle(fontFamily: 'Alex Brush', fontSize: 40),
23+
),
24+
],
25+
),
6326
),
6427
),
65-
floatingActionButton: FloatingActionButton(
66-
onPressed: _incrementCounter,
67-
tooltip: 'Increment',
68-
child: const Icon(Icons.add),
69-
),
7028
);
7129
}
7230
}

example/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ packages:
8080
path: ".."
8181
relative: true
8282
source: path
83-
version: "0.1.1"
83+
version: "0.2.0"
8484
fontsource_gen:
8585
dependency: "direct main"
8686
description:
87-
path: ".dart_tool/fontsource"
87+
path: ".fontsource"
8888
relative: true
8989
source: path
9090
version: "0.0.0"

example/pubspec.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ environment:
88
dependencies:
99
flutter:
1010
sdk: flutter
11-
1211
cupertino_icons: ^1.0.2
1312
fontsource:
1413
path: ../
1514
fontsource_gen:
16-
path: .dart_tool/fontsource
15+
path: .fontsource
1716

1817
dev_dependencies:
1918
flutter_test:
@@ -24,7 +23,8 @@ flutter:
2423
uses-material-design: true
2524

2625
fontsource:
27-
alex-brush:
28-
subsets: all
29-
weights: [400]
30-
styles: [normal]
26+
fonts:
27+
alex-brush:
28+
subsets: all
29+
weights: [400]
30+
styles: [normal]

lib/src/api/fetch_font_file.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Future<Uint8List> fetchFontFile(
3333
break;
3434
default:
3535
}
36-
var response = await http.get(Uri.parse(
36+
final response = await http.get(Uri.parse(
3737
'$apiUrl/v1/fonts/$fontId/$subset-$weight-$style.$ext${version == null ? '' : '?version=$version'}'));
3838

3939
return response.bodyBytes;

lib/src/api/list_font_metadata.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Future<List<FontMetadata>> listFontMetadata(
7676
String? category,
7777
String? version,
7878
String? type}) async {
79-
var query = {
79+
final query = {
8080
'id': id,
8181
'family': family,
8282
'subsets': subsets,
@@ -90,7 +90,7 @@ Future<List<FontMetadata>> listFontMetadata(
9090
'type': type,
9191
};
9292
query.removeWhere((_, value) => value == null);
93-
var response = await http
93+
final response = await http
9494
.get(Uri.parse('$apiUrl/v1/fonts?${Uri(queryParameters: query).query}'));
9595
List<dynamic> fonts = jsonDecode(response.body);
9696

lib/src/api/list_fonts.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'constants.dart';
66

77
/// Returns a list of font ids.
88
Future<List<String>> listFonts() async {
9-
var response = await http.get(Uri.parse('$apiUrl/fontlist'));
9+
final response = await http.get(Uri.parse('$apiUrl/fontlist'));
1010
Map<String, String> fontList =
1111
jsonDecode(response.body).cast<String, String>();
1212

lib/src/cli/config.dart

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'dart:io';
22
import 'package:fontsource/api.dart';
33
import 'package:yaml/yaml.dart';
44

5+
import '../utils.dart';
6+
57
class FontConfig {
68
List<String> subsets;
79
List<int> weights;
@@ -16,12 +18,19 @@ class FontConfig {
1618
}
1719
}
1820

19-
typedef FontsourceConfig = Map<String, FontConfig>;
21+
class FontsourceConfig {
22+
Map<String, FontConfig> fonts;
23+
FontsourceConfig(this.fonts);
24+
@override
25+
String toString() {
26+
return '{fonts: $fonts}';
27+
}
28+
}
2029

2130
Future<FontsourceConfig> getConfig() async {
2231
dynamic configYaml;
2332
try {
24-
File fontsourceFile = File('fontsource.yaml');
33+
File fontsourceFile = File(cwdJoin('fontsource.yaml'));
2534
if (fontsourceFile.existsSync()) {
2635
String fontsourceFileString = fontsourceFile.readAsStringSync();
2736

@@ -30,7 +39,7 @@ Future<FontsourceConfig> getConfig() async {
3039
}
3140
}
3241
if (configYaml == null) {
33-
File pubspecFile = File('pubspec.yaml');
42+
File pubspecFile = File(cwdJoin('pubspec.yaml'));
3443

3544
if (pubspecFile.existsSync()) {
3645
dynamic pubspecYaml = loadYaml(pubspecFile.readAsStringSync());
@@ -45,9 +54,10 @@ Future<FontsourceConfig> getConfig() async {
4554
throw Exception('Fontsource config not found.');
4655
}
4756

48-
Map configDynamicMap = configYaml;
49-
FontsourceConfig config = {};
50-
await Future.wait(configDynamicMap.keys.map((id) async {
57+
Map configMap = configYaml;
58+
Map fontsMap = configMap['fonts'] ?? {};
59+
final config = FontsourceConfig({});
60+
await Future.wait(fontsMap.keys.map((id) async {
5161
FontMetadata metadata;
5262
try {
5363
metadata = (await listFontMetadata(id: id))[0];
@@ -58,11 +68,10 @@ Future<FontsourceConfig> getConfig() async {
5868
List<String> subsets;
5969
List<int> weights;
6070
List<String> styles;
61-
if (configDynamicMap[id]?['subsets'] == null ||
62-
configDynamicMap[id]['subsets'] == 'all') {
71+
if (fontsMap[id]?['subsets'] == null || fontsMap[id]['subsets'] == 'all') {
6372
subsets = metadata.subsets;
6473
} else {
65-
subsets = (configDynamicMap[id]['subsets'] as YamlList)
74+
subsets = (fontsMap[id]['subsets'] as YamlList)
6675
.map((subset) => subset as String)
6776
.toList();
6877
for (var subset in subsets) {
@@ -72,11 +81,10 @@ Future<FontsourceConfig> getConfig() async {
7281
}
7382
}
7483
}
75-
if (configDynamicMap[id]?['weights'] == null ||
76-
configDynamicMap[id]['weights'] == 'all') {
84+
if (fontsMap[id]?['weights'] == null || fontsMap[id]['weights'] == 'all') {
7785
weights = metadata.weights;
7886
} else {
79-
weights = (configDynamicMap[id]['weights'] as YamlList)
87+
weights = (fontsMap[id]['weights'] as YamlList)
8088
.map((weight) => weight as int)
8189
.toList();
8290
for (var weight in weights) {
@@ -86,11 +94,10 @@ Future<FontsourceConfig> getConfig() async {
8694
}
8795
}
8896
}
89-
if (configDynamicMap[id]?['weights'] == null ||
90-
configDynamicMap[id]['styles'] == 'all') {
97+
if (fontsMap[id]?['weights'] == null || fontsMap[id]['styles'] == 'all') {
9198
styles = metadata.styles;
9299
} else {
93-
styles = (configDynamicMap[id]['styles'] as YamlList)
100+
styles = (fontsMap[id]['styles'] as YamlList)
94101
.map((style) => style as String)
95102
.toList();
96103
for (var style in styles) {
@@ -99,10 +106,10 @@ Future<FontsourceConfig> getConfig() async {
99106
}
100107
}
101108
}
102-
String? version = configDynamicMap[id]?['version'];
109+
String? version = fontsMap[id]?['version'];
103110
if (version == 'latest') version = null;
104-
config[id] = FontConfig(
105-
subsets, weights, styles, metadata, configDynamicMap[id]?['version']);
111+
config.fonts[id] = FontConfig(
112+
subsets, weights, styles, metadata, fontsMap[id]?['version']);
106113
}));
107114

108115
return config;

0 commit comments

Comments
 (0)