Skip to content

Commit 2ff6873

Browse files
committed
Changed way word-wrapping works to allow for Chinese characters
1 parent fa61212 commit 2ff6873

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

Line.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ CLine::CLine (const long nLineNumber,
3434
{
3535
hard_return = false;
3636
len = 0;
37-
last_space = -1;
3837
m_iPreambleOffset = 0;
3938
m_theTime = CTime::GetCurrentTime();
4039
QueryPerformanceCounter (&m_lineHighPerformanceTime);

OtherTypes.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ class CLine : public CObject
322322
bool hard_return;
323323
unsigned char flags;
324324
int len;
325-
int last_space;
326325
char * text; // allocated as necessary and then resized
327326
CStyleList styleList; // list of styles applying to text, see above
328327
CTime m_theTime; // time this line arrived

Utilities.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,3 +3441,17 @@ const char * p = sName;
34413441

34423442
return true;
34433443
} // end of IsSubName
3444+
3445+
// find the last occurrence of c in the string s, maximum length count
3446+
const char *strnrchr(const char *s, const char c, size_t count)
3447+
{
3448+
const char * ret = NULL;
3449+
size_t n = 0;
3450+
while (*s && n < count++)
3451+
{
3452+
if( *s == c )
3453+
ret = s; // remember where character is, keep searching
3454+
s++;
3455+
}
3456+
return ret;
3457+
} // end of strnrchr

dialogs/TextAttributesDlg.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,15 @@ void CTextAttributesDlg::OnLineInfo()
113113
YES_OR_NO (m_pLine->flags & BOOKMARK)
114114
));
115115

116+
// work out where the last space is
117+
const char * space = strnrchr(m_pLine->text, ' ', m_pLine->len);
118+
int last_space = -1;
119+
if (space)
120+
last_space = space - m_pLine->text;
121+
116122
INFO (TFormat (" Length = %i, last space = %i",
117123
m_pLine->len,
118-
m_pLine->last_space));
124+
last_space));
119125

120126
CString strText = CString (m_pLine->text, m_pLine->len);
121127

doc.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,57 +1627,56 @@ Unicode range UTF-8 bytes
16271627
(m_pCurrentLine->len >= m_pCurrentLine->iMemoryAllocated)) // emergency bail-out
16281628
{
16291629

1630+
int last_space = -1;
1631+
16301632
// do auto line wrapping here
16311633

1632-
// first see if we can split at a multibyte character if no space was found
1634+
// see if we can split at a multibyte character
16331635

1634-
if (m_pCurrentLine->last_space < 0 && // no space found
1635-
(m_pCurrentLine->len - m_pCurrentLine->last_space) >= m_nWrapColumn &&
1636+
if (m_pCurrentLine->len >= m_nWrapColumn &&
16361637
m_pCurrentLine->len >= 10 &&
16371638
!m_bUTF_8) // not for UTF-8
16381639
{
1639-
int multibyte_start = -1;
16401640
for (int i = 0; i < m_pCurrentLine->len - 1; i++)
16411641
{
16421642
unsigned char c1 = m_pCurrentLine->text [i];
16431643
unsigned char c2 = m_pCurrentLine->text [i + 1];
16441644
if (c1 >= 0x81 && c1 <= 0xFE && // first Big5 character
1645-
((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0xA1 && c2 <= 0xFE))) // second Big5 character
1646-
multibyte_start = i++; // remember position, jump the second byte
1647-
else if (c1 >= 0xA1 && c1 <= 0xF7 && c2 >= 0xA1 && c2 <= 0xFE) // GB2132
1648-
multibyte_start = i++; // remember position, jump the second byte
1649-
else if (c1 <= 0x7F)
1650-
multibyte_start = i; // we can split after any ordinary character
1645+
((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0xA1 && c2 <= 0xFE))) // second Big5 character
1646+
last_space = i++; // remember position, skip the second byte
1647+
else if (c1 >= 0xA1 && c1 <= 0xF7 &&
1648+
c2 >= 0xA1 && c2 <= 0xFE) // GB2132
1649+
last_space = i++; // remember position, skip the second byte
1650+
else if (c1 == ' ' && m_wrap)
1651+
last_space = i; // or split at a space
16511652
}
16521653

1653-
m_pCurrentLine->last_space = multibyte_start;
16541654
} // end of checking for a Big5 or GB2132 break point
16551655

16561656
if (!m_wrap ||
1657-
m_pCurrentLine->last_space < 0 ||
1658-
(m_pCurrentLine->len - m_pCurrentLine->last_space) >= m_nWrapColumn)
1657+
last_space < 0 ||
1658+
(m_pCurrentLine->len - last_space) >= m_nWrapColumn)
16591659
StartNewLine_KeepPreviousStyle (flags);
16601660
else
16611661
{
1662-
saved_count = m_pCurrentLine->len -
1663-
m_pCurrentLine->last_space;
1662+
saved_count = m_pCurrentLine->len - last_space;
16641663

16651664
// note - saved_count should not be zero because length is 1-relative
16661665
// (eg. 1) and last_space is zero-relative (eg. 0)
16671666
if (!m_indent_paras)
16681667
{
16691668
saved_count--; // one less to copy
1670-
m_pCurrentLine->last_space++; // one more on this line (the space)
1671-
m_pCurrentLine->len = m_pCurrentLine->last_space; // this line is longer
1669+
last_space++; // one more on this line (the space)
1670+
m_pCurrentLine->len = last_space; // this line is longer
16721671
} // end of indenting not wanted
16731672

16741673
// saved_count might be zero now, because of no indenting
16751674
if (saved_count > 0)
16761675
{
16771676
// save portion of text destined for new line
1678-
CString strText = CString (&m_pCurrentLine->text [m_pCurrentLine->last_space],
1677+
CString strText = CString (&m_pCurrentLine->text [last_space],
16791678
saved_count);
1680-
m_pCurrentLine->len = m_pCurrentLine->last_space;
1679+
m_pCurrentLine->len = last_space;
16811680

16821681
CLine * pPreviousLine = m_pCurrentLine; // remember this line
16831682

@@ -1744,9 +1743,6 @@ Unicode range UTF-8 bytes
17441743

17451744
} // end of line wrapping wanted and possible
17461745
} // end of line being full
1747-
else
1748-
if (c == ' ')
1749-
m_pCurrentLine->last_space = m_pCurrentLine->len;
17501746

17511747
ASSERT (m_pCurrentLine->text);
17521748

@@ -2189,9 +2185,15 @@ CString strLine (lpszText, size);
21892185
} // end of \n\n
21902186
else
21912187
{
2188+
// work out where the last space is
2189+
const char * space = strnrchr(m_pCurrentLine->text, ' ', m_pCurrentLine->len);
2190+
int last_space = -1;
2191+
if (space)
2192+
last_space = space - m_pCurrentLine->text;
2193+
21922194
// don't run words together - if a newline follows a word,
21932195
// insert a space
2194-
if (m_pCurrentLine->last_space != (m_pCurrentLine->len - 1))
2196+
if (last_space != (m_pCurrentLine->len - 1))
21952197
{
21962198
if (m_cLastChar == '.' && m_pCurrentLine->len < m_nWrapColumn)
21972199
AddToLine (" ", flags); // two spaces after period
@@ -2237,7 +2239,6 @@ CString strLine (lpszText, size);
22372239

22382240
m_pCurrentLine->hard_return = false;
22392241
m_pCurrentLine->len = 0;
2240-
m_pCurrentLine->last_space = -1;
22412242

22422243
} // end of letting a \r delete line contents
22432244

@@ -2254,8 +2255,14 @@ CString strLine (lpszText, size);
22542255
case ' ': // space
22552256
if (m_bInParagraph && !(flags & NOTE_OR_COMMAND)) // for <P>
22562257
{
2258+
// work out where the last space is
2259+
const char * space = strnrchr(m_pCurrentLine->text, ' ', m_pCurrentLine->len);
2260+
int last_space = -1;
2261+
if (space)
2262+
last_space = space - m_pCurrentLine->text;
2263+
22572264
// multiple consecutive spaces - discard extras
2258-
if (m_pCurrentLine->last_space == (m_pCurrentLine->len - 1))
2265+
if (last_space == (m_pCurrentLine->len - 1))
22592266
{
22602267
if (m_cLastChar != '\n')
22612268
m_cLastChar = c; // remember it

stdafx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ convenient for user programs that want to test its value. */
869869
#define DEBUG_PLUGIN_ID "138a692642ab4f9e7a1af63b" // special plugin ID for Debug "summary"
870870
bool IsPluginID (const char * sID);
871871
bool IsSubName (const char * sName);
872+
const char *strnrchr(const char *s, const char c, size_t count);
872873

873874
typedef struct
874875
{

0 commit comments

Comments
 (0)