1 /*
2 * Helper functions to use cairo with inkscape
3 *
4 * Copyright (C) 2007 bulia byak
5 *
6 * Released under GNU GPL
7 *
8 */
10 #include <cairo.h>
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
15 #include <libnr/n-art-bpath.h>
16 #include <libnr/nr-matrix-ops.h>
17 #include <libnr/nr-matrix-fns.h>
18 #include <libnr/nr-pixblock.h>
19 #include "../style.h"
20 #include "nr-arena.h"
23 /** Creates a cairo context to render to the given pixblock on the given area */
24 cairo_t *
25 nr_create_cairo_context (NRRectL *area, NRPixBlock *pb)
26 {
27 if (!nr_rect_l_test_intersect (&pb->area, area))
28 return NULL;
30 NRRectL clip;
31 nr_rect_l_intersect (&clip, &pb->area, area);
32 unsigned char *dpx = NR_PIXBLOCK_PX (pb) + (clip.y0 - pb->area.y0) * pb->rs + NR_PIXBLOCK_BPP (pb) * (clip.x0 - pb->area.x0);
33 int width = area->x1 - area->x0;
34 int height = area->y1 - area->y0;
35 // even though cairo cannot draw in nonpremul mode, select ARGB32 for R8G8B8A8N as the closest; later eliminate R8G8B8A8N everywhere
36 cairo_surface_t* cst = cairo_image_surface_create_for_data
37 (dpx,
38 ((pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8P || pb->mode == NR_PIXBLOCK_MODE_R8G8B8A8N) ? CAIRO_FORMAT_ARGB32 : (pb->mode == NR_PIXBLOCK_MODE_R8G8B8? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_A8)),
39 width,
40 height,
41 pb->rs);
42 cairo_t *ct = cairo_create (cst);
44 return ct;
45 }
47 /** Feeds path-creating calls to the cairo context translating them from the SPCurve, with the given transform and shift */
48 void
49 feed_curve_to_cairo (cairo_t *ct, NArtBpath *bpath, NR::Matrix trans, NR::Maybe<NR::Rect> area, bool optimize_stroke, double stroke_width)
50 {
51 NR::Point next(0,0), last(0,0);
52 if (!area || area->isEmpty())
53 return;
54 NR::Point shift = area->min();
55 NR::Rect view = *area;
56 view.growBy (stroke_width);
57 NR::Rect swept;
58 bool closed = false;
59 for (int i = 0; bpath[i].code != NR_END; i++) {
60 switch (bpath[i].code) {
61 case NR_MOVETO_OPEN:
62 case NR_MOVETO:
63 if (closed) cairo_close_path(ct);
64 closed = (bpath[i].code == NR_MOVETO);
65 next[NR::X] = bpath[i].x3;
66 next[NR::Y] = bpath[i].y3;
67 next *= trans;
68 last = next;
69 next -= shift;
70 cairo_move_to(ct, next[NR::X], next[NR::Y]);
71 break;
73 case NR_LINETO:
74 next[NR::X] = bpath[i].x3;
75 next[NR::Y] = bpath[i].y3;
76 next *= trans;
77 if (optimize_stroke) {
78 swept = NR::Rect(last, next);
79 //std::cout << "swept: " << swept;
80 //std::cout << "view: " << view;
81 //std::cout << "intersects? " << (swept.intersects(view)? "YES" : "NO") << "\n";
82 }
83 last = next;
84 next -= shift;
85 if (!optimize_stroke || swept.intersects(view))
86 cairo_line_to(ct, next[NR::X], next[NR::Y]);
87 else
88 cairo_move_to(ct, next[NR::X], next[NR::Y]);
89 break;
91 case NR_CURVETO: {
92 NR::Point tm1, tm2, tm3;
93 tm1[0]=bpath[i].x1;
94 tm1[1]=bpath[i].y1;
95 tm2[0]=bpath[i].x2;
96 tm2[1]=bpath[i].y2;
97 tm3[0]=bpath[i].x3;
98 tm3[1]=bpath[i].y3;
99 tm1 *= trans;
100 tm2 *= trans;
101 tm3 *= trans;
102 if (optimize_stroke) {
103 swept = NR::Rect(last, last);
104 swept.expandTo(tm1);
105 swept.expandTo(tm2);
106 swept.expandTo(tm3);
107 }
108 last = tm3;
109 tm1 -= shift;
110 tm2 -= shift;
111 tm3 -= shift;
112 if (!optimize_stroke || swept.intersects(view))
113 cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
114 else
115 cairo_move_to(ct, tm3[NR::X], tm3[NR::Y]);
116 break;
117 }
119 default:
120 break;
121 }
122 }
123 }
126 /*
127 Local Variables:
128 mode:c++
129 c-file-style:"stroustrup"
130 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
131 indent-tabs-mode:nil
132 fill-column:99
133 End:
134 */
135 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :