mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-20 09:23:10 +01:00
Text: stop COLR outline work past the budget and cap one outline's path elements
This commit is contained in:
1 parent
9fc43f2a35
commit
81870d2989
4 files changed
+85
-4
No files matched your search
+10
-2
@@ -114,7 +114,7 @@ fn outline(font: &skrifa::FontRef<'_>, location: LocationRef<'_>, gid: GlyphId)
|
||||
if let Some(g) = font.outline_glyphs().get(gid) {
|
||||
let _ = g.draw(DrawSettings::unhinted(Size::unscaled(), location), &mut pen);
|
||||
}
|
||||
let mut path = pen.0;
|
||||
let mut path = pen.finish();
|
||||
path.apply_affine(FLIP);
|
||||
path
|
||||
}
|
||||
@@ -359,7 +359,9 @@ impl ColorPainter for Tree<'_> {
|
||||
}
|
||||
|
||||
fn push_clip_glyph(&mut self, glyph_id: GlyphId) {
|
||||
let mut clip = outline(self.font, self.location, glyph_id);
|
||||
// Past the budget the glyph is drawn as its outline anyway: later layers are not drawn,
|
||||
// only opened and closed so the callbacks stay balanced.
|
||||
let mut clip = if self.too_large { BezPath::new() } else { outline(self.font, self.location, glyph_id) };
|
||||
clip.apply_affine(self.transform());
|
||||
self.charge(&clip);
|
||||
self.open(Open { clip: Some(clip), blend: None, children: vec![], flooded: false });
|
||||
@@ -381,6 +383,9 @@ impl ColorPainter for Tree<'_> {
|
||||
self.unsupported.get_or_insert("a fill outside any glyph");
|
||||
return;
|
||||
};
|
||||
if self.too_large {
|
||||
return;
|
||||
}
|
||||
let Some(clip) = self.groups.get(at).and_then(|g| g.clip.clone()) else { return };
|
||||
if at + 1 == self.groups.len()
|
||||
&& let Some(g) = self.groups.last_mut()
|
||||
@@ -392,6 +397,9 @@ impl ColorPainter for Tree<'_> {
|
||||
|
||||
/// The glyph's outline filled, the brush moved by `brush_transform` (the glyph stays).
|
||||
fn fill_glyph(&mut self, glyph_id: GlyphId, brush_transform: Option<Transform>, brush: Brush<'_>) {
|
||||
if self.too_large {
|
||||
return;
|
||||
}
|
||||
let mut path = outline(self.font, self.location, glyph_id);
|
||||
path.apply_affine(self.transform());
|
||||
let brush_xf = self.transform() * brush_transform.as_ref().map_or(Affine::IDENTITY, affine);
|
||||
|
||||
@@ -7,23 +7,74 @@ pub(super) fn svg_drawn(face: &FontFace, font: &skrifa::FontRef<'_>, gid: u32) -
|
||||
face.svg_glyphs && !crate::colr::has_colour_glyph(font, gid)
|
||||
}
|
||||
|
||||
/// A pen drawing outlines y down, as text space has them.
|
||||
/// Most path elements one glyph outline may have: a glyph that needs more (a variable font's
|
||||
/// composite glyph repeating a large component many times) is drawn as nothing, not kept.
|
||||
pub(crate) const MAX_OUTLINE_ELEMENTS: usize = 1 << 20;
|
||||
|
||||
/// A pen drawing outlines y down, as text space has them. It stops taking segments once the path
|
||||
/// holds [`MAX_OUTLINE_ELEMENTS`], and [`Self::finish`] then gives an empty path.
|
||||
pub(crate) struct FlipPen(pub(crate) BezPath);
|
||||
|
||||
impl FlipPen {
|
||||
/// The outline drawn, or an empty path when it passed [`MAX_OUTLINE_ELEMENTS`].
|
||||
pub(crate) fn finish(self) -> BezPath {
|
||||
if self.0.elements().len() >= MAX_OUTLINE_ELEMENTS { BezPath::new() } else { self.0 }
|
||||
}
|
||||
|
||||
fn full(&self) -> bool {
|
||||
self.0.elements().len() >= MAX_OUTLINE_ELEMENTS
|
||||
}
|
||||
}
|
||||
|
||||
impl OutlinePen for FlipPen {
|
||||
fn move_to(&mut self, x: f32, y: f32) {
|
||||
if self.full() {
|
||||
return;
|
||||
}
|
||||
self.0.move_to((x as f64, -y as f64));
|
||||
}
|
||||
fn line_to(&mut self, x: f32, y: f32) {
|
||||
if self.full() {
|
||||
return;
|
||||
}
|
||||
self.0.line_to((x as f64, -y as f64));
|
||||
}
|
||||
fn quad_to(&mut self, cx0: f32, cy0: f32, x: f32, y: f32) {
|
||||
if self.full() {
|
||||
return;
|
||||
}
|
||||
self.0.quad_to((cx0 as f64, -cy0 as f64), (x as f64, -y as f64));
|
||||
}
|
||||
fn curve_to(&mut self, cx0: f32, cy0: f32, cx1: f32, cy1: f32, x: f32, y: f32) {
|
||||
if self.full() {
|
||||
return;
|
||||
}
|
||||
self.0.curve_to((cx0 as f64, -cy0 as f64), (cx1 as f64, -cy1 as f64), (x as f64, -y as f64));
|
||||
}
|
||||
fn close(&mut self) {
|
||||
if self.full() {
|
||||
return;
|
||||
}
|
||||
self.0.close_path();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_pen_past_the_cap_gives_nothing() {
|
||||
let mut pen = FlipPen(BezPath::new());
|
||||
pen.move_to(0.0, 0.0);
|
||||
for i in 0..MAX_OUTLINE_ELEMENTS + 10 {
|
||||
pen.line_to(i as f32, 1.0);
|
||||
}
|
||||
assert_eq!(pen.0.elements().len(), MAX_OUTLINE_ELEMENTS, "it stops taking segments");
|
||||
assert!(pen.finish().elements().is_empty());
|
||||
let mut small = FlipPen(BezPath::new());
|
||||
small.move_to(0.0, 0.0);
|
||||
small.line_to(1.0, 1.0);
|
||||
assert_eq!(small.finish().elements().len(), 2);
|
||||
}
|
||||
}
|
||||
@@ -535,7 +535,7 @@ impl FontDb {
|
||||
{
|
||||
let _ = g.draw(DrawSettings::unhinted(Size::unscaled(), face.location()), &mut pen);
|
||||
}
|
||||
let p = Arc::new(pen.0);
|
||||
let p = Arc::new(pen.finish());
|
||||
let mut cache = self.outlines.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if cache.len() >= OUTLINE_CACHE_MAX {
|
||||
cache.clear();
|
||||
|
||||
@@ -411,3 +411,25 @@ fn a_colour_glyph_on_a_path_turns_with_it() {
|
||||
assert!((b.width() - upright.height()).abs() < 1.0 && (b.height() - upright.width()).abs() < 1.0, "on its side: {b:?}");
|
||||
assert!(b.y0 >= -1.0 && b.y1 <= 400.0, "along the path: {b:?}");
|
||||
}
|
||||
|
||||
/// A layer list that reuses one run of layers many times grows the tree past its node cap: the
|
||||
/// glyph is then no colour glyph (it draws its outline), and the layers after the cap are not drawn.
|
||||
#[test]
|
||||
fn layers_repeating_a_run_of_layers_pass_the_cap() {
|
||||
use write_fonts::tables::colr::{BaseGlyphList, BaseGlyphPaint, Colr, LayerList};
|
||||
use write_fonts::types::GlyphId16;
|
||||
let bytes = colr_font(false).unwrap();
|
||||
let o = skrifa::FontRef::new(&bytes).unwrap().charmap().map(COLOUR_CHAR).unwrap().to_u32();
|
||||
let o = GlyphId16::new(u16::try_from(o).unwrap());
|
||||
let font = |runs: u8| {
|
||||
// 255 glyph layers, then `runs` layers that each repeat all of them.
|
||||
let mut layers: Vec<ColrPaint> = (0..255).map(|_| ColrPaint::glyph(solid(0), o)).collect();
|
||||
layers.extend((0..runs).map(|_| ColrPaint::colr_layers(255, 0)));
|
||||
let mut colr = Colr::new(0, None, None, 0);
|
||||
colr.base_glyph_list = Some(BaseGlyphList::new(1, vec![BaseGlyphPaint::new(o, ColrPaint::colr_layers(runs, 255))])).into();
|
||||
colr.layer_list = Some(LayerList::new(layers.len() as u32, layers)).into();
|
||||
crate::test_fonts::colr_font_with(&format!("Colrtest Runs {runs}"), &colr).unwrap()
|
||||
};
|
||||
assert!(matches!(content(&font(2), COLOUR_CHAR), Some(ColourContent::Art(_))), "510 layers fit");
|
||||
assert!(content(&font(100), COLOUR_CHAR).is_none(), "25,500 layers do not");
|
||||
}
|
||||
Reference in new issue
Block a user