use f_office_drawingml::effect::{ShadowColor, ShadowOffset};
use f_office_paint::{FillRule, Group, GroupPlaces, Item, PathCommand, PathItem, Point, Turn};
use f_office_units::{Length, div_round_i128, saturated};
use crate::text::{DrawnShadow, Face, ShadowForm};
use crate::{Fonts, OutlineCommand};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct DevicePlace {
pub(super) glyph: u32,
pub(super) x: Length,
pub(super) y: Length,
}
pub(super) fn glyph_outlines(
(fonts, face, size, turn): (&dyn Fonts, &Face, Length, Option<Turn>),
(dx, dy): (Length, Length),
places: &[DevicePlace],
) -> Vec<PathCommand> {
let em = i128::from(face.metrics.units_per_em.max(1));
let size = i128::from(size.units());
let mut commands = Vec::new();
for place in places {
let Ok(outline) = fonts.outline(face.font, place.glyph) else {
continue;
};
let origin = (place.x + dx, place.y + dy);
let at = |point| at(point, origin, (size, em), turn);
commands.extend(outline.iter().map(|command| match *command {
OutlineCommand::MoveTo(p) => PathCommand::MoveTo(at(p)),
OutlineCommand::LineTo(p) => PathCommand::LineTo(at(p)),
OutlineCommand::QuadTo(c, p) => PathCommand::QuadTo(at(c), at(p)),
OutlineCommand::CubicTo(c, d, p) => PathCommand::CubicTo(at(c), at(d), at(p)),
OutlineCommand::Close => PathCommand::Close,
}));
}
commands
}
pub(super) fn item(
glyphs: (&dyn Fonts, &Face, Length, Option<Turn>),
shadow: DrawnShadow,
places: &[DevicePlace],
) -> Option<Item> {
let shift = match shadow.form {
ShadowForm::Paths => (shadow.across, shadow.down),
ShadowForm::Picture { .. } => (Length::ZERO, Length::ZERO),
};
let commands = glyph_outlines(glyphs, shift, places);
if commands.is_empty() {
return None;
}
match shadow.form {
ShadowForm::Paths => Some(Item::Group(Group {
items: vec![Item::Path(PathItem {
commands,
color: shadow.color,
fill_rule: FillRule::NonZero,
})],
clip: None,
places: GroupPlaces::Own,
})),
ShadowForm::Picture {
blur,
opacity,
outline_width,
} => super::super::shadow::stroked_picture(
&commands,
FillRule::NonZero,
outline_width,
ShadowOffset {
across: shadow.across,
down: shadow.down,
},
blur,
ShadowColor {
rgb: shadow.color,
opacity,
},
),
}
}
fn at(
[u, v]: [i32; 2],
(x, y): (Length, Length),
(size, em): (i128, i128),
turn: Option<Turn>,
) -> Point {
let (right, down) = (i128::from(u) * size, -i128::from(v) * size);
let (right, down, scale) = match turn {
None => (right, down, em),
Some(turn) => (
i128::from(turn.xx) * right + i128::from(turn.xy) * down,
i128::from(turn.yx) * right + i128::from(turn.yy) * down,
em * i128::from(Turn::SCALE),
),
};
let length = |value| Length::from_units(saturated(div_round_i128(value, scale)));
Point {
x: x + length(right),
y: y + length(down),
}
}
#[cfg(test)]
mod tests;