pub struct SnapGlyphParams {
pub glyph_left: f32,
pub glyph_top: f32,
pub render_w: f32,
pub render_h: f32,
pub cell_x0: f32,
pub cell_y0: f32,
pub cell_x1: f32,
pub cell_y1: f32,
pub snap_threshold: f32,
pub extension: f32,
}
pub fn snap_glyph_to_cell(p: SnapGlyphParams) -> (f32, f32, f32, f32) {
let SnapGlyphParams {
glyph_left,
glyph_top,
render_w,
render_h,
cell_x0,
cell_y0,
cell_x1,
cell_y1,
snap_threshold,
extension,
} = p;
let mut new_left = glyph_left;
let mut new_top = glyph_top;
let mut new_w = render_w;
let mut new_h = render_h;
let glyph_right = glyph_left + render_w;
let glyph_bottom = glyph_top + render_h;
if (glyph_left - cell_x0).abs() < snap_threshold {
new_left = cell_x0 - extension;
new_w = glyph_right - new_left;
}
if (glyph_right - cell_x1).abs() < snap_threshold {
new_w = cell_x1 + extension - new_left;
}
if (glyph_top - cell_y0).abs() < snap_threshold {
new_top = cell_y0 - extension;
new_h = glyph_bottom - new_top;
}
if (glyph_bottom - cell_y1).abs() < snap_threshold {
new_h = cell_y1 + extension - new_top;
}
let cell_cx = (cell_x0 + cell_x1) / 2.0;
let cell_cy = (cell_y0 + cell_y1) / 2.0;
if (glyph_bottom - cell_cy).abs() < snap_threshold {
new_h = cell_cy - new_top;
} else if (glyph_top - cell_cy).abs() < snap_threshold {
let bottom = new_top + new_h;
new_top = cell_cy;
new_h = bottom - new_top;
}
if (glyph_right - cell_cx).abs() < snap_threshold {
new_w = cell_cx - new_left;
} else if (glyph_left - cell_cx).abs() < snap_threshold {
let right = new_left + new_w;
new_left = cell_cx;
new_w = right - new_left;
}
(new_left, new_top, new_w, new_h)
}