Debian Codegraph @

texk/ttfdump/libttf/font.c

raw
1 /* font.c -- general font init and clean up codes
2 * Copyright (C) 1996 Li-Da Lho, All right reserved
3 */
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "ttf.h"
12 #include "ttfutil.h"
13 #include "protos.h"
14
15 /* $Id: font.c,v 1.1.1.1 1998/06/05 07:47:52 robert Exp $ */
16
17 static void ttfInitInterpreter(TTFontPtr font);
18
19 TTFontPtr ttfInitFont(char *filename)
20 {
21 TTFontPtr font;
22
23 font = XCALLOC1 (TTFont);
24
25 font->ttfname = filename;
26 if ((font->fp = fopen_truetype (filename)) == NULL)
27 {
28 fprintf(stderr,"Can't open ttf file %s\n",filename);
29 free(font);
30 return NULL;
31 }
32
33 ttfLoadFont(font, 0);
34 return font;
35 }
36 void ttfLoadFont(TTFontPtr font, ULONG offset)
37 {
38 xfseek(font->fp, offset, SEEK_SET, "ttfLoadFont");
39
40 /* offset table */
41 font->version = ttfGetFixed(font->fp);
42 font->numTables = ttfGetUSHORT(font->fp);
43
44 ttfInitTableDir(font,offset);
45
46 ttfLoadRequiredTables(font);
47 ttfLoadOptionalTables(font);
48 ttfLoadOpenTypeTables(font);
49
50 ttfInitInterpreter(font);
51
52 /* initialize the reference count to 1 */
53 font->refcount = XCALLOC1 (int);
54 *(font->refcount) = 1;
55 }
56 void ttfFreeFont(TTFontPtr font)
57 {
58 ttfFreeRequiredTables(font);
59
60 ttfFreeOptionalTables(font);
61
62 ttfFreeOpenTypeTables(font);
63
64 ttfFreeTableDir(font->dir);
65 free(font->refcount);
66 free(font);
67 }
68
69 void ttfLoadRequiredTables(TTFontPtr font)
70 {
71 ttfInitCMAP(font);
72 ttfInitNAME(font);
73
74 ttfInitMAXP(font);
75 ttfInitHEAD(font);
76
77 ttfInitGlyphCache(font);
78 ttfInitGLYF(font);
79
80 ttfInitHHEA(font);
81 ttfInitHMTX(font);
82
83 ttfInitPOST(font);
84 ttfInitOS2(font);
85 }
86
87 void ttfFreeRequiredTables(TTFontPtr font)
88 {
89 ttfFreeCMAP(font->cmap);
90 ttfFreeNAME(font->name);
91
92 ttfCleanUpGlyphCache(font);
93 ttfFreeHEAD(font->head);
94 ttfFreeMAXP(font->maxp);
95
96 ttfFreeHMTX(font->hmtx);
97 ttfFreeHHEA(font->hhea);
98
99 ttfFreePOST(font->post);
100 ttfFreeOS2(font->os2);
101 }
102
103 void ttfLoadOptionalTables(TTFontPtr font)
104 {
105 ttfInitCVT(font);
106 ttfInitFPGM(font);
107 ttfInitGASP(font);
108 ttfInitHDMX(font);
109 ttfInitKERN(font);
110 ttfInitLOCA(font);
111 ttfInitPREP(font);
112 ttfInitLTSH(font);
113 ttfInitPCLT(font);
114 ttfInitVDMX(font);
115 ttfInitVHEA(font);
116 ttfInitVMTX(font);
117 }
118 void ttfFreeOptionalTables(TTFontPtr font)
119 {
120 ttfFreeCVT(font->cvt);
121 ttfFreeFPGM(font->fpgm);
122 ttfFreeGASP(font->gasp);
123 ttfFreeHDMX(font->hdmx);
124 ttfFreeKERN(font->kern);
125 ttfFreeLOCA(font->loca);
126 ttfFreePREP(font->prep);
127 ttfFreeLTSH(font->ltsh);
128 ttfFreePCLT(font->pclt);
129 ttfFreeVDMX(font->vdmx);
130 ttfFreeVHEA(font->vhea);
131 ttfFreeVMTX(font->vmtx);
132 }
133
134 void ttfLoadOpenTypeTables(TTFontPtr font)
135 {
136 ttfInitGPOS(font);
137 ttfInitGSUB(font);
138 }
139 void ttfFreeOpenTypeTables(TTFontPtr font)
140 {
141 ttfFreeGPOS(font->gpos);
142 ttfFreeGSUB(font->gsub);
143 }
144
145 #if 0
146 /* Not used */
147 /* make a clone of the origional font
148 * This is use for fonts that support more than one encoding scheme
149 *
150 * Problems:
151 * 1. which of those members can be shared between two instances
152 * 2. How can we free a font safely
153 */
154 TTFontPtr ttfCloneFont(TTFontPtr font)
155 {
156 TTFontPtr newfont;
157
158 newfont = XTALLOC1 (TTFont);
159
160 memcpy(newfont, font, sizeof(TTFont));
161 newfont->refcount += 1;
162
163 return newfont;
164 }
165 #endif
166
167 static void ttfInitInterpreter(TTFontPtr font)
168 {
169 ttfInitStorageArea(font);
170 ttfInitStack(font);
171 ttfInitGraphicsState(font);
172 }