1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2011 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
33 SDL_Surface
*display
= NULL
;
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
)
53 case GP_FONT_BITMAP_1BPP
:
56 case GP_FONT_BITMAP_8BPP
:
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
);
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",
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');
98 void redraw_screen(void)
100 SDL_LockSurface(display
);
102 GP_Fill(&context
, black_pixel
);
104 GP_TextStyle style
= GP_DEFAULT_TEXT_STYLE
;
108 style
.font
= &GP_DefaultProportionalFont
;
111 style
.font
= &GP_DefaultConsoleFont
;
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 *);
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
,
138 GP_TextWidth(&style
, test_string
),
139 GP_FontHeight(style
.font
),
142 GP_RectXYWH(&context
,
144 GP_TextMaxWidth(&style
, strlen(test_string
)) + 1,
145 GP_FontHeight(style
.font
) + 1,
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)
174 while (SDL_WaitEvent(&event
) > 0) {
175 switch (event
.type
) {
177 case SDL_VIDEOEXPOSE
:
183 switch (event
.key
.keysym
.sym
) {
186 font_flag
= (font_flag
+ 1) % 3;
188 font_flag
= (font_flag
+ 1) % 2;
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);
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());
240 display
= SDL_SetVideoMode(640, 500, 0, SDL_SWSURFACE
);
241 if (display
== NULL
) {
242 fprintf(stderr
, "Could not open display: %s\n", SDL_GetError());
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
);