diff --git a/Cargo.toml b/Cargo.toml index d8ff6014f..2682af212 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ cairo-sys-rs = "0.5.0" failure = "0.1.1" freetype = "0.3.0" harfbuzz-sys = "0.1.15" -hexdump = "0.1.0" servo-fontconfig = "0.4.0" unicode-width = "0.1.4" vte = "0.3.2" @@ -18,9 +17,3 @@ xcb = "0.8.1" features = ["xcb"] version = "0.3.0" -[dependencies.sdl2] -features = [ - "bundled", - "static-link", -] -version = "0.31.0" diff --git a/src/font/fcwrap.rs b/src/font/fcwrap.rs index fbbb0b7cd..c8cc8b864 100644 --- a/src/font/fcwrap.rs +++ b/src/font/fcwrap.rs @@ -130,6 +130,20 @@ impl Pattern { } } + #[allow(dead_code)] + pub fn add_double(&mut self, key: &str, value: f64) -> Result<(), Error> { + let key = CString::new(key)?; + unsafe { + ensure!( + FcPatternAddDouble(self.pat, key.as_ptr(), value) != 0, + "failed to set double property {:?} -> {}", + key, + value + ); + Ok(()) + } + } + #[allow(dead_code)] pub fn add_integer(&mut self, key: &str, value: i32) -> Result<(), Error> { let key = CString::new(key)?; diff --git a/src/font/ftwrap.rs b/src/font/ftwrap.rs index fad75bea1..abab4b002 100644 --- a/src/font/ftwrap.rs +++ b/src/font/ftwrap.rs @@ -73,21 +73,24 @@ impl Face { } } - pub fn cell_metrics(&mut self) -> (i64, i64) { + pub fn cell_metrics(&mut self) -> (f64, f64) { unsafe { let metrics = &(*(*self.face).size).metrics; - let height = FT_MulFix(metrics.y_scale, (*self.face).height as i64) / 64; + let height = (metrics.y_scale as f64 * (*self.face).height as f64) / + (0x10000 as f64 * 64.0); - let mut width = 0; + let mut width = 0.0; for i in 32..128 { let glyph_pos = FT_Get_Char_Index(self.face, i); let res = FT_Load_Glyph(self.face, glyph_pos, FT_LOAD_COLOR as i32); if res.succeeded() { let glyph = &(*(*self.face).glyph); - width = width.max(((glyph.metrics.horiAdvance as f64) / 64f64).ceil() as i64); + if glyph.metrics.horiAdvance as f64 > width { + width = glyph.metrics.horiAdvance as f64; + } } } - (width, height) + (width / 64.0, height) } } diff --git a/src/font/hbwrap.rs b/src/font/hbwrap.rs index 065227ac7..f141d7797 100644 --- a/src/font/hbwrap.rs +++ b/src/font/hbwrap.rs @@ -1,11 +1,16 @@ //! Higher level harfbuzz bindings use failure::Error; +use freetype::freetype; pub use harfbuzz_sys::*; use std::mem; use std::ptr; use std::slice; +extern "C" { + fn hb_ft_font_set_load_flags(font: *mut hb_font_t, load_flags: i32); +} + pub fn language_from_string(s: &str) -> Result { unsafe { let lang = hb_language_from_string(s.as_ptr() as *const i8, s.len() as i32); @@ -52,6 +57,12 @@ impl Font { Font { font: unsafe { hb_ft_font_create_referenced(face.face) } } } + pub fn set_load_flags(&mut self, load_flags: freetype::FT_Int32) { + unsafe { + hb_ft_font_set_load_flags(self.font, load_flags); + } + } + /// Perform shaping. On entry, Buffer holds the text to shape. /// Once done, Buffer holds the output glyph and position info pub fn shape(&mut self, buf: &mut Buffer, features: Option<&[hb_feature_t]>) { diff --git a/src/font/mod.rs b/src/font/mod.rs index bc6c28c33..bfdb060bc 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -25,13 +25,13 @@ pub struct GlyphInfo { /// Which freetype glyph to load pub glyph_pos: u32, /// How far to advance the render cursor after drawing this glyph - pub x_advance: i32, + pub x_advance: f64, /// How far to advance the render cursor after drawing this glyph - pub y_advance: i32, + pub y_advance: f64, /// Destination render offset - pub x_offset: i32, + pub x_offset: f64, /// Destination render offset - pub y_offset: i32, + pub y_offset: f64, } impl GlyphInfo { @@ -49,10 +49,10 @@ impl GlyphInfo { font_idx, glyph_pos: info.codepoint, cluster: info.cluster, - x_advance: pos.x_advance / 64, - y_advance: pos.y_advance / 64, - x_offset: pos.x_offset / 64, - y_offset: pos.y_offset / 64, + x_advance: pos.x_advance as f64 / 64.0, + y_advance: pos.y_advance as f64 / 64.0, + x_offset: pos.x_offset as f64 / 64.0, + y_offset: pos.y_offset as f64 / 64.0, } } } @@ -63,9 +63,9 @@ struct FontInfo { font: hbwrap::Font, cairo_face: cairo::FontFace, /// nominal monospace cell height - cell_height: i64, + cell_height: f64, /// nominal monospace cell width - cell_width: i64, + cell_width: f64, } /// Holds "the" font selected by the user. In actuality, it @@ -95,7 +95,7 @@ impl Font { // Enable some filtering options and pull in the standard // fallback font selection from the user configuration - pattern.monospace()?; + //pattern.monospace()?; pattern.config_substitute(fcwrap::MatchKind::Pattern)?; pattern.default_substitute(); @@ -120,11 +120,18 @@ impl Font { let file = pat.get_file()?; debug!("load_next_fallback: file={}", file); + debug!("{}", pat.format("%{=unparse}")?); - let size = pat.get_double("size")?.ceil() as i64; + let size = pat.get_double("size")?; + let dpi = pat.get_double("dpi")? as u32; + debug!("set_char_size {} dpi={}", size, dpi); + // Scaling before truncating to integer minimizes the chances of hitting + // the fallback code for set_pixel_sizes below. + let size = (size * 64.0) as i64; let mut face = self.lib.new_face(file, 0)?; - match face.set_char_size(0, size * 64, 96, 96) { + + match face.set_char_size(size, size, dpi, dpi) { Err(err) => { let sizes = unsafe { let rec = &(*face.face); @@ -140,6 +147,7 @@ impl Font { size = size.max(info.height); } face.set_pixel_sizes(size as u32, size as u32)?; + debug!("fall back to set_pixel_sizes {}", size); } } Ok(_) => {} @@ -161,6 +169,11 @@ impl Font { Ok(()) } + pub fn get_cairo_font(&mut self, idx: usize) -> Result { + let font = self.get_font(idx)?; + Ok(font.cairo_face.clone()) + } + fn get_font(&mut self, idx: usize) -> Result<&mut FontInfo, Error> { if idx >= self.fonts.len() { self.load_next_fallback()?; @@ -173,11 +186,15 @@ impl Font { Ok(&mut self.fonts[idx]) } - pub fn get_metrics(&mut self) -> Result<(i64, i64), Error> { + pub fn get_metrics(&mut self) -> Result<(f64, f64), Error> { let font = self.get_font(0)?; Ok((font.cell_height, font.cell_width)) } + pub fn get_font_point_size(&self) -> Result { + self.pattern.get_double("size") + } + pub fn shape(&mut self, font_idx: usize, s: &str) -> Result, Error> { debug!( "shape text for font_idx {} with len {} {}", @@ -267,7 +284,7 @@ impl Font { } if info.codepoint != 0 { let text = &s[pos..pos + sizes[i]]; - debug!("glyph from `{}`", text); + //debug!("glyph from `{}`", text); cluster.push(GlyphInfo::new(text, font_idx, info, &positions[i])); } } @@ -286,7 +303,7 @@ impl Font { cluster.append(&mut shape); } - debug!("shaped: {:#?}", cluster); + //debug!("shaped: {:#?}", cluster); Ok(cluster) } @@ -308,10 +325,24 @@ impl Font { glyph_pos: u32, ) -> Result<&ftwrap::FT_GlyphSlotRec_, Error> { let info = &mut self.fonts[font_idx]; + + let render_mode = ftwrap::FT_Render_Mode::FT_RENDER_MODE_LCD; + + // when changing the load flags, we also need + // to change them for harfbuzz otherwise it won't + // hint correctly + let load_flags = (ftwrap::FT_LOAD_COLOR) as i32 | + // enable FT_LOAD_TARGET bits. There are no flags defined + // for these in the bindings so we do some bit magic for + // ourselves. This is how the FT_LOAD_TARGET_() macro + // assembles these bits. + (render_mode as i32) << 16; + + info.font.set_load_flags(load_flags); info.face.load_and_render_glyph( glyph_pos, - (ftwrap::FT_LOAD_COLOR) as i32, - ftwrap::FT_Render_Mode::FT_RENDER_MODE_LCD, + load_flags, + render_mode, ) } } diff --git a/src/main.rs b/src/main.rs index 2a46ff6a3..3ea021787 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ #[macro_use] extern crate failure; -extern crate hexdump; extern crate unicode_width; extern crate harfbuzz_sys; extern crate cairo; @@ -16,297 +15,27 @@ use failure::Error; extern crate xcb; use std::mem; - -/* - -extern crate sdl2; -use sdl2::event::{Event, WindowEvent}; -use sdl2::keyboard::Keycode; -use sdl2::pixels::{Color, PixelFormatEnum}; -use sdl2::rect::Rect; -use sdl2::render::{BlendMode, RenderTarget, Texture, TextureCreator}; - use std::slice; -*/ +use cairo::XCBSurface; +use cairo::prelude::*; -/* mod font; -use font::{Font, FontPattern, GlyphInfo, ftwrap}; -*/ +use font::{Font, FontPattern, ftwrap}; -/* fn cairo_err(status: cairo::Status) -> Error { format_err!("cairo status: {:?}", status) } -*/ - - -/* -struct Glyph<'a> { - has_color: bool, - tex: Option>, - width: u32, - height: u32, - info: GlyphInfo, - bearing_x: i32, - bearing_y: i32, - cairo_surface: cairo::ImageSurface, -} - -// 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 -#[cfg(target_endian = "big")] -const SDL_BGRA32: PixelFormatEnum = PixelFormatEnum::BGRA8888; -#[cfg(target_endian = "big")] -const SDL_ARGB32: PixelFormatEnum = PixelFormatEnum::ARGB8888; -#[cfg(target_endian = "little")] -const SDL_BGRA32: PixelFormatEnum = PixelFormatEnum::ARGB8888; -#[cfg(target_endian = "little")] -const SDL_ARGB32: PixelFormatEnum = PixelFormatEnum::BGRA8888; - -impl<'a> Glyph<'a> { - fn new( - texture_creator: &'a TextureCreator, - glyph: &ftwrap::FT_GlyphSlotRec_, - info: &GlyphInfo, - target_cell_height: i64, - target_cell_width: i64, - ) -> Result, Error> { - - let mut info = info.clone(); - let mut tex = None; - let mut width = glyph.bitmap.width as u32; - let mut height = glyph.bitmap.rows as u32; - let mut has_color = false; - - if width == 0 || height == 0 { - // Special case for zero sized bitmaps; we can't - // build a texture with zero dimenions, so we return - // a Glyph with tex=None. - } else { - - let mode: ftwrap::FT_Pixel_Mode = - unsafe { mem::transmute(glyph.bitmap.pixel_mode as u32) }; - - // pitch is the number of bytes per source row - let pitch = glyph.bitmap.pitch.abs() as usize; - let data = - unsafe { slice::from_raw_parts(glyph.bitmap.buffer, height as usize * pitch) }; - - let mut t = match mode { - ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_LCD => { - width = width / 3; - texture_creator - .create_texture_static(Some(PixelFormatEnum::BGR24), width, height) - .map_err(failure::err_msg)? - } - ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_BGRA => { - has_color = true; - texture_creator - .create_texture_static(Some(SDL_BGRA32), width, height) - .map_err(failure::err_msg)? - } - mode @ _ => bail!("unhandled pixel mode: {:?}", mode), - }; - - t.update(None, data, pitch)?; - tex = Some(t); - } - - let mut bearing_x = glyph.bitmap_left; - let mut bearing_y = glyph.bitmap_top; - - let scale = if (info.x_advance / info.num_cells as i32) as i64 > target_cell_width { - info.num_cells as f64 * (target_cell_width as f64 / info.x_advance as f64) - } else if height as i64 > target_cell_height { - target_cell_height as f64 / height as f64 - } else { - 1.0f64 - }; - - if scale != 1.0f64 { - debug!( - "scaling {:?} w={} {}, h={} {} by {}", - info, - width, - target_cell_width, - height, - target_cell_height, - scale - ); - width = (width as f64 * scale) as u32; - height = (height as f64 * scale) as u32; - bearing_x = (bearing_x as f64 * scale) as i32; - bearing_y = (bearing_y as f64 * scale) as i32; - info.x_advance = (info.x_advance as f64 * scale) as i32; - info.y_advance = (info.y_advance as f64 * scale) as i32; - info.x_offset = (info.x_offset as f64 * scale) as i32; - 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, - width, - height, - info, - bearing_x, - bearing_y, - cairo_surface, - }) - } -} - -fn glyphs_for_text<'a, T>( - texture_creator: &'a TextureCreator, - s: &str, -) -> Result>, Error> { - - let pattern = FontPattern::parse("Operator Mono SSm Lig:size=12:weight=SemiLight")?; - let mut font = Font::new(pattern)?; - - // We always load the cell_height for font 0, - // regardless of which font we are shaping here, - // so that we can scale glyphs appropriately - let (cell_height, cell_width) = font.get_metrics()?; - - let mut result = Vec::new(); - for info in font.shape(0, s)? { - if info.glyph_pos == 0 { - debug!("skip: no codepoint for this one {:?}", info); - continue; - } - - let glyph = font.load_glyph(info.font_idx, info.glyph_pos)?; - - let g = Glyph::new(texture_creator, glyph, &info, cell_height, cell_width)?; - result.push(g); - } - Ok(result) -} - -fn with_cairo_surface( - canvas: &mut sdl2::render::Canvas, - texture_creator: &TextureCreator, - mut f: F, -) -> Result<(), Error> { - let (width, height) = canvas.output_size().map_err(failure::err_msg)?; - println!("canvas size {} {}", width, height); - let mut tex = texture_creator.create_texture_streaming( - Some(PixelFormatEnum::ARGB8888), - width, - height, - )?; - tex.with_lock(None, |data, pitch| { - let surface = unsafe { - assert!(data.len() == pitch * height as usize); - cairo::ImageSurface::from_raw_full(cairo_sys::cairo_image_surface_create_for_data( - data.as_mut_ptr(), - cairo::Format::ARgb32, - width as i32, - height as i32, - pitch as i32, - )) - }.expect("failed to create cairo surface"); - - let cairo = cairo::Context::new(&surface); - f(cairo); - }).map_err(failure::err_msg)?; - canvas.copy(&tex, None, None).map_err(failure::err_msg) -} - -fn run() -> Result<(), Error> { - let sdl_context = sdl2::init().map_err(failure::err_msg)?; - let video_subsys = sdl_context.video().map_err(failure::err_msg)?; - let window = video_subsys - .window("wterm", 1024, 768) - .resizable() - .opengl() - .build()?; - let mut canvas = window.into_canvas().build()?; - let texture_creator = canvas.texture_creator(); - let mut glyphs = glyphs_for_text(&texture_creator, "x_advance != foo->bar(); ❤ 😍🤢")?; - - for event in sdl_context - .event_pump() - .map_err(failure::err_msg)? - .wait_iter() - { - match event { - Event::KeyDown { keycode: Some(Keycode::Escape), .. } | - Event::Quit { .. } => break, - Event::Window { win_event: WindowEvent::Resized(..), .. } => { - } - Event::Window { win_event: WindowEvent::Exposed, .. } => { - canvas.set_draw_color(Color::RGBA(0, 0, 0, 255)); - canvas.clear(); - canvas.set_blend_mode(BlendMode::Blend); - - let mut x = 10i32; - let mut y = 100i32; - for g in glyphs.iter_mut() { - if let &mut Some(ref mut tex) = &mut g.tex { - if !g.has_color { - tex.set_color_mod(0xb3, 0xb3, 0xb3); - } - canvas - .copy( - &tex, - None, - Some(Rect::new( - x + g.info.x_offset + g.bearing_x, - y - g.info.y_offset - g.bearing_y, - g.width, - g.height, - )), - ) - .map_err(failure::err_msg)?; - } - x += g.info.x_advance; - y += g.info.y_advance; - } - - with_cairo_surface(&mut canvas, &texture_creator, |cairo| { - cairo.set_source_rgb(4.0, 0.2, 0.2); - cairo.set_line_width(2.0); - cairo.rectangle(200.0, 200.0, 300.0, 300.0); - cairo.stroke(); - })?; - - canvas.present(); - } - _ => {} - } - } - Ok(()) -} -*/ struct TerminalWindow<'a> { window_id: u32, + screen_num: i32, conn: &'a xcb::Connection, width: u16, height: u16, + font: Font, + cell_height: f64, + cell_width: f64, } impl<'a> TerminalWindow<'a> { @@ -316,6 +45,15 @@ impl<'a> TerminalWindow<'a> { width: u16, height: u16, ) -> Result { + + let mut pattern = FontPattern::parse("Operator Mono SSm Lig:size=10:slant=roman")?; + pattern.add_double("dpi", 96.0)?; + let mut font = Font::new(pattern)?; + // we always load the cell_height for font 0, + // regardless of which font we are shaping here, + // so that we can scale glyphs appropriately + let (cell_height, cell_width) = font.get_metrics()?; + let setup = conn.get_setup(); let screen = setup.roots().nth(screen_num as usize).ok_or( failure::err_msg( @@ -350,15 +88,171 @@ impl<'a> TerminalWindow<'a> { Ok(TerminalWindow { window_id, + screen_num, conn, width, height, + font, + cell_height, + cell_width, }) } fn show(&self) { xcb::map_window(self.conn, self.window_id); } + + fn paint(&mut self) -> Result<(), Error> { + let screen = self.conn + .get_setup() + .roots() + .nth(self.screen_num as usize) + .ok_or(failure::err_msg("no screen?"))?; + let surface = cairo::Surface::create( + &cairo::XCBConnection(unsafe { mem::transmute(self.conn.get_raw_conn()) }), + &cairo::XCBDrawable(self.window_id), + &cairo::XCBVisualType(unsafe { + mem::transmute(&mut visual_for_screen(&screen).base as *mut _) + }), + self.width as i32, + self.height as i32, + ); + let ctx = cairo::Context::new(&surface); + let message = "x_advance != foo->bar(); ❤ 😍🤢"; + + ctx.set_source_rgba(0.8, 0.8, 0.8, 1.0); + + let mut x = 0.0; + let mut y = self.cell_height.ceil(); + let glyph_info = self.font.shape(0, message)?; + for info in glyph_info { + let ft_glyph = self.font.load_glyph(info.font_idx, info.glyph_pos)?; + + let scale = if (info.x_advance / info.num_cells as f64).floor() > self.cell_width { + info.num_cells as f64 * (self.cell_width / info.x_advance) + } else if ft_glyph.bitmap.rows as f64 > self.cell_height { + self.cell_height / ft_glyph.bitmap.rows as f64 + } else { + 1.0f64 + }; + + if ft_glyph.bitmap.width == 0 || ft_glyph.bitmap.rows == 0 { + // a whitespace glyph + } else { + + let mode: ftwrap::FT_Pixel_Mode = + unsafe { mem::transmute(ft_glyph.bitmap.pixel_mode as u32) }; + + // pitch is the number of bytes per source row + let pitch = ft_glyph.bitmap.pitch.abs() as usize; + let data = unsafe { + slice::from_raw_parts_mut( + ft_glyph.bitmap.buffer, + ft_glyph.bitmap.rows as usize * pitch, + ) + }; + + let cairo_surface = match mode { + ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_LCD => { + // The FT rasterization is often not aligned in the way that + // cairo would like, so let's allocate a surface of the correct + // size and fill that up. + let mut surface = cairo::ImageSurface::create( + cairo::Format::Rgb24, + (ft_glyph.bitmap.width / 3) as i32, + ft_glyph.bitmap.rows as i32, + ).map_err(cairo_err)?; + { + let dest_pitch = surface.get_stride() as usize; + // debug!("LCD {:?} dest_pitch={}", ft_glyph.bitmap, dest_pitch); + let mut dest_data = surface.get_data()?; + for y in 0..ft_glyph.bitmap.rows as usize { + let dest_offset = y * dest_pitch; + let src_offset = y * pitch; + dest_data[dest_offset..dest_offset + pitch] + .copy_from_slice(&data[src_offset..src_offset + pitch]); + } + } + Ok(surface) + } + ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_GRAY => { + // we get GRAY if we change FT_Render_Mode to FT_RENDER_MODE_NORMAL. + // TODO: this isn't right; the glyphs seems super skinny + let mut surface = cairo::ImageSurface::create( + cairo::Format::ARgb32, + ft_glyph.bitmap.width as i32 * 3, + ft_glyph.bitmap.rows as i32, + ).map_err(cairo_err)?; + { + let dest_pitch = surface.get_stride() as usize; + debug!("GRAY {:?} dest_pitch={}", ft_glyph.bitmap, dest_pitch); + let mut dest_data = surface.get_data()?; + for y in 0..ft_glyph.bitmap.rows as usize { + let dest_offset = y * dest_pitch; + let src_offset = y * pitch; + for x in 0..ft_glyph.bitmap.width as usize { + let gray = data[src_offset + x]; + // TODO: this is likely endian specific + dest_data[dest_offset + x] = gray; + dest_data[dest_offset + x + 1] = gray; + dest_data[dest_offset + x + 2] = gray; + dest_data[dest_offset + x + 3] = 0xff; + } + } + } + Ok(surface) + } + ftwrap::FT_Pixel_Mode::FT_PIXEL_MODE_BGRA => unsafe { + cairo::ImageSurface::from_raw_full( + cairo_sys::cairo_image_surface_create_for_data( + data.as_mut_ptr(), + cairo::Format::ARgb32, + ft_glyph.bitmap.width as i32, + ft_glyph.bitmap.rows as i32, + pitch as i32, + ), + ) + }, + mode @ _ => bail!("unhandled pixel mode: {:?}", mode), + }.map_err(cairo_err)?; + + let bearing_x = ft_glyph.bitmap_left as f64; + let bearing_y = ft_glyph.bitmap_top as f64; + + // TODO: colorize! + + debug!( + "x,y: {},{} bearing:{},{} off={},{} adv={},{} scale={}", + x, + y, + bearing_x, + bearing_y, + info.x_offset, + info.y_offset, + info.x_advance, + info.y_advance, + scale, + ); + ctx.translate(x, y); + ctx.scale(scale, scale); + ctx.set_source_surface( + &cairo_surface, + // Destination for the paint operation + info.x_offset + bearing_x, + -(info.y_offset + bearing_y), + ); + ctx.paint(); + ctx.identity_matrix(); + } + + x += scale * info.x_advance; + y += scale * info.y_advance; + } + + surface.flush(); + self.conn.flush(); + Ok(()) + } } fn visual_for_screen(screen: &xcb::Screen) -> xcb::Visualtype { @@ -373,14 +267,11 @@ fn visual_for_screen(screen: &xcb::Screen) -> xcb::Visualtype { unreachable!("screen doesn't have info on its own visual?"); } -fn run_xcb() -> Result<(), Error> { - use cairo::XCBSurface; - use cairo::prelude::*; - +fn run() -> Result<(), Error> { let (conn, screen_num) = xcb::Connection::connect(None)?; println!("Connected screen {}", screen_num); - let mut window = TerminalWindow::new(&conn, screen_num, 1024, 768)?; + let mut window = TerminalWindow::new(&conn, screen_num, 300, 300)?; window.show(); conn.flush(); @@ -393,31 +284,7 @@ fn run_xcb() -> Result<(), Error> { match r { xcb::EXPOSE => { println!("expose"); - let screen = conn.get_setup().roots().nth(screen_num as usize).ok_or( - failure::err_msg("no screen?"), - )?; - let surface = - cairo::Surface::create( - &cairo::XCBConnection(unsafe { mem::transmute(conn.get_raw_conn()) }), - &cairo::XCBDrawable(window.window_id), - &cairo::XCBVisualType(unsafe { - mem::transmute(&mut visual_for_screen(&screen).base as *mut _) - }), - window.width as i32, - window.height as i32, - ); - let ctx = cairo::Context::new(&surface); - ctx.select_font_face( - "Operator Mono", - cairo::FontSlant::Normal, - cairo::FontWeight::Normal, - ); - ctx.set_font_size(32.0); - ctx.set_source_rgb(0.8, 0.8, 0.8); - ctx.move_to(10.0, 50.0); - ctx.show_text("x_advance != foo->bar(); ❤ 😍🤢"); - surface.flush(); - conn.flush(); + window.paint()?; } xcb::CONFIGURE_NOTIFY => { let cfg: &xcb::ConfigureNotifyEvent = unsafe { xcb::cast_event(&event) }; @@ -439,7 +306,6 @@ fn run_xcb() -> Result<(), Error> { Ok(()) } - fn main() { - run_xcb().unwrap(); + run().unwrap(); }