-
Notifications
You must be signed in to change notification settings - Fork 262
/
Fl_Xlib_Graphics_Driver.cxx
292 lines (255 loc) · 7.77 KB
/
Fl_Xlib_Graphics_Driver.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
//
// "$Id$"
//
// Rectangle drawing routines for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2018 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:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
//
#include "../../config_lib.h"
#include "Fl_Xlib_Graphics_Driver.H"
#include "Fl_Font.H"
#include <FL/fl_draw.H>
#include <FL/platform.H>
#include <string.h>
extern XIC fl_xim_ic;
extern char fl_is_over_the_spot;
#if !USE_XFT
extern char *fl_get_font_xfld(int fnum, int size);
#endif
/*
* By linking this module, the following static method will instantiate the
* X11 Xlib Graphics driver as the main display driver.
*/
Fl_Graphics_Driver *Fl_Graphics_Driver::newMainGraphicsDriver()
{
return new Fl_Xlib_Graphics_Driver();
}
GC Fl_Xlib_Graphics_Driver::gc_ = NULL;
/* Reference to the current graphics context
For back-compatibility only. The preferred procedure to get this pointer is
Fl_Surface_Device::surface()->driver()->gc().
*/
GC fl_gc = 0;
Fl_Xlib_Graphics_Driver::Fl_Xlib_Graphics_Driver(void) {
mask_bitmap_ = NULL;
p_size = 0;
p = NULL;
line_delta_ = 0;
#if USE_PANGO
pfd_ = pango_font_description_new();
Fl_Graphics_Driver::font(0, 0);
#endif
offset_x_ = 0; offset_y_ = 0;
depth_ = 0;
clip_max_ = 32760; // clipping limit (2**15 - 8)
}
Fl_Xlib_Graphics_Driver::~Fl_Xlib_Graphics_Driver() {
if (p) free(p);
#if USE_PANGO
pango_font_description_free(pfd_);
#endif
}
void Fl_Xlib_Graphics_Driver::gc(void *value) {
gc_ = (GC)value;
fl_gc = gc_;
}
void Fl_Xlib_Graphics_Driver::scale(float f) {
#if USE_XFT
if (f != scale()) {
size_ = 0;
Fl_Graphics_Driver::scale(f);
//fprintf(stderr, "scale=%.2f\n", scale_);
line_style(FL_SOLID); // scale also default line width
/* Scaling >= 2 transforms 1-pixel wide lines into wider lines.
X11 draws 2-pixel wide lines so that half of the line width is above or at left
of a 1-pixel wide line that would be drawn with the same coordinates.
Thus, if the line starts at coordinates (0,0) half of the line width is invisible.
Similarly, if the line ends at w()-1 the last pixel of the window is not drawn.
What is wanted when scale_ == 2 is a visible 2-pixel wide line in the first case,
and a line at the window's edge in the 2nd case.
Setting line_delta_ to 1 and offsetting all line, rectangle, text and clip
coordinates by line_delta_ achieves what is wanted until scale_ <= 3.5.
*/
line_delta_ = (scale() > 1.75 ? 1 : 0);
}
#endif
}
void Fl_Xlib_Graphics_Driver::copy_offscreen(int x, int y, int w, int h, Fl_Offscreen pixmap, int srcx, int srcy) {
XCopyArea(fl_display, pixmap, fl_window, gc_, srcx*scale(), srcy*scale(), w*scale(), h*scale(), (x+offset_x_)*scale(), (y+offset_y_)*scale());
}
void Fl_Xlib_Graphics_Driver::add_rectangle_to_region(Fl_Region r, int X, int Y, int W, int H) {
XRectangle R;
R.x = X; R.y = Y; R.width = W; R.height = H;
XUnionRectWithRegion(&R, r, r);
}
void Fl_Xlib_Graphics_Driver::transformed_vertex0(float fx, float fy) {
short x = short(fx), y = short(fy);
if (!n || x != p[n-1].x || y != p[n-1].y) {
if (n >= p_size) {
p_size = p ? 2*p_size : 16;
p = (XPOINT*)realloc((void*)p, p_size*sizeof(*p));
}
p[n].x = x + line_delta_;
p[n].y = y + line_delta_;
n++;
}
}
void Fl_Xlib_Graphics_Driver::fixloop() { // remove equal points from closed path
while (n>2 && p[n-1].x == p[0].x && p[n-1].y == p[0].y) n--;
}
// FIXME: should be members of Fl_Xlib_Graphics_Driver
XRectangle fl_spot;
int fl_spotf = -1;
int fl_spots = -1;
void Fl_Xlib_Graphics_Driver::reset_spot(void)
{
fl_spot.x = -1;
fl_spot.y = -1;
//if (fl_xim_ic) XUnsetICFocus(fl_xim_ic);
}
void Fl_Xlib_Graphics_Driver::set_spot(int font, int size, int X, int Y, int W, int H, Fl_Window *win)
{
int change = 0;
XVaNestedList preedit_attr;
static XFontSet fs = NULL;
char **missing_list;
int missing_count;
char *def_string;
char *fnt = NULL;
bool must_free_fnt =true;
static XIC ic = NULL;
if (!fl_xim_ic || !fl_is_over_the_spot) return;
//XSetICFocus(fl_xim_ic);
if (X != fl_spot.x || Y != fl_spot.y) {
fl_spot.x = X;
fl_spot.y = Y;
fl_spot.height = H;
fl_spot.width = W;
change = 1;
}
if (font != fl_spotf || size != fl_spots) {
fl_spotf = font;
fl_spots = size;
change = 1;
if (fs) {
XFreeFontSet(fl_display, fs);
}
#if USE_XFT
#if defined(__GNUC__)
// FIXME: warning XFT support here
#endif /*__GNUC__*/
fnt = NULL; // fl_get_font_xfld(font, size);
if (!fnt) {fnt = (char*)"-misc-fixed-*";must_free_fnt=false;}
fs = XCreateFontSet(fl_display, fnt, &missing_list,
&missing_count, &def_string);
#else
fnt = fl_get_font_xfld(font, size);
if (!fnt) {fnt = (char*)"-misc-fixed-*";must_free_fnt=false;}
fs = XCreateFontSet(fl_display, fnt, &missing_list,
&missing_count, &def_string);
#endif
}
if (fl_xim_ic != ic) {
ic = fl_xim_ic;
change = 1;
}
if (fnt && must_free_fnt) free(fnt);
if (!change) return;
preedit_attr = XVaCreateNestedList(0,
XNSpotLocation, &fl_spot,
XNFontSet, fs, NULL);
XSetICValues(fl_xim_ic, XNPreeditAttributes, preedit_attr, NULL);
XFree(preedit_attr);
}
#if !USE_XFT
unsigned Fl_Xlib_Graphics_Driver::font_desc_size() {
return (unsigned)sizeof(Fl_Xlib_Fontdesc);
}
#endif
const char *Fl_Xlib_Graphics_Driver::font_name(int num) {
#if USE_XFT
return fl_fonts[num].name;
#else
return ((Fl_Xlib_Fontdesc*)fl_fonts)[num].name;
#endif
}
void Fl_Xlib_Graphics_Driver::font_name(int num, const char *name) {
#if USE_XFT
# if USE_PANGO
init_built_in_fonts();
# endif
Fl_Fontdesc *s = fl_fonts + num;
#else
Fl_Xlib_Fontdesc *s = ((Fl_Xlib_Fontdesc*)fl_fonts) + num;
#endif
if (s->name) {
if (!strcmp(s->name, name)) {s->name = name; return;}
#if !USE_XFT
if (s->xlist && s->n >= 0) XFreeFontNames(s->xlist);
#endif
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;
#if !USE_XFT
s->xlist = 0;
#endif
s->first = 0;
}
Region Fl_Xlib_Graphics_Driver::scale_clip(float f) {
Region r = rstack[rstackptr];
if (r == 0 || (f == 1 && offset_x_ == 0 && offset_y_ == 0) ) return 0;
int deltaf = f/2;
Region r2 = XCreateRegion();
for (int i = 0; i < r->numRects; i++) {
int x = (r->rects[i].x1 + offset_x_)*f;
int y = (r->rects[i].y1 + offset_y_)*f;
int w = int((r->rects[i].x2 + offset_x_) * f) - x;
int h = int((r->rects[i].y2 + offset_y_) * f) - y;
x += line_delta_ - deltaf;
y += line_delta_ - deltaf;
Region R = XRectangleRegion(x, y, w, h);
XUnionRegion(R, r2, r2);
::XDestroyRegion(R);
}
rstack[rstackptr] = r2;
return r;
}
void Fl_Xlib_Graphics_Driver::translate_all(int dx, int dy) { // reversibly adds dx,dy to the offset between user and graphical coordinates
if (depth_ < FL_XLIB_GRAPHICS_TRANSLATION_STACK_SIZE) {
stack_x_[depth_] = offset_x_;
stack_y_[depth_] = offset_y_;
depth_++;
} else {
Fl::warning("%s: translate stack overflow!", "Fl_Xlib_Graphics_Driver");
}
offset_x_ += dx;
offset_y_ += dy;
push_matrix();
translate(dx, dy);
}
void Fl_Xlib_Graphics_Driver::untranslate_all() { // undoes previous translate_all()
if (depth_ > 0) depth_--;
offset_x_ = stack_x_[depth_];
offset_y_ = stack_y_[depth_];
pop_matrix();
}
void Fl_Xlib_Graphics_Driver::set_current_() {
restore_clip();
}
//
// End of "$Id$".
//