add incomplete shaping plumbing

This commit is contained in:
Wez Furlong committed 2019-02-16 17:12:54 -08:00
1 parent a4e1bd827e
commit 12e2dcd181
5 files changed
+95 -16

No files matched your search

+3 -3
View File
@@ -21,6 +21,8 @@ serde_derive = "~1.0"
toml = "~0.4"
unicode-width = "~0.1"
directories = "~1.0"
font-loader = "0.8"
rusttype = "0.7"
[dependencies.term]
path = "term"
@@ -28,9 +30,6 @@ path = "term"
[dependencies.termwiz]
path = "termwiz"
[target.'cfg(windows)'.dependencies]
font-loader = "0.8"
rusttype = "0.7"
[target.'cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))'.dependencies]
freetype = "~0.4"
@@ -60,5 +59,6 @@ core-text = "~9.2"
[features]
debug-escape-sequences = ["term/debug-escape-sequences"]
force-glutin = []
force-rusttype = []
[workspace]
+64
View File
@@ -53,6 +53,58 @@ impl Drop for Font {
}
}
struct Blob {
blob: *mut hb_blob_t,
}
impl Drop for Blob {
fn drop(&mut self) {
unsafe {
hb_blob_destroy(self.blob);
}
}
}
impl Blob {
fn from_slice(data: &[u8]) -> Result<Self, Error> {
let blob = unsafe {
hb_blob_create(
data.as_ptr() as *const i8,
data.len() as u32,
HB_MEMORY_MODE_READONLY,
ptr::null_mut(),
None,
)
};
ensure!(!blob.is_null(), "failed to create harfbuzz blob for slice");
Ok(Self { blob })
}
}
struct Face {
face: *mut hb_face_t,
}
impl Drop for Face {
fn drop(&mut self) {
unsafe {
hb_face_destroy(self.face);
}
}
}
impl Face {
fn from_blob(blob: &Blob, idx: u32) -> Result<Face, Error> {
let face = unsafe { hb_face_create(blob.blob, idx) };
ensure!(
!face.is_null(),
"failed to create face from blob data at idx {}",
idx
);
Ok(Self { face })
}
}
impl Font {
#[cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))]
/// Create a harfbuzz face from a freetype font
@@ -66,6 +118,18 @@ impl Font {
}
}
/// Create a font from raw data
/// Harfbuzz doesn't know how to interpret this without registering
/// some callbacks
/// FIXME: need to specialize this for rusttype
pub fn new_from_slice(data: &[u8], idx: u32) -> Result<Font, Error> {
let blob = Blob::from_slice(data)?;
let face = Face::from_blob(&blob, idx)?;
let font = unsafe { hb_font_create(face.face) };
ensure!(!font.is_null(), "failed to convert face to font");
Ok(Self { font })
}
#[cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))]
pub fn set_load_flags(&mut self, load_flags: freetype::freetype::FT_Int32) {
unsafe {
+17 -8
View File
@@ -9,24 +9,27 @@ use std::rc::Rc;
pub mod system;
pub use self::system::*;
#[cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))]
#[cfg(any(
target_os = "android",
all(unix, not(feature = "force-rusttype"), not(target_os = "macos"))
))]
pub mod ftwrap;
#[cfg(all(unix, not(target_os = "macos")))]
#[cfg(all(unix, not(target_os = "macos"), not(feature = "force-rusttype")))]
pub mod fcwrap;
#[cfg(all(unix, not(target_os = "macos")))]
#[cfg(all(unix, not(target_os = "macos"), not(feature = "force-rusttype")))]
pub mod fontconfigandfreetype;
#[cfg(all(unix, not(target_os = "macos")))]
#[cfg(all(unix, not(target_os = "macos"), not(feature = "force-rusttype")))]
use self::fontconfigandfreetype::FontSystemImpl;
#[cfg(target_os = "macos")]
#[cfg(all(target_os = "macos", not(feature = "force-rusttype")))]
pub mod coretext;
#[cfg(target_os = "macos")]
#[cfg(all(target_os = "macos", not(feature = "force-rusttype")))]
use self::coretext::FontSystemImpl;
#[cfg(windows)]
#[cfg(any(windows, feature = "force-rusttype"))]
pub mod rtype;
#[cfg(windows)]
#[cfg(any(windows, feature = "force-rusttype"))]
use self::rtype::FontSystemImpl;
use super::config::{Config, TextStyle};
@@ -195,6 +198,9 @@ pub fn shape_with_harfbuzz(
Ok(shape) => Ok(shape),
Err(e) => {
eprintln!("{:?} for {:?}", e, substr);
if font_idx == 0 && s == "?" {
bail!("unable to find any usable glyphs for `?` in font_idx 0");
}
shape_with_harfbuzz(font, 0, "?")
}
}?;
@@ -234,6 +240,9 @@ pub fn shape_with_harfbuzz(
Ok(shape) => Ok(shape),
Err(e) => {
eprintln!("{:?} for {:?}", e, substr);
if font_idx == 0 && s == "?" {
bail!("unable to find any usable glyphs for `?` in font_idx 0");
}
shape_with_harfbuzz(font, 0, "?")
}
}?;
+9 -3
View File
@@ -9,10 +9,12 @@ use font::{
};
use font_loader::system_fonts;
use rusttype::{Font as RTFont, FontCollection, Scale};
use std::cell::RefCell;
struct NamedFontImpl<'a> {
collection: FontCollection<'a>,
font: RTFont<'a>,
hbfont: RefCell<harfbuzz::Font>,
scale: Scale,
}
@@ -27,17 +29,21 @@ impl RustTypeFonts {
impl FontSystem for RustTypeFonts {
fn load_font(&self, config: &Config, style: &TextStyle) -> Result<Box<NamedFont>, Error> {
let font_props = system_fonts::FontPropertyBuilder::new()
.family(&style.fontconfig_pattern)
//.family(&style.fontconfig_pattern)
.family("monospace")
.monospace()
.build();
let (data, idx) = system_fonts::get(&font_props)
.ok_or_else(|| format_err!("no font matching {:?}", style))?;
eprintln!("want idx {} in bytes of len {}", idx, data.len());
let hbfont = RefCell::new(harfbuzz::Font::new_from_slice(&data, idx as u32)?);
let collection = FontCollection::from_bytes(data)?;
let font = collection.font_at(idx as usize)?;
eprintln!("made a font for {:?}", style);
let scale = Scale::uniform(config.font_size as f32 * 96.0 / 72.0);
Ok(Box::new(NamedFontImpl {
collection,
hbfont,
font,
scale,
}))
@@ -46,7 +52,7 @@ impl FontSystem for RustTypeFonts {
impl<'a> NamedFont for NamedFontImpl<'a> {
fn get_fallback(&mut self, idx: FallbackIdx) -> Result<&Font, Error> {
ensure!(idx == 0, "no fallback fonts available");
ensure!(idx == 0, "no fallback fonts available (idx={})", idx);
Ok(self)
}
fn shape(&mut self, s: &str) -> Result<Vec<GlyphInfo>, Error> {
@@ -60,7 +66,7 @@ impl<'a> Font for NamedFontImpl<'a> {
buf: &mut harfbuzz::Buffer,
features: Option<&[harfbuzz::hb_feature_t]>,
) {
unimplemented!();
self.hbfont.borrow_mut().shape(buf, features)
}
fn has_color(&self) -> bool {
+2 -2
View File
@@ -28,9 +28,9 @@ extern crate termwiz;
extern crate toml;
extern crate unicode_width;
#[cfg(windows)]
#[cfg(any(windows, feature = "force-rusttype"))]
extern crate font_loader;
#[cfg(windows)]
#[cfg(any(windows, feature = "force-rusttype"))]
extern crate rusttype;
#[macro_use]