- fix: crash when input using input method in an empty file.

- optimize for ligature and force monospace both enabled.
This commit is contained in:
Roy Qu 2024-02-28 18:20:11 +08:00
parent 8b0c53dc69
commit 0a4acbec46
3 changed files with 25 additions and 21 deletions

View File

@ -836,6 +836,8 @@ QString Document::glyph(int line, int glyphIdx)
QString Document::glyphAt(int line, int charPos) QString Document::glyphAt(int line, int charPos)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (line<0 || line>=count())
return QString();
QList<int> glyphStartCharList = mLines[line]->glyphStartCharList(); QList<int> glyphStartCharList = mLines[line]->glyphStartCharList();
int glyphIdx = charToGlyphIndex(mLines[line]->lineText(), glyphStartCharList, charPos); int glyphIdx = charToGlyphIndex(mLines[line]->lineText(), glyphStartCharList, charPos);
return mLines[line]->glyph(glyphIdx); return mLines[line]->glyph(glyphIdx);
@ -844,6 +846,8 @@ QString Document::glyphAt(int line, int charPos)
int Document::charToGlyphStartChar(int line, int charPos) int Document::charToGlyphStartChar(int line, int charPos)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (line<0 || line>=count())
return 0;
QList<int> glyphStartCharList = mLines[line]->glyphStartCharList(); QList<int> glyphStartCharList = mLines[line]->glyphStartCharList();
int glyphIdx = charToGlyphIndex(mLines[line]->lineText(), glyphStartCharList, charPos); int glyphIdx = charToGlyphIndex(mLines[line]->lineText(), glyphStartCharList, charPos);
return mLines[line]->glyphStartChar(glyphIdx); return mLines[line]->glyphStartChar(glyphIdx);
@ -1029,6 +1033,8 @@ int Document::xposToGlyphIndex(int strWidth, QList<int> glyphPositionList, int x
int Document::charToGlyphStartPosition(int line, int charPos) int Document::charToGlyphStartPosition(int line, int charPos)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (line<0 || line>=count())
return 0;
QList<int> glyphStartCharList = mLines[line]->glyphStartCharList(); QList<int> glyphStartCharList = mLines[line]->glyphStartCharList();
int glyphIdx = charToGlyphIndex(mLines[line]->lineText(), glyphStartCharList, charPos); int glyphIdx = charToGlyphIndex(mLines[line]->lineText(), glyphStartCharList, charPos);
return mLines[line]->glyphStartPosition(glyphIdx); return mLines[line]->glyphStartPosition(glyphIdx);
@ -1047,7 +1053,7 @@ int Document::xposToGlyphStartChar(int line, int xpos)
int Document::charToGlyphStartPosition(int line, const QString newStr, int charPos) int Document::charToGlyphStartPosition(int line, const QString newStr, int charPos)
{ {
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (mLines[line]->lineText() == newStr) { if (line>=0 && line<count() && mLines[line]->lineText() == newStr) {
return charToGlyphStartPosition(line,charPos); return charToGlyphStartPosition(line,charPos);
} else { } else {
QList<int> glyphStartCharList = calcGlyphStartCharList(newStr); QList<int> glyphStartCharList = calcGlyphStartCharList(newStr);

View File

@ -379,13 +379,13 @@ void QSynEditPainter::paintToken(
bool tryLigature = false; bool tryLigature = false;
if (glyph.length()==0) { if (glyph.length()==0) {
} else if (glyph.length()==1 && glyph.front().unicode()<=32){ } else if (glyph.length()==1 && glyph.front().unicode()<=32){
} else if (mEdit->mOptions.testFlag(eoForceMonospace)
&& glyphWidth != mPainter->fontMetrics().horizontalAdvance(glyph)) {
} else { } else {
tryLigature = true; tryLigature = true;
} }
if (tryLigature) { if (tryLigature) {
QString textToPaint = glyph; QString textToPaint = glyph;
int oldGlyphWidth = glyphWidth;
int oldI = i;
while(i+1<endGlyph) { while(i+1<endGlyph) {
int glyphStart = glyphStartCharList[i+1]; int glyphStart = glyphStartCharList[i+1];
int glyphLen = calcSegmentInterval(glyphStartCharList,lineText.length(),i+1); int glyphLen = calcSegmentInterval(glyphStartCharList,lineText.length(),i+1);
@ -395,27 +395,27 @@ void QSynEditPainter::paintToken(
if ( glyph2.length()<1 if ( glyph2.length()<1
|| ||
(glyph2.length()==1 (glyph2.length()==1
&& glyph2.front().unicode()<32)) && glyph2.front().unicode()<=32))
break; break;
i+=1; int glyph2Width = calcSegmentInterval(glyphStartPositionList, tokenRight, i+1);
glyphWidth += calcSegmentInterval(glyphStartPositionList, tokenRight, i); if (mEdit->mOptions.testFlag(eoForceMonospace)) {
textToPaint+=glyph2; if (glyphWidth+glyph2Width != mPainter->fontMetrics().horizontalAdvance(textToPaint+glyph2)) {
break;
}
}
i++;
glyphWidth += glyph2Width;
textToPaint += glyph2;
if (tokenWidth + glyphWidth > last ) if (tokenWidth + glyphWidth > last )
break; break;
} }
if (glyphWidth if (!fontInited) {
== mPainter->fontMetrics().horizontalAdvance(textToPaint)) { mPainter->setFont(font);
if (!fontInited) { fontInited = true;
mPainter->setFont(font);
fontInited = true;
}
//qDebug()<<"paint 1:"<<textToPaint;
mPainter->drawText(nX,rcToken.bottom()-mPainter->fontMetrics().descent() , textToPaint);
drawed = true;
} else {
glyphWidth = oldGlyphWidth;
i=oldI;
} }
//qDebug()<<"paint 1:"<<textToPaint;
mPainter->drawText(nX,rcToken.bottom()-mPainter->fontMetrics().descent() , textToPaint);
drawed = true;
} }
} }
if (!drawed) { if (!drawed) {

View File

@ -864,14 +864,12 @@ QString QSynEdit::GetLeftSpacing(int charCount, bool wantTabs) const
int QSynEdit::charToGlyphLeft(int line, int charPos) const int QSynEdit::charToGlyphLeft(int line, int charPos) const
{ {
Q_ASSERT(line>=1 && line <= mDocument->count());
QString s = getDisplayStringAtLine(line); QString s = getDisplayStringAtLine(line);
return mDocument->charToGlyphStartPosition(line-1, s, charPos-1); return mDocument->charToGlyphStartPosition(line-1, s, charPos-1);
} }
int QSynEdit::charToGlyphLeft(int line, const QString &s, int charPos) const int QSynEdit::charToGlyphLeft(int line, const QString &s, int charPos) const
{ {
Q_ASSERT(line>=1 && line <= mDocument->count());
return mDocument->charToGlyphStartPosition(line-1, s, charPos-1); return mDocument->charToGlyphStartPosition(line-1, s, charPos-1);
} }