Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ready for Review: Fix font-config crash #168

Merged
merged 22 commits into from
Dec 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/window/concept_frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::cell::RefCell;
use std::cmp::max;
use std::io::Read;
use std::rc::Rc;

use andrew::line;
Expand All @@ -14,6 +13,8 @@ use wayland_client::protocol::{
};
use wayland_client::{Attached, DispatchData};

use log::error;

use super::{
ARGBColor, ButtonColorSpec, ButtonState, ColorSpec, Frame, FrameRequest, State, WindowState,
};
Expand Down Expand Up @@ -334,7 +335,7 @@ pub struct ConceptFrame {
surface_version: u32,
config: ConceptConfig,
title: Option<String>,
font_data: Option<Vec<u8>>,
font_data: Option<Result<Vec<u8>, ()>>,
}

impl Frame for ConceptFrame {
Expand Down Expand Up @@ -639,27 +640,29 @@ impl Frame for ConceptFrame {
// If theres no stored font data, find the first ttf regular sans font and
// store it
if self.font_data.is_none() {
if let Some(font) = fontconfig::FontConfig::new()
.unwrap()
.get_regular_family_fonts(&font_face)
.unwrap()
.iter()
.find(|p| p.extension().map(|e| e == "ttf").unwrap_or(false))
{
let mut font_data = Vec::new();
if let Ok(mut file) = ::std::fs::File::open(font) {
match file.read_to_end(&mut font_data) {
Ok(_) => self.font_data = Some(font_data),
Err(err) => {
log::error!("Could not read font file: {}", err)
}
}
let font_bytes = fontconfig::FontConfig::new()
.ok()
.and_then(|font_config| {
font_config.get_regular_family_fonts(&font_face).ok()
})
.and_then(|regular_family_fonts| {
regular_family_fonts
.iter()
.cloned()
.find(|p| p.extension().map_or(false, |e| e == "ttf"))
})
.and_then(|font| std::fs::read(font).ok());
match font_bytes {
Some(bytes) => self.font_data = Some(Ok(bytes)),
None => {
error!("No font could be found");
self.font_data = Some(Err(()))
}
}
}

// Create text from stored title and font data
if let Some(ref font_data) = self.font_data {
if let Some(Ok(ref font_data)) = self.font_data {
let title_color = self.config.title_color.get_for(self.active);
let mut title_text = text::Text::new(
(
Expand Down Expand Up @@ -714,7 +717,7 @@ impl Frame for ConceptFrame {
}
}
if let Err(err) = mmap.flush() {
log::error!("Failed to flush frame memory map: {}", err);
error!("Failed to flush frame memory map: {}", err);
}
}

Expand Down Expand Up @@ -928,7 +931,7 @@ fn change_pointer(pointer: &ThemedPointer, inner: &Inner, location: Location, se
};

if pointer.set_cursor(name, serial).is_err() {
log::error!("Failed to set cursor");
error!("Failed to set cursor");
}
}

Expand Down