pub struct TextSystem { /* private fields */ }Expand description
The platform-independent font database and metrics cache, shared by all windows.
Get it from App::text_system and use it to register fonts with
TextSystem::add_fonts, to measure glyphs and to resolve Fonts to
FontIds. To shape text for painting, use the WindowTextSystem of the
window, which also caches the layouts.
Implementations§
Source§impl TextSystem
impl TextSystem
Sourcepub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self
pub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self
Creates a text system that delegates font work to platform_text_system.
GPUI creates the text system for each application. You don’t need to call this yourself, except in tests that supply their own platform text system.
Sourcepub fn all_font_names(&self) -> Vec<String>
pub fn all_font_names(&self) -> Vec<String>
Returns the font family names that this text system can resolve.
Use these names as Font::family values. On the web, the list contains
only the families registered with TextSystem::add_fonts and the
.SystemUIFont alias, because the web has no system fonts.
Sourcepub fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()>
pub fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()>
Registers font files so that Fonts can resolve to them.
Each entry is the bytes of one font file, such as a .ttf or .otf.
Use Cow::Borrowed for bytes compiled into the binary with
include_bytes!, and Cow::Owned for bytes loaded at runtime. The
family names come from each font’s name table, so pass those names to
font or set Font::family to use them.
The web starts with an empty font database, so add the fonts your application uses before you open a window.
§Errors
Returns an error if an entry contains no font face that the platform supports. The parley backend, used on macOS, Windows, Linux, and the web, checks every entry before it registers any, so a failed call leaves the set of fonts unchanged.
§Examples
let bytes = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../assets/fonts/lilex/Lilex-Regular.ttf"
));
app.text_system()
.add_fonts(vec![Cow::Borrowed(bytes.as_slice())])
.expect("Lilex is a valid font");See text for how fonts are chosen.
Sourcepub fn resolve_font(&self, font: &Font) -> FontId
pub fn resolve_font(&self, font: &Font) -> FontId
Resolves a Font to the FontId of the face that will be used to draw it.
Resolution applies the family, weight, style, width, and fallbacks, and
the result is cached. Use the returned id with the metric methods on this
type, such as TextSystem::ascent.
§Panics
Panics if the platform cannot resolve the font. Pass the family name of
a font from TextSystem::all_font_names or one added with
TextSystem::add_fonts to avoid this.
Sourcepub fn prewarm_fonts(&self, fonts: &[Font])
pub fn prewarm_fonts(&self, fonts: &[Font])
Resolves each font and loads its platform caches ahead of time.
Loading system fonts can be slow, so call this from a background executor before the first frame that uses the fonts. Fonts that are not prewarmed are loaded when they are first used.
§Panics
Panics if any font cannot be resolved, as TextSystem::resolve_font does.
Sourcepub fn bounding_box(&self, font_id: FontId, font_size: Pixels) -> Bounds<Pixels>
pub fn bounding_box(&self, font_id: FontId, font_size: Pixels) -> Bounds<Pixels>
Returns the font’s bounding box at font_size in logical pixels.
The bounding box is the smallest rectangle that encloses every glyph in the font when they are all drawn at the same origin.
Sourcepub fn typographic_bounds(
&self,
font_id: FontId,
font_size: Pixels,
character: char,
) -> Result<Bounds<Pixels>>
pub fn typographic_bounds( &self, font_id: FontId, font_size: Pixels, character: char, ) -> Result<Bounds<Pixels>>
Returns the bounding rectangle of the glyph for character at font_size in logical pixels.
§Errors
Returns an error if the font has no glyph for character or the platform
cannot read its bounds.
Sourcepub fn advance(
&self,
font_id: FontId,
font_size: Pixels,
ch: char,
) -> Result<Size<Pixels>>
pub fn advance( &self, font_id: FontId, font_size: Pixels, ch: char, ) -> Result<Size<Pixels>>
Returns the advance of ch at font_size: how far the pen moves after drawing it.
The width of the result is the advance, which is the glyph’s horizontal
width in the font, not its ink width. Layout uses it to place the next character.
§Errors
Returns an error if the font has no glyph for ch.
Sourcepub fn units_per_em(&self, font_id: FontId) -> u32
pub fn units_per_em(&self, font_id: FontId) -> u32
Returns the number of font units in the font’s em square.
The em square is the grid that font sizes are measured on. Divide a font-unit measurement by this value to get a fraction of the font size.
Sourcepub fn ascent(&self, font_id: FontId, font_size: Pixels) -> Pixels
pub fn ascent(&self, font_id: FontId, font_size: Pixels) -> Pixels
Returns the distance from the baseline up to the top of the font’s tallest glyphs, at font_size.
Sourcepub fn descent(&self, font_id: FontId, font_size: Pixels) -> Pixels
pub fn descent(&self, font_id: FontId, font_size: Pixels) -> Pixels
Returns the distance from the baseline down to the bottom of the font’s lowest glyphs, at font_size.
The value is positive. It is the distance below the baseline, not a signed coordinate.
Sourcepub fn x_height(&self, font_id: FontId, font_size: Pixels) -> Pixels
pub fn x_height(&self, font_id: FontId, font_size: Pixels) -> Pixels
Returns the height of lowercase x at font_size.
Sourcepub fn baseline_offset(
&self,
font_id: FontId,
font_size: Pixels,
line_height: Pixels,
) -> Pixels
pub fn baseline_offset( &self, font_id: FontId, font_size: Pixels, line_height: Pixels, ) -> Pixels
Returns the distance from the top of a line box of line_height to the baseline.
The ascent and descent are centered in the line box, so extra
line_height is split evenly above and below the glyphs. Use this to
place text inside a box with a fixed height.
Sourcepub fn rasterize_glyph(
&self,
params: &RenderGlyphParams,
) -> Result<ValidatedRasterizedGlyph>
pub fn rasterize_glyph( &self, params: &RenderGlyphParams, ) -> Result<ValidatedRasterizedGlyph>
Rasterizes one glyph to pixels, and checks that the result is well formed.
GPUI calls this when it draws a glyph that is not in the glyph atlas yet. Application code rarely needs it.
§Errors
Returns an error if the platform cannot rasterize the glyph, or if the
pixel buffer does not match the size and format it reports. See
RasterizedGlyph::validate.
Auto Trait Implementations§
impl !Freeze for TextSystem
impl !RefUnwindSafe for TextSystem
impl !UnwindSafe for TextSystem
impl Send for TextSystem
impl Sync for TextSystem
impl Unpin for TextSystem
impl UnsafeUnpin for TextSystem
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