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
12use alloc::vec::Vec;
13
14use super::*;
15
16/// Find the level runs within a line and return them in visual order.
17///
18/// NOTE: This implementation is incomplete. The algorithm needs information about the text,
19/// including original `BidiClass` property of each character, to be able to perform correctly.
20/// Please see [`BidiInfo::visual_runs()`](../struct.BidiInfo.html#method.visual_runs) for the
21/// improved implementation.
22///
23/// `line` is a range of bytes indices within `levels`.
24///
25/// <http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels>
26#[deprecated(
27 since = "0.3.0",
28 note = "please use `BidiInfo::visual_runs()` instead."
29)]
30pub fn visual_runs(line: Range<usize>, levels: &[Level]) -> Vec<LevelRun> {
31 assert!(line.start <= levels.len());
32 assert!(line.end <= levels.len());
33
34 let mut runs = Vec::new();
35
36 // Find consecutive level runs.
37 let mut start = line.start;
38 let mut run_level = levels[start];
39 let mut min_level = run_level;
40 let mut max_level = run_level;
41
42 for (i, &new_level) in levels.iter().enumerate().take(line.end).skip(start + 1) {
43 if new_level != run_level {
44 // End of the previous run, start of a new one.
45 runs.push(start..i);
46 start = i;
47 run_level = new_level;
48
49 min_level = cmp::min(run_level, min_level);
50 max_level = cmp::max(run_level, max_level);
51 }
52 }
53 runs.push(start..line.end);
54
55 let run_count = runs.len();
56
57 // Re-order the odd runs.
58 // <http://www.unicode.org/reports/tr9/#L2>
59
60 // Stop at the lowest *odd* level.
61 min_level = min_level.new_lowest_ge_rtl().expect("Level error");
62
63 while max_level >= min_level {
64 // Look for the start of a sequence of consecutive runs of max_level or higher.
65 let mut seq_start = 0;
66 while seq_start < run_count {
67 if levels[runs[seq_start].start] < max_level {
68 seq_start += 1;
69 continue;
70 }
71
72 // Found the start of a sequence. Now find the end.
73 let mut seq_end = seq_start + 1;
74 while seq_end < run_count {
75 if levels[runs[seq_end].start] < max_level {
76 break;
77 }
78 seq_end += 1;
79 }
80
81 // Reverse the runs within this sequence.
82 runs[seq_start..seq_end].reverse();
83
84 seq_start = seq_end;
85 }
86 max_level
87 .lower(1)
88 .expect("Lowering embedding level below zero");
89 }
90
91 runs
92}
93