From 0d5773cb7f1748b5cf2f338ac41c1a9b8f99b664 Mon Sep 17 00:00:00 2001 From: Manuel Reimer Date: Sun, 9 Nov 2014 10:57:12 +0100 Subject: [PATCH] Center scaled image in the available target space when preserving aspect --- libcore/imageloader.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libcore/imageloader.c b/libcore/imageloader.c index a0247ac..d3f02fe 100644 --- a/libcore/imageloader.c +++ b/libcore/imageloader.c @@ -33,10 +33,17 @@ cImage *cImageLoader::CreateImage(int width, int height, bool preserveAspect) { double sx = width / (double)w; double sy = height / (double)h; if (preserveAspect) { - if (sx < sy) + double tx = 0; + double ty = 0; + if (sx < sy) { sy = sx; - if (sy < sx) + ty = (height - h * sy) / 2; + } + if (sy < sx) { sx = sy; + tx = (width - w * sx) / 2; + } + cairo_translate(cr, tx, ty); } cairo_scale(cr, sx, sy);