Created
August 31, 2021 12:41
-
-
Save matu3ba/b0ed535cf2ba9a052471a2bc23631058 to your computer and use it in GitHub Desktop.
reduced red_ftstring.c from freetype-demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************/ | |
/* */ | |
/* The FreeType project -- a free and portable quality TrueType renderer. */ | |
/* */ | |
/* Copyright (C) 1996-2021 by */ | |
/* D. Turner, R.Wilhelm, and W. Lemberg */ | |
/* */ | |
/* */ | |
/* ftstring.c - simple text string display */ | |
/* */ | |
/****************************************************************************/ | |
#include "common.h" | |
#include "ftcommon.h" | |
#include "mlgetopt.h" | |
#include <assert.h> | |
#include <math.h> | |
#include <stdarg.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
static const char *Sample[] = { | |
/* Custom string if any */ | |
NULL, | |
"Statustext", | |
"The quick brown fox jumps over the lazy dog"}; | |
enum { RENDER_MODE_STRING, N_RENDER_MODES }; | |
static struct status_ { | |
const char *keys; | |
const char *dims; | |
const char *device; | |
int render_mode; | |
unsigned long encoding; | |
int res; | |
int ptsize; /* current point size */ | |
int angle; | |
const char *text; | |
// TODO expand load and draw (100+ LOC each) | |
FTDemo_String_Context sc; | |
FT_Matrix trans_matrix; | |
int font_index; | |
const char *header; | |
char header_buffer[256]; | |
} status = {"", | |
DIM, | |
NULL, | |
RENDER_MODE_STRING, | |
FT_ENCODING_UNICODE, | |
72, | |
48, | |
0, | |
NULL, | |
{0, 0, 0x8000, 0, NULL, 0, 0}, | |
{0, 0, 0, 0}, | |
0, | |
NULL, | |
{0}}; | |
static FTDemo_Display *display; | |
static FTDemo_Handle *handle; | |
/*************************************************************************/ | |
/*************************************************************************/ | |
/*************************************************************************/ | |
/**** ****/ | |
/**** E V E N T H A N D L I N G ****/ | |
/**** ****/ | |
/*************************************************************************/ | |
/*************************************************************************/ | |
/*************************************************************************/ | |
static void event_text_change(void) { | |
static int i = INT_MAX - 1; | |
if (++i >= (int)(sizeof(Sample) / sizeof(Sample[0]))) | |
i = Sample[0] == NULL ? 1 : 0; | |
status.text = Sample[i]; | |
} | |
static int Process_Event(void) { | |
grEvent event; | |
int ret = 0; | |
if (*status.keys) | |
event.key = grKEY(*status.keys++); | |
else { | |
grListenSurface(display->surface, 0, &event); | |
if (event.type == gr_event_resize) | |
return ret; | |
} | |
switch (event.key) { | |
case grKeyEsc: | |
case grKEY('q'): | |
ret = 1; | |
goto Exit; | |
case grKeyTab: | |
event_text_change(); | |
FTDemo_String_Set(handle, status.text); | |
goto String; | |
default: | |
break; | |
} | |
String: | |
FTDemo_String_Load(handle, &status.sc); // pretty long method | |
Exit: | |
return ret; | |
} | |
static void write_header(FT_Error error_code) { | |
FTDemo_Draw_Header(handle, display, status.ptsize, status.res, -1, | |
error_code); | |
if (status.header) | |
grWriteCellString(display->bitmap, 0, 3 * HEADER_HEIGHT, status.header, | |
display->fore_color); | |
grRefreshSurface(display->surface); | |
} | |
static FT_Error Render_String(void) { | |
FTDemo_String_Draw(handle, display, &status.sc, | |
FT_MulFix(display->bitmap->width, status.sc.center), | |
display->bitmap->rows / 2); | |
return FT_Err_Ok; | |
} | |
int main(int argc, char **argv) { | |
/* Initialize engine */ | |
handle = FTDemo_New(); | |
status.ptsize = 20 * 64; | |
assert(status.ptsize > 0); | |
char* fontfile = "/usr/share/fonts/TTF/DejaVuSerifCondensed.ttf"; | |
handle->encoding = status.encoding; | |
for (; argc > 0; argc--, argv++) { | |
error = FTDemo_Install_Font(handle, fontfile, 0, 0); // 140 LOC | |
if (error) { | |
fprintf(stderr, "failed to install %s", fontfile); | |
if (error == FT_Err_Invalid_CharMap_Handle) | |
fprintf(stderr, ": missing valid charmap\n"); | |
else | |
fprintf(stderr, "\n"); | |
} | |
} | |
if (handle->num_fonts == 0) | |
PanicZ("could not open any font file"); | |
display = FTDemo_Display_New(status.device, status.dims); // 70 LOC | |
if (!display) | |
PanicZ("could not allocate display surface"); | |
grSetTitle(display->surface, "FreeType String Viewer - press ? for help"); | |
FTDemo_Icon(handle, display); //140 LOC | |
status.header = NULL; | |
event_text_change(); | |
FTDemo_Set_Current_Font(handle, handle->fonts[status.font_index]); // 40 LOC | |
FTDemo_Set_Current_Charsize(handle, status.ptsize, status.res); // 40 LOC | |
FTDemo_String_Set(handle, status.text); // 30 LOC | |
FTDemo_String_Load(handle, &status.sc); // 100 LOC | |
do { | |
FTDemo_Display_Clear(display); | |
switch (status.render_mode) { | |
case RENDER_MODE_STRING: | |
error = Render_String(); | |
break; | |
} | |
write_header(error); | |
status.header = 0; | |
} while (!Process_Event()); | |
printf("Execution completed successfully.\n"); | |
FTDemo_Display_Done(display); // 10 LOC | |
FTDemo_Done(handle); // 30 LOC but cleanup methods for all allocated objecst | |
exit(0); /* for safety reasons */ | |
/* return 0; */ /* never reached */ | |
} | |
/* End */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment