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
|
use crate::x::*;
use crate::geometry::{ Point, Offset, Size };
use std::cell::OnceCell;
use std::ffi::CString;
use std::sync::Arc;
use x11::xft::{
XftCharExists, XftDrawStringUtf8, XftTextExtentsUtf8, XftFontOpenName
};
use x11::xrender::XGlyphInfo;
pub struct Font {
display: Arc<Display>,
font_state: *mut x11::xft::XftFont,
metrics: OnceCell<(u16,u16)>,
}
pub struct TextSpan {
font: Arc<Font>,
text: CString,
extent: XGlyphInfo,
}
impl PartialEq for Font {
fn eq(&self, other: &Font) -> bool {
self.display == other.display
&& self.font_state == other.font_state
}
}
impl Font {
pub fn new(display: Arc<Display>, name: &str) -> xcb::Result<Arc<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())
};
let font = Arc::new(Font {
display, font_state,
metrics: OnceCell::new(),
});
let x_height = font.measure_character_heights("acemnorsuvwxz")?;
let ascent = font.text_span("bdfhiklt")?.draw_size().height;
let descent = font.text_span("gpqy")?.draw_size().height - x_height;
let _ = font.metrics.set((ascent, descent));
Ok(font)
}
pub fn ascent(self: &Arc<Self>) -> u16 {
let (ascent, _) = self.metrics.get().unwrap();
*ascent
}
pub fn descent(self: &Arc<Self>) -> u16 {
let (_, descent) = self.metrics.get().unwrap();
*descent
}
pub fn text_span(self: &Arc<Self>, text: &str) -> xcb::Result<TextSpan> {
TextSpan::new(Arc::clone(self), text)
}
fn measure_character_heights(self: &Arc<Self>, repertoire: &str)
-> xcb::Result<u16>
{
let mut max_height = 0;
for c in repertoire.chars() {
let span = self.text_span(&format!("{}", c))?;
max_height = max_height.max(span.draw_size().height);
}
Ok(max_height)
}
pub fn check_char_exists(self: &Arc<Self>, codepoint: u32) -> xcb::Result<bool> {
let xlib_display = self.display.connection().get_xlib_display();
let result = unsafe {
XftCharExists(xlib_display,
self.font_state,
codepoint)
};
Ok(result != 0)
}
}
impl TextSpan {
pub fn new(font: Arc<Font>, text: &str) -> xcb::Result<Self> {
let xlib_display = font.display.connection().get_xlib_display();
let text = CString::new(text).unwrap();
let mut extent = XGlyphInfo {
width: 0,
height: 0,
x: 0,
y: 0,
xOff: 0,
yOff: 0,
};
unsafe {
XftTextExtentsUtf8(xlib_display,
font.font_state,
text.as_ptr().cast(),
text.count_bytes() as i32,
&mut extent)
};
Ok(TextSpan { font, text, extent })
}
/* The coordinates given to draw() are those of the left-side baseline of
* the span's as-drawn pixel boundaries, in window coordinate space. Recall
* that window coordinate space puts the origin at the top-left.
*
* As-drawn boundaries are about what pixels actually get marked on the
* screen, which include the descender and any marks that are outside the
* font's bounds as described by its metrics.
*
* Though this can be counterintuitive to programmers, it is normal for
* fonts to draw outside their bounds. Font metrics are primarily intended
* to advise about graphic-design topics, which are socially defined and do
* not necessarily obey mathematical identities, even when such identities
* are offered as explanations of their meaning.
*
* Instead, the mathematical meaning of these metrics serves as a first
* layer, a literal meaning which provides a reference point for additional
* layers of meaning to be added atop, in the same fashion as how the
* duration of a musical measure is not actually fixed during a live
* performance.
*
* There are very good reasons that programmers find this state of affairs
* deeply saddening, but there is nothing to be done but deal with it.
*/
pub fn draw(&self, context: &FontGraphicContext, color: &Color,
location: &Point<i32>)
-> xcb::Result<()>
{
let xft_draw = context.get_xft_draw();
unsafe {
XftDrawStringUtf8(xft_draw,
color.as_xft_color(),
self.font.font_state,
location.x, location.y,
self.text.as_ptr().cast(),
self.text.count_bytes() as i32)
};
Ok(())
}
/* This returns the offset from the top-left of the span's logical parcel to the
* left-side baseline of its as-drawn pixel boundaries. See the note on draw() for
* more context on what these as-drawn boundaries mean and why.
*/
pub fn draw_offset(&self) -> Offset<i16> {
Offset {
x: self.extent.x,
y: self.extent.y,
}
}
/* This is the as-drawn pixel size, rounded up to the next whole pixel,
* so that it is safe to use as a bounding box. It is computed only with
* respect to characters present in the span, and only considers the marks
* they actually draw.
*
* There are two important implications. First, due to rounding,
* pixel_size() is likely to be wider than logical_offset(). Second, trailing
* spaces are not included, since they do not draw any marks.
*
* In the vertical dimension, this size is conceptually similar to the
* maximum ascent plus the maximum descent of any character present in the
* span. Since there is no way within xft to obtain the font's declared
* ascent or descent, this measurement may be the best way to approximate
* those measurements. Note that there is nothing that even approximates line
* height; just guess.
*
* If you're thinking this all sounds highly inconvenient, you are correct.
* See the note on draw() for some highly relevant philosophy.
*/
pub fn draw_size(&self) -> Size<u16> {
Size {
width: self.extent.width,
height: self.extent.height,
}
}
/* This returns the offset from the left-side baseline of the span's
* logical parcel to the new left-side baseline that a span occurring
* immediately after it would start from.
*
* This is rounded in some fashion, most likely to the nearest whole
* pixel, which causes it to often be smaller than draw_size(). See the note
* on draw() for some highly relevant philosophy.
*/
#[allow(unused)]
pub fn logical_offset(&self) -> Offset<i16> {
Offset {
x: self.extent.xOff,
y: self.extent.yOff,
}
}
}
|