From 52f39c4a2f25b5b8a5e80cac9889b90ebb409a22 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Mon, 7 Oct 2024 22:34:33 +0200 Subject: [PATCH] Use the aspect ratio of the content box The requisition box includes the margin, padding and border. To compute the aspect ratio of the image we need to remove them to get the content box. Once the adjustment is done, we add the borders again. --- dw/image.cc | 21 +++++++++++++-- dw/widget.cc | 27 ++++++++++++++++--- .../render/img-aspect-ratio-absolute.ref.html | 4 +-- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/dw/image.cc b/dw/image.cc index ba3348de..79331afb 100644 --- a/dw/image.cc +++ b/dw/image.cc @@ -181,6 +181,9 @@ void Image::sizeRequestSimpl (core::Requisition *requisition) DEBUG_MSG(1, "-- Image::sizeRequestSimpl() begins\n"); + DEBUG_MSG(1, "Image::sizeRequestImpl border: w=%d h=%d\n", + boxDiffWidth(), boxDiffHeight()); + /* First set a naive size based on the image properties if given */ if (buffer) { @@ -207,7 +210,7 @@ void Image::sizeRequestSimpl (core::Requisition *requisition) requisition->ascent += boxOffsetY (); requisition->descent += boxRestHeight (); - DEBUG_MSG(1, "initial requisition: w=%d, h=%d\n", + DEBUG_MSG(1, "Image: initial requisition (with border): w=%d, h=%d\n", requisition->width, requisition->ascent + requisition->descent); /* Then correct the size if needed, so it fits within the available space in @@ -217,7 +220,7 @@ void Image::sizeRequestSimpl (core::Requisition *requisition) correctRequisition (requisition, core::splitHeightPreserveDescent, true, true); - DEBUG_MSG(1, "corrected requisition: w=%d, h=%d\n", + DEBUG_MSG(1, "Image: corrected requisition: w=%d, h=%d\n", requisition->width, requisition->ascent + requisition->descent); DBG_OBJ_MSGF ("resize", 1, "=> %d * (%d + %d)", @@ -264,6 +267,13 @@ void Image::sizeAllocateImpl (core::Allocation *allocation) allocation->x, allocation->y, allocation->width, allocation->ascent, allocation->descent); + DEBUG_MSG(1, "Image::sizeAllocateImpl x=%d y=%d w=%d h=(%d + %d)\n", + allocation->x, allocation->y, allocation->width, + allocation->ascent, allocation->descent); + + DEBUG_MSG(1, "Image::sizeAllocateImpl border: w=%d h=%d\n", + boxDiffWidth(), boxDiffHeight()); + int newBufWidth = allocation->width - boxDiffWidth (); int newBufHeight = @@ -274,6 +284,9 @@ void Image::sizeAllocateImpl (core::Allocation *allocation) (newBufWidth != bufWidth || newBufHeight != bufHeight)) { DBG_OBJ_MSG ("resize", 1, "replacing buffer"); + DEBUG_MSG(1, "Image::sizeAllocateImpl new buffer size: w=%d h=%d\n", + newBufWidth, newBufHeight); + core::Imgbuf *oldBuffer = buffer; buffer = oldBuffer->getScaledBuf (newBufWidth, newBufHeight); oldBuffer->unref (); @@ -286,6 +299,10 @@ void Image::sizeAllocateImpl (core::Allocation *allocation) DBG_OBJ_SET_NUM ("bufHeight", bufHeight); } + DEBUG_MSG(1, "Image::sizeAllocateImpl x=%d y=%d w=%d h=(%d + %d)\n", + allocation->x, allocation->y, allocation->width, + allocation->ascent, allocation->descent); + DBG_OBJ_LEAVE (); } diff --git a/dw/widget.cc b/dw/widget.cc index eaa9e985..9a7f2b25 100644 --- a/dw/widget.cc +++ b/dw/widget.cc @@ -1972,6 +1972,14 @@ bool Widget::correctReqAspectRatio (int pass, Widget *child, Requisition *requis DEBUG_MSG(1, "Widget::correctReqAspectRatio() -- wReq=%d, hReq=%d, pass=%d\n", wReq, hReq, pass); + + DEBUG_MSG(1, "Widget::correctReqAspectRatio() -- border: w=%d, h=%d\n", + child->boxDiffWidth(), child->boxDiffHeight()); + + wReq -= child->boxDiffWidth(); + hReq -= child->boxDiffHeight(); + DEBUG_MSG(1, "Widget::correctReqAspectRatio() -- with border: wReq=%d, hReq=%d\n", + wReq, hReq); DEBUG_MSG(1, "child=%s, preferred ratio=%f\n", child->getClassName(), ratio); if (pass != PASS_KEEP && ratio > 0.0 && sizeSet) { @@ -1987,9 +1995,11 @@ bool Widget::correctReqAspectRatio (int pass, Widget *child, Requisition *requis if (pass == PASS_INCREASE) { /* Increase w */ int w = (float) hReq * ratio; + DEBUG_MSG(1, "increase w: %d -> %d\n", wReq, w); + w += child->boxDiffWidth(); + DEBUG_MSG(1, "increase w (with border): %d -> %d\n", wReq, w); requisition->width = w; changed = true; - DEBUG_MSG(1, "increase w: %d -> %d\n", wReq, w); } else if (pass == PASS_DECREASE) { /* Decrease h */ if (allowDecreaseHeight) { @@ -1998,9 +2008,11 @@ bool Widget::correctReqAspectRatio (int pass, Widget *child, Requisition *requis * reduce the corrected hight above the original height, without * making the requisition height smaller. */ int h = (float) wReq / ratio; + DEBUG_MSG(1, "decrease h: %d -> %d\n", hReq, h); + h += child->boxDiffHeight(); + DEBUG_MSG(1, "decrease h (with border): %d -> %d\n", hReq, h); splitHeightFun (h, &requisition->ascent, &requisition->descent); changed = true; - DEBUG_MSG(1, "decrease h: %d -> %d\n", hReq, h); } } } else if (curRatio > ratio) { @@ -2008,9 +2020,11 @@ bool Widget::correctReqAspectRatio (int pass, Widget *child, Requisition *requis if (pass == PASS_INCREASE) { /* Increase h */ int h = (float) wReq / ratio; + DEBUG_MSG(1, "increase h: %d -> %d\n", hReq, h); + h += child->boxDiffHeight(); + DEBUG_MSG(1, "increase h (width border): %d -> %d\n", hReq, h); splitHeightFun (h, &requisition->ascent, &requisition->descent); changed = true; - DEBUG_MSG(1, "increase h: %d -> %d\n", hReq, h); } else if (pass == PASS_DECREASE) { /* Decrease w */ if (allowDecreaseWidth) { @@ -2019,9 +2033,11 @@ bool Widget::correctReqAspectRatio (int pass, Widget *child, Requisition *requis * reduce the corrected width above the original width, without * making the requisition width smaller. */ int w = (float) hReq * ratio; + DEBUG_MSG(1, "decrease w: %d -> %d\n", wReq, w); + w += child->boxDiffWidth(); + DEBUG_MSG(1, "decrease w (width border): %d -> %d\n", wReq, w); requisition->width = w; changed = true; - DEBUG_MSG(1, "decrease w: %d -> %d\n", wReq, w); } } } else { @@ -2029,6 +2045,9 @@ bool Widget::correctReqAspectRatio (int pass, Widget *child, Requisition *requis } } + DEBUG_MSG(1, "Widget::correctReqAspectRatio() -- output: wReq=%d, hReq=%d, changed=%d\n", + requisition->width, requisition->ascent + requisition->descent, changed); + return changed; } diff --git a/test/html/render/img-aspect-ratio-absolute.ref.html b/test/html/render/img-aspect-ratio-absolute.ref.html index 84ad695e..015bdc77 100644 --- a/test/html/render/img-aspect-ratio-absolute.ref.html +++ b/test/html/render/img-aspect-ratio-absolute.ref.html @@ -7,8 +7,8 @@ min-width: 16px; } img { - width:755px; - height:755px; + width:749px; + height:749px; }