Use a color type in the display list

This commit is contained in:
Brian Anderson 2012-10-31 21:36:30 -07:00
parent 0e9b611e4f
commit d12e4fe8ed
5 changed files with 50 additions and 24 deletions

18
src/servo/gfx/color.rs Normal file
View file

@ -0,0 +1,18 @@
use azure::AzFloat;
use AzColor = azure::azure_hl::Color;
pub type Color = AzColor;
pub fn rgb(r: u8, g: u8, b: u8) -> AzColor {
rgba(r, g, b, 1.0)
}
pub fn rgba(r: u8, g: u8, b: u8, a: float) -> AzColor {
AzColor {
r: (r as AzFloat) / (255.0 as AzFloat),
g: (g as AzFloat) / (255.0 as AzFloat),
b: (b as AzFloat) / (255.0 as AzFloat),
a: (a as AzFloat) / (255.0 as AzFloat)
}
}

View file

@ -8,6 +8,7 @@ use render_context::RenderContext;
use servo_text::text_run;
use text::text_run::SendableTextRun;
use util::range::Range;
use color::{Color, rgb};
use std::arc::ARC;
use clone_arc = std::arc::clone;
@ -26,28 +27,28 @@ impl DisplayItemData {
}
pub enum DisplayItem {
SolidColor(DisplayItemData, u8, u8, u8),
SolidColor(DisplayItemData, Color),
// TODO: need to provide spacing data for text run.
// (i.e, to support rendering of CSS 'word-spacing' and 'letter-spacing')
// TODO: don't copy text runs, ever.
Text(DisplayItemData, ~SendableTextRun, Range),
Image(DisplayItemData, ARC<~image::base::Image>),
Border(DisplayItemData, Au, u8, u8, u8)
Border(DisplayItemData, Au, Color)
}
impl DisplayItem {
pure fn d(&self) -> &self/DisplayItemData {
match *self {
SolidColor(ref d, _, _, _) => d,
SolidColor(ref d, _) => d,
Text(ref d, _, _) => d,
Image(ref d, _) => d,
Border(ref d, _, _, _, _) => d
Border(ref d, _, _) => d
}
}
fn draw_into_context(&self, ctx: &RenderContext) {
match *self {
SolidColor(_, r,g,b) => ctx.draw_solid_color(&self.d().bounds, r, g, b),
SolidColor(_, color) => ctx.draw_solid_color(&self.d().bounds, color),
Text(_, run, range) => {
let new_run = @run.deserialize(ctx.font_cache);
let font = new_run.font;
@ -56,20 +57,20 @@ impl DisplayItem {
font.draw_text_into_context(ctx, new_run, range, baseline_origin);
},
Image(_, ref img) => ctx.draw_image(self.d().bounds, clone_arc(img)),
Border(_, width, r, g, b) => ctx.draw_border(&self.d().bounds, width, r, g, b),
Border(_, width, color) => ctx.draw_border(&self.d().bounds, width, color),
}
debug!("%?", {
ctx.draw_border(&self.d().bounds, au::from_px(1), 150, 150, 150);
ctx.draw_border(&self.d().bounds, au::from_px(1), rgb(150, 150, 150));
() });
}
static pure fn new_SolidColor(bounds: &Rect<Au>, r: u8, g: u8, b: u8) -> DisplayItem {
SolidColor(DisplayItemData::new(bounds), r, g, b)
static pure fn new_SolidColor(bounds: &Rect<Au>, color: Color) -> DisplayItem {
SolidColor(DisplayItemData::new(bounds), color)
}
static pure fn new_Border(bounds: &Rect<Au>, width: Au, r: u8, g: u8, b: u8) -> DisplayItem {
Border(DisplayItemData::new(bounds), width, r, g, b)
static pure fn new_Border(bounds: &Rect<Au>, width: Au, color: Color) -> DisplayItem {
Border(DisplayItemData::new(bounds), width, color)
}
static pure fn new_Text(bounds: &Rect<Au>, run: ~SendableTextRun, range: Range) -> DisplayItem {

View file

@ -30,20 +30,11 @@ impl RenderContext {
&self.canvas.draw_target
}
pub fn draw_solid_color(&self, bounds: &Rect<Au>, r: u8, g: u8, b: u8) {
let color = Color(r.to_float() as AzFloat,
g.to_float() as AzFloat,
b.to_float() as AzFloat,
1f as AzFloat);
pub fn draw_solid_color(&self, bounds: &Rect<Au>, color: Color) {
self.canvas.draw_target.fill_rect(&bounds.to_azure_rect(), &ColorPattern(color));
}
pub fn draw_border(&self, bounds: &Rect<Au>, width: Au, r: u8, g: u8, b: u8) {
let color = Color(r.to_float() as AzFloat,
g.to_float() as AzFloat,
b.to_float() as AzFloat,
1f as AzFloat);
pub fn draw_border(&self, bounds: &Rect<Au>, width: Au, color: Color) {
let pattern = ColorPattern(color);
let stroke_fields = 2; // CAP_SQUARE
let width_px = au::to_px(width);

View file

@ -407,7 +407,8 @@ impl RenderBox : RenderBoxMethods {
d.range));
// debug frames for text box bounds
debug!("%?", {
list.append_item(~DisplayItem::new_Border(&abs_box_bounds, au::from_px(1), 0, 0, 200))
list.append_item(~DisplayItem::new_Border(&abs_box_bounds, au::from_px(1),
rgb(0, 0, 200).to_gfx_color()))
; ()});
},
// TODO: items for background, border, outline
@ -429,7 +430,7 @@ impl RenderBox : RenderBoxMethods {
use std::cmp::FuzzyEq;
let bgcolor = self.d().node.compute_background_color();
if !bgcolor.alpha.fuzzy_eq(&0.0) {
list.append_item(~DisplayItem::new_SolidColor(abs_bounds, bgcolor.red, bgcolor.green, bgcolor.blue));
list.append_item(~DisplayItem::new_SolidColor(abs_bounds, bgcolor.to_gfx_color()));
}
}
@ -489,3 +490,17 @@ impl RenderBox : BoxedDebugMethods {
fmt!("box b%?: %?", self.d().id, repr)
}
}
// FIXME: This belongs somewhere else
trait ToGfxColor {
fn to_gfx_color(&self) -> gfx::color::Color;
}
impl Color: ToGfxColor {
fn to_gfx_color(&self) -> gfx::color::Color {
gfx::color::rgba(self.red,
self.green,
self.blue,
self.alpha)
}
}

View file

@ -68,6 +68,7 @@ pub mod layout {
}
pub mod gfx {
pub mod color;
pub mod geometry;
pub mod surface;
pub mod render_task;