/
PlatGTK.cxx
2160 lines (1929 loc) · 64.3 KB
/
PlatGTK.cxx
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
// Scintilla source code edit control
// PlatGTK.cxx - implementation of platform facilities on GTK+/Linux
// Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <glib.h>
#include <gmodule.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "Platform.h"
#include "Scintilla.h"
#include "ScintillaWidget.h"
#include "StringCopy.h"
#include "XPM.h"
#include "UniConversion.h"
#include "Converter.h"
static const double kPi = 3.14159265358979323846;
// The Pango version guard for pango_units_from_double and pango_units_to_double
// is more complex than simply implementing these here.
static int pangoUnitsFromDouble(double d) {
return static_cast<int>(d * PANGO_SCALE + 0.5);
}
static double doubleFromPangoUnits(int pu) {
return static_cast<double>(pu) / PANGO_SCALE;
}
static cairo_surface_t *CreateSimilarSurface(GdkWindow *window, cairo_content_t content, int width, int height) {
#if GTK_CHECK_VERSION(2,22,0)
return gdk_window_create_similar_surface(window, content, width, height);
#else
cairo_surface_t *window_surface, *surface;
g_return_val_if_fail(GDK_IS_WINDOW(window), NULL);
window_surface = GDK_DRAWABLE_GET_CLASS(window)->ref_cairo_surface(window);
surface = cairo_surface_create_similar(window_surface, content, width, height);
cairo_surface_destroy(window_surface);
return surface;
#endif
}
static GdkWindow *WindowFromWidget(GtkWidget *w) {
return gtk_widget_get_window(w);
}
#ifdef _MSC_VER
// Ignore unreferenced local functions in GTK+ headers
#pragma warning(disable: 4505)
#endif
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
enum encodingType { singleByte, UTF8, dbcs};
// Holds a PangoFontDescription*.
class FontHandle {
public:
PangoFontDescription *pfd;
int characterSet;
FontHandle() : pfd(0), characterSet(-1) {
}
FontHandle(PangoFontDescription *pfd_, int characterSet_) {
pfd = pfd_;
characterSet = characterSet_;
}
~FontHandle() {
if (pfd)
pango_font_description_free(pfd);
pfd = 0;
}
static FontHandle *CreateNewFont(const FontParameters &fp);
};
FontHandle *FontHandle::CreateNewFont(const FontParameters &fp) {
PangoFontDescription *pfd = pango_font_description_new();
if (pfd) {
pango_font_description_set_family(pfd,
(fp.faceName[0] == '!') ? fp.faceName+1 : fp.faceName);
pango_font_description_set_size(pfd, pangoUnitsFromDouble(fp.size));
pango_font_description_set_weight(pfd, static_cast<PangoWeight>(fp.weight));
pango_font_description_set_style(pfd, fp.italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
return new FontHandle(pfd,fp.characterSet);
}
return NULL;
}
// X has a 16 bit coordinate space, so stop drawing here to avoid wrapping
static const int maxCoordinate = 32000;
static FontHandle *PFont(Font &f) {
return static_cast<FontHandle *>(f.GetID());
}
static GtkWidget *PWidget(WindowID wid) {
return static_cast<GtkWidget *>(wid);
}
Point Point::FromLong(long lpoint) {
return Point(
Platform::LowShortFromLong(lpoint),
Platform::HighShortFromLong(lpoint));
}
Font::Font() : fid(0) {}
Font::~Font() {}
void Font::Create(const FontParameters &fp) {
Release();
fid = FontHandle::CreateNewFont(fp);
}
void Font::Release() {
if (fid)
delete static_cast<FontHandle *>(fid);
fid = 0;
}
// Required on OS X
#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif
// SurfaceID is a cairo_t*
class SurfaceImpl : public Surface {
encodingType et;
cairo_t *context;
cairo_surface_t *psurf;
int x;
int y;
bool inited;
bool createdGC;
PangoContext *pcontext;
PangoLayout *layout;
Converter conv;
int characterSet;
void SetConverter(int characterSet_);
public:
SurfaceImpl();
virtual ~SurfaceImpl();
void Init(WindowID wid);
void Init(SurfaceID sid, WindowID wid);
void InitPixMap(int width, int height, Surface *surface_, WindowID wid);
void Release();
bool Initialised();
void PenColour(ColourDesired fore);
int LogPixelsY();
int DeviceHeightFont(int points);
void MoveTo(int x_, int y_);
void LineTo(int x_, int y_);
void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back);
void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back);
void FillRectangle(PRectangle rc, ColourDesired back);
void FillRectangle(PRectangle rc, Surface &surfacePattern);
void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back);
void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int flags);
void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage);
void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back);
void Copy(PRectangle rc, Point from, Surface &surfaceSource);
void DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore);
void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore);
void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions);
XYPOSITION WidthText(Font &font_, const char *s, int len);
XYPOSITION WidthChar(Font &font_, char ch);
XYPOSITION Ascent(Font &font_);
XYPOSITION Descent(Font &font_);
XYPOSITION InternalLeading(Font &font_);
XYPOSITION ExternalLeading(Font &font_);
XYPOSITION Height(Font &font_);
XYPOSITION AverageCharWidth(Font &font_);
void SetClip(PRectangle rc);
void FlushCachedState();
void SetUnicodeMode(bool unicodeMode_);
void SetDBCSMode(int codePage);
};
#ifdef SCI_NAMESPACE
}
#endif
const char *CharacterSetID(int characterSet) {
switch (characterSet) {
case SC_CHARSET_ANSI:
return "";
case SC_CHARSET_DEFAULT:
return "ISO-8859-1";
case SC_CHARSET_BALTIC:
return "ISO-8859-13";
case SC_CHARSET_CHINESEBIG5:
return "BIG-5";
case SC_CHARSET_EASTEUROPE:
return "ISO-8859-2";
case SC_CHARSET_GB2312:
return "CP936";
case SC_CHARSET_GREEK:
return "ISO-8859-7";
case SC_CHARSET_HANGUL:
return "CP949";
case SC_CHARSET_MAC:
return "MACINTOSH";
case SC_CHARSET_OEM:
return "ASCII";
case SC_CHARSET_RUSSIAN:
return "KOI8-R";
case SC_CHARSET_OEM866:
return "CP866";
case SC_CHARSET_CYRILLIC:
return "CP1251";
case SC_CHARSET_SHIFTJIS:
return "SHIFT-JIS";
case SC_CHARSET_SYMBOL:
return "";
case SC_CHARSET_TURKISH:
return "ISO-8859-9";
case SC_CHARSET_JOHAB:
return "CP1361";
case SC_CHARSET_HEBREW:
return "ISO-8859-8";
case SC_CHARSET_ARABIC:
return "ISO-8859-6";
case SC_CHARSET_VIETNAMESE:
return "";
case SC_CHARSET_THAI:
return "ISO-8859-11";
case SC_CHARSET_8859_15:
return "ISO-8859-15";
default:
return "";
}
}
void SurfaceImpl::SetConverter(int characterSet_) {
if (characterSet != characterSet_) {
characterSet = characterSet_;
conv.Open("UTF-8", CharacterSetID(characterSet), false);
}
}
SurfaceImpl::SurfaceImpl() : et(singleByte),
context(0),
psurf(0),
x(0), y(0), inited(false), createdGC(false)
, pcontext(0), layout(0), characterSet(-1) {
}
SurfaceImpl::~SurfaceImpl() {
Release();
}
void SurfaceImpl::Release() {
et = singleByte;
if (createdGC) {
createdGC = false;
cairo_destroy(context);
}
context = 0;
if (psurf)
cairo_surface_destroy(psurf);
psurf = 0;
if (layout)
g_object_unref(layout);
layout = 0;
if (pcontext)
g_object_unref(pcontext);
pcontext = 0;
conv.Close();
characterSet = -1;
x = 0;
y = 0;
inited = false;
createdGC = false;
}
bool SurfaceImpl::Initialised() {
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 8, 0)
if (inited && context) {
if (cairo_status(context) == CAIRO_STATUS_SUCCESS) {
// Even when status is success, the target surface may have been
// finished whch may cause an assertion to fail crashing the application.
// The cairo_surface_has_show_text_glyphs call checks the finished flag
// and when set, sets the status to CAIRO_STATUS_SURFACE_FINISHED
// which leads to warning messages instead of crashes.
// Performing the check in this method as it is called rarely and has no
// other side effects.
cairo_surface_t *psurfContext = cairo_get_target(context);
if (psurfContext) {
cairo_surface_has_show_text_glyphs(psurfContext);
}
}
return cairo_status(context) == CAIRO_STATUS_SUCCESS;
}
#endif
return inited;
}
void SurfaceImpl::Init(WindowID wid) {
Release();
PLATFORM_ASSERT(wid);
// if we are only created from a window ID, we can't perform drawing
psurf = 0;
context = 0;
createdGC = false;
pcontext = gtk_widget_create_pango_context(PWidget(wid));
PLATFORM_ASSERT(pcontext);
layout = pango_layout_new(pcontext);
PLATFORM_ASSERT(layout);
inited = true;
}
void SurfaceImpl::Init(SurfaceID sid, WindowID wid) {
PLATFORM_ASSERT(sid);
Release();
PLATFORM_ASSERT(wid);
context = cairo_reference(static_cast<cairo_t *>(sid));
pcontext = gtk_widget_create_pango_context(PWidget(wid));
// update the Pango context in case sid isn't the widget's surface
pango_cairo_update_context(context, pcontext);
layout = pango_layout_new(pcontext);
cairo_set_line_width(context, 1);
createdGC = true;
inited = true;
}
void SurfaceImpl::InitPixMap(int width, int height, Surface *surface_, WindowID wid) {
PLATFORM_ASSERT(surface_);
Release();
SurfaceImpl *surfImpl = static_cast<SurfaceImpl *>(surface_);
PLATFORM_ASSERT(wid);
context = cairo_reference(surfImpl->context);
pcontext = gtk_widget_create_pango_context(PWidget(wid));
// update the Pango context in case surface_ isn't the widget's surface
pango_cairo_update_context(context, pcontext);
PLATFORM_ASSERT(pcontext);
layout = pango_layout_new(pcontext);
PLATFORM_ASSERT(layout);
if (height > 0 && width > 0)
psurf = CreateSimilarSurface(
WindowFromWidget(PWidget(wid)),
CAIRO_CONTENT_COLOR_ALPHA, width, height);
cairo_destroy(context);
context = cairo_create(psurf);
cairo_rectangle(context, 0, 0, width, height);
cairo_set_source_rgb(context, 1.0, 0, 0);
cairo_fill(context);
// This produces sharp drawing more similar to GDK:
//cairo_set_antialias(context, CAIRO_ANTIALIAS_NONE);
cairo_set_line_width(context, 1);
createdGC = true;
inited = true;
et = surfImpl->et;
}
void SurfaceImpl::PenColour(ColourDesired fore) {
if (context) {
ColourDesired cdFore(fore.AsLong());
cairo_set_source_rgb(context,
cdFore.GetRed() / 255.0,
cdFore.GetGreen() / 255.0,
cdFore.GetBlue() / 255.0);
}
}
int SurfaceImpl::LogPixelsY() {
return 72;
}
int SurfaceImpl::DeviceHeightFont(int points) {
int logPix = LogPixelsY();
return (points * logPix + logPix / 2) / 72;
}
void SurfaceImpl::MoveTo(int x_, int y_) {
x = x_;
y = y_;
}
static int Delta(int difference) {
if (difference < 0)
return -1;
else if (difference > 0)
return 1;
else
return 0;
}
void SurfaceImpl::LineTo(int x_, int y_) {
// cairo_line_to draws the end position, unlike Win32 or GDK with GDK_CAP_NOT_LAST.
// For simple cases, move back one pixel from end.
if (context) {
int xDiff = x_ - x;
int xDelta = Delta(xDiff);
int yDiff = y_ - y;
int yDelta = Delta(yDiff);
if ((xDiff == 0) || (yDiff == 0)) {
// Horizontal or vertical lines can be more precisely drawn as a filled rectangle
int xEnd = x_ - xDelta;
int left = Platform::Minimum(x, xEnd);
int width = abs(x - xEnd) + 1;
int yEnd = y_ - yDelta;
int top = Platform::Minimum(y, yEnd);
int height = abs(y - yEnd) + 1;
cairo_rectangle(context, left, top, width, height);
cairo_fill(context);
} else if ((abs(xDiff) == abs(yDiff))) {
// 45 degree slope
cairo_move_to(context, x + 0.5, y + 0.5);
cairo_line_to(context, x_ + 0.5 - xDelta, y_ + 0.5 - yDelta);
} else {
// Line has a different slope so difficult to avoid last pixel
cairo_move_to(context, x + 0.5, y + 0.5);
cairo_line_to(context, x_ + 0.5, y_ + 0.5);
}
cairo_stroke(context);
}
x = x_;
y = y_;
}
void SurfaceImpl::Polygon(Point *pts, int npts, ColourDesired fore,
ColourDesired back) {
PLATFORM_ASSERT(context);
PenColour(back);
cairo_move_to(context, pts[0].x + 0.5, pts[0].y + 0.5);
for (int i = 1; i < npts; i++) {
cairo_line_to(context, pts[i].x + 0.5, pts[i].y + 0.5);
}
cairo_close_path(context);
cairo_fill_preserve(context);
PenColour(fore);
cairo_stroke(context);
}
void SurfaceImpl::RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) {
if (context) {
cairo_rectangle(context, rc.left + 0.5, rc.top + 0.5,
rc.right - rc.left - 1, rc.bottom - rc.top - 1);
PenColour(back);
cairo_fill_preserve(context);
PenColour(fore);
cairo_stroke(context);
}
}
void SurfaceImpl::FillRectangle(PRectangle rc, ColourDesired back) {
PenColour(back);
if (context && (rc.left < maxCoordinate)) { // Protect against out of range
rc.left = lround(rc.left);
rc.right = lround(rc.right);
cairo_rectangle(context, rc.left, rc.top,
rc.right - rc.left, rc.bottom - rc.top);
cairo_fill(context);
}
}
void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfacePattern);
bool canDraw = surfi.psurf != NULL;
if (canDraw) {
PLATFORM_ASSERT(context);
// Tile pattern over rectangle
// Currently assumes 8x8 pattern
int widthPat = 8;
int heightPat = 8;
for (int xTile = rc.left; xTile < rc.right; xTile += widthPat) {
int widthx = (xTile + widthPat > rc.right) ? rc.right - xTile : widthPat;
for (int yTile = rc.top; yTile < rc.bottom; yTile += heightPat) {
int heighty = (yTile + heightPat > rc.bottom) ? rc.bottom - yTile : heightPat;
cairo_set_source_surface(context, surfi.psurf, xTile, yTile);
cairo_rectangle(context, xTile, yTile, widthx, heighty);
cairo_fill(context);
}
}
} else {
// Something is wrong so try to show anyway
// Shows up black because colour not allocated
FillRectangle(rc, ColourDesired(0));
}
}
void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) {
if (((rc.right - rc.left) > 4) && ((rc.bottom - rc.top) > 4)) {
// Approximate a round rect with some cut off corners
Point pts[] = {
Point(rc.left + 2, rc.top),
Point(rc.right - 2, rc.top),
Point(rc.right, rc.top + 2),
Point(rc.right, rc.bottom - 2),
Point(rc.right - 2, rc.bottom),
Point(rc.left + 2, rc.bottom),
Point(rc.left, rc.bottom - 2),
Point(rc.left, rc.top + 2),
};
Polygon(pts, ELEMENTS(pts), fore, back);
} else {
RectangleDraw(rc, fore, back);
}
}
static void PathRoundRectangle(cairo_t *context, double left, double top, double width, double height, int radius) {
double degrees = kPi / 180.0;
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 2, 0)
cairo_new_sub_path(context);
#else
// First arc is in the top-right corner and starts from a point on the top line
cairo_move_to(context, left + width - radius, top);
#endif
cairo_arc(context, left + width - radius, top + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc(context, left + width - radius, top + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc(context, left + radius, top + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc(context, left + radius, top + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path(context);
}
void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
ColourDesired outline, int alphaOutline, int /*flags*/) {
if (context && rc.Width() > 0) {
ColourDesired cdFill(fill.AsLong());
cairo_set_source_rgba(context,
cdFill.GetRed() / 255.0,
cdFill.GetGreen() / 255.0,
cdFill.GetBlue() / 255.0,
alphaFill / 255.0);
if (cornerSize > 0)
PathRoundRectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0, cornerSize);
else
cairo_rectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0);
cairo_fill(context);
ColourDesired cdOutline(outline.AsLong());
cairo_set_source_rgba(context,
cdOutline.GetRed() / 255.0,
cdOutline.GetGreen() / 255.0,
cdOutline.GetBlue() / 255.0,
alphaOutline / 255.0);
if (cornerSize > 0)
PathRoundRectangle(context, rc.left + 0.5, rc.top + 0.5, rc.right - rc.left - 1, rc.bottom - rc.top - 1, cornerSize);
else
cairo_rectangle(context, rc.left + 0.5, rc.top + 0.5, rc.right - rc.left - 1, rc.bottom - rc.top - 1);
cairo_stroke(context);
}
}
void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) {
PLATFORM_ASSERT(context);
if (rc.Width() > width)
rc.left += (rc.Width() - width) / 2;
rc.right = rc.left + width;
if (rc.Height() > height)
rc.top += (rc.Height() - height) / 2;
rc.bottom = rc.top + height;
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
#else
int stride = width * 4;
#endif
int ucs = stride * height;
std::vector<unsigned char> image(ucs);
for (int iy=0; iy<height; iy++) {
for (int ix=0; ix<width; ix++) {
unsigned char *pixel = &image[0] + iy*stride + ix * 4;
unsigned char alpha = pixelsImage[3];
pixel[2] = (*pixelsImage++) * alpha / 255;
pixel[1] = (*pixelsImage++) * alpha / 255;
pixel[0] = (*pixelsImage++) * alpha / 255;
pixel[3] = *pixelsImage++;
}
}
cairo_surface_t *psurfImage = cairo_image_surface_create_for_data(&image[0], CAIRO_FORMAT_ARGB32, width, height, stride);
cairo_set_source_surface(context, psurfImage, rc.left, rc.top);
cairo_rectangle(context, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
cairo_fill(context);
cairo_surface_destroy(psurfImage);
}
void SurfaceImpl::Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) {
PLATFORM_ASSERT(context);
PenColour(back);
cairo_arc(context, (rc.left + rc.right) / 2, (rc.top + rc.bottom) / 2,
Platform::Minimum(rc.Width(), rc.Height()) / 2, 0, 2*kPi);
cairo_fill_preserve(context);
PenColour(fore);
cairo_stroke(context);
}
void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfaceSource);
bool canDraw = surfi.psurf != NULL;
if (canDraw) {
PLATFORM_ASSERT(context);
cairo_set_source_surface(context, surfi.psurf,
rc.left - from.x, rc.top - from.y);
cairo_rectangle(context, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
cairo_fill(context);
}
}
std::string UTF8FromLatin1(const char *s, int len) {
std::string utfForm(len*2 + 1, '\0');
size_t lenU = 0;
for (int i=0; i<len; i++) {
unsigned int uch = static_cast<unsigned char>(s[i]);
if (uch < 0x80) {
utfForm[lenU++] = uch;
} else {
utfForm[lenU++] = static_cast<char>(0xC0 | (uch >> 6));
utfForm[lenU++] = static_cast<char>(0x80 | (uch & 0x3f));
}
}
utfForm.resize(lenU);
return utfForm;
}
static std::string UTF8FromIconv(const Converter &conv, const char *s, int len) {
if (conv) {
std::string utfForm(len*3+1, '\0');
char *pin = const_cast<char *>(s);
gsize inLeft = len;
char *putf = &utfForm[0];
char *pout = putf;
gsize outLeft = len*3+1;
gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
if (conversions != sizeFailure) {
*pout = '\0';
utfForm.resize(pout - putf);
return utfForm;
}
}
return std::string();
}
// Work out how many bytes are in a character by trying to convert using iconv,
// returning the first length that succeeds.
static size_t MultiByteLenFromIconv(const Converter &conv, const char *s, size_t len) {
for (size_t lenMB=1; (lenMB<4) && (lenMB <= len); lenMB++) {
char wcForm[2];
char *pin = const_cast<char *>(s);
gsize inLeft = lenMB;
char *pout = wcForm;
gsize outLeft = 2;
gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
if (conversions != sizeFailure) {
return lenMB;
}
}
return 1;
}
void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore) {
PenColour(fore);
if (context) {
XYPOSITION xText = rc.left;
if (PFont(font_)->pfd) {
std::string utfForm;
if (et == UTF8) {
pango_layout_set_text(layout, s, len);
} else {
SetConverter(PFont(font_)->characterSet);
utfForm = UTF8FromIconv(conv, s, len);
if (utfForm.empty()) { // iconv failed so treat as Latin1
utfForm = UTF8FromLatin1(s, len);
}
pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
}
pango_layout_set_font_description(layout, PFont(font_)->pfd);
pango_cairo_update_layout(context, layout);
#ifdef PANGO_VERSION
PangoLayoutLine *pll = pango_layout_get_line_readonly(layout,0);
#else
PangoLayoutLine *pll = pango_layout_get_line(layout,0);
#endif
cairo_move_to(context, xText, ybase);
pango_cairo_show_layout_line(context, pll);
}
}
}
void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore, ColourDesired back) {
FillRectangle(rc, back);
DrawTextBase(rc, font_, ybase, s, len, fore);
}
// On GTK+, exactly same as DrawTextNoClip
void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore, ColourDesired back) {
FillRectangle(rc, back);
DrawTextBase(rc, font_, ybase, s, len, fore);
}
void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
ColourDesired fore) {
// Avoid drawing spaces in transparent mode
for (int i=0; i<len; i++) {
if (s[i] != ' ') {
DrawTextBase(rc, font_, ybase, s, len, fore);
return;
}
}
}
class ClusterIterator {
PangoLayoutIter *iter;
PangoRectangle pos;
int lenPositions;
public:
bool finished;
XYPOSITION positionStart;
XYPOSITION position;
XYPOSITION distance;
int curIndex;
ClusterIterator(PangoLayout *layout, int len) : lenPositions(len), finished(false),
positionStart(0), position(0), distance(0), curIndex(0) {
iter = pango_layout_get_iter(layout);
pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
}
~ClusterIterator() {
pango_layout_iter_free(iter);
}
void Next() {
positionStart = position;
if (pango_layout_iter_next_cluster(iter)) {
pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
position = doubleFromPangoUnits(pos.x);
curIndex = pango_layout_iter_get_index(iter);
} else {
finished = true;
position = doubleFromPangoUnits(pos.x + pos.width);
curIndex = lenPositions;
}
distance = position - positionStart;
}
};
void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) {
if (font_.GetID()) {
const int lenPositions = len;
if (PFont(font_)->pfd) {
pango_layout_set_font_description(layout, PFont(font_)->pfd);
if (et == UTF8) {
// Simple and direct as UTF-8 is native Pango encoding
int i = 0;
pango_layout_set_text(layout, s, len);
ClusterIterator iti(layout, lenPositions);
while (!iti.finished) {
iti.Next();
int places = iti.curIndex - i;
while (i < iti.curIndex) {
// Evenly distribute space among bytes of this cluster.
// Would be better to find number of characters and then
// divide evenly between characters with each byte of a character
// being at the same position.
positions[i] = iti.position - (iti.curIndex - 1 - i) * iti.distance / places;
i++;
}
}
PLATFORM_ASSERT(i == lenPositions);
} else {
int positionsCalculated = 0;
if (et == dbcs) {
SetConverter(PFont(font_)->characterSet);
std::string utfForm = UTF8FromIconv(conv, s, len);
if (!utfForm.empty()) {
// Convert to UTF-8 so can ask Pango for widths, then
// Loop through UTF-8 and DBCS forms, taking account of different
// character byte lengths.
Converter convMeasure("UCS-2", CharacterSetID(characterSet), false);
pango_layout_set_text(layout, utfForm.c_str(), strlen(utfForm.c_str()));
int i = 0;
int clusterStart = 0;
ClusterIterator iti(layout, strlen(utfForm.c_str()));
while (!iti.finished) {
iti.Next();
int clusterEnd = iti.curIndex;
int places = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
int place = 1;
while (clusterStart < clusterEnd) {
size_t lenChar = MultiByteLenFromIconv(convMeasure, s+i, len-i);
while (lenChar--) {
positions[i++] = iti.position - (places - place) * iti.distance / places;
positionsCalculated++;
}
clusterStart += UTF8CharLength(static_cast<unsigned char>(utfForm.c_str()[clusterStart]));
place++;
}
}
PLATFORM_ASSERT(i == lenPositions);
}
}
if (positionsCalculated < 1 ) {
// Either 8-bit or DBCS conversion failed so treat as 8-bit.
SetConverter(PFont(font_)->characterSet);
const bool rtlCheck = PFont(font_)->characterSet == SC_CHARSET_HEBREW ||
PFont(font_)->characterSet == SC_CHARSET_ARABIC;
std::string utfForm = UTF8FromIconv(conv, s, len);
if (utfForm.empty()) {
utfForm = UTF8FromLatin1(s, len);
}
pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
int i = 0;
int clusterStart = 0;
// Each 8-bit input character may take 1 or 2 bytes in UTF-8
// and groups of up to 3 may be represented as ligatures.
ClusterIterator iti(layout, utfForm.length());
while (!iti.finished) {
iti.Next();
int clusterEnd = iti.curIndex;
int ligatureLength = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
if (rtlCheck && ((clusterEnd <= clusterStart) || (ligatureLength == 0) || (ligatureLength > 3))) {
// Something has gone wrong: exit quickly but pretend all the characters are equally spaced:
int widthLayout = 0;
pango_layout_get_size(layout, &widthLayout, NULL);
XYPOSITION widthTotal = doubleFromPangoUnits(widthLayout);
for (int bytePos=0; bytePos<lenPositions; bytePos++) {
positions[bytePos] = widthTotal / lenPositions * (bytePos + 1);
}
return;
}
PLATFORM_ASSERT(ligatureLength > 0 && ligatureLength <= 3);
for (int charInLig=0; charInLig<ligatureLength; charInLig++) {
positions[i++] = iti.position - (ligatureLength - 1 - charInLig) * iti.distance / ligatureLength;
}
clusterStart = clusterEnd;
}
while (i < lenPositions) {
// If something failed, fill in rest of the positions
positions[i++] = clusterStart;
}
PLATFORM_ASSERT(i == lenPositions);
}
}
}
} else {
// No font so return an ascending range of values
for (int i = 0; i < len; i++) {
positions[i] = i + 1;
}
}
}
XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) {
if (font_.GetID()) {
if (PFont(font_)->pfd) {
std::string utfForm;
pango_layout_set_font_description(layout, PFont(font_)->pfd);
PangoRectangle pos;
if (et == UTF8) {
pango_layout_set_text(layout, s, len);
} else {
SetConverter(PFont(font_)->characterSet);
utfForm = UTF8FromIconv(conv, s, len);
if (utfForm.empty()) { // iconv failed so treat as Latin1
utfForm = UTF8FromLatin1(s, len);
}
pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
}
#ifdef PANGO_VERSION
PangoLayoutLine *pangoLine = pango_layout_get_line_readonly(layout,0);
#else
PangoLayoutLine *pangoLine = pango_layout_get_line(layout,0);
#endif
pango_layout_line_get_extents(pangoLine, NULL, &pos);
return doubleFromPangoUnits(pos.width);
}
return 1;
} else {
return 1;
}
}
XYPOSITION SurfaceImpl::WidthChar(Font &font_, char ch) {
if (font_.GetID()) {
if (PFont(font_)->pfd) {
return WidthText(font_, &ch, 1);
}
return 1;
} else {
return 1;
}
}
// Ascent and descent determined by Pango font metrics.
XYPOSITION SurfaceImpl::Ascent(Font &font_) {
if (!(font_.GetID()))
return 1;
int ascent = 0;
if (PFont(font_)->pfd) {
PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
PFont(font_)->pfd, pango_context_get_language(pcontext));
ascent =
doubleFromPangoUnits(pango_font_metrics_get_ascent(metrics));
pango_font_metrics_unref(metrics);
}
if (ascent == 0) {
ascent = 1;
}
return ascent;
}
XYPOSITION SurfaceImpl::Descent(Font &font_) {
if (!(font_.GetID()))
return 1;
if (PFont(font_)->pfd) {
PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
PFont(font_)->pfd, pango_context_get_language(pcontext));
int descent = doubleFromPangoUnits(pango_font_metrics_get_descent(metrics));
pango_font_metrics_unref(metrics);
return descent;
}
return 0;
}
XYPOSITION SurfaceImpl::InternalLeading(Font &) {
return 0;
}
XYPOSITION SurfaceImpl::ExternalLeading(Font &) {
return 0;
}
XYPOSITION SurfaceImpl::Height(Font &font_) {
return Ascent(font_) + Descent(font_);
}
XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) {
return WidthChar(font_, 'n');
}
void SurfaceImpl::SetClip(PRectangle rc) {
PLATFORM_ASSERT(context);
cairo_rectangle(context, rc.left, rc.top, rc.right, rc.bottom);
cairo_clip(context);
}
void SurfaceImpl::FlushCachedState() {}
void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
if (unicodeMode_)
et = UTF8;
}
void SurfaceImpl::SetDBCSMode(int codePage) {
if (codePage && (codePage != SC_CP_UTF8))
et = dbcs;
}
Surface *Surface::Allocate(int) {
return new SurfaceImpl();
}
Window::~Window() {}
void Window::Destroy() {
if (wid) {
ListBox *listbox = dynamic_cast<ListBox*>(this);
if (listbox) {
gtk_widget_hide(GTK_WIDGET(wid));
// clear up window content
listbox->Clear();
// resize the window to the smallest possible size for it to adapt
// to future content
gtk_window_resize(GTK_WINDOW(wid), 1, 1);