Struct IconFamily

Source
pub struct IconFamily {
    pub elements: Vec<IconElement>,
}
Expand description

A set of icons stored in a single ICNS file.

Fields§

§elements: Vec<IconElement>

The icon elements stored in the ICNS file.

Implementations§

Source§

impl IconFamily

Source

pub fn new() -> IconFamily

Creates a new, empty icon family.

Examples found in repository?
examples/png2icns.rs (line 44)
34fn main() {
35    let num_args = env::args().count();
36    if num_args < 2 || num_args > 3 {
37        println!("Usage: png2icns <path> [<ostype>]");
38        return;
39    }
40    let png_path = env::args().nth(1).unwrap();
41    let png_path = Path::new(&png_path);
42    let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43    let image = Image::read_png(png_file).expect("failed to read PNG file");
44    let mut family = IconFamily::new();
45    let icns_path = if num_args == 3 {
46        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48        family
49            .add_icon_with_type(&image, icon_type)
50            .expect("failed to encode image");
51        png_path.with_extension(format!("{}.icns", ostype))
52    } else {
53        family.add_icon(&image).expect("failed to encode image");
54        png_path.with_extension("icns")
55    };
56    let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57    family.write(icns_file).expect("failed to write ICNS file");
58}
Source

pub fn is_empty(&self) -> bool

Returns true if the icon family contains no icons nor any other elements.

Source

pub fn add_icon(&mut self, image: &Image) -> Result<()>

Encodes the image into the family, automatically choosing an appropriate icon type based on the dimensions of the image. Returns an error if there is no supported icon type matching the image dimensions.

Examples found in repository?
examples/png2icns.rs (line 53)
34fn main() {
35    let num_args = env::args().count();
36    if num_args < 2 || num_args > 3 {
37        println!("Usage: png2icns <path> [<ostype>]");
38        return;
39    }
40    let png_path = env::args().nth(1).unwrap();
41    let png_path = Path::new(&png_path);
42    let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43    let image = Image::read_png(png_file).expect("failed to read PNG file");
44    let mut family = IconFamily::new();
45    let icns_path = if num_args == 3 {
46        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48        family
49            .add_icon_with_type(&image, icon_type)
50            .expect("failed to encode image");
51        png_path.with_extension(format!("{}.icns", ostype))
52    } else {
53        family.add_icon(&image).expect("failed to encode image");
54        png_path.with_extension("icns")
55    };
56    let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57    family.write(icns_file).expect("failed to write ICNS file");
58}
Source

pub fn add_icon_with_type( &mut self, image: &Image, icon_type: IconType, ) -> Result<()>

Encodes the image into the family using the given icon type. If the selected type has an associated mask type, the image mask will also be added to the family. Returns an error if the image has the wrong dimensions for the selected type.

Examples found in repository?
examples/png2icns.rs (line 49)
34fn main() {
35    let num_args = env::args().count();
36    if num_args < 2 || num_args > 3 {
37        println!("Usage: png2icns <path> [<ostype>]");
38        return;
39    }
40    let png_path = env::args().nth(1).unwrap();
41    let png_path = Path::new(&png_path);
42    let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43    let image = Image::read_png(png_file).expect("failed to read PNG file");
44    let mut family = IconFamily::new();
45    let icns_path = if num_args == 3 {
46        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48        family
49            .add_icon_with_type(&image, icon_type)
50            .expect("failed to encode image");
51        png_path.with_extension(format!("{}.icns", ostype))
52    } else {
53        family.add_icon(&image).expect("failed to encode image");
54        png_path.with_extension("icns")
55    };
56    let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57    family.write(icns_file).expect("failed to write ICNS file");
58}
Source

pub fn available_icons(&self) -> Vec<IconType>

Returns a list of all (non-mask) icon types for which the icon family contains the necessary element(s) for a complete icon image (including alpha channel). These icon types can be passed to the get_icon_with_type method to decode the icons.

Examples found in repository?
examples/icns2png.rs (line 49)
31fn main() {
32    let num_args = env::args().count();
33    if num_args < 2 || num_args > 3 {
34        println!("Usage: icns2png <path> [<ostype>]");
35        return;
36    }
37    let icns_path = env::args().nth(1).unwrap();
38    let icns_path = Path::new(&icns_path);
39    let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
40    let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
41    let (icon_type, png_path) = if num_args == 3 {
42        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
43        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
44        let png_path = icns_path.with_extension(format!("{}.png", ostype));
45        (icon_type, png_path)
46    } else {
47        // If no OSType is specified, extract the highest-resolution icon.
48        let &icon_type = family
49            .available_icons()
50            .iter()
51            .max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
52            .expect("ICNS file contains no icons");
53        let png_path = icns_path.with_extension("png");
54        (icon_type, png_path)
55    };
56    let image = family
57        .get_icon_with_type(icon_type)
58        .expect("ICNS file does not contain that icon type");
59    let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
60    image.write_png(png_file).expect("failed to write PNG file");
61}
Source

pub fn has_icon_with_type(&self, icon_type: IconType) -> bool

Determines whether the icon family contains a complete icon with the given type (including the mask, if the given icon type has an associated mask type).

Source

pub fn get_icon_with_type(&self, icon_type: IconType) -> Result<Image>

Decodes an image from the family with the given icon type. If the selected type has an associated mask type, the two elements will decoded together into a single image. Returns an error if the element(s) for the selected type are not present in the icon family, or the if the encoded data is malformed.

Examples found in repository?
examples/icns2png.rs (line 57)
31fn main() {
32    let num_args = env::args().count();
33    if num_args < 2 || num_args > 3 {
34        println!("Usage: icns2png <path> [<ostype>]");
35        return;
36    }
37    let icns_path = env::args().nth(1).unwrap();
38    let icns_path = Path::new(&icns_path);
39    let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
40    let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
41    let (icon_type, png_path) = if num_args == 3 {
42        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
43        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
44        let png_path = icns_path.with_extension(format!("{}.png", ostype));
45        (icon_type, png_path)
46    } else {
47        // If no OSType is specified, extract the highest-resolution icon.
48        let &icon_type = family
49            .available_icons()
50            .iter()
51            .max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
52            .expect("ICNS file contains no icons");
53        let png_path = icns_path.with_extension("png");
54        (icon_type, png_path)
55    };
56    let image = family
57        .get_icon_with_type(icon_type)
58        .expect("ICNS file does not contain that icon type");
59    let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
60    image.write_png(png_file).expect("failed to write PNG file");
61}
Source

pub fn read<R: Read>(reader: R) -> Result<IconFamily>

Reads an icon family from an ICNS file.

Examples found in repository?
examples/readicns.rs (line 16)
8fn main() {
9    if env::args().count() != 2 {
10        println!("Usage: readicns <path>");
11        return;
12    }
13    let path = env::args().nth(1).unwrap();
14    let file = File::open(path).expect("failed to open file");
15    let buffered = BufReader::new(file);
16    let family = IconFamily::read(buffered).expect("failed to read ICNS file");
17    println!("ICNS file contains {} element(s).", family.elements.len());
18    for (index, element) in family.elements.iter().enumerate() {
19        println!(
20            "Element {}: {} ({} byte payload)",
21            index,
22            element.ostype,
23            element.data.len()
24        );
25    }
26}
More examples
Hide additional examples
examples/icns2png.rs (line 40)
31fn main() {
32    let num_args = env::args().count();
33    if num_args < 2 || num_args > 3 {
34        println!("Usage: icns2png <path> [<ostype>]");
35        return;
36    }
37    let icns_path = env::args().nth(1).unwrap();
38    let icns_path = Path::new(&icns_path);
39    let icns_file = BufReader::new(File::open(icns_path).expect("failed to open ICNS file"));
40    let family = IconFamily::read(icns_file).expect("failed to read ICNS file");
41    let (icon_type, png_path) = if num_args == 3 {
42        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
43        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
44        let png_path = icns_path.with_extension(format!("{}.png", ostype));
45        (icon_type, png_path)
46    } else {
47        // If no OSType is specified, extract the highest-resolution icon.
48        let &icon_type = family
49            .available_icons()
50            .iter()
51            .max_by_key(|icon_type| icon_type.pixel_width() * icon_type.pixel_height())
52            .expect("ICNS file contains no icons");
53        let png_path = icns_path.with_extension("png");
54        (icon_type, png_path)
55    };
56    let image = family
57        .get_icon_with_type(icon_type)
58        .expect("ICNS file does not contain that icon type");
59    let png_file = BufWriter::new(File::create(png_path).expect("failed to create PNG file"));
60    image.write_png(png_file).expect("failed to write PNG file");
61}
Source

pub fn write<W: Write>(&self, writer: W) -> Result<()>

Writes the icon family to an ICNS file.

Examples found in repository?
examples/png2icns.rs (line 57)
34fn main() {
35    let num_args = env::args().count();
36    if num_args < 2 || num_args > 3 {
37        println!("Usage: png2icns <path> [<ostype>]");
38        return;
39    }
40    let png_path = env::args().nth(1).unwrap();
41    let png_path = Path::new(&png_path);
42    let png_file = BufReader::new(File::open(png_path).expect("failed to open PNG file"));
43    let image = Image::read_png(png_file).expect("failed to read PNG file");
44    let mut family = IconFamily::new();
45    let icns_path = if num_args == 3 {
46        let ostype = OSType::from_str(&env::args().nth(2).unwrap()).unwrap();
47        let icon_type = IconType::from_ostype(ostype).expect("unsupported ostype");
48        family
49            .add_icon_with_type(&image, icon_type)
50            .expect("failed to encode image");
51        png_path.with_extension(format!("{}.icns", ostype))
52    } else {
53        family.add_icon(&image).expect("failed to encode image");
54        png_path.with_extension("icns")
55    };
56    let icns_file = BufWriter::new(File::create(icns_path).expect("failed to create ICNS file"));
57    family.write(icns_file).expect("failed to write ICNS file");
58}
Source

pub fn total_length(&self) -> u32

Returns the encoded length of the file, in bytes, including the length of the header.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.