use f_office_paint::{Item, Rgb};
use f_office_units::scale::THOUSANDTHS;
use f_office_units::{Edges, Length};
use crate::chart::{AxisTitle, Columns, LayoutCorner, LegendPlace, PlotPlace, Title, TitleText};
use crate::layout::Layout;
use crate::params::{
CHART_AXIS_TITLE_GAP, CHART_CATEGORY_TITLE_BOTTOM, CHART_LINES_LABEL_START,
CHART_TITLE_CORNER_LEFT, CHART_TITLE_CORNER_TOP, CHART_TITLE_LINE_INSET,
CHART_TITLE_LINE_SHARE, CHART_TITLE_LINE_TOP, CHART_VALUE_TITLE_LEFT,
};
use crate::props::RunProps;
use super::categories::UPWARD;
use super::labels::ChartText;
use super::{LabelLines, ValueAxis, layout_corner};
impl Layout<'_> {
pub(super) fn chart_title(&mut self, title: &Title, frame: Edges<Length>) -> Option<Vec<Item>> {
let text = title.text.as_ref()?;
let lines = self.title_lines(title, frame)?;
let props = title_props(title.size, text);
let face = self.shaper.face(&props, true)?;
let face = self.shaper.faces().get(face)?;
let line = face.line_height();
let descent = face.win_line() - face.win_top();
let color = self.label_color(text.fill.as_ref());
let (middle, top) = match title.corner {
None => (
(frame.left + frame.right).div_toward_zero(2),
frame.top + CHART_TITLE_LINE_TOP,
),
Some(corner) => corner_place(frame, corner, &lines)?,
};
let mut baseline = top + line - descent;
let mut items = Vec::with_capacity(lines.len());
for shaped in lines {
if let Some(shaped) = shaped {
let width = shaped.width();
items.push(shaped.item(middle - width.div_toward_zero(2), baseline, color)?);
}
baseline += line;
}
Some(items)
}
pub(super) fn axis_title_rooms(&mut self, chart: &Columns) -> Option<(Length, Length)> {
let (left, bottom) = chart.axis_titles.sides(&chart.group)?;
let mut room = |title: Option<&AxisTitle>| {
title.map_or(Some(Length::ZERO), |title| {
Some(self.lines_of(&axis_title_props(title))?.single_line + CHART_AXIS_TITLE_GAP)
})
};
Some((room(left)?, room(bottom)?))
}
pub(super) fn axis_title_items(
&mut self,
chart: &Columns,
(axis, plot): (&ValueAxis, Edges<Length>),
frame: Edges<Length>,
) -> Option<Vec<Item>> {
let (left, bottom) = chart.axis_titles.sides(&chart.group)?;
let mut items = Vec::with_capacity(2);
if let Some(title) = left {
items.push(self.left_axis_title_item(chart, title, (axis, plot), frame)?);
}
if let Some(title) = bottom {
items.push(self.bottom_axis_title_item(chart, title, (axis, plot), frame)?);
}
Some(items)
}
fn left_axis_title_item(
&mut self,
chart: &Columns,
title: &AxisTitle,
(axis, plot): (&ValueAxis, Edges<Length>),
frame: Edges<Length>,
) -> Option<Item> {
let (shaped, lines, color) = self.axis_title_text(title)?;
let width = shaped.width();
let start = if let Some(corner) = title.corner {
let (left, top) = layout_corner(frame, corner)?;
let box_left = left.min(
frame.right - lines.single_line - CHART_TITLE_CORNER_TOP - CHART_TITLE_CORNER_TOP,
);
let box_top =
top.min(frame.bottom - width - CHART_TITLE_CORNER_LEFT - CHART_TITLE_CORNER_LEFT);
(
box_left + CHART_TITLE_CORNER_TOP + lines.single_top,
box_top + CHART_TITLE_CORNER_LEFT + width,
)
} else {
if width > plot.bottom - plot.top {
return None;
}
let box_left = match chart.plot_place {
PlotPlace::Automatic => frame.left + CHART_VALUE_TITLE_LEFT,
PlotPlace::Manual(_) => {
let labels = self.left_labels_room(chart, axis)?;
(plot.left - labels) - (lines.single_line + CHART_AXIS_TITLE_GAP)
+ (CHART_VALUE_TITLE_LEFT - CHART_LINES_LABEL_START)
}
PlotPlace::Other => return None,
};
(
box_left + lines.single_top,
(plot.top + plot.bottom + width).div_toward_zero(2),
)
};
shaped.turned_item(start, (UPWARD, color), |reached| {
Some((Length::ZERO, -reached))
})
}
fn bottom_axis_title_item(
&mut self,
chart: &Columns,
title: &AxisTitle,
(axis, plot): (&ValueAxis, Edges<Length>),
frame: Edges<Length>,
) -> Option<Item> {
let (shaped, lines, color) = self.axis_title_text(title)?;
let width = shaped.width();
let (start, baseline) = if let Some(corner) = title.corner {
let (left, top) = layout_corner(frame, corner)?;
let box_left =
left.min(frame.right - width - CHART_TITLE_CORNER_LEFT - CHART_TITLE_CORNER_LEFT);
let box_top = top.min(
frame.bottom - lines.single_line - CHART_TITLE_CORNER_TOP - CHART_TITLE_CORNER_TOP,
);
(
box_left + CHART_TITLE_CORNER_LEFT,
box_top + CHART_TITLE_CORNER_TOP + lines.single_top,
)
} else {
if width > plot.right - plot.left {
return None;
}
let baseline = match chart.plot_place {
PlotPlace::Automatic => {
let legend = if chart.legend == Some(LegendPlace::Bottom) {
self.legend_lines(chart)?.legend_room()?
} else {
Length::ZERO
};
frame.bottom - legend - CHART_CATEGORY_TITLE_BOTTOM - (lines.line - lines.top)
}
PlotPlace::Manual(_) => {
let below = self.below_labels_room(chart, axis, plot, frame)?;
plot.bottom + below + (lines.single_line + CHART_AXIS_TITLE_GAP)
- CHART_CATEGORY_TITLE_BOTTOM
- (lines.line - lines.top)
}
PlotPlace::Other => return None,
};
(
(plot.left + plot.right - width).div_toward_zero(2),
baseline,
)
};
shaped.item(start, baseline, color)
}
fn axis_title_text(&mut self, title: &AxisTitle) -> Option<(ChartText, LabelLines, Rgb)> {
let props = axis_title_props(title);
let line = title.text.lines.first()?;
let shaped = self.chart_text(&props, line)?;
let lines = self.lines_of(&props)?;
Some((shaped, lines, self.label_color(title.text.fill.as_ref())))
}
pub(super) fn title_line_count(&mut self, title: &Title, frame: Edges<Length>) -> Option<i64> {
let count = self
.title_lines(title, frame)
.map_or(title.lines, |lines| lines.len());
i64::try_from(count).ok()
}
fn title_lines(
&mut self,
title: &Title,
frame: Edges<Length>,
) -> Option<Vec<Option<ChartText>>> {
let text = title.text.as_ref()?;
let props = title_props(title.size, text);
let limit = (frame.right - frame.left)
.checked_mul(CHART_TITLE_LINE_SHARE)?
.div_toward_zero(THOUSANDTHS)
- CHART_TITLE_LINE_INSET;
let mut lines = Vec::with_capacity(text.lines.len());
for paragraph in &text.lines {
if paragraph.is_empty() {
lines.push(None);
continue;
}
let whole = self.chart_text(&props, paragraph)?;
if whole.width() <= limit {
lines.push(Some(whole));
continue;
}
for broken in self.broken_lines(&props, paragraph, limit)? {
lines.push(Some(broken));
}
}
Some(lines)
}
fn broken_lines(
&mut self,
props: &RunProps,
paragraph: &str,
limit: Length,
) -> Option<Vec<ChartText>> {
let words: Vec<&str> = paragraph.split(' ').collect();
if words.iter().any(|word| word.is_empty()) {
return None;
}
let mut lines = Vec::new();
let mut start = 0;
while start < words.len() {
let mut end = start + 1;
let mut line = self.chart_text(props, &words.get(start..end)?.join(" "))?;
if line.width() > limit {
return None;
}
while end < words.len() {
let longer = self.chart_text(props, &words.get(start..=end)?.join(" "))?;
if longer.width() > limit {
break;
}
line = longer;
end += 1;
}
lines.push(line);
start = end;
}
Some(lines)
}
}
fn corner_place(
frame: Edges<Length>,
corner: LayoutCorner,
lines: &[Option<ChartText>],
) -> Option<(Length, Length)> {
let (left, top) = layout_corner(frame, corner)?;
let widest = lines
.iter()
.flatten()
.map(ChartText::width)
.max()
.unwrap_or_default();
let start = left.min(frame.right - widest - CHART_TITLE_CORNER_LEFT - CHART_TITLE_CORNER_LEFT);
Some((
start + CHART_TITLE_CORNER_LEFT + widest.div_toward_zero(2),
top + CHART_TITLE_CORNER_TOP,
))
}
fn axis_title_props(title: &AxisTitle) -> RunProps {
title_props(title.size, &title.text)
}
fn title_props(size: i64, text: &TitleText) -> RunProps {
RunProps {
font_ascii: Some(text.font.clone()),
font_hansi: Some(text.font.clone()),
size: Some(size),
bold: text.bold.then_some(true),
italic: text.italic.then_some(true),
..RunProps::default()
}
}