post: drawing commands
[neatpost.git] / font.c
blob052b752354e92d7004d747dc9c5f18479e33b02a
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "post.h"
6 static void skipline(FILE* filp)
8 int c;
9 do {
10 c = getc(filp);
11 } while (c != '\n' && c != EOF);
14 struct glyph *font_find(struct font *fn, char *name)
16 int i;
17 for (i = 0; i < fn->n; i++)
18 if (!strcmp(name, fn->c[i]))
19 return fn->g[i];
20 return NULL;
23 struct glyph *font_glyph(struct font *fn, char *id)
25 int i;
26 for (i = 0; i < fn->nglyphs; i++)
27 if (!strcmp(fn->glyphs[i].id, id))
28 return &fn->glyphs[i];
29 return NULL;
32 static void font_charset(struct font *fn, FILE *fin)
34 char tok[ILNLEN];
35 char name[ILNLEN];
36 char id[ILNLEN];
37 struct glyph *glyph = NULL;
38 struct glyph *prev = NULL;
39 int wid, type;
40 while (fn->n < NGLYPHS) {
41 if (fscanf(fin, "%s", name) != 1)
42 break;
43 fscanf(fin, "%s", tok);
44 glyph = prev;
45 if (strcmp("\"", tok)) {
46 wid = atoi(tok);
47 fscanf(fin, "%d %s", &type, id);
48 skipline(fin);
49 glyph = &fn->glyphs[fn->nglyphs++];
50 strcpy(glyph->id, id);
51 strcpy(glyph->name, name);
52 glyph->wid = wid;
53 glyph->type = type;
54 glyph->font = fn;
56 prev = glyph;
57 strcpy(fn->c[fn->n], name);
58 fn->g[fn->n] = glyph;
59 fn->n++;
63 struct font *font_open(char *path)
65 struct font *fn = malloc(sizeof(*fn));
66 char tok[ILNLEN];
67 FILE *fin;
68 fin = fopen(path, "r");
69 memset(fn, 0, sizeof(*fn));
70 while (fscanf(fin, "%s", tok) == 1) {
71 if (tok[0] == '#') {
72 skipline(fin);
73 continue;
75 if (!strcmp("spacewidth", tok)) {
76 fscanf(fin, "%d", &fn->spacewid);
77 continue;
79 if (!strcmp("special", tok)) {
80 fn->special = 1;
81 continue;
83 if (!strcmp("name", tok)) {
84 fscanf(fin, "%s", fn->name);
85 continue;
87 if (!strcmp("fontname", tok)) {
88 fscanf(fin, "%s", fn->psname);
89 continue;
91 if (!strcmp("named", tok)) {
92 skipline(fin);
93 continue;
95 if (!strcmp("ligatures", tok)) {
96 while (fscanf(fin, "%s", tok) == 1)
97 if (!strcmp("0", tok))
98 break;
99 skipline(fin);
100 continue;
102 if (!strcmp("charset", tok)) {
103 font_charset(fn, fin);
104 break;
107 fclose(fin);
108 return fn;
111 void font_close(struct font *fn)
113 free(fn);