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

View File

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