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

Add support for fonts with different aspect ratio #65

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions completion/_catimg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _arguments \
'-r[force the resolution of the image]: :->resolution' \
'-t[disable true color and use 256 color instead]' \
'-w[specify the width of the displayed image]' \
'-a[specify the aspect ratio multiplier of the font]' \
'*: :_files' && ret=0

[[ "$state" == 'resolution' ]] && _values 'resolution value' 1 2 && ret=0
Expand Down
31 changes: 22 additions & 9 deletions src/catimg.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"looping\n" \
" -r: Resolution must be 1 or 2. By default catimg checks for unicode support to " \
"use higher resolution\n" \
" -a: Font aspect ratio multiplier. For a condensed font, use values greater than " \
"1.0, for wider fonts, less than 1.0\n" \
" -c: Convert colors to a restricted palette\n" \
" -t: Disables true color (24-bit) support, falling back to 256 color\n"

Expand Down Expand Up @@ -76,9 +78,10 @@ int main(int argc, char *argv[])
uint8_t convert = 0;
uint8_t true_color = 1;
uint8_t adjust_to_height = 0, adjust_to_width = 0;
float scale_cols = 0, scale_rows = 0;
float scale_cols = 1.0, scale_rows = 1.0;
float aspect = 1.0;

while ((c = getopt (argc, argv, "H:w:l:r:hct")) != -1)
while ((c = getopt (argc, argv, "H:w:l:r:a:hct")) != -1)
switch (c) {
case 'H':
rows = strtol(optarg, &num, 0);
Expand Down Expand Up @@ -106,6 +109,9 @@ int main(int argc, char *argv[])
case 'r':
precision = strtol(optarg, &num, 0);
break;
case 'a':
aspect = strtof(optarg, &num);
break;
case 'h':
printf(USAGE);
exit(0);
Expand Down Expand Up @@ -150,17 +156,24 @@ int main(int argc, char *argv[])
scale_rows = max_rows / (float)img.height;
if (adjust_to_height && scale_rows < scale_cols && max_rows < img.height)
// rows == 0 and adjust_to_height > adjust to height instead of width
img_resize(&img, scale_rows, scale_rows);
scale_cols = scale_rows;
else if (max_cols < img.width)
img_resize(&img, scale_cols, scale_cols);
scale_rows = scale_cols;
else scale_rows = scale_cols = 1.0;
} else if (cols > 0 && cols < img.width) {
scale_cols = cols / (float)img.width;
img_resize(&img, scale_cols, scale_cols);
} else if (rows > 0 && rows < img.height) {
scale_rows = rows / (float)img.height;
img_resize(&img, scale_rows, scale_rows);
scale_cols = scale_rows = cols / (float)img.width;
} else if (rows > 0 && rows < img.height) {
scale_cols = scale_rows = rows / (float)img.height;
}

if (aspect > 1.0) {
scale_rows /= aspect;
} else {
scale_cols *= aspect;
}

img_resize(&img, scale_cols, scale_rows);

if (convert)
img_convert_colors(&img);
/*printf("Loaded %s: %ux%u. Console width: %u\n", file, img.width, img.height, cols);*/
Expand Down