Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added /font/%7Bfontstack%7D/%7Brange%7D #1612

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/src/sources-fonts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions docs/src/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |

Expand Down
6 changes: 6 additions & 0 deletions martin/src/fonts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ impl FontSources {
.collect()
}

/// Returns an alphabetically sorted list of font stacks (i.e. the name of the font)
#[must_use]
pub fn get_fontstacks(&self) -> Vec<String> {
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<Vec<u8>> {
Expand Down
12 changes: 12 additions & 0 deletions martin/src/srv/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ async fn get_font(path: Path<FontRequest>, fonts: Data<FontSources>) -> 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<FontSources>) -> 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::*;
Expand Down
5 changes: 4 additions & 1 deletion martin/src/srv/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
{
Expand Down
4 changes: 4 additions & 0 deletions tests/expected/configured/fontstacks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"Overpass Mono Light",
"Overpass Mono Regular"
]
1 change: 1 addition & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ test_png sdf_spr_cmp_2 sdf_sprite/src1,[email protected]
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
Expand Down
Loading