Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #417

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions examples/editbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ static void SaveCandidates(EditBox *edit, const SDL_Event *event)
}
*dst = '\0';

edit->candidates = TTF_CreateText_Wrapped(TTF_GetTextEngine(edit->text), edit->font, candidate_text, 0, 0);
edit->candidates = TTF_CreateText(TTF_GetTextEngine(edit->text), edit->font, candidate_text, 0);
SDL_free(candidate_text);
if (edit->candidates) {
SDL_copyp(&edit->candidates->color, &edit->text->color);
float r, g, b, a;
TTF_GetTextColorFloat(edit->text, &r, &g, &b, &a);
TTF_SetTextColorFloat(edit->candidates, r, g, b, a);
} else {
ClearCandidates(edit);
}
Expand Down Expand Up @@ -342,6 +344,9 @@ EditBox *EditBox_Create(SDL_Window *window, SDL_Renderer *renderer, TTF_TextEngi
edit->highlight1 = -1;
edit->highlight2 = -1;

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

#ifdef TEST_SURFACE_ENGINE
/* Grab the window surface if we want to test the surface text engine.
* This isn't strictly necessary, we can still use the renderer if it's
Expand Down
5 changes: 1 addition & 4 deletions examples/showfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,7 @@ int main(int argc, char *argv[])
editRect.w -= 8.0f;
scene.edit = EditBox_Create(scene.window, scene.renderer, engine, font, &editRect);
if (scene.edit) {
scene.edit->text->color.r = forecol->r / 255.0f;
scene.edit->text->color.g = forecol->g / 255.0f;
scene.edit->text->color.b = forecol->b / 255.0f;
scene.edit->text->color.a = forecol->a / 255.0f;
TTF_SetTextColor(scene.edit->text, forecol->r, forecol->g, forecol->b, forecol->a);

EditBox_Insert(scene.edit, message);
}
Expand Down
1 change: 1 addition & 0 deletions include/SDL3_ttf/SDL_textengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ typedef struct TTF_TextLayout TTF_TextLayout;
struct TTF_TextData
{
TTF_Font *font; /**< The font used by this text, read-only. */
SDL_FColor color; /**< The color of the text, read-only. */

bool needs_layout_update; /**< True if the layout needs to be updated */
TTF_TextLayout *layout; /**< Cached layout information, read-only. */
Expand Down
155 changes: 154 additions & 1 deletion include/SDL3_ttf/SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,6 @@ typedef struct TTF_TextData TTF_TextData;
typedef struct TTF_Text
{
char *text; /**< A copy of the text used to create this text object, useful for layout and debugging. This will be freed automatically when the object is destroyed. */
SDL_FColor color; /**< The color of the text, read-write. You can change this anytime. */
int num_lines; /**< The number of lines in the text, 0 if it's empty */

int refcount; /**< Application reference count, used when freeing surface */
Expand Down Expand Up @@ -1549,6 +1548,8 @@ extern SDL_DECLSPEC SDL_PropertiesID SDLCALL TTF_GetTextProperties(TTF_Text *tex
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_GetTextEngine
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextEngine(TTF_Text *text, TTF_TextEngine *engine);

Expand All @@ -1563,6 +1564,8 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextEngine(TTF_Text *text, TTF_TextEngin
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_SetTextEngine
*/
extern SDL_DECLSPEC TTF_TextEngine * SDLCALL TTF_GetTextEngine(TTF_Text *text);

Expand All @@ -1580,6 +1583,8 @@ extern SDL_DECLSPEC TTF_TextEngine * SDLCALL TTF_GetTextEngine(TTF_Text *text);
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_GetTextFont
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextFont(TTF_Text *text, TTF_Font *font);

Expand All @@ -1594,9 +1599,99 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextFont(TTF_Text *text, TTF_Font *font)
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_SetTextFont
*/
extern SDL_DECLSPEC TTF_Font * SDLCALL TTF_GetTextFont(TTF_Text *text);

/**
* Set the color of a text object.
*
* The default text color is white (255, 255, 255, 255).
*
* \param text the TTF_Text to modify.
* \param r the red color value in the range of 0-255.
* \param g the green color value in the range of 0-255.
* \param b the blue color value in the range of 0-255.
* \param a the alpha value in the range of 0-255.
* \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.
*
* \sa TTF_GetTextColor
* \sa TTF_SetTextColorFloat
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextColor(TTF_Text *text, Uint8 r, Uint8 g, Uint8 b, Uint8 a);

/**
* Set the color of a text object.
*
* The default text color is white (1.0f, 1.0f, 1.0f, 1.0f).
*
* \param text the TTF_Text to modify.
* \param r the red color value, normally in the range of 0-1.
* \param g the green color value, normally in the range of 0-1.
* \param b the blue color value, normally in the range of 0-1.
* \param a the alpha value in the range of 0-1.
* \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.
*
* \sa TTF_GetTextColorFloat
* \sa TTF_SetTextColor
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextColorFloat(TTF_Text *text, float r, float g, float b, float a);

/**
* Get the color of a text object.
*
* \param text the TTF_Text to query.
* \param r a pointer filled in with the red color value in the range of 0-255, may be NULL.
* \param g a pointer filled in with the green color value in the range of 0-255, may be NULL.
* \param b a pointer filled in with the blue color value in the range of 0-255, may be NULL.
* \param a a pointer filled in with the alpha value in the range of 0-255, may be 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.
*
* \sa TTF_GetTextColorFloat
* \sa TTF_SetTextColor
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextColor(TTF_Text *text, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a);

/**
* Get the color of a text object.
*
* \param text the TTF_Text to query.
* \param r a pointer filled in with the red color value, normally in the range of 0-1, may be NULL.
* \param g a pointer filled in with the green color value, normally in the range of 0-1, may be NULL.
* \param b a pointer filled in with the blue color value, normally in the range of 0-1, may be NULL.
* \param a a pointer filled in with the alpha value in the range of 0-1, may be 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.
*
* \sa TTF_GetTextColor
* \sa TTF_SetTextColorFloat
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextColorFloat(TTF_Text *text, float *r, float *g, float *b, float *a);

/**
* Set the position of a text object.
*
Expand All @@ -1611,6 +1706,8 @@ extern SDL_DECLSPEC TTF_Font * SDLCALL TTF_GetTextFont(TTF_Text *text);
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_GetTextPosition
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextPosition(TTF_Text *text, int x, int y);

Expand All @@ -1627,6 +1724,8 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextPosition(TTF_Text *text, int x, int
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_SetTextPosition
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextPosition(TTF_Text *text, int *x, int *y);

Expand All @@ -1644,6 +1743,10 @@ extern SDL_DECLSPEC bool SDLCALL TTF_GetTextPosition(TTF_Text *text, int *x, int
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_AppendTextString
* \sa TTF_DeleteTextString
* \sa TTF_InsertTextString
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextString(TTF_Text *text, const char *string, size_t length);

Expand All @@ -1665,6 +1768,10 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextString(TTF_Text *text, const char *s
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_AppendTextString
* \sa TTF_DeleteTextString
* \sa TTF_SetTextString
*/
extern SDL_DECLSPEC bool SDLCALL TTF_InsertTextString(TTF_Text *text, int offset, const char *string, size_t length);

Expand All @@ -1682,6 +1789,10 @@ extern SDL_DECLSPEC bool SDLCALL TTF_InsertTextString(TTF_Text *text, int offset
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_DeleteTextString
* \sa TTF_InsertTextString
* \sa TTF_SetTextString
*/
extern SDL_DECLSPEC bool SDLCALL TTF_AppendTextString(TTF_Text *text, const char *string, size_t length);

Expand All @@ -1702,6 +1813,10 @@ extern SDL_DECLSPEC bool SDLCALL TTF_AppendTextString(TTF_Text *text, const char
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_AppendTextString
* \sa TTF_InsertTextString
* \sa TTF_SetTextString
*/
extern SDL_DECLSPEC bool SDLCALL TTF_DeleteTextString(TTF_Text *text, int offset, int length);

Expand All @@ -1718,6 +1833,8 @@ extern SDL_DECLSPEC bool SDLCALL TTF_DeleteTextString(TTF_Text *text, int offset
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_GetTextWrapping
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextWrapping(TTF_Text *text, int wrapLength);

Expand All @@ -1734,9 +1851,45 @@ extern SDL_DECLSPEC bool SDLCALL TTF_SetTextWrapping(TTF_Text *text, int wrapLen
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_SetTextWrapping
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetTextWrapping(TTF_Text *text, int *wrapLength);

/**
* Set whether whitespace should be visible when wrapping a text object.
*
* If the whitespace is visible, it will take up space for purposes of alignment and wrapping. This is good for editing, but looks better when centered or aligned if whitespace around line wrapping is hidden. This defaults false.
*
* \param text the TTF_Text to modify.
* \param visible true to show whitespace when wrapping text, false to hide it.
* \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.
*
* \sa TTF_TextWrapWhitespaceVisible
*/
extern SDL_DECLSPEC bool SDLCALL TTF_SetTextWrapWhitespaceVisible(TTF_Text *text, bool visible);

/**
* Return whether whitespace is shown when wrapping a text object.
*
* \param text the TTF_Text to query.
* \returns true if whitespace is shown when wrapping text, or false otherwise.
*
* \threadsafety This function should be called on the thread that created the
* text.
*
* \since This function is available since SDL_ttf 3.0.0.
*
* \sa TTF_SetTextWrapWhitespaceVisible
*/
extern SDL_DECLSPEC bool SDLCALL TTF_TextWrapWhitespaceVisible(TTF_Text *text);

/**
* Get the size of a text object.
*
Expand Down
2 changes: 1 addition & 1 deletion src/SDL_renderer_textengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ bool TTF_DrawRendererText(TTF_Text *text, float x, float y)
SDL_RenderGeometryRaw(renderer,
sequence->texture,
sequence->positions, 2 * sizeof(float),
&text->color, 0,
&text->internal->color, 0,
sequence->texcoords, 2 * sizeof(float),
sequence->num_rects * 4,
sequence->indices, sequence->num_rects * 6, sizeof(*sequence->indices));
Expand Down
10 changes: 5 additions & 5 deletions src/SDL_surface_textengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ bool TTF_DrawSurfaceText(TTF_Text *text, int x, int y, SDL_Surface *surface)
return true;
}

if (text->color.r != data->fcolor.r ||
text->color.g != data->fcolor.g ||
text->color.b != data->fcolor.b ||
text->color.a != data->fcolor.a) {
UpdateColor(data, &text->color);
if (text->internal->color.r != data->fcolor.r ||
text->internal->color.g != data->fcolor.g ||
text->internal->color.b != data->fcolor.b ||
text->internal->color.a != data->fcolor.a) {
UpdateColor(data, &text->internal->color);
}

for (int i = 0; i < data->num_ops; ++i) {
Expand Down
Loading
Loading