summaryrefslogtreecommitdiff
path: root/src/wrap-test.cpp
blob: 03df1db1fdb6491e47ae46424462af71e94546b6 (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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <SDL.h>

#include <GL/gl.h>

#include <sdl-freetype.h>
#include <sdl-freetype-opengl.h>

#ifdef HAVE_FONTCONFIG
#include <sdl-ft.h>
#endif

#include <stdio.h>

#include <vector>

enum TextFormat {
    TF_NONE       = 0,
    TF_CALCRECT   = 1 << 0,
    TF_CENTER     = 1 << 1,
    TF_LEFT       = 1 << 2,
    TF_RIGHT      = 1 << 3,
    TF_SINGLELINE = 1 << 4,
    TF_NOCLIP     = 1 << 5
};

struct TextRect {
    int x;
    int y;
    int width;
    int height;
};

struct TextColor {
    unsigned char r;
    unsigned char g;
    unsigned char b;
    unsigned char a;
};

typedef std::vector<sdl_freetype_glyph_t> GlyphVector;
struct TextLine {
    GlyphVector glyphs;
    sdl_freetype_text_fixed_extents_t exts;
};
typedef std::vector<TextLine> TextLineVector;

static int
DrawText(sdl_freetype_font_t * font,
	 FT_Int32 *text, int len,
	 TextRect *rect,
	 TextFormat format,
	 TextColor color)
{
    if (!font || !text)
	return 0;

    TextLineVector lines;

    if (len < 0) {
	FT_Int32 *s = text;

	len = 0;
	while (*s++)
	    len++;
    }

    sdl_freetype_font_fixed_extents_t exts;
    sdl_freetype_font_fixed_extents(font, &exts);

    sdl_freetype_fixed_t x = 0;
    sdl_freetype_fixed_t y = 0;
    sdl_freetype_fixed_t adv_x = 0;
    sdl_freetype_fixed_t adv_y = 0;
    sdl_freetype_fixed_t width = rect->width << 16;
    sdl_freetype_fixed_t maxwidth = 0;
    bool clip = (format & TF_NOCLIP) == 0;

    lines.push_back(TextLine());
    TextLine *line = &lines.back();
    memset(&line->exts, 0, sizeof(line->exts));

    for (int i = 0; i < len; i++) {
	sdl_freetype_glyph_t g;
	sdl_freetype_text_fixed_extents_t metrics;

	if (text[i] == '\r')
	    continue;

	if (clip && (y + exts.height) >> 16 > rect->height)
	    break;

	if (text[i] == '\n') {
	    if (format & TF_SINGLELINE)
		continue;
	    y += exts.height;

	    sdl_freetype_font_glyphs_fixed_extents(font, &line->exts,
						   &line->glyphs[0], line->glyphs.size());
	    if (line->exts.width > maxwidth)
		maxwidth = line->exts.width;

	    lines.push_back(TextLine());
	    line = &lines.back();
	    memset(&line->exts, 0, sizeof(line->exts));
	    adv_x = 0;
	    adv_y = 0;
	    x = 0;
	}

	x += adv_x;
	y += adv_y;
	g.x = x;
	g.y = y;
	g.index = sdl_freetype_font_glyph_index(font, text[i]);
	if (!g.index || sdl_freetype_font_glyph_fixed_metrics(font, g.index, &metrics)) {
	    adv_x = 0;
	    adv_y = 0;
	    continue;
	} else {
	    adv_x = metrics.x_advance;
	    adv_y = metrics.y_advance;
	}
	if (format & TF_SINGLELINE) {
	    if (!clip || x + metrics.width <= width)
		line->glyphs.push_back(g);
	    else
		break;
	} else {
	    if (clip && x + metrics.width > width) {
		sdl_freetype_font_glyphs_fixed_extents(font, &line->exts,
						       &line->glyphs[0], line->glyphs.size());
		if (line->exts.width > maxwidth)
		    maxwidth = line->exts.width;

		lines.push_back(TextLine());
		line = &lines.back();
		memset(&line->exts, 0, sizeof(line->exts));

		x = 0;
		y += exts.height;
		g.y = y;
		g.x = x;
	    }
	    line->glyphs.push_back(g);
	}
    }
    if (line->glyphs.size() && !line->exts.width)
	sdl_freetype_font_glyphs_fixed_extents(font, &line->exts,
					       &line->glyphs[0], line->glyphs.size());
    if (!(format & TF_CALCRECT)) {
	int xoffset;
	int yoffset;

	if (lines.size())
	    yoffset = (exts.height - exts.descent) >> 16;
	for (size_t i = 0; i < lines.size(); i++) {
	    TextLine &line = lines[i];

	    xoffset = line.exts.x_bearing >> 16;
	    if (format & TF_RIGHT)
		xoffset += rect->width - (line.exts.width >> 16);
	    else if (format & TF_CENTER)
		xoffset += (rect->width - (line.exts.width >> 16)) / 2;

	    sdl_freetype_font_show_glyphs(font, 0,
					  color.r, color.g, color.b, color.a,
					  rect->x + xoffset, rect->y + yoffset,
					  &line.glyphs[0], line.glyphs.size());
	}
    }

    if (!lines[0].glyphs.size())
	return 0;

    rect->width = maxwidth >> 16;
    return (lines.size() * exts.height) >> 16;
}

static void DrawLine(float x0, float y0, float x1, float y1)
{
    GLfloat verts[4];

    verts[0] = x0;
    verts[1] = y0;
    verts[2] = x1;
    verts[3] = y1;

    glEnableClientState (GL_VERTEX_ARRAY);

    glVertexPointer (2, GL_FLOAT, 0, verts);
    glDrawArrays (GL_LINES, 0, 2);

    glDisableClientState (GL_VERTEX_ARRAY);
}

static void DrawRect(float x, float y, float w, float h)
{
    DrawLine(x, y, x + w, y);
    DrawLine(x + w, y, x + w, y + h);
    DrawLine(x + w, y + h, x, y + h);
    DrawLine(x, y + h, x, y);
}

int
main(int argc, char *argv[])
{
    SDL_Surface * screen;
    Uint8 video_bpp;
    Uint32 videoflags;
    int done;
    SDL_Event event;
    float alpha, step;
    sdl_freetype_font_t * font;
    sdl_freetype_glyph_render_t * render;
    FT_Int32 text[] = { 'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '.', ' ',
			0x4f60, 0x597d, ',', ' ', 0x5927, 0x4e16, 0x754c, 0xff01, 0x0000 };

    /* Initialize SDL */
    if (SDL_Init (SDL_INIT_VIDEO) < 0)
        goto errquit0;

    videoflags = SDL_HWSURFACE;
    videoflags |= SDL_OPENGL;
    video_bpp = 32;

    /* Set 640x480 video mode */
    screen = SDL_SetVideoMode (640, 480, video_bpp, videoflags);
    if (!screen)
        goto errquit0;

    //use following code to create a font if fontconfig isn't
    //available.
    //font = sdl_freetype_font_create("/path/to/font.ttf",
    //                                pixel_size);
#ifdef HAVE_FONTCONFIG
    font = sdl_ft_create_font ("sans", 18,
                               SDL_FT_WEIGHT_NORMAL, SDL_FT_SLANT_NORMAL, 96);
#else
    if (argc < 2)
	goto errquit0;
    font = sdl_freetype_font_create(argv[1], 18 * 96 / 72.0);
#endif
    if (!font)
        goto errquit0;

    //create a opengl font renderer
    render = sdl_freetype_opengl_render_create ();
    if (!render)
        goto errquit1;

    //replace the default font renderer with  opengl one
    sdl_freetype_font_set_render (font, render);

    glMatrixMode (GL_PROJECTION) ;
    glLoadIdentity ();
    glOrtho (0, 640, 480, 0, -1.0, 1.0);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();

    glEnable (GL_TEXTURE_2D);
    glClearColor (0.8f, 0.8f, 0.8f, 0.0f);

    alpha = 1.0f;
    step = -0.01;
    /* Wait for a keystroke */
    done = 0;
    while (!done) {
        /* Check for events */
        while (SDL_PollEvent (&event)) {
            switch (event.type) {
            case SDL_MOUSEBUTTONDOWN:
                break;
            case SDL_KEYDOWN:
            case SDL_QUIT:
                done = 1;
                break;
            default:
                break;
            }
        }

        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();

	TextColor color;
	color.r = 255;
	color.g = 0;
	color.b = 0;
	color.a = alpha * 255;

	TextRect rect;
	rect.x = 400;
	rect.y = 100;
	rect.width = 200;
	rect.height = 100;

	int h;

	// draw a single line and left aligned text
	h = DrawText(font, text, -1, &rect, TextFormat(TF_SINGLELINE | TF_LEFT), color);
	DrawRect(400, 100, 200, 100);
	DrawLine(400, 100 + h, 400 + 200, 100 + h);

	// draw a multiple lines and left aligned text
	rect.x = 100;
	rect.y = 100;
	rect.width = 100;
	rect.height = 100;
	h = DrawText(font, text, -1, &rect, TF_LEFT, color);
	DrawRect(100, 100, 200, 100);
	DrawLine(100, 100 + h, 100 + 200, 100 + h);

	// draw a multiple lines and right aligned text
	rect.x = 100;
	rect.y = 200;
	rect.width = 200;
	rect.height = 100;
	h = DrawText(font, text, -1, &rect, TF_RIGHT, color);
	DrawRect(100, 200, 200, 100);
	DrawLine(100, 200 + h, 100 + 200, 200 + h);

	// draw a multiple lines and center aligned text
	rect.x = 100;
	rect.y = 300;
	rect.width = 200;
	rect.height = 100;
	h = DrawText(font, text, -1, &rect, TF_CENTER, color);
	DrawRect(100, 300, 200, 100);
	DrawLine(100, 300 + h, 100 + 200, 300 + h);

        SDL_GL_SwapBuffers();

        alpha += step;
        if (alpha < 0.0f) {
            alpha = 0.0f;
            step = -step;
        } else if (alpha > 1.0f) {
            alpha = 1.0f;
            step = -step;
        }

        SDL_Delay(20);
    }

 errquit1:
    sdl_freetype_font_destroy (font);
    sdl_freetype_fini ();

 errquit0:
    SDL_Quit();
    return 0;
}