summaryrefslogtreecommitdiff
path: root/hieroglyph/hggstate.c
blob: e3f40cd0a6064acbec327bb97117f9fc4cb710e5 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * hggsate.c
 * Copyright (C) 2006-2011 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <akira@tagoh.org>
 * 
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include "hgarray.h"
#include "hgint.h"
#include "hgmem.h"
#include "hgreal.h"
#include "hggstate.h"

#include "hggstate.proto.h"

HG_DEFINE_VTABLE_WITH (gstate, NULL, NULL, NULL);

/*< private >*/
static hg_usize_t
_hg_object_gstate_get_capsulated_size(void)
{
	return HG_ALIGNED_TO_POINTER (sizeof (hg_gstate_t));
}

static hg_uint_t
_hg_object_gstate_get_allocation_flags(void)
{
	return HG_MEM_FLAGS_DEFAULT;
}

static hg_bool_t
_hg_object_gstate_initialize(hg_object_t *object,
			     va_list      args)
{
	hg_gstate_t *gstate = (hg_gstate_t *)object;

	gstate->qpath = Qnil;
	gstate->qclippath = Qnil;
	gstate->qdashpattern = Qnil;

	return TRUE;
}

static hg_quark_t
_hg_object_gstate_copy(hg_object_t             *object,
		       hg_quark_iterate_func_t  func,
		       hg_pointer_t             user_data,
		       hg_pointer_t            *ret)
{
	hg_gstate_t *gstate = (hg_gstate_t *)object, *g = NULL;
	hg_quark_t retval;

	hg_return_val_if_fail (object->type == HG_TYPE_GSTATE, Qnil, HG_e_typecheck);

	if (object->on_copying != Qnil)
		return object->on_copying;

	object->on_copying = retval = hg_gstate_new(gstate->o.mem, (hg_pointer_t *)&g);
	if (retval != Qnil) {
		memcpy(&g->ctm, &gstate->ctm, sizeof (hg_gstate_t) - sizeof (hg_object_t) - (sizeof (hg_quark_t) * 3));
		if (gstate->qpath == Qnil) {
			g->qpath = Qnil;
		} else {
			g->qpath = func(gstate->qpath, user_data, NULL);
			if (g->qpath == Qnil) {
				hg_debug(HG_MSGCAT_GSTATE, "Unable to copy the path object");
				goto bail;
			}
			hg_mem_unref(gstate->o.mem,
				     g->qpath);
		}
		if (gstate->qclippath == Qnil) {
			g->qclippath = Qnil;
		} else {
			g->qclippath = func(gstate->qclippath, user_data, NULL);
			if (g->qclippath == Qnil) {
				hg_debug(HG_MSGCAT_GSTATE, "Unable to copy the clippath object");
				goto bail;
			}
			hg_mem_unref(gstate->o.mem,
				     g->qclippath);
		}
		if (gstate->qdashpattern == Qnil) {
			g->qdashpattern = Qnil;
		} else {
			g->qdashpattern = func(gstate->qdashpattern, user_data, NULL);
			if (g->qdashpattern == Qnil) {
				hg_debug(HG_MSGCAT_GSTATE, "Unable to copy the dashpattern object");
				goto bail;
			}
			hg_mem_unref(gstate->o.mem,
				     g->qdashpattern);
		}

		if (ret)
			*ret = g;
		else
			hg_mem_unlock_object(g->o.mem, retval);
	}
	goto finalize;
  bail:
	if (g) {
		hg_object_free(g->o.mem, retval);

		retval = Qnil;
	}
  finalize:
	object->on_copying = Qnil;

	return retval;
}

static hg_char_t *
_hg_object_gstate_to_cstr(hg_object_t             *object,
			  hg_quark_iterate_func_t  func,
			  hg_pointer_t             user_data)
{
	return g_strdup("-gstate-");
}

static hg_bool_t
_hg_object_gstate_gc_mark(hg_object_t *object)
{
	hg_gstate_t *gstate = (hg_gstate_t *)object;

	if (!hg_mem_gc_mark(gstate->o.mem, gstate->qpath))
		return FALSE;
	if (!hg_mem_gc_mark(gstate->o.mem, gstate->qclippath))
		return FALSE;
	if (!hg_mem_gc_mark(gstate->o.mem, gstate->qdashpattern))
		return FALSE;

	return TRUE;
}

static hg_bool_t
_hg_object_gstate_compare(hg_object_t             *o1,
			  hg_object_t             *o2,
			  hg_quark_compare_func_t  func,
			  hg_pointer_t             user_data)
{
	return FALSE;
}

/*< public >*/
/**
 * hg_gstate_new:
 * @mem:
 * @ret:
 *
 * FIXME
 *
 * Returns:
 */
hg_quark_t
hg_gstate_new(hg_mem_t     *mem,
	      hg_pointer_t *ret)
{
	hg_quark_t retval;
	hg_gstate_t *gstate = NULL;

	hg_return_val_if_fail (mem != NULL, Qnil, HG_e_VMerror);

	retval = hg_object_new(mem, (hg_pointer_t *)&gstate, HG_TYPE_GSTATE, 0);

	if (ret)
		*ret = gstate;
	else
		hg_mem_unlock_object(mem, retval);

	return retval;
}

/**
 * hg_gstate_set_ctm:
 * @gstate:
 * @matrix:
 *
 * FIXME
 */
void
hg_gstate_set_ctm(hg_gstate_t *gstate,
		  hg_matrix_t *matrix)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);
	hg_return_if_fail (matrix != NULL, HG_e_typecheck);

	memcpy(&gstate->ctm, matrix, sizeof (hg_matrix_t));
}

/**
 * hg_gstate_get_ctm:
 * @gstate:
 * @matrix:
 *
 * FIXME
 */
void
hg_gstate_get_ctm(hg_gstate_t *gstate,
		  hg_matrix_t *matrix)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);
	hg_return_if_fail (matrix != NULL, HG_e_typecheck);

	memcpy(matrix, &gstate->ctm, sizeof (hg_matrix_t));
}

/**
 * hg_gstate_set_path:
 * @gstate:
 * @qpath:
 *
 * FIXME
 */
void
hg_gstate_set_path(hg_gstate_t *gstate,
		   hg_quark_t   qpath)
{
	hg_uint_t mem_id;

	hg_return_if_fail (gstate != NULL, HG_e_typecheck);
	hg_return_if_fail (HG_IS_QPATH (qpath), HG_e_typecheck);

	mem_id = hg_quark_get_mem_id(gstate->o.self);

	hg_return_if_fail (hg_quark_has_mem_id(qpath, mem_id), HG_e_VMerror);

	gstate->qpath = qpath;
	hg_mem_unref(gstate->o.mem, qpath);
}

/**
 * hg_gstate_get_path:
 * @gstate:
 *
 * FIXME
 *
 * Returns:
 */
hg_quark_t
hg_gstate_get_path(hg_gstate_t *gstate)
{
	hg_return_val_if_fail (gstate != NULL, Qnil, HG_e_typecheck);

	return gstate->qpath;
}

/**
 * hg_gstate_set_clippath:
 * @gstate:
 * @qpath:
 *
 * FIXME
 */
void
hg_gstate_set_clippath(hg_gstate_t *gstate,
		       hg_quark_t   qpath)
{
	hg_uint_t mem_id;

	hg_return_if_fail (gstate != NULL, HG_e_typecheck);
	hg_return_if_fail (HG_IS_QPATH (qpath), HG_e_typecheck);

	mem_id = hg_quark_get_mem_id(gstate->o.self);

	hg_return_if_fail (hg_quark_has_mem_id(qpath, mem_id), HG_e_VMerror);

	gstate->qclippath = qpath;
	hg_mem_unref(gstate->o.mem, qpath);
}

/**
 * hg_gstate_get_clippath:
 * @gstate:
 *
 * FIXME
 *
 * Returns:
 */
hg_quark_t
hg_gstate_get_clippath(hg_gstate_t *gstate)
{
	hg_return_val_if_fail (gstate != NULL, Qnil, HG_e_typecheck);

	return gstate->qclippath;
}

/**
 * hg_gstate_set_rgbcolor:
 * @gstate:
 * @red:
 * @green:
 * @blue:
 *
 * FIXME
 */
void
hg_gstate_set_rgbcolor(hg_gstate_t *gstate,
		       hg_real_t    red,
		       hg_real_t    green,
		       hg_real_t    blue)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);

	gstate->color.type = HG_COLOR_RGB;
	gstate->color.is.rgb.red = red;
	gstate->color.is.rgb.green = green;
	gstate->color.is.rgb.blue = blue;
}

/**
 * hg_gstate_set_hsbcolor:
 * @gstate:
 * @hue:
 * @saturation:
 * @brightness:
 *
 * FIXME
 */
void
hg_gstate_set_hsbcolor(hg_gstate_t *gstate,
		       hg_real_t    hue,
		       hg_real_t    saturation,
		       hg_real_t    brightness)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);

	gstate->color.type = HG_COLOR_HSB;
	gstate->color.is.hsb.hue = hue;
	gstate->color.is.hsb.saturation = saturation;
	gstate->color.is.hsb.brightness = brightness;
}

/**
 * hg_gstate_set_gray:
 * @gstate:
 * @gray:
 *
 * FIXME
 */
void
hg_gstate_set_graycolor(hg_gstate_t *gstate,
			hg_real_t    gray)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);

	gstate->color.type = HG_COLOR_GRAY;
	gstate->color.is.rgb.red = gray;
	gstate->color.is.rgb.green = gray;
	gstate->color.is.rgb.blue = gray;
}

/**
 * hg_gstate_set_linewidth:
 * @gstate:
 * @width:
 *
 * FIXME
 */
void
hg_gstate_set_linewidth(hg_gstate_t *gstate,
			hg_real_t    width)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);

	gstate->linewidth = width;
}

/**
 * hg_gstate_set_linecap:
 * @gstate:
 * @linecap:
 *
 * FIXME
 */
void
hg_gstate_set_linecap(hg_gstate_t  *gstate,
		      hg_linecap_t  linecap)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);
	hg_return_if_fail (linecap >= 0 && linecap < HG_LINECAP_END, HG_e_rangecheck);

	gstate->linecap = linecap;
}

/**
 * hg_gstate_set_linejoin:
 * @gstate:
 * @linejoin:
 *
 * FIXME
 */
void
hg_gstate_set_linejoin(hg_gstate_t   *gstate,
		       hg_linejoin_t  linejoin)
{
	hg_return_if_fail (gstate != NULL, HG_e_typecheck);
	hg_return_if_fail (linejoin >= 0 && linejoin < HG_LINEJOIN_END, HG_e_rangecheck);

	gstate->linejoin = linejoin;
}

/**
 * hg_gstate_set_miterlimit:
 * @gstate:
 * @miterlen:
 *
 * FIXME
 *
 * Returns:
 */
hg_bool_t
hg_gstate_set_miterlimit(hg_gstate_t *gstate,
			 hg_real_t    miterlen)
{
	hg_return_val_if_fail (gstate != NULL, FALSE, HG_e_typecheck);

	if (miterlen < 1.0)
		return FALSE;

	gstate->miterlen = miterlen;

	return TRUE;
}

/**
 * hg_gstate_set_dash:
 * @gstate:
 * @qpattern:
 * @offset:
 *
 * FIXME
 *
 * Returns:
 */
hg_bool_t
hg_gstate_set_dash(hg_gstate_t *gstate,
		   hg_quark_t   qpattern,
		   hg_real_t    offset)
{
	hg_array_t *a;
	hg_mem_t *mem;
	hg_uint_t id;
	hg_usize_t i;
	hg_bool_t is_zero = TRUE;

	hg_return_val_if_fail (gstate != NULL, FALSE, HG_e_typecheck);
	hg_return_val_if_fail (HG_IS_QARRAY (qpattern), FALSE, HG_e_typecheck);

	/* initialize hg_errno to estimate properly */
	hg_errno = 0;

	id = hg_quark_get_mem_id(qpattern);

	hg_return_val_if_fail ((mem = hg_mem_spool_get(id)) != NULL, FALSE, HG_e_VMerror);
	hg_return_val_if_lock_fail (a, mem, qpattern, FALSE);

	if (hg_array_length(a) > 11) {
		hg_debug(HG_MSGCAT_GSTATE, "Array size for dash pattern is too big");
		hg_errno = HG_ERROR_ (HG_STATUS_FAILED, HG_e_limitcheck);
		goto finalize;
	}
	if (hg_array_length(a) > 0) {
		for (i = 0; i < hg_array_length(a); i++) {
			hg_quark_t q = hg_array_get(a, i);

			if (!HG_ERROR_IS_SUCCESS0 ())
				return FALSE;
			if (HG_IS_QINT (q)) {
				if (HG_INT (q) != 0)
					is_zero = FALSE;
			} else if (HG_IS_QREAL (q)) {
				if (!HG_REAL_IS_ZERO (q))
					is_zero = FALSE;
			} else {
				hg_debug(HG_MSGCAT_GSTATE, "Dash pattern contains non-numeric.");
				hg_errno = HG_ERROR_ (HG_STATUS_FAILED, HG_e_typecheck);
				goto finalize;
			}
		}
		if (is_zero) {
			hg_debug(HG_MSGCAT_GSTATE, "No patterns in Array");
			hg_errno = HG_ERROR_ (HG_STATUS_FAILED, HG_e_rangecheck);
			goto finalize;
		}
	}

	gstate->qdashpattern = qpattern;
	gstate->dash_offset = offset;
  finalize:
	hg_mem_unlock_object(mem, qpattern);

	return HG_ERROR_IS_SUCCESS0 ();
}