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
5 changes: 5 additions & 0 deletions pkgs/csslib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

- Correct `Font.hashCode` for partial/empty font styles and distribute hashes
across font families and other properties, avoiding hash-table collisions.
- Preserve fixed/relative line heights and independent font-family lists
when scaling fonts.
- `Font.scale` keeps the line height (scaling pixel heights and keeping
relative ratios), copies the font-family list, and no longer throws when
the font size is unspecified.
- Fix `Rgba.fromArgbValue`, `Rgba.argbValue` and `Hsla.color`, preserve alpha
for grayscale colors in `Hsla.fromRgba`, and keep the exact 8-bit alpha in
`Color.rgba`, `Color.hsla` and `Hsla.fromArgbValue` instead of rounding it to
Expand Down
21 changes: 17 additions & 4 deletions pkgs/csslib/lib/src/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1118,12 +1118,25 @@ class Font implements _StyleProperty {
return '${size}px $_fontsAsString';
}

Font scale(num ratio) => Font(
size: size! * ratio,
family: family,
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?.toList(),
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
67 changes: 67 additions & 0 deletions pkgs/csslib/test/font_scale_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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);

families[0] = 'Changed';
expect(doubled.family, ['Frank Ruhl', 'serif']);
doubled.family!.add('Custom');
expect(original.family, ['Changed', 'serif']);
});

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']);
family.add('serif');
expect(scaled.family, ['Alef']);

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

test('font without explicit line height retains its default', () {
final scaled = const Font(size: 10).scale(3);
expect(scaled.size, 30);
expect(scaled.lineHeight, isNull);
expect(scaled.lineHeightInPixels, closeTo(36, 0.000001));
});
}
Loading