Skip to content

Commit

Permalink
[gfx] [layout] [style] Upgrade unicode-bidi to 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
behnam committed May 23, 2017
1 parent 594479f commit 14c524d
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 34 deletions.
28 changes: 21 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/gfx/Cargo.toml
Expand Up @@ -37,6 +37,7 @@ smallvec = "0.3"
style = {path = "../style"}
style_traits = {path = "../style_traits"}
time = "0.1.12"
unicode-bidi = {version = "0.3", features = ["with_serde"]}
unicode-script = {version = "0.1", features = ["harfbuzz"]}
webrender_traits = {git = "https://github.com/servo/webrender", features = ["ipc"]}
xi-unicode = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions components/gfx/lib.rs
Expand Up @@ -69,6 +69,7 @@ extern crate smallvec;
extern crate style;
extern crate style_traits;
extern crate time;
extern crate unicode_bidi;
extern crate unicode_script;
extern crate webrender_traits;
extern crate xi_unicode;
Expand Down
7 changes: 4 additions & 3 deletions components/gfx/text/text_run.rs
Expand Up @@ -13,6 +13,7 @@ use std::slice::Iter;
use std::sync::Arc;
use style::str::char_is_whitespace;
use text::glyph::{ByteIndex, GlyphStore};
use unicode_bidi as bidi;
use webrender_traits;
use xi_unicode::LineBreakIterator;

Expand All @@ -32,7 +33,7 @@ pub struct TextRun {
pub font_key: webrender_traits::FontKey,
/// The glyph runs that make up this text run.
pub glyphs: Arc<Vec<GlyphRun>>,
pub bidi_level: u8,
pub bidi_level: bidi::Level,
pub extra_word_spacing: Au,
}

Expand Down Expand Up @@ -179,7 +180,7 @@ impl<'a> Iterator for CharacterSliceIterator<'a> {
}

impl<'a> TextRun {
pub fn new(font: &mut Font, text: String, options: &ShapingOptions, bidi_level: u8) -> TextRun {
pub fn new(font: &mut Font, text: String, options: &ShapingOptions, bidi_level: bidi::Level) -> TextRun {
let glyphs = TextRun::break_and_shape(font, &text, options);
TextRun {
text: Arc::new(text),
Expand Down Expand Up @@ -340,7 +341,7 @@ impl<'a> TextRun {
pub fn natural_word_slices_in_visual_order(&'a self, range: &Range<ByteIndex>)
-> NaturalWordSliceIterator<'a> {
// Iterate in reverse order if bidi level is RTL.
let reverse = self.bidi_level % 2 == 1;
let reverse = self.bidi_level.is_rtl();

let index = if reverse {
match self.index_of_first_glyph_run_containing(range.end() - ByteIndex(1)) {
Expand Down
2 changes: 1 addition & 1 deletion components/layout/Cargo.toml
Expand Up @@ -43,6 +43,6 @@ servo_url = {path = "../url"}
smallvec = "0.3"
style = {path = "../style"}
style_traits = {path = "../style_traits"}
unicode-bidi = "0.2"
unicode-bidi = {version = "0.3", features = ["with_serde"]}
unicode-script = {version = "0.1", features = ["harfbuzz"]}
webrender_traits = {git = "https://github.com/servo/webrender", features = ["ipc"]}
27 changes: 15 additions & 12 deletions components/layout/inline.rs
Expand Up @@ -36,7 +36,7 @@ use style::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
use style::properties::{longhands, ServoComputedValues};
use style::servo::restyle_damage::{BUBBLE_ISIZES, REFLOW, REFLOW_OUT_OF_FLOW, REPOSITION, RESOLVE_GENERATED_CONTENT};
use text;
use unicode_bidi;
use unicode_bidi as bidi;

/// `Line`s are represented as offsets into the child list, rather than
/// as an object that "owns" fragments. Choosing a different set of line
Expand Down Expand Up @@ -95,7 +95,7 @@ pub struct Line {
/// The bidirectional embedding level runs for this line, in visual order.
///
/// Can be set to `None` if the line is 100% left-to-right.
pub visual_runs: Option<Vec<(Range<FragmentIndex>, u8)>>,
pub visual_runs: Option<Vec<(Range<FragmentIndex>, bidi::Level)>>,

/// The bounds are the exact position and extents of the line with respect
/// to the parent box.
Expand Down Expand Up @@ -293,22 +293,25 @@ impl LineBreaker {
// (because we split fragments on level run boundaries during flow
// construction), so we can build a level array with just one entry per
// fragment.
let levels: Vec<u8> = self.new_fragments.iter().map(|fragment| match fragment.specific {
SpecificFragmentInfo::ScannedText(ref info) => info.run.bidi_level,
_ => para_level
}).collect();
let levels: Vec<bidi::Level> = self.new_fragments.iter().map(
|fragment| match fragment.specific {
SpecificFragmentInfo::ScannedText(ref info) => info.run.bidi_level,
_ => para_level
}
).collect();

let mut lines = mem::replace(&mut self.lines, Vec::new());

// If everything is LTR, don't bother with reordering.
let has_rtl = levels.iter().cloned().any(unicode_bidi::is_rtl);

if has_rtl {
if bidi::level::has_rtl(&levels) {
// Compute and store the visual ordering of the fragments within the
// line.
for line in &mut lines {
let range = line.range.begin().to_usize()..line.range.end().to_usize();
let runs = unicode_bidi::visual_runs(range, &levels);
// FIXME: Update to use BidiInfo::visual_runs, as this algorithm needs access to
// the original text and original BidiClass of its characters.
#[allow(deprecated)]
let runs = bidi::deprecated::visual_runs(range, &levels);
line.visual_runs = Some(runs.iter().map(|run| {
let start = FragmentIndex(run.start as isize);
let len = FragmentIndex(run.len() as isize);
Expand Down Expand Up @@ -957,11 +960,11 @@ impl InlineFlow {
let (range, level) = match line.visual_runs {
Some(ref runs) if is_ltr => runs[run_idx],
Some(ref runs) => runs[run_count - run_idx - 1], // reverse order for RTL runs
None => (line.range, 0)
None => (line.range, bidi::Level::ltr())
};
// If the bidi embedding direction is opposite the layout direction, lay out this
// run in reverse order.
let reverse = unicode_bidi::is_ltr(level) != is_ltr;
let reverse = level.is_ltr() != is_ltr;
let fragment_indices = if reverse {
(range.end().get() - 1..range.begin().get() - 1).step_by(-1)
} else {
Expand Down
16 changes: 8 additions & 8 deletions components/layout/text.rs
Expand Up @@ -28,7 +28,7 @@ use style::computed_values::{word_break, white_space};
use style::logical_geometry::{LogicalSize, WritingMode};
use style::properties::ServoComputedValues;
use style::properties::style_structs;
use unicode_bidi::{is_rtl, process_text};
use unicode_bidi as bidi;
use unicode_script::{Script, get_script};

/// Returns the concatenated text of a list of unscanned text fragments.
Expand Down Expand Up @@ -74,10 +74,10 @@ impl TextRunScanner {
// 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 bidi_info = process_text(&text, Some(para_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.
let bidi_levels = if bidi_info.levels.iter().cloned().any(is_rtl) {
let bidi_levels = if bidi_info.has_rtl() {
Some(&bidi_info.levels[..])
} else {
None
Expand Down Expand Up @@ -126,7 +126,7 @@ impl TextRunScanner {
font_context: &mut FontContext,
out_fragments: &mut Vec<Fragment>,
paragraph_bytes_processed: &mut usize,
bidi_levels: Option<&[u8]>,
bidi_levels: Option<&[bidi::Level]>,
mut last_whitespace: bool)
-> bool {
debug!("TextRunScanner: flushing {} fragments in range", self.clump.len());
Expand Down Expand Up @@ -211,7 +211,7 @@ impl TextRunScanner {

let bidi_level = match bidi_levels {
Some(levels) => levels[*paragraph_bytes_processed],
None => 0
None => bidi::Level::ltr(),
};

// Break the run if the new character has a different explicit script than the
Expand Down Expand Up @@ -310,7 +310,7 @@ impl TextRunScanner {
run_info_list.into_iter().map(|run_info| {
let mut options = options;
options.script = run_info.script;
if is_rtl(run_info.bidi_level) {
if run_info.bidi_level.is_rtl() {
options.flags.insert(RTL_FLAG);
}
let mut font = fontgroup.fonts.get(run_info.font_index).unwrap().borrow_mut();
Expand Down Expand Up @@ -522,7 +522,7 @@ struct RunInfo {
/// The index of the applicable font in the font group.
font_index: usize,
/// The bidirection embedding level of this text run.
bidi_level: u8,
bidi_level: bidi::Level,
/// The Unicode script property of this text run.
script: Script,
}
Expand All @@ -533,7 +533,7 @@ impl RunInfo {
text: String::new(),
insertion_point: None,
font_index: 0,
bidi_level: 0,
bidi_level: bidi::Level::ltr(),
script: Script::Common,
}
}
Expand Down
1 change: 1 addition & 0 deletions components/style/Cargo.toml
Expand Up @@ -67,6 +67,7 @@ style_derive = {path = "../style_derive"}
style_traits = {path = "../style_traits"}
servo_url = {path = "../url", optional = true}
time = "0.1"
unicode-bidi = {version = "0.3", features = ["with_serde"]}
unicode-segmentation = "1.0"

[target.'cfg(windows)'.dependencies]
Expand Down
1 change: 1 addition & 0 deletions components/style/lib.rs
Expand Up @@ -83,6 +83,7 @@ extern crate style_derive;
#[macro_use]
extern crate style_traits;
extern crate time;
extern crate unicode_bidi;
#[allow(unused_extern_crates)]
extern crate unicode_segmentation;

Expand Down
11 changes: 8 additions & 3 deletions components/style/logical_geometry.rs
Expand Up @@ -10,6 +10,7 @@ use euclid::side_offsets::SideOffsets2D;
use std::cmp::{max, min};
use std::fmt::{self, Debug, Error, Formatter};
use std::ops::{Add, Sub};
use unicode_bidi as bidi;

pub enum BlockFlowDirection {
TopToBottom,
Expand Down Expand Up @@ -131,9 +132,13 @@ impl WritingMode {
#[inline]
/// The default bidirectional embedding level for this writing mode.
///
/// Returns 0 if the mode is LTR, or 1 otherwise.
pub fn to_bidi_level(&self) -> u8 {
!self.is_bidi_ltr() as u8
/// Returns bidi level 0 if the mode is LTR, or 1 otherwise.
pub fn to_bidi_level(&self) -> bidi::Level {
if self.is_bidi_ltr() {
bidi::Level::ltr()
} else {
bidi::Level::rtl()
}
}
}

Expand Down

0 comments on commit 14c524d

Please sign in to comment.