Skip to content

Commit ffea1e6

Browse files
Rename Stretch, Style, Weight to have Font prefix (#211)
This brings them into line with how they're used in `parley::style` as well as closer to how they're named in CSS where these properties are `font-stretch` (since renamed to `font-width`), `font-style`, and `font-weight`. This is a precursor to extracting this code into a separate crate for styling text.
1 parent 4fc2a6d commit ffea1e6

File tree

9 files changed

+141
-129
lines changed

9 files changed

+141
-129
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ This release has an [MSRV] of 1.75.
2424

2525
### Changed
2626

27-
### Parley
27+
#### Fontique
28+
29+
- Breaking change: `Stretch`, `Style`, and `Weight` renamed to `FontStretch`, `FontStyle`, `FontWeight ([#211][] by [@waywardmonkeys][])
30+
31+
#### Parley
2832

2933
- Breaking change: `PlainEditor`'s semantics are no longer transactional ([#192][] by [@DJMcNab][])
3034

@@ -103,6 +107,7 @@ This release has an [MSRV] of 1.70.
103107
[#143]: https://github.com/linebender/parley/pull/143
104108
[#192]: https://github.com/linebender/parley/pull/192
105109
[#198]: https://github.com/linebender/parley/pull/198
110+
[#211]: https://github.com/linebender/parley/pull/211
106111

107112
[Unreleased]: https://github.com/linebender/parley/compare/v0.2.0...HEAD
108113
[0.2.0]: https://github.com/linebender/parley/releases/tag/v0.2.0

fontique/src/attributes.rs

+39-39
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ use core_maths::CoreFloat;
99

1010
use core::fmt;
1111

12-
/// Primary attributes for font matching: [`Stretch`], [`Style`] and [`Weight`].
12+
/// Primary attributes for font matching: [`FontStretch`], [`FontStyle`] and [`FontWeight`].
1313
///
1414
/// These are used to [configure] a [`Query`].
1515
///
1616
/// [configure]: crate::Query::set_attributes
1717
/// [`Query`]: crate::Query
1818
#[derive(Copy, Clone, PartialEq, Default, Debug)]
1919
pub struct Attributes {
20-
pub stretch: Stretch,
21-
pub style: Style,
22-
pub weight: Weight,
20+
pub stretch: FontStretch,
21+
pub style: FontStyle,
22+
pub weight: FontWeight,
2323
}
2424

2525
impl Attributes {
2626
/// Creates new attributes from the given stretch, style and weight.
27-
pub fn new(stretch: Stretch, style: Style, weight: Weight) -> Self {
27+
pub fn new(stretch: FontStretch, style: FontStyle, weight: FontWeight) -> Self {
2828
Self {
2929
stretch,
3030
style,
@@ -46,7 +46,7 @@ impl fmt::Display for Attributes {
4646
/// Visual width of a font-- a relative change from the normal aspect
4747
/// ratio, typically in the range `0.5` to `2.0`.
4848
///
49-
/// The default value is [`Stretch::NORMAL`] or `1.0`.
49+
/// The default value is [`FontStretch::NORMAL`] or `1.0`.
5050
///
5151
/// In variable fonts, this can be controlled with the `wdth` [axis].
5252
///
@@ -57,9 +57,9 @@ impl fmt::Display for Attributes {
5757
/// [axis]: crate::AxisInfo
5858
/// [`font-width`]: https://www.w3.org/TR/css-fonts-4/#font-width-prop
5959
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
60-
pub struct Stretch(f32);
60+
pub struct FontStretch(f32);
6161

62-
impl Stretch {
62+
impl FontStretch {
6363
/// Width that is 50% of normal.
6464
pub const ULTRA_CONDENSED: Self = Self(0.5);
6565

@@ -88,16 +88,16 @@ impl Stretch {
8888
pub const ULTRA_EXPANDED: Self = Self(2.0);
8989
}
9090

91-
impl Stretch {
91+
impl FontStretch {
9292
/// Creates a new stretch attribute with the given ratio.
9393
///
9494
/// This can also be created [from a percentage](Self::from_percentage).
9595
///
9696
/// # Example
9797
///
9898
/// ```
99-
/// # use fontique::Stretch;
100-
/// assert_eq!(Stretch::from_ratio(1.5), Stretch::EXTRA_EXPANDED);
99+
/// # use fontique::FontStretch;
100+
/// assert_eq!(FontStretch::from_ratio(1.5), FontStretch::EXTRA_EXPANDED);
101101
/// ```
102102
pub fn from_ratio(ratio: f32) -> Self {
103103
Self(ratio)
@@ -110,8 +110,8 @@ impl Stretch {
110110
/// # Example
111111
///
112112
/// ```
113-
/// # use fontique::Stretch;
114-
/// assert_eq!(Stretch::from_percentage(87.5), Stretch::SEMI_CONDENSED);
113+
/// # use fontique::FontStretch;
114+
/// assert_eq!(FontStretch::from_percentage(87.5), FontStretch::SEMI_CONDENSED);
115115
/// ```
116116
pub fn from_percentage(percentage: f32) -> Self {
117117
Self(percentage / 100.0)
@@ -124,8 +124,8 @@ impl Stretch {
124124
/// # Example
125125
///
126126
/// ```
127-
/// # use fontique::Stretch;
128-
/// assert_eq!(Stretch::NORMAL.ratio(), 1.0);
127+
/// # use fontique::FontStretch;
128+
/// assert_eq!(FontStretch::NORMAL.ratio(), 1.0);
129129
/// ```
130130
pub fn ratio(self) -> f32 {
131131
self.0
@@ -140,21 +140,21 @@ impl Stretch {
140140

141141
/// Returns `true` if the stretch is [normal].
142142
///
143-
/// [normal]: Stretch::NORMAL
143+
/// [normal]: FontStretch::NORMAL
144144
pub fn is_normal(self) -> bool {
145145
self == Self::NORMAL
146146
}
147147

148148
/// Returns `true` if the stretch is condensed (less than [normal]).
149149
///
150-
/// [normal]: Stretch::NORMAL
150+
/// [normal]: FontStretch::NORMAL
151151
pub fn is_condensed(self) -> bool {
152152
self < Self::NORMAL
153153
}
154154

155155
/// Returns `true` if the stretch is expanded (greater than [normal]).
156156
///
157-
/// [normal]: Stretch::NORMAL
157+
/// [normal]: FontStretch::NORMAL
158158
pub fn is_expanded(self) -> bool {
159159
self > Self::NORMAL
160160
}
@@ -164,10 +164,10 @@ impl Stretch {
164164
/// # Examples
165165
///
166166
/// ```
167-
/// # use fontique::Stretch;
168-
/// assert_eq!(Stretch::parse("semi-condensed"), Some(Stretch::SEMI_CONDENSED));
169-
/// assert_eq!(Stretch::parse("80%"), Some(Stretch::from_percentage(80.0)));
170-
/// assert_eq!(Stretch::parse("wideload"), None);
167+
/// # use fontique::FontStretch;
168+
/// assert_eq!(FontStretch::parse("semi-condensed"), Some(FontStretch::SEMI_CONDENSED));
169+
/// assert_eq!(FontStretch::parse("80%"), Some(FontStretch::from_percentage(80.0)));
170+
/// assert_eq!(FontStretch::parse("wideload"), None);
171171
/// ```
172172
pub fn parse(s: &str) -> Option<Self> {
173173
let s = s.trim();
@@ -191,7 +191,7 @@ impl Stretch {
191191
}
192192
}
193193

194-
impl fmt::Display for Stretch {
194+
impl fmt::Display for FontStretch {
195195
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196196
let value = self.0 * 1000.0;
197197
if value.fract() == 0.0 {
@@ -216,15 +216,15 @@ impl fmt::Display for Stretch {
216216
}
217217
}
218218

219-
impl Default for Stretch {
219+
impl Default for FontStretch {
220220
fn default() -> Self {
221221
Self::NORMAL
222222
}
223223
}
224224

225225
/// Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
226226
///
227-
/// The default value is [`Weight::NORMAL`] or `400.0`.
227+
/// The default value is [`FontWeight::NORMAL`] or `400.0`.
228228
///
229229
/// In variable fonts, this can be controlled with the `wght` [axis].
230230
///
@@ -235,9 +235,9 @@ impl Default for Stretch {
235235
/// [axis]: crate::AxisInfo
236236
/// [`font-weight`]: https://www.w3.org/TR/css-fonts-4/#font-weight-prop
237237
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
238-
pub struct Weight(f32);
238+
pub struct FontWeight(f32);
239239

240-
impl Weight {
240+
impl FontWeight {
241241
/// Weight value of 100.
242242
pub const THIN: Self = Self(100.0);
243243

@@ -272,7 +272,7 @@ impl Weight {
272272
pub const EXTRA_BLACK: Self = Self(950.0);
273273
}
274274

275-
impl Weight {
275+
impl FontWeight {
276276
/// Creates a new weight attribute with the given value.
277277
pub fn new(weight: f32) -> Self {
278278
Self(weight)
@@ -288,11 +288,11 @@ impl Weight {
288288
/// # Examples
289289
///
290290
/// ```
291-
/// # use fontique::Weight;
292-
/// assert_eq!(Weight::parse("normal"), Some(Weight::NORMAL));
293-
/// assert_eq!(Weight::parse("bold"), Some(Weight::BOLD));
294-
/// assert_eq!(Weight::parse("850"), Some(Weight::new(850.0)));
295-
/// assert_eq!(Weight::parse("invalid"), None);
291+
/// # use fontique::FontWeight;
292+
/// assert_eq!(FontWeight::parse("normal"), Some(FontWeight::NORMAL));
293+
/// assert_eq!(FontWeight::parse("bold"), Some(FontWeight::BOLD));
294+
/// assert_eq!(FontWeight::parse("850"), Some(FontWeight::new(850.0)));
295+
/// assert_eq!(FontWeight::parse("invalid"), None);
296296
/// ```
297297
pub fn parse(s: &str) -> Option<Self> {
298298
let s = s.trim();
@@ -304,13 +304,13 @@ impl Weight {
304304
}
305305
}
306306

307-
impl Default for Weight {
307+
impl Default for FontWeight {
308308
fn default() -> Self {
309309
Self::NORMAL
310310
}
311311
}
312312

313-
impl fmt::Display for Weight {
313+
impl fmt::Display for FontWeight {
314314
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
315315
let value = self.0;
316316
if value.fract() == 0.0 {
@@ -335,7 +335,7 @@ impl fmt::Display for Weight {
335335

336336
/// Visual style or 'slope' of a font.
337337
///
338-
/// The default value is [`Style::Normal`].
338+
/// The default value is [`FontStyle::Normal`].
339339
///
340340
/// In variable fonts, this can be controlled with the `ital`
341341
/// and `slnt` [axes] for italic and oblique styles, respectively.
@@ -347,7 +347,7 @@ impl fmt::Display for Weight {
347347
/// [axes]: crate::AxisInfo
348348
/// [`font-style`]: https://www.w3.org/TR/css-fonts-4/#font-style-prop
349349
#[derive(Copy, Clone, PartialEq, Default, Debug)]
350-
pub enum Style {
350+
pub enum FontStyle {
351351
/// An upright or "roman" style.
352352
#[default]
353353
Normal,
@@ -359,7 +359,7 @@ pub enum Style {
359359
Oblique(Option<f32>),
360360
}
361361

362-
impl Style {
362+
impl FontStyle {
363363
/// Parses a font style from a CSS value.
364364
pub fn parse(mut s: &str) -> Option<Self> {
365365
s = s.trim();
@@ -399,7 +399,7 @@ impl Style {
399399
}
400400
}
401401

402-
impl fmt::Display for Style {
402+
impl fmt::Display for FontStyle {
403403
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
404404
let value = match self {
405405
Self::Normal => "normal",

fontique/src/backend/fontconfig/cache.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright 2024 the Parley Authors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4-
use super::{Stretch, Style, Weight};
4+
use super::{FontStretch, FontStyle, FontWeight};
55
use fontconfig_cache_parser::{Cache, CharSetLeaf, Object, Pattern, Value};
66
use std::io::Read;
77
use std::path::PathBuf;
88

9-
impl Stretch {
9+
impl FontStretch {
1010
/// Creates a new stretch attribute with the given value from Fontconfig.
1111
///
1212
/// The values are determined based on the [fonts.conf documentation].
@@ -28,7 +28,7 @@ impl Stretch {
2828
}
2929
}
3030

31-
impl Style {
31+
impl FontStyle {
3232
/// Creates a new style attribute with the given value from Fontconfig.
3333
///
3434
/// The values are determined based on the [fonts.conf documentation].
@@ -43,7 +43,7 @@ impl Style {
4343
}
4444
}
4545

46-
impl Weight {
46+
impl FontWeight {
4747
/// Creates a new weight attribute with the given value from Fontconfig.
4848
///
4949
/// The values are determined based on the [fonts.conf documentation].
@@ -79,7 +79,7 @@ impl Weight {
7979
return Self::new(ot_a + (ot_b - ot_a) * t);
8080
}
8181
}
82-
Weight::EXTRA_BLACK
82+
Self::EXTRA_BLACK
8383
}
8484
}
8585

@@ -88,9 +88,9 @@ pub struct CachedFont {
8888
pub family: Vec<String>,
8989
pub path: PathBuf,
9090
pub index: u32,
91-
pub stretch: Stretch,
92-
pub style: Style,
93-
pub weight: Weight,
91+
pub stretch: FontStretch,
92+
pub style: FontStyle,
93+
pub weight: FontWeight,
9494
pub coverage: Coverage,
9595
}
9696

@@ -100,9 +100,9 @@ impl CachedFont {
100100
self.path.clear();
101101
self.index = 0;
102102
self.coverage.clear();
103-
self.weight = Weight::default();
104-
self.style = Style::default();
105-
self.stretch = Stretch::default();
103+
self.weight = FontWeight::default();
104+
self.style = FontStyle::default();
105+
self.stretch = FontStretch::default();
106106
}
107107
}
108108

@@ -177,21 +177,21 @@ fn parse_font(
177177
Object::Slant => {
178178
for val in elt.values().ok()? {
179179
if let Value::Int(i) = val.ok()? {
180-
font.style = Style::from_fc(i as _);
180+
font.style = FontStyle::from_fc(i as _);
181181
}
182182
}
183183
}
184184
Object::Weight => {
185185
for val in elt.values().ok()? {
186186
if let Value::Int(i) = val.ok()? {
187-
font.weight = Weight::from_fc(i as _);
187+
font.weight = FontWeight::from_fc(i as _);
188188
}
189189
}
190190
}
191191
Object::Width => {
192192
for val in elt.values().ok()? {
193193
if let Value::Int(i) = val.ok()? {
194-
font.stretch = Stretch::from_fc(i as _);
194+
font.stretch = FontStretch::from_fc(i as _);
195195
}
196196
}
197197
}

fontique/src/backend/fontconfig/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77

88
use super::{
9-
super::{Stretch, Style, Weight},
9+
super::{FontStretch, FontStyle, FontWeight},
1010
FallbackKey, FamilyId, FamilyInfo, FamilyName, FamilyNameMap, FontInfo, GenericFamily,
1111
GenericFamilyMap, Script, SourceInfo, SourcePathMap,
1212
};
@@ -301,9 +301,9 @@ struct RawFamily {
301301
struct RawFont {
302302
source: SourceInfo,
303303
index: u32,
304-
stretch: Stretch,
305-
style: Style,
306-
weight: Weight,
304+
stretch: FontStretch,
305+
style: FontStyle,
306+
weight: FontWeight,
307307
coverage: cache::Coverage,
308308
}
309309

0 commit comments

Comments
 (0)