diff options
Diffstat (limited to 'components/layout/text.rs')
-rw-r--r-- | components/layout/text.rs | 351 |
1 files changed, 202 insertions, 149 deletions
diff --git a/components/layout/text.rs b/components/layout/text.rs index ac93eaed03f..c8321e9a03b 100644 --- a/components/layout/text.rs +++ b/components/layout/text.rs @@ -55,7 +55,6 @@ fn text(fragments: &LinkedList<Fragment>) -> String { text } - /// A stack-allocated object for scanning an inline flow into `TextRun`-containing `TextFragment`s. pub struct TextRunScanner { pub clump: LinkedList<Fragment>, @@ -68,16 +67,25 @@ impl TextRunScanner { } } - pub fn scan_for_runs(&mut self, - font_context: &mut LayoutFontContext, - mut fragments: LinkedList<Fragment>) - -> InlineFragments { - debug!("TextRunScanner: scanning {} fragments for text runs...", fragments.len()); + pub fn scan_for_runs( + &mut self, + font_context: &mut LayoutFontContext, + mut fragments: LinkedList<Fragment>, + ) -> InlineFragments { + debug!( + "TextRunScanner: scanning {} fragments for text runs...", + fragments.len() + ); debug_assert!(!fragments.is_empty()); // Calculate bidi embedding levels, so we can split bidirectional fragments for reordering. let text = text(&fragments); - let para_level = fragments.front().unwrap().style.writing_mode.to_bidi_level(); + let para_level = fragments + .front() + .unwrap() + .style + .writing_mode + .to_bidi_level(); let bidi_info = bidi::BidiInfo::new(&text, Some(para_level)); // Optimization: If all the text is LTR, don't bother splitting on bidi levels. @@ -106,22 +114,25 @@ impl TextRunScanner { // Create a clump. split_first_fragment_at_newline_if_necessary(&mut fragments); self.clump.append(&mut split_off_head(&mut fragments)); - while !fragments.is_empty() && self.clump - .back() - .unwrap() - .can_merge_with_fragment(fragments.front() - .unwrap()) { + while !fragments.is_empty() && self + .clump + .back() + .unwrap() + .can_merge_with_fragment(fragments.front().unwrap()) + { split_first_fragment_at_newline_if_necessary(&mut fragments); self.clump.append(&mut split_off_head(&mut fragments)); } // Flush that clump to the list of fragments we're building up. - last_whitespace = self.flush_clump_to_list(font_context, - &mut new_fragments, - &mut paragraph_bytes_processed, - bidi_levels, - last_whitespace, - &mut linebreaker); + last_whitespace = self.flush_clump_to_list( + font_context, + &mut new_fragments, + &mut paragraph_bytes_processed, + bidi_levels, + last_whitespace, + &mut linebreaker, + ); } debug!("TextRunScanner: complete."); @@ -136,25 +147,31 @@ impl TextRunScanner { /// The flow keeps track of the fragments contained by all non-leaf DOM nodes. This is necessary /// for correct painting order. Since we compress several leaf fragments here, the mapping must /// be adjusted. - fn flush_clump_to_list(&mut self, - mut font_context: &mut LayoutFontContext, - out_fragments: &mut Vec<Fragment>, - paragraph_bytes_processed: &mut usize, - bidi_levels: Option<&[bidi::Level]>, - mut last_whitespace: bool, - linebreaker: &mut Option<LineBreakLeafIter>) - -> bool { - debug!("TextRunScanner: flushing {} fragments in range", self.clump.len()); + fn flush_clump_to_list( + &mut self, + mut font_context: &mut LayoutFontContext, + out_fragments: &mut Vec<Fragment>, + paragraph_bytes_processed: &mut usize, + bidi_levels: Option<&[bidi::Level]>, + mut last_whitespace: bool, + linebreaker: &mut Option<LineBreakLeafIter>, + ) -> bool { + debug!( + "TextRunScanner: flushing {} fragments in range", + self.clump.len() + ); debug_assert!(!self.clump.is_empty()); match self.clump.front().unwrap().specific { - SpecificFragmentInfo::UnscannedText(_) => {} + SpecificFragmentInfo::UnscannedText(_) => {}, _ => { - debug_assert!(self.clump.len() == 1, - "WAT: can't coalesce non-text nodes in flush_clump_to_list()!"); + debug_assert!( + self.clump.len() == 1, + "WAT: can't coalesce non-text nodes in flush_clump_to_list()!" + ); out_fragments.push(self.clump.pop_front().unwrap()); - return false - } + return false; + }, } // Concatenate all of the transformed strings together, saving the new character indices. @@ -173,17 +190,19 @@ impl TextRunScanner { let inherited_text_style = in_fragment.style().get_inherited_text(); font_group = font_context.font_group(font_style); compression = match in_fragment.white_space() { - WhiteSpace::Normal | - WhiteSpace::Nowrap => CompressionMode::CompressWhitespaceNewline, - WhiteSpace::Pre | - WhiteSpace::PreWrap => CompressionMode::CompressNone, + WhiteSpace::Normal | WhiteSpace::Nowrap => { + CompressionMode::CompressWhitespaceNewline + }, + WhiteSpace::Pre | WhiteSpace::PreWrap => CompressionMode::CompressNone, WhiteSpace::PreLine => CompressionMode::CompressWhitespace, }; text_transform = inherited_text_style.text_transform; letter_spacing = inherited_text_style.letter_spacing; - word_spacing = inherited_text_style.word_spacing.value() - .map(|lop| lop.to_hash_key()) - .unwrap_or((Au(0), NotNan::new(0.0).unwrap())); + word_spacing = inherited_text_style + .word_spacing + .value() + .map(|lop| lop.to_hash_key()) + .unwrap_or((Au(0), NotNan::new(0.0).unwrap())); text_rendering = inherited_text_style.text_rendering; word_break = inherited_text_style.word_break; } @@ -201,7 +220,7 @@ impl TextRunScanner { SpecificFragmentInfo::UnscannedText(ref text_fragment_info) => { text = &text_fragment_info.text; selection = text_fragment_info.selection; - } + }, _ => panic!("Expected an unscanned text fragment!"), }; insertion_point = match selection { @@ -209,14 +228,16 @@ impl TextRunScanner { // `range` is the range within the current fragment. To get the range // within the text run, offset it by the length of the preceding fragments. Some(range.begin() + ByteIndex(run_info.text.len() as isize)) - } - _ => None + }, + _ => None, }; let (mut start_position, mut end_position) = (0, 0); for (byte_index, character) in text.char_indices() { if !character.is_control() { - let font = font_group.borrow_mut().find_by_codepoint(&mut font_context, character); + let font = font_group + .borrow_mut() + .find_by_codepoint(&mut font_context, character); let bidi_level = match bidi_levels { Some(levels) => levels[*paragraph_bytes_processed], @@ -230,19 +251,20 @@ impl TextRunScanner { // http://www.unicode.org/reports/tr24/#Common let script = get_script(character); let compatible_script = is_compatible(script, run_info.script); - if compatible_script && !is_specific(run_info.script) && is_specific(script) { + if compatible_script && !is_specific(run_info.script) && is_specific(script) + { run_info.script = script; } let selected = match selection { Some(range) => range.contains(ByteIndex(byte_index as isize)), - None => false + None => false, }; // Now, if necessary, flush the mapping we were building up. let flush_run = !run_info.has_font(&font) || - run_info.bidi_level != bidi_level || - !compatible_script; + run_info.bidi_level != bidi_level || + !compatible_script; let new_mapping_needed = flush_run || mapping.selected != selected; if new_mapping_needed { @@ -250,22 +272,23 @@ impl TextRunScanner { // The run info values are uninitialized at this point so // flushing an empty mapping is pointless. if end_position > 0 { - mapping.flush(&mut mappings, - &mut run_info, - &**text, - compression, - text_transform, - &mut last_whitespace, - &mut start_position, - end_position); + mapping.flush( + &mut mappings, + &mut run_info, + &**text, + compression, + text_transform, + &mut last_whitespace, + &mut start_position, + end_position, + ); } if run_info.text.len() > 0 { if flush_run { run_info.flush(&mut run_info_list, &mut insertion_point); run_info = RunInfo::new(); } - mapping = RunMapping::new(&run_info_list[..], - fragment_index); + mapping = RunMapping::new(&run_info_list[..], fragment_index); } run_info.font = font; run_info.bidi_level = bidi_level; @@ -280,14 +303,16 @@ impl TextRunScanner { } // Flush the last mapping we created for this fragment to the list. - mapping.flush(&mut mappings, - &mut run_info, - &**text, - compression, - text_transform, - &mut last_whitespace, - &mut start_position, - end_position); + mapping.flush( + &mut mappings, + &mut run_info, + &**text, + compression, + text_transform, + &mut last_whitespace, + &mut start_position, + end_position, + ); } // Push the final run info. @@ -326,20 +351,25 @@ impl TextRunScanner { } // If no font is found (including fallbacks), there's no way we can render. - let font = - run_info.font - .or_else(|| font_group.borrow_mut().first(&mut font_context)) - .expect("No font found for text run!"); - - let (run, break_at_zero) = TextRun::new(&mut *font.borrow_mut(), - run_info.text, - &options, - run_info.bidi_level, - linebreaker); - result.push((ScannedTextRun { - run: Arc::new(run), - insertion_point: run_info.insertion_point, - }, break_at_zero)) + let font = run_info + .font + .or_else(|| font_group.borrow_mut().first(&mut font_context)) + .expect("No font found for text run!"); + + let (run, break_at_zero) = TextRun::new( + &mut *font.borrow_mut(), + run_info.text, + &options, + run_info.bidi_level, + linebreaker, + ); + result.push(( + ScannedTextRun { + run: Arc::new(run), + insertion_point: run_info.insertion_point, + }, + break_at_zero, + )) } result }; @@ -349,12 +379,14 @@ impl TextRunScanner { let mut mappings = mappings.into_iter().peekable(); let mut prev_fragments_to_meld = Vec::new(); - for (logical_offset, old_fragment) in - mem::replace(&mut self.clump, LinkedList::new()).into_iter().enumerate() { + for (logical_offset, old_fragment) in mem::replace(&mut self.clump, LinkedList::new()) + .into_iter() + .enumerate() + { let mut is_first_mapping_of_this_old_fragment = true; loop { match mappings.peek() { - Some(mapping) if mapping.old_fragment_index == logical_offset => {} + Some(mapping) if mapping.old_fragment_index == logical_offset => {}, Some(_) | None => { if is_first_mapping_of_this_old_fragment { // There were no mappings for this unscanned fragment. Transfer its @@ -365,13 +397,15 @@ impl TextRunScanner { prev_fragments_to_meld.push(old_fragment); } break; - } + }, }; let mapping = mappings.next().unwrap(); let (scanned_run, break_at_zero) = runs[mapping.text_run_index].clone(); - let mut byte_range = Range::new(ByteIndex(mapping.byte_range.begin() as isize), - ByteIndex(mapping.byte_range.length() as isize)); + let mut byte_range = Range::new( + ByteIndex(mapping.byte_range.begin() as isize), + ByteIndex(mapping.byte_range.length() as isize), + ); let mut flags = ScannedTextFlags::empty(); if !break_at_zero && mapping.byte_range.begin() == 0 { @@ -381,31 +415,34 @@ impl TextRunScanner { } let text_size = old_fragment.border_box.size; - let requires_line_break_afterward_if_wrapping_on_newlines = - scanned_run.run.text[mapping.byte_range.begin()..mapping.byte_range.end()] + let requires_line_break_afterward_if_wrapping_on_newlines = scanned_run.run.text + [mapping.byte_range.begin()..mapping.byte_range.end()] .ends_with('\n'); if requires_line_break_afterward_if_wrapping_on_newlines { byte_range.extend_by(ByteIndex(-1)); // Trim the '\n' - flags.insert(ScannedTextFlags::REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES); + flags.insert( + ScannedTextFlags::REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES, + ); } if mapping.selected { flags.insert(ScannedTextFlags::SELECTED); } - let insertion_point = if mapping.contains_insertion_point(scanned_run.insertion_point) { - scanned_run.insertion_point - } else { - None - }; + let insertion_point = + if mapping.contains_insertion_point(scanned_run.insertion_point) { + scanned_run.insertion_point + } else { + None + }; let mut new_text_fragment_info = Box::new(ScannedTextFragmentInfo::new( scanned_run.run, byte_range, text_size, insertion_point, - flags + flags, )); let new_metrics = new_text_fragment_info.run.metrics_for_range(&byte_range); @@ -415,20 +452,23 @@ impl TextRunScanner { let mut new_fragment = old_fragment.transform( bounding_box_size, - SpecificFragmentInfo::ScannedText(new_text_fragment_info)); + SpecificFragmentInfo::ScannedText(new_text_fragment_info), + ); let is_last_mapping_of_this_old_fragment = match mappings.peek() { Some(mapping) if mapping.old_fragment_index == logical_offset => false, - _ => true + _ => true, }; if let Some(ref mut context) = new_fragment.inline_context { for node in &mut context.nodes { if !is_last_mapping_of_this_old_fragment { - node.flags.remove(InlineFragmentNodeFlags::LAST_FRAGMENT_OF_ELEMENT); + node.flags + .remove(InlineFragmentNodeFlags::LAST_FRAGMENT_OF_ELEMENT); } if !is_first_mapping_of_this_old_fragment { - node.flags.remove(InlineFragmentNodeFlags::FIRST_FRAGMENT_OF_ELEMENT); + node.flags + .remove(InlineFragmentNodeFlags::FIRST_FRAGMENT_OF_ELEMENT); } } } @@ -447,14 +487,17 @@ impl TextRunScanner { } #[inline] -fn bounding_box_for_run_metrics(metrics: &RunMetrics, writing_mode: WritingMode) - -> LogicalSize<Au> { +fn bounding_box_for_run_metrics( + metrics: &RunMetrics, + writing_mode: WritingMode, +) -> LogicalSize<Au> { // TODO: When the text-orientation property is supported, the block and inline directions may // be swapped for horizontal glyphs in vertical lines. LogicalSize::new( writing_mode, metrics.bounding_box.size.width, - metrics.bounding_box.size.height) + metrics.bounding_box.size.height, + ) } /// Returns the metrics of the font represented by the given `FontStyleStruct`. @@ -465,8 +508,10 @@ fn bounding_box_for_run_metrics(metrics: &RunMetrics, writing_mode: WritingMode) /// /// Panics if no font can be found for the given font style. #[inline] -pub fn font_metrics_for_style(mut font_context: &mut LayoutFontContext, style: ::ServoArc<FontStyleStruct>) - -> FontMetrics { +pub fn font_metrics_for_style( + mut font_context: &mut LayoutFontContext, + style: ::ServoArc<FontStyleStruct>, +) -> FontMetrics { let font_group = font_context.font_group(style); let font = font_group.borrow_mut().first(&mut font_context); let font = font.as_ref().unwrap().borrow(); @@ -480,13 +525,13 @@ pub fn line_height_from_style(style: &ComputedValues, metrics: &FontMetrics) -> match style.get_inherited_text().line_height { LineHeight::Normal => Au::from(metrics.line_gap), LineHeight::Number(l) => font_size.scale_by(l.0), - LineHeight::Length(l) => Au::from(l) + LineHeight::Length(l) => Au::from(l), } } fn split_first_fragment_at_newline_if_necessary(fragments: &mut LinkedList<Fragment>) { if fragments.is_empty() { - return + return; } let new_fragment = { @@ -501,45 +546,46 @@ fn split_first_fragment_at_newline_if_necessary(fragments: &mut LinkedList<Fragm let unscanned_text_fragment_info = match first_fragment.specific { SpecificFragmentInfo::UnscannedText(ref mut unscanned_text_fragment_info) => { unscanned_text_fragment_info - } + }, _ => return, }; let position = match unscanned_text_fragment_info.text.find('\n') { Some(position) if position < unscanned_text_fragment_info.text.len() - 1 => { position - } + }, Some(_) | None => return, }; - string_before = - unscanned_text_fragment_info.text[..(position + 1)].to_owned(); - unscanned_text_fragment_info.text = - unscanned_text_fragment_info.text[(position + 1)..].to_owned().into_boxed_str(); + string_before = unscanned_text_fragment_info.text[..(position + 1)].to_owned(); + unscanned_text_fragment_info.text = unscanned_text_fragment_info.text[(position + 1)..] + .to_owned() + .into_boxed_str(); let offset = ByteIndex(string_before.len() as isize); match unscanned_text_fragment_info.selection { Some(ref mut selection) if selection.begin() >= offset => { // Selection is entirely in the second fragment. selection_before = None; selection.shift_by(-offset); - } + }, Some(ref mut selection) if selection.end() > offset => { // Selection is split across two fragments. selection_before = Some(Range::new(selection.begin(), offset)); *selection = Range::new(ByteIndex(0), selection.end() - offset); - } + }, _ => { // Selection is entirely in the first fragment. selection_before = unscanned_text_fragment_info.selection; unscanned_text_fragment_info.selection = None; - } + }, }; } first_fragment.transform( first_fragment.border_box.size, - SpecificFragmentInfo::UnscannedText(Box::new( - UnscannedTextFragmentInfo::new(string_before.into_boxed_str(), selection_before) - )) + SpecificFragmentInfo::UnscannedText(Box::new(UnscannedTextFragmentInfo::new( + string_before.into_boxed_str(), + selection_before, + ))), ) }; @@ -575,9 +621,7 @@ impl RunInfo { /// /// * `insertion_point`: The position of the insertion point, in characters relative to the start /// of this text run. - fn flush(mut self, - list: &mut Vec<RunInfo>, - insertion_point: &mut Option<ByteIndex>) { + fn flush(mut self, list: &mut Vec<RunInfo>, insertion_point: &mut Option<ByteIndex>) { if let Some(idx) = *insertion_point { let char_len = ByteIndex(self.text.len() as isize); if idx <= char_len { @@ -617,8 +661,7 @@ struct RunMapping { impl RunMapping { /// Given the current set of text runs, creates a run mapping for the next fragment. /// `run_info_list` describes the set of runs we've seen already. - fn new(run_info_list: &[RunInfo], fragment_index: usize) - -> RunMapping { + fn new(run_info_list: &[RunInfo], fragment_index: usize) -> RunMapping { RunMapping { byte_range: Range::new(0, 0), old_fragment_index: fragment_index, @@ -629,27 +672,36 @@ impl RunMapping { /// Flushes this run mapping to the list. `run_info` describes the text run that we're /// currently working on. `text` refers to the text of this fragment. - fn flush(mut self, - mappings: &mut Vec<RunMapping>, - run_info: &mut RunInfo, - text: &str, - compression: CompressionMode, - text_transform: TextTransform, - last_whitespace: &mut bool, - start_position: &mut usize, - end_position: usize) { + fn flush( + mut self, + mappings: &mut Vec<RunMapping>, + run_info: &mut RunInfo, + text: &str, + compression: CompressionMode, + text_transform: TextTransform, + last_whitespace: &mut bool, + start_position: &mut usize, + end_position: usize, + ) { let was_empty = *start_position == end_position; let old_byte_length = run_info.text.len(); - *last_whitespace = util::transform_text(&text[(*start_position)..end_position], - compression, - *last_whitespace, - &mut run_info.text); + *last_whitespace = util::transform_text( + &text[(*start_position)..end_position], + compression, + *last_whitespace, + &mut run_info.text, + ); // Account for `text-transform`. (Confusingly, this is not handled in "text // transformation" above, but we follow Gecko in the naming.) let is_first_run = *start_position == 0; - apply_style_transform_if_necessary(&mut run_info.text, old_byte_length, text_transform, - *last_whitespace, is_first_run); + apply_style_transform_if_necessary( + &mut run_info.text, + old_byte_length, + text_transform, + *last_whitespace, + is_first_run, + ); *start_position = end_position; let new_byte_length = run_info.text.len(); @@ -674,38 +726,39 @@ impl RunMapping { fn contains_insertion_point(&self, insertion_point: Option<ByteIndex>) -> bool { match insertion_point.map(ByteIndex::to_usize) { None => false, - Some(idx) => self.byte_range.begin() <= idx && idx <= self.byte_range.end() + Some(idx) => self.byte_range.begin() <= idx && idx <= self.byte_range.end(), } } } - /// Accounts for `text-transform`. /// /// FIXME(#4311, pcwalton): Title-case mapping can change length of the string; /// case mapping should be language-specific; `full-width`; /// use graphemes instead of characters. -fn apply_style_transform_if_necessary(string: &mut String, - first_character_position: usize, - text_transform: TextTransform, - last_whitespace: bool, - is_first_run: bool) { +fn apply_style_transform_if_necessary( + string: &mut String, + first_character_position: usize, + text_transform: TextTransform, + last_whitespace: bool, + is_first_run: bool, +) { match text_transform { - TextTransform::None => {} + TextTransform::None => {}, TextTransform::Uppercase => { let original = string[first_character_position..].to_owned(); string.truncate(first_character_position); for ch in original.chars().flat_map(|ch| ch.to_uppercase()) { string.push(ch); } - } + }, TextTransform::Lowercase => { let original = string[first_character_position..].to_owned(); string.truncate(first_character_position); for ch in original.chars().flat_map(|ch| ch.to_lowercase()) { string.push(ch); } - } + }, TextTransform::Capitalize => { let original = string[first_character_position..].to_owned(); string.truncate(first_character_position); @@ -719,7 +772,7 @@ fn apply_style_transform_if_necessary(string: &mut String, if capitalize_next_letter && character.is_alphabetic() { string.push(character.to_uppercase().next().unwrap()); capitalize_next_letter = false; - continue + continue; } string.push(character); @@ -729,7 +782,7 @@ fn apply_style_transform_if_necessary(string: &mut String, capitalize_next_letter = true } } - } + }, } } |