Skip to content

Commit 49cd65d

Browse files
Add FeatureSpec: typed OpenType feature selection
Introduce `text_primitives::FeatureSpec` (ligatures/caps/numeric) as a type-safe way to build OpenType `FontFeature` settings without parsing CSS text, with deterministic output and unit/doctests. Re-export the typed API from Parley and add ergonomic `From<FeatureSpec>` for `FontFeatures` so Parley callers can use it directly.
1 parent 7508192 commit 49cd65d

File tree

5 files changed

+577
-1
lines changed

5 files changed

+577
-1
lines changed

parley/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ pub use layout::Layout;
134134

135135
pub use editing::*;
136136
pub use layout::*;
137+
pub use setting::{
138+
Caps, FeatureSpec, Ligatures, Numeric, NumericFigure, NumericFraction, NumericSpacing, TriState,
139+
};
137140
pub use style::*;
138141

139142
#[deprecated(

parley/src/setting.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33

44
//! OpenType settings (features and variations).
55
6-
pub use text_primitives::{FontFeature, FontVariation, Tag};
6+
pub use text_primitives::{
7+
Caps, FeatureSpec, FontFeature, FontVariation, Ligatures, Numeric, NumericFigure,
8+
NumericFraction, NumericSpacing, Tag, TriState,
9+
};

parley/src/style/font.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2021 the Parley Authors
22
// SPDX-License-Identifier: Apache-2.0 OR MIT
33

4+
use crate::FeatureSpec;
45
use alloc::borrow::Cow;
56

67
pub use crate::setting::{FontFeature, FontVariation};
@@ -42,6 +43,22 @@ impl<'a, const N: usize> From<&'a [FontVariation; N]> for FontVariations<'a> {
4243
}
4344

4445
/// Font feature settings that can be supplied as a raw source string or a parsed slice.
46+
///
47+
/// # Typed construction (recommended)
48+
///
49+
/// When you want a type-safe API (rather than parsing CSS text), you can use
50+
/// [`FeatureSpec`] to build a list of low-level [`FontFeature`] settings and
51+
/// then pass them into Parley:
52+
///
53+
/// ```
54+
/// use parley::{Caps, FeatureSpec, FontFeatures};
55+
///
56+
/// let features = FontFeatures::from(FeatureSpec::new().with_caps(Caps::SmallCaps));
57+
///
58+
/// // Now `features` can be used anywhere Parley accepts font features (for example, a style
59+
/// // builder's `push_default` / `push` methods).
60+
/// let _ = features;
61+
/// ```
4562
#[derive(Clone, PartialEq, Debug)]
4663
pub enum FontFeatures<'a> {
4764
/// Setting source in CSS format.
@@ -74,3 +91,9 @@ impl<'a, const N: usize> From<&'a [FontFeature; N]> for FontFeatures<'a> {
7491
Self::List(Cow::Borrowed(&value[..]))
7592
}
7693
}
94+
95+
impl<'a> From<FeatureSpec> for FontFeatures<'a> {
96+
fn from(value: FeatureSpec) -> Self {
97+
Self::List(Cow::Owned(value.to_features()))
98+
}
99+
}

0 commit comments

Comments
 (0)