Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/libart_lgpl/art_uta_vpath.c
blob: b95a4ea657a709544dfee6de54a74aa355c61dd8 (plain)
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
/* Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 1998-2000 Raph Levien
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "art_uta_vpath.h"

#include <math.h>

#include "art_misc.h"
#include "art_vpath.h"
#include "art_uta.h"

#ifndef MAX
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
#endif /* MAX */

#ifndef MIN
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
#endif /* MIN */

/**
 * art_uta_add_line: Add a line to the uta.
 * @uta: The uta to modify.
 * @x0: X coordinate of line start point.
 * @y0: Y coordinate of line start point.
 * @x1: X coordinate of line end point.
 * @y1: Y coordinate of line end point.
 * @rbuf: Buffer containing first difference of winding number.
 * @rbuf_rowstride: Rowstride of @rbuf.
 *
 * Add the line (@x0, @y0) - (@x1, @y1) to @uta, and also update the
 * winding number buffer used for rendering the interior. @rbuf
 * contains the first partial difference (in the X direction) of the
 * winding number, measured in grid cells. Thus, each time that a line
 * crosses a horizontal uta grid line, an entry of @rbuf is
 * incremented if @y1 > @y0, decremented otherwise.
 *
 * Note that edge handling is fairly delicate. Please rtfs for
 * details.
 **/
void
art_uta_add_line (ArtUta *uta, double x0, double y0, double x1, double y1,
		  int *rbuf, int rbuf_rowstride)
{
  int xmin, ymin;
  double xmax, ymax;
  int xmaxf, ymaxf;
  int xmaxc, ymaxc;
  int xt0, yt0;
  int xt1, yt1;
  int xf0, yf0;
  int xf1, yf1;
  int ix, ix1;
  ArtUtaBbox bb;

  xmin = floor (MIN(x0, x1));
  xmax = MAX(x0, x1);
  xmaxf = floor (xmax);
  xmaxc = ceil (xmax);
  ymin = floor (MIN(y0, y1));
  ymax = MAX(y0, y1);
  ymaxf = floor (ymax);
  ymaxc = ceil (ymax);
  xt0 = (xmin >> ART_UTILE_SHIFT) - uta->x0;
  yt0 = (ymin >> ART_UTILE_SHIFT) - uta->y0;
  xt1 = (xmaxf >> ART_UTILE_SHIFT) - uta->x0;
  yt1 = (ymaxf >> ART_UTILE_SHIFT) - uta->y0;
  if (xt0 == xt1 && yt0 == yt1)
    {
      /* entirely inside a microtile, this is easy! */
      xf0 = xmin & (ART_UTILE_SIZE - 1);
      yf0 = ymin & (ART_UTILE_SIZE - 1);
      xf1 = (xmaxf & (ART_UTILE_SIZE - 1)) + xmaxc - xmaxf;
      yf1 = (ymaxf & (ART_UTILE_SIZE - 1)) + ymaxc - ymaxf;

      ix = yt0 * uta->width + xt0;
      bb = uta->utiles[ix];
      if (bb == 0)
	bb = ART_UTA_BBOX_CONS(xf0, yf0, xf1, yf1);
      else
	bb = ART_UTA_BBOX_CONS(MIN(ART_UTA_BBOX_X0(bb), xf0),
			   MIN(ART_UTA_BBOX_Y0(bb), yf0),
			   MAX(ART_UTA_BBOX_X1(bb), xf1),
			   MAX(ART_UTA_BBOX_Y1(bb), yf1));
      uta->utiles[ix] = bb;
    }
  else
    {
      double dx, dy;
      int sx, sy;

      dx = x1 - x0;
      dy = y1 - y0;
      sx = dx > 0 ? 1 : dx < 0 ? -1 : 0;
      sy = dy > 0 ? 1 : dy < 0 ? -1 : 0;
      if (ymin == ymaxf)
	{
	  /* special case horizontal (dx/dy slope would be infinite) */
	  xf0 = xmin & (ART_UTILE_SIZE - 1);
	  yf0 = ymin & (ART_UTILE_SIZE - 1);
	  xf1 = (xmaxf & (ART_UTILE_SIZE - 1)) + xmaxc - xmaxf;
	  yf1 = (ymaxf & (ART_UTILE_SIZE - 1)) + ymaxc - ymaxf;

	  ix = yt0 * uta->width + xt0;
	  ix1 = yt0 * uta->width + xt1;
	  while (ix != ix1)
	    {
	      bb = uta->utiles[ix];
	      if (bb == 0)
		bb = ART_UTA_BBOX_CONS(xf0, yf0, ART_UTILE_SIZE, yf1);
	      else
		bb = ART_UTA_BBOX_CONS(MIN(ART_UTA_BBOX_X0(bb), xf0),
				   MIN(ART_UTA_BBOX_Y0(bb), yf0),
				   ART_UTILE_SIZE,
				   MAX(ART_UTA_BBOX_Y1(bb), yf1));
	      uta->utiles[ix] = bb;
	      xf0 = 0;
	      ix++;
	    }
	  bb = uta->utiles[ix];
	  if (bb == 0)
	    bb = ART_UTA_BBOX_CONS(0, yf0, xf1, yf1);
	  else
	    bb = ART_UTA_BBOX_CONS(0,
			       MIN(ART_UTA_BBOX_Y0(bb), yf0),
			       MAX(ART_UTA_BBOX_X1(bb), xf1),
			       MAX(ART_UTA_BBOX_Y1(bb), yf1));
	  uta->utiles[ix] = bb;
	}
      else
	{
	  /* Do a Bresenham-style traversal of the line */
	  double dx_dy;
	  double x, y;
	  double xn, yn;

	  /* normalize coordinates to uta origin */
	  x0 -= uta->x0 << ART_UTILE_SHIFT;
	  y0 -= uta->y0 << ART_UTILE_SHIFT;
	  x1 -= uta->x0 << ART_UTILE_SHIFT;
	  y1 -= uta->y0 << ART_UTILE_SHIFT;
	  if (dy < 0)
	    {
	      double tmp;

	      tmp = x0;
	      x0 = x1;
	      x1 = tmp;

	      tmp = y0;
	      y0 = y1;
	      y1 = tmp;

	      dx = -dx;
	      sx = -sx;
	      dy = -dy;
	      /* we leave sy alone, because it would always be 1,
		 and we need it for the rbuf stuff. */
	    }
	  xt0 = ((int)floor (x0) >> ART_UTILE_SHIFT);
	  xt1 = ((int)floor (x1) >> ART_UTILE_SHIFT);
	  /* now [xy]0 is above [xy]1 */

	  ix = yt0 * uta->width + xt0;
	  ix1 = yt1 * uta->width + xt1;
#ifdef VERBOSE
	  printf ("%% ix = %d,%d; ix1 = %d,%d\n", xt0, yt0, xt1, yt1);
#endif

	  dx_dy = dx / dy;
	  x = x0;
	  y = y0;
	  while (ix != ix1)
	    {
	      int dix;

	      /* figure out whether next crossing is horizontal or vertical */
#ifdef VERBOSE
	      printf ("%% %d,%d\n", xt0, yt0);
#endif
	      yn = (yt0 + 1) << ART_UTILE_SHIFT;

	      /* xn is the intercept with bottom edge of this tile. The
		 following expression is careful to result in exactly
		 x1 when yn = y1. */
	      xn = x1 + dx_dy * (yn - y1);

	      if (xt0 != (int)floor (xn) >> ART_UTILE_SHIFT)
		{
		  /* horizontal crossing */
		  xt0 += sx;
		  dix = sx;
		  if (dx > 0)
		    {
		      xn = xt0 << ART_UTILE_SHIFT;
		      yn = y0 + (xn - x0) / dx_dy;

		      xf0 = (int)floor (x) & (ART_UTILE_SIZE - 1);
		      xf1 = ART_UTILE_SIZE;
		    }
		  else
		    {
		      xn = (xt0 + 1) << ART_UTILE_SHIFT;
		      yn = y0 + (xn - x0) / dx_dy;

		      xf0 = 0;
		      xmaxc = (int)ceil (x);
		      xf1 = xmaxc - ((xt0 + 1) << ART_UTILE_SHIFT);
		    }
		  ymaxf = (int)floor (yn);
		  ymaxc = (int)ceil (yn);
		  yf1 = (ymaxf & (ART_UTILE_SIZE - 1)) + ymaxc - ymaxf;
		}
	      else
		{
		  /* vertical crossing */
		  dix = uta->width;
		  xf0 = (int)floor (MIN(x, xn)) & (ART_UTILE_SIZE - 1);
		  xmax = MAX(x, xn);
		  xmaxc = (int)ceil (xmax);
		  xf1 = xmaxc - (xt0 << ART_UTILE_SHIFT);
		  yf1 = ART_UTILE_SIZE;

		  if (rbuf != NULL)
		    rbuf[yt0 * rbuf_rowstride + xt0] += sy;

		  yt0++;
		}
	      yf0 = (int)floor (y) & (ART_UTILE_SIZE - 1);
	      bb = uta->utiles[ix];
	      if (bb == 0)
		bb = ART_UTA_BBOX_CONS(xf0, yf0, xf1, yf1);
	      else
		bb = ART_UTA_BBOX_CONS(MIN(ART_UTA_BBOX_X0(bb), xf0),
				       MIN(ART_UTA_BBOX_Y0(bb), yf0),
				       MAX(ART_UTA_BBOX_X1(bb), xf1),
				       MAX(ART_UTA_BBOX_Y1(bb), yf1));
	      uta->utiles[ix] = bb;

	      x = xn;
	      y = yn;
	      ix += dix;
	    }
	  xmax = MAX(x, x1);
	  xmaxc = ceil (xmax);
	  ymaxc = ceil (y1);
	  xf0 = (int)floor (MIN(x1, x)) & (ART_UTILE_SIZE - 1);
	  yf0 = (int)floor (y) & (ART_UTILE_SIZE - 1);
	  xf1 = xmaxc - (xt0 << ART_UTILE_SHIFT);
	  yf1 = ymaxc - (yt0 << ART_UTILE_SHIFT);
	  bb = uta->utiles[ix];
	  if (bb == 0)
	    bb = ART_UTA_BBOX_CONS(xf0, yf0, xf1, yf1);
	  else
	    bb = ART_UTA_BBOX_CONS(MIN(ART_UTA_BBOX_X0(bb), xf0),
				   MIN(ART_UTA_BBOX_Y0(bb), yf0),
				   MAX(ART_UTA_BBOX_X1(bb), xf1),
				   MAX(ART_UTA_BBOX_Y1(bb), yf1));
	  uta->utiles[ix] = bb;
	}
    }
}

/**
 * art_uta_from_vpath: Generate uta covering a vpath.
 * @vec: The source vpath.
 *
 * Generates a uta covering @vec. The resulting uta is of course
 * approximate, ie it may cover more pixels than covered by @vec.
 *
 * Return value: the new uta.
 **/
ArtUta *
art_uta_from_vpath (const ArtVpath *vec)
{
  ArtUta *uta;
  ArtIRect bbox;
  int *rbuf;
  int i;
  double x, y;
  int sum;
  int xt, yt;
  ArtUtaBbox *utiles;
  ArtUtaBbox bb;
  int width;
  int height;
  int ix;

  art_vpath_bbox_irect (vec, &bbox);

  uta = art_uta_new_coords (bbox.x0, bbox.y0, bbox.x1, bbox.y1);

  width = uta->width;
  height = uta->height;
  utiles = uta->utiles;

  rbuf = art_new (int, width * height);
  for (i = 0; i < width * height; i++)
    rbuf[i] = 0;

  x = 0;
  y = 0;
  for (i = 0; vec[i].code != ART_END; i++)
    {
      switch (vec[i].code)
	{
	case ART_MOVETO:
	  x = vec[i].x;
	  y = vec[i].y;
	  break;
	case ART_LINETO:
	  art_uta_add_line (uta, vec[i].x, vec[i].y, x, y, rbuf, width);
	  x = vec[i].x;
	  y = vec[i].y;
	  break;
	default:
	  /* this shouldn't happen */
          art_free (rbuf);
          art_free (uta);
          return NULL;
	}
    }

  /* now add in the filling from rbuf */
  ix = 0;
  for (yt = 0; yt < height; yt++)
    {
      sum = 0;
      for (xt = 0; xt < width; xt++)
	{
	  sum += rbuf[ix];
	  /* Nonzero winding rule - others are possible, but hardly
	     worth it. */
	  if (sum != 0)
	    {
	      bb = utiles[ix];
	      bb &= 0xffff0000;
	      bb |= (ART_UTILE_SIZE << 8) | ART_UTILE_SIZE;
	      utiles[ix] = bb;
	      if (xt != width - 1)
		{
		  bb = utiles[ix + 1];
		  bb &= 0xffff00;
		  bb |= ART_UTILE_SIZE;
		  utiles[ix + 1] = bb;
		}
	      if (yt != height - 1)
		{
		  bb = utiles[ix + width];
		  bb &= 0xff0000ff;
		  bb |= ART_UTILE_SIZE << 8;
		  utiles[ix + width] = bb;
		  if (xt != width - 1)
		    {
		      utiles[ix + width + 1] &= 0xffff;
		    }
		}
	    }
	  ix++;
	}
    }

  art_free (rbuf);

  return uta;
}