diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-07-21 08:55:12 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-07-21 08:55:12 +0300 |
commit | 9ab619bd3c57d681a3906abf0dc10d5376d01319 (patch) | |
tree | 664d546bb36cd24afcf6d5fec2d0d7289b1f97aa /src/ui/inputwidget.c | |
parent | aaa5d18c3f1540c18bb9dae80927df2803c42049 (diff) |
Text: Finding the advance position of a character
Diffstat (limited to 'src/ui/inputwidget.c')
-rw-r--r-- | src/ui/inputwidget.c | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/ui/inputwidget.c b/src/ui/inputwidget.c index a5e8cf43..f7d41a44 100644 --- a/src/ui/inputwidget.c +++ b/src/ui/inputwidget.c | |||
@@ -1090,8 +1090,13 @@ static iBool findXBreaks_LineMover_(iWrapText *wrap, iRangecc wrappedText, | |||
1090 | static iBool moveCursorByLine_InputWidget_(iInputWidget *d, int dir) { | 1090 | static iBool moveCursorByLine_InputWidget_(iInputWidget *d, int dir) { |
1091 | const iInputLine *line = cursorLine_InputWidget_(d); | 1091 | const iInputLine *line = cursorLine_InputWidget_(d); |
1092 | iRangecc text = range_String(&line->text); | 1092 | iRangecc text = range_String(&line->text); |
1093 | iInt2 relCoord = measureRange_Text(d->font, (iRangecc){ text.start, | 1093 | iWrapText wrapText = { |
1094 | text.start + d->cursor.x }).advance; | 1094 | .text = rangeSize_String(&line->text, d->cursor.x), |
1095 | .maxWidth = width_Rect(contentBounds_InputWidget_(d)), | ||
1096 | .mode = (d->inFlags & isUrl_InputWidgetFlag ? anyCharacter_WrapTextMode | ||
1097 | : word_WrapTextMode), | ||
1098 | }; | ||
1099 | iInt2 relCoord = measure_WrapText(&wrapText, d->font).advance; | ||
1095 | const int cursorWidth = measureN_Text(d->font, charPos_InputWidget_(d, d->cursor), 1).advance.x; | 1100 | const int cursorWidth = measureN_Text(d->font, charPos_InputWidget_(d, d->cursor), 1).advance.x; |
1096 | int relLine = relCoord.y / lineHeight_Text(d->font); | 1101 | int relLine = relCoord.y / lineHeight_Text(d->font); |
1097 | if ((dir < 0 && relLine > 0) || (dir > 0 && relLine < numWrapLines_InputLine_(line) - 1)) { | 1102 | if ((dir < 0 && relLine > 0) || (dir > 0 && relLine < numWrapLines_InputLine_(line) - 1)) { |
@@ -1111,13 +1116,8 @@ static iBool moveCursorByLine_InputWidget_(iInputWidget *d, int dir) { | |||
1111 | else { | 1116 | else { |
1112 | return iFalse; | 1117 | return iFalse; |
1113 | } | 1118 | } |
1114 | iWrapText wrapText = { | 1119 | wrapText.text = range_String(&cursorLine_InputWidget_(d)->text); |
1115 | .text = range_String(&cursorLine_InputWidget_(d)->text), | 1120 | wrapText.hitPoint = addY_I2(relCoord, 1); //arelCddX_I2(relCoord, cursorWidth), |
1116 | .maxWidth = width_Rect(contentBounds_InputWidget_(d)), | ||
1117 | .mode = (d->inFlags & isUrl_InputWidgetFlag ? anyCharacter_WrapTextMode | ||
1118 | : word_WrapTextMode), | ||
1119 | .hitPoint = addY_I2(relCoord, 1), //arelCddX_I2(relCoord, cursorWidth), | ||
1120 | }; | ||
1121 | measure_WrapText(&wrapText, d->font); | 1121 | measure_WrapText(&wrapText, d->font); |
1122 | iAssert(wrapText.hitChar_out); | 1122 | iAssert(wrapText.hitChar_out); |
1123 | d->cursor.x = wrapText.hitChar_out - wrapText.text.start; | 1123 | d->cursor.x = wrapText.hitChar_out - wrapText.text.start; |
@@ -1129,7 +1129,8 @@ static iBool moveCursorByLine_InputWidget_(iInputWidget *d, int dir) { | |||
1129 | d->cursor.x += n; | 1129 | d->cursor.x += n; |
1130 | } | 1130 | } |
1131 | }*/ | 1131 | }*/ |
1132 | showCursor_InputWidget_(d); | 1132 | //showCursor_InputWidget_(d); |
1133 | setCursor_InputWidget(d, d->cursor); | ||
1133 | return iTrue; | 1134 | return iTrue; |
1134 | 1135 | ||
1135 | #if 0 | 1136 | #if 0 |
@@ -1896,20 +1897,28 @@ static iBool draw_MarkPainter_(iWrapText *wrapText, iRangecc wrappedText, int or | |||
1896 | wrappedText.end - cstr + mp->line->range.start | 1897 | wrappedText.end - cstr + mp->line->range.start |
1897 | }; | 1898 | }; |
1898 | if (mark.end <= lineRange.start || mark.start >= lineRange.end) { | 1899 | if (mark.end <= lineRange.start || mark.start >= lineRange.end) { |
1900 | mp->pos.y += lineHeight_Text(mp->d->font); | ||
1899 | return iTrue; /* outside of mark */ | 1901 | return iTrue; /* outside of mark */ |
1900 | } | 1902 | } |
1901 | iRect rect = { addX_I2(mp->pos, origin), init_I2(advance, lineHeight_Text(mp->d->font)) }; | 1903 | iRect rect = { addX_I2(mp->pos, origin), init_I2(advance, lineHeight_Text(mp->d->font)) }; |
1902 | if (mark.end < lineRange.end) { | 1904 | if (mark.end < lineRange.end) { |
1903 | /* Calculate where the mark ends. */ | 1905 | /* Calculate where the mark ends. */ |
1904 | const iRangecc markedPrefix = { cstr, cstr + mark.end - lineRange.start }; | 1906 | const iRangecc markedPrefix = { //cstr, cstr + mark.end - lineRange.start }; |
1907 | wrappedText.start, | ||
1908 | wrappedText.start + mark.end - lineRange.start | ||
1909 | }; | ||
1905 | rect.size.x = measureRange_Text(mp->d->font, markedPrefix).advance.x; | 1910 | rect.size.x = measureRange_Text(mp->d->font, markedPrefix).advance.x; |
1906 | } | 1911 | } |
1907 | if (mark.start > lineRange.start) { | 1912 | if (mark.start > lineRange.start) { |
1908 | /* Calculate where the mark starts. */ | 1913 | /* Calculate where the mark starts. */ |
1909 | const iRangecc unmarkedPrefix = { cstr, cstr + mark.start - lineRange.start }; | 1914 | const iRangecc unmarkedPrefix = { //cstr, cstr + mark.start - lineRange.start |
1915 | wrappedText.start, | ||
1916 | wrappedText.start + mark.start - lineRange.start | ||
1917 | }; | ||
1910 | adjustEdges_Rect(&rect, 0, 0, 0, measureRange_Text(mp->d->font, unmarkedPrefix).advance.x); | 1918 | adjustEdges_Rect(&rect, 0, 0, 0, measureRange_Text(mp->d->font, unmarkedPrefix).advance.x); |
1911 | } | 1919 | } |
1912 | rect.size.x = iMax(gap_UI / 3, rect.size.x); | 1920 | rect.size.x = iMax(gap_UI / 3, rect.size.x); |
1921 | mp->pos.y += lineHeight_Text(mp->d->font); | ||
1913 | fillRect_Paint(mp->paint, rect, uiMarked_ColorId); | 1922 | fillRect_Paint(mp->paint, rect, uiMarked_ColorId); |
1914 | return iTrue; | 1923 | return iTrue; |
1915 | } | 1924 | } |
@@ -2041,11 +2050,11 @@ static void draw_InputWidget_(const iInputWidget *d) { | |||
2041 | /* The bounds include visible characters, while advance includes whitespace as well. | 2050 | /* The bounds include visible characters, while advance includes whitespace as well. |
2042 | Normally only the advance is needed, but if the cursor is at a newline, the advance | 2051 | Normally only the advance is needed, but if the cursor is at a newline, the advance |
2043 | will have reset back to zero. */ | 2052 | will have reset back to zero. */ |
2044 | wrapText.text = range_String(text); | 2053 | wrapText.text = range_String(text); |
2045 | wrapText.text.end = wrapText.text.start + d->cursor.x; | 2054 | wrapText.hitChar = wrapText.text.start + d->cursor.x; |
2046 | iAssert(wrapText.text.end <= constEnd_String(text)); | ||
2047 | //const int prefixSize = maxWidth_TextMetrics( | 2055 | //const int prefixSize = maxWidth_TextMetrics( |
2048 | const iTextMetrics tm = measure_WrapText(&wrapText, d->font); | 2056 | measure_WrapText(&wrapText, d->font); |
2057 | const iInt2 advance = wrapText.hitAdvance_out; | ||
2049 | // const iInt2 curPos = addX_I2(addY_I2(contentBounds.pos, lineHeight_Text(d->font) | 2058 | // const iInt2 curPos = addX_I2(addY_I2(contentBounds.pos, lineHeight_Text(d->font) |
2050 | // * d->cursorLine), | 2059 | // * d->cursorLine), |
2051 | // prefixSize + | 2060 | // prefixSize + |
@@ -2054,7 +2063,7 @@ static void draw_InputWidget_(const iInputWidget *d) { | |||
2054 | //printf("%d -> tm.advance: %d, %d\n", d->cursor.x, tm.advance.x, tm.advance.y); | 2063 | //printf("%d -> tm.advance: %d, %d\n", d->cursor.x, tm.advance.x, tm.advance.y); |
2055 | const iInt2 curPos = add_I2(addY_I2(topLeft_Rect(contentBounds), visLineOffsetY + | 2064 | const iInt2 curPos = add_I2(addY_I2(topLeft_Rect(contentBounds), visLineOffsetY + |
2056 | visWrapsAbove * lineHeight_Text(d->font)), | 2065 | visWrapsAbove * lineHeight_Text(d->font)), |
2057 | addX_I2(tm.advance, | 2066 | addX_I2(advance, |
2058 | (d->mode == insert_InputMode ? -curSize.x / 2 : 0))); | 2067 | (d->mode == insert_InputMode ? -curSize.x / 2 : 0))); |
2059 | const iRect curRect = { curPos, curSize }; | 2068 | const iRect curRect = { curPos, curSize }; |
2060 | fillRect_Paint(&p, curRect, uiInputCursor_ColorId); | 2069 | fillRect_Paint(&p, curRect, uiInputCursor_ColorId); |