code review

This commit is contained in:
Clark Gaebel 2014-12-02 17:30:54 -08:00
parent 7d1e85f774
commit 85856d0072
2 changed files with 60 additions and 64 deletions

View file

@ -2,7 +2,7 @@
* 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/. */
#![feature(globs, macro_rules, phase, unsafe_destructor, if_let)]
#![feature(globs, macro_rules, phase, unsafe_destructor)]
#![deny(unused_imports)]
#![deny(unused_variables)]

View file

@ -153,7 +153,7 @@ impl<'a> RenderContext<'a> {
self.draw_target.fill_rect(&rect, ColorPatternRef(&pattern), Some(&draw_options));
}
fn draw_border_segment(&self, direction: Direction, bounds: &Rect<Au>, border: SideOffsets2D<f32>, radius: &BorderRadii<Option<AzFloat>>, color: SideOffsets2D<Color>, style: SideOffsets2D<border_style::T>) {
fn draw_border_segment(&self, direction: Direction, bounds: &Rect<Au>, border: SideOffsets2D<f32>, radius: &BorderRadii<AzFloat>, color: SideOffsets2D<Color>, style: SideOffsets2D<border_style::T>) {
let (style_select, color_select) = match direction {
Top => (style.top, color.top),
Left => (style.left, color.left),
@ -188,7 +188,7 @@ impl<'a> RenderContext<'a> {
}
}
fn draw_line_segment(&self, bounds: &Rect<Au>, radius: &BorderRadii<Option<AzFloat>>, color: Color, style: border_style::T) {
fn draw_line_segment(&self, bounds: &Rect<Au>, radius: &BorderRadii<AzFloat>, color: Color, style: border_style::T) {
let border = SideOffsets2D::new_all_same(bounds.size.width).to_float_px();
match style {
@ -300,7 +300,7 @@ impl<'a> RenderContext<'a> {
bounds: &Rect<f32>,
direction: Direction,
border: SideOffsets2D<f32>,
radius: &BorderRadii<Option<AzFloat>>,
radius: &BorderRadii<AzFloat>,
color: Color) {
// T = top, B = bottom, L = left, R = right
@ -339,128 +339,128 @@ impl<'a> RenderContext<'a> {
match direction {
Top => {
let edge_TL = box_TL + dx(radius.top_left.unwrap_or(0.).max(border.left));
let edge_TR = box_TR + dx(-radius.top_right.unwrap_or(0.).max(border.right));
let edge_TL = box_TL + dx(radius.top_left.max(border.left));
let edge_TR = box_TR + dx(-radius.top_right.max(border.right));
let edge_BR = edge_TR + dy(border.top);
let edge_BL = edge_TL + dy(border.top);
let corner_TL = edge_TL + dx_if(radius.top_left.is_none(), -border.left);
let corner_TR = edge_TR + dx_if(radius.top_right.is_none(), border.right);
let corner_TL = edge_TL + dx_if(radius.top_left == 0., -border.left);
let corner_TR = edge_TR + dx_if(radius.top_right == 0., border.right);
path_builder.move_to(corner_TL);
path_builder.line_to(corner_TR);
if let Some(top_right_radius) = radius.top_right {
if radius.top_right != 0. {
// the origin is the center of the arcs we're about to draw.
let origin = edge_TR + Point2D((border.right - top_right_radius).max(0.), top_right_radius);
let origin = edge_TR + Point2D((border.right - radius.top_right).max(0.), radius.top_right);
// the elbow is the inside of the border's curve.
let distance_to_elbow = (top_right_radius - border.top).max(0.);
let distance_to_elbow = (radius.top_right - border.top).max(0.);
path_builder.arc(origin, top_right_radius, rad_T, rad_TR, false);
path_builder.arc(origin, radius.top_right, rad_T, rad_TR, false);
path_builder.arc(origin, distance_to_elbow, rad_TR, rad_T, true);
}
path_builder.line_to(edge_BR);
path_builder.line_to(edge_BL);
if let Some(top_left_radius) = radius.top_left {
let origin = edge_TL + Point2D(-(border.left - top_left_radius).max(0.), top_left_radius);
let distance_to_elbow = (top_left_radius - border.top).max(0.);
if radius.top_left != 0. {
let origin = edge_TL + Point2D(-(border.left - radius.top_left).max(0.), radius.top_left);
let distance_to_elbow = (radius.top_left - border.top).max(0.);
path_builder.arc(origin, distance_to_elbow, rad_T, rad_TL, true);
path_builder.arc(origin, top_left_radius, rad_TL, rad_T, false);
path_builder.arc(origin, radius.top_left, rad_TL, rad_T, false);
}
}
Left => {
let edge_TL = box_TL + dy(radius.top_left.unwrap_or(0.).max(border.top));
let edge_BL = box_BL + dy(-radius.bottom_left.unwrap_or(0.).max(border.bottom));
let edge_TL = box_TL + dy(radius.top_left.max(border.top));
let edge_BL = box_BL + dy(-radius.bottom_left.max(border.bottom));
let edge_TR = edge_TL + dx(border.left);
let edge_BR = edge_BL + dx(border.left);
let corner_TL = edge_TL + dy_if(radius.top_left.is_none(), -border.top);
let corner_BL = edge_BL + dy_if(radius.bottom_left.is_none(), border.bottom);
let corner_TL = edge_TL + dy_if(radius.top_left == 0., -border.top);
let corner_BL = edge_BL + dy_if(radius.bottom_left == 0., border.bottom);
path_builder.move_to(corner_BL);
path_builder.line_to(corner_TL);
if let Some(top_left_radius) = radius.top_left {
let origin = edge_TL + Point2D(top_left_radius, -(border.top - top_left_radius).max(0.));
let distance_to_elbow = (top_left_radius - border.left).max(0.);
if radius.top_left != 0. {
let origin = edge_TL + Point2D(radius.top_left, -(border.top - radius.top_left).max(0.));
let distance_to_elbow = (radius.top_left - border.left).max(0.);
path_builder.arc(origin, top_left_radius, rad_L, rad_TL, false);
path_builder.arc(origin, radius.top_left, rad_L, rad_TL, false);
path_builder.arc(origin, distance_to_elbow, rad_TL, rad_L, true);
}
path_builder.line_to(edge_TR);
path_builder.line_to(edge_BR);
if let Some(bottom_left_radius) = radius.bottom_left {
let origin = edge_BL + Point2D(bottom_left_radius, (border.bottom - bottom_left_radius).max(0.));
let distance_to_elbow = (bottom_left_radius - border.left).max(0.);
if radius.bottom_left != 0. {
let origin = edge_BL + Point2D(radius.bottom_left, (border.bottom - radius.bottom_left).max(0.));
let distance_to_elbow = (radius.bottom_left - border.left).max(0.);
path_builder.arc(origin, distance_to_elbow, rad_L, rad_BL, true);
path_builder.arc(origin, bottom_left_radius, rad_BL, rad_L, false);
path_builder.arc(origin, radius.bottom_left, rad_BL, rad_L, false);
}
}
Right => {
let edge_TR = box_TR + dy(radius.top_right.unwrap_or(0.).max(border.top));
let edge_BR = box_BR + dy(-radius.bottom_right.unwrap_or(0.).max(border.bottom));
let edge_TR = box_TR + dy(radius.top_right.max(border.top));
let edge_BR = box_BR + dy(-radius.bottom_right.max(border.bottom));
let edge_TL = edge_TR + dx(-border.right);
let edge_BL = edge_BR + dx(-border.right);
let corner_TR = edge_TR + dy_if(radius.top_right.is_none(), -border.top);
let corner_BR = edge_BR + dy_if(radius.bottom_right.is_none(), border.bottom);
let corner_TR = edge_TR + dy_if(radius.top_right == 0., -border.top);
let corner_BR = edge_BR + dy_if(radius.bottom_right == 0., border.bottom);
path_builder.move_to(edge_BL);
path_builder.line_to(edge_TL);
if let Some(top_right_radius) = radius.top_right {
let origin = edge_TR + Point2D(-top_right_radius, -(border.top - top_right_radius).max(0.));
let distance_to_elbow = (top_right_radius - border.right).max(0.);
if radius.top_right != 0. {
let origin = edge_TR + Point2D(-radius.top_right, -(border.top - radius.top_right).max(0.));
let distance_to_elbow = (radius.top_right - border.right).max(0.);
path_builder.arc(origin, distance_to_elbow, rad_R, rad_TR, true);
path_builder.arc(origin, top_right_radius, rad_TR, rad_R, false);
path_builder.arc(origin, radius.top_right, rad_TR, rad_R, false);
}
path_builder.line_to(corner_TR);
path_builder.line_to(corner_BR);
if let Some(bottom_right_radius) = radius.bottom_right {
let origin = edge_BR + Point2D(-bottom_right_radius, (border.bottom - bottom_right_radius).max(0.));
let distance_to_elbow = (bottom_right_radius - border.right).max(0.);
if radius.bottom_right != 0. {
let origin = edge_BR + Point2D(-radius.bottom_right, (border.bottom - radius.bottom_right).max(0.));
let distance_to_elbow = (radius.bottom_right - border.right).max(0.);
path_builder.arc(origin, bottom_right_radius, rad_R, rad_BR, false);
path_builder.arc(origin, radius.bottom_right, rad_R, rad_BR, false);
path_builder.arc(origin, distance_to_elbow, rad_BR, rad_R, true);
}
}
Bottom => {
let edge_BL = box_BL + dx(radius.bottom_left.unwrap_or(0.).max(border.left));
let edge_BR = box_BR + dx(-radius.bottom_right.unwrap_or(0.).max(border.right));
let edge_BL = box_BL + dx(radius.bottom_left.max(border.left));
let edge_BR = box_BR + dx(-radius.bottom_right.max(border.right));
let edge_TL = edge_BL + dy(-border.bottom);
let edge_TR = edge_BR + dy(-border.bottom);
let corner_BR = edge_BR + dx_if(radius.bottom_right.is_none(), border.right);
let corner_BL = edge_BL + dx_if(radius.bottom_left.is_none(), -border.left);
let corner_BR = edge_BR + dx_if(radius.bottom_right == 0., border.right);
let corner_BL = edge_BL + dx_if(radius.bottom_left == 0., -border.left);
path_builder.move_to(edge_TL);
path_builder.line_to(edge_TR);
if let Some(bottom_right_radius) = radius.bottom_right {
let origin = edge_BR + Point2D((border.right - bottom_right_radius).max(0.), -bottom_right_radius);
let distance_to_elbow = (bottom_right_radius - border.bottom).max(0.);
if radius.bottom_right != 0. {
let origin = edge_BR + Point2D((border.right - radius.bottom_right).max(0.), -radius.bottom_right);
let distance_to_elbow = (radius.bottom_right - border.bottom).max(0.);
path_builder.arc(origin, distance_to_elbow, rad_B, rad_BR, true);
path_builder.arc(origin, bottom_right_radius, rad_BR, rad_B, false);
path_builder.arc(origin, radius.bottom_right, rad_BR, rad_B, false);
}
path_builder.line_to(corner_BR);
path_builder.line_to(corner_BL);
if let Some(bottom_left_radius) = radius.bottom_left {
let origin = edge_BL - Point2D((border.left - bottom_left_radius).max(0.), bottom_left_radius);
let distance_to_elbow = (bottom_left_radius - border.bottom).max(0.);
if radius.bottom_left != 0. {
let origin = edge_BL - Point2D((border.left - radius.bottom_left).max(0.), radius.bottom_left);
let distance_to_elbow = (radius.bottom_left - border.bottom).max(0.);
path_builder.arc(origin, bottom_left_radius, rad_B, rad_BL, false);
path_builder.arc(origin, radius.bottom_left, rad_B, rad_BL, false);
path_builder.arc(origin, distance_to_elbow, rad_BL, rad_B, true);
}
}
@ -530,7 +530,7 @@ impl<'a> RenderContext<'a> {
&draw_opts);
}
fn draw_solid_border_segment(&self, direction: Direction, bounds: &Rect<Au>, border: SideOffsets2D<f32>, radius: &BorderRadii<Option<AzFloat>>, color: Color) {
fn draw_solid_border_segment(&self, direction: Direction, bounds: &Rect<Au>, border: SideOffsets2D<f32>, radius: &BorderRadii<AzFloat>, color: Color) {
let rect = bounds.to_azure_rect();
self.draw_border_path(&rect, direction, border, radius, color);
}
@ -555,7 +555,7 @@ impl<'a> RenderContext<'a> {
return Color::new(color.r * scale_factor, color.g * scale_factor, color.b * scale_factor, color.a);
}
fn draw_double_border_segment(&self, direction: Direction, bounds: &Rect<Au>, border: SideOffsets2D<f32>, radius: &BorderRadii<Option<AzFloat>>, color: Color) {
fn draw_double_border_segment(&self, direction: Direction, bounds: &Rect<Au>, border: SideOffsets2D<f32>, radius: &BorderRadii<AzFloat>, color: Color) {
let scaled_border = SideOffsets2D::new((1.0/3.0) * border.top,
(1.0/3.0) * border.right,
(1.0/3.0) * border.bottom,
@ -571,7 +571,7 @@ impl<'a> RenderContext<'a> {
direction: Direction,
bounds: &Rect<Au>,
border: SideOffsets2D<f32>,
radius: &BorderRadii<Option<AzFloat>>,
radius: &BorderRadii<AzFloat>,
color: Color,
style: border_style::T) {
// original bounds as a Rect<f32>, with no scaling.
@ -602,7 +602,7 @@ impl<'a> RenderContext<'a> {
direction: Direction,
bounds: &Rect<Au>,
border: SideOffsets2D<f32>,
radius: &BorderRadii<Option<AzFloat>>,
radius: &BorderRadii<AzFloat>,
color: Color,
style: border_style::T) {
let is_inset = match style {
@ -723,17 +723,13 @@ impl ToSideOffsetsPx for SideOffsets2D<Au> {
}
trait ToRadiiPx {
fn to_radii_px(&self) -> BorderRadii<Option<AzFloat>>;
fn to_radii_px(&self) -> BorderRadii<AzFloat>;
}
impl ToRadiiPx for BorderRadii<Au> {
fn to_radii_px(&self) -> BorderRadii<Option<AzFloat>> {
fn to_nearest_px(x: Au) -> Option<AzFloat> {
if x == Au(0) {
None
} else {
Some(x.to_nearest_px() as AzFloat)
}
fn to_radii_px(&self) -> BorderRadii<AzFloat> {
fn to_nearest_px(x: Au) -> AzFloat {
x.to_nearest_px() as AzFloat
}
BorderRadii {