Make glyph rendering an optional feature (#1060)
Unfortunately I'm stuck because just enabling the "std" feature will
also enable the "skrifa/std" feature, and because of this skrifa will be
pulled in as a dependency even though the text feature is disabled. I'm
not sure how to best solve this without adding two
`skrifa-std`/`skrifa-libm` features that need to be enabled manually by
the user. Suggestions are welcome.
The main motivation is that it would allow using the renderer in
contexts where text rendering is already implemented with a different
method, without having to pull in skrifa as a dependency (e.g. resvg).
diff --git a/sparse_strips/vello_common/Cargo.toml b/sparse_strips/vello_common/Cargo.toml
index 9ba10bb..08bf3dc 100644
--- a/sparse_strips/vello_common/Cargo.toml
+++ b/sparse_strips/vello_common/Cargo.toml
@@ -22,22 +22,24 @@
fearless_simd = { workspace = true }
png = { workspace = true, optional = true }
roxmltree = { version = "0.20.0", optional = true }
-skrifa = { workspace = true }
+skrifa = { workspace = true, optional = true }
smallvec = { workspace = true }
libm = { version = "0.2.15", optional = true }
[features]
-default = ["std", "png"]
+default = ["std", "png", "text"]
# Enable using SIMD instructions for rendering
simd = []
# Get floating point functions from the standard library (likely using your target’s libc).
-std = ["peniko/std", "skrifa/std", "fearless_simd/std"]
+std = ["peniko/std", "skrifa?/std", "fearless_simd/std"]
# Use floating point implementations from libm.
-libm = ["peniko/libm", "skrifa/libm", "dep:libm", "fearless_simd/libm"]
+libm = ["peniko/libm", "skrifa?/libm", "dep:libm", "fearless_simd/libm"]
# Allow loading Pixmap from PNG, and drawing png glyphs.
png = ["std", "dep:png"]
# Enable multi-threaded rendering.
multithreading = []
+# Add support for text rendering
+text = ["dep:skrifa"]
# Development only features
diff --git a/sparse_strips/vello_common/src/lib.rs b/sparse_strips/vello_common/src/lib.rs
index 4ea612b..feed2d4 100644
--- a/sparse_strips/vello_common/src/lib.rs
+++ b/sparse_strips/vello_common/src/lib.rs
@@ -61,10 +61,12 @@
pub mod blurred_rounded_rect;
pub mod coarse;
+#[cfg(feature = "text")]
pub mod colr;
pub mod encode;
pub mod execute;
pub mod flatten;
+#[cfg(feature = "text")]
pub mod glyph;
pub mod mask;
pub mod math;
diff --git a/sparse_strips/vello_cpu/Cargo.toml b/sparse_strips/vello_cpu/Cargo.toml
index 9dbb4bf..93c7608 100644
--- a/sparse_strips/vello_cpu/Cargo.toml
+++ b/sparse_strips/vello_cpu/Cargo.toml
@@ -26,7 +26,7 @@
[features]
-default = ["std", "png"]
+default = ["std", "png", "text"]
# Get floating point functions from the standard library (likely using your target’s libc).
std = ["vello_common/std"]
# Use floating point implementations from libm.
@@ -42,6 +42,8 @@
"dep:crossbeam-channel",
"vello_common/multithreading",
]
+# Add support for text rendering
+text = ["vello_common/text"]
[lints]
workspace = true
diff --git a/sparse_strips/vello_cpu/src/lib.rs b/sparse_strips/vello_cpu/src/lib.rs
index 5b0740e..9c2ab50 100644
--- a/sparse_strips/vello_cpu/src/lib.rs
+++ b/sparse_strips/vello_cpu/src/lib.rs
@@ -124,6 +124,7 @@
pub use render::{RenderContext, RenderSettings};
pub use vello_common::fearless_simd::Level;
+#[cfg(feature = "text")]
pub use vello_common::glyph::Glyph;
pub use vello_common::mask::Mask;
pub use vello_common::paint::{Image, Paint, PaintType};
diff --git a/sparse_strips/vello_cpu/src/render.rs b/sparse_strips/vello_cpu/src/render.rs
index cb372ad..7031617 100644
--- a/sparse_strips/vello_cpu/src/render.rs
+++ b/sparse_strips/vello_cpu/src/render.rs
@@ -10,24 +10,27 @@
use crate::dispatch::single_threaded::SingleThreadedDispatcher;
use crate::kurbo::{PathEl, Point};
use alloc::boxed::Box;
+#[cfg(feature = "text")]
use alloc::sync::Arc;
use alloc::vec;
use alloc::vec::Vec;
use vello_common::blurred_rounded_rect::BlurredRoundedRectangle;
-use vello_common::color::{AlphaColor, Srgb};
-use vello_common::colr::{ColrPainter, ColrRenderer};
use vello_common::encode::{EncodeExt, EncodedPaint};
use vello_common::fearless_simd::Level;
-use vello_common::glyph::{GlyphRenderer, GlyphRunBuilder, GlyphType, PreparedGlyph};
use vello_common::kurbo::{Affine, BezPath, Cap, Join, Rect, Shape, Stroke};
use vello_common::mask::Mask;
-use vello_common::paint::{Image, Paint, PaintType};
-use vello_common::peniko;
+use vello_common::paint::{Paint, PaintType};
use vello_common::peniko::color::palette::css::BLACK;
-use vello_common::peniko::{BlendMode, Compose, Fill, Gradient, Mix};
-use vello_common::peniko::{Font, ImageQuality};
+use vello_common::peniko::{BlendMode, Compose, Fill, Mix};
use vello_common::pixmap::Pixmap;
+#[cfg(feature = "text")]
+use vello_common::{
+ color::{AlphaColor, Srgb},
+ colr::{ColrPainter, ColrRenderer},
+ glyph::{GlyphRenderer, GlyphRunBuilder, GlyphType, PreparedGlyph},
+};
+
pub(crate) const DEFAULT_TOLERANCE: f64 = 0.1;
/// A render context.
#[derive(Debug)]
@@ -41,6 +44,10 @@
pub(crate) fill_rule: Fill,
pub(crate) temp_path: BezPath,
pub(crate) encoded_paints: Vec<EncodedPaint>,
+ #[cfg_attr(
+ not(feature = "text"),
+ allow(dead_code, reason = "used when the `text` feature is enabled")
+ )]
pub(crate) level: Level,
dispatcher: Box<dyn Dispatcher>,
}
@@ -224,7 +231,8 @@
}
/// Creates a builder for drawing a run of glyphs that have the same attributes.
- pub fn glyph_run(&mut self, font: &Font) -> GlyphRunBuilder<'_, Self> {
+ #[cfg(feature = "text")]
+ pub fn glyph_run(&mut self, font: &crate::peniko::Font) -> GlyphRunBuilder<'_, Self> {
GlyphRunBuilder::new(font.clone(), self.transform, self)
}
@@ -389,6 +397,7 @@
}
}
+#[cfg(feature = "text")]
impl GlyphRenderer for RenderContext {
fn fill_glyph(&mut self, prepared_glyph: PreparedGlyph<'_>) {
match prepared_glyph.glyph_type {
@@ -412,15 +421,15 @@
let quality = if prepared_glyph.transform.as_coeffs()[0] < 0.5
|| prepared_glyph.transform.as_coeffs()[3] < 0.5
{
- ImageQuality::High
+ crate::peniko::ImageQuality::High
} else {
- ImageQuality::Medium
+ crate::peniko::ImageQuality::Medium
};
- let image = Image {
+ let image = vello_common::paint::Image {
pixmap: Arc::new(glyph.pixmap),
- x_extend: peniko::Extend::Pad,
- y_extend: peniko::Extend::Pad,
+ x_extend: crate::peniko::Extend::Pad,
+ y_extend: crate::peniko::Extend::Pad,
quality,
};
@@ -463,13 +472,13 @@
pix
};
- let image = Image {
+ let image = vello_common::paint::Image {
pixmap: Arc::new(glyph_pixmap),
- x_extend: peniko::Extend::Pad,
- y_extend: peniko::Extend::Pad,
+ x_extend: crate::peniko::Extend::Pad,
+ y_extend: crate::peniko::Extend::Pad,
// Since the pixmap will already have the correct size, no need to
// use a different image quality here.
- quality: ImageQuality::Low,
+ quality: crate::peniko::ImageQuality::Low,
};
self.set_paint(image);
@@ -503,6 +512,7 @@
}
}
+#[cfg(feature = "text")]
impl ColrRenderer for RenderContext {
fn push_clip_layer(&mut self, clip: &BezPath) {
Self::push_clip_layer(self, clip);
@@ -522,7 +532,7 @@
));
}
- fn fill_gradient(&mut self, gradient: Gradient) {
+ fn fill_gradient(&mut self, gradient: crate::peniko::Gradient) {
self.set_paint(gradient);
self.fill_rect(&Rect::new(
0.0,
diff --git a/sparse_strips/vello_hybrid/Cargo.toml b/sparse_strips/vello_hybrid/Cargo.toml
index 65ce9dc..ff0f41e 100644
--- a/sparse_strips/vello_hybrid/Cargo.toml
+++ b/sparse_strips/vello_hybrid/Cargo.toml
@@ -17,7 +17,7 @@
[dependencies]
bytemuck = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
-vello_common = { workspace = true, features = ["std"] }
+vello_common = { workspace = true, features = ["std", "text"] }
wgpu = { workspace = true, optional = true }
vello_sparse_shaders = { workspace = true, optional = true }
log = { workspace = true }