blockworx/render/watermark.rs
1//! The drawing-office stamp: "Viewing Rev 23" across the sheet while the
2//! time machine is open, the way a print is stamped DRAFT.
3//!
4//! Painted into the canvas rather than into the chrome, and painted first,
5//! so it sits behind the diagram and takes no clicks. Sized off the
6//! *visible* world rect, so it spans the viewport at any zoom — a stamp
7//! belongs to the sheet you are looking at, not to a place in the drawing.
8
9use egui::{Align2, FontId};
10
11use crate::canvas::Renderer;
12use crate::theme::{Role, Style, Theme};
13
14/// How much of the viewport's width the stamp is allowed to fill.
15const SPAN: f32 = 0.8;
16
17/// A first guess at the size, as a fraction of the viewport width; the
18/// measured text then scales it to [`SPAN`] exactly.
19const GUESS: f32 = 0.12;
20
21pub fn draw<R: Renderer>(style: &Style<'_, R>, text: &str) {
22 let Some(visible) = style.visible_world_bounds() else {
23 // A backend with no viewport is an export, and an export of a past
24 // rev is that rev's drawing — not a picture of someone looking at it.
25 return;
26 };
27 let font = Theme::canvas_font_at(visible.width() * GUESS);
28 let measured = style.text_size(text, &font);
29 if measured.x <= 0.0 {
30 return;
31 }
32 let fitted = FontId {
33 size: font.size * (visible.width() * SPAN / measured.x).min(1.0),
34 ..font
35 };
36 style.text(
37 visible.center(),
38 Align2::CENTER_CENTER,
39 text,
40 &fitted,
41 Role::RevWatermark,
42 );
43}