use crate::core::type_traits::prelude::*;
use crate::font::FontHeight;
use std::ops::{Deref, DerefMut};
use super::*;
#[deprecated]
pub type RunBuilder = Run;
#[derive(Clone, PartialEq, Debug, Default, Reflect)]
pub struct RunSet(Vec<Run>);
impl IntoIterator for RunSet {
type Item = Run;
type IntoIter = std::vec::IntoIter<Run>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<&[Run]> for RunSet {
fn from(value: &[Run]) -> Self {
Self(value.to_vec())
}
}
impl From<Vec<Run>> for RunSet {
fn from(value: Vec<Run>) -> Self {
Self(value)
}
}
impl Visit for RunSet {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
self.0.visit(name, visitor)
}
}
impl Deref for RunSet {
type Target = Vec<Run>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for RunSet {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl RunSet {
pub fn push(&mut self, run: Run) {
if let Some(last) = self.0.last_mut() {
if last.range == run.range {
*last = last.clone().with_values_from(run);
} else {
self.0.push(run);
}
} else {
self.0.push(run);
}
}
pub fn font_at(&self, index: usize) -> Option<FontResource> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.font().is_some() {
return Some(run.font().unwrap().clone());
}
}
None
}
pub fn font_size_at(&self, index: usize) -> Option<f32> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.font_size().is_some() {
return Some(run.font_size().unwrap());
}
}
None
}
pub fn brush_at(&self, index: usize) -> Option<Brush> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.brush().is_some() {
return Some(run.brush().unwrap().clone());
}
}
None
}
pub fn shadow_at(&self, index: usize) -> Option<bool> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.shadow().is_some() {
return Some(run.shadow().unwrap());
}
}
None
}
pub fn shadow_brush_at(&self, index: usize) -> Option<Brush> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.shadow_brush().is_some() {
return Some(run.shadow_brush().unwrap().clone());
}
}
None
}
pub fn shadow_dilation_at(&self, index: usize) -> Option<f32> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.shadow_dilation().is_some() {
return Some(run.shadow_dilation().unwrap());
}
}
None
}
pub fn shadow_offset_at(&self, index: usize) -> Option<Vector2<f32>> {
for run in self.0.iter().rev() {
if run.range.contains(&(index as u32)) && run.shadow_offset().is_some() {
return Some(run.shadow_offset().unwrap());
}
}
None
}
}
#[derive(Clone, PartialEq, Debug, Default, Visit, Reflect, TypeUuidProvider)]
#[type_uuid(id = "f0e5cc5d-0b82-4d6f-a505-12f890ffe7ea")]
pub struct Run {
pub range: Range<u32>,
font: Option<FontResource>,
brush: Option<Brush>,
font_size: Option<f32>,
shadow: Option<bool>,
shadow_brush: Option<Brush>,
shadow_dilation: Option<f32>,
shadow_offset: Option<Vector2<f32>>,
}
impl Run {
pub fn new(range: Range<u32>) -> Self {
Self {
range,
font: None,
brush: None,
font_size: None,
shadow: None,
shadow_brush: None,
shadow_dilation: None,
shadow_offset: None,
}
}
pub fn font(&self) -> Option<&FontResource> {
self.font.as_ref()
}
pub fn brush(&self) -> Option<&Brush> {
self.brush.as_ref()
}
pub fn font_size(&self) -> Option<f32> {
self.font_size
}
pub fn shadow(&self) -> Option<bool> {
self.shadow
}
pub fn shadow_brush(&self) -> Option<&Brush> {
self.shadow_brush.as_ref()
}
pub fn shadow_dilation(&self) -> Option<f32> {
self.shadow_dilation
}
pub fn shadow_offset(&self) -> Option<Vector2<f32>> {
self.shadow_offset
}
#[deprecated]
pub fn build(self) -> Self {
self
}
pub fn with_values_from(self, run: Run) -> Self {
Self {
range: self.range,
font: run.font.or(self.font),
brush: run.brush.or(self.brush),
font_size: run.font_size.or(self.font_size),
shadow: run.shadow.or(self.shadow),
shadow_brush: run.shadow_brush.or(self.shadow_brush),
shadow_dilation: run.shadow_dilation.or(self.shadow_dilation),
shadow_offset: run.shadow_offset.or(self.shadow_offset),
}
}
pub fn with_font(mut self, font: FontResource) -> Self {
self.font = Some(font);
self
}
pub fn with_brush(mut self, brush: Brush) -> Self {
self.brush = Some(brush);
self
}
pub fn with_size(mut self, size: f32) -> Self {
self.font_size = Some(size);
self
}
pub fn with_shadow(mut self, shadow: bool) -> Self {
self.shadow = Some(shadow);
self
}
pub fn with_shadow_brush(mut self, brush: Brush) -> Self {
self.shadow_brush = Some(brush);
self
}
pub fn with_shadow_dilation(mut self, size: f32) -> Self {
self.shadow_dilation = Some(size);
self
}
pub fn with_shadow_offset(mut self, offset: Vector2<f32>) -> Self {
self.shadow_offset = Some(offset);
self
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum DrawValueLayer {
Main,
Shadow,
}
#[derive(Clone, PartialEq, Debug)]
pub struct GlyphDrawValues {
pub atlas_page_index: usize,
pub font: FontResource,
pub brush: Brush,
pub height: FontHeight,
}