pub struct WindowTextSystem { /* private fields */ }Expand description
The text layout subsystem for one window, with a cache of shaped lines.
Get it with Window::text_system. It derefs to the shared TextSystem,
so you can call font methods on it directly.
Layout methods such as WindowTextSystem::shape_line and
WindowTextSystem::shape_text cache their results. A layout is reused when
the same text, font size, and shaping settings are requested again. The cache
keeps a layout only while the next frame uses it, so a layout that a frame
doesn’t request is dropped at the end of that frame. Changing only colors or
decorations reuses the glyphs and repaints them.
Implementations§
Source§impl WindowTextSystem
impl WindowTextSystem
Sourcepub fn new(text_system: Arc<TextSystem>) -> Self
pub fn new(text_system: Arc<TextSystem>) -> Self
Creates a window text system with an empty layout cache on top of text_system.
GPUI creates one for each window. You don’t need to call this yourself.
Sourcepub fn shape_line(
&self,
text: SharedString,
font_size: Pixels,
runs: &[TextRun],
) -> ShapedLine
pub fn shape_line( &self, text: SharedString, font_size: Pixels, runs: &[TextRun], ) -> ShapedLine
Shapes one line of text, ready to paint.
Use this for a single line of text that you paint with ShapedLine::paint.
For text that can wrap or has several paragraphs, use Self::shape_text.
runs styles the text, and the run lengths must add up to text.len().
Each run’s TextRun::font selects the face used for its bytes.
The result is cached for the frame, as described on WindowTextSystem.
§Panics
In debug builds, panics if text contains a \n. Release builds don’t
check this, and the result for such text is not meaningful. Use
Self::shape_text for multiple lines.
§Examples
window.update(|_view, window, _cx| {
let run = TextRun {
len: "Hello".len(),
font: font("Lilex"),
color: hsla(0., 0., 0., 1.),
..Default::default()
};
let line = window.text_system().shape_line("Hello".into(), px(16.), &[run]);
assert_eq!(line.len(), 5);
});Sourcepub fn shape_text<S: AsRef<str> + Into<SharedString>>(
&self,
text: S,
font_size: Pixels,
runs: &[TextRun],
wrap_width: Option<Pixels>,
line_clamp: Option<usize>,
) -> Result<ShapedText>
pub fn shape_text<S: AsRef<str> + Into<SharedString>>( &self, text: S, font_size: Pixels, runs: &[TextRun], wrap_width: Option<Pixels>, line_clamp: Option<usize>, ) -> Result<ShapedText>
Shapes text that may span several lines, ready to paint.
This is the function to use for paragraphs. Each \n starts a new line.
When wrap_width is Some, lines wrap at word boundaries to fit within
that many logical pixels. A word wider than that is not split, so it
overflows. None disables soft wrapping, and only \n breaks lines.
With a wrap width, line_clamp = Some(n) limits soft wrapping: once the
budget is exhausted, the rest of each paragraph occupies an unwrapped line.
Hard paragraph breaks are preserved, so the total can exceed n.
Some(0) behaves like Some(1) and disables soft wrapping; it does not hide text.
runs styles the text. Each run gives the number of UTF-8 bytes it covers,
and the runs must cover the whole string and end on character boundaries.
The Result is currently always Ok.
Shaping is cached as described on WindowTextSystem. To lay out text
inside an element, use StyledText instead. See
text for an overview.
§Examples
This example supplies the platform’s real text system. TestApp::new
uses a simplified test shaper, which does not reproduce paragraph layout.
let text_system = gpui_platform::current_platform(true).text_system();
let mut app = TestApp::with_text_system(text_system);
window.update(|_view, window, _cx| {
let text = "first line\nsecond line";
let run = TextRun {
len: text.len(),
font: font(".SystemUIFont"),
color: hsla(0., 0., 0., 1.),
..Default::default()
};
let shaped = window
.text_system()
.shape_text(text, px(16.), &[run.clone()], Some(px(200.)), None)
.unwrap();
assert_eq!(shaped.len(), text.len());
// A clamp cannot remove the hard break between these paragraphs.
let zero = window.text_system()
.shape_text(text, px(16.), &[run.clone()], Some(px(20.)), Some(0))
.unwrap();
let one = window.text_system()
.shape_text(text, px(16.), &[run], Some(px(20.)), Some(1))
.unwrap();
assert_eq!(zero.line_count(), 2);
assert_eq!(one.line_count(), 2);
});Sourcepub fn layout_inline(
&self,
request: InlineLayoutRequest<'_>,
) -> Arc<InlineLayout> ⓘ
pub fn layout_inline( &self, request: InlineLayoutRequest<'_>, ) -> Arc<InlineLayout> ⓘ
Lays out text and atomic boxes together, the way an inline element flows in a paragraph.
Each box is an element that sits between characters, such as an icon or a badge. Text wraps around the boxes. The result is cached like other layouts.
Sourcepub fn layout_line(
&self,
text: &str,
font_size: Pixels,
runs: &[TextRun],
) -> Arc<LineLayout> ⓘ
pub fn layout_line( &self, text: &str, font_size: Pixels, runs: &[TextRun], ) -> Arc<LineLayout> ⓘ
Lays out one line of text and returns the backend’s LineLayout.
Most code should call Self::shape_line instead, which returns a
ShapedLine that you can paint. Use this only when you need the raw
layout. text must not contain newlines, and nothing checks this here.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for WindowTextSystem
impl !RefUnwindSafe for WindowTextSystem
impl !UnwindSafe for WindowTextSystem
impl Send for WindowTextSystem
impl Sync for WindowTextSystem
impl Unpin for WindowTextSystem
impl UnsafeUnpin for WindowTextSystem
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
Source§type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
Source§fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T, C> ComponentsInto<C> for T
impl<T, C> ComponentsInto<C> for T
Source§fn components_into(self) -> C
fn components_into(self) -> C
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more