diff --git a/docs/src/sources-fonts.md b/docs/src/sources-fonts.md index a223eb06e..27e013a88 100644 --- a/docs/src/sources-fonts.md +++ b/docs/src/sources-fonts.md @@ -59,6 +59,18 @@ curl http://127.0.0.1:3000/catalog } ``` +we also provide similar information via the `/font/%7Bfontstack%7D/%7Brange%7D` endpoint, because some editors like +[maputnik](https://maputnik.github.io/) require this to render a list of fonts. + +```bash +curl http://127.0.0.1:3000/font/%7Bfontstack%7D/%7Brange%7D +[ + "Overpass Mono Bold", + "Overpass Mono Light", + "Overpass Mono SemiBold", +] +``` + ## Using from CLI A font file or directory can be configured from the [CLI](run-with-cli.md) with one or more `--font` parameters. diff --git a/docs/src/using.md b/docs/src/using.md index 3e5c40567..3df2ed529 100644 --- a/docs/src/using.md +++ b/docs/src/using.md @@ -13,6 +13,7 @@ Martin data is available via the HTTP `GET` endpoints: | `/sprite/{spriteID}[@2x].{json,png}` | [Sprite sources](sources-sprites.md) | | `/sdf_sprite/{spriteID}[@2x].{json,png}` | [SDF Sprite sources](sources-sprites.md) | | `/font/{font}/{start}-{end}` | [Font source](sources-fonts.md) | +| `/font/%7Bfontstack%7D/%7Brange%7D` | [List of all fontstacks](sources-fonts.md) | | `/font/{font1},…,{fontN}/{start}-{end}` | [Composite Font source](sources-fonts.md) | | `/health` | Martin server health check: returns 200 `OK` | diff --git a/martin/src/fonts/mod.rs b/martin/src/fonts/mod.rs index 7ab657844..5d3e11b93 100644 --- a/martin/src/fonts/mod.rs +++ b/martin/src/fonts/mod.rs @@ -155,6 +155,11 @@ impl FontSources { .collect() } + #[must_use] + pub fn get_fontstacks(&self) -> Vec { + self.fonts.keys().cloned().sorted().collect() + } + /// Given a list of IDs in a format "id1,id2,id3", return a combined font. #[allow(clippy::cast_possible_truncation)] pub fn get_font_range(&self, ids: &str, start: u32, end: u32) -> FontResult> { diff --git a/martin/src/srv/fonts.rs b/martin/src/srv/fonts.rs index a8808c17b..8f8794850 100644 --- a/martin/src/srv/fonts.rs +++ b/martin/src/srv/fonts.rs @@ -30,6 +30,18 @@ async fn get_font(path: Path, fonts: Data) -> ActixRes .body(data)) } +/// Returns the available font stacks +/// +/// Separate from the [`/catalog`](crate::srv::server::get_catalog) route to allow editors like +/// [maputnik](https://maputnik.github.io/) to list fonts. +/// To match the route `/font/%7Bfontstack%7D/%7Brange%7D`, macros are not possible due the route +/// clashing with the path-parameter mechanism. +#[allow(clippy::unused_async)] +pub(crate) async fn get_fontstacks(fonts: Data) -> HttpResponse { + let fontstacks = fonts.get_fontstacks(); + HttpResponse::Ok().json(fontstacks) +} + pub fn map_font_error(e: FontError) -> actix_web::Error { #[allow(clippy::enum_glob_use)] use FontError::*; diff --git a/martin/src/srv/server.rs b/martin/src/srv/server.rs index 75af0e6b2..4e14bfacd 100755 --- a/martin/src/srv/server.rs +++ b/martin/src/srv/server.rs @@ -120,7 +120,10 @@ pub fn router(cfg: &mut web::ServiceConfig, #[allow(unused_variables)] usr_cfg: .service(crate::srv::sprites::get_sprite_png); #[cfg(feature = "fonts")] - cfg.service(crate::srv::fonts::get_font); + cfg.service(crate::srv::fonts::get_font).service( + web::resource("/font/{fontstack}/{route}") // matches only /font/%7Bfontstack%7D/%7Brange%7D + .route(web::get().to(crate::srv::fonts::get_fontstacks)), + ); #[cfg(feature = "webui")] { diff --git a/tests/expected/configured/fontstacks.json b/tests/expected/configured/fontstacks.json new file mode 100644 index 000000000..c9f1945ab --- /dev/null +++ b/tests/expected/configured/fontstacks.json @@ -0,0 +1,4 @@ +[ + "Overpass Mono Light", + "Overpass Mono Regular" +] diff --git a/tests/test.sh b/tests/test.sh index e82074f6e..feb2d395f 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -380,6 +380,7 @@ test_png sdf_spr_cmp_2 sdf_sprite/src1,mysrc@2x.png test_font font_1 font/Overpass%20Mono%20Light/0-255 test_font font_2 font/Overpass%20Mono%20Regular/0-255 test_font font_3 font/Overpass%20Mono%20Regular,Overpass%20Mono%20Light/0-255 +test_jsn fontstacks "font/{fontstack}/{range}" # Test comments override test_jsn tbl_comment_cfg MixPoints