Skip to content

Commit

Permalink
Added support for "rem" units (#647)
Browse files Browse the repository at this point in the history
* Added tests for "support for rem units"

* Added support for "rem" units
  • Loading branch information
0xba1 authored Jan 13, 2022
1 parent 4ff4565 commit 49bcd4b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/src/utilities/numbers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ double? parseDouble(String? rawDouble, {bool tryParse = false}) {
}

rawDouble = rawDouble
.replaceFirst('rem', '')
.replaceFirst('em', '')
.replaceFirst('ex', '')
.replaceFirst('px', '')
Expand Down Expand Up @@ -46,12 +47,14 @@ double? parseDoubleWithUnits(
}) {
double unit = 1.0;

// 1 rem unit is equal to the root font size.
// 1 em unit is equal to the current font size.
if (rawDouble?.contains('em') ?? false) {
unit = fontSize;
}
// 1 ex unit is equal to the current x-height.
else if (rawDouble?.contains('ex') ?? false) {
if (rawDouble?.contains('rem') ?? false) {
unit = fontSize;
} else if (rawDouble?.contains('em') ?? false) {
unit = fontSize;
} else if (rawDouble?.contains('ex') ?? false) {
unit = xHeight;
}

Expand Down
10 changes: 10 additions & 0 deletions test/svg_parsers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ void main() {
parseFontSize(' 2em ', fontSize: fontSize, xHeight: xHeight),
equals(2 * fontSize),
);

expect(
parseFontSize('4rem', fontSize: fontSize, xHeight: xHeight),
equals(4 * fontSize),
);

expect(
parseFontSize(' 2rem ', fontSize: fontSize, xHeight: xHeight),
equals(2 * fontSize),
);

expect(
parseFontSize('4ex', fontSize: fontSize, xHeight: xHeight),
Expand Down

0 comments on commit 49bcd4b

Please sign in to comment.