use std::ops::Range;
use cosmic_text::{LineEnding, LineIter};
use crate::{
app::component::{DevtoolsFormatter, DevtoolsInspectable},
layout::text_style::{FontWeight, TextStyle},
node::TextColor,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ParagraphKind {
#[default]
Body,
Heading(u8),
Bullet,
Ordered,
Quote,
Code,
}
impl ParagraphKind {
pub(crate) fn canonical(self) -> Self {
match self {
Self::Heading(level) => Self::Heading(level.clamp(1, 6)),
kind => kind,
}
}
}
impl DevtoolsInspectable for ParagraphKind {
fn inspect(&self, formatter: &mut DevtoolsFormatter<'_>) {
formatter.value_debug(self);
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub enum ParagraphMarker {
#[default]
None,
Dot { size: f32, gap: f32, color: TextColor },
Number { gap: f32, color: TextColor },
Bar { width: f32, color: TextColor },
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ParagraphStyle {
pub font_size_scale: Option<f32>,
pub font_size: Option<f32>,
pub weight: Option<FontWeight>,
pub line_height: Option<f32>,
pub font_family: Option<std::sync::Arc<str>>,
pub color: Option<TextColor>,
pub indent: f32,
pub marker: ParagraphMarker,
pub background: Option<TextColor>,
pub background_radius: f32,
pub joined: bool,
}
impl ParagraphStyle {
pub fn new() -> Self {
Self::default()
}
pub fn font_size(mut self, size: f32) -> Self {
self.font_size = Some(size);
self
}
pub fn font_size_scale(mut self, scale: f32) -> Self {
self.font_size_scale = Some(scale);
self
}
pub fn weight(mut self, weight: FontWeight) -> Self {
self.weight = Some(weight);
self
}
pub fn line_height(mut self, line_height: f32) -> Self {
self.line_height = Some(line_height);
self
}
pub fn font_family(mut self, family: impl Into<std::sync::Arc<str>>) -> Self {
self.font_family = Some(family.into());
self
}
pub fn color(mut self, color: impl Into<TextColor>) -> Self {
self.color = Some(color.into());
self
}
pub fn indent(mut self, indent: f32) -> Self {
self.indent = indent;
self
}
pub fn marker(mut self, marker: ParagraphMarker) -> Self {
self.marker = marker;
self
}
pub fn background(mut self, color: impl Into<TextColor>, radius: f32) -> Self {
self.background = Some(color.into());
self.background_radius = radius;
self
}
pub fn joined(mut self, joined: bool) -> Self {
self.joined = joined;
self
}
pub(crate) fn text(&self, base: &TextStyle) -> TextStyle {
let mut text = base.clone();
if let Some(size) = self.font_size.filter(|size| size.is_finite() && *size > 0.0) {
text.font_size = size;
} else if let Some(scale) = self.font_size_scale.filter(|scale| scale.is_finite() && *scale > 0.0) {
text.font_size *= scale;
}
if let Some(weight) = self.weight {
text.weight = weight;
}
if let Some(line_height) = self.line_height.filter(|height| height.is_finite() && *height > 0.0) {
text.line_height = line_height;
}
if let Some(family) = &self.font_family {
text.font_family = family.clone();
}
text
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ParagraphStyles {
pub gap: f32,
pub body: ParagraphStyle,
pub headings: [ParagraphStyle; 6],
pub bullet: ParagraphStyle,
pub ordered: ParagraphStyle,
pub quote: ParagraphStyle,
pub code: ParagraphStyle,
}
impl ParagraphStyles {
pub fn new() -> Self {
Self::default()
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = gap;
self
}
pub fn with(mut self, kind: ParagraphKind, style: ParagraphStyle) -> Self {
match kind.canonical() {
ParagraphKind::Body => self.body = style,
ParagraphKind::Heading(level) => self.headings[usize::from(level) - 1] = style,
ParagraphKind::Bullet => self.bullet = style,
ParagraphKind::Ordered => self.ordered = style,
ParagraphKind::Quote => self.quote = style,
ParagraphKind::Code => self.code = style,
}
self
}
pub fn of(&self, kind: ParagraphKind) -> &ParagraphStyle {
match kind.canonical() {
ParagraphKind::Body => &self.body,
ParagraphKind::Heading(level) => &self.headings[usize::from(level) - 1],
ParagraphKind::Bullet => &self.bullet,
ParagraphKind::Ordered => &self.ordered,
ParagraphKind::Quote => &self.quote,
ParagraphKind::Code => &self.code,
}
}
}
pub(crate) fn paragraph_ranges(text: &str) -> Vec<Range<usize>> {
let mut ranges = Vec::new();
let mut open = true;
for (range, ending) in LineIter::new(text) {
ranges.push(range);
open = ending != LineEnding::None;
}
if open {
ranges.push(text.len()..text.len());
}
ranges
}
pub(crate) fn paragraph_of(ranges: &[Range<usize>], byte: usize) -> usize {
ranges.partition_point(|range| range.start <= byte).saturating_sub(1)
}
pub(crate) fn normalize_kinds(mut kinds: Vec<ParagraphKind>, count: usize) -> Vec<ParagraphKind> {
kinds.resize(count, ParagraphKind::Body);
for kind in &mut kinds {
*kind = kind.canonical();
}
kinds
}
pub(crate) fn splice_kinds(
kinds: &[ParagraphKind],
before: &[Range<usize>],
after: &[Range<usize>],
replaced: Range<usize>,
inserted: usize,
) -> Vec<ParagraphKind> {
let kinds = normalize_kinds(kinds.to_vec(), before.len());
let first = paragraph_of(before, replaced.start);
let last = paragraph_of(before, replaced.end);
let added = paragraph_of(after, replaced.start + inserted) - paragraph_of(after, replaced.start);
let mut out = Vec::with_capacity(after.len());
out.extend_from_slice(&kinds[..=first]);
out.extend(std::iter::repeat_n(kinds[first], added));
out.extend_from_slice(&kinds[(last + 1).min(kinds.len())..]);
normalize_kinds(out, after.len())
}
pub(crate) fn place_kinds(
mut kinds: Vec<ParagraphKind>,
after: &[Range<usize>],
start: usize,
inserted: usize,
pasted: &[ParagraphKind],
) -> Vec<ParagraphKind> {
let first = paragraph_of(after, start);
let last = paragraph_of(after, start + inserted);
if kinds.len() != after.len() || pasted.len() != last - first + 1 {
return kinds;
}
let nothing_before = after[first].start == start;
let nothing_after = after[last].end == start + inserted;
if first == last {
if nothing_before && nothing_after {
kinds[first] = pasted[0].canonical();
}
return kinds;
}
if nothing_before {
kinds[first] = pasted[0].canonical();
}
for index in first + 1..last {
kinds[index] = pasted[index - first].canonical();
}
if after[last].start != start + inserted {
kinds[last] = pasted[last - first].canonical();
}
kinds
}
#[cfg(test)]
#[path = "../../../tests/input/text_input/paragraph_ops.rs"]
mod tests;