2022-10-26 14:16:48 -06:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
#![allow(clippy::too_many_arguments)]
|
|
|
|
|
|
2022-11-08 08:43:27 -07:00
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
|
use alloc::vec::Vec;
|
2023-01-04 20:03:03 -07:00
|
|
|
use core::cmp::{max, min};
|
2023-08-16 14:13:36 -07:00
|
|
|
use core::fmt;
|
2022-11-08 08:43:27 -07:00
|
|
|
use core::mem;
|
2022-12-16 16:49:29 -07:00
|
|
|
use core::ops::Range;
|
2022-10-26 14:16:48 -06:00
|
|
|
use unicode_script::{Script, UnicodeScript};
|
2022-10-26 15:16:06 -06:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2022-10-26 14:16:48 -06:00
|
|
|
|
|
|
|
|
use crate::fallback::FontFallbackIter;
|
2023-06-20 06:07:24 +02:00
|
|
|
use crate::{Align, AttrsList, Color, Font, FontSystem, LayoutGlyph, LayoutLine, Wrap};
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2023-04-21 20:26:08 +02:00
|
|
|
/// The shaping strategy of some text.
|
2023-04-21 20:24:44 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
|
pub enum Shaping {
|
2023-04-21 20:26:08 +02:00
|
|
|
/// Basic shaping with no font fallback.
|
|
|
|
|
///
|
|
|
|
|
/// This shaping strategy is very cheap, but it will not display complex
|
|
|
|
|
/// scripts properly nor try to find missing glyphs in your system fonts.
|
|
|
|
|
///
|
|
|
|
|
/// You should use this strategy when you have complete control of the text
|
|
|
|
|
/// and the font you are displaying in your application.
|
2023-04-21 20:47:02 +02:00
|
|
|
#[cfg(feature = "swash")]
|
2023-04-21 20:24:44 +02:00
|
|
|
Basic,
|
2023-04-21 20:26:08 +02:00
|
|
|
/// Advanced text shaping and font fallback.
|
|
|
|
|
///
|
|
|
|
|
/// You will need to enable this strategy if the text contains a complex
|
|
|
|
|
/// script, the font used needs it, and/or multiple fonts in your system
|
|
|
|
|
/// may be needed to display all of the glyphs.
|
2023-04-21 20:24:44 +02:00
|
|
|
Advanced,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Shaping {
|
|
|
|
|
fn run(
|
|
|
|
|
self,
|
2023-07-18 19:18:56 -07:00
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
glyphs: &mut Vec<ShapeGlyph>,
|
2023-04-21 20:24:44 +02:00
|
|
|
font_system: &mut FontSystem,
|
|
|
|
|
line: &str,
|
|
|
|
|
attrs_list: &AttrsList,
|
|
|
|
|
start_run: usize,
|
|
|
|
|
end_run: usize,
|
|
|
|
|
span_rtl: bool,
|
2023-07-18 19:18:56 -07:00
|
|
|
) {
|
2023-04-21 20:24:44 +02:00
|
|
|
match self {
|
2023-04-21 20:47:02 +02:00
|
|
|
#[cfg(feature = "swash")]
|
2023-07-18 19:18:56 -07:00
|
|
|
Self::Basic => shape_skip(font_system, glyphs, line, attrs_list, start_run, end_run),
|
|
|
|
|
Self::Advanced => shape_run(
|
|
|
|
|
scratch,
|
|
|
|
|
glyphs,
|
|
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
start_run,
|
|
|
|
|
end_run,
|
|
|
|
|
span_rtl,
|
|
|
|
|
),
|
2023-04-21 20:24:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
/// A set of buffers containing allocations for shaped text.
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct ShapeBuffer {
|
|
|
|
|
/// Buffer for holding unicode text.
|
|
|
|
|
rustybuzz_buffer: Option<rustybuzz::UnicodeBuffer>,
|
|
|
|
|
|
|
|
|
|
/// Temporary buffers for scripts.
|
|
|
|
|
scripts: Vec<Script>,
|
|
|
|
|
|
|
|
|
|
/// Buffer for visual lines.
|
|
|
|
|
visual_lines: Vec<VisualLine>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 14:13:36 -07:00
|
|
|
impl fmt::Debug for ShapeBuffer {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.pad("ShapeBuffer { .. }")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 14:16:48 -06:00
|
|
|
fn shape_fallback(
|
2023-07-18 19:18:56 -07:00
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
glyphs: &mut Vec<ShapeGlyph>,
|
2022-10-26 14:16:48 -06:00
|
|
|
font: &Font,
|
|
|
|
|
line: &str,
|
2022-10-26 15:16:06 -06:00
|
|
|
attrs_list: &AttrsList,
|
|
|
|
|
start_run: usize,
|
|
|
|
|
end_run: usize,
|
2022-10-26 14:16:48 -06:00
|
|
|
span_rtl: bool,
|
2023-07-18 19:18:56 -07:00
|
|
|
) -> Vec<usize> {
|
2022-10-26 15:16:06 -06:00
|
|
|
let run = &line[start_run..end_run];
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2023-03-12 10:23:54 +01:00
|
|
|
let font_scale = font.rustybuzz().units_per_em() as f32;
|
2023-06-16 02:38:00 +02:00
|
|
|
let ascent = font.rustybuzz().ascender() as f32 / font_scale;
|
|
|
|
|
let descent = -font.rustybuzz().descender() as f32 / font_scale;
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut buffer = scratch.rustybuzz_buffer.take().unwrap_or_default();
|
2022-10-26 14:16:48 -06:00
|
|
|
buffer.set_direction(if span_rtl {
|
|
|
|
|
rustybuzz::Direction::RightToLeft
|
|
|
|
|
} else {
|
|
|
|
|
rustybuzz::Direction::LeftToRight
|
|
|
|
|
});
|
2022-10-26 15:16:06 -06:00
|
|
|
buffer.push_str(run);
|
2022-10-26 14:16:48 -06:00
|
|
|
buffer.guess_segment_properties();
|
|
|
|
|
|
2022-11-15 12:26:59 -07:00
|
|
|
let rtl = matches!(buffer.direction(), rustybuzz::Direction::RightToLeft);
|
2022-10-26 14:16:48 -06:00
|
|
|
assert_eq!(rtl, span_rtl);
|
|
|
|
|
|
2023-03-12 10:30:09 +01:00
|
|
|
let glyph_buffer = rustybuzz::shape(font.rustybuzz(), &[], buffer);
|
2022-10-26 14:16:48 -06:00
|
|
|
let glyph_infos = glyph_buffer.glyph_infos();
|
|
|
|
|
let glyph_positions = glyph_buffer.glyph_positions();
|
|
|
|
|
|
|
|
|
|
let mut missing = Vec::new();
|
2023-07-18 19:18:56 -07:00
|
|
|
glyphs.reserve(glyph_infos.len());
|
|
|
|
|
let glyph_start = glyphs.len();
|
2022-10-26 14:16:48 -06:00
|
|
|
for (info, pos) in glyph_infos.iter().zip(glyph_positions.iter()) {
|
|
|
|
|
let x_advance = pos.x_advance as f32 / font_scale;
|
|
|
|
|
let y_advance = pos.y_advance as f32 / font_scale;
|
|
|
|
|
let x_offset = pos.x_offset as f32 / font_scale;
|
|
|
|
|
let y_offset = pos.y_offset as f32 / font_scale;
|
|
|
|
|
|
2022-10-26 15:16:06 -06:00
|
|
|
let start_glyph = start_run + info.cluster as usize;
|
2022-10-26 14:16:48 -06:00
|
|
|
|
|
|
|
|
if info.glyph_id == 0 {
|
|
|
|
|
missing.push(start_glyph);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 09:19:03 -07:00
|
|
|
let attrs = attrs_list.get_span(start_glyph);
|
2022-10-26 14:16:48 -06:00
|
|
|
glyphs.push(ShapeGlyph {
|
|
|
|
|
start: start_glyph,
|
2022-10-26 15:16:06 -06:00
|
|
|
end: end_run, // Set later
|
2022-10-26 14:16:48 -06:00
|
|
|
x_advance,
|
|
|
|
|
y_advance,
|
|
|
|
|
x_offset,
|
|
|
|
|
y_offset,
|
2023-06-16 02:38:00 +02:00
|
|
|
ascent,
|
|
|
|
|
descent,
|
2023-03-14 00:39:50 +01:00
|
|
|
font_id: font.id(),
|
2022-11-15 12:26:59 -07:00
|
|
|
glyph_id: info.glyph_id.try_into().expect("failed to cast glyph ID"),
|
2022-12-14 09:19:03 -07:00
|
|
|
//TODO: color should not be related to shaping
|
|
|
|
|
color_opt: attrs.color_opt,
|
|
|
|
|
metadata: attrs.metadata,
|
2022-10-26 14:16:48 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adjust end of glyphs
|
|
|
|
|
if rtl {
|
2023-07-18 19:18:56 -07:00
|
|
|
for i in glyph_start + 1..glyphs.len() {
|
2022-10-26 14:16:48 -06:00
|
|
|
let next_start = glyphs[i - 1].start;
|
|
|
|
|
let next_end = glyphs[i - 1].end;
|
|
|
|
|
let prev = &mut glyphs[i];
|
|
|
|
|
if prev.start == next_start {
|
|
|
|
|
prev.end = next_end;
|
|
|
|
|
} else {
|
|
|
|
|
prev.end = next_start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-18 19:18:56 -07:00
|
|
|
for i in (glyph_start + 1..glyphs.len()).rev() {
|
2022-10-26 14:16:48 -06:00
|
|
|
let next_start = glyphs[i].start;
|
|
|
|
|
let next_end = glyphs[i].end;
|
|
|
|
|
let prev = &mut glyphs[i - 1];
|
|
|
|
|
if prev.start == next_start {
|
|
|
|
|
prev.end = next_end;
|
|
|
|
|
} else {
|
|
|
|
|
prev.end = next_start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
// Restore the buffer to save an allocation.
|
|
|
|
|
scratch.rustybuzz_buffer = Some(glyph_buffer.clear());
|
|
|
|
|
|
|
|
|
|
missing
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
|
2023-02-28 19:42:53 +01:00
|
|
|
fn shape_run(
|
2023-07-18 19:18:56 -07:00
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
glyphs: &mut Vec<ShapeGlyph>,
|
2023-03-12 10:30:20 +01:00
|
|
|
font_system: &mut FontSystem,
|
2022-10-26 15:16:06 -06:00
|
|
|
line: &str,
|
2022-11-04 09:44:54 -06:00
|
|
|
attrs_list: &AttrsList,
|
2022-10-26 15:16:06 -06:00
|
|
|
start_run: usize,
|
|
|
|
|
end_run: usize,
|
|
|
|
|
span_rtl: bool,
|
2023-07-18 19:18:56 -07:00
|
|
|
) {
|
|
|
|
|
// Re-use the previous script buffer if possible.
|
|
|
|
|
let mut scripts = {
|
|
|
|
|
let mut scripts = mem::take(&mut scratch.scripts);
|
|
|
|
|
scripts.clear();
|
|
|
|
|
scripts
|
|
|
|
|
};
|
2022-10-26 15:16:06 -06:00
|
|
|
for c in line[start_run..end_run].chars() {
|
|
|
|
|
match c.script() {
|
2023-01-04 20:03:03 -07:00
|
|
|
Script::Common | Script::Inherited | Script::Latin | Script::Unknown => (),
|
|
|
|
|
script => {
|
|
|
|
|
if !scripts.contains(&script) {
|
|
|
|
|
scripts.push(script);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-26 15:16:06 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
log::trace!(
|
|
|
|
|
" Run {:?}: '{}'",
|
|
|
|
|
&scratch.scripts,
|
|
|
|
|
&line[start_run..end_run],
|
|
|
|
|
);
|
2022-10-26 15:16:06 -06:00
|
|
|
|
2022-11-14 13:05:34 -05:00
|
|
|
let attrs = attrs_list.get_span(start_run);
|
2022-10-26 15:16:06 -06:00
|
|
|
|
2023-03-12 10:23:54 +01:00
|
|
|
let fonts = font_system.get_font_matches(attrs);
|
|
|
|
|
|
2023-03-14 00:39:50 +01:00
|
|
|
let default_families = [&attrs.family];
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &scripts);
|
2023-03-12 10:23:54 +01:00
|
|
|
|
|
|
|
|
let font = font_iter.next().expect("no default font found");
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
let glyph_start = glyphs.len();
|
|
|
|
|
let mut missing = shape_fallback(
|
|
|
|
|
scratch, glyphs, &font, line, attrs_list, start_run, end_run, span_rtl,
|
|
|
|
|
);
|
2022-10-26 15:16:06 -06:00
|
|
|
|
|
|
|
|
//TODO: improve performance!
|
|
|
|
|
while !missing.is_empty() {
|
|
|
|
|
let font = match font_iter.next() {
|
|
|
|
|
Some(some) => some,
|
|
|
|
|
None => break,
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-14 00:39:50 +01:00
|
|
|
log::trace!(
|
|
|
|
|
"Evaluating fallback with font '{}'",
|
|
|
|
|
font_iter.face_name(font.id())
|
|
|
|
|
);
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut fb_glyphs = Vec::new();
|
|
|
|
|
let fb_missing = shape_fallback(
|
|
|
|
|
scratch,
|
|
|
|
|
&mut fb_glyphs,
|
|
|
|
|
&font,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
start_run,
|
|
|
|
|
end_run,
|
|
|
|
|
span_rtl,
|
|
|
|
|
);
|
2022-10-26 15:16:06 -06:00
|
|
|
|
|
|
|
|
// Insert all matching glyphs
|
|
|
|
|
let mut fb_i = 0;
|
|
|
|
|
while fb_i < fb_glyphs.len() {
|
|
|
|
|
let start = fb_glyphs[fb_i].start;
|
|
|
|
|
let end = fb_glyphs[fb_i].end;
|
|
|
|
|
|
|
|
|
|
// Skip clusters that are not missing, or where the fallback font is missing
|
|
|
|
|
if !missing.contains(&start) || fb_missing.contains(&start) {
|
|
|
|
|
fb_i += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut missing_i = 0;
|
|
|
|
|
while missing_i < missing.len() {
|
|
|
|
|
if missing[missing_i] >= start && missing[missing_i] < end {
|
|
|
|
|
// println!("No longer missing {}", missing[missing_i]);
|
|
|
|
|
missing.remove(missing_i);
|
|
|
|
|
} else {
|
|
|
|
|
missing_i += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find prior glyphs
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut i = glyph_start;
|
2022-10-26 15:16:06 -06:00
|
|
|
while i < glyphs.len() {
|
|
|
|
|
if glyphs[i].start >= start && glyphs[i].end <= end {
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove prior glyphs
|
|
|
|
|
while i < glyphs.len() {
|
|
|
|
|
if glyphs[i].start >= start && glyphs[i].end <= end {
|
|
|
|
|
let _glyph = glyphs.remove(i);
|
|
|
|
|
// log::trace!("Removed {},{} from {}", _glyph.start, _glyph.end, i);
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while fb_i < fb_glyphs.len() {
|
|
|
|
|
if fb_glyphs[fb_i].start >= start && fb_glyphs[fb_i].end <= end {
|
|
|
|
|
let fb_glyph = fb_glyphs.remove(fb_i);
|
|
|
|
|
// log::trace!("Insert {},{} from font {} at {}", fb_glyph.start, fb_glyph.end, font_i, i);
|
|
|
|
|
glyphs.insert(i, fb_glyph);
|
|
|
|
|
i += 1;
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Debug missing font fallbacks
|
|
|
|
|
font_iter.check_missing(&line[start_run..end_run]);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
for glyph in glyphs.iter() {
|
|
|
|
|
log::trace!("'{}': {}, {}, {}, {}", &line[glyph.start..glyph.end], glyph.x_advance, glyph.y_advance, glyph.x_offset, glyph.y_offset);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
// Restore the scripts buffer.
|
|
|
|
|
scratch.scripts = scripts;
|
2022-10-26 15:16:06 -06:00
|
|
|
}
|
|
|
|
|
|
2023-04-21 20:47:02 +02:00
|
|
|
#[cfg(feature = "swash")]
|
2023-04-19 00:24:43 +02:00
|
|
|
fn shape_skip(
|
|
|
|
|
font_system: &mut FontSystem,
|
2023-07-18 19:18:56 -07:00
|
|
|
glyphs: &mut Vec<ShapeGlyph>,
|
2023-04-19 00:24:43 +02:00
|
|
|
line: &str,
|
|
|
|
|
attrs_list: &AttrsList,
|
|
|
|
|
start_run: usize,
|
|
|
|
|
end_run: usize,
|
2023-07-18 19:18:56 -07:00
|
|
|
) {
|
2023-04-19 00:24:43 +02:00
|
|
|
let attrs = attrs_list.get_span(start_run);
|
|
|
|
|
let fonts = font_system.get_font_matches(attrs);
|
|
|
|
|
|
|
|
|
|
let default_families = [&attrs.family];
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &[]);
|
2023-04-19 00:24:43 +02:00
|
|
|
|
|
|
|
|
let font = font_iter.next().expect("no default font found");
|
|
|
|
|
let font_id = font.id();
|
|
|
|
|
let font = font.as_swash();
|
|
|
|
|
|
|
|
|
|
let charmap = font.charmap();
|
2023-06-16 01:47:35 +02:00
|
|
|
let metrics = font.metrics(&[]);
|
2023-04-19 00:24:43 +02:00
|
|
|
let glyph_metrics = font.glyph_metrics(&[]).scale(1.0);
|
|
|
|
|
|
2023-06-16 02:39:54 +02:00
|
|
|
let ascent = metrics.ascent / f32::from(metrics.units_per_em);
|
|
|
|
|
let descent = metrics.descent / f32::from(metrics.units_per_em);
|
|
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
glyphs.extend(
|
|
|
|
|
line[start_run..end_run]
|
|
|
|
|
.chars()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.map(|(i, codepoint)| {
|
|
|
|
|
let glyph_id = charmap.map(codepoint);
|
|
|
|
|
let x_advance = glyph_metrics.advance_width(glyph_id);
|
|
|
|
|
|
|
|
|
|
ShapeGlyph {
|
|
|
|
|
start: i,
|
|
|
|
|
end: i + 1,
|
|
|
|
|
x_advance,
|
|
|
|
|
y_advance: 0.0,
|
|
|
|
|
x_offset: 0.0,
|
|
|
|
|
y_offset: 0.0,
|
|
|
|
|
ascent,
|
|
|
|
|
descent,
|
|
|
|
|
font_id,
|
|
|
|
|
glyph_id,
|
|
|
|
|
color_opt: attrs.color_opt,
|
|
|
|
|
metadata: attrs.metadata,
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
2023-04-19 00:24:43 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:56:53 -06:00
|
|
|
/// A shaped glyph
|
2023-07-07 21:44:21 -07:00
|
|
|
#[derive(Debug)]
|
2022-10-26 14:16:48 -06:00
|
|
|
pub struct ShapeGlyph {
|
|
|
|
|
pub start: usize,
|
|
|
|
|
pub end: usize,
|
|
|
|
|
pub x_advance: f32,
|
|
|
|
|
pub y_advance: f32,
|
|
|
|
|
pub x_offset: f32,
|
|
|
|
|
pub y_offset: f32,
|
2023-06-16 01:47:35 +02:00
|
|
|
pub ascent: f32,
|
|
|
|
|
pub descent: f32,
|
2023-03-02 18:16:57 -07:00
|
|
|
pub font_id: fontdb::ID,
|
2022-10-26 14:16:48 -06:00
|
|
|
pub glyph_id: u16,
|
|
|
|
|
pub color_opt: Option<Color>,
|
2022-12-14 09:19:03 -07:00
|
|
|
pub metadata: usize,
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ShapeGlyph {
|
2023-02-24 13:00:25 -07:00
|
|
|
fn layout(
|
|
|
|
|
&self,
|
2023-02-04 11:13:53 +01:00
|
|
|
font_size: f32,
|
2023-02-24 13:00:25 -07:00
|
|
|
x: f32,
|
|
|
|
|
y: f32,
|
|
|
|
|
w: f32,
|
|
|
|
|
level: unicode_bidi::Level,
|
|
|
|
|
) -> LayoutGlyph {
|
2022-10-26 14:16:48 -06:00
|
|
|
LayoutGlyph {
|
|
|
|
|
start: self.start,
|
|
|
|
|
end: self.end,
|
2023-06-20 06:07:24 +02:00
|
|
|
font_size,
|
|
|
|
|
font_id: self.font_id,
|
|
|
|
|
glyph_id: self.glyph_id,
|
2022-10-26 14:16:48 -06:00
|
|
|
x,
|
2023-06-20 06:07:24 +02:00
|
|
|
y,
|
2023-02-24 13:00:25 -07:00
|
|
|
w,
|
2022-12-16 16:49:29 -07:00
|
|
|
level,
|
2023-06-20 06:07:24 +02:00
|
|
|
x_offset: self.x_offset,
|
|
|
|
|
y_offset: self.y_offset,
|
2022-10-26 14:16:48 -06:00
|
|
|
color_opt: self.color_opt,
|
2022-12-14 09:19:03 -07:00
|
|
|
metadata: self.metadata,
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:56:53 -06:00
|
|
|
/// A shaped word (for word wrapping)
|
2023-07-07 21:44:21 -07:00
|
|
|
#[derive(Debug)]
|
2022-10-26 14:16:48 -06:00
|
|
|
pub struct ShapeWord {
|
|
|
|
|
pub blank: bool,
|
|
|
|
|
pub glyphs: Vec<ShapeGlyph>,
|
2022-12-17 14:37:05 -07:00
|
|
|
pub x_advance: f32,
|
|
|
|
|
pub y_advance: f32,
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ShapeWord {
|
2023-02-28 19:42:53 +01:00
|
|
|
pub fn new(
|
2023-03-12 10:30:20 +01:00
|
|
|
font_system: &mut FontSystem,
|
2022-10-26 14:16:48 -06:00
|
|
|
line: &str,
|
2022-11-04 09:44:54 -06:00
|
|
|
attrs_list: &AttrsList,
|
2022-12-16 16:49:29 -07:00
|
|
|
word_range: Range<usize>,
|
|
|
|
|
level: unicode_bidi::Level,
|
2022-10-26 14:16:48 -06:00
|
|
|
blank: bool,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping: Shaping,
|
2023-07-18 19:18:56 -07:00
|
|
|
) -> Self {
|
|
|
|
|
Self::new_in_buffer(
|
|
|
|
|
&mut ShapeBuffer::default(),
|
|
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
word_range,
|
|
|
|
|
level,
|
|
|
|
|
blank,
|
|
|
|
|
shaping,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Shape a word into a set of glyphs, using a scratch buffer.
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
|
pub fn new_in_buffer(
|
|
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
font_system: &mut FontSystem,
|
|
|
|
|
line: &str,
|
|
|
|
|
attrs_list: &AttrsList,
|
|
|
|
|
word_range: Range<usize>,
|
|
|
|
|
level: unicode_bidi::Level,
|
|
|
|
|
blank: bool,
|
|
|
|
|
shaping: Shaping,
|
2022-10-26 14:16:48 -06:00
|
|
|
) -> Self {
|
2022-12-16 16:49:29 -07:00
|
|
|
let word = &line[word_range.clone()];
|
2022-10-26 14:16:48 -06:00
|
|
|
|
|
|
|
|
log::trace!(
|
2022-10-26 15:16:06 -06:00
|
|
|
" Word{}: '{}'",
|
2022-10-26 14:16:48 -06:00
|
|
|
if blank { " BLANK" } else { "" },
|
2022-10-26 15:16:06 -06:00
|
|
|
word
|
2022-10-26 14:16:48 -06:00
|
|
|
);
|
|
|
|
|
|
2022-10-26 15:16:06 -06:00
|
|
|
let mut glyphs = Vec::new();
|
2022-12-16 16:49:29 -07:00
|
|
|
let span_rtl = level.is_rtl();
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2022-12-16 16:49:29 -07:00
|
|
|
let mut start_run = word_range.start;
|
2022-10-26 15:16:06 -06:00
|
|
|
let mut attrs = attrs_list.defaults();
|
2022-11-14 13:05:34 -05:00
|
|
|
for (egc_i, _egc) in word.grapheme_indices(true) {
|
2022-12-16 16:49:29 -07:00
|
|
|
let start_egc = word_range.start + egc_i;
|
2022-11-14 13:05:34 -05:00
|
|
|
let attrs_egc = attrs_list.get_span(start_egc);
|
2023-01-04 20:03:03 -07:00
|
|
|
if !attrs.compatible(&attrs_egc) {
|
2023-07-18 19:18:56 -07:00
|
|
|
shaping.run(
|
|
|
|
|
scratch,
|
|
|
|
|
&mut glyphs,
|
2022-10-26 15:16:06 -06:00
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
start_run,
|
|
|
|
|
start_egc,
|
2023-01-04 20:03:03 -07:00
|
|
|
span_rtl,
|
2023-07-18 19:18:56 -07:00
|
|
|
);
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2022-10-26 15:16:06 -06:00
|
|
|
start_run = start_egc;
|
|
|
|
|
attrs = attrs_egc;
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-16 16:49:29 -07:00
|
|
|
if start_run < word_range.end {
|
2023-07-18 19:18:56 -07:00
|
|
|
shaping.run(
|
|
|
|
|
scratch,
|
|
|
|
|
&mut glyphs,
|
2022-10-26 15:16:06 -06:00
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
start_run,
|
2022-12-16 16:49:29 -07:00
|
|
|
word_range.end,
|
2023-01-04 20:03:03 -07:00
|
|
|
span_rtl,
|
2023-07-18 19:18:56 -07:00
|
|
|
);
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 16:49:29 -07:00
|
|
|
let mut x_advance = 0.0;
|
|
|
|
|
let mut y_advance = 0.0;
|
|
|
|
|
for glyph in &glyphs {
|
|
|
|
|
x_advance += glyph.x_advance;
|
|
|
|
|
y_advance += glyph.y_advance;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 20:03:03 -07:00
|
|
|
Self {
|
|
|
|
|
blank,
|
|
|
|
|
glyphs,
|
|
|
|
|
x_advance,
|
|
|
|
|
y_advance,
|
|
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:56:53 -06:00
|
|
|
/// A shaped span (for bidirectional processing)
|
2023-07-07 21:44:21 -07:00
|
|
|
#[derive(Debug)]
|
2022-10-26 14:16:48 -06:00
|
|
|
pub struct ShapeSpan {
|
2022-12-16 16:49:29 -07:00
|
|
|
pub level: unicode_bidi::Level,
|
2022-10-26 14:16:48 -06:00
|
|
|
pub words: Vec<ShapeWord>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ShapeSpan {
|
2023-02-28 19:42:53 +01:00
|
|
|
pub fn new(
|
2023-03-12 10:30:20 +01:00
|
|
|
font_system: &mut FontSystem,
|
2022-10-26 14:16:48 -06:00
|
|
|
line: &str,
|
2022-11-04 09:44:54 -06:00
|
|
|
attrs_list: &AttrsList,
|
2022-12-16 16:49:29 -07:00
|
|
|
span_range: Range<usize>,
|
2022-10-26 14:16:48 -06:00
|
|
|
line_rtl: bool,
|
2022-12-16 16:49:29 -07:00
|
|
|
level: unicode_bidi::Level,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping: Shaping,
|
2023-07-18 19:18:56 -07:00
|
|
|
) -> Self {
|
|
|
|
|
Self::new_in_buffer(
|
|
|
|
|
&mut ShapeBuffer::default(),
|
|
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
span_range,
|
|
|
|
|
line_rtl,
|
|
|
|
|
level,
|
|
|
|
|
shaping,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Shape a span into a set of words, using a scratch buffer.
|
|
|
|
|
pub fn new_in_buffer(
|
|
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
font_system: &mut FontSystem,
|
|
|
|
|
line: &str,
|
|
|
|
|
attrs_list: &AttrsList,
|
|
|
|
|
span_range: Range<usize>,
|
|
|
|
|
line_rtl: bool,
|
|
|
|
|
level: unicode_bidi::Level,
|
|
|
|
|
shaping: Shaping,
|
2022-10-26 14:16:48 -06:00
|
|
|
) -> Self {
|
2022-12-16 16:49:29 -07:00
|
|
|
let span = &line[span_range.start..span_range.end];
|
2022-10-26 14:16:48 -06:00
|
|
|
|
|
|
|
|
log::trace!(
|
|
|
|
|
" Span {}: '{}'",
|
2022-12-16 16:49:29 -07:00
|
|
|
if level.is_rtl() { "RTL" } else { "LTR" },
|
2022-10-26 14:16:48 -06:00
|
|
|
span
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let mut words = Vec::new();
|
|
|
|
|
|
|
|
|
|
let mut start_word = 0;
|
|
|
|
|
for (end_lb, _) in unicode_linebreak::linebreaks(span) {
|
|
|
|
|
let mut start_lb = end_lb;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
for (i, c) in span[start_word..end_lb].char_indices().rev() {
|
|
|
|
|
// TODO: Not all whitespace characters are linebreakable, e.g. 00A0 (No-break
|
|
|
|
|
// space)
|
|
|
|
|
// https://www.unicode.org/reports/tr14/#GL
|
|
|
|
|
// https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
|
|
|
|
|
if c.is_whitespace() {
|
2022-10-26 14:16:48 -06:00
|
|
|
start_lb = start_word + i;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
} else {
|
2022-12-17 14:24:22 -07:00
|
|
|
break;
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if start_word < start_lb {
|
2023-07-18 19:18:56 -07:00
|
|
|
words.push(ShapeWord::new_in_buffer(
|
|
|
|
|
scratch,
|
2022-10-26 14:16:48 -06:00
|
|
|
font_system,
|
|
|
|
|
line,
|
2022-10-26 15:16:06 -06:00
|
|
|
attrs_list,
|
2022-12-16 16:49:29 -07:00
|
|
|
(span_range.start + start_word)..(span_range.start + start_lb),
|
|
|
|
|
level,
|
2022-10-26 14:16:48 -06:00
|
|
|
false,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping,
|
2022-10-26 14:16:48 -06:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
if start_lb < end_lb {
|
2022-12-17 14:24:22 -07:00
|
|
|
for (i, c) in span[start_lb..end_lb].char_indices() {
|
|
|
|
|
// assert!(c.is_whitespace());
|
2023-07-18 19:18:56 -07:00
|
|
|
words.push(ShapeWord::new_in_buffer(
|
|
|
|
|
scratch,
|
2022-12-17 14:24:22 -07:00
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
2023-01-04 20:03:03 -07:00
|
|
|
(span_range.start + start_lb + i)
|
|
|
|
|
..(span_range.start + start_lb + i + c.len_utf8()),
|
2022-12-17 14:24:22 -07:00
|
|
|
level,
|
|
|
|
|
true,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping,
|
2022-12-17 14:24:22 -07:00
|
|
|
));
|
|
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
start_word = end_lb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reverse glyphs in RTL lines
|
|
|
|
|
if line_rtl {
|
2022-11-15 12:26:59 -07:00
|
|
|
for word in &mut words {
|
2022-10-26 14:16:48 -06:00
|
|
|
word.glyphs.reverse();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reverse words in spans that do not match line direction
|
2022-12-16 16:49:29 -07:00
|
|
|
if line_rtl != level.is_rtl() {
|
2022-10-26 14:16:48 -06:00
|
|
|
words.reverse();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 20:03:03 -07:00
|
|
|
ShapeSpan { level, words }
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:56:53 -06:00
|
|
|
/// A shaped line (or paragraph)
|
2023-07-07 21:44:21 -07:00
|
|
|
#[derive(Debug)]
|
2022-10-26 14:16:48 -06:00
|
|
|
pub struct ShapeLine {
|
|
|
|
|
pub rtl: bool,
|
|
|
|
|
pub spans: Vec<ShapeSpan>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 12:48:37 -07:00
|
|
|
// Visual Line Ranges: (span_index, (first_word_index, first_glyph_index), (last_word_index, last_glyph_index))
|
|
|
|
|
type VlRange = (usize, (usize, usize), (usize, usize));
|
|
|
|
|
|
2023-03-12 15:33:34 -06:00
|
|
|
#[derive(Default)]
|
|
|
|
|
struct VisualLine {
|
|
|
|
|
ranges: Vec<VlRange>,
|
|
|
|
|
spaces: u32,
|
|
|
|
|
w: f32,
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 14:16:48 -06:00
|
|
|
impl ShapeLine {
|
2023-02-28 19:42:53 +01:00
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// Will panic if `line` contains more than one paragraph.
|
2023-04-19 00:24:43 +02:00
|
|
|
pub fn new(
|
|
|
|
|
font_system: &mut FontSystem,
|
|
|
|
|
line: &str,
|
|
|
|
|
attrs_list: &AttrsList,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping: Shaping,
|
2023-07-18 19:18:56 -07:00
|
|
|
) -> Self {
|
|
|
|
|
Self::new_in_buffer(
|
|
|
|
|
&mut ShapeBuffer::default(),
|
|
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
shaping,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Shape a line into a set of spans, using a scratch buffer.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// Will panic if `line` contains more than one paragraph.
|
|
|
|
|
pub fn new_in_buffer(
|
|
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
font_system: &mut FontSystem,
|
|
|
|
|
line: &str,
|
|
|
|
|
attrs_list: &AttrsList,
|
|
|
|
|
shaping: Shaping,
|
2023-04-19 00:24:43 +02:00
|
|
|
) -> Self {
|
2022-10-26 14:16:48 -06:00
|
|
|
let mut spans = Vec::new();
|
|
|
|
|
|
|
|
|
|
let bidi = unicode_bidi::BidiInfo::new(line, None);
|
|
|
|
|
let rtl = if bidi.paragraphs.is_empty() {
|
|
|
|
|
false
|
|
|
|
|
} else {
|
|
|
|
|
assert_eq!(bidi.paragraphs.len(), 1);
|
|
|
|
|
let para_info = &bidi.paragraphs[0];
|
|
|
|
|
let line_rtl = para_info.level.is_rtl();
|
|
|
|
|
|
|
|
|
|
log::trace!("Line {}: '{}'", if line_rtl { "RTL" } else { "LTR" }, line);
|
|
|
|
|
|
2022-11-26 22:39:05 -07:00
|
|
|
let line_range = para_info.range.clone();
|
2022-12-16 16:49:29 -07:00
|
|
|
let levels = Self::adjust_levels(&unicode_bidi::Paragraph::new(&bidi, para_info));
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2023-01-04 20:03:03 -07:00
|
|
|
// Find consecutive level runs. We use this to create Spans.
|
2022-12-16 16:49:29 -07:00
|
|
|
// Each span is a set of characters with equal levels.
|
|
|
|
|
let mut start = line_range.start;
|
|
|
|
|
let mut run_level = levels[start];
|
2023-07-18 19:18:56 -07:00
|
|
|
spans.reserve(line_range.end - start + 1);
|
2022-11-26 22:39:05 -07:00
|
|
|
|
2023-01-04 20:03:03 -07:00
|
|
|
for (i, &new_level) in levels
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.take(line_range.end)
|
|
|
|
|
.skip(start + 1)
|
|
|
|
|
{
|
2022-12-16 16:49:29 -07:00
|
|
|
if new_level != run_level {
|
|
|
|
|
// End of the previous run, start of a new one.
|
2023-07-18 19:18:56 -07:00
|
|
|
spans.push(ShapeSpan::new_in_buffer(
|
|
|
|
|
scratch,
|
2022-11-26 22:39:05 -07:00
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
2022-12-16 16:49:29 -07:00
|
|
|
start..i,
|
2022-10-26 14:16:48 -06:00
|
|
|
line_rtl,
|
2022-12-16 16:49:29 -07:00
|
|
|
run_level,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping,
|
2022-10-26 14:16:48 -06:00
|
|
|
));
|
2022-12-16 16:49:29 -07:00
|
|
|
start = i;
|
|
|
|
|
run_level = new_level;
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-18 19:18:56 -07:00
|
|
|
spans.push(ShapeSpan::new_in_buffer(
|
|
|
|
|
scratch,
|
2022-12-16 16:49:29 -07:00
|
|
|
font_system,
|
|
|
|
|
line,
|
|
|
|
|
attrs_list,
|
|
|
|
|
start..line_range.end,
|
|
|
|
|
line_rtl,
|
|
|
|
|
run_level,
|
2023-04-21 20:24:44 +02:00
|
|
|
shaping,
|
2022-12-16 16:49:29 -07:00
|
|
|
));
|
2022-10-26 14:16:48 -06:00
|
|
|
line_rtl
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-04 20:03:03 -07:00
|
|
|
Self { rtl, spans }
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-16 16:49:29 -07:00
|
|
|
// A modified version of first part of unicode_bidi::bidi_info::visual_run
|
2023-01-04 20:03:03 -07:00
|
|
|
fn adjust_levels(para: &unicode_bidi::Paragraph) -> Vec<unicode_bidi::Level> {
|
2022-12-16 16:49:29 -07:00
|
|
|
use unicode_bidi::BidiClass::*;
|
|
|
|
|
let text = para.info.text;
|
|
|
|
|
let levels = ¶.info.levels;
|
|
|
|
|
let original_classes = ¶.info.original_classes;
|
|
|
|
|
|
|
|
|
|
let mut levels = levels.clone();
|
|
|
|
|
let line_classes = &original_classes[..];
|
|
|
|
|
let line_levels = &mut levels[..];
|
|
|
|
|
|
|
|
|
|
// Reset some whitespace chars to paragraph level.
|
|
|
|
|
// <http://www.unicode.org/reports/tr9/#L1>
|
|
|
|
|
let mut reset_from: Option<usize> = Some(0);
|
|
|
|
|
let mut reset_to: Option<usize> = None;
|
2022-12-16 19:15:04 -07:00
|
|
|
for (i, c) in text.char_indices() {
|
2022-12-16 16:49:29 -07:00
|
|
|
match line_classes[i] {
|
|
|
|
|
// Ignored by X9
|
|
|
|
|
RLE | LRE | RLO | LRO | PDF | BN => {}
|
|
|
|
|
// Segment separator, Paragraph separator
|
|
|
|
|
B | S => {
|
|
|
|
|
assert_eq!(reset_to, None);
|
|
|
|
|
reset_to = Some(i + c.len_utf8());
|
2022-12-16 19:15:04 -07:00
|
|
|
if reset_from.is_none() {
|
2022-12-16 16:49:29 -07:00
|
|
|
reset_from = Some(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Whitespace, isolate formatting
|
|
|
|
|
WS | FSI | LRI | RLI | PDI => {
|
2022-12-16 19:15:04 -07:00
|
|
|
if reset_from.is_none() {
|
2022-12-16 16:49:29 -07:00
|
|
|
reset_from = Some(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
reset_from = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let (Some(from), Some(to)) = (reset_from, reset_to) {
|
|
|
|
|
for level in &mut line_levels[from..to] {
|
|
|
|
|
*level = para.para.level;
|
|
|
|
|
}
|
|
|
|
|
reset_from = None;
|
|
|
|
|
reset_to = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(from) = reset_from {
|
|
|
|
|
for level in &mut line_levels[from..] {
|
|
|
|
|
*level = para.para.level;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
levels
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A modified version of second part of unicode_bidi::bidi_info::visual run
|
2022-12-20 12:48:37 -07:00
|
|
|
fn reorder(&self, line_range: &[VlRange]) -> Vec<Range<usize>> {
|
2023-01-04 20:03:03 -07:00
|
|
|
let line: Vec<unicode_bidi::Level> = line_range
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(span_index, _, _)| self.spans[*span_index].level)
|
|
|
|
|
.collect();
|
2022-12-16 16:49:29 -07:00
|
|
|
// Find consecutive level runs.
|
|
|
|
|
let mut runs = Vec::new();
|
|
|
|
|
let mut start = 0;
|
|
|
|
|
let mut run_level = line[start];
|
|
|
|
|
let mut min_level = run_level;
|
|
|
|
|
let mut max_level = run_level;
|
|
|
|
|
|
|
|
|
|
for (i, &new_level) in line.iter().enumerate().skip(start + 1) {
|
|
|
|
|
if new_level != run_level {
|
|
|
|
|
// End of the previous run, start of a new one.
|
|
|
|
|
runs.push(start..i);
|
|
|
|
|
start = i;
|
|
|
|
|
run_level = new_level;
|
|
|
|
|
min_level = min(run_level, min_level);
|
|
|
|
|
max_level = max(run_level, max_level);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
runs.push(start..line.len());
|
|
|
|
|
|
|
|
|
|
let run_count = runs.len();
|
|
|
|
|
|
|
|
|
|
// Re-order the odd runs.
|
|
|
|
|
// <http://www.unicode.org/reports/tr9/#L2>
|
|
|
|
|
|
|
|
|
|
// Stop at the lowest *odd* level.
|
|
|
|
|
min_level = min_level.new_lowest_ge_rtl().expect("Level error");
|
|
|
|
|
|
|
|
|
|
while max_level >= min_level {
|
|
|
|
|
// Look for the start of a sequence of consecutive runs of max_level or higher.
|
|
|
|
|
let mut seq_start = 0;
|
|
|
|
|
while seq_start < run_count {
|
|
|
|
|
if line[runs[seq_start].start] < max_level {
|
|
|
|
|
seq_start += 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Found the start of a sequence. Now find the end.
|
|
|
|
|
let mut seq_end = seq_start + 1;
|
|
|
|
|
while seq_end < run_count {
|
|
|
|
|
if line[runs[seq_end].start] < max_level {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
seq_end += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reverse the runs within this sequence.
|
|
|
|
|
runs[seq_start..seq_end].reverse();
|
|
|
|
|
|
|
|
|
|
seq_start = seq_end;
|
|
|
|
|
}
|
|
|
|
|
max_level
|
|
|
|
|
.lower(1)
|
|
|
|
|
.expect("Lowering embedding level below zero");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runs
|
|
|
|
|
}
|
2023-01-04 20:03:03 -07:00
|
|
|
|
2023-02-22 18:31:49 -07:00
|
|
|
pub fn layout(
|
|
|
|
|
&self,
|
2023-02-04 11:13:53 +01:00
|
|
|
font_size: f32,
|
|
|
|
|
line_width: f32,
|
2023-02-22 18:31:49 -07:00
|
|
|
wrap: Wrap,
|
2023-02-23 14:23:56 -07:00
|
|
|
align: Option<Align>,
|
2023-02-22 18:31:49 -07:00
|
|
|
) -> Vec<LayoutLine> {
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut lines = Vec::with_capacity(1);
|
|
|
|
|
self.layout_to_buffer(
|
|
|
|
|
&mut ShapeBuffer::default(),
|
|
|
|
|
font_size,
|
|
|
|
|
line_width,
|
|
|
|
|
wrap,
|
|
|
|
|
align,
|
|
|
|
|
&mut lines,
|
|
|
|
|
);
|
|
|
|
|
lines
|
|
|
|
|
}
|
2022-10-31 12:04:33 -06:00
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
pub fn layout_to_buffer(
|
|
|
|
|
&self,
|
|
|
|
|
scratch: &mut ShapeBuffer,
|
|
|
|
|
font_size: f32,
|
|
|
|
|
line_width: f32,
|
|
|
|
|
wrap: Wrap,
|
|
|
|
|
align: Option<Align>,
|
|
|
|
|
layout_lines: &mut Vec<LayoutLine>,
|
|
|
|
|
) {
|
2023-02-23 14:23:56 -07:00
|
|
|
let align = align.unwrap_or({
|
|
|
|
|
if self.rtl {
|
|
|
|
|
Align::Right
|
|
|
|
|
} else {
|
|
|
|
|
Align::Left
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-16 16:49:29 -07:00
|
|
|
// This is used to create a visual line for empty lines (e.g. lines with only a <CR>)
|
2022-10-26 14:16:48 -06:00
|
|
|
let mut push_line = true;
|
2022-12-16 16:49:29 -07:00
|
|
|
|
|
|
|
|
// For each visual line a list of (span index, and range of words in that span)
|
|
|
|
|
// Note that a BiDi visual line could have multiple spans or parts of them
|
2023-02-22 18:31:49 -07:00
|
|
|
// let mut vl_range_of_spans = Vec::with_capacity(1);
|
2023-07-18 19:18:56 -07:00
|
|
|
let mut visual_lines: Vec<VisualLine> = {
|
|
|
|
|
let mut visual_lines = mem::take(&mut scratch.visual_lines);
|
|
|
|
|
visual_lines.clear();
|
|
|
|
|
visual_lines
|
|
|
|
|
};
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2023-03-13 08:50:24 -06:00
|
|
|
fn add_to_visual_line(
|
|
|
|
|
vl: &mut VisualLine,
|
|
|
|
|
span_index: usize,
|
|
|
|
|
start: (usize, usize),
|
|
|
|
|
end: (usize, usize),
|
|
|
|
|
width: f32,
|
|
|
|
|
number_of_blanks: u32,
|
|
|
|
|
) {
|
|
|
|
|
if end == start {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vl.ranges.push((span_index, start, end));
|
|
|
|
|
vl.w += width;
|
|
|
|
|
vl.spaces += number_of_blanks;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 16:49:29 -07:00
|
|
|
// This would keep the maximum number of spans that would fit on a visual line
|
|
|
|
|
// If one span is too large, this variable will hold the range of words inside that span
|
|
|
|
|
// that fits on a line.
|
2023-02-22 18:31:49 -07:00
|
|
|
// let mut current_visual_line: Vec<VlRange> = Vec::with_capacity(1);
|
|
|
|
|
let mut current_visual_line = VisualLine::default();
|
2022-12-19 11:54:19 -07:00
|
|
|
|
2022-12-20 12:48:37 -07:00
|
|
|
if wrap == Wrap::None {
|
|
|
|
|
for (span_index, span) in self.spans.iter().enumerate() {
|
2023-05-18 22:53:52 +01:00
|
|
|
let mut word_range_width = 0.;
|
|
|
|
|
let mut number_of_blanks: u32 = 0;
|
|
|
|
|
for word in span.words.iter() {
|
|
|
|
|
let word_width = font_size * word.x_advance;
|
|
|
|
|
word_range_width += word_width;
|
|
|
|
|
if word.blank {
|
|
|
|
|
number_of_blanks += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
add_to_visual_line(
|
|
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
|
|
|
|
(0, 0),
|
|
|
|
|
(span.words.len(), 0),
|
|
|
|
|
word_range_width,
|
|
|
|
|
number_of_blanks,
|
|
|
|
|
);
|
2022-12-20 12:48:37 -07:00
|
|
|
}
|
2023-01-04 20:03:03 -07:00
|
|
|
} else {
|
2022-12-20 12:48:37 -07:00
|
|
|
for (span_index, span) in self.spans.iter().enumerate() {
|
|
|
|
|
let mut word_range_width = 0.;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
let mut width_before_last_blank = 0.;
|
2023-03-12 15:33:34 -06:00
|
|
|
let mut number_of_blanks: u32 = 0;
|
2022-12-20 12:48:37 -07:00
|
|
|
|
|
|
|
|
// Create the word ranges that fits in a visual line
|
2023-01-04 20:03:03 -07:00
|
|
|
if self.rtl != span.level.is_rtl() {
|
|
|
|
|
// incongruent directions
|
2022-12-20 12:48:37 -07:00
|
|
|
let mut fitting_start = (span.words.len(), 0);
|
|
|
|
|
for (i, word) in span.words.iter().enumerate().rev() {
|
2023-03-12 15:33:34 -06:00
|
|
|
let word_width = font_size * word.x_advance;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
|
|
|
|
|
// Addition in the same order used to compute the final width, so that
|
|
|
|
|
// relayouts with that width as the `line_width` will produce the same
|
|
|
|
|
// wrapping results.
|
|
|
|
|
if current_visual_line.w + (word_range_width + word_width)
|
|
|
|
|
<= line_width
|
2023-08-25 21:17:56 -04:00
|
|
|
// Include one blank word over the width limit since it won't be
|
|
|
|
|
// counted in the final width
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
|| (word.blank
|
|
|
|
|
&& (current_visual_line.w + word_range_width) <= line_width)
|
|
|
|
|
{
|
2023-01-04 20:03:03 -07:00
|
|
|
// fits
|
2023-02-22 20:48:57 -07:00
|
|
|
if word.blank {
|
|
|
|
|
number_of_blanks += 1;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
width_before_last_blank = word_range_width;
|
2023-02-22 20:48:57 -07:00
|
|
|
}
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
word_range_width += word_width;
|
2022-12-20 12:48:37 -07:00
|
|
|
continue;
|
|
|
|
|
} else if wrap == Wrap::Glyph {
|
2022-12-19 14:54:23 -07:00
|
|
|
for (glyph_i, glyph) in word.glyphs.iter().enumerate().rev() {
|
2023-03-12 15:33:34 -06:00
|
|
|
let glyph_width = font_size * glyph.x_advance;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
if current_visual_line.w + (word_range_width + glyph_width)
|
|
|
|
|
<= line_width
|
|
|
|
|
{
|
2023-03-12 15:33:34 -06:00
|
|
|
word_range_width += glyph_width;
|
2022-12-19 14:54:23 -07:00
|
|
|
continue;
|
|
|
|
|
} else {
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
2023-01-04 20:03:03 -07:00
|
|
|
(i, glyph_i + 1),
|
|
|
|
|
fitting_start,
|
|
|
|
|
word_range_width,
|
2023-02-22 20:48:57 -07:00
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
|
|
|
|
visual_lines.push(current_visual_line);
|
|
|
|
|
current_visual_line = VisualLine::default();
|
|
|
|
|
|
2023-02-22 20:48:57 -07:00
|
|
|
number_of_blanks = 0;
|
2023-03-12 15:33:34 -06:00
|
|
|
word_range_width = glyph_width;
|
2023-01-04 20:03:03 -07:00
|
|
|
fitting_start = (i, glyph_i + 1);
|
2022-12-19 14:54:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-04 20:03:03 -07:00
|
|
|
} else {
|
|
|
|
|
// Wrap::Word
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
|
|
|
|
|
// TODO: What if the previous span ended with whitespace and the next
|
|
|
|
|
// span wraps a new line? Is that possible?
|
|
|
|
|
//
|
|
|
|
|
// TODO: This comment it outdated, the current word can be a
|
|
|
|
|
// whitespace.
|
|
|
|
|
//
|
|
|
|
|
// Current word causing a wrap is not whitespace, so we ignore the
|
|
|
|
|
// previous word if it's a whitespace
|
|
|
|
|
let trailing_blank = span
|
|
|
|
|
.words
|
|
|
|
|
.get(i + 1)
|
|
|
|
|
.map_or(false, |previous_word| previous_word.blank);
|
|
|
|
|
if trailing_blank {
|
|
|
|
|
number_of_blanks = number_of_blanks.saturating_sub(1);
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
|
|
|
|
(i + 2, 0),
|
2023-02-23 13:26:21 -07:00
|
|
|
fitting_start,
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
width_before_last_blank,
|
2023-02-23 13:26:21 -07:00
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
2023-02-23 13:26:21 -07:00
|
|
|
} else {
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
2023-02-23 13:26:21 -07:00
|
|
|
(i + 1, 0),
|
|
|
|
|
fitting_start,
|
|
|
|
|
word_range_width,
|
|
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
2023-02-22 20:48:57 -07:00
|
|
|
}
|
2023-03-12 15:33:34 -06:00
|
|
|
visual_lines.push(current_visual_line);
|
|
|
|
|
current_visual_line = VisualLine::default();
|
|
|
|
|
|
2023-02-22 20:48:57 -07:00
|
|
|
number_of_blanks = 0;
|
2022-12-19 14:54:23 -07:00
|
|
|
if word.blank {
|
|
|
|
|
word_range_width = 0.;
|
2023-03-13 13:08:35 -06:00
|
|
|
fitting_start = (i, 0);
|
2022-12-19 14:54:23 -07:00
|
|
|
} else {
|
2023-03-12 15:33:34 -06:00
|
|
|
word_range_width = word_width;
|
2023-01-04 20:03:03 -07:00
|
|
|
fitting_start = (i + 1, 0);
|
2022-12-19 14:54:23 -07:00
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
|
|
|
|
(0, 0),
|
|
|
|
|
fitting_start,
|
|
|
|
|
word_range_width,
|
|
|
|
|
number_of_blanks,
|
|
|
|
|
);
|
2023-01-04 20:03:03 -07:00
|
|
|
} else {
|
|
|
|
|
// congruent direction
|
|
|
|
|
let mut fitting_start = (0, 0);
|
2022-12-20 12:48:37 -07:00
|
|
|
for (i, word) in span.words.iter().enumerate() {
|
2023-03-12 15:33:34 -06:00
|
|
|
let word_width = font_size * word.x_advance;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
if current_visual_line.w + (word_range_width + word_width)
|
|
|
|
|
<= line_width
|
2023-08-25 21:17:56 -04:00
|
|
|
// Include one blank word over the width limit since it won't be
|
|
|
|
|
// counted in the final width.
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
|| (word.blank
|
|
|
|
|
&& (current_visual_line.w + word_range_width) <= line_width)
|
|
|
|
|
{
|
2023-01-04 20:03:03 -07:00
|
|
|
// fits
|
2023-02-22 20:48:57 -07:00
|
|
|
if word.blank {
|
|
|
|
|
number_of_blanks += 1;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
width_before_last_blank = word_range_width;
|
2023-02-22 20:48:57 -07:00
|
|
|
}
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
word_range_width += word_width;
|
2022-12-20 12:48:37 -07:00
|
|
|
continue;
|
|
|
|
|
} else if wrap == Wrap::Glyph {
|
2022-12-19 14:54:23 -07:00
|
|
|
for (glyph_i, glyph) in word.glyphs.iter().enumerate() {
|
2023-03-12 15:33:34 -06:00
|
|
|
let glyph_width = font_size * glyph.x_advance;
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
if current_visual_line.w + (word_range_width + glyph_width)
|
|
|
|
|
<= line_width
|
|
|
|
|
{
|
2023-03-12 15:33:34 -06:00
|
|
|
word_range_width += glyph_width;
|
2022-12-19 14:54:23 -07:00
|
|
|
continue;
|
|
|
|
|
} else {
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
2023-01-04 20:03:03 -07:00
|
|
|
fitting_start,
|
|
|
|
|
(i, glyph_i),
|
|
|
|
|
word_range_width,
|
2023-02-22 20:48:57 -07:00
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
|
|
|
|
visual_lines.push(current_visual_line);
|
|
|
|
|
current_visual_line = VisualLine::default();
|
|
|
|
|
|
2023-02-22 20:48:57 -07:00
|
|
|
number_of_blanks = 0;
|
2023-03-12 15:33:34 -06:00
|
|
|
word_range_width = glyph_width;
|
2022-12-19 14:54:23 -07:00
|
|
|
fitting_start = (i, glyph_i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-04 20:03:03 -07:00
|
|
|
} else {
|
|
|
|
|
// Wrap::Word
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
|
|
|
|
|
// Current word causing a wrap is not whitespace, so we ignore the
|
|
|
|
|
// previous word if it's a whitespace
|
|
|
|
|
let trailing_blank = i > 0 && span.words[i - 1].blank;
|
|
|
|
|
if trailing_blank {
|
|
|
|
|
number_of_blanks = number_of_blanks.saturating_sub(1);
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
2023-02-23 13:26:21 -07:00
|
|
|
fitting_start,
|
2023-03-13 13:08:35 -06:00
|
|
|
(i - 1, 0),
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
width_before_last_blank,
|
2023-02-23 13:26:21 -07:00
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
2023-02-23 13:26:21 -07:00
|
|
|
} else {
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
2023-02-23 13:26:21 -07:00
|
|
|
fitting_start,
|
|
|
|
|
(i, 0),
|
|
|
|
|
word_range_width,
|
|
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
2023-02-22 20:48:57 -07:00
|
|
|
}
|
2023-03-12 15:33:34 -06:00
|
|
|
visual_lines.push(current_visual_line);
|
|
|
|
|
current_visual_line = VisualLine::default();
|
2023-02-22 20:48:57 -07:00
|
|
|
number_of_blanks = 0;
|
2022-12-19 14:54:23 -07:00
|
|
|
|
|
|
|
|
if word.blank {
|
|
|
|
|
word_range_width = 0.;
|
2023-01-04 20:03:03 -07:00
|
|
|
fitting_start = (i + 1, 0);
|
2022-12-19 14:54:23 -07:00
|
|
|
} else {
|
2023-03-12 15:33:34 -06:00
|
|
|
word_range_width = word_width;
|
2022-12-19 14:54:23 -07:00
|
|
|
fitting_start = (i, 0);
|
|
|
|
|
}
|
2022-12-16 21:58:26 -07:00
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
2023-03-13 08:50:24 -06:00
|
|
|
add_to_visual_line(
|
2023-03-12 15:33:34 -06:00
|
|
|
&mut current_visual_line,
|
|
|
|
|
span_index,
|
2023-02-22 20:48:57 -07:00
|
|
|
fitting_start,
|
|
|
|
|
(span.words.len(), 0),
|
|
|
|
|
word_range_width,
|
|
|
|
|
number_of_blanks,
|
2023-03-12 15:33:34 -06:00
|
|
|
);
|
2022-12-18 00:00:29 -07:00
|
|
|
}
|
2022-12-16 16:49:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-26 20:10:38 -06:00
|
|
|
|
2023-02-22 18:31:49 -07:00
|
|
|
if !current_visual_line.ranges.is_empty() {
|
2023-03-12 15:33:34 -06:00
|
|
|
visual_lines.push(current_visual_line);
|
2022-12-16 16:49:29 -07:00
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
|
2022-12-19 11:54:19 -07:00
|
|
|
// Create the LayoutLines using the ranges inside visual lines
|
2023-08-13 21:42:33 -04:00
|
|
|
let start_x = if self.rtl { line_width } else { 0.0 };
|
|
|
|
|
let mut max_ascent: f32 = 0.;
|
|
|
|
|
let mut max_descent: f32 = 0.;
|
|
|
|
|
|
2023-03-12 15:33:34 -06:00
|
|
|
let number_of_visual_lines = visual_lines.len();
|
|
|
|
|
for (index, visual_line) in visual_lines.iter().enumerate() {
|
|
|
|
|
if visual_line.ranges.is_empty() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-02-22 18:31:49 -07:00
|
|
|
let new_order = self.reorder(&visual_line.ranges);
|
2022-12-16 16:49:29 -07:00
|
|
|
let mut glyphs = Vec::with_capacity(1);
|
2023-08-13 21:42:33 -04:00
|
|
|
let mut x = start_x;
|
|
|
|
|
let mut y = 0.;
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent = 0.;
|
|
|
|
|
max_descent = 0.;
|
2023-02-22 18:31:49 -07:00
|
|
|
let alignment_correction = match (align, self.rtl) {
|
2023-02-04 11:13:53 +01:00
|
|
|
(Align::Left, true) => line_width - visual_line.w,
|
2023-02-22 18:31:49 -07:00
|
|
|
(Align::Left, false) => 0.,
|
|
|
|
|
(Align::Right, true) => 0.,
|
2023-02-04 11:13:53 +01:00
|
|
|
(Align::Right, false) => line_width - visual_line.w,
|
|
|
|
|
(Align::Center, _) => (line_width - visual_line.w) / 2.0,
|
2023-07-07 21:31:17 -07:00
|
|
|
(Align::End, _) => line_width - visual_line.w,
|
2023-02-22 20:48:57 -07:00
|
|
|
(Align::Justified, _) => {
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
// TODO: Only certain `is_whitespace` chars are typically expanded.
|
|
|
|
|
//
|
|
|
|
|
// https://www.unicode.org/reports/tr14/#Introduction
|
|
|
|
|
// > When expanding or compressing interword space according to common
|
|
|
|
|
// > typographical practice, only the spaces marked by U+0020 SPACE and U+00A0
|
|
|
|
|
// > NO-BREAK SPACE are subject to compression, and only spaces marked by U+0020
|
|
|
|
|
// > SPACE, U+00A0 NO-BREAK SPACE, and occasionally spaces marked by U+2009 THIN
|
|
|
|
|
// > SPACE are subject to expansion. All other space characters normally have
|
|
|
|
|
// > fixed width.
|
|
|
|
|
//
|
|
|
|
|
// (also some spaces aren't followed by potential linebreaks but they could
|
|
|
|
|
// still be expanded)
|
|
|
|
|
|
2023-02-22 20:48:57 -07:00
|
|
|
// Don't justify the last line in a paragraph.
|
|
|
|
|
if visual_line.spaces > 0 && index != number_of_visual_lines - 1 {
|
2023-02-04 11:13:53 +01:00
|
|
|
(line_width - visual_line.w) / visual_line.spaces as f32
|
2023-02-22 20:48:57 -07:00
|
|
|
} else {
|
|
|
|
|
0.
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-22 18:31:49 -07:00
|
|
|
};
|
2022-12-16 16:49:29 -07:00
|
|
|
if self.rtl {
|
2023-02-22 20:48:57 -07:00
|
|
|
if align != Align::Justified {
|
|
|
|
|
x -= alignment_correction;
|
|
|
|
|
}
|
2022-12-16 16:49:29 -07:00
|
|
|
for range in new_order.iter().rev() {
|
2023-01-04 20:03:03 -07:00
|
|
|
for (
|
|
|
|
|
span_index,
|
|
|
|
|
(starting_word, starting_glyph),
|
|
|
|
|
(ending_word, ending_glyph),
|
2023-02-22 18:31:49 -07:00
|
|
|
) in visual_line.ranges[range.clone()].iter()
|
2023-01-04 20:03:03 -07:00
|
|
|
{
|
2022-12-16 16:49:29 -07:00
|
|
|
let span = &self.spans[*span_index];
|
2022-12-19 14:54:23 -07:00
|
|
|
if starting_word == ending_word {
|
2023-02-22 20:48:57 -07:00
|
|
|
let word_blank = span.words[*starting_word].blank;
|
2023-01-04 20:03:03 -07:00
|
|
|
for glyph in span.words[*starting_word].glyphs
|
|
|
|
|
[*starting_glyph..*ending_glyph]
|
|
|
|
|
.iter()
|
|
|
|
|
{
|
2023-02-04 11:13:53 +01:00
|
|
|
let x_advance = font_size * glyph.x_advance;
|
|
|
|
|
let y_advance = font_size * glyph.y_advance;
|
2023-02-22 18:31:49 -07:00
|
|
|
x -= x_advance;
|
2023-02-22 20:48:57 -07:00
|
|
|
if word_blank && align == Align::Justified {
|
|
|
|
|
x -= alignment_correction;
|
2023-02-24 13:00:25 -07:00
|
|
|
glyphs.push(glyph.layout(
|
|
|
|
|
font_size,
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
x_advance + alignment_correction,
|
|
|
|
|
span.level,
|
|
|
|
|
));
|
|
|
|
|
} else {
|
|
|
|
|
glyphs
|
|
|
|
|
.push(glyph.layout(font_size, x, y, x_advance, span.level));
|
2023-02-22 20:48:57 -07:00
|
|
|
}
|
2022-12-16 16:49:29 -07:00
|
|
|
y += y_advance;
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent = max_ascent.max(glyph.ascent);
|
|
|
|
|
max_descent = max_descent.max(glyph.descent);
|
2022-12-16 16:49:29 -07:00
|
|
|
}
|
2022-12-19 14:54:23 -07:00
|
|
|
} else {
|
2023-01-04 20:03:03 -07:00
|
|
|
for i in *starting_word..*ending_word + 1 {
|
2022-12-19 14:54:23 -07:00
|
|
|
if let Some(word) = span.words.get(i) {
|
|
|
|
|
let (g1, g2) = if i == *starting_word {
|
|
|
|
|
(*starting_glyph, word.glyphs.len())
|
|
|
|
|
} else if i == *ending_word {
|
|
|
|
|
(0, *ending_glyph)
|
|
|
|
|
} else {
|
|
|
|
|
(0, word.glyphs.len())
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-22 20:48:57 -07:00
|
|
|
let word_blank = word.blank;
|
2022-12-19 14:54:23 -07:00
|
|
|
for glyph in &word.glyphs[g1..g2] {
|
2023-02-04 11:13:53 +01:00
|
|
|
let x_advance = font_size * glyph.x_advance;
|
|
|
|
|
let y_advance = font_size * glyph.y_advance;
|
2023-02-24 13:00:25 -07:00
|
|
|
x -= x_advance;
|
2023-02-22 20:48:57 -07:00
|
|
|
if word_blank && align == Align::Justified {
|
|
|
|
|
x -= alignment_correction;
|
2023-02-24 13:00:25 -07:00
|
|
|
glyphs.push(glyph.layout(
|
|
|
|
|
font_size,
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
x_advance + alignment_correction,
|
|
|
|
|
span.level,
|
|
|
|
|
));
|
|
|
|
|
} else {
|
|
|
|
|
glyphs
|
|
|
|
|
.push(glyph.layout(
|
|
|
|
|
font_size, x, y, x_advance, span.level,
|
|
|
|
|
));
|
2022-12-19 14:54:23 -07:00
|
|
|
}
|
|
|
|
|
y += y_advance;
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent = max_ascent.max(glyph.ascent);
|
|
|
|
|
max_descent = max_descent.max(glyph.descent);
|
2022-12-19 14:54:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-23 14:23:56 -07:00
|
|
|
} else {
|
|
|
|
|
/* LTR */
|
2023-02-22 20:48:57 -07:00
|
|
|
if align != Align::Justified {
|
|
|
|
|
x += alignment_correction;
|
|
|
|
|
}
|
2022-12-16 16:49:29 -07:00
|
|
|
for range in new_order {
|
2023-01-04 20:03:03 -07:00
|
|
|
for (
|
|
|
|
|
span_index,
|
|
|
|
|
(starting_word, starting_glyph),
|
|
|
|
|
(ending_word, ending_glyph),
|
2023-02-22 18:31:49 -07:00
|
|
|
) in visual_line.ranges[range.clone()].iter()
|
2023-01-04 20:03:03 -07:00
|
|
|
{
|
2022-12-16 16:49:29 -07:00
|
|
|
let span = &self.spans[*span_index];
|
2022-12-19 14:54:23 -07:00
|
|
|
if starting_word == ending_word {
|
2023-02-22 20:48:57 -07:00
|
|
|
let word_blank = span.words[*starting_word].blank;
|
2023-01-04 20:03:03 -07:00
|
|
|
for glyph in span.words[*starting_word].glyphs
|
|
|
|
|
[*starting_glyph..*ending_glyph]
|
|
|
|
|
.iter()
|
|
|
|
|
{
|
2023-02-04 11:13:53 +01:00
|
|
|
let x_advance = font_size * glyph.x_advance;
|
|
|
|
|
let y_advance = font_size * glyph.y_advance;
|
2023-02-22 20:48:57 -07:00
|
|
|
if word_blank && align == Align::Justified {
|
2023-02-24 13:00:25 -07:00
|
|
|
glyphs.push(glyph.layout(
|
|
|
|
|
font_size,
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
x_advance + alignment_correction,
|
|
|
|
|
span.level,
|
|
|
|
|
));
|
2023-02-22 20:48:57 -07:00
|
|
|
x += alignment_correction;
|
2023-02-24 13:00:25 -07:00
|
|
|
} else {
|
|
|
|
|
glyphs
|
|
|
|
|
.push(glyph.layout(font_size, x, y, x_advance, span.level));
|
2023-02-22 20:48:57 -07:00
|
|
|
}
|
2023-02-24 13:00:25 -07:00
|
|
|
x += x_advance;
|
2022-12-16 16:49:29 -07:00
|
|
|
y += y_advance;
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent = max_ascent.max(glyph.ascent);
|
|
|
|
|
max_descent = max_descent.max(glyph.descent);
|
2022-12-16 16:49:29 -07:00
|
|
|
}
|
2022-12-19 14:54:23 -07:00
|
|
|
} else {
|
2023-01-04 20:03:03 -07:00
|
|
|
for i in *starting_word..*ending_word + 1 {
|
2022-12-19 14:54:23 -07:00
|
|
|
if let Some(word) = span.words.get(i) {
|
|
|
|
|
let (g1, g2) = if i == *starting_word {
|
|
|
|
|
(*starting_glyph, word.glyphs.len())
|
|
|
|
|
} else if i == *ending_word {
|
|
|
|
|
(0, *ending_glyph)
|
|
|
|
|
} else {
|
|
|
|
|
(0, word.glyphs.len())
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-22 20:48:57 -07:00
|
|
|
let word_blank = word.blank;
|
2022-12-19 14:54:23 -07:00
|
|
|
for glyph in &word.glyphs[g1..g2] {
|
2023-02-04 11:13:53 +01:00
|
|
|
let x_advance = font_size * glyph.x_advance;
|
|
|
|
|
let y_advance = font_size * glyph.y_advance;
|
2023-02-22 20:48:57 -07:00
|
|
|
if word_blank && align == Align::Justified {
|
2023-02-24 13:00:25 -07:00
|
|
|
glyphs.push(glyph.layout(
|
|
|
|
|
font_size,
|
|
|
|
|
x,
|
|
|
|
|
y,
|
|
|
|
|
x_advance + alignment_correction,
|
|
|
|
|
span.level,
|
|
|
|
|
));
|
2023-02-22 20:48:57 -07:00
|
|
|
x += alignment_correction;
|
2023-02-24 13:00:25 -07:00
|
|
|
} else {
|
|
|
|
|
glyphs
|
|
|
|
|
.push(glyph.layout(
|
|
|
|
|
font_size, x, y, x_advance, span.level,
|
|
|
|
|
));
|
2022-12-19 14:54:23 -07:00
|
|
|
}
|
2023-02-22 20:48:57 -07:00
|
|
|
x += x_advance;
|
2022-12-19 14:54:23 -07:00
|
|
|
y += y_advance;
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent = max_ascent.max(glyph.ascent);
|
|
|
|
|
max_descent = max_descent.max(glyph.descent);
|
2022-12-19 14:54:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-16 16:49:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-13 21:42:33 -04:00
|
|
|
|
2023-01-04 20:03:03 -07:00
|
|
|
layout_lines.push(LayoutLine {
|
Fix #134 and include a test for it.
Try to ensure that using "the width computed during an unconstrained
layout" as the width constraint during a relayout produces the same
layout. This is useful for certain UI layout algorithms.
See https://github.com/pop-os/cosmic-text/issues/134
* Instead of computing the LayoutLine width from the positioned and
aligned glyphs, we pass through width computed during line wrapping
(unless justified alignment is used, in this case we use the old
approach because the use case for measuring the width isn't really
applicable to justified text since that will just expand to the
provided width). For the produced width to later give the same
wrapping results when passed in as the `line_width` it needs to use
the same exact float arithmatic that was used to compute the width
that is compared against `line_width` when making line wrapping
choices. Passing this width through as the LayoutLine width is the
most covenient option without making more major changse to the API.
Nevertheless, I am imagining that if we get a dedicated measurement
method (i.e. that doesn't do the final positioning and alignment of
glyphs and which caches `Vec<VisualLine>`), then this width can just
be exposed there instead of preservering it in LayoutLine.
* Incidentally, this fixes
https://github.com/pop-os/cosmic-text/issues/169.
* Switch substraction from `fit_x` to checking whether potential
addition to the current line width would exceed the `line_width`. This
avoids the float error being dependent on the provided `line_width`
value.
* When eliminating trailing space from the line width, we avoid
backtracking with subtraction (which would not give the same exact
value due to float error) and instead save the previous width and use
that.
* If the previous word did not exceed the line_width, we now include a
single blank word even if it would cross the width limit since its
width won't be counted. This is necessary to get the same wrapping
behavior when re-using the measured width (which doesn't count a
single trailing blank word). Note, this whitespace logic may be
reworked anyway if <https://github.com/pop-os/cosmic-text/issues/155>
is addressed.
* Change tests to use `opt-level=1` to keep test runtime down.
* Add `fonts` folder for fonts used in tests.
* Fix an issue where a non-breaking whitespace was assumed to be the
start of a section of spaces which included characters that weren't
even whitespace.
* Add some TODOs about incongruencies between `is_whitespace`,
justification, and line breaks.
2023-08-24 22:37:32 -04:00
|
|
|
w: if align != Align::Justified {
|
|
|
|
|
visual_line.w
|
|
|
|
|
} else {
|
|
|
|
|
if self.rtl {
|
|
|
|
|
start_x - x
|
|
|
|
|
} else {
|
|
|
|
|
x
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent: max_ascent * font_size,
|
|
|
|
|
max_descent: max_descent * font_size,
|
2023-08-13 21:42:33 -04:00
|
|
|
glyphs: mem::take(&mut glyphs),
|
2023-01-04 20:03:03 -07:00
|
|
|
});
|
2022-12-16 16:49:29 -07:00
|
|
|
push_line = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if push_line {
|
2023-01-04 20:03:03 -07:00
|
|
|
layout_lines.push(LayoutLine {
|
|
|
|
|
w: 0.0,
|
2023-06-16 01:47:35 +02:00
|
|
|
max_ascent: max_ascent * font_size,
|
|
|
|
|
max_descent: max_descent * font_size,
|
2023-01-04 20:03:03 -07:00
|
|
|
glyphs: Default::default(),
|
|
|
|
|
});
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
2022-10-31 12:04:33 -06:00
|
|
|
|
2023-07-18 19:18:56 -07:00
|
|
|
// Restore the buffer to the scratch set to prevent reallocations.
|
|
|
|
|
scratch.visual_lines = visual_lines;
|
2022-10-26 14:16:48 -06:00
|
|
|
}
|
|
|
|
|
}
|