use super::*;
use cosmic_text::{
Attrs, Buffer, Family, FeatureTag, FontFeatures, LayoutGlyph, Metrics, Shaping, Wrap,
};
const TABULAR_FIGURES: FeatureTag = FeatureTag::new(b"tnum");
fn features(font: FontKey) -> FontFeatures {
let mut features = FontFeatures::new();
if font.tabular {
features.enable(TABULAR_FIGURES);
}
if font.monospace {
features
.disable(FeatureTag::STANDARD_LIGATURES)
.disable(FeatureTag::CONTEXTUAL_LIGATURES)
.disable(FeatureTag::CONTEXTUAL_ALTERNATES);
}
features
}
impl TextSystem {
pub fn build_layout(
&self,
text: &str,
size: f32,
font: FontKey,
wrap_width: f32,
) -> TextLayout {
self.build_layout_with_tab(text, size, font, wrap_width, DEFAULT_TAB, None)
}
pub(super) fn build_layout_with_tab(
&self,
text: &str,
size: f32,
font: FontKey,
wrap_width: f32,
tab: u16,
tab_stop: Option<f32>,
) -> TextLayout {
self.build_layout_runs(text, size, font, wrap_width, tab, tab_stop, None)
}
fn attrs(&self, font: FontKey) -> Attrs<'_> {
let name = if font.monospace {
self.mono.as_ref().map(|r| r.name.as_str())
} else {
Some(self.family.name.as_str())
};
let family = name.map_or(Family::Monospace, Family::Name);
let mut attrs = Attrs::new()
.family(family)
.weight(font.weight.to_fontdb())
.style(font.style);
if font.monospace || font.tabular {
attrs = attrs.font_features(features(font));
}
attrs
}
pub(super) fn build_layout_runs(
&self,
text: &str,
size: f32,
font: FontKey,
wrap_width: f32,
tab: u16,
tab_stop: Option<f32>,
runs: Option<(&[StyleRun], TextFont)>,
) -> TextLayout {
let line_height = size * 1.25;
let mut fonts = font_system().lock().unwrap();
let mut buffer = Buffer::new(&mut fonts, Metrics::new(size, line_height));
buffer.set_tab_width(&mut fonts, tab);
buffer.set_wrap(
&mut fonts,
if wrap_width.is_finite() {
Wrap::WordOrGlyph
} else {
Wrap::None
},
);
buffer.set_size(
&mut fonts,
wrap_width.is_finite().then_some(wrap_width.max(0.0)),
None,
);
let attrs = self.attrs(font);
match runs {
None => buffer.set_text(&mut fonts, text, &attrs, Shaping::Advanced, None),
Some((runs, base)) => {
let spans = runs.iter().enumerate().map(|(i, run)| {
let spec = run.font(base);
(
&text[run.start..run.end],
self.attrs(self.font_key(spec)).metadata(i),
)
});
buffer.set_rich_text(&mut fonts, spans, &attrs, Shaping::Advanced, None);
}
}
buffer.shape_until_scroll(&mut fonts, false);
let mut glyphs = Vec::new();
let mut lines = Vec::new();
let mut width = 0.0_f32;
let mut height = line_height;
let mut baseline = 0.0;
let mut previous_run = None;
let mut width_independent = true;
let (mut tab_line, mut tab_base) = (usize::MAX, 0.0);
for run in buffer.layout_runs() {
width_independent &= previous_run != Some(run.line_i) && !run.rtl;
previous_run = Some(run.line_i);
if glyphs.is_empty() {
baseline = run.line_y;
}
let mut run_glyphs = run.glyphs.to_vec();
let mut line_w = run.line_w;
if let Some(stop) = tab_stop.filter(|_| !run.rtl) {
if tab_line != run.line_i {
(tab_line, tab_base) = (run.line_i, 0.0);
}
line_w += snap_tabs(run.text, &mut run_glyphs, tab_base, stop);
tab_base += line_w;
}
width = width.max(line_w);
height = height.max(run.line_top + run.line_height);
lines.push(lines::build_line(&run, &run_glyphs));
glyphs.extend(run_glyphs.into_iter().map(|g| (g, run.line_y)));
}
if lines.is_empty() {
lines.push(lines::empty_line(line_height));
}
TextLayout {
glyphs,
baseline,
carets: lines::carets(&lines),
lines,
size: Vec2::new(width, height),
width_independent,
}
}
}
fn snap_tabs(text: &str, glyphs: &mut [LayoutGlyph], base: f32, stop: f32) -> f32 {
let mut shift = 0.0;
for glyph in glyphs {
glyph.x += shift;
if stop > 0.0 && text.get(glyph.start..glyph.end) == Some("\t") {
let x = base + glyph.x;
let width = ((x / stop + 1e-3).floor() + 1.0) * stop - x;
shift += width - glyph.w;
glyph.w = width;
}
}
shift
}