9a85008851029d18e77fe673223be6c535857a91
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::Rect area, bool optimize_stroke, double stroke_width)
50 {
51 NR::Point next(0,0), last(0,0);
52 NR::Point shift = area.min();
53 NR::Rect view = area;
54 view.growBy (stroke_width);
55 NR::Rect swept;
56 bool closed = false;
57 for (int i = 0; bpath[i].code != NR_END; i++) {
58 switch (bpath[i].code) {
59 case NR_MOVETO_OPEN:
60 case NR_MOVETO:
61 if (closed) cairo_close_path(ct);
62 closed = (bpath[i].code == NR_MOVETO);
63 next[NR::X] = bpath[i].x3;
64 next[NR::Y] = bpath[i].y3;
65 next *= trans;
66 last = next;
67 next -= shift;
68 cairo_move_to(ct, next[NR::X], next[NR::Y]);
69 break;
71 case NR_LINETO:
72 next[NR::X] = bpath[i].x3;
73 next[NR::Y] = bpath[i].y3;
74 next *= trans;
75 if (optimize_stroke) {
76 swept = NR::Rect(last, next);
77 //std::cout << "swept: " << swept;
78 //std::cout << "view: " << view;
79 //std::cout << "intersects? " << (swept.intersects(view)? "YES" : "NO") << "\n";
80 }
81 last = next;
82 next -= shift;
83 if (!optimize_stroke || swept.intersects(view))
84 cairo_line_to(ct, next[NR::X], next[NR::Y]);
85 else
86 cairo_move_to(ct, next[NR::X], next[NR::Y]);
87 break;
89 case NR_CURVETO: {
90 NR::Point tm1, tm2, tm3;
91 tm1[0]=bpath[i].x1;
92 tm1[1]=bpath[i].y1;
93 tm2[0]=bpath[i].x2;
94 tm2[1]=bpath[i].y2;
95 tm3[0]=bpath[i].x3;
96 tm3[1]=bpath[i].y3;
97 tm1 *= trans;
98 tm2 *= trans;
99 tm3 *= trans;
100 if (optimize_stroke) {
101 swept = NR::Rect(last, last);
102 swept.expandTo(tm1);
103 swept.expandTo(tm2);
104 swept.expandTo(tm3);
105 }
106 last = tm3;
107 tm1 -= shift;
108 tm2 -= shift;
109 tm3 -= shift;
110 if (!optimize_stroke || swept.intersects(view))
111 cairo_curve_to (ct, tm1[NR::X], tm1[NR::Y], tm2[NR::X], tm2[NR::Y], tm3[NR::X], tm3[NR::Y]);
112 else
113 cairo_move_to(ct, tm3[NR::X], tm3[NR::Y]);
114 break;
115 }
117 default:
118 break;
119 }
120 }
121 }
124 /*
125 Local Variables:
126 mode:c++
127 c-file-style:"stroustrup"
128 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
129 indent-tabs-mode:nil
130 fill-column:99
131 End:
132 */
133 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :