mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-08 10:44:56 +00:00
servo: Merge #10913 - Use xi-unicode for line breaking (from mbrubeck:xi-unicode); r=pcwalton
This uses the xi-unicode crate by @raphlinus to detect line-break opportunities, replacing Servo's custom code that only detects ASCII whitespace. xi-unicode is licensed under the Apache-2.0 license. See mbrubeck/servo#2 for some discussion on an earlier draft of this code. This PR implements the "search backward to find trailing whitespace" solution discussed there. r? @pcwalton Source-Repo: https://github.com/servo/servo Source-Revision: fc1e4c808541ca11e25831c7c6b5cfa924945fa5
This commit is contained in:
parent
570d798ed2
commit
eacfe16cd2
@ -44,6 +44,7 @@ azure = {git = "https://github.com/servo/rust-azure", features = ["plugins"]}
|
||||
layers = {git = "https://github.com/servo/rust-layers", features = ["plugins"]}
|
||||
ipc-channel = {git = "https://github.com/servo/ipc-channel"}
|
||||
webrender_traits = {git = "https://github.com/servo/webrender_traits"}
|
||||
xi-unicode = "0.0.1"
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
core-foundation = "0.2"
|
||||
|
@ -80,6 +80,7 @@ extern crate unicode_script;
|
||||
extern crate url;
|
||||
extern crate util;
|
||||
extern crate webrender_traits;
|
||||
extern crate xi_unicode;
|
||||
|
||||
pub use paint_context::PaintContext;
|
||||
|
||||
|
@ -12,7 +12,9 @@ use std::cmp::{Ordering, max};
|
||||
use std::slice::Iter;
|
||||
use std::sync::Arc;
|
||||
use text::glyph::{ByteIndex, GlyphStore};
|
||||
use util::str::char_is_whitespace;
|
||||
use webrender_traits;
|
||||
use xi_unicode::LineBreakIterator;
|
||||
|
||||
thread_local! {
|
||||
static INDEX_OF_FIRST_GLYPH_RUN_CACHE: Cell<Option<(*const TextRun, ByteIndex, usize)>> =
|
||||
@ -191,73 +193,40 @@ impl<'a> TextRun {
|
||||
|
||||
pub fn break_and_shape(font: &mut Font, text: &str, options: &ShapingOptions)
|
||||
-> Vec<GlyphRun> {
|
||||
// TODO(Issue #230): do a better job. See Gecko's LineBreaker.
|
||||
let mut glyphs = vec!();
|
||||
let mut byte_i = 0;
|
||||
let mut cur_slice_is_whitespace = false;
|
||||
let mut byte_last_boundary = 0;
|
||||
let mut slice = 0..0;
|
||||
|
||||
for ch in text.chars() {
|
||||
// Slices alternate between whitespace and non-whitespace,
|
||||
// representing line break opportunities.
|
||||
let can_break_before = if cur_slice_is_whitespace {
|
||||
match ch {
|
||||
' ' | '\t' | '\n' => false,
|
||||
_ => {
|
||||
cur_slice_is_whitespace = false;
|
||||
true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match ch {
|
||||
' ' | '\t' | '\n' => {
|
||||
cur_slice_is_whitespace = true;
|
||||
true
|
||||
},
|
||||
_ => false
|
||||
}
|
||||
};
|
||||
for (idx, _is_hard_break) in LineBreakIterator::new(text) {
|
||||
// Extend the slice to the next UAX#14 line break opportunity.
|
||||
slice.end = idx;
|
||||
let word = &text[slice.clone()];
|
||||
|
||||
// Create a glyph store for this slice if it's nonempty.
|
||||
if can_break_before && byte_i > byte_last_boundary {
|
||||
let slice = &text[byte_last_boundary .. byte_i];
|
||||
debug!("creating glyph store for slice {} (ws? {}), {} - {} in run {}",
|
||||
slice, !cur_slice_is_whitespace, byte_last_boundary, byte_i, text);
|
||||
|
||||
let mut options = *options;
|
||||
if !cur_slice_is_whitespace {
|
||||
options.flags.insert(IS_WHITESPACE_SHAPING_FLAG);
|
||||
}
|
||||
// Split off any trailing whitespace into a separate glyph run.
|
||||
let mut whitespace = slice.end..slice.end;
|
||||
if let Some((i, _)) = word.char_indices().rev()
|
||||
.take_while(|&(_, c)| char_is_whitespace(c)).last() {
|
||||
whitespace.start = slice.start + i;
|
||||
slice.end = whitespace.start;
|
||||
}
|
||||
|
||||
if slice.len() > 0 {
|
||||
glyphs.push(GlyphRun {
|
||||
glyph_store: font.shape_text(slice, &options),
|
||||
range: Range::new(ByteIndex(byte_last_boundary as isize),
|
||||
ByteIndex((byte_i - byte_last_boundary) as isize)),
|
||||
glyph_store: font.shape_text(&text[slice.clone()], options),
|
||||
range: Range::new(ByteIndex(slice.start as isize),
|
||||
ByteIndex(slice.len() as isize)),
|
||||
});
|
||||
byte_last_boundary = byte_i;
|
||||
}
|
||||
|
||||
byte_i = byte_i + ch.len_utf8();
|
||||
}
|
||||
|
||||
// Create a glyph store for the final slice if it's nonempty.
|
||||
if byte_i > byte_last_boundary {
|
||||
let slice = &text[byte_last_boundary..];
|
||||
debug!("creating glyph store for final slice {} (ws? {}), {} - {} in run {}",
|
||||
slice, cur_slice_is_whitespace, byte_last_boundary, text.len(), text);
|
||||
|
||||
let mut options = *options;
|
||||
if cur_slice_is_whitespace {
|
||||
if whitespace.len() > 0 {
|
||||
let mut options = options.clone();
|
||||
options.flags.insert(IS_WHITESPACE_SHAPING_FLAG);
|
||||
glyphs.push(GlyphRun {
|
||||
glyph_store: font.shape_text(&text[whitespace.clone()], &options),
|
||||
range: Range::new(ByteIndex(whitespace.start as isize),
|
||||
ByteIndex(whitespace.len() as isize)),
|
||||
});
|
||||
}
|
||||
|
||||
glyphs.push(GlyphRun {
|
||||
glyph_store: font.shape_text(slice, &options),
|
||||
range: Range::new(ByteIndex(byte_last_boundary as isize),
|
||||
ByteIndex((byte_i - byte_last_boundary) as isize)),
|
||||
});
|
||||
slice.start = whitespace.end;
|
||||
}
|
||||
|
||||
glyphs
|
||||
}
|
||||
|
||||
|
6
servo/components/servo/Cargo.lock
generated
6
servo/components/servo/Cargo.lock
generated
@ -739,6 +739,7 @@ dependencies = [
|
||||
"url 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"util 0.0.1",
|
||||
"webrender_traits 0.1.0 (git+https://github.com/servo/webrender_traits)",
|
||||
"xi-unicode 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2525,6 +2526,11 @@ dependencies = [
|
||||
"libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xi-unicode"
|
||||
version = "0.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "xml-rs"
|
||||
version = "0.2.2"
|
||||
|
6
servo/ports/cef/Cargo.lock
generated
6
servo/ports/cef/Cargo.lock
generated
@ -669,6 +669,7 @@ dependencies = [
|
||||
"url 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"util 0.0.1",
|
||||
"webrender_traits 0.1.0 (git+https://github.com/servo/webrender_traits)",
|
||||
"xi-unicode 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2390,6 +2391,11 @@ dependencies = [
|
||||
"libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xi-unicode"
|
||||
version = "0.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "xml-rs"
|
||||
version = "0.2.2"
|
||||
|
6
servo/ports/gonk/Cargo.lock
generated
6
servo/ports/gonk/Cargo.lock
generated
@ -672,6 +672,7 @@ dependencies = [
|
||||
"url 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"util 0.0.1",
|
||||
"webrender_traits 0.1.0 (git+https://github.com/servo/webrender_traits)",
|
||||
"xi-unicode 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2341,6 +2342,11 @@ dependencies = [
|
||||
"libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "xi-unicode"
|
||||
version = "0.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "xml-rs"
|
||||
version = "0.2.2"
|
||||
|
Loading…
Reference in New Issue
Block a user