Skip to main content

WindowTextSystem

Struct WindowTextSystem 

Source
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

Source

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.

Source

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);
});
Source

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);
});
Source

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.

Source

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§

Source§

impl Deref for WindowTextSystem

Source§

type Target = Arc<TextSystem>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where 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) -> D
where M: TransformMatrix<T>,

👎Deprecated since 0.7.7:

replaced by palette::chromatic_adaptation::AdaptIntoUnclamped

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

👎Deprecated since 0.7.7:

replaced by palette::chromatic_adaptation::AdaptIntoUnclamped

Convert the source color to the destination color using the bradford method by default.
Source§

impl<T, C> AdaptIntoUnclamped<T> for C
where T: AdaptFromUnclamped<C>,

Source§

type Scalar = <T as AdaptFromUnclamped<C>>::Scalar

The number type that’s used as the color’s components.
Source§

fn adapt_into_unclamped_with<M>(self) -> T

Adapt a color of type Self into a color of type T, using the custom matrix M. Read more
Source§

fn adapt_into_unclamped(self) -> T
where Bradford: LmsToXyz<Self::Scalar> + XyzToLms<Self::Scalar>,

Adapt a color of type Self into a color of type T, using the Bradford matrix. Read more
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
Source§

impl<T, C> ComponentsInto<C> for T

Source§

fn components_into(self) -> C

Cast this collection of color components into a collection of colors. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more