2020-02-08 01:01:23 +03:00
|
|
|
use crate::text::Font;
|
2020-02-09 00:08:31 +03:00
|
|
|
use crate::{text, GeomBatch};
|
2020-02-09 03:27:45 +03:00
|
|
|
use geom::Bounds;
|
2020-02-09 00:25:48 +03:00
|
|
|
use lru::LruCache;
|
2020-02-07 09:30:51 +03:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
2020-08-08 00:41:09 +03:00
|
|
|
#[cfg(not(feature = "wasm-backend"))]
|
|
|
|
use usvg::fontdb;
|
|
|
|
use usvg::Options;
|
2020-02-07 09:30:51 +03:00
|
|
|
|
|
|
|
// TODO We don't need refcell maybe? Can we take &mut Assets?
|
2019-11-29 09:41:08 +03:00
|
|
|
pub struct Assets {
|
2020-03-02 23:02:25 +03:00
|
|
|
pub default_line_height: RefCell<f64>,
|
2020-02-09 00:25:48 +03:00
|
|
|
text_cache: RefCell<LruCache<String, GeomBatch>>,
|
2020-02-08 01:01:23 +03:00
|
|
|
line_height_cache: RefCell<HashMap<(Font, usize), f64>>,
|
2020-05-18 01:40:04 +03:00
|
|
|
// Keyed by filename, then scale factor mangled into a hashable form. Tuple doesn't work
|
|
|
|
// because of borrowing.
|
2020-07-29 00:39:38 +03:00
|
|
|
svg_cache: RefCell<HashMap<String, (GeomBatch, Bounds)>>,
|
2020-08-08 00:41:09 +03:00
|
|
|
#[cfg(not(feature = "wasm-backend"))]
|
2020-06-26 21:22:22 +03:00
|
|
|
font_to_id: HashMap<Font, fontdb::ID>,
|
2020-02-07 22:51:41 +03:00
|
|
|
pub text_opts: Options,
|
2019-11-29 09:41:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Assets {
|
2020-07-29 00:39:38 +03:00
|
|
|
pub fn new(font_dir: String) -> Assets {
|
2019-11-29 09:41:08 +03:00
|
|
|
let mut a = Assets {
|
2020-03-02 23:02:25 +03:00
|
|
|
default_line_height: RefCell::new(0.0),
|
2020-02-09 00:25:48 +03:00
|
|
|
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()),
|
2020-08-08 00:41:09 +03:00
|
|
|
#[cfg(not(feature = "wasm-backend"))]
|
2020-06-26 21:22:22 +03:00
|
|
|
font_to_id: HashMap::new(),
|
2020-02-07 22:51:41 +03:00
|
|
|
text_opts: Options::default(),
|
2019-11-29 09:41:08 +03:00
|
|
|
};
|
2020-08-08 00:41:09 +03:00
|
|
|
#[cfg(not(feature = "wasm-backend"))]
|
|
|
|
{
|
|
|
|
a.text_opts.fontdb = fontdb::Database::new();
|
|
|
|
a.text_opts.fontdb.load_fonts_dir(font_dir);
|
|
|
|
for font in vec![
|
|
|
|
Font::BungeeInlineRegular,
|
|
|
|
Font::BungeeRegular,
|
|
|
|
Font::OverpassBold,
|
|
|
|
Font::OverpassRegular,
|
|
|
|
Font::OverpassSemiBold,
|
|
|
|
Font::OverpassMonoBold,
|
2020-08-18 06:30:07 +03:00
|
|
|
Font::ZcoolXiaoWei,
|
2020-08-08 00:41:09 +03:00
|
|
|
] {
|
|
|
|
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);
|
2020-06-26 21:22:22 +03:00
|
|
|
}
|
2019-11-29 09:41:08 +03:00
|
|
|
a
|
|
|
|
}
|
|
|
|
|
2020-02-15 01:28:25 +03:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-09 00:08:31 +03:00
|
|
|
// This seems to be missing line_gap, and line_gap is 0, so manually adjust here.
|
2020-06-26 21:22:22 +03:00
|
|
|
let line_height = self
|
|
|
|
.text_opts
|
|
|
|
.fontdb
|
|
|
|
.with_face_data(self.font_to_id[&font], |data, face_index| {
|
|
|
|
let font = ttf_parser::Font::from_data(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
|
|
|
})
|
2020-06-26 21:22:22 +03:00
|
|
|
.unwrap();
|
2020-07-29 00:39:38 +03:00
|
|
|
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
|
|
|
}
|
2020-02-07 09:30:51 +03:00
|
|
|
|
2020-02-15 01:28:25 +03:00
|
|
|
// TODO No text in wasm yet
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
pub fn line_height(&self, font: Font, font_size: usize) -> f64 {
|
2020-02-15 03:15:44 +03:00
|
|
|
let key = (font, font_size);
|
|
|
|
if let Some(height) = self.line_height_cache.borrow().get(&key) {
|
|
|
|
return *height;
|
|
|
|
}
|
|
|
|
|
2020-02-15 01:28:25 +03:00
|
|
|
text::SCALE_LINE_HEIGHT * 30.0
|
|
|
|
}
|
|
|
|
|
2020-02-09 00:25:48 +03:00
|
|
|
pub fn get_cached_text(&self, key: &String) -> Option<GeomBatch> {
|
|
|
|
self.text_cache.borrow_mut().get(key).cloned()
|
2020-02-07 09:30:51 +03:00
|
|
|
}
|
|
|
|
pub fn cache_text(&self, key: String, geom: GeomBatch) {
|
2020-02-09 00:25:48 +03:00
|
|
|
self.text_cache.borrow_mut().put(key, geom);
|
2020-02-07 09:30:51 +03:00
|
|
|
}
|
2020-02-09 03:27:45 +03:00
|
|
|
|
2020-07-29 00:39:38 +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
|
|
|
}
|
2020-03-02 23:02:25 +03:00
|
|
|
|
2020-07-29 00:39:38 +03:00
|
|
|
pub fn cache_svg(&self, key: String, geom: GeomBatch, bounds: Bounds) {
|
|
|
|
self.svg_cache.borrow_mut().insert(key, (geom, bounds));
|
2020-03-02 23:02:25 +03:00
|
|
|
}
|
2019-11-29 09:41:08 +03:00
|
|
|
}
|