Skip to content

Commit

Permalink
[GreenPad] make sure output buffer byte-size for TextOutW is 2 bytes …
Browse files Browse the repository at this point in the history
…aligned

this should fix TextOutW failing when USE_ORIGINAL_MEMMAN is used.

ported from RamonUnch/GreenPad@539286b
  • Loading branch information
roytam1 committed Nov 11, 2023
1 parent 8304c34 commit 0266197
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
18 changes: 14 additions & 4 deletions GreenPad/editwing/ip_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class Parser;
//@}
//=========================================================================

#ifdef USE_ORIGINAL_MEMMAN
// be sure to align alloc evenly because unicode is 2 bytes long
// and we append bytes, but alignement should remain 2bytes
// Otherwise some functions might fail such as TextOutW
#define EVEN(x) ( ((x)+1)&(~1ul) )
#else
#define EVEN(x) (x)
#endif

class Line : public Object
{
public:
Expand All @@ -33,7 +42,7 @@ class Line : public Object
Line( const unicode* str, ulong len )
: alen_( 10>len ? 10 : len )
, len_ ( len )
, str_ ( static_cast<unicode*>( mem().Alloc((alen_+1)*2+alen_) ) )
, str_ ( static_cast<unicode*>( mem().Alloc(EVEN((alen_+1)*2+alen_)) ) )
, commentBitReady_( false )
, isLineHeadCommented_( 0 )
{
Expand All @@ -43,7 +52,7 @@ class Line : public Object

~Line()
{
mem().DeAlloc( str_, (alen_+1)*2+alen_ );
mem().DeAlloc( str_, EVEN((alen_+1)*2+alen_) );
}

//@{ テキスト挿入(指定位置に指定サイズ) //@}
Expand All @@ -56,15 +65,15 @@ class Line : public Object
ulong psiz = (alen_+1)*2+alen_;
alen_ = len_+siz; // Max( alen_<<1, len_+siz );
unicode* tmpS =
static_cast<unicode*>( mem().Alloc((alen_+1)*2+alen_) );
static_cast<unicode*>( mem().Alloc(EVEN((alen_+1)*2+alen_)) );
uchar* tmpF =
reinterpret_cast<uchar*>(tmpS+alen_+1);
// コピー
memmove( tmpS, str_, at*2 );
memmove( tmpS+at+siz, str_+at, (len_-at+1)*2 );
memmove( tmpF, flgs, at );
// 古いのを削除
mem().DeAlloc( str_, psiz );
mem().DeAlloc( str_, EVEN(psiz) );
str_ = tmpS;
}
else
Expand Down Expand Up @@ -159,6 +168,7 @@ class Line : public Object
bool commentBitReady_;
};

#undef EVEN


//=========================================================================
Expand Down
28 changes: 4 additions & 24 deletions GreenPad/editwing/ip_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ inline void Painter::CharOut( unicode ch, int x, int y )
inline void Painter::StringOut
( const unicode* str, int len, int x, int y )
{
BOOL ret;
DWORD dwTimes;
#ifdef WIN32S
DWORD dwNum;
char psTXT1K[1024];
Expand All @@ -343,30 +341,14 @@ inline void Painter::StringOut
}
}
}
dwTimes=0; do {
ret = ::TextOutA( dc_, x, y, psText, dwNum );
++dwTimes;
} while(
# ifdef USE_ORIGINAL_MEMMAN
!ret && dwTimes < 2
# else
false
# endif
);
::TextOutA( dc_, x, y, psText, dwNum );

if (psText != psTXT1K)
delete []psText;
#else
dwTimes=0; do {
ret = ::TextOutW( dc_, x, y, str, len );
++dwTimes;
} while(
# ifdef USE_ORIGINAL_MEMMAN
!ret && dwTimes < 2
# else
false
# endif
);
// If unicode text is not 2bytes-aligned then TextOutW can randomly fail
// To avoid this we must be careful in the Line class...
::TextOutW( dc_, x, y, str, len );
#endif
}

Expand Down Expand Up @@ -680,8 +662,6 @@ void ViewImpl::DrawTXT( const VDrawInfo& v, Painter& p )
if( clr != (flg[i]&3) )
p.SetColor( clr=(flg[i]&3) );
p.StringOut( str+i, i2-i, x+v.XBASE, a.top );
//p.StringOut( str+i, i2-i, x+v.XBASE, a.top );
// 何故だか2度描きしないとうまくいかん…
break;
}
}
Expand Down

0 comments on commit 0266197

Please sign in to comment.