Skip to content

Commit

Permalink
Merge pull request #13 from yshean/feature/add-fontweight-support
Browse files Browse the repository at this point in the history
Add FontWeight support
  • Loading branch information
juliansteenbakker authored May 15, 2024
2 parents 3411095 + 66ff0f7 commit 78eb192
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion community_charts_flutter/lib/src/text_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'package:community_charts_common/community_charts_common.dart' as common
TextMeasurement,
TextStyle;
import 'package:flutter/rendering.dart'
show Color, TextBaseline, TextPainter, TextSpan, TextStyle;
show Color, TextBaseline, TextPainter, TextSpan, TextStyle, FontWeight;

/// Flutter implementation for text measurement and painter.
class TextElement implements common.TextElement {
Expand Down Expand Up @@ -152,6 +152,7 @@ class TextElement implements common.TextElement {
color: color,
fontSize: textStyle?.fontSize?.toDouble(),
fontFamily: textStyle?.fontFamily,
fontWeight: _effectiveFontWeight,
height: textStyle?.lineHeight)))
..textDirection = TextDirection.ltr
// TODO Flip once textAlign works
Expand Down Expand Up @@ -183,4 +184,17 @@ class TextElement implements common.TextElement {

_painterReady = true;
}

FontWeight? get _effectiveFontWeight {
final name = textStyle?.fontWeight?.toLowerCase();
if (name == null) return null;
final value = int.tryParse(name);
if (value == null) {
if (name == 'normal') return FontWeight.normal;
if (name == 'bold') return FontWeight.bold;
} else {
return FontWeight.values[value.clamp(100, 900) ~/ 100 - 1];
}
return null;
}
}

0 comments on commit 78eb192

Please sign in to comment.