Bug 1058040, part 1 - Move gfxTextContextPaint to a separate file to enable use in imagelib. r=dholbert

This commit is contained in:
Jonathan Watt 2016-07-22 12:07:39 +01:00
parent fb331bdb6d
commit c5d930fd27
11 changed files with 190 additions and 133 deletions

View File

@ -12,10 +12,10 @@
#include "gfx2DGlue.h"
#include "gfxMatrix.h"
#include "gfxPlatform.h"
#include "gfxSVGGlyphs.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/dom/SVGSVGElement.h"
#include "mozilla/RefPtr.h"
#include "mozilla/SVGContextPaint.h"
#include "nsComputedDOMStyle.h"
#include "nsFontMetrics.h"
#include "nsIFrame.h"

View File

@ -9,6 +9,7 @@
#include "mozilla/DebugOnly.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/SVGContextPaint.h"
#include "mozilla/Logging.h"

View File

@ -4,6 +4,7 @@
#include "gfxSVGGlyphs.h"
#include "mozilla/SVGContextPaint.h"
#include "nsError.h"
#include "nsIDOMDocument.h"
#include "nsString.h"
@ -459,46 +460,3 @@ gfxSVGGlyphsDocument::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) c
+ mSVGGlyphsDocumentURI.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}
AutoSetRestoreSVGContextPaint::AutoSetRestoreSVGContextPaint(
gfxTextContextPaint* aContextPaint,
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(aSVGDocument->IsBeingUsedAsImage(),
"nsSVGUtils::GetContextPaint assumes this");
if (mOuterContextPaint) {
mSVGDocument->UnsetProperty(nsGkAtoms::svgContextPaint);
}
DebugOnly<nsresult> res =
mSVGDocument->SetProperty(nsGkAtoms::svgContextPaint, aContextPaint);
NS_WARN_IF_FALSE(NS_SUCCEEDED(res), "Failed to set context paint");
}
AutoSetRestoreSVGContextPaint::~AutoSetRestoreSVGContextPaint()
{
mSVGDocument->UnsetProperty(nsGkAtoms::svgContextPaint);
if (mOuterContextPaint) {
DebugOnly<nsresult> res =
mSVGDocument->SetProperty(nsGkAtoms::svgContextPaint, mOuterContextPaint);
NS_WARN_IF_FALSE(NS_SUCCEEDED(res), "Failed to restore context paint");
}
}
void
gfxTextContextPaint::InitStrokeGeometry(gfxContext *aContext,
float devUnitsPerSVGUnit)
{
mStrokeWidth = aContext->CurrentLineWidth() / devUnitsPerSVGUnit;
aContext->CurrentDash(mDashes, &mDashOffset);
for (uint32_t i = 0; i < mDashes.Length(); i++) {
mDashes[i] /= devUnitsPerSVGUnit;
}
mDashOffset /= devUnitsPerSVGUnit;
}

View File

@ -13,6 +13,7 @@
#include "nsHashKeys.h"
#include "gfxPattern.h"
#include "mozilla/gfx/UserData.h"
#include "mozilla/SVGContextPaint.h"
#include "nsRefreshDriver.h"
class nsIDocument;
@ -163,94 +164,21 @@ private:
};
/**
* RAII class used to temporarily set and remove SVG context paint while
* painting a piece of SVG. The context paint is set on the SVG's owner
* document, as expected by nsSVGUtils::GetContextPaint. Any pre-existing
* context paint is restored after this class removes the context paint that it
* set.
*/
class MOZ_RAII AutoSetRestoreSVGContextPaint
{
public:
AutoSetRestoreSVGContextPaint(gfxTextContextPaint* aContextPaint,
nsIDocument* aSVGDocument);
~AutoSetRestoreSVGContextPaint();
private:
nsIDocument* mSVGDocument;
// The context paint that needs to be restored by our dtor after it removes
// aContextPaint:
void* mOuterContextPaint;
};
/**
* Used for trickling down paint information through to SVG glyphs.
*/
class gfxTextContextPaint
{
protected:
typedef mozilla::gfx::DrawTarget DrawTarget;
gfxTextContextPaint() { }
public:
/*
* Get text context pattern with the specified opacity value.
* This lets us inherit paints and paint opacities (i.e. fill/stroke and
* fill-opacity/stroke-opacity) separately.
*/
virtual already_AddRefed<gfxPattern> GetFillPattern(const DrawTarget* aDrawTarget,
float aOpacity,
const gfxMatrix& aCTM) = 0;
virtual already_AddRefed<gfxPattern> GetStrokePattern(const DrawTarget* aDrawTarget,
float aOpacity,
const gfxMatrix& aCTM) = 0;
virtual float GetFillOpacity() { return 1.0f; }
virtual float GetStrokeOpacity() { return 1.0f; }
void InitStrokeGeometry(gfxContext *aContext,
float devUnitsPerSVGUnit);
FallibleTArray<gfxFloat>& GetStrokeDashArray() {
return mDashes;
}
gfxFloat GetStrokeDashOffset() {
return mDashOffset;
}
gfxFloat GetStrokeWidth() {
return mStrokeWidth;
}
already_AddRefed<gfxPattern> GetFillPattern(const DrawTarget* aDrawTarget,
const gfxMatrix& aCTM) {
return GetFillPattern(aDrawTarget, GetFillOpacity(), aCTM);
}
already_AddRefed<gfxPattern> GetStrokePattern(const DrawTarget* aDrawTarget,
const gfxMatrix& aCTM) {
return GetStrokePattern(aDrawTarget, GetStrokeOpacity(), aCTM);
}
virtual ~gfxTextContextPaint() { }
private:
FallibleTArray<gfxFloat> mDashes;
gfxFloat mDashOffset;
gfxFloat mStrokeWidth;
};
/**
* For passing in patterns where the text context has no separate pattern
* opacity value.
* XXX This is a complete hack and should die (see bug 1291494).
*
* This class is used when code fails to pass through an SVGContextPaint from
* the context in which we are painting. In that case we create one of these
* as a fallback and have it wrap the gfxContext's current gfxPattern and
* pretend that that is the paint context's fill pattern. In some contexts
* that will be the case, in others it will not. As we convert more code to
* Moz2D the less likely it is that this hack will work. It will also make
* converting to Moz2D harder.
*/
class SimpleTextContextPaint : public gfxTextContextPaint
{
private:
static const mozilla::gfx::Color sZero;
public:
static gfxMatrix SetupDeviceToPatternMatrix(gfxPattern *aPattern,
const gfxMatrix& aCTM)
{
@ -264,6 +192,7 @@ public:
return deviceToUser * aPattern->GetMatrix();
}
public:
SimpleTextContextPaint(gfxPattern *aFillPattern, gfxPattern *aStrokePattern,
const gfxMatrix& aCTM) :
mFillPattern(aFillPattern ? aFillPattern : new gfxPattern(sZero)),
@ -293,11 +222,11 @@ public:
return strokePattern.forget();
}
float GetFillOpacity() {
float GetFillOpacity() const {
return mFillPattern ? 1.0f : 0.0f;
}
float GetStrokeOpacity() {
float GetStrokeOpacity() const {
return mStrokePattern ? 1.0f : 0.0f;
}

View File

@ -0,0 +1,57 @@
/* 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"
#include "nsIDocument.h"
using namespace mozilla;
void
gfxTextContextPaint::InitStrokeGeometry(gfxContext* aContext,
float devUnitsPerSVGUnit)
{
mStrokeWidth = aContext->CurrentLineWidth() / devUnitsPerSVGUnit;
aContext->CurrentDash(mDashes, &mDashOffset);
for (uint32_t i = 0; i < mDashes.Length(); i++) {
mDashes[i] /= devUnitsPerSVGUnit;
}
mDashOffset /= devUnitsPerSVGUnit;
}
AutoSetRestoreSVGContextPaint::AutoSetRestoreSVGContextPaint(
gfxTextContextPaint* aContextPaint,
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(),
"nsSVGUtils::GetContextPaint assumes this");
if (mOuterContextPaint) {
mSVGDocument->UnsetProperty(nsGkAtoms::svgContextPaint);
}
DebugOnly<nsresult> res =
mSVGDocument->SetProperty(nsGkAtoms::svgContextPaint, aContextPaint);
NS_WARN_IF_FALSE(NS_SUCCEEDED(res), "Failed to set context paint");
}
AutoSetRestoreSVGContextPaint::~AutoSetRestoreSVGContextPaint()
{
mSVGDocument->UnsetProperty(nsGkAtoms::svgContextPaint);
if (mOuterContextPaint) {
DebugOnly<nsresult> res =
mSVGDocument->SetProperty(nsGkAtoms::svgContextPaint, mOuterContextPaint);
NS_WARN_IF_FALSE(NS_SUCCEEDED(res), "Failed to restore context paint");
}
}

View File

@ -0,0 +1,104 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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_SVGCONTEXTPAINT_H_
#define MOZILLA_SVGCONTEXTPAINT_H_
#include "gfxMatrix.h"
#include "gfxPattern.h"
#include "gfxTypes.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/gfx/2D.h"
#include "nsTArray.h"
class gfxContext;
class nsIDocument;
/**
* This class is used to pass information about a context element through to
* SVG painting code in order to resolve the 'context-fill' and related
* keywords. See:
*
* https://www.w3.org/TR/SVG2/painting.html#context-paint
*
* This feature allows the color in an SVG-in-OpenType glyph to come from the
* computed style for the text that is being drawn, for example, or for color
* in an SVG embedded by an <img> element to come from the embedding <img>
* element.
*/
class gfxTextContextPaint
{
protected:
typedef mozilla::gfx::DrawTarget DrawTarget;
gfxTextContextPaint() {}
public:
virtual ~gfxTextContextPaint() {}
virtual already_AddRefed<gfxPattern> GetFillPattern(const DrawTarget* aDrawTarget,
float aOpacity,
const gfxMatrix& aCTM) = 0;
virtual already_AddRefed<gfxPattern> GetStrokePattern(const DrawTarget* aDrawTarget,
float aOpacity,
const gfxMatrix& aCTM) = 0;
virtual float GetFillOpacity() const = 0;
virtual float GetStrokeOpacity() const = 0;
already_AddRefed<gfxPattern> GetFillPattern(const DrawTarget* aDrawTarget,
const gfxMatrix& aCTM) {
return GetFillPattern(aDrawTarget, GetFillOpacity(), aCTM);
}
already_AddRefed<gfxPattern> GetStrokePattern(const DrawTarget* aDrawTarget,
const gfxMatrix& aCTM) {
return GetStrokePattern(aDrawTarget, GetStrokeOpacity(), aCTM);
}
// XXX This gets the geometry params from the gfxContext. We should get that
// information from the actual paint context!
void InitStrokeGeometry(gfxContext *aContext,
float devUnitsPerSVGUnit);
FallibleTArray<gfxFloat>& GetStrokeDashArray() {
return mDashes;
}
gfxFloat GetStrokeDashOffset() {
return mDashOffset;
}
gfxFloat GetStrokeWidth() {
return mStrokeWidth;
}
private:
FallibleTArray<gfxFloat> mDashes;
gfxFloat mDashOffset;
gfxFloat mStrokeWidth;
};
/**
* RAII class used to temporarily set and remove an SVGContextPaint while a
* piece of SVG is being painted. The context paint is set on the SVG's owner
* document, as expected by nsSVGUtils::GetContextPaint. Any pre-existing
* context paint is restored after this class removes the context paint that it
* set.
*/
class MOZ_RAII AutoSetRestoreSVGContextPaint
{
public:
AutoSetRestoreSVGContextPaint(gfxTextContextPaint* aContextPaint,
nsIDocument* aSVGDocument);
~AutoSetRestoreSVGContextPaint();
private:
nsIDocument* mSVGDocument;
// The context paint that needs to be restored by our dtor after it removes
// aContextPaint:
void* mOuterContextPaint;
};
#endif // MOZILLA_SVGCONTEXTPAINT_H_

View File

@ -9,9 +9,9 @@
#include "mozilla/Attributes.h"
#include "mozilla/RefPtr.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/SVGContextPaint.h"
#include "gfxMatrix.h"
#include "gfxRect.h"
#include "gfxSVGGlyphs.h"
#include "gfxTextRun.h"
#include "nsAutoPtr.h"
#include "nsIContent.h" // for GetContent
@ -151,10 +151,10 @@ public:
const gfxMatrix& aCTM) override;
void SetFillOpacity(float aOpacity) { mFillOpacity = aOpacity; }
float GetFillOpacity() override { return mFillOpacity; }
float GetFillOpacity() const override { return mFillOpacity; }
void SetStrokeOpacity(float aOpacity) { mStrokeOpacity = aOpacity; }
float GetStrokeOpacity() override { return mStrokeOpacity; }
float GetStrokeOpacity() const override { return mStrokeOpacity; }
struct Paint {
Paint() : mPaintType(eStyleSVGPaintType_None) {}

View File

@ -17,6 +17,10 @@ EXPORTS += [
'SVGImageContext.h',
]
EXPORTS.mozilla += [
'SVGContextPaint.h',
]
UNIFIED_SOURCES += [
'nsCSSClipPathInstance.cpp',
'nsCSSFilterInstance.cpp',
@ -43,6 +47,7 @@ UNIFIED_SOURCES += [
'nsSVGSwitchFrame.cpp',
'nsSVGUseFrame.cpp',
'nsSVGUtils.cpp',
'SVGContextPaint.cpp',
'SVGFEContainerFrame.cpp',
'SVGFEImageFrame.cpp',
'SVGFELeafFrame.cpp',

View File

@ -7,7 +7,9 @@
#include "nsSVGForeignObjectFrame.h"
// Keep others in (case-insensitive) order:
#include "DrawResult.h"
#include "gfxContext.h"
#include "nsDisplayList.h"
#include "nsGkAtoms.h"
#include "nsNameSpaceManager.h"
#include "nsLayoutUtils.h"
@ -23,6 +25,7 @@
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::image;
//----------------------------------------------------------------------
// Implementation

View File

@ -10,11 +10,11 @@
#include "gfx2DGlue.h"
#include "gfxContext.h"
#include "gfxPlatform.h"
#include "gfxSVGGlyphs.h"
#include "gfxUtils.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Helpers.h"
#include "mozilla/RefPtr.h"
#include "mozilla/SVGContextPaint.h"
#include "nsDisplayList.h"
#include "nsGkAtoms.h"
#include "nsLayoutUtils.h"

View File

@ -18,6 +18,7 @@
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/PatternHelpers.h"
#include "mozilla/Preferences.h"
#include "mozilla/SVGContextPaint.h"
#include "nsCSSClipPathInstance.h"
#include "nsCSSFrameConstructor.h"
#include "nsDisplayList.h"
@ -38,7 +39,6 @@
#include "nsSVGEffects.h"
#include "nsSVGFilterPaintCallback.h"
#include "nsSVGForeignObjectFrame.h"
#include "gfxSVGGlyphs.h"
#include "nsSVGInnerSVGFrame.h"
#include "nsSVGIntegrationUtils.h"
#include "nsSVGLength2.h"