use azul_css::{impl_option, impl_option_inner, AzString, OptionString};
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct StyledTextRun {
pub text: AzString,
pub font_family: OptionString,
pub font_size_px: f32,
pub color: azul_css::props::basic::ColorU,
pub is_bold: bool,
pub is_italic: bool,
}
azul_css::impl_option!(StyledTextRun, OptionStyledTextRun, copy = false, [Debug, Clone, PartialEq]);
azul_css::impl_vec!(StyledTextRun, StyledTextRunVec, StyledTextRunVecDestructor, StyledTextRunVecDestructorType, StyledTextRunVecSlice, OptionStyledTextRun);
azul_css::impl_vec_debug!(StyledTextRun, StyledTextRunVec);
azul_css::impl_vec_clone!(StyledTextRun, StyledTextRunVec, StyledTextRunVecDestructor);
azul_css::impl_vec_partialeq!(StyledTextRun, StyledTextRunVec);
#[derive(Debug, Clone, PartialEq)]
#[repr(C)]
pub struct ClipboardContent {
pub plain_text: AzString,
pub styled_runs: StyledTextRunVec,
}
impl_option!(
ClipboardContent,
OptionClipboardContent,
copy = false,
[Debug, Clone, PartialEq]
);
impl ClipboardContent {
#[must_use] pub fn to_html(&self) -> String {
use core::fmt::Write as _;
let mut html = String::from("<div>");
for run in self.styled_runs.as_slice() {
html.push_str("<span style=\"");
if let Some(font_family) = run.font_family.as_ref() {
let _ = write!(html, "font-family: {}; ", font_family.as_str());
}
let _ = write!(html, "font-size: {}px; ", run.font_size_px);
let _ = write!(
html,
"color: rgba({}, {}, {}, {}); ",
run.color.r,
run.color.g,
run.color.b,
f32::from(run.color.a) / 255.0
);
if run.is_bold {
html.push_str("font-weight: bold; ");
}
if run.is_italic {
html.push_str("font-style: italic; ");
}
html.push_str("\">");
let escaped = run
.text
.as_str()
.replace('&', "&")
.replace('<', "<")
.replace('>', ">");
html.push_str(&escaped);
html.push_str("</span>");
}
html.push_str("</div>");
html
}
}