annotate nuklear_ui/sfnt.c @ 1984:0d5f88e53dca mame_interp

Fix merge error
author Michael Pavone <pavone@retrodev.com>
date Sun, 10 May 2020 00:16:00 -0700
parents 098c11aaf8f0
children 369a52e302e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1527
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <stddef.h>
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <string.h>
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <stdlib.h>
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include "sfnt.h"
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include "../util.h"
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 static uint32_t big32(uint8_t *src)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 uint32_t ret = *(src++) << 24;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 ret |= *(src++) << 16;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 ret |= *(src++) << 8;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 ret |= *src;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 return ret;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 static uint32_t big16(uint8_t *src)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 uint32_t ret = *(src++) << 8;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 ret |= *src;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 return ret;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 #define MIN_RESOURCE_MAP_SIZE (16 + 12 + 2 + 8)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 sfnt_container *load_sfnt(uint8_t *buffer, uint32_t size)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 if (size < 0x100) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 uint32_t sfnt_res_count, sfnt_res_offset, res_offset;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 uint8_t type;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 if (!memcmp(buffer, "true", 4) || !memcmp(buffer, "OTTO", 4) || !memcmp(buffer, "typ1", 4) || !memcmp(buffer, "\0\x01\0\0", 4)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 type = CONTAINER_TTF;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 } else if (!memcmp(buffer, "ttcf", 4)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 type = CONTAINER_TTC;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 } else {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 static uint8_t all_zeroes[16];
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 uint32_t resource_map_off = big32(buffer + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 if (resource_map_off + MIN_RESOURCE_MAP_SIZE > size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 //first 16 bytes of map should match header or be all zeroes
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 if (memcmp(buffer, buffer + resource_map_off, 16) && memcmp(all_zeroes, buffer + resource_map_off, 16)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 uint32_t type_start_off = resource_map_off + big16(buffer + resource_map_off + 24);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 if (type_start_off + sizeof(uint16_t) > size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 uint32_t num_types = 1 + big16(buffer + type_start_off);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 if (type_start_off + sizeof(uint16_t) + 8 * num_types > size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 res_offset = big32(buffer);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 if (res_offset > size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 uint8_t *cur = buffer + type_start_off + 2;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 sfnt_res_count = 0;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 for (uint32_t i = 0; i < num_types; i++, cur += 8)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 if (!memcmp("sfnt", cur, 4)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 sfnt_res_count = 1 + big16(cur + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 sfnt_res_offset = type_start_off + big16(cur + 6);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 if (sfnt_res_offset + sfnt_res_count * 12 > size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 type = CONTAINER_DFONT;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 if (!sfnt_res_count) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 //No "sfnt" resources in this dfont
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 sfnt_container *sfnt = calloc(1, sizeof(sfnt_container));
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 sfnt->blob = buffer;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 sfnt->size = size;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 sfnt->container_type = type;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 switch (type)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 case CONTAINER_TTF:
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 sfnt->num_fonts = 1;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 sfnt->tables = calloc(1, sizeof(sfnt_table));
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 sfnt->tables->container = sfnt;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
87 sfnt->tables->data = buffer + 0xC;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
88 sfnt->tables->num_entries = big16(buffer + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
89 sfnt->tables->offset = 0;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
90 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
91 case CONTAINER_TTC: {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
92 sfnt->num_fonts = big32(buffer+8);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
93 sfnt->tables = calloc(sfnt->num_fonts, sizeof(sfnt_table));
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
94 uint8_t *offsets = buffer + 0xC;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
95 for (int i = 0; i < sfnt->num_fonts; i++, offsets += sizeof(uint32_t))
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
96 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
97 uint32_t offset = big32(offsets);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
98 sfnt->tables[i].data = buffer + offset + 0xC;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
99 sfnt->tables[i].container = sfnt;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
100 sfnt->tables[i].num_entries = big16(buffer + offset + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
101 sfnt->tables[i].offset = 0;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
102 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
103 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
104 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
105 case CONTAINER_DFONT:{
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
106 sfnt->num_fonts = sfnt_res_count;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
107 sfnt->tables = calloc(sfnt->num_fonts, sizeof(sfnt_table));
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
108 uint8_t *cur = buffer + sfnt_res_offset;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
109 for (int i = 0; i < sfnt->num_fonts; i++, cur += 12)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
110 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
111 uint32_t offset = res_offset + (big32(cur + 4) & 0xFFFFFF);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
112 if (offset + 4 > size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
113 sfnt->tables[i].num_entries = 0;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
114 sfnt->tables[i].data = NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
115 continue;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
116 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 uint32_t res_size = big32(buffer + offset);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 if (offset + 4 + res_size > size || res_size < 0xC) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 sfnt->tables[i].num_entries = 0;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 sfnt->tables[i].data = NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 continue;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 sfnt->tables[i].container = sfnt;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 sfnt->tables[i].data = buffer + offset + 4 + 0xC;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 sfnt->tables[i].num_entries = big16(buffer + offset + 4 + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 sfnt->tables[i].offset = offset + 4;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
130 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
131 return sfnt;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
132 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
133
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
134 uint8_t *sfnt_find_table(sfnt_table *sfnt, char *table, uint32_t *size_out)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
135 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
136 uint8_t *entry = sfnt->data;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
137 for (int i = 0; i < sfnt->num_entries; i++, entry += 16)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
138 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
139 if (!strncmp(entry, table, 4)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
140 if (size_out) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
141 *size_out = big32(entry + 12);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
142 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
143 return sfnt->container->blob + sfnt->offset + big32(entry + 8);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
144 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
145 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
146 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
147 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
148
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
149 char *sfnt_name(sfnt_table *sfnt, uint16_t name_type)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
150 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
151 uint32_t name_size;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
152 uint8_t *name_table = sfnt_find_table(sfnt, "name", &name_size);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
153 if (!name_table) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
154 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
155 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
156 uint16_t num_names = big16(name_table + 2);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
157 if ((6 + num_names *12) > name_size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
158 //count is too big for the name table size, abort
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
159 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
160 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
161 uint8_t *entry = name_table + 6;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
162 uint16_t name_length = 0, name_offset;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
163 uint8_t *unicode_entry = NULL, *macroman_entry = NULL, *winunicode_entry = NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
164 for (uint16_t i = 0; i < num_names; i++, entry += 12)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
165 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
166 if (big16(entry + 6) != name_type) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
167 continue;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
168 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
169 uint16_t language_id = big16(entry + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
170 if (language_id >= 0x8000) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
171 //ingore language tag records
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 continue;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 uint16_t platform_id = big16(entry);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175 if (platform_id == 0) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
176 //prefer Unicode first
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
177 unicode_entry = entry;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
178 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
179 } else if (platform_id == 3 && big16(entry + 2) < 2) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
180 if (!winunicode_entry || (language_id & 0xFF) == 0x09) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
181 winunicode_entry = entry;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
182 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 } else if (platform_id == 1 && big16(entry + 2) == 0) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 if (!macroman_entry || (language_id == 0)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
185 macroman_entry = entry;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
186 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
187 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
188 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
189 entry = unicode_entry ? unicode_entry : winunicode_entry ? winunicode_entry : macroman_entry;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 if (entry) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191 name_length = big16(entry + 8);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 name_offset = big16(entry + 10);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 if (!name_length) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 uint32_t full_off = name_offset + big16(name_table + 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
198 if ((full_off + name_length) > name_size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
201 if (entry == macroman_entry) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 //TODO: convert these properly to UTF-8
1559
098c11aaf8f0 Fix silly bug in handling of Mac Roman font names. Make Mac font search more exhaustive if the faster prefix check fails. Added a bunch of debug printfs in case those aren't sufficient to get things working on other machines
Michael Pavone <pavone@retrodev.com>
parents: 1527
diff changeset
203 char *ret = malloc(name_length + 1);
1527
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 memcpy(ret, name_table + full_off, name_length);
1559
098c11aaf8f0 Fix silly bug in handling of Mac Roman font names. Make Mac font search more exhaustive if the faster prefix check fails. Added a bunch of debug printfs in case those aren't sufficient to get things working on other machines
Michael Pavone <pavone@retrodev.com>
parents: 1527
diff changeset
205 ret[name_length] = 0;
1527
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 return ret;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 } else {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
208 return utf16be_to_utf8(name_table + full_off, name_length/2);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
211
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
212 uint8_t *sfnt_flatten(sfnt_table *sfnt, uint32_t *size_out)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
213 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
214 uint8_t *ret = NULL;;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
215 sfnt_container *cont = sfnt->container;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
216 switch(cont->container_type)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
217 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
218 case CONTAINER_TTF:
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
219 ret = cont->blob;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
220 if (size_out) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
221 *size_out = cont->size;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
222 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
223 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
224 case CONTAINER_TTC:
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
225 memmove(cont->blob, sfnt->data - 0xC, 0xC + sfnt->num_entries * 12);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
226 ret = cont->blob;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
227 if (size_out) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
228 *size_out = cont->size;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
229 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
230 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
231 case CONTAINER_DFONT:{
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
232 uint8_t * start = sfnt->data - 0xC;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
233 uint32_t size = big32(start - 4);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
234 if (size + (start-cont->blob) > cont->size) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
235 size = cont->size - (start-cont->blob);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
236 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
237 ret = malloc(size);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
238 memcpy(ret, start, size);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
239 free(cont->blob);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
240 if (size_out) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
241 *size_out = size;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
242 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
243 break;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
244 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
245 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
246 free(cont->tables);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
247 free(cont);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
248 return ret;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
249 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
250
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
251 sfnt_table *sfnt_subfamily_by_names(sfnt_container *sfnt, const char **names)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
252 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
253 for (int i = 0; i < sfnt->num_fonts; i++)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
254 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
255 for (const char **name = names; *name; name++)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
256 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
257 char *font_subfam = sfnt_name(sfnt->tables + i, SFNT_SUBFAMILY);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
258 if (font_subfam && !strcasecmp(*name, font_subfam)) {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
259 free(font_subfam);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
260 return sfnt->tables + i;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
261 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
262 free(font_subfam);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
263 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
264 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
265 return NULL;
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
266 }
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
267
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
268 void sfnt_free(sfnt_container *sfnt)
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
269 {
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
270 free(sfnt->tables);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
271 free(sfnt->blob);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
272 free(sfnt);
4f6e8acd7b6a Added support for TTC and dfont format true type fonts. More robust font selection on Windows
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
273 }