start pulling in cairo

This commit is contained in:
Wez Furlong committed 2018-01-18 07:20:29 -08:00
1 parent df64bce18a
commit 144d8cc3eb
6 files changed
+79 -15

No files matched your search

+5
View File
@@ -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",
+16
View File
@@ -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;
}
+8
View File
@@ -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 {
+10
View File
@@ -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 {
+4
View File
@@ -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,
});
+36 -15
View File
@@ -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,
})
}
}