summaryrefslogtreecommitdiff
path: root/src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx
blob: 6483dc30bf183d4715f36b744420fdcd41130e21 (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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
//
// Support for Cairo graphics for the Fast Light Tool Kit (FLTK).
//
// Copyright 2021-2024 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

/* \file
    Implementation of class Fl_Cairo_Graphics_Driver .
*/

#include <config.h>
#if USE_PANGO

#include "Fl_Cairo_Graphics_Driver.H"
#include "../../Fl_Screen_Driver.H"
#include <FL/platform.H>
#include <FL/fl_draw.H>
#include <FL/fl_utf8.h>
#include <cairo.h>
#include <pango/pangocairo.h>
#if ! PANGO_VERSION_CHECK(1,16,0)
#  error "Requires Pango 1.16 or higher"
#endif
#if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1,10,0)
#  error "Requires Cairo 1.10 or higher"
#endif
#include <math.h>
#include <stdlib.h>  // abs(int)
#include <string.h>  // memcpy()
#include <stdint.h>  // uint32_t

extern unsigned fl_cmap[256]; // defined in fl_color.cxx

// The predefined fonts that FLTK has with Pango:
static Fl_Fontdesc built_in_table[] = {
  {"Sans"},
  {"Sans Bold"},
  {"Sans Italic"},
  {"Sans Bold Italic"},
  {"Monospace"},
  {"Monospace Bold"},
  {"Monospace Italic"},
  {"Monospace Bold Italic"},
  {"Serif"},
  {"Serif Bold"},
  {"Serif Italic"},
  {"Serif Bold Italic"},
  {"Standard Symbols PS"}, // FL_SYMBOL
  {"Monospace"},
  {"Monospace Bold"},
  {"D050000L"},            // FL_ZAPF_DINGBATS
};


FL_EXPORT Fl_Fontdesc *fl_fonts = built_in_table;

// Number of fonts found by Fl::set_fonts(char*) beyond FL_FREE_FONT
// -1 denotes "not yet initialised"
Fl_Font Fl_Cairo_Graphics_Driver::font_count_ = -1;

// duplicated from Fl_PostScript.cxx
struct callback_data {
  const uchar *data;
  int D, LD;
};
static const int dashes_flat[5][7]={
{-1,0,0,0,0,0,0},
{3,1,-1,0,0,0,0},
{1,1,-1,0,0,0,0},
{3,1,1,1,-1,0,0},
{3,1,1,1,1,1,-1}
};
static const double dashes_cap[5][7]={
{-1,0,0,0,0,0,0},
{2,2,-1,0,0,0,0},
{0.01,1.99,-1,0,0,0,0},
{2,2,0.01,1.99,-1,0,0},
{2,2,0.01,1.99,0.01,1.99,-1}
};
static void draw_image_cb(void *data, int x, int y, int w, uchar *buf) {
  struct callback_data *cb_data;
  const uchar *curdata;

  cb_data = (struct callback_data*)data;
  int last = x+w;
  const size_t aD = abs(cb_data->D);
  curdata = cb_data->data + x*cb_data->D + y*cb_data->LD;
  for (; x<last; x++) {
    memcpy(buf, curdata, aD);
    buf += aD;
    curdata += cb_data->D;
  }
}
// end of duplicated part


Fl_Cairo_Graphics_Driver::Fl_Cairo_Graphics_Driver() : Fl_Graphics_Driver() {
  cairo_ = NULL;
  pango_layout_ = NULL;
  pango_context_ = NULL;
  dummy_cairo_ = NULL;
  linestyle_ = FL_SOLID;
  clip_ = NULL;
  scale_x = scale_y = 1;
  wld_scale = 1;
  angle = 0;
  left_margin = top_margin = 0;
  needs_commit_tag_ = NULL;
  what = NONE;
}

Fl_Cairo_Graphics_Driver::~Fl_Cairo_Graphics_Driver() {
  if (pango_layout_) g_object_unref(pango_layout_);
  if (pango_context_) g_object_unref(pango_context_);
}

const cairo_format_t Fl_Cairo_Graphics_Driver::cairo_format = CAIRO_FORMAT_ARGB32;


void Fl_Cairo_Graphics_Driver::set_cairo(cairo_t *cr, float s) {
  if (dummy_cairo_) {
    cairo_destroy(dummy_cairo_);
    dummy_cairo_ = NULL;
 }
  cairo_ = cr;
  cairo_restore(cairo_);
  line_style(0);
  cairo_save(cairo_);
  if (s == 0) s = scale();
  cairo_scale(cairo_, s, s);
  cairo_translate(cairo_, 0.5, 0.5);
}


void Fl_Cairo_Graphics_Driver::rectf(int x, int y, int w, int h) {
  cairo_rectangle(cairo_, x-0.5, y-0.5, w, h);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_fill(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::rect(int x, int y, int w, int h) {
  cairo_rectangle(cairo_, x, y, w-1, h-1);
  if (linestyle_ == FL_SOLID) cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  if (linestyle_ == FL_SOLID) cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

static bool need_antialias_none(cairo_t *cairo_, int style) {
  cairo_matrix_t matrix;
  cairo_get_matrix(cairo_, &matrix);
  double width = cairo_get_line_width(cairo_) * matrix.xx;
  bool needit = (style == FL_SOLID && width < 1.5);
  if (needit) cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  return needit;
}

void Fl_Cairo_Graphics_Driver::line(int x1, int y1, int x2, int y2) {
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, x1, y1);
  cairo_line_to(cairo_, x2, y2);
  bool needit = need_antialias_none(cairo_, linestyle_);
  cairo_stroke(cairo_);
  if (needit) cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::line(int x0, int y0, int x1, int y1, int x2, int y2) {
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, x0, y0);
  cairo_line_to(cairo_, x1, y1);
  cairo_line_to(cairo_, x2, y2);
  bool needit = need_antialias_none(cairo_, linestyle_);
  cairo_stroke(cairo_);
  if (needit) cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::xyline(int x, int y, int x1) {
  cairo_move_to(cairo_, x, y);
  cairo_line_to(cairo_, x1, y);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::xyline(int x, int y, int x1, int y2) {
  cairo_move_to(cairo_, x, y);
  cairo_line_to(cairo_, x1, y);
  cairo_line_to(cairo_, x1, y2);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::xyline(int x, int y, int x1, int y2, int x3) {
  cairo_move_to(cairo_, x, y);
  cairo_line_to(cairo_, x1, y);
  cairo_line_to(cairo_, x1, y2);
  cairo_line_to(cairo_, x3, y2);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::yxline(int x, int y, int y1) {
  cairo_move_to(cairo_, x, y);
  cairo_line_to(cairo_, x, y1);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::yxline(int x, int y, int y1, int x2) {
  cairo_move_to(cairo_, x, y);
  cairo_line_to(cairo_, x, y1);
  cairo_line_to(cairo_, x2, y1);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::yxline(int x, int y, int y1, int x2, int y3) {
  cairo_move_to(cairo_, x, y);
  cairo_line_to(cairo_, x, y1);
  cairo_line_to(cairo_, x2, y1);
  cairo_line_to(cairo_, x2, y3);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_stroke(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::loop(int x0, int y0, int x1, int y1, int x2, int y2) {
  cairo_save(cairo_);
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, x0, y0);
  cairo_line_to(cairo_, x1, y1);
  cairo_line_to(cairo_, x2, y2);
  cairo_close_path(cairo_);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::loop(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
  cairo_save(cairo_);
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, x0, y0);
  cairo_line_to(cairo_, x1, y1);
  cairo_line_to(cairo_, x2, y2);
  cairo_line_to(cairo_, x3, y3);
  cairo_close_path(cairo_);
  if ((y0==y1 && x1==x2 && y2==y3 && x3==x0) || (x0==x1 && y1==y2 && x2==x3 && y3==y0)) {
    cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  }
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::polygon(int x0, int y0, int x1, int y1, int x2, int y2) {
  cairo_save(cairo_);
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, x0, y0);
  cairo_line_to(cairo_, x1, y1);
  cairo_line_to(cairo_, x2, y2);
  cairo_close_path(cairo_);
  cairo_fill(cairo_);
  cairo_restore(cairo_);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::polygon(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
  cairo_save(cairo_);
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, x0, y0);
  cairo_line_to(cairo_, x1, y1);
  cairo_line_to(cairo_, x2, y2);
  cairo_line_to(cairo_, x3, y3);
  cairo_close_path(cairo_);
  if ((y0==y1 && x1==x2 && y2==y3 && x3==x0) || (x0==x1 && y1==y2 && x2==x3 && y3==y0)) {
    cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  }
  cairo_fill(cairo_);
  cairo_restore(cairo_);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::line_style(int style, int width, char* dashes) {
  linestyle_ = style;
  if(dashes){
    if(dashes != linedash_)
      strcpy(linedash_,dashes);

  } else
    linedash_[0]=0;
  char width0 = 0;
  if (!width){
    width=1; //for screen drawing compatibility
    width0=1;
  }
  cairo_set_line_width(cairo_, width);

  if(!style && (!dashes || !(*dashes)) && width0) //system lines
    style = FL_CAP_SQUARE;

  int cap = (style &0xf00);
  cairo_line_cap_t c_cap;
  if (cap == FL_CAP_SQUARE) c_cap = CAIRO_LINE_CAP_SQUARE;
  else if (cap == FL_CAP_FLAT) c_cap = CAIRO_LINE_CAP_BUTT;
  else if (cap == FL_CAP_ROUND) c_cap = CAIRO_LINE_CAP_ROUND;
  else c_cap = CAIRO_LINE_CAP_BUTT;
  cairo_set_line_cap(cairo_, c_cap);

  int join = (style & 0xf000);
  cairo_line_join_t c_join;
  if (join == FL_JOIN_MITER) c_join = CAIRO_LINE_JOIN_MITER;
  else if (join == FL_JOIN_ROUND)c_join = CAIRO_LINE_JOIN_ROUND;
  else if (join == FL_JOIN_BEVEL) c_join = CAIRO_LINE_JOIN_BEVEL;
  else c_join = CAIRO_LINE_JOIN_MITER;
  cairo_set_line_join(cairo_, c_join);

  double *ddashes = NULL;
  int l = 0;
  if (dashes && *dashes){
    ddashes = new double[strlen(dashes)];
    while (dashes[l]) {ddashes[l] = dashes[l]; l++; }
  } else if (style & 0xff) {
    ddashes = new double[6];
    if (style & 0x200){ // round and square caps, dash length need to be adjusted
      const double *dt = dashes_cap[style & 0xff];
      while (*dt >= 0){
        ddashes[l++] = width * (*dt);
        dt++;
      }
    } else {
      const int *ds = dashes_flat[style & 0xff];
      while (*ds >= 0){
        ddashes[l++] = width * (*ds);
        ds++;
      }
    }
  }
  cairo_set_dash(cairo_, ddashes, l, 0);
  cairo_set_antialias(cairo_, l ? CAIRO_ANTIALIAS_NONE : CAIRO_ANTIALIAS_DEFAULT);
  delete[] ddashes;
  check_status();
}

void Fl_Cairo_Graphics_Driver::color(unsigned char r, unsigned char g, unsigned char b) {
  Fl_Graphics_Driver::color( fl_rgb_color(r, g, b) );
  cr_ = r; cg_ = g; cb_ = b;
  double fr, fg, fb;
  fr = r/255.0;
  fg = g/255.0;
  fb = b/255.0;
  cairo_set_source_rgb(cairo_, fr, fg, fb);
  check_status();
}

void Fl_Cairo_Graphics_Driver::color(Fl_Color i) {
  Fl_Graphics_Driver::color(i);
  if (!cairo_) return; // no context yet? We will assign the color later.
  uchar r, g, b;
  double fa = 1.0;
  if (i & 0xFFFFFF00) {
    // translate rgb colors into color index
    r = i>>24;
    g = i>>16;
    b = i>> 8;
  } else {
    // translate index into rgb:
    unsigned c = fl_cmap[i];
    c = c ^ 0x000000ff; // trick to restore the color's correct alpha value
    r = c>>24;
    g = c>>16;
    b = c>> 8;
    fa = (c & 0xff)/255.0;
  }
  double fr = r/255.0;
  double fg = g/255.0;
  double fb = b/255.0;
  cairo_set_source_rgba(cairo_, fr, fg, fb, fa);
}

Fl_Color Fl_Cairo_Graphics_Driver::color() { return Fl_Graphics_Driver::color(); }


void Fl_Cairo_Graphics_Driver::concat(){
  cairo_matrix_t mat = {m.a , m.b , m.c , m.d , m.x , m.y};
  cairo_transform(cairo_, &mat);
}

void Fl_Cairo_Graphics_Driver::reconcat(){
  cairo_matrix_t mat = {m.a , m.b , m.c , m.d , m.x , m.y};
  cairo_status_t stat = cairo_matrix_invert(&mat);
  if (stat != CAIRO_STATUS_SUCCESS) {
    fputs("error in cairo_matrix_invert\n", stderr);
  }
  cairo_transform(cairo_, &mat);
}


void Fl_Cairo_Graphics_Driver::begin_line() {
  cairo_save(cairo_);
  concat();
  cairo_new_path(cairo_);
  gap_=1;
  what=LINE;
}

void Fl_Cairo_Graphics_Driver::begin_loop() {
  cairo_save(cairo_);
  concat();
  cairo_new_path(cairo_);
  gap_=1;
  what=LOOP;
}

void Fl_Cairo_Graphics_Driver::begin_polygon() {
  cairo_save(cairo_);
  concat();
  cairo_new_path(cairo_);
  gap_=1;
  what=POLYGON;
}

void Fl_Cairo_Graphics_Driver::vertex(double x, double y) {
  if (what==POINTS){
    return Fl_Graphics_Driver::vertex(x, y);
  }
  if (gap_){
    cairo_move_to(cairo_, x, y);
    gap_=0;
  } else {
    cairo_line_to(cairo_, x, y);
    surface_needs_commit();
  }
}

void Fl_Cairo_Graphics_Driver::curve(double x, double y, double x1, double y1, double x2, double y2, double x3, double y3)
{
  if(what==NONE) return;
  if (what == POINTS) Fl_Graphics_Driver::curve(x, y, x1, y1, x2, y2, x3, y3);
  else {
  if(gap_)
    cairo_move_to(cairo_, x, y);
  else
    cairo_line_to(cairo_, x, y);
  gap_=0;
  cairo_curve_to(cairo_, x1 , y1 , x2 , y2 , x3 , y3);
  }
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::circle(double x, double y, double r){
  if (what == NONE) {
    cairo_save(cairo_);
    concat();
    cairo_arc(cairo_, x, y, r, 0, 2*M_PI);
    cairo_stroke(cairo_);
    reconcat();
    cairo_restore(cairo_);
  } else {
    cairo_arc(cairo_, x, y, r, 0, 2*M_PI);
  }
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::arc(double x, double y, double r, double start, double a){
  if (what == NONE) return;
  if (gap_ == 1) cairo_new_sub_path(cairo_); // 1.2
  gap_ = 0;
  if (start > a)
    cairo_arc(cairo_, x, y, r, -start*M_PI/180, -a*M_PI/180);
  else
    cairo_arc_negative(cairo_, x, y, r, -start*M_PI/180, -a*M_PI/180);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::arc(int x, int y, int w, int h, double a1, double a2) {
  if (w <= 1 || h <= 1) return;
  cairo_save(cairo_);
  begin_line();
  cairo_translate(cairo_, x + w/2.0 -0.5 , y + h/2.0 - 0.5);
  cairo_scale(cairo_, (w-1)/2.0 , (h-1)/2.0);
  arc(0,0,1,a2,a1);
  cairo_scale(cairo_, 2.0/(w-1) , 2.0/(h-1));
  cairo_translate(cairo_, -x - w/2.0 +0.5 , -y - h/2.0 +0.5);
  end_line();
  cairo_restore(cairo_);
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::pie(int x, int y, int w, int h, double a1, double a2) {
  cairo_save(cairo_);
  begin_polygon();
  cairo_translate(cairo_, x + w/2.0 -0.5 , y + h/2.0 - 0.5);
  cairo_scale(cairo_, w/2.0 , h/2.0);
  vertex(0,0);
  arc(0.0,0.0, 1, a2, a1);
  end_polygon();
  cairo_restore(cairo_);
}

void Fl_Cairo_Graphics_Driver::end_points() {
 for (int i = 0; i < n; i++) {
   point(xpoint[i].x, xpoint[i].y);
 }
}

void Fl_Cairo_Graphics_Driver::end_line() {
  gap_ = 1;
  reconcat();
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
  what = NONE;
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::end_loop(){
  gap_ = 1;
  reconcat();
  cairo_close_path(cairo_);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
  what = NONE;
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::end_polygon() {
  gap_ = 1;
  reconcat();
  cairo_close_path(cairo_);
  cairo_fill(cairo_);
  cairo_restore(cairo_);
  what = NONE;
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::transformed_vertex(double x, double y) {
  if (what == POINTS) {
    Fl_Graphics_Driver::transformed_vertex(x, y);
  } else {
    reconcat();
    if (gap_) {
      cairo_move_to(cairo_, x, y);
      gap_ = 0;
    } else {
      cairo_line_to(cairo_, x, y);
      surface_needs_commit();
    }
    concat();
  }
}

void Fl_Cairo_Graphics_Driver::push_clip(int x, int y, int w, int h) {
  Clip *c = new Clip();
  clip_box(x,y,w,h,c->x,c->y,c->w,c->h);
  c->prev = clip_;
  clip_ = c;
  cairo_save(cairo_);
  cairo_rectangle(cairo_, clip_->x - 0.5 , clip_->y - 0.5 , clip_->w, clip_->h);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
  cairo_clip(cairo_);
  cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  check_status();
}

void Fl_Cairo_Graphics_Driver::push_no_clip() {
  Clip *c = new Clip();
  c->prev = clip_;
  clip_ = c;
  clip_->x = clip_->y = clip_->w = clip_->h = -1;
  cairo_save(cairo_);
  cairo_reset_clip(cairo_);
  check_status();
}

void Fl_Cairo_Graphics_Driver::pop_clip() {
  if(!clip_)return;
  Clip *c = clip_;
  clip_ = clip_->prev;
  delete c;
  cairo_restore(cairo_);
  check_status();
}

void Fl_Cairo_Graphics_Driver::ps_origin(int x, int y) {
  cairo_restore(cairo_);
  cairo_restore(cairo_);
  cairo_save(cairo_);
  cairo_scale(cairo_, scale_x, scale_y);
  cairo_translate(cairo_, x, y);
  cairo_rotate(cairo_, angle * M_PI / 180);
  cairo_save(cairo_);
  check_status();
}

void Fl_Cairo_Graphics_Driver::ps_translate(int x, int y)
{
  cairo_save(cairo_);
  cairo_translate(cairo_, x, y);
  cairo_save(cairo_);
  check_status();
}

void Fl_Cairo_Graphics_Driver::ps_untranslate(void)
{
  cairo_restore(cairo_);
  cairo_restore(cairo_);
  check_status();
}

void Fl_Cairo_Graphics_Driver::check_status(void) {
#ifdef DEBUG
  if (cairo_status(cairo_) !=  CAIRO_STATUS_SUCCESS) {
    fprintf(stderr,"we have a problem");
  }
#endif
}

void Fl_Cairo_Graphics_Driver::draw_image(Fl_Draw_Image_Cb call, void *data, int ix, int iy, int iw, int ih, int D)
{
  uchar *array = new uchar[iw * D * ih];
  for (int l = 0; l < ih; l++) {
    call(data, 0, l, iw, array + l*D*iw);
    if (D%2 == 0) for (int i = 0; i < iw; i++) {
      *(array + l*D*iw + i*D + D-1) = 0xff;
    }
  }
  Fl_RGB_Image *rgb = new Fl_RGB_Image(array, iw, ih, D);
  rgb->alloc_array  = 1;
  draw_rgb(rgb, ix, iy, iw, ih, 0, 0);
  delete rgb;
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::draw_image_mono(const uchar *data, int ix, int iy, int iw, int ih, int D, int LD)
{
  struct callback_data cb_data;
  const size_t aD = abs(D);
  if (!LD) LD = iw * aD;
  cb_data.data = data;
  cb_data.D = D;
  cb_data.LD = LD;
  draw_image(draw_image_cb, &cb_data, ix, iy, iw, ih, abs(D));
  surface_needs_commit();
}

void Fl_Cairo_Graphics_Driver::draw_image_mono(Fl_Draw_Image_Cb call, void *data, int ix, int iy, int iw, int ih, int D)
{
  draw_image(call, data, ix, iy, iw, ih, D);
}


int Fl_Cairo_Graphics_Driver::not_clipped(int x, int y, int w, int h) {
  if (!clip_) return 1;
  if (clip_->w < 0) return 1;
  int X = 0, Y = 0, W = 0, H = 0;
  clip_box(x, y, w, h, X, Y, W, H);
  if (W) return 1;
  return 0;
}


int Fl_Cairo_Graphics_Driver::clip_box(int x, int y, int w, int h, int &X, int &Y, int &W, int &H) {
  if (!clip_) {
    X = x; Y = y; W = w; H = h;
    return 0;
  }
  if (clip_->w < 0) {
    X = x; Y = y; W = w; H = h;
    return 1;
  }
  int ret = 0;
  if (x > (X=clip_->x)) {X=x; ret=1;}
  if (y > (Y=clip_->y)) {Y=y; ret=1;}
  if ((x+w) < (clip_->x+clip_->w)) {
    W=x+w-X;

    ret=1;

  }else
    W = clip_->x + clip_->w - X;
  if(W<0){
    W=0;
    return 1;
  }
  if ((y+h) < (clip_->y+clip_->h)) {
    H=y+h-Y;
    ret=1;
  }else
    H = clip_->y + clip_->h - Y;
  if(H<0){
    W=0;
    H=0;
    return 1;
  }
  return ret;
}


void Fl_Cairo_Graphics_Driver::point(int x, int y) {
  rectf(x, y, 1, 1);
}


void Fl_Cairo_Graphics_Driver::draw_image(const uchar *data, int ix, int iy, int iw, int ih, int D, int LD) {
  if (abs(D)<3){ //mono
    draw_image_mono(data, ix, iy, iw, ih, D, LD);
    return;
  }
  struct callback_data cb_data;
  if (!LD) LD = iw*abs(D);
  cb_data.data = data;
  cb_data.D = D;
  cb_data.LD = LD;
  Fl_Cairo_Graphics_Driver::draw_image(draw_image_cb, &cb_data, ix, iy, iw, ih, abs(D));
}


void Fl_Cairo_Graphics_Driver::overlay_rect(int x, int y, int w , int h) {
  cairo_save(cairo_);
  cairo_matrix_t mat;
  cairo_get_matrix(cairo_, &mat);
  float s = (float)mat.xx;
  cairo_matrix_init_identity(&mat);
  cairo_set_matrix(cairo_, &mat); // use drawing units
  int lwidth = s < 1 ? 1 : int(s);
  cairo_set_line_width(cairo_, lwidth);
  cairo_translate(cairo_, lwidth/2., lwidth/2.); // translate by half of line width
  double ddash = (lwidth > 2 ? lwidth : 2);
  if (linestyle_ == FL_DOT) {
    cairo_set_dash(cairo_, &ddash, 1, 0); // dash size = line width
  }
  // rectangle in drawing units
  int Xs = Fl_Scalable_Graphics_Driver::floor(x, s);
  int Ws = Fl_Scalable_Graphics_Driver::floor(x+w-1, s) - Xs;
  int Ys = Fl_Scalable_Graphics_Driver::floor(y, s);
  int Hs = Fl_Scalable_Graphics_Driver::floor(y+h-1, s) - Ys;
  cairo_move_to(cairo_, Xs, Ys);
  cairo_line_to(cairo_, Xs+Ws, Ys);
  cairo_line_to(cairo_, Xs+Ws, Ys+Hs);
  cairo_line_to(cairo_, Xs, Ys+Hs);
  cairo_close_path(cairo_);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
  surface_needs_commit();
}


void Fl_Cairo_Graphics_Driver::draw_cached_pattern_(Fl_Image *img, cairo_pattern_t *pat, int X, int Y, int W, int H, int cx, int cy, int cache_w, int cache_h) {
  // compute size of output image in drawing units
  cairo_matrix_t matrix;
  cairo_get_matrix(cairo_, &matrix);
  float s = (float)matrix.xx;
  int Xs = Fl_Scalable_Graphics_Driver::floor(X - cx, s);
  int Ws = Fl_Scalable_Graphics_Driver::floor(X - cx + img->w(), s) - Xs  ;
  int Ys = Fl_Scalable_Graphics_Driver::floor(Y - cy, s);
  int Hs = Fl_Scalable_Graphics_Driver::floor(Y - cy + img->h(), s) - Ys;
  if (Ws == 0 || Hs == 0) return;
  cairo_save(cairo_);
  bool need_extend = (cache_w != Ws || cache_h != Hs || (W >= 2 && H >= 2));
  if (need_extend || cx || cy || W < img->w() || H < img->h()) { // clip when necessary
    cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
    cairo_rectangle(cairo_, X - 0.5, Y - 0.5, W, H);
    cairo_clip(cairo_);
    cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT);
  }
  // remove any scaling and the current "0.5" translation useful for lines but bad for images
  matrix.xx = matrix.yy = 1;
  matrix.x0 -= 0.5 * s; matrix.y0 -= 0.5 * s;
  cairo_set_matrix(cairo_, &matrix);
  if (img->d() >= 1) cairo_set_source(cairo_, pat);
  if (need_extend) {
    bool condition = Fl_RGB_Image::scaling_algorithm() == FL_RGB_SCALING_BILINEAR &&
      (fabs(Ws/float(cache_w) - 1) > 0.02 || fabs(Hs/float(cache_h) - 1) > 0.02);
    cairo_pattern_set_filter(pat, condition ? CAIRO_FILTER_GOOD : CAIRO_FILTER_FAST);
    cairo_pattern_set_extend(pat, CAIRO_EXTEND_PAD);
  }
  cairo_matrix_init_scale(&matrix, double(cache_w)/Ws, double(cache_h)/Hs);
  cairo_matrix_translate(&matrix, -Xs , -Ys );
  cairo_pattern_set_matrix(pat, &matrix);
  if (img->d() > 1) cairo_paint(cairo_);
  else cairo_mask(cairo_, pat);
  cairo_restore(cairo_);
  surface_needs_commit();
}


void Fl_Cairo_Graphics_Driver::draw_rgb(Fl_RGB_Image *rgb,int XP, int YP, int WP, int HP, int cx, int cy) {
  int X, Y, W, H;
  // Don't draw an empty image...
  if (!rgb->d() || !rgb->array) {
    Fl_Graphics_Driver::draw_empty(rgb, XP, YP);
    return;
  }
  if (start_image(rgb, XP, YP, WP, HP, cx, cy, X, Y, W, H)) {
    return;
  }
  cairo_pattern_t *pat = (cairo_pattern_t*)*Fl_Graphics_Driver::id(rgb);
  if (!pat) {
    cache(rgb);
    pat = (cairo_pattern_t*)*Fl_Graphics_Driver::id(rgb);
  }
  draw_cached_pattern_(rgb, pat, X, Y, W, H, cx, cy, rgb->cache_w(), rgb->cache_h());
}


static cairo_user_data_key_t data_key_for_surface = {};

static void dealloc_surface_data(void *data) {
  delete[] (uchar*)data;
}


void Fl_Cairo_Graphics_Driver::cache(Fl_RGB_Image *rgb) {
  int stride = cairo_format_stride_for_width(Fl_Cairo_Graphics_Driver::cairo_format,
                                             rgb->data_w()); // 1.6
  uchar *BGRA = new uchar[stride * rgb->data_h()];
  memset(BGRA, 0, stride * rgb->data_h());
  int lrgb = rgb->ld() ? rgb->ld() : rgb->data_w() * rgb->d();
  uchar A = 0xff, R,G,B, *q;
  const uchar *r;
  float f = 1;
  if (rgb->d() >= 3) { // color images
    for (int j = 0; j < rgb->data_h(); j++) {
      r = rgb->array + j * lrgb;
      q = BGRA + j * stride;
      for (int i = 0; i < rgb->data_w(); i++) {
        R = *r;
        G = *(r+1);
        B = *(r+2);
        if (rgb->d() == 4) {
          A = *(r+3);
          f = float(A)/0xff;
        }
        // this produces ARGB data in native endian
        *(uint32_t *)q = A << 24 | (uchar)(R*f) << 16 | (uchar)(G*f) << 8 | (uchar)(B*f);
        r += rgb->d(); q += 4;
      }
    }
  } else if (rgb->d() == 1 || rgb->d() == 2) { // B&W
    for (int j = 0; j < rgb->data_h(); j++) {
      r = rgb->array + j * lrgb;
      q = BGRA + j * stride;
      for (int i = 0; i < rgb->data_w(); i++) {
        G = *r;
        if (rgb->d() == 2) {
          A = *(r+1);
          f = float(A)/0xff;
        }
        G = (uchar)(G * f);
        // this produces ARGB data in native endian
        *(uint32_t *)q = A << 24 | G << 16 | G << 8 | G;
        r += rgb->d(); q += 4;
      }
    }
  }
  cairo_surface_t *surf = cairo_image_surface_create_for_data(BGRA, Fl_Cairo_Graphics_Driver::cairo_format, rgb->data_w(), rgb->data_h(), stride);
  if (cairo_surface_status(surf) != CAIRO_STATUS_SUCCESS) return;
  (void)cairo_surface_set_user_data(surf, &data_key_for_surface, BGRA, dealloc_surface_data);
  cairo_pattern_t *pat = cairo_pattern_create_for_surface(surf);
  cairo_surface_destroy(surf);
  *Fl_Graphics_Driver::id(rgb) = (fl_uintptr_t)pat;
  int *pw, *ph;
  cache_w_h(rgb, pw, ph);
  *pw = rgb->data_w();
  *ph = rgb->data_h();
}


void Fl_Cairo_Graphics_Driver::uncache(Fl_RGB_Image *img, fl_uintptr_t &id_, fl_uintptr_t &mask_) {
  cairo_pattern_t *pat = (cairo_pattern_t*)id_;
  if (pat) {
    cairo_pattern_destroy(pat);
    id_ = 0;
  }
}


void Fl_Cairo_Graphics_Driver::draw_fixed(Fl_Bitmap *bm,int XP, int YP, int WP, int HP,
                                          int cx, int cy) {
  cairo_pattern_t *pat = NULL;
  float s = wld_scale * scale();
  XP = Fl_Scalable_Graphics_Driver::floor(XP, s);
  YP = Fl_Scalable_Graphics_Driver::floor(YP, s);
  cache_size(bm, WP, HP);
  cx *= s; cy *= s;
  cairo_matrix_t matrix; // temporarily remove scaling
  cairo_get_matrix(cairo_, &matrix);
  cairo_translate(cairo_, -0.5, -0.5);
  cairo_scale(cairo_, 1./s, 1/s);
  cairo_translate(cairo_, 0.5, 0.5);
  if (!bm->array) {
    draw_empty(bm, XP, YP);
  } else {
    pat = (cairo_pattern_t*)*Fl_Graphics_Driver::id(bm);
    color(color());
    int old_w = bm->w(), old_h = bm->h();
    bm->scale(bm->cache_w(), bm->cache_h(), 0, 1); // transiently
    draw_cached_pattern_(bm, pat, XP, YP, WP, HP, cx, cy, bm->cache_w(), bm->cache_h());
    bm->scale(old_w, old_h, 0, 1); // back
  }
  cairo_set_matrix(cairo_, &matrix);
}


cairo_pattern_t *Fl_Cairo_Graphics_Driver::bitmap_to_pattern(Fl_Bitmap *bm,
                                    bool complement, cairo_surface_t **p_surface) {
  int stride = cairo_format_stride_for_width(CAIRO_FORMAT_A1, bm->data_w()); // 1.6
  int w_bitmap = ((bm->data_w() + 7) / 8);
  uchar *BGRA = new uchar[stride * bm->data_h()];
  memset(BGRA, 0, stride * bm->data_h());
  for (int j = 0; j < bm->data_h(); j++) {
    uchar *r = (uchar *)bm->array + j * w_bitmap;
    uint32_t *q = (uint32_t *)(BGRA + j * stride);
    int k = 0;
    uint32_t mask32;
    uchar p;
    for (int i = 0; i < bm->data_w(); i++) {
      if (k == 0) {
#if WORDS_BIGENDIAN
        mask32 = (uint32_t)(1 << 31);
#else
        mask32 = 1;
#endif
      }
      if ((k & 7) == 0) { // (k & 7) == (k % 8)
        p = *r++;
        if (complement)
          p = ~p;
      }
      if (p & 1)
        (*q) |= mask32;
      k++;
      p >>= 1;
#if WORDS_BIGENDIAN
      mask32 >>= 1;
#else
      mask32 <<= 1;
#endif
      if (k == 32) {
        k = 0;
        q++;
      }
    }
  }
  cairo_surface_t *surf = cairo_image_surface_create_for_data(BGRA, CAIRO_FORMAT_A1, bm->data_w(), bm->data_h(), stride);
  cairo_pattern_t *pattern = cairo_pattern_create_for_surface(surf);
  if (p_surface) *p_surface = surf;
  else cairo_surface_destroy(surf);
  return pattern;
}


void Fl_Cairo_Graphics_Driver::cache(Fl_Bitmap *bm) {
  cairo_surface_t *surf;
  cairo_pattern_t *pattern = Fl_Cairo_Graphics_Driver::bitmap_to_pattern(bm, false, &surf);
  uchar *BGRA = cairo_image_surface_get_data(surf); // 1.2
  (void)cairo_surface_set_user_data(surf, &data_key_for_surface, BGRA, dealloc_surface_data);
  cairo_surface_destroy(surf);
  *Fl_Graphics_Driver::id(bm) = (fl_uintptr_t)pattern;
  int *pw, *ph;
  cache_w_h(bm, pw, ph);
  *pw = bm->data_w();
  *ph = bm->data_h();
}


void Fl_Cairo_Graphics_Driver::draw_fixed(Fl_Pixmap *pxm,int XP, int YP, int WP, int HP,
                                          int cx, int cy) {
  cairo_pattern_t *pat = NULL;
  float s = wld_scale * scale();
  XP = Fl_Scalable_Graphics_Driver::floor(XP, s);
  YP = Fl_Scalable_Graphics_Driver::floor(YP, s);
  cache_size(pxm, WP, HP);
  cx *= s; cy *= s;
  cairo_matrix_t matrix; // temporarily remove scaling
  cairo_get_matrix(cairo_, &matrix);
  cairo_translate(cairo_, -0.5, -0.5);
  cairo_scale(cairo_, 1/s, 1/s);
  cairo_translate(cairo_, 0.5, 0.5);
  // Don't draw an empty image...
  if (!pxm->data() || !pxm->w()) {
    Fl_Graphics_Driver::draw_empty(pxm, XP, YP);
  } else {
    pat = (cairo_pattern_t*)*Fl_Graphics_Driver::id(pxm);
    int old_w = pxm->w(), old_h = pxm->h();
    pxm->scale(pxm->cache_w(), pxm->cache_h(), 0, 1); // transiently
    draw_cached_pattern_(pxm, pat, XP, YP, WP, HP, cx, cy, pxm->cache_w(), pxm->cache_h());
    pxm->scale(old_w, old_h, 0, 1); // back
  }
  cairo_set_matrix(cairo_, &matrix);
}


void Fl_Cairo_Graphics_Driver::cache(Fl_Pixmap *pxm) {
  Fl_RGB_Image *rgb = new Fl_RGB_Image(pxm);
  cache(rgb);
  *Fl_Graphics_Driver::id(pxm) = *Fl_Graphics_Driver::id(rgb);
  *Fl_Graphics_Driver::id(rgb) = 0;
  delete rgb;
  int *pw, *ph;
  cache_w_h(pxm, pw, ph);
  *pw = pxm->data_w();
  *ph = pxm->data_h();
}


void Fl_Cairo_Graphics_Driver::uncache_pixmap(fl_uintptr_t p) {
  if (p) cairo_pattern_destroy((cairo_pattern_t*)p);
}


void Fl_Cairo_Graphics_Driver::delete_bitmask(fl_uintptr_t bm) {
  if (bm) cairo_pattern_destroy((cairo_pattern_t*)bm);
}


int Fl_Cairo_Graphics_Driver::height() {
  if (!font_descriptor()) font(0, 12);
  return ceil(((Fl_Cairo_Font_Descriptor*)font_descriptor())->line_height / float(PANGO_SCALE));
}


int Fl_Cairo_Graphics_Driver::descent() {
  return font_descriptor()->descent / float(PANGO_SCALE) + 0.5;
}


void Fl_Cairo_Graphics_Driver::init_built_in_fonts() {
  static int i = 0;
  if (!i) {
    while (i < FL_FREE_FONT) {
      i++;
      Fl::set_font((Fl_Font)i-1, fl_fonts[i-1].name);
    }
  }
}


// FIXME: this (static) function should likely be in Fl_Graphics_Driver.
// The code is the same as in src/drivers/Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx

static int font_name_process(const char *name, char &face) {
  int l = strlen(name);
  face = ' ';
  if      (l >  8 && !memcmp(name + l -  8, " Regular", 8)) l -= 8;
  else if (l >  6 && !memcmp(name + l -  6, " Plain", 6)) l -= 6;
  else if (l > 12 && !memcmp(name + l - 12, " Bold Italic", 12)) {l -= 12; face = 'P';}
  else if (l >  7 && !memcmp(name + l -  7, " Italic", 7)) {l -= 7; face = 'I';}
  else if (l >  5 && !memcmp(name + l -  5, " Bold", 5)) {l -= 5; face = 'B';}
  return l;
}

typedef int (*sort_f_type)(const void *aa, const void *bb);


static int font_sort(Fl_Fontdesc *fa, Fl_Fontdesc *fb) {
  char face_a, face_b;
  int la = font_name_process(fa->name, face_a);
  int lb = font_name_process(fb->name, face_b);
  int c = strncasecmp(fa->name, fb->name, la >= lb ? lb : la);
  return (c == 0 ? face_a - face_b : c);
}


Fl_Font Fl_Cairo_Graphics_Driver::set_fonts(const char* /*pattern_name*/)
{
  // Return immideatly if the fonts were already counted
  if (font_count_ != -1)
    return FL_FREE_FONT + font_count_;
  fl_open_display();
  int n_families, count = 0;
  PangoFontFamily **families;
  char *saved_lang = fl_getenv("LANG");
  const char *Clang = "LANG=C";
  if (saved_lang && strcmp(saved_lang, Clang)) {
    // Force LANG=C to prevent pango_font_face_get_face_name() below from returning
    // translated versions of Bold, Italic, etc… (see issue #732).
    // Unfortunately, using setlocale() doesn't do the job.
    char *p = saved_lang;
    saved_lang = (char*)malloc(strlen(p) + 6);
    memcpy(saved_lang, "LANG=", 5);
    strcpy(saved_lang + 5, p);
    fl_putenv(Clang);
  } else saved_lang = NULL;
  static PangoFontMap *pfmap_ = pango_cairo_font_map_get_default(); // 1.10
  Fl_Cairo_Graphics_Driver::init_built_in_fonts();
  pango_font_map_list_families(pfmap_, &families, &n_families);
  for (int fam = 0; fam < n_families; fam++) {
    PangoFontFace **faces;
    int n_faces;
    const char *fam_name = pango_font_family_get_name (families[fam]);
    int lfam = strlen(fam_name);
    pango_font_family_list_faces(families[fam], &faces, &n_faces);
    for (int j = 0; j < n_faces; j++) {
      const char *p = pango_font_face_get_face_name(faces[j]);
      // Remove " Regular" suffix from font names
      if (!strcasecmp(p, "regular")) p = NULL;
      // build the font's FLTK name
      int lfont = lfam + (p ? strlen(p) + 2 : 1);
      char *q = new char[lfont];
      if (p) snprintf(q, lfont, "%s %s", fam_name, p);
      else strcpy(q, fam_name);
      Fl::set_font((Fl_Font)(count++ + FL_FREE_FONT), q);
    }
    /*g_*/free(faces); // glib source code shows that g_free is equivalent to free
  }
  /*g_*/free(families);
  if (saved_lang) {
    fl_putenv(saved_lang);
    free(saved_lang);
  }
  // Sort the list into alphabetic order
  qsort(fl_fonts + FL_FREE_FONT, count, sizeof(Fl_Fontdesc), (sort_f_type)font_sort);
  font_count_ = count;
  return FL_FREE_FONT + count;
}


const char *Fl_Cairo_Graphics_Driver::font_name(int num) {
  return fl_fonts[num].name;
}


void Fl_Cairo_Graphics_Driver::font_name(int num, const char *name) {
  Fl_Fontdesc *s = fl_fonts + num;
  if (s->name) {
    if (!strcmp(s->name, name)) {s->name = name; return;}
    for (Fl_Font_Descriptor* f = s->first; f;) {
      Fl_Font_Descriptor* n = f->next; delete f; f = n;
    }
    s->first = 0;
  }
  s->name = name;
  s->fontname[0] = 0;
  s->first = 0;
}


// turn a stored font name into a pretty name:
#define ENDOFBUFFER  sizeof(fl_fonts->fontname)-1

const char* Fl_Cairo_Graphics_Driver::get_font_name(Fl_Font fnum, int* ap) {
  Fl_Fontdesc *f = fl_fonts + fnum;
  if (!f->fontname[0]) {
    strcpy(f->fontname, f->name); // to check
    const char* thisFont = f->name;
    if (!thisFont || !*thisFont) {if (ap) *ap = 0; return "";}
    int type = 0;
    if (strstr(f->name, "Bold")) type |= FL_BOLD;
    if (strstr(f->name, "Italic") || strstr(f->name, "Oblique")) type |= FL_ITALIC;
    f->fontname[ENDOFBUFFER] = (char)type;
  }
  if (ap) *ap = f->fontname[ENDOFBUFFER];
  return f->fontname;
}


int Fl_Cairo_Graphics_Driver::get_font_sizes(Fl_Font fnum, int*& sizep) {
  static int array[128];
  if (!fl_fonts) fl_fonts = calc_fl_fonts();
  Fl_Fontdesc *s = fl_fonts+fnum;
  if (!s->name) s = fl_fonts; // empty slot in table, use entry 0
  int cnt = 0;

  array[0] = 0;
  sizep = array;
  cnt = 1;

  return cnt;
}


Fl_Cairo_Font_Descriptor::Fl_Cairo_Font_Descriptor(const char* name, Fl_Fontsize size,
                                                   PangoContext *context) :
                                                      Fl_Font_Descriptor(name, size) {
  char *string = new char[strlen(name) + 10];
  strcpy(string, name);
  snprintf(string + strlen(string), 10, " %dpx", size);
  //A PangoFontDescription describes a font in an implementation-independent manner.
  fontref = pango_font_description_from_string(string);
  delete[] string;
  width = NULL;
  //A PangoFontset represents a set of PangoFont to use when rendering text.
  PangoFontset *fontset = pango_font_map_load_fontset(
                                      pango_cairo_font_map_get_default(), // 1.10
                                      context, fontref,
                                      pango_language_get_default() // 1.16
                                                      );
  PangoFontMetrics *metrics = pango_fontset_get_metrics(fontset);
  ascent = pango_font_metrics_get_ascent(metrics);
  descent = pango_font_metrics_get_descent(metrics);
/* Function pango_font_metrics_get_height() giving the line height of a pango font
 appears with pango version 1.44. However, with pango version 1.48.10 and below,
 this function gives values that make the underscore invisible on lowdpi display
 and at 100% scaling (the underscore becomes visible starting at 120% scaling).
 With pango version 1.50.6 (Ubuntu 22.04) this problem disappears.
 Consequently, function pango_font_metrics_get_height() is not used until version 1.50.6
*/
//#if PANGO_VERSION_CHECK(1,44,0)
#if PANGO_VERSION_CHECK(1,50,6)
  line_height = pango_font_metrics_get_height(metrics); // 1.44
#else
  line_height = (pango_font_metrics_get_ascent(metrics) + pango_font_metrics_get_descent(metrics)) * 1.025 + 0.5;
#endif
  pango_font_metrics_unref(metrics);
  g_object_unref(fontset);
//fprintf(stderr, "[%s](%d) ascent=%d descent=%d line_height=%d\n", name, size, ascent, descent, line_height);
}


Fl_Cairo_Font_Descriptor::~Fl_Cairo_Font_Descriptor() {
  pango_font_description_free(fontref);
  if (width) {
    for (int i = 0; i < 64; i++) delete[] width[i];
  }
  delete[] width;
}


static Fl_Font_Descriptor* find(Fl_Font fnum, Fl_Fontsize size, PangoContext *context) {
  Fl_Fontdesc* s = fl_fonts+fnum;
  if (!s->name) s = fl_fonts; // use 0 if fnum undefined
  Fl_Font_Descriptor* f;
  for (f = s->first; f; f = f->next)
    if (f->size == size) return f;
  f = new Fl_Cairo_Font_Descriptor(s->name, size, context);
  f->next = s->first;
  s->first = f;
  return f;
}


/* Implementation note :
 * The pixel width of a drawn string equals the sum of the widths of its
 * characters, except when kerning occurs.
 */
void Fl_Cairo_Graphics_Driver::font(Fl_Font fnum, Fl_Fontsize s) {
  if (!font_descriptor()) fl_open_display();
  if (!cairo_) {
    cairo_surface_t *surf = cairo_image_surface_create(Fl_Cairo_Graphics_Driver::cairo_format, 100, 100);
    cairo_ = cairo_create(surf);
    cairo_surface_destroy(surf);
    dummy_cairo_ = cairo_;
  }
  if (s == 0) return;
  if (fnum == -1) {
    Fl_Graphics_Driver::font(0, 0);
    return;
  }
  Fl_Graphics_Driver::font(fnum, s);
  if (!pango_context_) {
    //A PangoFontMap represents the set of fonts available for a particular rendering system.
    PangoFontMap *def_font_map = pango_cairo_font_map_get_default(); // 1.10
    //A PangoContext stores global information used to control the itemization process.
#if PANGO_VERSION_CHECK(1,22,0)
    pango_context_ = pango_font_map_create_context(def_font_map); // 1.22
#else
    pango_context_ = pango_context_new();
    pango_context_set_font_map(pango_context_, def_font_map);
#endif
    pango_layout_ = pango_layout_new(pango_context_);
  }
  font_descriptor( find(fnum, s, pango_context_) );
  //If no font description is set on the layout, the font description from the layout’s context is used.
  pango_context_set_font_description(pango_context_,
                                  ((Fl_Cairo_Font_Descriptor*)font_descriptor())->fontref);
}


// Scans the input string str with fl_utf8decode() that, by default, accepts
// also non-UTF-8 and processes it as if encoded in CP1252.
// Returns a true UTF-8 string and its length, possibly transformed from CP1252.
// If the input string is true UTF-8, the returned string is the same pointer as input.
// Otherwise, the returned string is in private memory allocated inside clean_utf8()
// and extended when necessary.
const char *Fl_Cairo_Graphics_Driver::clean_utf8(const char* str, int &n) {
  static char *utf8_buffer = NULL;
  static int utf8_buffer_len = 0;
  char *q = utf8_buffer;
  const char *p = str;
  const char *retval = str;
  int len, len2;
  const char *end = str + n;
  char buf4[4];
  while (p < end) {
    unsigned codepoint = fl_utf8decode(p, end, &len);
    if (retval != str || (len == 1 &&  *(uchar*)p >= 0x80)) { // switch to using utf8_buffer
      len2 = fl_utf8encode(codepoint, buf4);
      if (!utf8_buffer_len || utf8_buffer_len < (q - utf8_buffer) + len2) {
        utf8_buffer_len += (q - utf8_buffer) + len2 + 1000;
        utf8_buffer = (char *)realloc(utf8_buffer, utf8_buffer_len);
      }
      if (retval == str) {
        retval = utf8_buffer;
        q = utf8_buffer;
        if (p > str) { memcpy(q, str, p - str); q += (p - str); }
      }
      memcpy(q, buf4, len2);
      q += len2;
    }
    p += len;
  }
  if (retval != str) n = q - retval;
  return retval;
}


void Fl_Cairo_Graphics_Driver::draw(const char* str, int n, float x, float y) {
  if (!n) return;
  cairo_save(cairo_);
  Fl_Cairo_Font_Descriptor *fd = (Fl_Cairo_Font_Descriptor*)font_descriptor();
  cairo_translate(cairo_, x - 1, y - (fd->line_height - fd->descent) / float(PANGO_SCALE) - 1);
  str = clean_utf8(str, n);
  pango_layout_set_text(pango_layout_, str, n);
  pango_cairo_show_layout(cairo_, pango_layout_); // 1.1O
  cairo_restore(cairo_);
  surface_needs_commit();
}


void Fl_Cairo_Graphics_Driver::draw(int rotation, const char *str, int n, int x, int y)
{
  cairo_save(cairo_);
  cairo_translate(cairo_, x, y);
  cairo_rotate(cairo_, -rotation * M_PI / 180);
  this->draw(str, n, 0, 0);
  cairo_restore(cairo_);
}


void Fl_Cairo_Graphics_Driver::rtl_draw(const char* str, int n, int x, int y) {
  int w = (int)width(str, n);
  draw(str, n, x - w, y);
}


// cache the widths of single Unicode characters
double Fl_Cairo_Graphics_Driver::width(unsigned int utf32) {
  unsigned r=0;
  Fl_Cairo_Font_Descriptor *desc = NULL;
  if (utf32 <= 0xFFFF) {
    desc = (Fl_Cairo_Font_Descriptor*)font_descriptor();
    r = (utf32 & 0xFC00) >> 10;
    if (!desc->width) {
      desc->width = (int**)new int*[64];
      memset(desc->width, 0, 64*sizeof(int*));
    }
    if (!desc->width[r]) {
      desc->width[r] = (int*)new int[0x0400];
      for (int i = 0; i < 0x0400; i++) desc->width[r][i] = -1;
    } else {
      if ( desc->width[r][utf32&0x03FF] >= 0 ) { // already cached
        return desc->width[r][utf32 & 0x03FF] / double(PANGO_SCALE);
      }
    }
  }
  char buf4[4];
  int n = fl_utf8encode(utf32, buf4);
  int width = do_width_unscaled_(buf4, n);
  if (utf32 <= 0xFFFF) {
    desc->width[r][utf32 & 0x03FF] = width;
  }
  return width / double(PANGO_SCALE);
}


double Fl_Cairo_Graphics_Driver::width(const char* str, int n) {
  if (!font_descriptor()) return -1;
  if ((str == NULL) || (n == 0)) return 0.;
  if (n == fl_utf8len(*str)) { // str contains a single unicode character
    int l;
    unsigned c = fl_utf8decode(str, str+n, &l);
    return width(c); // that character's width may have been cached
  }
  return do_width_unscaled_(str, n) / double(PANGO_SCALE); // full width computation for multi-char strings
}


int Fl_Cairo_Graphics_Driver::do_width_unscaled_(const char* str, int n) {
  if (!n) return 0;
  str = clean_utf8(str, n);
  pango_layout_set_text(pango_layout_, str, n);
  PangoRectangle p_rect;
  pango_layout_get_extents(pango_layout_, NULL, &p_rect);
  return p_rect.width;
}


void Fl_Cairo_Graphics_Driver::text_extents(const char* txt, int n, int& dx, int& dy, int& w, int& h) {
  txt = clean_utf8(txt, n);
  pango_layout_set_text(pango_layout_, txt, n);
  PangoRectangle ink_rect;
  pango_layout_get_extents(pango_layout_, &ink_rect, NULL);
  double f = PANGO_SCALE;
  Fl_Cairo_Font_Descriptor *fd = (Fl_Cairo_Font_Descriptor*)font_descriptor();
  dx = ink_rect.x / f - 1;
  dy = (ink_rect.y - fd->line_height + fd->descent) / f - 1;
  w = ceil(ink_rect.width / f);
  h = ceil(ink_rect.height / f);
}

//
// Region-handling member functions.
// They are used ONLY if the cairo graphics driver is the display graphics driver.
// They are not used if the cairo graphics driver is used to draw PostScript.
// Type cairo_region_t and associated functions require cairo ≥ 1.10

Fl_Region Fl_Cairo_Graphics_Driver::XRectangleRegion(int x, int y, int w, int h) {
  cairo_rectangle_int_t rect = {x, y, w, h};
  return cairo_region_create_rectangle(&rect); // 1.10
}


void Fl_Cairo_Graphics_Driver::add_rectangle_to_region(Fl_Region r_, int X, int Y, int W, int H) {
  cairo_rectangle_int_t rect = {X, Y, W, H};
  cairo_region_union_rectangle((cairo_region_t*)r_, &rect); // 1.10
}


void Fl_Cairo_Graphics_Driver::XDestroyRegion(Fl_Region r_) {
  cairo_region_destroy((cairo_region_t*)r_); // 1.10
}


void Fl_Cairo_Graphics_Driver::restore_clip() {
  if (cairo_) {
    cairo_reset_clip(cairo_);
    // apply what's in rstack
    cairo_region_t *r = (cairo_region_t*)rstack[rstackptr];
    if (r) {
      if (!clip_) {
        clip_ = new Clip();
        clip_->prev = NULL;
      }
      int count = cairo_region_num_rectangles(r); // 1.10
      cairo_rectangle_int_t rect;
      for (int i = 0; i < count; i++) {
        cairo_region_get_rectangle(r, i, &rect); // 1.10
        cairo_rectangle(cairo_, rect.x - 0.5, rect.y - 0.5, rect.width, rect.height);
      }
      // put in clip_ the bounding rect of region r
      cairo_region_get_extents(r, &rect); // 1.10
      clip_->x = rect.x;
      clip_->y = rect.y;
      clip_->w = rect.width;
      clip_->h = rect.height;
      cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_NONE);
      cairo_clip(cairo_);
      cairo_set_antialias(cairo_, CAIRO_ANTIALIAS_DEFAULT );
    } else if (clip_) {
      clip_->w = -1;
    }
  }
}


char Fl_Cairo_Graphics_Driver::can_do_alpha_blending() {
  return 1;
}


float Fl_Cairo_Graphics_Driver::_scale() {
  float s = scale();
  if (s != 1.f && Fl_Display_Device::display_device()->is_current()) {
    cairo_scale(cairo_, 1./s, 1./s);
    Fl_Graphics_Driver::scale(1);
  }
  return s;
}


void Fl_Cairo_Graphics_Driver::restore_scale(float s) {
  if (s != 1.f && Fl_Display_Device::display_device()->is_current()) {
    cairo_scale(cairo_, s, s);
    Fl_Graphics_Driver::scale(s);
  }
}


void Fl_Cairo_Graphics_Driver::antialias(int state) {
  cairo_set_antialias(cairo_, state ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
}


int Fl_Cairo_Graphics_Driver::antialias() {
  return (cairo_get_antialias(cairo_) != CAIRO_ANTIALIAS_NONE);
}


void Fl_Cairo_Graphics_Driver::focus_rect(int x, int y, int w, int h)
{
  cairo_save(cairo_);
  cairo_set_line_width(cairo_, 1);
  cairo_set_line_cap(cairo_, CAIRO_LINE_CAP_BUTT);
  cairo_set_line_join(cairo_, CAIRO_LINE_JOIN_MITER);
  static double dots[2] = {1., 1.};
  cairo_set_dash(cairo_, dots, 2, 1);
  cairo_set_antialias(cairo_,  CAIRO_ANTIALIAS_NONE);
  cairo_rectangle(cairo_, x, y, w-1, h-1);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
  surface_needs_commit();
}


cairo_pattern_t *Fl_Cairo_Graphics_Driver::calc_cairo_mask(const Fl_RGB_Image *rgb) {
  int i, j, d = rgb->d(), w = rgb->data_w(), h = rgb->data_h(), ld = rgb->ld();
  int bytesperrow = cairo_format_stride_for_width(CAIRO_FORMAT_A1, w); // 1.6
  if (!ld) ld = d * w;
  unsigned u;
  uchar byte, onebit;
  // build a CAIRO_FORMAT_A1 surface covering the non-black part of the image
  uchar* bits = new uchar[h*bytesperrow]; // to store the surface data
  for (i = 0; i < h; i++) {
    const uchar* alpha = (const uchar*)*rgb->data() + i * ld;
    uchar *p = (uchar*)bits + i * bytesperrow;
    byte = 0;
    onebit = 1;
    for (j = 0; j < w; j++) {
      u = *alpha;
      u += *(alpha+1);
      u += *(alpha+2);
      if (u > 0) { // if the pixel is not black
        byte |= onebit; // turn on the corresponding bit of the bitmap
      }
      onebit = onebit << 1; // move the single set bit one position to the left
      if (onebit == 0 || j == w-1) {
        onebit = 1;
        *p++ = byte; // store in bitmap one pack of bits
        byte = 0;
      }
      alpha += d; // point to next rgb pixel
    }
  }
  cairo_surface_t *mask_surf = cairo_image_surface_create_for_data(bits,
    CAIRO_FORMAT_A1, w, h, bytesperrow);
  cairo_pattern_t *mask_pattern = cairo_pattern_create_for_surface(mask_surf);
  cairo_surface_destroy(mask_surf);
  return mask_pattern;
}

#endif // USE_PANGO