summaryrefslogtreecommitdiff
path: root/app/fonttosfnt/struct.c
blob: 2d346e6059b5124841518e28ba7d78fa5f154d1b (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
/*
Copyright (c) 2002-2003 by Juliusz Chroboczek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/* $XFree86: xc/programs/fonttosfnt/struct.c,v 1.3 2003/10/24 20:38:11 tsi Exp $ */

#include <stdlib.h>
#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "fonttosfnt.h"

FontPtr
makeFont()
{
    FontPtr font;

    font = malloc(sizeof(FontRec));
    if(font == NULL)
        return NULL;

    font->numNames = 0;
    font->names = NULL;
    font->flags = 0;
    font->weight = 500;
    font->width = 5;
    font->italicAngle = 0;
    font->underlinePosition = - TWO_SIXTEENTH;
    font->underlineThickness = TWO_SIXTEENTH;
    font->foundry = makeName("UNKN");
    font->strikes = NULL;
    return font;
}

StrikePtr
makeStrike(FontPtr font, int sizeX, int sizeY)
{
    StrikePtr strike, last_strike;

    strike = font->strikes;
    last_strike = NULL;
    while(strike) {
        if(strike->sizeX == sizeX && strike->sizeY == sizeY)
            return strike;
        last_strike = strike;
        strike = strike->next;
    }

    strike = malloc(sizeof(StrikeRec));
    if(strike == NULL)
        return NULL;
    strike->sizeX = sizeX;
    strike->sizeY = sizeY;
    strike->bitmaps = 
        calloc(FONT_CODES / FONT_SEGMENT_SIZE, sizeof(BitmapPtr*));
    if(strike->bitmaps == NULL) {
        free(strike);
        return NULL;
    }
    strike->numSbits = 0;
    strike->next = NULL;
    strike->bitmapSizeTableLocation = 0xDEADFACE;
    strike->indexSubTables = NULL;
    if(last_strike)
        last_strike->next = strike;
    else
        font->strikes = strike;
    return strike;
}

BitmapPtr
makeBitmap(StrikePtr strike, int code,
           int advanceWidth, int horiBearingX, int horiBearingY,
           int width, int height, int stride, unsigned char *raster, int crop)
{
    BitmapPtr bitmap;
    int i, j, x, y;
    int dx, dy, new_width, new_height;

    bitmap = malloc(sizeof(BitmapRec));
    if(bitmap == NULL) 
        return NULL;

    bitmap->index = -1;
    bitmap->width = 0;
    bitmap->height = 0;
    bitmap->stride = 0;
    bitmap->raster = NULL;
    bitmap->location = 0xDEADFACE;

    i = code / FONT_SEGMENT_SIZE;
    j = code % FONT_SEGMENT_SIZE;

    if(strike->bitmaps[i] == NULL) {
        strike->bitmaps[i] = calloc(FONT_SEGMENT_SIZE, sizeof(BitmapPtr));
    }
    if(strike->bitmaps[i] == NULL) {
        free(bitmap);
        return NULL;
    }
    if(strike->bitmaps[i][j] != NULL) {
        if(verbose_flag)
            fprintf(stderr, "Duplicate bitmap %d.\n", code);
        free(bitmap);
        return strike->bitmaps[i][j];
    }

    dx = 0;
    dy = 0;
    new_width = width;
    new_height = height;

    if(crop) {
        int empty;
        while(new_width > 0) {
            empty = 1;
            x = new_width - 1;
            for(y = 0; y < new_height; y++) {
                if(BITREF(raster, stride, x + dx, y + dy)) {
                    empty = 0;
                    break;
                }
            }
            if(empty)
                new_width--;
            else
                break;
        }
        while(new_height > 0) {
            empty = 1;
            y = new_height - 1;
            for(x = 0; x < new_width; x++) {
                if(BITREF(raster, stride, x + dx, y + dy)) {
                    empty = 0;
                    break;
                }
            }
            if(empty)
                new_height--;
            else
                break;
        }
        while(new_width > 0) {
            empty = 1;
            x = 0;
            for(y = 0; y < new_height; y++) {
                if(BITREF(raster, stride, x + dx, y + dy)) {
                    empty = 0;
                    break;
                }
            }
            if(empty) {
                dx++;
                new_width--;
            } else
                break;
        }
        while(new_height > 0) {
            empty = 1;
            y = 0;
            for(x = 0; x < new_width; x++) {
                if(BITREF(raster, stride, x + dx, y + dy)) {
                    empty = 0;
                    break;
                }
            }
            if(empty) {
                dy++;
                new_height--;
            } else
                break;
        }
    }


    bitmap->advanceWidth = advanceWidth;
    bitmap->horiBearingX = horiBearingX + dx;
    bitmap->horiBearingY = horiBearingY - dy;
    bitmap->width = new_width;
    bitmap->height = new_height;
    bitmap->stride = (new_width + 7) / 8;

    bitmap->raster = malloc(bitmap->height * bitmap->stride);
    if(bitmap->raster == NULL) {
        free(bitmap);
        return NULL;
    }
    memset(bitmap->raster, 0, bitmap->height * bitmap->stride);
    for(y = 0; y < new_height; y++) {
        for(x = 0; x < new_width; x++) {
            if(BITREF(raster, stride, x + dx, y + dy))
                bitmap->raster[y * bitmap->stride + x / 8] |=
                    1 << (7 - (x % 8));
        }
    }
    strike->bitmaps[i][j] = bitmap;
    strike->numSbits++;

    return bitmap;
}

IndexSubTablePtr
makeIndexSubTables(StrikePtr strike, CmapPtr cmap)
{
    IndexSubTablePtr table, first, last;
    BitmapPtr bitmap0, bitmap;
    int index, n;

    first = NULL;
    last = NULL;

    /* Assuming that we're writing bit-aligned data, small metrics
       and short offsets, a constant metrics segment saves 5 bytes 
       per glyph in the EBDT table, and 2 bytes per glyph in the EBLC
       table.  On the other hand, the overhead for a supplementary
       type 2 indexSubTable is 8 bytes for the indexSubTableArray
       entry and 20 bytes for the subtable itself.  It's worth
       splitting at 5 glyphs.  There's no analogue of a type 2
       indexSubTable with byte-aligned data, so we don't bother
       splitting when byte-aligning. */
    index = 0;
    while(index < 0xFFFF) {
        int constantMetrics = 1;
        bitmap0 = strikeBitmapIndex(strike, cmap, index);
        if(bitmap0 == NULL) {
            index++;
            continue;
        }
        n = 1;
        while((bitmap = strikeBitmapIndex(strike, cmap, index + n)) != NULL) {
            if(constantMetrics) {
                if(!SAME_METRICS(bitmap0, bitmap)) {
                    if(bit_aligned_flag && n >= 4)
                        break;
                    else
                        constantMetrics = 0;
                }
            } else if(bit_aligned_flag) {
                BitmapPtr b1 = strikeBitmapIndex(strike, cmap, index + n + 1);
                BitmapPtr b2 = strikeBitmapIndex(strike, cmap, index + n + 2);
                BitmapPtr b3 = strikeBitmapIndex(strike, cmap, index + n + 3);
                BitmapPtr b4 = strikeBitmapIndex(strike, cmap, index + n + 4);
                if(b1 && b2 && b3 && b4 &&
                   SAME_METRICS(bitmap, b1) && 
                   SAME_METRICS(bitmap, b2) && 
                   SAME_METRICS(bitmap, b3) && 
                   SAME_METRICS(bitmap, b4)) {
                    break;
                }
            }
            n++;
        }
        if(n <= 1)
            constantMetrics = 0;

        table = malloc(sizeof(IndexSubTableRec));
        table->firstGlyphIndex = index;
        table->lastGlyphIndex = index + n - 1;
        table->constantMetrics = constantMetrics;
        table->location = 0xDEADFACE;
        table->lastLocation = 0xDEADFACE;
        table->next = NULL;

        if(first == NULL) {
            first = table;
            last = table;
        } else {
            last->next = table;
            last = table;
        }
        index += n;
    }
    return first;
}

int
fontIndex(FontPtr font, int code)
{
    StrikePtr strike;
    BitmapPtr bitmap;

    if(code == 0)
        return 0;
    strike = font->strikes;
    while(strike) {
        bitmap = STRIKE_BITMAP(strike, code);
        if(bitmap)
            return bitmap->index;
        strike = strike->next;
    }
    return -1;
}

CmapPtr
makeCmap(FontPtr font)
{
    CmapPtr cmap_head = NULL;
    CmapPtr cmap_last = NULL;
    CmapPtr cmap;
    int code, i, index, maxindex = 0;

    code = 0;
    while(code < FONT_CODES) {
        index = fontIndex(font, code);
        if(index < 0) {
            code++;
            continue;
        }
        i = 1;
        while(code + i < FONT_CODES && 
              fontIndex(font, code + i) == index + i) {
            i++;
        }
        cmap = malloc(sizeof(CmapRec));
        if(cmap == NULL)
            return NULL;
        cmap->startCode = code;
        cmap->endCode = code + i - 1;
        cmap->index = index;
        cmap->next = NULL;
        cmap->maxindex = 0;
        if(maxindex < index + i - 1)
            maxindex = index + i - 1;
        if(cmap_head == NULL)
            cmap_head = cmap;
        else
            cmap_last->next = cmap;
        cmap_last = cmap;

        code += i;
    }
    cmap_head->maxindex = maxindex;
    cmap_head->inverse = calloc(maxindex + 1, sizeof(int));
    cmap = cmap_head;
    while(cmap) {
        for(i = cmap->index; 
            i <= cmap->endCode - cmap->startCode + cmap->index; i++) {
            cmap_head->inverse[i] =
                i - cmap->index + cmap->startCode;
        }
        cmap = cmap->next;
    }

    return cmap_head;
}

int
findIndex(CmapPtr cmap_head, int code)
{
    CmapPtr cmap;
    cmap = cmap_head;
    while(cmap) {
        if(cmap->endCode > code)
            return -1;
        if(cmap->startCode <= code)
            return cmap->index + code - cmap->startCode;
        cmap = cmap->next;
    }
    return -1;
}

int
findCode(CmapPtr cmap_head, int index)
{
    if(index < 0 || index > cmap_head->maxindex)
        return -1;
    return cmap_head->inverse[index];

}

int
maxIndex(CmapPtr cmap_head)
{
    return cmap_head->maxindex;
}

BitmapPtr
strikeBitmapIndex(StrikePtr strike, CmapPtr cmap, int index)
{
    int code = findCode(cmap, index);
    if(code < 0)
        return NULL;

    return STRIKE_BITMAP(strike, code);
}

void
strikeMetrics(StrikePtr strike,
              int *width_max_return, 
              int *x_min_return, int *y_min_return,
              int *x_max_return, int *y_max_return)
{
    BitmapPtr bitmap;
    int i;
    int width_max = 0;
    int x_min = 10000;
    int y_min = 10000;
    int x_max = -10000;
    int y_max = -10000;

    for(i = 0; i < FONT_CODES; i++) {
        bitmap = STRIKE_BITMAP(strike, i);
        if(!bitmap)
            continue;
        if(bitmap->advanceWidth > width_max)
            width_max = bitmap->advanceWidth;
        if(bitmap->horiBearingX < x_min)
            x_min = bitmap->horiBearingX;
        if(bitmap->horiBearingY > y_max)
            y_max = bitmap->horiBearingY;
        if(bitmap->horiBearingX + bitmap->width > x_max)
            x_max = bitmap->horiBearingX + bitmap->width;
        if(bitmap->horiBearingY - bitmap->height < y_min)
            y_min = bitmap->horiBearingY - bitmap->height;
    }

    if(width_max_return) *width_max_return = width_max;
    if(x_min_return) *x_min_return = x_min;
    if(y_min_return) *y_min_return = y_min;
    if(x_max_return) *x_max_return = x_max;
    if(y_max_return) *y_max_return = y_max;
}

int
glyphMetrics(FontPtr font, int code,
             int *width_return,
             int *x_min_return, int *y_min_return,
             int *x_max_return, int *y_max_return)
{
    StrikePtr strike;
    BitmapPtr bitmap;

    strike = font->strikes;
    while(strike) {
        bitmap = STRIKE_BITMAP(strike, code);
        if(bitmap) {
            if(width_return)
                *width_return = 
                    (((float)bitmap->advanceWidth + 0.5) / strike->sizeX) *
                    TWO_SIXTEENTH;
            if(x_min_return)
                *x_min_return =
                    ((float)bitmap->horiBearingX / strike->sizeX) * 
                    TWO_SIXTEENTH;
            if(y_min_return)
                *y_min_return =
                    (((float)bitmap->horiBearingY - bitmap->height) 
                     / strike->sizeY) * TWO_SIXTEENTH;
            /* For the following two, 0.9 instead of 0.5 might make
               more sense.  However, using different rounding rules
               for x_max and awidth causes problems for detecting
               charcell fonts. */
            if(x_max_return)
                *x_max_return =
                    (((float)bitmap->horiBearingX + bitmap->width + 0.5)
                     / strike->sizeX) * TWO_SIXTEENTH;
            if(y_max_return)
                *y_max_return =
                    (((float)bitmap->horiBearingY + 0.5) / strike->sizeY) *
                    TWO_SIXTEENTH;
            return 1;
        }
        strike = strike->next;
    }

    return -1;
}

void
fontMetrics(FontPtr font,
            int *max_awidth_return,
            int *min_x_return, int *min_y_return,
            int *max_x_return, int *max_y_return)
{
    int i, rc;
    int max_awidth = 0;
    int min_x = 10000 << 16, min_y = 10000 << 16;
    int max_x = -10000 << 16, max_y = -10000 << 16;
    for(i = 0; i < FONT_CODES; i++) {
        int awidth, x0, y0, x1, y1;
        rc = glyphMetrics(font, i, &awidth, &x0, &y0, &x1, &y1);
        if(rc < 0)
            continue;
        if(awidth > max_awidth)
            max_awidth = awidth;
        if(x0 < min_x) min_x = x0;
        if(y0 < min_y) min_y = y0;
        if(x1 > max_x) max_x = x1;
        if(y1 > max_y) max_y = y1;
    }
    if(max_awidth_return) *max_awidth_return = max_awidth;
    if(min_x_return) *min_x_return = min_x;
    if(min_y_return) *min_y_return = min_y;
    if(max_x_return) *max_x_return = max_x;
    if(max_y_return) *max_y_return = max_y;
}