82
_cairo_clear_surface (cairo_surface_t **surface)
84
cairo_surface_destroy (*surface);
89
cairo_surface_metadata_t *
90
_cairo_image_surface_get_metadata (cairo_surface_t *surface)
92
cairo_surface_metadata_t *metadata;
94
metadata = cairo_surface_get_user_data (surface, &surface_metadata_key);
95
if (metadata == NULL) {
96
metadata = g_new0 (cairo_surface_metadata_t, 1);
97
cairo_surface_set_user_data (surface, &surface_metadata_key, metadata, surface_metadata_free);
105
_cairo_image_surface_get_has_alpha (cairo_surface_t *surface)
107
cairo_surface_metadata_t *metadata;
112
metadata = cairo_surface_get_user_data (surface, &surface_metadata_key);
113
if (metadata != NULL)
114
return metadata->has_alpha;
116
return cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32;
121
_cairo_image_surface_copy (cairo_surface_t *source)
123
cairo_surface_t *result;
124
cairo_format_t format;
128
unsigned char *pixels;
129
cairo_status_t status;
131
int destination_stride;
132
unsigned char *p_source;
133
unsigned char *p_destination;
139
format = cairo_image_surface_get_format (source);
140
width = cairo_image_surface_get_width (source);
141
height = cairo_image_surface_get_height (source);
142
stride = cairo_format_stride_for_width (format, width);
143
pixels = g_try_malloc (stride * height);
147
result = cairo_image_surface_create_for_data (pixels, format, width, height, stride);
148
status = cairo_surface_status (result);
149
if (status != CAIRO_STATUS_SUCCESS) {
150
g_warning ("_cairo_image_surface_copy: could not create the surface: %s", cairo_status_to_string (status));
151
cairo_surface_destroy (result);
155
status = cairo_surface_set_user_data (result, &surface_pixels_key, pixels, surface_pixels_free);
156
if (status != CAIRO_STATUS_SUCCESS) {
157
g_warning ("_cairo_image_surface_copy: could not set the user data: %s", cairo_status_to_string (status));
158
cairo_surface_destroy (result);
162
cairo_surface_flush (result);
164
source_stride = cairo_image_surface_get_stride (source);
165
destination_stride = cairo_image_surface_get_stride (result);
166
p_source = cairo_image_surface_get_data (source);
167
p_destination = cairo_image_surface_get_data (result);
168
row_size = width * 4;
169
while (height-- > 0) {
170
memcpy (p_destination, p_source, row_size);
172
p_source += source_stride;
173
p_destination += destination_stride;
176
cairo_surface_mark_dirty (result);
183
_cairo_image_surface_copy_subsurface (cairo_surface_t *source,
189
cairo_surface_t *destination;
190
cairo_status_t status;
192
int destination_stride;
193
unsigned char *p_source;
194
unsigned char *p_destination;
197
g_return_val_if_fail (source != NULL, NULL);
198
g_return_val_if_fail (src_x + width <= cairo_image_surface_get_width (source), NULL);
199
g_return_val_if_fail (src_y + height <= cairo_image_surface_get_height (source), NULL);
201
destination = cairo_image_surface_create (cairo_image_surface_get_format (source), width, height);
202
status = cairo_surface_status (destination);
203
if (status != CAIRO_STATUS_SUCCESS) {
204
g_warning ("_cairo_image_surface_copy_subsurface: could not create the surface: %s", cairo_status_to_string (status));
205
cairo_surface_destroy (destination);
209
cairo_surface_flush (destination);
211
source_stride = cairo_image_surface_get_stride (source);
212
destination_stride = cairo_image_surface_get_stride (destination);
213
p_source = cairo_image_surface_get_data (source) + (src_y * source_stride) + (src_x * 4);
214
p_destination = cairo_image_surface_get_data (destination);
215
row_size = width * 4;
216
while (height-- > 0) {
217
memcpy (p_destination, p_source, row_size);
219
p_source += source_stride;
220
p_destination += destination_stride;
223
cairo_surface_mark_dirty (destination);
230
_cairo_image_surface_create_from_pixbuf (GdkPixbuf *pixbuf)
232
cairo_surface_t *surface;
233
cairo_surface_metadata_t *metadata;
240
unsigned char *s_pixels;
248
g_object_get (G_OBJECT (pixbuf),
251
"rowstride", &p_stride,
252
"n-channels", &p_n_channels,
255
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
256
cairo_surface_flush (surface);
257
s_stride = cairo_image_surface_get_stride (surface);
258
s_pixels = cairo_image_surface_get_data (surface);
260
metadata = _cairo_image_surface_get_metadata (surface);
261
metadata->has_alpha = (p_n_channels == 4);
263
if (p_n_channels == 4) {
267
for (h = 0; h < height; h++) {
271
for (w = 0; w < width; w++) {
274
pixel = CAIRO_RGBA_TO_UINT32 (p_iter[0], p_iter[1], p_iter[2], 0xff);
280
r = _cairo_multiply_alpha (p_iter[0], a);
281
g = _cairo_multiply_alpha (p_iter[1], a);
282
b = _cairo_multiply_alpha (p_iter[2], a);
283
pixel = CAIRO_RGBA_TO_UINT32 (r, g, b, a);
285
memcpy (s_iter, &pixel, sizeof (guint32));
288
p_iter += p_n_channels;
291
s_pixels += s_stride;
292
p_pixels += p_stride;
299
for (h = 0; h < height; h++) {
303
for (w = 0; w < width; w++) {
304
pixel = CAIRO_RGBA_TO_UINT32 (p_iter[0], p_iter[1], p_iter[2], 0xff);
305
memcpy (s_iter, &pixel, sizeof (guint32));
308
p_iter += p_n_channels;
311
s_pixels += s_stride;
312
p_pixels += p_stride;
316
cairo_surface_mark_dirty (surface);
323
_cairo_image_surface_create_compatible (cairo_surface_t *surface)
325
return cairo_image_surface_create (cairo_image_surface_get_format (surface),
326
cairo_image_surface_get_width (surface),
327
cairo_image_surface_get_height (surface));
332
_cairo_image_surface_scale_to (cairo_surface_t *surface,
335
cairo_filter_t filter)
337
cairo_surface_t *scaled;
340
scaled = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
341
cr = cairo_create (scaled);
342
cairo_scale (cr, (double) width / cairo_image_surface_get_width (surface), (double) height / cairo_image_surface_get_height (surface));
343
cairo_set_source_surface (cr, surface, 0, 0);
344
cairo_pattern_set_filter (cairo_get_source (cr), filter);
345
cairo_rectangle (cr, 0, 0, cairo_image_surface_get_width (surface), cairo_image_surface_get_height (surface));
355
_cairo_image_surface_transform_get_steps (cairo_format_t format,
358
GthTransform transform,
359
int *destination_width_p,
360
int *destination_height_p,
365
int destination_stride;
366
int destination_width = 0;
367
int destination_height = 0;
373
case GTH_TRANSFORM_NONE:
375
destination_width = width;
376
destination_height = height;
377
destination_stride = cairo_format_stride_for_width (format, destination_width);
379
line_step = destination_stride;
383
case GTH_TRANSFORM_FLIP_H:
384
destination_width = width;
385
destination_height = height;
386
destination_stride = cairo_format_stride_for_width (format, destination_width);
387
line_start = (destination_width - 1) * 4;
388
line_step = destination_stride;
392
case GTH_TRANSFORM_ROTATE_180:
393
destination_width = width;
394
destination_height = height;
395
destination_stride = cairo_format_stride_for_width (format, destination_width);
396
line_start = ((destination_height - 1) * destination_stride) + ((destination_width - 1) * 4);
397
line_step = -destination_stride;
401
case GTH_TRANSFORM_FLIP_V:
402
destination_width = width;
403
destination_height = height;
404
destination_stride = cairo_format_stride_for_width (format, destination_width);
405
line_start = (destination_height - 1) * destination_stride;
406
line_step = -destination_stride;
410
case GTH_TRANSFORM_TRANSPOSE:
411
destination_width = height;
412
destination_height = width;
413
destination_stride = cairo_format_stride_for_width (format, destination_width);
416
pixel_step = destination_stride;
419
case GTH_TRANSFORM_ROTATE_90:
420
destination_width = height;
421
destination_height = width;
422
destination_stride = cairo_format_stride_for_width (format, destination_width);
423
line_start = (destination_width - 1) * 4;
425
pixel_step = destination_stride;
428
case GTH_TRANSFORM_TRANSVERSE:
429
destination_width = height;
430
destination_height = width;
431
destination_stride = cairo_format_stride_for_width (format, destination_width);
432
line_start = ((destination_height - 1) * destination_stride) + ((destination_width - 1) * 4);
434
pixel_step = -destination_stride;
437
case GTH_TRANSFORM_ROTATE_270:
438
destination_width = height;
439
destination_height = width;
440
destination_stride = cairo_format_stride_for_width (format, destination_width);
441
line_start = (destination_height - 1) * destination_stride;
443
pixel_step = -destination_stride;
447
if (destination_width_p != NULL)
448
*destination_width_p = destination_width;
449
if (destination_height_p != NULL)
450
*destination_height_p = destination_height;
451
if (line_start_p != NULL)
452
*line_start_p = line_start;
453
if (line_step_p != NULL)
454
*line_step_p = line_step;
455
if (pixel_step_p != NULL)
456
*pixel_step_p = pixel_step;
461
_cairo_image_surface_transform (cairo_surface_t *source,
462
GthTransform transform)
464
cairo_surface_t *destination = NULL;
465
cairo_format_t format;
469
int destination_width;
470
int destination_height;
474
unsigned char *p_source_line;
475
unsigned char *p_destination_line;
476
unsigned char *p_source;
477
unsigned char *p_destination;
483
format = cairo_image_surface_get_format (source);
484
width = cairo_image_surface_get_width (source);
485
height = cairo_image_surface_get_height (source);
486
source_stride = cairo_image_surface_get_stride (source);
488
_cairo_image_surface_transform_get_steps (format,
498
destination = cairo_image_surface_create (format, destination_width, destination_height);
499
cairo_surface_flush (destination);
500
p_source_line = cairo_image_surface_get_data (source);
501
p_destination_line = cairo_image_surface_get_data (destination) + line_start;
502
while (height-- > 0) {
503
p_source = p_source_line;
504
p_destination = p_destination_line;
505
for (x = 0; x < width; x++) {
506
memcpy (p_destination, p_source, 4);
508
p_destination += pixel_step;
510
p_source_line += source_stride;
511
p_destination_line += line_step;
514
cairo_surface_mark_dirty (destination);
50
521
_cairo_paint_full_gradient (cairo_surface_t *surface,
51
522
GdkColor *h_color1,
52
523
GdkColor *h_color2,
135
cairo_move_to (cr, x, y + r);
137
cairo_arc (cr, x + r, y + r, r, 1.0 * M_PI, 1.5 * M_PI);
138
cairo_rel_line_to (cr, w - (r * 2), 0);
140
cairo_arc (cr, x + w - r, y + r, r, 1.5 * M_PI, 2.0 * M_PI);
141
cairo_rel_line_to (cr, 0, h - (r * 2));
143
cairo_arc (cr, x + w - r, y + h - r, r, 0.0 * M_PI, 0.5 * M_PI);
144
cairo_rel_line_to (cr, - (w - (r * 2)), 0);
146
cairo_arc (cr, x + r, y + h - r, r, 0.5 * M_PI, 1.0 * M_PI);
147
cairo_rel_line_to (cr, 0, - (h - (r * 2)));
592
cairo_rectangle (cr, x, y, w, h);
595
cairo_move_to (cr, x + 1, y);
596
cairo_rel_line_to (cr, w - 2, 0);
597
cairo_rel_line_to (cr, 1, 1);
598
cairo_rel_line_to (cr, 0, h - 2);
599
cairo_rel_line_to (cr, -1, 1);
600
cairo_rel_line_to (cr, - (w - 2), 0);
601
cairo_rel_line_to (cr, -1, -1);
602
cairo_rel_line_to (cr, 0, - (h - 2));
603
cairo_rel_line_to (cr, 1, -1);
606
cairo_move_to (cr, x, y + r);
608
cairo_arc (cr, x + r, y + r, r, 1.0 * M_PI, 1.5 * M_PI);
609
cairo_rel_line_to (cr, w - (r * 2), 0);
611
cairo_arc (cr, x + w - r, y + r, r, 1.5 * M_PI, 2.0 * M_PI);
612
cairo_rel_line_to (cr, 0, h - (r * 2));
614
cairo_arc (cr, x + w - r, y + h - r, r, 0.0 * M_PI, 0.5 * M_PI);
615
cairo_rel_line_to (cr, - (w - (r * 2)), 0);
617
cairo_arc (cr, x + r, y + h - r, r, 0.5 * M_PI, 1.0 * M_PI);
618
cairo_rel_line_to (cr, 0, - (h - (r * 2)));
301
773
cairo_restore (cr);
777
#define GOLDEN_RATIO 1.6180339887
778
#define GOLDER_RATIO_FACTOR (GOLDEN_RATIO / (1.0 + 2.0 * GOLDEN_RATIO))
779
#define GRID_STEP_1 10
780
#define GRID_STEP_2 (GRID_STEP_1 * 5)
781
#define GRID_STEP_3 (GRID_STEP_2 * 2)
785
_cairo_paint_grid (cairo_t *cr,
786
GdkRectangle *rectangle,
787
GthGridType grid_type)
794
cairo_device_to_user_distance (cr, &ux, &uy);
795
cairo_set_line_width (cr, MAX (ux, uy));
797
cairo_rectangle (cr, rectangle->x - ux + 0.5, rectangle->y - uy + 0.5, rectangle->width + (ux * 2), rectangle->height + (uy * 2));
800
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 9, 2)
801
/* cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE); */
804
cairo_rectangle (cr, rectangle->x + 0.5, rectangle->y + 0.5, rectangle->width - 0.5, rectangle->height - 0.5);
805
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
808
if (grid_type == GTH_GRID_NONE) {
813
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
815
if (grid_type == GTH_GRID_THIRDS) {
818
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.60);
819
for (i = 1; i < 3; i++) {
820
cairo_move_to (cr, rectangle->x + rectangle->width * i / 3 + 0.5, rectangle->y + 1.5);
821
cairo_line_to (cr, rectangle->x + rectangle->width * i / 3 + 0.5, rectangle->y + rectangle->height - 0.5);
823
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + rectangle->height * i / 3 + 0.5);
824
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + rectangle->height * i / 3 + 0.5);
828
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.10);
829
for (i = 1; i < 9; i++) {
834
cairo_move_to (cr, rectangle->x + rectangle->width * i / 9 + 0.5, rectangle->y + 1.5);
835
cairo_line_to (cr, rectangle->x + rectangle->width * i / 9 + 0.5, rectangle->y + rectangle->height - 0.5);
837
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + rectangle->height * i / 9 + 0.5);
838
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + rectangle->height * i / 9 + 0.5);
842
else if (grid_type == GTH_GRID_GOLDEN) {
843
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.60);
845
int grid_x0, grid_x1, grid_x2, grid_x3;
846
int grid_y0, grid_y1, grid_y2, grid_y3;
847
int x_delta, y_delta;
849
grid_x0 = rectangle->x;
850
grid_x3 = rectangle->x + rectangle->width;
852
grid_y0 = rectangle->y;
853
grid_y3 = rectangle->y + rectangle->height;
855
x_delta = rectangle->width * GOLDER_RATIO_FACTOR;
856
y_delta = rectangle->height * GOLDER_RATIO_FACTOR;
858
grid_x1 = grid_x0 + x_delta;
859
grid_x2 = grid_x3 - x_delta;
860
grid_y1 = grid_y0 + y_delta;
861
grid_y2 = grid_y3 - y_delta;
863
cairo_move_to (cr, grid_x1 + 0.5, grid_y0 + 0.5);
864
cairo_line_to (cr, grid_x1 + 0.5, grid_y3 + 0.5);
866
if (x_delta < rectangle->width / 2) {
867
cairo_move_to (cr, grid_x2 + 0.5, grid_y0 + 0.5);
868
cairo_line_to (cr, grid_x2 + 0.5, grid_y3 + 0.5);
871
cairo_move_to (cr, grid_x0 + 0.5, grid_y1 + 0.5);
872
cairo_line_to (cr, grid_x3 + 0.5, grid_y1 + 0.5);
874
if (y_delta < rectangle->height / 2) {
875
cairo_move_to (cr, grid_x0 + 0.5, grid_y2 + 0.5);
876
cairo_line_to (cr, grid_x3 + 0.5, grid_y2 + 0.5);
881
else if (grid_type == GTH_GRID_CENTER) {
884
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.60);
885
cairo_move_to (cr, rectangle->x + rectangle->width / 2 + 0.5, rectangle->y + 1.5);
886
cairo_line_to (cr, rectangle->x + rectangle->width / 2 + 0.5, rectangle->y + rectangle->height - 0.5);
887
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + rectangle->height / 2 + 0.5);
888
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + rectangle->height / 2 + 0.5);
891
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.10);
892
for (i = 1; i < 4; i++) {
897
cairo_move_to (cr, rectangle->x + rectangle->width * i / 4 + 0.5, rectangle->y + 1.5);
898
cairo_line_to (cr, rectangle->x + rectangle->width * i / 4 + 0.5, rectangle->y + rectangle->height - 0.5);
899
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + rectangle->height * i / 4 + 0.5);
900
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + rectangle->height * i / 4 + 0.5);
904
else if (grid_type == GTH_GRID_UNIFORM) {
909
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.40);
910
for (x = GRID_STEP_3; x < rectangle->width; x += GRID_STEP_3) {
911
cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
912
cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
914
for (y = GRID_STEP_3; y < rectangle->height; y += GRID_STEP_3) {
915
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
916
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
920
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.20);
921
for (x = GRID_STEP_2; x < rectangle->width; x += GRID_STEP_2) {
922
if (x % GRID_STEP_3 == 0)
924
cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
925
cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
927
for (y = GRID_STEP_2; y < rectangle->height; y += GRID_STEP_2) {
928
if (y % GRID_STEP_3 == 0)
930
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
931
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);
935
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.10);
936
for (x = GRID_STEP_1; x < rectangle->width; x += GRID_STEP_1) {
937
if (x % GRID_STEP_2 == 0)
939
cairo_move_to (cr, rectangle->x + x + 0.5, rectangle->y + 1.5);
940
cairo_line_to (cr, rectangle->x + x + 0.5, rectangle->y + rectangle->height - 0.5);
942
for (y = GRID_STEP_1; y < rectangle->height; y += GRID_STEP_1) {
943
if (y % GRID_STEP_2 == 0)
945
cairo_move_to (cr, rectangle->x + 1.5, rectangle->y + y + 0.5);
946
cairo_line_to (cr, rectangle->x + rectangle->width - 0.5, rectangle->y + y + 0.5);