File: font2.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (213 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <grass/gis.h>

struct glyph {
    unsigned int offset : 20;
    unsigned int count : 12;
};

static struct glyph *glyphs;
static int glyphs_alloc;

static unsigned char *xcoords, *ycoords;
static int coords_offset;
static int coords_alloc;

static int fontmap[1024];
static int num_chars;

static char current_font[16];
static int font_loaded;

static struct glyph *glyph_slot(int idx)
{
    if (glyphs_alloc <= idx) {
        int new_alloc = idx + ((glyphs_alloc > 0) ? 1000 : 4000);

        glyphs = G_realloc(glyphs, new_alloc * sizeof(struct glyph));
        memset(&glyphs[glyphs_alloc], 0,
               (new_alloc - glyphs_alloc) * sizeof(struct glyph));
        glyphs_alloc = new_alloc;
    }

    return &glyphs[idx];
}

static int coord_slots(int count)
{
    int n;

    if (coords_alloc < coords_offset + count) {
        coords_alloc =
            coords_offset + count + ((coords_alloc > 0) ? 10000 : 60000);
        xcoords = G_realloc(xcoords, coords_alloc);
        ycoords = G_realloc(ycoords, coords_alloc);
    }

    n = coords_offset;
    coords_offset += count;

    return n;
}

static void read_hersh(const char *filename)
{
    FILE *fp = fopen(filename, "r");

    if (!fp)
        return;

    while (!feof(fp)) {
        char buf[8];
        struct glyph *glyph;
        int coords;
        unsigned int i, idx, count;
        int c;

        switch (c = fgetc(fp)) {
        case '\r':
            fgetc(fp);
            continue;
        case '\n':
            continue;
        default:
            ungetc(c, fp);
            break;
        }

        if (fread(buf, 1, 5, fp) != 5)
            break;

        buf[5] = 0;
        idx = atoi(buf);

        if (fread(buf, 1, 3, fp) != 3)
            break;

        buf[3] = 0;
        count = atoi(buf);

        glyph = glyph_slot(idx);
        coords = coord_slots(count);

        glyph->offset = coords;
        glyph->count = count;

        for (i = 0; i < count; i++) {
            if ((i + 4) % 36 == 0) {
                /* skip newlines? */
                if (fgetc(fp) == '\r')
                    fgetc(fp);
            }

            xcoords[coords + i] = fgetc(fp);
            ycoords[coords + i] = fgetc(fp);
        }

        if (fgetc(fp) == '\r')
            fgetc(fp);
    }

    fclose(fp);
}

static void load_glyphs(void)
{
    int i;

    if (glyphs)
        return;

    for (i = 1; i <= 4; i++) {
        char buf[GPATH_MAX];

        sprintf(buf, "%s/fonts/hersh.oc%d", G_gisbase(), i);
        read_hersh(buf);
    }
}

static void read_fontmap(const char *name)
{
    char buf[GPATH_MAX];
    FILE *fp;

    num_chars = 0;
    memset(fontmap, 0, sizeof(fontmap));

    sprintf(buf, "%s/fonts/%s.hmp", G_gisbase(), name);

    fp = fopen(buf, "r");
    if (!fp) {
        G_warning("Unable to open font map '%s': %s. "
                  "Try running 'g.mkfontcap --overwrite'",
                  buf, strerror(errno));
        return;
    }

    while (fscanf(fp, "%s", buf) == 1) {
        int a, b;

        if (sscanf(buf, "%d-%d", &a, &b) == 2)
            while (a <= b)
                fontmap[num_chars++] = a++;
        else if (sscanf(buf, "%d", &a) == 1)
            fontmap[num_chars++] = a;
    }

    fclose(fp);
}

static void load_font(void)
{
    if (font_loaded)
        return;

    if (!glyphs)
        load_glyphs();

    read_fontmap(current_font);

    font_loaded = 1;
}

int font_init(const char *name)
{
    if (strcmp(name, current_font) == 0)
        return 0;

    strcpy(current_font, name);
    font_loaded = 0;

    return 0;
}

int get_char_vects(unsigned char achar, int *n, unsigned char **X,
                   unsigned char **Y)
{
    struct glyph *glyph;
    int i;

    if (!font_loaded)
        load_font();

    i = (int)achar - 040; /* translate achar to char# in font index */
    if (i <= 0 || i >= num_chars) {
        *n = 0;
        return 1;
    }

    glyph = &glyphs[fontmap[i]];

    *n = glyph->count;
    *X = &xcoords[glyph->offset];
    *Y = &ycoords[glyph->offset];

    return 0;
}