Skip to content

Commit

Permalink
editoast: front: host fonts for the map in editoast
Browse files Browse the repository at this point in the history
The glyphs are generated during the build step

Closes #10627

Signed-off-by: Tristram Gräbener <[email protected]>
  • Loading branch information
Tristramg committed Feb 13, 2025
1 parent 7283410 commit b8bae1b
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions editoast/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
*.snap.new
assets/sprites/*/sprites.*
assets/fonts/glyphs/*
7 changes: 6 additions & 1 deletion editoast/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ COPY --from=static_assets . /assets
#######################
# Build assets #
#######################
FROM alpine:latest AS editoast_assets
FROM ubuntu:latest AS editoast_assets
RUN apt update && apt install --yes wget
RUN wget https://github.com/flother/spreet/releases/download/v0.11.0/spreet-x86_64-unknown-linux-musl.tar.gz
RUN tar xvf spreet-x86_64-unknown-linux-musl.tar.gz --directory /usr/bin
RUN wget http://github.com/stadiamaps/sdf_font_tools/releases/download/cli-v1.4.2/build_pbf_glyphs.x86_64-unknown-linux-gnu -O /usr/bin/build_pbf_glyphs
RUN chmod +x /usr/bin/build_pbf_glyphs
COPY ./assets /assets
RUN /assets/sprites/generate-atlas.sh
RUN /assets/fonts/generate-glyphs.sh

######################
# Testing env: build #
Expand All @@ -40,6 +44,7 @@ RUN rustup component add llvm-tools && \
COPY --from=planner /editoast/recipe.json recipe.json
COPY --from=planner /editoast/editoast_derive/ editoast_derive
COPY --from=test_data . /tests/data
COPY --from=editoast_assets /assets /assets
ENV RUSTFLAGS="-Cinstrument-coverage -C target-feature=-crt-static -C link-arg=-fuse-ld=mold"
ENV LLVM_PROFILE_FILE="editoast-%p-%m.profraw"
RUN cargo chef cook --tests --recipe-path recipe.json
Expand Down
2 changes: 2 additions & 0 deletions editoast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $ diesel migration run
# build the assets
$ cargo install spreet
$ ./assets/sprites/generate-atlas.sh
$ cargo install build_pbf_glyphs
$ ./assets/fonts/generate-glyphs.sh
# Build and run
$ cargo build
$ cargo run -- runserver
Expand Down
Binary file added editoast/assets/fonts/Roboto Bold.ttf
Binary file not shown.
Binary file not shown.
Binary file added editoast/assets/fonts/Roboto Condensed.ttf
Binary file not shown.
Binary file added editoast/assets/fonts/Roboto Italic.ttf
Binary file not shown.
Binary file added editoast/assets/fonts/Roboto Medium.ttf
Binary file not shown.
Binary file added editoast/assets/fonts/Roboto Regular.ttf
Binary file not shown.
Binary file added editoast/assets/fonts/SNCF.ttf
Binary file not shown.
8 changes: 8 additions & 0 deletions editoast/assets/fonts/generate-glyphs.sh
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/
92 changes: 92 additions & 0 deletions editoast/src/views/fonts.rs
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);
}
}
2 changes: 2 additions & 0 deletions editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod authz;
mod documents;
pub mod electrical_profiles;
pub mod fonts;
pub mod infra;
mod layers;
mod openapi;
Expand Down Expand Up @@ -99,6 +100,7 @@ crate::routes! {
&authz,
&documents,
&electrical_profiles,
&fonts,
&infra,
&layers,
&projects,
Expand Down

0 comments on commit b8bae1b

Please sign in to comment.