diff --git a/source/core/screen.c b/source/core/screen.c index 881a3e4..4a309f6 100644 --- a/source/core/screen.c +++ b/source/core/screen.c @@ -534,6 +534,8 @@ static void screen_wrap_string(u32* lines, float* lineWidths, float* lineHeights float lh = 0; u32 linePos = 0; u32 lastAlignPos = 0; + int wordPos = -1; + float ww = 0; const uint8_t* p = (const uint8_t*) text; u32 code = 0; @@ -542,7 +544,15 @@ static void screen_wrap_string(u32* lines, float* lineWidths, float* lineHeights while(*p && (units = decode_utf8(&code, p)) != -1 && code > 0) { p += units; - float charWidth = scaleX * fontGetCharWidthInfo(fontGlyphIndexFromCodePoint(code))->charWidth; + float charWidth = 1; + if(code == '\t') { + code = ' '; + charWidth = 4 - (linePos - lastAlignPos) % 4; + + lastAlignPos = linePos; + } + + charWidth *= scaleX * fontGetCharWidthInfo(fontGlyphIndexFromCodePoint(code))->charWidth; if(code == '\n' || (wordWrap && lw + charWidth >= maxWidth)) { if(code == '\n') { @@ -550,21 +560,40 @@ static void screen_wrap_string(u32* lines, float* lineWidths, float* lineHeights lh = scaleY * fontGetInfo()->lineFeed; } + u32 oldLinePos = linePos; + + if(code != '\n' && wordPos != -1) { + linePos = (u32) wordPos; + lw -= ww; + } + screen_wrap_string_finish_line(&w, &h, &lw, &lh, &line, &linePos, &lastAlignPos, lines, lineWidths, lineHeights, maxLines); + + if(code != '\n' && wordPos != -1) { + linePos = oldLinePos - wordPos; + lw = ww; + } + + wordPos = -1; + ww = 0; + } + + if(code == ' ') { + wordPos = -1; + ww = 0; + } else if(wordPos == -1) { + wordPos = (int) linePos; + ww = 0; } if(code != '\n') { - u32 num = 1; - if(code == '\t') { - code = ' '; - num = 4 - (linePos - lastAlignPos) % 4; - - lastAlignPos = linePos; + if(wordPos != -1) { + ww += charWidth; } - lw += charWidth * num; + lw += charWidth; lh = scaleY * fontGetInfo()->lineFeed; linePos++; diff --git a/source/fbi/task/listtitledb.c b/source/fbi/task/listtitledb.c index 8c32721..1f22f23 100644 --- a/source/fbi/task/listtitledb.c +++ b/source/fbi/task/listtitledb.c @@ -171,11 +171,25 @@ static void task_populate_titledb_thread(void* arg) { if(titledbInfo != NULL) { titledbInfo->id = (u32) json_object_get_integer(entry, "id", 0); strncpy(titledbInfo->category, json_object_get_string(entry, "category", "Unknown"), sizeof(titledbInfo->category)); - strncpy(titledbInfo->headline, json_object_get_string(entry, "headline", ""), sizeof(titledbInfo->headline)); strncpy(titledbInfo->updatedAt, json_object_get_string(entry, "updated_at", ""), sizeof(titledbInfo->updatedAt)); strncpy(titledbInfo->meta.shortDescription, json_object_get_string(entry, "name", ""), sizeof(titledbInfo->meta.shortDescription)); strncpy(titledbInfo->meta.publisher, json_object_get_string(entry, "author", ""), sizeof(titledbInfo->meta.publisher)); + json_t* headline = json_object_get(entry, "headline"); + if(json_is_string(headline)) { + const char* val = json_string_value(headline); + + if(json_string_length(headline) > sizeof(titledbInfo->headline) - 1) { + snprintf(titledbInfo->headline, sizeof(titledbInfo->headline), "%.508s...", val); + } else { + strncpy(titledbInfo->headline, val, sizeof(titledbInfo->headline)); + } + } else { + titledbInfo->headline[0] = '\0'; + } + + + json_t* cias = json_object_get(entry, "cia"); if(json_is_array(cias)) { for(u32 j = 0; j < json_array_size(cias); j++) { diff --git a/source/fbi/task/uitask.c b/source/fbi/task/uitask.c index abef147..b1e6991 100644 --- a/source/fbi/task/uitask.c +++ b/source/fbi/task/uitask.c @@ -288,7 +288,6 @@ void task_draw_titledb_info(ui_view* view, void* data, float x1, float y1, float float infoWidth; screen_get_string_size_wrap(&infoWidth, NULL, infoText, 0.5f, 0.5f, x2 - x1 - 10); - // TODO: Wrap by word, not character? float infoX = x1 + (x2 - x1 - infoWidth) / 2; float infoY = y1 + (y2 - y1) / 2 - 8; screen_draw_string_wrap(infoText, infoX, infoY, 0.5f, 0.5f, COLOR_TEXT, true, infoX + infoWidth + 1);