e4f2c7b755ed5d7f444ec413400ee8642b9437f8
1 #define __SP_CANVAS_UTILS_C__
3 /*
4 * Helper stuff for SPCanvas
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 authors
10 * Copyright (C) 2001-2002 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
16 #include "libnr/nr-matrix-div.h"
17 #include "libnr/nr-pixops.h"
18 #include "sp-canvas-util.h"
20 #include <livarot/Shape.h>
21 #include <livarot/int-line.h>
22 #include <livarot/BitLigne.h>
24 void
25 sp_canvas_update_bbox (SPCanvasItem *item, int x1, int y1, int x2, int y2)
26 {
27 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
28 item->x1 = x1;
29 item->y1 = y1;
30 item->x2 = x2;
31 item->y2 = y2;
32 sp_canvas_request_redraw (item->canvas, (int)item->x1, (int)item->y1, (int)item->x2, (int)item->y2);
33 }
35 void
36 sp_canvas_item_reset_bounds (SPCanvasItem *item)
37 {
38 item->x1 = 0.0;
39 item->y1 = 0.0;
40 item->x2 = 0.0;
41 item->y2 = 0.0;
42 }
44 void
45 sp_canvas_prepare_buffer (SPCanvasBuf *buf)
46 {
47 if (buf->is_empty) {
48 sp_canvas_clear_buffer(buf);
49 buf->is_empty = false;
50 }
51 }
53 void
54 sp_canvas_clear_buffer (SPCanvasBuf *buf)
55 {
56 unsigned char r, g, b;
58 r = (buf->bg_color >> 16) & 0xff;
59 g = (buf->bg_color >> 8) & 0xff;
60 b = buf->bg_color & 0xff;
62 if ((r != g) || (r != b)) {
63 int x, y;
64 for (y = buf->rect.y0; y < buf->rect.y1; y++) {
65 unsigned char *p;
66 p = buf->buf + (y - buf->rect.y0) * buf->buf_rowstride;
67 for (x = buf->rect.x0; x < buf->rect.x1; x++) {
68 *p++ = r;
69 *p++ = g;
70 *p++ = b;
71 }
72 }
73 } else {
74 int y;
75 for (y = buf->rect.y0; y < buf->rect.y1; y++) {
76 memset (buf->buf + (y - buf->rect.y0) * buf->buf_rowstride, r, 3 * (buf->rect.x1 - buf->rect.x0));
77 }
78 }
79 }
81 NR::Matrix sp_canvas_item_i2p_affine (SPCanvasItem * item)
82 {
83 g_assert (item != NULL); // this may be overly zealous - it is
84 // plausible that this gets called
85 // with item == 0
87 return item->xform;
88 }
90 NR::Matrix sp_canvas_item_i2i_affine (SPCanvasItem * from, SPCanvasItem * to)
91 {
92 g_assert (from != NULL);
93 g_assert (to != NULL);
95 return sp_canvas_item_i2w_affine(from) / sp_canvas_item_i2w_affine(to);
96 }
98 void sp_canvas_item_set_i2w_affine (SPCanvasItem * item, NR::Matrix const &i2w)
99 {
100 g_assert (item != NULL);
102 sp_canvas_item_affine_absolute(item, i2w / sp_canvas_item_i2w_affine(item->parent));
103 }
105 void sp_canvas_item_move_to_z (SPCanvasItem * item, gint z)
106 {
107 g_assert (item != NULL);
109 gint current_z = sp_canvas_item_order (item);
111 if (current_z == -1) // not found in its parent
112 return;
114 if (z == current_z)
115 return;
117 if (z > current_z)
118 sp_canvas_item_raise (item, z - current_z);
120 sp_canvas_item_lower (item, current_z - z);
121 }
123 gint
124 sp_canvas_item_compare_z (SPCanvasItem * a, SPCanvasItem * b)
125 {
126 const gint o_a = sp_canvas_item_order (a);
127 const gint o_b = sp_canvas_item_order (b);
129 if (o_a > o_b) return -1;
130 if (o_a < o_b) return 1;
132 return 0;
133 }
135 // These two functions are used by canvasitems that use livarot (currently ctrlline and ctrlquadr)
137 void
138 ctrl_run_A8_OR (raster_info &dest,void *data,int st,float vst,int en,float ven)
139 {
140 union {
141 uint8_t comp[4];
142 uint32_t col;
143 } tempCol;
144 if ( st >= en ) return;
145 tempCol.col=*(uint32_t*)data;
147 unsigned int r, g, b, a;
148 r = NR_RGBA32_R (tempCol.col);
149 g = NR_RGBA32_G (tempCol.col);
150 b = NR_RGBA32_B (tempCol.col);
151 a = NR_RGBA32_A (tempCol.col);
152 if (a == 0) return;
154 vst*=a;
155 ven*=a;
157 if ( vst < 0 ) vst=0;
158 if ( vst > 255 ) vst=255;
159 if ( ven < 0 ) ven=0;
160 if ( ven > 255 ) ven=255;
161 float sv=vst;
162 float dv=ven-vst;
163 int len=en-st;
164 uint8_t* d=(uint8_t*)dest.buffer;
166 d+=3*(st-dest.startPix);
167 if ( fabs(dv) < 0.001 ) {
168 if ( sv > 249.999 ) {
169 /* Simple copy */
170 while (len > 0) {
171 d[0] = INK_COMPOSE (r, 255, d[0]);
172 d[1] = INK_COMPOSE (g, 255, d[1]);
173 d[2] = INK_COMPOSE (b, 255, d[2]);
174 d += 3;
175 len -= 1;
176 }
177 } else {
178 unsigned int c0_24=(int)sv;
179 c0_24&=0xFF;
180 while (len > 0) {
181 d[0] = INK_COMPOSE (r, c0_24, d[0]);
182 d[1] = INK_COMPOSE (g, c0_24, d[1]);
183 d[2] = INK_COMPOSE (b, c0_24, d[2]);
184 d += 3;
185 len -= 1;
186 }
187 }
188 } else {
189 if ( en <= st+1 ) {
190 sv=0.5*(vst+ven);
191 unsigned int c0_24=(int)sv;
192 c0_24&=0xFF;
193 d[0] = INK_COMPOSE (r, c0_24, d[0]);
194 d[1] = INK_COMPOSE (g, c0_24, d[1]);
195 d[2] = INK_COMPOSE (b, c0_24, d[2]);
196 } else {
197 dv/=len;
198 sv+=0.5*dv; // correction trapezoidale
199 sv*=65536;
200 dv*=65536;
201 int c0_24 = static_cast<int>(CLAMP(sv, 0, 16777216));
202 int s0_24 = static_cast<int>(dv);
203 while (len > 0) {
204 unsigned int ca;
205 /* Draw */
206 ca = c0_24 >> 16;
207 if ( ca > 255 ) ca=255;
208 d[0] = INK_COMPOSE (r, ca, d[0]);
209 d[1] = INK_COMPOSE (g, ca, d[1]);
210 d[2] = INK_COMPOSE (b, ca, d[2]);
211 d += 3;
212 c0_24 += s0_24;
213 c0_24 = CLAMP (c0_24, 0, 16777216);
214 len -= 1;
215 }
216 }
217 }
218 }
220 void nr_pixblock_render_ctrl_rgba (Shape* theS,uint32_t color,NRRectL &area,char* destBuf,int stride)
221 {
223 theS->CalcBBox();
224 float l=theS->leftX,r=theS->rightX,t=theS->topY,b=theS->bottomY;
225 int il,ir,it,ib;
226 il=(int)floor(l);
227 ir=(int)ceil(r);
228 it=(int)floor(t);
229 ib=(int)ceil(b);
231 // printf("bbox %i %i %i %i render %i %i %i %i\n",il,it,ir,ib,area.x0,area.y0,area.x1,area.y1);
233 if ( il >= area.x1 || ir <= area.x0 || it >= area.y1 || ib <= area.y0 ) return;
234 if ( il < area.x0 ) il=area.x0;
235 if ( it < area.y0 ) it=area.y0;
236 if ( ir > area.x1 ) ir=area.x1;
237 if ( ib > area.y1 ) ib=area.y1;
239 /* // version par FloatLigne
240 int curPt;
241 float curY;
242 theS->BeginRaster(curY,curPt,1.0);
244 FloatLigne* theI=new FloatLigne();
245 IntLigne* theIL=new IntLigne();
247 theS->Scan(curY,curPt,(float)(it),1.0);
249 char* mdata=(char*)destBuf;
250 uint32_t* ligStart=((uint32_t*)(mdata+(3*(il-area.x0)+stride*(it-area.y0))));
251 for (int y=it;y<ib;y++) {
252 theI->Reset();
253 if ( y&0x00000003 ) {
254 theS->Scan(curY,curPt,((float)(y+1)),theI,false,1.0);
255 } else {
256 theS->Scan(curY,curPt,((float)(y+1)),theI,true,1.0);
257 }
258 theI->Flatten();
259 theIL->Copy(theI);
261 raster_info dest;
262 dest.startPix=il;
263 dest.endPix=ir;
264 dest.sth=il;
265 dest.stv=y;
266 dest.buffer=ligStart;
267 theIL->Raster(dest,&color,bpath_run_A8_OR);
268 ligStart=((uint32_t*)(((char*)ligStart)+stride));
269 }
270 theS->EndRaster();
271 delete theI;
272 delete theIL; */
274 // version par BitLigne directe
275 int curPt;
276 float curY;
277 theS->BeginQuickRaster(curY, curPt);
279 BitLigne* theI[4];
280 for (int i=0;i<4;i++) theI[i]=new BitLigne(il,ir);
281 IntLigne* theIL=new IntLigne();
283 theS->QuickScan(curY,curPt,(float)(it),true,0.25);
285 char* mdata=(char*)destBuf;
286 uint32_t* ligStart=((uint32_t*)(mdata+(3*(il-area.x0)+stride*(it-area.y0))));
287 for (int y=it;y<ib;y++) {
288 for (int i=0;i<4;i++) theI[i]->Reset();
289 theS->QuickScan(curY,curPt,((float)(y+0.25)),fill_oddEven,theI[0],0.25);
290 theS->QuickScan(curY,curPt,((float)(y+0.5)),fill_oddEven,theI[1],0.25);
291 theS->QuickScan(curY,curPt,((float)(y+0.75)),fill_oddEven,theI[2],0.25);
292 theS->QuickScan(curY,curPt,((float)(y+1.0)),fill_oddEven,theI[3],0.25);
293 theIL->Copy(4,theI);
295 raster_info dest;
296 dest.startPix=il;
297 dest.endPix=ir;
298 dest.sth=il;
299 dest.stv=y;
300 dest.buffer=ligStart;
301 theIL->Raster(dest,&color,ctrl_run_A8_OR);
302 ligStart=((uint32_t*)(((char*)ligStart)+stride));
303 }
304 theS->EndQuickRaster();
305 for (int i=0;i<4;i++) delete theI[i];
306 delete theIL;
307 }