Skip to content

Commit f2d7b3a

Browse files
authored
Update cosmic-text to 0.14 (#870)
This allows us to specify font features, which means we can turn off kerning, ligatures and enable tabular nums for our monotonic text.
1 parent 78aba86 commit f2d7b3a

File tree

3 files changed

+49
-56
lines changed

3 files changed

+49
-56
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ image = { version = "0.25.0", features = [
6868
# Currently doesn't require any additional dependencies.
6969

7070
# Default Text Engine
71-
cosmic-text = { version = "0.13.2", default-features = false, features = [
71+
cosmic-text = { version = "0.14.1", default-features = false, features = [
7272
"no_std",
7373
], optional = true }
7474

src/rendering/default_text_engine/mod.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::{
1010
};
1111

1212
use cosmic_text::{
13-
Attrs, AttrsList, Family, FontSystem, ShapeLine, Shaping, Stretch, Style, Weight,
13+
Attrs, AttrsList, Family, FeatureTag, FontFeatures, FontSystem, ShapeLine, Shaping, Stretch,
14+
Style, Weight,
1415
fontdb::{Database, ID, Query, Source},
1516
rustybuzz::ttf_parser::{GlyphId, OutlineBuilder},
1617
};
@@ -27,7 +28,6 @@ mod color_font;
2728
struct CachedGlyph<P> {
2829
scale: f32,
2930
paths: Vec<(Option<Rgba>, P)>,
30-
unkerned_x_advance: f32,
3131
}
3232

3333
/// The text engine allows you to create fonts and manage text labels. That way
@@ -126,13 +126,22 @@ impl<P: SharedOwnership> TextEngine<P> {
126126
}
127127
}
128128

129-
let attrs_list = AttrsList::new(
130-
Attrs::new()
131-
.family(Family::Name(family))
132-
.stretch(stretch)
133-
.style(style)
134-
.weight(weight),
135-
);
129+
let mut attrs = Attrs::new()
130+
.family(Family::Name(family))
131+
.stretch(stretch)
132+
.style(style)
133+
.weight(weight);
134+
135+
if kind.is_monospaced() {
136+
let mut features = FontFeatures::new();
137+
features.disable(FeatureTag::KERNING);
138+
features.disable(FeatureTag::STANDARD_LIGATURES);
139+
features.disable(FeatureTag::CONTEXTUAL_LIGATURES);
140+
features.enable(FeatureTag::new(b"tnum"));
141+
attrs = attrs.font_features(features);
142+
}
143+
144+
let attrs_list = AttrsList::new(&attrs);
136145

137146
let monotonic = kind.is_monospaced().then(|| {
138147
let mut digit_glyphs = [(ID::dummy(), 0); 10];
@@ -248,26 +257,17 @@ impl<P: SharedOwnership> TextEngine<P> {
248257
&mut path_builder,
249258
);
250259

251-
// FIXME: We use the x advance of the individual
252-
// glyph to remove any kerning that happened during
253-
// shaping. This is a workaround for the fact that
254-
// cosmic-text doesn't provide a way to turn off
255-
// kerning (and / or enable tabular nums) at the
256-
// moment.
257-
// https://github.com/pop-os/cosmic-text/issues/229
258-
let unkerned_x_advance = cached_glyph.unkerned_x_advance;
259-
260260
let (x_advance, x_offset) = if monotonic
261261
.digit_glyphs
262262
.contains(&(glyph.font_id, glyph.glyph_id))
263263
{
264264
(
265265
monotonic.digit_width,
266-
0.5 * (monotonic.digit_width - unkerned_x_advance)
266+
0.5 * (monotonic.digit_width - glyph.x_advance)
267267
+ glyph.x_offset,
268268
)
269269
} else {
270-
(unkerned_x_advance, glyph.x_offset)
270+
(glyph.x_advance, glyph.x_offset)
271271
};
272272

273273
label
@@ -392,14 +392,7 @@ fn cache_glyph<'gc, P, PB: PathBuilder<Path = P>>(
392392
paths.push((color.map(|c| c.to_array()), path));
393393
});
394394
let scale = f32::recip(font.units_per_em() as _);
395-
CachedGlyph {
396-
scale,
397-
paths,
398-
unkerned_x_advance: font
399-
.glyph_hor_advance(glyph)
400-
.map(|v| v as f32 * scale)
401-
.unwrap_or_default(),
402-
}
395+
CachedGlyph { scale, paths }
403396
})
404397
}
405398

tests/rendering.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ fn default() {
4848
check(
4949
&state,
5050
&image_cache,
51-
"40afc8d9b921c018",
52-
"29bf0ddd39a138e7",
51+
"613e94c31d4d76c3",
52+
"c2ae6252eec1d1b5",
5353
"default",
5454
);
5555
}
@@ -187,8 +187,8 @@ fn actual_split_file() {
187187
check(
188188
&layout.state(&mut image_cache, &timer.snapshot()),
189189
&image_cache,
190-
"0bd8b41b95ae974c",
191-
"0a4b3f4cb44ffbbb",
190+
"ef12a76febd34908",
191+
"a261fbed384d6d51",
192192
"actual_split_file",
193193
);
194194
}
@@ -204,8 +204,8 @@ fn wsplit() {
204204
&layout.state(&mut image_cache, &timer.snapshot()),
205205
&image_cache,
206206
[250, 300],
207-
"be70116db88c23c1",
208-
"cf399969d8ac9baf",
207+
"445f85286cd8a439",
208+
"e198b005bb113d39",
209209
"wsplit",
210210
);
211211
}
@@ -224,8 +224,8 @@ fn timer_delta_background() {
224224
&layout.state(&mut image_cache, &timer.snapshot()),
225225
&image_cache,
226226
[250, 300],
227-
"4d9c5c580b0c435f",
228-
"bf8e5c596684688d",
227+
"e8c90ede5b64cad9",
228+
"951c1624fdd5b555",
229229
"timer_delta_background_ahead",
230230
);
231231

@@ -235,8 +235,8 @@ fn timer_delta_background() {
235235
&layout.state(&mut image_cache, &timer.snapshot()),
236236
&image_cache,
237237
[250, 300],
238-
"748fa26a41a8d5a3",
239-
"8bae1351d0dd52d7",
238+
"be5c85a9a5e3e4a5",
239+
"1f0cefb6b7cdc5ab",
240240
"timer_delta_background_stopped",
241241
);
242242
}
@@ -260,17 +260,17 @@ fn all_components() {
260260
&state,
261261
&image_cache,
262262
[300, 800],
263-
"ca9b92ae7357d0cd",
264-
"5e74113e4352d0b9",
263+
"ac1120ceabe1cf2f",
264+
"5ee085b9bd7d2551",
265265
"all_components",
266266
);
267267

268268
check_dims(
269269
&state,
270270
&image_cache,
271271
[150, 800],
272-
"ccbcbb6285133a58",
273-
"aef452d4bff2f3bd",
272+
"817568e8740f9b53",
273+
"f47d3dcaf69ecd66",
274274
"all_components_thin",
275275
);
276276
}
@@ -297,8 +297,8 @@ fn score_split() {
297297
&state,
298298
&image_cache,
299299
[300, 400],
300-
"6385959af93932cb",
301-
"ebbe996276cd8dd0",
300+
"cdf1b9e7ebc09f42",
301+
"96bcb8fcc09597cd",
302302
"score_split",
303303
);
304304
}
@@ -314,8 +314,8 @@ fn dark_layout() {
314314
check(
315315
&layout.state(&mut image_cache, &timer.snapshot()),
316316
&image_cache,
317-
"a47c590792c1bab5",
318-
"4ad7d528b3dc653d",
317+
"540515e778133820",
318+
"5ca393d8b84cb704",
319319
"dark_layout",
320320
);
321321
}
@@ -338,8 +338,8 @@ fn subsplits_layout() {
338338
&layout.state(&mut image_cache, &timer.snapshot()),
339339
&image_cache,
340340
[300, 800],
341-
"fc9658d65e0e0511",
342-
"e54f03f006192781",
341+
"de8412439f6ac04a",
342+
"a5e0f59de02a3f5f",
343343
"subsplits_layout",
344344
);
345345
}
@@ -362,8 +362,8 @@ fn background_image() {
362362
&layout.state(&mut image_cache, &timer.snapshot()),
363363
&image_cache,
364364
[300, 300],
365-
"81f6d2122b32065f",
366-
"57f284ed4a717a0d",
365+
"b48ae9755f316d47",
366+
"721485d08ac431f5",
367367
"background_image",
368368
);
369369
}
@@ -390,7 +390,7 @@ fn display_two_rows() {
390390
&image_cache,
391391
[200, 100],
392392
"0541122a81430490",
393-
"c5f95ed36a74130e",
393+
"e1eb912e295db230",
394394
"display_two_rows",
395395
);
396396
}
@@ -416,8 +416,8 @@ fn single_line_title() {
416416
&layout.state(&mut image_cache, &timer.snapshot()),
417417
&image_cache,
418418
[300, 60],
419-
"12d4b767f55e5545",
420-
"13bc0a3729c5df16",
419+
"b66b1db78eb324a4",
420+
"0a3977bc56c7b0ff",
421421
"single_line_title",
422422
);
423423
}
@@ -454,8 +454,8 @@ fn horizontal() {
454454
&layout.state(&mut image_cache, &timer.snapshot()),
455455
&image_cache,
456456
[1500, 40],
457-
"81d12a01d1b00092",
458-
"c22903674f3b0767",
457+
"45492bbd1df15cfa",
458+
"a56638d106e08232",
459459
"horizontal",
460460
);
461461
}

0 commit comments

Comments
 (0)