Commit 80441f0a authored by Stephen Chenney's avatar Stephen Chenney Committed by Commit Bot

Fix the sizing when a srcset is used with scale-down

The object-fit: scale-down property requires that the image be
sized to the smaller of "none" or "contain". When a srcset is used,
the IntrinsicSize of the image is scaled such that the srcset fills
the expected area. This causes scale-down to consider the intrinsic
size to be bigger than the actual image, and size things too large.

This change modifies the intrinsic size for content-fit: scale-down
in LayoutReplaced::ComputeObjectFit to un-apply the
ImageDevicePixelRatio, that is the scale that resized the src.

Chrome's behavior now matches Firefox.

BUG=818630

Change-Id: Ifbd63bbd6d905673e8058b49173b604cd9c8083e
Reviewed-on: https://chromium-review.googlesource.com/957131
Commit-Queue: Stephen Chenney <schenney@chromium.org>
Reviewed-by: default avatarPhilip Rogers <pdr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543082}
parent 8bb4213d
<!doctype html>
<style>
img {
object-fit: scale-down;
object-position: 0 0;
}
div {
width: 1000px;
height: 1000px;
background-color: blue;
}
</style>
<div>
<img src="resources/green-400-px-square.png">
</img>
</div>
<!doctype html>
<style>
img {
object-fit: scale-down;
object-position: 0 0;
}
div {
width: 1000px;
height: 1000px;
background-color: blue;
}
</style>
<div>
<img src="resources/blue-100-px-square.png"
srcset="resources/blue-100-px-square.png 100w,
resources/green-400-px-square.png 400w">
</img>
</div>
......@@ -156,7 +156,7 @@ void LayoutReplaced::ComputeIntrinsicSizingInfoForReplacedContent(
// Handle zoom & vertical writing modes here, as the embedded document
// doesn't know about them.
intrinsic_sizing_info.size.Scale(Style()->EffectiveZoom());
if (IsLayoutImage())
if (IsLayoutImage() && Style()->GetObjectFit() != EObjectFit::kScaleDown)
intrinsic_sizing_info.size.Scale(
ToLayoutImage(this)->ImageDevicePixelRatio());
......@@ -605,21 +605,30 @@ LayoutRect LayoutReplaced::ComputeObjectFit(
if (!intrinsic_size.Width() || !intrinsic_size.Height())
return content_rect;
LayoutSize scaled_intrinsic_size = intrinsic_size;
LayoutRect final_rect = content_rect;
switch (object_fit) {
case EObjectFit::kContain:
case EObjectFit::kScaleDown:
// Srcset images have an intrinsic size depending on their destination,
// but with object-fit: scale-down they need to use the underlying image
// src's size. So revert back to the original size in that case.
if (IsLayoutImage()) {
scaled_intrinsic_size.Scale(
1.0 / ToLayoutImage(this)->ImageDevicePixelRatio());
}
FALLTHROUGH;
case EObjectFit::kContain:
case EObjectFit::kCover:
final_rect.SetSize(final_rect.Size().FitToAspectRatio(
intrinsic_size, object_fit == EObjectFit::kCover
? kAspectRatioFitGrow
: kAspectRatioFitShrink));
scaled_intrinsic_size, object_fit == EObjectFit::kCover
? kAspectRatioFitGrow
: kAspectRatioFitShrink));
if (object_fit != EObjectFit::kScaleDown ||
final_rect.Width() <= intrinsic_size.Width())
final_rect.Width() <= scaled_intrinsic_size.Width())
break;
FALLTHROUGH;
case EObjectFit::kNone:
final_rect.SetSize(intrinsic_size);
final_rect.SetSize(scaled_intrinsic_size);
break;
case EObjectFit::kFill:
break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment