Bug 1311244 Part 2 - Create ShapeUtils, and move EnumerationToLength into it. r=dbaron

More functions in nsCSSClipPathInstance will be refactored and moved into
ShapeUtils in subsequent patches.

MozReview-Commit-ID: LmJUevY8YGr

--HG--
extra : rebase_source : 7cbfe60fec65833db3c7b7d7e9f3157b49b777eb
This commit is contained in:
Ting-Yu Lin 2017-01-06 16:35:29 +08:00
parent 8cddb87ec2
commit e42b15fcf6
4 changed files with 85 additions and 25 deletions

View File

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ShapeUtils.h"
#include <cstdlib>
namespace mozilla {
nscoord
ShapeUtils::ComputeShapeRadius(const StyleShapeRadius aType,
const nscoord aCenter,
const nscoord aPosMin,
const nscoord aPosMax)
{
nscoord dist1 = std::abs(aPosMin - aCenter);
nscoord dist2 = std::abs(aPosMax - aCenter);
nscoord length = 0;
switch (aType) {
case StyleShapeRadius::FarthestSide:
length = dist1 > dist2 ? dist1 : dist2;
break;
case StyleShapeRadius::ClosestSide:
length = dist1 > dist2 ? dist2 : dist1;
break;
}
return length;
}
} // namespace mozilla

35
layout/base/ShapeUtils.h Normal file
View File

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_ShapeUtils_h
#define mozilla_ShapeUtils_h
#include "nsCoord.h"
#include "nsStyleConsts.h"
namespace mozilla {
// ShapeUtils is a namespace class containing utility functions related to
// processing basic shapes in the CSS Shapes Module.
// https://drafts.csswg.org/css-shapes/#basic-shape-functions
//
struct ShapeUtils final
{
// Compute the length of a keyword <shape-radius>, i.e. closest-side or
// farthest-side, for a circle or an ellipse on a single dimension. The
// caller needs to call for both dimensions and combine the result.
// https://drafts.csswg.org/css-shapes/#typedef-shape-radius.
//
// @return The length of the radius in app units.
static nscoord ComputeShapeRadius(const StyleShapeRadius aType,
const nscoord aCenter,
const nscoord aPosMin,
const nscoord aPosMax);
};
} // namespace mozilla
#endif // mozilla_ShapeUtils_h

View File

@ -80,6 +80,7 @@ EXPORTS.mozilla += [
'RestyleManagerHandleInlines.h',
'ServoRestyleManager.h',
'ServoRestyleManagerInlines.h',
'ShapeUtils.h',
'StaticPresData.h',
]
@ -114,6 +115,7 @@ UNIFIED_SOURCES += [
'RestyleTracker.cpp',
'ScrollbarStyles.cpp',
'ServoRestyleManager.cpp',
'ShapeUtils.cpp',
'StackArena.cpp',
'StaticPresData.cpp',
'TouchManager.cpp',

View File

@ -11,6 +11,7 @@
#include "mozilla/dom/SVGSVGElement.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/PathHelpers.h"
#include "mozilla/ShapeUtils.h"
#include "nsCSSRendering.h"
#include "nsIFrame.h"
#include "nsRenderingContext.h"
@ -104,22 +105,6 @@ nsCSSClipPathInstance::CreateClipPath(DrawTarget* aDrawTarget)
return builder->Finish();
}
static void
EnumerationToLength(nscoord& aCoord, StyleShapeRadius aType,
nscoord aCenter, nscoord aPosMin, nscoord aPosMax)
{
nscoord dist1 = abs(aPosMin - aCenter);
nscoord dist2 = abs(aPosMax - aCenter);
switch (aType) {
case StyleShapeRadius::FarthestSide:
aCoord = dist1 > dist2 ? dist1 : dist2;
break;
case StyleShapeRadius::ClosestSide:
aCoord = dist1 > dist2 ? dist2 : dist1;
break;
}
}
already_AddRefed<Path>
nsCSSClipPathInstance::CreateClipPathCircle(DrawTarget* aDrawTarget,
const nsRect& aRefBox)
@ -140,11 +125,12 @@ nsCSSClipPathInstance::CreateClipPathCircle(DrawTarget* aDrawTarget,
nscoord r = 0;
if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
const auto styleShapeRadius = coords[0].GetEnumValue<StyleShapeRadius>();
nscoord horizontal, vertical;
EnumerationToLength(horizontal, styleShapeRadius,
center.x, aRefBox.x, aRefBox.x + aRefBox.width);
EnumerationToLength(vertical, styleShapeRadius,
center.y, aRefBox.y, aRefBox.y + aRefBox.height);
nscoord horizontal =
ShapeUtils::ComputeShapeRadius(styleShapeRadius, center.x, aRefBox.x,
aRefBox.x + aRefBox.width);
nscoord vertical =
ShapeUtils::ComputeShapeRadius(styleShapeRadius, center.y, aRefBox.y,
aRefBox.y + aRefBox.height);
if (styleShapeRadius == StyleShapeRadius::FarthestSide) {
r = horizontal > vertical ? horizontal : vertical;
} else {
@ -188,14 +174,18 @@ nsCSSClipPathInstance::CreateClipPathEllipse(DrawTarget* aDrawTarget,
MOZ_ASSERT(coords.Length() == 2, "wrong number of arguments");
nscoord rx = 0, ry = 0;
if (coords[0].GetUnit() == eStyleUnit_Enumerated) {
EnumerationToLength(rx, coords[0].GetEnumValue<StyleShapeRadius>(),
center.x, aRefBox.x, aRefBox.x + aRefBox.width);
rx = ShapeUtils::ComputeShapeRadius(coords[0].GetEnumValue<StyleShapeRadius>(),
center.x,
aRefBox.x,
aRefBox.x + aRefBox.width);
} else {
rx = nsRuleNode::ComputeCoordPercentCalc(coords[0], aRefBox.width);
}
if (coords[1].GetUnit() == eStyleUnit_Enumerated) {
EnumerationToLength(ry, coords[1].GetEnumValue<StyleShapeRadius>(),
center.y, aRefBox.y, aRefBox.y + aRefBox.height);
ry = ShapeUtils::ComputeShapeRadius(coords[1].GetEnumValue<StyleShapeRadius>(),
center.y,
aRefBox.y,
aRefBox.y + aRefBox.height);
} else {
ry = nsRuleNode::ComputeCoordPercentCalc(coords[1], aRefBox.height);
}