Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
39 changes: 36 additions & 3 deletions piet-cairo/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
//! Text functionality for Piet cairo backend

use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt;
use std::ops::{Range, RangeBounds};
use std::rc::Rc;

use glib::translate::{from_glib_full, ToGlibPtr};

use pango::prelude::FontFaceExt;
use pango::prelude::FontFamilyExt;
use pango::prelude::FontMapExt;
use pango::AttrList;
use pango::FontDescription;
use pango::{AttrList, FontFace};
use pango_sys::pango_attr_insert_hyphens_new;
use pangocairo::FontMap;

Expand Down Expand Up @@ -166,8 +170,37 @@ impl Text for CairoText {
type TextLayoutBuilder = CairoTextLayoutBuilder;

fn font_family(&mut self, family_name: &str) -> Option<FontFamily> {
//TODO: Veryify that a family exists with the requested name
Some(FontFamily::new_unchecked(family_name))
// The pango documentation says this is always a string, and never null.

let font = FontDescription::from_string(family_name);

let best_match = self
.pango_context
.list_families()
.iter()
.filter(|family| {
family.name().unwrap().contains(family_name)
|| family_name.contains(family.name().unwrap().as_str())
})
.map(|family| {
family
.list_faces()
.iter()
.map(|fontface| (family.clone(), fontface.clone()))
.collect::<Vec<(pango::FontFamily, FontFace)>>()
})
.flatten()
.map(|(family, fontface)| fontface.describe().map(|fontdesc| (family, fontdesc)))
.flatten()
.max_by(|a, b| {
if font.better_match(Some(&a.1), &b.1) {
Ordering::Less
} else {
Ordering::Greater
}
});

best_match.map(|(family, _)| FontFamily::new_unchecked(family.name().unwrap().as_str()))
}

fn load_font(&mut self, _data: &[u8]) -> Result<FontFamily, Error> {
Expand Down
2 changes: 1 addition & 1 deletion piet/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl FontFamily {
FontFamilyInner::SansSerif => "sans-serif",
FontFamilyInner::SystemUi => "system-ui",
FontFamilyInner::Monospace => "monospace",
FontFamilyInner::Named(s) => &s,
FontFamilyInner::Named(s) => s,
}
}

Expand Down