Skip to main content

sui_render_wgpu/
text_policy.rs

1use sui_core::Color;
2
3#[derive(Debug, Clone, Copy, PartialEq)]
4pub struct FeatheringOptions {
5    pub enabled: bool,
6    pub width: f32,
7}
8
9impl Default for FeatheringOptions {
10    fn default() -> Self {
11        Self::new(false, crate::resources::DEFAULT_FEATHER_WIDTH)
12    }
13}
14
15impl FeatheringOptions {
16    pub const fn new(enabled: bool, width: f32) -> Self {
17        Self { enabled, width }
18    }
19
20    pub fn clamped(self) -> Self {
21        Self {
22            enabled: self.enabled,
23            width: self.width.max(0.0),
24        }
25    }
26
27    pub fn effective_width(self) -> f32 {
28        if self.enabled {
29            self.width.max(0.0)
30        } else {
31            0.0
32        }
33    }
34}
35
36pub use sui_scene::TextRenderMode;
37
38/// The renderer's name for [`sui_scene::TextRenderHinting`].
39pub type TextHinting = sui_scene::TextRenderHinting;
40
41/// The renderer's name for [`sui_scene::TextRenderStemDarkening`].
42pub type StemDarkening = sui_scene::TextRenderStemDarkening;
43
44#[derive(Debug, Clone, Copy, PartialEq, Default)]
45pub enum TextCoveragePolicy {
46    #[default]
47    Perceptual,
48    /// Resolved luminances for the perceptual curve. Prefer `Perceptual` to
49    /// let the renderer resolve these from the text and its solid backdrop.
50    PerceptualLuminance {
51        text: f32,
52        background: f32,
53    },
54    Linear,
55    Gamma(f32),
56    CoverageBoost(f32),
57    TwoCoverageMinusCoverageSq,
58}
59
60impl TextCoveragePolicy {
61    pub fn normalized(self) -> Self {
62        match self {
63            Self::Perceptual => Self::Perceptual,
64            Self::PerceptualLuminance { text, background }
65                if text.is_finite() && background.is_finite() =>
66            {
67                Self::PerceptualLuminance {
68                    text: text.clamp(0.0, 1.0),
69                    background: background.clamp(0.0, 1.0),
70                }
71            }
72            Self::PerceptualLuminance { .. } => Self::Linear,
73            Self::Linear => Self::Linear,
74            Self::Gamma(gamma) if gamma.is_finite() && gamma > 0.0 => Self::Gamma(gamma),
75            Self::Gamma(_) => Self::Linear,
76            Self::CoverageBoost(amount) if amount.is_finite() && amount > 0.0 => {
77                Self::CoverageBoost(amount.clamp(0.0, 1.0))
78            }
79            Self::CoverageBoost(_) => Self::Linear,
80            Self::TwoCoverageMinusCoverageSq => Self::TwoCoverageMinusCoverageSq,
81        }
82    }
83
84    pub fn resolved_for_text_color(self, color: Color) -> Self {
85        self.resolved_for_text_background(color, None)
86    }
87
88    pub(crate) fn resolved_for_text_background(
89        self,
90        color: Color,
91        background: Option<Color>,
92    ) -> Self {
93        match self.normalized() {
94            Self::Perceptual => {
95                if !is_sdr_color(color) || background.is_some_and(|c| !is_sdr_color(c)) {
96                    return Self::Linear;
97                }
98                let text = encoded_srgb_luminance(color);
99                Self::PerceptualLuminance {
100                    text,
101                    background: background.map(encoded_srgb_luminance).unwrap_or(1.0 - text),
102                }
103            }
104            policy => policy,
105        }
106    }
107
108    pub fn apply(self, coverage: f32) -> f32 {
109        let coverage = coverage.clamp(0.0, 1.0);
110        match self.normalized() {
111            Self::Perceptual => perceptual_text_coverage(coverage, 0.0, 1.0),
112            Self::PerceptualLuminance { text, background } => {
113                perceptual_text_coverage(coverage, text, background)
114            }
115            Self::Linear => coverage,
116            Self::Gamma(gamma) => coverage.powf(gamma),
117            Self::CoverageBoost(amount) => apply_coverage_boost(coverage, amount),
118            Self::TwoCoverageMinusCoverageSq => (2.0 * coverage) - (coverage * coverage),
119        }
120    }
121}
122
123pub(crate) fn is_sdr_color(color: Color) -> bool {
124    let c = color.to_linear_srgb();
125    [c.red, c.green, c.blue]
126        .iter()
127        .all(|v| v.is_finite() && (0.0..=1.0).contains(v))
128}
129
130/// Contrast/gamma compensation modeled on Skia's mask-gamma construction,
131/// converted back to coverage for our linear-light framebuffer. The 1.8
132/// perceptual exponent is calibrated against Chrome's light/dark UI text;
133/// it is not the framebuffer transfer function. Keep in sync with both text
134/// atlas shaders. Endpoint preservation also keeps glyph padding transparent.
135pub(crate) fn perceptual_text_coverage(coverage: f32, text: f32, background: f32) -> f32 {
136    let c = coverage.clamp(0.0, 1.0);
137    if c == 0.0 || c == 1.0 {
138        return c;
139    }
140    let gamma = 1.8;
141    let a = apply_coverage_boost(c, 0.5 * background.powf(gamma));
142    let fg = encoded_srgb_to_linear_unit(text);
143    let bg = encoded_srgb_to_linear_unit(background);
144    if (fg - bg).abs() < 1e-4 {
145        return a;
146    }
147    let perceptual = (text.powf(gamma) * a + background.powf(gamma) * (1.0 - a)).powf(1.0 / gamma);
148    ((encoded_srgb_to_linear_unit(perceptual) - bg) / (fg - bg)).clamp(0.0, 1.0)
149}
150
151fn encoded_srgb_to_linear_unit(value: f32) -> f32 {
152    if value <= 0.04045 {
153        value / 12.92
154    } else {
155        ((value + 0.055) / 1.055).powf(2.4)
156    }
157}
158
159/// Chrome/Skia's per-channel sRGB mask correction, followed by conversion of
160/// the resulting encoded-space composite into coverage for our linear target.
161/// Uses the Windows reference contrast of 1.0. This is LCD-only; the portable
162/// grayscale policy keeps its separately calibrated luminance curve.
163#[cfg(test)]
164pub(crate) fn lcd_text_coverage(coverage: f32, text: f32, background: f32) -> f32 {
165    let c = coverage.clamp(0.0, 1.0);
166    if c == 0.0 || c == 1.0 {
167        return c;
168    }
169    let guessed_bg = 1.0 - text;
170    let guessed_linear = encoded_srgb_to_linear_unit(guessed_bg);
171    let foreground_linear = encoded_srgb_to_linear_unit(text);
172    let a = apply_coverage_boost(c, guessed_linear);
173    let encoded_alpha = if (text - guessed_bg).abs() < 1.0 / 256.0 {
174        a
175    } else {
176        (linear_srgb_to_encoded_unit(foreground_linear * a + guessed_linear * (1.0 - a))
177            - guessed_bg)
178            / (text - guessed_bg)
179    }
180    .clamp(0.0, 1.0);
181    let bg = encoded_srgb_to_linear_unit(background);
182    if (foreground_linear - bg).abs() < 1e-4 {
183        return c;
184    }
185    let desired = encoded_srgb_to_linear_unit(background + (text - background) * encoded_alpha);
186    ((desired - bg) / (foreground_linear - bg)).clamp(0.0, 1.0)
187}
188
189pub(crate) fn pack_lcd_background(color: Color) -> f32 {
190    let c = color.to_linear_srgb();
191    let [r, g, b] =
192        [c.red, c.green, c.blue].map(|v| (linear_srgb_to_encoded_unit(v) * 255.0).round() as u32);
193    // Exactly representable as an f32 integer and interpolated flat.
194    ((r << 16) | (g << 8) | b) as f32
195}
196
197pub(crate) fn apply_coverage_boost(coverage: f32, amount: f32) -> f32 {
198    coverage + (coverage * (1.0 - coverage) * amount.clamp(0.0, 1.0))
199}
200
201pub(crate) fn encoded_srgb_luminance(color: Color) -> f32 {
202    let linear = color.to_linear_srgb();
203    let red = linear_srgb_to_encoded_unit(linear.red);
204    let green = linear_srgb_to_encoded_unit(linear.green);
205    let blue = linear_srgb_to_encoded_unit(linear.blue);
206    ((0.2126 * red) + (0.7152 * green) + (0.0722 * blue)).clamp(0.0, 1.0)
207}
208
209pub(crate) fn linear_srgb_to_encoded_unit(channel: f32) -> f32 {
210    let value = channel.clamp(0.0, 1.0);
211    if value <= 0.003_130_8 {
212        value * 12.92
213    } else {
214        (1.055 * value.powf(1.0 / 2.4)) - 0.055
215    }
216}