-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
editoast: front: host fonts for the map in editoast
The glyphs are generated during the build step Closes #10627 Signed-off-by: Tristram Gräbener <[email protected]>
- Loading branch information
Showing
13 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
target | ||
*.snap.new | ||
assets/sprites/*/sprites.* | ||
assets/fonts/glyphs/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/sh | ||
|
||
# This script should be used to generate glyphs from ttf fonts. | ||
# Those glyphs are used to display text on the map | ||
# You will need build_pbf_glyphs, you can install it with: | ||
# `$ cargo install build_pbf_glyphs` | ||
|
||
build_pbf_glyphs assets/fonts/ assetss/fonts/glyphs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use axum::extract::Path; | ||
use axum::extract::Request; | ||
use axum::response::IntoResponse; | ||
use axum::Extension; | ||
use editoast_authz::BuiltinRole; | ||
use editoast_derive::EditoastError; | ||
use thiserror::Error; | ||
use tower::ServiceExt; | ||
use tower_http::services::ServeFile; | ||
|
||
use crate::client::get_dynamic_assets_path; | ||
use crate::error::Result; | ||
use crate::views::AuthenticationExt; | ||
use crate::views::AuthorizationError; | ||
|
||
crate::routes! { | ||
"/fonts/{font}/{glyph}" => fonts, | ||
} | ||
|
||
#[derive(Debug, Error, EditoastError)] | ||
#[editoast_error(base_id = "fonts")] | ||
enum FontErrors { | ||
#[error("File '{file}' not found")] | ||
#[editoast_error(status = 404)] | ||
FileNotFound { file: String }, | ||
} | ||
|
||
/// This endpoint is used by map libre to retrieve the fonts. They are separated by font and unicode block | ||
#[utoipa::path( | ||
get, path = "", | ||
tag = "fonts", | ||
params( | ||
("font" = String, Path, description = "Requested font"), | ||
("glyph" = String, Path, description = "Requested unicode block"), | ||
), | ||
responses( | ||
(status = 200, description = "Glyphs in PBF format of the font at the requested unicode block"), | ||
(status = 404, description = "Font not found"), | ||
), | ||
)] | ||
async fn fonts( | ||
Extension(auth): AuthenticationExt, | ||
Path((font, file_name)): Path<(String, String)>, | ||
request: Request, | ||
) -> Result<impl IntoResponse> { | ||
let authorized = auth | ||
.check_roles([BuiltinRole::MapRead].into()) | ||
.await | ||
.map_err(AuthorizationError::AuthError)?; | ||
if !authorized { | ||
return Err(AuthorizationError::Forbidden.into()); | ||
} | ||
|
||
let path = get_dynamic_assets_path().join(format!("fonts/glyphs/{font}/{file_name}")); | ||
|
||
println!("{:?}", path); | ||
if !path.is_file() { | ||
return Err(FontErrors::FileNotFound { file: file_name }.into()); | ||
} | ||
|
||
Ok(ServeFile::new(&path).oneshot(request).await) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::views::test_app::TestAppBuilder; | ||
|
||
use super::*; | ||
use axum::http::StatusCode; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
async fn test_font() { | ||
let app = TestAppBuilder::default_app(); | ||
let request = app.get("/fonts/Roboto%20Bold/0-255.pbf"); | ||
let response = app.fetch(request).assert_status(StatusCode::OK); | ||
assert_eq!("application/octet-stream", response.content_type()); | ||
let response = response.bytes(); | ||
let expected = | ||
std::fs::read(get_dynamic_assets_path().join("fonts/glyphs/Roboto Bold/0-255.pbf")) | ||
.unwrap(); | ||
assert_eq!(response, expected); | ||
} | ||
|
||
#[rstest] | ||
async fn test_font_not_found() { | ||
let app = TestAppBuilder::default_app(); | ||
let request = app.get("/fonts/Comic%20Sans/0-255.pbf"); | ||
app.fetch(request).assert_status(StatusCode::NOT_FOUND); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters