2014-07-09 21:04:59 +00:00
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* 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/. */
|
|
|
|
|
|
2014-11-10 12:33:35 +00:00
|
|
|
|
//! Geometry in flow-relative space.
|
2014-07-09 21:04:59 +00:00
|
|
|
|
|
|
|
|
|
use geom::{Size2D, Point2D, SideOffsets2D, Rect};
|
2014-12-18 11:42:50 +00:00
|
|
|
|
use geom::num::Zero;
|
2014-07-18 20:27:23 +00:00
|
|
|
|
use std::cmp::{min, max};
|
2015-02-12 00:24:45 +00:00
|
|
|
|
use std::fmt::{Debug, Formatter, Error};
|
2015-01-28 01:15:50 +00:00
|
|
|
|
use std::ops::{Add, Sub};
|
2014-07-09 21:04:59 +00:00
|
|
|
|
|
|
|
|
|
bitflags!(
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(RustcEncodable)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
flags WritingMode: u8 {
|
2014-11-13 03:48:31 +00:00
|
|
|
|
const FLAG_RTL = 1 << 0,
|
|
|
|
|
const FLAG_VERTICAL = 1 << 1,
|
|
|
|
|
const FLAG_VERTICAL_LR = 1 << 2,
|
|
|
|
|
const FLAG_SIDEWAYS_LEFT = 1 << 3
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
2015-01-28 01:15:50 +00:00
|
|
|
|
);
|
2014-07-09 21:04:59 +00:00
|
|
|
|
|
|
|
|
|
impl WritingMode {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_vertical(&self) -> bool {
|
2014-11-13 03:48:31 +00:00
|
|
|
|
self.intersects(FLAG_VERTICAL)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-12 09:12:51 +00:00
|
|
|
|
/// Assuming .is_vertical(), does the block direction go left to right?
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_vertical_lr(&self) -> bool {
|
2014-11-13 03:48:31 +00:00
|
|
|
|
self.intersects(FLAG_VERTICAL_LR)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-12 09:12:51 +00:00
|
|
|
|
/// Assuming .is_vertical(), does the inline direction go top to bottom?
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_inline_tb(&self) -> bool {
|
2014-11-13 03:48:31 +00:00
|
|
|
|
!(self.intersects(FLAG_SIDEWAYS_LEFT) ^ self.intersects(FLAG_RTL))
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_bidi_ltr(&self) -> bool {
|
2014-11-13 03:48:31 +00:00
|
|
|
|
!self.intersects(FLAG_RTL)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
2014-08-29 16:51:26 +00:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_sideways_left(&self) -> bool {
|
2014-11-13 03:48:31 +00:00
|
|
|
|
self.intersects(FLAG_SIDEWAYS_LEFT)
|
2014-08-29 16:51:26 +00:00
|
|
|
|
}
|
2015-04-27 17:29:15 +00:00
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn inline_start_physical_side(&self) -> PhysicalSide {
|
|
|
|
|
match (self.is_vertical(), self.is_inline_tb(), self.is_bidi_ltr()) {
|
|
|
|
|
(false, _, true) => PhysicalSide::Left,
|
|
|
|
|
(false, _, false) => PhysicalSide::Right,
|
|
|
|
|
(true, true, _) => PhysicalSide::Top,
|
|
|
|
|
(true, false, _) => PhysicalSide::Bottom,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn inline_end_physical_side(&self) -> PhysicalSide {
|
|
|
|
|
match (self.is_vertical(), self.is_inline_tb(), self.is_bidi_ltr()) {
|
|
|
|
|
(false, _, true) => PhysicalSide::Right,
|
|
|
|
|
(false, _, false) => PhysicalSide::Left,
|
|
|
|
|
(true, true, _) => PhysicalSide::Bottom,
|
|
|
|
|
(true, false, _) => PhysicalSide::Top,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn block_start_physical_side(&self) -> PhysicalSide {
|
|
|
|
|
match (self.is_vertical(), self.is_vertical_lr()) {
|
|
|
|
|
(false, _) => PhysicalSide::Top,
|
|
|
|
|
(true, true) => PhysicalSide::Left,
|
|
|
|
|
(true, false) => PhysicalSide::Right,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn block_end_physical_side(&self) -> PhysicalSide {
|
|
|
|
|
match (self.is_vertical(), self.is_vertical_lr()) {
|
|
|
|
|
(false, _) => PhysicalSide::Bottom,
|
|
|
|
|
(true, true) => PhysicalSide::Right,
|
|
|
|
|
(true, false) => PhysicalSide::Left,
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 00:24:45 +00:00
|
|
|
|
impl Debug for WritingMode {
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if self.is_vertical() {
|
|
|
|
|
try!(write!(formatter, "V"));
|
|
|
|
|
if self.is_vertical_lr() {
|
|
|
|
|
try!(write!(formatter, " LR"));
|
|
|
|
|
} else {
|
|
|
|
|
try!(write!(formatter, " RL"));
|
|
|
|
|
}
|
2014-11-13 03:48:31 +00:00
|
|
|
|
if self.intersects(FLAG_SIDEWAYS_LEFT) {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
try!(write!(formatter, " SidewaysL"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try!(write!(formatter, "H"));
|
|
|
|
|
}
|
|
|
|
|
if self.is_bidi_ltr() {
|
|
|
|
|
write!(formatter, " LTR")
|
|
|
|
|
} else {
|
|
|
|
|
write!(formatter, " RTL")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Wherever logical geometry is used, the writing mode is known based on context:
|
|
|
|
|
/// every method takes a `mode` parameter.
|
|
|
|
|
/// However, this context is easy to get wrong.
|
|
|
|
|
/// In debug builds only, logical geometry objects store their writing mode
|
|
|
|
|
/// (in addition to taking it as a parameter to methods) and check it.
|
|
|
|
|
/// In non-debug builds, make this storage zero-size and the checks no-ops.
|
|
|
|
|
#[cfg(ndebug)]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(RustcEncodable, PartialEq, Eq, Clone, Copy)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
struct DebugWritingMode;
|
|
|
|
|
|
|
|
|
|
#[cfg(not(ndebug))]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(RustcEncodable, PartialEq, Eq, Clone, Copy)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
struct DebugWritingMode {
|
|
|
|
|
mode: WritingMode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(ndebug)]
|
|
|
|
|
impl DebugWritingMode {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn check(&self, _other: WritingMode) {}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn check_debug(&self, _other: DebugWritingMode) {}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn new(_mode: WritingMode) -> DebugWritingMode {
|
|
|
|
|
DebugWritingMode
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(ndebug))]
|
|
|
|
|
impl DebugWritingMode {
|
|
|
|
|
#[inline]
|
|
|
|
|
fn check(&self, other: WritingMode) {
|
|
|
|
|
assert!(self.mode == other)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn check_debug(&self, other: DebugWritingMode) {
|
|
|
|
|
assert!(self.mode == other.mode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn new(mode: WritingMode) -> DebugWritingMode {
|
|
|
|
|
DebugWritingMode { mode: mode }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 00:24:45 +00:00
|
|
|
|
impl Debug for DebugWritingMode {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[cfg(ndebug)]
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
write!(formatter, "?")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(ndebug))]
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.mode.fmt(formatter)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A 2D size in flow-relative dimensions
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(RustcEncodable, PartialEq, Eq, Clone, Copy)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
pub struct LogicalSize<T> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub inline: T, // inline-size, a.k.a. logical width, a.k.a. measure
|
|
|
|
|
pub block: T, // block-size, a.k.a. logical height, a.k.a. extent
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode,
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 00:24:45 +00:00
|
|
|
|
impl<T: Debug> Debug for LogicalSize<T> {
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2015-01-28 01:15:50 +00:00
|
|
|
|
write!(formatter, "LogicalSize({:?}, i{:?}×b{:?})",
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.debug_writing_mode, self.inline, self.block)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Can not implement the Zero trait: its zero() method does not have the `mode` parameter.
|
|
|
|
|
impl<T: Zero> LogicalSize<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn zero(mode: WritingMode) -> LogicalSize<T> {
|
|
|
|
|
LogicalSize {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline: Zero::zero(),
|
|
|
|
|
block: Zero::zero(),
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Copy> LogicalSize<T> {
|
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn new(mode: WritingMode, inline: T, block: T) -> LogicalSize<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
LogicalSize {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline: inline,
|
|
|
|
|
block: block,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_physical(mode: WritingMode, size: Size2D<T>) -> LogicalSize<T> {
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
LogicalSize::new(mode, size.height, size.width)
|
|
|
|
|
} else {
|
|
|
|
|
LogicalSize::new(mode, size.width, size.height)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn width(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.inline
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_width(&mut self, mode: WritingMode, width: T) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block = width
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.inline = width
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn height(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.inline
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_height(&mut self, mode: WritingMode, height: T) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.inline = height
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block = height
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn to_physical(&self, mode: WritingMode) -> Size2D<T> {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
Size2D { width: self.block, height: self.inline }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
Size2D { width: self.inline, height: self.block }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn convert(&self, mode_from: WritingMode, mode_to: WritingMode) -> LogicalSize<T> {
|
|
|
|
|
if mode_from == mode_to {
|
|
|
|
|
self.debug_writing_mode.check(mode_from);
|
|
|
|
|
*self
|
|
|
|
|
} else {
|
|
|
|
|
LogicalSize::from_physical(mode_to, self.to_physical(mode_from))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Add<T, Output=T>> Add for LogicalSize<T> {
|
|
|
|
|
type Output = LogicalSize<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn add(self, other: LogicalSize<T>) -> LogicalSize<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalSize {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline: self.inline + other.inline,
|
|
|
|
|
block: self.block + other.block,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Sub<T, Output=T>> Sub for LogicalSize<T> {
|
|
|
|
|
type Output = LogicalSize<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn sub(self, other: LogicalSize<T>) -> LogicalSize<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalSize {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline: self.inline - other.inline,
|
|
|
|
|
block: self.block - other.block,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A 2D point in flow-relative dimensions
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(PartialEq, RustcEncodable, Eq, Clone, Copy)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
pub struct LogicalPoint<T> {
|
|
|
|
|
pub i: T, /// inline-axis coordinate
|
|
|
|
|
pub b: T, /// block-axis coordinate
|
|
|
|
|
debug_writing_mode: DebugWritingMode,
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 00:24:45 +00:00
|
|
|
|
impl<T: Debug> Debug for LogicalPoint<T> {
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2015-01-28 01:15:50 +00:00
|
|
|
|
write!(formatter, "LogicalPoint({:?} (i{:?}, b{:?}))",
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode, self.i, self.b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Can not implement the Zero trait: its zero() method does not have the `mode` parameter.
|
|
|
|
|
impl<T: Zero> LogicalPoint<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn zero(mode: WritingMode) -> LogicalPoint<T> {
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
i: Zero::zero(),
|
|
|
|
|
b: Zero::zero(),
|
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Copy> LogicalPoint<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new(mode: WritingMode, i: T, b: T) -> LogicalPoint<T> {
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
i: i,
|
|
|
|
|
b: b,
|
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Sub<T, Output=T>> LogicalPoint<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_physical(mode: WritingMode, point: Point2D<T>, container_size: Size2D<T>)
|
|
|
|
|
-> LogicalPoint<T> {
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
i: if mode.is_inline_tb() { point.y } else { container_size.height - point.y },
|
|
|
|
|
b: if mode.is_vertical_lr() { point.x } else { container_size.width - point.x },
|
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
i: if mode.is_bidi_ltr() { point.x } else { container_size.width - point.x },
|
|
|
|
|
b: point.y,
|
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn x(&self, mode: WritingMode, container_size: Size2D<T>) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
if mode.is_vertical_lr() { self.b } else { container_size.width - self.b }
|
|
|
|
|
} else {
|
|
|
|
|
if mode.is_bidi_ltr() { self.i } else { container_size.width - self.i }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_x(&mut self, mode: WritingMode, x: T, container_size: Size2D<T>) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
self.b = if mode.is_vertical_lr() { x } else { container_size.width - x }
|
|
|
|
|
} else {
|
|
|
|
|
self.i = if mode.is_bidi_ltr() { x } else { container_size.width - x }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn y(&self, mode: WritingMode, container_size: Size2D<T>) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
if mode.is_inline_tb() { self.i } else { container_size.height - self.i }
|
|
|
|
|
} else {
|
|
|
|
|
self.b
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_y(&mut self, mode: WritingMode, y: T, container_size: Size2D<T>) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
self.i = if mode.is_inline_tb() { y } else { container_size.height - y }
|
|
|
|
|
} else {
|
|
|
|
|
self.b = y
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn to_physical(&self, mode: WritingMode, container_size: Size2D<T>) -> Point2D<T> {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
Point2D {
|
|
|
|
|
x: if mode.is_vertical_lr() { self.b } else { container_size.width - self.b },
|
|
|
|
|
y: if mode.is_inline_tb() { self.i } else { container_size.height - self.i }
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Point2D {
|
|
|
|
|
x: if mode.is_bidi_ltr() { self.i } else { container_size.width - self.i },
|
|
|
|
|
y: self.b
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn convert(&self, mode_from: WritingMode, mode_to: WritingMode, container_size: Size2D<T>)
|
|
|
|
|
-> LogicalPoint<T> {
|
|
|
|
|
if mode_from == mode_to {
|
|
|
|
|
self.debug_writing_mode.check(mode_from);
|
|
|
|
|
*self
|
|
|
|
|
} else {
|
|
|
|
|
LogicalPoint::from_physical(
|
|
|
|
|
mode_to, self.to_physical(mode_from, container_size), container_size)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Add<T, Output=T>> LogicalPoint<T> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
/// This doesn’t really makes sense,
|
2014-12-12 09:12:51 +00:00
|
|
|
|
/// but happens when dealing with multiple origins.
|
2014-07-18 20:27:23 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn add_point(&self, other: &LogicalPoint<T>) -> LogicalPoint<T> {
|
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
i: self.i + other.i,
|
|
|
|
|
b: self.b + other.b,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Add<T,Output=T>> Add<LogicalSize<T>> for LogicalPoint<T> {
|
|
|
|
|
type Output = LogicalPoint<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn add(self, other: LogicalSize<T>) -> LogicalPoint<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
2014-07-18 20:27:23 +00:00
|
|
|
|
i: self.i + other.inline,
|
|
|
|
|
b: self.b + other.block,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Sub<T,Output=T>> Sub<LogicalSize<T>> for LogicalPoint<T> {
|
|
|
|
|
type Output = LogicalPoint<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn sub(self, other: LogicalSize<T>) -> LogicalPoint<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalPoint {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
2014-07-18 20:27:23 +00:00
|
|
|
|
i: self.i - other.inline,
|
|
|
|
|
b: self.b - other.block,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A "margin" in flow-relative dimensions
|
|
|
|
|
/// Represents the four sides of the margins, borders, or padding of a CSS box,
|
|
|
|
|
/// or a combination of those.
|
|
|
|
|
/// A positive "margin" can be added to a rectangle to obtain a bigger rectangle.
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(RustcEncodable, PartialEq, Eq, Clone, Copy)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
pub struct LogicalMargin<T> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub block_start: T,
|
|
|
|
|
pub inline_end: T,
|
|
|
|
|
pub block_end: T,
|
|
|
|
|
pub inline_start: T,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode,
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 00:24:45 +00:00
|
|
|
|
impl<T: Debug> Debug for LogicalMargin<T> {
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
write!(formatter,
|
2015-01-28 01:15:50 +00:00
|
|
|
|
"LogicalMargin({:?}, inline: {:?}..{:?} block: {:?}..{:?})",
|
2014-10-22 01:00:37 +00:00
|
|
|
|
self.debug_writing_mode,
|
|
|
|
|
self.inline_start,
|
|
|
|
|
self.inline_end,
|
|
|
|
|
self.block_start,
|
|
|
|
|
self.block_end)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Zero> LogicalMargin<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn zero(mode: WritingMode) -> LogicalMargin<T> {
|
|
|
|
|
LogicalMargin {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start: Zero::zero(),
|
|
|
|
|
inline_end: Zero::zero(),
|
|
|
|
|
block_end: Zero::zero(),
|
|
|
|
|
inline_start: Zero::zero(),
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Copy> LogicalMargin<T> {
|
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn new(mode: WritingMode, block_start: T, inline_end: T, block_end: T, inline_start: T)
|
|
|
|
|
-> LogicalMargin<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
LogicalMargin {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start: block_start,
|
|
|
|
|
inline_end: inline_end,
|
|
|
|
|
block_end: block_end,
|
|
|
|
|
inline_start: inline_start,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 20:27:23 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn new_all_same(mode: WritingMode, value: T) -> LogicalMargin<T> {
|
|
|
|
|
LogicalMargin::new(mode, value, value, value, value)
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_physical(mode: WritingMode, offsets: SideOffsets2D<T>) -> LogicalMargin<T> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
let block_start;
|
|
|
|
|
let inline_end;
|
|
|
|
|
let block_end;
|
|
|
|
|
let inline_start;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
if mode.is_vertical_lr() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start = offsets.left;
|
|
|
|
|
block_end = offsets.right;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start = offsets.right;
|
|
|
|
|
block_end = offsets.left;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
if mode.is_inline_tb() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = offsets.top;
|
|
|
|
|
inline_end = offsets.bottom;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = offsets.bottom;
|
|
|
|
|
inline_end = offsets.top;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start = offsets.top;
|
|
|
|
|
block_end = offsets.bottom;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_bidi_ltr() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = offsets.left;
|
|
|
|
|
inline_end = offsets.right;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = offsets.right;
|
|
|
|
|
inline_end = offsets.left;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-18 20:27:23 +00:00
|
|
|
|
LogicalMargin::new(mode, block_start, inline_end, block_end, inline_start)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn top(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_inline_tb() { self.inline_start } else { self.inline_end }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block_start
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_top(&mut self, mode: WritingMode, top: T) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_inline_tb() { self.inline_start = top } else { self.inline_end = top }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block_start = top
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn right(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_vertical_lr() { self.block_end } else { self.block_start }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_bidi_ltr() { self.inline_end } else { self.inline_start }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_right(&mut self, mode: WritingMode, right: T) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_vertical_lr() { self.block_end = right } else { self.block_start = right }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_bidi_ltr() { self.inline_end = right } else { self.inline_start = right }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn bottom(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_inline_tb() { self.inline_end } else { self.inline_start }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block_end
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_bottom(&mut self, mode: WritingMode, bottom: T) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_inline_tb() { self.inline_end = bottom } else { self.inline_start = bottom }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block_end = bottom
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn left(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_vertical_lr() { self.block_start } else { self.block_end }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_bidi_ltr() { self.inline_start } else { self.inline_end }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn set_left(&mut self, mode: WritingMode, left: T) {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_vertical_lr() { self.block_start = left } else { self.block_end = left }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
if mode.is_bidi_ltr() { self.inline_start = left } else { self.inline_end = left }
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn to_physical(&self, mode: WritingMode) -> SideOffsets2D<T> {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
let top;
|
|
|
|
|
let right;
|
|
|
|
|
let bottom;
|
|
|
|
|
let left;
|
|
|
|
|
if mode.is_vertical() {
|
|
|
|
|
if mode.is_vertical_lr() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
left = self.block_start;
|
|
|
|
|
right = self.block_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
right = self.block_start;
|
|
|
|
|
left = self.block_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
if mode.is_inline_tb() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
top = self.inline_start;
|
|
|
|
|
bottom = self.inline_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
bottom = self.inline_start;
|
|
|
|
|
top = self.inline_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
top = self.block_start;
|
|
|
|
|
bottom = self.block_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_bidi_ltr() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
left = self.inline_start;
|
|
|
|
|
right = self.inline_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
right = self.inline_start;
|
|
|
|
|
left = self.inline_end;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SideOffsets2D::new(top, right, bottom, left)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn convert(&self, mode_from: WritingMode, mode_to: WritingMode) -> LogicalMargin<T> {
|
|
|
|
|
if mode_from == mode_to {
|
|
|
|
|
self.debug_writing_mode.check(mode_from);
|
|
|
|
|
*self
|
|
|
|
|
} else {
|
|
|
|
|
LogicalMargin::from_physical(mode_to, self.to_physical(mode_from))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-18 11:42:50 +00:00
|
|
|
|
impl<T: PartialEq + Zero> LogicalMargin<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn is_zero(&self) -> bool {
|
|
|
|
|
self.block_start == Zero::zero() && self.inline_end == Zero::zero() &&
|
|
|
|
|
self.block_end == Zero::zero() && self.inline_start == Zero::zero()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Add<T, Output=T>> LogicalMargin<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn inline_start_end(&self) -> T {
|
|
|
|
|
self.inline_start + self.inline_end
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn block_start_end(&self) -> T {
|
|
|
|
|
self.block_start + self.block_end
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn top_bottom(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.inline_start_end()
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block_start_end()
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn left_right(&self, mode: WritingMode) -> T {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.block_start_end()
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
self.inline_start_end()
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Add<T, Output=T>> Add for LogicalMargin<T> {
|
|
|
|
|
type Output = LogicalMargin<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn add(self, other: LogicalMargin<T>) -> LogicalMargin<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalMargin {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start: self.block_start + other.block_start,
|
|
|
|
|
inline_end: self.inline_end + other.inline_end,
|
|
|
|
|
block_end: self.block_end + other.block_end,
|
|
|
|
|
inline_start: self.inline_start + other.inline_start,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Sub<T, Output=T>> Sub for LogicalMargin<T> {
|
|
|
|
|
type Output = LogicalMargin<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn sub(self, other: LogicalMargin<T>) -> LogicalMargin<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalMargin {
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start: self.block_start - other.block_start,
|
|
|
|
|
inline_end: self.inline_end - other.inline_end,
|
|
|
|
|
block_end: self.block_end - other.block_end,
|
|
|
|
|
inline_start: self.inline_start - other.inline_start,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A rectangle in flow-relative dimensions
|
2015-01-28 01:15:50 +00:00
|
|
|
|
#[derive(RustcEncodable, PartialEq, Eq, Clone, Copy)]
|
2014-07-09 21:04:59 +00:00
|
|
|
|
pub struct LogicalRect<T> {
|
|
|
|
|
pub start: LogicalPoint<T>,
|
|
|
|
|
pub size: LogicalSize<T>,
|
|
|
|
|
debug_writing_mode: DebugWritingMode,
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 00:24:45 +00:00
|
|
|
|
impl<T: Debug> Debug for LogicalRect<T> {
|
2015-01-08 15:00:57 +00:00
|
|
|
|
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
write!(formatter,
|
2015-01-28 01:15:50 +00:00
|
|
|
|
"LogicalRect({:?}, i{:?}×b{:?}, @ (i{:?},b{:?}))",
|
2014-10-22 01:00:37 +00:00
|
|
|
|
self.debug_writing_mode,
|
|
|
|
|
self.size.inline,
|
|
|
|
|
self.size.block,
|
|
|
|
|
self.start.i,
|
|
|
|
|
self.start.b)
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Zero> LogicalRect<T> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn zero(mode: WritingMode) -> LogicalRect<T> {
|
|
|
|
|
LogicalRect {
|
|
|
|
|
start: LogicalPoint::zero(mode),
|
|
|
|
|
size: LogicalSize::zero(mode),
|
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: Copy> LogicalRect<T> {
|
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn new(mode: WritingMode, inline_start: T, block_start: T, inline: T, block: T)
|
|
|
|
|
-> LogicalRect<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
LogicalRect {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
start: LogicalPoint::new(mode, inline_start, block_start),
|
|
|
|
|
size: LogicalSize::new(mode, inline, block),
|
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_point_size(mode: WritingMode, start: LogicalPoint<T>, size: LogicalSize<T>)
|
|
|
|
|
-> LogicalRect<T> {
|
|
|
|
|
start.debug_writing_mode.check(mode);
|
|
|
|
|
size.debug_writing_mode.check(mode);
|
|
|
|
|
LogicalRect {
|
|
|
|
|
start: start,
|
|
|
|
|
size: size,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Add<T, Output=T> + Sub<T, Output=T>> LogicalRect<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn from_physical(mode: WritingMode, rect: Rect<T>, container_size: Size2D<T>)
|
|
|
|
|
-> LogicalRect<T> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
let inline_start;
|
|
|
|
|
let block_start;
|
|
|
|
|
let inline;
|
|
|
|
|
let block;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline = rect.size.height;
|
|
|
|
|
block = rect.size.width;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_vertical_lr() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start = rect.origin.x;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
block_start = container_size.width - (rect.origin.x + rect.size.width);
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
if mode.is_inline_tb() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = rect.origin.y;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = container_size.height - (rect.origin.y + rect.size.height);
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline = rect.size.width;
|
|
|
|
|
block = rect.size.height;
|
|
|
|
|
block_start = rect.origin.y;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_bidi_ltr() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = rect.origin.x;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline_start = container_size.width - (rect.origin.x + rect.size.width);
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LogicalRect {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
start: LogicalPoint::new(mode, inline_start, block_start),
|
|
|
|
|
size: LogicalSize::new(mode, inline, block),
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: DebugWritingMode::new(mode),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn inline_end(&self) -> T {
|
|
|
|
|
self.start.i + self.size.inline
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2014-07-18 20:27:23 +00:00
|
|
|
|
pub fn block_end(&self) -> T {
|
|
|
|
|
self.start.b + self.size.block
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn to_physical(&self, mode: WritingMode, container_size: Size2D<T>) -> Rect<T> {
|
|
|
|
|
self.debug_writing_mode.check(mode);
|
|
|
|
|
let x;
|
|
|
|
|
let y;
|
|
|
|
|
let width;
|
|
|
|
|
let height;
|
|
|
|
|
if mode.is_vertical() {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
width = self.size.block;
|
|
|
|
|
height = self.size.inline;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
if mode.is_vertical_lr() {
|
|
|
|
|
x = self.start.b;
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
x = container_size.width - self.block_end();
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
if mode.is_inline_tb() {
|
|
|
|
|
y = self.start.i;
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
y = container_size.height - self.inline_end();
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
width = self.size.inline;
|
|
|
|
|
height = self.size.block;
|
2014-07-09 21:04:59 +00:00
|
|
|
|
y = self.start.b;
|
|
|
|
|
if mode.is_bidi_ltr() {
|
|
|
|
|
x = self.start.i;
|
|
|
|
|
} else {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
x = container_size.width - self.inline_end();
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Rect {
|
|
|
|
|
origin: Point2D { x: x, y: y },
|
|
|
|
|
size: Size2D { width: width, height: height },
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn convert(&self, mode_from: WritingMode, mode_to: WritingMode, container_size: Size2D<T>)
|
|
|
|
|
-> LogicalRect<T> {
|
|
|
|
|
if mode_from == mode_to {
|
|
|
|
|
self.debug_writing_mode.check(mode_from);
|
|
|
|
|
*self
|
|
|
|
|
} else {
|
|
|
|
|
LogicalRect::from_physical(
|
|
|
|
|
mode_to, self.to_physical(mode_from, container_size), container_size)
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-18 20:27:23 +00:00
|
|
|
|
|
|
|
|
|
pub fn translate(&self, offset: &LogicalPoint<T>) -> LogicalRect<T> {
|
|
|
|
|
LogicalRect {
|
|
|
|
|
start: self.start + LogicalSize {
|
|
|
|
|
inline: offset.i,
|
|
|
|
|
block: offset.b,
|
|
|
|
|
debug_writing_mode: offset.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
size: self.size,
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Ord + Add<T, Output=T> + Sub<T, Output=T>> LogicalRect<T> {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
pub fn union(&self, other: &LogicalRect<T>) -> LogicalRect<T> {
|
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
|
|
|
|
|
let inline_start = min(self.start.i, other.start.i);
|
|
|
|
|
let block_start = min(self.start.b, other.start.b);
|
|
|
|
|
LogicalRect {
|
|
|
|
|
start: LogicalPoint {
|
|
|
|
|
i: inline_start,
|
|
|
|
|
b: block_start,
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
size: LogicalSize {
|
|
|
|
|
inline: max(self.inline_end(), other.inline_end()) - inline_start,
|
|
|
|
|
block: max(self.block_end(), other.block_end()) - block_start,
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-09 21:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Add<T, Output=T> + Sub<T, Output=T>> Add<LogicalMargin<T>> for LogicalRect<T> {
|
|
|
|
|
type Output = LogicalRect<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn add(self, other: LogicalMargin<T>) -> LogicalRect<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalRect {
|
|
|
|
|
start: LogicalPoint {
|
|
|
|
|
// Growing a rectangle on the start side means pushing its
|
|
|
|
|
// start point on the negative direction.
|
2014-07-18 20:27:23 +00:00
|
|
|
|
i: self.start.i - other.inline_start,
|
|
|
|
|
b: self.start.b - other.block_start,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
size: LogicalSize {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline: self.size.inline + other.inline_start_end(),
|
|
|
|
|
block: self.size.block + other.block_start_end(),
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-01-28 01:15:50 +00:00
|
|
|
|
impl<T: Copy + Add<T, Output=T> + Sub<T, Output=T>> Sub<LogicalMargin<T>> for LogicalRect<T> {
|
|
|
|
|
type Output = LogicalRect<T>;
|
|
|
|
|
|
2014-07-09 21:04:59 +00:00
|
|
|
|
#[inline]
|
2015-01-28 01:15:50 +00:00
|
|
|
|
fn sub(self, other: LogicalMargin<T>) -> LogicalRect<T> {
|
2014-07-09 21:04:59 +00:00
|
|
|
|
self.debug_writing_mode.check_debug(other.debug_writing_mode);
|
|
|
|
|
LogicalRect {
|
|
|
|
|
start: LogicalPoint {
|
|
|
|
|
// Shrinking a rectangle on the start side means pushing its
|
|
|
|
|
// start point on the positive direction.
|
2014-07-18 20:27:23 +00:00
|
|
|
|
i: self.start.i + other.inline_start,
|
|
|
|
|
b: self.start.b + other.block_start,
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
size: LogicalSize {
|
2014-07-18 20:27:23 +00:00
|
|
|
|
inline: self.size.inline - other.inline_start_end(),
|
|
|
|
|
block: self.size.block - other.block_start_end(),
|
2014-07-09 21:04:59 +00:00
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
},
|
|
|
|
|
debug_writing_mode: self.debug_writing_mode,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-27 17:29:15 +00:00
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
|
|
|
pub enum PhysicalSide {
|
|
|
|
|
Top,
|
|
|
|
|
Right,
|
|
|
|
|
Bottom,
|
|
|
|
|
Left,
|
|
|
|
|
}
|
|
|
|
|
|