1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Text layout.
#![deny(unsafe_code)]
use fragment::{Fragment, SpecificFragmentInfo, ScannedTextFragmentInfo};
use inline::InlineFragments;
use gfx::font::{DISABLE_KERNING_SHAPING_FLAG, FontMetrics, IGNORE_LIGATURES_SHAPING_FLAG};
use gfx::font::{RunMetrics, ShapingFlags, ShapingOptions};
use gfx::font_context::FontContext;
use gfx::text::glyph::CharIndex;
use gfx::text::text_run::TextRun;
use gfx::text::util::{self, CompressionMode};
use util::linked_list::split_off_head;
use util::geometry::Au;
use util::logical_geometry::{LogicalSize, WritingMode};
use util::range::Range;
use util::smallvec::{SmallVec, SmallVec1};
use std::collections::LinkedList;
use std::mem;
use style::computed_values::{line_height, text_orientation, text_rendering, text_transform};
use style::computed_values::{white_space};
use style::properties::ComputedValues;
use style::properties::style_structs::Font as FontStyle;
use std::sync::Arc;
/// A stack-allocated object for scanning an inline flow into `TextRun`-containing `TextFragment`s.
pub struct TextRunScanner {
pub clump: LinkedList<Fragment>,
}
impl TextRunScanner {
pub fn new() -> TextRunScanner {
TextRunScanner {
clump: LinkedList::new(),
}
}
pub fn scan_for_runs(&mut self, font_context: &mut FontContext, mut fragments: LinkedList<Fragment>)
-> InlineFragments {
debug!("TextRunScanner: scanning {} fragments for text runs...", fragments.len());
// FIXME(pcwalton): We want to be sure not to allocate multiple times, since this is a
// performance-critical spot, but this may overestimate and allocate too much memory.
let mut new_fragments = Vec::with_capacity(fragments.len());
let mut last_whitespace = true;
while !fragments.is_empty() {
// Create a clump.
self.clump.append(&mut split_off_head(&mut fragments));
while !fragments.is_empty() && self.clump
.back()
.unwrap()
.can_merge_with_fragment(fragments.front()
.unwrap()) {
self.clump.append(&mut split_off_head(&mut fragments));
}
// Flush that clump to the list of fragments we're building up.
last_whitespace = self.flush_clump_to_list(font_context,
&mut new_fragments,
last_whitespace);
}
debug!("TextRunScanner: complete.");
InlineFragments {
fragments: new_fragments,
}
}
/// A "clump" is a range of inline flow leaves that can be merged together into a single
/// fragment. Adjacent text with the same style can be merged, and nothing else can.
///
/// The flow keeps track of the fragments contained by all non-leaf DOM nodes. This is necessary
/// for correct painting order. Since we compress several leaf fragments here, the mapping must
/// be adjusted.
fn flush_clump_to_list(&mut self,
font_context: &mut FontContext,
out_fragments: &mut Vec<Fragment>,
mut last_whitespace: bool)
-> bool {
debug!("TextRunScanner: flushing {} fragments in range", self.clump.len());
debug_assert!(!self.clump.is_empty());
match self.clump.front().unwrap().specific {
SpecificFragmentInfo::UnscannedText(_) => {}
_ => {
debug_assert!(self.clump.len() == 1,
"WAT: can't coalesce non-text nodes in flush_clump_to_list()!");
out_fragments.push(self.clump.pop_front().unwrap());
return last_whitespace
}
}
// TODO(#177): Text run creation must account for the renderability of text by font group
// fonts. This is probably achieved by creating the font group above and then letting
// `FontGroup` decide which `Font` to stick into the text run.
//
// Concatenate all of the transformed strings together, saving the new character indices.
let mut new_ranges: SmallVec1<Range<CharIndex>> = SmallVec1::new();
let mut new_line_positions: SmallVec1<NewLinePositions> = SmallVec1::new();
let mut char_total = CharIndex(0);
let run = {
let fontgroup;
let compression;
let text_transform;
let letter_spacing;
let word_spacing;
let text_rendering;
{
let in_fragment = self.clump.front().unwrap();
let font_style = in_fragment.style().get_font_arc();
let inherited_text_style = in_fragment.style().get_inheritedtext();
fontgroup = font_context.get_layout_font_group_for_style(font_style);
compression = match in_fragment.white_space() {
white_space::T::normal | white_space::T::nowrap => {
CompressionMode::CompressWhitespaceNewline
}
white_space::T::pre => CompressionMode::CompressNone,
};
text_transform = inherited_text_style.text_transform;
letter_spacing = inherited_text_style.letter_spacing;
word_spacing = inherited_text_style.word_spacing.unwrap_or(Au(0));
text_rendering = inherited_text_style.text_rendering;
}
// First, transform/compress text of all the nodes.
let mut run_text = String::new();
for in_fragment in self.clump.iter() {
let in_fragment = match in_fragment.specific {
SpecificFragmentInfo::UnscannedText(ref text_fragment_info) => {
&text_fragment_info.text
}
_ => panic!("Expected an unscanned text fragment!"),
};
let mut new_line_pos = Vec::new();
let old_length = CharIndex(run_text.chars().count() as isize);
last_whitespace = util::transform_text(in_fragment.as_slice(),
compression,
last_whitespace,
&mut run_text,
&mut new_line_pos);
new_line_positions.push(NewLinePositions(new_line_pos));
let added_chars = CharIndex(run_text.chars().count() as isize) - old_length;
new_ranges.push(Range::new(char_total, added_chars));
char_total = char_total + added_chars;
}
// Account for `text-transform`. (Confusingly, this is not handled in "text
// transformation" above, but we follow Gecko in the naming.)
self.apply_style_transform_if_necessary(&mut run_text, text_transform);
// Now create the run.
//
// TextRuns contain a cycle which is usually resolved by the teardown sequence.
// If no clump takes ownership, however, it will leak.
if run_text.len() == 0 {
self.clump = LinkedList::new();
return last_whitespace
}
// Per CSS 2.1 § 16.4, "when the resultant space between two characters is not the same
// as the default space, user agents should not use ligatures." This ensures that, for
// example, `finally` with a wide `letter-spacing` renders as `f i n a l l y` and not
// `fi n a l l y`.
let mut flags = ShapingFlags::empty();
match letter_spacing {
Some(Au(0)) | None => {}
Some(_) => flags.insert(IGNORE_LIGATURES_SHAPING_FLAG),
}
if text_rendering == text_rendering::T::optimizespeed {
flags.insert(IGNORE_LIGATURES_SHAPING_FLAG);
flags.insert(DISABLE_KERNING_SHAPING_FLAG)
}
let options = ShapingOptions {
letter_spacing: letter_spacing,
word_spacing: word_spacing,
flags: flags,
};
// FIXME(https://github.com/rust-lang/rust/issues/23338)
let mut font = fontgroup.fonts.get(0).borrow_mut();
Arc::new(box TextRun::new(&mut *font, run_text, &options))
};
// Make new fragments with the run and adjusted text indices.
debug!("TextRunScanner: pushing {} fragment(s)", self.clump.len());
for (logical_offset, old_fragment) in
mem::replace(&mut self.clump, LinkedList::new()).into_iter().enumerate() {
let range = *new_ranges.get(logical_offset);
if range.is_empty() {
debug!("Elided an `SpecificFragmentInfo::UnscannedText` because it was \
zero-length after compression");
continue
}
let text_size = old_fragment.border_box.size;
let &mut NewLinePositions(ref mut new_line_positions) =
new_line_positions.get_mut(logical_offset);
let mut new_text_fragment_info =
box ScannedTextFragmentInfo::new(run.clone(),
range,
mem::replace(new_line_positions, Vec::new()),
text_size);
let new_metrics = new_text_fragment_info.run.metrics_for_range(&range);
let bounding_box_size = bounding_box_for_run_metrics(&new_metrics,
old_fragment.style.writing_mode);
new_text_fragment_info.content_size = bounding_box_size;
let new_fragment =
old_fragment.transform(bounding_box_size,
SpecificFragmentInfo::ScannedText(new_text_fragment_info));
out_fragments.push(new_fragment)
}
last_whitespace
}
/// Accounts for `text-transform`.
///
/// FIXME(#4311, pcwalton): Case mapping can change length of the string; case mapping should
/// be language-specific; `full-width`; use graphemes instead of characters.
fn apply_style_transform_if_necessary(&mut self,
string: &mut String,
text_transform: text_transform::T) {
match text_transform {
text_transform::T::none => {}
text_transform::T::uppercase => {
let length = string.len();
let original = mem::replace(string, String::with_capacity(length));
for character in original.chars() {
string.push(character.to_uppercase())
}
}
text_transform::T::lowercase => {
let length = string.len();
let original = mem::replace(string, String::with_capacity(length));
for character in original.chars() {
string.push(character.to_lowercase())
}
}
text_transform::T::capitalize => {
let length = string.len();
let original = mem::replace(string, String::with_capacity(length));
let mut capitalize_next_letter = true;
for character in original.chars() {
// FIXME(#4311, pcwalton): Should be the CSS/Unicode notion of a *typographic
// letter unit*, not an *alphabetic* character:
//
// http://dev.w3.org/csswg/css-text/#typographic-letter-unit
if capitalize_next_letter && character.is_alphabetic() {
string.push(character.to_uppercase());
capitalize_next_letter = false;
continue
}
string.push(character);
// FIXME(#4311, pcwalton): Try UAX29 instead of just whitespace.
if character.is_whitespace() {
capitalize_next_letter = true
}
}
}
}
}
}
struct NewLinePositions(Vec<CharIndex>);
#[inline]
fn bounding_box_for_run_metrics(metrics: &RunMetrics, writing_mode: WritingMode)
-> LogicalSize<Au> {
// This does nothing, but it will fail to build
// when more values are added to the `text-orientation` CSS property.
// This will be a reminder to update the code below.
let dummy: Option<text_orientation::T> = None;
match dummy {
Some(text_orientation::T::sideways_right) |
Some(text_orientation::T::sideways_left) |
Some(text_orientation::T::sideways) |
None => {}
}
// In vertical sideways or horizontal upright text,
// the "width" of text metrics is always inline
// This will need to be updated when other text orientations are supported.
LogicalSize::new(
writing_mode,
metrics.bounding_box.size.width,
metrics.bounding_box.size.height)
}
/// Returns the metrics of the font represented by the given `FontStyle`, respectively.
///
/// `#[inline]` because often the caller only needs a few fields from the font metrics.
#[inline]
pub fn font_metrics_for_style(font_context: &mut FontContext, font_style: Arc<FontStyle>)
-> FontMetrics {
let fontgroup = font_context.get_layout_font_group_for_style(font_style);
// FIXME(https://github.com/rust-lang/rust/issues/23338)
let font = fontgroup.fonts.get(0).borrow();
font.metrics.clone()
}
/// Returns the line block-size needed by the given computed style and font size.
pub fn line_height_from_style(style: &ComputedValues, metrics: &FontMetrics) -> Au {
let font_size = style.get_font().font_size;
match style.get_inheritedbox().line_height {
line_height::T::Normal => metrics.line_gap,
line_height::T::Number(l) => font_size.scale_by(l),
line_height::T::Length(l) => l
}
}
|