Commit eb69d642 authored by davve@opera.com's avatar davve@opera.com

Teach SVGImage::containerSize() to deduce intrinsic values

When an SVG has either intrinsic width or height but not both, and a
intrinsic ratio, the missing intrinsic width or height can be
calculated. Doing so enables us to return correct intrinsic width and
height for SVG images in more cases when the image has no renderer.

Also, this patch removes the unspecified use of the viewBox attribute
as the source of intrinsic size. A small number of tests depended on
this and has been updated. The reasoning to make this change is
because we don't use the viewBox for intrinsic size in any other
context than SVGImage. And we only use it until the renderer has a
containerSize() anyway.

http://www.whatwg.org/specs/web-apps/current-work/multipage/edits.html#dom-img-width
http://www.whatwg.org/specs/web-apps/current-work/multipage/edits.html#dom-img-height

BUG=376895

Review URL: https://codereview.chromium.org/308643002

git-svn-id: svn://svn.chromium.org/blink/trunk@175784 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 1f2c5b56
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
margin: 5px; margin: 5px;
width: 130px; width: 130px;
height: 130px; height: 130px;
background: url('resources/ducky.png') no-repeat 0 0 /100% 100%, url('resources/blue-circle.svg'); background: url('resources/ducky.png') no-repeat 0 0 /100% 100%, url('resources/blue-circle.svg') 0 0 / 100% 100%;
float: left; float: left;
} }
</style> </style>
......
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
div { div {
width: 130px; width: 130px;
height: 130px; height: 130px;
background-size: 130px 130px;
background: url('resources/blue-circle.svg'), green; background: url('resources/blue-circle.svg'), green;
background-size: 130px 130px;
float: left; float: left;
margin: 5px; margin: 5px;
} }
......
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100"> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="blue"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="blue">
</circle> </circle>
</svg> </svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100"> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100" viewBox="0 0 100 100">
<defs> <defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="0%" y2="100%"> <linearGradient id="grad" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,0,0);" /> <stop offset="0%" style="stop-color:rgb(255,0,0);" />
...@@ -8,4 +8,4 @@ ...@@ -8,4 +8,4 @@
</linearGradient> </linearGradient>
</defs> </defs>
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="url(#grad)" /> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="url(#grad)" />
</svg> </svg>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"> <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 100 100">
<rect fill="#ff0000" x="0" y="0" width="100" height="50"/> <rect fill="#ff0000" x="0" y="0" width="100" height="50"/>
<rect fill="#00ff00" x="0" y="50" width="100" height="50"/> <rect fill="#00ff00" x="0" y="50" width="100" height="50"/>
</svg> </svg>
\ No newline at end of file
<?xml version="1.0"?> <?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100" viewBox="0 0 100 100">
<style> <style>
a rect { a rect {
fill: green; fill: green;
......
<?xml version="1.0"?> <?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100" viewBox="0 0 100 100">
<style>a rect { fill: green } a:link rect, a:visited rect { fill: red }</style> <style>a rect { fill: green } a:link rect, a:visited rect { fill: red }</style>
<a xlink:href="http://www.example.com/"> <a xlink:href="http://www.example.com/">
<rect x="0" y="0" width="100" height="100"/> <rect x="0" y="0" width="100" height="100"/>
......
<!DOCTYPE html>
<title>Test that intrinsic height can be resolved</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='300' viewBox='0 180 150 220' style='background:blue'></svg>">
<img src="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' height='440' viewBox='0 180 150 220' style='background:purple'></svg>">
<script>
var imgs = document.getElementsByTagName('img');
function waitForComplete(img, i) {
if (img.complete) {
assert_equals(img.width, 300);
assert_equals(img.height, 440);
this.done();
} else {
setTimeout(t.step_func(waitForComplete.bind(this, img, i + 1)), 1);
}
}
var t1 = async_test("Test that intrinsic height can be calculated from intrinsic width and intrinsic ratio");
t1.step(waitForComplete.bind(t1, imgs[0], 0));
var t2 = async_test("Test that intrinsic width can be calculated from intrinsic height and intrinsic ratio");
t2.step(waitForComplete.bind(t2, imgs[1], 0));
</script>
...@@ -162,16 +162,21 @@ IntSize SVGImage::containerSize() const ...@@ -162,16 +162,21 @@ IntSize SVGImage::containerSize() const
// Assure that a container size is always given for a non-identity zoom level. // Assure that a container size is always given for a non-identity zoom level.
ASSERT(renderer->style()->effectiveZoom() == 1); ASSERT(renderer->style()->effectiveZoom() == 1);
FloatSize currentSize; FloatSize intrinsicSize;
if (rootElement->hasIntrinsicWidth() && rootElement->hasIntrinsicHeight()) double intrinsicRatio = 0;
currentSize = rootElement->currentViewportSize(); renderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
else
currentSize = rootElement->currentViewBoxRect().size(); if (intrinsicSize.isEmpty() && intrinsicRatio) {
if (!intrinsicSize.width() && intrinsicSize.height())
intrinsicSize.setWidth(intrinsicSize.height() * intrinsicRatio);
else if (intrinsicSize.width() && !intrinsicSize.height())
intrinsicSize.setHeight(intrinsicSize.width() / intrinsicRatio);
}
if (!currentSize.isEmpty()) if (!intrinsicSize.isEmpty())
return IntSize(static_cast<int>(ceilf(currentSize.width())), static_cast<int>(ceilf(currentSize.height()))); return expandedIntSize(intrinsicSize);
// As last resort, use CSS default intrinsic size. // As last resort, use CSS replaced element fallback size.
return IntSize(300, 150); return IntSize(300, 150);
} }
......
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