Commit 5165cb2e authored by Fredrik Söderqvist's avatar Fredrik Söderqvist Committed by Commit Bot

Return Optional<float> from SVGSVGElement::Intrinsic{Width,Height}

This provides a more succinct way to express a potentially missing
value, and allows dropping the separate HasIntrinsic{Width,Height}.

For the usage in SVGSVGElement::CurrentViewBoxRect, just drop the use of
IntrinsicWidth/Height() since we resolve percentages against the current
viewport - which matches what a plain SVGLengthContext resolve does in
this case.

Change-Id: I0f6b36e4f34e3ef42adc374100d6a82647301c57
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2509853Reviewed-by: default avatarStephen Chenney <schenney@chromium.org>
Commit-Queue: Fredrik Söderquist <fs@opera.com>
Cr-Commit-Position: refs/heads/master@{#822661}
parent 52e99ca7
......@@ -62,12 +62,8 @@ LayoutSVGRoot::LayoutSVGRoot(SVGElement* node)
auto* svg = To<SVGSVGElement>(node);
DCHECK(svg);
LayoutSize intrinsic_size(svg->IntrinsicWidth(), svg->IntrinsicHeight());
if (!svg->HasIntrinsicWidth())
intrinsic_size.SetWidth(LayoutUnit(kDefaultWidth));
if (!svg->HasIntrinsicHeight())
intrinsic_size.SetHeight(LayoutUnit(kDefaultHeight));
SetIntrinsicSize(intrinsic_size);
SetIntrinsicSize(LayoutSize(svg->IntrinsicWidth().value_or(kDefaultWidth),
svg->IntrinsicHeight().value_or(kDefaultHeight)));
}
LayoutSVGRoot::~LayoutSVGRoot() = default;
......@@ -80,10 +76,12 @@ void LayoutSVGRoot::UnscaledIntrinsicSizingInfo(
auto* svg = To<SVGSVGElement>(GetNode());
DCHECK(svg);
base::Optional<float> intrinsic_width = svg->IntrinsicWidth();
base::Optional<float> intrinsic_height = svg->IntrinsicHeight();
intrinsic_sizing_info.size =
FloatSize(svg->IntrinsicWidth(), svg->IntrinsicHeight());
intrinsic_sizing_info.has_width = svg->HasIntrinsicWidth();
intrinsic_sizing_info.has_height = svg->HasIntrinsicHeight();
FloatSize(intrinsic_width.value_or(0), intrinsic_height.value_or(0));
intrinsic_sizing_info.has_width = intrinsic_width.has_value();
intrinsic_sizing_info.has_height = intrinsic_height.has_value();
if (!intrinsic_sizing_info.size.IsEmpty()) {
intrinsic_sizing_info.aspect_ratio = intrinsic_sizing_info.size;
......
......@@ -593,15 +593,10 @@ FloatRect SVGSVGElement::CurrentViewBoxRect() const {
// If no viewBox is specified but non-relative width/height values, then we
// should always synthesize a viewBox if we're embedded through a SVGImage.
FloatSize synthesized_view_box_size(IntrinsicWidth(), IntrinsicHeight());
if (!HasIntrinsicWidth())
synthesized_view_box_size.SetWidth(
width()->CurrentValue()->ScaleByPercentage(
CurrentViewportSize().Width()));
if (!HasIntrinsicHeight())
synthesized_view_box_size.SetHeight(
height()->CurrentValue()->ScaleByPercentage(
CurrentViewportSize().Height()));
SVGLengthContext length_context(this);
FloatSize synthesized_view_box_size(
width()->CurrentValue()->Value(length_context),
height()->CurrentValue()->Value(length_context));
return FloatRect(FloatPoint(), synthesized_view_box_size);
}
......@@ -637,34 +632,26 @@ FloatSize SVGSVGElement::CurrentViewportSize() const {
return viewport_rect.Size();
}
bool SVGSVGElement::HasIntrinsicWidth() const {
base::Optional<float> SVGSVGElement::IntrinsicWidth() const {
const SVGLength& width_attr = *width()->CurrentValue();
// TODO(crbug.com/979895): This is the result of a refactoring, which might
// have revealed an existing bug that we are not handling math functions
// involving percentages correctly. Fix it if necessary.
return !width()->CurrentValue()->IsPercentage();
if (width_attr.IsPercentage())
return base::nullopt;
SVGLengthContext length_context(this);
return width_attr.Value(length_context);
}
bool SVGSVGElement::HasIntrinsicHeight() const {
base::Optional<float> SVGSVGElement::IntrinsicHeight() const {
const SVGLength& height_attr = *height()->CurrentValue();
// TODO(crbug.com/979895): This is the result of a refactoring, which might
// have revealed an existing bug that we are not handling math functions
// involving percentages correctly. Fix it if necessary.
return !height()->CurrentValue()->IsPercentage();
}
float SVGSVGElement::IntrinsicWidth() const {
if (!HasIntrinsicWidth())
return 0;
SVGLengthContext length_context(this);
return width()->CurrentValue()->Value(length_context);
}
float SVGSVGElement::IntrinsicHeight() const {
if (!HasIntrinsicHeight())
return 0;
if (height_attr.IsPercentage())
return base::nullopt;
SVGLengthContext length_context(this);
return height()->CurrentValue()->Value(length_context);
return height_attr.Value(length_context);
}
AffineTransform SVGSVGElement::ViewBoxToViewTransform(
......
......@@ -48,8 +48,8 @@ class SVGSVGElement final : public SVGGraphicsElement,
public:
explicit SVGSVGElement(Document&);
float IntrinsicWidth() const;
float IntrinsicHeight() const;
base::Optional<float> IntrinsicWidth() const;
base::Optional<float> IntrinsicHeight() const;
FloatSize CurrentViewportSize() const;
FloatRect CurrentViewBoxRect() const;
const SVGPreserveAspectRatio* CurrentPreserveAspectRatio() const;
......@@ -99,9 +99,6 @@ class SVGSVGElement final : public SVGGraphicsElement,
Element* anchor_node);
bool ZoomAndPanEnabled() const;
bool HasIntrinsicWidth() const;
bool HasIntrinsicHeight() const;
SVGAnimatedLength* x() const { return x_.Get(); }
SVGAnimatedLength* y() const { return y_.Get(); }
SVGAnimatedLength* width() const { return width_.Get(); }
......
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