-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #31 - behnam:serde, r=mbrubeck
Add serde support and bump version to 0.3.0 Add `with_serde` feature, which implements serde for the new `struct Level`, mainly used in `servo`, supporting serde `0.8`, `0.9` and `1.0`. Add tests for the `with_serde` feature. The `serde_tests` modules only works for `serde:>=1.0`, though. `servo` has dependency on the loose implementation of `visual_runs()`, which couldn not be improved without breaking the API, as it needs more information to process the levels correctly, and the call-site in `servo` does not have all the information needed and needs a non-trivial change to work with the new improved version. Therefore, I have moved the old version to a `deprecated` module, to be used for now until `servo` is fixed and we drop the old implementation. Bump version to `0.3.0`, as we now ready for a release: can build `servo` (patch ready) and `idna` crates. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/unicode-bidi/31) <!-- Reviewable:end -->
- Loading branch information
Showing
4 changed files
with
189 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2015 The Servo Project Developers. See the | ||
// COPYRIGHT file at the top-level directory of this distribution. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! This module holds deprecated assets only. | ||
|
||
use super::*; | ||
|
||
/// Find the level runs within a line and return them in visual order. | ||
/// | ||
/// NOTE: This implementation is incomplete. The algorithm needs information about the text, | ||
/// including original BidiClass property of each character, to be able to perform correctly. | ||
/// Please see [`BidiInfo::visual_runs()`](../struct.BidiInfo.html#method.visual_runs) for the | ||
/// improved implementation. | ||
/// | ||
/// `line` is a range of bytes indices within `levels`. | ||
/// | ||
/// http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels | ||
#[deprecated(since="0.3.0", note="please use `BidiInfo::visual_runs()` instead.")] | ||
pub fn visual_runs(line: Range<usize>, levels: &[Level]) -> Vec<LevelRun> { | ||
assert!(line.start <= levels.len()); | ||
assert!(line.end <= levels.len()); | ||
|
||
let mut runs = Vec::new(); | ||
|
||
// Find consecutive level runs. | ||
let mut start = line.start; | ||
let mut level = levels[start]; | ||
let mut min_level = level; | ||
let mut max_level = level; | ||
|
||
for i in (start + 1)..line.end { | ||
let new_level = levels[i]; | ||
if new_level != level { | ||
// End of the previous run, start of a new one. | ||
runs.push(start..i); | ||
start = i; | ||
level = new_level; | ||
|
||
min_level = min(level, min_level); | ||
max_level = max(level, max_level); | ||
} | ||
} | ||
runs.push(start..line.end); | ||
|
||
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 levels[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 levels[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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters