diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 6bceb8be4..51f8ee188 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -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`) diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 5927f062a..b70ca4007 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -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) diff --git a/pkgs/csslib/test/font_scale_test.dart b/pkgs/csslib/test/font_scale_test.dart new file mode 100644 index 000000000..681d1cb6c --- /dev/null +++ b/pkgs/csslib/test/font_scale_test.dart @@ -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 = ['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); + }); +}