|
I modified a copy to support the Chinese language,
I modified funciton rtf_write_header,change language
strcpy( rtfText, "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052{\\fonttbl" );
modiyed fuciton rtf_init,to add chinese fonts
strcat( rtfFontTable, "{\\f0\\froman\\fcharset0\\fprq2{\\*\\panose 02020603050405020304}Times New Roman;}" );
strcat( rtfFontTable, "{\\f13\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'cb\\'ce\\'cc\\'e5{\\*\\falt SimSun};}" );
strcat( rtfFontTable, "{\\f17\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'ba\\'da\\'cc\\'e5{\\*\\falt SimHei};}" );
strcat( rtfFontTable, "{\\f109\\froman\\fcharset238\\fprq2 Times New Roman CE;}" );
strcat( rtfFontTable, "{\\f110\\froman\\fcharset204\\fprq2 Times New Roman Cyr;}" );
strcat( rtfFontTable, "{\\f55\\fmodern\\fcharset134\\fprq1{\\*\\panose 02010609030101010101}\\'bf\\'ac\\'cc\\'e5_GB2312;}" );
strcat( rtfFontTable, "{\\f621\\fnil\\fcharset0\\fprq2 @\\'cb\\'ce\\'cc\\'e5 Western;}" );
Add a funciton to translate chinese string to ascii string
string StrToASC(char* text)
{
string strResult;
int iLen = strlen(text);
for(int i = 0; i<iLen; i++)
{
BYTE chData = text[i];
if(chData > 0xa0 && chData != 0)
{
char szHex[12]={0};
wsprintf(szHex,"\\'%2.2x", chData);
strResult += szHex;
}
else
{
strResult += (char) chData;
}
}
return strResult;
}
then modified fuction rtf_start_paragraph,
int rtf_start_paragraph(char* text, bool newPar)
{
string strText = StrToASC(text);
int error = RTF_SUCCESS;
if(rtfParFormat.paragraphText)
{
delete[]rtfParFormat.paragraphText;
rtfParFormat.paragraphText = NULL;
}
rtfParFormat.paragraphText = new char[strText.size()+1];
strcpy( rtfParFormat.paragraphText, strText.c_str() );
rtfParFormat.newParagraph = newPar;
if( !rtf_write_paragraphformat() )
error = RTF_PARAGRAPHFORMAT_ERROR;
return error;
}
almost thoese are what i did,sorry for my pool English
PS,there are many memory leaks......
|
|
|
|
|
你好 我看了你那个支持中文显示的代码 发现StrToASC函数中for(int i = 0; i<iLen; i++)
和if(chData > 0xa0 && chData != 0)不明白什么意思 能不能解释一下
我现在正在作者方面的东西
|
|
|
|
|
这个是由于网页显示问题,实际上你要是懂html的话,你就懂了,比如>这个代表的是大于号,其它慢慢查吧,在html相关的符号说明有的
This is the web page display problems, in fact, if you know html, then you will understand, such as> this represents a greater-than symbol, the other you can check the book about html and you can find it
-- modified 20-Jul-12 2:12am.
|
|
|
|
|
你改编的能支持中文的工程能发给我学习一下吗 谢谢!
|
|
|
|
|
#include <SSTREAM>
#include <WINDOWS.H>
inline std::string InputToRTFstream(const char* input)
{
DWORD dwNum = ::MultiByteToWideChar (CP_ACP, 0, input, -1, NULL, 0);
wchar_t* wpstr = new wchar_t[::strlen(input) + 1];
::MultiByteToWideChar(CP_ACP, 0, input, -1, wpstr, dwNum);
std::wstring str(wpstr);
std::ostringstream sstr;
for(int i = 0; i < str.length(); i++)
{
sstr << "\\u" << wpstr[i] << " ";
}
delete[] wpstr;
return sstr.str();
}
|
|
|
|
|
Great!
Thanks.
Now supports Cyrillics!
|
|
|
|
|
I am using vst2005.
rebuilt the sample with the library as part of the source and got a stack corruption error around variable tblrw
int rtf_start_tablerow()
{
// Set error flag
int error = RTF_SUCCESS;
char tblrw[] = "";
tblrw is only allocated with 1 byte of storage in the original code
char tblrw[1024] = "";
fixes the stack.
|
|
|
|
|
IE, my application supports czech, polish and other european languages. I notice the API calls are all char*. Will it cope with these other languages?
|
|
|
|
|
I modified a copy to support the Chinese language,
I modified funciton rtf_write_header,change language
strcpy( rtfText, "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052{\\fonttbl" );
modiyed fuciton rtf_init,to add chinese fonts
strcat( rtfFontTable, "{\\f0\\froman\\fcharset0\\fprq2{\\*\\panose 02020603050405020304}Times New Roman;}" );
strcat( rtfFontTable, "{\\f13\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'cb\\'ce\\'cc\\'e5{\\*\\falt SimSun};}" );
strcat( rtfFontTable, "{\\f17\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'ba\\'da\\'cc\\'e5{\\*\\falt SimHei};}" );
strcat( rtfFontTable, "{\\f109\\froman\\fcharset238\\fprq2 Times New Roman CE;}" );
strcat( rtfFontTable, "{\\f110\\froman\\fcharset204\\fprq2 Times New Roman Cyr;}" );
strcat( rtfFontTable, "{\\f55\\fmodern\\fcharset134\\fprq1{\\*\\panose 02010609030101010101}\\'bf\\'ac\\'cc\\'e5_GB2312;}" );
strcat( rtfFontTable, "{\\f621\\fnil\\fcharset0\\fprq2 @\\'cb\\'ce\\'cc\\'e5 Western;}" );
Add a funciton to translate chinese string to ascii string
string StrToASC(char* text)
{
string strResult;
int iLen = strlen(text);
for(int i = 0; i<iLen; i++)
{
BYTE chData = text[i];
if(chData > 0xa0 && chData != 0)
{
char szHex[12]={0};
wsprintf(szHex,"\\'%2.2x", chData);
strResult += szHex;
}
else
{
strResult += (char) chData;
}
}
return strResult;
}
almost thoese are what i did,sorry for my pool English
|
|
|
|
|
你好! 我也想让RTFLib支持中文 你说的 modiyed fuciton rtf_init,to add chinese fonts 是这样的形式
strcpy( rtfFontTable, "" );
strcat( rtfFontTable, "{\\f0\\froman\\fcharset0\\cpg1252 Times New Roman}" );
strcat( rtfFontTable, "{\\f1\\fswiss\\fcharset0\\cpg1252 Arial}" );
strcat( rtfFontTable, "{\\f2\\fmodern\\fcharset0\\cpg1252 Courier New}" );
strcat( rtfFontTable, "{\\f3\\fscript\\fcharset0\\cpg1252 Cursive}" );
strcat( rtfFontTable, "{\\f4\\fdecor\\fcharset0\\cpg1252 Old English}" );
strcat( rtfFontTable, "{\\f5\\ftech\\fcharset0\\cpg1252 Symbol}" );
strcat( rtfFontTable, "{\\f6\\fbidi\\fcharset0\\cpg1252 Miriam}" );
strcat( rtfFontTable, "{\\f13\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'cb\\'ce\\'cc\\'e5{\\*\\falt SimSun};}" );
strcat( rtfFontTable, "{\\f17\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'ba\\'da\\'cc\\'e5{\\*\\falt SimHei};}" );
strcat( rtfFontTable, "{\\f109\\froman\\fcharset238\\fprq2 Times New Roman CE;}" );
strcat( rtfFontTable, "{\\f110\\froman\\fcharset204\\fprq2 Times New Roman Cyr;}" );
strcat( rtfFontTable, "{\\f55\\fmodern\\fcharset134\\fprq1{\\*\\panose 02010609030101010101}\\'bf\\'ac\\'cc\\'e5_GB2312;}" );
strcat( rtfFontTable, "{\\f621\\fnil\\fcharset0\\fprq2 @\\'cb\\'ce\\'cc\\'e5 Western;}" );
还是这样的形式
strcat( rtfFontTable, "{\\f0\\froman\\fcharset0\\fprq2{\\*\\panose 02020603050405020304}Times New Roman;}" );
strcat( rtfFontTable, "{\\f13\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'cb\\'ce\\'cc\\'e5{\\*\\falt SimSun};}" );
strcat( rtfFontTable, "{\\f17\\fnil\\fcharset134\\fprq2{\\*\\panose 02010600030101010101}\\'ba\\'da\\'cc\\'e5{\\*\\falt SimHei};}" );
strcat( rtfFontTable, "{\\f109\\froman\\fcharset238\\fprq2 Times New Roman CE;}" );
strcat( rtfFontTable, "{\\f110\\froman\\fcharset204\\fprq2 Times New Roman Cyr;}" );
strcat( rtfFontTable, "{\\f55\\fmodern\\fcharset134\\fprq1{\\*\\panose 02010609030101010101}\\'bf\\'ac\\'cc\\'e5_GB2312;}" );
strcat( rtfFontTable, "{\\f621\\fnil\\fcharset0\\fprq2 @\\'cb\\'ce\\'cc\\'e5 Western;}" );
|
|
|
|
|
Nice work!
It's a pity there are so many memory leaks though.
I found an other one in rtf_load_image, near the end of the function there's:
char* str = rtf_bin_hex_convert( buffer, size );<br />
delete []buffer;
but str is never deleted. So add (at the end of the code block):
delete []str;
after:
strcpy( rtfText, "}" );<br />
fwrite( rtfText, 1, strlen(rtfText), rtfFile );
BTW: The code by MRSilver features the same leak
|
|
|
|
|
delete []str; //this is not enought for a good coder.
uselly, we set the pointer to null point,after we delete the memory. so it's best to be like this:
delte []str;
str=NULL;//"NULL" is for visual studio compiler.standarded C is "null"
|
|
|
|
|
Gday,
The rtflib was of great help for me. Nevertheless i met some quicks but these have been solved. Additionally i found one not yet discussed in this forum, so i want to share it with you.
In function rtf_write_paragraphformat -rather at the end of the function-, the actual writing to rtfFile takes place. All information is stored into the variable rtfText, which is an array of char of length 4096. When writing long texts, the upper bounds of rtfText are exceeded, which causes nasty errors. Therefore i propose to split the meta information and the actual text and write them separatedly:
<br />
if ( rtfParFormat.tabbedText == false )<br />
{<br />
sprintf( rtfText, "\n%s\\fi%d\\li%d\\ri%d\\sb%d\\sa%d\\sl%d%s ", text, <br />
rtfParFormat.firstLineIndent, rtfParFormat.leftIndent, <br />
rtfParFormat.rightIndent, rtfParFormat.spaceBefore, <br />
rtfParFormat.spaceAfter, rtfParFormat.lineSpacing, font );<br />
}<br />
else<br />
sprintf( rtfText, "\\tab " );<br />
<br />
if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )<br />
result = false;<br />
<br />
if ( fwrite( rtfParFormat.paragraphText, 1, <br />
strlen(rtfParFormat.paragraphText), rtfFile) < <br />
strlen(rtfParFormat.paragraphText))<br />
result = false;<br />
<br />
return result;<br />
<br />
21 cm, Hydrogens universal wavelength
|
|
|
|
|
First thanks for sharing your work.
You can load images with less memory and less code:
IPicture* pPic;<br />
HRESULT hr = OleLoadPicturePath(const_cast<LPOLESTR>(pszImagePath),<br />
NULL,<br />
0,<br />
0,<br />
IID_IPicture,<br />
reinterpret_cast<LPVOID *>(&pPic));<br />
if(SUCCEEDED(hr) && pPic!= NULL)<br />
{<br />
....<br />
}
instead of:
int imageFile = _open( image, _O_RDONLY | _O_BINARY );<br />
struct _stat st;<br />
_fstat( imageFile, &st );<br />
DWORD nSize = st.st_size;<br />
BYTE* pBuff = new BYTE[nSize];<br />
_read( imageFile, pBuff, nSize );<br />
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);<br />
void* pData = GlobalLock(hGlobal);<br />
memcpy(pData, pBuff, nSize);<br />
GlobalUnlock(hGlobal);<br />
IStream* pStream = NULL;<br />
if ( CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK )<br />
{<br />
HRESULT hr;<br />
if ((hr = OleLoadPicture( pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&rtfPicture)) != S_OK)<br />
error = RTF_IMAGE_ERROR;<br />
<br />
pStream->Release();<br />
}<br />
delete []pBuff;<br />
_close(imageFile);
Thanks again,
Abbas
|
|
|
|
|
Is there a way to set the document to be in landscape instead of protrait mode?
Thanks
|
|
|
|
|
thanks for share although haven't read the code thoroughly, i want to know if it can show jpg files directly in that rtf file.
|
|
|
|
|
I love it extremely, thanks
|
|
|
|
|
Hi, I have a template named hf.dot in which I have defined a header. I want to generate a RTF file that shows that header. Inside the RTF file, I can link to the template and use it's styles with a code like this:
{\template c:\\hf.dot}\linkstyles
but I don't know how to use the header. Any idea ?
|
|
|
|
|
Hi Darko!
Thanks for very usefull library.
I have some solution for unicode suport in mfc applicatons (RTF Specification, version 1.6 on http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp[^]).
In rtflib.h add new function declaration:
int rtf_start_paragraph(CString sUni, bool newPar)
in rtflib.cpp add this code:
int rtf_start_paragraph(CString sUni, bool newPar)<br />
{<br />
char * s = (char *)(LPCTSTR) sUni;<br />
#ifdef _UNICODE<br />
char cWord[1024];<br />
strcpy( cWord , "{\\uc{" );<br />
for(int i = 0; i < sUni.GetLength()*2; i+=2){<br />
char lo = s[i];<br />
char hi = s[i+1];<br />
strcat( cWord , "\\u" );<br />
int iUni = hi*256+lo;<br />
char buffer[20];<br />
_itoa( iUni, buffer, 10 );<br />
strcat( cWord , buffer );<br />
}<br />
strcat( cWord , "}}");<br />
return rtf_start_paragraph(cWord, newPar);<br />
#else<br />
return rtf_start_paragraph(s, newPar);<br />
#endif<br />
}<br />
|
|
|
|
|
It would be nice if this rtflib could be unicode aware!
Andrew
|
|
|
|
|
if(int(lo) < 0 )
iUni += 256;
|
|
|
|
|
rtf_start_paragraph shoulb be like this
int rtf_start_paragraph(char* text, bool newPar)
{
// Set error flag
int error = RTF_SUCCESS;
// Copy paragraph text
rtfParFormat.paragraphText = new char[strlen(text)+1];
strcpy( rtfParFormat.paragraphText, text );
// Set new paragraph
rtfParFormat.newParagraph = newPar;
// Starts new RTF paragraph
if( !rtf_write_paragraphformat() )
error = RTF_PARAGRAPHFORMAT_ERROR;
delete [] rtfParFormat.paragraphText;
rtfParFormat.paragraphText=NULL;
// Return error flag
return error;
}
is required to reserve 1 more char for copying the NULL terminating string on the next strcpy
also the pointer is freed after being used.
more memory leaks on rtf_write_paragraphformat:
...
// Format paragraph border type
char *br = rtf_get_bordername(rtfParFormat.BORDERS.borderType);
strcat( border, br );
// add this after the call to delete the returned pointer
delete []br;
br=NULL;
and more on the same function at:
// Format paragraph shading
char* sh = rtf_get_shadingname( rtfParFormat.SHADING.shadingType, false );
// add this after the call to delete the returned pointer
strcat( text, sh );
delete sh;
sh=NULL;
some improvements could be made to insert a picture that you've already loaded from file, add this function to the library (.cpp and .h) and you're done:
// Inserts image from IPicture
int rtf_insert_image(IPicture* pImage, int width, int height)
{
// Set error flag
int error = RTF_SUCCESS;
// If IIpicture is valid
if ( pImage != NULL )
{
// Calculate image size
long hmWidth;
long hmHeight;
pImage->get_Width(&hmWidth);
pImage->get_Height(&hmHeight);
int nWidth = MulDiv( hmWidth, GetDeviceCaps(GetDC(NULL),LOGPIXELSX), 2540 );
int nHeight = MulDiv( hmHeight, GetDeviceCaps(GetDC(NULL),LOGPIXELSY), 2540 );
// Create metafile;
HDC hdcMeta = CreateMetaFile(NULL);
// Render picture to metafile
pImage->Render( hdcMeta, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL );
// Close metafile
HMETAFILE hmf = CloseMetaFile(hdcMeta);
// Get metafile data
UINT size = GetMetaFileBitsEx( hmf, 0, NULL );
BYTE* buffer = new BYTE[size];
GetMetaFileBitsEx( hmf, size, buffer );
DeleteMetaFile(hmf);
// Convert metafile binary data to hexadecimal
char* str = rtf_bin_hex_convert( buffer, size );
delete []buffer;
// Format picture paragraph
RTF_PARAGRAPH_FORMAT* pf = rtf_get_paragraphformat();
pf->paragraphText = "";
rtf_write_paragraphformat();
// Writes RTF picture data
char rtfText[1024];
sprintf( rtfText, "\n{\\pict\\wmetafile8\\picwgoal%d\\pichgoal%d\\picscalex%d\\picscaley%d\n", hmWidth, hmHeight, width, height );
if ( fwrite( rtfText, 1, strlen(rtfText), rtfFile ) < strlen(rtfText) )
error = RTF_IMAGE_ERROR;
fwrite( str, 1, 2*size, rtfFile );
strcpy( rtfText, "}" );
fwrite( rtfText, 1, strlen(rtfText), rtfFile );
} else
{
error = RTF_IMAGE_ERROR;
}
// Return error flag
return error;
}
|
|
|
|
|
When creating tables, sometimes initial cell text contents is "\trleft0" and then the rest of cell text. What am I doing wrong?
Mei You Qian
|
|
|
|
|
FYI, problem appears only when including the rtflib.cpp file.
If I link with rtflib.lib, the problem does not appear.
May be the rtflib.cpp file is outdated?
Mei You Qian
|
|
|
|
|
In rtflib.cpp, line 1053, replace
char tblrw[] = "";
with
char tblrw[5];
|
|
|
|
|