]> icculus.org git repositories - dana/openbox.git/blob - glft/render.c
remove unneeded/uncompiled junk
[dana/openbox.git] / glft / render.c
1 #include "render.h"
2 #include "font.h"
3 #include "debug.h"
4 #include "glft.h"
5 #include <glib.h>
6 #include <GL/glx.h>
7
8 #define TPOINTS 15.0
9
10 #define TOFLOAT(x) (((x) >> 6) + ((x) & 63)/64.0)
11
12 void GlftRenderGlyph(FT_Face face, struct GlftGlyph *g)
13 {
14     unsigned char *padbuf;
15     int err, i;
16     FT_GlyphSlot slot = face->glyph;
17
18     err = FT_Render_Glyph(slot, ft_render_mode_normal);
19         g_assert(!err);
20
21     g->texw = slot->bitmap.width;
22     g->texh = slot->bitmap.rows;
23
24     g->left = slot->bitmap_left;
25
26     g->yoff = slot->bitmap.rows - slot->bitmap_top;
27     g->padx = 1;
28     while (g->padx < slot->bitmap.width)
29        g->padx <<= 1;
30
31     g->pady = 1;
32     while (g->pady < slot->bitmap.rows)
33        g->pady <<= 1;
34
35     padbuf = g_new0(unsigned char, g->padx * g->pady);
36     for (i = 0; i < slot->bitmap.rows; i++)
37         memcpy(padbuf + i*g->padx,
38                slot->bitmap.buffer + i*slot->bitmap.width,
39                slot->bitmap.width);
40     glBindTexture(GL_TEXTURE_2D, g->tnum);
41     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
42     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
43     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
44     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
45     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
46     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
47     glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->padx, g->pady,
48                  0, GL_ALPHA, GL_UNSIGNED_BYTE, padbuf);
49
50     g_free(padbuf);
51 }
52
53 static void drawstring(struct GlftFont *font, const char *str, int bytes,
54                        struct GlftColor *color, int x, int y)
55 {
56     const char *c;
57     struct GlftGlyph *g, *p = NULL;
58
59     y += font->descent/2; /* XXX fixme */
60
61     glColor4f(color->r, color->g, color->b, color->a);
62     glPushMatrix();
63     glTranslatef(x, y, 0.0);
64     c = str;
65     while (c - str < bytes) {
66         g = GlftFontGlyph(font, c);
67         if (g) {
68             glTranslatef(GlftFontAdvance(font, p, g), 0.0, 0.0);
69             glBindTexture(GL_TEXTURE_2D, g->tnum);
70
71             glBegin(GL_QUADS);
72
73             glTexCoord2f(0, g->texh/(float)g->pady);
74             glVertex2i(g->left, 0 - g->yoff);
75
76             glTexCoord2f(g->texw/(float)g->padx, g->texh/(float)g->pady);
77             glVertex2i(g->left + g->texw, 0 - g->yoff);
78
79             glTexCoord2f(g->texw/(float)g->padx, 0);
80             glVertex2i(g->left + g->texw, g->texh - g->yoff);
81
82             glTexCoord2f(0, 0);
83             glVertex2i(g->left, g->texh - g->yoff);
84             glEnd();
85         } else
86             glTranslatef(font->max_advance_width, 0.0, 0.0);
87         p = g;
88         c = g_utf8_next_char(c);
89     }
90
91     glPopMatrix();
92 }
93
94 void GlftRenderString(struct GlftFont *font, const char *str, int bytes,
95                       struct GlftColor *color, int x, int y)
96 {
97     if (!g_utf8_validate(str, bytes, NULL)) {
98         GlftDebug("Invalid UTF-8 in string\n");
99         return;
100     }
101
102     if (font->shadow && font->shadow_alpha != 0) {
103         struct GlftColor c;
104         if (font->shadow_alpha > 0) {
105             c.r = c.g = c.b = 0.0;
106             c.a = font->shadow_alpha;
107         } else {
108             c.r = c.g = c.b = 1.0;
109             c.a = -font->shadow_alpha;
110         }
111         drawstring(font, str, bytes, &c,
112                    x + font->shadow_offset, y);
113         y += font->shadow_offset;
114     }
115     drawstring(font, str, bytes, color, x, y);
116 }
117
118 void GlftMeasureString(struct GlftFont *font,
119                        const char *str,
120                        int bytes,
121                        int *w,
122                        int *h)
123 {
124     const char *c;
125     struct GlftGlyph *g, *p = NULL;
126
127     if (!g_utf8_validate(str, bytes, NULL)) {
128         GlftDebug("Invalid UTF-8 in string\n");
129         return;
130     }
131
132     *w = 0;
133     *h = 0;
134
135     c = str;
136     while (c - str < bytes) {
137         g = GlftFontGlyph(font, c);
138         if (g) {
139             *w += GlftFontAdvance(font, p, g);
140             *h = MAX(g->height, *h);
141         } else
142             *w += font->max_advance_width;
143         p = g;
144         c = g_utf8_next_char(c);
145     }
146     if (font->shadow) {
147         *w += font->shadow_offset;
148         *h += font->shadow_offset;
149     }
150 }