From 08caf7412fc3e86a76c47876eb9bfab9804f2182 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Thu, 24 Mar 2016 16:06:12 -0700 Subject: [PATCH] Convert ScannedTextFragment fields into bitflags --- components/layout/display_list_builder.rs | 4 +- components/layout/fragment.rs | 50 ++++++++++++++--------- components/layout/inline.rs | 2 +- components/layout/text.rs | 14 +++++-- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/components/layout/display_list_builder.rs b/components/layout/display_list_builder.rs index a55a1f2bdc3..d539bed9805 100644 --- a/components/layout/display_list_builder.rs +++ b/components/layout/display_list_builder.rs @@ -930,7 +930,7 @@ impl FragmentDisplayListBuilding for Fragment { // Draw a highlighted background if the text is selected. // // TODO: Allow non-text fragments to be selected too. - if scanned_text_fragment_info.selected { + if scanned_text_fragment_info.selected() { state.add_display_item( DisplayItem::SolidColorClass(box SolidColorDisplayItem { base: BaseDisplayItem::new(stacking_relative_border_box, @@ -1116,7 +1116,7 @@ impl FragmentDisplayListBuilding for Fragment { // // NB: According to CSS-BACKGROUNDS, text shadows render in *reverse* order (front // to back). - let text_color = if text_fragment.selected { + let text_color = if text_fragment.selected() { SELECTION_FOREGROUND_COLOR } else { self.style().get_color().color diff --git a/components/layout/fragment.rs b/components/layout/fragment.rs index a5197af7d46..31647c387ac 100644 --- a/components/layout/fragment.rs +++ b/components/layout/fragment.rs @@ -657,9 +657,6 @@ pub struct ScannedTextFragmentInfo { /// The position of the insertion point in characters, if any. pub insertion_point: Option, - /// Is this fragment selected? - pub selected: bool, - /// The range within the above text run that this represents. pub range: Range, @@ -668,9 +665,18 @@ pub struct ScannedTextFragmentInfo { /// performing incremental reflow. pub range_end_including_stripped_whitespace: CharIndex, - /// Whether a line break is required after this fragment if wrapping on newlines (e.g. if - /// `white-space: pre` is in effect). - pub requires_line_break_afterward_if_wrapping_on_newlines: bool, + pub flags: ScannedTextFlags, +} + +bitflags! { + flags ScannedTextFlags: u8 { + /// Whether a line break is required after this fragment if wrapping on newlines (e.g. if + /// `white-space: pre` is in effect). + const REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES = 0x01, + + /// Is this fragment selected? + const SELECTED = 0x02, + } } impl ScannedTextFragmentInfo { @@ -679,20 +685,25 @@ impl ScannedTextFragmentInfo { range: Range, content_size: LogicalSize, insertion_point: Option, - selected: bool, - requires_line_break_afterward_if_wrapping_on_newlines: bool) + flags: ScannedTextFlags) -> ScannedTextFragmentInfo { ScannedTextFragmentInfo { run: run, range: range, insertion_point: insertion_point, - selected: selected, content_size: content_size, range_end_including_stripped_whitespace: range.end(), - requires_line_break_afterward_if_wrapping_on_newlines: - requires_line_break_afterward_if_wrapping_on_newlines, + flags: flags, } } + + pub fn requires_line_break_afterward_if_wrapping_on_newlines(&self) -> bool { + self.flags.contains(REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES) + } + + pub fn selected(&self) -> bool { + self.flags.contains(SELECTED) + } } /// Describes how to split a fragment. This is used during line breaking as part of the return @@ -866,16 +877,17 @@ impl Fragment { let size = LogicalSize::new(self.style.writing_mode, split.inline_size, self.border_box.size.block); - let requires_line_break_afterward_if_wrapping_on_newlines = - self.requires_line_break_afterward_if_wrapping_on_newlines(); + let flags = match self.specific { + SpecificFragmentInfo::ScannedText(ref info) => info.flags, + _ => ScannedTextFlags::empty() + }; // FIXME(pcwalton): This should modify the insertion point as necessary. let info = box ScannedTextFragmentInfo::new( text_run, split.range, size, None, - false, - requires_line_break_afterward_if_wrapping_on_newlines); + flags); self.transform(size, SpecificFragmentInfo::ScannedText(info)) } @@ -1665,9 +1677,9 @@ impl Fragment { this_info.range.extend_to(other_info.range_end_including_stripped_whitespace); this_info.content_size.inline = this_info.run.metrics_for_range(&this_info.range).advance_width; - this_info.requires_line_break_afterward_if_wrapping_on_newlines = - this_info.requires_line_break_afterward_if_wrapping_on_newlines || - other_info.requires_line_break_afterward_if_wrapping_on_newlines; + if other_info.requires_line_break_afterward_if_wrapping_on_newlines() { + this_info.flags.insert(REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES); + } self.border_padding.inline_end = next_fragment.border_padding.inline_end; self.border_box.size.inline = this_info.content_size.inline + self.border_padding.inline_start_end(); @@ -2231,7 +2243,7 @@ impl Fragment { pub fn requires_line_break_afterward_if_wrapping_on_newlines(&self) -> bool { match self.specific { SpecificFragmentInfo::ScannedText(ref scanned_text) => { - scanned_text.requires_line_break_afterward_if_wrapping_on_newlines + scanned_text.requires_line_break_afterward_if_wrapping_on_newlines() } _ => false, } diff --git a/components/layout/inline.rs b/components/layout/inline.rs index 6f102f268fa..2ede4ba9872 100644 --- a/components/layout/inline.rs +++ b/components/layout/inline.rs @@ -354,7 +354,7 @@ impl LineBreaker { let need_to_merge = match (&mut result.specific, &candidate.specific) { (&mut SpecificFragmentInfo::ScannedText(ref mut result_info), &SpecificFragmentInfo::ScannedText(ref candidate_info)) => { - result_info.selected == candidate_info.selected && + result_info.selected() == candidate_info.selected() && util::arc_ptr_eq(&result_info.run, &candidate_info.run) && inline_contexts_are_equal(&result.inline_context, &candidate.inline_context) diff --git a/components/layout/text.rs b/components/layout/text.rs index 4e339c28a2c..6cc7adf7424 100644 --- a/components/layout/text.rs +++ b/components/layout/text.rs @@ -7,7 +7,8 @@ #![deny(unsafe_code)] use app_units::Au; -use fragment::{Fragment, ScannedTextFragmentInfo, SpecificFragmentInfo, UnscannedTextFragmentInfo}; +use fragment::{Fragment, REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES, ScannedTextFlags}; +use fragment::{ScannedTextFragmentInfo, SELECTED, SpecificFragmentInfo, UnscannedTextFragmentInfo}; use gfx::font::{DISABLE_KERNING_SHAPING_FLAG, FontMetrics, IGNORE_LIGATURES_SHAPING_FLAG}; use gfx::font::{RTL_FLAG, RunMetrics, ShapingFlags, ShapingOptions}; use gfx::font_context::FontContext; @@ -344,13 +345,20 @@ impl TextRunScanner { } let text_size = old_fragment.border_box.size; + + let mut flags = ScannedTextFlags::empty(); + if mapping.selected { + flags.insert(SELECTED); + } + if requires_line_break_afterward_if_wrapping_on_newlines { + flags.insert(REQUIRES_LINE_BREAK_AFTERWARD_IF_WRAPPING_ON_NEWLINES); + } let mut new_text_fragment_info = box ScannedTextFragmentInfo::new( scanned_run.run, mapping.char_range, text_size, scanned_run.insertion_point, - mapping.selected, - requires_line_break_afterward_if_wrapping_on_newlines); + flags); let new_metrics = new_text_fragment_info.run.metrics_for_range(&mapping.char_range); let writing_mode = old_fragment.style.writing_mode;