fontgraphy 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 43

fontgraphy #

WIP

The fontgraphy package for Flutter allows you to easily use any of the fonts (and their variants) from fontgraphy.ir in your Flutter app.

Getting Started #

With the fontgraphy package, .ttf or .otf files do not need to be stored in your assets folder and mapped in the pubspec. Instead, they can be fetched once via http at runtime, and cached in the app's file system. This is ideal for development, and can be the preferred behavior for production apps that are looking to reduce the app bundle size. Still, you may at any time choose to include the font file in the assets, and the Google Fonts package will prioritize pre-bundled files over http fetching. Because of this, the Google Fonts package allows developers to choose between pre-bundling the fonts and loading them over http, while using the same API.

For example, say you want to use the Lato font from Google Fonts in your Flutter app.

First, add the fontgraphy package to your pubspec dependencies.

To import Fontgraphy:

import 'package:fontgraphy/fontgraphy.dart';

To use Fontgraphy with the default TextStyle:

Text(
  'This is Google Fonts',
  style: Fontgraphy.lato(),
),

Or, if you want to load the font dynamically:

Text(
  'This is Google Fonts',
  style: Fontgraphy.getFont('Lato'),
),

To use Fontgraphy with an existing TextStyle:

Text(
  'This is Google Fonts',
  style: Fontgraphy.lato(
    textStyle: TextStyle(color: Colors.blue, letterSpacing: .5),
  ),
),

or

Text(
  'This is Google Fonts',
  style: Fontgraphy.lato(textStyle: Theme.of(context).textTheme.display1),
),

To override the fontSize, fontWeight, or fontStyle:

Text(
  'This is Google Fonts',
  style: Fontgraphy.lato(
    textStyle: Theme.of(context).textTheme.display1,
    fontSize: 48,
    fontWeight: FontWeight.w700,
    fontStyle: FontStyle.italic,
  ),
),

You can also use Fontgraphy.latoTextTheme() to make or modify an entire text theme to use the "Lato" font.

MaterialApp(
  theme: ThemeData(
    textTheme: Fontgraphy.latoTextTheme(
      Theme.of(context).textTheme,
    ),
  ),
);

Or, if you want a TextTheme where a couple of styles should use a different font:

final textTheme = Theme.of(context).textTheme;

MaterialApp(
  theme: ThemeData(
    textTheme: Fontgraphy.latoTextTheme(textTheme).copyWith(
      body1: Fontgraphy.oswald(textStyle: textTheme.body1),
    ),
  ),
);

Bundling font files in your application's assets #

The fontgraphy package will automatically use matching font files in your pubspec.yaml's assets (rather than fetching them at runtime via HTTP). Once you've settled on the fonts you want to use:

  1. Download the font files from https://fonts.google.com. You only need to download the weights and styles you are using for any given family. Italic styles will include Italic in the filename. Font weights map to file names as follows:
{
  FontWeight.w100: 'Thin',
  FontWeight.w200: 'ExtraLight',
  FontWeight.w300: 'Light',
  FontWeight.w400: 'Regular',
  FontWeight.w500: 'Medium',
  FontWeight.w600: 'SemiBold',
  FontWeight.w700: 'Bold',
  FontWeight.w800: 'ExtraBold',
  FontWeight.w900: 'Black',
}
  1. Move those fonts to a top-level app directory (e.g. fontgraphy).

  1. Ensure that you have listed the folder (e.g. fontgraphy/) in your pubspec.yaml under assets.

Note: Since these files are listed as assets, there is no need to list them in the fonts section of the pubspec.yaml. This can be done because the files are consistently named from the Google Fonts API (so be sure not to rename them!)

Licensing Fonts #

The fonts on fonts.google.com include license files for each font. For example, the Lato font comes with an OFL.txt file.

Once you've decided on the fonts you want in your published app, you should add the appropriate licenses to your flutter app's LicenseRegistry.

For example:

void main() {
  LicenseRegistry.addLicense(() async* {
    final license = await rootBundle.loadString('fontgraphy/OFL.txt');
    yield LicenseEntryWithLineBreaks(['fontgraphy'], license);
  });
  
  runApp(...);
}

[1.0.0] - 2020-04-22 #

[0.7.0] - 2020-04-22 #

  • Added the following variable fonts:
    • Bellota
    • Bellota Text
    • Comic Neue
    • Crimson Pro
    • Fira Code
    • Gotu
    • Hepta Slab
    • Inria Sans
    • Inter
    • Literata
    • Manrope
    • Markazi Text
    • Public Sans
    • Sen
    • Spartan
    • Viaoda Libre

[0.6.2] - 2020-04-17 #

  • Clean up code.

[0.6.1] - 2020-04-17 #

  • Memoize asset manifest.

[0.6.0] - 2020-04-16 #

  • Rename config.allowHttp to config.allowRuntimeFetching.

[0.5.0] - 2020-04-14 #

  • Use more accurate naming algorithm for Fontgraphy.foo and Fontgraphy.fooTextTheme.

[0.4.3] - 2020-04-14 #

  • Add Fontgraphy.getTextTheme(...) method for dynamically getting a text theme from a font name.

[0.4.2] - 2020-04-14 #

  • Change loadFontIfNecessary to only follow through once per unique family when called in parallel.

[0.4.1] - 2020-04-13 #

  • Update README to include instructions for how to include licenses for fonts.

[0.4.0] - 2020-03-20 #

  • Added ability to load fonts dynamically through getFont method.
  • Added asMap method which returns a map with font family names mapped to methods.

[0.3.10] - 2020-03-18 #

Update Fonts API url in generator to add in missing fonts.

[0.3.9] - 2020-02-13 #

  • Fix path_provider usage for web

[0.3.8] - 2020-02-10 #

  • Add byte length and checksum verification for font files downloaded.

[0.3.7] - 2020-02-03 #

  • Fix asset font loading bug
  • Update asset font README instructions

[0.3.6] - 2020-01-31 #

  • Add a config to the Fontgraphy class with an allowHttp option.

[0.3.5] - 2020-01-23 #

  • Update generator to get most up-to-date urls from fonts.google.com.
  • Add CONTRIBUTING.md.

[0.3.4] - 2020-01-23 #

  • Store downloaded font files in device's support directory instead of documents directory.

[0.3.3] - 2020-01-22 #

  • Update font URLs to https to properly support web.

[0.3.2] - 2019-01-07 #

  • README image path fixes.

[0.3.1] - 2019-01-07 #

  • README fixes.

[0.3.0] - 2019-01-07 #

  • Added dartdocs to every public method in the google fonts package.

  • Added the ability to include font files in pubspec assets (see README).

[0.2.0] - 2019-12-12 #

  • Updated to include all fonts currently on fonts.google.com.

[0.1.1] - 2019-12-10 #

Generated method names changed back to pre 0.1.0 (breaking change). #

For example,

Fontgraphy.latoTextStyle(...) is now Fontgraphy.lato(...).

Text theme parameters are now optional positional parameters (breaking change). #

For example,

Fontgraphy.latoTextTheme(textTheme: TextTheme(...)) is now Fontgraphy.latoTextTheme(TextTheme(...)).

[0.1.0] - 2019-12-06 #

Generated method names changed (breaking change). #

For example,

Fontgraphy.lato(...) is now Fontgraphy.latoTextStyle(...).

Text theme support #

Every font family now also has a TextTheme method.

For example, the Lato font now has Fontgraphy.latoTextStyle() and Fontgraphy.latoTextTheme().

See README for more examples.

Other cleanups #

Refactored implementation, updated READMEs, and usage docs.

[0.0.8] - 2019-12-04 #

Internal refactor and added tests.

[0.0.7] - 2019-12-04 #

BETA support for Flutter web.

[0.0.6] - 2019-12-04 #

Minor updates to README.

[0.0.5] - 2019-11-20 #

Mark as experimental in more places.

[0.0.4] - 2019-11-20 #

Add pubspec instructions to README.

[0.0.3] - 2019-11-20 #

Fix homepage and main gif.

[0.0.2] - 2019-11-20 #

Update README with import instructions.

[0.0.1] - 2019-11-15 #

The initial release supports all 960 fonts and variants from fonts.google.com.

ttf files are downloaded via http on demand, and saved to local disk so that they can be loaded without making another http request for future font requests. Fonts are loaded asynchronously through the font loader and Text widgets that use them are refreshed when they are ready.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:fontgraphy/fontgraphy.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    final TextStyle display1 = Theme.of(context).textTheme.display1;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'این یک متن آزمایشی می باشد.',
              style: Fontgraphy.titrzebr(fontSize: 25),
              textDirection: TextDirection.rtl,
            ),
            Text(
              '$_counter'
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  fontgraphy: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:fontgraphy/fontgraphy.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
43
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:fontgraphy/fontgraphy.dart that imports:
  • package:fontgraphy/src/fontgraphy_base.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform ios

Because:

  • package:fontgraphy/fontgraphy.dart that imports:
  • package:fontgraphy/src/fontgraphy_base.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform macos

Because:

  • package:fontgraphy/fontgraphy.dart that imports:
  • package:fontgraphy/src/fontgraphy_base.dart that imports:
  • package:path_provider/path_provider.dart that imports:
  • package:path_provider_linux/path_provider_linux.dart that declares support for platforms: linux

Package does not support Flutter platform web

Because:

  • package:fontgraphy/fontgraphy.dart that imports:
  • package:fontgraphy/src/fontgraphy_base.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: android, ios, linux, macos

Package does not support Flutter platform windows

Because:

  • package:fontgraphy/fontgraphy.dart that imports:
  • package:fontgraphy/src/fontgraphy_base.dart that imports:
  • package:path_provider/path_provider.dart that declares support for platforms: android, ios, linux, macos

Package not compatible with SDK dart

Because:

  • fontgraphy that is a package requiring null.

Health suggestions

Fix lib/fontgraphy.dart. (-25 points)

Analysis of lib/fontgraphy.dart reported 900 hints, including:

line 308 col 7: 'display4' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is headline1. This feature was deprecated after v1.13.8..

line 308 col 55: 'display4' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is headline1. This feature was deprecated after v1.13.8..

line 309 col 7: 'display3' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is headline2. This feature was deprecated after v1.13.8..

line 309 col 55: 'display3' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is headline2. This feature was deprecated after v1.13.8..

line 310 col 7: 'display2' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is headline3. This feature was deprecated after v1.13.8..

Fix lib/src/fontgraphy_base.dart. (-0.50 points)

Analysis of lib/src/fontgraphy_base.dart reported 1 hint:

line 9 col 8: Unused import: 'package:crypto/crypto.dart'.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
crypto ^2.1.3 2.1.5
flutter 0.0.0
http ^0.12.0+2 0.12.1
path_provider ^1.4.0 1.6.11
pedantic ^1.8.0 1.9.0 1.9.2
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
file 5.2.1
http_parser 3.1.4
intl 0.16.1
meta 1.1.8 1.2.2
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_macos 0.0.4+3
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
console ^3.1.0
flutter_test
mockito ^4.1.1
mustache ^1.1.1
protobuf ^1.0.1
protoc_plugin any