Skip to content

Commit

Permalink
math: Make sure image extends to an edge when scaling
Browse files Browse the repository at this point in the history
Also make sure dimensions don't fall short of request. We do this by
always rounding up before capping the size with MIN().

Fixes #212 (GitHub).
  • Loading branch information
hpjansson committed Sep 3, 2024
1 parent 3422917 commit 32df6a4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 5 additions & 4 deletions chafa/chafa-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "config.h"

#include <math.h>
#include "chafa.h"
#include "internal/chafa-private.h"

Expand Down Expand Up @@ -124,19 +125,19 @@ chafa_calc_canvas_geometry (gint src_width,

if (dest_width < 1)
{
dest_width = dest_height * (src_aspect / font_ratio) + 0.5;
dest_width = ceil (dest_height * (src_aspect / font_ratio));
}
else if (dest_height < 1)
{
dest_height = (dest_width / src_aspect) * font_ratio + 0.5;
dest_height = ceil ((dest_width / src_aspect) * font_ratio);
}
else if (src_aspect > dest_aspect)
{
dest_height = dest_width * (font_ratio / src_aspect) + 0.5;
dest_height = ceil (dest_width * (font_ratio / src_aspect));
}
else
{
dest_width = dest_height * (src_aspect / font_ratio) + 0.5;
dest_width = ceil (dest_height * (src_aspect / font_ratio));
}
}

Expand Down
7 changes: 5 additions & 2 deletions chafa/internal/chafa-math-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* along with Chafa. If not, see <http://www.gnu.org/licenses/>. */

#include "config.h"

#include <math.h>
#include "internal/chafa-private.h"
#include "internal/chafa-math-util.h"

Expand Down Expand Up @@ -71,6 +73,7 @@ chafa_tuck_and_align (gint src_width, gint src_height,
case CHAFA_TUCK_SHRINK_TO_FIT:
if (src_width <= dest_width && src_height <= dest_height)
{
/* Image fits entirely in dest. Do alignment only, no scaling. */
*width_out = src_width;
*height_out = src_height;
break;
Expand All @@ -82,8 +85,8 @@ chafa_tuck_and_align (gint src_width, gint src_height,
ratio [0] = (gfloat) dest_width / (gfloat) src_width;
ratio [1] = (gfloat) dest_height / (gfloat) src_height;

*width_out = src_width * MIN (ratio [0], ratio [1]);
*height_out = src_height * MIN (ratio [0], ratio [1]);
*width_out = ceilf (src_width * MIN (ratio [0], ratio [1]));
*height_out = ceilf (src_height * MIN (ratio [0], ratio [1]));
break;

default:
Expand Down

0 comments on commit 32df6a4

Please sign in to comment.