From c030d0f13546c4cab018fbdd63756b4eec3fb17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Ka=C5=BAmierczak?= Date: Tue, 14 Jan 2025 15:00:41 +0100 Subject: [PATCH] Add `register_font` endpoint (#918) --- Cargo.lock | 83 +++++++++++++++++++++------------- Cargo.toml | 3 +- src/config.rs | 7 +++ src/routes.rs | 3 ++ src/routes/register_request.rs | 32 ++++++++++++- src/state.rs | 3 +- 6 files changed, 96 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db68fa1b4..c4b3ae267 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -301,7 +301,7 @@ checksum = "965c2d33e53cb6b267e148a4cb0760bc01f4904c1cd4bb4002a085bb016d1490" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", "synstructure 0.13.1", ] @@ -324,7 +324,7 @@ checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -335,7 +335,7 @@ checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -381,6 +381,7 @@ dependencies = [ "matchit", "memchr", "mime", + "multer", "percent-encoding", "pin-project-lite", "rustversion", @@ -486,7 +487,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.75", + "syn 2.0.96", "which", ] @@ -505,7 +506,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -613,7 +614,7 @@ checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -624,9 +625,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" -version = "1.4.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "calloop" @@ -800,7 +801,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -1220,7 +1221,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -1247,7 +1248,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -1264,7 +1265,7 @@ checksum = "35de3b547387863c8f82013c4f79f1c2162edee956383e4089e1d04c18c4f16c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -1406,7 +1407,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -1713,7 +1714,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -1805,7 +1806,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -2644,6 +2645,7 @@ dependencies = [ "ffmpeg-next", "fs_extra", "futures-util", + "glyphon", "http-body-util", "image", "libc", @@ -2868,6 +2870,23 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96a1fe2275b68991faded2c80aa4a33dba398b77d276038b8f50701a22e55918" +[[package]] +name = "multer" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83e87776546dc87511aa5ee218730c92b666d7264ab6ed41f9d215af9cd5224b" +dependencies = [ + "bytes", + "encoding_rs", + "futures-util", + "http", + "httparse", + "memchr", + "mime", + "spin", + "version_check", +] + [[package]] name = "naga" version = "22.1.0" @@ -3106,7 +3125,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -3193,7 +3212,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -3361,7 +3380,7 @@ checksum = "ec2e072ecce94ec471b13398d5402c188e76ac03cf74dd1a975161b23a3f6d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -3478,7 +3497,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c64d9ba0963cdcea2e1b2230fbae2bab30eb25a174be395c41e764bfb65dd62" dependencies = [ "proc-macro2", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -3510,9 +3529,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" dependencies = [ "unicode-ident", ] @@ -4239,7 +4258,7 @@ checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -4607,9 +4626,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.75" +version = "2.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" +checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" dependencies = [ "proc-macro2", "quote", @@ -4651,7 +4670,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -4723,7 +4742,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -4883,7 +4902,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -4998,7 +5017,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -5496,7 +5515,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", "wasm-bindgen-shared", ] @@ -5530,7 +5549,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -6595,7 +6614,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] @@ -6615,7 +6634,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.75", + "syn 2.0.96", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index c0261eb4a..b7c10c00f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,9 +95,10 @@ rand = { workspace = true } reqwest = { workspace = true } rubato = { workspace = true } tokio = { workspace = true } -axum = { version = "0.7.4", features = ["ws"] } +axum = { version = "0.7.4", features = ["ws", "multipart"] } futures-util = { workspace = true } wgpu = { workspace = true } +glyphon = { workspace = true } http-body-util = "0.1.2" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/src/config.rs b/src/config.rs index c6a11c91c..4d24287b7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,6 +26,7 @@ pub struct Config { pub output_sample_rate: u32, pub stun_servers: Arc>, pub required_wgpu_features: WgpuFeatures, + pub load_system_fonts: bool, } #[derive(Debug, Clone)] @@ -191,6 +192,11 @@ fn try_read_config() -> Result { Err(_) => queue::DEFAULT_BUFFER_DURATION, }; + let load_system_fonts = match env::var("LIVE_COMPOSITOR_LOAD_SYSTEM_FONTS") { + Ok(enable) => bool_env_from_str(&enable).unwrap_or(true), + Err(_) => true, + }; + let log_file = match env::var("LIVE_COMPOSITOR_LOG_FILE") { Ok(path) => Some(Arc::from(PathBuf::from(path))), Err(_) => None, @@ -236,6 +242,7 @@ fn try_read_config() -> Result { output_sample_rate, stun_servers, required_wgpu_features, + load_system_fonts, }; Ok(config) } diff --git a/src/routes.rs b/src/routes.rs index 6a41f3568..f18be9442 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -56,6 +56,8 @@ pub fn routes(state: ApiState) -> Router { .route("/:id/register", post(register_request::handle_shader)) .route("/:id/unregister", post(unregister_request::handle_shader)); + let font = Router::new().route("/register", post(register_request::handle_font)); + async fn handle_start(State(state): State) -> Result { Pipeline::start(&state.pipeline); Ok(Response::Ok {}) @@ -67,6 +69,7 @@ pub fn routes(state: ApiState) -> Router { .nest("/api/image", image) .nest("/api/web-renderer", web) .nest("/api/shader", shader) + .nest("/api/font", font) // Start request .route("/api/start", post(handle_start)) // WebSocket - events diff --git a/src/routes/register_request.rs b/src/routes/register_request.rs index 9fcff502d..a0f0b50d8 100644 --- a/src/routes/register_request.rs +++ b/src/routes/register_request.rs @@ -1,5 +1,8 @@ -use axum::extract::{Path, State}; +use std::sync::Arc; + +use axum::extract::{Multipart, Path, State}; use compositor_pipeline::pipeline::{input::InputInitInfo, Port}; +use glyphon::fontdb::Source; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -139,3 +142,30 @@ pub(super) async fn handle_image( .await .unwrap() } + +pub(super) async fn handle_font( + State(api): State, + mut multipart: Multipart, +) -> Result { + let Some(field) = multipart + .next_field() + .await + .map_err(|err| ApiError::malformed_request(&err))? + else { + return Err(ApiError::malformed_request(&"Missing font file")); + }; + + let bytes = field + .bytes() + .await + .map_err(|err| ApiError::malformed_request(&err))?; + + let binary_font_source = Source::Binary(Arc::new(bytes)); + + tokio::task::spawn_blocking(move || { + Pipeline::register_font(&api.pipeline.lock().unwrap(), binary_font_source); + Ok(Response::Ok {}) + }) + .await + .unwrap() +} diff --git a/src/state.rs b/src/state.rs index 7b2464f31..a6b46a25c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -50,6 +50,7 @@ impl ApiState { output_sample_rate, stun_servers, required_wgpu_features, + load_system_fonts, .. } = config.clone(); let (pipeline, event_loop) = Pipeline::new(pipeline::Options { @@ -62,7 +63,7 @@ impl ApiState { stun_servers, wgpu_features: required_wgpu_features, wgpu_ctx: None, - load_system_fonts: Some(true), + load_system_fonts: Some(load_system_fonts), tokio_rt: Some(runtime), })?; Ok((