summaryrefslogtreecommitdiff
path: root/framebuffer/fb_plotters.c
blob: 46ef82a95ed5124d672cd7239575947169e9d13e (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
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
/*
 * Copyright 2008 Vincent Sanders <vince@simtec.co.uk>
 * Copyright 2009 Michael Drake <tlsa@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>

#include "utils/log.h"
#include "utils/utf8.h"
#include "desktop/browser.h"
#include "desktop/plotters.h"

#include "framebuffer/fb_gui.h"
#include "framebuffer/fb_tk.h"
#include "framebuffer/fb_plotters.h"
#include "framebuffer/fb_bitmap.h"
#include "framebuffer/fb_font.h"
#include "framebuffer/fb_frontend.h"

extern fbtk_widget_t *fbtk;

/* max height the poly plotter can cope with */
#define WINDOW_HEIGHT (2048)

/* Currently selected ploting routines. */
struct plotter_table plot;

/* Current plotting context */
bbox_t fb_plot_ctx;

enum {
        POINT_LEFTOF_REGION = 1,
        POINT_RIGHTOF_REGION = 2,
        POINT_ABOVE_REGION = 4,
        POINT_BELOW_REGION = 8,
};

#define REGION(x,y,cx1,cx2,cy1,cy2) ( ( (y) > (cy2) ? POINT_BELOW_REGION : 0) |  \
                                      ( (y) < (cy1) ? POINT_ABOVE_REGION : 0) |  \
                                      ( (x) > (cx2) ? POINT_RIGHTOF_REGION : 0) |  \
                                      ( (x) < (cx1) ? POINT_LEFTOF_REGION : 0) )

#define SWAP(a, b) do { int t; t=(a); (a)=(b); (b)=t;  } while(0)

/* clip a rectangle to another rectangle */
bool fb_plotters_clip_rect(const bbox_t * restrict clip,
                           int * restrict x0, int * restrict y0, 
                           int * restrict x1, int * restrict y1)
{
        char region1;
        char region2;

	if (*x1 < *x0) SWAP(*x0, *x1);

	if (*y1 < *y0) SWAP(*y0, *y1);

	region1 = REGION(*x0, *y0, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
	region2 = REGION(*x1, *y1, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);

        /* area lies entirely outside the clipping rectangle */
        if ((region1 | region2) && (region1 & region2))
                return false;

        if (*x0 < clip->x0)
                *x0 = clip->x0;
        if (*x0 > clip->x1)
                *x0 = clip->x1;

        if (*x1 < clip->x0)
                *x1 = clip->x0;
        if (*x1 > clip->x1)
                *x1 = clip->x1;

        if (*y0 < clip->y0)
                *y0 = clip->y0;
        if (*y0 > clip->y1)
                *y0 = clip->y1;

        if (*y1 < clip->y0)
                *y1 = clip->y0;
        if (*y1 > clip->y1)
                *y1 = clip->y1;

        return true;
}

bool fb_plotters_clip_rect_ctx(int *x0, int *y0, int *x1, int *y1)
{
        return fb_plotters_clip_rect(&fb_plot_ctx, x0, y0, x1, y1);
}

/** Clip a line to a bounding box.
 */
bool fb_plotters_clip_line(const bbox_t *clip,
                           int *x0, int *y0, int *x1, int *y1)
{
        char region1;
        char region2;
        region1 = REGION(*x0, *y0, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
        region2 = REGION(*x1, *y1, clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);

        while (region1 | region2) {
                if (region1 & region2) {
                        /* line lies entirely outside the clipping rectangle */
                        return false;
                }

                if (region1) {
                        /* first point */
                        if (region1 & POINT_BELOW_REGION) {
                                /* divide line at bottom */
                                *x0 = (*x0 + (*x1 - *x0) *
                                       (clip->y1 - 1 - *y0) / (*y1-*y0));
                                *y0 = clip->y1 - 1;
                        } else if (region1 & POINT_ABOVE_REGION) {
                                /* divide line at top */
                                *x0 = (*x0 + (*x1 - *x0) *
                                       (clip->y0 - *y0) / (*y1-*y0));
                                *y0 = clip->y0;
                        } else if (region1 & POINT_RIGHTOF_REGION) {
                                /* divide line at right */
                                *y0 = (*y0 + (*y1 - *y0) *
                                       (clip->x1  - 1 - *x0) / (*x1-*x0));
                                *x0 = clip->x1 - 1;
                        } else if (region1 & POINT_LEFTOF_REGION) {
                                /* divide line at right */
                                *y0 = (*y0 + (*y1 - *y0) *
                                       (clip->x0 - *x0) / (*x1-*x0));
                                *x0 = clip->x0;
                        }

                        region1 = REGION(*x0, *y0,
                                         clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
                } else {
                        /* second point */
                        if (region2 & POINT_BELOW_REGION) {
                                /* divide line at bottom*/
                                *x1 = (*x0 + (*x1 - *x0) *
                                       (clip->y1  - 1 - *y0) / (*y1-*y0));
                                *y1 = clip->y1 - 1;
                        } else if (region2 & POINT_ABOVE_REGION) {
                                /* divide line at top*/
                                *x1 = (*x0 + (*x1 - *x0) *
                                       (clip->y0 - *y0) / (*y1-*y0));
                                *y1 = clip->y0;
                        } else if (region2 & POINT_RIGHTOF_REGION) {
                                /* divide line at right*/
                                *y1 = (*y0 + (*y1 - *y0) *
                                       (clip->x1  - 1 - *x0) / (*x1 - *x0));
                                *x1 = clip->x1 - 1;
                        } else if (region2 & POINT_LEFTOF_REGION) {
                                /* divide line at right*/
                                *y1 = (*y0 + (*y1 - *y0) *
                                       (clip->x0 - *x0) / (*x1 - *x0));
                                *x1 = clip->x0;
                        }

                        region2 = REGION(*x1, *y1,
                                         clip->x0, clip->x1 - 1, clip->y0, clip->y1 - 1);
                }
        }

        return true;
}

bool fb_plotters_clip_line_ctx(int *x0, int *y0, int *x1, int *y1)
{
        return fb_plotters_clip_line(&fb_plot_ctx, x0, y0, x1, y1);
}

/* generic setting of clipping rectangle */
bool fb_clip(int x0, int y0, int x1, int y1)
{
        bbox_t clip;

	if (x1 < x0) SWAP(x0, x1);
	if (y1 < y0) SWAP(y0, y1);

        clip.x0 = fbtk_get_x(fbtk);
        clip.y0 = fbtk_get_y(fbtk);
        clip.x1 = fbtk_get_width(fbtk);
        clip.y1 = fbtk_get_height(fbtk);

        if (fb_plotters_clip_rect(&clip, &x0, &y0, &x1, &y1)) {
                /* new clipping region is inside the root window */
                fb_plot_ctx.x0 = x0;
                fb_plot_ctx.y0 = y0;
                fb_plot_ctx.x1 = x1;
                fb_plot_ctx.y1 = y1;
                 }

        /*LOG(("%d, %d - %d, %d clipped to %d, %d - %d, %d",
             x0,y0,x1,y1,
             fb_plot_ctx.x0, fb_plot_ctx.y0, fb_plot_ctx.x1, fb_plot_ctx.y1)); */

	return true;
}

typedef bool (linefn_t)(int x0, int y0, int x1, int y1, int width, colour c,
		bool dotted, bool dashed);


/**
 * Find find first filled span along horizontal line at given coordinate
 *
 * \param  p	array of polygon vertices (x1, y1, x2, y2, ... , xN, yN)
 * \param  n	number of polygon vertex values (N * 2)
 * \param  x	current position along current scan line
 * \param  y	position of current scan line
 * \param  x0	updated to start of filled area
 * \param  x1	updated to end of filled area
 * \return true	if an intersection was found
 */

static bool fb_plotters_find_span(const int *p, int n, int x, int y,
		int *x0, int *x1)
{
	int i;
	int p_x0, p_y0;
	int p_x1, p_y1;
	int x_new;
	bool direction = false;

	*x0 = *x1 = INT_MAX;

	for (i = 0; i < n; i = i + 2) {
		/* get line endpoints */
		if (i != n - 2) {
			/* not the last line */
			p_x0 = p[i];		p_y0 = p[i + 1];
			p_x1 = p[i + 2];	p_y1 = p[i + 3];
		} else {
			/* last line; 2nd endpoint is first vertex */
			p_x0 = p[i];	p_y0 = p[i + 1];
			p_x1 = p[0];	p_y1 = p[1];
		}
		/* ignore horizontal lines */
		if (p_y0 == p_y1)
			continue;

		/* ignore lines that don't cross this y level */
		if ((y < p_y0 && y < p_y1) || (y > p_y0 && y > p_y1))
			continue;

		if (p_x0 == p_x1) {
			/* vertical line, x is constant */
			x_new = p_x0;
		} else {
			/* find intersect */
			x_new = p_x0 + ((long long)(y - p_y0) * (p_x1 - p_x0)) /
					(p_y1 - p_y0);
		}

		/* ignore intersections before current x */
		if (x_new < x)
			continue;

		/* set nearest intersections as filled area endpoints */
		if (x_new < *x0) {
			/* nearer than first endpoint */
			*x1 = *x0;
			*x0 = x_new;
			direction = (p_y0 > p_y1);
		} else if (x_new == *x0) {
			/* same as first endpoint */
			if ((p_y0 > p_y1) != direction)
				*x1 = x_new;
		} else if (x_new < *x1) {
			/* nearer than second endpoint */
			*x1 = x_new;
		}

	}
	if (*x0 == INT_MAX)
		/* no span found */
		return false;

	/* span found */
	if (*x1 == INT_MAX) {
		*x1 = *x0;
		*x0 = x;
		return true;
	}

	return true;
}


/**
 * Plot a polygon
 *
 * \param  p	   array of polygon vertices (x1, y1, x2, y2, ... , xN, yN)
 * \param  n	   number of polygon vertices (N)
 * \param  c	   fill colour
 * \param  linefn  function to call to plot a horizontal line
 * \return true	   if no errors
 */

bool fb_plotters_polygon(const int *p, unsigned int n, colour c,
		linefn_t linefn)
{
	int poly_x0, poly_y0; /* Bounding box top left corner */
	int poly_x1, poly_y1; /* Bounding box bottom right corner */
	int i, j; /* indexes */
	int x0, x1; /* filled span extents */
	int y; /* current y coordinate */
	int y_max; /* bottom of plot area */

	/* find no. of vertex values */
	int v = n * 2;

	/* Can't plot polygons with 2 or fewer vertices */
	if (n <= 2)
		return true;

	/* Find polygon bounding box */
	poly_x0 = poly_x1 = *p;
	poly_y0 = poly_y1 = p[1];
	for (i = 2; i < v; i = i + 2) {
		j = i + 1;
		if (p[i] < poly_x0)
			poly_x0 = p[i];
		else if (p[i] > poly_x1)
			poly_x1 = p[i];
		if (p[j] < poly_y0)
			poly_y0 = p[j];
		else if (p[j] > poly_y1)
			poly_y1 = p[j];
	}

	/* Don't try to plot it if it's outside the clip rectangle */
	if (fb_plot_ctx.y1 < poly_y0 || fb_plot_ctx.y0 > poly_y1 ||
			fb_plot_ctx.x1 < poly_x0 || fb_plot_ctx.x0 > poly_x1)
		return true;

	/* Find the top of the important area */
	if (poly_y0 > fb_plot_ctx.y0)
		y = poly_y0;
	else
		y = fb_plot_ctx.y0;

	/* Find the bottom of the important area */
	if (poly_y1 < fb_plot_ctx.y1)
		y_max = poly_y1;
	else
		y_max = fb_plot_ctx.y1;

	for (; y < y_max; y++) {
		x1 = poly_x0;
		/* For each row */
		while (fb_plotters_find_span(p, v, x1, y, &x0, &x1)) {
			/* don't draw anything outside clip region */
			if (x1 < fb_plot_ctx.x0)
				continue;
			else if (x0 < fb_plot_ctx.x0)
				x0 = fb_plot_ctx.x0;
			if (x0 > fb_plot_ctx.x1)
				break;
			else if (x1 > fb_plot_ctx.x1)
				x1 = fb_plot_ctx.x1;

			/* draw this filled span on current row */
			linefn(x0, y, x1, y, 1, c, false, false);

			/* don't look for more spans if already at end of clip
			 * region or polygon */
			if (x1 == fb_plot_ctx.x1 || x1 == poly_x1)
				break;

			if (x0 == x1)
				x1++;
		}
	}
	return true;
}

bool fb_plotters_bitmap_tile(int x, int y,
                             int width, int height,
                             struct bitmap *bitmap, colour bg,
                             bool repeat_x, bool repeat_y,
                             struct content *content,
                             bool (bitmapfn)(int x, int y,
                                             int width, int height,
                                             struct bitmap *bitmap,
                                             colour bg,
                                             struct content *content))
{
	int xf,yf;

	/* x and y define coordinate of top left of of the initial explicitly
	 * placed tile. The width and height are the image scaling and the
	 * bounding box defines the extent of the repeat (which may go in all
	 * four directions from the initial tile).
	 */

	LOG(("x %d, y %d, width %d, height %d, bitmap %p, repx %d repy %d content %p", x,y,width,height,bitmap,repeat_x, repeat_y, content));

	if (!(repeat_x || repeat_y)) {
		/* Not repeating at all, so just pass it on */
		LOG(("Not repeating"));
		return bitmapfn(x, y, width, height, bitmap, bg,content);
	}

	/* get left most tile position */
	if (repeat_x)
		for (; x > fb_plot_ctx.x0; x -= width)
			;

	/* get top most tile position */
	if (repeat_y)
		for (; y > fb_plot_ctx.y0; y -= height)
			;

	/* tile down and across to extents */
	for (xf = x; xf < fb_plot_ctx.x1; xf += width) {
		for (yf = y; yf < fb_plot_ctx.y1; yf += height) {
			bitmapfn(xf, yf, width, height, bitmap, bg, content);
			if (!repeat_y)
				break;
		}
		if (!repeat_x)
	   		break;
	}
	return true;
}

bool fb_plotters_move_block(int srcx, int srcy, int width, int height, int dstx, int dsty)
{
	uint8_t *srcptr = (framebuffer->ptr +
                           (srcy * framebuffer->linelen) +
			   ((srcx * framebuffer->bpp) / 8));

	uint8_t *dstptr = (framebuffer->ptr +
                           (dsty * framebuffer->linelen) +
			   ((dstx * framebuffer->bpp) / 8));

        bbox_t redrawbox;
        int hloop;

	LOG(("from (%d,%d) w %d h %d to (%d,%d)",srcx,srcy,width,height,dstx,dsty));

        if (width == framebuffer->width) {
                /* take shortcut and use memmove */
                memmove(dstptr, srcptr, (width * height * framebuffer->bpp) / 8);
        } else {
                if (srcy > dsty) {
                        for (hloop = height; hloop > 0; hloop--) {
                                memmove(dstptr, srcptr, (width * framebuffer->bpp) / 8);
                                srcptr += framebuffer->linelen;
                                dstptr += framebuffer->linelen;
                        }
                } else {
                        srcptr += height * framebuffer->linelen;
                        dstptr += height * framebuffer->linelen;
                        for (hloop = height; hloop > 0; hloop--) {
                                srcptr -= framebuffer->linelen;
                                dstptr -= framebuffer->linelen;
                                memmove(dstptr, srcptr, (width * framebuffer->bpp) / 8);
                        }
                }
        }
        /* callback to the os specific routine in case it needs to do something
         * explicit to redraw
         */
        redrawbox.x0 = dstx;
        redrawbox.y0 = dsty;
        redrawbox.x1 = dstx + width;
        redrawbox.y1 = dsty + height;
        fb_os_redraw(&redrawbox);

        return true;
}

/*
 * Local Variables:
 * c-basic-offset:8
 * End:
 */