Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Make it possible to obtain FontWeight integer value #35183

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,40 @@ enum FontStyle {

/// The thickness of the glyphs used to draw the text
class FontWeight {
const FontWeight._(this.index);
const FontWeight._(this.index, this.value);

/// The encoded integer value of this font weight.
final int index;

/// The thickness value of this font weight.
final int value;

/// Thin, the least thick
static const FontWeight w100 = FontWeight._(0);
static const FontWeight w100 = FontWeight._(0, 100);

/// Extra-light
static const FontWeight w200 = FontWeight._(1);
static const FontWeight w200 = FontWeight._(1, 200);

/// Light
static const FontWeight w300 = FontWeight._(2);
static const FontWeight w300 = FontWeight._(2, 300);

/// Normal / regular / plain
static const FontWeight w400 = FontWeight._(3);
static const FontWeight w400 = FontWeight._(3, 400);

/// Medium
static const FontWeight w500 = FontWeight._(4);
static const FontWeight w500 = FontWeight._(4, 500);

/// Semi-bold
static const FontWeight w600 = FontWeight._(5);
static const FontWeight w600 = FontWeight._(5, 600);

/// Bold
static const FontWeight w700 = FontWeight._(6);
static const FontWeight w700 = FontWeight._(6, 700);

/// Extra-bold
static const FontWeight w800 = FontWeight._(7);
static const FontWeight w800 = FontWeight._(7, 800);

/// Black, the most thick
static const FontWeight w900 = FontWeight._(8);
static const FontWeight w900 = FontWeight._(8, 900);

/// The default font weight.
static const FontWeight normal = w400;
Expand Down
21 changes: 11 additions & 10 deletions lib/web_ui/lib/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ enum PlaceholderAlignment {
}

class FontWeight {
const FontWeight._(this.index);
const FontWeight._(this.index, this.value);
final int index;
static const FontWeight w100 = FontWeight._(0);
static const FontWeight w200 = FontWeight._(1);
static const FontWeight w300 = FontWeight._(2);
static const FontWeight w400 = FontWeight._(3);
static const FontWeight w500 = FontWeight._(4);
static const FontWeight w600 = FontWeight._(5);
static const FontWeight w700 = FontWeight._(6);
static const FontWeight w800 = FontWeight._(7);
static const FontWeight w900 = FontWeight._(8);
final int value;
static const FontWeight w100 = FontWeight._(0, 100);
static const FontWeight w200 = FontWeight._(1, 200);
static const FontWeight w300 = FontWeight._(2, 300);
static const FontWeight w400 = FontWeight._(3, 400);
static const FontWeight w500 = FontWeight._(4, 500);
static const FontWeight w600 = FontWeight._(5, 600);
static const FontWeight w700 = FontWeight._(6, 700);
static const FontWeight w800 = FontWeight._(7, 800);
static const FontWeight w900 = FontWeight._(8, 900);
static const FontWeight normal = w400;
static const FontWeight bold = w700;
static const List<FontWeight> values = <FontWeight>[
Expand Down
12 changes: 12 additions & 0 deletions lib/web_ui/test/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,16 @@ Future<void> testMain() async {
equals('hello'));
});
});

test('FontWeights have the correct value', () {
expect(FontWeight.w100.value, 100);
expect(FontWeight.w200.value, 200);
expect(FontWeight.w300.value, 300);
expect(FontWeight.w400.value, 400);
expect(FontWeight.w500.value, 500);
expect(FontWeight.w600.value, 600);
expect(FontWeight.w700.value, 700);
expect(FontWeight.w800.value, 800);
expect(FontWeight.w900.value, 900);
});
}
16 changes: 14 additions & 2 deletions testing/dart/text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Future<Uint8List> readFile(String fileName) async {
return file.readAsBytes();
}

void testFontWeightLerp() {
void testFontWeight() {
test('FontWeight.lerp works with non-null values', () {
expect(FontWeight.lerp(FontWeight.w400, FontWeight.w600, .5), equals(FontWeight.w500));
});
Expand All @@ -35,6 +35,18 @@ void testFontWeightLerp() {
test('FontWeight.lerp returns FontWeight.w400 if b is null', () {
expect(FontWeight.lerp(FontWeight.w400, null, 1), equals(FontWeight.w400));
});

test('FontWeights have the correct value', () {
expect(FontWeight.w100.value, 100);
expect(FontWeight.w200.value, 200);
expect(FontWeight.w300.value, 300);
expect(FontWeight.w400.value, 400);
expect(FontWeight.w500.value, 500);
expect(FontWeight.w600.value, 600);
expect(FontWeight.w700.value, 700);
expect(FontWeight.w800.value, 800);
expect(FontWeight.w900.value, 900);
});
}

void testParagraphStyle() {
Expand Down Expand Up @@ -277,7 +289,7 @@ void testFontVariation() {
}

void main() {
testFontWeightLerp();
testFontWeight();
testParagraphStyle();
testTextStyle();
testTextHeightBehavior();
Expand Down