use crate::error::SelectionError;
use crate::family_handle::FamilyHandle;
use crate::family_name::FamilyName;
use crate::handle::Handle;
use crate::properties::Properties;
use crate::source::Source;
use std::{
any::Any,
fmt,
ops::{Index, IndexMut},
slice,
};
#[allow(missing_debug_implementations)]
pub struct MultiSource {
subsources: Vec<Box<dyn Source>>,
}
impl MultiSource {
pub fn from_sources(subsources: Vec<Box<dyn Source>>) -> MultiSource {
MultiSource { subsources }
}
pub fn all_fonts(&self) -> Result<Vec<Handle>, SelectionError> {
let mut handles = vec![];
for subsource in &self.subsources {
handles.extend(subsource.all_fonts()?.into_iter())
}
Ok(handles)
}
pub fn all_families(&self) -> Result<Vec<String>, SelectionError> {
let mut families = vec![];
for subsource in &self.subsources {
families.extend(subsource.all_families()?.into_iter())
}
Ok(families)
}
pub fn select_family_by_name(&self, family_name: &str) -> Result<FamilyHandle, SelectionError> {
for subsource in &self.subsources {
match subsource.select_family_by_name(family_name) {
Ok(family) => return Ok(family),
Err(SelectionError::NotFound) => {}
Err(err) => return Err(err),
}
}
Err(SelectionError::NotFound)
}
pub fn select_by_postscript_name(
&self,
postscript_name: &str,
) -> Result<Handle, SelectionError> {
for subsource in &self.subsources {
match subsource.select_by_postscript_name(postscript_name) {
Ok(font) => return Ok(font),
Err(SelectionError::NotFound) => {}
Err(err) => return Err(err),
}
}
Err(SelectionError::NotFound)
}
#[inline]
pub fn select_best_match(
&self,
family_names: &[FamilyName],
properties: &Properties,
) -> Result<Handle, SelectionError> {
<Self as Source>::select_best_match(self, family_names, properties)
}
#[inline]
pub fn iter(&self) -> MultiIter<'_> {
MultiIter(self.subsources.iter())
}
#[inline]
pub fn iter_mut(&mut self) -> MultiIterMut<'_> {
MultiIterMut(self.subsources.iter_mut())
}
pub fn find_source<T: Source + 'static>(&self) -> Option<&T> {
self.iter().find_map(|v| v.as_any().downcast_ref())
}
pub fn find_source_mut<T: Source + 'static>(&mut self) -> Option<&mut T> {
self.iter_mut().find_map(|v| v.as_mut_any().downcast_mut())
}
}
impl Source for MultiSource {
#[inline]
fn all_fonts(&self) -> Result<Vec<Handle>, SelectionError> {
self.all_fonts()
}
#[inline]
fn all_families(&self) -> Result<Vec<String>, SelectionError> {
self.all_families()
}
#[inline]
fn select_family_by_name(&self, family_name: &str) -> Result<FamilyHandle, SelectionError> {
self.select_family_by_name(family_name)
}
#[inline]
fn select_by_postscript_name(&self, postscript_name: &str) -> Result<Handle, SelectionError> {
self.select_by_postscript_name(postscript_name)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn Any {
self
}
}
impl Index<usize> for MultiSource {
type Output = dyn Source;
fn index(&self, idx: usize) -> &Self::Output {
&*self.subsources[idx]
}
}
impl IndexMut<usize> for MultiSource {
fn index_mut(&mut self, idx: usize) -> &mut Self::Output {
&mut *self.subsources[idx]
}
}
pub struct MultiIter<'a>(slice::Iter<'a, Box<dyn Source>>);
impl<'a> Iterator for MultiIter<'a> {
type Item = &'a dyn Source;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|v| &**v)
}
}
impl fmt::Debug for MultiIter<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MultiIter").finish()
}
}
pub struct MultiIterMut<'a>(slice::IterMut<'a, Box<dyn Source>>);
impl<'a> Iterator for MultiIterMut<'a> {
type Item = &'a mut dyn Source;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|v| &mut **v)
}
}
impl fmt::Debug for MultiIterMut<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MultiIterMut").finish()
}
}