| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use icu_properties::bidi::BidiClassAdapter; |
| 6 |
use icu_properties::maps; |
| 7 |
|
| 8 |
use unicode_bidi::level::Level; |
| 9 |
use unicode_bidi::utf16; |
| 10 |
use unicode_bidi::Direction; |
| 11 |
|
| 12 |
use core::ops::Range; |
| 13 |
use core::slice; |
| 14 |
|
| 15 |
/// LevelRun type to be returned to C++. |
| 16 |
/// 32-bit indexes (rather than usize) are sufficient here because Gecko works |
| 17 |
/// with 32-bit indexes when collecting the text buffer for a paragraph. |
| 18 |
#[repr(C)] |
| 19 |
pub struct LevelRun { |
| 20 |
start: u32, |
| 21 |
length: u32, |
| 22 |
level: u8, |
| 23 |
} |
| 24 |
|
| 25 |
/// Bidi object to be exposed to Gecko via FFI. |
| 26 |
pub struct UnicodeBidi<'a> { |
| 27 |
paragraph_info: utf16::ParagraphBidiInfo<'a>, |
| 28 |
resolved: Option<(Vec<Level>, Vec<Range<usize>>)>, |
| 29 |
} |
| 30 |
|
| 31 |
impl UnicodeBidi<'_> { |
| 32 |
/// Create a new UnicodeBidi object representing the given text. This creates |
| 33 |
/// the unicode-bidi ParagraphBidiInfo struct, and will cache the resolved |
| 34 |
/// levels and visual-runs array once created. |
| 35 |
/// The caller is responsible to ensure the text buffer remains valid |
| 36 |
/// as long as the UnicodeBidi object exists. |
| 37 |
fn new<'a>(text: *const u16, length: usize, level: u8) -> Box<Self> { |
| 38 |
let text = unsafe { slice::from_raw_parts(text, length) }; |
| 39 |
let level = if let Ok(level) = Level::new(level) { |
| 40 |
Some(level) |
| 41 |
} else { |
| 42 |
None |
| 43 |
}; |
| 44 |
let adapter = BidiClassAdapter::new(maps::bidi_class()); |
| 45 |
Box::new(UnicodeBidi { |
| 46 |
paragraph_info: utf16::ParagraphBidiInfo::new_with_data_source(&adapter, text, level), |
| 47 |
resolved: None, |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
#[inline] |
| 52 |
fn resolved(&mut self) -> &(Vec<Level>, Vec<Range<usize>>) { |
| 53 |
if self.resolved.is_none() { |
| 54 |
let len = self.paragraph_info.text.len(); |
| 55 |
self.resolved = Some(self.paragraph_info.visual_runs(0..len)); |
| 56 |
} |
| 57 |
self.resolved.as_ref().unwrap() |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/// Create a new UnicodeBidi object for the given text. |
| 62 |
/// NOTE that the text buffer must remain valid for the lifetime of this object! |
| 63 |
#[no_mangle] |
| 64 |
pub extern "C" fn bidi_new<'a>(text: *const u16, length: usize, level: u8) -> *mut UnicodeBidi<'a> { |
| 65 |
Box::into_raw(UnicodeBidi::<'a>::new(text, length, level)) |
| 66 |
} |
| 67 |
|
| 68 |
/// Destroy the Bidi object. |
| 69 |
#[no_mangle] |
| 70 |
pub extern "C" fn bidi_destroy(bidi: *mut UnicodeBidi) { |
| 71 |
if bidi.is_null() { |
| 72 |
return; |
| 73 |
} |
| 74 |
let _ = unsafe { Box::from_raw(bidi) }; |
| 75 |
} |
| 76 |
|
| 77 |
/// Get the length of the text covered by the Bidi object. |
| 78 |
#[no_mangle] |
| 79 |
pub extern "C" fn bidi_get_length(bidi: &UnicodeBidi) -> i32 { |
| 80 |
bidi.paragraph_info.text.len().try_into().unwrap() |
| 81 |
} |
| 82 |
|
| 83 |
/// Get the paragraph direction: LTR=1, RTL=-1, mixed=0. |
| 84 |
#[no_mangle] |
| 85 |
pub extern "C" fn bidi_get_direction(bidi: &UnicodeBidi) -> i8 { |
| 86 |
match bidi.paragraph_info.direction() { |
| 87 |
Direction::Mixed => 0, |
| 88 |
Direction::Ltr => 1, |
| 89 |
Direction::Rtl => -1, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/// Get the paragraph level. |
| 94 |
#[no_mangle] |
| 95 |
pub extern "C" fn bidi_get_paragraph_level(bidi: &UnicodeBidi) -> u8 { |
| 96 |
bidi.paragraph_info.paragraph_level.into() |
| 97 |
} |
| 98 |
|
| 99 |
/// Get the number of runs present. |
| 100 |
#[no_mangle] |
| 101 |
pub extern "C" fn bidi_count_runs(bidi: &mut UnicodeBidi) -> i32 { |
| 102 |
if bidi.paragraph_info.text.is_empty() { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
bidi.resolved().1.len().try_into().unwrap() |
| 106 |
} |
| 107 |
|
| 108 |
/// Get a pointer to the Levels array. The resulting pointer is valid only as long as |
| 109 |
/// the UnicodeBidi object exists! |
| 110 |
#[no_mangle] |
| 111 |
pub extern "C" fn bidi_get_levels(bidi: &mut UnicodeBidi) -> *const Level { |
| 112 |
bidi.resolved().0.as_ptr() |
| 113 |
} |
| 114 |
|
| 115 |
/// Get the extent of the run at the given index in the visual runs array. |
| 116 |
/// This would panic!() if run_index is out of range (see bidi_count_runs), |
| 117 |
/// or if the run's start or length exceeds u32::MAX (which cannot happen |
| 118 |
/// because Gecko can't create such a huge text buffer). |
| 119 |
#[no_mangle] |
| 120 |
pub extern "C" fn bidi_get_visual_run(bidi: &mut UnicodeBidi, run_index: u32) -> LevelRun { |
| 121 |
let level_runs = &bidi.resolved().1; |
| 122 |
let start = level_runs[run_index as usize].start; |
| 123 |
let length = level_runs[run_index as usize].end - start; |
| 124 |
LevelRun { |
| 125 |
start: start.try_into().unwrap(), |
| 126 |
length: length.try_into().unwrap(), |
| 127 |
level: bidi.resolved().0[start].into(), |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// Return index map showing the result of reordering using the given levels array. |
| 132 |
/// (This is a generic helper that does not use a UnicodeBidi object, it just takes an |
| 133 |
/// arbitrary array of levels.) |
| 134 |
#[no_mangle] |
| 135 |
pub extern "C" fn bidi_reorder_visual(levels: *const u8, length: usize, index_map: *mut i32) { |
| 136 |
let levels = unsafe { slice::from_raw_parts(levels as *const Level, length) }; |
| 137 |
let result = unsafe { slice::from_raw_parts_mut(index_map, length) }; |
| 138 |
let reordered = utf16::BidiInfo::reorder_visual(levels); |
| 139 |
for i in 0..length { |
| 140 |
result[i] = reordered[i].try_into().unwrap(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/// Get the base direction for the given text, returning 1 for LTR, -1 for RTL, |
| 145 |
/// and 0 for neutral. If first_paragraph is true, only the first paragraph will be considered; |
| 146 |
/// if false, subsequent paragraphs may be considered until a non-neutral character is found. |
| 147 |
#[no_mangle] |
| 148 |
pub extern "C" fn bidi_get_base_direction( |
| 149 |
text: *const u16, |
| 150 |
length: usize, |
| 151 |
first_paragraph: bool, |
| 152 |
) -> i8 { |
| 153 |
let text = unsafe { slice::from_raw_parts(text, length) }; |
| 154 |
let adapter = BidiClassAdapter::new(maps::bidi_class()); |
| 155 |
let direction = if first_paragraph { |
| 156 |
unicode_bidi::get_base_direction_with_data_source(&adapter, text) |
| 157 |
} else { |
| 158 |
unicode_bidi::get_base_direction_full_with_data_source(&adapter, text) |
| 159 |
}; |
| 160 |
match direction { |
| 161 |
Direction::Mixed => 0, |
| 162 |
Direction::Ltr => 1, |
| 163 |
Direction::Rtl => -1, |
| 164 |
} |
| 165 |
} |