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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Font object for widget text styling.
//!
//! Wraps [`QFont`](https://doc.qt.io/qt-6/qfont.html).
use cxx::let_cxx_string;
use crate::ffi;
/// A font object that can be applied to widgets.
///
/// `Font` uses a **builder pattern**: call [`Font::new`] to obtain a
/// [`Builder`], chain `.family()`, `.point_size()`, `.bold()`, then call
/// `.build()`.
///
/// # Memory safety
///
/// The underlying `QFont` is allocated on the heap and automatically
/// deleted when the `Font` wrapper is dropped.
///
/// # Example
///
/// ```no_run
/// use qtrs::Font;
///
/// let font = Font::new()
/// .family("WenQuanYi Micro Hei")
/// .point_size(14)
/// .bold(true)
/// .build();
///
/// label.set_font(&font);
/// ```
pub struct Font {
ptr: *mut ffi::QFont,
}
impl Font {
/// Start building a new `QFont`.
///
/// Returns a [`Builder`]. Chain configuration, then call `.build()`.
pub fn new() -> Builder {
Builder::new()
}
/// Get the font family name.
pub fn family(&self) -> String {
debug_assert!(!self.ptr.is_null(), "Font::family on null pointer");
unsafe { ffi::QFont_family(self.ptr) }
}
/// Get the point size.
pub fn point_size(&self) -> i32 {
debug_assert!(!self.ptr.is_null(), "Font::point_size on null pointer");
unsafe { ffi::QFont_pointSize(self.ptr) }
}
/// Get the pixel size (or 0 if not set).
pub fn pixel_size(&self) -> i32 {
debug_assert!(!self.ptr.is_null(), "Font::pixel_size on null pointer");
unsafe { ffi::QFont_pixelSize(self.ptr) }
}
/// Check if the font is bold.
pub fn bold(&self) -> bool {
debug_assert!(!self.ptr.is_null(), "Font::bold on null pointer");
unsafe { ffi::QFont_bold(self.ptr) }
}
/// Check if the font is italic.
pub fn italic(&self) -> bool {
debug_assert!(!self.ptr.is_null(), "Font::italic on null pointer");
unsafe { ffi::QFont_italic(self.ptr) }
}
/// Check if the font is underlined.
pub fn underline(&self) -> bool {
debug_assert!(!self.ptr.is_null(), "Font::underline on null pointer");
unsafe { ffi::QFont_underline(self.ptr) }
}
/// Check if the font has strikethrough.
pub fn strike_out(&self) -> bool {
debug_assert!(!self.ptr.is_null(), "Font::strike_out on null pointer");
unsafe { ffi::QFont_strikeOut(self.ptr) }
}
/// Get the font weight (0-99, 50 = normal, 75 = bold).
pub fn weight(&self) -> i32 {
debug_assert!(!self.ptr.is_null(), "Font::weight on null pointer");
unsafe { ffi::QFont_weight(self.ptr) }
}
/// Wrap an existing `QFont*`.
#[doc(hidden)]
pub(crate) fn from_raw(ptr: *mut ffi::QFont) -> Self {
debug_assert!(!ptr.is_null());
Self { ptr }
}
/// Get the raw pointer for C++ interop.
#[doc(hidden)]
pub fn raw_ptr(&self) -> *mut ffi::QFont {
self.ptr
}
}
impl Drop for Font {
fn drop(&mut self) {
if self.ptr.is_null() {
return;
}
unsafe { ffi::QFont_delete(self.ptr) };
self.ptr = std::ptr::null_mut();
}
}
// ============================================================
// Builder
// ============================================================
/// Builder for [`Font`].
///
/// Collects font properties, then creates the C++ `QFont` in
/// [`build`](Self::build).
pub struct Builder {
family: Option<String>,
point_size: Option<i32>,
pixel_size: Option<i32>,
bold: Option<bool>,
italic: Option<bool>,
underline: Option<bool>,
strike_out: Option<bool>,
weight: Option<i32>,
}
impl Builder {
fn new() -> Self {
Self {
family: None,
point_size: None,
pixel_size: None,
bold: None,
italic: None,
underline: None,
strike_out: None,
weight: None,
}
}
/// Set the font family (e.g., "Arial", "WenQuanYi Micro Hei").
pub fn family(mut self, family: impl Into<String>) -> Self {
self.family = Some(family.into());
self
}
/// Set the font size in points (1/72 inch).
pub fn point_size(mut self, size: i32) -> Self {
self.point_size = Some(size);
self
}
/// Set the font size in pixels.
pub fn pixel_size(mut self, size: i32) -> Self {
self.pixel_size = Some(size);
self
}
/// Enable bold font weight.
pub fn bold(mut self, bold: bool) -> Self {
self.bold = Some(bold);
self
}
/// Enable italic style.
pub fn italic(mut self, italic: bool) -> Self {
self.italic = Some(italic);
self
}
/// Enable underline.
pub fn underline(mut self, underline: bool) -> Self {
self.underline = Some(underline);
self
}
/// Enable strikethrough.
pub fn strike_out(mut self, strike_out: bool) -> Self {
self.strike_out = Some(strike_out);
self
}
/// Set the font weight (0-99).
///
/// Common values:
/// - 50 = Normal (default)
/// - 75 = Bold
/// - 25 = Light
/// - 87 = Black
pub fn weight(mut self, weight: i32) -> Self {
self.weight = Some(weight);
self
}
/// Create the C++ `QFont` and return the Rust wrapper.
///
/// This is the terminal method of the builder pattern.
pub fn build(self) -> Font {
let ptr = unsafe { ffi::QFont_new() };
assert!(!ptr.is_null(), "QFont_new returned null");
let font = Font { ptr };
// Apply all configured properties
if let Some(family) = &self.family {
let_cxx_string!(c_family = family);
unsafe { ffi::QFont_setFamily(font.ptr, &c_family); }
}
if let Some(size) = self.point_size {
unsafe { ffi::QFont_setPointSize(font.ptr, size); }
}
if let Some(size) = self.pixel_size {
unsafe { ffi::QFont_setPixelSize(font.ptr, size); }
}
if let Some(bold) = self.bold {
unsafe { ffi::QFont_setBold(font.ptr, bold); }
}
if let Some(italic) = self.italic {
unsafe { ffi::QFont_setItalic(font.ptr, italic); }
}
if let Some(underline) = self.underline {
unsafe { ffi::QFont_setUnderline(font.ptr, underline); }
}
if let Some(strike_out) = self.strike_out {
unsafe { ffi::QFont_setStrikeOut(font.ptr, strike_out); }
}
if let Some(weight) = self.weight {
unsafe { ffi::QFont_setWeight(font.ptr, weight); }
}
font
}
}
impl Default for Font {
fn default() -> Self {
Self::new().build()
}
}
// ============================================================
// FontExt — blanket impl over all AsWidget types
// ============================================================
use crate::widget::AsWidget;
/// Extension trait that gives every widget `font()` / `set_font()`.
///
/// Blanket-implemented for all types that implement [`AsWidget`], so every
/// widget in qtrs automatically gets these methods.
pub trait FontExt: AsWidget {
/// Get the current font of this widget.
///
/// Returns a **copy** of the widget's font. Changes to the returned
/// [`Font`] do not affect the widget — call [`set_font`](FontExt::set_font)
/// to apply changes.
fn font(&self) -> Font {
let ptr = unsafe { crate::ffi::QWidget_font(self.widget_ptr()) };
debug_assert!(!ptr.is_null(), "QWidget_font returned null");
Font::from_raw(ptr)
}
/// Set the font for this widget.
fn set_font(&self, font: &Font) {
unsafe { crate::ffi::QWidget_setFont(self.widget_ptr(), font.raw_ptr()); }
}
}
impl<T: AsWidget> FontExt for T {}