mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
Add some primitive text line breaking
This commit is contained in:
parent
b7c241a091
commit
632bffd8f4
3 changed files with 58 additions and 13 deletions
|
@ -84,14 +84,20 @@ fn box_to_display_items(list: dl::DisplayList, box: @Box, origin: Point2D<au>) {
|
||||||
|
|
||||||
match box.kind {
|
match box.kind {
|
||||||
TextBoxKind(subbox) => {
|
TextBoxKind(subbox) => {
|
||||||
let run = copy subbox.run;
|
let runs = &mut subbox.runs;
|
||||||
assert run.is_some();
|
|
||||||
list.push(~dl::SolidColor(bounds, 255u8, 255u8, 255u8));
|
list.push(~dl::SolidColor(bounds, 255u8, 255u8, 255u8));
|
||||||
let glyph_run = text_run_to_dl_glyph_run(run.get_ref());
|
|
||||||
list.push(~dl::Glyphs(bounds, glyph_run));
|
let mut bounds = bounds;
|
||||||
|
for uint::range(0, runs.len()) |i| {
|
||||||
|
bounds.size.height = runs[i].size().height;
|
||||||
|
let glyph_run = text_run_to_dl_glyph_run(&mut runs[i]);
|
||||||
|
list.push(~dl::Glyphs(bounds, glyph_run));
|
||||||
|
bounds.origin.y += bounds.size.height;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
fn text_run_to_dl_glyph_run(text_run: &TextRun) -> dl::GlyphRun {
|
pure fn text_run_to_dl_glyph_run(text_run: &mut TextRun) ->
|
||||||
|
dl::GlyphRun {
|
||||||
dl::GlyphRun {
|
dl::GlyphRun {
|
||||||
glyphs: copy text_run.glyphs
|
glyphs: copy text_run.glyphs
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
#[doc="Text layout."]
|
#[doc="Text layout."]
|
||||||
|
|
||||||
use geom::size::Size2D;
|
use geom::size::Size2D;
|
||||||
use gfx::geometry::au;
|
use gfx::geometry::{au, px_to_au};
|
||||||
use servo_text::text_run::TextRun;
|
use servo_text::text_run::TextRun;
|
||||||
use servo_text::font_library::FontLibrary;
|
use servo_text::font_library::FontLibrary;
|
||||||
use base::{Box, TextBoxKind};
|
use base::{Box, TextBoxKind};
|
||||||
|
|
||||||
struct TextBox {
|
struct TextBox {
|
||||||
text: ~str,
|
text: ~str,
|
||||||
mut run: Option<TextRun>,
|
mut runs: ~[TextRun],
|
||||||
}
|
}
|
||||||
|
|
||||||
fn TextBox(text: ~str) -> TextBox {
|
fn TextBox(text: ~str) -> TextBox {
|
||||||
TextBox {
|
TextBox {
|
||||||
text: text,
|
text: text,
|
||||||
run: None,
|
runs: ~[],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,9 +33,48 @@ impl @Box : TextLayout {
|
||||||
// FIXME: The font library should not be initialized here
|
// FIXME: The font library should not be initialized here
|
||||||
let flib = FontLibrary();
|
let flib = FontLibrary();
|
||||||
let font = flib.get_test_font();
|
let font = flib.get_test_font();
|
||||||
let run = TextRun(font, subbox.text);
|
|
||||||
self.bounds.size = run.size();
|
// Do line breaking.
|
||||||
subbox.run = Some(run);
|
let mut current = TextRun(font, subbox.text);
|
||||||
|
let mut lines = dvec::DVec();
|
||||||
|
let mut width_left = px_to_au(800);
|
||||||
|
let mut max_width = au(0);
|
||||||
|
|
||||||
|
while current.size().width > width_left {
|
||||||
|
let min_width = current.min_break_width();
|
||||||
|
|
||||||
|
debug!("line %d, current width %d, width left %d, min width %d",
|
||||||
|
lines.len() as int,
|
||||||
|
*current.size().width as int,
|
||||||
|
*width_left as int,
|
||||||
|
*min_width as int);
|
||||||
|
|
||||||
|
if min_width > width_left {
|
||||||
|
// Too bad, we couldn't break. Overflow.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (prev_line, next_line) = current.split(font, width_left);
|
||||||
|
let prev_width = prev_line.size().width;
|
||||||
|
if max_width < prev_width {
|
||||||
|
max_width = prev_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push(move prev_line);
|
||||||
|
current = next_line;
|
||||||
|
}
|
||||||
|
|
||||||
|
let remaining_width = current.size().width;
|
||||||
|
if max_width < remaining_width {
|
||||||
|
max_width = remaining_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
let line_count = 1 + (lines.len() as i32);
|
||||||
|
let total_height = au(*current.size().height * line_count);
|
||||||
|
lines.push(move current);
|
||||||
|
|
||||||
|
self.bounds.size = Size2D(max_width, total_height);
|
||||||
|
subbox.runs = dvec::unwrap(lines);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,8 @@ struct TextRun {
|
||||||
|
|
||||||
impl TextRun {
|
impl TextRun {
|
||||||
/// The size of the entire TextRun
|
/// The size of the entire TextRun
|
||||||
fn size() -> Size2D<au> { self.size_ }
|
pure fn size() -> Size2D<au> { self.size_ }
|
||||||
fn min_break_width() -> au { self.min_break_width_ }
|
pure fn min_break_width() -> au { self.min_break_width_ }
|
||||||
|
|
||||||
/// Split a run of text in two
|
/// Split a run of text in two
|
||||||
// FIXME: Should be storing a reference to the Font inside
|
// FIXME: Should be storing a reference to the Font inside
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue