Skip to content
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
2 changes: 2 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 1.0.3-wip

- `Font.scale` keeps the line height (scaling pixel heights and keeping
relative ratios) and no longer throws when the font size is unspecified.
- Fix `Font.cssExpression` output for partial fonts and line heights.
- Correct `Font.hashCode` for partial/empty font styles.
- `Color.css` now reads the `hsl()`/`hsla()` hue as degrees (optional `deg`)
Expand Down
19 changes: 16 additions & 3 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,25 @@ class Font implements _StyleProperty {
].join(' ');
}

Font scale(num ratio) => Font(
size: size! * ratio,
Font scale(num ratio) {
final currentLineHeight = lineHeight;
final scaledLineHeight = currentLineHeight == null
? null
: LineHeight(
currentLineHeight.inPixels
? currentLineHeight.height * ratio
: currentLineHeight.height,
inPixels: currentLineHeight.inPixels,
);
return Font(
size: size == null ? null : size! * ratio,
family: family,
weight: weight,
style: style,
variant: variant);
variant: variant,
lineHeight: scaledLineHeight,
);
}

/// The lineHeight, provides an indirect means to specify the leading. The
/// leading is the difference between the font-size height and the (used)
Expand Down
53 changes: 53 additions & 0 deletions pkgs/csslib/test/font_scale_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:csslib/parser.dart';
import 'package:test/test.dart';

void main() {
test('scaling keeps and scales absolute line heights', () {
final families = ['Frank Ruhl', 'serif'];
final original = Font(
size: 12,
family: families,
weight: FontWeight.bold,
lineHeight: const LineHeight(18),
);
final doubled = original.scale(2);

expect(doubled.size, 24);
expect(doubled.lineHeight!.height, 36);
expect(doubled.lineHeight!.inPixels, isTrue);
expect(doubled.lineHeightInPixels, 36);
expect(doubled.weight, FontWeight.bold);
});

test('relative line height remains a ratio when the font scales', () {
const original = Font(
size: 10,
lineHeight: LineHeight(1.5, inPixels: false),
);
final scaled = original.scale(2);

expect(scaled.size, 20);
expect(scaled.lineHeight!.height, 1.5);
expect(scaled.lineHeight!.inPixels, isFalse);
expect(scaled.lineHeightInPixels, 30);
expect(original.lineHeightInPixels, 15);
});

test('scaling partial font families does not require font size', () {
final family = <String>['Alef'];
final original = Font(family: family);
final scaled = original.scale(1.5);

expect(scaled.size, isNull);
expect(scaled.family, ['Alef']);

const withLineHeight = Font(lineHeight: LineHeight(18));
final scaledHeight = withLineHeight.scale(2);
expect(scaledHeight.size, isNull);
expect(scaledHeight.lineHeight!.height, 36);
});
}
Loading