diff --git a/escher/include/escher/text_area.h b/escher/include/escher/text_area.h index 845ce8b0e..1cce3b495 100644 --- a/escher/include/escher/text_area.h +++ b/escher/include/escher/text_area.h @@ -119,7 +119,7 @@ protected: bool removeEndOfLine() override; bool removeStartOfLine(); protected: - KDRect glyphFrameAtPosition(const char * position) const override; + KDRect glyphFrameAtPosition(const char * text, const char * position) const override; Text m_text; }; diff --git a/escher/include/escher/text_field.h b/escher/include/escher/text_field.h index 766051bc0..2cc18c2dc 100644 --- a/escher/include/escher/text_field.h +++ b/escher/include/escher/text_field.h @@ -67,7 +67,7 @@ protected: constexpr static int k_maxBufferSize = 152; private: void layoutSubviews() override; - KDRect glyphFrameAtPosition(const char * position) const override; + KDRect glyphFrameAtPosition(const char * buffer, const char * position) const override; bool m_isEditing; char * m_textBuffer; char * m_draftTextBuffer; diff --git a/escher/include/escher/text_input.h b/escher/include/escher/text_input.h index 24a8cdeb5..80de41a50 100644 --- a/escher/include/escher/text_input.h +++ b/escher/include/escher/text_input.h @@ -36,7 +36,7 @@ protected: protected: virtual void layoutSubviews() override; void reloadRectFromPosition(const char * position, bool lineBreak = false); - virtual KDRect glyphFrameAtPosition(const char * position) const = 0; + virtual KDRect glyphFrameAtPosition(const char * buffer, const char * position) const = 0; TextCursorView m_cursorView; const KDFont * m_font; const char * m_cursorLocation; diff --git a/escher/src/text_area.cpp b/escher/src/text_area.cpp index 3478e06ba..7e9ece6e7 100644 --- a/escher/src/text_area.cpp +++ b/escher/src/text_area.cpp @@ -426,7 +426,8 @@ bool TextArea::ContentView::removeStartOfLine() { return false; } -KDRect TextArea::ContentView::glyphFrameAtPosition(const char * position) const { +KDRect TextArea::ContentView::glyphFrameAtPosition(const char * text, const char * position) const { + assert(text == m_text.text()); KDSize glyphSize = m_font->glyphSize(); Text::Position p = m_text.positionAtPointer(position); diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 0ef6931e6..555e51c99 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -45,7 +45,7 @@ void TextField::ContentView::drawRect(KDContext * ctx, KDRect rect) const { backgroundColor = KDColorWhite; } ctx->fillRect(bounds(), backgroundColor); - ctx->drawString(text(), glyphFrameAtPosition(text()).origin(), m_font, m_textColor, backgroundColor); + ctx->drawString(text(), glyphFrameAtPosition(text(), text()).origin(), m_font, m_textColor, backgroundColor); } const char * TextField::ContentView::text() const { @@ -207,14 +207,15 @@ void TextField::ContentView::layoutSubviews() { TextInput::ContentView::layoutSubviews(); } -KDRect TextField::ContentView::glyphFrameAtPosition(const char * position) const { - assert(position != nullptr); +KDRect TextField::ContentView::glyphFrameAtPosition(const char * buffer, const char * position) const { + assert(buffer != nullptr && position != nullptr); + assert(position >= buffer); KDSize glyphSize = m_font->glyphSize(); KDCoordinate cursorWidth = m_cursorView.minimalSizeForOptimalDisplay().width(); KDCoordinate horizontalOffset = m_horizontalAlignment == 0.0f ? 0.0f : - m_horizontalAlignment * (m_frame.width() - m_font->stringSize(text()).width() - cursorWidth); + m_horizontalAlignment * (m_frame.width() - m_font->stringSize(buffer).width() - cursorWidth); return KDRect( - horizontalOffset + m_font->stringSizeUntil(text(), position).width(), + horizontalOffset + m_font->stringSizeUntil(buffer, position).width(), m_verticalAlignment * (m_frame.height() - glyphSize.height()), glyphSize); } diff --git a/escher/src/text_input.cpp b/escher/src/text_input.cpp index de0da525e..952dd79d5 100644 --- a/escher/src/text_input.cpp +++ b/escher/src/text_input.cpp @@ -20,7 +20,7 @@ void TextInput::ContentView::setFont(const KDFont * font) { } KDRect TextInput::ContentView::cursorRect() { - return glyphFrameAtPosition(m_cursorLocation); + return glyphFrameAtPosition(editedText(), m_cursorLocation); } void TextInput::ContentView::layoutSubviews() { @@ -32,7 +32,7 @@ void TextInput::ContentView::reloadRectFromPosition(const char * position, bool } KDRect TextInput::ContentView::dirtyRectFromPosition(const char * position, bool lineBreak) const { - KDRect glyphRect = glyphFrameAtPosition(position); + KDRect glyphRect = glyphFrameAtPosition(text(), position); KDRect dirtyRect = KDRect( glyphRect.x(), glyphRect.y(), diff --git a/kandinsky/src/font.cpp b/kandinsky/src/font.cpp index 68c01495d..4915337c5 100644 --- a/kandinsky/src/font.cpp +++ b/kandinsky/src/font.cpp @@ -6,7 +6,7 @@ constexpr static int k_tabCharacterWidth = 4; KDSize KDFont::stringSizeUntil(const char * text, const char * limit) const { - if (text == nullptr) { + if (text == nullptr || (limit != nullptr && text >= limit)) { return KDSizeZero; } KDSize stringSize = KDSize(0, m_glyphSize.height());