work save

This commit is contained in:
Roy Qu 2024-02-22 14:24:29 +08:00
parent d2b8e8de0f
commit d6fa17cdd9
2 changed files with 87 additions and 17 deletions

View File

@ -833,7 +833,8 @@ QList<int> calcGlyphPositions(const QString &text)
if (ch.isHighSurrogate() && i+1<text.length() && QChar::isLowSurrogate(text[i+1].unicode())) { if (ch.isHighSurrogate() && i+1<text.length() && QChar::isLowSurrogate(text[i+1].unicode())) {
//character that larger than 0xffff //character that larger than 0xffff
glyphPositions.append(i); glyphPositions.append(i);
i++; i+=2;
continue;
} else if (ch.combiningClass()!=0 && !glyphPositions.isEmpty()) { } else if (ch.combiningClass()!=0 && !glyphPositions.isEmpty()) {
//a Combining character //a Combining character
} else { } else {
@ -849,11 +850,19 @@ int Document::stringColumns(const QString &str, int colsBefore) const
QList<int> glyphPositions = calcGlyphPositions(str); QList<int> glyphPositions = calcGlyphPositions(str);
int totalColumns; int totalColumns;
calcGlyphColumns(str,glyphPositions,colsBefore, totalColumns); calcGlyphColumns(str,glyphPositions,colsBefore, totalColumns);
return totalColumns; return totalColumns - colsBefore;
}
int Document::charToColumn(int line, int charPos)
{
QMutexLocker locker(&mMutex);
QList<int> glyphPositions = mLines[line]->glyphPositions();
} }
int Document::charToColumn(const QString &lineText, int charPos) const int Document::charToColumn(const QString &lineText, int charPos) const
{ {
QMutexLocker locker(&mMutex);
QList<int> glyphPositions = calcGlyphPositions(lineText); QList<int> glyphPositions = calcGlyphPositions(lineText);
return charToColumn(lineText, glyphPositions, charPos); return charToColumn(lineText, glyphPositions, charPos);
} }
@ -984,19 +993,33 @@ DocumentLine::DocumentLine():
{ {
} }
QStringRef DocumentLine::getGlyph(int i) const int DocumentLine::glyphEnd(int i) const
{ {
Q_ASSERT(i>=0 && i<mGlyphPositions.length()); Q_ASSERT(i>=0 && i<mGlyphPositions.length());
int start = mGlyphPositions[i]; if (i+1<mGlyphPositions.length()) {
int end; end = mGlyphPositions[i+1];
if (i+1<mGlyphPositions.length()) { } else {
end = mGlyphPositions[i+1]; end = mLineText.length();
} else { }
end = mLineText.length();
}
return mLineText.midRef(start,end-start);
} }
QString DocumentLine::getGlyph(int i) const
{
int start = glyphStart(i);
int end = glyphEnd(i);
return mLineText.mid(start,end-start);
}
int DocumentLine::getGlyphEndColumn(int i) const
{
Q_ASSERT(mColumns>=0);
Q_ASSERT(i>=0 && i<mGlyphColumns.length());
if (i+1<mGlyphColumns.length()) {
end = mGlyphColumns[i+1];
} else {
end = mColumns;
}
}
void DocumentLine::setLineText(const QString &newLineText) void DocumentLine::setLineText(const QString &newLineText)
{ {

View File

@ -81,12 +81,48 @@ private:
const QList<int>& glyphColumns() const { const QList<int>& glyphColumns() const {
return mGlyphColumns; return mGlyphColumns;
} }
/**
* @brief get start index of the chars representing the specified glyph.
* @param i index of the glyph of the line (starting from 0)
* @return
*/
int glyphStart(int i) const {
Q_ASSERT(i>=0 && i<mGlyphPositions.length());
return mGlyphPositions[i];
}
/**
* @brief get end index of the chars representing the specified glyph.
* @param i index of the glyph of the line (starting from 0)
* @return
*/
int glyphEnd(int i) const;
/** /**
* @brief get the chars representing the specified glyph. * @brief get the chars representing the specified glyph.
* @param i index of the glyph of the line (starting from 0) * @param i index of the glyph of the line (starting from 0)
* @return the chars representing the specified glyph * @return the chars representing the specified glyph
*/ */
QStringRef getGlyph(int i) const; QString getGlyph(int i) const;
/**
* @brief get start column of the specified glyph.
* @param i index of the glyph of the line (starting from 0)
* @return
*/
int getGlyphStartColumn(int i) const {
Q_ASSERT(mColumns>=0);
Q_ASSERT(i>=0 && i<mGlyphColumns.length());
return mGlyphColumns[i];
}
/**
* @brief get end column of the specified glyph.
* @param i index of the glyph of the line (starting from 0)
* @return
*/
int getGlyphEndColumn(int i) const;
/** /**
* @brief get the line text * @brief get the line text
@ -390,16 +426,27 @@ public:
void loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding); void loadFromFile(const QString& filename, const QByteArray& encoding, QByteArray& realEncoding);
void saveToFile(QFile& file, const QByteArray& encoding, void saveToFile(QFile& file, const QByteArray& encoding,
const QByteArray& defaultEncoding, QByteArray& realEncoding); const QByteArray& defaultEncoding, QByteArray& realEncoding);
/**
* @brief calculate display width (in columns) of a string
*
* The string may contains tab char, whose width depends on the tab size and it's position
*
* @param str the string to be displayed
* @param colsBefore columns before the string
* @return width of the string, don't including colsBefore
*/
int stringColumns(const QString &str, int colsBefore) const; int stringColumns(const QString &str, int colsBefore) const;
int glyphStart(int line, int glyphIdx);
int glyphEnd(int line, int glyphIdx);
int glyphStartColumn(int line, int glyphIdx);
int glyphEndColumn(int line, int glyphIdx);
int charToGlyphIndex(int line, int charPos);
int charToColumn(int line, int charPos); int charToColumn(int line, int charPos);
int columnToChar(int line, int column); int columnToChar(int line, int column);
int charToColumn(int line, const QString newStr, int charPos); int charToColumn(int line, const QString newStr, int charPos);
int columnToChar(int line, const QString newStr, int column); int columnToChar(int line, const QString newStr, int column);
int glyphStart(int line, int glyphIdx);
int glyphEnd(int line, int glyphIdx);
int glyphColumns(int line, int glyphIdx);
int charToGlyphIndex(int line, int charPos);
int columnToGlyphIndex(int line, int column); int columnToGlyphIndex(int line, int column);