use {
crate::*,
minimad::*,
unicode_width::{
UnicodeWidthChar,
UnicodeWidthStr,
},
};
pub static ELLIPSIS: &str = "…";
#[derive(Debug, Clone, Copy)]
pub struct Fitter {
mid_token_ellision: bool,
mid_compound_ellision: bool,
align: Alignment,
}
impl Default for Fitter {
fn default() -> Self {
Self {
mid_token_ellision: true,
mid_compound_ellision: true,
align: Alignment::Unspecified,
}
}
}
#[derive(Debug, Clone, Copy)]
struct CharInfo {
byte_idx: usize,
width: usize, }
fn str_char_infos(s: &str) -> Vec<CharInfo> {
s.char_indices()
.map(|(byte_idx, char)| CharInfo {
byte_idx,
width: char.width().unwrap_or(0),
})
.collect()
}
#[derive(Debug, Clone)]
struct Zone {
compound_idx: usize,
byte_start_idx: usize,
char_infos: Vec<CharInfo>,
removable_width: usize, }
impl Zone {
fn token(compounds: &[Compound], min_removable_width: usize) -> Vec<Zone> {
let mut zones = Vec::new();
for (compound_idx, compound) in compounds.iter().enumerate() {
let s = compound.src;
if s.len() < min_removable_width + 2 {
continue;
}
let mut byte_start: Option<usize> = None;
for (byte_idx, char) in s.char_indices() {
if char.is_whitespace() {
if let Some(byte_start_idx) = byte_start {
if byte_idx - byte_start_idx >= min_removable_width + 2 {
let zs = &s[byte_start_idx..byte_idx];
let removable_width = zs.width();
if removable_width >= min_removable_width {
let char_infos = str_char_infos(zs);
zones.push(Zone {
compound_idx,
byte_start_idx,
char_infos,
removable_width,
});
}
}
byte_start = None;
}
} else if byte_start.is_none() {
byte_start = Some(byte_idx);
}
}
if let Some(byte_start_idx) = byte_start {
let byte_end_idx = s.len();
if byte_end_idx - byte_start_idx >= min_removable_width + 2 {
let zs = &s[byte_start_idx..];
let removable_width = zs.width();
if removable_width >= min_removable_width {
let char_infos = str_char_infos(zs);
zones.push(Zone {
compound_idx,
byte_start_idx,
char_infos,
removable_width,
});
}
}
}
}
zones
}
fn biggest_token(compounds: &[Compound], min_removable_width: usize) -> Option<Zone> {
Zone::token(compounds, min_removable_width)
.drain(..)
.max_by_key(|z| z.removable_width)
}
fn compounds(compounds: &[Compound], min_removable_width: usize) -> Vec<Zone> {
compounds.iter()
.enumerate()
.filter_map(|(compound_idx, compound)| {
let char_infos = str_char_infos(compound.src);
if char_infos.len() < 2 + min_removable_width {
return None;
}
let removable = &compound.src[char_infos[1].byte_idx..char_infos[char_infos.len()-1].byte_idx];
let removable_width = removable.width();
if removable_width < min_removable_width {
None
} else {
Some(Zone {
compound_idx,
byte_start_idx: 0,
char_infos,
removable_width,
})
}
})
.collect()
}
fn biggest_compound(compounds: &[Compound], min_removable_width: usize) -> Option<Zone> {
Zone::compounds(compounds, min_removable_width)
.drain(..)
.max_by_key(|z| z.removable_width)
}
fn cut(&self, compounds: &mut Vec<Compound>, to_remove: usize) -> usize {
if self.removable_width < 2 {
return 0;
}
let compound = &compounds[self.compound_idx];
let len = self.char_infos.len();
let mut start_char_idx = len / 2;
let mut end_char_idx = start_char_idx;
let mut removed_width = 0;
loop {
if (end_char_idx-start_char_idx)%2 == 0 {
if end_char_idx + 1 >= len {
break;
}
end_char_idx += 1;
} else {
if start_char_idx <= 1 {
break;
}
start_char_idx -= 1;
}
let start_byte_idx = self.byte_start_idx + self.char_infos[start_char_idx].byte_idx;
let end_byte_idx = self.byte_start_idx + self.char_infos[end_char_idx].byte_idx;
removed_width = (compound.src[start_byte_idx..end_byte_idx]).width();
if removed_width >= to_remove {
break;
}
}
let start_byte_idx = self.byte_start_idx + self.char_infos[start_char_idx].byte_idx;
let end_byte_idx = self.byte_start_idx + self.char_infos[end_char_idx].byte_idx;
let head = compound.sub(0, start_byte_idx);
let tail = compound.tail(end_byte_idx);
compounds[self.compound_idx] = head;
compounds.insert(self.compound_idx+1, Compound::raw_str(ELLIPSIS));
compounds.insert(self.compound_idx+2, tail);
removed_width - 1
}
}
impl Fitter {
pub fn for_align(align: Alignment) -> Self {
let internal_ellision = align == Alignment::Unspecified;
Self {
mid_token_ellision: internal_ellision,
mid_compound_ellision: internal_ellision,
align,
}
}
pub fn fit(
self,
fc: &mut FmtComposite<'_>,
max_width: usize,
skin: &MadSkin
) {
if fc.visible_length <= max_width {
return;
} else if max_width == 0 {
fc.compounds.clear();
fc.visible_length = 0;
return;
} else if max_width == 1 {
fc.compounds.clear();
fc.compounds.push(Compound::raw_str(ELLIPSIS));
fc.visible_length = 1;
return;
}
let mut excess = fc.visible_length - max_width;
if self.mid_token_ellision {
while excess > 0 {
let mut gain = 0;
if let Some(zone) = Zone::biggest_token(&fc.compounds, 3) {
gain = zone.cut(&mut fc.compounds, excess + 1);
}
if gain == 0 {
break;
}
excess -= gain.min(excess);
}
}
if self.mid_compound_ellision {
while excess > 0 {
let mut gain = 0;
if let Some(zone) = Zone::biggest_compound(&fc.compounds, 2) {
gain = zone.cut(&mut fc.compounds, excess + 1);
}
if gain == 0 {
break;
}
excess -= gain.min(excess);
}
}
if excess == 0 {
fc.recompute_width(skin);
return;
}
let compounds = &mut fc.compounds;
let (mut excess_left, mut excess_right) = match self.align {
Alignment::Right => (excess + 1, 0),
Alignment::Left | Alignment:: Unspecified => (0, excess + 1),
Alignment::Center => {
let left = excess / 2;
let right = excess - left;
if left > 0 {
(left + 1, right + 1)
} else {
(0, right + 1)
}
},
};
if excess_left > 0 {
while excess_left > 0 && !compounds.is_empty() {
let compound = &mut compounds[0];
let char_infos = str_char_infos(compound.src);
let mut last_removed_char_idx = 0;
let mut removed_width = 0;
loop {
removed_width += char_infos[last_removed_char_idx].width;
if removed_width >= excess_left || last_removed_char_idx + 1 == char_infos.len() {
break;
}
last_removed_char_idx += 1;
}
if last_removed_char_idx + 1 == char_infos.len() {
compounds.remove(0);
excess_left -= removed_width.min(excess_left);
} else {
compound.src = &compound.src[char_infos[last_removed_char_idx+1].byte_idx..];
excess_left = 0;
}
}
compounds.insert(0, Compound::raw_str(ELLIPSIS));
}
if excess_right > 0 {
while excess_right > 0 && !compounds.is_empty() {
let last_idx = compounds.len()-1;
let compound = &mut compounds[last_idx];
let char_infos = str_char_infos(compound.src);
let mut removed_width = 0;
let mut end_byte_idx = compound.src.len();
for ci in char_infos.iter().rev() {
end_byte_idx = ci.byte_idx;
removed_width += ci.width;
if removed_width >= excess_right {
break;
}
}
if end_byte_idx == 0 {
compounds.pop();
excess_right -= removed_width.min(excess_right);
} else {
compound.src = &compound.src[..end_byte_idx];
excess_right = 0;
}
}
compounds.push(Compound::raw_str(ELLIPSIS));
}
fc.recompute_width(skin);
}
}
#[cfg(test)]
mod fit_tests {
use minimad::{
Alignment,
Composite,
};
use crate::{
Fitter,
FmtComposite,
};
fn check_fit_align(src: &str, target_width: usize, align: Alignment) {
dbg!((target_width, align));
let skin = crate::get_default_skin();
let mut fc = FmtComposite::from(Composite::from_inline(src), skin);
let fitter = Fitter::for_align(align);
fitter.fit(&mut fc, target_width, skin);
dbg!(&fc);
assert!(fc.visible_length <= target_width); }
fn check_fit(src: &str, target_width: usize) {
check_fit_align(src, target_width, Alignment::Right);
check_fit_align(src, target_width, Alignment::Left);
check_fit_align(src, target_width, Alignment::Center);
check_fit_align(src, target_width, Alignment::Unspecified);
}
#[test]
fn test_fit() {
let sentence = "This sentence has **short** and **much longer** parts, and some Korean: *一曰道,二曰天*.";
check_fit(sentence, 60);
check_fit(sentence, 40);
let five_issues = "一曰道,二曰天,三曰地,四曰將,五曰法。";
check_fit(five_issues, 15);
check_fit(five_issues, 8);
let status = "ab *cd* `12345 123456789`";
check_fit(status, 17);
check_fit(status, 2);
}
}