3
* @brief Cairo drawing context with Inkscape extensions
6
* Krzysztof Kosiński <tweenk.pl@gmail.com>
8
* Copyright (C) 2011 Authors
9
* Released under GNU GPL, read the file 'COPYING' for more information
12
#include "display/drawing-context.h"
13
#include "display/drawing-surface.h"
14
#include "display/cairo-utils.h"
15
#include "helper/geom.h"
22
/** @class DrawingContext::Save
23
* @brief RAII idiom for saving the state of DrawingContext. */
25
DrawingContext::Save::Save()
28
DrawingContext::Save::Save(DrawingContext &ct)
33
DrawingContext::Save::~Save()
39
void DrawingContext::Save::save(DrawingContext &ct)
42
// TODO: it might be better to treat this occurence as a bug
49
/** @class DrawingContext
50
* @brief Minimal wrapper over Cairo.
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.
58
DrawingContext::DrawingContext(cairo_t *ct, Geom::Point const &origin)
60
, _surface(new DrawingSurface(cairo_get_group_target(ct), origin))
61
, _delete_surface(true)
62
, _restore_context(true)
64
_surface->_has_context = true;
67
cairo_translate(_ct, -origin[Geom::X], -origin[Geom::Y]);
70
DrawingContext::DrawingContext(cairo_surface_t *surface, Geom::Point const &origin)
72
, _surface(new DrawingSurface(surface, origin))
73
, _delete_surface(true)
74
, _restore_context(false)
76
_surface->_has_context = true;
77
_ct = _surface->createRawContext();
80
DrawingContext::DrawingContext(DrawingSurface &s)
81
: _ct(s.createRawContext())
83
, _delete_surface(false)
84
, _restore_context(false)
87
DrawingContext::~DrawingContext()
89
if (_restore_context) {
93
_surface->_has_context = false;
94
if (_delete_surface) {
99
void DrawingContext::arc(Geom::Point const ¢er, double radius, Geom::AngleInterval const &angle)
101
double from = angle.initialAngle();
102
double to = angle.finalAngle();
104
cairo_arc(_ct, center[X], center[Y], radius, from, to);
106
cairo_arc_negative(_ct, center[X], center[Y], radius, to, from);
110
void DrawingContext::transform(Geom::Affine const &trans) {
111
ink_cairo_transform(_ct, trans);
114
void DrawingContext::path(Geom::PathVector const &pv) {
115
feed_pathvector_to_cairo(_ct, pv);
118
void DrawingContext::paint(double alpha) {
119
if (alpha == 1.0) cairo_paint(_ct);
120
else cairo_paint_with_alpha(_ct, alpha);
122
void DrawingContext::setSource(guint32 rgba) {
123
ink_cairo_set_source_rgba32(_ct, rgba);
125
void DrawingContext::setSource(DrawingSurface *s) {
126
Geom::Point origin = s->origin();
127
cairo_set_source_surface(_ct, s->raw(), origin[X], origin[Y]);
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);
135
Geom::Rect DrawingContext::targetLogicalBounds() const
137
Geom::Rect ret(_surface->area());
141
} // end namespace Inkscape
146
c-file-style:"stroustrup"
147
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
152
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :