Text: Text core rewrite + freetype support.
[gfxprim.git] / tests / SDL / fonttest.c
blobfdcf622c9ede3e5f5aa4d210f2e42774770c5e8d
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2011 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <SDL/SDL.h>
30 #include "GP.h"
31 #include "GP_SDL.h"
33 SDL_Surface *display = NULL;
34 GP_Context context;
36 static GP_Pixel white_pixel, gray_pixel, dark_gray_pixel, black_pixel,
37 red_pixel, blue_pixel;
39 static const char *test_strings[] = {
40 " !\"#$%&\047()*+,-./0123456789:;<=>?@",
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
42 "abcdefghijklmnopqrstuvwxyz{|}~",
43 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor..."
46 static int font_flag = 0;
47 static int tracking = 0;
48 GP_FontFace *font = NULL;
50 static const char *glyph_bitmap_format_name(const GP_FontBitmapFormat format)
52 switch (format) {
53 case GP_FONT_BITMAP_1BPP:
54 return "1BPP";
55 break;
56 case GP_FONT_BITMAP_8BPP:
57 return "8BPP";
58 break;
59 default:
60 return "Unknown";
64 static void print_character_metadata(const GP_FontFace *font, int c)
66 const GP_GlyphBitmap *glyph = GP_GetGlyphBitmap(font, c);
67 fprintf(stderr, "Properties of the character '%c':\n", c);
69 if (glyph) {
70 fprintf(stderr, " bitmap width: %d, height: %d\n",
71 glyph->width, glyph->height);
72 fprintf(stderr, " bearing_x: %d bearing_y %d\n",
73 glyph->bearing_x, glyph->bearing_y);
74 fprintf(stderr, " advance_x: %d\n",
75 glyph->advance_x);
76 } else {
77 fprintf(stderr, "(null)\n");
81 static void print_font_properties(const GP_FontFace *font)
83 fprintf(stderr, "Font properties:\n");
84 fprintf(stderr, " Height: ascend: %d, descend: %d\n",
85 GP_FontAscend(font), GP_FontDescend(font));
86 fprintf(stderr, " Glyph bitmap format: %s\n",
87 glyph_bitmap_format_name(font->glyph_bitmap_format));
88 fprintf(stderr, " Bounding box width: %d, heigth: %d\n",
89 GP_FontMaxWidth(font), GP_FontHeight(font));
91 print_character_metadata(font, 'a');
92 print_character_metadata(font, 'm');
93 print_character_metadata(font, '0');
96 #define SPACING 120
98 void redraw_screen(void)
100 SDL_LockSurface(display);
102 GP_Fill(&context, black_pixel);
104 GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
106 switch (font_flag) {
107 case 0:
108 style.font = &GP_DefaultProportionalFont;
109 break;
110 case 1:
111 style.font = &GP_DefaultConsoleFont;
112 break;
113 case 2:
114 style.font = font;
115 break;
118 print_font_properties(style.font);
120 /* Text alignment (we are always drawing to the right
121 * and below the starting point).
123 int align = GP_ALIGN_RIGHT|GP_VALIGN_BELOW;
125 const size_t TEST_STRING_COUNT = sizeof(test_strings)/sizeof(const char *);
126 size_t i;
127 for (i = 0; i < TEST_STRING_COUNT; i++) {
128 const char *test_string = test_strings[i];
130 style.pixel_xmul = 1;
131 style.pixel_ymul = 1;
132 style.pixel_xspace = 0;
133 style.pixel_yspace = 0;
134 style.char_xspace = tracking;
136 GP_FillRectXYWH(&context,
137 16, SPACING*i + 16,
138 GP_TextWidth(&style, test_string),
139 GP_FontHeight(style.font),
140 red_pixel);
142 GP_RectXYWH(&context,
143 15, SPACING*i + 15,
144 GP_TextMaxWidth(&style, strlen(test_string)) + 1,
145 GP_FontHeight(style.font) + 1,
146 blue_pixel);
148 GP_Text(&context, &style, 16, SPACING*i + 16, align,
149 white_pixel, red_pixel, test_string);
151 style.pixel_xmul = 2;
152 style.pixel_ymul = 2;
153 style.pixel_yspace = 1;
155 GP_Text(&context, &style, 34, SPACING*i + 44, align,
156 white_pixel, black_pixel, test_string);
158 style.pixel_xmul = 4;
159 style.pixel_ymul = 2;
160 style.pixel_xspace = 1;
161 style.pixel_yspace = 1;
163 GP_Text(&context, &style, 64, SPACING*i + 88, align,
164 dark_gray_pixel, black_pixel, test_string);
167 SDL_UnlockSurface(display);
170 void event_loop(void)
172 SDL_Event event;
174 while (SDL_WaitEvent(&event) > 0) {
175 switch (event.type) {
177 case SDL_VIDEOEXPOSE:
178 redraw_screen();
179 SDL_Flip(display);
180 break;
182 case SDL_KEYDOWN:
183 switch (event.key.keysym.sym) {
184 case SDLK_SPACE:
185 if (font)
186 font_flag = (font_flag + 1) % 3;
187 else
188 font_flag = (font_flag + 1) % 2;
190 redraw_screen();
191 SDL_Flip(display);
192 break;
193 case SDLK_UP:
194 tracking++;
195 redraw_screen();
196 SDL_Flip(display);
197 break;
198 case SDLK_DOWN:
199 tracking--;
200 redraw_screen();
201 SDL_Flip(display);
202 break;
203 case SDLK_ESCAPE:
204 return;
205 default:
206 break;
208 break;
210 case SDL_QUIT:
211 return;
216 void print_instructions(void)
218 printf("Use the following keys to control the test:\n");
219 printf(" Esc ................. exit\n");
220 printf(" Space ............... change font\n");
221 printf(" up/down ............. increase/decrease tracking\n");
224 int main(int argc, char *argv[])
226 print_instructions();
228 GP_SetDebugLevel(10);
230 if (argc > 1) {
231 fprintf(stderr, "\nLoading font '%s'\n", argv[1]);
232 font = GP_FontFaceLoad(argv[1], 13, 19);
235 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
236 fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
237 return 1;
240 display = SDL_SetVideoMode(640, 500, 0, SDL_SWSURFACE);
241 if (display == NULL) {
242 fprintf(stderr, "Could not open display: %s\n", SDL_GetError());
243 goto fail;
246 GP_SDL_ContextFromSurface(&context, display);
248 white_pixel = GP_ColorToContextPixel(GP_COL_WHITE, &context);
249 gray_pixel = GP_ColorToContextPixel(GP_COL_GRAY_LIGHT, &context);
250 dark_gray_pixel = GP_ColorToContextPixel(GP_COL_GRAY_DARK, &context);
251 black_pixel = GP_ColorToContextPixel(GP_COL_BLACK, &context);
252 red_pixel = GP_ColorToContextPixel(GP_COL_RED, &context);
253 blue_pixel = GP_ColorToContextPixel(GP_COL_BLUE, &context);
255 redraw_screen();
256 SDL_Flip(display);
258 event_loop();
260 SDL_Quit();
261 return 0;
263 fail:
264 SDL_Quit();
265 return 1;