diff --git a/wezterm-font/src/hbwrap.rs b/wezterm-font/src/hbwrap.rs index 4c940df47..b07a8771e 100644 --- a/wezterm-font/src/hbwrap.rs +++ b/wezterm-font/src/hbwrap.rs @@ -4,8 +4,9 @@ use freetype; pub use harfbuzz::*; use crate::locator::{FontDataHandle, FontDataSource}; -use crate::rasterizer::colr::DrawOp; +use crate::rasterizer::colr::{ColorLine, ColorStop, DrawOp}; use anyhow::{ensure, Context, Error}; +use cairo::Extend; use memmap2::{Mmap, MmapOptions}; use std::ffi::CStr; use std::io::Read; @@ -13,6 +14,7 @@ use std::ops::Range; use std::os::raw::{c_char, c_int, c_uint, c_void}; use std::sync::Arc; use std::{mem, slice}; +use wezterm_color_types::SrgbaPixel; extern "C" { fn hb_ft_font_set_load_flags(font: *mut hb_font_t, load_flags: i32); @@ -603,7 +605,7 @@ impl PaintOp { _user_data: *mut ::std::os::raw::c_void, ) { let ops = Self::paint_data(paint_data); - let color_line = ColorLine::new(color_line); + let color_line = ColorLine::new_from_hb(color_line); ops.push(Self::PaintLinearGradient { color_line, x0, @@ -628,7 +630,7 @@ impl PaintOp { _user_data: *mut ::std::os::raw::c_void, ) { let ops = Self::paint_data(paint_data); - let color_line = ColorLine::new(color_line); + let color_line = ColorLine::new_from_hb(color_line); ops.push(Self::PaintRadialGradient { color_line, x0, @@ -651,7 +653,7 @@ impl PaintOp { _user_data: *mut ::std::os::raw::c_void, ) { let ops = Self::paint_data(paint_data); - let color_line = ColorLine::new(color_line); + let color_line = ColorLine::new_from_hb(color_line); ops.push(Self::PaintSweepGradient { color_line, x0, @@ -836,14 +838,8 @@ impl DrawOp { } } -#[derive(Clone, Debug)] -pub struct ColorLine { - pub color_stops: Vec, - pub extend: hb_paint_extend_t, -} - impl ColorLine { - pub fn new(line: *mut hb_color_line_t) -> Self { + pub fn new_from_hb(line: *mut hb_color_line_t) -> Self { let num_stops = unsafe { hb_color_line_get_color_stops(line, 0, std::ptr::null_mut(), std::ptr::null_mut()) }; @@ -857,26 +853,42 @@ impl ColorLine { }, ); - // Docs say: Note that due to variations being applied, the returned color stops may be out - // of order. It is the callers responsibility to ensure that color stops are sorted by - // their offset before they are used. - unsafe { let mut count = num_stops; hb_color_line_get_color_stops(line, 0, &mut count, color_stops.as_mut_ptr()); } - color_stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); - let extend = unsafe { hb_color_line_get_extend(line) }; Self { - color_stops, - extend, + color_stops: color_stops + .into_iter() + .map(|stop| ColorStop { + offset: stop.offset, + color: hb_color_to_srgba_pixel(stop.color), + }) + .collect(), + extend: hb_extend_to_cairo(extend), } } } +fn hb_color_to_srgba_pixel(color: hb_color_t) -> SrgbaPixel { + let red = unsafe { hb_color_get_red(color) }; + let green = unsafe { hb_color_get_green(color) }; + let blue = unsafe { hb_color_get_blue(color) }; + let alpha = unsafe { hb_color_get_alpha(color) }; + SrgbaPixel::rgba(red, green, blue, alpha) +} + +fn hb_extend_to_cairo(extend: hb_paint_extend_t) -> Extend { + match extend { + hb_paint_extend_t::HB_PAINT_EXTEND_PAD => Extend::Pad, + hb_paint_extend_t::HB_PAINT_EXTEND_REPEAT => Extend::Repeat, + hb_paint_extend_t::HB_PAINT_EXTEND_REFLECT => Extend::Reflect, + } +} + pub struct Buffer { buf: *mut hb_buffer_t, } diff --git a/wezterm-font/src/rasterizer/colr.rs b/wezterm-font/src/rasterizer/colr.rs index dc0d2fa3e..e9f59a874 100644 --- a/wezterm-font/src/rasterizer/colr.rs +++ b/wezterm-font/src/rasterizer/colr.rs @@ -152,6 +152,10 @@ fn normalize_color_line(color_line: &mut ColorLine) -> (f64, f64) { let mut smallest = color_line.color_stops[0].offset; let mut largest = smallest; + color_line + .color_stops + .sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); + for stop in &color_line.color_stops[1..] { smallest = smallest.min(stop.offset); largest = largest.max(stop.offset); diff --git a/wezterm-font/src/rasterizer/harfbuzz.rs b/wezterm-font/src/rasterizer/harfbuzz.rs index 53250192f..c22e9bd30 100644 --- a/wezterm-font/src/rasterizer/harfbuzz.rs +++ b/wezterm-font/src/rasterizer/harfbuzz.rs @@ -1,16 +1,12 @@ use crate::hbwrap::{ hb_color, hb_color_get_alpha, hb_color_get_blue, hb_color_get_green, hb_color_get_red, - hb_color_t, hb_paint_composite_mode_t, hb_paint_extend_t, hb_tag_to_string, ColorLine, Font, - PaintOp, IS_PNG, + hb_color_t, hb_paint_composite_mode_t, hb_tag_to_string, Font, PaintOp, IS_PNG, }; -use crate::rasterizer::colr::DrawOp; +use crate::rasterizer::colr::{paint_linear_gradient, paint_radial_gradient, DrawOp}; use crate::rasterizer::FAKE_ITALIC_SKEW; use crate::units::PixelLength; use crate::{FontRasterizer, ParsedFont, RasterizedGlyph}; -use cairo::{ - Content, Context, Extend, Format, ImageSurface, LinearGradient, Matrix, Operator, - RadialGradient, RecordingSurface, -}; +use cairo::{Content, Context, Format, ImageSurface, Matrix, Operator, RecordingSurface}; use image::DynamicImage::{ImageLuma8, ImageLumaA8}; use image::GenericImageView; @@ -472,157 +468,3 @@ fn hb_color_to_rgba(color: hb_color_t) -> (f64, f64, f64, f64) { let alpha = unsafe { hb_color_get_alpha(color) } as f64; (red / 255., green / 255., blue / 255., alpha / 255.) } - -fn normalize_color_line(color_line: &mut ColorLine) -> (f64, f64) { - let mut smallest = color_line.color_stops[0].offset; - let mut largest = smallest; - - for stop in &color_line.color_stops[1..] { - smallest = smallest.min(stop.offset); - largest = largest.max(stop.offset); - } - - if smallest != largest { - for stop in &mut color_line.color_stops { - stop.offset = (stop.offset - smallest) / (largest - smallest); - } - } - - // NOTE: hb-cairo-utils will call back out to some other state - // to fill in the color when is_foreground is true, defaulting - // to black with alpha varying by the alpha channel of the - // color value in the stop. Do we need to do something like - // that here? - - (smallest as f64, largest as f64) -} - -struct ReduceAnchorsIn { - x0: f64, - y0: f64, - x1: f64, - y1: f64, - x2: f64, - y2: f64, -} - -struct ReduceAnchorsOut { - xx0: f64, - yy0: f64, - xx1: f64, - yy1: f64, -} - -fn reduce_anchors( - ReduceAnchorsIn { - x0, - y0, - x1, - y1, - x2, - y2, - }: ReduceAnchorsIn, -) -> ReduceAnchorsOut { - let q2x = x2 - x0; - let q2y = y2 - y0; - let q1x = x1 - x0; - let q1y = y1 - y0; - - let s = q2x * q2x + q2y * q2y; - if s < 0.000001 { - return ReduceAnchorsOut { - xx0: x0, - yy0: y0, - xx1: x1, - yy1: y1, - }; - } - - let k = (q2x * q1x + q2y * q1y) / s; - ReduceAnchorsOut { - xx0: x0, - yy0: y0, - xx1: x1 - k * q2x, - yy1: y1 - k * q2y, - } -} - -fn paint_linear_gradient( - context: &Context, - x0: f64, - y0: f64, - x1: f64, - y1: f64, - x2: f64, - y2: f64, - mut color_line: ColorLine, -) -> anyhow::Result<()> { - let (min_stop, max_stop) = normalize_color_line(&mut color_line); - let anchors = reduce_anchors(ReduceAnchorsIn { - x0, - y0, - x1, - y1, - x2, - y2, - }); - - let xxx0 = anchors.xx0 + min_stop * (anchors.xx1 - anchors.xx0); - let yyy0 = anchors.yy0 + min_stop * (anchors.yy1 - anchors.yy0); - let xxx1 = anchors.xx0 + max_stop * (anchors.xx1 - anchors.xx0); - let yyy1 = anchors.yy0 + max_stop * (anchors.yy1 - anchors.yy0); - - let pattern = LinearGradient::new(xxx0, yyy0, xxx1, yyy1); - pattern.set_extend(hb_extend_to_cairo(color_line.extend)); - - for stop in &color_line.color_stops { - let (r, g, b, a) = hb_color_to_rgba(stop.color); - pattern.add_color_stop_rgba(stop.offset.into(), r, g, b, a); - } - - context.set_source(pattern)?; - context.paint()?; - - Ok(()) -} - -fn paint_radial_gradient( - context: &Context, - x0: f64, - y0: f64, - r0: f64, - x1: f64, - y1: f64, - r1: f64, - mut color_line: ColorLine, -) -> anyhow::Result<()> { - let (min_stop, max_stop) = normalize_color_line(&mut color_line); - - let xx0 = x0 + min_stop * (x1 - x0); - let yy0 = y0 + min_stop * (y1 - y0); - let xx1 = x0 + max_stop * (x1 - x0); - let yy1 = y0 + max_stop * (y1 - y0); - let rr0 = r0 + min_stop * (r1 - r0); - let rr1 = r0 + max_stop * (r1 - r0); - - let pattern = RadialGradient::new(xx0, yy0, rr0, xx1, yy1, rr1); - pattern.set_extend(hb_extend_to_cairo(color_line.extend)); - - for stop in &color_line.color_stops { - let (r, g, b, a) = hb_color_to_rgba(stop.color); - pattern.add_color_stop_rgba(stop.offset.into(), r, g, b, a); - } - - context.set_source(pattern)?; - context.paint()?; - - Ok(()) -} - -fn hb_extend_to_cairo(extend: hb_paint_extend_t) -> Extend { - match extend { - hb_paint_extend_t::HB_PAINT_EXTEND_PAD => Extend::Pad, - hb_paint_extend_t::HB_PAINT_EXTEND_REPEAT => Extend::Repeat, - hb_paint_extend_t::HB_PAINT_EXTEND_REFLECT => Extend::Reflect, - } -}