abstreet/widgetry/src/assets.rs

146 lines
5.3 KiB
Rust
Raw Normal View History

2020-02-08 01:01:23 +03:00
use crate::text::Font;
2020-09-26 01:09:18 +03:00
use crate::{text, EventCtx, GeomBatch, GfxCtx};
2020-02-09 03:27:45 +03:00
use geom::Bounds;
use lru::LruCache;
use std::cell::RefCell;
use std::collections::HashMap;
use usvg::fontdb;
use usvg::Options;
// TODO We don't need refcell maybe? Can we take &mut Assets?
2019-11-29 09:41:08 +03:00
pub struct Assets {
pub default_line_height: RefCell<f64>,
text_cache: RefCell<LruCache<String, GeomBatch>>,
2020-02-08 01:01:23 +03:00
line_height_cache: RefCell<HashMap<(Font, usize), f64>>,
// Keyed by filename, then scale factor mangled into a hashable form. Tuple doesn't work
// because of borrowing.
svg_cache: RefCell<HashMap<String, (GeomBatch, Bounds)>>,
font_to_id: HashMap<Font, fontdb::ID>,
pub text_opts: Options,
2019-11-29 09:41:08 +03:00
}
impl Assets {
2020-09-21 20:18:02 +03:00
pub fn new() -> Assets {
2019-11-29 09:41:08 +03:00
let mut a = Assets {
default_line_height: RefCell::new(0.0),
text_cache: RefCell::new(LruCache::new(500)),
2020-02-08 01:01:23 +03:00
line_height_cache: RefCell::new(HashMap::new()),
2020-02-09 03:27:45 +03:00
svg_cache: RefCell::new(HashMap::new()),
font_to_id: HashMap::new(),
text_opts: Options::default(),
2019-11-29 09:41:08 +03:00
};
2020-09-21 20:18:02 +03:00
// TODO These paths are now hardcoded for WASM. This is reasonable, since the fonts
// available are currently fixed anyway. For widgetry to become a library, need to figure
// out how to override these.
a.text_opts.fontdb = fontdb::Database::new();
a.text_opts.fontdb.load_font_data(
include_bytes!("../../data/system/fonts/BungeeInline-Regular.ttf").to_vec(),
);
a.text_opts
.fontdb
.load_font_data(include_bytes!("../../data/system/fonts/Bungee-Regular.ttf").to_vec());
a.text_opts
.fontdb
.load_font_data(include_bytes!("../../data/system/fonts/Overpass-Bold.ttf").to_vec());
a.text_opts.fontdb.load_font_data(
include_bytes!("../../data/system/fonts/OverpassMono-Bold.ttf").to_vec(),
);
a.text_opts.fontdb.load_font_data(
include_bytes!("../../data/system/fonts/Overpass-Regular.ttf").to_vec(),
);
a.text_opts.fontdb.load_font_data(
include_bytes!("../../data/system/fonts/Overpass-SemiBold.ttf").to_vec(),
);
a.text_opts.fontdb.load_font_data(
include_bytes!("../../data/system/fonts/ZCOOLXiaoWei-Regular.ttf").to_vec(),
);
for font in vec![
Font::BungeeInlineRegular,
Font::BungeeRegular,
Font::OverpassBold,
Font::OverpassRegular,
Font::OverpassSemiBold,
Font::OverpassMonoBold,
Font::ZcoolXiaoWei,
] {
a.font_to_id.insert(
font,
a.text_opts
.fontdb
.query(&fontdb::Query {
families: &vec![fontdb::Family::Name(font.family())],
weight: match font {
Font::OverpassBold | Font::OverpassMonoBold => fontdb::Weight::BOLD,
Font::OverpassSemiBold => fontdb::Weight::SEMIBOLD,
_ => fontdb::Weight::NORMAL,
},
stretch: fontdb::Stretch::Normal,
style: fontdb::Style::Normal,
})
.unwrap(),
);
}
*a.default_line_height.borrow_mut() =
a.line_height(text::DEFAULT_FONT, text::DEFAULT_FONT_SIZE);
2019-11-29 09:41:08 +03:00
a
}
2020-02-08 01:01:23 +03:00
pub fn line_height(&self, font: Font, font_size: usize) -> f64 {
let key = (font, font_size);
if let Some(height) = self.line_height_cache.borrow().get(&key) {
return *height;
}
// This seems to be missing line_gap, and line_gap is 0, so manually adjust here.
let line_height = self
.text_opts
.fontdb
.with_face_data(self.font_to_id[&font], |data, face_index| {
2020-09-24 19:36:26 +03:00
let font = ttf_parser::Face::from_slice(data, face_index).unwrap();
let units_per_em = font.units_per_em().unwrap();
let ascent = font.ascender();
let descent = font.descender();
let scale = (font_size as f64) / (units_per_em as f64);
((ascent - descent) as f64) * scale
2020-02-08 01:01:23 +03:00
})
.unwrap();
let height = text::SCALE_LINE_HEIGHT * line_height;
2020-02-08 01:01:23 +03:00
self.line_height_cache.borrow_mut().insert(key, height);
height
2019-11-29 09:41:08 +03:00
}
pub fn get_cached_text(&self, key: &String) -> Option<GeomBatch> {
self.text_cache.borrow_mut().get(key).cloned()
}
pub fn cache_text(&self, key: String, geom: GeomBatch) {
self.text_cache.borrow_mut().put(key, geom);
}
2020-02-09 03:27:45 +03:00
pub fn get_cached_svg(&self, key: &str) -> Option<(GeomBatch, Bounds)> {
self.svg_cache.borrow().get(key).cloned()
2020-02-09 03:27:45 +03:00
}
pub fn cache_svg(&self, key: String, geom: GeomBatch, bounds: Bounds) {
self.svg_cache.borrow_mut().insert(key, (geom, bounds));
}
2019-11-29 09:41:08 +03:00
}
2020-09-26 01:09:18 +03:00
impl std::convert::AsRef<Assets> for GfxCtx<'_> {
fn as_ref(&self) -> &Assets {
&self.prerender.assets
}
}
impl std::convert::AsRef<Assets> for EventCtx<'_> {
fn as_ref(&self) -> &Assets {
&self.prerender.assets
}
}
impl std::convert::AsRef<Assets> for Assets {
fn as_ref(&self) -> &Assets {
&self
}
}