use crate::model::{bin_data::BinDataContent, image::ImageEffect, style::HeadType};
use crate::renderer::{
layout::{utils::find_bin_data_bytes, CellContext},
render_tree::ImageNode,
style_resolver::ResolvedStyleSet,
};
pub(super) fn image_reference(styles: &ResolvedStyleSet, para_style_id: usize) -> Option<u16> {
let style = styles.para_styles.get(para_style_id)?;
if style.head_type != HeadType::Bullet {
return None;
}
styles
.bullets
.get(usize::from(style.numbering_id.checked_sub(1)?))?
.image_reference()
}
pub(super) fn image(
styles: &ResolvedStyleSet,
para_style_id: usize,
data: Option<&[BinDataContent]>,
section_index: usize,
para_index: usize,
cell: Option<&CellContext>,
) -> Option<ImageNode> {
let id = image_reference(styles, para_style_id)?;
let style = &styles.para_styles[para_style_id];
let bullet = &styles.bullets[usize::from(style.numbering_id) - 1];
let bytes = find_bin_data_bytes(data?, id).filter(|bytes| !bytes.is_empty())?;
let mut image = ImageNode::new(id, Some(bytes));
image.section_index = Some(section_index);
image.para_index = Some(cell.map_or(para_index, |context| context.parent_para_index));
image.cell_context = cell.cloned();
if let Some(context) = cell {
let (cell_index, cell_para_index, outer_table_control_index) = context.last_image_indices();
image.cell_index = cell_index;
image.cell_para_index = cell_para_index;
image.outer_table_control_index = outer_table_control_index;
}
image.contrast = bullet.image_data[0] as i8;
image.brightness = bullet.image_data[1] as i8;
image.effect = match bullet.image_data[2] {
1 => ImageEffect::GrayScale,
2 => ImageEffect::BlackWhite,
3 => ImageEffect::Pattern8x8,
_ => ImageEffect::RealPic,
};
Some(image)
}