add explicit freetype2 linking
[charfbuzz.git] / hb-open-file.c
blob18b89705a885521c6d8d08448eac1f65395a3a0d
1 /*
2 Port from c++ is protected by a GNU Lesser GPLv3
3 Copyright © 2013 Sylvain BERTRAND <sylvain.bertrand@gmail.com>
4 <sylware@legeek.net>
5 */
6 /*XXX:have to go*/
7 #define _GNU_SOURCE
8 #include <endian.h>
9 #include <stdint.h>
10 #include <stddef.h>
12 #include "hb.h"
13 #include "hb-private.h"
14 #include "hb-open-file-private.h"
16 static struct ot_fnt_face *ttc_hdr_ver1_get_face(struct ttc_hdr_ver1
17 *ttc_hdr_ver1, unsigned i)
19 uint32_t of;
20 uint8_t *base; /*the beginning of the file */
22 if (!ttc_hdr_ver1)
23 return NULL;
25 of = be32toh(ttc_hdr_ver1->of_tbls_ofs[i]);
26 base = (uint8_t *) ttc_hdr_ver1;
27 return (struct ot_fnt_face *)(base + of);
30 static struct ot_fnt_face *ttc_hdr_get_face(struct ttc_hdr *ttc_hdr, unsigned i)
32 if (!ttc_hdr)
33 return NULL;
35 switch (ttc_hdr->u.hdr.ver.major) {
36 case 2: /*version 2 is compatible with version 1 */
37 case 1:
38 return ttc_hdr_ver1_get_face(&ttc_hdr->u.ver1, i);
39 default:
40 return NULL;
44 struct ot_fnt_face *ot_fnt_file_get_face(struct ot_fnt_file *ot_fnt_file,
45 unsigned i)
47 uint32_t tag;
49 if (!ot_fnt_file)
50 return NULL;
52 tag = be32toh(ot_fnt_file->u.tag);
54 switch (tag) {
55 /*Note: for non-collection SFNT data we ignore index. This is because
56 Apple dfont container is a container of SFNT's. So each SFNT is a
57 non-TTC, but the index is more than zero. */
58 case CFF_TAG: /*All the non-collection tags */
59 case TRUE_TAG:
60 case TYP1_TAG:
61 case TRUETYPE_TAG:
62 return &ot_fnt_file->u.fnt_face;
63 case TTC_TAG:
64 return ttc_hdr_get_face(&ot_fnt_file->u.ttc_hdr, i);
65 default:
66 return NULL;
70 static struct tbl_rec *ot_fnt_face_get_tbl(struct ot_fnt_face *ot_fnt_face,
71 unsigned i)
73 /*XXX:check is useless in known code paths */
74 if (!ot_fnt_face)
75 return NULL;
77 if (i >= ot_fnt_face->tbls_n)
78 return NULL; /*XXX:original code use a "null" object */
79 return &ot_fnt_face->tbls[i];
82 static hb_bool_t ot_fnt_face_find_tbl_idx(struct ot_fnt_face *ot_fnt_face,
83 hb_tag_t tag, unsigned *tbl_idx)
85 unsigned cnt;
86 unsigned i;
88 /*XXX:check is useless in known code paths */
89 if (!ot_fnt_face)
90 return FALSE;
92 cnt = ot_fnt_face->tbls_n;
93 for (i = 0; i < cnt; ++i) {
94 uint32_t tbl_tag;
96 tbl_tag = be32toh(ot_fnt_face->tbls[i].tag);
97 if (tag == tbl_tag) {
98 if (tbl_idx)
99 *tbl_idx = i;
100 return TRUE;
103 if (tbl_idx)
104 *tbl_idx = IDX_NOT_FOUND;
105 return FALSE;
108 struct tbl_rec *ot_fnt_face_get_tbl_by_tag(struct ot_fnt_face *ot_fnt_face,
109 hb_tag_t tag)
111 unsigned tbl_idx;
113 if (!ot_fnt_face)
114 return NULL;
116 ot_fnt_face_find_tbl_idx(ot_fnt_face, tag, &tbl_idx);
117 return ot_fnt_face_get_tbl(ot_fnt_face, tbl_idx);