tony / oxwm

Branch: ab1e2fc
Switch branches/tags
master
tonybanters fixed some warnings 50d8817 2025-10-04
135 lines · 3.4 KB Raw Blame History
1 use anyhow::Result;
2 use std::ffi::CString;
3 use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName};
4 use x11::xlib::{Colormap, Display, Drawable, Visual};
5 use x11::xrender::XRenderColor;
6
7 pub struct Font {
8 xft_font: *mut XftFont,
9 display: *mut Display,
10 }
11
12 impl Font {
13 pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self> {
14 let font_name_cstr = CString::new(font_name)?;
15
16 let xft_font = unsafe { XftFontOpenName(display, screen, font_name_cstr.as_ptr()) };
17
18 if xft_font.is_null() {
19 anyhow::bail!("Failed to load font: {}", font_name);
20 }
21
22 Ok(Font { xft_font, display })
23 }
24
25 pub fn height(&self) -> u16 {
26 unsafe {
27 let font = &*self.xft_font;
28 font.height as u16
29 }
30 }
31
32 pub fn ascent(&self) -> i16 {
33 unsafe {
34 let font = &*self.xft_font;
35 font.ascent as i16
36 }
37 }
38
39 pub fn text_width(&self, text: &str) -> u16 {
40 unsafe {
41 let mut extents = std::mem::zeroed();
42 x11::xft::XftTextExtentsUtf8(
43 self.display,
44 self.xft_font,
45 text.as_ptr(),
46 text.len() as i32,
47 &mut extents,
48 );
49 extents.width
50 }
51 }
52 }
53
54 impl Drop for Font {
55 fn drop(&mut self) {
56 unsafe {
57 if !self.xft_font.is_null() {
58 x11::xft::XftFontClose(self.display, self.xft_font);
59 }
60 }
61 }
62 }
63
64 pub struct FontDraw {
65 xft_draw: *mut XftDraw,
66 }
67
68 impl FontDraw {
69 pub fn new(
70 display: *mut Display,
71 drawable: Drawable,
72 visual: *mut Visual,
73 colormap: Colormap,
74 ) -> Result<Self> {
75 let xft_draw = unsafe { x11::xft::XftDrawCreate(display, drawable, visual, colormap) };
76
77 if xft_draw.is_null() {
78 anyhow::bail!("Failed to create XftDraw");
79 }
80
81 Ok(FontDraw { xft_draw })
82 }
83
84 pub fn draw_text(&self, font: &Font, color: u32, x: i16, y: i16, text: &str) {
85 let red = ((color >> 16) & 0xFF) as u16;
86 let green = ((color >> 8) & 0xFF) as u16;
87 let blue = (color & 0xFF) as u16;
88
89 let render_color = XRenderColor {
90 red: red << 8 | red,
91 green: green << 8 | green,
92 blue: blue << 8 | blue,
93 alpha: 0xFFFF,
94 };
95
96 let mut xft_color: XftColor = unsafe { std::mem::zeroed() };
97
98 unsafe {
99 x11::xft::XftColorAllocValue(
100 x11::xft::XftDrawDisplay(self.xft_draw),
101 x11::xft::XftDrawVisual(self.xft_draw),
102 x11::xft::XftDrawColormap(self.xft_draw),
103 &render_color,
104 &mut xft_color,
105 );
106
107 XftDrawStringUtf8(
108 self.xft_draw,
109 &xft_color,
110 font.xft_font,
111 x as i32,
112 y as i32,
113 text.as_ptr(),
114 text.len() as i32,
115 );
116
117 x11::xft::XftColorFree(
118 x11::xft::XftDrawDisplay(self.xft_draw),
119 x11::xft::XftDrawVisual(self.xft_draw),
120 x11::xft::XftDrawColormap(self.xft_draw),
121 &mut xft_color,
122 );
123 }
124 }
125 }
126
127 impl Drop for FontDraw {
128 fn drop(&mut self) {
129 unsafe {
130 if !self.xft_draw.is_null() {
131 x11::xft::XftDrawDestroy(self.xft_draw);
132 }
133 }
134 }
135 }