-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.c
176 lines (148 loc) · 3.58 KB
/
font.c
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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gtk/gtk.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include "fconv.h"
static FT_Vector last;
static FT_Face fc;
extern PangoFontMap *map;
extern PangoContext *ctx;
extern PangoFont *ft;
static ft2d (const FT_Vector *s, point_t *d)
{
d->x = (double)s->x;
d->y = (double)s->y;
}
static int line4 (const FT_Vector *a, const FT_Vector *b,
const FT_Vector *c, const FT_Vector *d)
{
if (a->x == b->x && b->x == c->x && c->x == d->x)
return 1;
if (a->y == b->y && b->y == c->y && c->y == d->y)
return 1;
return 0;
}
static int line3 (const FT_Vector *a, const FT_Vector *b,
const FT_Vector *c)
{
double r1, r2;
double ax, ay, bx, by, cx, cy;
r1 = 0;
r2 = 1;
if (a->x == b->x && b->x == c->x)
return 1;
if (a->y == b->y && b->y == c->y)
return 1;
if (a->x == b->x && a->y == b->y)
return 1;
if (b->x == c->x && b->y == c->y)
return 1;
if (a->x != b->x && b->x != c->x) {
ax = (double)a->x;
ay = (double)a->y;
bx = (double)b->x;
by = (double)b->y;
cx = (double)c->x;
cy = (double)c->y;
r1 = (by - ay) / (bx - ax);
r2 = (cy - by) / (cx - bx);
}
return (r1 == r2) ? 1 : 0;
}
static int f_move_to (const FT_Vector *to, void *user)
{
gpart_t gp;
// printf ("move to %+d:%+d\n", to->x, to->y);
gp.type = move_to;
ft2d (&last, &(gp.points[0]));
ft2d (to, &(gp.points[1]));
add_part ((glyph_t *)user, &gp);
last = *to;
return 0;
}
static int f_line_to (const FT_Vector *to, void *user)
{
gpart_t gp;
// printf ("line to %+d:%+d\n", to->x, to->y);
gp.type = line_to;
ft2d (&last, &(gp.points[0]));
ft2d (to, &(gp.points[1]));
add_part ((glyph_t *)user, &gp);
last = *to;
return 0;
}
static int f_conic_to (const FT_Vector *c1, const FT_Vector *to, void *user)
{
gpart_t gp;
// printf ("%d: conic to %+d:%+d %+d:%+d\n", piece, c1->x, c1->y, to->x, to->y);
if (line3 (&last, c1, to))
return f_line_to (to, user);
gp.type = conic_to;
ft2d (&last, &(gp.points[0]));
ft2d (c1, &(gp.points[1]));
ft2d (to, &(gp.points[2]));
add_part ((glyph_t *)user, &gp);
last = *to;
return 0;
}
static int f_cubic_to (const FT_Vector *c1, const FT_Vector *c2,
const FT_Vector *to, void *user)
{
gpart_t gp;
// printf ("%d: cubic to %+d:%+d %+d:%+d %+d:%+d\n", piece, c1->x, c1->y,
// c2->x, c2->y, to->x, to->y);
if (line4 (&last, c1, c2, to))
return f_line_to (to, user);
gp.type = cubic_to;
ft2d (&last, &(gp.points[0]));
ft2d (c1, &(gp.points[1]));
ft2d (c2, &(gp.points[2]));
ft2d (to, &(gp.points[3]));
add_part ((glyph_t *)user, &gp);
last = *to;
return 0;
}
void set_font (PangoFontDescription *desc)
{
ft = pango_font_map_load_font (map, ctx, desc);
fc = pango_ft2_font_get_face (ft);
pango_font_description_free (desc);
}
void outline_glyph (char c, glyph_t *g)
{
int err;
unsigned int gl_idx;
FT_Outline ol;
FT_Outline_Funcs ifs;
FT_Glyph_Metrics gm;
ifs.shift = 0;
ifs.delta = 0;
ifs.move_to = f_move_to;
ifs.line_to = f_line_to;
ifs.conic_to = f_conic_to;
ifs.cubic_to = f_cubic_to;
// printf ("=============decomposing glyph %c\n", c);
gl_idx = FT_Get_Char_Index (fc, (FT_ULong)c);
if (!gl_idx) {
printf ("No glyph for char %c\n", c);
return;
}
err = FT_Load_Glyph (fc, gl_idx, FT_LOAD_NO_SCALE);
if (err) {
printf ("Error loading glyph for char %c\n", c);
return;
}
last.x = last.y = 0;
gm = fc->glyph->metrics;
g->asc = (double)gm.horiBearingY;
ol = fc->glyph->outline;
err = FT_Outline_Decompose (&ol, &ifs, g);
if (err) {
printf ("Error decomposing glyph\n");
return;
}
// printf ("================================\n");
}