From 144d8cc3eb23e1d0a3204629850fce35a7913065 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 18 Jan 2018 07:20:29 -0800 Subject: [PATCH] start pulling in cairo --- Cargo.toml | 5 +++++ src/font/cairo.rs | 16 +++++++++++++++ src/font/fcwrap.rs | 8 ++++++++ src/font/ftwrap.rs | 10 +++++++++ src/font/mod.rs | 4 ++++ src/main.rs | 51 ++++++++++++++++++++++++++++++++-------------- 6 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/font/cairo.rs diff --git a/Cargo.toml b/Cargo.toml index 726a02cc1..6d1df85f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ name = "wterm" version = "0.1.0" [dependencies] +cairo-sys-rs = "0.5.0" failure = "0.1.1" freetype = "0.3.0" harfbuzz-sys = "0.1.15" @@ -12,6 +13,10 @@ servo-fontconfig = "0.4.0" unicode-width = "0.1.4" vte = "0.3.2" +[dependencies.cairo-rs] +features = ["xcb"] +version = "0.3.0" + [dependencies.sdl2] features = [ "bundled", diff --git a/src/font/cairo.rs b/src/font/cairo.rs new file mode 100644 index 000000000..c32cb069e --- /dev/null +++ b/src/font/cairo.rs @@ -0,0 +1,16 @@ + +use cairo_sys; +use fontconfig::fontconfig::FcPattern; +use freetype::freetype::FT_Face; + +pub use cairo::*; + +extern "C" { + pub fn cairo_ft_font_face_create_for_ft_face( + face: FT_Face, + load_flags: i32, + ) -> *mut cairo_sys::cairo_font_face_t; + pub fn cairo_ft_font_face_create_for_pattern( + pattern: *mut FcPattern, + ) -> *mut cairo_sys::cairo_font_face_t; +} diff --git a/src/font/fcwrap.rs b/src/font/fcwrap.rs index e7a701048..fbbb0b7cd 100644 --- a/src/font/fcwrap.rs +++ b/src/font/fcwrap.rs @@ -1,5 +1,6 @@ //! Slightly higher level helper for fontconfig +use self::super::cairo; use failure::{self, Error}; pub use fontconfig::fontconfig::*; use std::ffi::{CStr, CString}; @@ -295,6 +296,13 @@ impl Pattern { } } } + + #[allow(dead_code)] + pub fn as_cairo(&self) -> cairo::FontFace { + unsafe { + cairo::FontFace::from_raw_full(cairo::cairo_ft_font_face_create_for_pattern(self.pat)) + } + } } impl Drop for Pattern { diff --git a/src/font/ftwrap.rs b/src/font/ftwrap.rs index 974aeec29..fad75bea1 100644 --- a/src/font/ftwrap.rs +++ b/src/font/ftwrap.rs @@ -1,5 +1,6 @@ //! Higher level freetype bindings +use self::super::cairo; use failure::Error; pub use freetype::freetype::*; use std::ffi::CString; @@ -89,6 +90,15 @@ impl Face { (width, height) } } + + #[allow(dead_code)] + pub fn as_cairo(&self) -> cairo::FontFace { + unsafe { + cairo::FontFace::from_raw_full( + cairo::cairo_ft_font_face_create_for_ft_face(self.face, 0), + ) + } + } } pub struct Library { diff --git a/src/font/mod.rs b/src/font/mod.rs index 4367b4690..bc6c28c33 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -5,6 +5,7 @@ use unicode_width::UnicodeWidthStr; pub mod ftwrap; pub mod hbwrap; pub mod fcwrap; +pub mod cairo; pub use self::fcwrap::Pattern as FontPattern; @@ -60,6 +61,7 @@ impl GlyphInfo { struct FontInfo { face: ftwrap::Face, font: hbwrap::Font, + cairo_face: cairo::FontFace, /// nominal monospace cell height cell_height: i64, /// nominal monospace cell width @@ -148,9 +150,11 @@ impl Font { let (cell_width, cell_height) = face.cell_metrics(); debug!("metrics: width={} height={}", cell_width, cell_height); + let cairo_face = face.as_cairo(); self.fonts.push(FontInfo { face, font, + cairo_face, cell_height, cell_width, }); diff --git a/src/main.rs b/src/main.rs index 2e7281964..47834a54b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate failure; extern crate hexdump; extern crate unicode_width; extern crate harfbuzz_sys; +extern crate cairo; +extern crate cairo_sys; #[cfg(not(target_os = "macos"))] extern crate fontconfig; // from servo-fontconfig #[cfg(not(target_os = "macos"))] @@ -34,6 +36,11 @@ struct Glyph<'a> { info: GlyphInfo, bearing_x: i32, bearing_y: i32, + cairo_surface: cairo::ImageSurface, +} + +fn cairo_err(status: cairo::Status) -> Error { + format_err!("cairo status: {:?}", status) } impl<'a> Glyph<'a> { @@ -51,6 +58,18 @@ impl<'a> Glyph<'a> { let mut height = glyph.bitmap.rows as u32; let mut has_color = false; + // Note: the pixelformat is BGRA and we really + // want to use BGRA32 as the PixelFormatEnum + // value (which is endian corrected) but that + // is missing. Instead, we have to go to the + // lower level pixel format value and handle + // the endianness for ourselves + let pixel_format = if cfg!(target_endian = "big") { + PixelFormatEnum::BGRA8888 + } else { + PixelFormatEnum::ARGB8888 + }; + if width == 0 || height == 0 { // Special case for zero sized bitmaps; we can't // build a texture with zero dimenions, so we return @@ -75,21 +94,7 @@ impl<'a> Glyph<'a> { ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_BGRA => { has_color = true; texture_creator - .create_texture_static( - // Note: the pixelformat is BGRA and we really - // want to use BGRA32 as the PixelFormatEnum - // value (which is endian corrected) but that - // is missing. Instead, we have to go to the - // lower level pixel format value and handle - // the endianness for ourselves - Some(if cfg!(target_endian = "big") { - PixelFormatEnum::BGRA8888 - } else { - PixelFormatEnum::ARGB8888 - }), - width, - height, - ) + .create_texture_static(Some(pixel_format), width, height) .map_err(failure::err_msg)? } mode @ _ => bail!("unhandled pixel mode: {:?}", mode), @@ -130,6 +135,21 @@ impl<'a> Glyph<'a> { info.y_offset = (info.y_offset as f64 * scale) as i32; } + let cairo_surface = cairo::ImageSurface::create( + cairo::Format::ARgb32, + target_cell_width as i32, + target_cell_height as i32, + ).map_err(cairo_err)?; + + let cairo_glyph = cairo::Glyph { + index: info.glyph_pos as u64, + x: (0 + info.x_offset + bearing_x) as f64, + y: (target_cell_height as i32 - info.y_offset - bearing_y) as f64, + }; + println!("cairo_glyph: {:?}", cairo_glyph); + let cairo_context = cairo::Context::new(&cairo_surface); + cairo_context.set_source_rgba(0.0, 0.0, 0.0, 0.0); + Ok(Glyph { has_color, tex, @@ -138,6 +158,7 @@ impl<'a> Glyph<'a> { info, bearing_x, bearing_y, + cairo_surface, }) } }