Skip to content

Commit

Permalink
Cleanup and polish on the new TTF_Text API
Browse files Browse the repository at this point in the history
* showfont uses the API to show the caption
* showfont now has an -editbox command line option to show the editbox
* TTF_MeasureString() returns width in pixels and max_length in bytes
* Removed TTF_CreateText_Wrapped()
* TTF_SetTextWrapping() has been renamed TTF_SetTextWrapWidth()
* TTF_GetTextWrapping() has been renamed TTF_GetTextWrapWidth()

Fixes #418
  • Loading branch information
slouken committed Oct 21, 2024
1 parent 3adac8a commit 69300de
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 347 deletions.
5 changes: 4 additions & 1 deletion examples/editbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ EditBox *EditBox_Create(SDL_Window *window, SDL_Renderer *renderer, TTF_TextEngi
edit->window = window;
edit->renderer = renderer;
edit->font = font;
edit->text = TTF_CreateText_Wrapped(engine, font, NULL, 0, (int)SDL_floorf(rect->w));
edit->text = TTF_CreateText(engine, font, NULL, 0);
if (!edit->text) {
EditBox_Destroy(edit);
return NULL;
Expand All @@ -344,6 +344,9 @@ EditBox *EditBox_Create(SDL_Window *window, SDL_Renderer *renderer, TTF_TextEngi
edit->highlight1 = -1;
edit->highlight2 = -1;

/* Wrap the editbox text within the editbox area */
TTF_SetTextWrapWidth(edit->text, (int)SDL_floorf(rect->w));

/* Show the whitespace when wrapping, so it can be edited */
TTF_SetTextWrapWhitespaceVisible(edit->text, true);

Expand Down
113 changes: 56 additions & 57 deletions examples/showfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@


#define TTF_SHOWFONT_USAGE \
"Usage: %s [-textengine surface|renderer] [-shaded] [-blended] [-wrapped] [-b] [-i] [-u] [-s] [-outline size] [-hintlight|-hintmono|-hintnone] [-nokerning] [-wrap] [-align left|center|right] [-fgcol r,g,b,a] [-bgcol r,g,b,a] <font>.ttf [ptsize] [text]\n"
"Usage: %s [-textengine surface|renderer] [-shaded] [-blended] [-wrapped] [-b] [-i] [-u] [-s] [-outline size] [-hintlight|-hintmono|-hintnone] [-nokerning] [-wrap] [-align left|center|right] [-fgcol r,g,b,a] [-bgcol r,g,b,a] [-editbox] <font>.ttf [ptsize] [text]\n"

typedef enum
{
Expand All @@ -61,8 +61,8 @@ typedef struct {
SDL_Surface *window_surface;
SDL_Renderer *renderer;
TTF_Font *font;
SDL_Texture *caption;
SDL_FRect captionRect;
TTF_Text *caption;
SDL_Rect captionRect;
SDL_Texture *message;
SDL_FRect messageRect;
TextEngine textEngine;
Expand Down Expand Up @@ -96,7 +96,17 @@ static void DrawScene(Scene *scene)
EditBox_Draw(scene->edit);
}

SDL_RenderTexture(renderer, scene->caption, NULL, &scene->captionRect);
switch (scene->textEngine) {
case TextEngineSurface:
/* Flush the renderer so we can draw directly to the window surface */
SDL_FlushRenderer(renderer);
TTF_DrawSurfaceText(scene->caption, scene->captionRect.w, scene->captionRect.h, scene->window_surface);
break;
case TextEngineRenderer:
TTF_DrawRendererText(scene->caption, (float)scene->captionRect.x, (float)scene->captionRect.y);
break;
}

SDL_RenderTexture(renderer, scene->message, NULL, &scene->messageRect);
SDL_RenderPresent(renderer);

Expand Down Expand Up @@ -269,26 +279,20 @@ int main(int argc, char *argv[])
SDL_Color *backcol;
SDL_Event event;
TTF_TextEngine *engine = NULL;
TextRenderMethod rendermethod;
int renderstyle;
int outline;
int hinting;
int kerning;
int wrap;
TextRenderMethod rendermethod = TextRenderShaded;
int renderstyle = TTF_STYLE_NORMAL;
int outline = 0;
int hinting = TTF_HINTING_NORMAL;
int kerning = 1;
bool wrap = false;
TTF_HorizontalAlignment align = TTF_HORIZONTAL_ALIGN_LEFT;
int dump;
bool editbox = false;
bool dump = false;
char *message, string[128];

/* Look for special execution mode */
dump = 0;
/* Look for special rendering types */
SDL_zero(scene);
rendermethod = TextRenderShaded;
renderstyle = TTF_STYLE_NORMAL;
outline = 0;
hinting = TTF_HINTING_NORMAL;
kerning = 1;
wrap = 0;
scene.textEngine = TextEngineRenderer;

/* Default is black and white */
forecol = &black;
backcol = &white;
Expand Down Expand Up @@ -341,7 +345,7 @@ int main(int argc, char *argv[])
kerning = 0;
} else
if (SDL_strcmp(argv[i], "-wrap") == 0) {
wrap = 1;
wrap = true;
} else
if (SDL_strcmp(argv[i], "-align") == 0 && argv[i+1]) {
++i;
Expand All @@ -356,9 +360,6 @@ int main(int argc, char *argv[])
return (1);
}
} else
if (SDL_strcmp(argv[i], "-dump") == 0) {
dump = 1;
} else
if (SDL_strcmp(argv[i], "-fgcol") == 0 && argv[i+1]) {
int r, g, b, a = SDL_ALPHA_OPAQUE;
if (SDL_sscanf(argv[++i], "%d,%d,%d,%d", &r, &g, &b, &a) < 3) {
Expand All @@ -380,6 +381,12 @@ int main(int argc, char *argv[])
backcol->g = (Uint8)g;
backcol->b = (Uint8)b;
backcol->a = (Uint8)a;
} else
if (SDL_strcmp(argv[i], "-editbox") == 0) {
editbox = true;
} else
if (SDL_strcmp(argv[i], "-dump") == 0) {
dump = true;
} else {
SDL_Log(TTF_SHOWFONT_USAGE, argv0);
return(1);
Expand Down Expand Up @@ -467,25 +474,33 @@ int main(int argc, char *argv[])
Cleanup(2);
}

/* Show which font file we're looking at */
SDL_snprintf(string, sizeof(string), "Font file: %s", argv[0]); /* possible overflow */
switch (rendermethod) {
case TextRenderShaded:
text = TTF_RenderText_Shaded(font, string, 0, *forecol, *backcol);
switch (scene.textEngine) {
case TextEngineSurface:
engine = TTF_CreateSurfaceTextEngine();
if (!engine) {
SDL_Log("Couldn't create surface text engine: %s\n", SDL_GetError());
Cleanup(2);
}
break;
case TextRenderBlended:
text = TTF_RenderText_Blended(font, string, 0, *forecol);
case TextEngineRenderer:
engine = TTF_CreateRendererTextEngine(scene.renderer);
if (!engine) {
SDL_Log("Couldn't create renderer text engine: %s\n", SDL_GetError());
Cleanup(2);
}
break;
default:
break;
}
if (text != NULL) {
scene.captionRect.x = 4.0f;
scene.captionRect.y = 4.0f;
scene.captionRect.w = (float)text->w;
scene.captionRect.h = (float)text->h;
scene.caption = SDL_CreateTextureFromSurface(scene.renderer, text);
SDL_DestroySurface(text);
}

/* Show which font file we're looking at */
SDL_snprintf(string, sizeof(string), "Font file: %s", argv[0]); /* possible overflow */
scene.caption = TTF_CreateText(engine, font, string, 0);
TTF_SetTextColor(scene.caption, forecol->r, forecol->g, forecol->b, forecol->a);
scene.captionRect.x = 4;
scene.captionRect.y = 4;
TTF_GetTextSize(scene.caption, &scene.captionRect.w, &scene.captionRect.h);

/* Render and center the message */
if (argc > 2) {
message = argv[2];
Expand Down Expand Up @@ -521,23 +536,7 @@ int main(int argc, char *argv[])
SDL_Log("Font is generally %d big, and string is %d big\n",
TTF_GetFontHeight(font), text->h);

switch (scene.textEngine) {
case TextEngineSurface:
engine = TTF_CreateSurfaceTextEngine();
if (!engine) {
SDL_Log("Couldn't create surface text engine: %s\n", SDL_GetError());
}
break;
case TextEngineRenderer:
engine = TTF_CreateRendererTextEngine(scene.renderer);
if (!engine) {
SDL_Log("Couldn't create renderer text engine: %s\n", SDL_GetError());
}
break;
default:
break;
}
if (engine) {
if (editbox) {
scene.textRect.x = 8.0f;
scene.textRect.y = scene.captionRect.y + scene.captionRect.h + 4.0f;
scene.textRect.w = WIDTH / 2 - scene.textRect.x * 2;
Expand Down Expand Up @@ -601,7 +600,7 @@ int main(int argc, char *argv[])
default:
break;
}
SDL_DestroyTexture(scene.caption);
TTF_DestroyText(scene.caption);
SDL_DestroyTexture(scene.message);
Cleanup(0);

Expand Down
6 changes: 4 additions & 2 deletions examples/testapp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1064,9 +1064,11 @@ int main(void)
break;
}
} else if (textengine_mode == 1) {
text_obj = TTF_CreateText_Wrapped(engine_surface, font, text, 0, wrap_size);
text_obj = TTF_CreateText(engine_surface, font, text, 0);
TTF_SetTextWrapWidth(text_obj, wrap_size);
} else {
text_obj = TTF_CreateText_Wrapped(engine_renderer, font, text, 0, wrap_size);
text_obj = TTF_CreateText(engine_renderer, font, text, 0);
TTF_SetTextWrapWidth(text_obj, wrap_size);
}

if (print_elapsed_ticks) {
Expand Down
Loading

0 comments on commit 69300de

Please sign in to comment.