From da546221e24a43fc9caca4a56340167e549a5660 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:01:04 +0200 Subject: [PATCH 1/5] fix(csslib): preserve font line heights and family lists when scaling --- pkgs/csslib/CHANGELOG.md | 2 ++ pkgs/csslib/lib/src/property.dart | 19 ++++++++-- pkgs/csslib/test/font_scale_test.dart | 51 +++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 pkgs/csslib/test/font_scale_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 1c51e98f48..649af2f469 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -2,6 +2,8 @@ - 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. - 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 diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index a6a83d72a7..3b6857b161 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1118,12 +1118,25 @@ class Font implements _StyleProperty { return '${size}px $_fontsAsString'; } - Font scale(num ratio) => Font( + 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! * ratio, - family: family, + 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) diff --git a/pkgs/csslib/test/font_scale_test.dart b/pkgs/csslib/test/font_scale_test.dart new file mode 100644 index 0000000000..f05a64157c --- /dev/null +++ b/pkgs/csslib/test/font_scale_test.dart @@ -0,0 +1,51 @@ +// 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', () { + final original = Font( + size: 10, + lineHeight: const 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('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)); + }); +} From 3fb6ce0ace76bcb3ee2251bafa8d0ec81c8a1da9 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:16:38 +0200 Subject: [PATCH 2/5] test(csslib): use const for fixed relative font fixture --- pkgs/csslib/test/font_scale_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/csslib/test/font_scale_test.dart b/pkgs/csslib/test/font_scale_test.dart index f05a64157c..b0046888b2 100644 --- a/pkgs/csslib/test/font_scale_test.dart +++ b/pkgs/csslib/test/font_scale_test.dart @@ -29,9 +29,9 @@ void main() { }); test('relative line height remains a ratio when the font scales', () { - final original = Font( + const original = Font( size: 10, - lineHeight: const LineHeight(1.5, inPixels: false), + lineHeight: LineHeight(1.5, inPixels: false), ); final scaled = original.scale(2); From 94c4840966e10b3e0534fe52d9e40176f770fb41 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:36:20 +0200 Subject: [PATCH 3/5] fix(csslib): support scaling font styles with unspecified size --- pkgs/csslib/lib/src/property.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 3b6857b161..e233da1a1d 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1129,7 +1129,7 @@ class Font implements _StyleProperty { inPixels: currentLineHeight.inPixels, ); return Font( - size: size! * ratio, + size: size == null ? null : size! * ratio, family: family?.toList(), weight: weight, style: style, From 2245334835207ec16b24094de6a42427c25acda7 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 20:36:40 +0200 Subject: [PATCH 4/5] test(csslib): cover scaling partial font styles --- pkgs/csslib/test/font_scale_test.dart | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkgs/csslib/test/font_scale_test.dart b/pkgs/csslib/test/font_scale_test.dart index b0046888b2..7f72bb54a4 100644 --- a/pkgs/csslib/test/font_scale_test.dart +++ b/pkgs/csslib/test/font_scale_test.dart @@ -42,6 +42,22 @@ void main() { 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']); + 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); From 91172fb059e128a91bab3a1c3761154c9495bafa Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 21:35:24 +0200 Subject: [PATCH 5/5] Describe all Font.scale fixes in the changelog --- pkgs/csslib/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 649af2f469..1868125515 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -4,6 +4,9 @@ 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