Skip to content

Commit

Permalink
Use the aspect ratio of the content box
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rodarima committed Oct 7, 2024
1 parent 08c617e commit 52f39c4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
21 changes: 19 additions & 2 deletions dw/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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)",
Expand Down Expand Up @@ -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 =
Expand All @@ -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 ();
Expand All @@ -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 ();
}

Expand Down
27 changes: 23 additions & 4 deletions dw/widget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -1998,19 +2008,23 @@ 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) {
/* W is too big compared to H. Try to decrease W or increase H. */
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) {
Expand All @@ -2019,16 +2033,21 @@ 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 {
/* Current ratio is the preferred one. */
}
}

DEBUG_MSG(1, "Widget::correctReqAspectRatio() -- output: wReq=%d, hReq=%d, changed=%d\n",
requisition->width, requisition->ascent + requisition->descent, changed);

return changed;
}

Expand Down
4 changes: 2 additions & 2 deletions test/html/render/img-aspect-ratio-absolute.ref.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
min-width: 16px;
}
img {
width:755px;
height:755px;
width:749px;
height:749px;
}
</style>
</head>
Expand Down

0 comments on commit 52f39c4

Please sign in to comment.