| 1 |
#include "support/util.h" |
| 2 |
#include "otfcc/sfnt.h" |
| 3 |
|
| 4 |
#define OTFCC_CHR(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) ) |
| 5 |
|
| 6 |
static void otfcc_read_packets(otfcc_SplineFontContainer *font, FILE *file) { |
| 7 |
for (uint32_t count = 0; count < font->count; count++) { |
| 8 |
(void)fseek(file, font->offsets[count], SEEK_SET); |
| 9 |
|
| 10 |
font->packets[count].sfnt_version = otfcc_get32u(file); |
| 11 |
font->packets[count].numTables = otfcc_get16u(file); |
| 12 |
font->packets[count].searchRange = otfcc_get16u(file); |
| 13 |
font->packets[count].entrySelector = otfcc_get16u(file); |
| 14 |
font->packets[count].rangeShift = otfcc_get16u(file); |
| 15 |
NEW(font->packets[count].pieces, font->packets[count].numTables); |
| 16 |
|
| 17 |
for (uint32_t i = 0; i < font->packets[count].numTables; i++) { |
| 18 |
font->packets[count].pieces[i].tag = otfcc_get32u(file); |
| 19 |
font->packets[count].pieces[i].checkSum = otfcc_get32u(file); |
| 20 |
font->packets[count].pieces[i].offset = otfcc_get32u(file); |
| 21 |
font->packets[count].pieces[i].length = otfcc_get32u(file); |
| 22 |
NEW(font->packets[count].pieces[i].data, font->packets[count].pieces[i].length); |
| 23 |
} |
| 24 |
|
| 25 |
for (uint32_t i = 0; i < font->packets[0].numTables; i++) { |
| 26 |
(void)fseek(file, font->packets[count].pieces[i].offset, SEEK_SET); |
| 27 |
(void)fread(font->packets[count].pieces[i].data, font->packets[count].pieces[i].length, |
| 28 |
1, file); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
otfcc_SplineFontContainer *otfcc_readSFNT(FILE *file) { |
| 34 |
if (!file) return NULL; |
| 35 |
otfcc_SplineFontContainer *font; |
| 36 |
NEW(font); |
| 37 |
|
| 38 |
font->type = otfcc_get32u(file); |
| 39 |
|
| 40 |
switch (font->type) { |
| 41 |
case OTFCC_CHR('O','T','T','O'): |
| 42 |
case 0x00010000: |
| 43 |
case OTFCC_CHR('t','r','u','e'): |
| 44 |
case OTFCC_CHR('t','y','p','1'): |
| 45 |
font->count = 1; |
| 46 |
NEW(font->offsets, font->count); |
| 47 |
NEW(font->packets, font->count); |
| 48 |
font->offsets[0] = 0; |
| 49 |
otfcc_read_packets(font, file); |
| 50 |
break; |
| 51 |
|
| 52 |
case OTFCC_CHR('t','t','c','f'): |
| 53 |
(void)otfcc_get32u(file); |
| 54 |
font->count = otfcc_get32u(file); |
| 55 |
NEW(font->offsets, font->count); |
| 56 |
NEW(font->packets, font->count); |
| 57 |
|
| 58 |
for (uint32_t i = 0; i < font->count; i++) { |
| 59 |
font->offsets[i] = otfcc_get32u(file); |
| 60 |
} |
| 61 |
|
| 62 |
otfcc_read_packets(font, file); |
| 63 |
break; |
| 64 |
|
| 65 |
default: |
| 66 |
font->count = 0; |
| 67 |
font->offsets = NULL; |
| 68 |
font->packets = NULL; |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
fclose(file); |
| 73 |
|
| 74 |
return font; |
| 75 |
} |
| 76 |
|
| 77 |
void otfcc_deleteSFNT(otfcc_SplineFontContainer *font) { |
| 78 |
if (!font) return; |
| 79 |
if (font->count > 0) { |
| 80 |
for (uint32_t count = 0; count < font->count; count++) { |
| 81 |
for (int i = 0; i < font->packets[count].numTables; i++) { |
| 82 |
FREE(font->packets[count].pieces[i].data); |
| 83 |
} |
| 84 |
FREE(font->packets[count].pieces); |
| 85 |
} |
| 86 |
FREE(font->packets); |
| 87 |
} |
| 88 |
FREE(font->offsets); |
| 89 |
FREE(font); |
| 90 |
} |