diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-07-15 18:18:45 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-07-15 18:18:45 +0300 |
commit | aed6837cc1e1bfb9ccb238900ad7387444b624eb (patch) | |
tree | 3b502166a9b9cd69b46ea6c11949be82bedaff53 /src/ui/text.c | |
parent | 9f7bbeecba762213c7dec4555e0cbb1da2b2ea66 (diff) |
Fixing regressions text metrics, InputWidget
`run_Font_` was moving the Y cursor position twice for each line break.
Checking for the HarfBuzz UNSAFE_TO_BREAK flag leads to some unexpected behavior near edges of words.
The old `tryAdvanceNoWrap` method should return the maximum horizontal advance of the text, and not the cursor position's advance.
`draw_WrapText` used the wrong foreground color.
`TextBuf` now uses WrapText to do all the measuring and drawing, making things much simpler.
Diffstat (limited to 'src/ui/text.c')
-rw-r--r-- | src/ui/text.c | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/ui/text.c b/src/ui/text.c index 0a3b7b2e..b283d700 100644 --- a/src/ui/text.c +++ b/src/ui/text.c | |||
@@ -1450,7 +1450,7 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { | |||
1450 | wrapResumePos = run->logical.end; | 1450 | wrapResumePos = run->logical.end; |
1451 | wrapRuns.end = runIndex; | 1451 | wrapRuns.end = runIndex; |
1452 | wrapResumeRunIndex = runIndex + 1; | 1452 | wrapResumeRunIndex = runIndex + 1; |
1453 | yCursor += d->height; | 1453 | //yCursor += d->height; |
1454 | break; | 1454 | break; |
1455 | } | 1455 | } |
1456 | wrapResumeRunIndex = runCount; | 1456 | wrapResumeRunIndex = runCount; |
@@ -1489,10 +1489,10 @@ static iRect run_Font_(iFont *d, const iRunArgs *args) { | |||
1489 | prevCh = ch; | 1489 | prevCh = ch; |
1490 | } | 1490 | } |
1491 | else { | 1491 | else { |
1492 | if (~glyphFlags & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) { | 1492 | //if (~glyphFlags & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) { |
1493 | safeBreakPos = logPos; | 1493 | safeBreakPos = logPos; |
1494 | breakAdvance = wrapAdvance; | 1494 | breakAdvance = wrapAdvance; |
1495 | } | 1495 | //} |
1496 | } | 1496 | } |
1497 | /* Out of room? */ | 1497 | /* Out of room? */ |
1498 | if (wrapAdvance + xOffset + glyph->d[0].x + glyph->rect[0].size.x > | 1498 | if (wrapAdvance + xOffset + glyph->d[0].x + glyph->rect[0].size.x > |
@@ -1787,7 +1787,8 @@ iInt2 tryAdvanceNoWrap_Text(int fontId, iRangecc text, int width, const char **e | |||
1787 | .maxWidth = width, | 1787 | .maxWidth = width, |
1788 | .wrapFunc = cbAdvanceOneLine_, | 1788 | .wrapFunc = cbAdvanceOneLine_, |
1789 | .context = endPos }; | 1789 | .context = endPos }; |
1790 | return measure_WrapText(&wrap, fontId).bounds.size; | 1790 | iTextMetrics tm = measure_WrapText(&wrap, fontId); |
1791 | return init_I2(maxWidth_TextMetrics(tm), tm.bounds.size.y); | ||
1791 | } | 1792 | } |
1792 | 1793 | ||
1793 | iTextMetrics measureN_Text(int fontId, const char *text, size_t n) { | 1794 | iTextMetrics measureN_Text(int fontId, const char *text, size_t n) { |
@@ -1969,7 +1970,7 @@ void draw_WrapText(iWrapText *d, int fontId, iInt2 pos, int color) { | |||
1969 | .text = d->text, | 1970 | .text = d->text, |
1970 | .pos = pos, | 1971 | .pos = pos, |
1971 | .wrap = d, | 1972 | .wrap = d, |
1972 | .color = color, | 1973 | .color = color & mask_ColorId, |
1973 | }); | 1974 | }); |
1974 | } | 1975 | } |
1975 | 1976 | ||
@@ -2088,6 +2089,13 @@ iDefineTypeConstructionArgs(TextBuf, (int font, int color, const char *text), fo | |||
2088 | 2089 | ||
2089 | static void initWrap_TextBuf_(iTextBuf *d, int font, int color, int maxWidth, iBool doWrap, const char *text) { | 2090 | static void initWrap_TextBuf_(iTextBuf *d, int font, int color, int maxWidth, iBool doWrap, const char *text) { |
2090 | SDL_Renderer *render = text_.render; | 2091 | SDL_Renderer *render = text_.render; |
2092 | iWrapText wrapText = { | ||
2093 | .text = range_CStr(text), | ||
2094 | .maxWidth = maxWidth, | ||
2095 | .mode = (doWrap ? word_WrapTextMode : anyCharacter_WrapTextMode), | ||
2096 | }; | ||
2097 | d->size = measure_WrapText(&wrapText, font).bounds.size; | ||
2098 | #if 0 | ||
2091 | if (maxWidth == 0) { | 2099 | if (maxWidth == 0) { |
2092 | d->size = measure_Text(font, text).bounds.size; | 2100 | d->size = measure_Text(font, text).bounds.size; |
2093 | } | 2101 | } |
@@ -2101,6 +2109,7 @@ static void initWrap_TextBuf_(iTextBuf *d, int font, int color, int maxWidth, iB | |||
2101 | d->size.y += iMax(size.y, lineHeight_Text(font)); | 2109 | d->size.y += iMax(size.y, lineHeight_Text(font)); |
2102 | } | 2110 | } |
2103 | } | 2111 | } |
2112 | #endif | ||
2104 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); | 2113 | SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); |
2105 | if (d->size.x * d->size.y) { | 2114 | if (d->size.x * d->size.y) { |
2106 | d->texture = SDL_CreateTexture(render, | 2115 | d->texture = SDL_CreateTexture(render, |
@@ -2119,7 +2128,7 @@ static void initWrap_TextBuf_(iTextBuf *d, int font, int color, int maxWidth, iB | |||
2119 | SDL_SetRenderDrawColor(render, 255, 255, 255, 0); | 2128 | SDL_SetRenderDrawColor(render, 255, 255, 255, 0); |
2120 | SDL_RenderClear(render); | 2129 | SDL_RenderClear(render); |
2121 | SDL_SetTextureBlendMode(text_.cache, SDL_BLENDMODE_NONE); /* blended when TextBuf is drawn */ | 2130 | SDL_SetTextureBlendMode(text_.cache, SDL_BLENDMODE_NONE); /* blended when TextBuf is drawn */ |
2122 | const int fg = color | fillBackground_ColorId; | 2131 | #if 0 |
2123 | iRangecc range = range_CStr(text); | 2132 | iRangecc range = range_CStr(text); |
2124 | if (maxWidth == 0) { | 2133 | if (maxWidth == 0) { |
2125 | draw_Text_(font, zero_I2(), fg, range); | 2134 | draw_Text_(font, zero_I2(), fg, range); |
@@ -2137,6 +2146,8 @@ static void initWrap_TextBuf_(iTextBuf *d, int font, int color, int maxWidth, iB | |||
2137 | pos.y += lineHeight_Text(font); | 2146 | pos.y += lineHeight_Text(font); |
2138 | } | 2147 | } |
2139 | } | 2148 | } |
2149 | #endif | ||
2150 | draw_WrapText(&wrapText, font, zero_I2(), color | fillBackground_ColorId); | ||
2140 | SDL_SetTextureBlendMode(text_.cache, SDL_BLENDMODE_BLEND); | 2151 | SDL_SetTextureBlendMode(text_.cache, SDL_BLENDMODE_BLEND); |
2141 | SDL_SetRenderTarget(render, oldTarget); | 2152 | SDL_SetRenderTarget(render, oldTarget); |
2142 | SDL_SetTextureBlendMode(d->texture, SDL_BLENDMODE_BLEND); | 2153 | SDL_SetTextureBlendMode(d->texture, SDL_BLENDMODE_BLEND); |