summaryrefslogtreecommitdiff
path: root/src/toolbox.c
blob: 683571d2f160c5f80648e14d81dfce5d2dfac2d7 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#include <oslib/displayfield.h>
#include <oslib/osfile.h>
#include <oslib/quit.h>
#include <oslib/slider.h>
#include <oslib/stringset.h>
#include <oslib/writablefield.h>
#include <Event.h>

#include "encoding.h"
#include "ft.h"
#include "fm.h"
#include "glyph.h"
#include "glyphs.h"
#include "intmetrics.h"
#include "outlines.h"
#include "utils.h"

#define Convert_Font 0x101

static int ttf2f_quit;
static messagetrans_control_block messages_file;
static toolbox_o main_window;
static int converting;

typedef struct ttf2f_message_list {
	int first;
	int rest[];
} ttf2f_message_list;

typedef union message_list {
	ttf2f_message_list ttf2f;
	wimp_message_list wimp;
	toolbox_action_list toolbox;
} message_list;

#define PTR_WIMP_MESSAGE_LIST(l) ((wimp_message_list *) (message_list *) (l))
#define PTR_TOOLBOX_ACTION_LIST(l) ((toolbox_action_list *) (message_list *) (l))

/* Wimp Messages we're interested in */
static ttf2f_message_list messages = {
	message_DATA_LOAD,
	{
		message_QUIT,
		0
	}
};

/* Toolbox events we're interested in */
static ttf2f_message_list tbcodes = {
	Convert_Font,
	{
		action_ERROR,
		action_QUIT_QUIT,
		0
	}
};

static void ttf2f_init(int argc, char **argv);
static void ttf2f_exit(void);
static void register_toolbox_handlers(void);
static osbool toolbox_event_quit(bits event_code, toolbox_action *event,
		toolbox_block *id_block, void *handle);
static osbool toolbox_event_error(bits event_code, toolbox_action *event,
		toolbox_block *id_block, void *handle);
static osbool convert_font(bits event_code, toolbox_action *event,
		toolbox_block *id_block, void *handle);
static void register_message_handlers(void);
static int wimp_message_quit(wimp_message *message,void *handle);
static int wimp_message_dataload(wimp_message *message,void *handle);
static void progress_bar(int value);

int main(int argc, char **argv)
{
	ttf2f_init(argc, argv);

	while (!ttf2f_quit)
		ttf2f_poll(0);

	ttf2f_exit();

	return 0;
}


void ttf2f_init(int argc, char **argv)
{
	os_error *error;
	toolbox_block toolbox_block;
	char *stringset;
	ttf2f_result res;

	UNUSED(argc);
	UNUSED(argv);

	ft_init();

	res = glyph_load_list();
	if (res != TTF2F_RESULT_OK)
		exit(1);

	event_initialise(&toolbox_block);

	event_set_mask(wimp_MASK_NULL |
			wimp_MASK_LEAVING |
			wimp_MASK_ENTERING |
			wimp_QUEUE_MOUSE |
			wimp_QUEUE_KEY |
			wimp_MASK_LOSE |
			wimp_MASK_GAIN);

	register_toolbox_handlers();
	register_message_handlers();

	error = xtoolbox_initialise(0, 310,
				PTR_WIMP_MESSAGE_LIST(&messages), 
				PTR_TOOLBOX_ACTION_LIST(&tbcodes),
				"<TTF2F$Dir>", &messages_file,
				&toolbox_block, NULL, NULL, NULL);
	if (error) {
		fprintf(stderr, "toolbox_initialise: 0x%x: %s\n",
			error->errnum, error->errmess);
			exit(1);
	}

	error = xtoolbox_create_object(0, (toolbox_id) "main", &main_window);
	if (error) {
		fprintf(stderr, "toolbox_create_object: 0x%x: %s\n",
			error->errnum, error->errmess);
		exit(1);
	}

	stringset = getenv("Font$Path");
	if (stringset) {
		error = xstringset_set_available(0, main_window, 7,
				stringset);
		if (error) {
			fprintf(stderr,
				"stringset_set_available: 0x%x: %s\n",
				error->errnum, error->errmess);
			exit(1);
		}
	}
}

void ttf2f_poll(int active)
{
	int event;
	unsigned int mask;
	wimp_block block;

	if (active || converting) {
		event_set_mask(0x3972);
		event_poll(&event, &block, 0);
	} else {
		event_get_mask(&mask);
		event_set_mask(mask | wimp_MASK_NULL);
		event_poll(&event, &block, 0);
	}
}

void ttf2f_exit(void)
{
	ft_fini();

	glyph_destroy_list();
}

/**
 * Register event handlers with the toolbox
 */
void register_toolbox_handlers(void)
{
	osbool success;

	success = event_register_toolbox_handler(event_ANY,
			action_QUIT_QUIT, toolbox_event_quit, NULL);
	if (success == FALSE)
		fprintf(stderr, "registering quit_QUIT failed\n");

	success = event_register_toolbox_handler(event_ANY, 
			action_ERROR, toolbox_event_error, NULL);
	if (success == FALSE)
		fprintf(stderr, "registering action_ERROR failed\n");

	success = event_register_toolbox_handler(event_ANY, 
			Convert_Font, convert_font, NULL);
	if (success == FALSE)
		fprintf(stderr, "registering Convert_Font failed\n");
}

/**
 * Handle quit events
 */
osbool toolbox_event_quit(bits event_code, toolbox_action *event,
		toolbox_block *id_block, void *handle)
{
	UNUSED(event_code);
	UNUSED(event);
	UNUSED(id_block);
	UNUSED(handle);

	ttf2f_quit = 1;

	return TRUE;
}

/**
 * Handle toolbox errors
 */
osbool toolbox_event_error(bits event_code, toolbox_action *event,
		toolbox_block *id_block, void *handle)
{
	toolbox_action_error_block *error = 
			(toolbox_action_error_block *) event;

	UNUSED(event_code);
	UNUSED(id_block);
	UNUSED(handle);

	fprintf(stderr, "toolbox error: 0x%x: %s\n",
		error->errnum, error->errmess);

	return TRUE;
}

/**
 * Register message handlers
 */
void register_message_handlers(void)
{
	osbool success;

	success = event_register_message_handler(message_QUIT, 
			wimp_message_quit, NULL);
	if (success == FALSE)
		fprintf(stderr, "registering message_QUIT handler failed\n");

	success = event_register_message_handler(message_DATA_LOAD,
			wimp_message_dataload, NULL);
	if (success == FALSE)
		fprintf(stderr,
			"registering message_DATA_LOAD handler failed\n");
}

/**
 * Handle quit messages
 */
osbool wimp_message_quit(wimp_message *message, void *handle)
{
	UNUSED(message);
	UNUSED(handle);

	ttf2f_quit = 1;

	return TRUE;
}

/**
 * Handle dataload messages
 */
osbool wimp_message_dataload(wimp_message *message, void *handle)
{
	os_error *error;
	wimp_message_data_xfer *dl = &message->data.data_xfer;

	UNUSED(handle);

	error = xdisplayfield_set_value(0, main_window, 0, dl->file_name);
	if (error != NULL) {
		fprintf(stderr, "displayfield_set_value: 0x%x: %s\n",
			error->errnum, error->errmess);
	}

	message->action = message_DATA_LOAD_ACK;
	message->your_ref = message->my_ref;

	error = xwimp_send_message(wimp_USER_MESSAGE, message, message->sender);
	if (error != NULL) {
		fprintf(stderr, "wimp_send_message: 0x%x: %s\n",
			error->errnum, error->errmess);
	}

	return TRUE;
}




osbool convert_font(bits event_code, toolbox_action *event,
		toolbox_block *id_block, void *handle)
{
	os_error *error, erblock = { 123456, "Invalid Parameters" };
	char ifilename[256], ofilename[256], save_in[1024];
	char *dot, *last_dot, *save;
	int fail;
	ttf2f_ctx ctx;
	ttf2f_result res;

	UNUSED(event_code);
	UNUSED(event);
	UNUSED(id_block);
	UNUSED(handle);

	if (converting)
		return TRUE;

	converting = 1;

	/* get input file */
	error = xdisplayfield_get_value(0, main_window, 0, ifilename, 256, 0);
	if (error) {
		fprintf(stderr, "displayfield_get_value: 0x%x: %s\n",
			error->errnum, error->errmess);
	}

	/* read font name */
	error = xwritablefield_get_value(0, main_window, 3, ofilename, 256, 0);
	if (error) {
		fprintf(stderr, "writablefield_get_value: 0x%x: %s\n",
			error->errnum, error->errmess);
	}

	/* read save location */
	error = xstringset_get_selected_string(0, main_window, 7, 
			save_in, 1024, 0);
	if (error) {
		fprintf(stderr, "stringset_get_selected: 0x%x: %s\n",
			error->errnum, error->errmess);
	}

	/* sanity check */
	if (strcmp(save_in, "Save in") == 0 || strcmp(ofilename, "") == 0 ||
		strcmp(ifilename, "Filename") == 0) {
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* create output directories */
	last_dot = ofilename;
	save = save_in + strlen(save_in);

	for (dot = last_dot; *dot != '\0'; dot++) {
		if (*dot == '.') {
			error = xosfile_create_dir(save_in, 0);
			if (error) {
				fprintf(stderr, "os_file: 0x%x: %s\n",
					error->errnum, error->errmess);
				wimp_report_error(error, 0x5, "TTF2f");
				converting  = 0;
				return TRUE;
			}
		}

		*(save++) = *dot;
	}
	*save = '\0';

	error = xosfile_create_dir(save_in, 0);
	if (error) {
		fprintf(stderr, "os_file: 0x%x: %s\n",
			error->errnum, error->errmess);
		wimp_report_error(error, 0x5, "TTF2f");
		converting  = 0;
		return TRUE;
	}

	ctx.face = open_font(ifilename);
	if (ctx.face == NULL) {
		snprintf(erblock.errmess, 252, "Unknown font format");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	ctx.nglyphs = count_glyphs(&ctx);

	ctx.glyphs = calloc(ctx.nglyphs, sizeof(struct glyph));
	if (ctx.glyphs == NULL) {
		close_font(ctx.face);
		fprintf(stderr, "malloc failed\n");
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* Initialise glyph list */
	for (size_t i = 0; i != ctx.nglyphs; i++) {
		struct glyph *g = &ctx.glyphs[i];

		g->code = -1;
	}

	/* create buffer for font metrics data */
	ctx.metrics = calloc(1, sizeof(struct font_metrics));
	if (ctx.metrics == NULL) {
		free(ctx.glyphs);
		close_font(ctx.face);
		fprintf(stderr, "malloc failed\n");
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* read global font metrics */
	fail = fnmetrics(&ctx);
	if (fail) {
		free(ctx.metrics);
		free(ctx.glyphs);
		close_font(ctx.face);
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* map glyph ids to charcodes */
	fail = glenc(&ctx);
	if (fail) {
		free(ctx.metrics->name_copyright);
		free(ctx.metrics->name_full);
		free(ctx.metrics->name_version);
		free(ctx.metrics->name_ps);
		free(ctx.metrics);
		free(ctx.glyphs);
		close_font(ctx.face);
		snprintf(erblock.errmess, 252, "Unknown encoding");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* extract glyph names */
	fail = glnames(&ctx);
	if (fail) {
		free(ctx.metrics->name_copyright);
		free(ctx.metrics->name_full);
		free(ctx.metrics->name_version);
		free(ctx.metrics->name_ps);
		free(ctx.metrics);
		free(ctx.glyphs);
		close_font(ctx.face);
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* olive */
	slider_set_colour(0, main_window, 8, 13, 0);

	/* extract glyph metrics */
	glmetrics(&ctx, progress_bar);

	/* red */
	slider_set_colour(0, main_window, 8, 11, 13);

	/* write intmetrics file */
	res = intmetrics_write(save_in, ofilename, &ctx, progress_bar);
	if (res != TTF2F_RESULT_OK) {
		free(ctx.metrics->name_copyright);
		free(ctx.metrics->name_full);
		free(ctx.metrics->name_version);
		free(ctx.metrics->name_ps);
		free(ctx.metrics);
		free(ctx.glyphs);
		close_font(ctx.face);
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* blue */
	slider_set_colour(0, main_window, 8, 8, 11);

	/* write outlines file */
	res = outlines_write(save_in, ofilename, &ctx, progress_bar);
	if (res != TTF2F_RESULT_OK) {
		free(ctx.metrics->name_copyright);
		free(ctx.metrics->name_full);
		free(ctx.metrics->name_version);
		free(ctx.metrics->name_ps);
		free(ctx.metrics);
		free(ctx.glyphs);
		close_font(ctx.face);
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* green */
	slider_set_colour(0, main_window, 8, 10, 8);

	/* write encoding file */
	res = encoding_write(save_in, ofilename, &ctx, 
			ENCODING_TYPE_NORMAL, progress_bar);
	if (res != TTF2F_RESULT_OK) {
		free(ctx.metrics->name_copyright);
		free(ctx.metrics->name_full);
		free(ctx.metrics->name_version);
		free(ctx.metrics->name_ps);
		free(ctx.metrics);
		free(ctx.glyphs);
		close_font(ctx.face);
		snprintf(erblock.errmess, 252, "Insufficient memory");
		wimp_report_error(&erblock, 0x5, "TTF2f");
		converting = 0;
		return TRUE;
	}

	/* reset slider */
	slider_set_colour(0, main_window, 8, 8, 0);
	slider_set_value(0, main_window, 8, 0);

	free(ctx.metrics->name_copyright);
	free(ctx.metrics->name_full);
	free(ctx.metrics->name_version);
	free(ctx.metrics->name_ps);
	free(ctx.metrics);
	free(ctx.glyphs);

	close_font(ctx.face);

	converting = 0;

	return TRUE;
}

void progress_bar(int value)
{
	slider_set_value(0, main_window, 8, value);
}