Skip to content

Commit

Permalink
FontWeight
Browse files Browse the repository at this point in the history
  • Loading branch information
LongCatIsLooong committed Aug 22, 2023
1 parent fd52779 commit 476e6fb
Showing 1 changed file with 60 additions and 38 deletions.
98 changes: 60 additions & 38 deletions lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,47 @@ enum FontStyle {
}

/// The thickness of the glyphs used to draw the text
class FontWeight {
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;

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

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

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

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

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

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

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

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

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

// Users are not allowed to specify a `FontWeight` outside of the w100-w900
// range but j
const FontWeight._(this.value);

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

/// The default font weight.
static const FontWeight normal = w400;

/// A commonly used font weight that is heavier than normal.
static const FontWeight bold = w700;

/// A list of all the font weights.
static const List<FontWeight> values = <FontWeight>[
w100, w200, w300, w400, w500, w600, w700, w800, w900
];

/// Linearly interpolates between two font weights.
///
/// Rather than using fractional weights, the interpolation rounds to the
Expand Down Expand Up @@ -87,21 +81,6 @@ class FontWeight {
}
return values[_lerpInt((a ?? normal).index, (b ?? normal).index, t).round().clamp(0, 8)];
}

@override
String toString() {
return const <int, String>{
0: 'FontWeight.w100',
1: 'FontWeight.w200',
2: 'FontWeight.w300',
3: 'FontWeight.w400',
4: 'FontWeight.w500',
5: 'FontWeight.w600',
6: 'FontWeight.w700',
7: 'FontWeight.w800',
8: 'FontWeight.w900',
}[index]!;
}
}

/// A feature tag and value that affect the selection of glyphs in a font.
Expand Down Expand Up @@ -2575,6 +2554,18 @@ class LineMetrics {
required this.lineNumber,
});

LineMetrics._(
this.hardBreak,
this.ascent,
this.descent,
this.unscaledAscent,
this.height,
this.width,
this.left,
this.baseline,
this.lineNumber,
);

/// True if this line ends with an explicit line break (e.g. '\n') or is the end
/// of the paragraph. False otherwise.
final bool hardBreak;
Expand Down Expand Up @@ -2795,6 +2786,14 @@ abstract class Paragraph {
/// to repeatedly call this. Instead, cache the results.
List<LineMetrics> computeLineMetrics();

LineMetrics? getLineMetricsAt(int lineNumber);

int get numberOfLines;

int? getLineNumber(int codeUnitOffset);

FontInfo? getFontInfoAt(int codeUnitOffset);

/// Release the resources used by this object. The object is no longer usable
/// after this method is called.
void dispose();
Expand Down Expand Up @@ -2974,6 +2973,29 @@ base class _NativeParagraph extends NativeFieldWrapperClass1 implements Paragrap
@Native<Handle Function(Pointer<Void>)>(symbol: 'Paragraph::computeLineMetrics')
external Float64List _computeLineMetrics();

@override
LineMetrics? getLineMetricsAt(int lineNumber) {
return _getLineMetricsAt(lineNumber, LineMetrics._);
}
@Native<Handle Function(Pointer<Void>, Uint32, Handle)>(symbol: 'Paragraph::getLineMetricsAt')
external LineMetrics? _getLineMetricsAt(int lineNumber, Function constructor);

@override
FontInfo? getFontInfoAt(int codeUnitOffset) {
assert(codeUnitOffset >= 0);
return _getFontInfoAt(codeUnitOffset, FontInfo.new);
}
@Native<Handle Function(Pointer<Void>, Uint32, Handle)>(symbol: 'Paragraph::getFontInfoAt')
external FontInfo? _getFontInfoAt(int codeUnitOffset, Function constructor);

@override
@Native<Uint32 Function(Pointer<Void>)>(symbol: 'Paragraph::getNumberOfLines')
external int get numberOfLines;

@override
@Native<Uint32 Function(Pointer<Void>, Uint32)>(symbol: 'Paragraph::getLineNumberAt')
external int getLineNumber(int codeUnitOffset);

@override
void dispose() {
assert(!_disposed);
Expand Down

0 comments on commit 476e6fb

Please sign in to comment.