]>
Commit | Line | Data |
---|---|---|
abe05a73 XL |
1 | // Copyright 2015 The Servo Project Developers. See the |
2 | // COPYRIGHT file at the top-level directory of this distribution. | |
3 | // | |
4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
7 | // option. This file may not be copied, modified, or distributed | |
8 | // except according to those terms. | |
9 | ||
10 | //! This module holds deprecated assets only. | |
11 | ||
abe05a73 XL |
12 | use super::*; |
13 | ||
14 | /// Find the level runs within a line and return them in visual order. | |
15 | /// | |
16 | /// NOTE: This implementation is incomplete. The algorithm needs information about the text, | |
17 | /// including original `BidiClass` property of each character, to be able to perform correctly. | |
18 | /// Please see [`BidiInfo::visual_runs()`](../struct.BidiInfo.html#method.visual_runs) for the | |
19 | /// improved implementation. | |
20 | /// | |
21 | /// `line` is a range of bytes indices within `levels`. | |
22 | /// | |
23 | /// <http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels> | |
24 | #[deprecated(since = "0.3.0", note = "please use `BidiInfo::visual_runs()` instead.")] | |
25 | pub fn visual_runs(line: Range<usize>, levels: &[Level]) -> Vec<LevelRun> { | |
26 | assert!(line.start <= levels.len()); | |
27 | assert!(line.end <= levels.len()); | |
28 | ||
29 | let mut runs = Vec::new(); | |
30 | ||
31 | // Find consecutive level runs. | |
32 | let mut start = line.start; | |
33 | let mut run_level = levels[start]; | |
34 | let mut min_level = run_level; | |
35 | let mut max_level = run_level; | |
36 | ||
37 | for (i, &new_level) in levels.iter().enumerate().take(line.end).skip(start + 1) { | |
38 | if new_level != run_level { | |
39 | // End of the previous run, start of a new one. | |
40 | runs.push(start..i); | |
41 | start = i; | |
42 | run_level = new_level; | |
43 | ||
44 | min_level = min(run_level, min_level); | |
45 | max_level = max(run_level, max_level); | |
46 | } | |
47 | } | |
48 | runs.push(start..line.end); | |
49 | ||
50 | let run_count = runs.len(); | |
51 | ||
52 | // Re-order the odd runs. | |
53 | // <http://www.unicode.org/reports/tr9/#L2> | |
54 | ||
55 | // Stop at the lowest *odd* level. | |
56 | min_level = min_level.new_lowest_ge_rtl().expect("Level error"); | |
57 | ||
58 | while max_level >= min_level { | |
59 | // Look for the start of a sequence of consecutive runs of max_level or higher. | |
60 | let mut seq_start = 0; | |
61 | while seq_start < run_count { | |
62 | if levels[runs[seq_start].start] < max_level { | |
63 | seq_start += 1; | |
64 | continue; | |
65 | } | |
66 | ||
67 | // Found the start of a sequence. Now find the end. | |
68 | let mut seq_end = seq_start + 1; | |
69 | while seq_end < run_count { | |
70 | if levels[runs[seq_end].start] < max_level { | |
71 | break; | |
72 | } | |
73 | seq_end += 1; | |
74 | } | |
75 | ||
76 | // Reverse the runs within this sequence. | |
77 | runs[seq_start..seq_end].reverse(); | |
78 | ||
79 | seq_start = seq_end; | |
80 | } | |
81 | max_level.lower(1).expect( | |
82 | "Lowering embedding level below zero", | |
83 | ); | |
84 | } | |
85 | ||
86 | runs | |
87 | } |