Add the ability to split lines from the middle of an already-existing flow operation

This commit is contained in:
David Wendt 2020-03-13 01:10:52 -04:00
parent 7e7ca88c3d
commit 4739e08c11
3 changed files with 29 additions and 9 deletions

View File

@ -331,9 +331,12 @@ impl<'gc> EditText<'gc> {
breakpoints.push(break_base);
}
for breakpoint in
font.split_wrapped_lines(natural_line, height, self.line_width())
{
for breakpoint in font.split_wrapped_lines(
natural_line,
height,
self.line_width(),
Twips::from_pixels(0.0),
) {
breakpoints.push(break_base + breakpoint);
}

View File

@ -154,10 +154,10 @@ impl<'gc> Font<'gc> {
}
/// Measure a particular string's metrics (width and height).
pub fn measure(self, text: &str, height: Twips) -> (Twips, Twips) {
pub fn measure(self, text: &str, font_size: Twips) -> (Twips, Twips) {
let mut size = (Twips::new(0), Twips::new(0));
self.evaluate(text, Default::default(), height, |transform, _glyph| {
self.evaluate(text, Default::default(), font_size, |transform, _glyph| {
let tx = transform.matrix.tx;
let ty = transform.matrix.ty;
size.0 = std::cmp::max(size.0, tx);
@ -172,13 +172,26 @@ impl<'gc> Font<'gc> {
///
/// This function assumes only `" "` is valid whitespace to split words on,
/// and will not attempt to break words that are longer than `width`.
pub fn split_wrapped_lines(self, text: &str, height: Twips, width: Twips) -> Vec<usize> {
///
/// The given `offset` determines the start of the initial line.
///
/// TODO: This function and, more generally, this entire file will need to
/// be internationalized to implement AS3 `flash.text.engine`.
pub fn split_wrapped_lines(
self,
text: &str,
font_size: Twips,
width: Twips,
offset: Twips,
) -> Vec<usize> {
let mut result = vec![];
let mut current_width = width;
let mut current_word = text;
let mut current_width = width
.checked_sub(offset)
.unwrap_or_else(|| Twips::from_pixels(0.0));
let mut current_word = &text[0..0];
for word in text.split(' ') {
let measure = self.measure(word, height);
let measure = self.measure(word, font_size);
let line_start = current_word.as_ptr() as usize - text.as_ptr() as usize;
let line_end = if (line_start + current_word.len() + 1) < text.len() {
line_start + current_word.len() + 1

View File

@ -95,6 +95,10 @@ impl Twips {
pub fn to_pixels(self) -> f64 {
f64::from(self.0) / Self::TWIPS_PER_PIXEL
}
pub fn checked_sub(self, other: Self) -> Option<Self> {
Some(Self(self.0.checked_sub(other.0)?))
}
}
impl std::ops::Add for Twips {