use std::collections::HashMap;
use std::sync::Arc;
use fontdue::Font;
use tiny_skia::{ Pixmap, PixmapPaint, Transform };
use crate::system_fonts::default_handle;
use crate::theme::{ FontRegistry, FontStyle };
use super::SoftwareCanvas;
impl SoftwareCanvas
{
pub fn new( width: u32, height: u32 ) -> Self
{
let handle = default_handle();
Self
{
pixmap: Pixmap::new( width, height ).expect( "pixmap" ),
font: handle.font.clone(),
font_bytes: handle.bytes.clone(),
font_face: handle.face,
font_registry: None,
dpi_scale: 1.0,
layout_viewport: None,
density: None,
global_alpha: 1.0,
glyph_cache: HashMap::new(),
clip_mask: None,
clip_bounds: Vec::new(),
}
}
pub fn sub_canvas( &self, width: u32, height: u32 ) -> SoftwareCanvas
{
SoftwareCanvas
{
pixmap: Pixmap::new( width, height ).expect( "pixmap" ),
font: Arc::clone( &self.font ),
font_bytes: Arc::clone( &self.font_bytes ),
font_face: self.font_face,
font_registry: self.font_registry.as_ref().map( Arc::clone ),
dpi_scale: self.dpi_scale,
layout_viewport: Some( self.layout_viewport.unwrap_or(
( self.pixmap.width() as f32, self.pixmap.height() as f32 ) ) ),
density: self.density,
global_alpha: self.global_alpha,
glyph_cache: HashMap::new(),
clip_mask: None,
clip_bounds: Vec::new(),
}
}
pub fn set_font_registry( &mut self, registry: Arc<FontRegistry> )
{
self.font_registry = Some( registry );
}
pub fn font_for( &self, family: &str, weight: u16, style: FontStyle ) -> Arc<Font>
{
self.font_registry
.as_ref()
.and_then( |r| r.resolve( family, weight, style ) )
.unwrap_or_else( || Arc::clone( &self.font ) )
}
pub fn font_for_char( &self, ch: char ) -> Arc<Font>
{
if self.font.lookup_glyph_index( ch ) != 0
{
return Arc::clone( &self.font );
}
crate::system_fonts::lookup( ch ).unwrap_or_else( || Arc::clone( &self.font ) )
}
pub fn font_handle_for_char( &self, ch: char ) -> crate::system_fonts::FontHandle
{
if self.font.lookup_glyph_index( ch ) != 0
{
return crate::system_fonts::FontHandle
{
font: Arc::clone( &self.font ),
bytes: Arc::clone( &self.font_bytes ),
face: self.font_face,
};
}
crate::system_fonts::lookup_handle( ch ).unwrap_or_else( ||
crate::system_fonts::FontHandle
{
font: Arc::clone( &self.font ),
bytes: Arc::clone( &self.font_bytes ),
face: self.font_face,
}
)
}
pub fn blit( &mut self, src: &SoftwareCanvas, dest_x: i32, dest_y: i32 )
{
let paint = PixmapPaint::default();
let t = Transform::from_translate( dest_x as f32, dest_y as f32 );
self.pixmap.draw_pixmap( 0, 0, src.pixmap.as_ref(), &paint, t, self.clip_mask.as_ref() );
}
pub fn resize( &mut self, width: u32, height: u32 )
{
self.pixmap = Pixmap::new( width, height ).expect( "pixmap" );
}
}