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: usizeImplementations§
source§impl<'a> Attrs<'a>
impl<'a> Attrs<'a>
sourcepub fn new() -> Self
pub fn new() -> Self
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
}sourcepub fn monospaced(self, monospaced: bool) -> Self
pub fn monospaced(self, monospaced: bool) -> Self
Set monospaced
sourcepub fn matches(&self, face: &FaceInfo) -> bool
pub fn matches(&self, face: &FaceInfo) -> bool
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()
})
}sourcepub fn compatible(&self, other: &Self) -> bool
pub fn compatible(&self, other: &Self) -> bool
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}
}