Bug 718266 - Simplify dasharray code. r=jwatt

This commit is contained in:
Robert Longson 2012-01-16 09:19:24 +00:00
parent 23660a956c
commit 85497421d0
2 changed files with 39 additions and 66 deletions

View File

@ -100,22 +100,14 @@ nsSVGGeometryFrame::GetStrokeWidth()
GetStyleSVG()->mStrokeWidth);
}
nsresult
nsSVGGeometryFrame::GetStrokeDashArray(gfxFloat **aDashes, PRUint32 *aCount)
bool
nsSVGGeometryFrame::GetStrokeDashData(FallibleTArray<gfxFloat>& dashes,
gfxFloat *dashOffset)
{
nsSVGElement *ctx = static_cast<nsSVGElement*>
(mContent->IsNodeOfType(nsINode::eTEXT) ?
mContent->GetParent() : mContent);
*aDashes = nsnull;
*aCount = 0;
PRUint32 count = GetStyleSVG()->mStrokeDasharrayLength;
gfxFloat *dashes = nsnull;
if (count) {
const nsStyleCoord *dasharray = GetStyleSVG()->mStrokeDasharray;
nsPresContext *presContext = PresContext();
gfxFloat totalLength = 0.0f;
if (!count || !dashes.SetLength(count)) {
return false;
}
gfxFloat pathScale = 1.0;
@ -123,50 +115,34 @@ nsSVGGeometryFrame::GetStrokeDashArray(gfxFloat **aDashes, PRUint32 *aCount)
pathScale = static_cast<nsSVGPathElement*>(mContent)->
GetPathLengthScale(nsSVGPathElement::eForStroking);
if (pathScale <= 0) {
return NS_OK;
return false;
}
}
dashes = new gfxFloat[count];
if (dashes) {
nsSVGElement *ctx = static_cast<nsSVGElement*>
(mContent->IsNodeOfType(nsINode::eTEXT) ?
mContent->GetParent() : mContent);
const nsStyleCoord *dasharray = GetStyleSVG()->mStrokeDasharray;
nsPresContext *presContext = PresContext();
gfxFloat totalLength = 0.0;
for (PRUint32 i = 0; i < count; i++) {
dashes[i] =
nsSVGUtils::CoordToFloat(presContext,
ctx,
dasharray[i]) * pathScale;
if (dashes[i] < 0.0f) {
delete [] dashes;
return NS_OK;
if (dashes[i] < 0.0) {
return false;
}
totalLength += dashes[i];
}
} else {
return NS_ERROR_OUT_OF_MEMORY;
}
if (totalLength == 0.0f) {
delete [] dashes;
return NS_OK;
}
*aDashes = dashes;
*aCount = count;
}
return NS_OK;
}
float
nsSVGGeometryFrame::GetStrokeDashoffset()
{
nsSVGElement *ctx = static_cast<nsSVGElement*>
(mContent->IsNodeOfType(nsINode::eTEXT) ?
mContent->GetParent() : mContent);
return
nsSVGUtils::CoordToFloat(PresContext(),
*dashOffset = nsSVGUtils::CoordToFloat(presContext,
ctx,
GetStyleSVG()->mStrokeDashoffset);
return (totalLength > 0.0);
}
PRUint16
@ -304,12 +280,10 @@ nsSVGGeometryFrame::SetupCairoStrokeHitGeometry(gfxContext *aContext)
{
SetupCairoStrokeGeometry(aContext);
gfxFloat *dashArray;
PRUint32 count;
GetStrokeDashArray(&dashArray, &count);
if (count > 0) {
aContext->SetDash(dashArray, count, GetStrokeDashoffset());
delete [] dashArray;
AutoFallibleTArray<gfxFloat, 10> dashes;
gfxFloat dashOffset;
if (GetStrokeDashData(dashes, &dashOffset)) {
aContext->SetDash(dashes.Elements(), dashes.Length(), dashOffset);
}
}

View File

@ -116,8 +116,7 @@ protected:
virtual PRUint16 GetHitTestFlags();
private:
nsresult GetStrokeDashArray(double **arr, PRUint32 *count);
float GetStrokeDashoffset();
bool GetStrokeDashData(FallibleTArray<gfxFloat>& dashes, gfxFloat *dashOffset);
/**
* Returns the given 'fill-opacity' or 'stroke-opacity' value multiplied by