summary refs log tree commit diff
path: root/src/x/font.rs
blob: e0e3602383c9a400b0f711a094825aabe493ab50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::x::*;

use std::ffi::CString;
use std::sync::Arc;

use x11::xft::{ XftDrawStringUtf8, XftFontOpenName };


pub struct Font {
  _display: Arc<Display>,
  font_state: *mut x11::xft::XftFont,
}


impl Font {
  pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Font> {
    let xlib_display = display.connection().get_xlib_display();
    let screen_index = display.screen_index();

    let name = CString::new(name).unwrap();
    let font_state = unsafe {
      XftFontOpenName(xlib_display, screen_index, name.as_ptr().cast())
    };

    Ok(Font { _display: display, font_state })
  }

  pub fn draw(&self, context: &FontGraphicContext, color: &Color,
	      x: i32, y: i32, text: &str)
    -> xcb::Result<()>
  {
    let xft_draw = context.get_xft_draw();
    let text = CString::new(text).unwrap();
    unsafe {
      XftDrawStringUtf8(xft_draw, color.as_xft_color(), self.font_state, x, y,
                        text.as_ptr().cast(), text.count_bytes() as i32)
    };

    Ok(())
  }
}