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

Better carriage-return + newline handling #399

Merged
merged 1 commit into from
Oct 4, 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
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
Loading