pub(crate) mod fill;
mod pass;
pub mod rng;
mod rough;
pub use pass::{
roughen_element, roughen_scene, roughen_scene_fit, roughen_scene_fit_if, roughen_scene_if,
};
pub use rng::Rng;
pub use crate::scene::{Fill, SketchFill, SketchFillStyle};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SketchKind {
Rect,
RoundedRect,
Circle,
Ellipse,
Line,
Polyline,
Polygon,
Arc,
Pie,
Path,
GradientPath,
}
impl SketchKind {
#[must_use]
pub fn of(el: &crate::scene::Element) -> Option<Self> {
use crate::scene::Element as E;
Some(match el {
E::Rect { .. } => Self::Rect,
E::RoundedRect { .. } => Self::RoundedRect,
E::Circle { .. } => Self::Circle,
E::Ellipse { .. } => Self::Ellipse,
E::Line { .. } => Self::Line,
E::Polyline { .. } => Self::Polyline,
E::Polygon { .. } => Self::Polygon,
E::Arc { .. } => Self::Arc,
E::Pie { .. } => Self::Pie,
E::Path { .. } => Self::Path,
E::GradientPath { .. } => Self::GradientPath,
E::Text { .. } | E::Image { .. } => return None,
})
}
const fn bit(self) -> u16 {
1 << (self as u16)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SketchKinds(u16);
impl SketchKinds {
pub const NONE: Self = Self(0);
pub const ALL: Self = Self(0b0000_0111_1111_1111);
#[must_use]
pub fn only(kinds: &[SketchKind]) -> Self {
let mut s = Self::NONE;
for k in kinds {
s = s.with(*k);
}
s
}
#[must_use]
pub fn with(self, kind: SketchKind) -> Self {
Self(self.0 | kind.bit())
}
#[must_use]
pub fn without(self, kind: SketchKind) -> Self {
Self(self.0 & !kind.bit())
}
#[must_use]
pub fn contains(self, kind: SketchKind) -> bool {
self.0 & kind.bit() != 0
}
#[must_use]
pub fn is_empty(self) -> bool {
self.0 == 0
}
}
impl Default for SketchKinds {
fn default() -> Self {
Self::ALL
}
}
#[derive(Debug, Clone)]
pub struct SketchOptions {
pub roughness: f64,
pub bowing: f64,
pub seed: Option<u64>,
pub multistroke: bool,
pub curve_step: f64,
pub kinds: SketchKinds,
pub outline_when_unstroked: bool,
pub outline_width: f64,
pub fill: Option<SketchFillStyle>,
pub min_fill_area: Option<f64>,
pub fill_angle: f64,
pub fill_gap: f64,
pub fill_weight: f64,
pub max_fill_strokes: usize,
}
impl Default for SketchOptions {
fn default() -> Self {
Self {
roughness: 0.7,
bowing: 0.5,
seed: None,
multistroke: true,
curve_step: 12.0,
kinds: SketchKinds::ALL,
outline_when_unstroked: true,
outline_width: 1.0,
fill: None,
min_fill_area: None,
fill_angle: -41.0,
fill_gap: 4.0,
fill_weight: 0.5,
max_fill_strokes: 2000,
}
}
}
impl SketchOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_roughness(mut self, roughness: f64) -> Self {
self.roughness = roughness.max(0.0);
self
}
#[must_use]
pub fn with_bowing(mut self, bowing: f64) -> Self {
self.bowing = bowing;
self
}
#[must_use]
pub fn with_seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}
#[must_use]
pub fn with_multistroke(mut self, multistroke: bool) -> Self {
self.multistroke = multistroke;
self
}
#[must_use]
pub fn with_curve_step(mut self, step: f64) -> Self {
self.curve_step = step.max(0.5);
self
}
#[must_use]
pub fn with_kinds(mut self, kinds: SketchKinds) -> Self {
self.kinds = kinds;
self
}
#[must_use]
pub fn with_outline_when_unstroked(mut self, on: bool) -> Self {
self.outline_when_unstroked = on;
self
}
#[must_use]
pub fn with_fill(mut self, style: SketchFillStyle) -> Self {
self.fill = Some(style);
self
}
#[must_use]
pub fn with_min_fill_area(mut self, area: f64) -> Self {
self.min_fill_area = Some(area.max(0.0));
self
}
#[must_use]
pub fn with_fill_angle(mut self, angle: f64) -> Self {
self.fill_angle = angle;
self
}
#[must_use]
pub fn with_fill_gap(mut self, gap: f64) -> Self {
self.fill_gap = gap;
self
}
#[must_use]
pub fn with_fill_weight(mut self, weight: f64) -> Self {
self.fill_weight = weight;
self
}
#[must_use]
pub fn with_max_fill_strokes(mut self, max: usize) -> Self {
self.max_fill_strokes = max.max(1);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometry::{Color, Point, Rect};
use crate::render::SvgRenderer;
use crate::scene::{Element, FillStrokeStyle, Scene};
#[test]
fn defaults_are_stable() {
let o = SketchOptions::default();
assert_eq!(o.roughness, 0.7);
assert_eq!(o.bowing, 0.5);
assert!(o.multistroke);
assert!(o.fill.is_none(), "默认不改动填充");
assert_eq!(o.kinds, SketchKinds::ALL);
assert!(o.seed.is_none());
}
#[test]
fn kinds_bitmask_roundtrips() {
let k = SketchKinds::NONE
.with(SketchKind::Rect)
.with(SketchKind::Circle)
.without(SketchKind::Rect);
assert!(!k.contains(SketchKind::Rect));
assert!(k.contains(SketchKind::Circle));
assert!(!k.is_empty());
assert!(SketchKinds::NONE.is_empty());
assert_eq!(
SketchKinds::only(&[SketchKind::Pie]),
SketchKinds::NONE.with(SketchKind::Pie)
);
}
#[test]
fn text_and_image_have_no_kind() {
let text = Element::text(
"x",
Point::new(0.0, 0.0),
crate::text::TextStyle::new(Color::BLACK, 12.0, "sans-serif"),
);
assert_eq!(SketchKind::of(&text), None);
}
#[test]
fn untouched_scene_renders_identically() {
let build = || {
let mut s = Scene::new(120.0, 80.0);
s.push(Element::rect(
Rect::new(10.0, 10.0, 100.0, 60.0),
FillStrokeStyle {
fill: Some(Fill::Solid(Color::rgb(0xee, 0xee, 0xff))),
stroke: Some(crate::scene::Stroke::new(Color::BLACK, 1.5)),
},
));
s
};
let mut a = build();
let b = build();
roughen_scene(&mut a, &SketchOptions::new().with_kinds(SketchKinds::NONE));
let ra = {
let mut r = SvgRenderer::new(120.0, 80.0);
crate::render::Renderer::render_scene(&mut r, &a);
r.into_string()
};
let rb = {
let mut r = SvgRenderer::new(120.0, 80.0);
crate::render::Renderer::render_scene(&mut r, &b);
r.into_string()
};
assert_eq!(ra, rb);
}
}