aboutsummaryrefslogtreecommitdiff
path: root/libfreetype/freetype.c
blob: ccbd7c01d99bffc106c84c7db4a0d7bf2697f0be (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
#include "freetype/freetype.h"
#include "freetype.h"

static char* fterrstr(int);

#ifdef EXT_WIN
extern char rootdir[];
#endif

char*
ftnewface(char *path, int index, FTface *f, FTfaceinfo *finfo)
{
	FT_Library ft_lib;
	FT_Face ft_face;
	char *err;
#ifdef EXT_WIN
	char *path2;
#endif

	err = fterrstr(FT_Init_FreeType(&ft_lib));
	if (err != nil)
		return err;

#ifdef EXT_WIN
	path2 = malloc(strlen(rootdir) + 1 + strlen(path) + 1);
	sprintf(path2, "%s%s", rootdir, path);
	path = path2;
#endif
	
	err = fterrstr(FT_New_Face(ft_lib, path, index, &ft_face));

#ifdef EXT_WIN
	free(path2);
#endif

	if (err != nil) {
		FT_Done_FreeType(ft_lib);
		return err;
	}

	f->ft_lib = ft_lib;
	f->ft_face = ft_face;
	finfo->nfaces = ft_face->num_faces;
	finfo->index = ft_face->face_index;
	finfo->style = ft_face->style_flags;
	finfo->height = (FT_MulFix(ft_face->height, ft_face->size->metrics.y_scale)+32)/64;
	finfo->ascent = (FT_MulFix(ft_face->ascender, ft_face->size->metrics.y_scale)+32)/64;
	finfo->familyname = ft_face->family_name;
	finfo->stylename = ft_face->style_name;

	finfo->num_glyphs = ft_face->num_glyphs;
	
	return nil;
}

char*
ftloadmemface(void *buf, int nbytes, int index, FTface *f, FTfaceinfo *finfo)
{
	USED(buf);
	USED(f);
	USED(finfo);
	return "not implemented";
}

char*
ftsetcharsize(FTface f, int pt, int hdpi, int vdpi, FTfaceinfo *finfo)
{
	FT_Face ft_face = f.ft_face;
	char *err;

	err = fterrstr(FT_Set_Char_Size(ft_face, 0, pt, hdpi, vdpi));
	if (err != nil)
		return err;
	finfo->height = (FT_MulFix(ft_face->height, ft_face->size->metrics.y_scale)+32)/64;
	finfo->ascent = (FT_MulFix(ft_face->ascender, ft_face->size->metrics.y_scale)+32)/64;
	return nil;
}

void
ftsettransform(FTface f, FTmatrix *m, FTvector *v)
{
	/* FTMatrix and FTVector are compatible with FT_Matrix and FT_Vector */
	FT_Set_Transform(f.ft_face, (FT_Matrix*)m, (FT_Vector*)v);
}

int
fthaschar(FTface f, int c)
{
	return FT_Get_Char_Index(f.ft_face, c) != 0;
}

char*
ftloadglyph(FTface f, int ix, FTglyph *g)
{
	FT_Face ft_face = f.ft_face;
	FT_GlyphSlot ft_glyph;
	char *err;

	ix = FT_Get_Char_Index(ft_face, ix);
	err = fterrstr(FT_Load_Glyph(ft_face, ix, FT_LOAD_NO_BITMAP|FT_LOAD_RENDER|FT_LOAD_CROP_BITMAP));
	if (err != nil)
		return err;

	
	ft_glyph = ft_face->glyph;
	g->top = ft_glyph->bitmap_top;
	g->left = ft_glyph->bitmap_left;
	g->height = ft_glyph->bitmap.rows;
	g->width = ft_glyph->bitmap.width;
	g->advx = ft_glyph->advance.x;
	g->advy = ft_glyph->advance.y;
	g->bpr = ft_glyph->bitmap.pitch;
	g->bitmap = ft_glyph->bitmap.buffer;
	return nil;
}

void
ftdoneface(FTface f)
{
	if (f.ft_face != nil)
		FT_Done_Face(f.ft_face);
	if (f.ft_lib != nil)
		FT_Done_FreeType(f.ft_lib);
}

/*
 * get the freetype error strings
 */

typedef struct FTerr FTerr;
struct FTerr {
	int		code;
	char*	text;
};

#define FT_NOERRORDEF_(l,c,t)
#define FT_ERRORDEF_(l,c,t)	c,t,

static FTerr fterrs[] = {
#include "freetype/fterrdef.h"
	-1, "",
};

static char*
fterrstr(int code)
{
	int i;
	if (code == 0)
		return nil;
	for (i = 0; fterrs[i].code > 0; i++) {
		if (fterrs[i].code == code)
			return fterrs[i].text;
	}
	return "unknown FreeType error";
}