-
Notifications
You must be signed in to change notification settings - Fork 2
/
cfb.c
353 lines (315 loc) · 8.98 KB
/
cfb.c
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
/*
* Frame buffer low level functions for QEmacs
*
* Copyright (c) 2001, 2002 Fabrice Bellard.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#include "cfb.h"
#include "fbfrender.h"
static unsigned int cfb15_get_color(unsigned int color)
{
unsigned int r, g, b;
r = (color >> 16) & 0xff;
g = (color >> 8) & 0xff;
b = (color) & 0xff;
return ((((r) >> 3) << 10) | (((g) >> 3) << 5) | ((b) >> 3));
}
static unsigned int cfb16_get_color(unsigned int color)
{
unsigned int r, g, b;
r = (color >> 16) & 0xff;
g = (color >> 8) & 0xff;
b = (color) & 0xff;
return ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3));
}
static unsigned int cfb24_get_color(unsigned int color)
{
return color & 0xffffff;
}
static void cfb16_fill_rectangle(QEditScreen *s,
int x1, int y1, int w, int h, QEColor color)
{
CFBContext *cfb = s->priv_data;
unsigned char *dest, *d;
int y, n;
unsigned int col;
col = cfb->get_color(color);
col = (col << 16) | col;
dest = cfb->base + y1 * cfb->wrap + x1 * 2;
if (color == QECOLOR_XOR) {
/* XXX: suppress this mess */
for (y = 0; y < h; y++) {
d = dest;
for (n = w; n != 0; n--) {
((short *)d)[0] ^= 0xffff;
d += 2;
}
dest += cfb->wrap;
}
} else {
for (y = 0; y < h; y++) {
d = dest;
n = w;
if (((int)d & 3) != 0) {
((short *)d)[0] = col;
d += 2;
n--;
}
while (n >= 8) {
((int *)d)[0] = col;
((int *)d)[1] = col;
((int *)d)[2] = col;
((int *)d)[3] = col;
d += 16;
n -= 8;
}
while (n > 0) {
((short *)d)[0] = col;
d += 2;
n--;
}
dest += cfb->wrap;
}
}
}
static void cfb32_fill_rectangle(QEditScreen *s,
int x1, int y1, int w, int h, QEColor color)
{
CFBContext *cfb = s->priv_data;
unsigned char *dest, *d;
int y, n;
unsigned int col;
col = cfb->get_color(color);
dest = cfb->base + y1 * cfb->wrap + x1 * 4;
if (color == QECOLOR_XOR) {
/* XXX: suppress this mess */
for (y = 0; y < h; y++) {
d = dest;
for (n = w; n != 0; n--) {
((int *)d)[0] ^= 0x00ffffff;
d += 4;
}
dest += cfb->wrap;
}
} else {
for (y = 0; y < h; y++) {
d = dest;
n = w;
while (n >= 4) {
((int *)d)[0] = col;
((int *)d)[1] = col;
((int *)d)[2] = col;
((int *)d)[3] = col;
d += 16;
n -= 4;
}
while (n > 0) {
((int *)d)[0] = col;
d += 4;
n--;
}
dest += cfb->wrap;
}
}
}
static void cfb16_draw_glyph(QEditScreen *s1,
int x1, int y1, int w, int h, QEColor color,
unsigned char *glyph, int glyph_wrap)
{
CFBContext *cfb = s1->priv_data;
unsigned char *dest, *d, *s, *src;
int n;
unsigned int col;
col = cfb->get_color(color);
dest = cfb->base + y1 * cfb->wrap + x1 * 2;
src = glyph;
while (h > 0) {
n = w;
s = src;
d = dest;
while (n >= 4) {
if (s[0] >= 0x80)
((short *)d)[0] = col;
if (s[1] >= 0x80)
((short *)d)[1] = col;
if (s[2] >= 0x80)
((short *)d)[2] = col;
if (s[3] >= 0x80)
((short *)d)[3] = col;
s += 4;
d += 4 * 2;
n -= 4;
}
while (n > 0) {
if (s[0] >= 0x80)
((short *)d)[0] = col;
s++;
d += 2;
n--;
}
h--;
src += glyph_wrap;
dest += cfb->wrap;
}
}
static void cfb32_draw_glyph(QEditScreen *s1,
int x1, int y1, int w, int h, QEColor color,
unsigned char *glyph, int glyph_wrap)
{
CFBContext *cfb = s1->priv_data;
unsigned char *dest, *d, *s, *src;
int n;
unsigned int col;
col = cfb->get_color(color);
dest = cfb->base + y1 * cfb->wrap + x1 * 4;
src = glyph;
while (h > 0) {
n = w;
s = src;
d = dest;
while (n >= 4) {
if (s[0] >= 0x80)
((int *)d)[0] = col;
if (s[1] >= 0x80)
((int *)d)[1] = col;
if (s[2] >= 0x80)
((int *)d)[2] = col;
if (s[3] >= 0x80)
((int *)d)[3] = col;
s += 4;
d += 4 * 4;
n -= 4;
}
while (n > 0) {
if (s[0] >= 0x80)
((int *)d)[0] = col;
s++;
d += 4;
n--;
}
h--;
src += glyph_wrap;
dest += cfb->wrap;
}
}
static void cfb_draw_text(QEditScreen *s, QEFont *font,
int x_start, int y, const unsigned int *str, int len,
QEColor color)
{
CFBContext *cfb = s->priv_data;
GlyphCache *g;
unsigned char *glyph_ptr;
int i, x1, y1, x2, y2, wrap, x;
unsigned int cc;
x = x_start;
for (i = 0;i < len; i++) {
cc = str[i];
g = decode_cached_glyph(s, font, cc);
if (!g)
continue;
x1 = x + g->x;
x2 = x1 + g->w;
y2 = y - g->y;
y1 = y2 - g->h;
glyph_ptr = g->data;
wrap = g->w;
if (x1 >= s->clip_x1 && y1 >= s->clip_y1 &&
x2 <= s->clip_x2 && y2 <= s->clip_y2)
goto draw;
if (x2 <= s->clip_x1 || y2 <= s->clip_y1 ||
x1 >= s->clip_x2 || y1 >= s->clip_y2)
goto nodraw;
if (x1 < s->clip_x1) {
glyph_ptr += s->clip_x1 - x1;
x1 = s->clip_x1;
}
if (x2 > s->clip_x2)
x2 = s->clip_x2;
if (y1 < s->clip_y1) {
glyph_ptr += (s->clip_y1 - y1) * wrap;
y1 = s->clip_y1;
}
if (y2 > s->clip_y2)
y2 = s->clip_y2;
draw:
cfb->draw_glyph(s, x1, y1,
x2 - x1, y2 - y1,
color, glyph_ptr, wrap);
nodraw:
x += g->xincr;
}
/* underline synthesis */
if (font->style & (QE_STYLE_UNDERLINE | QE_STYLE_LINE_THROUGH)) {
int dy, h, w;
h = (font->descent + 2) / 4;
if (h < 1)
h = 1;
w = x - x_start;
if (font->style & QE_STYLE_UNDERLINE) {
dy = (font->descent + 1) / 3;
fill_rectangle(s, x_start, y + dy, w, h, color);
}
if (font->style & QE_STYLE_LINE_THROUGH) {
dy = -(font->ascent / 2 - 1);
fill_rectangle(s, x_start, y + dy, w, h, color);
}
}
}
static void cfb_set_clip(__unused__ QEditScreen *s,
__unused__ int x, __unused__ int y,
__unused__ int w, __unused__ int h)
{
}
int cfb_init(QEditScreen *s,
void *base, int wrap, int depth, const char *font_path)
{
CFBContext *cfb = s->priv_data;
cfb->base = base;
cfb->wrap = wrap;
cfb->depth = depth;
cfb->bpp = (depth + 7) / 8;
switch (depth) {
case 15:
cfb->get_color = cfb15_get_color;
break;
case 16:
cfb->get_color = cfb16_get_color;
break;
case 24:
default:
cfb->get_color = cfb24_get_color;
break;
}
switch (cfb->bpp) {
case 2:
s->dpy.dpy_fill_rectangle = cfb16_fill_rectangle;
cfb->draw_glyph = cfb16_draw_glyph;
break;
default:
case 4:
s->dpy.dpy_fill_rectangle = cfb32_fill_rectangle;
cfb->draw_glyph = cfb32_draw_glyph;
break;
}
s->dpy.dpy_set_clip = cfb_set_clip;
s->dpy.dpy_draw_text = cfb_draw_text;
/* fbf font handling init */
s->dpy.dpy_text_metrics = fbf_text_metrics;
s->dpy.dpy_open_font = fbf_open_font;
s->dpy.dpy_close_font = fbf_close_font;
return fbf_render_init(font_path);
}