1/**
2 * Copyright (C) 2007 Rob Buis <buis@kde.org>
3 * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 Google, Inc.  All rights reserved.
6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
7 * Copyright (C) 2012 Zoltan Herczeg <zherczeg@webkit.org>.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#ifndef SVGRenderingContext_h
26#define SVGRenderingContext_h
27
28#include "ImageBuffer.h"
29#include "PaintInfo.h"
30
31namespace WebCore {
32
33class AffineTransform;
34class RenderElement;
35class RenderObject;
36class FloatRect;
37class RenderSVGResourceFilter;
38
39// SVGRenderingContext
40class SVGRenderingContext {
41public:
42    enum NeedsGraphicsContextSave {
43        SaveGraphicsContext,
44        DontSaveGraphicsContext,
45    };
46
47    // Does not start rendering.
48    SVGRenderingContext()
49        : m_renderingFlags(0)
50        , m_renderer(nullptr)
51        , m_paintInfo(nullptr)
52        , m_savedContext(nullptr)
53#if ENABLE(FILTERS)
54        , m_filter(nullptr)
55#endif
56    {
57    }
58
59    SVGRenderingContext(RenderElement& object, PaintInfo& paintinfo, NeedsGraphicsContextSave needsGraphicsContextSave = DontSaveGraphicsContext)
60        : m_renderingFlags(0)
61        , m_renderer(nullptr)
62        , m_paintInfo(nullptr)
63        , m_savedContext(nullptr)
64#if ENABLE(FILTERS)
65        , m_filter(nullptr)
66#endif
67    {
68        prepareToRenderSVGContent(object, paintinfo, needsGraphicsContextSave);
69    }
70
71    // Automatically finishes context rendering.
72    ~SVGRenderingContext();
73
74    // Used by all SVG renderers who apply clip/filter/etc. resources to the renderer content.
75    void prepareToRenderSVGContent(RenderElement&, PaintInfo&, NeedsGraphicsContextSave = DontSaveGraphicsContext);
76    bool isRenderingPrepared() const { return m_renderingFlags & RenderingPrepared; }
77
78    static bool createImageBuffer(const FloatRect& paintRect, const AffineTransform& absoluteTransform, std::unique_ptr<ImageBuffer>&, ColorSpace, RenderingMode);
79    // Patterns need a different float-to-integer coordinate mapping.
80    static bool createImageBufferForPattern(const FloatRect& absoluteTargetRect, const FloatRect& clampedAbsoluteTargetRect, std::unique_ptr<ImageBuffer>&, ColorSpace, RenderingMode);
81
82    static void renderSubtreeToImageBuffer(ImageBuffer*, RenderElement&, const AffineTransform&);
83    static void clipToImageBuffer(GraphicsContext*, const AffineTransform& absoluteTransform, const FloatRect& targetRect, std::unique_ptr<ImageBuffer>&, bool safeToClear);
84
85    static float calculateScreenFontSizeScalingFactor(const RenderObject&);
86    static void calculateTransformationToOutermostCoordinateSystem(const RenderObject&, AffineTransform& absoluteTransform);
87    static IntSize clampedAbsoluteSize(const IntSize&);
88    static FloatRect clampedAbsoluteTargetRect(const FloatRect& absoluteTargetRect);
89    static void clear2DRotation(AffineTransform&);
90
91    static IntRect calculateImageBufferRect(const FloatRect& targetRect, const AffineTransform& absoluteTransform)
92    {
93        return enclosingIntRect(absoluteTransform.mapRect(targetRect));
94    }
95
96    // Support for the buffered-rendering hint.
97    bool bufferForeground(std::unique_ptr<ImageBuffer>&);
98
99private:
100    // To properly revert partially successful initializtions in the destructor, we record all successful steps.
101    enum RenderingFlags {
102        RenderingPrepared = 1,
103        RestoreGraphicsContext = 1 << 1,
104        EndOpacityLayer = 1 << 2,
105        EndShadowLayer = 1 << 3,
106        EndFilterLayer = 1 << 4,
107        PrepareToRenderSVGContentWasCalled = 1 << 5
108    };
109
110    // List of those flags which require actions during the destructor.
111    const static int ActionsNeeded = RestoreGraphicsContext | EndOpacityLayer | EndShadowLayer | EndFilterLayer;
112
113    int m_renderingFlags;
114    RenderElement* m_renderer;
115    PaintInfo* m_paintInfo;
116    GraphicsContext* m_savedContext;
117    LayoutRect m_savedPaintRect;
118#if ENABLE(FILTERS)
119    RenderSVGResourceFilter* m_filter;
120#endif
121};
122
123} // namespace WebCore
124
125#endif // SVGRenderingContext_h
126