Debian Codegraph @

gdraw/ggdkcdraw.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /* Copyright (C) 2016 by Jeremy Tan */
3 /*
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13
14 * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <fontforge-config.h>
30
31 /**
32 * \file ggdkcdraw.c
33 * \brief Cairo drawing functionality
34 * This is based on gxcdraw.c, but modified to suit GDK.
35 */
36
37 #include "ggdkdrawP.h"
38
39
40 #include "ustring.h"
41
42 #include <assert.h>
43 #include <math.h>
44
45 // Private member functions
46
47 static void _GGDKDraw_CheckAutoPaint(GGDKWindow gw) {
48 if (gw->cc == NULL) {
49 assert(!gw->is_pixmap);
50 if (!gw->is_in_paint) {
51 assert(false && "No window should be drawing outside of expose calls");
52 Log(LOGWARN, "Dirty window is drawing outside of expose call: 0x%p [%s]", gw, gw->window_title);
53 }
54
55 if (gw->display->dirty_window != gw) {
56 _GGDKDraw_CleanupAutoPaint(gw->display);
57 gw->display->dirty_window = gw;
58 }
59
60 // Shut up! I know what I'm doing.
61 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
62 gw->cc = gdk_cairo_create(gw->w);
63 G_GNUC_END_IGNORE_DEPRECATIONS
64
65 // Unlike GDK2, it turns out you can draw over child windows
66 // But we don't want that. Must be something to do with alpha transparency.
67 if (!gdk_window_has_native(gw->w)) {
68 cairo_region_t *r = _GGDKDraw_ExcludeChildRegions(gw, NULL, false);
69 if (r != NULL) {
70 gdk_cairo_region(gw->cc, r);
71 cairo_clip(gw->cc);
72 cairo_region_destroy(r);
73 }
74 }
75 }
76 }
77
78 static void GGDKDraw_StippleMePink(GGDKWindow gw, int ts, Color fg) {
79 static unsigned char grey_init[8] = {0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa};
80 static unsigned char fence_init[8] = {0x55, 0x22, 0x55, 0x88, 0x55, 0x22, 0x55, 0x88};
81 uint8_t *spt;
82 int bit, i, j;
83 uint32_t *data;
84 static uint32_t space[8 * 8];
85 static cairo_pattern_t *pat = NULL;
86
87 if ((fg >> 24) != 0xff) {
88 int alpha = fg >> 24, r = COLOR_RED(fg), g = COLOR_GREEN(fg), b = COLOR_BLUE(fg);
89 r = (alpha * r + 128) / 255;
90 g = (alpha * g + 128) / 255;
91 b = (alpha * b + 128) / 255;
92 fg = (alpha << 24) | (r << 16) | (g << 8) | b;
93 }
94
95 spt = (ts == 2) ? fence_init : grey_init;
96 for (i = 0; i < 8; ++i) {
97 data = space + 8 * i;
98 for (j = 0, bit = 0x80; bit != 0; ++j, bit >>= 1) {
99 if (spt[i]&bit) {
100 data[j] = fg;
101 } else {
102 data[j] = 0;
103 }
104 }
105 }
106 if (pat == NULL) {
107 cairo_surface_t *is = cairo_image_surface_create_for_data((uint8_t *) space,
108 CAIRO_FORMAT_ARGB32, 8, 8, 8 * 4);
109 pat = cairo_pattern_create_for_surface(is);
110 cairo_surface_destroy(is);
111 cairo_pattern_set_extend(pat, CAIRO_EXTEND_REPEAT);
112 }
113 cairo_set_source(gw->cc, pat);
114 }
115
116 static int GGDKDrawSetcolfunc(GGDKWindow gw, GGC *mine) {
117 Color fg = mine->fg;
118
119 if ((fg >> 24) == 0) {
120 fg |= 0xff000000;
121 }
122
123 if (mine->ts != 0) {
124 GGDKDraw_StippleMePink(gw, mine->ts, fg);
125 } else {
126 cairo_set_source_rgba(gw->cc, COLOR_RED(fg) / 255.0,
127 COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0, (fg >> 24) / 255.);
128 }
129 return true;
130 }
131
132 static int GGDKDrawSetline(GGDKWindow gw, GGC *mine) {
133 Color fg = mine->fg;
134 double dashes[2] = {mine->dash_len, mine->skip_len};
135
136 if ((fg >> 24) == 0) {
137 fg |= 0xff000000;
138 }
139
140 if (mine->line_width <= 0) {
141 mine->line_width = 1;
142 }
143 cairo_set_line_width(gw->cc, mine->line_width);
144 cairo_set_dash(gw->cc, dashes, (mine->dash_len == 0) ? 0 : 2, mine->dash_offset);
145
146 // I don't use line join/cap. On a screen with small line_width they are irrelevant
147
148 if (mine->ts != 0) {
149 GGDKDraw_StippleMePink(gw, mine->ts, fg);
150 } else {
151 cairo_set_source_rgba(gw->cc, COLOR_RED(fg) / 255.0, COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0,
152 (fg >> 24) / 255.0);
153 }
154 return mine->line_width;
155 }
156
157 // Pango text
158 static PangoFontDescription *_GGDKDraw_configfont(GWindow w, GFont *font) {
159 GGDKWindow gw = (GGDKWindow) w;
160 PangoFontDescription *fd;
161
162 // Initialize Cairo and Pango if not initialized, e.g. root window
163 if (gw->pango_layout == NULL && !_GGDKDraw_InitPangoCairo(gw)) {
164 return NULL;
165 }
166
167 PangoFontDescription **fdbase = &font->pangoc_fd;
168 if (*fdbase != NULL) {
169 return *fdbase;
170 }
171 *fdbase = fd = pango_font_description_new();
172 if (*fdbase == NULL) {
173 return NULL;
174 }
175
176 pango_font_description_set_family(fd, font->rq.utf8_family_name);
177 pango_font_description_set_style(fd, (font->rq.style & fs_italic) ?
178 PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
179 pango_font_description_set_variant(fd, (font->rq.style & fs_smallcaps) ?
180 PANGO_VARIANT_SMALL_CAPS : PANGO_VARIANT_NORMAL);
181 pango_font_description_set_weight(fd, font->rq.weight);
182 pango_font_description_set_stretch(fd,
183 (font->rq.style & fs_condensed) ? PANGO_STRETCH_CONDENSED :
184 (font->rq.style & fs_extended) ? PANGO_STRETCH_EXPANDED :
185 PANGO_STRETCH_NORMAL);
186
187 if (font->rq.style & fs_vertical) {
188 //FIXME: not sure this is the right thing
189 pango_font_description_set_gravity(fd, PANGO_GRAVITY_WEST);
190 }
191
192 if (font->rq.point_size <= 0) {
193 // Any negative (pixel) values should be converted when font opened
194 GDrawIError("Bad point size for Pango");
195 }
196
197 // Pango doesn't give me any control over the resolution on X, so I do my
198 // own conversion from points to pixels
199 // But under pangocairo I can set the resolution, so behavior is different
200 pango_font_description_set_absolute_size(fd,
201 GDrawPointsToPixels(w, font->rq.point_size * PANGO_SCALE));
202 return fd;
203 }
204
205 // Strangely the equivalent routine was not part of the pangocairo library
206 // Oh there's pango_cairo_layout_path but that's more restrictive and probably
207 // less efficient
208 static void _GGDKDraw_MyCairoRenderLayout(cairo_t *cc, Color fg, PangoLayout *layout, int x, int y) {
209 PangoRectangle rect, r2;
210 PangoLayoutIter *iter;
211
212 iter = pango_layout_get_iter(layout);
213 do {
214 PangoLayoutRun *run = pango_layout_iter_get_run(iter);
215 if (run != NULL) { // NULL runs mark end of line
216 pango_layout_iter_get_run_extents(iter, &r2, &rect);
217 cairo_move_to(cc, x + (rect.x + PANGO_SCALE / 2) / PANGO_SCALE, y + (rect.y + PANGO_SCALE / 2) / PANGO_SCALE);
218 if (COLOR_ALPHA(fg) == 0) {
219 cairo_set_source_rgba(cc, COLOR_RED(fg) / 255.0,
220 COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0, 1.0);
221 } else {
222 cairo_set_source_rgba(cc, COLOR_RED(fg) / 255.0,
223 COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0, COLOR_ALPHA(fg) / 255.);
224 }
225 pango_cairo_show_glyph_string(cc, run->item->analysis.font, run->glyphs);
226 }
227 } while (pango_layout_iter_next_run(iter));
228 pango_layout_iter_free(iter);
229 }
230
231 static void _GGDKDraw_EllipsePath(cairo_t *cc, double cx, double cy, double width, double height) {
232 cairo_new_path(cc);
233 cairo_move_to(cc, cx, cy + height);
234 cairo_curve_to(cc,
235 cx + .552 * width, cy + height,
236 cx + width, cy + .552 * height,
237 cx + width, cy);
238 cairo_curve_to(cc,
239 cx + width, cy - .552 * height,
240 cx + .552 * width, cy - height,
241 cx, cy - height);
242 cairo_curve_to(cc,
243 cx - .552 * width, cy - height,
244 cx - width, cy - .552 * height,
245 cx - width, cy);
246 cairo_curve_to(cc,
247 cx - width, cy + .552 * height,
248 cx - .552 * width, cy + height,
249 cx, cy + height);
250 cairo_close_path(cc);
251 }
252
253 static cairo_surface_t *_GGDKDraw_GImage2Surface(GImage *image, GRect *src) {
254 struct _GImage *base = (image->list_len == 0) ? image->u.image : image->u.images[0];
255 cairo_format_t type;
256 uint8_t *pt;
257 uint32_t *idata, *ipt, *ito;
258 int i, j, jj, tjj, stride;
259 int bit, tobit;
260 cairo_surface_t *cs;
261
262 if (base->image_type == it_rgba) {
263 type = CAIRO_FORMAT_ARGB32;
264 } else if (base->image_type == it_true && base->trans != COLOR_UNKNOWN) {
265 type = CAIRO_FORMAT_ARGB32;
266 } else if (base->image_type == it_index && base->clut->trans_index != COLOR_UNKNOWN) {
267 type = CAIRO_FORMAT_ARGB32;
268 } else if (base->image_type == it_true) {
269 type = CAIRO_FORMAT_RGB24;
270 } else if (base->image_type == it_index) {
271 type = CAIRO_FORMAT_RGB24;
272 } else if (base->image_type == it_mono && base->clut != NULL &&
273 base->clut->trans_index != COLOR_UNKNOWN) {
274 type = CAIRO_FORMAT_A1;
275 } else {
276 type = CAIRO_FORMAT_RGB24;
277 }
278
279 /* We can't reuse the image's data for alpha images because we must */
280 /* premultiply each channel by alpha. We can reuse it for non-transparent*/
281 /* rgb images */
282 if (base->image_type == it_true && type == CAIRO_FORMAT_RGB24) {
283 idata = ((uint32_t *)(base->data + src->y * base->bytes_per_line)) + src->x;
284 return cairo_image_surface_create_for_data((uint8_t *) idata, type,
285 src->width, src->height,
286 base->bytes_per_line);
287 }
288
289 cs = cairo_image_surface_create(type, src->width, src->height);
290 stride = cairo_image_surface_get_stride(cs);
291 cairo_surface_flush(cs);
292 idata = (uint32_t *)cairo_image_surface_get_data(cs);
293
294 if (base->image_type == it_rgba) {
295 ipt = ((uint32_t *)(base->data + src->y * base->bytes_per_line)) + src->x;
296 ito = idata;
297 for (i = 0; i < src->height; ++i) {
298 for (j = 0; j < src->width; ++j) {
299 uint32_t orig = ipt[j];
300 int alpha = orig >> 24;
301 if (alpha == 0xff) {
302 ito[j] = orig;
303 } else if (alpha == 0) {
304 ito[j] = 0x00000000;
305 } else
306 ito[j] = (alpha << 24) |
307 ((COLOR_RED(orig) * alpha / 255) << 16) |
308 ((COLOR_GREEN(orig) * alpha / 255) << 8) |
309 ((COLOR_BLUE(orig) * alpha / 255));
310 }
311 ipt = (uint32_t *)(((uint8_t *) ipt) + base->bytes_per_line);
312 ito = (uint32_t *)(((uint8_t *) ito) + stride);
313 }
314 } else if (base->image_type == it_true && base->trans != COLOR_UNKNOWN) {
315 Color trans = base->trans;
316 ipt = ((uint32_t *)(base->data + src->y * base->bytes_per_line)) + src->x;
317 ito = idata;
318 for (i = 0; i < src->height; ++i) {
319 for (j = 0; j < src->width; ++j) {
320 if (ipt[j] == trans) {
321 ito[j] = 0x00000000;
322 } else {
323 ito[j] = ipt[j] | 0xff000000;
324 }
325 }
326 ipt = (uint32_t *)(((uint8_t *) ipt) + base->bytes_per_line);
327 ito = (uint32_t *)(((uint8_t *) ito) + stride);
328 }
329 } else if (base->image_type == it_true) {
330 ipt = ((uint32_t *)(base->data + src->y * base->bytes_per_line)) + src->x;
331 ito = idata;
332 for (i = 0; i < src->height; ++i) {
333 for (j = 0; j < src->width; ++j) {
334 ito[j] = ipt[j] | 0xff000000;
335 }
336 ipt = (uint32_t *)(((uint8_t *) ipt) + base->bytes_per_line);
337 ito = (uint32_t *)(((uint8_t *) ito) + stride);
338 }
339 } else if (base->image_type == it_index && base->clut->trans_index != COLOR_UNKNOWN) {
340 int trans = base->clut->trans_index;
341 Color *clut = base->clut->clut;
342 pt = base->data + src->y * base->bytes_per_line + src->x;
343 ito = idata;
344 for (i = 0; i < src->height; ++i) {
345 for (j = 0; j < src->width; ++j) {
346 int index = pt[j];
347 if (index == trans) {
348 ito[j] = 0x00000000;
349 } else
350 /* In theory RGB24 images don't need the alpha channel set*/
351 /* but there is a bug in Cairo 1.2, and they do. */
352 {
353 ito[j] = clut[index] | 0xff000000;
354 }
355 }
356 pt += base->bytes_per_line;
357 ito = (uint32_t *)(((uint8_t *) ito) + stride);
358 }
359 } else if (base->image_type == it_index) {
360 Color *clut = base->clut->clut;
361 pt = base->data + src->y * base->bytes_per_line + src->x;
362 ito = idata;
363 for (i = 0; i < src->height; ++i) {
364 for (j = 0; j < src->width; ++j) {
365 int index = pt[j];
366 ito[j] = clut[index] | 0xff000000;
367 }
368 pt += base->bytes_per_line;
369 ito = (uint32_t *)(((uint8_t *) ito) + stride);
370 }
371 #ifdef WORDS_BIGENDIAN
372 } else if (base->image_type == it_mono && base->clut != NULL &&
373 base->clut->trans_index != COLOR_UNKNOWN) {
374 pt = base->data + src->y * base->bytes_per_line + (src->x >> 3);
375 ito = idata;
376 if (base->clut->trans_index == 0) {
377 for (i = 0; i < src->height; ++i) {
378 bit = (0x80 >> (src->x & 0x7));
379 tobit = 0x80000000;
380 for (j = jj = tjj = 0; j < src->width; ++j) {
381 if (pt[jj]&bit) {
382 ito[tjj] |= tobit;
383 }
384 if ((bit >>= 1) == 0) {
385 bit = 0x80;
386 ++jj;
387 }
388 if ((tobit >>= 1) == 0) {
389 tobit = 0x80000000;
390 ++tjj;
391 }
392 }
393 pt += base->bytes_per_line;
394 ito = (uint32_t *)(((uint8_t *) ito) + stride);
395 }
396 } else {
397 for (i = 0; i < src->height; ++i) {
398 bit = (0x80 >> (src->x & 0x7));
399 tobit = 0x80000000;
400 for (j = jj = tjj = 0; j < src->width; ++j) {
401 if (!(pt[jj]&bit)) {
402 ito[tjj] |= tobit;
403 }
404 if ((bit >>= 1) == 0) {
405 bit = 0x80;
406 ++jj;
407 }
408 if ((tobit >>= 1) == 0) {
409 tobit = 0x80000000;
410 ++tjj;
411 }
412 }
413 pt += base->bytes_per_line;
414 ito = (uint32_t *)(((uint8_t *) ito) + stride);
415 }
416 }
417 #else
418 } else if (base->image_type == it_mono && base->clut != NULL &&
419 base->clut->trans_index != COLOR_UNKNOWN) {
420 pt = base->data + src->y * base->bytes_per_line + (src->x >> 3);
421 ito = idata;
422 if (base->clut->trans_index == 0) {
423 for (i = 0; i < src->height; ++i) {
424 bit = (0x80 >> (src->x & 0x7));
425 tobit = 1;
426 for (j = jj = tjj = 0; j < src->width; ++j) {
427 if (pt[jj]&bit) {
428 ito[tjj] |= tobit;
429 }
430 if ((bit >>= 1) == 0) {
431 bit = 0x80;
432 ++jj;
433 }
434 if ((tobit <<= 1) == 0) {
435 tobit = 0x1;
436 ++tjj;
437 }
438 }
439 pt += base->bytes_per_line;
440 ito = (uint32_t *)(((uint8_t *) ito) + stride);
441 }
442 } else {
443 for (i = 0; i < src->height; ++i) {
444 bit = (0x80 >> (src->x & 0x7));
445 tobit = 1;
446 for (j = jj = tjj = 0; j < src->width; ++j) {
447 if (!(pt[jj]&bit)) {
448 ito[tjj] |= tobit;
449 }
450 if ((bit >>= 1) == 0) {
451 bit = 0x80;
452 ++jj;
453 }
454 if ((tobit <<= 1) == 0) {
455 tobit = 0x1;
456 ++tjj;
457 }
458 }
459 pt += base->bytes_per_line;
460 ito = (uint32_t *)(((uint8_t *) ito) + stride);
461 }
462 }
463 #endif
464 } else {
465 Color fg = base->clut == NULL ? 0xffffff : base->clut->clut[1];
466 Color bg = base->clut == NULL ? 0x000000 : base->clut->clut[0];
467 /* In theory RGB24 images don't need the alpha channel set*/
468 /* but there is a bug in Cairo 1.2, and they do. */
469 fg |= 0xff000000;
470 bg |= 0xff000000;
471 pt = base->data + src->y * base->bytes_per_line + (src->x >> 3);
472 ito = idata;
473 for (i = 0; i < src->height; ++i) {
474 bit = (0x80 >> (src->x & 0x7));
475 for (j = jj = 0; j < src->width; ++j) {
476 ito[j] = (pt[jj] & bit) ? fg : bg;
477 if ((bit >>= 1) == 0) {
478 bit = 0x80;
479 ++jj;
480 }
481 }
482 pt += base->bytes_per_line;
483 ito = (uint32_t *)(((uint8_t *) ito) + stride);
484 }
485 }
486 cairo_surface_mark_dirty(cs);
487 return cs;
488 }
489
490 static GImage *_GGDKDraw_GImageExtract(struct _GImage *base, GRect *src, GRect *size,
491 double xscale, double yscale) {
492 static GImage temp;
493 static struct _GImage tbase;
494 static uint8_t *data;
495 static int dlen;
496 int r, c;
497
498 memset(&temp, 0, sizeof(temp));
499 tbase = *base;
500 temp.u.image = &tbase;
501 tbase.width = size->width;
502 tbase.height = size->height;
503 if (base->image_type == it_mono) {
504 tbase.bytes_per_line = (size->width + 7) / 8;
505 } else if (base->image_type == it_index) {
506 tbase.bytes_per_line = size->width;
507 } else {
508 tbase.bytes_per_line = 4 * size->width;
509 }
510 if (tbase.bytes_per_line * size->height > dlen) {
511 data = realloc(data, dlen = tbase.bytes_per_line * size->height);
512 }
513 tbase.data = data;
514
515 /* I used to use rint(x). Now I use floor(x). For normal images rint */
516 /* might be better, but for text we need floor */
517
518 if (base->image_type == it_mono) {
519 memset(data, 0, tbase.height * tbase.bytes_per_line);
520 for (r = 0; r < size->height; ++r) {
521 int or = ((int) floor((r + size->y) / yscale));
522 uint8_t *pt = data + r * tbase.bytes_per_line;
523 uint8_t *opt = base->data + or * base->bytes_per_line;
524 for (c = 0; c < size->width; ++c) {
525 int oc = ((int) floor((c + size->x) / xscale));
526 if (opt[oc >> 3] & (0x80 >> (oc & 7))) {
527 pt[c >> 3] |= (0x80 >> (c & 7));
528 }
529 }
530 }
531 } else if (base->image_type == it_index) {
532 for (r = 0; r < size->height; ++r) {
533 int or = ((int) floor((r + size->y) / yscale));
534 uint8_t *pt = data + r * tbase.bytes_per_line;
535 uint8_t *opt = base->data + or * base->bytes_per_line;
536 for (c = 0; c < size->width; ++c) {
537 int oc = ((int) floor((c + size->x) / xscale));
538 *pt++ = opt[oc];
539 }
540 }
541 } else {
542 for (r = 0; r < size->height; ++r) {
543 int or = ((int) floor((r + size->y) / yscale));
544 uint32_t *pt = (uint32_t *)(data + r * tbase.bytes_per_line);
545 uint32_t *opt = (uint32_t *)(base->data + or * base->bytes_per_line);
546 for (c = 0; c < size->width; ++c) {
547 int oc = ((int) floor((c + size->x) / xscale));
548 *pt++ = opt[oc];
549 }
550 }
551 }
552 return (&temp);
553 }
554
555 // Protected member functions
556
557 bool _GGDKDraw_InitPangoCairo(GGDKWindow gw) {
558 if (gw->is_pixmap) {
559 gw->cc = cairo_create(gw->cs);
560 if (gw->cc == NULL) {
561 Log(LOGDEBUG, "GGDKDRAW: Cairo context creation failed!");
562 return false;
563 }
564 }
565
566 // Establish Pango layout context
567 gw->pango_layout = pango_layout_new(gw->display->pangoc_context);
568 if (gw->pango_layout == NULL) {
569 Log(LOGDEBUG, "GGDKDRAW: Pango layout creation failed!");
570 if (gw->cc != NULL) {
571 cairo_destroy(gw->cc);
572 gw->cc = NULL;
573 }
574 return false;
575 }
576
577 return true;
578 }
579
580 cairo_region_t *_GGDKDraw_ExcludeChildRegions(GGDKWindow gw, cairo_region_t *r, bool force) {
581 GList_Glib *children = gdk_window_peek_children(gw->w);
582 cairo_region_t *reg = NULL;
583
584 if (children) {
585 if (r == NULL) {
586 reg = gdk_window_get_clip_region(gw->w);
587 } else {
588 reg = cairo_region_copy(r);
589 }
590
591 while (children != NULL) {
592 cairo_region_t *chr = gdk_window_get_clip_region((GdkWindow *)children->data);
593 int dx, dy;
594
595 gdk_window_get_position((GdkWindow *)children->data, &dx, &dy);
596 cairo_region_translate(chr, dx, dy);
597 cairo_region_subtract(reg, chr);
598 cairo_region_destroy(chr);
599 children = children->next;
600 }
601 } else if (force) {
602 reg = gdk_window_get_clip_region(gw->w);
603 if (r != NULL) {
604 cairo_region_intersect(reg, r);
605 }
606 }
607
608 return reg;
609 }
610
611 void _GGDKDraw_CleanupAutoPaint(GGDKDisplay *gdisp) {
612 if (gdisp->dirty_window != NULL) {
613 GGDKWindow gw = gdisp->dirty_window;
614
615 if (gw->cc != NULL) {
616 cairo_destroy(gw->cc);
617 gw->cc = NULL;
618 }
619 if (gw->is_in_paint) {
620 //Log(LOGDEBUG, "ENDED PAINT %p", gw);
621 #if !defined(GDK_WINDOWING_WIN32) && !defined(GDK_WINDOWING_QUARTZ)
622 if (gw->cs != NULL) {
623 assert(gw->expose_region != NULL);
624 #ifdef GGDKDRAW_GDK_3_22
625 cairo_t *cc = cairo_reference(gdk_drawing_context_get_cairo_context(gw->drawing_ctx));
626 #else
627 cairo_t *cc = gdk_cairo_create(gw->w);
628 #endif
629
630 gdk_cairo_region(cc, gw->expose_region);
631 cairo_clip(cc);
632
633 cairo_set_source_surface(cc, gw->cs, 0, 0);
634 cairo_set_operator(cc, CAIRO_OPERATOR_SOURCE);
635 cairo_paint(cc);
636
637 cairo_destroy(cc);
638 cairo_region_destroy(gw->expose_region);
639 gw->expose_region = NULL;
640 cairo_surface_destroy(gw->cs);
641 gw->cs = NULL;
642 }
643 #endif
644 #ifdef GGDKDRAW_GDK_3_22
645 gdk_window_end_draw_frame(gw->w, gw->drawing_ctx);
646 #else
647 gdk_window_end_paint(gw->w);
648 #endif
649 gw->is_in_paint = false;
650 }
651 gdisp->dirty_window = NULL;
652 }
653 }
654
655 void GGDKDrawPushClip(GWindow w, GRect *rct, GRect *old) {
656 //Log(LOGDEBUG, " ");
657
658 // Return the current clip, and intersect the current clip with the desired
659 // clip to get the new clip.
660 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
661
662 *old = w->ggc->clip;
663 w->ggc->clip = *rct;
664 if (w->ggc->clip.x + w->ggc->clip.width > old->x + old->width) {
665 w->ggc->clip.width = old->x + old->width - w->ggc->clip.x;
666 }
667 if (w->ggc->clip.y + w->ggc->clip.height > old->y + old->height) {
668 w->ggc->clip.height = old->y + old->height - w->ggc->clip.y;
669 }
670 if (w->ggc->clip.x < old->x) {
671 if (w->ggc->clip.width > (old->x - w->ggc->clip.x)) {
672 w->ggc->clip.width -= (old->x - w->ggc->clip.x);
673 } else {
674 w->ggc->clip.width = 0;
675 }
676 w->ggc->clip.x = old->x;
677 }
678 if (w->ggc->clip.y < old->y) {
679 if (w->ggc->clip.height > (old->y - w->ggc->clip.y)) {
680 w->ggc->clip.height -= (old->y - w->ggc->clip.y);
681 } else {
682 w->ggc->clip.height = 0;
683 }
684 w->ggc->clip.y = old->y;
685 }
686 if (w->ggc->clip.height < 0 || w->ggc->clip.width < 0) {
687 // Negative values mean large positive values, so if we want to clip
688 // to nothing force clip outside window
689 w->ggc->clip.x = w->ggc->clip.y = -100;
690 w->ggc->clip.height = w->ggc->clip.width = 1;
691 }
692
693 GGDKWindow gw = (GGDKWindow)w;
694
695 cairo_save(gw->cc);
696 cairo_new_path(gw->cc);
697 cairo_rectangle(gw->cc, gw->ggc->clip.x, gw->ggc->clip.y,
698 gw->ggc->clip.width, gw->ggc->clip.height);
699 cairo_clip(gw->cc);
700 }
701
702 void GGDKDrawPopClip(GWindow w, GRect *old) {
703 //Log(LOGDEBUG, " ");
704 GGDKWindow gw = (GGDKWindow)w;
705 if (old) {
706 gw->ggc->clip = *old;
707 }
708 // cc can be NULL if the autopaint cleanup had to run because
709 // a raise/lower/move/resize function was called.
710 if (gw->cc != NULL) {
711 cairo_restore(gw->cc);
712 }
713 }
714
715 void GGDKDrawDrawLine(GWindow w, int32_t x, int32_t y, int32_t xend, int32_t yend, Color col) {
716 //Log(LOGDEBUG, " ");
717 GGDKWindow gw = (GGDKWindow)w;
718 _GGDKDraw_CheckAutoPaint(gw);
719
720 w->ggc->fg = col;
721
722 int width = GGDKDrawSetline(gw, gw->ggc);
723 cairo_new_path(gw->cc);
724 if (width & 1) {
725 cairo_move_to(gw->cc, x + .5, y + .5);
726 cairo_line_to(gw->cc, xend + .5, yend + .5);
727 } else {
728 cairo_move_to(gw->cc, x, y);
729 cairo_line_to(gw->cc, xend, yend);
730 }
731 cairo_stroke(gw->cc);
732
733 }
734
735 void GGDKDrawDrawArrow(GWindow w, int32_t x, int32_t y, int32_t xend, int32_t yend, int16_t arrows, Color col) {
736 //Log(LOGDEBUG, " ");
737 GGDKWindow gw = (GGDKWindow)w;
738 _GGDKDraw_CheckAutoPaint(gw);
739
740 gw->ggc->fg = col;
741
742 int width = GGDKDrawSetline(gw, gw->ggc);
743 if (width & 1) {
744 x += .5;
745 y += .5;
746 xend += .5;
747 yend += .5;
748 }
749
750 const double head_angle = 0.5;
751 double angle = atan2(yend - y, xend - x) + FF_PI;
752 double length = sqrt((x - xend) * (x - xend) + (y - yend) * (y - yend));
753
754 cairo_new_path(gw->cc);
755 cairo_move_to(gw->cc, x, y);
756 cairo_line_to(gw->cc, xend, yend);
757 cairo_stroke(gw->cc);
758
759 if (length < 2) { //No point arrowing something so small
760
761 return;
762 } else if (length > 20) {
763 length = 10;
764 } else {
765 length *= 2. / 3.;
766 }
767 cairo_new_path(gw->cc);
768 cairo_move_to(gw->cc, xend, yend);
769 cairo_line_to(gw->cc, xend + length * cos(angle - head_angle), yend + length * sin(angle - head_angle));
770 cairo_line_to(gw->cc, xend + length * cos(angle + head_angle), yend + length * sin(angle + head_angle));
771 cairo_close_path(gw->cc);
772 cairo_fill(gw->cc);
773
774 }
775
776 void GGDKDrawDrawRect(GWindow w, GRect *rect, Color col) {
777 //Log(LOGDEBUG, " ");
778 GGDKWindow gw = (GGDKWindow)w;
779 _GGDKDraw_CheckAutoPaint(gw);
780
781 gw->ggc->fg = col;
782
783 int width = GGDKDrawSetline(gw, gw->ggc);
784 cairo_new_path(gw->cc);
785 if (width & 1) {
786 cairo_rectangle(gw->cc, rect->x + .5, rect->y + .5, rect->width, rect->height);
787 } else {
788 cairo_rectangle(gw->cc, rect->x, rect->y, rect->width, rect->height);
789 }
790 cairo_stroke(gw->cc);
791
792 }
793
794 void GGDKDrawFillRect(GWindow w, GRect *rect, Color col) {
795 //Log(LOGDEBUG, " ");
796 GGDKWindow gw = (GGDKWindow)w;
797 _GGDKDraw_CheckAutoPaint(gw);
798
799 gw->ggc->fg = col;
800
801 GGDKDrawSetcolfunc(gw, gw->ggc);
802
803 cairo_new_path(gw->cc);
804 cairo_rectangle(gw->cc, rect->x, rect->y, rect->width, rect->height);
805 cairo_fill(gw->cc);
806
807 }
808
809 void GGDKDrawFillRoundRect(GWindow w, GRect *rect, int radius, Color col) {
810 //Log(LOGDEBUG, " ");
811 GGDKWindow gw = (GGDKWindow)w;
812 _GGDKDraw_CheckAutoPaint(gw);
813
814 gw->ggc->fg = col;
815
816 GGDKDrawSetcolfunc(gw, gw->ggc);
817
818 double degrees = FF_PI / 180.0;
819 int rr = (radius <= (rect->height + 1) / 2) ? (radius > 0 ? radius : 0) : (rect->height + 1) / 2;
820 cairo_new_path(gw->cc);
821 cairo_arc(gw->cc, rect->x + rect->width - rr, rect->y + rr, rr, -90 * degrees, 0 * degrees);
822 cairo_arc(gw->cc, rect->x + rect->width - rr, rect->y + rect->height - rr, rr, 0 * degrees, 90 * degrees);
823 cairo_arc(gw->cc, rect->x + rr, rect->y + rect->height - rr, rr, 90 * degrees, 180 * degrees);
824 cairo_arc(gw->cc, rect->x + rr, rect->y + rr, rr, 180 * degrees, 270 * degrees);
825 cairo_close_path(gw->cc);
826 cairo_fill(gw->cc);
827
828 }
829
830 void GGDKDrawDrawEllipse(GWindow w, GRect *rect, Color col) {
831 //Log(LOGDEBUG, " ");
832 GGDKWindow gw = (GGDKWindow)w;
833 _GGDKDraw_CheckAutoPaint(gw);
834
835 gw->ggc->fg = col;
836
837 // It is tempting to use the cairo arc command and scale the
838 // coordinates to get an ellipse, but that distorts the stroke width.
839 int lwidth = GGDKDrawSetline(gw, gw->ggc);
840 double cx, cy, width, height;
841
842 width = rect->width / 2.0;
843 height = rect->height / 2.0;
844 cx = rect->x + width;
845 cy = rect->y + height;
846 if (lwidth & 1) {
847 if (rint(width) == width) {
848 cx += .5;
849 }
850 if (rint(height) == height) {
851 cy += .5;
852 }
853 }
854 _GGDKDraw_EllipsePath(gw->cc, cx, cy, width, height);
855 cairo_stroke(gw->cc);
856
857 }
858
859 void GGDKDrawFillEllipse(GWindow w, GRect *rect, Color col) {
860 //Log(LOGDEBUG, " ");
861 GGDKWindow gw = (GGDKWindow)w;
862 _GGDKDraw_CheckAutoPaint(gw);
863
864
865 gw->ggc->fg = col;
866 GGDKDrawSetcolfunc(gw, gw->ggc);
867
868 // It is tempting to use the cairo arc command and scale the
869 // coordinates to get an ellipse, but that distorts the stroke width.
870 double cx, cy, width, height;
871 width = rect->width / 2.0;
872 height = rect->height / 2.0;
873 cx = rect->x + width;
874 cy = rect->y + height;
875 _GGDKDraw_EllipsePath(gw->cc, cx, cy, width, height);
876 cairo_fill(gw->cc);
877
878 }
879
880 /**
881 * \brief Draws an arc (circular and elliptical).
882 *
883 * \param [in] gw The window to draw on.
884 * \param [in] rect The bounding box of the arc. If width!=height, then
885 * an elliptical arc will be drawn.
886 * \param [in] sangle The start angle in degrees * 64 (Cartesian)
887 * \param [in] eangle The angle offset from the start in degrees * 64 (positive CCW)
888 */
889 void GGDKDrawDrawArc(GWindow w, GRect *rect, int32_t sangle, int32_t eangle, Color col) {
890 //Log(LOGDEBUG, " ");
891 GGDKWindow gw = (GGDKWindow)w;
892 _GGDKDraw_CheckAutoPaint(gw);
893
894 gw->ggc->fg = col;
895
896 // Leftover from XDrawArc: sangle/eangle in degrees*64.
897 double start = -(sangle + eangle) * FF_PI / 11520., end = -sangle * FF_PI / 11520.;
898 int width = GGDKDrawSetline(gw, gw->ggc);
899
900 cairo_new_path(gw->cc);
901 cairo_save(gw->cc);
902 if (width & 1) {
903 cairo_translate(gw->cc, rect->x + .5 + rect->width / 2., rect->y + .5 + rect->height / 2.);
904 } else {
905 cairo_translate(gw->cc, rect->x + rect->width / 2., rect->y + rect->height / 2.);
906 }
907 cairo_scale(gw->cc, rect->width / 2., rect->height / 2.);
908 cairo_arc(gw->cc, 0., 0., 1., start, end);
909 cairo_restore(gw->cc);
910 cairo_stroke(gw->cc);
911
912 }
913
914 void GGDKDrawDrawPoly(GWindow w, GPoint *pts, int16_t cnt, Color col) {
915 //Log(LOGDEBUG, " ");
916 GGDKWindow gw = (GGDKWindow)w;
917 _GGDKDraw_CheckAutoPaint(gw);
918
919 gw->ggc->fg = col;
920
921 int width = GGDKDrawSetline(gw, gw->ggc);
922 double off = (width & 1) ? .5 : 0;
923
924 cairo_new_path(gw->cc);
925 cairo_move_to(gw->cc, pts[0].x + off, pts[0].y + off);
926 for (int i = 1; i < cnt; ++i) {
927 cairo_line_to(gw->cc, pts[i].x + off, pts[i].y + off);
928 }
929 cairo_stroke(gw->cc);
930
931 }
932
933 void GGDKDrawFillPoly(GWindow w, GPoint *pts, int16_t cnt, Color col) {
934 //Log(LOGDEBUG, " ");
935 GGDKWindow gw = (GGDKWindow)w;
936 _GGDKDraw_CheckAutoPaint(gw);
937
938 gw->ggc->fg = col;
939
940 GGDKDrawSetcolfunc(gw, gw->ggc);
941
942 cairo_new_path(gw->cc);
943 cairo_move_to(gw->cc, pts[0].x, pts[0].y);
944 for (int i = 1; i < cnt; ++i) {
945 cairo_line_to(gw->cc, pts[i].x, pts[i].y);
946 }
947 cairo_close_path(gw->cc);
948 cairo_fill(gw->cc);
949
950 cairo_set_line_width(gw->cc, 1);
951 cairo_new_path(gw->cc);
952 cairo_move_to(gw->cc, pts[0].x + .5, pts[0].y + .5);
953 for (int i = 1; i < cnt; ++i) {
954 cairo_line_to(gw->cc, pts[i].x + .5, pts[i].y + .5);
955 }
956 cairo_close_path(gw->cc);
957 cairo_stroke(gw->cc);
958
959 }
960
961 void GGDKDrawDrawImage(GWindow w, GImage *image, GRect *src, int32_t x, int32_t y) {
962 //Log(LOGDEBUG, " ");
963 GGDKWindow gw = (GGDKWindow)w;
964 _GGDKDraw_CheckAutoPaint(gw);
965
966 cairo_surface_t *is = _GGDKDraw_GImage2Surface(image, src), *cs = is;
967 struct _GImage *base = (image->list_len == 0) ? image->u.image : image->u.images[0];
968
969 if (cairo_image_surface_get_format(is) == CAIRO_FORMAT_A1) {
970 /* No color info, just alpha channel */
971 Color fg = base->clut->trans_index == 0 ? base->clut->clut[1] : base->clut->clut[0];
972 #ifdef GDK_WINDOWING_QUARTZ
973 // The quartz backend cannot mask/render A1 surfaces directly
974 // So render to intermediate ARGB32 surface first, then render that to screen
975 cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, src->width, src->height);
976 cairo_t *cc = cairo_create(cs);
977 cairo_set_source_rgba(cc, COLOR_RED(fg) / 255.0, COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0, 1.0);
978 cairo_mask_surface(cc, is, 0, 0);
979 cairo_destroy(cc);
980 #else
981 cairo_set_source_rgba(gw->cc, COLOR_RED(fg) / 255.0, COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0, 1.0);
982 cairo_mask_surface(gw->cc, cs, x, y);
983 cs = NULL;
984 #endif
985 }
986
987 if (cs != NULL) {
988 cairo_set_source_surface(gw->cc, cs, x, y);
989 cairo_rectangle(gw->cc, x, y, src->width, src->height);
990 cairo_fill(gw->cc);
991
992 if (cs != is) {
993 cairo_surface_destroy(cs);
994 }
995 }
996 /* Clear source and mask, in case we need to */
997 cairo_new_path(gw->cc);
998 cairo_set_source_rgba(gw->cc, 0, 0, 0, 0);
999
1000 cairo_surface_destroy(is);
1001 }
1002
1003 // What we really want to do is use the grey levels as an alpha channel
1004 void GGDKDrawDrawGlyph(GWindow w, GImage *image, GRect *src, int32_t x, int32_t y) {
1005 //Log(LOGDEBUG, " ");
1006 GGDKWindow gw = (GGDKWindow)w;
1007 _GGDKDraw_CheckAutoPaint(gw);
1008
1009 struct _GImage *base = (image->list_len) == 0 ? image->u.image : image->u.images[0];
1010 cairo_surface_t *is;
1011
1012 if (base->image_type != it_index) {
1013 GGDKDrawDrawImage(w, image, src, x, y);
1014 } else {
1015 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_A8, src->width);
1016 uint8_t *basedata = malloc(stride * src->height),
1017 *data = basedata,
1018 *srcd = base->data + src->y * base->bytes_per_line + src->x;
1019 int factor = base->clut->clut_len == 256 ? 1 :
1020 base->clut->clut_len == 16 ? 17 :
1021 base->clut->clut_len == 4 ? 85 : 255;
1022 int i, j;
1023 Color fg = base->clut->clut[base->clut->clut_len - 1];
1024
1025 for (i = 0; i < src->height; ++i) {
1026 for (j = 0; j < src->width; ++j) {
1027 data[j] = factor * srcd[j];
1028 }
1029 srcd += base->bytes_per_line;
1030 data += stride;
1031 }
1032 is = cairo_image_surface_create_for_data(basedata, CAIRO_FORMAT_A8,
1033 src->width, src->height, stride);
1034 cairo_set_source_rgba(gw->cc, COLOR_RED(fg) / 255.0, COLOR_GREEN(fg) / 255.0, COLOR_BLUE(fg) / 255.0, 1.0);
1035 cairo_mask_surface(gw->cc, is, x, y);
1036 /* I think the mask is sufficient, setting a rectangle would provide */
1037 /* a new mask? */
1038 /*cairo_rectangle(gw->cc,x,y,src->width,src->height);*/
1039 /* I think setting the mask also draws... at least so the tutorial implies */
1040 /* cairo_fill(gw->cc);*/
1041 /* Presumably that doesn't leave the mask surface pattern lying around */
1042 /* but dereferences it so we can free it */
1043 cairo_surface_destroy(is);
1044 free(basedata);
1045 }
1046
1047 }
1048
1049 void GGDKDrawDrawImageMagnified(GWindow w, GImage *image, GRect *src, int32_t x, int32_t y, int32_t width, int32_t height) {
1050 //Log(LOGDEBUG, " ");
1051 GGDKWindow gw = (GGDKWindow)w;
1052 _GGDKDraw_CheckAutoPaint(gw);
1053
1054 struct _GImage *base = (image->list_len == 0) ? image->u.image : image->u.images[0];
1055 GRect full;
1056 double xscale, yscale;
1057 GRect viewable;
1058
1059 viewable = gw->ggc->clip;
1060 if (viewable.width > gw->pos.width - viewable.x) {
1061 viewable.width = gw->pos.width - viewable.x;
1062 }
1063 if (viewable.height > gw->pos.height - viewable.y) {
1064 viewable.height = gw->pos.height - viewable.y;
1065 }
1066
1067 xscale = (base->width >= 1) ? ((double)(width)) / (base->width) : 1;
1068 yscale = (base->height >= 1) ? ((double)(height)) / (base->height) : 1;
1069 /* Intersect the clip rectangle with the scaled image to find the */
1070 /* portion of screen that we want to draw */
1071 if (viewable.x < x) {
1072 viewable.width -= (x - viewable.x);
1073 viewable.x = x;
1074 }
1075 if (viewable.y < y) {
1076 viewable.height -= (y - viewable.y);
1077 viewable.y = y;
1078 }
1079 if (viewable.x + viewable.width > x + width) {
1080 viewable.width = x + width - viewable.x;
1081 }
1082 if (viewable.y + viewable.height > y + height) {
1083 viewable.height = y + height - viewable.y;
1084 }
1085 if (viewable.height < 0 || viewable.width < 0) {
1086
1087 return;
1088 }
1089
1090 /* Now find that same rectangle in the coordinates of the unscaled image */
1091 /* (translation & scale) */
1092 viewable.x -= x;
1093 viewable.y -= y;
1094 full.x = viewable.x / xscale;
1095 full.y = viewable.y / yscale;
1096 full.width = viewable.width / xscale;
1097 full.height = viewable.height / yscale;
1098 if (full.x + full.width > base->width) {
1099 full.width = base->width - full.x; /* Rounding errors */
1100 }
1101 if (full.y + full.height > base->height) {
1102 full.height = base->height - full.y; /* Rounding errors */
1103 }
1104 /* Rounding errors */
1105 {
1106 GImage *temp = _GGDKDraw_GImageExtract(base, &full, &viewable, xscale, yscale);
1107 GRect src;
1108 src.x = src.y = 0;
1109 src.width = viewable.width;
1110 src.height = viewable.height;
1111 GGDKDrawDrawImage(w, temp, &src, x + viewable.x, y + viewable.y);
1112 }
1113
1114 }
1115
1116 void GGDKDrawDrawPixmap(GWindow w, GWindow pixmap, GRect *src, int32_t x, int32_t y) {
1117 //Log(LOGDEBUG, " ");
1118 GGDKWindow gw = (GGDKWindow)w, gpixmap = (GGDKWindow)pixmap;
1119 _GGDKDraw_CheckAutoPaint(gw);
1120
1121 if (!gpixmap->is_pixmap) {
1122 return;
1123 }
1124
1125 cairo_set_source_surface(gw->cc, gpixmap->cs, x - src->x, y - src->y);
1126 cairo_rectangle(gw->cc, x, y, src->width, src->height);
1127 cairo_fill(gw->cc);
1128 }
1129
1130 void GGDKDrawPathStartNew(GWindow w) {
1131 //Log(LOGDEBUG, " ");
1132 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
1133 cairo_new_path(((GGDKWindow)w)->cc);
1134 }
1135
1136 void GGDKDrawPathClose(GWindow w) {
1137 //Log(LOGDEBUG, " ");
1138 cairo_close_path(((GGDKWindow)w)->cc);
1139 }
1140
1141 void GGDKDrawPathMoveTo(GWindow w, double x, double y) {
1142 //Log(LOGDEBUG, " ");
1143 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
1144 cairo_move_to(((GGDKWindow)w)->cc, x, y);
1145 }
1146
1147 void GGDKDrawPathLineTo(GWindow w, double x, double y) {
1148 //Log(LOGDEBUG, " ");
1149 cairo_line_to(((GGDKWindow)w)->cc, x, y);
1150 }
1151
1152 void GGDKDrawPathCurveTo(GWindow w, double cx1, double cy1, double cx2, double cy2, double x, double y) {
1153 //Log(LOGDEBUG, " ");
1154 cairo_curve_to(((GGDKWindow)w)->cc, cx1, cy1, cx2, cy2, x, y);
1155 }
1156
1157 void GGDKDrawPathStroke(GWindow w, Color col) {
1158 //Log(LOGDEBUG, " ");
1159 w->ggc->fg = col;
1160 GGDKDrawSetline((GGDKWindow) w, w->ggc);
1161 cairo_stroke(((GGDKWindow)w)->cc);
1162 }
1163
1164 void GGDKDrawPathFill(GWindow w, Color col) {
1165 //Log(LOGDEBUG, " ");
1166 cairo_set_source_rgba(((GGDKWindow) w)->cc, COLOR_RED(col) / 255.0, COLOR_GREEN(col) / 255.0, COLOR_BLUE(col) / 255.0,
1167 (col >> 24) / 255.0);
1168 cairo_fill(((GGDKWindow) w)->cc);
1169 }
1170
1171 void GGDKDrawPathFillAndStroke(GWindow w, Color fillcol, Color strokecol) {
1172 //Log(LOGDEBUG, " ");
1173 // This function is unused, so it's unclear if it's implemented correctly.
1174 GGDKWindow gw = (GGDKWindow) w;
1175
1176 cairo_save(gw->cc);
1177 cairo_set_source_rgba(gw->cc, COLOR_RED(fillcol) / 255.0, COLOR_GREEN(fillcol) / 255.0, COLOR_BLUE(fillcol) / 255.0,
1178 (fillcol >> 24) / 255.0);
1179 cairo_fill(gw->cc);
1180 cairo_restore(gw->cc);
1181 w->ggc->fg = strokecol;
1182 GGDKDrawSetline(gw, gw->ggc);
1183 cairo_fill_preserve(gw->cc);
1184 cairo_stroke(gw->cc);
1185 }
1186
1187 void GGDKDrawStartNewSubPath(GWindow w) {
1188 //Log(LOGDEBUG, " ");
1189 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
1190 cairo_new_sub_path(((GGDKWindow)w)->cc);
1191 }
1192
1193 int GGDKDrawFillRuleSetWinding(GWindow w) {
1194 //Log(LOGDEBUG, " ");
1195 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
1196 cairo_set_fill_rule(((GGDKWindow)w)->cc, CAIRO_FILL_RULE_WINDING);
1197 return 1;
1198 }
1199
1200 int GGDKDrawDoText8(GWindow w, int32_t x, int32_t y, const char *text, int32_t cnt, Color col, enum text_funcs drawit,
1201 struct tf_arg *arg) {
1202 //Log(LOGDEBUG, " ");
1203
1204 GGDKWindow gw = (GGDKWindow) w;
1205 struct font_instance *fi = gw->ggc->fi;
1206 PangoRectangle rect, ink;
1207 PangoFontDescription *fd;
1208
1209 if (fi == NULL) {
1210 return 0;
1211 }
1212
1213 fd = _GGDKDraw_configfont(w, fi);
1214 if (fd == NULL) {
1215 return 0;
1216 }
1217
1218 pango_layout_set_font_description(gw->pango_layout, fd);
1219 pango_layout_set_text(gw->pango_layout, (char *)text, cnt);
1220 pango_layout_get_pixel_extents(gw->pango_layout, &ink, &rect);
1221
1222 if (drawit == tf_drawit) {
1223 _GGDKDraw_CheckAutoPaint(gw);
1224 _GGDKDraw_MyCairoRenderLayout(gw->cc, col, gw->pango_layout, x, y);
1225 } else if (drawit == tf_rect) {
1226 PangoLayoutIter *iter;
1227 PangoLayoutRun *run;
1228 PangoFontMetrics *fm;
1229
1230 arg->size.lbearing = ink.x - rect.x;
1231 arg->size.rbearing = ink.x + ink.width - rect.x;
1232 arg->size.width = rect.width;
1233 if (*text == '\0') {
1234 // There are no runs if there are no characters
1235 memset(&arg->size, 0, sizeof(arg->size));
1236 } else {
1237 iter = pango_layout_get_iter(gw->pango_layout);
1238 run = pango_layout_iter_get_run(iter);
1239 if (run == NULL) {
1240 // Pango doesn't give us runs in a couple of other places
1241 // surrogates, not unicode (0xfffe, 0xffff), etc.
1242 memset(&arg->size, 0, sizeof(arg->size));
1243 } else {
1244 int baseline = pango_layout_iter_get_baseline(iter) / PANGO_SCALE;
1245 fm = pango_font_get_metrics(run->item->analysis.font, NULL);
1246 arg->size.fas = pango_font_metrics_get_ascent(fm) / PANGO_SCALE;
1247 arg->size.fds = pango_font_metrics_get_descent(fm) / PANGO_SCALE;
1248 // Layout rects have origin in top left corner extending down/right
1249 arg->size.as = baseline - ink.y;
1250 arg->size.ds = ink.y + ink.height - baseline;
1251 if (arg->size.ds < 0) {
1252 Log(LOGDEBUG, "Negative descent fas(%d) fds(%d) as(%d) ds(%d) baseline(%d) ink_x(%d) ink_y(%d) ink_width(%d) ink_height(%d) font(%s %d %d) text(%s)",
1253 arg->size.fas, arg->size.fds, arg->size.as, arg->size.ds,
1254 baseline, ink.x, ink.y, ink.width, ink.height,
1255 fi->rq.utf8_family_name, (int)fi->rq.point_size, (int)fi->rq.weight,
1256 text);
1257 --arg->size.as;
1258 arg->size.ds = 0;
1259 }
1260 pango_font_metrics_unref(fm);
1261 }
1262 pango_layout_iter_free(iter);
1263 }
1264 }
1265 return rect.width;
1266 }
1267
1268 void GGDKDrawPushClipOnly(GWindow w) {
1269 //Log(LOGDEBUG, " ");
1270 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
1271 cairo_save(((GGDKWindow)w)->cc);
1272 }
1273
1274 void GGDKDrawClipPreserve(GWindow w) {
1275 //Log(LOGDEBUG, " ");
1276 _GGDKDraw_CheckAutoPaint((GGDKWindow)w);
1277 cairo_clip_preserve(((GGDKWindow)w)->cc);
1278 }
1279
1280 // PANGO LAYOUT
1281
1282 void GGDKDrawGetFontMetrics(GWindow gw, GFont *fi, int *as, int *ds, int *ld) {
1283 //Log(LOGDEBUG, " ");
1284
1285 GGDKDisplay *gdisp = ((GGDKWindow) gw)->display;
1286 PangoFont *pfont;
1287 PangoFontMetrics *fm;
1288 PangoFontMap *pfm;
1289
1290 _GGDKDraw_configfont(gw, fi);
1291 pfm = pango_context_get_font_map(gdisp->pangoc_context);
1292 pfont = pango_font_map_load_font(pfm, gdisp->pangoc_context, fi->pangoc_fd);
1293 fm = pango_font_get_metrics(pfont, NULL);
1294 *as = pango_font_metrics_get_ascent(fm) / PANGO_SCALE;
1295 *ds = pango_font_metrics_get_descent(fm) / PANGO_SCALE;
1296 *ld = 0;
1297 pango_font_metrics_unref(fm);
1298 if (pfont != NULL) {
1299 g_object_unref(pfont);
1300 }
1301 }
1302
1303 void GGDKDrawLayoutInit(GWindow w, char *text, int cnt, GFont *fi) {
1304 //Log(LOGDEBUG, " ");
1305 GGDKWindow gw = (GGDKWindow) w;
1306 PangoFontDescription *fd;
1307
1308 if (fi == NULL) {
1309 fi = gw->ggc->fi;
1310 }
1311
1312 fd = _GGDKDraw_configfont(w, fi);
1313 pango_layout_set_font_description(gw->pango_layout, fd);
1314 pango_layout_set_text(gw->pango_layout, text, cnt);
1315 }
1316
1317 void GGDKDrawLayoutDraw(GWindow w, int32_t x, int32_t y, Color fg) {
1318 //Log(LOGDEBUG, " ");
1319 GGDKWindow gw = (GGDKWindow)w;
1320
1321 _GGDKDraw_MyCairoRenderLayout(gw->cc, fg, gw->pango_layout, x, y);
1322 }
1323
1324 void GGDKDrawLayoutIndexToPos(GWindow w, int index, GRect *pos) {
1325 //Log(LOGDEBUG, " ");
1326 GGDKWindow gw = (GGDKWindow)w;
1327 PangoRectangle rect;
1328
1329 pango_layout_index_to_pos(gw->pango_layout, index, &rect);
1330 pos->x = rect.x / PANGO_SCALE;
1331 pos->y = rect.y / PANGO_SCALE;
1332 pos->width = rect.width / PANGO_SCALE;
1333 pos->height = rect.height / PANGO_SCALE;
1334 }
1335
1336 int GGDKDrawLayoutXYToIndex(GWindow w, int x, int y) {
1337 //Log(LOGDEBUG, " ");
1338 GGDKWindow gw = (GGDKWindow)w;
1339 int trailing, index;
1340
1341 // Pango returns the last character if x is negative, not the first.
1342 if (x < 0) {
1343 x = 0;
1344 }
1345 pango_layout_xy_to_index(gw->pango_layout, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
1346
1347 // If I give Pango a position after the last character on a line, it
1348 // returns to me the first character. Strange. And annoying -- you click
1349 // at the end of a line and the cursor moves to the start
1350 // Of course in right to left text an initial position is correct...
1351 if ((index + trailing) == 0 && x > 0) {
1352 PangoRectangle rect;
1353 pango_layout_get_pixel_extents(gw->pango_layout, &rect, NULL);
1354 if (x >= rect.width) {
1355 x = rect.width - 1;
1356 pango_layout_xy_to_index(gw->pango_layout, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
1357 }
1358 }
1359 return index + trailing;
1360 }
1361
1362 void GGDKDrawLayoutExtents(GWindow w, GRect *size) {
1363 //Log(LOGDEBUG, " ");
1364 GGDKWindow gw = (GGDKWindow)w;
1365 PangoRectangle rect;
1366
1367 pango_layout_get_pixel_extents(gw->pango_layout, NULL, &rect);
1368 size->x = rect.x;
1369 size->y = rect.y;
1370 size->width = rect.width;
1371 size->height = rect.height;
1372 }
1373
1374 void GGDKDrawLayoutSetWidth(GWindow w, int width) {
1375 //Log(LOGDEBUG, " ");
1376 GGDKWindow gw = (GGDKWindow)w;
1377
1378 pango_layout_set_width(gw->pango_layout, (width == -1) ? -1 : width * PANGO_SCALE);
1379 }
1380
1381 int GGDKDrawLayoutLineCount(GWindow w) {
1382 //Log(LOGDEBUG, " ");
1383 GGDKWindow gw = (GGDKWindow)w;
1384
1385 return pango_layout_get_line_count(gw->pango_layout);
1386 }
1387
1388 int GGDKDrawLayoutLineStart(GWindow w, int l) {
1389 //Log(LOGDEBUG, " ");
1390 GGDKWindow gw = (GGDKWindow)w;
1391 PangoLayoutLine *line = pango_layout_get_line(gw->pango_layout, l);
1392
1393 if (line == NULL) {
1394 return -1;
1395 }
1396
1397 return line->start_index;
1398 }
1399