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

@ -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)]
pub struct FeatureTag([u8; 4]);
@ -171,7 +171,9 @@ pub struct FontFeatures {
impl FontFeatures {
pub fn new() -> Self {
Self { features: Vec::new() }
Self {
features: Vec::new(),
}
}
pub fn set(&mut self, tag: FeatureTag, value: u32) -> &mut Self {
@ -311,7 +313,7 @@ impl<'a> Attrs<'a> {
self
}
/// Set [FontFeatures]
/// Set [`FontFeatures`]
pub fn font_features(mut self, font_features: FontFeatures) -> Self {
self.font_features = font_features;
self
@ -342,8 +344,8 @@ pub struct FontMatchAttrs {
weight: Weight,
}
impl<'a> From<Attrs<'a>> for FontMatchAttrs {
fn from(attrs: Attrs<'a>) -> Self {
impl<'a> From<&Attrs<'a>> for FontMatchAttrs {
fn from(attrs: &Attrs<'a>) -> Self {
Self {
family: FamilyOwned::new(attrs.family),
stretch: attrs.stretch,
@ -371,7 +373,7 @@ pub struct AttrsOwned {
}
impl AttrsOwned {
pub fn new(attrs: Attrs) -> Self {
pub fn new(attrs: &Attrs) -> Self {
Self {
color_opt: attrs.color_opt,
family_owned: FamilyOwned::new(attrs.family),
@ -412,7 +414,7 @@ pub struct AttrsList {
impl AttrsList {
/// Create a new attributes list with a set of default [Attrs]
pub fn new(defaults: Attrs) -> Self {
pub fn new(defaults: &Attrs) -> Self {
Self {
defaults: AttrsOwned::new(defaults),
spans: RangeMap::new(),
@ -440,7 +442,7 @@ impl AttrsList {
}
/// 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.
if range.is_empty() {
return;
@ -462,7 +464,7 @@ impl AttrsList {
/// Split attributes list at an offset
#[allow(clippy::missing_panics_doc)]
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();
//get the keys we need to remove or fix.
@ -496,7 +498,7 @@ impl AttrsList {
}
/// 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.spans.clear();
self

View file

@ -266,7 +266,7 @@ impl Buffer {
/// Will panic if `metrics.line_height` is zero.
pub fn new(font_system: &mut FontSystem, metrics: Metrics) -> Self {
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
}
@ -676,7 +676,7 @@ impl Buffer {
&mut self,
font_system: &mut FontSystem,
text: &str,
attrs: Attrs,
attrs: &Attrs,
shaping: Shaping,
) {
self.lines.clear();
@ -710,10 +710,10 @@ impl Buffer {
/// buffer.set_rich_text(
/// &mut font_system,
/// [
/// ("hello, ", attrs),
/// ("cosmic\ntext", attrs.family(Family::Monospace)),
/// ("hello, ", attrs.clone()),
/// ("cosmic\ntext", attrs.clone().family(Family::Monospace)),
/// ],
/// attrs,
/// &attrs,
/// Shaping::Advanced,
/// None,
/// );
@ -722,7 +722,7 @@ impl Buffer {
&mut self,
font_system: &mut FontSystem,
spans: I,
default_attrs: Attrs,
default_attrs: &Attrs,
shaping: Shaping,
alignment: Option<Align>,
) where
@ -758,7 +758,7 @@ impl Buffer {
.lines
.get_mut(line_count)
.map(BufferLine::reclaim_attrs)
.unwrap_or_else(|| AttrsList::new(Attrs::new()))
.unwrap_or_else(|| AttrsList::new(&Attrs::new()))
.reset(default_attrs);
let mut line_string = self
.lines
@ -792,7 +792,7 @@ impl Buffer {
let text_end = line_string.len();
// Only add attrs if they don't match the 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
.get_mut(line_count + 1)
.map(BufferLine::reclaim_attrs)
.unwrap_or_else(|| AttrsList::new(Attrs::new()))
.unwrap_or_else(|| AttrsList::new(&Attrs::new()))
.reset(default_attrs);
let next_line_string = self
.lines
@ -1428,7 +1428,7 @@ impl BorrowedWithFontSystem<'_, Buffer> {
}
/// 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);
}
@ -1438,14 +1438,14 @@ impl BorrowedWithFontSystem<'_, Buffer> {
/// # use cosmic_text::{Attrs, Buffer, Family, FontSystem, Metrics, Shaping};
/// # let mut font_system = FontSystem::new();
/// 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);
/// buffer.set_rich_text(
/// &mut font_system,
/// [
/// ("hello, ", attrs),
/// ("cosmic\ntext", attrs.family(Family::Monospace)),
/// ("hello, ", attrs.clone()),
/// ("cosmic\ntext", attrs.clone().family(Family::Monospace)),
/// ],
/// attrs,
/// &attrs,
/// Shaping::Advanced,
/// None,
/// );
@ -1453,7 +1453,7 @@ impl BorrowedWithFontSystem<'_, Buffer> {
pub fn set_rich_text<'r, 's, I>(
&mut self,
spans: I,
default_attrs: Attrs,
default_attrs: &Attrs,
shaping: Shaping,
alignment: Option<Align>,
) where

View file

@ -162,13 +162,13 @@ impl BufferLine {
if other.attrs_list.defaults() != self.attrs_list.defaults() {
// If default formatting does not match, make a new span for it
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() {
// Add previous attrs spans
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();
@ -283,7 +283,7 @@ impl BufferLine {
Self {
text: String::default(),
ending: LineEnding::default(),
attrs_list: AttrsList::new(Attrs::new()),
attrs_list: AttrsList::new(&Attrs::new()),
align: None,
shape_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`].
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.

View file

@ -380,7 +380,7 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
let line = BufferLine::new(
String::new(),
ending,
AttrsList::new(attrs_list.as_ref().map_or_else(
AttrsList::new(&attrs_list.as_ref().map_or_else(
|| {
buffer
.lines
@ -404,7 +404,7 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
// Collect attributes
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

View file

@ -88,7 +88,7 @@ impl<'syntax_system, 'buffer> SyntaxEditor<'syntax_system, 'buffer> {
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)?;
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
@ -332,9 +332,11 @@ impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
);
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 {
let span_attrs = attrs
.clone() // Clone attrs for modification
.color(Color::rgba(
style.foreground.r,
style.foreground.g,
@ -352,8 +354,8 @@ impl<'buffer> Edit<'buffer> for SyntaxEditor<'_, 'buffer> {
} else {
Weight::NORMAL
}); //TODO: underline
if span_attrs != attrs {
attrs_list.add_span(range, span_attrs);
if span_attrs != original_attrs {
attrs_list.add_span(range, &span_attrs);
}
}

View file

@ -4,7 +4,7 @@ use unicode_script::Script;
use super::Fallback;
/// A platform-specific font fallback list, for MacOS.
/// A platform-specific font fallback list, for `MacOS`.
#[derive(Debug)]
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
if self.font_matches_cache.len() >= Self::FONT_MATCHES_CACHE_SIZE_LIMIT {
log::trace!("clear font mache cache");

View file

@ -36,7 +36,7 @@
//! let attrs = Attrs::new();
//!
//! // 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
//! buffer.shape_until_scroll(true);

View file

@ -146,7 +146,7 @@ fn shape_fallback(
// Convert attrs::Feature to rustybuzz::Feature
for feature in attrs.font_features.features {
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,
0..usize::MAX,
));
@ -261,7 +261,7 @@ fn shape_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 mut font_iter = FontFallbackIter::new(
@ -389,7 +389,7 @@ fn shape_run_cached(
let run_range = start_run..end_run;
let mut key = ShapeRunKey {
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(),
};
for (attrs_range, attrs) in attrs_list.spans.overlapping(&run_range) {
@ -444,7 +444,7 @@ fn shape_skip(
end_run: usize,
) {
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 mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &[], "");