use f_office_drawingml::color::on_white;
use f_office_drawingml::theme::ShapeTheme;
use f_office_effects::{Angle, Column, ColumnChart, Picture, PixelLength, Scene, Share, WallLine};
use f_office_paint::{
Image, ImageColor, ImageFormat, ImageItem, InkConversion, Item, Opacity, Rgb,
};
use f_office_units::scale::{DEGREE, PERCENT};
use f_office_units::{Edges, Length};
use super::{Clusters, DrawnText, ValueAxis, bar_paint, holds_all_data_labels, room_units, share};
use crate::chart::{ColumnView, Columns, PlotPlace, Series};
use crate::layout::{Layout, decisions};
use crate::params::{
CHART_AXIS_STEPS, CHART_COLUMN_3D_CATEGORY_DROP, CHART_COLUMN_3D_LINE_TONE,
CHART_COLUMN_3D_LINE_WIDTH, CHART_COLUMN_3D_MARGIN, CHART_COLUMN_3D_PIXELS_PER_INCH,
CHART_COLUMN_3D_PLOT_MOVES, CHART_COLUMN_3D_ROOM, CHART_COLUMN_3D_SIDE_TONE,
CHART_COLUMN_3D_TOP_TONE, CHART_COLUMN_3D_VALUE_BASELINE_DESCENTS,
};
impl Layout<'_> {
pub(super) fn columns_3d(
&mut self,
chart: &Columns,
(gap, gap_depth, view): (i64, i64, ColumnView),
frame: Edges<Length>,
) -> Option<(Vec<Item>, DrawnText)> {
if chart.plot_place != PlotPlace::Automatic {
return None;
}
let shape = (gap, gap_depth, view);
let (axis, twin) = self.flush_axis_and_plot(chart, shape, frame)?;
if axis.crossing != axis.low || axis.low != super::ZERO {
return None;
}
let plot = moved(twin);
let picture = self.column_picture(chart, shape, (&axis, plot))?;
let front = picture.front(plot)?;
let mut items = vec![picture.item()];
let all_data_labels = holds_all_data_labels(chart, false);
let axes_and_legend = self.column_texts(chart, (&axis, front), (twin, frame), &mut items);
Some((
items,
DrawnText {
axes_and_legend,
data_labels: all_data_labels,
},
))
}
fn column_texts(
&mut self,
chart: &Columns,
(axis, front): (&ValueAxis, Edges<Length>),
(twin, frame): (Edges<Length>, Edges<Length>),
items: &mut Vec<Item>,
) -> bool {
let (Some(lines), Some(value_lines)) = (self.label_lines(chart), self.value_lines(chart))
else {
return false;
};
let (Some(flat), Some(solid)) = (
value_lines.value_baseline(),
value_lines.value_baseline_with(CHART_COLUMN_3D_VALUE_BASELINE_DESCENTS),
) else {
return false;
};
let drop = solid - flat;
let lowered = Edges {
top: front.top + drop,
bottom: front.bottom + drop,
..front
};
let (labels, mut all) = self.shown_value_labels(chart, axis, lowered, value_lines);
items.extend(labels);
if !chart.axis_forms.category_hidden {
let anchor = front.bottom + CHART_COLUMN_3D_CATEGORY_DROP;
let categories = self.category_labels(chart, (front, anchor, frame), lines);
all &= categories.is_some();
items.extend(categories.into_iter().flatten());
}
let title = chart.title.as_ref().map_or(Some(()), |title| {
self.chart_title(title, frame)
.map(|lines| items.extend(lines))
});
let legend = chart.legend.map_or(Some(()), |_| {
self.chart_legend(chart, twin, frame)
.map(|legend| items.extend(legend))
});
all && title.is_some() && legend.is_some()
}
fn flush_axis_and_plot(
&mut self,
chart: &Columns,
shape: (i64, i64, ColumnView),
frame: Edges<Length>,
) -> Option<(ValueAxis, Edges<Length>)> {
let axis = ValueAxis::flush_for_chart(chart, CHART_AXIS_STEPS)?;
let plot = self.font_line_plot_with(chart, (&axis, None), frame)?;
let context = self.context;
decisions::chart_axis_room(&context.quirks, (axis, plot), |(axis, plot)| {
let line = self.chart_line(chart, chart.value_label_size)?;
let moved = moved(plot);
let front = self
.column_picture(chart, shape, (&axis, moved))?
.front(moved)?;
let most = room_units(front.bottom - front.top, line);
if axis.steps <= most {
return Some((axis, plot));
}
let axis = ValueAxis::flush_for_chart(chart, most)?;
let plot = self.font_line_plot_with(chart, (&axis, None), frame)?;
Some((axis, plot))
})
}
fn column_picture(
&self,
chart: &Columns,
(gap, gap_depth, view): (i64, i64, ColumnView),
(axis, plot): (&ValueAxis, Edges<Length>),
) -> Option<ColumnPicture> {
let (width, height) = (plot.right - plot.left, plot.bottom - plot.top);
let clusters = Clusters::of(chart, (gap, 0), width)?;
let depth = clusters
.thickness
.checked_mul(view.depth)?
.div_toward_zero(PERCENT);
let floor_depth = depth
.checked_mul(PERCENT + gap_depth)?
.div_toward_zero(PERCENT);
let front_depth = depth.checked_mul(gap_depth)?.div_toward_zero(2 * PERCENT);
let mut scene = ColumnChart::new(
(
Angle::from_sixty_thousandths(view.tilt.checked_mul(DEGREE)?),
Angle::from_sixty_thousandths(view.turn.checked_mul(DEGREE)?),
),
(pixels(width)?, pixels(height)?),
pixels(floor_depth)?,
(
PixelLength::ratio(CHART_COLUMN_3D_MARGIN.0, CHART_COLUMN_3D_MARGIN.1)?,
PixelLength::ratio(CHART_COLUMN_3D_ROOM.0, CHART_COLUMN_3D_ROOM.1)?,
),
);
scene.gridlines = self.wall_lines(chart, (axis, height))?;
let fronts = ColumnFronts {
clusters: &clusters,
axis,
height,
depths: (front_depth, depth),
};
scene.columns = self.solid_columns(chart, &fronts)?;
ColumnPicture::placed(plot, scene)
}
fn wall_lines(
&self,
chart: &Columns,
(axis, height): (&ValueAxis, Length),
) -> Option<Vec<WallLine>> {
let theme = &self.shaper.theme().shapes;
let Some((rgb, alpha, line_width)) =
chart.gridlines.as_ref().and_then(|look| look.line(theme))
else {
return Some(Vec::new());
};
let color = tone(on_white(rgb, alpha), CHART_COLUMN_3D_LINE_TONE);
let line_width = pixels(
line_width
.checked_mul(CHART_COLUMN_3D_LINE_WIDTH.0)?
.div_round(CHART_COLUMN_3D_LINE_WIDTH.1),
)?;
axis.labels()
.map(|value| {
let up = share(height, value, axis.high)?;
Some(WallLine::new(pixels(up)?, line_width, color))
})
.collect()
}
fn solid_columns(&self, chart: &Columns, fronts: &ColumnFronts<'_>) -> Option<Vec<Column>> {
let theme = &self.shaper.theme().shapes;
let mut columns = Vec::new();
for category in 0..i64::try_from(chart.categories).ok()? {
for (index, series) in (0_i64..).zip(chart.bar_series()) {
fronts.push_column(&mut columns, series, (category, index), theme)?;
}
}
Some(columns)
}
}
struct ColumnFronts<'a> {
clusters: &'a Clusters,
axis: &'a ValueAxis,
height: Length,
depths: (Length, Length),
}
impl ColumnFronts<'_> {
fn push_column(
&self,
columns: &mut Vec<Column>,
series: &Series,
(category, index): (i64, i64),
theme: &ShapeTheme,
) -> Option<()> {
let at = usize::try_from(category).ok()?;
let Some(value) = series.values.get(at).copied().flatten() else {
return Some(());
};
if value.digits < 0 {
return None;
}
let Some(paint) = bar_paint(&series.fill, category, theme) else {
return Some(());
};
let super::BarPaint::Color(fill) = paint else {
return None;
};
let left = self.clusters.start((category, index))?;
let right = left + self.clusters.thickness;
let up = share(self.height, value.min(self.axis.high), self.axis.high)?;
let (front, depth) = self.depths;
columns.push(Column::new(
(pixels(left)?, pixels(right)?),
pixels(up)?,
(pixels(front)?, pixels(front + depth)?),
[
channels(fill),
tone(fill, CHART_COLUMN_3D_TOP_TONE),
tone(fill, CHART_COLUMN_3D_SIDE_TONE),
],
));
Some(())
}
}
struct ColumnPicture {
place: Edges<Length>,
picture: Picture,
}
impl ColumnPicture {
fn placed(plot: Edges<Length>, scene: ColumnChart) -> Option<Self> {
let (width, height) = (plot.right - plot.left, plot.bottom - plot.top);
let (across, down) = scene.box_shares()?;
let box_width = part(width, across)?;
let box_height = part(height, down)?;
let left = plot.left + (width - box_width).div_toward_zero(2);
let top = plot.top + (height - box_height).div_toward_zero(2);
Some(Self {
place: Edges {
left,
top,
right: left + box_width,
bottom: top + box_height,
},
picture: Picture {
width: picture_pixels(box_width)?,
height: picture_pixels(box_height)?,
scene: Scene::Columns(scene),
},
})
}
fn item(&self) -> Item {
Item::Image(ImageItem {
x: self.place.left,
y: self.place.top,
width: self.place.right - self.place.left,
height: self.place.bottom - self.place.top,
clip: None,
turn: None,
image: Image {
format: ImageFormat::EffectPicture,
data: self.picture.encode().into(),
opacity: Opacity::FULL,
color: ImageColor::Original,
inks: InkConversion::EmbeddedProfile,
},
})
}
fn front(&self, plot: Edges<Length>) -> Option<Edges<Length>> {
let (width, height) = (plot.right - plot.left, plot.bottom - plot.top);
let corner = self.page_point(PixelLength::ZERO, PixelLength::ZERO)?;
let right = self.page_point(pixels(width)?, PixelLength::ZERO)?;
let top = self.page_point(PixelLength::ZERO, pixels(height)?)?;
Some(Edges {
left: corner.0,
top: top.1,
right: right.0,
bottom: corner.1,
})
}
fn page_point(&self, across: PixelLength, up: PixelLength) -> Option<(Length, Length)> {
let point = self.picture.front_point(across, up)?;
let place = self.place;
Some((
place.left + part(place.right - place.left, point.across)?,
place.top + part(place.bottom - place.top, point.down)?,
))
}
}
fn moved(twin: Edges<Length>) -> Edges<Length> {
let [left, top, right, bottom] = CHART_COLUMN_3D_PLOT_MOVES;
Edges {
left: twin.left + left,
top: twin.top + top,
right: twin.right + right,
bottom: twin.bottom + bottom,
}
}
fn pixels(length: Length) -> Option<PixelLength> {
PixelLength::ratio(
length
.units()
.checked_mul(CHART_COLUMN_3D_PIXELS_PER_INCH)?,
Length::UNITS_PER_INCH,
)
}
pub(super) fn picture_pixels(side: Length) -> Option<u32> {
let units = side.units().checked_mul(CHART_COLUMN_3D_PIXELS_PER_INCH)?;
let whole = units.div_euclid(Length::UNITS_PER_INCH)
+ i64::from(units.rem_euclid(Length::UNITS_PER_INCH) != 0);
u32::try_from(whole).ok().filter(|&n| n > 0)?.checked_add(1)
}
fn part(length: Length, share: Share) -> Option<Length> {
let whole = i64::from(Share::ratio(1, 1)?.hundred_thousandths());
Some(
length
.checked_mul(i64::from(share.hundred_thousandths()))?
.div_round(whole),
)
}
const fn channels(color: Rgb) -> [u8; 3] {
[color.r, color.g, color.b]
}
pub(super) fn tone(color: Rgb, (numerator, denominator): (i64, i64)) -> [u8; 3] {
channels(color).map(|channel| {
let toned = (i64::from(channel) * numerator * 2 + denominator) / (denominator * 2);
u8::try_from(toned).unwrap_or(u8::MAX)
})
}