Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Oct 28, 2023
1 parent 80998d2 commit d25db18
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 39 deletions.
4 changes: 2 additions & 2 deletions martin/src/args/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PathBuf>,

#[serde(flatten)]
Expand Down Expand Up @@ -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)?,
})
}

Expand Down
64 changes: 32 additions & 32 deletions martin/src/fonts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,37 +208,6 @@ fn get_available_codepoints(face: &mut Face) -> Option<GetGlyphInfo> {
}
}

pub fn resolve_fonts(config: &mut Option<OneOrMany<PathBuf>>) -> Result<FontSources, FontError> {
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<String, FontSource>,
Expand All @@ -258,6 +227,37 @@ pub struct CatalogFontEntry {
}

impl FontSources {
pub fn resolve(config: &mut OptOneMany<PathBuf>) -> Result<Self, FontError> {
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
Expand Down
2 changes: 2 additions & 0 deletions martin/tests/mb_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ async fn mbt_get_catalog() {
content_type: image/webp
name: ne2sr
sprites: {}
fonts: {}
"###);
}

Expand Down Expand Up @@ -100,6 +101,7 @@ async fn mbt_get_catalog_gzip() {
content_type: image/webp
name: ne2sr
sprites: {}
fonts: {}
"###);
}

Expand Down
1 change: 1 addition & 0 deletions martin/tests/pg_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ postgres:
content_type: application/x-protobuf
description: public.table_source_multiple_geom.geom2
sprites: {}
fonts: {}
"###);
}

Expand Down
2 changes: 2 additions & 0 deletions martin/tests/pmt_server_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ async fn pmt_get_catalog() {
stamen_toner__raster_CC-BY-ODbL_z3:
content_type: image/png
sprites: {}
fonts: {}
"###);
}

Expand All @@ -72,6 +73,7 @@ async fn pmt_get_catalog_gzip() {
p_png:
content_type: image/png
sprites: {}
fonts: {}
"###);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/expected/auto/catalog_auto.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,6 @@
"description": "Major cities from Natural Earth data"
}
},
"sprites": {}
"sprites": {},
"fonts": {}
}
3 changes: 2 additions & 1 deletion tests/expected/configured/catalog_cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
"sub/circle"
]
}
}
},
"fonts": {}
}

0 comments on commit d25db18

Please sign in to comment.