Fix underline DPI (#35816)

Release Notes:

- Fixed wavy underlines looking inconsistent on different displays
This commit is contained in:
localcc 2025-08-11 13:57:30 +02:00 committed by GitHub
parent 086ea3c619
commit 702a95ffb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 7 deletions

View file

@ -1057,6 +1057,9 @@ fn vs_underline(@builtin(vertex_index) vertex_id: u32, @builtin(instance_index)
@fragment
fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
const WAVE_FREQUENCY: f32 = 2.0;
const WAVE_HEIGHT_RATIO: f32 = 0.8;
// Alpha clip first, since we don't have `clip_distance`.
if (any(input.clip_distances < vec4<f32>(0.0))) {
return vec4<f32>(0.0);
@ -1069,9 +1072,11 @@ fn fs_underline(input: UnderlineVarying) -> @location(0) vec4<f32> {
}
let half_thickness = underline.thickness * 0.5;
let st = (input.position.xy - underline.bounds.origin) / underline.bounds.size.y - vec2<f32>(0.0, 0.5);
let frequency = M_PI_F * 3.0 * underline.thickness / 3.0;
let amplitude = 1.0 / (4.0 * underline.thickness);
let frequency = M_PI_F * WAVE_FREQUENCY * underline.thickness / underline.bounds.size.y;
let amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.y;
let sine = sin(st.x * frequency) * amplitude;
let dSine = cos(st.x * frequency) * amplitude * frequency;
let distance = (st.y - sine) / sqrt(1.0 + dSine * dSine);

View file

@ -567,15 +567,20 @@ vertex UnderlineVertexOutput underline_vertex(
fragment float4 underline_fragment(UnderlineFragmentInput input [[stage_in]],
constant Underline *underlines
[[buffer(UnderlineInputIndex_Underlines)]]) {
const float WAVE_FREQUENCY = 2.0;
const float WAVE_HEIGHT_RATIO = 0.8;
Underline underline = underlines[input.underline_id];
if (underline.wavy) {
float half_thickness = underline.thickness * 0.5;
float2 origin =
float2(underline.bounds.origin.x, underline.bounds.origin.y);
float2 st = ((input.position.xy - origin) / underline.bounds.size.height) -
float2(0., 0.5);
float frequency = (M_PI_F * (3. * underline.thickness)) / 8.;
float amplitude = 1. / (2. * underline.thickness);
float frequency = (M_PI_F * WAVE_FREQUENCY * underline.thickness) / underline.bounds.size.height;
float amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.height;
float sine = sin(st.x * frequency) * amplitude;
float dSine = cos(st.x * frequency) * amplitude * frequency;
float distance = (st.y - sine) / sqrt(1. + dSine * dSine);

View file

@ -914,7 +914,7 @@ float4 path_rasterization_fragment(PathFragmentInput input): SV_Target {
float2 dx = ddx(input.st_position);
float2 dy = ddy(input.st_position);
PathRasterizationSprite sprite = path_rasterization_sprites[input.vertex_id];
Background background = sprite.color;
Bounds bounds = sprite.bounds;
@ -1021,13 +1021,18 @@ UnderlineVertexOutput underline_vertex(uint vertex_id: SV_VertexID, uint underli
}
float4 underline_fragment(UnderlineFragmentInput input): SV_Target {
const float WAVE_FREQUENCY = 2.0;
const float WAVE_HEIGHT_RATIO = 0.8;
Underline underline = underlines[input.underline_id];
if (underline.wavy) {
float half_thickness = underline.thickness * 0.5;
float2 origin = underline.bounds.origin;
float2 st = ((input.position.xy - origin) / underline.bounds.size.y) - float2(0., 0.5);
float frequency = (M_PI_F * (3. * underline.thickness)) / 8.;
float amplitude = 1. / (2. * underline.thickness);
float frequency = (M_PI_F * WAVE_FREQUENCY * underline.thickness) / underline.bounds.size.y;
float amplitude = (underline.thickness * WAVE_HEIGHT_RATIO) / underline.bounds.size.y;
float sine = sin(st.x * frequency) * amplitude;
float dSine = cos(st.x * frequency) * amplitude * frequency;
float distance = (st.y - sine) / sqrt(1. + dSine * dSine);