From 4c249968d90ecf434c0308e2ea3558d312b6f359 Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Tue, 10 May 2016 10:58:03 +1000 Subject: [PATCH] Round border-*-width values to pixels in geckolib. --- ports/geckolib/properties.mako.rs | 11 +++++++++-- ports/geckolib/values.rs | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ports/geckolib/properties.mako.rs b/ports/geckolib/properties.mako.rs index 4037ea58fe1..a4d5b2a3cd1 100644 --- a/ports/geckolib/properties.mako.rs +++ b/ports/geckolib/properties.mako.rs @@ -31,6 +31,7 @@ use style::properties::make_cascade_vec; use style::properties::style_struct_traits::*; use gecko_style_structs::{nsStyleUnion, nsStyleUnit}; use values::{ToGeckoStyleCoord, convert_rgba_to_nscolor, convert_nscolor_to_rgba}; +use values::round_border_to_device_pixels; #[derive(Clone)] pub struct GeckoComputedValues { @@ -264,9 +265,14 @@ def set_gecko_property(ffi_name, expr): <%call expr="impl_color_copy(ident, gecko_ffi_name, color_flags_ffi_name)"> -<%def name="impl_app_units(ident, gecko_ffi_name, need_clone)"> +<%def name="impl_app_units(ident, gecko_ffi_name, need_clone, round_to_pixels=False)"> fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) { + % if round_to_pixels: + let au_per_device_px = Au(self.gecko.mTwipsPerPixel); + self.gecko.${gecko_ffi_name} = round_border_to_device_pixels(v, au_per_device_px).0; + % else: self.gecko.${gecko_ffi_name} = v.0; + % endif } <%call expr="impl_simple_copy(ident, gecko_ffi_name)"> %if need_clone: @@ -499,7 +505,8 @@ fn static_assert() { <% impl_color("border_%s_color" % side.ident, "mBorderColor[%s]" % side.index, color_flags_ffi_name="mBorderStyle[%s]" % side.index) %> - <% impl_app_units("border_%s_width" % side.ident, "mComputedBorder.%s" % side.ident, need_clone=False) %> + <% impl_app_units("border_%s_width" % side.ident, "mComputedBorder.%s" % side.ident, need_clone=False, + round_to_pixels=True) %> fn border_${side.ident}_has_nonzero_width(&self) -> bool { self.gecko.mComputedBorder.${side.ident} != 0 diff --git a/ports/geckolib/values.rs b/ports/geckolib/values.rs index 172ce193b8a..03dd8ffeb58 100644 --- a/ports/geckolib/values.rs +++ b/ports/geckolib/values.rs @@ -2,8 +2,10 @@ * 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/. */ +use app_units::Au; use cssparser::RGBA; use gecko_style_structs::{nsStyleUnion, nsStyleUnit}; +use std::cmp::max; use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto, LengthOrPercentageOrNone}; pub trait ToGeckoStyleCoord { @@ -81,3 +83,15 @@ pub fn convert_nscolor_to_rgba(color: u32) -> RGBA { alpha: (((color >> 24) & 0xff) as f32) / 255.0, } } + +#[inline] +pub fn round_border_to_device_pixels(width: Au, au_per_device_px: Au) -> Au { + // Round width down to the nearest device pixel, but any non-zero value that + // would round down to zero is clamped to 1 device pixel. Used for storing + // computed values of border-*-width and outline-width. + if width == Au(0) { + Au(0) + } else { + max(au_per_device_px, Au(width.0 / au_per_device_px.0 * au_per_device_px.0)) + } +}