Skip to content

Commit

Permalink
Better carriage-return + newline handling
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 4, 2024
1 parent 4be7c39 commit 0abb68e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
26 changes: 13 additions & 13 deletions examples/editbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
#define CURSOR_BLINK_INTERVAL_MS 500


static bool GetHighlightExtents(EditBox *edit, int *marker1, int *marker2)
{
if (edit->highlight1 >= 0 && edit->highlight2 >= 0) {
*marker1 = SDL_min(edit->highlight1, edit->highlight2);
*marker2 = SDL_max(edit->highlight1, edit->highlight2) - 1;
if (*marker2 > *marker1) {
return true;
}
}
return false;
}

EditBox *EditBox_Create(TTF_Text *text, const SDL_FRect *rect)
{
EditBox *edit = (EditBox *)SDL_calloc(1, sizeof(*edit));
Expand All @@ -51,6 +39,18 @@ void EditBox_Destroy(EditBox *edit)
SDL_free(edit);
}

static bool GetHighlightExtents(EditBox *edit, int *marker1, int *marker2)
{
if (edit->highlight1 >= 0 && edit->highlight2 >= 0) {
*marker1 = SDL_min(edit->highlight1, edit->highlight2);
*marker2 = SDL_max(edit->highlight1, edit->highlight2) - 1;
if (*marker2 > *marker1) {
return true;
}
}
return false;
}

void EditBox_Draw(EditBox *edit, SDL_Renderer *renderer)
{
if (!edit) {
Expand Down Expand Up @@ -137,7 +137,7 @@ static void MoveCursorIndex(EditBox *edit, int direction)
}
} else {
if (TTF_GetTextSubString(edit->text, edit->cursor, &substring) &&
TTF_GetTextSubString(edit->text, substring.offset + substring.length, &substring)) {
TTF_GetTextSubString(edit->text, substring.offset + SDL_max(substring.length, 1), &substring)) {
edit->cursor = substring.offset;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3449,7 +3449,7 @@ static bool GetWrappedLines(TTF_Font *font, const char *text, size_t length, int
save_text = spot;
save_length = left;
// Break, if new line
if (c == '\n' || c == '\r') {
if (c == '\n' || (c == '\r' && *spot != '\n')) {
break;
}
}
Expand All @@ -3472,7 +3472,7 @@ static bool GetWrappedLines(TTF_Font *font, const char *text, size_t length, int
TTF_Line *line = &strLines[i];
while (line->length > 0 &&
CharacterIsDelimiter(line->text[line->length - 1]) &&
line->text[line->length - 1] != '\n') {
!CharacterIsNewLine(line->text[line->length - 1])) {
--line->length;
}
}
Expand Down Expand Up @@ -3854,6 +3854,10 @@ static bool LayoutTextWrapped(TTF_Text *text)
for (i = 0; i < numLines; i++) {
int xstart, ystart, line_width, xoffset;

if (strLines[i].length == 0) {
continue;
}

// Initialize xstart, ystart and compute positions
if (!TTF_Size_Internal(font, strLines[i].text, strLines[i].length, &line_width, NULL, &xstart, &ystart, NO_MEASUREMENT)) {
goto done;
Expand Down

0 comments on commit 0abb68e

Please sign in to comment.