summaryrefslogtreecommitdiff
path: root/src/encoding.c
blob: ae4b43c996aa19e7a15ece6de20044d5fa7572cc (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
#include <stdio.h>
#include <string.h>

#include "encoding.h"
#include "fm.h"
#include "glyph.h"
#include "utils.h"

/**
 * Write font encoding file
 *
 * \param savein     Location to save in
 * \param name       The font name
 * \param ctx        Conversion context
 * \param type       File format to use - 0 = full; 1 = sparse
 * \param callback   Progress callback function
 */
ttf2f_result encoding_write(const char *savein, const char *name,
		ttf2f_ctx *ctx, encoding_type type,
		void (*callback)(int progress))
{
	FILE *output;
	struct glyph *g;
	size_t i;
	char out[1024];

	snprintf(out, 1024, "%s" DIR_SEP "Encoding", savein);
	if ((output = fopen(out, "w+")) == NULL)
		return TTF2F_RESULT_OPEN;

	fprintf(output, "%% %sEncoding 1.00\n", name);
	fprintf(output, "%% Encoding file for font '%s'\n\n", name);

	if (type == ENCODING_TYPE_NORMAL) {
		for (i = 0; i != 32; i++) {
			fprintf(output, "/.notdef\n");
		}
	}

	for (i = 0; i != ctx->nglyphs; i++) {
		g = &ctx->glyphs[i];

		callback(i * 100 / ctx->nglyphs);
		ttf2f_poll(1);

		if (type == ENCODING_TYPE_SPARSE) {
			if (g->name != 0) {
				/* .notdef is implicit */
				if (strcmp(g->name, ".notdef") == 0)
					continue;
				fprintf(output, "%4.4zX;%s;COMMENT\n", i+32,
							g->name);
			} else if (g->code != (unsigned int) -1) {
				fprintf(output, "%4.4zX;uni%04X;COMMENT\n",
							i+32, g->code);
			} else {
				fprintf(output, "# Skipping %4.4zX\n", i+32);
			}
		} else {
			if (g->name != 0) {
				fprintf(output, "/%s\n", g->name);
			} else if (g->code != (unsigned int) -1) {
				fprintf(output, "/uni%4.4X\n", g->code);
			} else {
				fprintf(output, "/.notdef\n");
			}
		}
	}

	fclose(output);

	return TTF2F_RESULT_OK;
}