From a2dcf772787afe64dce116d9fab8c30bacbd70eb Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 22:17:04 +0200 Subject: [PATCH 1/3] [csslib] Keep line height and family list in Font.scale --- pkgs/csslib/CHANGELOG.md | 42 +++++++++++++++++ pkgs/csslib/lib/src/property.dart | 21 +++++++-- pkgs/csslib/test/font_scale_test.dart | 67 +++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 pkgs/csslib/test/font_scale_test.dart diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 2b5e3472da..1868125515 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,5 +1,47 @@ ## 1.0.3-wip +- 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 + two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, + and `Color.css` throws a `FormatException` for unsupported colors. +- Correct CSS namespace URI serialization in pretty-printed stylesheets while + preserving compact `url()` representation. +- Fix fractional alpha in the numeric `Color` constructor and normalize CSS + short and eight-digit hex colors, with validation of malformed input and + unknown color names. + `Color.css` also accepts named colors and `transparent`, and + `Rgba.fromString`/`Hsla.fromString` now parse their argument only once. +- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by + default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and + accepts percentages for saturation, lightness, RGB channels and alpha. + Functional colors with the wrong number of components or invalid or + non-finite values now throw a `FormatException` whose source is the color. +- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the + start of `selector()` and `parseSelectorGroup()` input and of style rules. +- Use native integer hexadecimal formatting for named CSS colors, reducing + temporary allocations and avoiding hangs on negative conversion inputs. +- Speed up named CSS color lookups by indexing the color table. +- Recognize the `@left-bottom` page-margin directive, and stop parsing + `@right-bottom` as `@left-bottom`. +- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`. +- Reduce repeated CSS unit, directive, and media keyword scans by indexing + keyword tables by length while retaining allocation-free token matching. +- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing + benchmark and regression coverage for Unicode and escaped identifiers. +- Flatten nested top-level selectors with a single ordered pass instead of + repeated linear searches and list insertions on large stylesheets. +- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, + preventing stack overflow on comment-heavy stylesheets. +- End HTML comments at the first `-->` even when more dashes precede it, and + don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 0188c2b8e2..31b7d09b1b 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1037,12 +1037,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) diff --git a/pkgs/csslib/test/font_scale_test.dart b/pkgs/csslib/test/font_scale_test.dart new file mode 100644 index 0000000000..7f72bb54a4 --- /dev/null +++ b/pkgs/csslib/test/font_scale_test.dart @@ -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 = ['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)); + }); +} From 6d9be964be267177de6f673c4774fb4fcbf69203 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Tue, 10 Nov 2026 23:26:22 +0200 Subject: [PATCH 2/3] Keep only this change's changelog entry --- pkgs/csslib/CHANGELOG.md | 39 --------------------------------------- 1 file changed, 39 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 1868125515..67f7713c96 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,47 +1,8 @@ ## 1.0.3-wip -- 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 - two digits. `Rgba.fromString` and `Hsla.fromString` parse the CSS color once, - and `Color.css` throws a `FormatException` for unsupported colors. -- Correct CSS namespace URI serialization in pretty-printed stylesheets while - preserving compact `url()` representation. -- Fix fractional alpha in the numeric `Color` constructor and normalize CSS - short and eight-digit hex colors, with validation of malformed input and - unknown color names. - `Color.css` also accepts named colors and `transparent`, and - `Rgba.fromString`/`Hsla.fromString` now parse their argument only once. -- `Color.css` now reads the `hsl()`/`hsla()` hue as a CSS angle (degrees by - default, or `deg`, `grad`, `rad` and `turn`) instead of a 0..1 fraction, and - accepts percentages for saturation, lightness, RGB channels and alpha. - Functional colors with the wrong number of components or invalid or - non-finite values now throw a `FormatException` whose source is the color. -- Correctly parse unit-like type selectors (`rem`, `vw`, `dpi`, etc.) at the - start of `selector()` and `parseSelectorGroup()` input and of style rules. -- Use native integer hexadecimal formatting for named CSS colors, reducing - temporary allocations and avoiding hangs on negative conversion inputs. -- Speed up named CSS color lookups by indexing the color table. -- Recognize the `@left-bottom` page-margin directive, and stop parsing - `@right-bottom` as `@left-bottom`. -- Recognize the `rebeccapurple` color keyword and add `Color.rebeccaPurple`. -- Reduce repeated CSS unit, directive, and media keyword scans by indexing - keyword tables by length while retaining allocation-free token matching. -- Reduce allocations when tokenizing ordinary CSS identifiers; add a parsing - benchmark and regression coverage for Unicode and escaped identifiers. -- Flatten nested top-level selectors with a single ordered pass instead of - repeated linear searches and list insertions on large stylesheets. -- Avoid recursive tokenizer calls when skipping CSS comments and whitespace, - preventing stack overflow on comment-heavy stylesheets. -- End HTML comments at the first `-->` even when more dashes precede it, and - don't end comments at a NUL character. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, From 83ef601ddff0b7edef1d5405ea66239e65f615bf Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Sat, 14 Nov 2026 18:39:45 +0200 Subject: [PATCH 3/3] Address review feedback --- pkgs/csslib/CHANGELOG.md | 3 +-- pkgs/csslib/lib/src/property.dart | 2 +- pkgs/csslib/test/font_scale_test.dart | 14 -------------- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/pkgs/csslib/CHANGELOG.md b/pkgs/csslib/CHANGELOG.md index 67f7713c96..2010afb00a 100644 --- a/pkgs/csslib/CHANGELOG.md +++ b/pkgs/csslib/CHANGELOG.md @@ -1,8 +1,7 @@ ## 1.0.3-wip - `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. + relative ratios) and no longer throws when the font size is unspecified. - Fix the values of the `aliceblue` color keyword and the `Color.aliceBlue`, `Color.antiqueWhite` and `Color.peru` constants. - Fix unhandled exceptions (`RangeError`, `FormatException`, `StateError`, diff --git a/pkgs/csslib/lib/src/property.dart b/pkgs/csslib/lib/src/property.dart index 31b7d09b1b..fb11c1c602 100644 --- a/pkgs/csslib/lib/src/property.dart +++ b/pkgs/csslib/lib/src/property.dart @@ -1049,7 +1049,7 @@ class Font implements _StyleProperty { ); return Font( size: size == null ? null : size! * ratio, - family: family?.toList(), + family: family, weight: weight, style: style, variant: variant, diff --git a/pkgs/csslib/test/font_scale_test.dart b/pkgs/csslib/test/font_scale_test.dart index 7f72bb54a4..681d1cb6c8 100644 --- a/pkgs/csslib/test/font_scale_test.dart +++ b/pkgs/csslib/test/font_scale_test.dart @@ -21,11 +21,6 @@ void main() { 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', () { @@ -49,19 +44,10 @@ void main() { 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)); - }); }