use super::{
DiagramFrame, DiagramStyle, LEADER_LINE_LENGTH, LEADER_LINE_MIN_SPAN, MIN_LABEL_SPAN,
fill::{draw_edge, to_px},
layout::PanelLayout,
};
use crate::pixel_font::{GLYPH_HEIGHT, GLYPH_WIDTH, glyph};
pub fn draw_panel_labels(
frame: &mut DiagramFrame,
layout: &PanelLayout,
style: &DiagramStyle,
label_spots: Vec<(usize, f32, f32, f32, f32)>,
) {
for (facet_id, cx, cy, w, h) in label_spots {
if w < LEADER_LINE_MIN_SPAN || h < LEADER_LINE_MIN_SPAN {
continue;
}
let Some(label) = style.facet_labels.get(facet_id) else {
continue;
};
if label.is_empty() {
continue;
}
let Some(idx) = frame.idx(to_px(cx.round()), to_px(cy.round())) else {
continue;
};
if frame.pick[idx] != u32::try_from(facet_id).expect("a facet id fits in u32") + 1 {
continue;
}
let (label_mid_x, label_mid_y) = if w < MIN_LABEL_SPAN || h < MIN_LABEL_SPAN {
let (dir_x, dir_y) = {
let (dx, dy) = (cx - layout.center_x, cy - layout.center_y);
let dist = dx.hypot(dy).max(1e-3);
(dx / dist, dy / dist)
};
let leader_end = (
dir_x.mul_add(LEADER_LINE_LENGTH, cx),
dir_y.mul_add(LEADER_LINE_LENGTH, cy),
);
let depth_here = frame.depth[idx];
draw_edge(
frame,
(cx, cy, depth_here),
(leader_end.0, leader_end.1, depth_here),
style.edge_color,
layout.clip,
1,
);
leader_end
} else {
(cx, cy)
};
let (label_w, label_h) = text_size(label, 1);
draw_text(
frame,
label_mid_x - label_w as f32 / 2.0,
label_mid_y - label_h as f32 / 2.0,
label,
1,
style.edge_color,
Some(layout.clip),
);
}
}
pub fn text_size(text: &str, scale: u32) -> (u32, u32) {
let len = u32::try_from(text.chars().count()).expect("a label's length fits in u32");
let glyph_height = u32::try_from(GLYPH_HEIGHT).expect("the glyph height fits in u32");
if len == 0 {
return (0, glyph_height * scale);
}
let glyph_width = u32::try_from(GLYPH_WIDTH).expect("the glyph width fits in u32");
let width = (len * (glyph_width + 1) - 1) * scale;
(width, glyph_height * scale)
}
pub fn draw_text(
frame: &mut DiagramFrame,
x: f32,
y: f32,
text: &str,
scale: u32,
color: [u8; 3],
clip: Option<(i32, i32, i32, i32)>,
) {
let (width, height) = (frame.width, frame.height);
draw_text_into_buffer(
TextCanvas {
color_buf: &mut frame.color,
width,
height,
},
x,
y,
text,
scale,
color,
clip,
);
}
pub struct TextCanvas<'a> {
pub color_buf: &'a mut [u8],
pub width: u32,
pub height: u32,
}
pub fn draw_text_into_buffer(
canvas: TextCanvas<'_>,
x: f32,
y: f32,
text: &str,
scale: u32,
color: [u8; 3],
clip: Option<(i32, i32, i32, i32)>,
) {
let TextCanvas {
color_buf,
width,
height,
} = canvas;
let clip = clip.unwrap_or_else(|| (0, 0, width.cast_signed() - 1, height.cast_signed() - 1));
let scale = scale.max(1).cast_signed();
let glyph_width = i32::try_from(GLYPH_WIDTH).expect("the glyph width fits in i32");
let mut pen_x = to_px(x.round());
let pen_y = to_px(y.round());
for ch in text.chars() {
let rows = glyph(ch);
for (row, bits) in rows.iter().enumerate() {
for col in 0..GLYPH_WIDTH {
if bits & (1 << (GLYPH_WIDTH - 1 - col)) == 0 {
continue;
}
let col_offset = i32::try_from(col).expect("a glyph column fits in i32");
let row_offset = i32::try_from(row).expect("a glyph row fits in i32");
let px0 = pen_x + col_offset * scale;
let py0 = pen_y + row_offset * scale;
for sy in 0..scale {
for sx in 0..scale {
let (px, py) = (px0 + sx, py0 + sy);
if px < clip.0 || py < clip.1 || px > clip.2 || py > clip.3 {
continue;
}
if px < 0
|| py < 0
|| px.cast_unsigned() >= width
|| py.cast_unsigned() >= height
{
continue;
}
let idx = (py.cast_unsigned() * width + px.cast_unsigned()) as usize;
let o = idx * 4;
color_buf[o] = color[0];
color_buf[o + 1] = color[1];
color_buf[o + 2] = color[2];
color_buf[o + 3] = 255;
}
}
}
}
pen_x += (glyph_width + 1) * scale;
}
}