no_std support
This commit is contained in:
parent
e95671f68f
commit
268805ba0c
18 changed files with 190 additions and 38 deletions
|
|
@ -1,6 +1,8 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use std::sync::Arc;
|
||||
use alloc::sync::Arc;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::vec::Vec;
|
||||
use unicode_script::Script;
|
||||
|
||||
use crate::Font;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use std::ops::Deref;
|
||||
use core::ops::Deref;
|
||||
|
||||
pub struct Font<'a> {
|
||||
pub info: &'a fontdb::FaceInfo,
|
||||
|
|
@ -14,10 +14,12 @@ impl<'a> Font<'a> {
|
|||
pub fn new(info: &'a fontdb::FaceInfo) -> Option<Self> {
|
||||
let data = match &info.source {
|
||||
fontdb::Source::Binary(data) => data.deref().as_ref(),
|
||||
#[cfg(feature = "std")]
|
||||
fontdb::Source::File(path) => {
|
||||
log::warn!("Unsupported fontdb Source::File('{}')", path.display());
|
||||
return None;
|
||||
}
|
||||
#[cfg(feature = "std")]
|
||||
fontdb::Source::SharedFile(_path, data) => data.deref().as_ref(),
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use std::sync::Arc;
|
||||
use alloc::sync::Arc;
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::{
|
||||
string::String,
|
||||
vec::Vec,
|
||||
};
|
||||
|
||||
use crate::Font;
|
||||
|
||||
|
|
|
|||
9
src/font/system/mod.rs
Normal file
9
src/font/system/mod.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#[cfg(not(feature = "std"))]
|
||||
pub use self::no_std::*;
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod no_std;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub use self::std::*;
|
||||
#[cfg(feature = "std")]
|
||||
mod std;
|
||||
75
src/font/system/no_std.rs
Normal file
75
src/font/system/no_std.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||
|
||||
use alloc::{
|
||||
string::{String, ToString},
|
||||
sync::Arc,
|
||||
vec::Vec,
|
||||
};
|
||||
|
||||
use crate::{Attrs, Font, FontMatches};
|
||||
|
||||
/// Access system fonts
|
||||
pub struct FontSystem{
|
||||
locale: String,
|
||||
db: fontdb::Database,
|
||||
}
|
||||
|
||||
impl FontSystem {
|
||||
pub fn new() -> Self {
|
||||
//TODO: get locale from argument?
|
||||
let locale = "en-US".to_string();
|
||||
|
||||
//TODO: allow loading fonts from memory
|
||||
let mut db = fontdb::Database::new();
|
||||
{
|
||||
//TODO: configurable default fonts
|
||||
db.set_monospace_family("Fira Mono");
|
||||
db.set_sans_serif_family("Fira Sans");
|
||||
db.set_serif_family("DejaVu Serif");
|
||||
}
|
||||
|
||||
Self {
|
||||
locale,
|
||||
db,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn locale(&self) -> &str {
|
||||
&self.locale
|
||||
}
|
||||
|
||||
pub fn db(&self) -> &fontdb::Database {
|
||||
&self.db
|
||||
}
|
||||
|
||||
pub fn get_font<'a>(&'a self, id: fontdb::ID) -> Option<Arc<Font<'a>>> {
|
||||
let face = self.db.face(id)?;
|
||||
match Font::new(face) {
|
||||
Some(font) => Some(Arc::new(font)),
|
||||
None => {
|
||||
log::warn!("failed to load font '{}'", face.post_script_name);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_font_matches<'a>(&'a self, attrs: Attrs) -> Arc<FontMatches<'a>> {
|
||||
let mut fonts = Vec::new();
|
||||
for face in self.db.faces() {
|
||||
if !attrs.matches(face) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match self.get_font(face.id) {
|
||||
Some(font) => fonts.push(font),
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
Arc::new(FontMatches {
|
||||
locale: &self.locale,
|
||||
default_family: self.db.family_name(&attrs.family).to_string(),
|
||||
fonts
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue