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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#include <lib9.h>
#include <kernel.h>
#include "interp.h"
#include "isa.h"
#include "runt.h"
#include "raise.h"
#include "freetypemod.h"
#include "freetype.h"
typedef struct Face Face;
struct Face {
Freetype_Face freetypeface; /* limbo part */
FTface ftface; /* private parts */
};
Type* TMatrix;
Type* TVector;
Type* TFace;
Type* TGlyph;
static uchar Matrixmap[] = Freetype_Matrix_map;
static uchar Vectormap[] = Freetype_Vector_map;
static uchar Facemap[] = Freetype_Face_map;
static uchar Glyphmap[] = Freetype_Glyph_map;
static void freeface(Heap*, int);
static Face* ckface(Freetype_Face*);
void
freetypemodinit(void)
{
builtinmod("$Freetype", Freetypemodtab, Freetypemodlen);
TMatrix = dtype(freeheap, sizeof(Freetype_Matrix), Matrixmap, sizeof(Matrixmap));
TVector = dtype(freeheap, sizeof(Freetype_Vector), Vectormap, sizeof(Vectormap));
TFace = dtype(freeface, sizeof(Face), Facemap, sizeof(Facemap));
TGlyph = dtype(freeheap, sizeof(Freetype_Glyph), Glyphmap, sizeof(Glyphmap));
}
void
Face_haschar(void *fp)
{
F_Face_haschar *f = fp;
Face *face;
*f->ret = 0;
face = ckface(f->face);
release();
*f->ret = fthaschar(face->ftface, f->c);
acquire();
}
void
Face_loadglyph(void *fp)
{
F_Face_loadglyph *f = fp;
Heap *h;
Face *face;
Freetype_Glyph *g;
FTglyph ftg;
int n, i, s1bpr, s2bpr;
char *err;
face = ckface(f->face);
destroy(*f->ret);
*f->ret = H;
release();
err = ftloadglyph(face->ftface, f->c, &ftg);
acquire();
if (err != nil) {
kwerrstr(err);
return;
}
h = heap(TGlyph);
if (h == H) {
kwerrstr(exNomem);
return;
}
g = H2D(Freetype_Glyph*, h);
n = ftg.width*ftg.height;
h = heaparray(&Tbyte, n);
if (h == H) {
destroy(g);
kwerrstr(exNomem);
return;
}
g->bitmap = H2D(Array*, h);
g->top = ftg.top;
g->left = ftg.left;
g->height = ftg.height;
g->width = ftg.width;
g->advance.x = ftg.advx;
g->advance.y = ftg.advy;
s1bpr = ftg.width;
s2bpr = ftg.bpr;
for (i = 0; i < ftg.height; i++)
memcpy(g->bitmap->data+(i*s1bpr), ftg.bitmap+(i*s2bpr), s1bpr);
*f->ret = g;
}
void
Freetype_newface(void *fp)
{
F_Freetype_newface *f = fp;
Heap *h;
Face *face;
Freetype_Face *limboface;
FTfaceinfo finfo;
char *path;
char *err;
destroy(*f->ret);
*f->ret = H;
h = heapz(TFace);
if (h == H) {
kwerrstr(exNomem);
return;
}
face = H2D(Face*, h);
limboface = (Freetype_Face*)face;
*f->ret = limboface;
path = strdup(string2c(f->path)); /* string2c() can call error() */
release();
err = ftnewface(path, f->index, &face->ftface, &finfo);
acquire();
free(path);
if (err != nil) {
*f->ret = H;
destroy(face);
kwerrstr(err);
return;
}
limboface->nfaces = finfo.nfaces;
limboface->index = finfo.index;
limboface->style = finfo.style;
limboface->height = finfo.height;
limboface->ascent = finfo.ascent;
limboface->familyname = c2string(finfo.familyname, strlen(finfo.familyname));
limboface->stylename = c2string(finfo.stylename, strlen(finfo.stylename));
*f->ret = limboface;
}
void
Freetype_newmemface(void *fp)
{
F_Freetype_newmemface *f = fp;
destroy(*f->ret);
*f->ret = H;
kwerrstr("not implemented");
}
void
Face_setcharsize(void *fp)
{
F_Face_setcharsize *f = fp;
Face *face;
Freetype_Face *limboface;
FTfaceinfo finfo;
char *err;
face = ckface(f->face);
limboface = (Freetype_Face*)face;
release();
err = ftsetcharsize(face->ftface, f->pts, f->hdpi, f->vdpi, &finfo);
acquire();
if (err == nil) {
limboface->height = finfo.height;
limboface->ascent = finfo.ascent;
}
retstr(err, f->ret);
}
void
Face_settransform(void *fp)
{
F_Face_settransform *f = fp;
FTmatrix *m = nil;
FTvector *v = nil;
Face *face;
face = ckface(f->face);
/*
* ftsettransform() has no error return
* we have one for consistency - but always nil for now
*/
destroy(*f->ret);
*f->ret = H;
if (f->m != H)
m = (FTmatrix*)(f->m);
if (f->v != H)
v = (FTvector*)(f->v);
release();
ftsettransform(face->ftface, m, v);
acquire();
}
static void
freeface(Heap *h, int swept)
{
Face *face = H2D(Face*, h);
if (!swept) {
destroy(face->freetypeface.familyname);
destroy(face->freetypeface.stylename);
}
release();
ftdoneface(face->ftface);
acquire();
memset(&face->ftface, 0, sizeof(face->ftface));
}
static Face*
ckface(Freetype_Face *face)
{
if (face == nil || face == H)
error("nil Face");
if (D2H(face)->t != TFace)
error(exType);
return (Face*)face;
}
|