2016-07-22 14:07:39 +03:00
|
|
|
/* 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 "SVGContextPaint.h"
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
2016-07-25 19:34:18 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2016-07-22 14:07:39 +03:00
|
|
|
#include "nsIDocument.h"
|
2016-07-25 15:27:00 +03:00
|
|
|
#include "nsSVGPaintServerFrame.h"
|
2016-07-25 19:34:18 +03:00
|
|
|
#include "nsSVGEffects.h"
|
|
|
|
#include "nsSVGPaintServerFrame.h"
|
2016-07-25 15:27:00 +03:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
2016-07-22 14:07:39 +03:00
|
|
|
|
2016-07-22 16:56:09 +03:00
|
|
|
namespace mozilla {
|
2016-07-22 14:07:39 +03:00
|
|
|
|
2016-07-25 19:34:18 +03:00
|
|
|
/**
|
|
|
|
* Stores in |aTargetPaint| information on how to reconstruct the current
|
|
|
|
* fill or stroke pattern. Will also set the paint opacity to transparent if
|
|
|
|
* the paint is set to "none".
|
|
|
|
* @param aOuterContextPaint pattern information from the outer text context
|
|
|
|
* @param aTargetPaint where to store the current pattern information
|
|
|
|
* @param aFillOrStroke member pointer to the paint we are setting up
|
|
|
|
* @param aProperty the frame property descriptor of the fill or stroke paint
|
|
|
|
* server frame
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
SetupInheritablePaint(const DrawTarget* aDrawTarget,
|
|
|
|
const gfxMatrix& aContextMatrix,
|
|
|
|
nsIFrame* aFrame,
|
|
|
|
float& aOpacity,
|
|
|
|
SVGContextPaint* aOuterContextPaint,
|
|
|
|
SVGContextPaintImpl::Paint& aTargetPaint,
|
|
|
|
nsStyleSVGPaint nsStyleSVG::*aFillOrStroke,
|
|
|
|
nsSVGEffects::PaintingPropertyDescriptor aProperty)
|
|
|
|
{
|
|
|
|
const nsStyleSVG *style = aFrame->StyleSVG();
|
|
|
|
nsSVGPaintServerFrame *ps =
|
|
|
|
nsSVGEffects::GetPaintServer(aFrame, aFillOrStroke, aProperty);
|
|
|
|
|
|
|
|
if (ps) {
|
|
|
|
RefPtr<gfxPattern> pattern =
|
|
|
|
ps->GetPaintServerPattern(aFrame, aDrawTarget, aContextMatrix,
|
|
|
|
aFillOrStroke, aOpacity);
|
|
|
|
if (pattern) {
|
|
|
|
aTargetPaint.SetPaintServer(aFrame, aContextMatrix, ps);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aOuterContextPaint) {
|
|
|
|
RefPtr<gfxPattern> pattern;
|
2016-10-11 09:56:11 +03:00
|
|
|
switch ((style->*aFillOrStroke).Type()) {
|
2016-07-25 19:34:18 +03:00
|
|
|
case eStyleSVGPaintType_ContextFill:
|
|
|
|
pattern = aOuterContextPaint->GetFillPattern(aDrawTarget, aOpacity,
|
|
|
|
aContextMatrix);
|
|
|
|
break;
|
|
|
|
case eStyleSVGPaintType_ContextStroke:
|
|
|
|
pattern = aOuterContextPaint->GetStrokePattern(aDrawTarget, aOpacity,
|
|
|
|
aContextMatrix);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (pattern) {
|
2016-10-11 09:56:11 +03:00
|
|
|
aTargetPaint.SetContextPaint(aOuterContextPaint, (style->*aFillOrStroke).Type());
|
2016-07-25 19:34:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nscolor color =
|
|
|
|
nsSVGUtils::GetFallbackOrPaintColor(aFrame->StyleContext(), aFillOrStroke);
|
|
|
|
aTargetPaint.SetColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawMode
|
|
|
|
SVGContextPaintImpl::Init(const DrawTarget* aDrawTarget,
|
|
|
|
const gfxMatrix& aContextMatrix,
|
|
|
|
nsIFrame* aFrame,
|
|
|
|
SVGContextPaint* aOuterContextPaint)
|
|
|
|
{
|
|
|
|
DrawMode toDraw = DrawMode(0);
|
|
|
|
|
|
|
|
const nsStyleSVG *style = aFrame->StyleSVG();
|
|
|
|
|
|
|
|
// fill:
|
2016-10-11 09:56:11 +03:00
|
|
|
if (style->mFill.Type() == eStyleSVGPaintType_None) {
|
2016-07-25 19:34:18 +03:00
|
|
|
SetFillOpacity(0.0f);
|
|
|
|
} else {
|
|
|
|
float opacity = nsSVGUtils::GetOpacity(style->FillOpacitySource(),
|
|
|
|
style->mFillOpacity,
|
|
|
|
aOuterContextPaint);
|
|
|
|
|
|
|
|
SetupInheritablePaint(aDrawTarget, aContextMatrix, aFrame,
|
|
|
|
opacity, aOuterContextPaint,
|
|
|
|
mFillPaint, &nsStyleSVG::mFill,
|
|
|
|
nsSVGEffects::FillProperty());
|
|
|
|
|
|
|
|
SetFillOpacity(opacity);
|
|
|
|
|
|
|
|
toDraw |= DrawMode::GLYPH_FILL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stroke:
|
2016-10-11 09:56:11 +03:00
|
|
|
if (style->mStroke.Type() == eStyleSVGPaintType_None) {
|
2016-07-25 19:34:18 +03:00
|
|
|
SetStrokeOpacity(0.0f);
|
|
|
|
} else {
|
|
|
|
float opacity = nsSVGUtils::GetOpacity(style->StrokeOpacitySource(),
|
|
|
|
style->mStrokeOpacity,
|
|
|
|
aOuterContextPaint);
|
|
|
|
|
|
|
|
SetupInheritablePaint(aDrawTarget, aContextMatrix, aFrame,
|
|
|
|
opacity, aOuterContextPaint,
|
|
|
|
mStrokePaint, &nsStyleSVG::mStroke,
|
|
|
|
nsSVGEffects::StrokeProperty());
|
|
|
|
|
|
|
|
SetStrokeOpacity(opacity);
|
|
|
|
|
|
|
|
toDraw |= DrawMode::GLYPH_STROKE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return toDraw;
|
|
|
|
}
|
|
|
|
|
2016-07-22 14:07:39 +03:00
|
|
|
void
|
2016-07-22 16:56:09 +03:00
|
|
|
SVGContextPaint::InitStrokeGeometry(gfxContext* aContext,
|
|
|
|
float devUnitsPerSVGUnit)
|
2016-07-22 14:07:39 +03:00
|
|
|
{
|
|
|
|
mStrokeWidth = aContext->CurrentLineWidth() / devUnitsPerSVGUnit;
|
|
|
|
aContext->CurrentDash(mDashes, &mDashOffset);
|
|
|
|
for (uint32_t i = 0; i < mDashes.Length(); i++) {
|
|
|
|
mDashes[i] /= devUnitsPerSVGUnit;
|
|
|
|
}
|
|
|
|
mDashOffset /= devUnitsPerSVGUnit;
|
|
|
|
}
|
|
|
|
|
2016-07-25 19:34:18 +03:00
|
|
|
/* static */ SVGContextPaint*
|
|
|
|
SVGContextPaint::GetContextPaint(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
nsIDocument* ownerDoc = aContent->OwnerDoc();
|
|
|
|
|
|
|
|
if (!ownerDoc->IsBeingUsedAsImage()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<SVGContextPaint*>(
|
|
|
|
ownerDoc->GetProperty(nsGkAtoms::svgContextPaint));
|
|
|
|
}
|
|
|
|
|
2016-07-25 15:27:00 +03:00
|
|
|
already_AddRefed<gfxPattern>
|
2016-07-25 18:04:20 +03:00
|
|
|
SVGContextPaintImpl::GetFillPattern(const DrawTarget* aDrawTarget,
|
2016-07-25 15:27:00 +03:00
|
|
|
float aOpacity,
|
|
|
|
const gfxMatrix& aCTM)
|
|
|
|
{
|
|
|
|
return mFillPaint.GetPattern(aDrawTarget, aOpacity, &nsStyleSVG::mFill, aCTM);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxPattern>
|
2016-07-25 18:04:20 +03:00
|
|
|
SVGContextPaintImpl::GetStrokePattern(const DrawTarget* aDrawTarget,
|
2016-07-25 15:27:00 +03:00
|
|
|
float aOpacity,
|
|
|
|
const gfxMatrix& aCTM)
|
|
|
|
{
|
|
|
|
return mStrokePaint.GetPattern(aDrawTarget, aOpacity, &nsStyleSVG::mStroke, aCTM);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxPattern>
|
2016-07-25 18:04:20 +03:00
|
|
|
SVGContextPaintImpl::Paint::GetPattern(const DrawTarget* aDrawTarget,
|
2016-07-25 15:27:00 +03:00
|
|
|
float aOpacity,
|
|
|
|
nsStyleSVGPaint nsStyleSVG::*aFillOrStroke,
|
|
|
|
const gfxMatrix& aCTM)
|
|
|
|
{
|
|
|
|
RefPtr<gfxPattern> pattern;
|
|
|
|
if (mPatternCache.Get(aOpacity, getter_AddRefs(pattern))) {
|
|
|
|
// Set the pattern matrix just in case it was messed with by a previous
|
|
|
|
// caller. We should get the same matrix each time a pattern is constructed
|
|
|
|
// so this should be fine.
|
|
|
|
pattern->SetMatrix(aCTM * mPatternMatrix);
|
|
|
|
return pattern.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (mPaintType) {
|
|
|
|
case eStyleSVGPaintType_None:
|
|
|
|
pattern = new gfxPattern(Color());
|
|
|
|
mPatternMatrix = gfxMatrix();
|
|
|
|
break;
|
|
|
|
case eStyleSVGPaintType_Color: {
|
|
|
|
Color color = Color::FromABGR(mPaintDefinition.mColor);
|
|
|
|
color.a *= aOpacity;
|
|
|
|
pattern = new gfxPattern(color);
|
|
|
|
mPatternMatrix = gfxMatrix();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case eStyleSVGPaintType_Server:
|
|
|
|
pattern = mPaintDefinition.mPaintServerFrame->GetPaintServerPattern(mFrame,
|
|
|
|
aDrawTarget,
|
|
|
|
mContextMatrix,
|
|
|
|
aFillOrStroke,
|
|
|
|
aOpacity);
|
|
|
|
{
|
|
|
|
// m maps original-user-space to pattern space
|
|
|
|
gfxMatrix m = pattern->GetMatrix();
|
|
|
|
gfxMatrix deviceToOriginalUserSpace = mContextMatrix;
|
|
|
|
if (!deviceToOriginalUserSpace.Invert()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// mPatternMatrix maps device space to pattern space via original user space
|
|
|
|
mPatternMatrix = deviceToOriginalUserSpace * m;
|
|
|
|
}
|
|
|
|
pattern->SetMatrix(aCTM * mPatternMatrix);
|
|
|
|
break;
|
|
|
|
case eStyleSVGPaintType_ContextFill:
|
|
|
|
pattern = mPaintDefinition.mContextPaint->GetFillPattern(aDrawTarget,
|
|
|
|
aOpacity, aCTM);
|
|
|
|
// Don't cache this. mContextPaint will have cached it anyway. If we
|
|
|
|
// cache it, we'll have to compute mPatternMatrix, which is annoying.
|
|
|
|
return pattern.forget();
|
|
|
|
case eStyleSVGPaintType_ContextStroke:
|
|
|
|
pattern = mPaintDefinition.mContextPaint->GetStrokePattern(aDrawTarget,
|
|
|
|
aOpacity, aCTM);
|
|
|
|
// Don't cache this. mContextPaint will have cached it anyway. If we
|
|
|
|
// cache it, we'll have to compute mPatternMatrix, which is annoying.
|
|
|
|
return pattern.forget();
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "invalid paint type");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPatternCache.Put(aOpacity, pattern);
|
|
|
|
return pattern.forget();
|
|
|
|
}
|
|
|
|
|
2016-07-22 14:07:39 +03:00
|
|
|
AutoSetRestoreSVGContextPaint::AutoSetRestoreSVGContextPaint(
|
2016-07-22 16:56:09 +03:00
|
|
|
SVGContextPaint* aContextPaint,
|
2016-07-22 14:07:39 +03:00
|
|
|
nsIDocument* aSVGDocument)
|
|
|
|
: mSVGDocument(aSVGDocument)
|
|
|
|
, mOuterContextPaint(aSVGDocument->GetProperty(nsGkAtoms::svgContextPaint))
|
|
|
|
{
|
|
|
|
// The way that we supply context paint is to temporarily set the context
|
|
|
|
// paint on the owner document of the SVG that we're painting while it's
|
|
|
|
// being painted.
|
|
|
|
|
|
|
|
MOZ_ASSERT(aContextPaint);
|
|
|
|
MOZ_ASSERT(aSVGDocument->IsBeingUsedAsImage(),
|
2016-07-25 19:34:18 +03:00
|
|
|
"SVGContextPaint::GetContextPaint assumes this");
|
2016-07-22 14:07:39 +03:00
|
|
|
|
|
|
|
if (mOuterContextPaint) {
|
|
|
|
mSVGDocument->UnsetProperty(nsGkAtoms::svgContextPaint);
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugOnly<nsresult> res =
|
|
|
|
mSVGDocument->SetProperty(nsGkAtoms::svgContextPaint, aContextPaint);
|
|
|
|
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(res), "Failed to set context paint");
|
2016-07-22 14:07:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoSetRestoreSVGContextPaint::~AutoSetRestoreSVGContextPaint()
|
|
|
|
{
|
|
|
|
mSVGDocument->UnsetProperty(nsGkAtoms::svgContextPaint);
|
|
|
|
if (mOuterContextPaint) {
|
|
|
|
DebugOnly<nsresult> res =
|
|
|
|
mSVGDocument->SetProperty(nsGkAtoms::svgContextPaint, mOuterContextPaint);
|
|
|
|
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(res), "Failed to restore context paint");
|
2016-07-22 14:07:39 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-22 16:56:09 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|