]> icculus.org git repositories - dana/openbox.git/blob - glft/render.c
fix brain damage in lineto
[dana/openbox.git] / glft / render.c
1 #include "render.h"
2 #include "font.h"
3 #include "debug.h"
4 #include <glib.h>
5 #include <GL/glx.h>
6
7 #include FT_OUTLINE_H
8
9 struct GlftWalkState {
10     int drawing;
11     float x, y;
12 };
13
14 static struct GlftWalkState state;
15
16 int GlftMoveToFunc(FT_Vector *to, void *user)
17 {
18     state.x = (to->x >> 6) + (to->x & 63)/64;
19     state.y = (to->y >> 6) + (to->y & 63)/64;
20     printf("move to %f:%f\n", state.x, state.y);
21     if (state.drawing) {
22         glEnd();
23         state.drawing = 0;
24     }
25     return 0;
26 }
27
28 int GlftLineToFunc(FT_Vector *to, void *user)
29 {
30     if (!state.drawing) {
31         glBegin(GL_LINES);
32         glVertex2f(state.x, state.y);
33         state.drawing = 1;
34     } else
35         glVertex2f(state.x, state.y);
36     state.x = (to->x >> 6) + (to->x & 63)/64;
37     state.y = (to->y >> 6) + (to->y & 63)/64;
38     printf("line to %f:%f\n", state.x, state.y);
39     glVertex2f(state.x, state.y);
40     return 0;
41 }
42
43 int GlftConicToFunc(FT_Vector *c, FT_Vector *to, void *user)
44 {
45     GlftLineToFunc(to, user);
46     printf("conic the hedgehog!\n");
47     return 0;
48 }
49
50 int GlftCubicToFunc(FT_Vector *c1, FT_Vector *c2, FT_Vector *to, void 
51 *user)
52 {
53     GlftLineToFunc(to, user);
54     printf("cubic\n");
55     return 0;
56 }
57
58 FT_Outline_Funcs GlftFuncs = {
59     GlftMoveToFunc,
60     GlftLineToFunc,
61     GlftConicToFunc,
62     GlftCubicToFunc,
63     0,
64     0
65 };
66
67 void GlftRenderGlyph(FT_Face face, unsigned int dlist)
68 {
69     int err;
70     FT_GlyphSlot slot = face->glyph;
71
72     state.x = 0;
73     state.y = 0;
74     state.drawing = 0;
75
76     glNewList(dlist, GL_COMPILE);
77     err = FT_Outline_Decompose(&slot->outline, &GlftFuncs, NULL);
78     g_assert(!err);
79     if (state.drawing)
80         glEnd();
81     glEndList();
82 }
83
84 void GlftRenderString(struct GlftFont *font, const char *str, int bytes,
85                       int x, int y)
86 {
87     const char *c;
88     struct GlftGlyph *g;
89
90     if (!g_utf8_validate(str, bytes, NULL)) {
91         GlftDebug("Invalid UTF-8 in string\n");
92         return;
93     }
94
95     glPushMatrix();
96
97     c = str;
98     while (c - str < bytes) {
99         g = GlftFontGlyph(font, c);
100         if (g) {
101             glCallList(g->dlist);
102             glTranslatef(g->width, 0.0, 0.0);
103         } else
104             glTranslatef(font->max_advance_width, 0.0, 0.0);
105         c = g_utf8_next_char(c);
106     }
107
108     glPopMatrix();
109 }
110
111 void GlftMeasureString(struct GlftFont *font,
112                        const char *str,
113                        int bytes,
114                        int *w,
115                        int *h)
116 {
117     const char *c;
118     struct GlftGlyph *g;
119
120     if (!g_utf8_validate(str, bytes, NULL)) {
121         GlftDebug("Invalid UTF-8 in string\n");
122         return;
123     }
124
125     *w = 0;
126     *h = 0;
127
128     c = str;
129     while (c - str < bytes) {
130         g = GlftFontGlyph(font, c);
131         if (g) {
132             *w += g->width;
133             *h = MAX(g->height, *h);
134         } else {
135             *w += font->max_advance_width;
136         }
137         c = g_utf8_next_char(c);
138     }
139 }