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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
#define __SP_CAIRO_RENDERER_C__
/** \file
* Rendering with Cairo.
*/
/*
* Author:
* Miklos Erdelyi <erdelyim@gmail.com>
*
* Copyright (C) 2006 Miklos Erdelyi
*
* Licensed under GNU GPL
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_CAIRO_PDF
#ifndef PANGO_ENABLE_BACKEND
#define PANGO_ENABLE_BACKEND
#endif
#ifndef PANGO_ENABLE_ENGINE
#define PANGO_ENABLE_ENGINE
#endif
#include <signal.h>
#include <errno.h>
#include <libnr/n-art-bpath.h>
#include <libnr/nr-matrix-ops.h>
#include <libnr/nr-matrix-fns.h>
#include <libnr/nr-matrix-translate-ops.h>
#include <libnr/nr-scale-matrix-ops.h>
#include <glib/gmem.h>
#include <glibmm/i18n.h>
#include "display/nr-arena.h"
#include "display/nr-arena-item.h"
#include "display/nr-arena-group.h"
#include "display/curve.h"
#include "display/canvas-bpath.h"
#include "sp-item.h"
#include "sp-item-group.h"
#include "marker-status.h"
#include "style.h"
#include "sp-marker.h"
#include "sp-linear-gradient.h"
#include "sp-radial-gradient.h"
#include "sp-root.h"
#include "sp-shape.h"
#include "sp-use.h"
#include "sp-text.h"
#include "sp-flowtext.h"
#include "sp-image.h"
#include "sp-symbol.h"
#include "sp-pattern.h"
#include "sp-mask.h"
#include "sp-clippath.h"
#include <unit-constants.h>
#include "cairo-renderer.h"
#include "cairo-render-context.h"
#include "extension/system.h"
#include "io/sys.h"
#include <cairo.h>
// include support for only the compiled-in surface types
#ifdef CAIRO_HAS_PDF_SURFACE
#include <cairo-pdf.h>
#endif
#ifdef CAIRO_HAS_PS_SURFACE
#include <cairo-ps.h>
#endif
//#define TRACE(_args) g_printf _args
#define TRACE(_args)
// FIXME: expose these from sp-clippath/mask.cpp
struct SPClipPathView {
SPClipPathView *next;
unsigned int key;
NRArenaItem *arenaitem;
NRRect bbox;
};
struct SPMaskView {
SPMaskView *next;
unsigned int key;
NRArenaItem *arenaitem;
NRRect bbox;
};
namespace Inkscape {
namespace Extension {
namespace Internal {
CairoRenderer::CairoRenderer(void)
{}
CairoRenderer::~CairoRenderer(void)
{
/* restore default signal handling for SIGPIPE */
#if !defined(_WIN32) && !defined(__WIN32__)
(void) signal(SIGPIPE, SIG_DFL);
#endif
return;
}
CairoRenderContext*
CairoRenderer::createContext(void)
{
CairoRenderContext *new_context = new CairoRenderContext(this);
g_assert( new_context != NULL );
new_context->_state_stack = NULL;
new_context->_state = NULL;
// create initial render state
CairoRenderState *state = new_context->_createState();
nr_matrix_set_identity(&state->transform);
new_context->_state_stack = g_slist_prepend(new_context->_state_stack, state);
new_context->_state = state;
return new_context;
}
void
CairoRenderer::destroyContext(CairoRenderContext *ctx)
{
delete ctx;
}
/*
Here comes the rendering part which could be put into the 'render' methods of SPItems'
*/
/* The below functions are copy&pasted plus slightly modified from *_invoke_print functions. */
static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx);
static void sp_group_render(SPItem *item, CairoRenderContext *ctx);
static void sp_use_render(SPItem *item, CairoRenderContext *ctx);
static void sp_shape_render(SPItem *item, CairoRenderContext *ctx);
static void sp_text_render(SPItem *item, CairoRenderContext *ctx);
static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx);
static void sp_image_render(SPItem *item, CairoRenderContext *ctx);
static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx);
static void sp_shape_render (SPItem *item, CairoRenderContext *ctx)
{
NRRect pbox;
SPShape *shape = SP_SHAPE(item);
if (!shape->curve) return;
/* fixme: Think (Lauris) */
sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
NR::Matrix const i2d = sp_item_i2d_affine(item);
SPStyle* style = SP_OBJECT_STYLE (item);
CairoRenderer *renderer = ctx->getRenderer();
NRBPath bp;
bp.path = SP_CURVE_BPATH(shape->curve);
ctx->renderPath(&bp, style, &pbox);
for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
if (sp_shape_marker_required (shape, m, bp)) {
SPMarker* marker = SP_MARKER (shape->marker[m]);
SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
tr = NR::scale(style->stroke_width.computed) * tr;
}
tr = marker_item->transform * marker->c2p * tr;
NR::Matrix old_tr = marker_item->transform;
marker_item->transform = tr;
renderer->renderItem (ctx, marker_item);
marker_item->transform = old_tr;
}
}
}
}
static void sp_group_render(SPItem *item, CairoRenderContext *ctx)
{
SPGroup *group = SP_GROUP(item);
CairoRenderer *renderer = ctx->getRenderer();
TRACE(("group op: %f\n", SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value)));
GSList *l = g_slist_reverse(group->childList(false));
while (l) {
SPObject *o = SP_OBJECT (l->data);
if (SP_IS_ITEM(o)) {
renderer->renderItem (ctx, SP_ITEM (o));
}
l = g_slist_remove (l, o);
}
}
static void sp_use_render(SPItem *item, CairoRenderContext *ctx)
{
bool translated = false;
NRMatrix tp;
SPUse *use = SP_USE(item);
CairoRenderer *renderer = ctx->getRenderer();
if ((use->x._set && use->x.computed != 0) || (use->y._set && use->y.computed != 0)) {
nr_matrix_set_translate(&tp, use->x.computed, use->y.computed);
ctx->pushState();
ctx->transform(&tp);
translated = true;
}
if (use->child && SP_IS_ITEM(use->child)) {
renderer->renderItem(ctx, SP_ITEM(use->child));
}
if (translated) {
ctx->popState();
}
}
static void sp_text_render(SPItem *item, CairoRenderContext *ctx)
{
SPText *group = SP_TEXT (item);
group->layout.showGlyphs(ctx);
}
static void sp_flowtext_render(SPItem *item, CairoRenderContext *ctx)
{
SPFlowtext *group = SP_FLOWTEXT(item);
group->layout.showGlyphs(ctx);
}
static void sp_image_render(SPItem *item, CairoRenderContext *ctx)
{
SPImage *image;
NRMatrix tp, s, t;
guchar *px;
int w, h, rs;
image = SP_IMAGE (item);
if (!image->pixbuf) return;
if ((image->width.computed <= 0.0) || (image->height.computed <= 0.0)) return;
px = gdk_pixbuf_get_pixels (image->pixbuf);
w = gdk_pixbuf_get_width (image->pixbuf);
h = gdk_pixbuf_get_height (image->pixbuf);
rs = gdk_pixbuf_get_rowstride (image->pixbuf);
double x = image->x.computed;
double y = image->y.computed;
double width = image->width.computed;
double height = image->height.computed;
if (image->aspect_align != SP_ASPECT_NONE) {
calculatePreserveAspectRatio (image->aspect_align, image->aspect_clip, (double)w, (double)h,
&x, &y, &width, &height);
}
if (image->aspect_clip == SP_ASPECT_SLICE && !ctx->getCurrentState()->has_overflow) {
ctx->addClippingRect(image->x.computed, image->y.computed, image->width.computed, image->height.computed);
}
nr_matrix_set_translate (&tp, x, y);
nr_matrix_set_scale (&s, width / (double)w, height / (double)h);
nr_matrix_multiply (&t, &s, &tp);
ctx->renderImage (px, w, h, rs, &t, SP_OBJECT_STYLE (item));
}
static void sp_symbol_render(SPItem *item, CairoRenderContext *ctx)
{
SPSymbol *symbol = SP_SYMBOL(item);
if (!SP_OBJECT_IS_CLONED (symbol))
return;
/* Cloned <symbol> is actually renderable */
ctx->pushState();
ctx->transform(&symbol->c2p);
// apply viewbox if set
if (0 /*symbol->viewBox_set*/) {
NRMatrix vb2user;
double x, y, width, height;
double view_width, view_height;
x = 0.0;
y = 0.0;
width = 1.0;
height = 1.0;
view_width = symbol->viewBox.x1 - symbol->viewBox.x0;
view_height = symbol->viewBox.y1 - symbol->viewBox.y0;
calculatePreserveAspectRatio(symbol->aspect_align, symbol->aspect_clip, view_width, view_height,
&x, &y,&width, &height);
// [itemTransform *] translate(x, y) * scale(w/vw, h/vh) * translate(-vx, -vy);
nr_matrix_set_identity(&vb2user);
vb2user[0] = width / view_width;
vb2user[3] = height / view_height;
vb2user[4] = x - symbol->viewBox.x0 * vb2user[0];
vb2user[5] = y - symbol->viewBox.y0 * vb2user[3];
ctx->transform(&vb2user);
}
sp_group_render(item, ctx);
ctx->popState();
}
static void sp_root_render(SPItem *item, CairoRenderContext *ctx)
{
SPRoot *root = SP_ROOT(item);
CairoRenderer *renderer = ctx->getRenderer();
if (!ctx->getCurrentState()->has_overflow && SP_OBJECT(item)->parent)
ctx->addClippingRect(root->x.computed, root->y.computed, root->width.computed, root->height.computed);
ctx->pushState();
renderer->setStateForItem(ctx, item);
ctx->transform(root->c2p);
sp_group_render(item, ctx);
ctx->popState();
}
static void sp_item_invoke_render(SPItem *item, CairoRenderContext *ctx)
{
if (SP_IS_ROOT(item)) {
TRACE(("root\n"));
return sp_root_render(item, ctx);
} else if (SP_IS_GROUP(item)) {
TRACE(("group\n"));
return sp_group_render(item, ctx);
} else if (SP_IS_SHAPE(item)) {
TRACE(("shape\n"));
return sp_shape_render(item, ctx);
} else if (SP_IS_USE(item)) {
TRACE(("use begin---\n"));
sp_use_render(item, ctx);
TRACE(("---use end\n"));
} else if (SP_IS_SYMBOL(item)) {
TRACE(("symbol\n"));
return sp_symbol_render(item, ctx);
} else if (SP_IS_TEXT(item)) {
TRACE(("text\n"));
return sp_text_render(item, ctx);
} else if (SP_IS_FLOWTEXT(item)) {
TRACE(("flowtext\n"));
return sp_flowtext_render(item, ctx);
} else if (SP_IS_IMAGE(item)) {
TRACE(("image\n"));
return sp_image_render(item, ctx);
}
}
void
CairoRenderer::setStateForItem(CairoRenderContext *ctx, SPItem const *item)
{
SPStyle const *style = SP_OBJECT_STYLE(item);
ctx->setStateForStyle(style);
CairoRenderState *state = ctx->getCurrentState();
state->clip_path = item->clip_ref->getObject();
state->mask = item->mask_ref->getObject();
// If parent_has_userspace is true the parent state's transform
// has to be used for the mask's/clippath's context.
// This is so because we use the image's/(flow)text's transform for positioning
// instead of explicitly specifying it and letting the renderer do the
// transformation before rendering the item.
if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item) || SP_IS_IMAGE(item))
state->parent_has_userspace = TRUE;
TRACE(("set op: %f\n", state->opacity));
}
void
CairoRenderer::renderItem(CairoRenderContext *ctx, SPItem *item)
{
ctx->pushState();
setStateForItem(ctx, item);
CairoRenderState *state = ctx->getCurrentState();
state->need_layer = ( state->mask || state->clip_path || state->opacity != 1.0 );
if (state->need_layer) {
state->merge_opacity = FALSE;
ctx->pushLayer();
}
ctx->transform(item->transform);
sp_item_invoke_render(item, ctx);
if (state->need_layer)
ctx->popLayer();
ctx->popState();
}
bool
CairoRenderer::setupDocument(CairoRenderContext *ctx, SPDocument *doc)
{
g_assert( ctx != NULL );
if (ctx->_vector_based_target) {
// width and height in pt
ctx->_width = sp_document_width(doc) * PT_PER_PX;
ctx->_height = sp_document_height(doc) * PT_PER_PX;
} else {
ctx->_width = sp_document_width(doc);
ctx->_height = sp_document_height(doc);
}
NRRect d;
bool pageBoundingBox = true;
if (pageBoundingBox) {
d.x0 = d.y0 = 0;
d.x1 = ceil(ctx->_width);
d.y1 = ceil(ctx->_height);
} else {
SPItem* doc_item = SP_ITEM(sp_document_root(doc));
sp_item_invoke_bbox(doc_item, &d, sp_item_i2r_affine(doc_item), TRUE);
if (ctx->_vector_based_target) {
// convert from px to pt
d.x0 *= PT_PER_PX;
d.x1 *= PT_PER_PX;
d.y0 *= PT_PER_PX;
d.y1 *= PT_PER_PX;
}
}
TRACE(("%f x %f\n", ctx->_width, ctx->_height));
return ctx->setupSurface(d.x1-d.x0, d.y1-d.y0);
}
#include "macros.h" // SP_PRINT_*
void
CairoRenderer::applyClipPath(CairoRenderContext *ctx, SPClipPath const *cp)
{
g_assert( ctx != NULL && ctx->_is_valid );
if (cp == NULL)
return;
CairoRenderContext::CairoRenderMode saved_mode = ctx->getRenderMode();
ctx->setRenderMode(CairoRenderContext::RENDER_MODE_CLIP);
NRMatrix saved_ctm;
if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
NRMatrix t;
//SP_PRINT_DRECT("clipd", cp->display->bbox);
NRRect clip_bbox(cp->display->bbox);
nr_matrix_set_scale(&t, clip_bbox.x1 - clip_bbox.x0, clip_bbox.y1 - clip_bbox.y0);
t.c[4] = clip_bbox.x0;
t.c[5] = clip_bbox.y0;
nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
ctx->getTransform(&saved_ctm);
ctx->setTransform(&t);
}
TRACE(("BEGIN clip\n"));
SPObject *co = SP_OBJECT(cp);
for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
SPItem *item = SP_ITEM(child);
renderItem(ctx, item);
}
}
TRACE(("END clip\n"));
// do clipping only if this was the first call to applyClipPath
if (ctx->getClipMode() == CairoRenderContext::CLIP_MODE_PATH
&& saved_mode == CairoRenderContext::RENDER_MODE_NORMAL)
cairo_clip(ctx->_cr);
if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX)
ctx->setTransform(&saved_ctm);
ctx->setRenderMode(saved_mode);
}
void
CairoRenderer::applyMask(CairoRenderContext *ctx, SPMask const *mask)
{
g_assert( ctx != NULL && ctx->_is_valid );
if (mask == NULL)
return;
//SP_PRINT_DRECT("maskd", &mask->display->bbox);
NRRect mask_bbox(mask->display->bbox);
// TODO: should the bbox be transformed if maskUnits != userSpaceOnUse ?
if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
NRMatrix t;
nr_matrix_set_scale(&t, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
t.c[4] = mask_bbox.x0;
t.c[5] = mask_bbox.y0;
nr_matrix_multiply(&t, &t, &ctx->getCurrentState()->transform);
ctx->setTransform(&t);
}
// clip mask contents
ctx->addClippingRect(mask_bbox.x0, mask_bbox.y0, mask_bbox.x1 - mask_bbox.x0, mask_bbox.y1 - mask_bbox.y0);
ctx->pushState();
TRACE(("BEGIN mask\n"));
SPObject *co = SP_OBJECT(mask);
for (SPObject *child = sp_object_first_child(co) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
if (SP_IS_ITEM(child)) {
SPItem *item = SP_ITEM(child);
renderItem(ctx, item);
}
}
TRACE(("END mask\n"));
ctx->popState();
}
void
calculatePreserveAspectRatio(unsigned int aspect_align, unsigned int aspect_clip, double vp_width, double vp_height,
double *x, double *y, double *width, double *height)
{
if (aspect_align == SP_ASPECT_NONE)
return;
double scalex, scaley, scale;
double new_width, new_height;
scalex = *width / vp_width;
scaley = *height / vp_height;
scale = (aspect_clip == SP_ASPECT_MEET) ? MIN(scalex, scaley) : MAX(scalex, scaley);
new_width = vp_width * scale;
new_height = vp_height * scale;
/* Now place viewbox to requested position */
switch (aspect_align) {
case SP_ASPECT_XMIN_YMIN:
break;
case SP_ASPECT_XMID_YMIN:
*x -= 0.5 * (new_width - *width);
break;
case SP_ASPECT_XMAX_YMIN:
*x -= 1.0 * (new_width - *width);
break;
case SP_ASPECT_XMIN_YMID:
*y -= 0.5 * (new_height - *height);
break;
case SP_ASPECT_XMID_YMID:
*x -= 0.5 * (new_width - *width);
*y -= 0.5 * (new_height - *height);
break;
case SP_ASPECT_XMAX_YMID:
*x -= 1.0 * (new_width - *width);
*y -= 0.5 * (new_height - *height);
break;
case SP_ASPECT_XMIN_YMAX:
*y -= 1.0 * (new_height - *height);
break;
case SP_ASPECT_XMID_YMAX:
*x -= 0.5 * (new_width - *width);
*y -= 1.0 * (new_height - *height);
break;
case SP_ASPECT_XMAX_YMAX:
*x -= 1.0 * (new_width - *width);
*y -= 1.0 * (new_height - *height);
break;
default:
break;
}
*width = new_width;
*height = new_height;
}
#include "clear-n_.h"
} /* namespace Internal */
} /* namespace Extension */
} /* namespace Inkscape */
#undef TRACE
/* End of GNU GPL code */
#endif /* HAVE_CAIRO_PDF */
/*
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:encoding=utf-8:textwidth=99 :
|