FontFeature class
A feature tag and value that affect the selection of glyphs in a font.
Different fonts support different features. Consider using a tool such as wakamaifondue.com/ to examine your fonts to determine what features are available.
This example shows usage of several OpenType font features,
including Small Caps (selected manually using the "smcp" code),
old-style figures, fractional ligatures, and stylistic sets.
link
To create a local project with this code sample, run:
flutter create --sample=dart.dart_ui.FontFeature.1 mysample
import 'package:flutter/material.dart';
/// Flutter code sample for [FontFeature].
void main() => runApp(const ExampleApp());
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: ExampleWidget(),
);
}
}
final TextStyle titleStyle = TextStyle(
fontSize: 18,
fontFeatures: const <FontFeature>[FontFeature.enable('smcp')],
color: Colors.blueGrey[600],
);
class ExampleWidget extends StatelessWidget {
const ExampleWidget({super.key});
@override
Widget build(BuildContext context) {
// The Cardo, Milonga and Raleway Dots fonts can be downloaded from Google
// Fonts (https://www.google.com/fonts).
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Spacer(flex: 5),
Text('regular numbers have their place:', style: titleStyle),
const Text('The 1972 cup final was a 1-1 draw.',
style: TextStyle(
fontFamily: 'Cardo',
fontSize: 24,
)),
const Spacer(),
Text('but old-style figures blend well with lower case:',
style: titleStyle),
const Text('The 1972 cup final was a 1-1 draw.',
style: TextStyle(
fontFamily: 'Cardo',
fontSize: 24,
fontFeatures: <FontFeature>[
FontFeature.oldstyleFigures()
])),
const Spacer(),
const Divider(),
const Spacer(),
Text('fractions look better with a custom ligature:',
style: titleStyle),
const Text('Add 1/2 tsp of flour and stir.',
style: TextStyle(
fontFamily: 'Milonga',
fontSize: 24,
fontFeatures: <FontFeature>[
FontFeature.alternativeFractions()
])),
const Spacer(),
const Divider(),
const Spacer(),
Text('multiple stylistic sets in one font:', style: titleStyle),
const Text('Raleway Dots',
style: TextStyle(fontFamily: 'Raleway Dots', fontSize: 48)),
Text('Raleway Dots',
style: TextStyle(
fontFeatures: <FontFeature>[FontFeature.stylisticSet(1)],
fontFamily: 'Raleway Dots',
fontSize: 48,
)),
const Spacer(flex: 5),
],
),
),
);
}
}
Some fonts also support continuous font variations; see the FontVariation class.
See also:
-
en.wikipedia.org/wiki/List_of_typographic_features, Wikipedia's description of these typographic features.
-
docs.microsoft.com/en-us/typography/opentype/spec/featuretags, Microsoft's registry of these features.
Constructors
- FontFeature(String feature, [int value = 1])
-
Creates a FontFeature object, which can be added to a TextStyle to
change how the engine selects glyphs when rendering text.
const
- FontFeature.alternative(int value)
-
Access alternative glyphs. (
aalt
)const - FontFeature.alternativeFractions()
-
Use alternative ligatures to represent fractions. (
afrc
)const - FontFeature.caseSensitiveForms()
-
Enable case-sensitive forms. (
case
)const - FontFeature.characterVariant(int value)
-
Select a character variant. (
cv01
throughcv99
)factory - FontFeature.contextualAlternates()
-
Enable contextual alternates. (
calt
)const - FontFeature.denominator()
-
Display digits as denominators. (
dnom
)const - FontFeature.disable(String feature)
-
Create a FontFeature object that disables the feature with the given tag.
const
- FontFeature.enable(String feature)
-
Create a FontFeature object that enables the feature with the given tag.
const
- FontFeature.fractions()
-
Use ligatures to represent fractions. (
afrc
)const - FontFeature.historicalForms()
-
Use historical forms. (
hist
)const - FontFeature.historicalLigatures()
-
Use historical ligatures. (
hlig
)const - FontFeature.liningFigures()
-
Use lining figures. (
lnum
)const - FontFeature.localeAware({bool enable = true})
-
Use locale-specific glyphs. (
locl
)const - FontFeature.notationalForms([int value = 1])
-
Display alternative glyphs for numerals (alternate annotation forms). (
nalt
)const - FontFeature.numerators()
-
Display digits as numerators. (
numr
)const - FontFeature.oldstyleFigures()
-
Use old style figures. (
onum
)const - FontFeature.ordinalForms()
-
Use ordinal forms for alphabetic glyphs. (
ordn
)const - FontFeature.proportionalFigures()
-
Use proportional (varying width) figures. (
pnum
)const - FontFeature.randomize()
-
Randomize the alternate forms used in text. (
rand
)const - FontFeature.scientificInferiors()
-
Use scientific inferiors. (
sinf
)const - FontFeature.slashedZero()
-
Use the slashed zero. (
zero
)const - FontFeature.stylisticAlternates()
-
Enable stylistic alternates. (
salt
)const - FontFeature.stylisticSet(int value)
-
Select a stylistic set. (
ss01
throughss20
)factory - FontFeature.subscripts()
-
Enable subscripts. (
subs
)const - FontFeature.superscripts()
-
Enable superscripts. (
sups
)const - FontFeature.swash([int value = 1])
-
Enable swash glyphs. (
swsh
)const - FontFeature.tabularFigures()
-
Use tabular (monospace) figures. (
tnum
)const
Properties
- feature → String
-
The tag that identifies the effect of this feature. Must consist of 4
ASCII characters (typically lowercase letters).
final
- hashCode → int
-
The hash code for this object.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value → int
-
The value assigned to this feature.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override