1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

font: refactor: extract common function

This commit is contained in:
Wez Furlong 2023-08-21 09:40:24 -07:00
parent dd6f2a4d7f
commit 85bb023295
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
3 changed files with 63 additions and 123 deletions

View File

@ -237,3 +237,63 @@ fn reduce_anchors(
yy1: y1 - k * q2y,
}
}
pub fn apply_draw_ops_to_context(ops: &[DrawOp], context: &Context) -> anyhow::Result<()> {
let mut current = None;
context.new_path();
for op in ops {
match op {
DrawOp::MoveTo { to_x, to_y } => {
context.move_to((*to_x).into(), (*to_y).into());
current.replace((to_x, to_y));
}
DrawOp::LineTo { to_x, to_y } => {
context.line_to((*to_x).into(), (*to_y).into());
current.replace((to_x, to_y));
}
DrawOp::QuadTo {
control_x,
control_y,
to_x,
to_y,
} => {
let (x, y) =
current.ok_or_else(|| anyhow::anyhow!("QuadTo has no current position"))?;
// Express quadratic as a cubic
// <https://stackoverflow.com/a/55034115/149111>
context.curve_to(
(x + (2. / 3.) * (control_x - x)).into(),
(y + (2. / 3.) * (control_y - y)).into(),
(to_x + (2. / 3.) * (control_x - to_x)).into(),
(to_y + (2. / 3.) * (control_y - to_y)).into(),
(*to_x).into(),
(*to_y).into(),
);
current.replace((to_x, to_y));
}
DrawOp::CubicTo {
control1_x,
control1_y,
control2_x,
control2_y,
to_x,
to_y,
} => {
context.curve_to(
(*control1_x).into(),
(*control1_y).into(),
(*control2_x).into(),
(*control2_y).into(),
(*to_x).into(),
(*to_y).into(),
);
current.replace((to_x, to_y));
}
DrawOp::ClosePath => {
context.close_path();
}
}
}
Ok(())
}

View File

@ -5,8 +5,8 @@ use crate::ftwrap::{
};
use crate::parser::ParsedFont;
use crate::rasterizer::colr::{
paint_linear_gradient, paint_radial_gradient, paint_sweep_gradient, ColorLine, ColorStop,
DrawOp, PaintOp,
apply_draw_ops_to_context, paint_linear_gradient, paint_radial_gradient, paint_sweep_gradient,
ColorLine, ColorStop, PaintOp,
};
use crate::rasterizer::harfbuzz::{argb_to_rgba, HarfbuzzRasterizer};
use crate::rasterizer::{FontRasterizer, FAKE_ITALIC_SKEW};
@ -827,63 +827,3 @@ fn record_to_cairo_surface(
Ok((surface, has_color))
}
fn apply_draw_ops_to_context(ops: &[DrawOp], context: &Context) -> anyhow::Result<()> {
let mut current = None;
context.new_path();
for op in ops {
match op {
DrawOp::MoveTo { to_x, to_y } => {
context.move_to((*to_x).into(), (*to_y).into());
current.replace((to_x, to_y));
}
DrawOp::LineTo { to_x, to_y } => {
context.line_to((*to_x).into(), (*to_y).into());
current.replace((to_x, to_y));
}
DrawOp::QuadTo {
control_x,
control_y,
to_x,
to_y,
} => {
let (x, y) =
current.ok_or_else(|| anyhow::anyhow!("QuadTo has no current position"))?;
// Express quadratic as a cubic
// <https://stackoverflow.com/a/55034115/149111>
context.curve_to(
(x + (2. / 3.) * (control_x - x)).into(),
(y + (2. / 3.) * (control_y - y)).into(),
(to_x + (2. / 3.) * (control_x - to_x)).into(),
(to_y + (2. / 3.) * (control_y - to_y)).into(),
(*to_x).into(),
(*to_y).into(),
);
current.replace((to_x, to_y));
}
DrawOp::CubicTo {
control1_x,
control1_y,
control2_x,
control2_y,
to_x,
to_y,
} => {
context.curve_to(
(*control1_x).into(),
(*control1_y).into(),
(*control2_x).into(),
(*control2_y).into(),
(*to_x).into(),
(*to_y).into(),
);
current.replace((to_x, to_y));
}
DrawOp::ClosePath => {
context.close_path();
}
}
}
Ok(())
}

View File

@ -3,7 +3,7 @@ use crate::hbwrap::{
hb_color_t, hb_paint_composite_mode_t, hb_tag_to_string, Font, PaintOp, IS_PNG,
};
use crate::rasterizer::colr::{
paint_linear_gradient, paint_radial_gradient, paint_sweep_gradient, DrawOp,
apply_draw_ops_to_context, paint_linear_gradient, paint_radial_gradient, paint_sweep_gradient,
};
use crate::rasterizer::FAKE_ITALIC_SKEW;
use crate::units::PixelLength;
@ -407,66 +407,6 @@ fn hb_paint_mode_to_operator(mode: hb_paint_composite_mode_t) -> Operator {
}
}
fn apply_draw_ops_to_context(ops: &[DrawOp], context: &Context) -> anyhow::Result<()> {
let mut current = None;
context.new_path();
for op in ops {
match op {
DrawOp::MoveTo { to_x, to_y } => {
context.move_to((*to_x).into(), (*to_y).into());
current.replace((to_x, to_y));
}
DrawOp::LineTo { to_x, to_y } => {
context.line_to((*to_x).into(), (*to_y).into());
current.replace((to_x, to_y));
}
DrawOp::QuadTo {
control_x,
control_y,
to_x,
to_y,
} => {
let (x, y) =
current.ok_or_else(|| anyhow::anyhow!("QuadTo has no current position"))?;
// Express quadratic as a cubic
// <https://stackoverflow.com/a/55034115/149111>
context.curve_to(
(x + (2. / 3.) * (control_x - x)).into(),
(y + (2. / 3.) * (control_y - y)).into(),
(to_x + (2. / 3.) * (control_x - to_x)).into(),
(to_y + (2. / 3.) * (control_y - to_y)).into(),
(*to_x).into(),
(*to_y).into(),
);
current.replace((to_x, to_y));
}
DrawOp::CubicTo {
control1_x,
control1_y,
control2_x,
control2_y,
to_x,
to_y,
} => {
context.curve_to(
(*control1_x).into(),
(*control1_y).into(),
(*control2_x).into(),
(*control2_y).into(),
(*to_x).into(),
(*to_y).into(),
);
current.replace((to_x, to_y));
}
DrawOp::ClosePath => {
context.close_path();
}
}
}
Ok(())
}
fn hb_color_to_rgba(color: hb_color_t) -> (f64, f64, f64, f64) {
let red = unsafe { hb_color_get_red(color) } as f64;
let green = unsafe { hb_color_get_green(color) } as f64;