Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add the column layout type #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ Catwalk makes it easy to generate [beautiful preview images](https://raw.githubu

You can install Catwalk using one of the methods below:

| Installation Method | Instructions |
| ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| crates.io | `cargo install catppuccin-catwalk` |
| Source | `cargo install --git https://github.com/catppuccin/catwalk catppuccin-catwalk` |
| Homebrew | `brew install catppuccin/tap/catwalk` |
| Installation Method | Instructions |
| ------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| crates.io | `cargo install catppuccin-catwalk` |
| Source | `cargo install --git https://github.com/catppuccin/catwalk catppuccin-catwalk` |
| Homebrew | `brew install catppuccin/tap/catwalk` |
| Nix | `nix profile install github:catppuccin/catwalk` <br/> `nix run github:catppuccin/catwalk -- [OPTIONS] <images>` |
| Binaries<br/>(Windows, MacOS & Linux) | Available from the [latest GitHub release](https://github.com/catppuccin/catwalk/releases). |
| Binaries<br/>(Windows, MacOS & Linux) | Available from the [latest GitHub release](https://github.com/catppuccin/catwalk/releases). |

## Usage

Expand All @@ -53,7 +53,7 @@ Arguments:

Options:
-o, --output <OUTPUT> Path to output file [default: ./preview.webp]
-l, --layout <LAYOUT> Layout to use [default: composite] [possible values: composite, stacked, grid, row]
-l, --layout <LAYOUT> Layout to use [default: composite] [possible values: composite, stacked, grid, row, column]
-r, --radius <RADIUS> Radius of rounded corners (percentage)
-g, --gap <GAP> Size of gaps between pictures for the `grid` layout
-C, --directory <DIRECTORY> Change to <DIRECTORY> before processing files [default: .]
Expand All @@ -65,11 +65,11 @@ Options:
### Examples

```console
$ catwalk latte.webp frappe.webp macchiato.webp mocha.webp --output catwalk.webp
catwalk latte.webp frappe.webp macchiato.webp mocha.webp --output catwalk.webp
```

```console
$ catwalk latte.png frappe.png macchiato.png mocha.png --directory ./assets/
catwalk latte.png frappe.png macchiato.png mocha.png --directory ./assets/
```

&nbsp;
Expand Down
32 changes: 20 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ pub enum Layout {
Stacked,
Grid,
Row,
Column,
}

enum GridLayouts {
Grid,
Row,
Column,
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -229,14 +231,10 @@ impl Magic {
fn gen_grid(&self, layout: &GridLayouts) -> Image<Rgba> {
// Round images
let gap = self.gap;
let rounded: Vec<Image<Rgba>> = self
.images
.iter()
.map(|x| self.rounding_mask.mask(x))
.collect();
let rounded = self.images.iter().map(|x| self.rounding_mask.mask(x));

// Create final
let mut result = match layout {
// Create a blank image canvas to place each image into.
let mut canvas = match layout {
GridLayouts::Grid => Image::new(
(self.width * 2) + (gap * 3),
(self.height * 2) + (gap * 3),
Expand All @@ -247,25 +245,34 @@ impl Magic {
self.height + (gap * 2),
Rgba::transparent(),
),
GridLayouts::Column => Image::new(
(self.width) + gap,
(self.height * 4) + (gap * 5),
Rgba::transparent(),
),
}
.with_overlay_mode(OverlayMode::Merge);
// calculate the top left coordinates for each image, and paste
rounded.iter().enumerate().for_each(|(i, img)| {

// calculate the top left coordinates for each image, and copy
// their image data into the canvas.
rounded.enumerate().for_each(|(i, img)| {
let x = match layout {
GridLayouts::Row => i % 4,
GridLayouts::Grid => i % 2,
GridLayouts::Column => 0,
};
let y = match layout {
GridLayouts::Row => 0,
GridLayouts::Grid => i / 2,
GridLayouts::Column => i % 4,
};
result.paste(
canvas.paste(
gap + (self.width + gap) * x as u32,
gap + (self.height + gap) * y as u32,
img,
&img,
);
});
result
canvas
}
/// Generates a mask for the given image.
fn gen_mask(w: f32, index: usize, aa_level: u32, inverse_slope: f32) -> TrapMask {
Expand All @@ -288,6 +295,7 @@ impl Magic {
Layout::Stacked => self.gen_stacked(),
Layout::Grid => self.gen_grid(&GridLayouts::Grid),
Layout::Row => self.gen_grid(&GridLayouts::Row),
Layout::Column => self.gen_grid(&GridLayouts::Column),
}
}

Expand Down