tony / oxwm

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