From d25db188fc59cb3c9a08b800a4bb5be342e7e175 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 20 Oct 2023 14:27:21 -0400 Subject: [PATCH] updates --- martin/src/args/root.rs | 4 +- martin/src/config.rs | 6 +- martin/src/fonts/mod.rs | 64 +++++++++++----------- martin/tests/mb_server_test.rs | 2 + martin/tests/pg_server_test.rs | 1 + martin/tests/pmt_server_test.rs | 2 + tests/expected/auto/catalog_auto.json | 3 +- tests/expected/configured/catalog_cfg.json | 3 +- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/martin/src/args/root.rs b/martin/src/args/root.rs index 8e2a1fba5..90a6b0cf9 100644 --- a/martin/src/args/root.rs +++ b/martin/src/args/root.rs @@ -10,7 +10,7 @@ use crate::args::srv::SrvArgs; use crate::args::State::{Ignore, Share, Take}; use crate::config::Config; use crate::file_config::FileConfigEnum; -use crate::{Error, OneOrMany, Result}; +use crate::{Error, OptOneMany, Result}; #[derive(Parser, Debug, PartialEq, Default)] #[command(about, version)] @@ -85,7 +85,7 @@ impl Args { } if !self.meta.font.is_empty() { - config.fonts = OneOrMany::new_opt(self.meta.font); + config.fonts = OptOneMany::new(self.meta.font); } cli_strings.check() diff --git a/martin/src/config.rs b/martin/src/config.rs index 9ed340bb5..625485e01 100644 --- a/martin/src/config.rs +++ b/martin/src/config.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use subst::VariableMap; use crate::file_config::{resolve_files, FileConfigEnum}; -use crate::fonts::{resolve_fonts, FontSources}; +use crate::fonts::FontSources; use crate::mbtiles::MbtSource; use crate::pg::PgConfig; use crate::pmtiles::PmtSource; @@ -45,7 +45,7 @@ pub struct Config { #[serde(default, skip_serializing_if = "FileConfigEnum::is_none")] pub sprites: FileConfigEnum, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "OptOneMany::is_none")] pub fonts: OptOneMany, #[serde(flatten)] @@ -85,7 +85,7 @@ impl Config { Ok(ServerState { tiles: self.resolve_tile_sources(idr).await?, sprites: SpriteSources::resolve(&mut self.sprites)?, - fonts: resolve_fonts(&mut self.fonts)?, + fonts: FontSources::resolve(&mut self.fonts)?, }) } diff --git a/martin/src/fonts/mod.rs b/martin/src/fonts/mod.rs index 955ce89b7..1d0e8a59f 100644 --- a/martin/src/fonts/mod.rs +++ b/martin/src/fonts/mod.rs @@ -15,7 +15,7 @@ use regex::Regex; use serde::{Deserialize, Serialize}; use crate::fonts::FontError::IoError; -use crate::OneOrMany; +use crate::OptOneMany; const MAX_UNICODE_CP: usize = 0xFFFF; const CP_RANGE_SIZE: usize = 256; @@ -208,37 +208,6 @@ fn get_available_codepoints(face: &mut Face) -> Option { } } -pub fn resolve_fonts(config: &mut Option>) -> Result { - let Some(cfg) = config else { - return Ok(FontSources::default()); - }; - - let mut fonts = HashMap::new(); - let lib = Library::init()?; - - for path in cfg.iter() { - let disp_path = path.display(); - if path.exists() { - recurse_dirs(&lib, path, &mut fonts)?; - } else { - warn!("Ignoring non-existent font source {disp_path}"); - }; - } - - let mut masks = Vec::with_capacity(MAX_UNICODE_CP_RANGE_ID + 1); - - let mut bs = BitSet::with_capacity(CP_RANGE_SIZE); - for v in 0..=MAX_UNICODE_CP { - bs.insert(v); - if v % CP_RANGE_SIZE == (CP_RANGE_SIZE - 1) { - masks.push(bs); - bs = BitSet::with_capacity(CP_RANGE_SIZE); - } - } - - Ok(FontSources { fonts, masks }) -} - #[derive(Debug, Clone, Default)] pub struct FontSources { fonts: HashMap, @@ -258,6 +227,37 @@ pub struct CatalogFontEntry { } impl FontSources { + pub fn resolve(config: &mut OptOneMany) -> Result { + if config.is_empty() { + return Ok(Self::default()); + } + + let mut fonts = HashMap::new(); + let lib = Library::init()?; + + for path in config.iter() { + let disp_path = path.display(); + if path.exists() { + recurse_dirs(&lib, path, &mut fonts)?; + } else { + warn!("Ignoring non-existent font source {disp_path}"); + }; + } + + let mut masks = Vec::with_capacity(MAX_UNICODE_CP_RANGE_ID + 1); + + let mut bs = BitSet::with_capacity(CP_RANGE_SIZE); + for v in 0..=MAX_UNICODE_CP { + bs.insert(v); + if v % CP_RANGE_SIZE == (CP_RANGE_SIZE - 1) { + masks.push(bs); + bs = BitSet::with_capacity(CP_RANGE_SIZE); + } + } + + Ok(Self { fonts, masks }) + } + #[must_use] pub fn get_catalog(&self) -> FontCatalog { self.fonts diff --git a/martin/tests/mb_server_test.rs b/martin/tests/mb_server_test.rs index 31f3177ef..046920f24 100644 --- a/martin/tests/mb_server_test.rs +++ b/martin/tests/mb_server_test.rs @@ -69,6 +69,7 @@ async fn mbt_get_catalog() { content_type: image/webp name: ne2sr sprites: {} + fonts: {} "###); } @@ -100,6 +101,7 @@ async fn mbt_get_catalog_gzip() { content_type: image/webp name: ne2sr sprites: {} + fonts: {} "###); } diff --git a/martin/tests/pg_server_test.rs b/martin/tests/pg_server_test.rs index 341e9aabc..3a48728a3 100644 --- a/martin/tests/pg_server_test.rs +++ b/martin/tests/pg_server_test.rs @@ -115,6 +115,7 @@ postgres: content_type: application/x-protobuf description: public.table_source_multiple_geom.geom2 sprites: {} + fonts: {} "###); } diff --git a/martin/tests/pmt_server_test.rs b/martin/tests/pmt_server_test.rs index 3ed778313..b9f89628d 100644 --- a/martin/tests/pmt_server_test.rs +++ b/martin/tests/pmt_server_test.rs @@ -54,6 +54,7 @@ async fn pmt_get_catalog() { stamen_toner__raster_CC-BY-ODbL_z3: content_type: image/png sprites: {} + fonts: {} "###); } @@ -72,6 +73,7 @@ async fn pmt_get_catalog_gzip() { p_png: content_type: image/png sprites: {} + fonts: {} "###); } diff --git a/tests/expected/auto/catalog_auto.json b/tests/expected/auto/catalog_auto.json index 3ce4162f5..59082de5e 100644 --- a/tests/expected/auto/catalog_auto.json +++ b/tests/expected/auto/catalog_auto.json @@ -163,5 +163,6 @@ "description": "Major cities from Natural Earth data" } }, - "sprites": {} + "sprites": {}, + "fonts": {} } diff --git a/tests/expected/configured/catalog_cfg.json b/tests/expected/configured/catalog_cfg.json index 2fb48ab5d..b6cee7284 100644 --- a/tests/expected/configured/catalog_cfg.json +++ b/tests/expected/configured/catalog_cfg.json @@ -53,5 +53,6 @@ "sub/circle" ] } - } + }, + "fonts": {} }