Print text to screen. Aw, yeah.

This commit is contained in:
Brian Anderson 2012-06-16 16:43:20 -07:00
parent f37897924b
commit 8d2eb11582
6 changed files with 96 additions and 32 deletions

View file

@ -6,6 +6,7 @@ import dl = layout::display_list;
import azure::*;
import azure::bindgen::*;
import libc::size_t;
import text::text_run::text_run;
enum Msg {
RenderMsg(dl::display_list),
@ -36,7 +37,6 @@ fn renderer<S: sink send copy>(sink: S) -> chan<Msg> {
#debug("renderer: rendering");
clear(draw_target);
draw_display_list(draw_target, display_list);
draw_some_text(draw_target);
#debug("renderer: returning surface");
sink.draw(draw_target_ch, draw_target);
}
@ -56,6 +56,30 @@ impl to_float for u8 {
}
}
fn draw_display_list(
draw_target: AzDrawTargetRef,
display_list: dl::display_list
) {
for display_list.each {|item|
#debug["drawing %?", item];
alt item.item_type {
dl::display_item_solid_color(r, g, b) {
draw_solid_color(draw_target, item, r, g, b);
}
dl::display_item_image(image) {
draw_image(draw_target, item, image);
}
dl::display_item_text(text_run) {
draw_text(draw_target, item, text_run);
}
dl::padding(*) {
fail "should never see padding";
}
}
}
}
fn draw_solid_color(draw_target: AzDrawTargetRef, item: dl::display_item,
r: u8, g: u8, b: u8) {
let bounds = copy (*item).bounds;
@ -127,28 +151,68 @@ fn draw_image(draw_target: AzDrawTargetRef, item: dl::display_item,
}
}
fn draw_display_list(
draw_target: AzDrawTargetRef,
display_list: dl::display_list
) {
for display_list.each {|item|
#debug["drawing %?", item];
fn draw_text(draw_target: AzDrawTargetRef, item: dl::display_item, text_run: text_run) {
alt item.item_type {
dl::display_item_solid_color(r, g, b) {
draw_solid_color(draw_target, item, r, g, b);
}
dl::display_item_image(image) {
draw_image(draw_target, item, image);
}
dl::display_item_text(text_run) {
// FIXME
}
dl::padding(*) {
fail "should never see padding";
}
}
}
import ptr::{addr_of, null};
import vec::unsafe::to_ptr;
import libc::types::common::c99::{uint16_t, uint32_t};
import geom::point::Point2D;
import text::font::{font, create_test_font};
import azure::{AzNativeFont, AzFloat, AZ_NATIVE_FONT_CAIRO_FONT_FACE};
import azure::bindgen::{AzCreateScaledFontWithCairo,
AzReleaseScaledFont,
AzCreateColorPattern,
AzReleaseColorPattern};
let bounds = copy (*item).bounds;
let font = create_test_font();
let nfont: AzNativeFont = {
mType: AZ_NATIVE_FONT_CAIRO_FONT_FACE,
mFont: null()
};
let azfont = AzCreateScaledFontWithCairo(addr_of(nfont), 1f as AzFloat, font.cairo_font);
assert azfont.is_not_null();
let color = {
r: 0f as AzFloat,
g: 0f as AzFloat,
b: 0f as AzFloat,
a: 1f as AzFloat
};
let pattern = AzCreateColorPattern(addr_of(color));
assert pattern.is_not_null();
let options: AzDrawOptions = {
mAlpha: 1f as AzFloat,
fields: 0 as uint16_t
};
let mut origin = Point2D(bounds.origin.x, bounds.origin.y.add(bounds.size.height));
let azglyphs = text_run.glyphs.map { |glyph|
let azglyph: AzGlyph = {
mIndex: glyph.codepoint as uint32_t,
mPosition: {
x: au_to_px(origin.x.add(glyph.pos.offset.x)) as AzFloat,
y: au_to_px(origin.y.add(glyph.pos.offset.y)) as AzFloat
}
};
origin = Point2D(origin.x.add(glyph.pos.advance.x),
origin.y.add(glyph.pos.advance.y));
azglyph
};
let glyphbuf: AzGlyphBuffer = unsafe {{
mGlyphs: to_ptr(azglyphs),
mNumGlyphs: azglyphs.len() as uint32_t
}};
AzDrawTargetFillGlyphs(draw_target, azfont, addr_of(glyphbuf),
pattern, addr_of(options), null());
AzReleaseColorPattern(pattern);
AzReleaseScaledFont(azfont);
}
#[cfg(target_os = "macos")]

View file

@ -156,7 +156,7 @@ fn should_calculate_the_bounds_of_the_text_box_background_color() {
let expected = Rect(
Point2D(px_to_au(0), px_to_au(0)),
Size2D(px_to_au(110), px_to_au(14))
Size2D(px_to_au(220), px_to_au(20))
);
assert di[0].bounds == expected;
@ -174,7 +174,7 @@ fn should_calculate_the_bounds_of_the_text_items() {
let expected = Rect(
Point2D(px_to_au(0), px_to_au(0)),
Size2D(px_to_au(110), px_to_au(14))
Size2D(px_to_au(220), px_to_au(20))
);
assert di[1].bounds == expected;

View file

@ -46,6 +46,6 @@ fn should_calculate_the_size_of_the_text_box() {
let subbox = alt check b.kind { TextBox(subbox) { subbox } };
b.reflow_text(px_to_au(800), subbox);
let expected = Size2D(px_to_au(110), px_to_au(14));
assert b.bounds.size == Size2D(px_to_au(110), px_to_au(14));
let expected = Size2D(px_to_au(220), px_to_au(20));
assert b.bounds.size == expected;
}

View file

@ -75,7 +75,7 @@ class font/& {
}
fn glyph_h_advance(_glyph: uint) -> int {
10
20
}
}
@ -109,7 +109,7 @@ fn get_cairo_font(buf: &[u8]) -> (*cairo_scaled_font_t, fn@()) {
let fontmatrix = idmatrix;
cairo_matrix_scale(addr_of(fontmatrix),
300f as c_double, 400f as c_double);
20f as c_double, 20f as c_double);
let options = cairo_font_options_create();
let cfont = cairo_scaled_font_create(face, addr_of(fontmatrix),
@ -205,7 +205,7 @@ fn should_get_glyph_advance() {
let font = create_test_font();
let x = font.glyph_h_advance(40u);
// This number is bogus
assert x == 10;
assert x == 20;
}
fn should_be_able_to_create_instances_in_multiple_threads() {

View file

@ -156,5 +156,5 @@ fn should_get_glyph_h_advance() {
let font = font::create_test_font();
let glyphs = shape_text(&font, "firecracker");
// This number is just a placeholder and probably not correct
assert glyphs.all { |glyph| glyph.pos.advance.x == px_to_au(10) };
assert glyphs.all { |glyph| glyph.pos.advance.x == px_to_au(20) };
}

View file

@ -15,7 +15,7 @@ class text_run {
}
fn size() -> Size2D<au> {
let height = px_to_au(14);
let height = px_to_au(20);
let pen_start_x = px_to_au(0);
let pen_start_y = height;
let pen_start = Point2D(pen_start_x, pen_start_y);
@ -32,6 +32,6 @@ fn should_calculate_the_total_size() {
let font = create_test_font();
let run = text_run(&font, "firecracker");
let expected = Size2D(px_to_au(110), px_to_au(14));
let expected = Size2D(px_to_au(220), px_to_au(20));
assert run.size() == expected;
}