/*
 * Copyright (C) 2010 Igalia S.L.
 * Copyright (C) 2011 ProFUSION embedded systems
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CairoUtilities_h
#define CairoUtilities_h

#include "Color.h"
#include "FloatSize.h"
#include "GraphicsTypes.h"
#include "RefPtrCairo.h"
#include <wtf/RefPtr.h>
#include <wtf/PassRefPtr.h>
#include <cairo.h>

#if USE(FREETYPE)
#include <cairo-ft.h>
#endif

namespace WebCore {
class AffineTransform;
class FloatRect;
class FloatPoint;
class GraphicsContext;
class IntSize;
class IntRect;
class Path;
class PlatformContextCairo;
struct GraphicsContextState;

#if USE(FREETYPE)
class CairoFtFaceLocker {
public:
    CairoFtFaceLocker(cairo_scaled_font_t* scaledFont)
        : m_scaledFont(scaledFont)
        , m_ftFace(cairo_ft_scaled_font_lock_face(scaledFont))
    {
    }

    ~CairoFtFaceLocker()
    {
        if (m_ftFace)
            cairo_ft_scaled_font_unlock_face(m_scaledFont);
    }

    FT_Face ftFace() const { return m_ftFace; }

private:
    cairo_scaled_font_t* m_scaledFont;
    FT_Face m_ftFace;
};
#endif

struct ShadowState {
    explicit ShadowState(const GraphicsContextState&);

    bool isVisible() const;
    bool isRequired(PlatformContextCairo&) const;

    FloatSize offset;
    float blur;
    Color color;
    bool ignoreTransforms;
};

void copyContextProperties(cairo_t* srcCr, cairo_t* dstCr);

void setSourceRGBAFromColor(cairo_t* context, const Color& color);

void appendPathToCairoContext(cairo_t* to, cairo_t* from);
void setPathOnCairoContext(cairo_t* to, cairo_t* from);
void appendWebCorePathToCairoContext(cairo_t* context, const Path& path);
void appendRegionToCairoContext(cairo_t*, const cairo_region_t*);
cairo_operator_t toCairoOperator(CompositeOperator op);
cairo_operator_t toCairoOperator(BlendMode blendOp);
void drawPatternToCairoContext(GraphicsContext* context, cairo_surface_t* image, const IntSize& imageSize, const FloatRect& tileRect,
                               const AffineTransform& patternTransform, const FloatPoint& phase, cairo_operator_t op, const FloatRect& destRect);
PassRefPtr<cairo_surface_t> copyCairoImageSurface(cairo_surface_t*);

void copyRectFromCairoSurfaceToContext(cairo_surface_t* from, cairo_t* to, const IntSize& offset, const IntRect&);
void copyRectFromOneSurfaceToAnother(cairo_surface_t* from, cairo_surface_t* to, const IntSize& offset, const IntRect&);

cairo_surface_t* getPathSurface();

cairo_surface_t* createAcceleratedSurface(cairo_format_t format, float width, float height);

bool getSurfaceSize(cairo_surface_t* surface, int& width, int& height);

cairo_surface_t* copySurface(cairo_surface_t* surface);

bool lockSurface(cairo_surface_t* surface, unsigned char** ret_ptr, int* ret_pitch, bool overwrite);
void unlockSurface(cairo_surface_t* surface, bool dirty);

bool extractSurfaceData(cairo_surface_t* surface, unsigned char* buffer, int buffer_len);

bool getSurfaceFormat(cairo_surface_t* surface, cairo_format_t& format);

} // namespace WebCore

#endif // CairoUtilities_h
