use f_office_paint::{
Group, GroupClip, GroupPlaces, Item, PathCommand, PathItem, Point, Rect, Rgb,
};
use f_office_units::{Edges, Length, div_round};
use crate::art_pictures::{
ArtItem, ArtPicture, ArtPictures, ArtRect, SidePicture, UNIT, UnitCommand, UnitPoint,
};
use crate::body::ArtSide;
use crate::grid::{Device, Rounding};
use crate::params::{ART_PICTURE_EXTRA_DOTS, ART_STROKE_UNIT};
use crate::props::ArtStyle;
pub(super) fn corner_dots(size: u32, device: &Device) -> i64 {
Length::from_points(i64::from(size)).map_or(0, |side| device.count(side, Rounding::HalfUp))
}
#[derive(Clone, Copy)]
struct PartBox {
left: Length,
top: Length,
right: Length,
bottom: Length,
}
type DrawnSide<'a> = Option<(ArtSide, &'a ArtPicture, Length)>;
struct SideRun<'a> {
side: DrawnSide<'a>,
own: fn(&ArtPicture) -> &SidePicture,
across: bool,
along: (Length, Length),
outer: Length,
after: bool,
}
struct CornerRun<'a> {
side: DrawnSide<'a>,
shapes: fn(&ArtPicture) -> &[ArtItem],
at: (Length, Length),
before: (bool, bool),
}
pub(super) fn art_items(
sides: &Edges<Option<ArtSide>>,
edges: Edges<Length>,
device: &Device,
pictures: &ArtPictures,
items: &mut Vec<Item>,
) -> Vec<(ArtStyle, u32)> {
let mut missing = Vec::new();
let mut found = |side: Option<ArtSide>| {
let art = side?;
let picture = pictures.picture(art.style, art.size);
if picture.is_none() && !missing.contains(&(art.style, art.size)) {
missing.push((art.style, art.size));
}
Some((art, picture?, device.dots(corner_dots(art.size, device))))
};
let drawn = Edges {
left: found(sides.left),
top: found(sides.top),
right: found(sides.right),
bottom: found(sides.bottom),
};
let mut border = Vec::new();
for run in side_runs(drawn, edges) {
run.draw(device, &mut border);
}
for corner in corner_runs(drawn, edges) {
corner.draw(&mut border);
}
if !border.is_empty() {
items.push(Item::Group(Group {
items: border,
clip: None,
places: GroupPlaces::Points,
}));
}
missing
}
fn side_runs(drawn: Edges<DrawnSide<'_>>, edges: Edges<Length>) -> [SideRun<'_>; 4] {
let Edges {
left,
top,
right,
bottom,
} = edges;
[
SideRun {
side: drawn.top,
own: |p| &p.sides.top,
across: true,
along: (left, right),
outer: top,
after: true,
},
SideRun {
side: drawn.bottom,
own: |p| &p.sides.bottom,
across: true,
along: (left, right),
outer: bottom,
after: false,
},
SideRun {
side: drawn.left,
own: |p| &p.sides.left,
across: false,
along: (top, bottom),
outer: left,
after: true,
},
SideRun {
side: drawn.right,
own: |p| &p.sides.right,
across: false,
along: (top, bottom),
outer: right,
after: false,
},
]
}
fn corner_runs(drawn: Edges<DrawnSide<'_>>, edges: Edges<Length>) -> [CornerRun<'_>; 4] {
let Edges {
left,
top,
right,
bottom,
} = edges;
[
CornerRun {
side: drawn.top.or(drawn.left),
shapes: |p| &p.corners.top_left,
at: (left, top),
before: (false, false),
},
CornerRun {
side: drawn.top.or(drawn.right),
shapes: |p| &p.corners.top_right,
at: (right, top),
before: (true, false),
},
CornerRun {
side: drawn.bottom.or(drawn.left),
shapes: |p| &p.corners.bottom_left,
at: (left, bottom),
before: (false, true),
},
CornerRun {
side: drawn.bottom.or(drawn.right),
shapes: |p| &p.corners.bottom_right,
at: (right, bottom),
before: (true, true),
},
]
}
impl SideRun<'_> {
fn draw(self, device: &Device, items: &mut Vec<Item>) {
let Some((art, picture, corner)) = self.side else {
return;
};
let band = if self.after {
(self.outer, self.outer + corner)
} else {
(self.outer - corner, self.outer)
};
let (first, last) = (self.along.0 + corner, self.along.1 - corner);
let part = |along: (Length, Length)| PartBox::of(self.across, along, band);
let color = art.color.rgb();
let own = (self.own)(picture);
for along in squares(first, last, corner, device) {
draw(&own.repeat, part(along), color, items);
}
draw(&own.span, part((first, last)), color, items);
}
}
impl CornerRun<'_> {
fn draw(self, items: &mut Vec<Item>) {
let Some((art, picture, corner)) = self.side else {
return;
};
let span = |at: Length, before: bool| {
if before {
(at - corner, at)
} else {
(at, at + corner)
}
};
let square = PartBox::of(
true,
span(self.at.0, self.before.0),
span(self.at.1, self.before.1),
);
draw((self.shapes)(picture), square, art.color.rgb(), items);
}
}
impl PartBox {
const fn of(across: bool, along: (Length, Length), band: (Length, Length)) -> Self {
if across {
Self {
left: along.0,
top: band.0,
right: along.1,
bottom: band.1,
}
} else {
Self {
left: band.0,
top: along.0,
right: band.1,
bottom: along.1,
}
}
}
}
fn squares(first: Length, last: Length, corner: Length, device: &Device) -> Vec<(Length, Length)> {
let length = device.count(last - first, Rounding::HalfUp);
let step = device.count(corner, Rounding::HalfUp) + ART_PICTURE_EXTRA_DOTS;
let count = if step > 0 { length / step } else { 0 };
if count <= 0 {
return Vec::new();
}
let base = length / count;
let longer = length - count * base;
let offset = |index: i64| index * base + (index - (count - longer)).max(0);
(0..count)
.map(|index| {
(
first + device.dots(offset(index)),
first + device.dots(offset(index + 1)),
)
})
.collect()
}
fn draw(picture: &[ArtItem], part: PartBox, color: Option<Rgb>, items: &mut Vec<Item>) {
let place = |point: UnitPoint| Point {
x: share(part.left, part.right, point.x),
y: share(part.top, part.bottom, point.y),
};
let outline = |commands: &[UnitCommand]| -> Vec<PathCommand> {
commands
.iter()
.map(|command| match *command {
UnitCommand::Move(to) => PathCommand::MoveTo(place(to)),
UnitCommand::Line(to) => PathCommand::LineTo(place(to)),
UnitCommand::Quad(control, to) => PathCommand::QuadTo(place(control), place(to)),
UnitCommand::Cubic(first, second, to) => {
PathCommand::CubicTo(place(first), place(second), place(to))
}
UnitCommand::Close => PathCommand::Close,
})
.collect()
};
let fill = |own: Rgb| match color {
Some(side) if own == Rgb::BLACK => side,
_ => own,
};
let mut open: Vec<(GroupClip, Vec<Item>)> = Vec::new();
for item in picture {
match item {
ArtItem::Rect(rect) => {
let (top_left, bottom_right) = (place(rect.top_left), place(rect.bottom_right));
let drawn = Rect {
x: top_left.x,
y: top_left.y,
width: bottom_right.x - top_left.x,
height: bottom_right.y - top_left.y,
color: fill(rect.color),
};
push_stroked(held(&mut open, items), drawn, stroke_width(rect));
}
ArtItem::Shape(shape) => {
let fill = fill(shape.color);
let path = Item::Path(PathItem {
commands: outline(&shape.commands),
color: fill,
fill_rule: shape.fill_rule,
});
held(&mut open, items).push(path);
}
ArtItem::Clip(clip) => open.push((
GroupClip {
commands: outline(&clip.commands),
fill_rule: clip.fill_rule,
},
Vec::new(),
)),
ArtItem::End => end_clip(&mut open, items),
}
}
while !open.is_empty() {
end_clip(&mut open, items);
}
}
fn stroke_width(rect: &ArtRect) -> Length {
Length::from_units(div_round(
i64::from(rect.stroke) * Length::UNITS_PER_POINT,
i64::from(ART_STROKE_UNIT),
))
}
fn push_stroked(items: &mut Vec<Item>, rect: Rect, width: Length) {
let places = GroupPlaces::Stroked { width };
if let Some(Item::Group(group)) = items.last_mut()
&& group.places == places
&& group.clip.is_none()
{
group.items.push(Item::Rect(rect));
return;
}
items.push(Item::Group(Group {
items: vec![Item::Rect(rect)],
clip: None,
places,
}));
}
fn held<'a>(open: &'a mut [(GroupClip, Vec<Item>)], items: &'a mut Vec<Item>) -> &'a mut Vec<Item> {
match open.last_mut() {
Some((_, inside)) => inside,
None => items,
}
}
fn end_clip(open: &mut Vec<(GroupClip, Vec<Item>)>, items: &mut Vec<Item>) {
if let Some((clip, inside)) = open.pop() {
held(open, items).push(Item::Group(Group {
items: inside,
clip: Some(clip),
places: GroupPlaces::Points,
}));
}
}
fn share(from: Length, to: Length, millionths: i32) -> Length {
let span = i128::from((to - from).units()) * i128::from(millionths);
let unit = i128::from(UNIT);
let moved = (span * 2 + unit).div_euclid(unit * 2);
from + Length::from_units(i64::try_from(moved).unwrap_or(0))
}