summaryrefslogtreecommitdiff
path: root/backend/impress/r_gradient.c
blob: 79636fe2f50c09d19484fa37aa14c412816258a6 (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
/* imposter (OO.org Impress viewer)
** Copyright (C) 2003-2005 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU General Public License.
*/

#include <config.h>
#include "common.h"
#include "internal.h"
#include <math.h>

#define GRAD_LINEAR 0
#define GRAD_AXIAL 1
#define GRAD_SQUARE 2
#define GRAD_RECTANGULAR 3
#define GRAD_RADIAL 4
#define GRAD_ELLIPTICAL 5

typedef struct Gradient_s {
	int type;
	ImpColor start;
	int start_intensity;
	ImpColor end;
	int end_intensity;
	int angle;
	int border;
	int steps;
	int offset_x;
	int offset_y;
} Gradient;

typedef struct Rectangle_s {
	int Left;
	int Top;
	int Right;
	int Bottom;
} Rectangle;

static void
poly_rotate (ImpPoint *poly, int n, int cx, int cy, double fAngle)
{
	int i;
	long nX, nY;

	for (i = 0; i < n; i++) {
		nX = poly->x - cx;
		nY = poly->y - cy;
		poly->x = (cos(fAngle) * nX + sin(fAngle) * nY) + cx;
		poly->y = - (sin(fAngle)* nX - cos(fAngle) * nY) + cy;
		poly++;
	}
}

static void
r_draw_gradient_simple (ImpRenderCtx *ctx, void *drw_data, Gradient *grad)
{
	Rectangle rRect = { 0, 0, ctx->pix_w - 1, ctx->pix_h - 1 };
	Rectangle aRect, aFullRect;
	ImpPoint poly[4], tempoly[2];
	ImpColor gcol;
	double fW, fH, fDX, fDY, fAngle;
	double fScanLine, fScanInc;
	long redSteps, greenSteps, blueSteps;
	long nBorder;
	int i, nSteps, nSteps2;
	int cx, cy;

	cx = rRect.Left + (rRect.Right - rRect.Left) / 2;
	cy = rRect.Top + (rRect.Bottom - rRect.Top) / 2;

	aRect = rRect;
	aRect.Top--; aRect.Left--; aRect.Bottom++; aRect.Right++;
	fW = rRect.Right - rRect.Left;
	fH = rRect.Bottom - rRect.Top;
	fAngle = (((double) grad->angle) * 3.14 / 1800.0);
	fDX = fW * fabs (cos (fAngle)) + fH * fabs (sin (fAngle));
	fDY = fH * fabs (cos (fAngle)) + fW * fabs (sin (fAngle));
	fDX = (fDX - fW) * 0.5 - 0.5;
	fDY = (fDY - fH) * 0.5 - 0.5;
	aRect.Left -= fDX;
	aRect.Right += fDX;
	aRect.Top -= fDY;
	aRect.Bottom += fDY;
	aFullRect = aRect;

	nBorder = grad->border * (aRect.Bottom - aRect.Top) / 100;
	if (grad->type == GRAD_LINEAR) {
		aRect.Top += nBorder;
	} else {
		nBorder >>= 1;
		aRect.Top += nBorder;
		aRect.Bottom -= nBorder;
	}

	if (aRect.Top > (aRect.Bottom - 1))
		aRect.Top = aRect.Bottom - 1;

	poly[0].x = aFullRect.Left;
	poly[0].y = aFullRect.Top;
	poly[1].x = aFullRect.Right;
	poly[1].y = aFullRect.Top;
	poly[2].x = aRect.Right;
	poly[2].y = aRect.Top;
	poly[3].x = aRect.Left;
	poly[3].y = aRect.Top;
	poly_rotate (&poly[0], 4, cx, cy, fAngle);

	redSteps = grad->end.red - grad->start.red;
	greenSteps = grad->end.green - grad->start.green;
	blueSteps = grad->end.blue - grad->start.blue;
	nSteps = grad->steps;
	if (nSteps == 0) {
		long mr;
		mr = aRect.Bottom - aRect.Top;
		if (mr < 50)
			nSteps = mr / 2;
		else
			nSteps = mr / 4;
		mr = abs(redSteps);
		if (abs(greenSteps) > mr) mr = abs(greenSteps);
		if (abs(blueSteps) > mr) mr = abs(blueSteps);
		if (mr < nSteps) nSteps = mr;
	}

	if (grad->type == GRAD_AXIAL) {
		if (nSteps & 1) nSteps++;
		nSteps2 = nSteps + 2;
		gcol = grad->end;
		redSteps <<= 1;
		greenSteps <<= 1;
		blueSteps <<= 1;
	} else {
		nSteps2 = nSteps + 1;
		gcol = grad->start;
	}

	fScanLine = aRect.Top;
	fScanInc  = (double)(aRect.Bottom - aRect.Top) / (double)nSteps;

	for (i = 0; i < nSteps2; i++) {
		// draw polygon
		ctx->drw->set_fg_color(drw_data, &gcol);
		ctx->drw->draw_polygon(drw_data, 1, &poly[0], 4);
		// calc next polygon
		aRect.Top = (long)(fScanLine += fScanInc);
		if (i == nSteps) {
			tempoly[0].x = aFullRect.Left;
			tempoly[0].y = aFullRect.Bottom;
			tempoly[1].x = aFullRect.Right;
			tempoly[1].y = aFullRect.Bottom;
		} else {
			tempoly[0].x = aRect.Left;
			tempoly[0].y = aRect.Top;
			tempoly[1].x = aRect.Right;
			tempoly[1].y = aRect.Top;
		}
		poly_rotate (&tempoly[0], 2, cx, cy, fAngle);
		poly[0] = poly[3];
		poly[1] = poly[2];
		poly[2] = tempoly[1];
		poly[3] = tempoly[0];
		// calc next color
		if (grad->type == GRAD_LINEAR) {
			gcol.red = grad->start.red + ((redSteps * i) / nSteps2);
			gcol.green = grad->start.green + ((greenSteps * i) / nSteps2);
			gcol.blue = grad->start.blue + ((blueSteps * i) / nSteps2);
		} else {
			if (i >= nSteps) {
				gcol.red = grad->end.red;
				gcol.green = grad->end.green;
				gcol.blue = grad->end.blue;
			} else {
				if (i <= (nSteps / 2)) {
					gcol.red = grad->end.red - ((redSteps * i) / nSteps2);
					gcol.green = grad->end.green - ((greenSteps * i) / nSteps2);
					gcol.blue = grad->end.blue - ((blueSteps * i) / nSteps2);
				} else {
					int i2 = i - nSteps / 2;
					gcol.red = grad->start.red + ((redSteps * i2) / nSteps2);
					gcol.green = grad->start.green + ((greenSteps * i2) / nSteps2);
					gcol.blue = grad->start.blue + ((blueSteps * i2) / nSteps2);
				}
			}
		}
	}
}

static void
r_draw_gradient_complex (ImpRenderCtx *ctx, void *drw_data, Gradient *grad)
{
	Rectangle rRect = { 0, 0, ctx->pix_w - 1, ctx->pix_h - 1 };
	Rectangle aRect = rRect;
	ImpColor gcol;
	ImpPoint poly[4];
	double fAngle = (((double) grad->angle) * 3.14 / 1800.0);
	long redSteps, greenSteps, blueSteps;
	long nZW, nZH;
	long bX, bY;
	long sW, sH;
	long cx, cy;
	int i;
	long nSteps;
	double sTop, sLeft, sRight, sBottom, sInc;
	int minRect;

	redSteps = grad->end.red - grad->start.red;
	greenSteps = grad->end.green - grad->start.green;
	blueSteps = grad->end.blue - grad->start.blue;

	if (grad->type == GRAD_SQUARE || grad->type == GRAD_RECTANGULAR) {
		double fW = aRect.Right - aRect.Left;
		double fH = aRect.Bottom - aRect.Top;
		double fDX = fW * fabs (cos (fAngle)) + fH * fabs (sin (fAngle));
		double fDY = fH * fabs (cos (fAngle)) + fW * fabs (sin (fAngle));
		fDX = (fDX - fW) * 0.5 - 0.5;
		fDY = (fDY - fH) * 0.5 - 0.5;
		aRect.Left -= fDX;
		aRect.Right += fDX;
		aRect.Top -= fDY;
		aRect.Bottom += fDY;
	}

	sW = aRect.Right - aRect.Left;
	sH = aRect.Bottom - aRect.Top;

	if (grad->type == GRAD_SQUARE) {
		if (sW > sH) sH = sW; else sW = sH;
	} else if (grad->type == GRAD_RADIAL) {
		sW = 0.5 + sqrt ((double)sW*(double)sW + (double)sH*(double)sH);
		sH = sW;
	} else if (grad->type == GRAD_ELLIPTICAL) {
		sW = 0.5 + (double)sW * 1.4142;
		sH = 0.5 + (double)sH * 1.4142;
	}

	nZW = (aRect.Right - aRect.Left) * grad->offset_x / 100;
	nZH = (aRect.Bottom - aRect.Top) * grad->offset_y / 100;
	bX = grad->border * sW / 100;
	bY = grad->border * sH / 100;
	cx = aRect.Left + nZW;
	cy = aRect.Top + nZH;

	sW -= bX;
	sH -= bY;

	aRect.Left = cx - ((aRect.Right - aRect.Left) >> 1);
	aRect.Top = cy - ((aRect.Bottom - aRect.Top) >> 1);

	nSteps = grad->steps;
	minRect = aRect.Right - aRect.Left;
	if (aRect.Bottom - aRect.Top < minRect) minRect = aRect.Bottom - aRect.Top;
	if (nSteps == 0) {
		long mr;
		if (minRect < 50)
			nSteps = minRect / 2;
		else
			nSteps = minRect / 4;
		mr = abs(redSteps);
		if (abs(greenSteps) > mr) mr = abs(greenSteps);
		if (abs(blueSteps) > mr) mr = abs(blueSteps);
		if (mr < nSteps) nSteps = mr;
	}

	sLeft = aRect.Left;
	sTop = aRect.Top;
	sRight = aRect.Right;
	sBottom = aRect.Bottom;
	sInc = (double) minRect / (double) nSteps * 0.5;

	gcol = grad->start;
	poly[0].x = rRect.Left;
	poly[0].y = rRect.Top;
	poly[1].x = rRect.Right;
	poly[1].y = rRect.Top;
	poly[2].x = rRect.Right;
	poly[2].y = rRect.Bottom;
	poly[3].x = rRect.Left;
	poly[3].y = rRect.Bottom;
	ctx->drw->set_fg_color(drw_data, &gcol);
	ctx->drw->draw_polygon(drw_data, 1, &poly[0], 4);

	for (i = 0; i < nSteps; i++) {
		aRect.Left = (long) (sLeft += sInc);
		aRect.Top = (long) (sTop += sInc);
		aRect.Right = (long) (sRight -= sInc);
		aRect.Bottom = (long) (sBottom -= sInc);
		if (aRect.Bottom - aRect.Top < 2 || aRect.Right - aRect.Left < 2)
			break;

		gcol.red = grad->start.red + (redSteps * (i+1) / nSteps);
		gcol.green = grad->start.green + (greenSteps * (i+1) / nSteps);
		gcol.blue = grad->start.blue + (blueSteps * (i+1) / nSteps);
		ctx->drw->set_fg_color(drw_data, &gcol);

		if (grad->type == GRAD_RADIAL || grad->type == GRAD_ELLIPTICAL) {
			ctx->drw->draw_arc(drw_data, 1, aRect.Left, aRect.Top,
				aRect.Right - aRect.Left, aRect.Bottom - aRect.Top,
				0, 360);
		} else {
			poly[0].x = aRect.Left;
			poly[0].y = aRect.Top;
			poly[1].x = aRect.Right;
			poly[1].y = aRect.Top;
			poly[2].x = aRect.Right;
			poly[2].y = aRect.Bottom;
			poly[3].x = aRect.Left;
			poly[3].y = aRect.Bottom;
			poly_rotate (&poly[0], 4, cx, cy, fAngle);
			ctx->drw->draw_polygon(drw_data, 1, &poly[0], 4);
		}
	}
}

void
r_draw_gradient (ImpRenderCtx *ctx, void *drw_data, iks *node)
{
//	GdkGC *gc;
	Gradient grad;
	char *stil, *tmp;
	iks *x;

	stil = r_get_style (ctx, node, "draw:fill-gradient-name");
	x = iks_find_with_attrib (iks_find (ctx->styles, "office:styles"),
		"draw:gradient", "draw:name", stil);
	if (x) {
		memset (&grad, 0, sizeof (Gradient));
		grad.type = -1;
		grad.offset_x = 50;
		grad.offset_y = 50;

		tmp = iks_find_attrib (x, "draw:start-color");
		if (tmp) r_parse_color (tmp, &grad.start);
		tmp = iks_find_attrib (x, "draw:start-intensity");
		if (tmp) {
			int val = atoi (tmp);
			grad.start.red = grad.start.red * val / 100;
			grad.start.green = grad.start.green * val / 100;
			grad.start.blue = grad.start.blue * val / 100;
		}
		tmp = iks_find_attrib (x, "draw:end-color");
		if (tmp) r_parse_color (tmp, &grad.end);
		tmp = iks_find_attrib (x, "draw:end-intensity");
		if (tmp) {
			int val = atoi (tmp);
			grad.end.red = grad.end.red * val / 100;
			grad.end.green = grad.end.green * val / 100;
			grad.end.blue = grad.end.blue * val / 100;
		}
		tmp = iks_find_attrib (x, "draw:angle");
		if (tmp) grad.angle = atoi(tmp) % 3600;
		tmp = iks_find_attrib (x, "draw:border");
		if (tmp) grad.border = atoi(tmp);
		tmp = r_get_style (ctx, node, "draw:gradient-step-count");
		if (tmp) grad.steps = atoi (tmp);
		tmp = iks_find_attrib (x, "draw:cx");
		if (tmp) grad.offset_x = atoi (tmp);
		tmp = iks_find_attrib (x, "draw:cy");
		if (tmp) grad.offset_y = atoi (tmp);
		tmp = iks_find_attrib (x, "draw:style");
		if (iks_strcmp (tmp, "linear") == 0)
			grad.type = GRAD_LINEAR;
		else if (iks_strcmp (tmp, "axial") == 0)
			grad.type = GRAD_AXIAL;
		else if (iks_strcmp (tmp, "radial") == 0)
			grad.type = GRAD_RADIAL;
		else if (iks_strcmp (tmp, "rectangular") == 0)
			grad.type = GRAD_RECTANGULAR;
		else if (iks_strcmp (tmp, "ellipsoid") == 0)
			grad.type = GRAD_ELLIPTICAL;
		else if (iks_strcmp (tmp, "square") == 0)
			grad.type = GRAD_SQUARE;

		if (grad.type == -1) return;

//		gc = ctx->gc;
//		ctx->gc = gdk_gc_new (ctx->d);
//		gdk_gc_copy (ctx->gc, gc);

		if (grad.type == GRAD_LINEAR || grad.type == GRAD_AXIAL)
			r_draw_gradient_simple (ctx, drw_data, &grad);
		else
			r_draw_gradient_complex (ctx, drw_data, &grad);

//		g_object_unref (ctx->gc);
//		ctx->gc = gc;
	}
}