1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/**
* @file
* @brief Group belonging to an SVG drawing element
*//*
* Authors:
* Krzysztof Kosiński <tweenk.pl@gmail.com>
*
* Copyright (C) 2011 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "display/cairo-utils.h"
#include "display/canvas-bpath.h" // for SPWindRule (WTF!)
#include "display/drawing.h"
#include "display/drawing-context.h"
#include "display/drawing-surface.h"
#include "display/drawing-text.h"
#include "helper/geom.h"
#include "libnrtype/font-instance.h"
#include "style.h"
namespace Inkscape {
DrawingGlyphs::DrawingGlyphs(Drawing &drawing)
: DrawingItem(drawing)
, _glyph_transform(NULL)
, _font(NULL)
, _glyph(0)
{}
DrawingGlyphs::~DrawingGlyphs()
{
if (_font) {
_font->Unref();
_font = NULL;
}
delete _glyph_transform;
}
void
DrawingGlyphs::setGlyph(font_instance *font, int glyph, Geom::Affine const &trans)
{
_markForRendering();
if (trans.isIdentity()) {
delete _glyph_transform; // delete NULL; is safe
_glyph_transform = NULL;
} else {
_glyph_transform = new Geom::Affine(trans);
}
if (font) font->Ref();
if (_font) _font->Unref();
_font = font;
_glyph = glyph;
_markForUpdate(STATE_ALL, false);
}
unsigned
DrawingGlyphs::_updateItem(Geom::IntRect const &area, UpdateContext const &ctx, unsigned flags, unsigned reset)
{
DrawingText *ggroup = dynamic_cast<DrawingText *>(_parent);
if (!ggroup) throw InvalidItemException();
if (!_font || !ggroup->_style) return STATE_ALL;
if (ggroup->_nrstyle.fill.type == NRStyle::PAINT_NONE &&
ggroup->_nrstyle.stroke.type == NRStyle::PAINT_NONE)
{
return STATE_ALL;
}
Geom::OptRect b;
Geom::Affine t = _glyph_transform ? *_glyph_transform * ctx.ctm : ctx.ctm;
_x = t[4];
_y = t[5];
b = bounds_exact_transformed(*_font->PathVector(_glyph), t);
if (b && ggroup->_nrstyle.stroke.type != NRStyle::PAINT_NONE) {
float width, scale;
scale = ctx.ctm.descrim();
width = MAX(0.125, ggroup->_nrstyle.stroke_width * scale);
if ( fabs(ggroup->_nrstyle.stroke_width * scale) > 0.01 ) { // FIXME: this is always true
b->expandBy(width);
}
// those pesky miters, now
float miterMax = width * ggroup->_nrstyle.miter_limit;
if ( miterMax > 0.01 ) {
// grunt mode. we should compute the various miters instead
// (one for each point on the curve)
b->expandBy(miterMax);
}
}
if (b) {
_bbox = b->roundOutwards();
} else {
_bbox = Geom::OptIntRect();
}
return STATE_ALL;
}
DrawingItem *
DrawingGlyphs::_pickItem(Geom::Point const &p, double delta, bool /*sticky*/)
{
if (!_font || !_bbox) return NULL;
// With text we take a simple approach: pick if the point is in a characher bbox
Geom::Rect expanded(*_bbox);
expanded.expandBy(delta);
if (expanded.contains(p)) return this;
return NULL;
}
DrawingText::DrawingText(Drawing &drawing)
: DrawingGroup(drawing)
{}
DrawingText::~DrawingText()
{}
void
DrawingText::clear()
{
_markForRendering();
_children.clear_and_dispose(DeleteDisposer());
}
void
DrawingText::addComponent(font_instance *font, int glyph, Geom::Affine const &trans)
{
if (!font || !font->PathVector(glyph)) return;
_markForRendering();
DrawingGlyphs *ng = new DrawingGlyphs(_drawing);
ng->setGlyph(font, glyph, trans);
appendChild(ng);
}
void
DrawingText::setStyle(SPStyle *style)
{
_nrstyle.set(style);
DrawingGroup::setStyle(style);
}
void
DrawingText::setPaintBox(Geom::OptRect const &box)
{
_paintbox = box;
_markForUpdate(STATE_ALL, false);
}
unsigned
DrawingText::_updateItem(Geom::IntRect const &area, UpdateContext const &ctx, unsigned flags, unsigned reset)
{
_nrstyle.update();
return DrawingGroup::_updateItem(area, ctx, flags, reset);
}
void
DrawingText::_renderItem(DrawingContext &ct, Geom::IntRect const &area, unsigned flags)
{
if (_drawing.outline()) {
DrawingContext::Save save(ct);
guint32 rgba = _drawing.outlinecolor;
ct.setSource(rgba);
ct.setTolerance(1.25); // low quality, but good enough for outline mode
ct.newPath();
ct.transform(_ctm);
for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
DrawingGlyphs *g = dynamic_cast<DrawingGlyphs *>(&*i);
if (!g) throw InvalidItemException();
Inkscape::DrawingContext::Save save(ct);
if (g->_glyph_transform) {
ct.transform(*g->_glyph_transform);
}
ct.path(*g->_font->PathVector(g->_glyph));
ct.fill();
}
return;
}
// NOTE: this is very similar to drawing-shape.cpp; the only difference is in path feeding
bool has_stroke, has_fill;
Inkscape::DrawingContext::Save save(ct);
ct.transform(_ctm);
has_fill = _nrstyle.prepareFill(ct, _paintbox);
has_stroke = _nrstyle.prepareStroke(ct, _paintbox);
if (has_fill || has_stroke) {
for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
DrawingGlyphs *g = dynamic_cast<DrawingGlyphs *>(&*i);
if (!g) throw InvalidItemException();
Inkscape::DrawingContext::Save save(ct);
if (g->_glyph_transform) {
ct.transform(*g->_glyph_transform);
}
ct.path(*g->_font->PathVector(g->_glyph));
}
if (has_fill) {
_nrstyle.applyFill(ct);
ct.fillPreserve();
}
if (has_stroke) {
_nrstyle.applyStroke(ct);
ct.strokePreserve();
}
ct.newPath(); // clear path
}
}
void
DrawingText::_clipItem(DrawingContext &ct, Geom::IntRect const &area)
{
Inkscape::DrawingContext::Save save(ct);
// handle clip-rule
if (_style) {
if (_style->clip_rule.computed == SP_WIND_RULE_EVENODD) {
ct.setFillRule(CAIRO_FILL_RULE_EVEN_ODD);
} else {
ct.setFillRule(CAIRO_FILL_RULE_WINDING);
}
}
ct.transform(_ctm);
for (ChildrenList::iterator i = _children.begin(); i != _children.end(); ++i) {
DrawingGlyphs *g = dynamic_cast<DrawingGlyphs *>(&*i);
if (!g) throw InvalidItemException();
Inkscape::DrawingContext::Save save(ct);
if (g->_glyph_transform) {
ct.transform(*g->_glyph_transform);
}
ct.path(*g->_font->PathVector(g->_glyph));
}
ct.fill();
}
DrawingItem *
DrawingText::_pickItem(Geom::Point const &p, double delta, bool sticky)
{
DrawingItem *picked = DrawingGroup::_pickItem(p, delta, sticky);
if (picked) return this;
return NULL;
}
bool
DrawingText::_canClip()
{
return true;
}
} // end namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|