fyrox_ui/formatted_text/
run.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::core::type_traits::prelude::*;
22use crate::font::FontHeight;
23use std::ops::{Deref, DerefMut};
24
25use super::*;
26
27#[deprecated]
28pub type RunBuilder = Run;
29
30#[derive(Clone, PartialEq, Debug, Default, Reflect)]
31pub struct RunSet(Vec<Run>);
32
33impl From<&[Run]> for RunSet {
34    fn from(value: &[Run]) -> Self {
35        Self(value.to_vec())
36    }
37}
38
39impl From<Vec<Run>> for RunSet {
40    fn from(value: Vec<Run>) -> Self {
41        Self(value)
42    }
43}
44
45impl Visit for RunSet {
46    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
47        self.0.visit(name, visitor)
48    }
49}
50
51impl Deref for RunSet {
52    type Target = Vec<Run>;
53
54    fn deref(&self) -> &Self::Target {
55        &self.0
56    }
57}
58
59impl DerefMut for RunSet {
60    fn deref_mut(&mut self) -> &mut Self::Target {
61        &mut self.0
62    }
63}
64
65impl RunSet {
66    pub fn font_at(&self, index: usize) -> Option<FontResource> {
67        for run in self.0.iter().rev() {
68            if run.range.contains(&(index as u32)) && run.font().is_some() {
69                return Some(run.font().unwrap().clone());
70            }
71        }
72        None
73    }
74    pub fn font_size_at(&self, index: usize) -> Option<f32> {
75        for run in self.0.iter().rev() {
76            if run.range.contains(&(index as u32)) && run.font_size().is_some() {
77                return Some(run.font_size().unwrap());
78            }
79        }
80        None
81    }
82    pub fn brush_at(&self, index: usize) -> Option<Brush> {
83        for run in self.0.iter().rev() {
84            if run.range.contains(&(index as u32)) && run.brush().is_some() {
85                return Some(run.brush().unwrap().clone());
86            }
87        }
88        None
89    }
90    pub fn shadow_at(&self, index: usize) -> Option<bool> {
91        for run in self.0.iter().rev() {
92            if run.range.contains(&(index as u32)) && run.shadow().is_some() {
93                return Some(run.shadow().unwrap());
94            }
95        }
96        None
97    }
98    pub fn shadow_brush_at(&self, index: usize) -> Option<Brush> {
99        for run in self.0.iter().rev() {
100            if run.range.contains(&(index as u32)) && run.shadow_brush().is_some() {
101                return Some(run.shadow_brush().unwrap().clone());
102            }
103        }
104        None
105    }
106    pub fn shadow_dilation_at(&self, index: usize) -> Option<f32> {
107        for run in self.0.iter().rev() {
108            if run.range.contains(&(index as u32)) && run.shadow_dilation().is_some() {
109                return Some(run.shadow_dilation().unwrap());
110            }
111        }
112        None
113    }
114    pub fn shadow_offset_at(&self, index: usize) -> Option<Vector2<f32>> {
115        for run in self.0.iter().rev() {
116            if run.range.contains(&(index as u32)) && run.shadow_offset().is_some() {
117                return Some(run.shadow_offset().unwrap());
118            }
119        }
120        None
121    }
122}
123
124/// The style of a partion of text within a range.
125#[derive(Clone, PartialEq, Debug, Default, Visit, Reflect, TypeUuidProvider)]
126#[type_uuid(id = "f0e5cc5d-0b82-4d6f-a505-12f890ffe7ea")]
127pub struct Run {
128    /// The range of characters that this run applies to within the text.
129    pub range: Range<u32>,
130    font: Option<FontResource>,
131    brush: Option<Brush>,
132    font_size: Option<f32>,
133    shadow: Option<bool>,
134    shadow_brush: Option<Brush>,
135    shadow_dilation: Option<f32>,
136    shadow_offset: Option<Vector2<f32>>,
137}
138
139impl Run {
140    pub fn new(range: Range<u32>) -> Self {
141        Self {
142            range,
143            font: None,
144            brush: None,
145            font_size: None,
146            shadow: None,
147            shadow_brush: None,
148            shadow_dilation: None,
149            shadow_offset: None,
150        }
151    }
152    /// The font of the characters in this run, or None if the font is unmodified.
153    pub fn font(&self) -> Option<&FontResource> {
154        self.font.as_ref()
155    }
156    /// The brush of the characters in this run, or None if the brush is unmodified.
157    pub fn brush(&self) -> Option<&Brush> {
158        self.brush.as_ref()
159    }
160    /// The size of the characters in this run, or None if the size is unmodified.
161    pub fn font_size(&self) -> Option<f32> {
162        self.font_size
163    }
164    /// True if the characters in this run should have a shadow. None if this run does not change
165    /// whether the characters are shadowed.
166    pub fn shadow(&self) -> Option<bool> {
167        self.shadow
168    }
169    /// The brush for the shadow of the characters in this run, or None if the brush is unmodified.
170    pub fn shadow_brush(&self) -> Option<&Brush> {
171        self.shadow_brush.as_ref()
172    }
173    /// The dilation for the shadow in this run, or None if the dilation is unmodified.
174    pub fn shadow_dilation(&self) -> Option<f32> {
175        self.shadow_dilation
176    }
177    /// The offset for the shadow in this run, or None if the offset is unmodified.
178    pub fn shadow_offset(&self) -> Option<Vector2<f32>> {
179        self.shadow_offset
180    }
181    #[deprecated]
182    pub fn build(self) -> Self {
183        self
184    }
185    /// Set this run to modify the font of the text within the range.
186    pub fn with_font(mut self, font: FontResource) -> Self {
187        self.font = Some(font);
188        self
189    }
190    /// Set this run to modify the brush of the text within the range.
191    pub fn with_brush(mut self, brush: Brush) -> Self {
192        self.brush = Some(brush);
193        self
194    }
195    /// Set this run to modify the size of the text within the range.
196    pub fn with_size(mut self, size: f32) -> Self {
197        self.font_size = Some(size);
198        self
199    }
200    /// Set this run to enable or disable the shadow of the text within the range.
201    pub fn with_shadow(mut self, shadow: bool) -> Self {
202        self.shadow = Some(shadow);
203        self
204    }
205    /// Set this run to modify the brush of the shadow within the range.
206    pub fn with_shadow_brush(mut self, brush: Brush) -> Self {
207        self.shadow_brush = Some(brush);
208        self
209    }
210    /// Set this run to modify the dilation of the shadow within the range.
211    pub fn with_shadow_dilation(mut self, size: f32) -> Self {
212        self.shadow_dilation = Some(size);
213        self
214    }
215    /// Set this run to modify the offset of the shadow within the range.
216    pub fn with_shadow_offset(mut self, offset: Vector2<f32>) -> Self {
217        self.shadow_offset = Some(offset);
218        self
219    }
220}
221
222#[derive(Clone, Copy, Eq, PartialEq)]
223pub enum DrawValueLayer {
224    Main,
225    Shadow,
226}
227
228#[derive(Clone, PartialEq, Debug)]
229pub struct GlyphDrawValues {
230    pub atlas_page_index: usize,
231    pub font: FontResource,
232    pub brush: Brush,
233    /// Font size scaled by super sampling scaling to pick correct atlas page.
234    pub height: FontHeight,
235}