Struct cosmic_text::Attrs

source ·
pub struct Attrs<'a> {
    pub color_opt: Option<Color>,
    pub family: Family<'a>,
    pub monospaced: bool,
    pub stretch: Stretch,
    pub style: Style,
    pub weight: Weight,
    pub metadata: usize,
}
Expand description

Text attributes

Fields§

§color_opt: Option<Color>§family: Family<'a>§monospaced: bool§stretch: Stretch§style: Style§weight: Weight§metadata: usize

Implementations§

Create a new set of attributes with sane defaults

This defaults to a regular Sans-Serif font.

Examples found in repository?
src/buffer.rs (line 177)
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    pub fn new(
        font_system: &'a FontSystem,
        metrics: Metrics,
    ) -> Self {
        let mut buffer = Self {
            font_system,
            lines: Vec::new(),
            metrics,
            width: 0,
            height: 0,
            scroll: 0,
            redraw: false,
        };
        buffer.set_text("", Attrs::new());
        buffer
    }

Set Color

Set Family

Set monospaced

Set Stretch

Set Style

Set Weight

Set metadata

Check if font matches

Examples found in repository?
src/font/system/std.rs (line 100)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    pub fn get_font_matches<'a>(&'a self, attrs: Attrs) -> Arc<FontMatches<'a>> {
        self.0.with(|fields| {
            let mut font_matches_cache = fields.font_matches_cache.lock().expect("failed to lock font matches cache");
            //TODO: do not create AttrsOwned unless entry does not already exist
            font_matches_cache.entry(AttrsOwned::new(attrs)).or_insert_with(|| {
                let now = std::time::Instant::now();

                let mut fonts = Vec::new();
                for face in fields.db.faces() {
                    if !attrs.matches(face) {
                        continue;
                    }

                    if let Some(font) = get_font(&fields, face.id) {
                        fonts.push(font);
                    }
                }

                let font_matches = Arc::new(FontMatches {
                    locale: fields.locale,
                    default_family: fields.db.family_name(&attrs.family).to_string(),
                    fonts
                });

                let elapsed = now.elapsed();
                log::debug!("font matches for {:?} in {:?}", attrs, elapsed);

                font_matches
            }).clone()
        })
    }

Check if this set of attributes can be shaped with another

Examples found in repository?
src/shape.rs (line 310)
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    pub fn new<'a>(
        font_system: &'a FontSystem,
        line: &str,
        attrs_list: &AttrsList,
        word_range: Range<usize>,
        level: unicode_bidi::Level,
        blank: bool,
    ) -> Self {
        let word = &line[word_range.clone()];

        log::trace!(
            "      Word{}: '{}'",
            if blank { " BLANK" } else { "" },
            word
        );

        let mut glyphs = Vec::new();
        let span_rtl = level.is_rtl();

        let mut start_run = word_range.start;
        let mut attrs = attrs_list.defaults();
        for (egc_i, _egc) in word.grapheme_indices(true) {
            let start_egc = word_range.start + egc_i;
            let attrs_egc = attrs_list.get_span(start_egc);
            if ! attrs.compatible(&attrs_egc) {
                //TODO: more efficient
                glyphs.append(&mut shape_run(
                    font_system,
                    line,
                    attrs_list,
                    start_run,
                    start_egc,
                    span_rtl
                ));

                start_run = start_egc;
                attrs = attrs_egc;
            }
        }
        if start_run < word_range.end {
            //TODO: more efficient
            glyphs.append(&mut shape_run(
                font_system,
                line,
                attrs_list,
                start_run,
                word_range.end,
                span_rtl
            ));
        }

        let mut x_advance = 0.0;
        let mut y_advance = 0.0;
        for glyph in &glyphs {
            x_advance += glyph.x_advance;
            y_advance += glyph.y_advance;
        }

        Self { blank, glyphs, x_advance, y_advance}
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.