Skip to content

Commit

Permalink
Update: include newer widechar_width.h which is faster for ASCII char…
Browse files Browse the repository at this point in the history
…acters

Upstream have made an improvement and also updated to Unicode 12.1.0.

Also update some error messages from `TTextEdit` class that referred to
wrong method.

Signed-off-by: Stephen Lyons <slysven@virginmedia.com>
  • Loading branch information
SlySven committed Jul 24, 2019
1 parent 07d37ad commit 67449e5
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 207 deletions.
22 changes: 10 additions & 12 deletions src/TTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,34 +550,32 @@ int TTextEdit::drawGrapheme(QPainter& painter, const QPoint& cursor, const QStri
painter.drawText(textRect.x(), textRect.bottom() - mFontDescent, grapheme);
return charWidth;
}

int TTextEdit::getGraphemeWidth(uint unicode) const
{
// mk_wcwidth returns -1 (on error), 0 on a control or combining
// diacritical codepoint or 1 for a normal character or 2 on a wide
// character.
// mk_wcwidth_cjk does the same except it returns 2 instead of 1 for
// characters that have an "ambiguous" East Asian width:
// replaced by wide_wcwidth which returns 1 or 2 or a number of
// (negative) special values:
// mWideAmbigousWidthGlyphs
// Markus Kuhn's mk_wcwidth()/mk_wcwidth_cjk():
// https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c have not been updated since
// Unicode 5 and we have replaced them by wide_wcwidth:
// https://github.com/ridiculousfish/widecharwidth
// which returns 1 or 2 or a number of (negative) special values:
switch (widechar_wcwidth(unicode)) {
case 1: // Draw as normal/narrow
return 1;
case 2: // Draw as wide
return 2;
case widechar_nonprint: // -1 = The character is not printable.
qDebug().nospace().noquote() << "TTextEdit::drawGrapheme(...) WARN - trying to draw a Unicode character which is unprintable, codepoint number: U+" << QString::number(unicode, 16) << ".";
qDebug().nospace().noquote() << "TTextEdit::getGraphemeWidth(...) WARN - trying to get width of a Unicode character which is unprintable, codepoint number: U+" << QString::number(unicode, 16) << ".";
return 1;
case widechar_combining: // -2 = The character is a zero-width combiner.
qWarning().nospace().noquote() << "TTextEdit::drawGrapheme(...) WARN - trying to draw a Unicode character which is a zero width combiner, codepoint number: U+" << QString::number(unicode, 16) << ".";
qWarning().nospace().noquote() << "TTextEdit::getGraphemeWidth(...) WARN - trying to get width of a Unicode character which is a zero width combiner, codepoint number: U+" << QString::number(unicode, 16) << ".";
return 1; // Previous code treated this as a normal width character
case widechar_ambiguous: // -3 = The character is East-Asian ambiguous width.
return mWideAmbigousWidthGlyphs ? 2 : 1;
case widechar_private_use: // -4 = The character is for private use - we cannot know for certain what width to used
qDebug().nospace().noquote() << "TTextEdit::drawGrapheme(...) WARN - trying to draw a Private User Character, we cannot know how wide it is, codepoint number: U+" << QString::number(unicode, 16) << ".";
qDebug().nospace().noquote() << "TTextEdit::getGraphemeWidth(...) WARN - trying to get width of a Private User Character, we cannot know how wide it is, codepoint number: U+" << QString::number(unicode, 16) << ".";
return 1;
case widechar_unassigned: // -5 = The character is unassigned.
qWarning().nospace().noquote() << "TTextEdit::drawGrapheme(...) WARN - trying to draw a Unicode character which was not previously assigned and we do not know how wide it is, codepoint number: U+" << QString::number(unicode, 16) << ".";
qWarning().nospace().noquote() << "TTextEdit::getGraphemeWidth(...) WARN - trying to get width of a Unicode character which was not previously assigned and we do not know how wide it is, codepoint number: U+" << QString::number(unicode, 16) << ".";
return 1;
case widechar_widened_in_9: // -6 = Width is 1 in Unicode 8, 2 in Unicode 9+.
if (mUseOldUnicode8) {
Expand Down
Loading

0 comments on commit 67449e5

Please sign in to comment.