use f_office_drawingml::effect::ShadowOffset;
use f_office_paint::{Opacity, Outline, Rgb};
use f_office_units::scale::{DRAWING_WHOLE, PERCENT};
use f_office_units::{Length, div_round_i128, saturated};
use super::face::{DrawnGlow, DrawnReflection, DrawnShadow, ShadowForm, ShadowRoom};
use crate::params::{
LEGACY_TEXT_SHADOW_DIVISOR, LEGACY_TEXT_SHADOW_GRAY, REFLECTION_DIST_SCALE,
REFLECTION_END_ALPHA_FLOOR, SHADOW_BOX_STEP, TEXT_SHADOW_BASE_OFFSET, TEXT_SHADOW_POWER,
TEXT_SHADOW_SIZE,
};
use crate::props::{RunProps, TextGlow, TextReflection, TextShadow};
pub(crate) fn legacy_offset(size: Length) -> Length {
let step = SHADOW_BOX_STEP.units();
let divisor = i128::from(LEGACY_TEXT_SHADOW_DIVISOR) * i128::from(step);
let steps = div_round_i128(i128::from(size.units()), divisor).max(1);
Length::from_units(saturated(steps * i128::from(step)))
}
pub(super) fn legacy_shadow(size: Length) -> DrawnShadow {
let offset = legacy_offset(size);
let gray = LEGACY_TEXT_SHADOW_GRAY;
DrawnShadow {
across: offset,
down: offset,
color: Rgb {
r: gray,
g: gray,
b: gray,
},
form: ShadowForm::Paths,
}
}
pub(super) fn room(shadow: TextShadow, size: Length) -> ShadowRoom {
let share = share(size);
let blur = scaled(shadow.blur, share);
let down = scaled(
ShadowOffset::of(shadow.distance, shadow.direction).down,
share,
);
ShadowRoom {
above: (blur - down).max(Length::ZERO),
below: (blur + down).max(Length::ZERO),
}
}
pub(super) fn sharp(shadow: TextShadow, scaled: bool) -> Option<(Rgb, Opacity)> {
if scaled {
return None;
}
let [r, g, b] = shadow.color?;
let opacity = Opacity::clamped(DRAWING_WHOLE - shadow.transparency);
if opacity.share() <= 0 {
return None;
}
Some((Rgb { r, g, b }, opacity))
}
pub(super) fn drawn(
shadow: TextShadow,
outline: Option<Outline>,
scaled_text: bool,
size: Length,
) -> Option<DrawnShadow> {
let (color, opacity) = sharp(shadow, scaled_text)?;
let share = share(size);
let length = div_round_i128(
i128::from(shadow.distance.units()) * share
+ i128::from(TEXT_SHADOW_BASE_OFFSET.units()) * (ONE - share),
ONE,
);
let offset = ShadowOffset::of(Length::from_units(saturated(length)), shadow.direction);
let blur = scaled(shadow.blur, share);
let form = if blur <= Length::ZERO && opacity == Opacity::FULL && outline.is_none() {
ShadowForm::Paths
} else {
ShadowForm::Picture {
blur: blur.max(Length::ZERO),
opacity,
outline_width: outline.map_or(Length::ZERO, |line| line.width),
}
};
Some(DrawnShadow {
across: offset.across,
down: offset.down,
color,
form,
})
}
pub(super) fn glow_room(glow: TextGlow, size: Length) -> ShadowRoom {
let radius = scaled(glow.radius, share(size));
let half = radius.div_round(2);
let room = (half + half.div_round(2)).max(Length::ZERO);
ShadowRoom {
above: room,
below: room,
}
}
pub(super) fn drawn_glow(glow: TextGlow, scaled_text: bool, size: Length) -> Option<DrawnGlow> {
if scaled_text || glow.radius <= Length::ZERO {
return None;
}
let [r, g, b] = glow.color?;
let opacity = Opacity::clamped(DRAWING_WHOLE - glow.transparency);
if opacity.share() <= 0 {
return None;
}
let radius = scaled(glow.radius, share(size));
Some(DrawnGlow {
radius,
color: Rgb { r, g, b },
opacity,
})
}
fn reflection_pivot(distance: Length, descent: Length) -> Length {
if distance <= Length::ZERO || descent <= Length::ZERO {
return Length::ZERO;
}
let scaled = div_round_i128(
i128::from(distance.units()) * i128::from(descent.units()),
i128::from(REFLECTION_DIST_SCALE.units()),
);
Length::from_units(saturated(scaled))
}
fn reflection_blur(blur: Length, size: Length) -> Length {
if blur <= Length::ZERO || size <= Length::ZERO {
return Length::ZERO;
}
let scaled = div_round_i128(
i128::from(blur.units()) * i128::from(size.units()),
i128::from(TEXT_SHADOW_SIZE.units()),
);
Length::from_units(saturated(scaled))
}
pub(super) fn reflection_room(
reflection: TextReflection,
(ascent, descent, size): (Length, Length, Length),
) -> ShadowRoom {
if !reflection.downward {
return ShadowRoom::default();
}
let pivot = reflection_pivot(reflection.distance, descent);
let blur = reflection_blur(reflection.blur, size);
let eff_end = if reflection.end_alpha >= REFLECTION_END_ALPHA_FLOOR {
DRAWING_WHOLE
} else {
reflection.end_pos
};
let ascent_part = Length::from_units(saturated(div_round_i128(
i128::from(ascent.units()) * i128::from(eff_end),
i128::from(DRAWING_WHOLE),
)));
let total_below = ascent_part + pivot * 2 + blur;
ShadowRoom {
above: Length::ZERO,
below: (total_below - descent).max(Length::ZERO),
}
}
pub(super) fn drawn_reflection(
reflection: TextReflection,
outline: Option<Outline>,
scaled_text: bool,
(ascent, descent, size): (Length, Length, Length),
) -> Option<DrawnReflection> {
if scaled_text || !reflection.downward {
return None;
}
let start_opacity = Opacity::clamped(reflection.start_alpha);
let end_raw = if reflection.end_alpha < REFLECTION_END_ALPHA_FLOOR {
0
} else {
reflection.end_alpha
};
let end_opacity = Opacity::clamped(end_raw);
if start_opacity.share() <= 0 && end_opacity.share() <= 0 {
return None;
}
let pivot = reflection_pivot(reflection.distance, descent);
let blur = reflection_blur(reflection.blur, size);
let full_span = (ascent + pivot).max(Length::ZERO);
let at_pos = |pos: i64| {
pivot
+ Length::from_units(saturated(div_round_i128(
i128::from(full_span.units()) * i128::from(pos),
i128::from(DRAWING_WHOLE),
)))
};
let fade_start = at_pos(reflection.start_pos);
let fade_end = at_pos(reflection.end_pos).max(fade_start);
Some(DrawnReflection {
pivot,
blur,
fade_start,
fade_end,
start_opacity,
end_opacity,
outline_width: outline.map_or(Length::ZERO, |line| line.width),
})
}
pub(super) fn scaled_run(props: &RunProps) -> bool {
props.scale.is_some_and(|scale| scale != PERCENT)
}
const ONE: i128 = 1 << (u64::BITS - u8::BITS);
fn share(size: Length) -> i128 {
let (power, root) = TEXT_SHADOW_POWER;
let units = i128::from(size.units());
let reference = i128::from(TEXT_SHADOW_SIZE.units());
if units <= 0 || reference <= 0 || units >= reference << root {
return 0;
}
let ratio = div_round_i128(units * ONE, reference);
let mut base = if ratio > ONE { 2 * ONE } else { ONE };
loop {
let below = times_itself(base, root - 1);
let next = div_round_i128(
i128::from(root - 1) * base + div_round_i128(ratio * ONE, below),
i128::from(root),
);
if next >= base {
return times_itself(base, power);
}
base = next;
}
}
fn times_itself(value: i128, count: u32) -> i128 {
(0..count).fold(ONE, |product, _| div_round_i128(product * value, ONE))
}
fn scaled(length: Length, share: i128) -> Length {
Length::from_units(saturated(div_round_i128(
i128::from(length.units()) * share,
ONE,
)))
}
#[cfg(test)]
mod tests;