summaryrefslogtreecommitdiffstats
path: root/src/kernel/qrichtext.cpp
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2025-01-30 23:40:14 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2025-02-13 22:39:51 +0900
commit67cb0f6762768ee0d32adef1d7307ff7bb985407 (patch)
tree9c391724f3eae5ba2b8279ea9de6245a1f44d25b /src/kernel/qrichtext.cpp
parentfd79f0c8b020ff0c60b62c83745beb030ef38997 (diff)
downloadtqt-67cb0f6762768ee0d32adef1d7307ff7bb985407.tar.gz
tqt-67cb0f6762768ee0d32adef1d7307ff7bb985407.zip
Fix editing of text containing surrogate characters.
This relates to issue #162. Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it> (cherry picked from commit 9c648bea9bfb1113c070a05b36f78ff006d0877a)
Diffstat (limited to 'src/kernel/qrichtext.cpp')
-rw-r--r--src/kernel/qrichtext.cpp33
1 files changed, 28 insertions, 5 deletions
diff --git a/src/kernel/qrichtext.cpp b/src/kernel/qrichtext.cpp
index 4609a340b..6f09cc185 100644
--- a/src/kernel/qrichtext.cpp
+++ b/src/kernel/qrichtext.cpp
@@ -221,12 +221,19 @@ TQTextCursor *TQTextDeleteCommand::execute( TQTextCursor *c )
cursor.setParagraph( s );
cursor.setIndex( index );
int len = text.size();
+
if ( c )
*c = cursor;
if ( doc ) {
doc->setSelectionStart( TQTextDocument::Temp, cursor );
for ( int i = 0; i < len; ++i )
+ {
+ if (text[i].c.isHighSurrogate() && i < (len - 1) && text[i + 1].c.isLowSurrogate())
+ {
+ ++i; // This is required for correct advancement when handling surrogate pairs
+ }
cursor.gotoNextLetter();
+ }
doc->setSelectionEnd( TQTextDocument::Temp, cursor );
doc->removeSelectedText( TQTextDocument::Temp, &cursor );
if ( c )
@@ -1250,9 +1257,17 @@ bool TQTextCursor::removePreviousChar()
{
tmpX = -1;
if ( !atParagStart() ) {
- para->remove( idx-1, 1 );
+ if (para->at(idx - 1)->c.isLowSurrogate() && idx > 1 && para->at(idx - 2)->c.isHighSurrogate())
+ {
+ para->remove(idx - 2, 2);
+ idx -= 2;
+ }
+ else
+ {
+ para->remove(idx - 1, 1);
+ idx--;
+ }
int h = para->rect().height();
- idx--;
// shouldn't be needed, just to make sure.
fixCursorPosition();
para->format( -1, TRUE );
@@ -4009,6 +4024,7 @@ int TQTextString::width( int idx ) const
{
int w = 0;
TQTextStringChar *c = &at( idx );
+ // '!c->charStop' already takes care of low surrogate chars
if ( !c->charStop || c->c.unicode() == 0xad || c->c.unicode() == 0x2028 )
return 0;
#ifndef TQT_NO_TEXTCUSTOMITEM
@@ -4292,10 +4308,10 @@ void TQTextParagraph::format( int start, bool doMove )
if ( !str || str->length() == 0 || !formatter() )
return;
- if ( hasdoc &&
- document()->preProcessor() &&
- ( needPreProcess || state == -1 ) )
+ if ( hasdoc && document()->preProcessor() && ( needPreProcess || state == -1 ) )
+ {
document()->preProcessor()->process( document(), this, invalid <= 0 ? 0 : invalid );
+ }
needPreProcess = FALSE;
if ( invalid == -1 )
@@ -5784,6 +5800,7 @@ int TQTextFormatterBreakWords::format( TQTextDocument *doc, TQTextParagraph *par
lastChr = c->c;
lastFormat = c->format();
}
+
bool lastWasOwnLineCustomItem = lastBreak == -2;
bool hadBreakableChar = lastBreak != -1;
bool lastWasHardBreak = lastChr == TQChar_linesep;
@@ -5793,6 +5810,12 @@ int TQTextFormatterBreakWords::format( TQTextDocument *doc, TQTextParagraph *par
c->format()->setPainter( painter );
c = &string->at( i );
+ // Skip over low surrogate chars
+ if (c->c.isLowSurrogate())
+ {
+ continue;
+ }
+
if (lastFormat != c->format() && !c->c.isSpace()
&& lastFormat->font().italic() && !c->format()->font().italic()) {
int rb = lastFormat->fontMetrics().rightBearing(lastChr);