This commit is contained in:
bflatastic committed 2026-11-19 00:18:55 +07:00
commit deaf8848e4
4 files changed
+47 -8

No files matched your search

+7 -1
View File
@@ -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
}
@@ -507,6 +507,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 };
let innermost = at + 1 == self.groups.len();
let before = self.groups.last().map_or(0, |g| g.children.len());
@@ -523,6 +526,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 path = self.layer_outline(glyph_id);
let brush_xf = self.transform() * brush_transform.as_ref().map_or(Affine::IDENTITY, affine);
self.transforms.push(brush_xf);
+17 -6
View File
@@ -7,14 +7,20 @@ pub(super) fn svg_drawn(face: &FontFace, font: &skrifa::FontRef<'_>, gid: u32) -
face.svg_glyphs && !crate::colr::has_colour_glyph(font, gid)
}
/// The most path elements one glyph outline keeps. Real outlines have hundreds; a variable
/// composite glyph can repeat a big base outline into millions. Elements past it are dropped.
/// 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.
/// 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
}
@@ -77,12 +83,17 @@ mod tests {
use super::*;
#[test]
fn a_pen_keeps_at_most_the_element_cap() {
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, 0.0);
pen.line_to(i as f32, 1.0);
}
assert_eq!(pen.0.elements().len(), MAX_OUTLINE_ELEMENTS);
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);
}
}
+1 -1
View File
@@ -656,7 +656,7 @@ impl FontDb {
// As before, keep the prefix emitted before a malformed glyph reports an error.
let _ = font.draw_to(gid, &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();
+22
View File
@@ -714,3 +714,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");
}