Skip to content

Commit be4e2b1

Browse files
huacnleeAmaanBilwar
authored andcommitted
gpui: Fix macOS font render clipped bug again (zed-industries#47001)
Continue zed-industries#45957 zed-industries#46906 to fix font render issue again. Release Notes: - Fixed macOS font render clipped bug when use `.SystemUIFont`. | .SystemUIFont | .ZedMono | | --- | --- | | <img width="1034" height="978" alt="image" src="https://github.com/user-attachments/assets/96b815a1-9484-4a38-8391-a4af3db51a36" /> | <img width="1034" height="978" alt="image" src="https://github.com/user-attachments/assets/1aaece42-9acd-47b0-a0a0-3cb425f40301" /> | ```bash cargo run -p gpui --example text ``` Test in Zed with `.ZedMono` font: <img width="815" height="844" alt="image" src="https://github.com/user-attachments/assets/1e44d186-cee5-4f54-910a-4c4602ca010e" /> With `.ZedSans`: <img width="815" height="844" alt="image" src="https://github.com/user-attachments/assets/1e502f3e-794c-4082-9faf-5a920adc1214" /> `Monaco`: <img width="815" height="844" alt="image" src="https://github.com/user-attachments/assets/906a3fd2-715a-4f53-b6fe-4614f4c6edab" /> `Menlo`: <img width="815" height="844" alt="image" src="https://github.com/user-attachments/assets/a8f8c302-2083-477c-ae72-f6e5c7f91d00" />
1 parent 5847846 commit be4e2b1

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

‎crates/gpui/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ windows = { version = "0.61", features = ["Win32_Foundation"] }
144144
backtrace.workspace = true
145145
collections = { workspace = true, features = ["test-support"] }
146146
env_logger.workspace = true
147-
gpui_platform.workspace = true
147+
gpui_platform = { workspace = true, features = ["font-kit"] }
148148
lyon = { version = "1.0", features = ["extra"] }
149149
rand.workspace = true
150150
scheduler = { workspace = true, features = ["test-support"] }

‎crates/gpui/examples/text.rs‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![cfg_attr(target_family = "wasm", no_main)]
22

33
use std::{
4+
borrow::Cow,
45
ops::{Deref, DerefMut},
56
sync::Arc,
67
};
@@ -204,7 +205,7 @@ impl RenderOnce for CharacterGrid {
204205
"❮", "<=", "!=", "==", "--", "++", "=>", "->", "🏀", "🎊", "😍", "❤️", "👍", "👎",
205206
];
206207

207-
let columns = 11;
208+
let columns = 20;
208209
let rows = characters.len().div_ceil(columns);
209210

210211
let grid_rows = (0..rows).map(|row_idx| {
@@ -238,15 +239,41 @@ impl RenderOnce for CharacterGrid {
238239

239240
struct TextExample {
240241
next_id: usize,
242+
font_family: SharedString,
241243
}
242244

243245
impl TextExample {
244246
fn next_id(&mut self) -> usize {
245247
self.next_id += 1;
246248
self.next_id
247249
}
250+
251+
fn button(
252+
text: &str,
253+
cx: &mut Context<Self>,
254+
on_click: impl Fn(&mut Self, &mut Context<Self>) + 'static,
255+
) -> impl IntoElement {
256+
div()
257+
.id(text.to_string())
258+
.flex_none()
259+
.child(text.to_string())
260+
.bg(gpui::black())
261+
.text_color(gpui::white())
262+
.active(|this| this.opacity(0.8))
263+
.px_3()
264+
.py_1()
265+
.on_click(cx.listener(move |this, _, _, cx| on_click(this, cx)))
266+
}
248267
}
249268

269+
const FONT_FAMILIES: [&str; 5] = [
270+
".ZedMono",
271+
".SystemUIFont",
272+
"Menlo",
273+
"Monaco",
274+
"Courier New",
275+
];
276+
250277
impl Render for TextExample {
251278
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
252279
let tcx = cx.text_context();
@@ -265,7 +292,26 @@ impl Render for TextExample {
265292
let step_up_6 = step_up_5 * type_scale;
266293

267294
div()
295+
.font_family(self.font_family.clone())
268296
.size_full()
297+
.child(
298+
div()
299+
.bg(gpui::white())
300+
.border_b_1()
301+
.border_color(gpui::black())
302+
.p_3()
303+
.flex()
304+
.child(Self::button(&self.font_family, cx, |this, cx| {
305+
let new_family = FONT_FAMILIES
306+
.iter()
307+
.position(|f| *f == this.font_family.as_str())
308+
.map(|idx| FONT_FAMILIES[(idx + 1) % FONT_FAMILIES.len()])
309+
.unwrap_or(FONT_FAMILIES[0]);
310+
311+
this.font_family = SharedString::new(new_family);
312+
cx.notify();
313+
})),
314+
)
269315
.child(
270316
div()
271317
.id("text-example")
@@ -307,6 +353,15 @@ fn run_example() {
307353
items: vec![],
308354
}]);
309355

356+
let fonts = [include_bytes!(
357+
"../../../assets/fonts/lilex/Lilex-Regular.ttf"
358+
)]
359+
.iter()
360+
.map(|b| Cow::Borrowed(&b[..]))
361+
.collect();
362+
363+
_ = cx.text_system().add_fonts(fonts);
364+
310365
cx.init_colors();
311366
cx.set_global(GlobalTextContext(Arc::new(TextContext::default())));
312367

@@ -323,7 +378,12 @@ fn run_example() {
323378
))),
324379
..Default::default()
325380
},
326-
|_window, cx| cx.new(|_cx| TextExample { next_id: 0 }),
381+
|_window, cx| {
382+
cx.new(|_cx| TextExample {
383+
next_id: 0,
384+
font_family: ".ZedMono".into(),
385+
})
386+
},
327387
)
328388
.unwrap();
329389

‎crates/gpui_macos/src/text_system.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,22 @@ impl MacTextSystemState {
361361
fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
362362
let font = &self.fonts[params.font_id.0];
363363
let scale = Transform2F::from_scale(params.scale_factor);
364-
Ok(bounds_from_rect_i(font.raster_bounds(
364+
let mut bounds: Bounds<DevicePixels> = bounds_from_rect_i(font.raster_bounds(
365365
params.glyph_id.0,
366366
params.font_size.into(),
367367
scale,
368368
HintingOptions::None,
369369
font_kit::canvas::RasterizationOptions::GrayscaleAa,
370-
)?))
370+
)?);
371+
372+
// Add 3% of font size as padding, clamped between 1 and 5 pixels
373+
// to avoid clipping of anti-aliased edges.
374+
let pad =
375+
((params.font_size.as_f32() * 0.03 * params.scale_factor).ceil() as i32).clamp(1, 5);
376+
bounds.origin.x -= DevicePixels(pad);
377+
bounds.size.width += DevicePixels(pad);
378+
379+
Ok(bounds)
371380
}
372381

373382
fn rasterize_glyph(

0 commit comments

Comments
 (0)