sl_map_web/routes/
text.rs1use axum::Json;
8use axum::extract::State;
9use serde::{Deserialize, Serialize};
10
11use crate::auth::CurrentUser;
12use crate::error::Error;
13use crate::state::AppState;
14
15#[derive(Debug, Deserialize)]
17pub struct MeasureRequest {
18 pub font_id: String,
20 pub font_px: f32,
22 pub lines: Vec<String>,
24}
25
26#[derive(Debug, Serialize)]
28pub struct MeasureResponse {
29 pub width: u32,
31 pub height: u32,
33}
34
35pub async fn measure(
44 _user: CurrentUser,
45 State(state): State<AppState>,
46 Json(req): Json<MeasureRequest>,
47) -> Result<Json<MeasureResponse>, Error> {
48 if !(req.font_px.is_finite() && req.font_px > 0f32) {
49 return Err(Error::BadRequest(format!(
50 "font size must be a positive number of pixels, got {}",
51 req.font_px
52 )));
53 }
54 let font_path = state
55 .fonts
56 .path_for(&req.font_id)
57 .ok_or_else(|| Error::BadRequest(format!("unknown font_id `{}`", req.font_id)))?;
58 let font = sl_map_apis::text::load_font(font_path)?;
59 let scale = ab_glyph::PxScale::from(req.font_px);
60 let (width, height) = sl_map_apis::text::measure_text(scale, &font, &req.lines);
61 Ok(Json(MeasureResponse { width, height }))
62}