~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to src/display/drawing-context.cpp

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file
 
3
 * @brief Cairo drawing context with Inkscape extensions
 
4
 *//*
 
5
 * Authors:
 
6
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 
7
 *
 
8
 * Copyright (C) 2011 Authors
 
9
 * Released under GNU GPL, read the file 'COPYING' for more information
 
10
 */
 
11
 
 
12
#include "display/drawing-context.h"
 
13
#include "display/drawing-surface.h"
 
14
#include "display/cairo-utils.h"
 
15
#include "helper/geom.h"
 
16
 
 
17
namespace Inkscape {
 
18
 
 
19
using Geom::X;
 
20
using Geom::Y;
 
21
 
 
22
/** @class DrawingContext::Save
 
23
 * @brief RAII idiom for saving the state of DrawingContext. */
 
24
 
 
25
DrawingContext::Save::Save()
 
26
    : _ct(NULL)
 
27
{}
 
28
DrawingContext::Save::Save(DrawingContext &ct)
 
29
    : _ct(&ct)
 
30
{
 
31
    _ct->save();
 
32
}
 
33
DrawingContext::Save::~Save()
 
34
{
 
35
    if (_ct) {
 
36
        _ct->restore();
 
37
    }
 
38
}
 
39
void DrawingContext::Save::save(DrawingContext &ct)
 
40
{
 
41
    if (_ct) {
 
42
        // TODO: it might be better to treat this occurence as a bug
 
43
        _ct->restore();
 
44
    }
 
45
    _ct = &ct;
 
46
    _ct->save();
 
47
}
 
48
 
 
49
/** @class DrawingContext
 
50
 * @brief Minimal wrapper over Cairo.
 
51
 *
 
52
 * This is a wrapper over cairo_t, extended with operations that work
 
53
 * with 2Geom geometrical primitives. Some of this is probably duplicated
 
54
 * in cairo-render-context.cpp, which provides higher level operations
 
55
 * for drawing entire SPObjects when exporting.
 
56
 */
 
57
 
 
58
DrawingContext::DrawingContext(cairo_t *ct, Geom::Point const &origin)
 
59
    : _ct(ct)
 
60
    , _surface(new DrawingSurface(cairo_get_group_target(ct), origin))
 
61
    , _delete_surface(true)
 
62
    , _restore_context(true)
 
63
{
 
64
    _surface->_has_context = true;
 
65
    cairo_reference(_ct);
 
66
    cairo_save(_ct);
 
67
    cairo_translate(_ct, -origin[Geom::X], -origin[Geom::Y]);
 
68
}
 
69
 
 
70
DrawingContext::DrawingContext(cairo_surface_t *surface, Geom::Point const &origin)
 
71
    : _ct(NULL)
 
72
    , _surface(new DrawingSurface(surface, origin))
 
73
    , _delete_surface(true)
 
74
    , _restore_context(false)
 
75
{
 
76
    _surface->_has_context = true;
 
77
    _ct = _surface->createRawContext();
 
78
}
 
79
 
 
80
DrawingContext::DrawingContext(DrawingSurface &s)
 
81
    : _ct(s.createRawContext())
 
82
    , _surface(&s)
 
83
    , _delete_surface(false)
 
84
    , _restore_context(false)
 
85
{}
 
86
 
 
87
DrawingContext::~DrawingContext()
 
88
{
 
89
    if (_restore_context) {
 
90
        cairo_restore(_ct);
 
91
    }
 
92
    cairo_destroy(_ct);
 
93
    _surface->_has_context = false;
 
94
    if (_delete_surface) {
 
95
        delete _surface;
 
96
    }
 
97
}
 
98
 
 
99
void DrawingContext::arc(Geom::Point const &center, double radius, Geom::AngleInterval const &angle)
 
100
{
 
101
    double from = angle.initialAngle();
 
102
    double to = angle.finalAngle();
 
103
    if (to > from) {
 
104
        cairo_arc(_ct, center[X], center[Y], radius, from, to);
 
105
    } else {
 
106
        cairo_arc_negative(_ct, center[X], center[Y], radius, to, from);
 
107
    }
 
108
}
 
109
 
 
110
void DrawingContext::transform(Geom::Affine const &trans) {
 
111
    ink_cairo_transform(_ct, trans);
 
112
}
 
113
 
 
114
void DrawingContext::path(Geom::PathVector const &pv) {
 
115
    feed_pathvector_to_cairo(_ct, pv);
 
116
}
 
117
 
 
118
void DrawingContext::paint(double alpha) {
 
119
    if (alpha == 1.0) cairo_paint(_ct);
 
120
    else cairo_paint_with_alpha(_ct, alpha);
 
121
}
 
122
void DrawingContext::setSource(guint32 rgba) {
 
123
    ink_cairo_set_source_rgba32(_ct, rgba);
 
124
}
 
125
void DrawingContext::setSource(DrawingSurface *s) {
 
126
    Geom::Point origin = s->origin();
 
127
    cairo_set_source_surface(_ct, s->raw(), origin[X], origin[Y]);
 
128
}
 
129
void DrawingContext::setSourceCheckerboard() {
 
130
    cairo_pattern_t *check = ink_cairo_pattern_create_checkerboard();
 
131
    cairo_set_source(_ct, check);
 
132
    cairo_pattern_destroy(check);
 
133
}
 
134
 
 
135
Geom::Rect DrawingContext::targetLogicalBounds() const
 
136
{
 
137
    Geom::Rect ret(_surface->area());
 
138
    return ret;
 
139
}
 
140
 
 
141
} // end namespace Inkscape
 
142
 
 
143
/*
 
144
  Local Variables:
 
145
  mode:c++
 
146
  c-file-style:"stroustrup"
 
147
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
148
  indent-tabs-mode:nil
 
149
  fill-column:99
 
150
  End:
 
151
*/
 
152
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :