forked from iPodLinux-Community/Floydzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts.c
347 lines (307 loc) · 7.65 KB
/
fonts.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
/*
* Copyright (C) 2005 Courtney Cavin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// KERIPO MOD
#include "_mods.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "pz.h"
#include "mlist.h"
#include "ipod.h"
#include "settings.h"
// KERIPO MOD
// defined in "_mods.h"
/*
#define FONTCONF "font.lst"
#ifdef IPOD
#define FONTDIR "/usr/share/fonts/"
#else
#define FONTDIR "fonts/"
#endif
*/
typedef struct _fontlist_st {
char file[128];
char name[64];
int size;
} fontlist_st;
static GR_WINDOW_ID font_wid;
static GR_GC_ID font_gc;
static GR_FONT_ID font_font;
static GR_FONT_ID pz_font;
fontlist_st fl[64]; /* max 64 fonts */
static int num_fonts = 0;
static int cur_font = 0;
static int foc;
static menu_st *main_menu;
int parseline(char *parseline, char *file, char *name, char *size)
{
int i;
typedef struct {
char definer[2];
char *string;
int required;
} vars;
vars cvars[] = {
{"[]", NULL, 1},
{"()", NULL, 1},
{"<>", NULL, 0},
{"", NULL}
};
char line[128];
char *ptr;
char *cur;
cvars[0].string = file;
cvars[1].string = name;
cvars[2].string = size;
strcpy(line, parseline);
ptr = line;
while (1) {
while (*ptr == ' ' || *ptr == '\t')
ptr++;
if (*ptr == '\n' || *ptr == '\r' || *ptr =='\0' || *ptr == '#')
break;
for (i = 0; cvars[i].string != NULL; i++) {
if (*ptr != cvars[i].definer[0])
continue;
for (cur = ++ptr; *ptr != cvars[i].definer[1]; ptr++)
if(*ptr == '\n' || *ptr == '\r' || *ptr == '\0')
return -1;
strncpy(cvars[i].string, cur, ptr - cur);
cvars[i].string[ptr - cur] = '\0';
if (cvars[i].required == 1)
cvars[i].required = 2;
break;
}
if (cvars[i].string == NULL)
return -1;
ptr++;
}
for (i = 0; cvars[i].string != NULL; i++)
if (cvars[i].required == 1)
return -1;
return 0;
}
int populate_fontlist() {
FILE *fd;
fd = fopen(FONTDIR FONTCONF, "r");
if (fd == NULL)
return -1;
num_fonts = 0;
while (!feof(fd) && !ferror(fd)) {
char file[64];
char name[64];
char size[4];
char line[128];
fgets(line, 127, fd);
if (strlen(line) <= 0)
continue;
if (line[0] == '#')
continue;
if (parseline(line, file, name, size) >= 0) {
strcpy(fl[num_fonts].file, FONTDIR);
strcat(fl[num_fonts].file, file);
strcpy(fl[num_fonts].name, name);
fl[num_fonts].size = atoi(size);
if (num_fonts++ >= 63)
break;
}
}
fclose(fd);
return num_fonts;
}
void destroy_font(int local)
{
if (local && font_font) {
GrDestroyFont(font_font);
font_font = 0;
}
if (!local && pz_font) {
GrDestroyFont(pz_font);
pz_font = 0;
}
}
void set_font(char *file, int size, GR_GC_ID gc, int local)
{
GR_FONT_ID lfont;
lfont = GrCreateFont((GR_CHAR *)file, size, NULL);
if (lfont == 0) {
pz_error(_("Unable to load font"));
return;
}
GrSetGCFont(gc, lfont);
destroy_font(local);
if (local) {
font_font = lfont;
}
else {
pz_font = lfont;
foc = cur_font;
}
}
void load_font()
{
char *file;
char *size;
file = (char *)get_string_setting(FONT_FILE);
if (file == NULL)
return;
size = strrchr(file, ',');
*size++ = '\0';
set_font(file, atoi(size), pz_get_gc(0), 0);
free(file);
}
static void save_font()
{
char fontline[128];
snprintf(fontline, 127, "%s,%d", fl[foc].file, fl[foc].size);
set_string_setting(FONT_FILE, fontline);
}
static void font_clear_window()
{
GrClearWindow(font_wid, GR_FALSE);
GrSetGCForeground( font_gc, appearance_get_color(CS_BG) );
GrFillRect( font_wid, font_gc, 0, 0, screen_info.cols, screen_info.rows );
GrSetGCBackground( font_gc, appearance_get_color(CS_BG) );
GrSetGCForeground( font_gc, appearance_get_color(CS_FG) );
}
static void draw_font()
{
char buf[128];
char *ptr;
GR_SIZE width, height, base;
font_clear_window();
/* display font name and size */
snprintf( buf, 128, "%s (%d)", fl[cur_font].name, fl[cur_font].size);
GrGetGCTextSize(font_gc, buf, -1, GR_TFASCII,
&width, &height, &base);
GrText(font_wid, font_gc, (screen_info.cols - width) >> 1, 40,
buf, -1, GR_TFASCII);
/* display a sampler */
GrGetGCTextSize(font_gc, "AaBbCcDdEe", -1, GR_TFASCII,
&width, &height, &base);
GrText(font_wid, font_gc, (screen_info.cols - width) >> 1, 65,
"AaBbCcDdEe", -1, GR_TFASCII);
GrGetGCTextSize(font_gc, "1234567890", -1, GR_TFASCII,
&width, &height, &base);
GrText(font_wid, font_gc, (screen_info.cols - width) >> 1, 65+height,
"1234567890", -1, GR_TFASCII);
ptr = _("Spin wheel to change");
/* display instructions */
GrGetGCTextSize(font_gc, ptr, -1, GR_TFASCII, &width, &height, &base);
GrText(font_wid, font_gc, (screen_info.cols-width) >> 1, 10,
ptr, -1, GR_TFASCII);
}
static void change_font(int num)
{
GR_SIZE width, height, base;
char *load = _("Loading...");
font_clear_window();
GrGetGCTextSize(font_gc, load, -1, GR_TFASCII, &width, &height, &base);
GrText(font_wid, font_gc, (screen_info.cols - width) >> 1,
(screen_info.rows - HEADER_TOPLINE) >> 1, load,
-1, GR_TFASCII);
set_font(fl[cur_font].file, fl[cur_font].size, font_gc, 1);
}
static void font_exposure()
{
pz_draw_header(_("Fonts"));
draw_font();
}
static int font_event_handler(GR_EVENT *e)
{
int ret = 0;
switch (e->type) {
case GR_EVENT_TYPE_KEY_DOWN:
switch (e->keystroke.ch) {
case IPOD_WHEEL_COUNTERCLOCKWISE:
if( num_fonts > 0 ) {
cur_font = (cur_font - 1) < 0 ? num_fonts - 1 :
(cur_font - 1);
change_font(cur_font);
draw_font();
}
ret |= KEY_CLICK;
break;
case IPOD_WHEEL_CLOCKWISE:
if( num_fonts > 0 ) {
cur_font = (cur_font + 1) > (num_fonts - 1) ?
0 : (cur_font + 1);
change_font(cur_font);
draw_font();
}
ret |= KEY_CLICK;
break;
case IPOD_BUTTON_ACTION:
case '\n':
if( num_fonts > 0 ) {
set_font(fl[cur_font].file, fl[cur_font].size,
pz_get_gc(0), 0);
new_message_window(_("Selected font."));
}
ret |= KEY_CLICK;
break;
case IPOD_BUTTON_MENU:
destroy_font(1);
GrDestroyGC(font_gc);
pz_close_window(font_wid);
if (foc == -1)
break;
save_font();
break;
default:
ret |= KEY_UNUSED;
break;
}
break;
default:
ret |= EVENT_UNUSED;
break;
}
return ret;
}
GR_FONT_ID get_current_font()
{
GR_GC_INFO gc_info;
GrGetGCInfo(pz_get_gc(0), &gc_info);
return gc_info.font;
}
void new_font_window(menu_st *menu)
{
main_menu = menu;
if (num_fonts == 0) {
if (populate_fontlist() <= 0) {
pz_error(_("Unable to load fontlist %s%s"),
FONTDIR, FONTCONF);
return;
}
}
foc = -1;
font_gc = pz_get_gc(1); /* Grab a root GC copy */
GrSetGCUseBackground(font_gc, GR_FALSE); /* Don't Draw Background */
GrSetGCForeground(font_gc, BLACK); /* Foreground; Black */
font_wid = pz_new_window(0, HEADER_TOPLINE + 1, screen_info.cols,
screen_info.rows - (HEADER_TOPLINE + 1), font_exposure,
font_event_handler); /* Create New Window */
GrSelectEvents(font_wid, GR_EVENT_MASK_EXPOSURE | /* Events Accepted */
GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_KEY_UP);
GrMapWindow(font_wid); /* Draw Window to Screen */
change_font(cur_font);
}