Skip to content

Commit

Permalink
Implement dynamic font support /font/<name>/<start>-<end>
Browse files Browse the repository at this point in the history
This will NOT compile until stadiamaps/sdf_font_tools#13 is merged and published

This implements dynamic font protobuf generation, allowing users to request font ranges on the fly, and combining them in any order, e.g. `Font1,Font2,Font3`, same as with sprites and tiles

This is a first iteration, without any multithreading support. In theory, this could be done far faster by generating SDFs with multiple threads.

Current steps:
* during init, figure out all glyphs available in each font, and store them as a bitset
* during request:
  * combine requested bitsets to figure out which glyph should come from which font file
  * load those glyphs from files (using a single instance of the freetype lib)
  * convert them to SDFs and package them into a protobuf
  • Loading branch information
nyurik committed Oct 17, 2023
1 parent a1f794a commit 3af3070
Show file tree
Hide file tree
Showing 10 changed files with 616 additions and 7 deletions.
181 changes: 177 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ actix-rt = "2"
actix-web = "4"
anyhow = "1.0"
async-trait = "0.1"
bit-set = "0.5.3"
brotli = "3"
cargo-husky = { version = "1", features = ["user-hooks"], default-features = false }
clap = { version = "4", features = ["derive"] }
Expand All @@ -36,6 +37,7 @@ log = "0.4"
martin-mbtiles = { path = "./martin-mbtiles", version = "0.6.0", default-features = false }
martin-tile-utils = { path = "./martin-tile-utils", version = "0.1.0" }
num_cpus = "1"
pbf_font_tools = { version = "2.4.0", features = ["freetype"], path = "../sdf_font_tools/pbf_font_tools" }
pmtiles = { version = "0.3", features = ["mmap-async-tokio", "tilejson"] }
postgis = "0.9"
postgres = { version = "0.19", features = ["with-time-0_3", "with-uuid-1", "with-serde_json-1"] }
Expand Down
2 changes: 2 additions & 0 deletions martin-mbtiles/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use sqlite_hashes::rusqlite;

use crate::MbtType;

use crate::mbtiles::MbtType;

#[derive(thiserror::Error, Debug)]
pub enum MbtError {
#[error("The source and destination MBTiles files are the same: {}", .0.display())]
Expand Down
2 changes: 2 additions & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ actix-rt.workspace = true
actix-web.workspace = true
actix.workspace = true
async-trait.workspace = true
bit-set.workspace = true
brotli.workspace = true
clap.workspace = true
deadpool-postgres.workspace = true
Expand All @@ -69,6 +70,7 @@ log.workspace = true
martin-mbtiles.workspace = true
martin-tile-utils.workspace = true
num_cpus.workspace = true
pbf_font_tools.workspace = true
pmtiles.workspace = true
postgis.workspace = true
postgres-protocol.workspace = true
Expand Down
9 changes: 8 additions & 1 deletion 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, Result};
use crate::{Error, OneOrMany, Result};

#[derive(Parser, Debug, PartialEq, Default)]
#[command(about, version)]
Expand Down Expand Up @@ -44,6 +44,9 @@ pub struct MetaArgs {
/// Export a directory with SVG files as a sprite source. Can be specified multiple times.
#[arg(short, long)]
pub sprite: Vec<PathBuf>,
/// Export a directory with font files as a font source. Can be specified multiple times.
#[arg(short, long)]
pub font: Vec<PathBuf>,
}

impl Args {
Expand Down Expand Up @@ -81,6 +84,10 @@ impl Args {
config.sprites = FileConfigEnum::new(self.meta.sprite);
}

if !self.meta.font.is_empty() {
config.fonts = OneOrMany::new_opt(self.meta.font);
}

cli_strings.check()
}
}
Expand Down
16 changes: 15 additions & 1 deletion martin/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::collections::HashMap;
use std::fs::File;
use std::future::Future;
use std::io::prelude::*;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::pin::Pin;

use futures::future::try_join_all;
use serde::{Deserialize, Serialize};
use subst::VariableMap;

use crate::file_config::{resolve_files, FileConfigEnum};
use crate::fonts::{resolve_fonts, FontSources};
use crate::mbtiles::MbtSource;
use crate::pg::PgConfig;
use crate::pmtiles::PmtSource;
Expand All @@ -24,6 +25,7 @@ pub type UnrecognizedValues = HashMap<String, serde_yaml::Value>;
pub struct AllSources {
pub sources: Sources,
pub sprites: SpriteSources,
pub fonts: FontSources,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
Expand All @@ -43,6 +45,9 @@ pub struct Config {
#[serde(skip_serializing_if = "Option::is_none")]
pub sprites: Option<FileConfigEnum>,

#[serde(skip_serializing_if = "Option::is_none")]
pub fonts: Option<OneOrMany<PathBuf>>,

#[serde(flatten)]
pub unrecognized: UnrecognizedValues,
}
Expand Down Expand Up @@ -83,6 +88,14 @@ impl Config {
false
};

any |= if let Some(cfg) = &mut self.fonts {
// TODO: support for unrecognized fonts?
// res.extend(cfg.finalize("fonts.")?);
!cfg.is_empty()
} else {
false
};

if any {
Ok(res)
} else {
Expand Down Expand Up @@ -123,6 +136,7 @@ impl Config {
})
.sort(),
sprites: resolve_sprites(&mut self.sprites)?,
fonts: resolve_fonts(&mut self.fonts)?,
})
}
}
Expand Down
Loading

0 comments on commit 3af3070

Please sign in to comment.