Skip to content

Commit

Permalink
clippy: fix warnings in components/gfx (#31560)
Browse files Browse the repository at this point in the history
* clippy: fix warnings in components/gfx

* refactor: switched the order of impl so that its intent is clearer

* fix: add font context default in other platforms
  • Loading branch information
eerii committed Mar 8, 2024
1 parent 1771f9a commit 88033bd
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 237 deletions.
32 changes: 14 additions & 18 deletions components/gfx/font.rs
Expand Up @@ -96,7 +96,7 @@ impl FontTableTagConversions for FontTableTag {
(self >> 24) as u8,
(self >> 16) as u8,
(self >> 8) as u8,
(self >> 0) as u8,
*self as u8,
];
str::from_utf8(&bytes).unwrap().to_owned()
}
Expand Down Expand Up @@ -190,7 +190,7 @@ impl Font {
let metrics = handle.metrics();

Font {
handle: handle,
handle,
shaper: None,
descriptor,
metrics,
Expand Down Expand Up @@ -403,7 +403,7 @@ impl FontGroup {
.font_family
.families
.iter()
.map(|family| FontGroupFamily::new(descriptor.clone(), &family))
.map(|family| FontGroupFamily::new(descriptor.clone(), family))
.collect();

FontGroup {
Expand All @@ -419,7 +419,7 @@ impl FontGroup {
/// found, returns None.
pub fn find_by_codepoint<S: FontSource>(
&mut self,
mut font_context: &mut FontContext<S>,
font_context: &mut FontContext<S>,
codepoint: char,
) -> Option<FontRef> {
let should_look_for_small_caps = self.descriptor.variant == font_variant_caps::T::SmallCaps &&
Expand All @@ -436,43 +436,40 @@ impl FontGroup {

let has_glyph = |font: &FontRef| font.borrow().has_glyph_for(codepoint);

if let Some(font) = self.find(&mut font_context, |font| has_glyph(font)) {
if let Some(font) = self.find(font_context, has_glyph) {
return font_or_synthesized_small_caps(font);
}

if let Some(ref last_matching_fallback) = self.last_matching_fallback {
if has_glyph(&last_matching_fallback) {
if has_glyph(last_matching_fallback) {
return font_or_synthesized_small_caps(last_matching_fallback.clone());
}
}

if let Some(font) = self.find_fallback(&mut font_context, Some(codepoint), has_glyph) {
if let Some(font) = self.find_fallback(font_context, Some(codepoint), has_glyph) {
self.last_matching_fallback = Some(font.clone());
return font_or_synthesized_small_caps(font);
}

self.first(&mut font_context)
self.first(font_context)
}

/// Find the first available font in the group, or the first available fallback font.
pub fn first<S: FontSource>(
&mut self,
mut font_context: &mut FontContext<S>,
) -> Option<FontRef> {
self.find(&mut font_context, |_| true)
.or_else(|| self.find_fallback(&mut font_context, None, |_| true))
pub fn first<S: FontSource>(&mut self, font_context: &mut FontContext<S>) -> Option<FontRef> {
self.find(font_context, |_| true)
.or_else(|| self.find_fallback(font_context, None, |_| true))
}

/// Find a font which returns true for `predicate`. This method mutates because we may need to
/// load new font data in the process of finding a suitable font.
fn find<S, P>(&mut self, mut font_context: &mut FontContext<S>, predicate: P) -> Option<FontRef>
fn find<S, P>(&mut self, font_context: &mut FontContext<S>, predicate: P) -> Option<FontRef>
where
S: FontSource,
P: FnMut(&FontRef) -> bool,
{
self.families
.iter_mut()
.filter_map(|family| family.font(&mut font_context))
.filter_map(|family| family.font(font_context))
.find(predicate)
}

Expand Down Expand Up @@ -567,8 +564,7 @@ impl RunMetrics {
}

pub fn get_and_reset_text_shaping_performance_counter() -> usize {
let value = TEXT_SHAPING_PERFORMANCE_COUNTER.swap(0, Ordering::SeqCst);
value
TEXT_SHAPING_PERFORMANCE_COUNTER.swap(0, Ordering::SeqCst)
}

/// The scope within which we will look for a font.
Expand Down
34 changes: 14 additions & 20 deletions components/gfx/font_cache_thread.rs
Expand Up @@ -31,6 +31,7 @@ use crate::platform::font_list::{
use crate::platform::font_template::FontTemplateData;

/// A list of font templates that make up a given font family.
#[derive(Default)]
pub struct FontTemplates {
templates: Vec<FontTemplate>,
}
Expand Down Expand Up @@ -61,10 +62,6 @@ impl SerializedFontTemplate {
}

impl FontTemplates {
pub fn new() -> FontTemplates {
FontTemplates { templates: vec![] }
}

/// Find a font in this family that matches a given descriptor.
pub fn find_font_for_style(
&mut self,
Expand Down Expand Up @@ -214,7 +211,7 @@ impl FontCache {
font_key: font_template_info.font_key,
},
)));
let _ = bytes_sender.send(&*font_template_info.font_template.bytes());
let _ = bytes_sender.send(&font_template_info.font_template.bytes());
},
};
},
Expand Down Expand Up @@ -262,7 +259,7 @@ impl FontCache {
};

if !self.web_families.contains_key(&family_name) {
let templates = FontTemplates::new();
let templates = FontTemplates::default();
self.web_families.insert(family_name.clone(), templates);
}

Expand Down Expand Up @@ -298,7 +295,7 @@ impl FontCache {
FetchResponseMsg::ProcessResponseChunk(new_bytes) => {
trace!("@font-face {} chunk={:?}", family_name, new_bytes);
if *response_valid.lock().unwrap() {
bytes.lock().unwrap().extend(new_bytes.into_iter())
bytes.lock().unwrap().extend(new_bytes)
}
},
FetchResponseMsg::ProcessResponseEOF(response) => {
Expand All @@ -312,7 +309,7 @@ impl FontCache {
channel_to_self.send(msg).unwrap();
return;
}
let bytes = mem::replace(&mut *bytes.lock().unwrap(), vec![]);
let bytes = mem::take(&mut *bytes.lock().unwrap());
trace!("@font-face {} data={:?}", family_name, bytes);
let bytes = match fontsan::process(&bytes) {
Ok(san) => san,
Expand Down Expand Up @@ -365,10 +362,7 @@ impl FontCache {
self.local_families.clear();
for_each_available_family(|family_name| {
let family_name = LowercaseString::new(&family_name);
if !self.local_families.contains_key(&family_name) {
let templates = FontTemplates::new();
self.local_families.insert(family_name, templates);
}
self.local_families.entry(family_name).or_default();
});
}

Expand Down Expand Up @@ -443,7 +437,7 @@ impl FontCache {

FontTemplateInfo {
font_template: template,
font_key: font_key,
font_key,
}
}

Expand All @@ -454,13 +448,13 @@ impl FontCache {
) -> Option<FontTemplateInfo> {
match family_descriptor.scope {
FontSearchScope::Any => self
.find_font_in_web_family(&template_descriptor, &family_descriptor.name)
.find_font_in_web_family(template_descriptor, &family_descriptor.name)
.or_else(|| {
self.find_font_in_local_family(&template_descriptor, &family_descriptor.name)
self.find_font_in_local_family(template_descriptor, &family_descriptor.name)
}),

FontSearchScope::Local => {
self.find_font_in_local_family(&template_descriptor, &family_descriptor.name)
self.find_font_in_local_family(template_descriptor, &family_descriptor.name)
},
}
.map(|t| self.get_font_template_info(t))
Expand Down Expand Up @@ -489,12 +483,12 @@ impl FontCacheThread {
let generic_fonts = populate_generic_fonts();

let mut cache = FontCache {
port: port,
port,
channel_to_self,
generic_fonts,
local_families: HashMap::new(),
web_families: HashMap::new(),
font_context: FontContextHandle::new(),
font_context: FontContextHandle::default(),
core_resource_thread,
webrender_api,
webrender_fonts: HashMap::new(),
Expand All @@ -506,7 +500,7 @@ impl FontCacheThread {
})
.expect("Thread spawning failed");

FontCacheThread { chan: chan }
FontCacheThread { chan }
}

pub fn add_web_font(
Expand Down Expand Up @@ -621,7 +615,7 @@ impl Deref for LowercaseString {

#[inline]
fn deref(&self) -> &str {
&*self.inner
&self.inner
}
}

Expand Down
57 changes: 27 additions & 30 deletions components/gfx/font_context.rs
Expand Up @@ -65,7 +65,7 @@ pub struct FontContext<S: FontSource> {

impl<S: FontSource> FontContext<S> {
pub fn new(font_source: S) -> FontContext<S> {
let handle = FontContextHandle::new();
let handle = FontContextHandle::default();
FontContext {
platform_handle: handle,
font_source,
Expand Down Expand Up @@ -99,8 +99,8 @@ impl<S: FontSource> FontContext<S> {
style,
};

if let Some(ref font_group) = self.font_group_cache.get(&cache_key) {
return (*font_group).clone();
if let Some(font_group) = self.font_group_cache.get(&cache_key) {
return font_group.clone();
}

let font_group = Rc::new(RefCell::new(FontGroup::new(&cache_key.style)));
Expand Down Expand Up @@ -150,30 +150,27 @@ impl<S: FontSource> FontContext<S> {
family_descriptor: family_descriptor.clone(),
};

self.font_cache
.get(&cache_key)
.map(|v| v.clone())
.unwrap_or_else(|| {
debug!(
"FontContext::font cache miss for font_descriptor={:?} family_descriptor={:?}",
font_descriptor, family_descriptor
);

let font = self
.font_template(&font_descriptor.template_descriptor, family_descriptor)
.and_then(|template_info| {
self.create_font(
template_info,
font_descriptor.to_owned(),
synthesized_small_caps_font,
)
.ok()
})
.map(|font| Rc::new(RefCell::new(font)));

self.font_cache.insert(cache_key, font.clone());
font
})
self.font_cache.get(&cache_key).cloned().unwrap_or_else(|| {
debug!(
"FontContext::font cache miss for font_descriptor={:?} family_descriptor={:?}",
font_descriptor, family_descriptor
);

let font = self
.font_template(&font_descriptor.template_descriptor, family_descriptor)
.and_then(|template_info| {
self.create_font(
template_info,
font_descriptor.to_owned(),
synthesized_small_caps_font,
)
.ok()
})
.map(|font| Rc::new(RefCell::new(font)));

self.font_cache.insert(cache_key, font.clone());
font
})
}

fn font_template(
Expand All @@ -182,19 +179,19 @@ impl<S: FontSource> FontContext<S> {
family_descriptor: &FontFamilyDescriptor,
) -> Option<FontTemplateInfo> {
let cache_key = FontTemplateCacheKey {
template_descriptor: template_descriptor.clone(),
template_descriptor: *template_descriptor,
family_descriptor: family_descriptor.clone(),
};

self.font_template_cache.get(&cache_key).map(|v| v.clone()).unwrap_or_else(|| {
self.font_template_cache.get(&cache_key).cloned().unwrap_or_else(|| {
debug!(
"FontContext::font_template cache miss for template_descriptor={:?} family_descriptor={:?}",
template_descriptor,
family_descriptor
);

let template_info = self.font_source.font_template(
template_descriptor.clone(),
*template_descriptor,
family_descriptor.clone(),
);

Expand Down
16 changes: 5 additions & 11 deletions components/gfx/font_template.rs
Expand Up @@ -109,18 +109,12 @@ impl FontTemplate {
None => None,
};

let maybe_strong_ref = match maybe_data {
Some(data) => Some(Arc::new(data)),
None => None,
};
let maybe_strong_ref = maybe_data.map(Arc::new);

let maybe_weak_ref = match maybe_strong_ref {
Some(ref strong_ref) => Some(Arc::downgrade(strong_ref)),
None => None,
};
let maybe_weak_ref = maybe_strong_ref.as_ref().map(Arc::downgrade);

Ok(FontTemplate {
identifier: identifier,
identifier,
descriptor: None,
weak_ref: maybe_weak_ref,
strong_ref: maybe_strong_ref,
Expand Down Expand Up @@ -161,7 +155,7 @@ impl FontTemplate {
fctx: &FontContextHandle,
requested_desc: &FontTemplateDescriptor,
) -> Option<Arc<FontTemplateData>> {
self.descriptor(&fctx).and_then(|descriptor| {
self.descriptor(fctx).and_then(|descriptor| {
if *requested_desc == descriptor {
self.data().ok()
} else {
Expand All @@ -177,7 +171,7 @@ impl FontTemplate {
font_context: &FontContextHandle,
requested_descriptor: &FontTemplateDescriptor,
) -> Option<(Arc<FontTemplateData>, f32)> {
self.descriptor(&font_context).and_then(|descriptor| {
self.descriptor(font_context).and_then(|descriptor| {
self.data()
.ok()
.map(|data| (data, descriptor.distance_from(requested_descriptor)))
Expand Down

0 comments on commit 88033bd

Please sign in to comment.