use crate::{Error, LabelEndpoints, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Finite(u64);
impl Finite {
pub(crate) const fn constant(value: f64) -> Self {
Self(value.to_bits())
}
pub(crate) fn new(value: f64) -> Result<Self> {
if !value.is_finite() {
return Err(Error::Data("text layout coordinates must be finite".into()));
}
Ok(Self(if value == 0.0 { 0 } else { value.to_bits() }))
}
pub(crate) const fn get(self) -> f64 {
f64::from_bits(self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextAnchor {
pub(crate) horizontal: Finite,
pub(crate) vertical: VerticalAnchor,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum VerticalAnchor {
Fraction(Finite),
FirstBaseline,
CenterBaseline,
}
impl TextAnchor {
pub fn fractions(horizontal: f64, vertical: f64) -> Result<Self> {
Ok(Self {
horizontal: Finite::new(horizontal)?,
vertical: VerticalAnchor::Fraction(Finite::new(vertical)?),
})
}
pub fn first_baseline(horizontal: f64) -> Result<Self> {
Ok(Self {
horizontal: Finite::new(horizontal)?,
vertical: VerticalAnchor::FirstBaseline,
})
}
pub fn center_baseline(horizontal: f64) -> Result<Self> {
Ok(Self {
horizontal: Finite::new(horizontal)?,
vertical: VerticalAnchor::CenterBaseline,
})
}
#[must_use]
pub const fn get_horizontal(&self) -> f64 {
self.horizontal.get()
}
#[must_use]
pub const fn get_vertical_fraction(&self) -> Option<f64> {
match self.vertical {
VerticalAnchor::Fraction(value) => Some(value.get()),
VerticalAnchor::FirstBaseline | VerticalAnchor::CenterBaseline => None,
}
}
#[must_use]
pub const fn is_first_baseline(&self) -> bool {
matches!(self.vertical, VerticalAnchor::FirstBaseline)
}
}
macro_rules! anchors {
($($name:ident: ($x:literal,$y:literal)),+ $(,)?) => {impl TextAnchor {$(
#[doc = concat!("Attach the block's `",stringify!($name),"` point.")]
pub const fn $name() -> Self {Self {horizontal:Finite(($x as f64).to_bits()),vertical:VerticalAnchor::Fraction(Finite(($y as f64).to_bits()))}}
)+}};
}
anchors! {top_left:(0.0,0.0),top_center:(0.5,0.0),top_right:(1.0,0.0),center_left:(0.0,0.5),center:(0.5,0.5),center_right:(1.0,0.5),bottom_left:(0.0,1.0),bottom_center:(0.5,1.0),bottom_right:(1.0,1.0)}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextOffsetFrame {
Screen,
TextLocal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextOffset {
pub(crate) dx: Finite,
pub(crate) dy: Finite,
pub(crate) frame: TextOffsetFrame,
}
impl TextOffset {
pub fn new(dx: f64, dy: f64, frame: TextOffsetFrame) -> Result<Self> {
Ok(Self {
dx: Finite::new(dx)?,
dy: Finite::new(dy)?,
frame,
})
}
#[must_use]
pub const fn get_dx(&self) -> f64 {
self.dx.get()
}
#[must_use]
pub const fn get_dy(&self) -> f64 {
self.dy.get()
}
#[must_use]
pub const fn get_frame(&self) -> TextOffsetFrame {
self.frame
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextBounds {
pub(crate) width: Option<Finite>,
pub(crate) height: Option<Finite>,
pub(crate) lines: Option<usize>,
}
impl TextBounds {
pub fn new(
max_width: Option<f64>,
max_height: Option<f64>,
max_lines: Option<usize>,
) -> Result<Self> {
let bound = |value: f64| {
if value < 0.0 {
return Err(Error::Data("text bounds must be nonnegative".into()));
}
Finite::new(value)
};
Ok(Self {
width: max_width.map(bound).transpose()?,
height: max_height.map(bound).transpose()?,
lines: max_lines,
})
}
#[must_use]
pub const fn get_max_width(&self) -> Option<f64> {
match self.width {
Some(value) => Some(value.get()),
None => None,
}
}
#[must_use]
pub const fn get_max_height(&self) -> Option<f64> {
match self.height {
Some(value) => Some(value.get()),
None => None,
}
}
#[must_use]
pub const fn get_max_lines(&self) -> Option<usize> {
self.lines
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextWrapping {
Legal,
NoSoftWrap,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextDirection {
#[default]
Auto,
LeftToRight,
RightToLeft,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextElision {
Start,
Middle,
End,
Left,
Right,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextEllipsis {
pub(crate) position: TextElision,
pub(crate) marker: String,
pub(crate) lines: Option<usize>,
pub(crate) distinct: Option<bool>,
}
impl TextEllipsis {
pub fn new(position: TextElision) -> Self {
Self {
position,
marker: "…".into(),
lines: Some(1),
distinct: None,
}
}
#[must_use]
pub fn marker(mut self, text: impl Into<String>) -> Self {
self.marker = text.into();
self
}
#[must_use]
pub const fn max_lines(mut self, value: Option<usize>) -> Self {
self.lines = value;
self
}
#[must_use]
pub const fn distinct(mut self, value: bool) -> Self {
self.distinct = Some(value);
self
}
#[must_use]
pub const fn get_position(&self) -> TextElision {
self.position
}
#[must_use]
pub fn get_marker(&self) -> &str {
&self.marker
}
#[must_use]
pub const fn get_max_lines(&self) -> Option<usize> {
self.lines
}
#[must_use]
pub const fn get_distinct(&self) -> Option<bool> {
self.distinct
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextOverlap {
RequireNonOverlap,
Allow,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextFailure {
Error,
Hide,
Clip,
Visible,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextOverflow {
pub(crate) failure: TextFailure,
pub(crate) ellipsis: Option<TextEllipsis>,
}
impl TextOverflow {
pub const fn complete() -> Self {
Self {
failure: TextFailure::Error,
ellipsis: None,
}
}
pub const fn clip() -> Self {
Self {
failure: TextFailure::Clip,
ellipsis: None,
}
}
pub const fn visible() -> Self {
Self {
failure: TextFailure::Visible,
ellipsis: None,
}
}
pub const fn hide() -> Self {
Self {
failure: TextFailure::Hide,
ellipsis: None,
}
}
pub fn ellipsis(value: TextEllipsis, failure: TextFailure) -> Self {
Self {
failure,
ellipsis: Some(value),
}
}
#[must_use]
pub const fn get_failure(&self) -> TextFailure {
self.failure
}
#[must_use]
pub const fn get_ellipsis(&self) -> Option<&TextEllipsis> {
self.ellipsis.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TextVisibility {
pub(crate) hidden: bool,
pub(crate) program: VisibilityProgram,
pub(crate) endpoints: crate::LabelEndpoints,
pub(crate) separation: Finite,
pub(crate) boundary: Option<Finite>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum VisibilityProgram {
All,
Every { stride: usize, offset: usize },
Greedy,
Parity,
}
impl TextVisibility {
pub const fn all() -> Self {
Self {
hidden: false,
program: VisibilityProgram::All,
endpoints: LabelEndpoints::None,
separation: Finite(0),
boundary: None,
}
}
pub const fn hidden() -> Self {
Self {
hidden: true,
..Self::all()
}
}
pub fn every(stride: usize, offset: usize, endpoints: LabelEndpoints) -> Result<Self> {
if stride == 0 || offset >= stride {
return Err(Error::Data(
"text visibility requires positive stride and offset below stride".into(),
));
}
Ok(Self {
program: VisibilityProgram::Every { stride, offset },
endpoints,
..Self::all()
})
}
pub const fn greedy(endpoints: LabelEndpoints) -> Self {
Self {
program: VisibilityProgram::Greedy,
endpoints,
..Self::all()
}
}
pub const fn parity(endpoints: LabelEndpoints) -> Self {
Self {
program: VisibilityProgram::Parity,
endpoints,
..Self::all()
}
}
pub fn separation(mut self, value: f64) -> Result<Self> {
if !matches!(
self.program,
VisibilityProgram::Greedy | VisibilityProgram::Parity
) || value < 0.0
{
return Err(Error::Data(
"text separation requires nonnegative clearance and an overlap reduction program"
.into(),
));
}
self.separation = Finite::new(value)?;
Ok(self)
}
pub fn boundary_tolerance(mut self, value: Option<f64>) -> Result<Self> {
self.boundary = value.map(Finite::new).transpose()?;
Ok(self)
}
#[must_use]
pub const fn is_hidden(&self) -> bool {
self.hidden
}
#[must_use]
pub const fn get_every(&self) -> Option<(usize, usize)> {
match self.program {
VisibilityProgram::Every { stride, offset } => Some((stride, offset)),
VisibilityProgram::All | VisibilityProgram::Greedy | VisibilityProgram::Parity => None,
}
}
#[must_use]
pub const fn is_greedy(&self) -> bool {
matches!(self.program, VisibilityProgram::Greedy)
}
#[must_use]
pub const fn is_parity(&self) -> bool {
matches!(self.program, VisibilityProgram::Parity)
}
#[must_use]
pub const fn get_endpoints(&self) -> LabelEndpoints {
self.endpoints
}
#[must_use]
pub const fn get_separation(&self) -> f64 {
self.separation.get()
}
#[must_use]
pub const fn get_boundary_tolerance(&self) -> Option<f64> {
match self.boundary {
Some(value) => Some(value.get()),
None => None,
}
}
pub(crate) fn reduces(&self) -> bool {
!matches!(self.program, VisibilityProgram::All) || self.boundary.is_some()
}
pub(crate) fn protects(&self, index: usize, eligible: &[usize]) -> bool {
(matches!(self.endpoints, LabelEndpoints::First | LabelEndpoints::Both)
&& eligible.first() == Some(&index))
|| (matches!(self.endpoints, LabelEndpoints::Last | LabelEndpoints::Both)
&& eligible.last() == Some(&index))
}
pub(crate) fn includes(&self, index: usize, eligible: &[usize]) -> bool {
!self.hidden
&& (self.protects(index, eligible)
|| match self.program {
VisibilityProgram::Every { stride, offset } => index % stride == offset,
_ => true,
})
}
}