fix for test.sh

This commit is contained in:
Stewart Connor 2025-03-31 17:03:51 +11:00
parent 53763c157b
commit e828131c92
15 changed files with 136 additions and 109 deletions

View file

@ -31,7 +31,7 @@ fn main() {
let attrs = Attrs::new().family(Family::Monospace); let attrs = Attrs::new().family(Family::Monospace);
match fs::read_to_string(&path) { match fs::read_to_string(&path) {
Ok(text) => buffer.set_text(&text, attrs, Shaping::Advanced), Ok(text) => buffer.set_text(&text, &attrs, Shaping::Advanced),
Err(err) => { Err(err) => {
log::error!("failed to load {:?}: {}", path, err); log::error!("failed to load {:?}: {}", path, err);
} }

View file

@ -21,89 +21,112 @@ use winit::{
fn set_buffer_text(buffer: &mut BorrowedWithFontSystem<'_, Buffer>) { fn set_buffer_text(buffer: &mut BorrowedWithFontSystem<'_, Buffer>) {
let attrs = Attrs::new(); let attrs = Attrs::new();
let serif_attrs = attrs.family(Family::Serif); let serif_attrs = attrs.clone().family(Family::Serif);
let mono_attrs = attrs.family(Family::Monospace); let mono_attrs = attrs.clone().family(Family::Monospace);
let comic_attrs = attrs.family(Family::Name("Comic Neue")); let comic_attrs = attrs.clone().family(Family::Name("Comic Neue"));
let spans: &[(&str, Attrs)] = &[ let spans: &[(&str, Attrs)] = &[
("Font size 64 ", attrs.metrics(Metrics::relative(64.0, 1.2))), (
("Font size 8 ", attrs.metrics(Metrics::relative(8.0, 1.2))), "Font size 64 ",
("Font size 20 ", attrs.metrics(Metrics::relative(20.0, 1.2))), attrs.clone().metrics(Metrics::relative(64.0, 1.2)),
("Font size 14 ", attrs.metrics(Metrics::relative(14.0, 1.2))), ),
(
"Font size 8 ",
attrs.clone().metrics(Metrics::relative(8.0, 1.2)),
),
(
"Font size 20 ",
attrs.clone().metrics(Metrics::relative(20.0, 1.2)),
),
(
"Font size 14 ",
attrs.clone().metrics(Metrics::relative(14.0, 1.2)),
),
( (
"Font size 48\n", "Font size 48\n",
attrs.metrics(Metrics::relative(48.0, 1.2)), attrs.clone().metrics(Metrics::relative(48.0, 1.2)),
), ),
("B", attrs.weight(Weight::BOLD)), ("B", attrs.clone().weight(Weight::BOLD)),
("old ", attrs), ("old ", attrs.clone()),
("I", attrs.style(Style::Italic)), ("I", attrs.clone().style(Style::Italic)),
("talic ", attrs), ("talic ", attrs.clone()),
("f", attrs), ("f", attrs.clone()),
("i ", attrs), ("i ", attrs.clone()),
("f", attrs.weight(Weight::BOLD)), ("f", attrs.clone().weight(Weight::BOLD)),
("i ", attrs), ("i ", attrs.clone()),
("f", attrs.style(Style::Italic)), ("f", attrs.clone().style(Style::Italic)),
("i \n", attrs), ("i \n", attrs.clone()),
("Sans-Serif Normal ", attrs), ("Sans-Serif Normal ", attrs.clone()),
("Sans-Serif Bold ", attrs.weight(Weight::BOLD)), ("Sans-Serif Bold ", attrs.clone().weight(Weight::BOLD)),
("Sans-Serif Italic ", attrs.style(Style::Italic)), ("Sans-Serif Italic ", attrs.clone().style(Style::Italic)),
( (
"Sans-Serif Fake Italic ", "Sans-Serif Fake Italic ",
attrs.cache_key_flags(CacheKeyFlags::FAKE_ITALIC), attrs.clone().cache_key_flags(CacheKeyFlags::FAKE_ITALIC),
), ),
( (
"Sans-Serif Bold Italic\n", "Sans-Serif Bold Italic\n",
attrs.weight(Weight::BOLD).style(Style::Italic), attrs.clone().weight(Weight::BOLD).style(Style::Italic),
), ),
("Serif Normal ", serif_attrs), ("Serif Normal ", serif_attrs.clone()),
("Serif Bold ", serif_attrs.weight(Weight::BOLD)), ("Serif Bold ", serif_attrs.clone().weight(Weight::BOLD)),
("Serif Italic ", serif_attrs.style(Style::Italic)), ("Serif Italic ", serif_attrs.clone().style(Style::Italic)),
( (
"Serif Bold Italic\n", "Serif Bold Italic\n",
serif_attrs.weight(Weight::BOLD).style(Style::Italic), serif_attrs
.clone()
.weight(Weight::BOLD)
.style(Style::Italic),
), ),
("Mono Normal ", mono_attrs), ("Mono Normal ", mono_attrs.clone()),
("Mono Bold ", mono_attrs.weight(Weight::BOLD)), ("Mono Bold ", mono_attrs.clone().weight(Weight::BOLD)),
("Mono Italic ", mono_attrs.style(Style::Italic)), ("Mono Italic ", mono_attrs.clone().style(Style::Italic)),
( (
"Mono Bold Italic\n", "Mono Bold Italic\n",
mono_attrs.weight(Weight::BOLD).style(Style::Italic), mono_attrs.clone().weight(Weight::BOLD).style(Style::Italic),
), ),
("Comic Normal ", comic_attrs), ("Comic Normal ", comic_attrs.clone()),
("Comic Bold ", comic_attrs.weight(Weight::BOLD)), ("Comic Bold ", comic_attrs.clone().weight(Weight::BOLD)),
("Comic Italic ", comic_attrs.style(Style::Italic)), ("Comic Italic ", comic_attrs.clone().style(Style::Italic)),
( (
"Comic Bold Italic\n", "Comic Bold Italic\n",
comic_attrs.weight(Weight::BOLD).style(Style::Italic), comic_attrs
.clone()
.weight(Weight::BOLD)
.style(Style::Italic),
), ),
("R", attrs.color(Color::rgb(0xFF, 0x00, 0x00))), ("R", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
("A", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))), ("A", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
("I", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))), ("I", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
("N", attrs.color(Color::rgb(0x00, 0xFF, 0x00))), ("N", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
("B", attrs.color(Color::rgb(0x00, 0x00, 0xFF))), ("B", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
("O", attrs.color(Color::rgb(0x4B, 0x00, 0x82))), ("O", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
("W ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))), ("W ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
("Red ", attrs.color(Color::rgb(0xFF, 0x00, 0x00))), ("Red ", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
("Orange ", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))), ("Orange ", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
("Yellow ", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))), ("Yellow ", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
("Green ", attrs.color(Color::rgb(0x00, 0xFF, 0x00))), ("Green ", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
("Blue ", attrs.color(Color::rgb(0x00, 0x00, 0xFF))), ("Blue ", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
("Indigo ", attrs.color(Color::rgb(0x4B, 0x00, 0x82))), ("Indigo ", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
("Violet ", attrs.color(Color::rgb(0x94, 0x00, 0xD3))), ("Violet ", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
("U", attrs.color(Color::rgb(0x94, 0x00, 0xD3))), ("U", attrs.clone().color(Color::rgb(0x94, 0x00, 0xD3))),
("N", attrs.color(Color::rgb(0x4B, 0x00, 0x82))), ("N", attrs.clone().color(Color::rgb(0x4B, 0x00, 0x82))),
("I", attrs.color(Color::rgb(0x00, 0x00, 0xFF))), ("I", attrs.clone().color(Color::rgb(0x00, 0x00, 0xFF))),
("C", attrs.color(Color::rgb(0x00, 0xFF, 0x00))), ("C", attrs.clone().color(Color::rgb(0x00, 0xFF, 0x00))),
("O", attrs.color(Color::rgb(0xFF, 0xFF, 0x00))), ("O", attrs.clone().color(Color::rgb(0xFF, 0xFF, 0x00))),
("R", attrs.color(Color::rgb(0xFF, 0x7F, 0x00))), ("R", attrs.clone().color(Color::rgb(0xFF, 0x7F, 0x00))),
("N\n", attrs.color(Color::rgb(0xFF, 0x00, 0x00))), ("N\n", attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00))),
( (
"生活,삶,जिंदगी 😀 FPS\n", "生活,삶,जिंदगी 😀 FPS\n",
attrs.color(Color::rgb(0xFF, 0x00, 0x00)), attrs.clone().color(Color::rgb(0xFF, 0x00, 0x00)),
), ),
]; ];
buffer.set_rich_text(spans.iter().copied(), attrs, Shaping::Advanced, None); buffer.set_rich_text(
spans.iter().map(|(text, attrs)| (*text, attrs.clone())),
&attrs,
Shaping::Advanced,
None,
);
} }
fn main() { fn main() {

View file

@ -36,7 +36,7 @@ fn main() {
let text = std::env::args() let text = std::env::args()
.nth(1) .nth(1)
.unwrap_or(" Hi, Rust! 🦀 ".to_string()); .unwrap_or(" Hi, Rust! 🦀 ".to_string());
buffer.set_text(&text, attrs, Shaping::Advanced); buffer.set_text(&text, &attrs, Shaping::Advanced);
// Perform shaping as desired // Perform shaping as desired
buffer.shape_until_scroll(true); buffer.shape_until_scroll(true);

View file

@ -125,7 +125,7 @@ impl From<CacheMetrics> for Metrics {
} }
} }
} }
/// A 4-byte OpenType feature tag identifier /// A 4-byte `OpenType` feature tag identifier
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct FeatureTag([u8; 4]); pub struct FeatureTag([u8; 4]);
@ -171,7 +171,9 @@ pub struct FontFeatures {
impl FontFeatures { impl FontFeatures {
pub fn new() -> Self { pub fn new() -> Self {
Self { features: Vec::new() } Self {
features: Vec::new(),
}
} }
pub fn set(&mut self, tag: FeatureTag, value: u32) -> &mut Self { pub fn set(&mut self, tag: FeatureTag, value: u32) -> &mut Self {
@ -311,7 +313,7 @@ impl<'a> Attrs<'a> {
self self
} }
/// Set [FontFeatures] /// Set [`FontFeatures`]
pub fn font_features(mut self, font_features: FontFeatures) -> Self { pub fn font_features(mut self, font_features: FontFeatures) -> Self {
self.font_features = font_features; self.font_features = font_features;
self self
@ -342,8 +344,8 @@ pub struct FontMatchAttrs {
weight: Weight, weight: Weight,
} }
impl<'a> From<Attrs<'a>> for FontMatchAttrs { impl<'a> From<&Attrs<'a>> for FontMatchAttrs {
fn from(attrs: Attrs<'a>) -> Self { fn from(attrs: &Attrs<'a>) -> Self {
Self { Self {
family: FamilyOwned::new(attrs.family), family: FamilyOwned::new(attrs.family),
stretch: attrs.stretch, stretch: attrs.stretch,
@ -371,7 +373,7 @@ pub struct AttrsOwned {
} }
impl AttrsOwned { impl AttrsOwned {
pub fn new(attrs: Attrs) -> Self { pub fn new(attrs: &Attrs) -> Self {
Self { Self {
color_opt: attrs.color_opt, color_opt: attrs.color_opt,
family_owned: FamilyOwned::new(attrs.family), family_owned: FamilyOwned::new(attrs.family),
@ -412,7 +414,7 @@ pub struct AttrsList {
impl AttrsList { impl AttrsList {
/// Create a new attributes list with a set of default [Attrs] /// Create a new attributes list with a set of default [Attrs]
pub fn new(defaults: Attrs) -> Self { pub fn new(defaults: &Attrs) -> Self {
Self { Self {
defaults: AttrsOwned::new(defaults), defaults: AttrsOwned::new(defaults),
spans: RangeMap::new(), spans: RangeMap::new(),
@ -440,7 +442,7 @@ impl AttrsList {
} }
/// Add an attribute span, removes any previous matching parts of spans /// Add an attribute span, removes any previous matching parts of spans
pub fn add_span(&mut self, range: Range<usize>, attrs: Attrs) { pub fn add_span(&mut self, range: Range<usize>, attrs: &Attrs) {
//do not support 1..1 or 2..1 even if by accident. //do not support 1..1 or 2..1 even if by accident.
if range.is_empty() { if range.is_empty() {
return; return;
@ -462,7 +464,7 @@ impl AttrsList {
/// Split attributes list at an offset /// Split attributes list at an offset
#[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_panics_doc)]
pub fn split_off(&mut self, index: usize) -> Self { pub fn split_off(&mut self, index: usize) -> Self {
let mut new = Self::new(self.defaults.as_attrs()); let mut new = Self::new(&self.defaults.as_attrs());
let mut removes = Vec::new(); let mut removes = Vec::new();
//get the keys we need to remove or fix. //get the keys we need to remove or fix.
@ -496,7 +498,7 @@ impl AttrsList {
} }
/// Resets the attributes with new defaults. /// Resets the attributes with new defaults.
pub(crate) fn reset(mut self, default: Attrs) -> Self { pub(crate) fn reset(mut self, default: &Attrs) -> Self {
self.defaults = AttrsOwned::new(default); self.defaults = AttrsOwned::new(default);
self.spans.clear(); self.spans.clear();
self self

View file

@ -266,7 +266,7 @@ impl Buffer {
/// Will panic if `metrics.line_height` is zero. /// Will panic if `metrics.line_height` is zero.
pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self { pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self {
let mut buffer = Self::new_empty(metrics); let mut buffer = Self::new_empty(metrics);
buffer.set_text(font_system, "", Attrs::new(), Shaping::Advanced); buffer.set_text(font_system, "", &Attrs::new(), Shaping::Advanced);
buffer buffer
} }
@ -676,7 +676,7 @@ impl Buffer {
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
text: &str, text: &str,
attrs: Attrs, attrs: &Attrs,
shaping: Shaping, shaping: Shaping,
) { ) {
self.lines.clear(); self.lines.clear();
@ -710,10 +710,10 @@ impl Buffer {
/// buffer.set_rich_text( /// buffer.set_rich_text(
/// &mut font_system, /// &mut font_system,
/// [ /// [
/// ("hello, ", attrs), /// ("hello, ", attrs.clone()),
/// ("cosmic\ntext", attrs.family(Family::Monospace)), /// ("cosmic\ntext", attrs.clone().family(Family::Monospace)),
/// ], /// ],
/// attrs, /// &attrs,
/// Shaping::Advanced, /// Shaping::Advanced,
/// None, /// None,
/// ); /// );
@ -722,7 +722,7 @@ impl Buffer {
&mut self, &mut self,
font_system: &mut FontSystem, font_system: &mut FontSystem,
spans: I, spans: I,
default_attrs: Attrs, default_attrs: &Attrs,
shaping: Shaping, shaping: Shaping,
alignment: Option<Align>, alignment: Option<Align>,
) where ) where
@ -758,7 +758,7 @@ impl Buffer {
.lines .lines
.get_mut(line_count) .get_mut(line_count)
.map(BufferLine::reclaim_attrs) .map(BufferLine::reclaim_attrs)
.unwrap_or_else(|| AttrsList::new(Attrs::new())) .unwrap_or_else(|| AttrsList::new(&Attrs::new()))
.reset(default_attrs); .reset(default_attrs);
let mut line_string = self let mut line_string = self
.lines .lines
@ -792,7 +792,7 @@ impl Buffer {
let text_end = line_string.len(); let text_end = line_string.len();
// Only add attrs if they don't match the defaults // Only add attrs if they don't match the defaults
if *attrs != attrs_list.defaults() { if *attrs != attrs_list.defaults() {
attrs_list.add_span(text_start..text_end, *attrs); attrs_list.add_span(text_start..text_end, attrs);
} }
} }
@ -811,7 +811,7 @@ impl Buffer {
.lines .lines
.get_mut(line_count + 1) .get_mut(line_count + 1)
.map(BufferLine::reclaim_attrs) .map(BufferLine::reclaim_attrs)
.unwrap_or_else(|| AttrsList::new(Attrs::new())) .unwrap_or_else(|| AttrsList::new(&Attrs::new()))
.reset(default_attrs); .reset(default_attrs);
let next_line_string = self let next_line_string = self
.lines .lines
@ -1428,7 +1428,7 @@ impl BorrowedWithFontSystem<'_, Buffer> {
} }
/// Set text of buffer, using provided attributes for each line by default /// Set text of buffer, using provided attributes for each line by default
pub fn set_text(&mut self, text: &str, attrs: Attrs, shaping: Shaping) { pub fn set_text(&mut self, text: &str, attrs: &Attrs, shaping: Shaping) {
self.inner.set_text(self.font_system, text, attrs, shaping); self.inner.set_text(self.font_system, text, attrs, shaping);
} }
@ -1438,14 +1438,14 @@ impl BorrowedWithFontSystem<'_, Buffer> {
/// # use cosmic_text::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping}; /// # use cosmic_text::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping};
/// # let mut font_system = FontSystem::new(); /// # let mut font_system = FontSystem::new();
/// let mut buffer = Buffer::new_empty(Metrics::new(32.0, 44.0)); /// let mut buffer = Buffer::new_empty(Metrics::new(32.0, 44.0));
/// let mut buffer = buffer.borrow_with(&mut font_system);
/// let attrs = Attrs::new().family(Family::Serif); /// let attrs = Attrs::new().family(Family::Serif);
/// buffer.set_rich_text( /// buffer.set_rich_text(
/// &mut font_system,
/// [ /// [
/// ("hello, ", attrs), /// ("hello, ", attrs.clone()),
/// ("cosmic\ntext", attrs.family(Family::Monospace)), /// ("cosmic\ntext", attrs.clone().family(Family::Monospace)),
/// ], /// ],
/// attrs, /// &attrs,
/// Shaping::Advanced, /// Shaping::Advanced,
/// None, /// None,
/// ); /// );
@ -1453,7 +1453,7 @@ impl BorrowedWithFontSystem<'_, Buffer> {
pub fn set_rich_text<'r, 's, I>( pub fn set_rich_text<'r, 's, I>(
&mut self, &mut self,
spans: I, spans: I,
default_attrs: Attrs, default_attrs: &Attrs,
shaping: Shaping, shaping: Shaping,
alignment: Option<Align>, alignment: Option<Align>,
) where ) where

View file

@ -162,13 +162,13 @@ impl BufferLine {
if other.attrs_list.defaults() != self.attrs_list.defaults() { if other.attrs_list.defaults() != self.attrs_list.defaults() {
// If default formatting does not match, make a new span for it // If default formatting does not match, make a new span for it
self.attrs_list self.attrs_list
.add_span(len..len + other.text().len(), other.attrs_list.defaults()); .add_span(len..len + other.text().len(), &other.attrs_list.defaults());
} }
for (other_range, attrs) in other.attrs_list.spans_iter() { for (other_range, attrs) in other.attrs_list.spans_iter() {
// Add previous attrs spans // Add previous attrs spans
let range = other_range.start + len..other_range.end + len; let range = other_range.start + len..other_range.end + len;
self.attrs_list.add_span(range, attrs.as_attrs()); self.attrs_list.add_span(range, &attrs.as_attrs());
} }
self.reset(); self.reset();
@ -283,7 +283,7 @@ impl BufferLine {
Self { Self {
text: String::default(), text: String::default(),
ending: LineEnding::default(), ending: LineEnding::default(),
attrs_list: AttrsList::new(Attrs::new()), attrs_list: AttrsList::new(&Attrs::new()),
align: None, align: None,
shape_opt: Cached::Empty, shape_opt: Cached::Empty,
layout_opt: Cached::Empty, layout_opt: Cached::Empty,
@ -296,7 +296,7 @@ impl BufferLine {
/// ///
/// The buffer line is in an invalid state after this is called. See [`Self::reset_new`]. /// The buffer line is in an invalid state after this is called. See [`Self::reset_new`].
pub(crate) fn reclaim_attrs(&mut self) -> AttrsList { pub(crate) fn reclaim_attrs(&mut self) -> AttrsList {
mem::replace(&mut self.attrs_list, AttrsList::new(Attrs::new())) mem::replace(&mut self.attrs_list, AttrsList::new(&Attrs::new()))
} }
/// Reclaim text memory that isn't needed any longer. /// Reclaim text memory that isn't needed any longer.

View file

@ -380,7 +380,7 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
let line = BufferLine::new( let line = BufferLine::new(
String::new(), String::new(),
ending, ending,
AttrsList::new(attrs_list.as_ref().map_or_else( AttrsList::new(&attrs_list.as_ref().map_or_else(
|| { || {
buffer buffer
.lines .lines
@ -404,7 +404,7 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
// Collect attributes // Collect attributes
let mut final_attrs = attrs_list.unwrap_or_else(|| { let mut final_attrs = attrs_list.unwrap_or_else(|| {
AttrsList::new(line.attrs_list().get_span(cursor.index.saturating_sub(1))) AttrsList::new(&line.attrs_list().get_span(cursor.index.saturating_sub(1)))
}); });
// Append the inserted text, line by line // Append the inserted text, line by line

View file

@ -88,7 +88,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
foreground.a, foreground.a,
)); ));
} }
line.set_attrs_list(AttrsList::new(attrs)); line.set_attrs_list(AttrsList::new(&attrs));
} }
}); });
} }
@ -125,7 +125,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
let text = fs::read_to_string(path)?; let text = fs::read_to_string(path)?;
self.editor.with_buffer_mut(|buffer| { self.editor.with_buffer_mut(|buffer| {
buffer.set_text(font_system, &text, attrs, Shaping::Advanced); buffer.set_text(font_system, &text, &attrs, Shaping::Advanced);
}); });
//TODO: re-use text //TODO: re-use text
@ -332,9 +332,11 @@ impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
); );
let attrs = line.attrs_list().defaults(); let attrs = line.attrs_list().defaults();
let mut attrs_list = AttrsList::new(attrs); let mut attrs_list = AttrsList::new(&attrs);
let original_attrs = attrs.clone(); // Store a clone for comparison
for (style, _, range) in ranges { for (style, _, range) in ranges {
let span_attrs = attrs let span_attrs = attrs
.clone() // Clone attrs for modification
.color(Color::rgba( .color(Color::rgba(
style.foreground.r, style.foreground.r,
style.foreground.g, style.foreground.g,
@ -352,8 +354,8 @@ impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
} else { } else {
Weight::NORMAL Weight::NORMAL
}); //TODO: underline }); //TODO: underline
if span_attrs != attrs { if span_attrs != original_attrs {
attrs_list.add_span(range, span_attrs); attrs_list.add_span(range, &span_attrs);
} }
} }

View file

@ -4,7 +4,7 @@ use unicode_script::Script;
use super::Fallback; use super::Fallback;
/// A platform-specific font fallback list, for MacOS. /// A platform-specific font fallback list, for `MacOS`.
#[derive(Debug)] #[derive(Debug)]
pub struct PlatformFallback; pub struct PlatformFallback;

View file

@ -306,7 +306,7 @@ impl FontSystem {
}) })
} }
pub fn get_font_matches(&mut self, attrs: Attrs<'_>) -> Arc<Vec<FontMatchKey>> { pub fn get_font_matches(&mut self, attrs: &Attrs<'_>) -> Arc<Vec<FontMatchKey>> {
// Clear the cache first if it reached the size limit // Clear the cache first if it reached the size limit
if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT { if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT {
log::trace!("clear font mache cache"); log::trace!("clear font mache cache");

View file

@ -36,7 +36,7 @@
//! let attrs = Attrs::new(); //! let attrs = Attrs::new();
//! //!
//! // Add some text! //! // Add some text!
//! buffer.set_text("Hello, Rust! 🦀\n", attrs, Shaping::Advanced); //! buffer.set_text("Hello, Rust! 🦀\n", &attrs, Shaping::Advanced);
//! //!
//! // Perform shaping as desired //! // Perform shaping as desired
//! buffer.shape_until_scroll(true); //! buffer.shape_until_scroll(true);

View file

@ -146,7 +146,7 @@ fn shape_fallback(
// Convert attrs::Feature to rustybuzz::Feature // Convert attrs::Feature to rustybuzz::Feature
for feature in attrs.font_features.features { for feature in attrs.font_features.features {
rb_font_features.push(rustybuzz::Feature::new( rb_font_features.push(rustybuzz::Feature::new(
rustybuzz::ttf_parser::Tag::from_bytes(&feature.tag.as_bytes()), rustybuzz::ttf_parser::Tag::from_bytes(feature.tag.as_bytes()),
feature.value, feature.value,
0..usize::MAX, 0..usize::MAX,
)); ));
@ -261,7 +261,7 @@ fn shape_run(
let attrs = attrs_list.get_span(start_run); let attrs = attrs_list.get_span(start_run);
let fonts = font_system.get_font_matches(attrs); let fonts = font_system.get_font_matches(&attrs);
let default_families = [&attrs.family]; let default_families = [&attrs.family];
let mut font_iter = FontFallbackIter::new( let mut font_iter = FontFallbackIter::new(
@ -389,7 +389,7 @@ fn shape_run_cached(
let run_range = start_run..end_run; let run_range = start_run..end_run;
let mut key = ShapeRunKey { let mut key = ShapeRunKey {
text: line[run_range.clone()].to_string(), text: line[run_range.clone()].to_string(),
default_attrs: AttrsOwned::new(attrs_list.defaults()), default_attrs: AttrsOwned::new(&attrs_list.defaults()),
attrs_spans: Vec::new(), attrs_spans: Vec::new(),
}; };
for (attrs_range, attrs) in attrs_list.spans.overlapping(&run_range) { for (attrs_range, attrs) in attrs_list.spans.overlapping(&run_range) {
@ -444,7 +444,7 @@ fn shape_skip(
end_run: usize, end_run: usize,
) { ) {
let attrs = attrs_list.get_span(start_run); let attrs = attrs_list.get_span(start_run);
let fonts = font_system.get_font_matches(attrs); let fonts = font_system.get_font_matches(&attrs);
let default_families = [&attrs.family]; let default_families = [&attrs.family];
let mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &[], ""); let mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &[], "");

View file

@ -36,7 +36,7 @@ impl Default for DrawTestCfg {
let font = Attrs::new().family(Family::Serif); let font = Attrs::new().family(Family::Serif);
Self { Self {
name: "default".into(), name: "default".into(),
font: AttrsOwned::new(font), font: AttrsOwned::new(&font),
text: "".into(), text: "".into(),
font_size: 16.0, font_size: 16.0,
line_height: 20.0, line_height: 20.0,
@ -60,7 +60,7 @@ impl DrawTestCfg {
} }
pub fn font_attrs(mut self, attrs: Attrs) -> Self { pub fn font_attrs(mut self, attrs: Attrs) -> Self {
self.font = AttrsOwned::new(attrs); self.font = AttrsOwned::new(&attrs);
self self
} }
@ -92,7 +92,7 @@ impl DrawTestCfg {
Some((self.canvas_width - margins * 2) as f32), Some((self.canvas_width - margins * 2) as f32),
Some((self.canvas_height - margins * 2) as f32), Some((self.canvas_height - margins * 2) as f32),
); );
buffer.set_text(&self.text, self.font.as_attrs(), Shaping::Advanced); buffer.set_text(&self.text, &self.font.as_attrs(), Shaping::Advanced);
buffer.shape_until_scroll(true); buffer.shape_until_scroll(true);
// Black // Black

View file

@ -11,7 +11,7 @@ use cosmic_text::{
fn stable_wrap() { fn stable_wrap() {
let font_size = 18.0; let font_size = 18.0;
let attrs = AttrsList::new( let attrs = AttrsList::new(
Attrs::new() &Attrs::new()
.family(Family::Name("FiraMono")) .family(Family::Name("FiraMono"))
.weight(Weight::MEDIUM), .weight(Weight::MEDIUM),
); );
@ -105,7 +105,7 @@ fn wrap_extra_line() {
// Add some text! // Add some text!
buffer.set_wrap(Wrap::Word); buffer.set_wrap(Wrap::Word);
buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing\n\nweeewoooo minim sint cillum sint consectetur cupidatat.", Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced); buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing\n\nweeewoooo minim sint cillum sint consectetur cupidatat.", &Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced);
// Set a size for the text buffer, in pixels // Set a size for the text buffer, in pixels
buffer.set_size(Some(50.0), Some(1000.0)); buffer.set_size(Some(50.0), Some(1000.0));

View file

@ -15,7 +15,7 @@ fn wrap_word_fallback() {
let mut buffer = buffer.borrow_with(&mut font_system); let mut buffer = buffer.borrow_with(&mut font_system);
buffer.set_wrap(Wrap::WordOrGlyph); buffer.set_wrap(Wrap::WordOrGlyph);
buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.", Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced); buffer.set_text("Lorem ipsum dolor sit amet, qui minim labore adipisicing minim sint cillum sint consectetur cupidatat.", &Attrs::new().family(cosmic_text::Family::Name("Inter")), Shaping::Advanced);
buffer.set_size(Some(50.0), Some(1000.0)); buffer.set_size(Some(50.0), Some(1000.0));
buffer.shape_until_scroll(false); buffer.shape_until_scroll(false);