aboutsummaryrefslogtreecommitdiff
path: root/font/src
diff options
context:
space:
mode:
authorChristian Duerr <chrisduerr@users.noreply.github.com>2018-06-17 09:19:30 +0000
committerGitHub <noreply@github.com>2018-06-17 09:19:30 +0000
commit5ba34d4f9766a55a06ed5e3e44cc384af1b09f65 (patch)
tree11965fbe66f5d25ea00be1a02e74b724c4071d3c /font/src
parent0f700a01bd73623cdfc0afc4a54f9e82f46d8f49 (diff)
downloadalacritty-5ba34d4f9766a55a06ed5e3e44cc384af1b09f65.tar.gz
alacritty-5ba34d4f9766a55a06ed5e3e44cc384af1b09f65.zip
Move to cargo clippy
Using clippy as a library has been deprecated, instead the `cargo clippy` command should be used instead. To comply with this change clippy has been removed from the `Cargo.toml` and is now installed with cargo when building in CI. This has also lead to a few new clippy issues to show up, this includes everything in the `font` subdirectory. This has been fixed and `font` should now be covered by clippy CI too. This also upgrades all dependencies, as a result this fixes #1341 and this fixes #1344.
Diffstat (limited to 'font/src')
-rw-r--r--font/src/darwin/byte_order.rs4
-rw-r--r--font/src/darwin/mod.rs51
-rw-r--r--font/src/ft/fc/char_set.rs8
-rw-r--r--font/src/ft/fc/config.rs2
-rw-r--r--font/src/ft/fc/object_set.rs9
-rw-r--r--font/src/ft/fc/pattern.rs38
-rw-r--r--font/src/ft/mod.rs30
-rw-r--r--font/src/lib.rs37
8 files changed, 102 insertions, 77 deletions
diff --git a/font/src/darwin/byte_order.rs b/font/src/darwin/byte_order.rs
index 29efb5b1..2ea46fcb 100644
--- a/font/src/darwin/byte_order.rs
+++ b/font/src/darwin/byte_order.rs
@@ -24,7 +24,7 @@ pub const kCGBitmapByteOrder32Host: u32 = kCGBitmapByteOrder32Little;
pub const kCGBitmapByteOrder32Host: u32 = kCGBitmapByteOrder32Big;
#[cfg(target_endian = "little")]
-pub fn extract_rgb(bytes: Vec<u8>) -> Vec<u8> {
+pub fn extract_rgb(bytes: &[u8]) -> Vec<u8> {
let pixels = bytes.len() / 4;
let mut rgb = Vec::with_capacity(pixels * 3);
@@ -32,7 +32,7 @@ pub fn extract_rgb(bytes: Vec<u8>) -> Vec<u8> {
let offset = i * 4;
rgb.push(bytes[offset + 2]);
rgb.push(bytes[offset + 1]);
- rgb.push(bytes[offset + 0]);
+ rgb.push(bytes[offset]);
}
rgb
diff --git a/font/src/darwin/mod.rs b/font/src/darwin/mod.rs
index 14381113..843d75dd 100644
--- a/font/src/darwin/mod.rs
+++ b/font/src/darwin/mod.rs
@@ -132,8 +132,8 @@ impl ::Rasterize for Rasterizer {
Ok(Rasterizer {
fonts: HashMap::new(),
keys: HashMap::new(),
- device_pixel_ratio: device_pixel_ratio,
- use_thin_strokes: use_thin_strokes,
+ device_pixel_ratio,
+ use_thin_strokes,
})
}
@@ -196,7 +196,7 @@ impl Rasterizer {
for descriptor in descriptors {
if descriptor.style_name == style {
// Found the font we want
- let scaled_size = size.as_f32_pts() as f64 * self.device_pixel_ratio as f64;
+ let scaled_size = f64::from(size.as_f32_pts()) * f64::from(self.device_pixel_ratio);
let font = descriptor.to_font(scaled_size, true);
return Ok(font);
}
@@ -220,7 +220,7 @@ impl Rasterizer {
Slant::Normal => false,
_ => true,
};
- let scaled_size = size.as_f32_pts() as f64 * self.device_pixel_ratio as f64;
+ let scaled_size = f64::from(size.as_f32_pts()) * f64::from(self.device_pixel_ratio);
let descriptors = descriptors_for_family(&desc.name[..]);
for descriptor in descriptors {
@@ -250,7 +250,7 @@ impl Rasterizer {
font: &Font,
) -> Option<Result<RasterizedGlyph, Error>> {
let scaled_size = self.device_pixel_ratio * glyph.size.as_f32_pts();
- font.get_glyph(glyph.c, scaled_size as _, self.use_thin_strokes)
+ font.get_glyph(glyph.c, f64::from(scaled_size), self.use_thin_strokes)
.map(|r| Some(Ok(r)))
.unwrap_or_else(|e| match e {
Error::MissingGlyph(_) => None,
@@ -301,7 +301,7 @@ pub fn get_family_names() -> Vec<String> {
/// Return fallback descriptors for font/language list
fn cascade_list_for_languages(
ct_font: &CTFont,
- languages: &Vec<String>
+ languages: &[String]
) -> Vec<Descriptor> {
// convert language type &Vec<String> -> CFArray
@@ -368,12 +368,11 @@ impl Descriptor {
// many chars. We add the symbols back in.
// Investigate if we can actually use the .-prefixed
// fallbacks somehow.
- descriptors_for_family("Apple Symbols")
- .into_iter()
- .next() // should only have one element; use it
- .map(|descriptor| {
- fallbacks.push(descriptor.to_font(size, false))
- });
+ if let Some(descriptor) =
+ descriptors_for_family("Apple Symbols").into_iter().next()
+ {
+ fallbacks.push(descriptor.to_font(size, false))
+ };
// Include Menlo in the fallback list as well
fallbacks.insert(0, Font {
@@ -390,9 +389,9 @@ impl Descriptor {
};
Font {
- ct_font: ct_font,
- cg_font: cg_font,
- fallbacks: fallbacks,
+ ct_font,
+ cg_font,
+ fallbacks,
}
}
}
@@ -424,8 +423,8 @@ impl Font {
let line_height = (ascent + descent + leading + 0.5).floor();
Metrics {
- average_advance: average_advance,
- line_height: line_height,
+ average_advance,
+ line_height,
descent: -(self.ct_font.descent() as f32),
}
}
@@ -482,13 +481,13 @@ impl Font {
}
let glyph_index = self.glyph_index(character)
- .ok_or(Error::MissingGlyph(character))?;
+ .ok_or_else(|| Error::MissingGlyph(character))?;
let bounds = self.bounding_rect_for_glyph(Default::default(), glyph_index);
let rasterized_left = bounds.origin.x.floor() as i32;
let rasterized_width =
- (bounds.origin.x - (rasterized_left as f64) + bounds.size.width).ceil() as u32;
+ (bounds.origin.x - f64::from(rasterized_left) + bounds.size.width).ceil() as u32;
let rasterized_descent = (-bounds.origin.y).ceil() as i32;
let rasterized_ascent = (bounds.size.height + bounds.origin.y).ceil() as i32;
let rasterized_height = (rasterized_descent + rasterized_ascent) as u32;
@@ -519,8 +518,8 @@ impl Font {
let context_rect = CGRect::new(
&CGPoint::new(0.0, 0.0),
&CGSize::new(
- rasterized_width as f64,
- rasterized_height as f64
+ f64::from(rasterized_width),
+ f64::from(rasterized_height)
)
);
@@ -542,8 +541,8 @@ impl Font {
// Set fill color to white for drawing the glyph
cg_context.set_rgb_fill_color(1.0, 1.0, 1.0, 1.0);
let rasterization_origin = CGPoint {
- x: -rasterized_left as f64,
- y: rasterized_descent as f64,
+ x: f64::from(-rasterized_left),
+ y: f64::from(rasterized_descent),
};
self.ct_font.draw_glyphs(&[glyph_index as CGGlyph],
@@ -552,7 +551,7 @@ impl Font {
let rasterized_pixels = cg_context.data().to_vec();
- let buf = extract_rgb(rasterized_pixels);
+ let buf = extract_rgb(&rasterized_pixels);
Ok(RasterizedGlyph {
c: character,
@@ -560,7 +559,7 @@ impl Font {
top: (bounds.size.height + bounds.origin.y).ceil() as i32,
width: rasterized_width as i32,
height: rasterized_height as i32,
- buf: buf,
+ buf,
})
}
@@ -585,7 +584,7 @@ impl Font {
);
if res {
- Some(glyphs[0] as u32)
+ Some(u32::from(glyphs[0]))
} else {
None
}
diff --git a/font/src/ft/fc/char_set.rs b/font/src/ft/fc/char_set.rs
index e6fe027a..151d14a3 100644
--- a/font/src/ft/fc/char_set.rs
+++ b/font/src/ft/fc/char_set.rs
@@ -24,7 +24,13 @@ foreign_type! {
}
impl CharSet {
- pub fn new() -> CharSet {
+ pub fn new() -> Self {
+ Self::default()
+ }
+}
+
+impl Default for CharSet {
+ fn default() -> Self {
CharSet(unsafe { FcCharSetCreate() })
}
}
diff --git a/font/src/ft/fc/config.rs b/font/src/ft/fc/config.rs
index 92993fe8..9744b37a 100644
--- a/font/src/ft/fc/config.rs
+++ b/font/src/ft/fc/config.rs
@@ -38,7 +38,7 @@ impl Config {
impl ConfigRef {
/// Returns one of the two sets of fonts from the configuration as
/// specified by `set`.
- pub fn get_fonts<'a>(&'a self, set: SetName) -> &'a FontSetRef {
+ pub fn get_fonts(&self, set: SetName) -> &FontSetRef {
unsafe {
let ptr = FcConfigGetFonts(self.as_ptr(), set as u32);
FontSetRef::from_ptr(ptr)
diff --git a/font/src/ft/fc/object_set.rs b/font/src/ft/fc/object_set.rs
index 42e03f64..47d11674 100644
--- a/font/src/ft/fc/object_set.rs
+++ b/font/src/ft/fc/object_set.rs
@@ -24,8 +24,13 @@ foreign_type! {
}
impl ObjectSet {
- #[allow(dead_code)]
- pub fn new() -> ObjectSet {
+ pub fn new() -> Self {
+ Self::default()
+ }
+}
+
+impl Default for ObjectSet {
+ fn default() -> Self {
ObjectSet(unsafe {
FcObjectSetCreate()
})
diff --git a/font/src/ft/fc/pattern.rs b/font/src/ft/fc/pattern.rs
index 592d3859..bb0c4c38 100644
--- a/font/src/ft/fc/pattern.rs
+++ b/font/src/ft/fc/pattern.rs
@@ -39,8 +39,8 @@ pub struct StringPropertyIter<'a> {
impl<'a> StringPropertyIter<'a> {
fn new<'b>(pattern: &'b PatternRef, object: &'b [u8]) -> StringPropertyIter<'b> {
StringPropertyIter {
- pattern: pattern,
- object: object,
+ pattern,
+ object,
index: 0
}
}
@@ -82,8 +82,8 @@ pub struct BooleanPropertyIter<'a> {
impl<'a> BooleanPropertyIter<'a> {
fn new<'b>(pattern: &'b PatternRef, object: &'b [u8]) -> BooleanPropertyIter<'b> {
BooleanPropertyIter {
- pattern: pattern,
- object: object,
+ pattern,
+ object,
index: 0
}
}
@@ -101,7 +101,7 @@ impl<'a> BooleanPropertyIter<'a> {
};
if result == FcResultMatch {
- Some(!(value == 0))
+ Some(value != 0)
} else {
None
}
@@ -118,8 +118,8 @@ pub struct IntPropertyIter<'a> {
impl<'a> IntPropertyIter<'a> {
fn new<'b>(pattern: &'b PatternRef, object: &'b [u8]) -> IntPropertyIter<'b> {
IntPropertyIter {
- pattern: pattern,
- object: object,
+ pattern,
+ object,
index: 0
}
}
@@ -171,7 +171,7 @@ pub struct HintStylePropertyIter<'a> {
}
impl<'a> HintStylePropertyIter<'a> {
- fn new<'b>(pattern: &'b PatternRef) -> HintStylePropertyIter<'b> {
+ fn new(pattern: &PatternRef) -> HintStylePropertyIter {
HintStylePropertyIter {
inner: IntPropertyIter::new(pattern, b"hintstyle\0")
}
@@ -201,7 +201,7 @@ pub struct LcdFilterPropertyIter<'a> {
}
impl<'a> LcdFilterPropertyIter<'a> {
- fn new<'b>(pattern: &'b PatternRef) -> LcdFilterPropertyIter<'b> {
+ fn new(pattern: &PatternRef) -> LcdFilterPropertyIter {
LcdFilterPropertyIter {
inner: IntPropertyIter::new(pattern, b"lcdfilter\0")
}
@@ -236,14 +236,14 @@ pub struct DoublePropertyIter<'a> {
impl<'a> DoublePropertyIter<'a> {
fn new<'b>(pattern: &'b PatternRef, object: &'b [u8]) -> DoublePropertyIter<'b> {
DoublePropertyIter {
- pattern: pattern,
- object: object,
+ pattern,
+ object,
index: 0
}
}
fn get_value(&self, index: usize) -> Option<f64> {
- let mut value = 0 as c_double;
+ let mut value = f64::from(0);
let result = unsafe {
FcPatternGetDouble(
@@ -379,7 +379,13 @@ macro_rules! string_accessor {
}
impl Pattern {
- pub fn new() -> Pattern {
+ pub fn new() -> Self {
+ Self::default()
+ }
+}
+
+impl Default for Pattern {
+ fn default() -> Self {
Pattern(unsafe { FcPatternCreate() })
}
}
@@ -483,11 +489,11 @@ impl PatternRef {
BooleanPropertyIter::new(self, object)
}
- pub fn hintstyle<'a>(&'a self) -> HintStylePropertyIter<'a> {
+ pub fn hintstyle(&self) -> HintStylePropertyIter {
HintStylePropertyIter::new(self)
}
- pub fn lcdfilter<'a>(&'a self) -> LcdFilterPropertyIter<'a> {
+ pub fn lcdfilter(&self) -> LcdFilterPropertyIter {
LcdFilterPropertyIter::new(self)
}
@@ -565,7 +571,7 @@ impl PatternRef {
RgbaPropertyIter::new(self, b"rgba\0")
}
- pub fn set_rgba(&self, rgba: Rgba) -> bool {
+ pub fn set_rgba(&self, rgba: &Rgba) -> bool {
unsafe {
self.add_integer(b"rgba\0", rgba.to_isize())
}
diff --git a/font/src/ft/mod.rs b/font/src/ft/mod.rs
index 68d2faf3..7c5fb768 100644
--- a/font/src/ft/mod.rs
+++ b/font/src/ft/mod.rs
@@ -80,8 +80,8 @@ impl ::Rasterize for FreeTypeRasterizer {
Ok(FreeTypeRasterizer {
faces: HashMap::new(),
keys: HashMap::new(),
- library: library,
- device_pixel_ratio: device_pixel_ratio,
+ library,
+ device_pixel_ratio,
})
}
@@ -94,7 +94,7 @@ impl ::Rasterize for FreeTypeRasterizer {
Ok(Metrics {
average_advance: full.cell_width,
line_height: height,
- descent: descent,
+ descent,
})
}
@@ -172,7 +172,7 @@ impl FreeTypeRasterizer {
} as f64;
Ok(FullMetrics {
- size_metrics: size_metrics,
+ size_metrics,
cell_width: width
})
}
@@ -188,7 +188,7 @@ impl FreeTypeRasterizer {
pattern.add_family(&desc.name);
pattern.set_weight(weight.into_fontconfig_type());
pattern.set_slant(slant.into_fontconfig_type());
- pattern.add_pixelsize(size.as_f32_pts() as _);
+ pattern.add_pixelsize(f64::from(size.as_f32_pts()));
let font = fc::font_match(fc::Config::get_current(), &mut pattern)
.ok_or_else(|| Error::MissingFont(desc.to_owned()))?;
@@ -210,7 +210,7 @@ impl FreeTypeRasterizer {
let mut pattern = fc::Pattern::new();
pattern.add_family(&desc.name);
pattern.add_style(style);
- pattern.add_pixelsize(size.as_f32_pts() as _);
+ pattern.add_pixelsize(f64::from(size.as_f32_pts()));
let font = fc::font_match(fc::Config::get_current(), &mut pattern)
.ok_or_else(|| Error::MissingFont(desc.to_owned()))?;
@@ -244,12 +244,12 @@ impl FreeTypeRasterizer {
};
let face = Face {
- ft_face: ft_face,
+ ft_face,
key: FontKey::next(),
load_flags: Self::ft_load_flags(pattern),
render_mode: Self::ft_render_mode(pattern),
lcd_filter: Self::ft_lcd_filter(pattern),
- non_scalable: non_scalable,
+ non_scalable,
};
debug!("Loaded Face {:?}", face);
@@ -269,14 +269,10 @@ impl FreeTypeRasterizer {
let use_initial_face = if self.faces.contains_key(&glyph_key.font_key) {
// Get face and unwrap since we just checked for presence.
- let face = self.faces.get(&glyph_key.font_key).unwrap();
+ let face = &self.faces[&glyph_key.font_key];
let index = face.ft_face.get_char_index(c as usize);
- if index != 0 || have_recursed {
- true
- } else {
- false
- }
+ index != 0 || have_recursed
} else {
false
};
@@ -334,7 +330,7 @@ impl FreeTypeRasterizer {
// Render a normal character if it's not a cursor
let font_key = self.face_for_glyph(glyph_key, false)?;
- let face = self.faces.get(&font_key).unwrap();
+ let face = &self.faces[&font_key];
let index = face.ft_face.get_char_index(glyph_key.c as usize);
let size = face.non_scalable.as_ref()
@@ -360,7 +356,7 @@ impl FreeTypeRasterizer {
left: glyph.bitmap_left(),
width: pixel_width,
height: glyph.bitmap().rows(),
- buf: buf,
+ buf,
})
}
@@ -490,7 +486,7 @@ impl FreeTypeRasterizer {
}
Ok((bitmap.width(), packed))
},
- mode @ _ => panic!("unhandled pixel mode: {:?}", mode)
+ mode => panic!("unhandled pixel mode: {:?}", mode)
}
}
diff --git a/font/src/lib.rs b/font/src/lib.rs
index 330ed362..05680da2 100644
--- a/font/src/lib.rs
+++ b/font/src/lib.rs
@@ -116,7 +116,7 @@ impl FontDesc {
{
FontDesc {
name: name.into(),
- style: style
+ style,
}
}
}
@@ -146,7 +146,7 @@ impl FontKey {
}
}
-#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+#[derive(Debug, Copy, Clone, Eq)]
pub struct GlyphKey {
pub c: char,
pub font_key: FontKey,
@@ -165,6 +165,19 @@ impl Hash for GlyphKey {
}
}
+impl PartialEq for GlyphKey {
+ fn eq(&self, other: &Self) -> bool {
+ unsafe {
+ // This transmute is fine:
+ //
+ // - If GlyphKey ever becomes a different size, this will fail to compile
+ // - Result is being used for equality checking and has no fields (it's a u64)
+ let other = ::std::mem::transmute::<GlyphKey, u64>(*other);
+ ::std::mem::transmute::<GlyphKey, u64>(*self).eq(&other)
+ }
+ }
+}
+
/// Font size stored as integer
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Size(i16);
@@ -183,7 +196,7 @@ impl Size {
/// Get the f32 size in points
pub fn as_f32_pts(self) -> f32 {
- self.0 as f32 / Size::factor()
+ f32::from(self.0) / Size::factor()
}
}
@@ -224,14 +237,14 @@ pub fn get_underline_cursor_glyph(descent: i32, width: i32) -> Result<Rasterized
let buf = vec![255u8; (width * height * 3) as usize];
// Create a custom glyph with the rectangle data attached to it
- return Ok(RasterizedGlyph {
+ Ok(RasterizedGlyph {
c: UNDERLINE_CURSOR_CHAR,
top: descent + height,
left: 0,
height,
width,
- buf: buf,
- });
+ buf,
+ })
}
// Returns a custom beam cursor character
@@ -245,14 +258,14 @@ pub fn get_beam_cursor_glyph(
let buf = vec![255u8; (beam_width * height * 3) as usize];
// Create a custom glyph with the rectangle data attached to it
- return Ok(RasterizedGlyph {
+ Ok(RasterizedGlyph {
c: BEAM_CURSOR_CHAR,
top: ascent,
left: 0,
height,
width: beam_width,
- buf: buf,
- });
+ buf,
+ })
}
// Returns a custom box cursor character
@@ -276,14 +289,14 @@ pub fn get_box_cursor_glyph(
}
// Create a custom glyph with the rectangle data attached to it
- return Ok(RasterizedGlyph {
+ Ok(RasterizedGlyph {
c: BOX_CURSOR_CHAR,
top: ascent,
left: 0,
height,
width,
- buf: buf,
- });
+ buf,
+ })
}
struct BufDebugger<'a>(&'a [u8]);