2012-05-03 14:31:02 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#endif
|
|
|
|
#include <math.h>
|
|
|
|
|
2013-08-14 00:00:52 -07:00
|
|
|
#include "mozilla/Alignment.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "cairo.h"
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
|
|
|
|
#include "gfxColor.h"
|
|
|
|
#include "gfxMatrix.h"
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxPattern.h"
|
2007-07-23 15:02:17 -07:00
|
|
|
#include "gfxPlatform.h"
|
2011-01-02 17:48:09 -08:00
|
|
|
#include "gfxTeeSurface.h"
|
2013-03-18 07:25:50 -07:00
|
|
|
#include "GeckoProfiler.h"
|
2013-10-07 16:15:59 -07:00
|
|
|
#include "gfx2DGlue.h"
|
2013-11-01 06:29:53 -07:00
|
|
|
#include "mozilla/gfx/PathHelpers.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-01-21 01:24:01 -08:00
|
|
|
#if CAIRO_HAS_DWRITE_FONT
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2013-10-09 21:44:09 -07:00
|
|
|
UserDataKey gfxContext::sDontUseAsSourceKey;
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
/* This class lives on the stack and allows gfxContext users to easily, and
|
|
|
|
* performantly get a gfx::Pattern to use for drawing in their current context.
|
|
|
|
*/
|
|
|
|
class GeneralPattern
|
|
|
|
{
|
|
|
|
public:
|
2013-07-31 08:44:31 -07:00
|
|
|
GeneralPattern(gfxContext *aContext) : mContext(aContext), mPattern(nullptr) {}
|
2012-01-04 23:17:51 -08:00
|
|
|
~GeneralPattern() { if (mPattern) { mPattern->~Pattern(); } }
|
|
|
|
|
|
|
|
operator mozilla::gfx::Pattern&()
|
|
|
|
{
|
|
|
|
gfxContext::AzureState &state = mContext->CurrentState();
|
|
|
|
|
|
|
|
if (state.pattern) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return *state.pattern->GetPattern(mContext->mDT, state.patternTransformChanged ? &state.patternTransform : nullptr);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else if (state.sourceSurface) {
|
2012-03-29 11:53:43 -07:00
|
|
|
Matrix transform = state.surfTransform;
|
|
|
|
|
|
|
|
if (state.patternTransformChanged) {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mContext->mTransform;
|
2012-03-29 11:53:43 -07:00
|
|
|
mat.Invert();
|
|
|
|
|
2012-09-27 14:00:51 -07:00
|
|
|
transform = transform * state.patternTransform * mat;
|
2012-03-29 11:53:43 -07:00
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
mPattern = new (mSurfacePattern.addr())
|
2012-03-29 11:53:43 -07:00
|
|
|
SurfacePattern(state.sourceSurface, EXTEND_CLAMP, transform);
|
2012-01-04 23:17:51 -08:00
|
|
|
return *mPattern;
|
|
|
|
} else {
|
|
|
|
mPattern = new (mColorPattern.addr())
|
|
|
|
ColorPattern(state.color);
|
|
|
|
return *mPattern;
|
2011-07-14 10:02:20 -07:00
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
union {
|
|
|
|
mozilla::AlignedStorage2<mozilla::gfx::ColorPattern> mColorPattern;
|
|
|
|
mozilla::AlignedStorage2<mozilla::gfx::SurfacePattern> mSurfacePattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
gfxContext *mContext;
|
|
|
|
Pattern *mPattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
gfxContext::gfxContext(gfxASurface *surface)
|
2013-07-31 08:44:31 -07:00
|
|
|
: mRefCairo(nullptr)
|
2012-03-06 13:44:18 -08:00
|
|
|
, mSurface(surface)
|
2012-01-04 23:17:51 -08:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(gfxContext);
|
|
|
|
|
|
|
|
mCairo = cairo_create(surface->CairoSurface());
|
|
|
|
mFlags = surface->GetDefaultContextFlags();
|
|
|
|
if (mSurface->GetRotateForLandscape()) {
|
|
|
|
// Rotate page 90 degrees to draw landscape page on portrait paper
|
|
|
|
gfxIntSize size = mSurface->GetSize();
|
|
|
|
Translate(gfxPoint(0, size.width));
|
|
|
|
gfxMatrix matrix(0, -1,
|
|
|
|
1, 0,
|
|
|
|
0, 0);
|
|
|
|
Multiply(matrix);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
gfxContext::gfxContext(DrawTarget *aTarget)
|
|
|
|
: mPathIsRect(false)
|
|
|
|
, mTransformChanged(false)
|
2013-07-31 08:44:31 -07:00
|
|
|
, mCairo(nullptr)
|
|
|
|
, mRefCairo(nullptr)
|
|
|
|
, mSurface(nullptr)
|
2012-01-04 23:17:51 -08:00
|
|
|
, mFlags(0)
|
|
|
|
, mDT(aTarget)
|
|
|
|
, mOriginalDT(aTarget)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(gfxContext);
|
|
|
|
|
|
|
|
mStateStack.SetLength(1);
|
|
|
|
CurrentState().drawTarget = mDT;
|
|
|
|
mDT->SetTransform(Matrix());
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxContext::~gfxContext()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_destroy(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
if (mRefCairo) {
|
|
|
|
cairo_destroy(mRefCairo);
|
|
|
|
}
|
|
|
|
if (mDT) {
|
|
|
|
for (int i = mStateStack.Length() - 1; i >= 0; i--) {
|
|
|
|
for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
|
|
|
|
mDT->PopClip();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStateStack[i].clipWasReset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mDT->Flush();
|
|
|
|
}
|
|
|
|
MOZ_COUNT_DTOR(gfxContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxASurface *
|
|
|
|
gfxContext::OriginalSurface()
|
|
|
|
{
|
2013-08-22 21:53:53 -07:00
|
|
|
if (mCairo || mSurface) {
|
|
|
|
return mSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOriginalDT && mOriginalDT->GetType() == BACKEND_CAIRO) {
|
|
|
|
cairo_surface_t *s =
|
|
|
|
(cairo_surface_t*)mOriginalDT->GetNativeSurface(NATIVE_SURFACE_CAIRO_SURFACE);
|
|
|
|
if (s) {
|
|
|
|
mSurface = gfxASurface::Wrap(s);
|
|
|
|
return mSurface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxASurface>
|
|
|
|
gfxContext::CurrentSurface(gfxFloat *dx, gfxFloat *dy)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_surface_t *s = cairo_get_group_target(mCairo);
|
|
|
|
if (s == mSurface->CairoSurface()) {
|
|
|
|
if (dx && dy)
|
|
|
|
cairo_surface_get_device_offset(s, dx, dy);
|
2013-04-22 04:15:59 -07:00
|
|
|
nsRefPtr<gfxASurface> ret = mSurface;
|
|
|
|
return ret.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dx && dy)
|
|
|
|
cairo_surface_get_device_offset(s, dx, dy);
|
|
|
|
return gfxASurface::Wrap(s);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-08-22 21:53:53 -07:00
|
|
|
if (mDT->GetType() == BACKEND_CAIRO) {
|
|
|
|
cairo_surface_t *s =
|
|
|
|
(cairo_surface_t*)mDT->GetNativeSurface(NATIVE_SURFACE_CAIRO_SURFACE);
|
|
|
|
if (s) {
|
|
|
|
if (dx && dy)
|
|
|
|
cairo_surface_get_device_offset(s, dx, dy);
|
|
|
|
return gfxASurface::Wrap(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
if (dx && dy) {
|
|
|
|
*dx = *dy = 0;
|
|
|
|
}
|
|
|
|
// An Azure context doesn't have a surface backing it.
|
2013-04-28 04:52:10 -07:00
|
|
|
return nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_t *
|
|
|
|
gfxContext::GetCairo()
|
|
|
|
{
|
|
|
|
if (mCairo) {
|
|
|
|
return mCairo;
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:53:53 -07:00
|
|
|
if (mDT->GetType() == BACKEND_CAIRO) {
|
|
|
|
cairo_t *ctx =
|
2013-09-13 02:06:54 -07:00
|
|
|
(cairo_t*)mDT->GetNativeSurface(NATIVE_SURFACE_CAIRO_CONTEXT);
|
2013-08-22 21:53:53 -07:00
|
|
|
if (ctx) {
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mRefCairo) {
|
|
|
|
// Set transform!
|
|
|
|
return mRefCairo;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRefCairo = cairo_create(gfxPlatform::GetPlatform()->ScreenReferenceSurface()->CairoSurface());
|
|
|
|
|
|
|
|
return mRefCairo;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Save()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_save(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
CurrentState().transform = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
mStateStack.AppendElement(AzureState(CurrentState()));
|
|
|
|
CurrentState().clipWasReset = false;
|
|
|
|
CurrentState().pushedClips.Clear();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Restore()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_restore(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
for (unsigned int c = 0; c < CurrentState().pushedClips.Length(); c++) {
|
|
|
|
mDT->PopClip();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurrentState().clipWasReset &&
|
|
|
|
CurrentState().drawTarget == mStateStack[mStateStack.Length() - 2].drawTarget) {
|
|
|
|
PushClipsToDT(mDT);
|
|
|
|
}
|
|
|
|
|
|
|
|
mStateStack.RemoveElementAt(mStateStack.Length() - 1);
|
|
|
|
|
|
|
|
mDT = CurrentState().drawTarget;
|
2012-09-27 14:00:51 -07:00
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
ChangeTransform(CurrentState().transform, false);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// drawing
|
|
|
|
void
|
|
|
|
gfxContext::NewPath()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_new_path(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-07-31 08:44:31 -07:00
|
|
|
mPath = nullptr;
|
|
|
|
mPathBuilder = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
mPathIsRect = false;
|
|
|
|
mTransformChanged = false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::ClosePath()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_close_path(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->Close();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-10-14 20:23:21 -07:00
|
|
|
already_AddRefed<gfxPath> gfxContext::CopyPath()
|
2008-07-18 11:26:02 -07:00
|
|
|
{
|
2013-10-14 20:23:21 -07:00
|
|
|
nsRefPtr<gfxPath> path;
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2013-10-14 20:23:21 -07:00
|
|
|
path = new gfxPath(cairo_copy_path(mCairo));
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-10-14 20:23:21 -07:00
|
|
|
EnsurePath();
|
|
|
|
path = new gfxPath(mPath);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2013-10-14 20:23:21 -07:00
|
|
|
return path.forget();
|
2008-07-18 11:26:02 -07:00
|
|
|
}
|
|
|
|
|
2013-10-14 20:23:21 -07:00
|
|
|
void gfxContext::SetPath(gfxPath* path)
|
2008-07-18 11:26:02 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2013-10-14 20:23:21 -07:00
|
|
|
cairo_new_path(mCairo);
|
2008-07-18 11:26:02 -07:00
|
|
|
if (path->mPath->status == CAIRO_STATUS_SUCCESS && path->mPath->num_data != 0)
|
|
|
|
cairo_append_path(mCairo, path->mPath);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-10-14 20:23:21 -07:00
|
|
|
MOZ_ASSERT(path->mMoz2DPath, "Can't mix cairo and azure paths!");
|
|
|
|
MOZ_ASSERT(path->mMoz2DPath->GetBackendType() == mDT->GetType());
|
|
|
|
mPath = path->mMoz2DPath;
|
|
|
|
mPathBuilder = nullptr;
|
|
|
|
mPathIsRect = false;
|
|
|
|
mTransformChanged = false;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2008-07-18 11:26:02 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPoint
|
2012-01-04 23:17:51 -08:00
|
|
|
gfxContext::CurrentPoint()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
double x, y;
|
|
|
|
cairo_get_current_point(mCairo, &x, &y);
|
|
|
|
return gfxPoint(x, y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
return ThebesPoint(mPathBuilder->CurrentPoint());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Stroke()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_stroke_preserve(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
if (mPathIsRect) {
|
2012-12-13 07:34:51 -08:00
|
|
|
MOZ_ASSERT(!mTransformChanged);
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
mDT->StrokeRect(mRect, GeneralPattern(this),
|
|
|
|
state.strokeOptions,
|
|
|
|
DrawOptions(1.0f, GetOp(), state.aaMode));
|
|
|
|
} else {
|
|
|
|
EnsurePath();
|
|
|
|
|
|
|
|
mDT->Stroke(mPath, GeneralPattern(this), state.strokeOptions,
|
|
|
|
DrawOptions(1.0f, GetOp(), state.aaMode));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Fill()
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("gfxContext", "Fill");
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_fill_preserve(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
FillAzure(1.0f);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 22:26:37 -08:00
|
|
|
void
|
|
|
|
gfxContext::FillWithOpacity(gfxFloat aOpacity)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
|
|
|
// This method exists in the hope that one day cairo gets a direct
|
|
|
|
// API for this, and then we would change this method to use that
|
|
|
|
// API instead.
|
|
|
|
if (aOpacity != 1.0) {
|
|
|
|
gfxContextAutoSaveRestore saveRestore(this);
|
|
|
|
Clip();
|
|
|
|
Paint(aOpacity);
|
|
|
|
} else {
|
|
|
|
Fill();
|
|
|
|
}
|
2011-01-25 22:26:37 -08:00
|
|
|
} else {
|
2012-01-04 23:17:51 -08:00
|
|
|
FillAzure(Float(aOpacity));
|
2011-01-25 22:26:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxContext::MoveTo(const gfxPoint& pt)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_move_to(mCairo, pt.x, pt.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->MoveTo(ToPoint(pt));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-04-30 20:31:25 -07:00
|
|
|
void
|
|
|
|
gfxContext::NewSubPath()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-04-30 20:31:25 -07:00
|
|
|
cairo_new_sub_path(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
// XXX - This has no users, we should kill it, it should be equivelant to a
|
|
|
|
// MoveTo to the path's current point.
|
|
|
|
}
|
2007-04-30 20:31:25 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxContext::LineTo(const gfxPoint& pt)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_line_to(mCairo, pt.x, pt.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->LineTo(ToPoint(pt));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::CurveTo(const gfxPoint& pt1, const gfxPoint& pt2, const gfxPoint& pt3)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_curve_to(mCairo, pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->BezierTo(ToPoint(pt1), ToPoint(pt2), ToPoint(pt3));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-23 10:25:00 -07:00
|
|
|
void
|
|
|
|
gfxContext::QuadraticCurveTo(const gfxPoint& pt1, const gfxPoint& pt2)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2008-07-23 10:25:00 -07:00
|
|
|
double cx, cy;
|
|
|
|
cairo_get_current_point(mCairo, &cx, &cy);
|
|
|
|
cairo_curve_to(mCairo,
|
|
|
|
(cx + pt1.x * 2.0) / 3.0,
|
|
|
|
(cy + pt1.y * 2.0) / 3.0,
|
|
|
|
(pt1.x * 2.0 + pt2.x) / 3.0,
|
|
|
|
(pt1.y * 2.0 + pt2.y) / 3.0,
|
|
|
|
pt2.x,
|
|
|
|
pt2.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->QuadraticBezierTo(ToPoint(pt1), ToPoint(pt2));
|
|
|
|
}
|
2008-07-23 10:25:00 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxContext::Arc(const gfxPoint& center, gfxFloat radius,
|
|
|
|
gfxFloat angle1, gfxFloat angle2)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_arc(mCairo, center.x, center.y, radius, angle1, angle2);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->Arc(ToPoint(center), Float(radius), Float(angle1), Float(angle2));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::NegativeArc(const gfxPoint& center, gfxFloat radius,
|
|
|
|
gfxFloat angle1, gfxFloat angle2)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_arc_negative(mCairo, center.x, center.y, radius, angle1, angle2);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->Arc(ToPoint(center), Float(radius), Float(angle2), Float(angle1));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Line(const gfxPoint& start, const gfxPoint& end)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
MoveTo(start);
|
|
|
|
LineTo(end);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPathBuilder->MoveTo(ToPoint(start));
|
|
|
|
mPathBuilder->LineTo(ToPoint(end));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX snapToPixels is only valid when snapping for filled
|
|
|
|
// rectangles and for even-width stroked rectangles.
|
|
|
|
// For odd-width stroked rectangles, we need to offset x/y by
|
|
|
|
// 0.5...
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
gfxContext::Rectangle(const gfxRect& rect, bool snapToPixels)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (snapToPixels) {
|
|
|
|
gfxRect snappedRect(rect);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
if (UserToDevicePixelSnapped(snappedRect, true))
|
2008-10-09 14:49:15 -07:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mCairo, &mat);
|
|
|
|
cairo_identity_matrix(mCairo);
|
|
|
|
Rectangle(snappedRect);
|
|
|
|
cairo_set_matrix(mCairo, &mat);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-18 20:07:21 -07:00
|
|
|
cairo_rectangle(mCairo, rect.X(), rect.Y(), rect.Width(), rect.Height());
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
Rect rec = ToRect(rect);
|
|
|
|
|
|
|
|
if (snapToPixels) {
|
|
|
|
gfxRect newRect(rect);
|
2012-02-14 08:17:19 -08:00
|
|
|
if (UserToDevicePixelSnapped(newRect, true)) {
|
2012-09-27 14:00:51 -07:00
|
|
|
gfxMatrix mat = ThebesMatrix(mTransform);
|
2012-01-04 23:17:51 -08:00
|
|
|
mat.Invert();
|
|
|
|
|
|
|
|
// We need the user space rect.
|
|
|
|
rec = ToRect(mat.TransformBounds(newRect));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mPathBuilder && !mPathIsRect) {
|
|
|
|
mPathIsRect = true;
|
|
|
|
mRect = rec;
|
|
|
|
return;
|
|
|
|
}
|
2012-12-13 07:34:51 -08:00
|
|
|
|
|
|
|
EnsurePathBuilder();
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
mPathBuilder->MoveTo(rec.TopLeft());
|
|
|
|
mPathBuilder->LineTo(rec.TopRight());
|
|
|
|
mPathBuilder->LineTo(rec.BottomRight());
|
|
|
|
mPathBuilder->LineTo(rec.BottomLeft());
|
|
|
|
mPathBuilder->Close();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Ellipse(const gfxPoint& center, const gfxSize& dimensions)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
gfxSize halfDim = dimensions / 2.0;
|
|
|
|
gfxRect r(center - gfxPoint(halfDim.width, halfDim.height), dimensions);
|
|
|
|
gfxCornerSizes c(halfDim, halfDim, halfDim, halfDim);
|
2008-07-23 10:25:00 -07:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
RoundedRectangle (r, c);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxContext::Polygon(const gfxPoint *points, uint32_t numPoints)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (numPoints == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cairo_move_to(mCairo, points[0].x, points[0].y);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 1; i < numPoints; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_line_to(mCairo, points[i].x, points[i].y);
|
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
if (numPoints == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsurePathBuilder();
|
|
|
|
|
|
|
|
mPathBuilder->MoveTo(ToPoint(points[0]));
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 1; i < numPoints; i++) {
|
2012-01-04 23:17:51 -08:00
|
|
|
mPathBuilder->LineTo(ToPoint(points[i]));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::DrawSurface(gfxASurface *surface, const gfxSize& size)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_save(mCairo);
|
|
|
|
cairo_set_source_surface(mCairo, surface->CairoSurface(), 0, 0);
|
|
|
|
cairo_new_path(mCairo);
|
|
|
|
|
|
|
|
// pixel-snap this
|
2011-10-17 07:59:28 -07:00
|
|
|
Rectangle(gfxRect(gfxPoint(0.0, 0.0), size), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
cairo_fill(mCairo);
|
|
|
|
cairo_restore(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-05-21 08:27:32 -07:00
|
|
|
// Lifetime needs to be limited here since we may wrap surface's data.
|
2012-01-04 23:17:51 -08:00
|
|
|
RefPtr<SourceSurface> surf =
|
|
|
|
gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mDT, surface);
|
|
|
|
|
2013-10-24 07:35:29 -07:00
|
|
|
if (!surf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
Rect rect(0, 0, Float(size.width), Float(size.height));
|
|
|
|
rect.Intersect(Rect(0, 0, Float(surf->GetSize().width), Float(surf->GetSize().height)));
|
|
|
|
|
|
|
|
// XXX - Should fix pixel snapping.
|
|
|
|
mDT->DrawSurface(surf, rect, rect);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// transform stuff
|
|
|
|
void
|
|
|
|
gfxContext::Translate(const gfxPoint& pt)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_translate(mCairo, pt.x, pt.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix newMatrix = mTransform;
|
2012-05-21 22:29:19 -07:00
|
|
|
|
|
|
|
ChangeTransform(newMatrix.Translate(Float(pt.x), Float(pt.y)));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Scale(gfxFloat x, gfxFloat y)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_scale(mCairo, x, y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix newMatrix = mTransform;
|
2012-05-21 22:29:19 -07:00
|
|
|
|
|
|
|
ChangeTransform(newMatrix.Scale(Float(x), Float(y)));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Rotate(gfxFloat angle)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_rotate(mCairo, angle);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
Matrix rotation = Matrix::Rotation(Float(angle));
|
2012-09-27 14:00:51 -07:00
|
|
|
ChangeTransform(rotation * mTransform);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Multiply(const gfxMatrix& matrix)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
const cairo_matrix_t& mat = reinterpret_cast<const cairo_matrix_t&>(matrix);
|
|
|
|
cairo_transform(mCairo, &mat);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
ChangeTransform(ToMatrix(matrix) * mTransform);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-09-04 17:15:52 -07:00
|
|
|
void
|
|
|
|
gfxContext::MultiplyAndNudgeToIntegers(const gfxMatrix& matrix)
|
|
|
|
{
|
|
|
|
if (mCairo) {
|
|
|
|
const cairo_matrix_t& mat = reinterpret_cast<const cairo_matrix_t&>(matrix);
|
|
|
|
cairo_transform(mCairo, &mat);
|
|
|
|
// XXX nudging to integers not currently supported for Thebes
|
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix transform = ToMatrix(matrix) * mTransform;
|
2012-09-04 17:15:52 -07:00
|
|
|
transform.NudgeToIntegers();
|
|
|
|
ChangeTransform(transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxContext::SetMatrix(const gfxMatrix& matrix)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
const cairo_matrix_t& mat = reinterpret_cast<const cairo_matrix_t&>(matrix);
|
|
|
|
cairo_set_matrix(mCairo, &mat);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 17:03:20 -07:00
|
|
|
ChangeTransform(ToMatrix(matrix));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::IdentityMatrix()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_identity_matrix(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-05-21 22:29:19 -07:00
|
|
|
ChangeTransform(Matrix());
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxMatrix
|
|
|
|
gfxContext::CurrentMatrix() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mCairo, &mat);
|
|
|
|
return gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat));
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
return ThebesMatrix(mTransform);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-15 14:08:08 -07:00
|
|
|
void
|
|
|
|
gfxContext::NudgeCurrentMatrixToIntegers()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2010-07-15 14:08:08 -07:00
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mCairo, &mat);
|
2010-09-23 16:06:27 -07:00
|
|
|
gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat)).NudgeToIntegers();
|
2010-07-15 14:08:08 -07:00
|
|
|
cairo_set_matrix(mCairo, &mat);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
gfxMatrix matrix = ThebesMatrix(mTransform);
|
|
|
|
matrix.NudgeToIntegers();
|
2012-09-27 14:00:51 -07:00
|
|
|
ChangeTransform(ToMatrix(matrix));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2010-07-15 14:08:08 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPoint
|
|
|
|
gfxContext::DeviceToUser(const gfxPoint& point) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPoint ret = point;
|
|
|
|
cairo_device_to_user(mCairo, &ret.x, &ret.y);
|
|
|
|
return ret;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix matrix = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
matrix.Invert();
|
|
|
|
|
|
|
|
return ThebesPoint(matrix * ToPoint(point));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxSize
|
|
|
|
gfxContext::DeviceToUser(const gfxSize& size) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxSize ret = size;
|
|
|
|
cairo_device_to_user_distance(mCairo, &ret.width, &ret.height);
|
|
|
|
return ret;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix matrix = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
matrix.Invert();
|
|
|
|
|
|
|
|
return ThebesSize(matrix * ToSize(size));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect
|
|
|
|
gfxContext::DeviceToUser(const gfxRect& rect) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxRect ret = rect;
|
2011-04-18 20:07:21 -07:00
|
|
|
cairo_device_to_user(mCairo, &ret.x, &ret.y);
|
|
|
|
cairo_device_to_user_distance(mCairo, &ret.width, &ret.height);
|
2007-03-22 10:30:00 -07:00
|
|
|
return ret;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix matrix = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
matrix.Invert();
|
|
|
|
|
|
|
|
return ThebesRect(matrix.TransformBounds(ToRect(rect)));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxPoint
|
|
|
|
gfxContext::UserToDevice(const gfxPoint& point) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxPoint ret = point;
|
|
|
|
cairo_user_to_device(mCairo, &ret.x, &ret.y);
|
|
|
|
return ret;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
return ThebesPoint(mTransform * ToPoint(point));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxSize
|
|
|
|
gfxContext::UserToDevice(const gfxSize& size) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxSize ret = size;
|
|
|
|
cairo_user_to_device_distance(mCairo, &ret.width, &ret.height);
|
|
|
|
return ret;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
const Matrix &matrix = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
2013-08-27 16:36:02 -07:00
|
|
|
gfxSize newSize;
|
|
|
|
newSize.width = size.width * matrix._11 + size.height * matrix._12;
|
|
|
|
newSize.height = size.width * matrix._21 + size.height * matrix._22;
|
2012-01-04 23:17:51 -08:00
|
|
|
return newSize;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect
|
|
|
|
gfxContext::UserToDevice(const gfxRect& rect) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2011-04-18 20:07:21 -07:00
|
|
|
double xmin = rect.X(), ymin = rect.Y(), xmax = rect.XMost(), ymax = rect.YMost();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
double x[3], y[3];
|
|
|
|
x[0] = xmin; y[0] = ymax;
|
|
|
|
x[1] = xmax; y[1] = ymax;
|
|
|
|
x[2] = xmax; y[2] = ymin;
|
|
|
|
|
|
|
|
cairo_user_to_device(mCairo, &xmin, &ymin);
|
|
|
|
xmax = xmin;
|
|
|
|
ymax = ymin;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
cairo_user_to_device(mCairo, &x[i], &y[i]);
|
2013-01-15 04:22:03 -08:00
|
|
|
xmin = std::min(xmin, x[i]);
|
|
|
|
xmax = std::max(xmax, x[i]);
|
|
|
|
ymin = std::min(ymin, y[i]);
|
|
|
|
ymax = std::max(ymax, y[i]);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
const Matrix &matrix = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
return ThebesRect(matrix.TransformBounds(ToRect(rect)));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
|
|
|
gfxContext::UserToDevicePixelSnapped(gfxRect& rect, bool ignoreScale) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (GetFlags() & FLAG_DISABLE_SNAPPING)
|
|
|
|
return false;
|
2008-01-06 16:50:18 -08:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
// if we're not at 1.0 scale, don't snap, unless we're
|
|
|
|
// ignoring the scale. If we're not -just- a scale,
|
|
|
|
// never snap.
|
|
|
|
const gfxFloat epsilon = 0.0000001;
|
2010-10-19 16:06:00 -07:00
|
|
|
#define WITHIN_E(a,b) (fabs((a)-(b)) < epsilon)
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mCairo, &mat);
|
2010-06-27 17:29:57 -07:00
|
|
|
if (!ignoreScale &&
|
2010-10-19 16:06:00 -07:00
|
|
|
(!WITHIN_E(mat.xx,1.0) || !WITHIN_E(mat.yy,1.0) ||
|
2012-01-04 23:17:51 -08:00
|
|
|
!WITHIN_E(mat.xy,0.0) || !WITHIN_E(mat.yx,0.0)))
|
|
|
|
return false;
|
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
if (!ignoreScale &&
|
|
|
|
(!WITHIN_E(mat._11,1.0) || !WITHIN_E(mat._22,1.0) ||
|
|
|
|
!WITHIN_E(mat._12,0.0) || !WITHIN_E(mat._21,0.0)))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2010-10-19 16:06:00 -07:00
|
|
|
#undef WITHIN_E
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
gfxPoint p1 = UserToDevice(rect.TopLeft());
|
|
|
|
gfxPoint p2 = UserToDevice(rect.TopRight());
|
|
|
|
gfxPoint p3 = UserToDevice(rect.BottomRight());
|
|
|
|
|
|
|
|
// Check that the rectangle is axis-aligned. For an axis-aligned rectangle,
|
|
|
|
// two opposite corners define the entire rectangle. So check if
|
|
|
|
// the axis-aligned rectangle with opposite corners p1 and p3
|
|
|
|
// define an axis-aligned rectangle whose other corners are p2 and p4.
|
|
|
|
// We actually only need to check one of p2 and p4, since an affine
|
|
|
|
// transform maps parallelograms to parallelograms.
|
|
|
|
if (p2 == gfxPoint(p1.x, p3.y) || p2 == gfxPoint(p3.x, p1.y)) {
|
|
|
|
p1.Round();
|
|
|
|
p3.Round();
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
rect.MoveTo(gfxPoint(std::min(p1.x, p3.x), std::min(p1.y, p3.y)));
|
|
|
|
rect.SizeTo(gfxSize(std::max(p1.x, p3.x) - rect.X(),
|
|
|
|
std::max(p1.y, p3.y) - rect.Y()));
|
2012-01-04 23:17:51 -08:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
|
|
|
gfxContext::UserToDevicePixelSnapped(gfxPoint& pt, bool ignoreScale) const
|
2008-11-04 14:01:21 -08:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (GetFlags() & FLAG_DISABLE_SNAPPING)
|
|
|
|
return false;
|
2008-11-04 14:01:21 -08:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
// if we're not at 1.0 scale, don't snap, unless we're
|
|
|
|
// ignoring the scale. If we're not -just- a scale,
|
|
|
|
// never snap.
|
|
|
|
const gfxFloat epsilon = 0.0000001;
|
|
|
|
#define WITHIN_E(a,b) (fabs((a)-(b)) < epsilon)
|
|
|
|
if (mCairo) {
|
2008-11-04 14:01:21 -08:00
|
|
|
cairo_matrix_t mat;
|
|
|
|
cairo_get_matrix(mCairo, &mat);
|
2012-01-04 23:17:51 -08:00
|
|
|
if (!ignoreScale &&
|
|
|
|
(!WITHIN_E(mat.xx,1.0) || !WITHIN_E(mat.yy,1.0) ||
|
|
|
|
!WITHIN_E(mat.xy,0.0) || !WITHIN_E(mat.yx,0.0)))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
if (!ignoreScale &&
|
|
|
|
(!WITHIN_E(mat._11,1.0) || !WITHIN_E(mat._22,1.0) ||
|
|
|
|
!WITHIN_E(mat._12,0.0) || !WITHIN_E(mat._21,0.0)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#undef WITHIN_E
|
2008-11-04 14:01:21 -08:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
pt = UserToDevice(pt);
|
|
|
|
pt.Round();
|
|
|
|
return true;
|
2008-11-04 14:01:21 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
gfxContext::PixelSnappedRectangleAndSetPattern(const gfxRect& rect,
|
|
|
|
gfxPattern *pattern)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
gfxRect r(rect);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
// Bob attempts to pixel-snap the rectangle, and returns true if
|
|
|
|
// the snapping succeeds. If it does, we need to set up an
|
|
|
|
// identity matrix, because the rectangle given back is in device
|
|
|
|
// coordinates.
|
|
|
|
//
|
|
|
|
// We then have to call a translate to dr.pos afterwards, to make
|
|
|
|
// sure the image lines up in the right place with our pixel
|
|
|
|
// snapped rectangle.
|
|
|
|
//
|
|
|
|
// If snapping wasn't successful, we just translate to where the
|
|
|
|
// pattern would normally start (in app coordinates) and do the
|
|
|
|
// same thing.
|
|
|
|
Rectangle(r, true);
|
|
|
|
SetPattern(pattern);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetAntialiasMode(AntialiasMode mode)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mode == MODE_ALIASED) {
|
|
|
|
cairo_set_antialias(mCairo, CAIRO_ANTIALIAS_NONE);
|
|
|
|
} else if (mode == MODE_COVERAGE) {
|
|
|
|
cairo_set_antialias(mCairo, CAIRO_ANTIALIAS_DEFAULT);
|
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
if (mode == MODE_ALIASED) {
|
|
|
|
CurrentState().aaMode = AA_NONE;
|
|
|
|
} else if (mode == MODE_COVERAGE) {
|
|
|
|
CurrentState().aaMode = AA_SUBPIXEL;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext::AntialiasMode
|
|
|
|
gfxContext::CurrentAntialiasMode() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_antialias_t aa = cairo_get_antialias(mCairo);
|
|
|
|
if (aa == CAIRO_ANTIALIAS_NONE)
|
|
|
|
return MODE_ALIASED;
|
|
|
|
return MODE_COVERAGE;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
if (CurrentState().aaMode == AA_NONE) {
|
|
|
|
return MODE_ALIASED;
|
|
|
|
}
|
|
|
|
return MODE_COVERAGE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetDash(gfxLineType ltype)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
static double dash[] = {5.0, 5.0};
|
|
|
|
static double dot[] = {1.0, 1.0};
|
|
|
|
|
|
|
|
switch (ltype) {
|
|
|
|
case gfxLineDashed:
|
|
|
|
SetDash(dash, 2, 0.0);
|
|
|
|
break;
|
|
|
|
case gfxLineDotted:
|
|
|
|
SetDash(dot, 2, 0.0);
|
|
|
|
break;
|
|
|
|
case gfxLineSolid:
|
|
|
|
default:
|
2012-07-30 07:20:58 -07:00
|
|
|
SetDash(nullptr, 0, 0.0);
|
2012-01-04 23:17:51 -08:00
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetDash(gfxFloat *dashes, int ndash, gfxFloat offset)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_dash(mCairo, dashes, ndash, offset);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
|
|
|
|
state.dashPattern.SetLength(ndash);
|
|
|
|
for (int i = 0; i < ndash; i++) {
|
|
|
|
state.dashPattern[i] = Float(dashes[i]);
|
|
|
|
}
|
|
|
|
state.strokeOptions.mDashLength = ndash;
|
|
|
|
state.strokeOptions.mDashOffset = Float(offset);
|
2013-07-31 08:44:31 -07:00
|
|
|
state.strokeOptions.mDashPattern = ndash ? state.dashPattern.Elements()
|
|
|
|
: nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-06-29 14:34:58 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
gfxContext::CurrentDash(FallibleTArray<gfxFloat>& dashes, gfxFloat* offset) const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2011-06-29 14:34:58 -07:00
|
|
|
int count = cairo_get_dash_count(mCairo);
|
|
|
|
if (count <= 0 || !dashes.SetLength(count)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
cairo_get_dash(mCairo, dashes.Elements(), offset);
|
|
|
|
return true;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
const AzureState &state = CurrentState();
|
|
|
|
int count = state.strokeOptions.mDashLength;
|
|
|
|
|
|
|
|
if (count <= 0 || !dashes.SetLength(count)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
dashes[i] = state.dashPattern[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
*offset = state.strokeOptions.mDashOffset;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-29 14:34:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFloat
|
|
|
|
gfxContext::CurrentDashOffset() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2011-06-29 14:34:58 -07:00
|
|
|
if (cairo_get_dash_count(mCairo) <= 0) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
gfxFloat offset;
|
2013-07-31 08:44:31 -07:00
|
|
|
cairo_get_dash(mCairo, nullptr, &offset);
|
2011-06-29 14:34:58 -07:00
|
|
|
return offset;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return CurrentState().strokeOptions.mDashOffset;
|
|
|
|
}
|
2011-06-29 14:34:58 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetLineWidth(gfxFloat width)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_line_width(mCairo, width);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
CurrentState().strokeOptions.mLineWidth = Float(width);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFloat
|
|
|
|
gfxContext::CurrentLineWidth() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return cairo_get_line_width(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return CurrentState().strokeOptions.mLineWidth;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetOperator(GraphicsOperator op)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-11-29 14:19:53 -08:00
|
|
|
if (mFlags & FLAG_SIMPLIFY_OPERATORS) {
|
2007-11-29 12:06:56 -08:00
|
|
|
if (op != OPERATOR_SOURCE &&
|
|
|
|
op != OPERATOR_CLEAR &&
|
|
|
|
op != OPERATOR_OVER)
|
|
|
|
op = OPERATOR_OVER;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_operator(mCairo, (cairo_operator_t)op);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
if (op == OPERATOR_CLEAR) {
|
|
|
|
CurrentState().opIsClear = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CurrentState().opIsClear = false;
|
|
|
|
CurrentState().op = CompositionOpForOp(op);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext::GraphicsOperator
|
|
|
|
gfxContext::CurrentOperator() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return (GraphicsOperator)cairo_get_operator(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return ThebesOp(CurrentState().op);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetLineCap(GraphicsLineCap cap)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_line_cap(mCairo, (cairo_line_cap_t)cap);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
CurrentState().strokeOptions.mLineCap = ToCapStyle(cap);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext::GraphicsLineCap
|
|
|
|
gfxContext::CurrentLineCap() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return (GraphicsLineCap)cairo_get_line_cap(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return ThebesLineCap(CurrentState().strokeOptions.mLineCap);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetLineJoin(GraphicsLineJoin join)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_line_join(mCairo, (cairo_line_join_t)join);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
CurrentState().strokeOptions.mLineJoin = ToJoinStyle(join);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext::GraphicsLineJoin
|
|
|
|
gfxContext::CurrentLineJoin() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return (GraphicsLineJoin)cairo_get_line_join(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return ThebesLineJoin(CurrentState().strokeOptions.mLineJoin);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetMiterLimit(gfxFloat limit)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_miter_limit(mCairo, limit);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
CurrentState().strokeOptions.mMiterLimit = Float(limit);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFloat
|
|
|
|
gfxContext::CurrentMiterLimit() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return cairo_get_miter_limit(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return CurrentState().strokeOptions.mMiterLimit;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetFillRule(FillRule rule)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_fill_rule(mCairo, (cairo_fill_rule_t)rule);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
CurrentState().fillRule = rule == FILL_RULE_WINDING ? FILL_WINDING : FILL_EVEN_ODD;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext::FillRule
|
|
|
|
gfxContext::CurrentFillRule() const
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return (FillRule)cairo_get_fill_rule(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return FILL_RULE_WINDING;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// clipping
|
|
|
|
void
|
|
|
|
gfxContext::Clip(const gfxRect& rect)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_new_path(mCairo);
|
2011-04-18 20:07:21 -07:00
|
|
|
cairo_rectangle(mCairo, rect.X(), rect.Y(), rect.Width(), rect.Height());
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_clip(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-07-31 08:44:31 -07:00
|
|
|
AzureState::PushedClip clip = { nullptr, ToRect(rect), mTransform };
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().pushedClips.AppendElement(clip);
|
|
|
|
mDT->PushClipRect(ToRect(rect));
|
|
|
|
NewPath();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Clip()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_clip_preserve(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-12-13 07:34:51 -08:00
|
|
|
if (mPathIsRect) {
|
|
|
|
MOZ_ASSERT(!mTransformChanged);
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
AzureState::PushedClip clip = { nullptr, mRect, mTransform };
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().pushedClips.AppendElement(clip);
|
|
|
|
mDT->PushClipRect(mRect);
|
|
|
|
} else {
|
|
|
|
EnsurePath();
|
|
|
|
mDT->PushClip(mPath);
|
2012-09-27 14:00:51 -07:00
|
|
|
AzureState::PushedClip clip = { mPath, Rect(), mTransform };
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().pushedClips.AppendElement(clip);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::ResetClip()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_reset_clip(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
for (int i = mStateStack.Length() - 1; i >= 0; i--) {
|
|
|
|
for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
|
|
|
|
mDT->PopClip();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStateStack[i].clipWasReset) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrentState().pushedClips.Clear();
|
|
|
|
CurrentState().clipWasReset = true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::UpdateSurfaceClip()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NewPath();
|
2010-04-05 19:28:54 -07:00
|
|
|
// we paint an empty rectangle to ensure the clip is propagated to
|
|
|
|
// the destination surface
|
|
|
|
SetDeviceColor(gfxRGBA(0,0,0,0));
|
|
|
|
Rectangle(gfxRect(0,1,1,0));
|
2007-03-22 10:30:00 -07:00
|
|
|
Fill();
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-05-22 12:30:03 -07:00
|
|
|
gfxRect
|
|
|
|
gfxContext::GetClipExtents()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-05-22 12:30:03 -07:00
|
|
|
double xmin, ymin, xmax, ymax;
|
|
|
|
cairo_clip_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
|
|
|
|
return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-09-27 14:00:51 -07:00
|
|
|
Rect rect = GetAzureDeviceSpaceClipBounds();
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
if (rect.width == 0 || rect.height == 0) {
|
|
|
|
return gfxRect(0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
mat.Invert();
|
|
|
|
rect = mat.TransformBounds(rect);
|
|
|
|
|
|
|
|
return ThebesRect(rect);
|
|
|
|
}
|
2007-05-22 12:30:03 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-08-26 17:03:04 -07:00
|
|
|
gfxContext::ClipContainsRect(const gfxRect& aRect)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2011-08-26 17:03:04 -07:00
|
|
|
cairo_rectangle_list_t *clip =
|
|
|
|
cairo_copy_clip_rectangle_list(mCairo);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result = false;
|
2011-08-26 17:03:04 -07:00
|
|
|
|
|
|
|
if (clip->status == CAIRO_STATUS_SUCCESS) {
|
|
|
|
for (int i = 0; i < clip->num_rectangles; i++) {
|
|
|
|
gfxRect rect(clip->rectangles[i].x, clip->rectangles[i].y,
|
|
|
|
clip->rectangles[i].width, clip->rectangles[i].height);
|
|
|
|
if (rect.Contains(aRect)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
result = true;
|
2011-08-26 17:03:04 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
cairo_rectangle_list_destroy(clip);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
unsigned int lastReset = 0;
|
|
|
|
for (int i = mStateStack.Length() - 2; i > 0; i--) {
|
|
|
|
if (mStateStack[i].clipWasReset) {
|
|
|
|
lastReset = i;
|
2013-08-12 05:51:45 -07:00
|
|
|
break;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we always return false when the clip list contains a
|
|
|
|
// non-rectangular clip or a non-rectilinear transform, our 'total' clip
|
|
|
|
// is always a rectangle if we hit the end of this function.
|
|
|
|
Rect clipBounds(0, 0, Float(mDT->GetSize().width), Float(mDT->GetSize().height));
|
|
|
|
|
|
|
|
for (unsigned int i = lastReset; i < mStateStack.Length(); i++) {
|
|
|
|
for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
|
|
|
|
AzureState::PushedClip &clip = mStateStack[i].pushedClips[c];
|
|
|
|
if (clip.path || !clip.transform.IsRectilinear()) {
|
|
|
|
// Cairo behavior is we return false if the clip contains a non-
|
|
|
|
// rectangle.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
Rect clipRect = mTransform.TransformBounds(clip.rect);
|
|
|
|
|
|
|
|
clipBounds.IntersectRect(clipBounds, clipRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clipBounds.Contains(ToRect(aRect));
|
|
|
|
}
|
2011-08-26 17:03:04 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// rendering sources
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetColor(const gfxRGBA& c)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2013-09-06 20:08:36 -07:00
|
|
|
if (gfxPlatform::GetCMSMode() == eCMSMode_All) {
|
2007-07-23 15:02:17 -07:00
|
|
|
|
2008-09-10 19:21:03 -07:00
|
|
|
gfxRGBA cms;
|
2013-09-06 20:08:36 -07:00
|
|
|
qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
|
|
|
|
if (transform)
|
|
|
|
gfxPlatform::TransformPixel(c, cms, transform);
|
2008-09-10 19:21:03 -07:00
|
|
|
|
|
|
|
// Use the original alpha to avoid unnecessary float->byte->float
|
|
|
|
// conversion errors
|
|
|
|
cairo_set_source_rgba(mCairo, cms.r, cms.g, cms.b, c.a);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cairo_set_source_rgba(mCairo, c.r, c.g, c.b, c.a);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-07-31 08:44:31 -07:00
|
|
|
CurrentState().pattern = nullptr;
|
|
|
|
CurrentState().sourceSurfCairo = nullptr;
|
|
|
|
CurrentState().sourceSurface = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
2013-09-06 20:08:36 -07:00
|
|
|
if (gfxPlatform::GetCMSMode() == eCMSMode_All) {
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
gfxRGBA cms;
|
2013-09-06 20:08:36 -07:00
|
|
|
qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
|
|
|
|
if (transform)
|
|
|
|
gfxPlatform::TransformPixel(c, cms, transform);
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
// Use the original alpha to avoid unnecessary float->byte->float
|
|
|
|
// conversion errors
|
|
|
|
CurrentState().color = ToColor(cms);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CurrentState().color = ToColor(c);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-04-28 14:27:05 -07:00
|
|
|
void
|
|
|
|
gfxContext::SetDeviceColor(const gfxRGBA& c)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2008-04-28 14:27:05 -07:00
|
|
|
cairo_set_source_rgba(mCairo, c.r, c.g, c.b, c.a);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-07-31 08:44:31 -07:00
|
|
|
CurrentState().pattern = nullptr;
|
|
|
|
CurrentState().sourceSurfCairo = nullptr;
|
|
|
|
CurrentState().sourceSurface = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().color = ToColor(c);
|
|
|
|
}
|
2008-04-28 14:27:05 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-04-28 14:27:05 -07:00
|
|
|
gfxContext::GetDeviceColor(gfxRGBA& c)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return cairo_pattern_get_rgba(cairo_get_source(mCairo),
|
|
|
|
&c.r,
|
|
|
|
&c.g,
|
|
|
|
&c.b,
|
|
|
|
&c.a) == CAIRO_STATUS_SUCCESS;
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
if (CurrentState().sourceSurface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (CurrentState().pattern) {
|
|
|
|
gfxRGBA color;
|
|
|
|
return CurrentState().pattern->GetSolidColor(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
c = ThebesRGBA(CurrentState().color);
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetSource(gfxASurface *surface, const gfxPoint& offset)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2011-01-02 17:48:09 -08:00
|
|
|
NS_ASSERTION(surface->GetAllowUseAsSource(), "Surface not allowed to be used as source!");
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_source_surface(mCairo, surface->CairoSurface(), offset.x, offset.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
CurrentState().surfTransform = Matrix(1.0f, 0, 0, 1.0f, Float(offset.x), Float(offset.y));
|
2013-07-31 08:44:31 -07:00
|
|
|
CurrentState().pattern = nullptr;
|
2012-03-29 11:53:43 -07:00
|
|
|
CurrentState().patternTransformChanged = false;
|
2012-05-21 08:27:32 -07:00
|
|
|
// Keep the underlying cairo surface around while we keep the
|
|
|
|
// sourceSurface.
|
|
|
|
CurrentState().sourceSurfCairo = surface;
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().sourceSurface =
|
|
|
|
gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mDT, surface);
|
2013-10-24 07:35:29 -07:00
|
|
|
CurrentState().color = Color(0, 0, 0, 0);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::SetPattern(gfxPattern *pattern)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_set_source(mCairo, pattern->CairoPattern());
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-07-31 08:44:31 -07:00
|
|
|
CurrentState().sourceSurfCairo = nullptr;
|
|
|
|
CurrentState().sourceSurface = nullptr;
|
2012-03-29 11:53:43 -07:00
|
|
|
CurrentState().patternTransformChanged = false;
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().pattern = pattern;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxPattern>
|
|
|
|
gfxContext::GetPattern()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_pattern_t *pat = cairo_get_source(mCairo);
|
|
|
|
NS_ASSERTION(pat, "I was told this couldn't be null");
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
nsRefPtr<gfxPattern> wrapper;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pat)
|
|
|
|
wrapper = new gfxPattern(pat);
|
|
|
|
else
|
|
|
|
wrapper = new gfxPattern(gfxRGBA(0,0,0,0));
|
|
|
|
|
2013-04-22 04:15:59 -07:00
|
|
|
return wrapper.forget();
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
nsRefPtr<gfxPattern> pat;
|
|
|
|
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
if (state.pattern) {
|
|
|
|
pat = state.pattern;
|
|
|
|
} else if (state.sourceSurface) {
|
|
|
|
NS_ASSERTION(false, "Ugh, this isn't good.");
|
|
|
|
} else {
|
|
|
|
pat = new gfxPattern(ThebesRGBA(state.color));
|
|
|
|
}
|
|
|
|
return pat.forget();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// masking
|
|
|
|
void
|
|
|
|
gfxContext::Mask(gfxPattern *pattern)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_mask(mCairo, pattern->CairoPattern());
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2013-05-12 07:03:12 -07:00
|
|
|
if (pattern->Extend() == gfxPattern::EXTEND_NONE) {
|
|
|
|
// In this situation the mask will be fully transparent (i.e. nothing
|
|
|
|
// will be drawn) outside of the bounds of the surface. We can support
|
|
|
|
// that by clipping out drawing to that area.
|
2013-06-19 14:14:24 -07:00
|
|
|
Point offset;
|
|
|
|
if (pattern->IsAzure()) {
|
2013-05-12 07:03:12 -07:00
|
|
|
// This is an Azure pattern. i.e. this was the result of a PopGroup and
|
|
|
|
// then the extend mode was changed to EXTEND_NONE.
|
|
|
|
// XXX - We may need some additional magic here in theory to support
|
|
|
|
// device offsets in these patterns, but no problems have been observed
|
|
|
|
// yet because of this. And it would complicate things a little further.
|
2013-06-19 14:14:24 -07:00
|
|
|
offset = Point(0.f, 0.f);
|
|
|
|
} else if (pattern->GetType() == gfxPattern::PATTERN_SURFACE) {
|
|
|
|
nsRefPtr<gfxASurface> asurf = pattern->GetSurface();
|
|
|
|
gfxPoint deviceOffset = asurf->GetDeviceOffset();
|
|
|
|
offset = Point(-deviceOffset.x, -deviceOffset.y);
|
|
|
|
|
|
|
|
// this lets GetAzureSurface work
|
|
|
|
pattern->GetPattern(mDT);
|
2013-05-12 07:03:12 -07:00
|
|
|
}
|
|
|
|
|
2013-06-19 14:14:24 -07:00
|
|
|
if (pattern->IsAzure() || pattern->GetType() == gfxPattern::PATTERN_SURFACE) {
|
|
|
|
RefPtr<SourceSurface> mask = pattern->GetAzureSurface();
|
2013-06-21 11:55:24 -07:00
|
|
|
Matrix mat = ToMatrix(pattern->GetInverseMatrix());
|
2013-07-30 07:15:27 -07:00
|
|
|
Matrix old = mTransform;
|
2013-06-19 14:14:24 -07:00
|
|
|
// add in the inverse of the pattern transform so that when we
|
|
|
|
// MaskSurface we are transformed to the place matching the pattern transform
|
2013-07-30 07:15:27 -07:00
|
|
|
mat = mat * mTransform;
|
2013-05-12 07:03:12 -07:00
|
|
|
|
2013-06-19 14:14:24 -07:00
|
|
|
ChangeTransform(mat);
|
|
|
|
mDT->MaskSurface(GeneralPattern(this), mask, offset, DrawOptions(1.0f, CurrentState().op, CurrentState().aaMode));
|
|
|
|
ChangeTransform(old);
|
|
|
|
return;
|
2013-05-12 07:03:12 -07:00
|
|
|
}
|
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
mDT->Mask(GeneralPattern(this), *pattern->GetPattern(mDT), DrawOptions(1.0f, CurrentState().op, CurrentState().aaMode));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Mask(gfxASurface *surface, const gfxPoint& offset)
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("gfxContext", "Mask");
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_mask_surface(mCairo, surface->CairoSurface(), offset.x, offset.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-05-21 08:27:32 -07:00
|
|
|
// Lifetime needs to be limited here as we may simply wrap surface's data.
|
2012-01-04 23:17:51 -08:00
|
|
|
RefPtr<SourceSurface> sourceSurf =
|
|
|
|
gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mDT, surface);
|
|
|
|
|
2013-10-24 07:35:29 -07:00
|
|
|
if (!sourceSurf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
gfxPoint pt = surface->GetDeviceOffset();
|
2013-05-12 07:03:12 -07:00
|
|
|
|
|
|
|
// We clip here to bind to the mask surface bounds, see above.
|
2013-06-12 20:57:51 -07:00
|
|
|
mDT->MaskSurface(GeneralPattern(this),
|
|
|
|
sourceSurf,
|
|
|
|
Point(offset.x - pt.x, offset.y - pt.y),
|
2012-06-13 15:40:43 -07:00
|
|
|
DrawOptions(1.0f, CurrentState().op, CurrentState().aaMode));
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::Paint(gfxFloat alpha)
|
|
|
|
{
|
2013-03-15 21:47:02 -07:00
|
|
|
PROFILER_LABEL("gfxContext", "Paint");
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_paint_with_alpha(mCairo, alpha);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
|
2013-08-22 21:53:12 -07:00
|
|
|
if (state.sourceSurface && !state.sourceSurfCairo &&
|
|
|
|
!state.patternTransformChanged && !state.opIsClear)
|
|
|
|
{
|
|
|
|
// This is the case where a PopGroupToSource has been done and this
|
|
|
|
// paint is executed without changing the transform or the source.
|
|
|
|
Matrix oldMat = mDT->GetTransform();
|
|
|
|
|
|
|
|
IntSize surfSize = state.sourceSurface->GetSize();
|
|
|
|
|
|
|
|
Matrix mat;
|
|
|
|
mat.Translate(-state.deviceOffset.x, -state.deviceOffset.y);
|
|
|
|
mDT->SetTransform(mat);
|
|
|
|
|
|
|
|
mDT->DrawSurface(state.sourceSurface,
|
|
|
|
Rect(state.sourceSurfaceDeviceOffset, Size(surfSize.width, surfSize.height)),
|
|
|
|
Rect(Point(), Size(surfSize.width, surfSize.height)),
|
|
|
|
DrawSurfaceOptions(), DrawOptions(alpha, GetOp()));
|
|
|
|
mDT->SetTransform(oldMat);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
Matrix mat = mDT->GetTransform();
|
|
|
|
mat.Invert();
|
|
|
|
Rect paintRect = mat.TransformBounds(Rect(Point(0, 0), Size(mDT->GetSize())));
|
|
|
|
|
|
|
|
if (state.opIsClear) {
|
|
|
|
mDT->ClearRect(paintRect);
|
|
|
|
} else {
|
|
|
|
mDT->FillRect(paintRect, GeneralPattern(this),
|
|
|
|
DrawOptions(Float(alpha), GetOp()));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// groups
|
|
|
|
|
|
|
|
void
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxContext::PushGroup(gfxContentType content)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_push_group_with_content(mCairo, (cairo_content_t) content);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-10-03 11:18:19 -07:00
|
|
|
PushNewDT(content);
|
2012-09-27 14:00:51 -07:00
|
|
|
|
2012-10-03 11:18:19 -07:00
|
|
|
PushClipsToDT(mDT);
|
|
|
|
mDT->SetTransform(GetDTTransform());
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2011-01-02 17:48:08 -08:00
|
|
|
}
|
|
|
|
|
2011-01-02 17:48:08 -08:00
|
|
|
static gfxRect
|
|
|
|
GetRoundOutDeviceClipExtents(gfxContext* aCtx)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
gfxContextMatrixAutoSaveRestore save(aCtx);
|
|
|
|
aCtx->IdentityMatrix();
|
|
|
|
gfxRect r = aCtx->GetClipExtents();
|
|
|
|
r.RoundOut();
|
|
|
|
return r;
|
2011-01-02 17:48:08 -08:00
|
|
|
}
|
|
|
|
|
2011-01-16 12:23:31 -08:00
|
|
|
/**
|
|
|
|
* Copy the contents of aSrc to aDest, translated by aTranslation.
|
|
|
|
*/
|
2011-01-02 17:48:09 -08:00
|
|
|
static void
|
2011-01-16 12:23:31 -08:00
|
|
|
CopySurface(gfxASurface* aSrc, gfxASurface* aDest, const gfxPoint& aTranslation)
|
2011-01-02 17:48:09 -08:00
|
|
|
{
|
|
|
|
cairo_t *cr = cairo_create(aDest->CairoSurface());
|
2011-01-16 12:23:31 -08:00
|
|
|
cairo_set_source_surface(cr, aSrc->CairoSurface(), aTranslation.x, aTranslation.y);
|
2011-01-02 17:48:09 -08:00
|
|
|
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
|
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
}
|
|
|
|
|
2011-01-02 17:48:08 -08:00
|
|
|
void
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxContext::PushGroupAndCopyBackground(gfxContentType content)
|
2011-01-02 17:48:08 -08:00
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2013-09-24 13:45:13 -07:00
|
|
|
if (content == GFX_CONTENT_COLOR_ALPHA &&
|
2012-05-03 14:31:02 -07:00
|
|
|
!(GetFlags() & FLAG_DISABLE_COPY_BACKGROUND)) {
|
|
|
|
nsRefPtr<gfxASurface> s = CurrentSurface();
|
2013-09-24 13:45:13 -07:00
|
|
|
if ((s->GetAllowUseAsSource() || s->GetType() == gfxSurfaceTypeTee) &&
|
|
|
|
(s->GetContentType() == GFX_CONTENT_COLOR ||
|
2012-05-03 14:31:02 -07:00
|
|
|
s->GetOpaqueRect().Contains(GetRoundOutDeviceClipExtents(this)))) {
|
|
|
|
cairo_push_group_with_content(mCairo, CAIRO_CONTENT_COLOR);
|
|
|
|
nsRefPtr<gfxASurface> d = CurrentSurface();
|
|
|
|
|
2013-09-24 13:45:13 -07:00
|
|
|
if (d->GetType() == gfxSurfaceTypeTee) {
|
|
|
|
NS_ASSERTION(s->GetType() == gfxSurfaceTypeTee, "Mismatched types");
|
2012-05-03 14:31:02 -07:00
|
|
|
nsAutoTArray<nsRefPtr<gfxASurface>,2> ss;
|
|
|
|
nsAutoTArray<nsRefPtr<gfxASurface>,2> ds;
|
|
|
|
static_cast<gfxTeeSurface*>(s.get())->GetSurfaces(&ss);
|
|
|
|
static_cast<gfxTeeSurface*>(d.get())->GetSurfaces(&ds);
|
|
|
|
NS_ASSERTION(ss.Length() == ds.Length(), "Mismatched lengths");
|
|
|
|
gfxPoint translation = d->GetDeviceOffset() - s->GetDeviceOffset();
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < ss.Length(); ++i) {
|
2012-05-03 14:31:02 -07:00
|
|
|
CopySurface(ss[i], ds[i], translation);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CopySurface(s, d, gfxPoint(0, 0));
|
2011-01-02 17:48:08 -08:00
|
|
|
}
|
2012-05-03 14:31:02 -07:00
|
|
|
d->SetOpaqueRect(s->GetOpaqueRect());
|
|
|
|
return;
|
|
|
|
}
|
2011-01-02 17:48:08 -08:00
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-05-03 14:31:02 -07:00
|
|
|
IntRect clipExtents;
|
|
|
|
if (mDT->GetFormat() != FORMAT_B8G8R8X8) {
|
|
|
|
gfxRect clipRect = GetRoundOutDeviceClipExtents(this);
|
|
|
|
clipExtents = IntRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
|
|
|
|
}
|
2013-10-09 21:44:09 -07:00
|
|
|
if ((mDT->GetFormat() == FORMAT_B8G8R8X8 ||
|
|
|
|
mDT->GetOpaqueRect().Contains(clipExtents)) &&
|
|
|
|
!mDT->GetUserData(&sDontUseAsSourceKey)) {
|
2012-05-03 14:31:02 -07:00
|
|
|
DrawTarget *oldDT = mDT;
|
|
|
|
RefPtr<SourceSurface> source = mDT->Snapshot();
|
2012-09-27 14:00:51 -07:00
|
|
|
Point oldDeviceOffset = CurrentState().deviceOffset;
|
|
|
|
|
2013-09-24 13:45:13 -07:00
|
|
|
PushNewDT(GFX_CONTENT_COLOR);
|
2012-10-04 05:54:57 -07:00
|
|
|
|
|
|
|
Point offset = CurrentState().deviceOffset - oldDeviceOffset;
|
2012-05-03 14:31:02 -07:00
|
|
|
Rect surfRect(0, 0, Float(mDT->GetSize().width), Float(mDT->GetSize().height));
|
2012-09-27 14:00:51 -07:00
|
|
|
Rect sourceRect = surfRect;
|
|
|
|
sourceRect.x += offset.x;
|
|
|
|
sourceRect.y += offset.y;
|
2012-10-03 11:18:19 -07:00
|
|
|
|
2012-10-01 15:00:15 -07:00
|
|
|
mDT->SetTransform(Matrix());
|
2012-09-27 14:00:51 -07:00
|
|
|
mDT->DrawSurface(source, surfRect, sourceRect);
|
2012-05-03 14:31:02 -07:00
|
|
|
mDT->SetOpaqueRect(oldDT->GetOpaqueRect());
|
2012-10-03 11:18:19 -07:00
|
|
|
|
|
|
|
PushClipsToDT(mDT);
|
|
|
|
mDT->SetTransform(GetDTTransform());
|
2012-05-03 14:31:02 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2012-05-03 14:31:02 -07:00
|
|
|
PushGroup(content);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxPattern>
|
|
|
|
gfxContext::PopGroup()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_pattern_t *pat = cairo_pop_group(mCairo);
|
2013-04-22 04:15:59 -07:00
|
|
|
nsRefPtr<gfxPattern> wrapper = new gfxPattern(pat);
|
2007-07-23 01:54:52 -07:00
|
|
|
cairo_pattern_destroy(pat);
|
2013-04-22 04:15:59 -07:00
|
|
|
return wrapper.forget();
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
RefPtr<SourceSurface> src = mDT->Snapshot();
|
2012-09-27 14:00:51 -07:00
|
|
|
Point deviceOffset = CurrentState().deviceOffset;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
Restore();
|
|
|
|
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
mat.Invert();
|
2012-09-27 14:00:51 -07:00
|
|
|
|
|
|
|
Matrix deviceOffsetTranslation;
|
|
|
|
deviceOffsetTranslation.Translate(deviceOffset.x, deviceOffset.y);
|
|
|
|
|
|
|
|
nsRefPtr<gfxPattern> pat = new gfxPattern(src, deviceOffsetTranslation * mat);
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
return pat.forget();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::PopGroupToSource()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
cairo_pop_group_to_source(mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
RefPtr<SourceSurface> src = mDT->Snapshot();
|
2012-09-27 14:00:51 -07:00
|
|
|
Point deviceOffset = CurrentState().deviceOffset;
|
2012-01-04 23:17:51 -08:00
|
|
|
Restore();
|
2013-07-31 08:44:31 -07:00
|
|
|
CurrentState().sourceSurfCairo = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
CurrentState().sourceSurface = src;
|
2013-08-22 21:53:12 -07:00
|
|
|
CurrentState().sourceSurfaceDeviceOffset = deviceOffset;
|
2013-07-31 08:44:31 -07:00
|
|
|
CurrentState().pattern = nullptr;
|
2012-03-29 11:53:43 -07:00
|
|
|
CurrentState().patternTransformChanged = false;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
mat.Invert();
|
2012-09-27 14:00:51 -07:00
|
|
|
|
|
|
|
Matrix deviceOffsetTranslation;
|
|
|
|
deviceOffsetTranslation.Translate(deviceOffset.x, deviceOffset.y);
|
|
|
|
CurrentState().surfTransform = deviceOffsetTranslation * mat;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxContext::PointInFill(const gfxPoint& pt)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return cairo_in_fill(mCairo, pt.x, pt.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return mPath->ContainsPoint(ToPoint(pt), mTransform);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxContext::PointInStroke(const gfxPoint& pt)
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return cairo_in_stroke(mCairo, pt.x, pt.y);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
2012-10-29 08:54:53 -07:00
|
|
|
return mPath->StrokeContainsPoint(CurrentState().strokeOptions,
|
|
|
|
ToPoint(pt),
|
|
|
|
mTransform);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-01-27 06:55:53 -08:00
|
|
|
gfxRect
|
|
|
|
gfxContext::GetUserPathExtent()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2008-01-27 06:55:53 -08:00
|
|
|
double xmin, ymin, xmax, ymax;
|
|
|
|
cairo_path_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
|
|
|
|
return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return ThebesRect(mPath->GetBounds());
|
|
|
|
}
|
2008-01-27 06:55:53 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxRect
|
|
|
|
gfxContext::GetUserFillExtent()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
double xmin, ymin, xmax, ymax;
|
|
|
|
cairo_fill_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
|
|
|
|
return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return ThebesRect(mPath->GetBounds());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect
|
|
|
|
gfxContext::GetUserStrokeExtent()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
2007-03-22 10:30:00 -07:00
|
|
|
double xmin, ymin, xmax, ymax;
|
|
|
|
cairo_stroke_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
|
|
|
|
return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
return ThebesRect(mPath->GetStrokedBounds(CurrentState().strokeOptions, mTransform));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-04-16 13:18:32 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-01-09 14:53:59 -08:00
|
|
|
gfxContext::HasError()
|
|
|
|
{
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mCairo) {
|
|
|
|
return cairo_status(mCairo) != CAIRO_STATUS_SUCCESS;
|
|
|
|
} else {
|
|
|
|
// As far as this is concerned, an Azure context is never in error.
|
2012-02-14 08:17:19 -08:00
|
|
|
return false;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2008-01-09 14:53:59 -08:00
|
|
|
}
|
2008-07-23 10:25:00 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::RoundedRectangle(const gfxRect& rect,
|
|
|
|
const gfxCornerSizes& corners,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool draw_clockwise)
|
2008-07-23 10:25:00 -07:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// For CW drawing, this looks like:
|
2010-04-27 09:15:01 -07:00
|
|
|
//
|
2008-07-23 10:25:00 -07:00
|
|
|
// ...******0** 1 C
|
|
|
|
// ****
|
|
|
|
// *** 2
|
|
|
|
// **
|
|
|
|
// *
|
|
|
|
// *
|
|
|
|
// 3
|
|
|
|
// *
|
|
|
|
// *
|
|
|
|
//
|
2008-12-04 21:01:37 -08:00
|
|
|
// Where 0, 1, 2, 3 are the control points of the Bezier curve for
|
|
|
|
// the corner, and C is the actual corner point.
|
2008-07-23 10:25:00 -07:00
|
|
|
//
|
|
|
|
// At the start of the loop, the current point is assumed to be
|
|
|
|
// the point adjacent to the top left corner on the top
|
|
|
|
// horizontal. Note that corner indices start at the top left and
|
|
|
|
// continue clockwise, whereas in our loop i = 0 refers to the top
|
|
|
|
// right corner.
|
|
|
|
//
|
2008-12-04 21:01:37 -08:00
|
|
|
// When going CCW, the control points are swapped, and the first
|
|
|
|
// corner that's drawn is the top left (along with the top segment).
|
|
|
|
//
|
|
|
|
// There is considerable latitude in how one chooses the four
|
|
|
|
// control points for a Bezier curve approximation to an ellipse.
|
|
|
|
// For the overall path to be continuous and show no corner at the
|
|
|
|
// endpoints of the arc, points 0 and 3 must be at the ends of the
|
|
|
|
// straight segments of the rectangle; points 0, 1, and C must be
|
|
|
|
// collinear; and points 3, 2, and C must also be collinear. This
|
|
|
|
// leaves only two free parameters: the ratio of the line segments
|
|
|
|
// 01 and 0C, and the ratio of the line segments 32 and 3C. See
|
|
|
|
// the following papers for extensive discussion of how to choose
|
|
|
|
// these ratios:
|
|
|
|
//
|
|
|
|
// Dokken, Tor, et al. "Good approximation of circles by
|
|
|
|
// curvature-continuous Bezier curves." Computer-Aided
|
|
|
|
// Geometric Design 7(1990) 33--41.
|
|
|
|
// Goldapp, Michael. "Approximation of circular arcs by cubic
|
|
|
|
// polynomials." Computer-Aided Geometric Design 8(1991) 227--238.
|
|
|
|
// Maisonobe, Luc. "Drawing an elliptical arc using polylines,
|
|
|
|
// quadratic, or cubic Bezier curves."
|
|
|
|
// http://www.spaceroots.org/documents/ellipse/elliptical-arc.pdf
|
|
|
|
//
|
|
|
|
// We follow the approach in section 2 of Goldapp (least-error,
|
|
|
|
// Hermite-type approximation) and make both ratios equal to
|
|
|
|
//
|
|
|
|
// 2 2 + n - sqrt(2n + 28)
|
|
|
|
// alpha = - * ---------------------
|
|
|
|
// 3 n - 4
|
|
|
|
//
|
|
|
|
// where n = 3( cbrt(sqrt(2)+1) - cbrt(sqrt(2)-1) ).
|
|
|
|
//
|
|
|
|
// This is the result of Goldapp's equation (10b) when the angle
|
|
|
|
// swept out by the arc is pi/2, and the parameter "a-bar" is the
|
|
|
|
// expression given immediately below equation (21).
|
|
|
|
//
|
|
|
|
// Using this value, the maximum radial error for a circle, as a
|
|
|
|
// fraction of the radius, is on the order of 0.2 x 10^-3.
|
|
|
|
// Neither Dokken nor Goldapp discusses error for a general
|
|
|
|
// ellipse; Maisonobe does, but his choice of control points
|
|
|
|
// follows different constraints, and Goldapp's expression for
|
|
|
|
// 'alpha' gives much smaller radial error, even for very flat
|
|
|
|
// ellipses, than Maisonobe's equivalent.
|
|
|
|
//
|
|
|
|
// For the various corners and for each axis, the sign of this
|
|
|
|
// constant changes, or it might be 0 -- it's multiplied by the
|
|
|
|
// appropriate multiplier from the list before using.
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
if (mCairo) {
|
2008-12-04 21:01:37 -08:00
|
|
|
const gfxFloat alpha = 0.55191497064665766025;
|
2008-07-23 10:25:00 -07:00
|
|
|
|
|
|
|
typedef struct { gfxFloat a, b; } twoFloats;
|
|
|
|
|
|
|
|
twoFloats cwCornerMults[4] = { { -1, 0 },
|
|
|
|
{ 0, -1 },
|
|
|
|
{ +1, 0 },
|
|
|
|
{ 0, +1 } };
|
|
|
|
twoFloats ccwCornerMults[4] = { { +1, 0 },
|
|
|
|
{ 0, -1 },
|
|
|
|
{ -1, 0 },
|
|
|
|
{ 0, +1 } };
|
|
|
|
|
|
|
|
twoFloats *cornerMults = draw_clockwise ? cwCornerMults : ccwCornerMults;
|
|
|
|
|
|
|
|
gfxPoint pc, p0, p1, p2, p3;
|
|
|
|
|
|
|
|
if (draw_clockwise)
|
2011-04-18 20:07:21 -07:00
|
|
|
cairo_move_to(mCairo, rect.X() + corners[NS_CORNER_TOP_LEFT].width, rect.Y());
|
2008-07-23 10:25:00 -07:00
|
|
|
else
|
2011-04-18 20:07:21 -07:00
|
|
|
cairo_move_to(mCairo, rect.X() + rect.Width() - corners[NS_CORNER_TOP_RIGHT].width, rect.Y());
|
2008-07-23 10:25:00 -07:00
|
|
|
|
2010-04-27 09:15:02 -07:00
|
|
|
NS_FOR_CSS_CORNERS(i) {
|
2008-07-23 10:25:00 -07:00
|
|
|
// the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw)
|
2010-04-27 09:15:02 -07:00
|
|
|
mozilla::css::Corner c = mozilla::css::Corner(draw_clockwise ? ((i+1) % 4) : ((4-i) % 4));
|
2008-07-23 10:25:00 -07:00
|
|
|
|
|
|
|
// i+2 and i+3 respectively. These are used to index into the corner
|
|
|
|
// multiplier table, and were deduced by calculating out the long form
|
|
|
|
// of each corner and finding a pattern in the signs and values.
|
|
|
|
int i2 = (i+2) % 4;
|
|
|
|
int i3 = (i+3) % 4;
|
|
|
|
|
2010-04-27 09:15:02 -07:00
|
|
|
pc = rect.AtCorner(c);
|
2008-07-23 10:25:00 -07:00
|
|
|
|
|
|
|
if (corners[c].width > 0.0 && corners[c].height > 0.0) {
|
|
|
|
p0.x = pc.x + cornerMults[i].a * corners[c].width;
|
|
|
|
p0.y = pc.y + cornerMults[i].b * corners[c].height;
|
|
|
|
|
|
|
|
p3.x = pc.x + cornerMults[i3].a * corners[c].width;
|
|
|
|
p3.y = pc.y + cornerMults[i3].b * corners[c].height;
|
|
|
|
|
|
|
|
p1.x = p0.x + alpha * cornerMults[i2].a * corners[c].width;
|
|
|
|
p1.y = p0.y + alpha * cornerMults[i2].b * corners[c].height;
|
|
|
|
|
|
|
|
p2.x = p3.x - alpha * cornerMults[i3].a * corners[c].width;
|
|
|
|
p2.y = p3.y - alpha * cornerMults[i3].b * corners[c].height;
|
|
|
|
|
|
|
|
cairo_line_to (mCairo, p0.x, p0.y);
|
|
|
|
cairo_curve_to (mCairo,
|
|
|
|
p1.x, p1.y,
|
|
|
|
p2.x, p2.y,
|
|
|
|
p3.x, p3.y);
|
|
|
|
} else {
|
|
|
|
cairo_line_to (mCairo, pc.x, pc.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_close_path (mCairo);
|
2012-01-04 23:17:51 -08:00
|
|
|
} else {
|
|
|
|
EnsurePathBuilder();
|
2013-11-01 06:29:53 -07:00
|
|
|
Size radii[] = { ToSize(corners[NS_CORNER_TOP_LEFT]),
|
|
|
|
ToSize(corners[NS_CORNER_TOP_RIGHT]),
|
|
|
|
ToSize(corners[NS_CORNER_BOTTOM_RIGHT]),
|
|
|
|
ToSize(corners[NS_CORNER_BOTTOM_LEFT]) };
|
|
|
|
AppendRoundedRectToPath(mPathBuilder, ToRect(rect), radii, draw_clockwise);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
2008-07-23 10:25:00 -07:00
|
|
|
}
|
2011-10-25 20:19:13 -07:00
|
|
|
|
2011-11-16 19:44:16 -08:00
|
|
|
#ifdef MOZ_DUMP_PAINTING
|
2011-10-25 20:19:13 -07:00
|
|
|
void
|
|
|
|
gfxContext::WriteAsPNG(const char* aFile)
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxASurface> surf = CurrentSurface();
|
|
|
|
if (surf) {
|
|
|
|
surf->WriteAsPNG(aFile);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("No surface found!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::DumpAsDataURL()
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxASurface> surf = CurrentSurface();
|
|
|
|
if (surf) {
|
|
|
|
surf->DumpAsDataURL();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("No surface found!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::CopyAsDataURL()
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxASurface> surf = CurrentSurface();
|
|
|
|
if (surf) {
|
|
|
|
surf->CopyAsDataURL();
|
|
|
|
} else {
|
|
|
|
NS_WARNING("No surface found!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::EnsurePath()
|
|
|
|
{
|
|
|
|
if (mPathBuilder) {
|
|
|
|
mPath = mPathBuilder->Finish();
|
2013-07-31 08:44:31 -07:00
|
|
|
mPathBuilder = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPath) {
|
|
|
|
if (mTransformChanged) {
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix mat = mTransform;
|
2012-01-04 23:17:51 -08:00
|
|
|
mat.Invert();
|
|
|
|
mat = mPathTransform * mat;
|
|
|
|
mPathBuilder = mPath->TransformedCopyToBuilder(mat, CurrentState().fillRule);
|
|
|
|
mPath = mPathBuilder->Finish();
|
2013-07-31 08:44:31 -07:00
|
|
|
mPathBuilder = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
|
|
|
|
mTransformChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurrentState().fillRule == mPath->GetFillRule()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPathBuilder = mPath->CopyToBuilder(CurrentState().fillRule);
|
|
|
|
|
|
|
|
mPath = mPathBuilder->Finish();
|
2013-07-31 08:44:31 -07:00
|
|
|
mPathBuilder = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnsurePathBuilder();
|
|
|
|
mPath = mPathBuilder->Finish();
|
2013-07-31 08:44:31 -07:00
|
|
|
mPathBuilder = nullptr;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::EnsurePathBuilder()
|
|
|
|
{
|
2012-12-13 07:34:51 -08:00
|
|
|
if (mPathBuilder && !mTransformChanged) {
|
2012-01-04 23:17:51 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPath) {
|
2012-12-13 07:34:51 -08:00
|
|
|
if (!mTransformChanged) {
|
|
|
|
mPathBuilder = mPath->CopyToBuilder(CurrentState().fillRule);
|
2013-07-31 08:44:31 -07:00
|
|
|
mPath = nullptr;
|
2012-12-13 07:34:51 -08:00
|
|
|
} else {
|
|
|
|
Matrix invTransform = mTransform;
|
|
|
|
invTransform.Invert();
|
|
|
|
Matrix toNewUS = mPathTransform * invTransform;
|
|
|
|
mPathBuilder = mPath->TransformedCopyToBuilder(toNewUS, CurrentState().fillRule);
|
|
|
|
}
|
|
|
|
return;
|
2012-12-12 12:37:09 -08:00
|
|
|
}
|
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
DebugOnly<PathBuilder*> oldPath = mPathBuilder.get();
|
2012-12-12 12:37:09 -08:00
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
if (!mPathBuilder) {
|
|
|
|
mPathBuilder = mDT->CreatePathBuilder(CurrentState().fillRule);
|
|
|
|
|
|
|
|
if (mPathIsRect) {
|
|
|
|
mPathBuilder->MoveTo(mRect.TopLeft());
|
|
|
|
mPathBuilder->LineTo(mRect.TopRight());
|
|
|
|
mPathBuilder->LineTo(mRect.BottomRight());
|
|
|
|
mPathBuilder->LineTo(mRect.BottomLeft());
|
|
|
|
mPathBuilder->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTransformChanged) {
|
|
|
|
// This could be an else if since this should never happen when
|
2013-07-31 08:44:31 -07:00
|
|
|
// mPathBuilder is nullptr and mPath is nullptr. But this way we can
|
|
|
|
// assert if all the state is as expected.
|
2012-12-13 07:34:51 -08:00
|
|
|
MOZ_ASSERT(oldPath);
|
|
|
|
MOZ_ASSERT(!mPathIsRect);
|
|
|
|
|
|
|
|
Matrix invTransform = mTransform;
|
|
|
|
invTransform.Invert();
|
|
|
|
Matrix toNewUS = mPathTransform * invTransform;
|
|
|
|
|
|
|
|
RefPtr<Path> path = mPathBuilder->Finish();
|
|
|
|
mPathBuilder = path->TransformedCopyToBuilder(toNewUS, CurrentState().fillRule);
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mPathIsRect = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::FillAzure(Float aOpacity)
|
|
|
|
{
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
|
|
|
|
CompositionOp op = GetOp();
|
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
if (mPathIsRect) {
|
|
|
|
MOZ_ASSERT(!mTransformChanged);
|
|
|
|
|
2012-01-04 23:17:51 -08:00
|
|
|
if (state.opIsClear) {
|
|
|
|
mDT->ClearRect(mRect);
|
|
|
|
} else if (op == OP_SOURCE) {
|
|
|
|
// Emulate cairo operator source which is bound by mask!
|
|
|
|
mDT->ClearRect(mRect);
|
|
|
|
mDT->FillRect(mRect, GeneralPattern(this), DrawOptions(aOpacity));
|
|
|
|
} else {
|
|
|
|
mDT->FillRect(mRect, GeneralPattern(this), DrawOptions(aOpacity, op, state.aaMode));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EnsurePath();
|
|
|
|
|
|
|
|
NS_ASSERTION(!state.opIsClear, "We shouldn't be clearing complex paths!");
|
|
|
|
|
|
|
|
mDT->Fill(mPath, GeneralPattern(this), DrawOptions(aOpacity, op, state.aaMode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxContext::PushClipsToDT(DrawTarget *aDT)
|
|
|
|
{
|
|
|
|
// Tricky, we have to restore all clips -since the last time- the clip
|
|
|
|
// was reset. If we didn't reset the clip, just popping the clips we
|
|
|
|
// added was fine.
|
|
|
|
unsigned int lastReset = 0;
|
|
|
|
for (int i = mStateStack.Length() - 2; i > 0; i--) {
|
|
|
|
if (mStateStack[i].clipWasReset) {
|
|
|
|
lastReset = i;
|
2013-08-12 05:51:45 -07:00
|
|
|
break;
|
2012-01-04 23:17:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't need to save the old transform, we'll be setting a new one soon!
|
|
|
|
|
|
|
|
// Push all clips from the last state on the stack where the clip was
|
|
|
|
// reset to the clip before ours.
|
|
|
|
for (unsigned int i = lastReset; i < mStateStack.Length() - 1; i++) {
|
|
|
|
for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
|
2012-09-27 14:00:51 -07:00
|
|
|
aDT->SetTransform(mStateStack[i].pushedClips[c].transform * GetDeviceTransform());
|
2012-01-04 23:17:51 -08:00
|
|
|
if (mStateStack[i].pushedClips[c].path) {
|
|
|
|
aDT->PushClip(mStateStack[i].pushedClips[c].path);
|
|
|
|
} else {
|
|
|
|
aDT->PushClipRect(mStateStack[i].pushedClips[c].rect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositionOp
|
|
|
|
gfxContext::GetOp()
|
|
|
|
{
|
|
|
|
if (CurrentState().op != OP_SOURCE) {
|
|
|
|
return CurrentState().op;
|
|
|
|
}
|
|
|
|
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
if (state.pattern) {
|
|
|
|
if (state.pattern->IsOpaque()) {
|
|
|
|
return OP_OVER;
|
|
|
|
} else {
|
|
|
|
return OP_SOURCE;
|
|
|
|
}
|
|
|
|
} else if (state.sourceSurface) {
|
|
|
|
if (state.sourceSurface->GetFormat() == FORMAT_B8G8R8X8) {
|
|
|
|
return OP_OVER;
|
|
|
|
} else {
|
|
|
|
return OP_SOURCE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (state.color.a > 0.999) {
|
|
|
|
return OP_OVER;
|
|
|
|
} else {
|
|
|
|
return OP_SOURCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-29 11:53:43 -07:00
|
|
|
|
|
|
|
/* SVG font code can change the transform after having set the pattern on the
|
|
|
|
* context. When the pattern is set it is in user space, if the transform is
|
|
|
|
* changed after doing so the pattern needs to be converted back into userspace.
|
2012-12-13 07:34:51 -08:00
|
|
|
* We just store the old pattern transform here so that we only do the work
|
|
|
|
* needed here if the pattern is actually used.
|
|
|
|
* We need to avoid doing this when this ChangeTransform comes from a restore,
|
|
|
|
* since the current pattern and the current transform are both part of the
|
|
|
|
* state we know the new CurrentState()'s values are valid. But if we assume
|
|
|
|
* a change they might become invalid since patternTransformChanged is part of
|
|
|
|
* the state and might be false for the restored AzureState.
|
2012-03-29 11:53:43 -07:00
|
|
|
*/
|
|
|
|
void
|
2012-12-13 07:34:51 -08:00
|
|
|
gfxContext::ChangeTransform(const Matrix &aNewMatrix, bool aUpdatePatternTransform)
|
2012-03-29 11:53:43 -07:00
|
|
|
{
|
|
|
|
AzureState &state = CurrentState();
|
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
if (aUpdatePatternTransform && (state.pattern || state.sourceSurface)
|
2012-03-29 11:53:43 -07:00
|
|
|
&& !state.patternTransformChanged) {
|
2012-09-27 14:00:51 -07:00
|
|
|
state.patternTransform = mTransform;
|
2012-03-29 11:53:43 -07:00
|
|
|
state.patternTransformChanged = true;
|
|
|
|
}
|
2012-05-21 22:29:19 -07:00
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
if (mPathIsRect) {
|
2012-05-21 22:29:19 -07:00
|
|
|
Matrix invMatrix = aNewMatrix;
|
|
|
|
|
|
|
|
invMatrix.Invert();
|
|
|
|
|
2012-09-27 14:00:51 -07:00
|
|
|
Matrix toNewUS = mTransform * invMatrix;
|
2012-05-21 22:29:19 -07:00
|
|
|
|
2012-12-13 07:34:51 -08:00
|
|
|
if (toNewUS.IsRectilinear()) {
|
2012-05-21 22:29:19 -07:00
|
|
|
mRect = toNewUS.TransformBounds(mRect);
|
2012-09-07 05:32:21 -07:00
|
|
|
mRect.NudgeToIntegers();
|
2012-12-13 07:34:51 -08:00
|
|
|
} else {
|
2012-05-21 22:29:19 -07:00
|
|
|
mPathBuilder = mDT->CreatePathBuilder(CurrentState().fillRule);
|
|
|
|
|
|
|
|
mPathBuilder->MoveTo(toNewUS * mRect.TopLeft());
|
|
|
|
mPathBuilder->LineTo(toNewUS * mRect.TopRight());
|
|
|
|
mPathBuilder->LineTo(toNewUS * mRect.BottomRight());
|
|
|
|
mPathBuilder->LineTo(toNewUS * mRect.BottomLeft());
|
|
|
|
mPathBuilder->Close();
|
2012-12-13 07:34:51 -08:00
|
|
|
|
|
|
|
mPathIsRect = false;
|
2012-05-21 22:29:19 -07:00
|
|
|
}
|
2012-12-13 07:34:51 -08:00
|
|
|
|
2012-05-21 22:29:19 -07:00
|
|
|
// No need to consider the transform changed now!
|
|
|
|
mTransformChanged = false;
|
2012-12-13 07:34:51 -08:00
|
|
|
} else if ((mPath || mPathBuilder) && !mTransformChanged) {
|
|
|
|
mTransformChanged = true;
|
|
|
|
mPathTransform = mTransform;
|
2012-05-21 22:29:19 -07:00
|
|
|
}
|
|
|
|
|
2012-09-27 14:00:51 -07:00
|
|
|
mTransform = aNewMatrix;
|
|
|
|
|
|
|
|
mDT->SetTransform(GetDTTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect
|
|
|
|
gfxContext::GetAzureDeviceSpaceClipBounds()
|
|
|
|
{
|
|
|
|
unsigned int lastReset = 0;
|
|
|
|
for (int i = mStateStack.Length() - 1; i > 0; i--) {
|
|
|
|
if (mStateStack[i].clipWasReset) {
|
|
|
|
lastReset = i;
|
2013-08-12 05:51:45 -07:00
|
|
|
break;
|
2012-09-27 14:00:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect rect(CurrentState().deviceOffset.x, CurrentState().deviceOffset.y,
|
|
|
|
Float(mDT->GetSize().width), Float(mDT->GetSize().height));
|
|
|
|
for (unsigned int i = lastReset; i < mStateStack.Length(); i++) {
|
|
|
|
for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
|
|
|
|
AzureState::PushedClip &clip = mStateStack[i].pushedClips[c];
|
|
|
|
if (clip.path) {
|
|
|
|
Rect bounds = clip.path->GetBounds(clip.transform);
|
|
|
|
rect.IntersectRect(rect, bounds);
|
|
|
|
} else {
|
|
|
|
rect.IntersectRect(rect, clip.transform.TransformBounds(clip.rect));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix
|
|
|
|
gfxContext::GetDeviceTransform() const
|
|
|
|
{
|
|
|
|
Matrix mat;
|
|
|
|
mat.Translate(-CurrentState().deviceOffset.x, -CurrentState().deviceOffset.y);
|
|
|
|
return mat;
|
2012-09-27 14:00:51 -07:00
|
|
|
}
|
2012-09-27 14:00:51 -07:00
|
|
|
|
|
|
|
Matrix
|
|
|
|
gfxContext::GetDTTransform() const
|
|
|
|
{
|
|
|
|
Matrix mat = mTransform;
|
|
|
|
mat._31 -= CurrentState().deviceOffset.x;
|
|
|
|
mat._32 -= CurrentState().deviceOffset.y;
|
|
|
|
return mat;
|
2012-10-03 11:18:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxContext::PushNewDT(gfxContentType content)
|
2012-10-03 11:18:19 -07:00
|
|
|
{
|
|
|
|
Rect clipBounds = GetAzureDeviceSpaceClipBounds();
|
|
|
|
clipBounds.RoundOut();
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
clipBounds.width = std::max(1.0f, clipBounds.width);
|
|
|
|
clipBounds.height = std::max(1.0f, clipBounds.height);
|
2012-10-03 11:18:19 -07:00
|
|
|
|
|
|
|
RefPtr<DrawTarget> newDT =
|
|
|
|
mDT->CreateSimilarDrawTarget(IntSize(int32_t(clipBounds.width), int32_t(clipBounds.height)),
|
|
|
|
gfxPlatform::GetPlatform()->Optimal2DFormatForContent(content));
|
|
|
|
|
|
|
|
Save();
|
|
|
|
|
|
|
|
CurrentState().drawTarget = newDT;
|
|
|
|
CurrentState().deviceOffset = clipBounds.TopLeft();
|
|
|
|
|
|
|
|
mDT = newDT;
|
|
|
|
}
|
2013-01-21 01:24:01 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Work out whether cairo will snap inter-glyph spacing to pixels.
|
|
|
|
*
|
|
|
|
* Layout does not align text to pixel boundaries, so, with font drawing
|
|
|
|
* backends that snap glyph positions to pixels, it is important that
|
|
|
|
* inter-glyph spacing within words is always an integer number of pixels.
|
|
|
|
* This ensures that the drawing backend snaps all of the word's glyphs in the
|
|
|
|
* same direction and so inter-glyph spacing remains the same.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gfxContext::GetRoundOffsetsToPixels(bool *aRoundX, bool *aRoundY)
|
|
|
|
{
|
|
|
|
*aRoundX = false;
|
|
|
|
// Could do something fancy here for ScaleFactors of
|
|
|
|
// AxisAlignedTransforms, but we leave things simple.
|
|
|
|
// Not much point rounding if a matrix will mess things up anyway.
|
|
|
|
// Also return false for non-cairo contexts.
|
|
|
|
if (CurrentMatrix().HasNonTranslation() || mDT) {
|
|
|
|
*aRoundY = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All raster backends snap glyphs to pixels vertically.
|
|
|
|
// Print backends set CAIRO_HINT_METRICS_OFF.
|
|
|
|
*aRoundY = true;
|
|
|
|
|
|
|
|
cairo_t *cr = GetCairo();
|
|
|
|
cairo_scaled_font_t *scaled_font = cairo_get_scaled_font(cr);
|
|
|
|
// Sometimes hint metrics gets set for us, most notably for printing.
|
|
|
|
cairo_font_options_t *font_options = cairo_font_options_create();
|
|
|
|
cairo_scaled_font_get_font_options(scaled_font, font_options);
|
|
|
|
cairo_hint_metrics_t hint_metrics =
|
|
|
|
cairo_font_options_get_hint_metrics(font_options);
|
|
|
|
cairo_font_options_destroy(font_options);
|
|
|
|
|
|
|
|
switch (hint_metrics) {
|
|
|
|
case CAIRO_HINT_METRICS_OFF:
|
|
|
|
*aRoundY = false;
|
|
|
|
return;
|
|
|
|
case CAIRO_HINT_METRICS_DEFAULT:
|
|
|
|
// Here we mimic what cairo surface/font backends do. Printing
|
|
|
|
// surfaces have already been handled by hint_metrics. The
|
|
|
|
// fallback show_glyphs implementation composites pixel-aligned
|
|
|
|
// glyph surfaces, so we just pick surface/font combinations that
|
|
|
|
// override this.
|
|
|
|
switch (cairo_scaled_font_get_type(scaled_font)) {
|
|
|
|
#if CAIRO_HAS_DWRITE_FONT // dwrite backend is not in std cairo releases yet
|
|
|
|
case CAIRO_FONT_TYPE_DWRITE:
|
|
|
|
// show_glyphs is implemented on the font and so is used for
|
|
|
|
// all surface types; however, it may pixel-snap depending on
|
|
|
|
// the dwrite rendering mode
|
|
|
|
if (!cairo_dwrite_scaled_font_get_force_GDI_classic(scaled_font) &&
|
|
|
|
gfxWindowsPlatform::GetPlatform()->DWriteMeasuringMode() ==
|
|
|
|
DWRITE_MEASURING_MODE_NATURAL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
case CAIRO_FONT_TYPE_QUARTZ:
|
|
|
|
// Quartz surfaces implement show_glyphs for Quartz fonts
|
|
|
|
if (cairo_surface_get_type(cairo_get_target(cr)) ==
|
|
|
|
CAIRO_SURFACE_TYPE_QUARTZ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through:
|
|
|
|
case CAIRO_HINT_METRICS_ON:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*aRoundX = true;
|
|
|
|
return;
|
|
|
|
}
|