Have TextBuffer own FontMatches
This commit is contained in:
parent
1c341f3126
commit
0f446368ca
5 changed files with 42 additions and 49 deletions
|
|
@ -42,9 +42,6 @@ lazy_static::lazy_static! {
|
|||
static ref FONT_SYSTEM: FontSystem = FontSystem::new();
|
||||
}
|
||||
|
||||
//TODO: find out how to do this!
|
||||
static mut FONT_MATCHES: Option<FontMatches<'static>> = None;
|
||||
|
||||
static FONT_SIZES: &'static [TextMetrics] = &[
|
||||
TextMetrics::new(10, 14), // Caption
|
||||
TextMetrics::new(14, 20), // Body
|
||||
|
|
@ -57,38 +54,6 @@ static FONT_SIZES: &'static [TextMetrics] = &[
|
|||
fn main() -> cosmic::iced::Result {
|
||||
env_logger::init();
|
||||
|
||||
let font_matches: FontMatches<'static> = FONT_SYSTEM.matches(|info| -> bool {
|
||||
#[cfg(feature = "mono")]
|
||||
let monospaced = true;
|
||||
|
||||
#[cfg(not(feature = "mono"))]
|
||||
let monospaced = false;
|
||||
|
||||
let matched = {
|
||||
info.style == fontdb::Style::Normal &&
|
||||
info.weight == fontdb::Weight::NORMAL &&
|
||||
info.stretch == fontdb::Stretch::Normal &&
|
||||
(info.monospaced == monospaced || info.post_script_name.contains("Emoji"))
|
||||
};
|
||||
|
||||
if matched {
|
||||
log::debug!(
|
||||
"{:?}: family '{}' postscript name '{}' style {:?} weight {:?} stretch {:?} monospaced {:?}",
|
||||
info.id,
|
||||
info.family,
|
||||
info.post_script_name,
|
||||
info.style,
|
||||
info.weight,
|
||||
info.stretch,
|
||||
info.monospaced
|
||||
);
|
||||
}
|
||||
|
||||
matched
|
||||
}).unwrap();
|
||||
|
||||
unsafe { FONT_MATCHES = Some(font_matches); }
|
||||
|
||||
let mut settings = settings();
|
||||
settings.window.min_size = Some((400, 100));
|
||||
Window::run(settings)
|
||||
|
|
@ -134,9 +99,39 @@ impl Application for Window {
|
|||
type Theme = Theme;
|
||||
|
||||
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
|
||||
let font_matches: FontMatches<'static> = FONT_SYSTEM.matches(|info| -> bool {
|
||||
#[cfg(feature = "mono")]
|
||||
let monospaced = true;
|
||||
|
||||
#[cfg(not(feature = "mono"))]
|
||||
let monospaced = false;
|
||||
|
||||
let matched = {
|
||||
info.style == fontdb::Style::Normal &&
|
||||
info.weight == fontdb::Weight::NORMAL &&
|
||||
info.stretch == fontdb::Stretch::Normal &&
|
||||
(info.monospaced == monospaced || info.post_script_name.contains("Emoji"))
|
||||
};
|
||||
|
||||
if matched {
|
||||
log::debug!(
|
||||
"{:?}: family '{}' postscript name '{}' style {:?} weight {:?} stretch {:?} monospaced {:?}",
|
||||
info.id,
|
||||
info.family,
|
||||
info.post_script_name,
|
||||
info.style,
|
||||
info.weight,
|
||||
info.stretch,
|
||||
info.monospaced
|
||||
);
|
||||
}
|
||||
|
||||
matched
|
||||
}).unwrap();
|
||||
|
||||
let font_size_i = 1; // Body
|
||||
let buffer = TextBuffer::new(
|
||||
unsafe { FONT_MATCHES.as_ref().unwrap() },
|
||||
font_matches,
|
||||
FONT_SIZES[font_size_i],
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ fn main() {
|
|||
|
||||
let line_x = 8 * display_scale;
|
||||
let mut buffer = TextBuffer::new(
|
||||
&font_matches,
|
||||
font_matches,
|
||||
font_sizes[font_size_i]
|
||||
);
|
||||
buffer.set_size(
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ fn main() {
|
|||
let font_size_default = 1; // Body
|
||||
|
||||
let mut buffer = TextBuffer::new(
|
||||
&font_matches,
|
||||
font_matches,
|
||||
font_sizes[font_size_default]
|
||||
);
|
||||
buffer.set_size(
|
||||
|
|
|
|||
|
|
@ -167,7 +167,7 @@ impl TextBufferLine {
|
|||
|
||||
/// A buffer of text that is shaped and laid out
|
||||
pub struct TextBuffer<'a> {
|
||||
font_matches: &'a FontMatches<'a>,
|
||||
font_matches: FontMatches<'a>,
|
||||
lines: Vec<TextBufferLine>,
|
||||
metrics: TextMetrics,
|
||||
width: i32,
|
||||
|
|
@ -182,7 +182,7 @@ pub struct TextBuffer<'a> {
|
|||
|
||||
impl<'a> TextBuffer<'a> {
|
||||
pub fn new(
|
||||
font_matches: &'a FontMatches<'a>,
|
||||
font_matches: FontMatches<'a>,
|
||||
metrics: TextMetrics,
|
||||
) -> Self {
|
||||
let mut buffer = Self {
|
||||
|
|
@ -217,7 +217,7 @@ impl<'a> TextBuffer<'a> {
|
|||
reshaped += 1;
|
||||
}
|
||||
let layout = line.layout(
|
||||
self.font_matches,
|
||||
&self.font_matches,
|
||||
self.metrics.font_size,
|
||||
self.width
|
||||
);
|
||||
|
|
@ -247,7 +247,7 @@ impl<'a> TextBuffer<'a> {
|
|||
reshaped += 1;
|
||||
}
|
||||
let layout = line.layout(
|
||||
self.font_matches,
|
||||
&self.font_matches,
|
||||
self.metrics.font_size,
|
||||
self.width
|
||||
);
|
||||
|
|
@ -309,7 +309,7 @@ impl<'a> TextBuffer<'a> {
|
|||
if line.shape_opt.is_some() {
|
||||
line.layout_opt = None;
|
||||
line.layout(
|
||||
self.font_matches,
|
||||
&self.font_matches,
|
||||
self.metrics.font_size,
|
||||
self.width
|
||||
);
|
||||
|
|
@ -368,7 +368,7 @@ impl<'a> TextBuffer<'a> {
|
|||
fn set_layout_cursor(&mut self, cursor: LayoutCursor) {
|
||||
let line = &mut self.lines[cursor.line.get()];
|
||||
let layout = line.layout(
|
||||
self.font_matches,
|
||||
&self.font_matches,
|
||||
self.metrics.font_size,
|
||||
self.width
|
||||
);
|
||||
|
|
@ -547,7 +547,7 @@ impl<'a> TextBuffer<'a> {
|
|||
let layout_len = {
|
||||
let line = &mut self.lines[cursor.line.get()];
|
||||
let layout = line.layout(
|
||||
self.font_matches,
|
||||
&self.font_matches,
|
||||
self.metrics.font_size,
|
||||
self.width
|
||||
);
|
||||
|
|
@ -984,7 +984,7 @@ impl<'a> TextBuffer<'a> {
|
|||
|
||||
for glyph in layout_line.glyphs.iter() {
|
||||
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
|
||||
self.cache.with_pixels(self.font_matches, cache_key, color, |x, y, color| {
|
||||
self.cache.with_pixels(&self.font_matches, cache_key, color, |x, y, color| {
|
||||
f(x_int + x, line_y + y_int + y, 1, 1, color)
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,9 +35,7 @@ impl FontSystem {
|
|||
assert_eq!(db.len(), db.faces().len());
|
||||
for i in 0..db.len() {
|
||||
let id = db.faces()[i].id;
|
||||
unsafe {
|
||||
db.make_shared_face_data(id);
|
||||
}
|
||||
unsafe { db.make_shared_face_data(id); }
|
||||
}
|
||||
|
||||
Self { locale, db }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue