2019-07-19 08:32:41 +00:00
|
|
|
use crate::backend::render::{RenderBackend, ShapeHandle};
|
2019-05-04 18:45:11 +00:00
|
|
|
|
2019-08-15 20:48:51 +00:00
|
|
|
type Error = Box<dyn std::error::Error>;
|
2019-05-04 18:45:11 +00:00
|
|
|
|
|
|
|
pub struct Font {
|
|
|
|
glyphs: Vec<ShapeHandle>,
|
2019-10-07 18:39:44 +00:00
|
|
|
|
|
|
|
/// The scaling applied to the font height to render at the proper size.
|
|
|
|
/// This depends on the DefineFont tag version.
|
|
|
|
scale: f32,
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Font {
|
2019-08-15 20:48:51 +00:00
|
|
|
pub fn from_swf_tag(renderer: &mut dyn RenderBackend, tag: &swf::Font) -> Result<Font, Error> {
|
2019-05-04 18:45:11 +00:00
|
|
|
let mut glyphs = vec![];
|
|
|
|
for glyph in &tag.glyphs {
|
2019-07-19 08:32:41 +00:00
|
|
|
let shape_handle = renderer.register_glyph_shape(glyph);
|
2019-05-04 18:45:11 +00:00
|
|
|
glyphs.push(shape_handle);
|
|
|
|
}
|
2019-10-07 18:39:44 +00:00
|
|
|
Ok(Font {
|
|
|
|
glyphs,
|
|
|
|
|
|
|
|
/// DefineFont3 stores coordinates at 20x the scale of DefineFont1/2.
|
|
|
|
/// (SWF19 p.164)
|
|
|
|
scale: if tag.version >= 3 { 20480.0 } else { 1024.0 },
|
|
|
|
})
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_glyph(&self, i: usize) -> Option<ShapeHandle> {
|
2019-05-09 21:14:21 +00:00
|
|
|
self.glyphs.get(i).cloned()
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|
2019-10-07 18:39:44 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn scale(&self) -> f32 {
|
|
|
|
self.scale
|
|
|
|
}
|
2019-05-04 18:45:11 +00:00
|
|
|
}
|