Skip to content

Commit

Permalink
Added TTF_SetTextPosition() and TTF_GetTextPosition()
Browse files Browse the repository at this point in the history
This allows you to lay out multiple text objects within a single text area.

Also switched text objects to always wrap on newlines.
  • Loading branch information
slouken committed Oct 19, 2024
1 parent 00f319f commit 4924574
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 156 deletions.
42 changes: 36 additions & 6 deletions examples/showfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ static void DrawScene(Scene *scene)
}
}

static void AdjustTextOffset(TTF_Text *text, int xoffset, int yoffset)
{
int x, y;

TTF_GetTextPosition(text, &x, &y);
x += xoffset;
y += yoffset;
TTF_SetTextPosition(text, x, y);
}

static void HandleKeyDown(Scene *scene, SDL_Event *event)
{
int style, outline;
Expand Down Expand Up @@ -197,16 +207,36 @@ static void HandleKeyDown(Scene *scene, SDL_Event *event)
TTF_SetFontStyle(scene->font, style);
break;

case SDLK_LEFT:
if (event->key.mod & SDL_KMOD_CTRL) {
AdjustTextOffset(scene->edit->text, -1, 0);
}
break;

case SDLK_RIGHT:
if (event->key.mod & SDL_KMOD_CTRL) {
AdjustTextOffset(scene->edit->text, 1, 0);
}
break;

case SDLK_UP:
/* Increase font size */
ptsize = TTF_GetFontSize(scene->font);
TTF_SetFontSize(scene->font, ptsize + 1.0f);
if (event->key.mod & SDL_KMOD_CTRL) {
AdjustTextOffset(scene->edit->text, 0, -1);
} else {
/* Increase font size */
ptsize = TTF_GetFontSize(scene->font);
TTF_SetFontSize(scene->font, ptsize + 1.0f);
}
break;

case SDLK_DOWN:
/* Decrease font size */
ptsize = TTF_GetFontSize(scene->font);
TTF_SetFontSize(scene->font, ptsize - 1.0f);
if (event->key.mod & SDL_KMOD_CTRL) {
AdjustTextOffset(scene->edit->text, 0, 1);
} else {
/* Decrease font size */
ptsize = TTF_GetFontSize(scene->font);
TTF_SetFontSize(scene->font, ptsize - 1.0f);
}
break;

case SDLK_ESCAPE:
Expand Down
2 changes: 2 additions & 0 deletions include/SDL3_ttf/SDL_textengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ struct TTF_TextData

bool needs_layout_update; /**< True if the layout needs to be updated */
TTF_TextLayout *layout; /**< Cached layout information, read-only. */
int x; /**< The x offset of the upper left corner of this text, in pixels, read-only. */
int y; /**< The y offset of the upper left corner of this text, in pixels, read-only. */
int w; /**< The width of this text, in pixels, read-only. */
int h; /**< The height of this text, in pixels, read-only. */
int num_ops; /**< The number of drawing operations to render this text, read-only. */
Expand Down
145 changes: 129 additions & 16 deletions include/SDL3_ttf/SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,7 @@ extern SDL_DECLSPEC void SDLCALL TTF_DestroyRendererTextEngine(TTF_TextEngine *e
/**
* Create a text object from UTF-8 text and a text engine.
*
* This will not word-wrap the string; you'll get a surface with a single line
* of text, as long as the string requires. You can use
* TTF_CreateText_Wrapped() instead if you need to wrap the output to multiple
* lines.
*
* This will not wrap on newline characters.
* This function is equivalent to `TTF_CreateText_Wrapped(engine, font, text, 0)` and will wrap on newline characters.
*
* \param engine the text engine to use when creating the text object, may be
* NULL.
Expand Down Expand Up @@ -1534,7 +1529,10 @@ extern SDL_DECLSPEC TTF_Text * SDLCALL TTF_CreateText_Wrapped(TTF_TextEngine *en
* \returns a valid property ID on success or 0 on failure; call
* SDL_GetError() for more information.
*
* \since This function is available since SDL 3.0.0.
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC SDL_PropertiesID SDLCALL TTF_GetTextProperties(TTF_Text *text);

Expand All @@ -1545,6 +1543,11 @@ extern SDL_DECLSPEC SDL_PropertiesID SDLCALL TTF_GetTextProperties(TTF_Text *tex
* \param engine the text engine to use for drawing.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextEngine(TTF_Text *text, TTF_TextEngine *engine);

Expand All @@ -1554,6 +1557,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextEngine(TTF_Text *text, TTF_TextEngin
* \param text the TTF_Text to query.
* \returns the TTF_TextEngine used by the text on success or NULL on failure;
* call SDL_GetError() for more information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC TTF_TextEngine * SDLCALL TTF_GetTextEngine(TTF_Text *text);

Expand All @@ -1566,6 +1574,11 @@ extern SDL_DECLSPEC TTF_TextEngine * SDLCALL TTF_GetTextEngine(TTF_Text *text);
*
* \param text the TTF_Text to modify.
* \param font the font to use, may be NULL.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextFont(TTF_Text *text, TTF_Font *font);

Expand All @@ -1575,9 +1588,44 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextFont(TTF_Text *text, TTF_Font *font)
* \param text the TTF_Text to query.
* \returns the TTF_Font used by the text on success or NULL on failure; call
* SDL_GetError() for more information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC TTF_Font * SDLCALL TTF_GetTextFont(TTF_Text *text);

/**
* Set the position of a text object.
*
* This can be used to position multiple text objects within a single wrapping text area.
*
* \param text the TTF_Text to modify.
* \param x the x offset of the upper left corner of this text in pixels.
* \param y the y offset of the upper left corner of this text in pixels.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextPosition(TTF_Text *text, int x, int y);

/**
* Get the position of a text object.
*
* \param text the TTF_Text to query.
* \param x a pointer filled in with the x offset of the upper left corner of this text in pixels, may be NULL.
* \param y a pointer filled in with the y offset of the upper left corner of this text in pixels, may be NULL.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextPosition(TTF_Text *text, int *x, int *y);

/**
* Set the UTF-8 text used by a text object.
*
Expand All @@ -1587,6 +1635,11 @@ extern SDL_DECLSPEC TTF_Font * SDLCALL TTF_GetTextFont(TTF_Text *text);
* text.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextString(TTF_Text *text, const char *string, size_t length);

Expand All @@ -1603,6 +1656,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextString(TTF_Text *text, const char *s
* text.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_InsertTextString(TTF_Text *text, int offset, const char *string, size_t length);

Expand All @@ -1615,6 +1673,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_InsertTextString(TTF_Text *text, int offset
* text.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_AppendTextString(TTF_Text *text, const char *string, size_t length);

Expand All @@ -1630,35 +1693,45 @@ extern SDL_DECLSPEC bool SDLCALL TTF_AppendTextString(TTF_Text *text, const char
* remainder of the string.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_DeleteTextString(TTF_Text *text, int offset, int length);

/**
* Set whether wrapping is enabled on a text object.
*
* \param text the TTF_Text to modify.
* \param wrap true if wrapping should be enabled, false if it should be
* disabled.
* \param wrapLength the maximum width in pixels, 0 to wrap on newline
* characters, or -1 to leave wrapLength unchanged.
* characters.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextWrapping(TTF_Text *text, bool wrap, int wrapLength);
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextWrapping(TTF_Text *text, int wrapLength);

/**
* Get whether wrapping is enabled on a text object.
*
* \param text the TTF_Text to query.
* \param wrap a pointer filled in with true if wrapping is enabled, false if
* it is disabled, may be NULL.
* \param wrapLength a pointer filled in with the maximum width in pixels or 0
* if the text is being wrapped on newline characters, may
* be NULL.
* if the text is being wrapped on newline characters.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextWrapping(TTF_Text *text, bool *wrap, int *wrapLength);
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextWrapping(TTF_Text *text, int *wrapLength);

/**
* Get the size of a text object.
Expand All @@ -1673,6 +1746,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetTextWrapping(TTF_Text *text, bool *wrap,
* NULL.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSize(TTF_Text *text, int *w, int *h);

Expand Down Expand Up @@ -1727,6 +1805,11 @@ typedef struct TTF_SubString
* offset.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSubString(TTF_Text *text, int offset, TTF_SubString *substring);

Expand All @@ -1745,6 +1828,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSubString(TTF_Text *text, int offset
* offset.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSubStringForLine(TTF_Text *text, int line, TTF_SubString *substring);

Expand All @@ -1761,6 +1849,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSubStringForLine(TTF_Text *text, int
* call SDL_GetError() for more information. This is a single
* allocation that should be freed with SDL_free() when it is no
* longer needed.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC TTF_SubString ** SDLCALL TTF_GetTextSubStringsForRange(TTF_Text *text, int offset, int length, int *count);

Expand All @@ -1778,6 +1871,11 @@ extern SDL_DECLSPEC TTF_SubString ** SDLCALL TTF_GetTextSubStringsForRange(TTF_T
* the given point.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSubStringForPoint(TTF_Text *text, int x, int y, TTF_SubString *substring);

Expand All @@ -1791,6 +1889,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetTextSubStringForPoint(TTF_Text *text, in
* \param substring the TTF_SubString to query.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetPreviousTextSubString(TTF_Text *text, const TTF_SubString *substring, TTF_SubString *previous);

Expand All @@ -1805,6 +1908,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetPreviousTextSubString(TTF_Text *text, co
* \param next a pointer filled in with the next substring.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetNextTextSubString(TTF_Text *text, const TTF_SubString *substring, TTF_SubString *next);

Expand All @@ -1818,6 +1926,11 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetNextTextSubString(TTF_Text *text, const
* \param text the TTF_Text to update.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_UpdateText(TTF_Text *text);

Expand Down
Loading

0 comments on commit 4924574

Please sign in to comment.