summaryrefslogtreecommitdiff
path: root/Source/DirectFB/tests/smiley_test.c
blob: aab79936bcbf19665ae0c7a4b81df893c1e5699e (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <direct/types.h>

#include <directfb.h>


/* macro for a safe call to DirectFB functions */
#define DFBCHECK(x...)                                           \
do {                                                             \
     int err = x;                                                \
     if (err != DFB_OK) {                                        \
          fprintf( stderr, "%s <%d>:\n\t", __FILE__, __LINE__ ); \
          DirectFBErrorFatal( #x, err );                         \
     }                                                           \
} while (0)


#define ERROR(x...)                                              \
do {                                                             \
     fprintf (stderr, "%s <%i>: ", __FILE__, __LINE__);          \
        fprintf (stderr, x);                                     \
        fprintf (stderr, "\n");                                  \
     exit (-1);                                                  \
} while (0)


static char **fontname_list;
static int fontname_count;

static IDirectFB             *dfb;
static IDirectFBEventBuffer  *keybuffer;
static IDirectFBDisplayLayer *layer;
static IDirectFBSurface      *surface;

static int show_help         = 0;
static int show_ascender     = 0;
static int show_descender    = 0;
static int show_baseline     = 0;
static int show_glyphrect    = 0;
static int show_glyphadvance = 0;
static int show_glyphorigin  = 0;

static int antialias         = 1;
static int unicode_mode      = 1;

static DFBEnumerationResult
encoding_callback( DFBTextEncodingID  id,
                   const char        *name,
                   void              *context )
{
     printf( "  (%02d) %s\n", id, name );

     return DFENUM_OK;
}

static void
render_font_page (IDirectFBSurface *surface,
                  const char       *fontname1,
                  const char       *fontname2)
{
     DFBFontDescription fontdesc;
     IDirectFBFont *font1, *font2;
     int width, height;
     int bwidth, bheight;
     int xborder, yborder;
     int baseoffset;
     int ascender, descender;
     char label[32];
     int i, j;

     surface->GetSize (surface, &width, &height);

     bwidth = width * 7 / 8;
     bheight = height * 7 / 8;

     xborder = (width - bwidth) / 2;
     yborder = (height - bheight) / 2;

     fontdesc.flags = DFDESC_ATTRIBUTES;
     fontdesc.attributes = antialias ? 0 : DFFA_MONOCHROME;

//     DFBCHECK(dfb->CreateFont (dfb, NULL, NULL, &fixedfont));
//     surface->SetFont (surface, fixedfont);

     fontdesc.flags = DFDESC_HEIGHT | DFDESC_ATTRIBUTES;
     fontdesc.height = 56;//9 * bheight / glyphs_per_yline / 16;
     fontdesc.attributes = antialias ? 0 : DFFA_MONOCHROME;
     fontdesc.attributes |= unicode_mode ? 0 : DFFA_NOCHARMAP;

     if (dfb->CreateFont (dfb, fontname1, &fontdesc, &font1) != DFB_OK) {

          static const char *msg = "failed opening '";
          char text [strlen(msg) + strlen(fontname1) + 2];

          strcpy (text, msg);
          strcpy (text + strlen(msg), fontname1);
          strcpy (text + strlen(msg) + strlen(fontname1), "'");
          printf("GEG: %s\n", text);
          surface->SetColor (surface, 0xff, 0x00, 0x00, 0xff);
          surface->DrawString (surface,
                               text, -1, width/2, 10, DSTF_TOPCENTER);
          return;
     }

     if (dfb->CreateFont (dfb, fontname2, &fontdesc, &font2) != DFB_OK) {

          static const char *msg = "failed opening '";
          char text [strlen(msg) + strlen(fontname2) + 2];

          strcpy (text, msg);
          strcpy (text + strlen(msg), fontname2);
          strcpy (text + strlen(msg) + strlen(fontname2), "'");
          printf("GEG: %s\n", text);
          surface->SetColor (surface, 0xff, 0x00, 0x00, 0xff);
          surface->DrawString (surface,
                               text, -1, width/2, 10, DSTF_TOPCENTER);
          return;
     }

     {
          static bool done = false;

          if (!done) {
               printf( "\nEncodings %s\n", fontname1 );
               font1->EnumEncodings( font1, encoding_callback, NULL );
               done = true;
          }
     }

     {
          static bool done = false;

          if (!done) {
               printf( "\nEncodings %s\n", fontname2 );
               font2->EnumEncodings( font2, encoding_callback, NULL );
               done = true;
          }
     }

     surface->SetFont(surface, font1);
     surface->DrawString(surface, "Hello", -1, 100, 100, DSTF_LEFT);
     surface->SetFont(surface, font2);
     surface->DrawString(surface, "AB", -1, 100, 100, DSTF_LEFT);

     font1->Release (font1);
     font2->Release (font2);
}

static void
cleanup( void )
{
     if (keybuffer) keybuffer->Release (keybuffer);
     if (surface)   surface->Release (surface);
     if (layer)     layer->Release (layer);
     if (dfb)       dfb->Release (dfb);
}

static void
print_usage( void )
{
     printf ("DirectFB Font Viewer version " VERSION "\n\n");
     printf ("Usage: df_fonts <fontfile> ... <fontfile>\n\n");
}

int
main( int argc, char *argv[] )
{
     DFBSurfaceDescription surface_desc;
     DFBInputEvent evt;
     int first_glyph  = 0;
     int current_font = 0;
     int update       = 1;

     if (argc < 2 || strcmp(argv[1], "--help") == 0) {
          print_usage();
          return EXIT_FAILURE;
     }

     fontname_count = argc - 1;
     fontname_list  = argv + 1;

     DFBCHECK(DirectFBInit(&argc, &argv));
     DFBCHECK(DirectFBSetOption("bg-none", NULL));
     DFBCHECK(DirectFBCreate(&dfb));

     atexit (cleanup);

     //dfb->SetCooperativeLevel(dfb, DFSCL_FULLSCREEN);

     surface_desc.flags = DSDESC_CAPS;
     surface_desc.caps = DSCAPS_PRIMARY /*| DSCAPS_DOUBLE*/;

     DFBCHECK(dfb->CreateSurface(dfb, &surface_desc, &surface));

     DFBCHECK(dfb->CreateInputEventBuffer(dfb, DICAPS_KEYS,
                                          DFB_FALSE, &keybuffer));

     while (1) {
          surface->Clear (surface, 0xff, 0xff, 0xff, 0xff);
          render_font_page (surface, argv[1], argv[2]);
          surface->Flip (surface, NULL, DSFLIP_WAITFORSYNC);

          keybuffer->WaitForEvent(keybuffer);

          while (keybuffer->GetEvent(keybuffer, DFB_EVENT(&evt)) == DFB_OK) {

          }
     }

     return EXIT_SUCCESS;
}