summaryrefslogtreecommitdiff
path: root/Build/source/texk/ttfdump/libttf/font.c
blob: 474d4f008e48ff174929dbf9caa54db91d513d03 (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
/* font.c -- general font init and clean up codes
 * Copyright (C) 1996 Li-Da Lho, All right reserved  
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "ttf.h"
#include "ttfutil.h"
#include "protos.h"

#ifdef MEMCHECK
#include <dmalloc.h>
#endif

/* 	$Id: font.c,v 1.1.1.1 1998/06/05 07:47:52 robert Exp $	 */

#ifndef lint
static char vcid[] = "$Id: font.c,v 1.1.1.1 1998/06/05 07:47:52 robert Exp $";
#endif /* lint */

static void ttfInitInterpreter(TTFontPtr font);

TTFontPtr ttfInitFont(char *filename)
{
    TTFontPtr font;

    font = (TTFontPtr) calloc(1,sizeof(TTFont));

    font->ttfname = filename;
    if ((font->fp = fopen (filename,"r")) == NULL)
	{
	    fprintf(stderr,"Can't open ttf file %s\n",filename);
	    free(font);
	    return NULL;
	}
    
    ttfLoadFont(font, 0);
    return font;
}
void ttfLoadFont(TTFontPtr font, ULONG offset)
{
    if (fseek(font->fp,offset,SEEK_SET) != 0)
	ttfError("Fseek Failed\n");

    /* offset table */
    font->version = ttfGetFixed(font->fp);
    font->numTables = ttfGetUSHORT(font->fp);

    ttfInitTableDir(font,offset);
    
    ttfLoadRequiredTables(font);
    ttfLoadOptionalTables(font);

    ttfInitInterpreter(font);

    /* initialize the reference count to 1 */
    font->refcount = (int *) calloc(1, sizeof(int));
    *(font->refcount) = 1;
}
void ttfFreeFont(TTFontPtr font)
{
    ttfFreeRequiredTables(font);
    
    ttfFreeOptionalTables(font);
    
    ttfFreeTableDir(font->dir);
    free(font->refcount);
    free(font);
}

void ttfLoadRequiredTables(TTFontPtr font)
{
    ttfInitCMAP(font);
    ttfInitNAME(font);

    ttfInitMAXP(font);
    ttfInitHEAD(font);
    ttfInitLOCA(font);

    ttfInitGlyphCache(font);
    ttfInitGLYF(font);

    ttfInitHHEA(font);
    ttfInitHMTX(font);

    ttfInitPOST(font);
    ttfInitOS2(font);
}

void ttfFreeRequiredTables(TTFontPtr font)
{
    ttfFreeCMAP(font->cmap);
    ttfFreeNAME(font->name);

    ttfCleanUpGlyphCache(font);
    ttfFreeLOCA(font->loca);
    ttfFreeHEAD(font->head);
    ttfFreeMAXP(font->maxp);

    ttfFreeHMTX(font->hmtx);
    ttfFreeHHEA(font->hhea);
     
    ttfFreePOST(font->post);
    ttfFreeOS2(font->os2);
}

void ttfLoadOptionalTables(TTFontPtr font)
{
    ttfInitCVT(font);
    ttfInitFPGM(font);
    ttfInitGASP(font);
    ttfInitHDMX(font);
    ttfInitKERN(font);
    ttfInitPREP(font);
    ttfInitLTSH(font);
    ttfInitPCLT(font);
    ttfInitVDMX(font);
    ttfInitVHEA(font);
    ttfInitVMTX(font);
}
void ttfFreeOptionalTables(TTFontPtr font)
{
    ttfFreeCVT(font->cvt);
    ttfFreeFPGM(font->fpgm);
    ttfFreeGASP(font->gasp);
    ttfFreeHDMX(font->hdmx);
    ttfFreeKERN(font->kern);
    ttfFreePREP(font->prep);
    ttfFreeLTSH(font->ltsh);
    ttfFreePCLT(font->pclt);
    ttfFreeVDMX(font->vdmx);
    ttfFreeVHEA(font->vhea);
    ttfFreeVMTX(font->vmtx);
}

/* make a clone of the origional font
 * This is use for fonts that support more than one encoding scheme
 *
 * Problems: 
 *       1. which of those members can be shared between two instances
 *       2. How can we free a font safely
 */
TTFontPtr ttfCloneFont(TTFontPtr font)
{
    TTFontPtr newfont;

    newfont = (TTFontPtr) calloc(1,sizeof(TTFont));
    
    memcpy(newfont, font, sizeof(TTFont));
    newfont->refcount += 1;

    return newfont;
}
static void ttfInitInterpreter(TTFontPtr font)
{
    ttfInitStorageArea(font);
    ttfInitStack(font);
    ttfInitGraphicsState(font);    
}